autoplace_pipes.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. --[[
  2. autorouting for pipes
  3. To connect pipes to some node, include this in the node def...
  4. pipe_connections = {
  5. pattern = <string>, -- if supplied, search for this pattern instead of the exact node name
  6. left = <bool>, -- true (or 1) if the left side of the node needs to connect to a pipe
  7. right = <bool>, -- or from the right side, etc.
  8. top = <bool>,
  9. bottom = <bool>,
  10. front = <bool>,
  11. back = <bool>,
  12. left_param2 = <num>, -- the node must have this param2 to connect from the left
  13. right_param2 = <num>, -- or right, etc.
  14. top_param2 = <num>, -- Omit some or all of these to skip checking param2 for those sides
  15. bottom_param2 = <num>,
  16. front_param2 = <num>,
  17. back_param2 = <num>,
  18. },
  19. ...then add, pipeworks.scan_for_pipe_objects(pos)
  20. to your node's after_dig_node and after_place_node callbacks.
  21. ]]--
  22. -- get the axis dir (just 6 faces) of target node, assumes the pipe is the axis
  23. function pipeworks.get_axis_dir(nodetable, pattern)
  24. local pxm,pxp,pym,pyp,pzm,pzp
  25. if string.find(nodetable.nxm.name, pattern)
  26. and minetest.facedir_to_dir(nodetable.nxm.param2).x ~= 0 then
  27. pxm=1
  28. end
  29. if string.find(nodetable.nxp.name, pattern)
  30. and minetest.facedir_to_dir(nodetable.nxp.param2).x ~= 0 then
  31. pxp=1
  32. end
  33. if string.find(nodetable.nzm.name, pattern)
  34. and minetest.facedir_to_dir(nodetable.nzm.param2).z ~= 0 then
  35. pzm=1
  36. end
  37. if string.find(nodetable.nzp.name, pattern)
  38. and minetest.facedir_to_dir(nodetable.nzp.param2).z ~= 0 then
  39. pzp=1
  40. end
  41. if string.find(nodetable.nym.name, pattern)
  42. and minetest.facedir_to_dir(nodetable.nym.param2).y ~= 0 then
  43. pym=1
  44. end
  45. if string.find(nodetable.nyp.name, pattern)
  46. and minetest.facedir_to_dir(nodetable.nyp.param2).y ~= 0 then
  47. pyp=1
  48. end
  49. local match = pxm or pxp or pym or pyp or pzm or pzp
  50. return match,pxm,pxp,pym,pyp,pzm,pzp
  51. end
  52. local tube_table = {[0] = 1, 2, 2, 4, 2, 4, 4, 5, 2, 3, 4, 6, 4, 6, 5, 7, 2, 4, 3, 6, 4, 5, 6, 7, 4, 6, 6, 8, 5, 7, 7, 9, 2, 4, 4, 5, 3, 6, 6, 7, 4, 6, 5, 7, 6, 8, 7, 9, 4, 5, 6, 7, 6, 7, 8, 9, 5, 7, 7, 9, 7, 9, 9, 10}
  53. local tube_table_facedirs = {[0] = 0, 0, 5, 0, 3, 4, 3, 0, 2, 0, 2, 0, 6, 4, 3, 0, 7, 12, 5, 12, 7, 4, 5, 5, 18, 20, 16, 0, 7, 4, 7, 0, 1, 8, 1, 1, 1, 13, 1, 1, 10, 8, 2, 2, 17, 4, 3, 6, 9, 9, 9, 9, 21, 13, 1, 1, 10, 10, 11, 2, 19, 4, 3, 0}
  54. local function autoroute_pipes(pos)
  55. local nctr = minetest.get_node(pos)
  56. local state = "_empty"
  57. if (string.find(nctr.name, "pipeworks:pipe_") == nil) then return end
  58. if (string.find(nctr.name, "_loaded") ~= nil) then state = "_loaded" end
  59. local nsurround = pipeworks.scan_pipe_surroundings(pos)
  60. if nsurround == 0 then nsurround = 9 end
  61. minetest.swap_node(pos, {name = "pipeworks:pipe_"..tube_table[nsurround]..state,
  62. param2 = tube_table_facedirs[nsurround]})
  63. end
  64. function pipeworks.scan_for_pipe_objects(pos)
  65. autoroute_pipes({ x=pos.x-1, y=pos.y , z=pos.z })
  66. autoroute_pipes({ x=pos.x+1, y=pos.y , z=pos.z })
  67. autoroute_pipes({ x=pos.x , y=pos.y-1, z=pos.z })
  68. autoroute_pipes({ x=pos.x , y=pos.y+1, z=pos.z })
  69. autoroute_pipes({ x=pos.x , y=pos.y , z=pos.z-1 })
  70. autoroute_pipes({ x=pos.x , y=pos.y , z=pos.z+1 })
  71. autoroute_pipes(pos)
  72. end
  73. -- auto-rotation code for various devices the pipes attach to
  74. function pipeworks.scan_pipe_surroundings(pos)
  75. local pxm=0
  76. local pxp=0
  77. local pym=0
  78. local pyp=0
  79. local pzm=0
  80. local pzp=0
  81. local nxm = minetest.get_node({ x=pos.x-1, y=pos.y , z=pos.z })
  82. local nxp = minetest.get_node({ x=pos.x+1, y=pos.y , z=pos.z })
  83. local nym = minetest.get_node({ x=pos.x , y=pos.y-1, z=pos.z })
  84. local nyp = minetest.get_node({ x=pos.x , y=pos.y+1, z=pos.z })
  85. local nzm = minetest.get_node({ x=pos.x , y=pos.y , z=pos.z-1 })
  86. local nzp = minetest.get_node({ x=pos.x , y=pos.y , z=pos.z+1 })
  87. local nodetable = {
  88. nxm = nxm,
  89. nxp = nxp,
  90. nym = nym,
  91. nyp = nyp,
  92. nzm = nzm,
  93. nzp = nzp
  94. }
  95. -- standard handling for pipes...
  96. if string.find(nxm.name, "pipeworks:pipe_") then pxm=1 end
  97. if string.find(nxp.name, "pipeworks:pipe_") then pxp=1 end
  98. if string.find(nym.name, "pipeworks:pipe_") then pym=1 end
  99. if string.find(nyp.name, "pipeworks:pipe_") then pyp=1 end
  100. if string.find(nzm.name, "pipeworks:pipe_") then pzm=1 end
  101. if string.find(nzp.name, "pipeworks:pipe_") then pzp=1 end
  102. -- Special handling for valves...
  103. local match,a,b,c,d,e,f = pipeworks.get_axis_dir(nodetable, "pipeworks:valve")
  104. if match then
  105. pxm = a or pxm
  106. pxp = b or pxp
  107. pym = c or pym
  108. pyp = d or pyp
  109. pzm = e or pzm
  110. pzp = f or pzp
  111. end
  112. -- ...flow sensors...
  113. local match,a,b,c,d,e,f = pipeworks.get_axis_dir(nodetable, "pipeworks:flow_sensor")
  114. if match then
  115. pxm = a or pxm
  116. pxp = b or pxp
  117. pym = c or pym
  118. pyp = d or pyp
  119. pzm = e or pzm
  120. pzp = f or pzp
  121. end
  122. -- ...sealed pipe entry/exit...
  123. local match,a,b,c,d,e,f = pipeworks.get_axis_dir(nodetable, "pipeworks:entry_panel")
  124. if match then
  125. pxm = a or pxm
  126. pxp = b or pxp
  127. pym = c or pym
  128. pyp = d or pyp
  129. pzm = e or pzm
  130. pzp = f or pzp
  131. end
  132. -- ...straight-only pipe...
  133. local match,a,b,c,d,e,f = pipeworks.get_axis_dir(nodetable, "pipeworks:straight_pipe")
  134. if match then
  135. pxm = a or pxm
  136. pxp = b or pxp
  137. pym = c or pym
  138. pyp = d or pyp
  139. pzm = e or pzm
  140. pzp = f or pzp
  141. end
  142. -- ... other nodes
  143. local def_left = minetest.registered_nodes[nxp.name] -- the node that {pos} is to the left of (not the
  144. local def_right = minetest.registered_nodes[nxm.name] -- ...note that is AT the left!), etc.
  145. local def_bottom = minetest.registered_nodes[nyp.name]
  146. local def_top = minetest.registered_nodes[nym.name]
  147. local def_front = minetest.registered_nodes[nzp.name]
  148. local def_back = minetest.registered_nodes[nzm.name]
  149. if def_left and def_left.pipe_connections and def_left.pipe_connections.left
  150. and (not def_left.pipe_connections.pattern or string.find(nxp.name, def_left.pipe_connections.pattern))
  151. and (not def_left.pipe_connections.left_param2 or (nxp.param2 == def_left.pipe_connections.left_param2)) then
  152. pxp = 1
  153. end
  154. if def_right and def_right.pipe_connections and def_right.pipe_connections.right
  155. and (not def_right.pipe_connections.pattern or string.find(nxm.name, def_right.pipe_connections.pattern))
  156. and (not def_right.pipe_connections.right_param2 or (nxm.param2 == def_right.pipe_connections.right_param2)) then
  157. pxm = 1
  158. end
  159. if def_top and def_top.pipe_connections and def_top.pipe_connections.top
  160. and (not def_top.pipe_connections.pattern or string.find(nym.name, def_top.pipe_connections.pattern))
  161. and (not def_top.pipe_connections.top_param2 or (nym.param2 == def_top.pipe_connections.top_param2)) then
  162. pym = 1
  163. end
  164. if def_bottom and def_bottom.pipe_connections and def_bottom.pipe_connections.bottom
  165. and (not def_bottom.pipe_connections.pattern or string.find(nyp.name, def_bottom.pipe_connections.pattern))
  166. and (not def_bottom.pipe_connections.bottom_param2 or (nyp.param2 == def_bottom.pipe_connections.bottom_param2)) then
  167. pyp = 1
  168. end
  169. if def_front and def_front.pipe_connections and def_front.pipe_connections.front
  170. and (not def_front.pipe_connections.pattern or string.find(nzp.name, def_front.pipe_connections.pattern))
  171. and (not def_front.pipe_connections.front_param2 or (nzp.param2 == def_front.pipe_connections.front_param2)) then
  172. pzp = 1
  173. end
  174. if def_back and def_back.pipe_connections and def_back.pipe_connections.back
  175. and (not def_back.pipe_connections.pattern or string.find(nzm.name, def_back.pipe_connections.pattern))
  176. and (not def_back.pipe_connections.back_param2 or (nzm.param2 == def_back.pipe_connections.back_param2)) then
  177. pzm = 1
  178. end
  179. print("stage 2 returns "..pxm+8*pxp+2*pym+16*pyp+4*pzm+32*pzp..
  180. " for nodes surrounding "..minetest.get_node(pos).name.." at "..minetest.pos_to_string(pos))
  181. return pxm+8*pxp+2*pym+16*pyp+4*pzm+32*pzp
  182. end
  183. function pipeworks.look_for_stackable_tanks(pos)
  184. local tym = minetest.get_node({ x=pos.x , y=pos.y-1, z=pos.z })
  185. if string.find(tym.name, "pipeworks:storage_tank_") ~= nil or
  186. string.find(tym.name, "pipeworks:expansion_tank_") ~= nil then
  187. minetest.add_node(pos, { name = "pipeworks:expansion_tank_0", param2 = tym.param2})
  188. end
  189. end