From cb43a7ab7675632710577157be6eb2c81b408f47 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Tue, 25 Mar 2025 21:24:29 +0100 Subject: [PATCH] Refactor Rotary Macerator --- machines/rotary_macerator.lua | 301 ++++++++++++++++++---------------- 1 file changed, 160 insertions(+), 141 deletions(-) diff --git a/machines/rotary_macerator.lua b/machines/rotary_macerator.lua index bfa75b9..aa88b9c 100644 --- a/machines/rotary_macerator.lua +++ b/machines/rotary_macerator.lua @@ -15,12 +15,68 @@ -- along with this program. If not, see . local S=minetest.get_translator("industrialtest") +industrialtest.RotaryMacerator=table.copy(industrialtest.ActivatedElectricMachine) +industrialtest.internal.unpackTableInto(industrialtest.RotaryMacerator,{ + name="industrialtest:rotary_macerator", + description=S("Rotary Macerator"), + tiles={ + "industrialtest_advanced_machine_block.png", + "industrialtest_advanced_machine_block.png", + "industrialtest_advanced_machine_block.png", + "industrialtest_advanced_machine_block.png", + "industrialtest_advanced_machine_block.png", + "industrialtest_advanced_machine_block.png^industrialtest_macerator_front.png" + }, + sounds="metal", + requiresWrench=true, + facedir=true, + storageLists={ + "src", + "dst", + "upgrades", + "powerStorage" + }, + powerLists={ + { + list="powerStorage", + direction="i" + } + }, + active={ + tiles={ + "industrialtest_advanced_machine_block.png", + "industrialtest_advanced_machine_block.png", + "industrialtest_advanced_machine_block.png", + "industrialtest_advanced_machine_block.png", + "industrialtest_advanced_machine_block.png", + "industrialtest_advanced_machine_block.png^industrialtest_macerator_front_active.png" + } + }, + capacity=industrialtest.api.lvPowerFlow*2, + flow=industrialtest.api.lvPowerFlow, + hasPowerInput=true, + ioConfig="iiiiii", + _opPower=60, + _maintainSpeedOpPower=10 +}) -local rotaryMacerator={} -rotaryMacerator.opPower=60 -rotaryMacerator.maintainSpeedOpPower=10 +function industrialtest.RotaryMacerator.onConstruct(self,pos) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + inv:set_size("src",1) + inv:set_size("modifier",1) + inv:set_size("powerStorage",1) + inv:set_size("dst",1) + inv:set_size("upgrades",4) + meta:set_int("rpm",0) + meta:set_float("srcTime",0) + meta:set_float("maxSrcTime",0) + meta:set_int("maintainSpeed",0) + industrialtest.ActivatedElectricMachine.onConstruct(self,pos) +end -rotaryMacerator.getFormspec=function(pos) +function industrialtest.RotaryMacerator.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 maxSrcTime=meta:get_float("maxSrcTime") @@ -47,149 +103,138 @@ rotaryMacerator.getFormspec=function(pos) "listring[context;src]", "listring[context;dst]" } - return table.concat(formspec,"") + return parentFormspec..table.concat(formspec,"") end -rotaryMacerator.onConstruct=function(pos,meta,inv) - inv:set_size("src",1) - inv:set_size("modifier",1) - inv:set_size("powerStorage",1) - inv:set_size("dst",1) - inv:set_size("upgrades",4) - meta:set_int("rpm",0) - meta:set_float("srcTime",0) - meta:set_float("maxSrcTime",0) - meta:set_int("maintainSpeed",0) -end - -rotaryMacerator.onTimer=function(pos,elapsed,meta,inv) +function industrialtest.RotaryMacerator.update(self,pos,elapsed,meta,inv) local shouldRerunTimer=false local shouldUpdateFormspec=false - local srcSlot=inv:get_stack("src",1) - local modifierSlot=inv:get_stack("modifier",1) - local dstSlot=inv:get_stack("dst",1) local rpm=meta:get_int("rpm") local maintainSpeed=meta:get_int("maintainSpeed") - - shouldRerunTimer,shouldUpdateFormspec=industrialtest.internal.chargeFromPowerStorageItem(meta,inv) local powerAmount=meta:get_int("industrialtest.powerAmount") - if maintainSpeed==1 and powerAmount>=rotaryMacerator.maintainSpeedOpPower then + if maintainSpeed==1 and powerAmount>=self._maintainSpeedOpPower then local newRpm=math.max(rpm+10*elapsed,0) if newRpm>rpm then meta:set_int("rpm",newRpm) shouldUpdateFormspec=true end - industrialtest.api.addPower(meta,-rotaryMacerator.maintainSpeedOpPower) + industrialtest.api.addPower(meta,-self._maintainSpeedOpPower) shouldRerunTimer=true elseif rpm>0 then meta:set_int("rpm",math.max(rpm-1000*elapsed,0)) - shouldRerunTimer=shouldRerunTimer or rpm>0 + shouldRerunTimer=rpm>0 shouldUpdateFormspec=true end - - if powerAmount>=rotaryMacerator.opPower and not srcSlot:is_empty() then - local result=industrialtest.api.getMaceratorRecipeResult(srcSlot:get_name()) - if result then - meta:set_float("srcTime",0) - meta:set_float("maxSrcTime",result.time) - minetest.swap_node(pos,{ - name="industrialtest:rotary_macerator_active", - param2=minetest.get_node(pos).param2 - }) - minetest.get_node_timer(pos):start(industrialtest.updateDelay) - return false,shouldUpdateFormspec - end - end return shouldRerunTimer,shouldUpdateFormspec end -rotaryMacerator.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,count) +function industrialtest.RotaryMacerator.allowMetadataInventoryMove(self,pos,fromList,fromIndex,toList,count) if toList=="dst" then return 0 end - return count + return industrialtest.ActivatedElectricMachine.allowMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count) end -rotaryMacerator.allowMetadataInventoryPut=function(pos,listname,index,stack) +function industrialtest.RotaryMacerator.allowMetadataInventoryPut(self,pos,listname,index,stack) if listname=="dst" then return 0 end - return stack:get_count() + return industrialtest.ActivatedElectricMachine.allowMetadataInventoryPut(self,pos,listname,index,stack) end -rotaryMacerator.onMetadataInventoryMove=function(pos) - minetest.get_node_timer(pos):start(industrialtest.updateDelay) -end - -rotaryMacerator.onMetadataInventoryPut=function(pos) - minetest.get_node_timer(pos):start(industrialtest.updateDelay) -end - -rotaryMacerator.onMetadataInventoryTake=function(pos,listname) - if listname=="dst" then - minetest.get_node_timer(pos):start(industrialtest.updateDelay) +function industrialtest.RotaryMacerator.onMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count) + industrialtest.ActivatedElectricMachine.onMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count) + if toList=="src" or toList=="modifier" or fromList=="dst" then + self:triggerIfNeeded(pos) end end -rotaryMacerator.activeOnTimer=function(pos,elapsed,meta,inv) +function industrialtest.RotaryMacerator.onMetadataInventoryPut(self,pos,listname,index,stack,player) + industrialtest.ActivatedElectricMachine.onMetadataInventoryPut(self,pos,listname,index,stack,player) + if listname=="src" or listname=="modifier" then + self:triggerIfNeeded(pos) + end +end + +function industrialtest.RotaryMacerator.onMetadataInventoryTake(self,pos,listname,index,stack) + industrialtest.ActivatedElectricMachine.onMetadataInventoryTake(self,pos,listname,index,stack) + if listname=="dst" then + self:triggerIfNeeded(pos) + end +end + +function industrialtest.RotaryMacerator.onReceiveFields(self,pos,formname,fields) + if not fields.maintainSpeed then + return + end + local meta=minetest.get_meta(pos) + local maintainSpeed=meta:get_int("maintainSpeed") + if maintainSpeed==1 then + meta:set_int("maintainSpeed",0) + elseif meta:get_int("industrialtest.powerAmount")>=self._maintainSpeedOpPower then + meta:set_int("maintainSpeed",1) + self:trigger(pos) + end + self:updateFormspec(pos) +end + +function industrialtest.RotaryMacerator.shouldActivate(self,pos) + local meta=minetest.get_meta(pos) + local powerAmount=meta:get_int("industrialtest.powerAmount") + if powerAmount=meta:get_float("maxSrcTime") then + if srcTime>=maxSrcTime then + local result,modified=self.getRecipeResult(pos) + local resultStack=ItemStack(result.output) local multiplier=math.min(srcSlot:get_count(),speed) local prevCount=resultStack:get_count() resultStack:set_count(resultStack:get_count()*multiplier) local leftover=inv:add_item("dst",resultStack) meta:set_float("srcTime",0) - meta:set_float("maxSrcTime",0) srcSlot:take_item(multiplier-leftover:get_count()/prevCount) inv:set_stack("src",1,srcSlot) meta:set_int("rpm",math.min(rpm+750*elapsed,7500)) if modified then + local modifierSlot=inv:get_stack("modifier",1) local modifierMeta=modifierSlot:get_meta() local uses=result.uses if modifierMeta:contains("uses") then @@ -215,55 +260,29 @@ rotaryMacerator.activeOnTimer=function(pos,elapsed,meta,inv) industrialtest.api.addPower(meta,-requiredPower) - return true,true + return true +end + +industrialtest.RotaryMacerator:register() + +function industrialtest.RotaryMacerator.getRecipeResult(pos) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local srcSlot=inv:get_stack("src",1) + local modifierSlot=inv:get_stack("modifier",1) + + local result + local modified=false + if modifierSlot:is_empty() then + result=industrialtest.api.getMaceratorRecipeResult(srcSlot:get_name()) + else + result=industrialtest.api.getRotaryMaceratorModifier(srcSlot:get_name(),modifierSlot:get_name()) + modified=true + end + + return result,modified end -industrialtest.internal.registerMachine({ - name="rotary_macerator", - displayName=S("Rotary Macerator"), - capacity=industrialtest.api.lvPowerFlow*2, - getFormspec=rotaryMacerator.getFormspec, - flow=industrialtest.api.lvPowerFlow, - ioConfig="iiiiii", - requiresWrench=true, - registerActiveVariant=true, - sounds="metal", - powerSlots={"powerStorage"}, - storageSlots={"src","modifier","powerStorage","dst","upgrades"}, - groups={ - _industrialtest_hasPowerInput=1 - }, - customKeys={ - tiles={ - "industrialtest_advanced_machine_block.png", - "industrialtest_advanced_machine_block.png", - "industrialtest_advanced_machine_block.png", - "industrialtest_advanced_machine_block.png", - "industrialtest_advanced_machine_block.png", - "industrialtest_advanced_machine_block.png^industrialtest_macerator_front.png" - }, - paramtype2="facedir", - legacy_facedir_simple=true - }, - activeCustomKeys={ - tiles={ - "industrialtest_advanced_machine_block.png", - "industrialtest_advanced_machine_block.png", - "industrialtest_advanced_machine_block.png", - "industrialtest_advanced_machine_block.png", - "industrialtest_advanced_machine_block.png", - "industrialtest_advanced_machine_block.png^industrialtest_macerator_front_active.png" - } - }, - onConstruct=rotaryMacerator.onConstruct, - onTimer=rotaryMacerator.onTimer, - allowMetadataInventoryMove=rotaryMacerator.allowMetadataInventoryMove, - allowMetadataInventoryPut=rotaryMacerator.allowMetadataInventoryPut, - onMetadataInventoryPut=rotaryMacerator.onMetadataInventoryPut, - onMetadataInventoryMove=rotaryMacerator.onMetadataInventoryMove, - onMetadataInventoryTake=rotaryMacerator.onMetadataInventoryTake, - activeOnTimer=rotaryMacerator.activeOnTimer -}) minetest.register_craft({ type="shaped", output="industrialtest:rotary_macerator",