From 9c770532e6e3cb1157e467015a97aef97a8a8893 Mon Sep 17 00:00:00 2001 From: thetaepsilon-gamedev Date: Sat, 7 Oct 2017 19:19:09 +0100 Subject: [PATCH] new flow logic: abms.lua: refactor run_input to run as part of master run() ABM --- new_flow_logic/abm_register.lua | 2 ++ new_flow_logic/abms.lua | 31 ++++++++++++++----- .../flowable_node_registry_install.lua | 4 --- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/new_flow_logic/abm_register.lua b/new_flow_logic/abm_register.lua index 1c7eede..dbd37e6 100644 --- a/new_flow_logic/abm_register.lua +++ b/new_flow_logic/abm_register.lua @@ -50,6 +50,7 @@ register.balance = register_abm_balance -- register a node for the input ABM. -- intakefn is run on the node to determine how much water can be taken (and update it's environment accordingly). -- maxpressure is the maximum pressure that this input can drive, beyond which pressure will not be raised. +--[[ local register_abm_input = function(nodename, maxpressure, intakefn) minetest.register_abm({ nodenames = { nodename }, @@ -61,6 +62,7 @@ local register_abm_input = function(nodename, maxpressure, intakefn) }) end register.input = register_abm_input +]] -- register a node for the output ABM. -- threshold determines the minimum pressure, over which outputfn is called. diff --git a/new_flow_logic/abms.lua b/new_flow_logic/abms.lua index 671a69d..eb551d8 100644 --- a/new_flow_logic/abms.lua +++ b/new_flow_logic/abms.lua @@ -66,6 +66,14 @@ local get_pressure_access = function(pos) end +-- logging is unreliable when something is crashing... +local nilexplode = function(caller, label, value) + if value == nil then + error(caller..": "..label.." was nil") + end +end + + flowlogic.run = function(pos, node) local nodename = node.name @@ -73,6 +81,14 @@ flowlogic.run = function(pos, node) local nodepressure = get_pressure_access(pos) local currentpressure = nodepressure.get() + -- if node is an input: run intake phase + local inputdef = pipeworks.flowables.inputs.list[nodename] + if inputdef then + currentpressure = flowlogic.run_input(pos, node, currentpressure, inputdef) + --debuglog("post-intake currentpressure is "..currentpressure) + --nilexplode("run()", "currentpressure", currentpressure) + end + -- balance pressure with neighbours currentpressure = flowlogic.balance_pressure(pos, node, currentpressure) @@ -131,23 +147,22 @@ end -flowlogic.run_input = function(pos, node, maxpressure, intakefn) +flowlogic.run_input = function(pos, node, currentpressure, inputdef) -- intakefn allows a given input node to define it's own intake logic. -- this function will calculate the maximum amount of water that can be taken in; -- the intakefn will be given this and is expected to return the actual absorption amount. - local meta = minetest.get_meta(pos) - local currentpressure = meta:get_float(label_pressure) + local maxpressure = inputdef.maxpressure local intake_limit = maxpressure - currentpressure - if intake_limit <= 0 then return end + if intake_limit <= 0 then return currentpressure end - local actual_intake = intakefn(pos, intake_limit) + local actual_intake = inputdef.intakefn(pos, intake_limit) --pipeworks.logger("run_input@"..formatvec(pos).." oldpressure "..currentpressure.." intake_limit "..intake_limit.." actual_intake "..actual_intake) - if actual_intake <= 0 then return end + if actual_intake <= 0 then return currentpressure end local newpressure = actual_intake + currentpressure - -- debuglog("oldpressure "..currentpressure.." intake_limit "..intake_limit.." actual_intake "..actual_intake.." newpressure "..newpressure) - meta:set_float(label_pressure, newpressure) + --debuglog("run_input() end, oldpressure "..currentpressure.." intake_limit "..intake_limit.." actual_intake "..actual_intake.." newpressure "..newpressure) + return newpressure end diff --git a/new_flow_logic/flowable_node_registry_install.lua b/new_flow_logic/flowable_node_registry_install.lua index 35dbe97..6da87dc 100644 --- a/new_flow_logic/flowable_node_registry_install.lua +++ b/new_flow_logic/flowable_node_registry_install.lua @@ -62,10 +62,6 @@ register.intake = function(nodename, maxpressure, intakefn) checkbase(nodename) if list[nodename] then duplicateerr("pipeworks.flowables.inputs", nodename) end list[nodename] = { maxpressure=maxpressure, intakefn=intakefn } - table.insert(pipeworks.flowables.inputs.nodenames, nodename) - if pipeworks.toggles.pressure_logic then - abmregister.input(nodename, maxpressure, intakefn) - end regwarning("intake", nodename) end