2013-01-04 06:15:23 +01:00
|
|
|
-- This file provides the actual flow and pathfinding logic that makes water
|
|
|
|
-- move through the pipes.
|
|
|
|
--
|
|
|
|
-- Contributed by mauvebic, 2013-01-03, with tweaks by Vanessa Ezekowitz
|
|
|
|
--
|
|
|
|
|
|
|
|
local check4liquids = function(pos)
|
|
|
|
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-05-10 21:59:56 +02:00
|
|
|
if string.find(name,'water') then
|
2013-07-01 05:55:07 +02:00
|
|
|
minetest.remove_node(coords[i])
|
2013-05-10 21:59:56 +02:00
|
|
|
return true
|
|
|
|
end
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local check4inflows = function(pos,node)
|
|
|
|
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}, }
|
|
|
|
local newnode = false
|
|
|
|
local source = false
|
|
|
|
for i =1,6 do
|
|
|
|
if newnode then break end
|
2013-07-01 05:55:07 +02:00
|
|
|
local name = minetest.get_node(coords[i]).name
|
2013-01-04 06:15:23 +01:00
|
|
|
if (name == 'pipeworks:pump_on' and check4liquids(coords[i])) or string.find(name,'_loaded') then
|
|
|
|
if string.find(name,'_loaded') then
|
2013-07-01 05:55:07 +02:00
|
|
|
local 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
|
|
|
|
newnode = string.gsub(node.name,'empty','loaded')
|
|
|
|
source = {x=coords[i].x,y=coords[i].y,z=coords[i].z}
|
|
|
|
if newnode ~= nil then dbg(newnode) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if newnode then
|
2013-01-04 07:12:39 +01:00
|
|
|
dbg(newnode..' to replace '..node.name)
|
2013-07-01 05:55:07 +02:00
|
|
|
minetest.add_node(pos,{name=newnode, param2 = node.param2})
|
|
|
|
minetest.get_meta(pos):set_string('source',minetest.pos_to_string(source))
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local checksources = function(pos,node)
|
2013-07-01 05:55:07 +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
|
|
|
|
if not ((source == 'pipeworks:pump_on' and check4liquids(sourcepos)) or string.find(source,'_loaded') or source == 'ignore' ) then
|
|
|
|
newnode = string.gsub(node.name,'loaded','empty')
|
|
|
|
end
|
|
|
|
|
|
|
|
if newnode then dbg(newnode..' to replace '..node.name) end
|
|
|
|
if newnode then
|
2013-07-01 05:55:07 +02:00
|
|
|
minetest.add_node(pos,{name=newnode, param2 = node.param2})
|
|
|
|
minetest.get_meta(pos):set_string('source','')
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local update_outlet = function(pos)
|
2013-07-01 05:55:07 +02:00
|
|
|
local top = minetest.get_node({x=pos.x,y=pos.y+1,z=pos.z}).name
|
2013-01-04 06:15:23 +01:00
|
|
|
if string.find(top,'_loaded') then
|
2013-07-01 05:55:07 +02:00
|
|
|
local name = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name
|
2013-05-10 21:59:56 +02:00
|
|
|
if name == 'air' or name == "default:water_source" or name == "default:water_flowing" then
|
2013-07-01 05:55:07 +02:00
|
|
|
minetest.add_node({x=pos.x,y=pos.y-1,z=pos.z},{name='default:water_source'})
|
2013-04-28 23:45:25 +02:00
|
|
|
end
|
2013-07-01 05:55:07 +02:00
|
|
|
elseif minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name == 'default:water_source' then
|
|
|
|
minetest.remove_node({x=pos.x,y=pos.y-1,z=pos.z})
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local spigot_check = function(pos,node)
|
2013-01-15 01:55:41 +01:00
|
|
|
local fdir=node.param2
|
2013-01-04 06:15:23 +01: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-01-15 01:55:41 +01:00
|
|
|
dbg(fdir..' checking '..minetest.pos_to_string(check[fdir+1])..' for spigot at '..minetest.pos_to_string(pos))
|
2013-07-01 05:55:07 +02:00
|
|
|
local top = minetest.get_node(check[fdir+1]).name
|
2013-01-04 06:15:23 +01:00
|
|
|
dbg('found '..top)
|
2013-07-01 05:55:07 +02:00
|
|
|
local name = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name
|
2013-05-10 21:59:56 +02:00
|
|
|
if string.find(top,'_loaded') and (name == 'air' or name == "default:water_source" or name == "default:water_flowing") then
|
2013-07-01 05:55:07 +02:00
|
|
|
minetest.add_node({x=pos.x,y=pos.y-1,z=pos.z},{name='default:water_source'})
|
|
|
|
minetest.add_node(pos,{name='pipeworks:spigot_pouring', param2 = fdir})
|
2013-04-28 23:45:25 +02:00
|
|
|
else
|
2013-07-01 05:55:07 +02:00
|
|
|
if minetest.get_node(pos).name == 'pipeworks:spigot_pouring' then
|
|
|
|
minetest.add_node({x=pos.x,y=pos.y,z=pos.z},{name='pipeworks:spigot', param2 = fdir})
|
2013-05-10 22:17:47 +02:00
|
|
|
if name == 'air' or name == "default:water_source" or name == "default:water_flowing" then
|
2013-07-01 05:55:07 +02:00
|
|
|
minetest.remove_node({x=pos.x,y=pos.y-1,z=pos.z})
|
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-05-10 23:28:11 +02:00
|
|
|
table.insert(pipes_empty_nodenames,"pipeworks:valve_on_empty")
|
|
|
|
table.insert(pipes_empty_nodenames,"pipeworks:valve_off_empty")
|
|
|
|
table.insert(pipes_empty_nodenames,"pipeworks:valve_on_loaded")
|
|
|
|
table.insert(pipes_empty_nodenames,"pipeworks:entry_panel_empty")
|
2013-05-11 00:28:57 +02:00
|
|
|
table.insert(pipes_empty_nodenames,"pipeworks:flow_sensor_empty")
|
2013-05-10 23:28:11 +02:00
|
|
|
|
|
|
|
table.insert(pipes_full_nodenames,"pipeworks:valve_on_empty")
|
|
|
|
table.insert(pipes_full_nodenames,"pipeworks:valve_off_empty")
|
|
|
|
table.insert(pipes_full_nodenames,"pipeworks:valve_on_loaded")
|
|
|
|
table.insert(pipes_full_nodenames,"pipeworks:entry_panel_loaded")
|
2013-05-11 00:28:57 +02:00
|
|
|
table.insert(pipes_full_nodenames,"pipeworks:flow_sensor_loaded")
|
2013-05-10 23:28:11 +02:00
|
|
|
|
2013-01-04 06:15:23 +01:00
|
|
|
minetest.register_abm({
|
2013-01-04 07:34:25 +01:00
|
|
|
nodenames = pipes_empty_nodenames,
|
|
|
|
interval = 1,
|
2013-01-04 06:15:23 +01:00
|
|
|
chance = 1,
|
|
|
|
action = function(pos, node, active_object_count, active_object_count_wider) check4inflows(pos,node) end
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_abm({
|
2013-01-04 07:34:25 +01:00
|
|
|
nodenames = pipes_full_nodenames,
|
|
|
|
interval = 1,
|
2013-01-04 06:15:23 +01:00
|
|
|
chance = 1,
|
|
|
|
action = function(pos, node, active_object_count, active_object_count_wider) checksources(pos,node) end
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_abm({
|
2013-01-14 20:17:07 +01:00
|
|
|
nodenames = {'pipeworks:outlet','pipeworks:spigot','pipeworks:spigot_pouring'},
|
2013-01-04 07:34:25 +01:00
|
|
|
interval = 1,
|
2013-01-04 06:15:23 +01:00
|
|
|
chance = 1,
|
|
|
|
action = function(pos, node, active_object_count, active_object_count_wider)
|
|
|
|
if node.name == 'pipeworks:outlet' then update_outlet(pos)
|
2013-01-14 20:17:07 +01:00
|
|
|
elseif node.name == 'pipeworks:spigot' or node.name == 'pipeworks:spigot_pouring' then spigot_check(pos,node) end
|
2013-01-04 06:15:23 +01:00
|
|
|
end
|
|
|
|
})
|