2014-01-01 21:57:51 +01:00
|
|
|
-- This file provides the actual flow logic that makes liquids
|
2013-01-04 06:15:23 +01:00
|
|
|
-- move through the pipes.
|
2014-01-01 21:57:51 +01:00
|
|
|
|
|
|
|
local finite_liquids = minetest.setting_getbool("liquid_finite")
|
2015-02-03 08:03:31 +01:00
|
|
|
local pipe_liquid_shows_loaded = 1
|
2014-01-01 21:57:51 +01:00
|
|
|
|
2015-02-03 07:23:16 +01:00
|
|
|
if mesecon then
|
|
|
|
pipereceptor_on = {
|
|
|
|
receptor = {
|
|
|
|
state = mesecon.state.on,
|
|
|
|
rules = pipeworks.mesecons_rules
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pipereceptor_off = {
|
|
|
|
receptor = {
|
|
|
|
state = mesecon.state.off,
|
|
|
|
rules = pipeworks.mesecons_rules
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-01-01 21:57:51 +01:00
|
|
|
-- Evaluate and balance liquid in all pipes
|
|
|
|
|
|
|
|
minetest.register_abm({
|
|
|
|
nodenames = pipeworks.pipe_nodenames,
|
2015-02-03 08:03:31 +01:00
|
|
|
interval = 1,
|
2014-01-01 21:57:51 +01:00
|
|
|
chance = 1,
|
|
|
|
action = function(pos, node, active_object_count, active_object_count_wider)
|
|
|
|
local coords = {
|
|
|
|
{x = pos.x, y = pos.y, z = pos.z},
|
|
|
|
{x = pos.x, y = pos.y-1, z = pos.z},
|
|
|
|
{x = pos.x, y = pos.y+1, z = pos.z},
|
|
|
|
{x = pos.x-1, y = pos.y, z = pos.z},
|
|
|
|
{x = pos.x+1, y = pos.y, z = pos.z},
|
|
|
|
{x = pos.x, y = pos.y, z = pos.z-1},
|
|
|
|
{x = pos.x, y = pos.y, z = pos.z+1},
|
|
|
|
}
|
|
|
|
|
|
|
|
local num_connections = 0
|
|
|
|
local connection_list = {}
|
|
|
|
local total_level = 0
|
|
|
|
|
|
|
|
for _,adjacentpos in ipairs(coords) do
|
|
|
|
local adjacent_node = minetest.get_node(adjacentpos)
|
|
|
|
if adjacent_node and string.find(dump(pipeworks.pipe_nodenames), adjacent_node.name) then
|
|
|
|
|
|
|
|
local node_level = (minetest.get_meta(adjacentpos):get_float("liquid_level")) or 0
|
|
|
|
if node_level < 0 then node_level = 0 end
|
|
|
|
|
|
|
|
total_level = total_level + node_level
|
|
|
|
num_connections = num_connections + 1
|
|
|
|
table.insert(connection_list, adjacentpos)
|
|
|
|
end
|
2013-05-10 21:59:56 +02:00
|
|
|
end
|
2014-01-01 21:57:51 +01:00
|
|
|
|
|
|
|
local average_level = total_level / num_connections
|
|
|
|
|
|
|
|
for _, connected_pipe_pos in ipairs(connection_list) do
|
|
|
|
|
|
|
|
local newnode
|
|
|
|
local connected_pipe = minetest.get_node(connected_pipe_pos)
|
|
|
|
local pipe_name = string.match(connected_pipe.name, "pipeworks:pipe_%d.*_")
|
|
|
|
|
|
|
|
if connected_pipe and pipe_name then
|
|
|
|
minetest.get_meta(connected_pipe_pos):set_float("liquid_level", average_level)
|
|
|
|
|
|
|
|
if average_level > pipe_liquid_shows_loaded then
|
|
|
|
newnode = pipe_name.."loaded"
|
|
|
|
else
|
|
|
|
newnode = pipe_name.."empty"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if newnode and connected_pipe.name ~= newnode then
|
|
|
|
minetest.swap_node(connected_pipe_pos, {name = newnode, param2 = connected_pipe.param2})
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-01 21:57:51 +01:00
|
|
|
})
|
2013-01-04 06:15:23 +01:00
|
|
|
|
2014-01-01 21:57:51 +01:00
|
|
|
-- Process all pumps in the area
|
|
|
|
|
|
|
|
minetest.register_abm({
|
|
|
|
nodenames = {"pipeworks:pump_on", "pipeworks:pump_off"},
|
2015-02-03 04:25:23 +01:00
|
|
|
interval = 2,
|
2014-01-01 21:57:51 +01:00
|
|
|
chance = 1,
|
|
|
|
action = function(pos, node, active_object_count, active_object_count_wider)
|
|
|
|
local minp = {x = pos.x-1, y = pos.y-1, z = pos.z-1}
|
|
|
|
local maxp = {x = pos.x+1, y = pos.y, z = pos.z+1}
|
|
|
|
local pos_above = {x = pos.x, y = pos.y+1, z = pos.z}
|
|
|
|
local node_above = minetest.get_node(pos_above)
|
|
|
|
if not node_above then return end
|
|
|
|
|
|
|
|
local meta = minetest.get_meta(pos_above)
|
|
|
|
local node_level_above = meta:get_float("liquid_level")
|
|
|
|
if node_level_above == nil then node_level_above = 0 end
|
|
|
|
local pipe_name = string.match(node_above.name, "pipeworks:pipe_%d.*_")
|
|
|
|
|
|
|
|
if pipe_name then
|
|
|
|
if node.name == "pipeworks:pump_on" then
|
|
|
|
local water_nodes = minetest.find_nodes_in_area(minp, maxp,
|
|
|
|
{"default:water_source", "default:water_flowing"})
|
2015-02-03 04:25:23 +01:00
|
|
|
|
|
|
|
if (node_level_above < 4 ) and #water_nodes > 1 then
|
2014-01-01 21:57:51 +01:00
|
|
|
meta:set_float("liquid_level", node_level_above + 4) -- add water to the pipe
|
2013-07-04 06:01:08 +02:00
|
|
|
end
|
2014-01-01 21:57:51 +01:00
|
|
|
else
|
|
|
|
if node_level_above > 0 then
|
2015-02-03 04:25:23 +01:00
|
|
|
meta:set_float("liquid_level", node_level_above - 0.5 ) -- leak the pipe down
|
2013-07-04 06:01:08 +02:00
|
|
|
end
|
2013-05-10 22:17:47 +02:00
|
|
|
end
|
2013-04-28 23:45:25 +02:00
|
|
|
end
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
2014-01-01 21:57:51 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
-- Process all fountainheads in the area
|
|
|
|
|
|
|
|
minetest.register_abm({
|
|
|
|
nodenames = {"pipeworks:fountainhead"},
|
2015-02-03 04:25:23 +01:00
|
|
|
interval = 2,
|
2014-01-01 21:57:51 +01:00
|
|
|
chance = 1,
|
|
|
|
action = function(pos, node, active_object_count, active_object_count_wider)
|
|
|
|
local pos_above = {x = pos.x, y = pos.y+1, z = pos.z}
|
|
|
|
local node_above = minetest.get_node(pos_above)
|
|
|
|
if not node_above then return end
|
|
|
|
|
|
|
|
local pos_below = {x = pos.x, y = pos.y-1, z = pos.z}
|
|
|
|
local node_below = minetest.get_node(pos_below)
|
|
|
|
if not node_below then return end
|
|
|
|
|
|
|
|
local node_level_below = (minetest.get_meta(pos_below):get_float("liquid_level")) or 0
|
|
|
|
|
2015-02-03 08:03:31 +01:00
|
|
|
if node_level_below > 0.9
|
2014-01-01 21:57:51 +01:00
|
|
|
and (node_above.name == "air" or node_above.name == "default:water_flowing") then
|
|
|
|
minetest.set_node(pos_above, {name = "default:water_source"})
|
2015-02-03 08:03:31 +01:00
|
|
|
elseif node_level_below < 0.75 and node_above.name == "default:water_source" then
|
2014-01-01 21:57:51 +01:00
|
|
|
minetest.set_node(pos_above, {name = "air"})
|
|
|
|
end
|
|
|
|
|
2015-02-03 04:25:23 +01:00
|
|
|
if node_level_below >= 1
|
2014-01-01 21:57:51 +01:00
|
|
|
and (node_above.name == "air" or node_above.name == "default:water_source") then
|
|
|
|
minetest.get_meta(pos_below):set_float("liquid_level", node_level_below - 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
|
|
|
-- Process all spigots in the area
|
|
|
|
|
|
|
|
minetest.register_abm({
|
|
|
|
nodenames = {"pipeworks:spigot","pipeworks:spigot_pouring"},
|
2015-02-03 04:25:23 +01:00
|
|
|
interval = 2,
|
2014-01-01 21:57:51 +01:00
|
|
|
chance = 1,
|
|
|
|
action = function(pos, node, active_object_count, active_object_count_wider)
|
|
|
|
local pos_below = {x = pos.x, y = pos.y-1, z = pos.z}
|
|
|
|
local below_node = minetest.get_node(pos_below)
|
|
|
|
if not below_node then return end
|
|
|
|
|
|
|
|
if below_node.name == "air" or below_node.name == "default:water_flowing"
|
|
|
|
or below_node.name == "default:water_source" then
|
|
|
|
local fdir = node.param2
|
|
|
|
local fdir_to_pos = {
|
|
|
|
{x = pos.x, y = pos.y, z = pos.z+1},
|
|
|
|
{x = pos.x+1, y = pos.y, z = pos.z },
|
|
|
|
{x = pos.x, y = pos.y, z = pos.z-1},
|
|
|
|
{x = pos.x-1, y = pos.y, z = pos.z }
|
|
|
|
}
|
|
|
|
|
|
|
|
local pos_adjacent = fdir_to_pos[fdir+1]
|
|
|
|
local adjacent_node = minetest.get_node(pos_adjacent)
|
|
|
|
if not adjacent_node then return end
|
|
|
|
local adjacent_node_level = (minetest.get_meta(pos_adjacent):get_float("liquid_level")) or 0
|
|
|
|
local pipe_name = string.match(adjacent_node.name, "pipeworks:pipe_%d.*_")
|
|
|
|
|
2015-02-03 08:03:31 +01:00
|
|
|
if pipe_name and adjacent_node_level > 0.9
|
2014-01-01 21:57:51 +01:00
|
|
|
and (below_node.name == "air" or below_node.name == "default:water_flowing") then
|
|
|
|
minetest.set_node(pos, {name = "pipeworks:spigot_pouring", param2 = fdir})
|
|
|
|
minetest.set_node(pos_below, {name = "default:water_source"})
|
2013-10-28 03:38:19 +01:00
|
|
|
end
|
2014-01-01 21:57:51 +01:00
|
|
|
|
2015-02-03 08:03:31 +01:00
|
|
|
if (pipe_name and adjacent_node_level < 0.75)
|
2014-01-01 21:57:51 +01:00
|
|
|
or (node.name ~= "pipeworks:spigot" and not pipe_name) then
|
|
|
|
minetest.set_node(pos,{name = "pipeworks:spigot", param2 = fdir})
|
|
|
|
if below_node.name == "default:water_source" then
|
|
|
|
minetest.set_node(pos_below, {name = "air"})
|
2013-10-28 03:38:19 +01:00
|
|
|
end
|
|
|
|
end
|
2014-01-01 21:57:51 +01:00
|
|
|
|
2015-02-03 04:25:23 +01:00
|
|
|
if adjacent_node_level >= 1
|
2014-01-01 21:57:51 +01:00
|
|
|
and (below_node.name == "air" or below_node.name == "default:water_source") then
|
|
|
|
minetest.get_meta(pos_adjacent):set_float("liquid_level", adjacent_node_level - 1)
|
|
|
|
end
|
2013-10-28 03:38:19 +01:00
|
|
|
end
|
|
|
|
end
|
2014-01-01 21:57:51 +01:00
|
|
|
})
|
|
|
|
|
2015-02-03 07:05:12 +01:00
|
|
|
pipeworks.device_nodenames = {}
|
|
|
|
|
|
|
|
table.insert(pipeworks.device_nodenames,"pipeworks:valve_on_empty")
|
|
|
|
table.insert(pipeworks.device_nodenames,"pipeworks:valve_off_empty")
|
|
|
|
table.insert(pipeworks.device_nodenames,"pipeworks:valve_on_loaded")
|
|
|
|
table.insert(pipeworks.device_nodenames,"pipeworks:flow_sensor_empty")
|
|
|
|
table.insert(pipeworks.device_nodenames,"pipeworks:flow_sensor_loaded")
|
|
|
|
|
|
|
|
minetest.register_abm({
|
|
|
|
nodenames = pipeworks.device_nodenames,
|
|
|
|
interval = 2,
|
|
|
|
chance = 1,
|
|
|
|
action = function(pos, node, active_object_count, active_object_count_wider)
|
|
|
|
local fdir = node.param2
|
|
|
|
local fdir_mod4 = fdir % 4
|
|
|
|
local fdir_mod4_p2 = (fdir+2) % 4
|
|
|
|
|
|
|
|
local fdir_to_pos = {
|
|
|
|
{x = pos.x+1, y = pos.y, z = pos.z },
|
|
|
|
{x = pos.x, y = pos.y, z = pos.z-1},
|
|
|
|
{x = pos.x-1, y = pos.y, z = pos.z },
|
|
|
|
{x = pos.x, y = pos.y, z = pos.z+1},
|
|
|
|
}
|
|
|
|
|
|
|
|
local pos_adjacent1 = fdir_to_pos[fdir_mod4 + 1]
|
|
|
|
local pos_adjacent2 = fdir_to_pos[fdir_mod4_p2 + 1]
|
|
|
|
|
|
|
|
local adjacent_node1 = minetest.get_node(pos_adjacent1)
|
|
|
|
local adjacent_node2 = minetest.get_node(pos_adjacent2)
|
|
|
|
|
|
|
|
if not adjacent_node1 or not adjacent_node2 then return end
|
|
|
|
|
|
|
|
local my_level = (minetest.get_meta(pos):get_float("liquid_level")) or 0
|
|
|
|
local adjacent_node_level1 = (minetest.get_meta(pos_adjacent1):get_float("liquid_level")) or 0
|
|
|
|
local adjacent_node_level2 = (minetest.get_meta(pos_adjacent2):get_float("liquid_level")) or 0
|
|
|
|
|
2015-02-03 08:05:14 +01:00
|
|
|
if not string.match(node.name, "pipeworks:valve_off") then
|
2015-02-03 07:05:12 +01:00
|
|
|
|
2015-02-03 08:05:14 +01:00
|
|
|
local num_connections = 1
|
|
|
|
local set1
|
|
|
|
local set2
|
|
|
|
local total_level = my_level
|
2015-02-03 07:05:12 +01:00
|
|
|
|
2015-02-03 08:05:14 +01:00
|
|
|
if string.find(dump(pipeworks.pipe_nodenames), adjacent_node1.name) or
|
|
|
|
(string.find(dump(pipeworks.device_nodenames), adjacent_node1.name) and
|
|
|
|
(adjacent_node1.param2 == fdir_mod4 or adjacent_node1.param2 == fdir_mod4_p2)) then
|
|
|
|
num_connections = num_connections + 1
|
|
|
|
total_level = total_level + adjacent_node_level1
|
|
|
|
set1 = true
|
|
|
|
end
|
2015-02-03 07:05:12 +01:00
|
|
|
|
2015-02-03 08:05:14 +01:00
|
|
|
if string.find(dump(pipeworks.pipe_nodenames), adjacent_node2.name) or
|
|
|
|
(string.find(dump(pipeworks.device_nodenames), adjacent_node2.name) and
|
|
|
|
(adjacent_node2.param2 == fdir_mod4 or adjacent_node2.param2 == fdir_mod4_p2)) then
|
|
|
|
num_connections = num_connections + 1
|
|
|
|
total_level = total_level + adjacent_node_level2
|
|
|
|
set2 = true
|
|
|
|
end
|
2015-02-03 07:05:12 +01:00
|
|
|
|
2015-02-03 08:05:14 +01:00
|
|
|
local average_level = total_level / num_connections
|
2015-02-03 07:05:12 +01:00
|
|
|
|
2015-02-03 08:05:14 +01:00
|
|
|
minetest.get_meta(pos):set_float("liquid_level", average_level)
|
2015-02-03 07:05:12 +01:00
|
|
|
|
2015-02-03 08:05:14 +01:00
|
|
|
if set1 then
|
|
|
|
minetest.get_meta(pos_adjacent1):set_float("liquid_level", average_level)
|
|
|
|
end
|
2015-02-03 07:23:16 +01:00
|
|
|
|
2015-02-03 08:05:14 +01:00
|
|
|
if set2 then
|
|
|
|
minetest.get_meta(pos_adjacent2):set_float("liquid_level", average_level)
|
|
|
|
end
|
|
|
|
|
|
|
|
if node.name == "pipeworks:flow_sensor_empty" or
|
|
|
|
node.name == "pipeworks:flow_sensor_loaded" then
|
|
|
|
local sensor = string.match(node.name, "pipeworks:flow_sensor_")
|
|
|
|
local newnode = nil
|
2015-02-03 07:23:16 +01:00
|
|
|
|
2015-02-03 08:05:14 +01:00
|
|
|
if my_level > 1 then
|
|
|
|
newnode = sensor.."loaded"
|
|
|
|
else
|
|
|
|
newnode = sensor.."empty"
|
|
|
|
end
|
|
|
|
|
|
|
|
if newnode ~= node.name then
|
|
|
|
minetest.swap_node(pos, {name = newnode, param2 = node.param2})
|
|
|
|
if mesecon then
|
|
|
|
if newnode == "pipeworks:flow_sensor_empty" then
|
|
|
|
mesecon.receptor_off(pos, rules)
|
|
|
|
else
|
|
|
|
mesecon.receptor_on(pos, rules)
|
2015-02-03 07:23:16 +01:00
|
|
|
end
|
2015-02-03 07:05:12 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-02-03 08:05:14 +01:00
|
|
|
end
|
2015-02-03 07:05:12 +01:00
|
|
|
end
|
|
|
|
})
|
|
|
|
|
|
|
|
-- table.insert(pipeworks.device_nodenames,"pipeworks:entry_panel")
|