2013-01-04 06:15:23 +01:00
|
|
|
-- This file provides the actual flow and pathfinding logic that makes water
|
|
|
|
-- move through the pipes.
|
|
|
|
--
|
2013-07-14 04:39:25 +02:00
|
|
|
-- Contributed by mauvebic, 2013-01-03, rewritten a bit by Vanessa Ezekowitz
|
2013-01-04 06:15:23 +01:00
|
|
|
--
|
|
|
|
|
2017-05-18 11:33:56 +02:00
|
|
|
local finitewater = minetest.settings:get_bool("liquid_finite")
|
2013-07-04 06:01:08 +02:00
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
pipeworks.check_for_liquids = function(pos)
|
2013-01-04 06:15:23 +01:00
|
|
|
local coords = {
|
|
|
|
{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}, }
|
|
|
|
for i =1,6 do
|
2013-07-01 05:55:07 +02:00
|
|
|
local name = minetest.get_node(coords[i]).name
|
2013-08-08 04:16:47 +02:00
|
|
|
if name and string.find(name,"water") then
|
2013-07-04 06:01:08 +02:00
|
|
|
if finitewater then minetest.remove_node(coords[i]) end
|
2013-05-10 21:59:56 +02:00
|
|
|
return true
|
|
|
|
end
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
pipeworks.check_for_inflows = function(pos,node)
|
2013-01-04 06:15:23 +01:00
|
|
|
local coords = {
|
|
|
|
{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},
|
2017-03-17 14:02:34 +01:00
|
|
|
{x=pos.x,y=pos.y,z=pos.z+1},
|
|
|
|
}
|
2013-01-04 06:15:23 +01:00
|
|
|
local newnode = false
|
|
|
|
local source = false
|
2017-03-17 14:02:34 +01:00
|
|
|
for i = 1, 6 do
|
2013-01-04 06:15:23 +01:00
|
|
|
if newnode then break end
|
2017-03-17 14:02:34 +01:00
|
|
|
local testnode = minetest.get_node(coords[i])
|
|
|
|
local name = testnode.name
|
2013-12-15 08:53:10 +01:00
|
|
|
if name and (name == "pipeworks:pump_on" and pipeworks.check_for_liquids(coords[i])) or string.find(name,"_loaded") then
|
2013-07-04 06:01:08 +02:00
|
|
|
if string.find(name,"_loaded") then
|
2013-12-21 11:11:36 +01:00
|
|
|
source = minetest.get_meta(coords[i]):get_string("source")
|
2013-01-04 06:15:23 +01:00
|
|
|
if source == minetest.pos_to_string(pos) then break end
|
|
|
|
end
|
2017-03-17 14:02:34 +01:00
|
|
|
if string.find(name, "valve") or string.find(name, "sensor") then
|
|
|
|
|
|
|
|
if ((i == 3 or i == 4) and minetest.facedir_to_dir(testnode.param2).x ~= 0)
|
|
|
|
or ((i == 5 or i == 6) and minetest.facedir_to_dir(testnode.param2).z ~= 0)
|
|
|
|
or ((i == 1 or i == 2) and minetest.facedir_to_dir(testnode.param2).y ~= 0) then
|
|
|
|
|
|
|
|
newnode = string.gsub(node.name,"empty","loaded")
|
|
|
|
source = {x=coords[i].x,y=coords[i].y,z=coords[i].z}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
newnode = string.gsub(node.name,"empty","loaded")
|
|
|
|
source = {x=coords[i].x,y=coords[i].y,z=coords[i].z}
|
|
|
|
end
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if newnode then
|
2013-07-01 05:55:07 +02:00
|
|
|
minetest.add_node(pos,{name=newnode, param2 = node.param2})
|
2013-07-04 06:01:08 +02:00
|
|
|
minetest.get_meta(pos):set_string("source",minetest.pos_to_string(source))
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
pipeworks.check_sources = function(pos,node)
|
2013-07-04 06:01:08 +02:00
|
|
|
local sourcepos = minetest.string_to_pos(minetest.get_meta(pos):get_string("source"))
|
2013-05-10 23:28:11 +02:00
|
|
|
if not sourcepos then return end
|
2013-07-01 05:55:07 +02:00
|
|
|
local source = minetest.get_node(sourcepos).name
|
2013-01-04 06:15:23 +01:00
|
|
|
local newnode = false
|
2013-12-15 08:53:10 +01:00
|
|
|
if source and not ((source == "pipeworks:pump_on" and pipeworks.check_for_liquids(sourcepos)) or string.find(source,"_loaded") or source == "ignore" ) then
|
2013-07-04 06:01:08 +02:00
|
|
|
newnode = string.gsub(node.name,"loaded","empty")
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if newnode then
|
2013-07-01 05:55:07 +02:00
|
|
|
minetest.add_node(pos,{name=newnode, param2 = node.param2})
|
2013-07-04 06:01:08 +02:00
|
|
|
minetest.get_meta(pos):set_string("source","")
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
pipeworks.spigot_check = function(pos, node)
|
2013-07-04 06:01:08 +02:00
|
|
|
local belowname = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name
|
2013-08-08 04:16:47 +02:00
|
|
|
if belowname and (belowname == "air" or belowname == "default:water_flowing" or belowname == "default:water_source") then
|
2013-07-04 06:01:08 +02:00
|
|
|
local spigotname = minetest.get_node(pos).name
|
2016-06-12 02:09:07 +02:00
|
|
|
local fdir=node.param2 % 4
|
2013-07-04 06:01:08 +02:00
|
|
|
local check = {
|
|
|
|
{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}
|
|
|
|
}
|
2013-08-08 04:16:47 +02:00
|
|
|
local near_node = minetest.get_node(check[fdir+1])
|
|
|
|
if near_node and string.find(near_node.name, "_loaded") then
|
|
|
|
if spigotname and spigotname == "pipeworks:spigot" then
|
2013-07-04 06:01:08 +02:00
|
|
|
minetest.add_node(pos,{name = "pipeworks:spigot_pouring", param2 = fdir})
|
|
|
|
if finitewater or belowname ~= "default:water_source" then
|
|
|
|
minetest.add_node({x=pos.x,y=pos.y-1,z=pos.z},{name = "default:water_source"})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if spigotname == "pipeworks:spigot_pouring" then
|
|
|
|
minetest.add_node({x=pos.x,y=pos.y,z=pos.z},{name = "pipeworks:spigot", param2 = fdir})
|
|
|
|
if belowname == "default:water_source" and not finitewater then
|
|
|
|
minetest.remove_node({x=pos.x,y=pos.y-1,z=pos.z})
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
pipeworks.fountainhead_check = function(pos, node)
|
2013-10-28 03:38:19 +01:00
|
|
|
local abovename = minetest.get_node({x=pos.x,y=pos.y+1,z=pos.z}).name
|
|
|
|
if abovename and (abovename == "air" or abovename == "default:water_flowing" or abovename == "default:water_source") then
|
|
|
|
local fountainhead_name = minetest.get_node(pos).name
|
|
|
|
local near_node = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z})
|
|
|
|
if near_node and string.find(near_node.name, "_loaded") then
|
|
|
|
if fountainhead_name and fountainhead_name == "pipeworks:fountainhead" then
|
|
|
|
minetest.add_node(pos,{name = "pipeworks:fountainhead_pouring"})
|
|
|
|
if finitewater or abovename ~= "default:water_source" then
|
|
|
|
minetest.add_node({x=pos.x,y=pos.y+1,z=pos.z},{name = "default:water_source"})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if fountainhead_name == "pipeworks:fountainhead_pouring" then
|
|
|
|
minetest.add_node({x=pos.x,y=pos.y,z=pos.z},{name = "pipeworks:fountainhead"})
|
|
|
|
if abovename == "default:water_source" and not finitewater then
|
|
|
|
minetest.remove_node({x=pos.x,y=pos.y+1,z=pos.z})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-27 15:14:33 +02:00
|
|
|
|
2017-09-27 15:59:25 +02:00
|
|
|
|
|
|
|
|
2017-09-27 17:02:30 +02:00
|
|
|
-- global values and thresholds for water behaviour
|
|
|
|
-- TODO: add some way of setting this per-world
|
|
|
|
local thresholds = {}
|
|
|
|
-- limit on pump pressure - will not absorb more than can be taken
|
|
|
|
thresholds.pump_pressure = 2
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-27 15:59:25 +02:00
|
|
|
-- borrowed from above: might be useable to replace the above coords tables
|
|
|
|
local make_coords_offsets = function(pos, include_base)
|
|
|
|
local coords = {
|
|
|
|
{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},
|
|
|
|
}
|
|
|
|
if include_base then table.insert(coords, pos) end
|
|
|
|
return coords
|
2017-09-27 15:14:33 +02:00
|
|
|
end
|
|
|
|
|
2017-09-27 16:49:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- 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.
|
2017-09-27 17:02:30 +02:00
|
|
|
-- will take care of zero or negative-valued limits.
|
2017-09-27 16:49:03 +02:00
|
|
|
pipeworks.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
|
|
|
|
if total >= limit then break end
|
|
|
|
local name = minetest.get_node(tpos).name
|
|
|
|
if name == "default:water_source" then
|
|
|
|
minetest.remove_node(tpos)
|
|
|
|
total = total + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return total
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-27 15:59:25 +02:00
|
|
|
local label_pressure = "pipeworks.water_pressure"
|
|
|
|
local label_haspressure = "pipeworks.is_pressure_node"
|
2017-09-27 15:14:33 +02:00
|
|
|
pipeworks.balance_pressure = function(pos, node)
|
2017-09-27 15:59:25 +02:00
|
|
|
-- debuglog("balance_pressure() "..node.name.." at "..pos.x.." "..pos.y.." "..pos.z)
|
|
|
|
-- check the pressure of all nearby nodes, and average it out.
|
|
|
|
-- for the moment, only balance neighbour nodes if it already has a pressure value.
|
|
|
|
-- XXX: maybe this could be used to add fluid behaviour to other mod's nodes too?
|
|
|
|
|
|
|
|
-- unconditionally include self in nodes to average over
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local currentpressure = meta:get_float(label_pressure)
|
|
|
|
meta:set_int(label_haspressure, 1)
|
|
|
|
local connections = { meta }
|
|
|
|
local totalv = currentpressure
|
|
|
|
local totalc = 1
|
|
|
|
|
|
|
|
-- then handle neighbours, but if not a pressure node don't consider them at all
|
|
|
|
for _, npos in ipairs(make_coords_offsets(pos, false)) do
|
|
|
|
local neighbour = minetest.get_meta(npos)
|
|
|
|
local haspressure = (neighbour:get_int(label_haspressure) ~= 0)
|
|
|
|
if haspressure then
|
|
|
|
local n = neighbour:get_float(label_pressure)
|
|
|
|
table.insert(connections, neighbour)
|
|
|
|
totalv = totalv + n
|
|
|
|
totalc = totalc + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local average = totalv / totalc
|
|
|
|
for _, targetmeta in ipairs(connections) do
|
|
|
|
targetmeta:set_float(label_pressure, average)
|
|
|
|
end
|
2017-09-27 15:14:33 +02:00
|
|
|
end
|
2017-09-27 17:02:30 +02:00
|
|
|
|
|
|
|
pipeworks.run_pump_intake = function(pos, node)
|
|
|
|
-- try to absorb nearby water nodes, but only up to limit.
|
|
|
|
-- NB: check_for_liquids_v2 handles zero or negative from the following subtraction
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local currentpressure = meta:get_float(label_pressure)
|
|
|
|
local intake_limit = thresholds.pump_pressure - currentpressure
|
|
|
|
local actual_intake = pipeworks.check_for_liquids_v2(pos, limit)
|
|
|
|
local newpressure = actual_intake + currentpressure
|
|
|
|
meta:set_float(label_pressure, newpressure)
|
|
|
|
end
|