summaryrefslogtreecommitdiff
path: root/flowing_logic.lua
diff options
context:
space:
mode:
authorVanessa Ezekowitz <vanessaezekowitz@gmail.com>2013-01-04 00:15:23 -0500
committerVanessa Ezekowitz <vanessaezekowitz@gmail.com>2013-01-04 00:15:23 -0500
commit6419ecb1178fb6073fa9b7e153f96c3fd5946a87 (patch)
tree3a34a0f1f484b4af6d78beb132be85722b891bb7 /flowing_logic.lua
parent70d8e92aff5c4868b0625baf21ce3684d26e93a7 (diff)
downloadpipeworks-6419ecb1178fb6073fa9b7e153f96c3fd5946a87.tar
pipeworks-6419ecb1178fb6073fa9b7e153f96c3fd5946a87.tar.gz
pipeworks-6419ecb1178fb6073fa9b7e153f96c3fd5946a87.tar.bz2
pipeworks-6419ecb1178fb6073fa9b7e153f96c3fd5946a87.tar.xz
pipeworks-6419ecb1178fb6073fa9b7e153f96c3fd5946a87.zip
First stage of integrating Mauvebic's water flowing code. This is experimental
and doesn't move water yet - but at least it doesn't break anything :-)
Diffstat (limited to 'flowing_logic.lua')
-rw-r--r--flowing_logic.lua110
1 files changed, 110 insertions, 0 deletions
diff --git a/flowing_logic.lua b/flowing_logic.lua
new file mode 100644
index 0000000..6fa37e3
--- /dev/null
+++ b/flowing_logic.lua
@@ -0,0 +1,110 @@
+-- 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
+ local name = minetest.env:get_node(coords[i]).name
+ if string.find(name,'water') then return true end
+ 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
+ local name = minetest.env:get_node(coords[i]).name
+ if (name == 'pipeworks:pump_on' and check4liquids(coords[i])) or string.find(name,'_loaded') then
+ if string.find(name,'_loaded') then
+ local source = minetest.env:get_meta(coords[i]):get_string('source')
+ 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 dbg(newnode..' to replace '..node.name) end
+ if newnode then
+ minetest.env:add_node(pos,{name=newnode})
+ minetest.env:get_meta(pos):set_string('source',minetest.pos_to_string(source))
+ end
+end
+
+local checksources = function(pos,node)
+ local sourcepos = minetest.string_to_pos(minetest.env:get_meta(pos):get_string('source'))
+ local source = minetest.env:get_node(sourcepos).name
+ 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
+ minetest.env:add_node(pos,{name=newnode})
+ minetest.env:get_meta(pos):set_string('source','')
+ end
+end
+
+local update_outlet = function(pos)
+ local top = minetest.env:get_node({x=pos.x,y=pos.y+1,z=pos.z}).name
+ if string.find(top,'_loaded') then
+ minetest.env:add_node({x=pos.x,y=pos.y-1,z=pos.z},{name='default:water_source'})
+ elseif minetest.env:get_node({x=pos.x,y=pos.y-1,z=pos.z}).name == 'default:water_source' then
+ minetest.env:remove_node({x=pos.x,y=pos.y-1,z=pos.z})
+ end
+end
+
+local spigot_check = function(pos,node)
+ 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} }
+ dbg(node.param2..' checking '..minetest.pos_to_string(check[node.param2+1])..' for spigot at '..minetest.pos_to_string(pos))
+ local top = minetest.env:get_node(check[node.param2+1]).name
+ dbg('found '..top)
+ if string.find(top,'_loaded') then
+ minetest.env:add_node({x=pos.x,y=pos.y-1,z=pos.z},{name='default:water_source'})
+ elseif minetest.env:get_node({x=pos.x,y=pos.y-1,z=pos.z}).name == 'default:water_source' then
+ minetest.env:remove_node({x=pos.x,y=pos.y-1,z=pos.z})
+ end
+end
+
+minetest.register_abm({
+ nodenames = empty_nodenames,
+ interval = 15,
+ chance = 1,
+ action = function(pos, node, active_object_count, active_object_count_wider) check4inflows(pos,node) end
+})
+
+minetest.register_abm({
+ nodenames = full_nodenames,
+ interval = 10,
+ chance = 1,
+ action = function(pos, node, active_object_count, active_object_count_wider) checksources(pos,node) end
+})
+
+minetest.register_abm({
+ nodenames = {'pipeworks:outlet','pipeworks:spigot'},
+ interval = 10,
+ chance = 1,
+ action = function(pos, node, active_object_count, active_object_count_wider)
+ if node.name == 'pipeworks:outlet' then update_outlet(pos)
+ elseif node.name == 'pipeworks:spigot' then spigot_check(pos,node) end
+ end
+})