From 18a1cbc9838e926eeeac4f79a38f761cc92309af Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Mon, 10 Mar 2025 11:15:31 +0100 Subject: [PATCH] Refactor base for simple electric item processors --- init.lua | 1 + machines/common.lua | 344 -------------------- machines/simple_electric_item_processor.lua | 220 +++++++++++++ 3 files changed, 221 insertions(+), 344 deletions(-) create mode 100644 machines/simple_electric_item_processor.lua diff --git a/init.lua b/init.lua index 814ba51..6afbad6 100644 --- a/init.lua +++ b/init.lua @@ -43,6 +43,7 @@ dofile(modpath.."/machines/machine.lua") dofile(modpath.."/machines/activated_machine.lua") dofile(modpath.."/machines/electric_machine.lua") dofile(modpath.."/machines/activated_electric_machine.lua") +dofile(modpath.."/machines/simple_electric_item_processor.lua") dofile(modpath.."/machines/canning_machine.lua") dofile(modpath.."/machines/chargepad.lua") dofile(modpath.."/machines/compressor.lua") diff --git a/machines/common.lua b/machines/common.lua index 479fb55..c9eaea2 100644 --- a/machines/common.lua +++ b/machines/common.lua @@ -15,7 +15,6 @@ -- along with this program. If not, see . local machine={} -local simpleElectricItemProcessor={} 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 @@ -159,346 +158,3 @@ function industrialtest.internal.registerMachine(config) end industrialtest.api.addTag("industrialtest:"..config.name,"usesTimer") end - -local function craftResultProxy(method,item) - if method=="cooking" then - local output,after=minetest.get_craft_result({ - method=method, - width=1, - items={item} - }) - return { - item=output.item, - time=output.time, - src=after.items[1] - } - elseif method=="industrialtest.macerating" then - local output=industrialtest.api.getMaceratorRecipeResult(item:get_name()) - if not output then - return { - item=ItemStack(), - time=0, - src=item - } - end - local srcAfter=ItemStack(item:get_name()) - srcAfter:set_count(item:get_count()-1) - return { - item=ItemStack(output.output), - time=output.time, - src=srcAfter - } - elseif method=="industrialtest.compressing" then - local output=industrialtest.api.getCompressorRecipeResult(item:get_name()) - if not output or item:get_count()0 and "image[3.4,2.8;1,1;industrialtest_gui_electricity_bg.png^[lowpart:"..powerPercent..":industrialtest_gui_electricity_fg.png]" - or "image[3.4,2.8;1,1;industrialtest_gui_electricity_bg.png]"), - "list[context;powerStorage;3.4,3.9;1,1]", - industrialtest.internal.getItemSlotBg(3.4,3.9,1,1), - (srcPercent>0 and "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[lowpart:"..srcPercent..":gui_furnace_arrow_fg.png^[transformR270]" - or "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[transformR270]"), - "list[context;dst;6.4,2.8;1,1]", - industrialtest.internal.getItemSlotBg(6.4,2.8,1,1), - "list[context;upgrades;9,0.9;1,4]", - industrialtest.internal.getItemSlotBg(9,0.9,1,4), - "listring[context;src]", - "listring[context;dst]" - } - return table.concat(formspec,"") -end - -simpleElectricItemProcessor.onPowerFlow=function(pos) - -- FIXME: this probably will require refactor so node timer won't be started - -- just to test if machine can process item - minetest.get_node_timer(pos):start(industrialtest.updateDelay) -end - -simpleElectricItemProcessor.onConstruct=function(pos,meta,inv) - inv:set_size("src",1) - inv:set_size("dst",1) - inv:set_size("powerStorage",1) - inv:set_size("upgrades",4) - meta:set_float("srcTime",-1) - meta:set_float("maxSrcTime",0) -end - -simpleElectricItemProcessor.onTimer=function(pos,elapsed,meta,inv,config) - local srcSlot=inv:get_stack("src",1) - local powerStorageSlot=inv:get_stack("powerStorage",1) - local shouldUpdateFormspec=false - local shouldRerunTimer=false - local requiredPower=elapsed*config.opPower*industrialtest.api.getMachineSpeed(meta) - - shouldRerunTimer,shouldUpdateFormspec=industrialtest.internal.chargeFromPowerStorageItem(meta,inv) - - if srcSlot:get_count()>0 and meta:get_int("industrialtest.powerAmount")>=requiredPower then - local output=craftResultProxy(config.method,srcSlot) - if output.time>0 and inv:room_for_item("dst",output.item) then - if meta:get_float("maxSrcTime")<=0 then - meta:set_float("srcTime",0) - meta:set_float("maxSrcTime",output.time*config.efficiency) - end - minetest.swap_node(pos,{ - name="industrialtest:"..config.name.."_active", - param2=minetest.get_node(pos).param2 - }) - minetest.get_node_timer(pos):start(industrialtest.updateDelay) - end - end - return shouldRerunTimer,shouldUpdateFormspec -end - -simpleElectricItemProcessor.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,count) - if toList=="dst" then - return 0 - elseif toList=="upgrades" then - -- TODO: Add support for upgrades when they will be added - return 0 - end - return count -end - -simpleElectricItemProcessor.allowMetadataInventoryPut=function(pos,listname,index,stack) - if listname=="dst" then - return 0 - elseif listname=="src" then - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - local srcSlot=inv:get_stack("src",1) - if srcSlot:get_name()~=stack:get_name() then - meta:set_float("srcTime",-1) - meta:set_float("maxSrcTime",0) - end - elseif listname=="upgrades" then - --TODO: See allow_metadata_inventory_move - return 0 - end - return stack:get_count() -end - -simpleElectricItemProcessor.onMetadataInventoryPut=function(pos) - minetest.get_node_timer(pos):start(industrialtest.updateDelay) -end - -simpleElectricItemProcessor.onMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toIndex,count) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - local srcSlot=inv:get_stack("src",1) - local dstSlot=inv:get_stack("dst",1) - if fromList=="src" and count==srcSlot:get_count() then - meta:set_float("srcTime",-1) - meta:set_float("maxSrcTime",0) - if meta:get_int("industrialtest.powerAmount")>0 then - meta:set_string("formspec",simpleElectricItemProcessor.getFormspec(pos)) - end - elseif fromList=="dst" and dstSlot:get_free_space()>0 then - minetest.get_node_timer(pos):start(industrialtest.updateDelay) - end -end - -simpleElectricItemProcessor.onMetadataInventoryTake=function(pos,listname,index,stack) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - local srcSlot=inv:get_stack("src",1) - local dstSlot=inv:get_stack("dst",1) - if listname=="src" and stack:get_count()==srcSlot:get_count() then - meta:set_float("srcTime",-1) - meta:set_float("maxSrcTime",0) - if meta:get_int("industrialtest.powerAmount")>0 then - meta:set_string("formspec",simpleElectricItemProcessor.getFormspec(pos)) - end - elseif listname=="dst" and dstSlot:get_free_space()>0 then - minetest.get_node_timer(pos):start(industrialtest.updateDelay) - end -end - -simpleElectricItemProcessor.activeOnTimer=function(pos,elapsed,meta,inv,config) - local srcSlot=inv:get_stack("src",1) - local powerStorageSlot=inv:get_stack("powerStorage",1) - local shouldUpdateFormspec=false - local shouldRerunTimer=false - local requiredPower=elapsed*config.opPower*industrialtest.api.getMachineSpeed(meta) - - shouldRerunTimer,shouldUpdateFormspec=industrialtest.internal.chargeFromPowerStorageItem(meta,inv) - - if srcSlot:get_count()>0 and meta:get_float("maxSrcTime")<=0 and meta:get_int("industrialtest.powerAmount")>=requiredPower then - local output=craftResultProxy(config.method,srcSlot) - if output.time>0 and inv:room_for_item("dst",output.item) then - meta:set_float("srcTime",0) - meta:set_float("maxSrcTime",output.time*config.efficiency) - end - end - if srcSlot:get_count()==0 and meta:get_float("maxSrcTime")>0 then - meta:set_float("srcTime",-1) - meta:set_float("maxSrcTime",0) - shouldUpdateFormspec=true - end - if meta:get_float("maxSrcTime")>0 then - if meta:get_int("industrialtest.powerAmount")>=requiredPower then - meta:set_int("industrialtest.powerAmount",meta:get_int("industrialtest.powerAmount")-requiredPower) - meta:set_float("srcTime",meta:get_float("srcTime")+elapsed) - shouldRerunTimer=true - end - shouldUpdateFormspec=true - end - if meta:get_float("maxSrcTime")<=0 or meta:get_int("industrialtest.powerAmount")=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 - srcSlot:set_count(srcSlot:get_count()-multiplier*usedItems) - inv:set_stack("src",1,srcSlot) - end - return shouldRerunTimer,shouldUpdateFormspec -end - -function industrialtest.internal.registerSimpleElectricItemProcessor(config) - local machineBlockTexture=config.machineBlockTexture or "industrialtest_machine_block.png" - industrialtest.internal.registerMachine({ - name=config.name, - displayName=config.displayName, - capacity=config.capacity, - getFormspec=simpleElectricItemProcessor.getFormspec, - flow=config.flow, - ioConfig="iiiiii", - requiresWrench=config.requiresWrench, - registerActiveVariant=true, - sounds="metal", - powerSlots={"powerStorage"}, - storageSlots={"src","dst","powerStorage","upgrades"}, - groups={ - _industrialtest_hasPowerInput=1 - }, - customKeys={ - tiles={ - machineBlockTexture..(config.customTopTexture and "^industrialtest_"..config.name.."_top.png" or ""), - machineBlockTexture..(config.customBottomTexture and "^industrialtest_"..config.name.."_bottom.png" or ""), - machineBlockTexture..(config.customRightTexture and "^industrialtest_"..config.name.."_right.png" or ""), - machineBlockTexture..(config.customLeftTexture and "^industrialtest_"..config.name.."_left.png" or ""), - machineBlockTexture..(config.customBackTexture and "^industrialtest_"..config.name.."_back.png" or ""), - machineBlockTexture..(config.customFrontTexture and "^industrialtest_"..config.name.."_front.png" or "") - }, - paramtype2="facedir", - legacy_facedir_simple=true, - _industrialtest_onPowerFlow=simpleElectricItemProcessor.onPowerFlow - }, - activeCustomKeys={ - tiles={ - machineBlockTexture..(config.customTopTexture and "^industrialtest_"..config.name.."_top_active.png" or ""), - machineBlockTexture..(config.customBottomTexture and "^industrialtest_"..config.name.."_bottom_active.png" or ""), - machineBlockTexture..(config.customRightTexture and "^industrialtest_"..config.name.."_right_active.png" or ""), - machineBlockTexture..(config.customLeftTexture and "^industrialtest_"..config.name.."_left_active.png" or ""), - machineBlockTexture..(config.customBackTexture and "^industrialtest_"..config.name.."_back_active.png" or ""), - machineBlockTexture..(config.customFrontTexture and "^industrialtest_"..config.name.."_front_active.png" or "") - } - }, - onConstruct=simpleElectricItemProcessor.onConstruct, - onTimer=function(pos,elapsed,meta,inv) - return simpleElectricItemProcessor.onTimer(pos,elapsed,meta,inv,config) - end, - allowMetadataInventoryMove=simpleElectricItemProcessor.allowMetadataInventoryMove, - allowMetadataInventoryPut=simpleElectricItemProcessor.allowMetadataInventoryPut, - onMetadataInventoryPut=simpleElectricItemProcessor.onMetadataInventoryPut, - onMetadataInventoryMove=simpleElectricItemProcessor.onMetadataInventoryMove, - onMetadataInventoryTake=simpleElectricItemProcessor.onMetadataInventoryTake, - activeOnTimer=function(pos,elapsed,meta,inv) - return simpleElectricItemProcessor.activeOnTimer(pos,elapsed,meta,inv,config) - end - }) - industrialtest.api.addTag("industrialtest:"..config.name,"simpleElectricItemProcessor") -end diff --git a/machines/simple_electric_item_processor.lua b/machines/simple_electric_item_processor.lua new file mode 100644 index 0000000..18ceb6e --- /dev/null +++ b/machines/simple_electric_item_processor.lua @@ -0,0 +1,220 @@ +-- IndustrialTest +-- Copyright (C) 2025 mrkubax10 + +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. + +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. + +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +industrialtest.SimpleElectricItemProcessor=table.copy(industrialtest.ActivatedElectricMachine) +industrialtest.internal.unpackTableInto(industrialtest.SimpleElectricItemProcessor,{ + facedir=true, + sounds="metal", + storageLists={ + "src", + "dst", + "powerStorage", + "upgrades" + }, + powerLists={ + { + list="powerStorage", + direction="i" + } + }, + hasPowerInput=true, + ioConfig="iiiiii" +}) + +function industrialtest.SimpleElectricItemProcessor.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("powerStorage",1) + inv:set_size("upgrades",4) + meta:set_float("srcTime",-1) + meta:set_float("maxSrcTime",0) + industrialtest.ActivatedElectricMachine.onConstruct(self,pos) +end + +function industrialtest.SimpleElectricItemProcessor.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")/meta:get_float("maxSrcTime")*100 + local formspec={ + "list[context;src;3.4,1.8;1,1]", + industrialtest.internal.getItemSlotBg(3.4,1.8,1,1), + (powerPercent>0 and "image[3.4,2.8;1,1;industrialtest_gui_electricity_bg.png^[lowpart:"..powerPercent..":industrialtest_gui_electricity_fg.png]" + or "image[3.4,2.8;1,1;industrialtest_gui_electricity_bg.png]"), + "list[context;powerStorage;3.4,3.9;1,1]", + industrialtest.internal.getItemSlotBg(3.4,3.9,1,1), + (srcPercent>0 and "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[lowpart:"..srcPercent..":gui_furnace_arrow_fg.png^[transformR270]" + or "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[transformR270]"), + "list[context;dst;6.4,2.8;1,1]", + industrialtest.internal.getItemSlotBg(6.4,2.8,1,1), + "list[context;upgrades;9,0.9;1,4]", + industrialtest.internal.getItemSlotBg(9,0.9,1,4), + "listring[context;src]", + "listring[context;dst]" + } + return parentFormspec..table.concat(formspec,"") +end + +function industrialtest.SimpleElectricItemProcessor.allowMetadataInventoryMove(self,pos,fromList,fromIndex,toList,count) + if toList=="dst" then + return 0 + end + return industrialtest.ActivatedElectricMachine.allowMetadataInventoryMove(self,pos,fromList,fromIndex,toList,count) +end + +function industrialtest.SimpleElectricItemProcessor.allowMetadataInventoryPut(self,pos,listname,index,stack) + if listname=="dst" then + return 0 + elseif listname=="src" then + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local srcSlot=inv:get_stack("src",1) + if srcSlot:get_name()~=stack:get_name() then + meta:set_float("srcTime",-1) + meta:set_float("maxSrcTime",0) + end + end + return industrialtest.ActivatedElectricMachine.allowMetadataInventoryPut(self,pos,listname,index,stack) +end + +function industrialtest.SimpleElectricItemProcessor.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 srcSlot=inv:get_stack("src",1) + local dstSlot=inv:get_stack("dst",1) + if fromList=="src" and count==srcSlot:get_count() then + meta:set_float("srcTime",-1) + meta:set_float("maxSrcTime",0) + if meta:get_int("industrialtest.powerAmount")>0 then + self:updateFormspec(pos) + end + elseif fromList=="dst" and dstSlot:get_free_space()>0 then + self:triggerIfNeeded(pos) + end +end + +function industrialtest.SimpleElectricItemProcessor.onMetadataInventoryPut(self,pos) + industrialtest.ActivatedElectricMachine.onMetadataInventoryPut(self,pos) + self:triggerIfNeeded(pos) +end + +function industrialtest.SimpleElectricItemProcessor.onMetadataInventoryTake(self,pos,listname,index,stack) + industrialtest.ActivatedElectricMachine.onMetadataInventoryTake(self,pos,listname,index,stack) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local srcSlot=inv:get_stack("src",1) + local dstSlot=inv:get_stack("dst",1) + if listname=="src" and stack:get_count()==srcSlot:get_count() then + meta:set_float("srcTime",-1) + meta:set_float("maxSrcTime",0) + if meta:get_int("industrialtest.powerAmount")>0 then + self:updateFormspec(pos) + end + elseif listname=="dst" and dstSlot:get_free_space()>0 then + self:triggerIfNeeded(pos) + end +end + +function industrialtest.SimpleElectricItemProcessor.register(self) + industrialtest.ActivatedElectricMachine.register(self) + industrialtest.api.addTag(self.name,"simpleElectricItemProcessor") +end + +function industrialtest.SimpleElectricItemProcessor.shouldActivate(self,pos) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + + -- TODO: Take elapsed time into account + local requiredPower=self.opPower*industrialtest.api.getMachineSpeed(meta) + if meta:get_int("industrialtest.powerAmount")0 then + local output=self:getCraftResult(srcSlot) + return output and output.time>0 and inv:room_for_item("dst",output.item) + end + return false +end + +function industrialtest.SimpleElectricItemProcessor.shouldDeactivate(self,pos) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + + -- TODO: Take elapsed time into account + local requiredPower=self.opPower*industrialtest.api.getMachineSpeed(meta) + if meta:get_int("industrialtest.powerAmount")=maxSrcTime then + local output=self:getCraftResult(srcSlot) + 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 + srcSlot:set_count(srcSlot:get_count()-multiplier*usedItems) + inv:set_stack("src",1,srcSlot) + end + + return true +end + +function industrialtest.SimpleElectricItemProcessor.getCraftResult(self,itemstack) + -- Dummy method +end