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.
-- 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.
--[[
local register_abm_output = function(nodename, threshold, outputfn)
minetest.register_abm({
nodenames = { nodename },
@ -77,6 +78,7 @@ local register_abm_output = function(nodename, threshold, outputfn)
})
end
register.output = register_abm_output
]]
-- old spigot ABM code, not yet migrated
--[[

View File

@ -68,6 +68,7 @@ end
flowlogic.run = function(pos, node)
local nodename = node.name
-- get the current pressure value.
local nodepressure = get_pressure_access(pos)
local currentpressure = nodepressure.get()
@ -75,6 +76,18 @@ flowlogic.run = function(pos, node)
-- balance pressure with neighbours
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
nodepressure.set(currentpressure)
end
@ -176,20 +189,18 @@ end
flowlogic.run_output = function(pos, node, threshold, outputfn)
-- callback for output devices.
-- takes care of checking a minimum pressure value and updating the node metadata.
flowlogic.run_output = function(pos, node, currentpressure, upper, lower, outputfn)
-- processing step for water output devices.
-- 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".
-- 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.
local meta = minetest.get_meta(pos)
-- sometimes I wonder if meta:get_* returning default values would ever be problematic.
-- 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 result = currentpressure
if currentpressure > lower then
local takenpressure = outputfn(pos, node, currentpressure)
local newpressure = currentpressure - takenpressure
if newpressure < 0 then currentpressure = 0 end
meta:set_float(label_pressure, newpressure)
if newpressure < 0 then newpressure = 0 end
result = newpressure
end
return result
end

View File

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