Implement Overclocker Upgrade

This commit is contained in:
2023-11-25 17:58:42 +01:00
parent d9ea7782c2
commit be8809e008
5 changed files with 150 additions and 17 deletions

View File

@@ -17,7 +17,7 @@
local machine={}
local simpleElectricItemProcessor={}
function industrialtest.internal.mclAfterDigNode(pos,oldmeta,lists)
industrialtest.internal.mclAfterDigNode=function(pos,oldmeta,lists)
-- Taken from https://git.minetest.land/MineClone2/MineClone2/src/branch/master/mods/ITEMS/mcl_furnaces/init.lua#L538
local meta=minetest.get_meta(pos)
local meta2=meta
@@ -33,6 +33,20 @@ function industrialtest.internal.mclAfterDigNode(pos,oldmeta,lists)
meta:from_table(meta2:to_table())
end
industrialtest.internal.allowMoveToUpgradeSlot=function(pos,toIndex,stack)
local def=minetest.registered_items[stack:get_name()]
if not def or not def.groups or not def.groups._industrialtest_machineUpgrade then
return 0
end
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local targetSlot=inv:get_stack("upgrades",toIndex)
if not targetSlot:is_empty() then
return 0
end
return stack:get_count()
end
machine.getFormspec=function(pos,config)
local formspec
if industrialtest.mtgAvailable then
@@ -157,6 +171,11 @@ machine.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toInde
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local movedItemStack=inv:get_stack(fromList,1)
if toList=="upgrades" then
return industrialtest.internal.allowMoveToUpgradeSlot(pos,toIndex,movedItemStack)
end
local found=false
if config.powerSlots then
for _,value in ipairs(config.powerSlots) do
@@ -169,13 +188,19 @@ machine.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toInde
if found and not industrialtest.api.hasPowerStorage(movedItemStack:get_meta()) then
return 0
end
if config.allowMetadataInventoryMove then
return config.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count)
end
return count
end
machine.allowMetadataInventoryPut=function(pos,listname,index,stack,player,config)
if listname=="upgrades" then
return industrialtest.internal.allowMoveToUpgradeSlot(pos,index,stack)
end
local found=false
if config.powerSlots then
for _,value in ipairs(config.powerSlots) do
@@ -188,12 +213,42 @@ machine.allowMetadataInventoryPut=function(pos,listname,index,stack,player,confi
if found and not industrialtest.api.hasPowerStorage(stack:get_meta()) then
return 0
end
if config.allowMetadataInventoryPut then
return config.allowMetadataInventoryPut(pos,listname,index,stack,player)
end
return stack:get_count()
end
machine.onMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toIndex,count)
if toList=="upgrades" then
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local stack=inv:get_stack(fromList,fromIndex)
industrialtest.internal.applyUpgrade(meta,stack)
elseif fromList=="upgrades" then
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local stack=inv:get_stack(fromList,fromIndex)
industrialtest.internal.removeUpgrade(meta,stack)
end
end
machine.onMetadataInventoryPut=function(pos,listname,index,stack)
if listname=="upgrades" then
local meta=minetest.get_meta(pos)
industrialtest.internal.applyUpgrade(meta,stack)
end
end
machine.onMetadataInventoryTake=function(pos,listname,index,stack)
if listname=="upgrades" then
local meta=minetest.get_meta(pos)
industrialtest.internal.removeUpgrade(meta,stack)
end
end
machine.updateFormspec=function(pos,config)
if config.withoutFormspec then
return
@@ -220,16 +275,19 @@ function industrialtest.internal.registerMachine(config)
return machine.allowMetadataInventoryPut(pos,listname,index,stack,player,config)
end,
on_metadata_inventory_put=function(pos,listname,index,stack,player)
machine.onMetadataInventoryPut(pos,listname,index,stack)
if config.onMetadataInventoryPut then
config.onMetadataInventoryPut(pos,listname,index,stack,player)
end
end,
on_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count)
machine.onMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex)
if config.onMetadataInventoryPut then
config.onMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count)
end
end,
on_metadata_inventory_take=function(pos,listname,index,stack,player)
machine.onMetadataInventoryTake(pos,listname,index,stack)
if config.onMetadataInventoryTake then
config.onMetadataInventoryTake(pos,listname,index,stack,player)
end
@@ -490,7 +548,7 @@ simpleElectricItemProcessor.onTimer=function(pos,elapsed,meta,inv,config)
local powerStorageSlot=inv:get_stack("powerStorage",1)
local shouldUpdateFormspec=false
local shouldRerunTimer=false
local requiredPower=elapsed*config.opPower
local requiredPower=elapsed*config.opPower*industrialtest.api.getMachineSpeed(meta)
if powerStorageSlot:get_count()>0 then
local stackMeta=powerStorageSlot:get_meta()
if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then
@@ -586,7 +644,7 @@ simpleElectricItemProcessor.activeOnTimer=function(pos,elapsed,meta,inv,config)
local powerStorageSlot=inv:get_stack("powerStorage",1)
local shouldUpdateFormspec=false
local shouldRerunTimer=false
local requiredPower=elapsed*config.opPower
local requiredPower=elapsed*config.opPower*industrialtest.api.getMachineSpeed(meta)
if powerStorageSlot:get_count()>0 then
local stackMeta=powerStorageSlot:get_meta()
@@ -626,12 +684,20 @@ simpleElectricItemProcessor.activeOnTimer=function(pos,elapsed,meta,inv,config)
end
if meta:get_float("srcTime")>=meta:get_float("maxSrcTime") then
local output=craftResultProxy(config.method,srcSlot)
local speed=industrialtest.api.getMachineSpeed(meta)
local usedItems=srcSlot:get_count()-output.src:get_count()
local multiplier=1
if srcSlot:get_count()>=speed*usedItems then
multiplier=speed
end
if output.item:get_count()>0 then
output.item:set_count(output.item:get_count()*multiplier)
inv:add_item("dst",output.item)
meta:set_float("srcTime",-1)
meta:set_float("maxSrcTime",0)
end
inv:set_stack("src",1,output.src)
srcSlot:set_count(srcSlot:get_count()-multiplier*usedItems)
inv:set_stack("src",1,srcSlot)
end
return shouldRerunTimer,shouldUpdateFormspec
end