new flow logic: abms.lua: refactor flowlogic.run_output() into a processing stage of flowlogic.run()

This commit is contained in:
thetaepsilon-gamedev 2017-10-07 17:55:14 +01:00
parent 016f9de82f
commit 65b3448796
3 changed files with 24 additions and 14 deletions

View File

@ -66,6 +66,7 @@ register.input = register_abm_input
-- threshold determines the minimum pressure, over which outputfn is called. -- threshold determines the minimum pressure, over which outputfn is called.
-- outputfn is then given the current pressure, and returns the pressure relieved by the output process. -- outputfn is then given the current pressure, and returns the pressure relieved by the output process.
-- outputfn is expected to update environment, nearby world etc. as appropriate for the node. -- outputfn is expected to update environment, nearby world etc. as appropriate for the node.
--[[
local register_abm_output = function(nodename, threshold, outputfn) local register_abm_output = function(nodename, threshold, outputfn)
minetest.register_abm({ minetest.register_abm({
nodenames = { nodename }, nodenames = { nodename },
@ -77,6 +78,7 @@ local register_abm_output = function(nodename, threshold, outputfn)
}) })
end end
register.output = register_abm_output register.output = register_abm_output
]]
-- old spigot ABM code, not yet migrated -- old spigot ABM code, not yet migrated
--[[ --[[

View File

@ -68,6 +68,7 @@ end
flowlogic.run = function(pos, node) flowlogic.run = function(pos, node)
local nodename = node.name
-- get the current pressure value. -- get the current pressure value.
local nodepressure = get_pressure_access(pos) local nodepressure = get_pressure_access(pos)
local currentpressure = nodepressure.get() local currentpressure = nodepressure.get()
@ -75,6 +76,18 @@ flowlogic.run = function(pos, node)
-- balance pressure with neighbours -- balance pressure with neighbours
currentpressure = flowlogic.balance_pressure(pos, node, currentpressure) currentpressure = flowlogic.balance_pressure(pos, node, currentpressure)
-- if node is an output: run output phase
local output = pipeworks.flowables.outputs.list[nodename]
if output then
currentpressure = flowlogic.run_output(
pos,
node,
currentpressure,
output.upper,
output.lower,
output.outputfn)
end
-- set the new pressure -- set the new pressure
nodepressure.set(currentpressure) nodepressure.set(currentpressure)
end end
@ -176,20 +189,18 @@ end
flowlogic.run_output = function(pos, node, threshold, outputfn) flowlogic.run_output = function(pos, node, currentpressure, upper, lower, outputfn)
-- callback for output devices. -- processing step for water output devices.
-- takes care of checking a minimum pressure value and updating the node metadata. -- takes care of checking a minimum pressure value and updating the resulting pressure level
-- the outputfn is provided the current pressure and returns the pressure "taken". -- the outputfn is provided the current pressure and returns the pressure "taken".
-- as an example, using this with the above spigot function, -- as an example, using this with the above spigot function,
-- the spigot function tries to output a water source if it will fit in the world. -- the spigot function tries to output a water source if it will fit in the world.
local meta = minetest.get_meta(pos) local result = currentpressure
-- sometimes I wonder if meta:get_* returning default values would ever be problematic. if currentpressure > lower then
-- though here it doesn't matter, an uninit'd node returns 0, which is fine for a new, empty node.
local currentpressure = meta:get_float(label_pressure)
if currentpressure > threshold then
local takenpressure = outputfn(pos, node, currentpressure) local takenpressure = outputfn(pos, node, currentpressure)
local newpressure = currentpressure - takenpressure local newpressure = currentpressure - takenpressure
if newpressure < 0 then currentpressure = 0 end if newpressure < 0 then newpressure = 0 end
meta:set_float(label_pressure, newpressure) result = newpressure
end end
return result
end end

View File

@ -75,10 +75,7 @@ register.output = function(nodename, upper, lower, outputfn)
error("pipeworks.flowables.outputs duplicate registration!") error("pipeworks.flowables.outputs duplicate registration!")
end end
checkbase(nodename) checkbase(nodename)
pipeworks.flowables.outputs.list[nodename] = { threshold=threshold, outputfn=outputfn } pipeworks.flowables.outputs.list[nodename] = { upper=upper, lower=lower, outputfn=outputfn }
if pipeworks.toggles.pressure_logic then
abmregister.output(nodename, lower, outputfn)
end
-- output ABM now part of main flow logic ABM to preserve ordering. -- output ABM now part of main flow logic ABM to preserve ordering.
-- note that because outputs have to be a flowable first -- note that because outputs have to be a flowable first
-- (and the installation of the flow logic ABM is conditional), -- (and the installation of the flow logic ABM is conditional),