From 3486ee319ee4fafbf83c583dd9fedfaed92db7c4 Mon Sep 17 00:00:00 2001 From: thetaepsilon-gamedev Date: Sun, 1 Oct 2017 00:44:14 +0100 Subject: abms.lua: refactor and generalise run_pump_intake() to allow passing custom intake functions --- new_flow_logic/abm_register.lua | 4 ++-- new_flow_logic/abms.lua | 20 ++++++++++---------- new_flow_logic/flowable_node_registry.lua | 3 +-- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/new_flow_logic/abm_register.lua b/new_flow_logic/abm_register.lua index 505ca4b..57c8a47 100644 --- a/new_flow_logic/abm_register.lua +++ b/new_flow_logic/abm_register.lua @@ -24,13 +24,13 @@ register.balance = register_abm_balance -- register a node for the pump ABM. -- maxpressure is the maximum pressure that this pump can drive. -local register_abm_input = function(nodename, maxpressure) +local register_abm_input = function(nodename, maxpressure, intakefn) minetest.register_abm({ nodenames = { nodename }, interval = 1, chance = 1, action = function(pos, node, active_object_count, active_object_count_wider) - pipeworks.flowlogic.run_pump_intake(pos, node, maxpressure) + pipeworks.flowlogic.run_input(pos, node, maxpressure, intakefn) end }) end diff --git a/new_flow_logic/abms.lua b/new_flow_logic/abms.lua index ece81c1..48dcb0b 100644 --- a/new_flow_logic/abms.lua +++ b/new_flow_logic/abms.lua @@ -31,12 +31,7 @@ end -- new version of liquid check -- accepts a limit parameter to only delete water blocks that the receptacle can accept, -- and returns it so that the receptacle can update it's pressure values. --- this should ensure that water blocks aren't vanished from existance. --- will take care of zero or negative-valued limits. local check_for_liquids_v2 = function(pos, limit) - if not limit then - limit = 6 - end local coords = make_coords_offsets(pos, false) local total = 0 for index, tpos in ipairs(coords) do @@ -89,15 +84,20 @@ end -flowlogic.run_pump_intake = function(pos, node, maxpressure) - -- try to absorb nearby water nodes, but only up to limit. - -- NB: check_for_liquids_v2 handles zero or negative from the following subtraction +flowlogic.run_input = function(pos, node, maxpressure, intakefn) + -- 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 intake_limit = maxpressure - currentpressure - local actual_intake = check_for_liquids_v2(pos, intake_limit) + if intake_limit <= 0 then return end + + local actual_intake = intakefn(pos, intake_limit) + if actual_intake <= 0 then return end + if actual_intake >= intake_limit then actual_intake = intake_limit 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) diff --git a/new_flow_logic/flowable_node_registry.lua b/new_flow_logic/flowable_node_registry.lua index 3cb2a67..1465561 100644 --- a/new_flow_logic/flowable_node_registry.lua +++ b/new_flow_logic/flowable_node_registry.lua @@ -51,7 +51,6 @@ local checkbase = function(nodename) end -- Register a node as a simple intake. --- See new_flow_logic for the details of this. -- Expects node to be registered as a flowable (is present in flowables.list.all), -- so that water can move out of it. -- maxpressure is the maximum pipeline pressure that this node can drive. @@ -61,6 +60,6 @@ register.intake_simple = function(nodename, maxpressure) pipeworks.flowables.inputs.list[nodename] = { maxpressure=maxpressure } table.insert(pipeworks.flowables.inputs.nodenames, nodename) if pipeworks.enable_new_flow_logic then - abmregister.input(nodename, maxpressure) + abmregister.input(nodename, maxpressure, pipeworks.flowlogic.check_for_liquids_v2) end end -- cgit v1.2.3