on_timer for fluid transfer

This commit is contained in:
Evert Prants 2018-06-23 19:10:08 +03:00
parent 1a1b3caeaa
commit 708b9a3587
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 82 additions and 81 deletions

View File

@ -84,105 +84,105 @@ local function fluid_targets(p_pos, pos)
return targets return targets
end end
----------------------- function elefluid.transfer_timer_tick(pos, elapsed)
-- Main Transfer ABM -- local refresh = true
----------------------- local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
local meta1 = nil
local targets = {}
minetest.register_abm({ -- Only allow the node directly behind to be a start of a network
nodenames = {"group:elefluid_transport_source"}, local tpos = vector.add(minetest.facedir_to_dir(node.param2), pos)
label = "elepower Fluid Transfer Tick", local tname = minetest.get_node(tpos).name
interval = 1, if not ele.helpers.get_item_group(tname, "elefluid_transport") and
chance = 1, not ele.helpers.get_item_group(tname, "fluid_container") then
action = function(pos, node, active_object_count, active_object_count_wider) minetest.forceload_free_block(pos)
local meta = minetest.get_meta(pos) return
local meta1 = nil end
local targets = {} -- Retrieve network
targets = fluid_targets(pos, tpos)
-- Only allow the node directly behind to be a start of a network -- No targets, don't proceed
local tpos = vector.add(minetest.facedir_to_dir(node.param2), pos) if #targets == 0 then
local tname = minetest.get_node(tpos).name return true
if not ele.helpers.get_item_group(tname, "elefluid_transport") and end
not ele.helpers.get_item_group(tname, "fluid_container") then
minetest.forceload_free_block(pos)
return
end
-- Retrieve network -- Begin transfer
minetest.forceload_block(pos) local srcpos = ele.helpers.face_front(pos, node.param2)
targets = fluid_targets(pos, tpos) local srcnode = minetest.get_node(srcpos)
-- No targets, don't proceed -- Make sure source node is not air
if #targets == 0 then if not srcnode or srcnode.name == "air" then
return return true
end end
-- Begin transfer -- Make sure source node is a registered fluid container
local srcpos = ele.helpers.face_front(pos, node.param2) if not ele.helpers.get_item_group(srcnode.name, "fluid_container") then
local srcnode = minetest.get_node(srcpos) return true
end
-- Make sure source node is not air local srcmeta = minetest.get_meta(srcpos)
if not srcnode or srcnode.name == "air" then local srcdef = minetest.registered_nodes[srcnode.name]
return local buffers = fluid_lib.get_node_buffers(srcpos)
end if not buffers then return true end
-- Make sure source node is a registered fluid container -- Limit the amount of fluid pumped per cycle
if not ele.helpers.get_item_group(srcnode.name, "fluid_container") then local pcapability = ele.helpers.get_node_property(meta, pos, "fluid_pump_capacity")
return local pumped = 0
end
local srcmeta = minetest.get_meta(srcpos) -- Transfer some fluid here
local srcdef = minetest.registered_nodes[srcnode.name] for _,pos in pairs(targets) do
local buffers = fluid_lib.get_node_buffers(srcpos) if not vector.equals(pos, srcpos) then
if not buffers then return nil end if pumped >= pcapability then break end
local pp = fluid_lib.get_node_buffers(pos)
-- Limit the amount of fluid pumped per cycle local changed = false
local pcapability = ele.helpers.get_node_property(meta, pos, "fluid_pump_capacity")
local pumped = 0
-- Transfer some fluid here if pp ~= nil then
for _,pos in pairs(targets) do for name in pairs(pp) do
if not vector.equals(pos, srcpos) then for bname in pairs(buffers) do
if pumped >= pcapability then break end if pumped >= pcapability then break end
local pp = fluid_lib.get_node_buffers(pos) local buffer_data = fluid_lib.get_buffer_data(srcpos, bname)
local target_data = fluid_lib.get_buffer_data(pos, name)
local changed = false if (target_data.fluid == buffer_data.fluid or target_data.fluid == "") and
buffer_data.fluid ~= "" and buffer_data.amount > 0 and
(buffer_data.drainable == nil or buffer_data.drainable == true) and
fluid_lib.buffer_accepts_fluid(pos, name, buffer_data.fluid) then
if pp ~= nil then if fluid_lib.can_insert_into_buffer(pos, name, buffer_data.fluid, pcapability) > 0 then
for name in pairs(pp) do local res_f, count = fluid_lib.take_from_buffer(srcpos, bname, pcapability)
for bname in pairs(buffers) do if count > 0 then
if pumped >= pcapability then break end fluid_lib.insert_into_buffer(pos, name, res_f, count)
local buffer_data = fluid_lib.get_buffer_data(srcpos, bname) pumped = pumped + count
local target_data = fluid_lib.get_buffer_data(pos, name) changed = true
if (target_data.fluid == buffer_data.fluid or target_data.fluid == "") and
buffer_data.fluid ~= "" and buffer_data.amount > 0 and
(buffer_data.drainable == nil or buffer_data.drainable == true) and
fluid_lib.buffer_accepts_fluid(pos, name, buffer_data.fluid) then
if fluid_lib.can_insert_into_buffer(pos, name, buffer_data.fluid, pcapability) > 0 then
local res_f, count = fluid_lib.take_from_buffer(srcpos, bname, pcapability)
if count > 0 then
fluid_lib.insert_into_buffer(pos, name, res_f, count)
pumped = pumped + count
changed = true
end
end end
end end
end end
end end
end end
end
if changed then if changed then
minetest.get_node_timer(srcpos):start(1.0) minetest.get_node_timer(srcpos):start(1.0)
minetest.get_node_timer(pos):start(1.0) minetest.get_node_timer(pos):start(1.0)
end
end end
end end
end, end
})
function elefluid.clear_networks(pos) return refresh
-- TODO: Fluid network cache end
function elefluid.refresh_node(pos)
minetest.get_node_timer(pos):start(1.0)
return return
end end
minetest.register_lbm({
label = "Fluid Transfer Tick",
name = "elepower_fapi:fluid_transfer_tick",
nodenames = {"group:elefluid_transport_source"},
run_at_every_load = true,
action = elefluid.refresh_node,
})

View File

@ -10,6 +10,7 @@ function elefluid.register_transfer_node(nodename, nodedef)
nodedef.groups["elefluid_transport_source"] = 1 nodedef.groups["elefluid_transport_source"] = 1
nodedef.paramtype2 = "facedir" nodedef.paramtype2 = "facedir"
nodedef.legacy_facedir_simple = true nodedef.legacy_facedir_simple = true
nodedef.on_timer = elefluid.transfer_timer_tick
local orig_construct = nodedef.on_construct local orig_construct = nodedef.on_construct
nodedef.on_construct = function (pos) nodedef.on_construct = function (pos)
@ -17,14 +18,14 @@ function elefluid.register_transfer_node(nodename, nodedef)
meta:set_int("fluid_store", 0) meta:set_int("fluid_store", 0)
meta:set_string("fluid", "") meta:set_string("fluid", "")
elefluid.clear_networks(pos) elefluid.refresh_node(pos)
if orig_construct then if orig_construct then
orig_construct(pos) orig_construct(pos)
end end
end end
nodedef.after_destruct = elefluid.clear_networks nodedef.after_destruct = elefluid.refresh_node
-- Default transfer capacity -- Default transfer capacity
if not nodedef.ele_fluid_pump_capacity then if not nodedef.ele_fluid_pump_capacity then