flowing_logic.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. --
  6. local finitewater = minetest.settings:get_bool("liquid_finite")
  7. pipeworks.check_for_liquids = function(pos)
  8. local coords = {
  9. {x=pos.x,y=pos.y-1,z=pos.z},
  10. {x=pos.x,y=pos.y+1,z=pos.z},
  11. {x=pos.x-1,y=pos.y,z=pos.z},
  12. {x=pos.x+1,y=pos.y,z=pos.z},
  13. {x=pos.x,y=pos.y,z=pos.z-1},
  14. {x=pos.x,y=pos.y,z=pos.z+1}, }
  15. for i =1,6 do
  16. local name = minetest.get_node(coords[i]).name
  17. if name and string.find(name,"water") then
  18. if finitewater then minetest.remove_node(coords[i]) end
  19. return true
  20. end
  21. end
  22. return false
  23. end
  24. pipeworks.check_for_inflows = function(pos,node)
  25. local coords = {
  26. {x=pos.x,y=pos.y-1,z=pos.z},
  27. {x=pos.x,y=pos.y+1,z=pos.z},
  28. {x=pos.x-1,y=pos.y,z=pos.z},
  29. {x=pos.x+1,y=pos.y,z=pos.z},
  30. {x=pos.x,y=pos.y,z=pos.z-1},
  31. {x=pos.x,y=pos.y,z=pos.z+1},
  32. }
  33. local newnode = false
  34. local source = false
  35. for i = 1, 6 do
  36. if newnode then break end
  37. local testnode = minetest.get_node(coords[i])
  38. local name = testnode.name
  39. if name and (name == "pipeworks:pump_on" and pipeworks.check_for_liquids(coords[i])) or string.find(name,"_loaded") then
  40. if string.find(name,"_loaded") then
  41. source = minetest.get_meta(coords[i]):get_string("source")
  42. if source == minetest.pos_to_string(pos) then break end
  43. end
  44. if string.find(name, "valve") or string.find(name, "sensor")
  45. or string.find(name, "straight_pipe") or string.find(name, "panel") then
  46. if ((i == 3 or i == 4) and minetest.facedir_to_dir(testnode.param2).x ~= 0)
  47. or ((i == 5 or i == 6) and minetest.facedir_to_dir(testnode.param2).z ~= 0)
  48. or ((i == 1 or i == 2) and minetest.facedir_to_dir(testnode.param2).y ~= 0) then
  49. newnode = string.gsub(node.name,"empty","loaded")
  50. source = {x=coords[i].x,y=coords[i].y,z=coords[i].z}
  51. end
  52. else
  53. newnode = string.gsub(node.name,"empty","loaded")
  54. source = {x=coords[i].x,y=coords[i].y,z=coords[i].z}
  55. end
  56. end
  57. end
  58. if newnode then
  59. minetest.add_node(pos,{name=newnode, param2 = node.param2})
  60. minetest.get_meta(pos):set_string("source",minetest.pos_to_string(source))
  61. end
  62. end
  63. pipeworks.check_sources = function(pos,node)
  64. local sourcepos = minetest.string_to_pos(minetest.get_meta(pos):get_string("source"))
  65. if not sourcepos then return end
  66. local source = minetest.get_node(sourcepos).name
  67. local newnode = false
  68. if source and not ((source == "pipeworks:pump_on" and pipeworks.check_for_liquids(sourcepos)) or string.find(source,"_loaded") or source == "ignore" ) then
  69. newnode = string.gsub(node.name,"loaded","empty")
  70. end
  71. if newnode then
  72. minetest.add_node(pos,{name=newnode, param2 = node.param2})
  73. minetest.get_meta(pos):set_string("source","")
  74. end
  75. end
  76. pipeworks.spigot_check = function(pos, node)
  77. local belowname = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name
  78. if belowname and (belowname == "air" or belowname == "default:water_flowing" or belowname == "default:water_source") then
  79. local spigotname = minetest.get_node(pos).name
  80. local fdir=node.param2 % 4
  81. local check = {
  82. {x=pos.x,y=pos.y,z=pos.z+1},
  83. {x=pos.x+1,y=pos.y,z=pos.z},
  84. {x=pos.x,y=pos.y,z=pos.z-1},
  85. {x=pos.x-1,y=pos.y,z=pos.z}
  86. }
  87. local near_node = minetest.get_node(check[fdir+1])
  88. if near_node and string.find(near_node.name, "_loaded") then
  89. if spigotname and spigotname == "pipeworks:spigot" then
  90. minetest.add_node(pos,{name = "pipeworks:spigot_pouring", param2 = fdir})
  91. if finitewater or belowname ~= "default:water_source" then
  92. minetest.add_node({x=pos.x,y=pos.y-1,z=pos.z},{name = "default:water_source"})
  93. end
  94. end
  95. else
  96. if spigotname == "pipeworks:spigot_pouring" then
  97. minetest.add_node({x=pos.x,y=pos.y,z=pos.z},{name = "pipeworks:spigot", param2 = fdir})
  98. if belowname == "default:water_source" and not finitewater then
  99. minetest.remove_node({x=pos.x,y=pos.y-1,z=pos.z})
  100. end
  101. end
  102. end
  103. end
  104. end
  105. pipeworks.fountainhead_check = function(pos, node)
  106. local abovename = minetest.get_node({x=pos.x,y=pos.y+1,z=pos.z}).name
  107. if abovename and (abovename == "air" or abovename == "default:water_flowing" or abovename == "default:water_source") then
  108. local fountainhead_name = minetest.get_node(pos).name
  109. local near_node = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z})
  110. if near_node and string.find(near_node.name, "_loaded") then
  111. if fountainhead_name and fountainhead_name == "pipeworks:fountainhead" then
  112. minetest.add_node(pos,{name = "pipeworks:fountainhead_pouring"})
  113. if finitewater or abovename ~= "default:water_source" then
  114. minetest.add_node({x=pos.x,y=pos.y+1,z=pos.z},{name = "default:water_source"})
  115. end
  116. end
  117. else
  118. if fountainhead_name == "pipeworks:fountainhead_pouring" then
  119. minetest.add_node({x=pos.x,y=pos.y,z=pos.z},{name = "pipeworks:fountainhead"})
  120. if abovename == "default:water_source" and not finitewater then
  121. minetest.remove_node({x=pos.x,y=pos.y+1,z=pos.z})
  122. end
  123. end
  124. end
  125. end
  126. end