new flow logic: implement post-transition hook with mesecons support, add mesecons transition rules for flow sensor

This commit is contained in:
thetaepsilon-gamedev 2017-10-08 17:38:28 +01:00
parent ce0983d239
commit d5e3f1cf68
4 changed files with 37 additions and 2 deletions

View File

@ -526,7 +526,7 @@ new_flow_logic_register.simple(nodename_sensor_empty)
new_flow_logic_register.simple(nodename_sensor_loaded)
-- activate flow sensor at roughly half the pressure pumps drive pipes
local sensor_pressure_set = { { nodename_sensor_empty, 0.0 }, { nodename_sensor_loaded, 1.0 } }
new_flow_logic_register.transition_simple_set(sensor_pressure_set)
new_flow_logic_register.transition_simple_set(sensor_pressure_set, { mesecons=pipeworks.mesecons_rules })

View File

@ -111,6 +111,7 @@ flowlogic.run = function(pos, node)
local newnode = flowlogic.run_transition(node, currentpressure)
--pipeworks.logger("flowlogic.run()@"..formatvec(pos).." transition, new node name = "..dump(newnode).." pressure "..tostring(currentpressure))
minetest.swap_node(pos, newnode)
flowlogic.run_transition_post(pos, newnode)
end
-- set the new pressure
@ -289,3 +290,26 @@ flowlogic.run_transition = function(node, currentpressure)
return result
end
-- post-update hook for run_transition
-- among other things, updates mesecons if present.
-- node here means the new node, returned from run_transition() above
flowlogic.run_transition_post = function(pos, node)
local mesecons_def = minetest.registered_nodes[node.name].mesecons
local mesecons_rules = pipeworks.flowables.transitions.mesecons[node.name]
if minetest.global_exists("mesecon") and (mesecons_def ~= nil) and mesecons_rules then
if type(mesecons_def) ~= "table" then
pipeworks.logger("flowlogic.run_transition_post() BUG mesecons def for "..node.name.."not a table: got "..tostring(mesecons_def))
else
local receptor = mesecons_def.receptor
if receptor then
local state = receptor.state
if state == mesecon.state.on then
mesecon.receptor_on(pos, mesecons_rules)
elseif state == mesecon.state.off then
mesecon.receptor_off(pos, mesecons_rules)
end
end
end
end
end

View File

@ -32,6 +32,7 @@ pipeworks.flowables.outputs.list = {}
pipeworks.flowables.transitions = {}
pipeworks.flowables.transitions.list = {} -- master list
pipeworks.flowables.transitions.simple = {} -- nodes that change based purely on pressure
pipeworks.flowables.transitions.mesecons = {} -- table of mesecons rules to apply on transition

View File

@ -145,8 +145,9 @@ local simpleseterror = function(msg)
end
local simple_transitions = pipeworks.flowables.transitions.simple
register.transition_simple_set = function(nodeset)
register.transition_simple_set = function(nodeset, extras)
local set = {}
if extras == nil then extras = {} end
local length = #nodeset
if length < 2 then simpleseterror("nodeset needs at least two elements!") end
@ -175,4 +176,13 @@ register.transition_simple_set = function(nodeset)
--pipeworks.logger("register.transition_simple_set() after sort: nodename "..element.nodename.." value "..tostring(element.threshold))
simple_transitions[element.nodename] = set
end
-- handle extra options
-- if mesecons rules table was passed, set for each node
if extras.mesecons then
local mesecons_rules = pipeworks.flowables.transitions.mesecons
for _, element in ipairs(set) do
mesecons_rules[element.nodename] = extras.mesecons
end
end
end