From 8724b563c45079de1192b9679baf5573bc5caa58 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Wed, 15 Nov 2023 21:44:02 +0100 Subject: [PATCH] Fix some machines locking sometimes --- api.lua | 8 ----- machines/common.lua | 47 ++++++++++++++++++------------ machines/fluid_generator.lua | 27 ++++++++--------- machines/power_storage.lua | 5 +--- machines/solar_panel_generator.lua | 2 +- nodes.lua | 8 ----- utils.lua | 5 +--- 7 files changed, 46 insertions(+), 56 deletions(-) diff --git a/api.lua b/api.lua index 686655e..4f6183d 100644 --- a/api.lua +++ b/api.lua @@ -445,14 +445,6 @@ industrialtest.api.getNetwork=function(meta) return minetest.deserialize(meta:get_string("industrialtest.network")) end --- \brief Starts node timer of neighbouring nodes with center node --- \param pos Vector with position of center node --- \returns nil -industrialtest.api.triggerNeighbours=function(pos) - for _,value in ipairs(industrialtest.api.getConnections(pos)) do - minetest.get_node_timer(value):start(industrialtest.updateDelay) - end -end -- \brief Returns opposite side of provided one -- \param side Side number. See industrialtest.api.addPowerStorage for order -- \returns Opposite side diff --git a/machines/common.lua b/machines/common.lua index a8f26d5..a282629 100644 --- a/machines/common.lua +++ b/machines/common.lua @@ -72,6 +72,7 @@ machine.onConstruct=function(pos,config) local connectionMeta=minetest.get_meta(conn) if industrialtest.api.isNetworkMaster(connectionMeta) then industrialtest.api.createNetworkMapForNode(conn) + minetest.get_node_timer(conn):start(industrialtest.updateDelay) else local def=minetest.registered_nodes[minetest.get_node(conn).name] if def.groups._industrialtest_cable then @@ -100,10 +101,25 @@ end machine.onDestruct=function(pos) local meta=minetest.get_meta(pos) - local networks=industrialtest.api.isAttachedToNetwork(meta) - if networks then - for _,network in ipairs(networks) do - industrialtest.api.removeNodeFromNetwork(network,pos) + if industrialtest.api.isNetworkMaster(meta) then + local network=industrialtest.api.createNetworkMap(pos,true) + for _,endpoint in ipairs(network) do + local endpointMeta=minetest.get_meta(endpoint.position) + local networks=industrialtest.api.isAttachedToNetwork(endpointMeta) + for key,value in ipairs(networks) do + if value.x==pos.x and value.y==pos.y and value.z==pos.z then + table.remove(networks,key) + break + end + end + endpointMeta:set_string("industrialtest.networks",minetest.serialize(networks)) + end + else + local networks=industrialtest.api.isAttachedToNetwork(meta) + if networks then + for _,network in ipairs(networks) do + industrialtest.api.removeNodeFromNetwork(network,pos) + end end end end @@ -119,7 +135,7 @@ machine.onTimer=function(pos,elapsed,config) end if shouldUpdateFormspec then - meta:set_string("formspec",machine.getFormspec(pos,config)) + machine.updateFormspec(pos,config) end return shouldRerunTimer @@ -179,7 +195,8 @@ function industrialtest.internal.registerMachine(config) end, on_destruct=machine.onDestruct, on_timer=function(pos,elapsed) - return machine.onTimer(pos,elapsed,config) + local shouldRerunTimer,_=machine.onTimer(pos,elapsed,config) + return shouldRerunTimer end, allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) return machine.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count,config) @@ -256,12 +273,12 @@ function industrialtest.internal.registerMachine(config) local shouldRerunTimer=false local shouldUpdateFormspec=false - if config.onTimer then + if config.activeOnTimer then shouldRerunTimer,shouldUpdateFormspec=config.activeOnTimer(pos,elapsed,meta,inv) end if shouldUpdateFormspec then - meta:set_string("formspec",machine.getFormspec(pos,config)) + machine.updateFormspec(pos,config) end return shouldRerunTimer @@ -441,9 +458,6 @@ simpleElectricItemProcessor.onTimer=function(pos,elapsed,meta,inv,config) minetest.get_node_timer(pos):start(industrialtest.updateDelay) end end - if not industrialtest.api.isFullyCharged(meta) then - industrialtest.api.triggerNeighbours(pos) - end return shouldRerunTimer,shouldUpdateFormspec end @@ -549,8 +563,8 @@ simpleElectricItemProcessor.activeOnTimer=function(pos,elapsed,meta,inv,config) end if meta:get_float("maxSrcTime")<=0 or meta:get_int("industrialtest.powerAmount")0) if fluidSlot:get_count()>0 and meta:get_float("fluidAmount")<=9000 then - local fuel=getFuelByItem(fluidSlot:get_name()) + local fuel=config.getFuelByItem(fluidSlot:get_name()) if fuel and (fuel.name==meta:get_string("fluid") or meta:get_string("fluid")=="") then local leftover=false local leftoverAddingSucceeded=false @@ -96,7 +96,7 @@ fluidGenerator.onTimer=function(pos,elapsed,meta,inv,getFuel,getFuelByItem) end if meta:get_float("fluidAmount")>=50 and not industrialtest.api.isFullyCharged(meta) then meta:set_float("fluidAmount",meta:get_int("fluidAmount")-50*elapsed) - local toAdd=math.ceil(getFuel(meta:get_string("fluid")).calorificValue*elapsed) + local toAdd=math.ceil(config.getFuel(meta:get_string("fluid")).calorificValue*elapsed) industrialtest.api.addPower(meta,toAdd) shouldUpdateFormspec=true if config.registerActiveVariant then @@ -126,7 +126,7 @@ fluidGenerator.metadataChange=function(pos) minetest.get_node_timer(pos):start(industrialtest.updateDelay) end -fluidGenerator.activeOnTimer=function(pos,elapsed,meta,inv,getFuelByItem) +fluidGenerator.activeOnTimer=function(pos,elapsed,meta,inv,config) local fluidSlot=inv:get_stack("fluid",1) local chargedSlot=inv:get_stack("charged",1) local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos) @@ -134,7 +134,7 @@ fluidGenerator.activeOnTimer=function(pos,elapsed,meta,inv,getFuelByItem) local shouldRerunTimer=(afterFlow and meta:get_int("industrialtest.powerAmount")>0) if fluidSlot:get_count()>0 and meta:get_float("fluidAmount")<=9000 then - local fuel=getFuelByItem(fluidSlot:get_name()) + local fuel=config.getFuelByItem(fluidSlot:get_name()) if fuel and (fuel.name==meta:get_string("fluid") or meta:get_string("fluid")=="") then local leftover=false local leftoverAddingSucceeded=false @@ -152,7 +152,6 @@ fluidGenerator.activeOnTimer=function(pos,elapsed,meta,inv,getFuelByItem) inv:set_stack("fluid",1,fluidSlot) meta:set_string("fluid",fuel.name) meta:set_float("fluidAmount",meta:get_float("fluidAmount")+1000) - minetest.chat_send_all(meta:get_float("fluidAmount")) shouldUpdateFormspec=true shouldRerunTimer=false end @@ -166,8 +165,8 @@ fluidGenerator.activeOnTimer=function(pos,elapsed,meta,inv,getFuelByItem) shouldRerunTimer=true else minetest.swap_node(pos,{ - name="industrialtest:"..config.name, - param2=minetest.get_node(pos).param2 + name="industrialtest:"..config.name, + param2=minetest.get_node(pos).param2 }) minetest.get_node_timer(pos):start(industrialtest.updateDelay) end @@ -189,7 +188,7 @@ local function registerFluidGenerator(config) name=config.name, displayName=config.displayName, getFormspec=function(pos) - return fluidGenerator.getFormspec(pos,config.getFuel) + return fluidGenerator.getFormspec(pos,config) end, capacity=7000, flow=industrialtest.api.lvPowerFlow, @@ -216,7 +215,7 @@ local function registerFluidGenerator(config) }, onConstruct=fluidGenerator.onConstruct, onTimer=function(pos,elapsed,meta,inv) - return fluidGenerator.onTimer(pos,elapsed,meta,inv,config.getFuel,config.getFuelByItem) + return fluidGenerator.onTimer(pos,elapsed,meta,inv,config) end, onMetadataInventoryPut=fluidGenerator.metadataChange, onMetadataInventoryMove=fluidGenerator.metadataChange @@ -233,7 +232,9 @@ local function registerFluidGenerator(config) }, light_source=8 } - definition.activeOnTimer=fluidGenerator.activeOnTimer + definition.activeOnTimer=function(pos,elapsed,meta,inv) + return fluidGenerator.activeOnTimer(pos,elapsed,meta,inv,config) + end end industrialtest.internal.registerMachine(definition) end diff --git a/machines/power_storage.lua b/machines/power_storage.lua index a872919..93492ec 100644 --- a/machines/power_storage.lua +++ b/machines/power_storage.lua @@ -71,9 +71,6 @@ powerStorage.onTimer=function(pos,elapsed,meta,inv,config) shouldRerunTimer=true shouldUpdateFormspec=true end - if not industrialtest.api.isFullyCharged(meta) then - industrialtest.api.triggerNeighbours(pos) - end return shouldRerunTimer,shouldUpdateFormspec end @@ -117,7 +114,7 @@ local function registerPowerStorageNode(config) getFormspec=powerStorage.getFormspec, onConstruct=powerStorage.onConstruct, onTimer=function(pos,elapsed,meta,inv) - powerStorage.onTimer(pos,elapsed,meta,inv,config) + return powerStorage.onTimer(pos,elapsed,meta,inv,config) end, onMetadataInventoryPut=powerStorage.onMetadataInventoryPut, onMetadataInventoryMove=powerStorage.onMetadataInventoryMove diff --git a/machines/solar_panel_generator.lua b/machines/solar_panel_generator.lua index 4fa1e2b..3a3f8a8 100644 --- a/machines/solar_panel_generator.lua +++ b/machines/solar_panel_generator.lua @@ -91,7 +91,7 @@ local function registerSolarPanelGenerator(config) }, onConstruct=solarPanel.onConstruct, onTimer=function(pos,elapsed,meta,inv) - solarPanel.onTimer(pos,elapsed,meta,inv,config) + return solarPanel.onTimer(pos,elapsed,meta,inv,config) end }) end diff --git a/nodes.lua b/nodes.lua index e11ea15..75120a6 100644 --- a/nodes.lua +++ b/nodes.lua @@ -359,11 +359,3 @@ if industrialtest.mtgAvailable then end }) end - --- Node callbacks -minetest.register_on_placenode(function(pos,newNode) - local def=minetest.registered_nodes[newNode.name] - if def and def.groups and def.groups._industrialtest_hasPowerInput then - industrialtest.api.triggerNeighbours(pos) - end -end) diff --git a/utils.lua b/utils.lua index d17a044..b3b0c62 100644 --- a/utils.lua +++ b/utils.lua @@ -36,14 +36,13 @@ local function inspectNode(pos,playerName) "field[0.5,5.4;2,0.5;powerAmount;"..S("Power Amount")..";"..powerAmount.."]", "field[0.5,6.2;2,0.5;powerIOConfig;"..S("Power IO Config")..";"..powerIOConfig.."]", "button[0.5,6.8;2,0.5;update;"..S("Update").."]", - "button[4.2,1.25;2.8,0.5;triggerNeighbours;"..S("Trigger Neighbours").."]", "label[4.2,2.25;"..S("Connections:").."]" } local connections=industrialtest.api.getConnections(pos) local sides={"X-","X+","Y-","Y+","Z-","Z+"} local sideString="" for _,value in ipairs(connections) do - sideString=sideString..sides[value].." " + sideString=sideString.."("..value.x..", "..value.y..", "..value.z..")\n" end table.insert(formspec,"label[4.2,2.65;"..sideString.."]") powerStorageInspectorContext[playerName]=pos @@ -78,8 +77,6 @@ minetest.register_on_player_receive_fields(function(player,formname,fields) def._industrialtest_updateFormspec(context) end minetest.close_formspec(player:get_player_name(),formname) - elseif fields.triggerNeighbours then - industrialtest.api.triggerNeighbours(context) end powerStorageInspectorContext[player:get_player_name()]=nil return true