pump.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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("springs:pump", {
  32. description = "Pump",
  33. drawtype = "normal",
  34. tiles = {"default_steel_block.png"},
  35. is_ground_content = false,
  36. paramtype2 = "facedir",
  37. groups = {cracky = 1, petroleum_fixture=1, oddly_breakable_by_hand = 3},
  38. sounds = default.node_sound_glass_defaults(),
  39. on_place = minetest.rotate_node,
  40. on_construct = function(pos)
  41. local meta = minetest.get_meta(pos)
  42. meta:set_string("formspec", pump_formspec)
  43. --springs.pipes.on_construct(pos)
  44. end,
  45. --[[ not working, apparently due to an "undocumented feature" in the engine ;)
  46. on_receive_fields = function(pos, form, fields, player)
  47. local meta = minetest:get_meta(pos)
  48. local mf = meta:get_string("formspec")
  49. print(dump(mf).."\n")
  50. if fields.start then
  51. print("start")
  52. swap_node(pos, "springs:pump_on")
  53. -- refetch the meta after the node swap just to be sure
  54. local meta = minetest:get_meta(pos)
  55. meta:set_string("formspec", "")
  56. meta:set_string("formspec", pump_formspec_on)
  57. local mf = meta:get_string("formspec")
  58. print(dump(mf))
  59. minetest.show_formspec(player:get_player_name(), "", pump_formspec_on)
  60. end
  61. end,
  62. ]]
  63. on_punch = function(pos)
  64. swap_node(pos, "springs:pump_on")
  65. end,
  66. })
  67. minetest.register_node("springs:pump_on", {
  68. description = "Pump (Active)",
  69. drawtype = "normal",
  70. tiles = {"default_tin_block.png"},
  71. is_ground_content = false,
  72. paramtype2 = "facedir",
  73. groups = {cracky = 1, petroleum_fixture=1, oddly_breakable_by_hand = 3, --[[not_in_creaetive_inventory=1]]},
  74. sounds = default.node_sound_glass_defaults(),
  75. on_place = minetest.rotate_node,
  76. drop = "springs:pump",
  77. on_construct = function(pos)
  78. local meta = minetest.get_meta(pos)
  79. meta:set_string("formspec", pump_formspec_on)
  80. --springs.pipes.on_construct(pos)
  81. end,
  82. --[[ not working, apparently due to an "undocumented feature" in the engine ;)
  83. on_receive_fields = function(pos, form, fields, player)
  84. if fields.stop then
  85. print("stop")
  86. swap_node(pos, {name="springs:pump"})
  87. local meta = minetest:get_meta(pos)
  88. meta:set_string("formspec", pump_formspec)
  89. minetest.show_formspec(player:get_player_name(), "", pump_formspec)
  90. end
  91. end,
  92. ]]
  93. on_punch = function(pos)
  94. swap_node(pos, "springs:pump")
  95. end,
  96. })
  97. minetest.register_abm({
  98. nodenames = {"springs:pump_on"},
  99. interval = 1,
  100. chance = 1,
  101. action = function(pos, node, active_object_count, active_object_count_wider)
  102. local node = minetest.get_node(pos)
  103. local back_dir = minetest.facedir_to_dir(node.param2)
  104. local backpos = vector.add(pos, back_dir)
  105. local backnet = springs.pipes.get_net(backpos)
  106. if backnet == nil then
  107. print("pump no backnet at "..minetest.pos_to_string(backpos))
  108. return
  109. end
  110. local front_dir = vector.multiply(back_dir, -1)
  111. local frontpos = vector.add(pos, front_dir)
  112. local frontnet = springs.pipes.get_net(frontpos)
  113. if frontnet == nil then
  114. print("pump no frontnet at "..minetest.pos_to_string(frontpos))
  115. return
  116. end
  117. -- if backnet.fluid ~= frontnet.fluid and backnet.fluid ~= "air" then
  118. -- print("pump: bad_fluid")
  119. -- return -- incompatible fluids
  120. -- end
  121. local lift = 70
  122. --print("fpos ".. minetest.pos_to_string(frontpos) .. " | bpos "..minetest.pos_to_string(backpos))
  123. --print("fp ".. frontnet.in_pressure .. " | bp "..backnet.in_pressure)
  124. local taken, fluid = springs.pipes.take_fluid(frontpos, 10, 0, 2)
  125. local pushed = springs.pipes.push_fluid(backpos, fluid, taken, lift)
  126. --print("pumped " ..taken .. " > "..pushed)
  127. -- if pushed < taken then
  128. -- print("pump leaked ".. (taken - pushed))
  129. -- end
  130. --print("")
  131. end
  132. })
  133. minetest.register_craft({
  134. output = "springs:pump 1",
  135. recipe = {
  136. {"", "", ""},
  137. {"springs:pipe", "default:furnace", "springs:pipe"},
  138. {"", "", ""},
  139. }
  140. })