From 432fd72597ed4b94bf82571060bf81572240ba45 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Tue, 18 Apr 2023 21:48:08 +0200 Subject: [PATCH] Implement power storage nodes --- api.lua | 50 +++++++++++------ machines.lua | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+), 16 deletions(-) diff --git a/api.lua b/api.lua index ec7299f..baf52f9 100644 --- a/api.lua +++ b/api.lua @@ -201,6 +201,21 @@ industrialtest.api.transferPowerToItem=function(srcMeta,itemstack,amount) srcMeta:set_int("industrialtest.powerAmount",srcMeta:get_int("industrialtest.powerAmount")-actualFlow) return actualFlow end +-- \brief Adds power to destination metadata while subtracting it from source itemstack +-- \param srcItemstack ItemStack from which subtract power +-- \param meta MetaDataRef to which add power +-- \param amount How much power should be transferred +-- \returns How much of power was actually transferred +industrialtest.api.transferPowerFromItem=function(srcItemstack,meta,amount) + local srcMeta=srcItemstack:get_meta() + local currentFlow=math.min(srcMeta:get_int("industrialtest.powerAmount"),amount) + if currentFlow==0 then + return 0 + end + local actualFlow=industrialtest.api.addPower(meta,currentFlow) + industrialtest.api.addPowerToItem(srcItemstack,-actualFlow) + return actualFlow +end -- \brief Transfers power from source node to all neighbouring nodes -- \param pos Vector with position of source node -- \returns two values: true if any neighbouring node has room for more power, false otherwise @@ -241,24 +256,27 @@ industrialtest.api.powerFlow=function(pos) local roomAvailable=false local transferred=false for key,value in ipairs(neighbours) do - if industrialtest.api.isPowerOutput(meta,key) and value~=0 then - if industrialtest.api.transferPower(meta,value,powerDistribution)>0 then - transferred=true - end - local def=minetest.registered_nodes[minetest.get_node(neighbourPositions[key]).name] - if def then - local updateFormspec=def._industrialtest_updateFormspec - if updateFormspec then - updateFormspec(neighbourPositions[key]) + if value~=0 then + local normalizedKey=industrialtest.api.normalizeSide(pos,key) + if industrialtest.api.isPowerOutput(meta,normalizedKey) then + if industrialtest.api.transferPower(meta,value,powerDistribution)>0 then + transferred=true end - local onPowerFlow=def._industrialtest_onPowerFlow - if onPowerFlow and transferred then - onPowerFlow(neighbourPositions[key],industrialtest.api.getOppositeSide(key)) + local def=minetest.registered_nodes[minetest.get_node(neighbourPositions[key]).name] + if def then + local updateFormspec=def._industrialtest_updateFormspec + if updateFormspec then + updateFormspec(neighbourPositions[key]) + end + local onPowerFlow=def._industrialtest_onPowerFlow + if onPowerFlow and transferred then + onPowerFlow(neighbourPositions[key],industrialtest.api.getOppositeSide(key)) + end + end + minetest.get_node_timer(neighbourPositions[key]):start(industrialtest.updateDelay) + if not industrialtest.api.isFullyCharged(value) then + roomAvailable=true end - end - minetest.get_node_timer(neighbourPositions[key]):start(industrialtest.updateDelay) - if not industrialtest.api.isFullyCharged(value) then - roomAvailable=true end end end diff --git a/machines.lua b/machines.lua index 28d385f..6cc3605 100644 --- a/machines.lua +++ b/machines.lua @@ -128,6 +128,11 @@ local function registerMachine(config) config.onMetadataInventoryPut(pos,listname,index,stack,player) end end, + on_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) + if config.onMetadataInventoryPut then + config.onMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count) + end + end, on_metadata_inventory_take=function(pos,listname,index,stack,player) if config.onMetadataInventoryTake then config.onMetadataInventoryTake(pos,listname,index,stack,player) @@ -1807,3 +1812,152 @@ minetest.register_craft({ {"","industrialtest:insulated_iron_cable",""} } }) + +-- Power storage nodes +local function registerPowerStorageNode(config) + local function getFormspec(pos) + local meta=minetest.get_meta(pos) + local charged=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity") + local formspec + if industrialtest.mtgAvailable then + formspec={ + "list[context;charged;1,2.5;1,1]", + "listring[context;charged]", + "label[0.9,3.9;"..S("Charge").."]", + "list[context;discharged;3,2.5;1,1]", + "listring[context;discharged]", + "label[2.7,3.9;"..S("Discharge").."]", + "box[9,1;0.3,4.8;#202020]", + (charged>0 and "box[9,"..(1+4.8-(charged*4.8))..";0.3,"..(charged*4.8)..";#FF1010]" or "") + } + elseif industrialtest.mclAvailable then + formspec={ + "list[context;charged;1,2.5;1,1]", + "listring[context;charged]", + mcl_formspec.get_itemslot_bg(1,2.5,1,1), + "label[0.9,3.9;"..S("Charge").."]", + "list[context;discharged;3,2.5;1,1]", + "listring[context;discharged]", + mcl_formspec.get_itemslot_bg(3,2.5,1,1), + "label[2.7,3.9;"..S("Discharge").."]", + "box[9,1;0.3,4.8;#202020]", + (charged>0 and "box[9,"..(1+4.8-(charged*4.8))..";0.3,"..(charged*4.8)..";#FF1010]" or "") + } + end + return table.concat(formspec,"") + end + registerMachine({ + name=config.name, + displayName=config.displayName, + capacity=config.capacity, + flow=config.flow, + ioConfig="iiiioi", + sounds=config.sounds, + powerSlots={"charged","discharged"}, + storageSlots={"charged","discharged"}, + registerActiveVariant=false, + groups={ + _industrialtest_hasPowerOutput=1, + _industrialtest_hasPowerInput=1 + }, + customKeys={ + tiles={ + config.machineBlockTexture, + config.machineBlockTexture, + config.machineBlockTexture, + config.machineBlockTexture, + config.machineBlockTexture, + config.machineBlockTexture.."^industrialtest_"..config.name.."_front.png" + }, + paramtype2="facedir", + legacy_facedir_simple=true + }, + requiresWrench=config.requiresWrench, + getFormspec=getFormspec, + onConstruct=function(pos,meta,inv) + inv:set_size("charged",1) + inv:set_size("discharged",1) + end, + onTimer=function(pos,elapsed,meta,inv) + local chargedSlot=inv:get_stack("charged",1) + local dischargedSlot=inv:get_stack("discharged",1) + local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos) + local shouldUpdateFormspec=flowTransferred + local shouldRerunTimer=(afterFlow and meta:get_int("industrialtest.powerAmount")>0) + + if chargedSlot:get_count()>0 and meta:get_int("industrialtest.powerAmount")>0 and industrialtest.api.transferPowerToItem(meta,chargedSlot,config.flow)>0 then + inv:set_stack("charged",1,chargedSlot) + shouldRerunTimer=true + shouldUpdateFormspec=true + end + if dischargedSlot:get_count()>0 and not industrialtest.api.isFullyCharged(meta) and industrialtest.api.transferPowerFromItem(dischargedSlot,meta,config.flow)>0 then + inv:set_stack("discharged",1,dischargedSlot) + shouldRerunTimer=true + shouldUpdateFormspec=true + end + if not industrialtest.api.isFullyCharged(meta) then + industrialtest.api.triggerNeighbours(pos) + end + + return shouldRerunTimer,shouldUpdateFormspec + end, + onMetadataInventoryPut=function(pos) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + end, + onMetadataInventoryMove=function(pos) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + end + }) +end + +registerPowerStorageNode({ + name="batbox", + displayName="BatBox", + capacity=25000, + flow=industrialtest.api.lvPowerFlow, + sounds="wood", + machineBlockTexture="industrialtest_wood_machine_block.png", + requiresWrench=false +}) +minetest.register_craft({ + type="shaped", + output="industrialtest:batbox", + recipe={ + {"group:wood","industrialtest:insulated_tin_cable","group:wood"}, + {"industrialtest:re_battery","industrialtest:re_battery","industrialtest:re_battery"}, + {"group:wood","group:wood","group:wood"} + } +}) + +registerPowerStorageNode({ + name="cesu", + displayName="CESU", + capacity=400000, + flow=industrialtest.api.mvPowerFlow, + sounds="metal", + machineBlockTexture="industrialtest_bronze_machine_block.png", + requiresWrench=false +}) +-- TODO: CESU craft + +registerPowerStorageNode({ + name="mfe", + displayName="MFE", + capacity=3000000, + flow=industrialtest.api.hvPowerFlow, + sounds="metal", + machineBlockTexture="industrialtest_machine_block.png", + requiresWrench=true +}) +-- TODO: MFE craft + +registerPowerStorageNode({ + name="mfsu", + displayName="MFSU", + capacity=30000000, + flow=industrialtest.api.evPowerFlow, + sounds="metal", + machineBlockTexture="industrialtest_advanced_machine_block.png", + requiresWrench=false +}) +-- TODO: MFSU craft