mirror of
https://github.com/mt-mods/pipeworks.git
synced 2024-12-24 18:00:22 +01:00
abms.lua: refactor and generalise run_pump_intake() to allow passing custom intake functions
This commit is contained in:
parent
aee23d7642
commit
3486ee319e
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user