Tweak chains selbox
[minetest_hades/hades_revisited.git] / mods / pipeworks / flowing_logic.lua
blobe32867eb704fad93c9f007cc05e7a1a0378806b0
1 -- This file provides the actual flow and pathfinding logic that makes water
2 -- move through the pipes.
3 --
4 -- Contributed by mauvebic, 2013-01-03, rewritten a bit by Vanessa Ezekowitz
5 --
7 local finitewater = minetest.settings:get_bool("liquid_finite")
9 pipeworks.check_for_liquids = function(pos)
10 local coords = {
11 {x=pos.x,y=pos.y-1,z=pos.z},
12 {x=pos.x,y=pos.y+1,z=pos.z},
13 {x=pos.x-1,y=pos.y,z=pos.z},
14 {x=pos.x+1,y=pos.y,z=pos.z},
15 {x=pos.x,y=pos.y,z=pos.z-1},
16 {x=pos.x,y=pos.y,z=pos.z+1}, }
17 for i =1,6 do
18 local name = minetest.get_node(coords[i]).name
19 if name and string.find(name,"water") then
20 if finitewater then minetest.remove_node(coords[i]) end
21 return true
22 end
23 end
24 return false
25 end
27 pipeworks.check_for_inflows = function(pos,node)
28 local coords = {
29 {x=pos.x,y=pos.y-1,z=pos.z},
30 {x=pos.x,y=pos.y+1,z=pos.z},
31 {x=pos.x-1,y=pos.y,z=pos.z},
32 {x=pos.x+1,y=pos.y,z=pos.z},
33 {x=pos.x,y=pos.y,z=pos.z-1},
34 {x=pos.x,y=pos.y,z=pos.z+1}, }
35 local newnode = false
36 local source = false
37 for i =1,6 do
38 if newnode then break end
39 local name = minetest.get_node(coords[i]).name
40 if name and (name == "pipeworks:pump_on" and pipeworks.check_for_liquids(coords[i])) or string.find(name,"_loaded") then
41 if string.find(name,"_loaded") then
42 source = minetest.get_meta(coords[i]):get_string("source")
43 if source == minetest.pos_to_string(pos) then break end
44 end
45 newnode = string.gsub(node.name,"empty","loaded")
46 source = {x=coords[i].x,y=coords[i].y,z=coords[i].z}
47 end
48 end
49 if newnode then
50 minetest.add_node(pos,{name=newnode, param2 = node.param2})
51 minetest.get_meta(pos):set_string("source",minetest.pos_to_string(source))
52 end
53 end
55 pipeworks.check_sources = function(pos,node)
56 local sourcepos = minetest.string_to_pos(minetest.get_meta(pos):get_string("source"))
57 if not sourcepos then return end
58 local source = minetest.get_node(sourcepos).name
59 local newnode = false
60 if source and not ((source == "pipeworks:pump_on" and pipeworks.check_for_liquids(sourcepos)) or string.find(source,"_loaded") or source == "ignore" ) then
61 newnode = string.gsub(node.name,"loaded","empty")
62 end
64 if newnode then
65 minetest.add_node(pos,{name=newnode, param2 = node.param2})
66 minetest.get_meta(pos):set_string("source","")
67 end
68 end
70 pipeworks.spigot_check = function(pos, node)
71 local belowname = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name
72 if belowname and (belowname == "air" or belowname == "hades_core:water_flowing" or belowname == "hades_core:water_source") then
73 local spigotname = minetest.get_node(pos).name
74 local fdir=node.param2
75 local check = {
76 {x=pos.x,y=pos.y,z=pos.z+1},
77 {x=pos.x+1,y=pos.y,z=pos.z},
78 {x=pos.x,y=pos.y,z=pos.z-1},
79 {x=pos.x-1,y=pos.y,z=pos.z}
81 local near_node = minetest.get_node(check[fdir+1])
82 if near_node and string.find(near_node.name, "_loaded") then
83 if spigotname and spigotname == "pipeworks:spigot" then
84 minetest.add_node(pos,{name = "pipeworks:spigot_pouring", param2 = fdir})
85 if finitewater or belowname ~= "hades_core:water_source" then
86 minetest.add_node({x=pos.x,y=pos.y-1,z=pos.z},{name = "hades_core:water_source"})
87 end
88 end
89 else
90 if spigotname == "pipeworks:spigot_pouring" then
91 minetest.add_node({x=pos.x,y=pos.y,z=pos.z},{name = "pipeworks:spigot", param2 = fdir})
92 if belowname == "hades_core:water_source" and not finitewater then
93 -- minetest.remove_node({x=pos.x,y=pos.y-1,z=pos.z})
94 end
95 end
96 end
97 end
98 end
100 pipeworks.fountainhead_check = function(pos, node)
101 local abovename = minetest.get_node({x=pos.x,y=pos.y+1,z=pos.z}).name
102 if abovename and (abovename == "air" or abovename == "hades_core:water_flowing" or abovename == "hades_core:water_source") then
103 local fountainhead_name = minetest.get_node(pos).name
104 local near_node = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z})
105 if near_node and string.find(near_node.name, "_loaded") then
106 if fountainhead_name and fountainhead_name == "pipeworks:fountainhead" then
107 minetest.add_node(pos,{name = "pipeworks:fountainhead_pouring"})
108 if finitewater or abovename ~= "hades_core:water_source" then
109 minetest.add_node({x=pos.x,y=pos.y+1,z=pos.z},{name = "hades_core:water_source"})
112 else
113 if fountainhead_name == "pipeworks:fountainhead_pouring" then
114 minetest.add_node({x=pos.x,y=pos.y,z=pos.z},{name = "pipeworks:fountainhead"})
117 if abovename == "hades_core:water_source" and not finitewater then
119 -- minetest.remove_node({x=pos.x,y=pos.y+1,z=pos.z})