Refactor Canning Machine

This commit is contained in:
mrkubax10 2025-01-08 22:47:11 +01:00
parent 6934a8b342
commit ca99ab38b5

View File

@ -15,15 +15,66 @@
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
local S=minetest.get_translator("industrialtest")
local canningMachine={}
industrialtest.CanningMachine=table.copy(industrialtest.ActivatedElectricMachine)
industrialtest.internal.unpackTableInto(industrialtest.CanningMachine,{
name="industrialtest:canning_machine",
description=S("Canning Machine"),
tiles={
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png^industrialtest_canning_machine_front.png",
"industrialtest_machine_block.png"
},
sounds="metal",
facedir=true,
storageLists={
"src",
"dst",
"leftover",
"upgrades",
"powerStorage"
},
powerLists={"powerStorage"},
active={
tiles={
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png^industrialtest_canning_machine_front_active.png",
"industrialtest_machine_block.png"
}
},
capacity=industrialtest.api.lvPowerFlow*2,
flow=industrialtest.api.lvPowerFlow,
ioConfig="iiiiii",
requiresWrench=true,
hasPowerInput=true,
_opPower=200,
_canningTime=5
})
canningMachine.opPower=200
canningMachine.canningTime=5
function industrialtest.CanningMachine.onConstruct(self,pos)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
inv:set_size("src",1)
inv:set_size("dst",1)
inv:set_size("leftover",1)
inv:set_size("powerStorage",1)
inv:set_size("upgrades",4)
meta:set_float("srcTime",0)
industrialtest.ActivatedElectricMachine.onConstruct(self,pos)
end
canningMachine.getFormspec=function(pos)
function industrialtest.CanningMachine.getFormspec(self,pos)
local parentFormspec=industrialtest.ActivatedElectricMachine.getFormspec(self,pos)
local meta=minetest.get_meta(pos)
local powerPercent=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100
local srcPercent=meta:get_float("srcTime")/canningMachine.canningTime*100
local srcPercent=meta:get_float("srcTime")/self._canningTime*100
local formspec={
"list[context;src;3.4,1.8;1,1]",
industrialtest.internal.getItemSlotBg(3.4,1.8,1,1),
@ -42,44 +93,10 @@ canningMachine.getFormspec=function(pos)
"listring[context;src]",
"listring[context;dst]"
}
return table.concat(formspec,"")
return parentFormspec..table.concat(formspec,"")
end
canningMachine.onConstruct=function(pos,meta,inv)
inv:set_size("src",1)
inv:set_size("dst",1)
inv:set_size("leftover",1)
inv:set_size("powerStorage",1)
inv:set_size("upgrades",4)
meta:set_float("srcTime",0)
end
canningMachine.onTimer=function(pos,elapsed,meta,inv)
local shouldRerunTimer=false
local shouldUpdateFormspec=false
local fuelSlot=inv:get_stack("src",1)
local targetSlot=inv:get_stack("dst",1)
local leftoverSlot=inv:get_stack("leftover",1)
local powerStorageSlot=inv:get_stack("powerStorage",1)
local targetMeta=targetSlot:get_meta()
shouldRerunTimer,shouldUpdateFormspec=industrialtest.internal.chargeFromPowerStorageItem(meta,inv)
local def=fuelSlot:get_definition()
if not fuelSlot:is_empty() and not targetSlot:is_empty() and meta:get_int("industrialtest.powerAmount")>=canningMachine.opPower and (not def._industrialtest_emptyVariant or leftoverSlot:item_fits(ItemStack(def._industrialtest_emptyVariant))) and
(not industrialtest.api.itemHasFluidStorage(fuelSlot) or fuelSlot:get_meta():get_int("industrialtest.fluidAmount")>0) and targetMeta:get_int("industrialtest.fluidAmount")<targetMeta:get_int("industrialtest.fluidCapacity") then
minetest.swap_node(pos,{
name="industrialtest:canning_machine_active",
param2=minetest.get_node(pos).param2
})
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
return false,shouldUpdateFormspec
end
return shouldRerunTimer,true
end
canningMachine.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,count)
function industrialtest.CanningMachine.allowMetadataInventoryMove(self,pos,fromList,fromIndex,toList,count)
if toList=="src" then
local inv=minetest.get_meta(pos):get_inventory()
local itemstack=inv:get_stack(fromList,fromIndex)
@ -92,10 +109,10 @@ canningMachine.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList
local def=itemstack:get_definition()
return (def.groups and def.groups._industrialtest_fueled) and count or 0
end
return count
return math.min(count,industrialtest.ActivatedElectricMachine.allowMetadataInventoryMove(self,pos,fromList,fromIndex,toList,count))
end
canningMachine.allowMetadataInventoryPut=function(pos,listname,index,stack)
function industrialtest.CanningMachine.allowMetadataInventoryPut(self,pos,listname,index,stack)
if listname=="src" then
local def=stack:get_definition()
return (def.groups and def.groups._industrialtest_fuel) and stack:get_count() or 0
@ -104,10 +121,10 @@ canningMachine.allowMetadataInventoryPut=function(pos,listname,index,stack)
local def=stack:get_definition()
return (def.groups and def.groups._industrialtest_fueled) and stack:get_count() or 0
end
return stack:get_count()
return math.min(stack:get_count(),industrialtest.ActivatedElectricMachine.allowMetadataInventoryPut(self,pos,listname,index,stack))
end
canningMachine.allowMetadataInventoryTake=function(pos,listname,index,stack)
function industrialtest.CanningMachine.allowMetadataInventoryTake(self,pos,listname,index,stack)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local fuelSlot=inv:get_stack("src",1)
@ -116,14 +133,11 @@ canningMachine.allowMetadataInventoryTake=function(pos,listname,index,stack)
meta:set_float("srcTime",0)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end
return stack:get_count()
return industrialtest.ActivatedElectricMachine.allowMetadataInventoryTake(self,pos,listname,index,stack)
end
canningMachine.onMetadataInventoryPut=function(pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end
canningMachine.onMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toIndex,count)
function industrialtest.CanningMachine.onMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count)
industrialtest.ActivatedElectricMachine.onMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local fuelSlot=inv:get_stack("src",1)
@ -134,41 +148,62 @@ canningMachine.onMetadataInventoryMove=function(pos,fromList,fromIndex,toList,to
end
end
canningMachine.activeOnTimer=function(pos,elapsed,meta,inv)
function industrialtest.CanningMachine.onMetadataInventoryPut(self,pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
industrialtest.ActivatedElectricMachine.onMetadataInventoryPut(self,pos,listname,index,stack)
end
function industrialtest.CanningMachine.shouldActivate(self,pos)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local fuelSlot=inv:get_stack("src",1)
local targetSlot=inv:get_stack("dst",1)
local leftoverSlot=inv:get_stack("leftover",1)
local powerStorageSlot=inv:get_stack("powerStorage",1)
local targetMeta=targetSlot:get_meta()
local def=fuelSlot:get_definition()
return not fuelSlot:is_empty() and not targetSlot:is_empty() and meta:get_int("industrialtest.powerAmount")>=self._opPower and (not def._industrialtest_emptyVariant or leftoverSlot:item_fits(ItemStack(def._industrialtest_emptyVariant))) and
(not industrialtest.api.itemHasFluidStorage(fuelSlot) or fuelSlot:get_meta():get_int("industrialtest.fluidAmount")>0) and targetMeta:get_int("industrialtest.fluidAmount")<targetMeta:get_int("industrialtest.fluidCapacity")
end
function industrialtest.CanningMachine.shouldDeactivate(self,pos)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local fuelSlot=inv:get_stack("src",1)
local fuelDef=fuelSlot:get_definition()
local targetSlot=inv:get_stack("dst",1)
local targetMeta=targetSlot:get_meta()
local leftoverSlot=inv:get_stack("leftover",1)
return fuelSlot:is_empty() or targetSlot:is_empty() or meta:get_int("industrialtest.powerAmount")<self._opPower or
(industrialtest.api.itemHasFluidStorage(fuelSlot) and industrialtest.api.isItemFluidStorageEmpty(fuelSlot)) or
industrialtest.api.isItemFluidStorageFull(targetSlot) or
targetMeta:get_int("industrialtest.fluidCapacity")-targetMeta:get_int("industrialtest.fluidAmount")<fuelDef._industrialtest_fuelAmount or
(fuelDef._industrialtest_emptyVariant and not leftoverSlot:item_fits(ItemStack(fuelDef._industrialtest_emptyVariant)))
end
function industrialtest.CanningMachine.afterDeactivation(self,pos)
minetest.debug("deactivated")
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local targetSlot=inv:get_stack("dst",1)
if meta:get_int("industrialtest.powerAmount")>=self._opPower or industrialtest.api.isItemFluidStorageFull(targetSlot) then
meta:set_float("srcTime",0)
end
self:updateFormspec(pos)
end
function industrialtest.CanningMachine.activeUpdate(self,pos,elapsed,meta,inv)
local shouldUpdateFormspec=false
local fuelSlot=inv:get_stack("src",1)
local targetSlot=inv:get_stack("dst",1)
local powerStorageSlot=inv:get_stack("powerStorage",1)
shouldRerunTimer,shouldUpdateFormspec=industrialtest.internal.chargeFromPowerStorageItem(meta,inv)
if fuelSlot:is_empty() or targetSlot:is_empty() or meta:get_int("industrialtest.powerAmount")<canningMachine.opPower then
if meta:get_int("industrialtest.powerAmount")>=canningMachine.opPower then
meta:set_float("srcTime",0)
end
minetest.swap_node(pos,{
name="industrialtest:canning_machine",
param2=minetest.get_node(pos).param2
})
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
return false,true
end
local fuelMeta=fuelSlot:get_meta()
local targetMeta=targetSlot:get_meta()
if (industrialtest.api.itemHasFluidStorage(fuelSlot) and fuelMeta:get_int("industrialtest.fluidAmount")==0) or targetMeta:get_int("industrialtest.fluidAmount")==targetMeta:get_int("industrialtest.fluidCapacity") then
meta:set_float("srcTime",0)
minetest.swap_node(pos,{
name="industrialtest:canning_machine",
param2=minetest.get_node(pos).param2
})
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
return false,true
end
local srcTime=meta:get_float("srcTime")
srcTime=srcTime+elapsed*industrialtest.api.getMachineSpeed(meta)
if srcTime>=canningMachine.canningTime then
local srcTime=meta:get_float("srcTime")+elapsed*industrialtest.api.getMachineSpeed(meta)
if srcTime>=self._canningTime then
if industrialtest.api.itemHasFluidStorage(fuelSlot) then
industrialtest.api.transferFluidToItem(fuelSlot,targetSlot,fuelMeta:get_int("industrialtest.fluidAmount"))
inv:set_stack("src",1,fuelSlot)
@ -177,11 +212,6 @@ canningMachine.activeOnTimer=function(pos,elapsed,meta,inv)
local def=fuelSlot:get_definition()
local leftoverSlot=inv:get_stack("leftover",1)
if targetMeta:get_int("industrialtest.fluidCapacity")-targetMeta:get_int("industrialtest.fluidAmount")<def._industrialtest_fuelAmount or (def._industrialtest_emptyVariant and not leftoverSlot:item_fits(ItemStack(def._industrialtest_emptyVariant))) then
minetest.swap_node(pos,{
name="industrialtest:canning_machine",
param2=minetest.get_node(pos).param2
})
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
return false,shouldUpdateFormspec
end
industrialtest.api.addFluidToItem(targetSlot,def._industrialtest_fuelAmount)
@ -195,57 +225,13 @@ canningMachine.activeOnTimer=function(pos,elapsed,meta,inv)
else
meta:set_float("srcTime",srcTime)
end
industrialtest.api.addPower(meta,-canningMachine.opPower)
industrialtest.api.addPower(meta,-self._opPower)
return true,true
end
industrialtest.internal.registerMachine({
name="canning_machine",
displayName=S("Canning Machine"),
capacity=industrialtest.api.lvPowerFlow*2,
getFormspec=canningMachine.getFormspec,
flow=industrialtest.api.lvPowerFlow,
ioConfig="iiiiii",
requiresWrench=true,
registerActiveVariant=true,
sounds="metal",
powerSlots={"powerStorage"},
storageSlots={"src","dst","powerStorage","upgrades"},
groups={
_industrialtest_hasPowerInput=1
},
customKeys={
tiles={
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png^industrialtest_canning_machine_front.png"
},
paramtype2="facedir",
legacy_facedir_simple=true
},
activeCustomKeys={
tiles={
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png^industrialtest_canning_machine_front_active.png"
}
},
onConstruct=canningMachine.onConstruct,
onTimer=canningMachine.onTimer,
allowMetadataInventoryMove=canningMachine.allowMetadataInventoryMove,
allowMetadataInventoryPut=canningMachine.allowMetadataInventoryPut,
allowMetadataInventoryTake=canningMachine.allowMetadataInventoryTake,
onMetadataInventoryPut=canningMachine.onMetadataInventoryPut,
onMetadataInventoryMove=canningMachine.onMetadataInventoryMove,
activeOnTimer=canningMachine.activeOnTimer
})
industrialtest.CanningMachine:register()
minetest.register_craft({
type="shaped",
output="industrialtest:canning_machine",