pump.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. local pump_formspec =
  2. "size[10,8;]" ..
  3. default.gui_bg ..
  4. default.gui_bg_img ..
  5. default.gui_slots ..
  6. "button[5,1;1,4;start;Start]" ..
  7. "list[current_player;main;0,3.85;8,1;]" ..
  8. "list[current_player;main;0,5.08;8,3;8]" ..
  9. "listring[context;main]" ..
  10. "listring[current_player;main]" ..
  11. default.get_hotbar_bg(0, 3.85)
  12. local pump_formspec_on =
  13. "size[10,8;]" ..
  14. default.gui_bg ..
  15. default.gui_bg_img ..
  16. default.gui_slots ..
  17. "button[5,1;1,4;stop;Stop]" ..
  18. "list[current_player;main;0,3.85;8,1;]" ..
  19. "list[current_player;main;0,5.08;8,3;8]" ..
  20. "listring[context;main]" ..
  21. "listring[current_player;main]" ..
  22. default.get_hotbar_bg(0, 3.85)
  23. local function swap_node(pos, name)
  24. local node = minetest.get_node(pos)
  25. if node.name == name then
  26. return
  27. end
  28. node.name = name
  29. minetest.swap_node(pos, node)
  30. end
  31. minetest.register_node("bitumen:pump", {
  32. description = "Pump",
  33. drawtype = "normal",
  34. tiles = {
  35. "default_steel_block.png^bitumen_arrow.png^[transformR90", "default_steel_block.png^bitumen_arrow.png^[transformR270",
  36. "default_steel_block.png^bitumen_arrow.png", "default_steel_block.png^bitumen_arrow.png^[transformR180",
  37. "default_copper_block.png", "default_tin_block.png",
  38. },
  39. is_ground_content = false,
  40. paramtype2 = "facedir",
  41. groups = {cracky = 1, petroleum_fixture=1, oddly_breakable_by_hand = 3},
  42. sounds = default.node_sound_glass_defaults(),
  43. on_place = minetest.rotate_node,
  44. on_construct = function(pos)
  45. local meta = minetest.get_meta(pos)
  46. meta:set_string("formspec", pump_formspec)
  47. --bitumen.pipes.on_construct(pos)
  48. end,
  49. --[[ not working, apparently due to an "undocumented feature" in the engine ;)
  50. on_receive_fields = function(pos, form, fields, player)
  51. local meta = minetest:get_meta(pos)
  52. local mf = meta:get_string("formspec")
  53. print(dump(mf).."\n")
  54. if fields.start then
  55. print("start")
  56. swap_node(pos, "bitumen:pump_on")
  57. -- refetch the meta after the node swap just to be sure
  58. local meta = minetest:get_meta(pos)
  59. meta:set_string("formspec", "")
  60. meta:set_string("formspec", pump_formspec_on)
  61. local mf = meta:get_string("formspec")
  62. print(dump(mf))
  63. minetest.show_formspec(player:get_player_name(), "", pump_formspec_on)
  64. end
  65. end,
  66. ]]
  67. on_punch = function(pos)
  68. swap_node(pos, "bitumen:pump_on")
  69. end,
  70. })
  71. minetest.register_node("bitumen:pump_on", {
  72. description = "Pump (Active)",
  73. drawtype = "normal",
  74. tiles = {
  75. "default_steel_block.png^bitumen_arrow_yellow.png^[transformR90", "default_steel_block.png^bitumen_arrow_yellow.png^[transformR270",
  76. "default_steel_block.png^bitumen_arrow_yellow.png", "default_steel_block.png^bitumen_arrow_yellow.png^[transformR180",
  77. "default_copper_block.png", "default_tin_block.png",
  78. },
  79. is_ground_content = false,
  80. paramtype2 = "facedir",
  81. groups = {cracky = 1, petroleum_fixture=1, oddly_breakable_by_hand = 3, --[[not_in_creaetive_inventory=1]]},
  82. sounds = default.node_sound_glass_defaults(),
  83. on_place = minetest.rotate_node,
  84. drop = "bitumen:pump",
  85. on_construct = function(pos)
  86. local meta = minetest.get_meta(pos)
  87. meta:set_string("formspec", pump_formspec_on)
  88. --bitumen.pipes.on_construct(pos)
  89. end,
  90. --[[ not working, apparently due to an "undocumented feature" in the engine ;)
  91. on_receive_fields = function(pos, form, fields, player)
  92. if fields.stop then
  93. print("stop")
  94. swap_node(pos, {name="bitumen:pump"})
  95. local meta = minetest:get_meta(pos)
  96. meta:set_string("formspec", pump_formspec)
  97. minetest.show_formspec(player:get_player_name(), "", pump_formspec)
  98. end
  99. end,
  100. ]]
  101. on_punch = function(pos)
  102. swap_node(pos, "bitumen:pump")
  103. end,
  104. })
  105. minetest.register_abm({
  106. nodenames = {"bitumen:pump_on"},
  107. interval = 1,
  108. chance = 1,
  109. action = function(pos, node, active_object_count, active_object_count_wider)
  110. local node = minetest.get_node(pos)
  111. local back_dir = minetest.facedir_to_dir(node.param2)
  112. local backpos = vector.add(pos, back_dir)
  113. local backnet = bitumen.pipes.get_net(backpos)
  114. if backnet == nil then
  115. print("bpump no backnet at "..minetest.pos_to_string(backpos))
  116. return
  117. end
  118. local front_dir = vector.multiply(back_dir, -1)
  119. local frontpos = vector.add(pos, front_dir)
  120. local frontnet = bitumen.pipes.get_net(frontpos)
  121. if frontnet == nil then
  122. print("bpump no frontnet at "..minetest.pos_to_string(frontpos))
  123. return
  124. end
  125. if backnet.fluid ~= frontnet.fluid and backnet.fluid ~= "air" then
  126. print("bpump: bad_fluid")
  127. return -- incompatible fluids
  128. end
  129. local lift = 25
  130. --print("fpos ".. minetest.pos_to_string(frontpos) .. " | bpos "..minetest.pos_to_string(backpos))
  131. --print("fp ".. frontnet.in_pressure .. " | bp "..backnet.in_pressure)
  132. -- HACK: hardcoded buffer amount
  133. local to_take = math.min(20, 64 - backnet.buffer)
  134. if to_take == 0 then
  135. -- print("pump: output pipe full")
  136. return
  137. end
  138. local taken, fluid = bitumen.pipes.take_fluid(frontpos, to_take)
  139. local pushed = bitumen.pipes.push_fluid(backpos, fluid, taken, lift)
  140. -- print("bitumen pumped " ..taken .. " > "..pushed)
  141. if pushed < taken then
  142. print("bitumen pump leaked ".. (taken - pushed) .. " at " ..pos.x..","..pos.y..","..pos.z)
  143. end
  144. --print("")
  145. end
  146. })
  147. -- gas-powered pump
  148. minetest.register_node("bitumen:pump_gas", {
  149. description = "Gas-powered Pump",
  150. drawtype = "normal",
  151. tiles = {
  152. "default_steel_block.png^default_obsidian_glass.png^bitumen_arrow.png^[transformR90", "default_steel_block.png^default_obsidian_glass.png^bitumen_arrow.png^[transformR270",
  153. "default_steel_block.png^default_obsidian_glass.png^bitumen_arrow.png", "default_steel_block.png^default_obsidian_glass.png^bitumen_arrow.png^[transformR180",
  154. "default_copper_block.png^default_obsidian_glass.png", "default_tin_block.png^default_obsidian_glass.png",
  155. },
  156. is_ground_content = false,
  157. paramtype2 = "facedir",
  158. groups = {cracky = 1, petroleum_fixture=1, oddly_breakable_by_hand = 3},
  159. sounds = default.node_sound_glass_defaults(),
  160. on_place = minetest.rotate_node,
  161. on_construct = function(pos)
  162. local meta = minetest.get_meta(pos)
  163. meta:set_string("formspec", pump_formspec)
  164. --bitumen.pipes.on_construct(pos)
  165. end,
  166. --[[ not working, apparently due to an "undocumented feature" in the engine ;)
  167. on_receive_fields = function(pos, form, fields, player)
  168. local meta = minetest:get_meta(pos)
  169. local mf = meta:get_string("formspec")
  170. print(dump(mf).."\n")
  171. if fields.start then
  172. print("start")
  173. swap_node(pos, "bitumen:pump_on")
  174. -- refetch the meta after the node swap just to be sure
  175. local meta = minetest:get_meta(pos)
  176. meta:set_string("formspec", "")
  177. meta:set_string("formspec", pump_formspec_on)
  178. local mf = meta:get_string("formspec")
  179. print(dump(mf))
  180. minetest.show_formspec(player:get_player_name(), "", pump_formspec_on)
  181. end
  182. end,
  183. ]]
  184. on_punch = function(pos)
  185. swap_node(pos, "bitumen:pump_gas_on")
  186. end,
  187. })
  188. minetest.register_node("bitumen:pump_gas_on", {
  189. description = "Gas-powered Pump (Active)",
  190. drawtype = "normal",
  191. tiles = {
  192. "default_steel_block.png^default_obsidian_glass.png^bitumen_arrow_yellow.png^[transformR90", "default_steel_block.png^default_obsidian_glass.png^bitumen_arrow_yellow.png^[transformR270",
  193. "default_steel_block.png^default_obsidian_glass.png^bitumen_arrow_yellow.png", "default_steel_block.png^default_obsidian_glass.png^bitumen_arrow_yellow.png^[transformR180",
  194. "default_copper_block.png^default_obsidian_glass.png", "default_tin_block.png^default_obsidian_glass.png",
  195. },
  196. is_ground_content = false,
  197. paramtype2 = "facedir",
  198. groups = {cracky = 1, petroleum_fixture=1, oddly_breakable_by_hand = 3, --[[not_in_creaetive_inventory=1]]},
  199. sounds = default.node_sound_glass_defaults(),
  200. on_place = minetest.rotate_node,
  201. drop = "bitumen:pump_gas",
  202. on_construct = function(pos)
  203. local meta = minetest.get_meta(pos)
  204. meta:set_string("formspec", pump_formspec_on)
  205. --bitumen.pipes.on_construct(pos)
  206. end,
  207. --[[ not working, apparently due to an "undocumented feature" in the engine ;)
  208. on_receive_fields = function(pos, form, fields, player)
  209. if fields.stop then
  210. print("stop")
  211. swap_node(pos, {name="bitumen:pump"})
  212. local meta = minetest:get_meta(pos)
  213. meta:set_string("formspec", pump_formspec)
  214. minetest.show_formspec(player:get_player_name(), "", pump_formspec)
  215. end
  216. end,
  217. ]]
  218. on_punch = function(pos)
  219. swap_node(pos, "bitumen:pump_gas")
  220. end,
  221. })
  222. minetest.register_abm({
  223. nodenames = {"bitumen:pump_gas_on"},
  224. interval = 1,
  225. chance = 1,
  226. action = function(pos)
  227. local node = minetest.get_node(pos)
  228. local meta = minetest.get_meta(pos)
  229. local fuel = meta:get_int("fuel")
  230. -- print("pump fuel "..fuel)
  231. if fuel <= 0 then
  232. local below = {x=pos.x, y=pos.y-1, z=pos.z}
  233. local bnode = minetest.get_node(below)
  234. if bnode.name == "bitumen:oil_drum" then
  235. local bmeta = minetest.get_meta(below)
  236. local fluid = bmeta:get_string("fluid")
  237. if fluid == "bitumen:gasoline" then
  238. local fill = bmeta:get_int("fill")
  239. print("pump fill "..fill)
  240. if fill >= 1 then
  241. fill = fill - 1
  242. bmeta:set_int("fill", fill)
  243. local max_fill = bmeta:get_int("maxfill")
  244. bmeta:set_string("infotext", fluid .." (".. math.floor(((fill*100)/max_fill)+0.5) .."%)")
  245. fuel = 60
  246. end
  247. end
  248. end
  249. end
  250. if fuel <= 0 then
  251. -- minetest.set_node(pos, {name="bitumen:pump_gas"})
  252. print("pump out of gas " .. fuel)
  253. swap_node(pos, "bitumen:pump_gas")
  254. return
  255. else
  256. fuel = fuel - 1
  257. meta:set_int("fuel", fuel)
  258. end
  259. local back_dir = minetest.facedir_to_dir(node.param2)
  260. local backpos = vector.add(pos, back_dir)
  261. local backnet = bitumen.pipes.get_net(backpos)
  262. if backnet == nil then
  263. print("bpump no backnet at "..minetest.pos_to_string(backpos))
  264. return
  265. end
  266. local front_dir = vector.multiply(back_dir, -1)
  267. local frontpos = vector.add(pos, front_dir)
  268. local frontnet = bitumen.pipes.get_net(frontpos)
  269. if frontnet == nil then
  270. print("bpump no frontnet at "..minetest.pos_to_string(frontpos))
  271. return
  272. end
  273. if backnet.fluid ~= frontnet.fluid and backnet.fluid ~= "air" then
  274. print("bpump: bad_fluid")
  275. return -- incompatible fluids
  276. end
  277. local lift = 50
  278. --print("fpos ".. minetest.pos_to_string(frontpos) .. " | bpos "..minetest.pos_to_string(backpos))
  279. --print("fp ".. frontnet.in_pressure .. " | bp "..backnet.in_pressure)
  280. -- HACK: hardcoded buffer amount
  281. local to_take = 64 - backnet.buffer
  282. if to_take <= 0 then
  283. -- print("pump: output pipe full")
  284. return
  285. end
  286. local taken, fluid = bitumen.pipes.take_fluid(frontpos, to_take)
  287. local pushed = bitumen.pipes.push_fluid(backpos, fluid, taken, lift)
  288. -- print("bitumen pumped " ..taken .. " > "..pushed)
  289. if pushed < taken then
  290. print("bitumen pump leaked ".. (taken - pushed) .. " at " ..pos.x..","..pos.y..","..pos.z)
  291. end
  292. --print("")
  293. end
  294. })