Fix some machines locking sometimes

This commit is contained in:
mrkubax10 2023-11-15 21:44:02 +01:00
parent 659e8ad5ed
commit 8724b563c4
7 changed files with 46 additions and 56 deletions

View File

@ -445,14 +445,6 @@ industrialtest.api.getNetwork=function(meta)
return minetest.deserialize(meta:get_string("industrialtest.network")) return minetest.deserialize(meta:get_string("industrialtest.network"))
end 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 -- \brief Returns opposite side of provided one
-- \param side Side number. See industrialtest.api.addPowerStorage for order -- \param side Side number. See industrialtest.api.addPowerStorage for order
-- \returns Opposite side -- \returns Opposite side

View File

@ -72,6 +72,7 @@ machine.onConstruct=function(pos,config)
local connectionMeta=minetest.get_meta(conn) local connectionMeta=minetest.get_meta(conn)
if industrialtest.api.isNetworkMaster(connectionMeta) then if industrialtest.api.isNetworkMaster(connectionMeta) then
industrialtest.api.createNetworkMapForNode(conn) industrialtest.api.createNetworkMapForNode(conn)
minetest.get_node_timer(conn):start(industrialtest.updateDelay)
else else
local def=minetest.registered_nodes[minetest.get_node(conn).name] local def=minetest.registered_nodes[minetest.get_node(conn).name]
if def.groups._industrialtest_cable then if def.groups._industrialtest_cable then
@ -100,12 +101,27 @@ end
machine.onDestruct=function(pos) machine.onDestruct=function(pos)
local meta=minetest.get_meta(pos) local meta=minetest.get_meta(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) local networks=industrialtest.api.isAttachedToNetwork(meta)
if networks then if networks then
for _,network in ipairs(networks) do for _,network in ipairs(networks) do
industrialtest.api.removeNodeFromNetwork(network,pos) industrialtest.api.removeNodeFromNetwork(network,pos)
end end
end end
end
end end
machine.onTimer=function(pos,elapsed,config) machine.onTimer=function(pos,elapsed,config)
@ -119,7 +135,7 @@ machine.onTimer=function(pos,elapsed,config)
end end
if shouldUpdateFormspec then if shouldUpdateFormspec then
meta:set_string("formspec",machine.getFormspec(pos,config)) machine.updateFormspec(pos,config)
end end
return shouldRerunTimer return shouldRerunTimer
@ -179,7 +195,8 @@ function industrialtest.internal.registerMachine(config)
end, end,
on_destruct=machine.onDestruct, on_destruct=machine.onDestruct,
on_timer=function(pos,elapsed) on_timer=function(pos,elapsed)
return machine.onTimer(pos,elapsed,config) local shouldRerunTimer,_=machine.onTimer(pos,elapsed,config)
return shouldRerunTimer
end, end,
allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count)
return machine.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count,config) return machine.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count,config)
@ -256,12 +273,12 @@ function industrialtest.internal.registerMachine(config)
local shouldRerunTimer=false local shouldRerunTimer=false
local shouldUpdateFormspec=false local shouldUpdateFormspec=false
if config.onTimer then if config.activeOnTimer then
shouldRerunTimer,shouldUpdateFormspec=config.activeOnTimer(pos,elapsed,meta,inv) shouldRerunTimer,shouldUpdateFormspec=config.activeOnTimer(pos,elapsed,meta,inv)
end end
if shouldUpdateFormspec then if shouldUpdateFormspec then
meta:set_string("formspec",machine.getFormspec(pos,config)) machine.updateFormspec(pos,config)
end end
return shouldRerunTimer return shouldRerunTimer
@ -441,9 +458,6 @@ simpleElectricItemProcessor.onTimer=function(pos,elapsed,meta,inv,config)
minetest.get_node_timer(pos):start(industrialtest.updateDelay) minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end end
end end
if not industrialtest.api.isFullyCharged(meta) then
industrialtest.api.triggerNeighbours(pos)
end
return shouldRerunTimer,shouldUpdateFormspec return shouldRerunTimer,shouldUpdateFormspec
end end
@ -563,9 +577,6 @@ simpleElectricItemProcessor.activeOnTimer=function(pos,elapsed,meta,inv,config)
end end
inv:set_stack("src",1,output.src) inv:set_stack("src",1,output.src)
end end
if not industrialtest.api.isFullyCharged(meta) then
industrialtest.api.triggerNeighbours(pos)
end
return shouldRerunTimer,shouldUpdateFormspec return shouldRerunTimer,shouldUpdateFormspec
end end
@ -610,7 +621,7 @@ function industrialtest.internal.registerSimpleElectricItemProcessor(config)
}, },
onConstruct=simpleElectricItemProcessor.onConstruct, onConstruct=simpleElectricItemProcessor.onConstruct,
onTimer=function(pos,elapsed,meta,inv) onTimer=function(pos,elapsed,meta,inv)
simpleElectricItemProcessor.onTimer(pos,elapsed,meta,inv,config) return simpleElectricItemProcessor.onTimer(pos,elapsed,meta,inv,config)
end, end,
allowMetadataInventoryMove=simpleElectricItemProcessor.allowMetadataInventoryMove, allowMetadataInventoryMove=simpleElectricItemProcessor.allowMetadataInventoryMove,
allowMetadataInventoryPut=simpleElectricItemProcessor.allowMetadataInventoryPut, allowMetadataInventoryPut=simpleElectricItemProcessor.allowMetadataInventoryPut,
@ -618,7 +629,7 @@ function industrialtest.internal.registerSimpleElectricItemProcessor(config)
onMetadataInventoryMove=simpleElectricItemProcessor.onMetadataInventoryMove, onMetadataInventoryMove=simpleElectricItemProcessor.onMetadataInventoryMove,
onMetadataInventoryTake=simpleElectricItemProcessor.onMetadataInventoryTake, onMetadataInventoryTake=simpleElectricItemProcessor.onMetadataInventoryTake,
activeOnTimer=function(pos,elapsed,meta,inv) activeOnTimer=function(pos,elapsed,meta,inv)
simpleElectricItemProcessor.activeOnTimer(pos,elapsed,meta,inv,config) return simpleElectricItemProcessor.activeOnTimer(pos,elapsed,meta,inv,config)
end end
}) })
end end

View File

@ -17,13 +17,13 @@
local S=minetest.get_translator("industrialtest") local S=minetest.get_translator("industrialtest")
local fluidGenerator={} local fluidGenerator={}
fluidGenerator.getFormspec=function(pos,getFuel) fluidGenerator.getFormspec=function(pos,config)
local meta=minetest.get_meta(pos) local meta=minetest.get_meta(pos)
local fluidPercent=meta:get_float("fluidAmount")/100 local fluidPercent=meta:get_float("fluidAmount")/100
local powerPercent=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity") local powerPercent=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")
local fluid=meta:get_string("fluid") local fluid=meta:get_string("fluid")
local formspec local formspec
local fuel=getFuel(fluid) local fuel=config.getFuel(fluid)
local tile=(fuel and fuel.texture or "industrialtest_gui_fluid_bg.png") local tile=(fuel and fuel.texture or "industrialtest_gui_fluid_bg.png")
if industrialtest.mtgAvailable then if industrialtest.mtgAvailable then
formspec={ formspec={
@ -64,14 +64,14 @@ fluidGenerator.onConstruct=function(pos,meta,inv)
meta:set_string("fluid","") meta:set_string("fluid","")
end end
fluidGenerator.onTimer=function(pos,elapsed,meta,inv,getFuel,getFuelByItem) fluidGenerator.onTimer=function(pos,elapsed,meta,inv,config)
local fluidSlot=inv:get_stack("fluid",1) local fluidSlot=inv:get_stack("fluid",1)
local chargedSlot=inv:get_stack("charged",1) local chargedSlot=inv:get_stack("charged",1)
local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos) local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos)
local shouldUpdateFormspec=false local shouldUpdateFormspec=false
local shouldRerunTimer=(afterFlow and meta:get_int("industrialtest.powerAmount")>0) local shouldRerunTimer=(afterFlow and meta:get_int("industrialtest.powerAmount")>0)
if fluidSlot:get_count()>0 and meta:get_float("fluidAmount")<=9000 then 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 if fuel and (fuel.name==meta:get_string("fluid") or meta:get_string("fluid")=="") then
local leftover=false local leftover=false
local leftoverAddingSucceeded=false local leftoverAddingSucceeded=false
@ -96,7 +96,7 @@ fluidGenerator.onTimer=function(pos,elapsed,meta,inv,getFuel,getFuelByItem)
end end
if meta:get_float("fluidAmount")>=50 and not industrialtest.api.isFullyCharged(meta) then if meta:get_float("fluidAmount")>=50 and not industrialtest.api.isFullyCharged(meta) then
meta:set_float("fluidAmount",meta:get_int("fluidAmount")-50*elapsed) 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) industrialtest.api.addPower(meta,toAdd)
shouldUpdateFormspec=true shouldUpdateFormspec=true
if config.registerActiveVariant then if config.registerActiveVariant then
@ -126,7 +126,7 @@ fluidGenerator.metadataChange=function(pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay) minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end 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 fluidSlot=inv:get_stack("fluid",1)
local chargedSlot=inv:get_stack("charged",1) local chargedSlot=inv:get_stack("charged",1)
local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos) 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) local shouldRerunTimer=(afterFlow and meta:get_int("industrialtest.powerAmount")>0)
if fluidSlot:get_count()>0 and meta:get_float("fluidAmount")<=9000 then 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 if fuel and (fuel.name==meta:get_string("fluid") or meta:get_string("fluid")=="") then
local leftover=false local leftover=false
local leftoverAddingSucceeded=false local leftoverAddingSucceeded=false
@ -152,7 +152,6 @@ fluidGenerator.activeOnTimer=function(pos,elapsed,meta,inv,getFuelByItem)
inv:set_stack("fluid",1,fluidSlot) inv:set_stack("fluid",1,fluidSlot)
meta:set_string("fluid",fuel.name) meta:set_string("fluid",fuel.name)
meta:set_float("fluidAmount",meta:get_float("fluidAmount")+1000) meta:set_float("fluidAmount",meta:get_float("fluidAmount")+1000)
minetest.chat_send_all(meta:get_float("fluidAmount"))
shouldUpdateFormspec=true shouldUpdateFormspec=true
shouldRerunTimer=false shouldRerunTimer=false
end end
@ -189,7 +188,7 @@ local function registerFluidGenerator(config)
name=config.name, name=config.name,
displayName=config.displayName, displayName=config.displayName,
getFormspec=function(pos) getFormspec=function(pos)
return fluidGenerator.getFormspec(pos,config.getFuel) return fluidGenerator.getFormspec(pos,config)
end, end,
capacity=7000, capacity=7000,
flow=industrialtest.api.lvPowerFlow, flow=industrialtest.api.lvPowerFlow,
@ -216,7 +215,7 @@ local function registerFluidGenerator(config)
}, },
onConstruct=fluidGenerator.onConstruct, onConstruct=fluidGenerator.onConstruct,
onTimer=function(pos,elapsed,meta,inv) 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, end,
onMetadataInventoryPut=fluidGenerator.metadataChange, onMetadataInventoryPut=fluidGenerator.metadataChange,
onMetadataInventoryMove=fluidGenerator.metadataChange onMetadataInventoryMove=fluidGenerator.metadataChange
@ -233,7 +232,9 @@ local function registerFluidGenerator(config)
}, },
light_source=8 light_source=8
} }
definition.activeOnTimer=fluidGenerator.activeOnTimer definition.activeOnTimer=function(pos,elapsed,meta,inv)
return fluidGenerator.activeOnTimer(pos,elapsed,meta,inv,config)
end
end end
industrialtest.internal.registerMachine(definition) industrialtest.internal.registerMachine(definition)
end end

View File

@ -71,9 +71,6 @@ powerStorage.onTimer=function(pos,elapsed,meta,inv,config)
shouldRerunTimer=true shouldRerunTimer=true
shouldUpdateFormspec=true shouldUpdateFormspec=true
end end
if not industrialtest.api.isFullyCharged(meta) then
industrialtest.api.triggerNeighbours(pos)
end
return shouldRerunTimer,shouldUpdateFormspec return shouldRerunTimer,shouldUpdateFormspec
end end
@ -117,7 +114,7 @@ local function registerPowerStorageNode(config)
getFormspec=powerStorage.getFormspec, getFormspec=powerStorage.getFormspec,
onConstruct=powerStorage.onConstruct, onConstruct=powerStorage.onConstruct,
onTimer=function(pos,elapsed,meta,inv) onTimer=function(pos,elapsed,meta,inv)
powerStorage.onTimer(pos,elapsed,meta,inv,config) return powerStorage.onTimer(pos,elapsed,meta,inv,config)
end, end,
onMetadataInventoryPut=powerStorage.onMetadataInventoryPut, onMetadataInventoryPut=powerStorage.onMetadataInventoryPut,
onMetadataInventoryMove=powerStorage.onMetadataInventoryMove onMetadataInventoryMove=powerStorage.onMetadataInventoryMove

View File

@ -91,7 +91,7 @@ local function registerSolarPanelGenerator(config)
}, },
onConstruct=solarPanel.onConstruct, onConstruct=solarPanel.onConstruct,
onTimer=function(pos,elapsed,meta,inv) onTimer=function(pos,elapsed,meta,inv)
solarPanel.onTimer(pos,elapsed,meta,inv,config) return solarPanel.onTimer(pos,elapsed,meta,inv,config)
end end
}) })
end end

View File

@ -359,11 +359,3 @@ if industrialtest.mtgAvailable then
end end
}) })
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)

View File

@ -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,5.4;2,0.5;powerAmount;"..S("Power Amount")..";"..powerAmount.."]",
"field[0.5,6.2;2,0.5;powerIOConfig;"..S("Power IO Config")..";"..powerIOConfig.."]", "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[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:").."]" "label[4.2,2.25;"..S("Connections:").."]"
} }
local connections=industrialtest.api.getConnections(pos) local connections=industrialtest.api.getConnections(pos)
local sides={"X-","X+","Y-","Y+","Z-","Z+"} local sides={"X-","X+","Y-","Y+","Z-","Z+"}
local sideString="" local sideString=""
for _,value in ipairs(connections) do for _,value in ipairs(connections) do
sideString=sideString..sides[value].." " sideString=sideString.."("..value.x..", "..value.y..", "..value.z..")\n"
end end
table.insert(formspec,"label[4.2,2.65;"..sideString.."]") table.insert(formspec,"label[4.2,2.65;"..sideString.."]")
powerStorageInspectorContext[playerName]=pos powerStorageInspectorContext[playerName]=pos
@ -78,8 +77,6 @@ minetest.register_on_player_receive_fields(function(player,formname,fields)
def._industrialtest_updateFormspec(context) def._industrialtest_updateFormspec(context)
end end
minetest.close_formspec(player:get_player_name(),formname) minetest.close_formspec(player:get_player_name(),formname)
elseif fields.triggerNeighbours then
industrialtest.api.triggerNeighbours(context)
end end
powerStorageInspectorContext[player:get_player_name()]=nil powerStorageInspectorContext[player:get_player_name()]=nil
return true return true