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"))
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

View File

@ -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")<requiredPower then
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
@ -563,9 +577,6 @@ simpleElectricItemProcessor.activeOnTimer=function(pos,elapsed,meta,inv,config)
end
inv:set_stack("src",1,output.src)
end
if not industrialtest.api.isFullyCharged(meta) then
industrialtest.api.triggerNeighbours(pos)
end
return shouldRerunTimer,shouldUpdateFormspec
end
@ -610,7 +621,7 @@ function industrialtest.internal.registerSimpleElectricItemProcessor(config)
},
onConstruct=simpleElectricItemProcessor.onConstruct,
onTimer=function(pos,elapsed,meta,inv)
simpleElectricItemProcessor.onTimer(pos,elapsed,meta,inv,config)
return simpleElectricItemProcessor.onTimer(pos,elapsed,meta,inv,config)
end,
allowMetadataInventoryMove=simpleElectricItemProcessor.allowMetadataInventoryMove,
allowMetadataInventoryPut=simpleElectricItemProcessor.allowMetadataInventoryPut,
@ -618,7 +629,7 @@ function industrialtest.internal.registerSimpleElectricItemProcessor(config)
onMetadataInventoryMove=simpleElectricItemProcessor.onMetadataInventoryMove,
onMetadataInventoryTake=simpleElectricItemProcessor.onMetadataInventoryTake,
activeOnTimer=function(pos,elapsed,meta,inv)
simpleElectricItemProcessor.activeOnTimer(pos,elapsed,meta,inv,config)
return simpleElectricItemProcessor.activeOnTimer(pos,elapsed,meta,inv,config)
end
})
end

View File

@ -17,13 +17,13 @@
local S=minetest.get_translator("industrialtest")
local fluidGenerator={}
fluidGenerator.getFormspec=function(pos,getFuel)
fluidGenerator.getFormspec=function(pos,config)
local meta=minetest.get_meta(pos)
local fluidPercent=meta:get_float("fluidAmount")/100
local powerPercent=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")
local fluid=meta:get_string("fluid")
local formspec
local fuel=getFuel(fluid)
local fuel=config.getFuel(fluid)
local tile=(fuel and fuel.texture or "industrialtest_gui_fluid_bg.png")
if industrialtest.mtgAvailable then
formspec={
@ -64,14 +64,14 @@ fluidGenerator.onConstruct=function(pos,meta,inv)
meta:set_string("fluid","")
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 chargedSlot=inv:get_stack("charged",1)
local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos)
local shouldUpdateFormspec=false
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
@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

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,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