rock_crusher.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. local function swap_node(pos, name)
  2. local node = minetest.get_node(pos)
  3. if node.name == name then
  4. return
  5. end
  6. node.name = name
  7. minetest.swap_node(pos, node)
  8. end
  9. --[[
  10. local function try_turn_on(pos)
  11. local bpos = {x=pos.x, y=pos.y - 1, z=pos.z}
  12. local bnode = minetest.get_node(bpos)
  13. local bmeta = minetest.env:get_meta(bpos)
  14. if not bmeta or bnode.name ~= "bitumen:oil_drum" then
  15. swap_node(pos, "bitumen:kerosene_light")
  16. destruct_light(pos)
  17. return
  18. end
  19. local fluid = bmeta:get_string("fluid")
  20. local fill = bmeta:get_int("fill")
  21. local max_fill = bmeta:get_int("maxfill")
  22. if not fill or fill == 0 then
  23. swap_node(pos, "bitumen:kerosene_light")
  24. destruct_light(pos)
  25. return
  26. end
  27. local taken = 1
  28. -- turn on
  29. for _,v in ipairs(loffs) do
  30. local p = vector.add(pos, v)
  31. local n = minetest.get_node(p)
  32. if n.name == "air" then
  33. minetest.set_node(p, {name="bitumen:magic_light"})
  34. -- else
  35. end
  36. end
  37. bmeta:set_float("fill", math.max(fill - taken, 0))
  38. bmeta:set_string("infotext", fluid .." (".. math.floor(((fill-taken)*100/max_fill)+0.5) .."%)")
  39. end
  40. ]]
  41. local rock_crusher_formspec =
  42. "size[10,9;]" ..
  43. default.gui_bg ..
  44. default.gui_bg_img ..
  45. default.gui_slots ..
  46. "list[context;main;0,0.3;5,4;]" ..
  47. "list[current_player;main;0,4.85;8,1;]" ..
  48. "list[current_player;main;0,6.08;8,3;8]" ..
  49. "listring[context;main]" ..
  50. "listring[current_player;main]" ..
  51. default.get_hotbar_bg(0, 4.85)
  52. local function take_gas(itemstack, amount)
  53. if itemstack:get_name() ~= "bitumen:oil_drum_filled" then
  54. return false
  55. end
  56. local smeta = itemstack:get_meta()
  57. if smeta:get_string("fluid") ~= "bitumen:gasoline" then
  58. return false
  59. end
  60. local fill = smeta:get_float("fill")
  61. if fill < amount then
  62. return false
  63. end
  64. smeta:set_float("fill", fill - amount)
  65. return true
  66. end
  67. function crushtimer(pos, elapsed)
  68. local meta = minetest.get_meta(pos)
  69. local fuel = meta:get_float("fuel") or 0.0
  70. if fuel <= 0 then
  71. -- try to get some fuel
  72. local inv = meta:get_inventory()
  73. local st = inv:get_stack("main", 1)
  74. if take_gas(st, 1) then
  75. inv:set_stack("main", 1, st);
  76. fuel = fuel + 1
  77. else
  78. -- out of fuel, turn off
  79. return false
  80. end
  81. end
  82. fuel = fuel - .1
  83. meta:set_float("fuel", fuel)
  84. -- try to grind some rocks
  85. pos.y = pos.y + 1
  86. -- local tnode = minetest.get_node(pos)
  87. local tmeta = minetest.get_meta(pos)
  88. local tinv = tmeta:get_inventory()
  89. local cob = tinv:remove_item("main", "default:cobble 1")
  90. if cob:get_count() <= 0 then
  91. cob = tinv:remove_item("main", "default:desert_cobble 1")
  92. end
  93. if cob:get_count() > 0 then
  94. pos.y = pos.y - 2
  95. local bmeta = minetest.get_meta(pos)
  96. local binv = bmeta:get_inventory()
  97. binv:add_item("main", "default:gravel 1")
  98. end
  99. return true
  100. end
  101. minetest.register_node("bitumen:rock_crusher", {
  102. description = "Small Gas Rock Crusher",
  103. drawtype = "nodebox",
  104. node_box = {
  105. type = "fixed",
  106. fixed = {
  107. { -.5, -.5, -.5, .5, .5, .5},
  108. { -.7, -.3, -.7, .7, .3, .7},
  109. },
  110. },
  111. selection_box = {
  112. type = "fixed",
  113. fixed = {
  114. {-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
  115. },
  116. },
  117. paramtype = "light",
  118. is_ground_content = false,
  119. tiles = { "default_wood.png" },
  120. walkable = true,
  121. groups = { cracky = 3, },
  122. on_construct = function(pos)
  123. local meta = minetest.get_meta(pos)
  124. local inv = meta:get_inventory()
  125. inv:set_size("main", 1)
  126. meta:set_string("formspec", rock_crusher_formspec);
  127. end,
  128. on_punch = function(pos)
  129. swap_node(pos, "bitumen:rock_crusher_on")
  130. minetest.get_node_timer(pos):start(2.0)
  131. --try_turn_on(pos)
  132. end,
  133. on_timer = function(pos)
  134. return false
  135. end,
  136. })
  137. minetest.register_node("bitumen:rock_crusher_on", {
  138. description = "Small Gas Rock Crusher",
  139. drawtype = "nodebox",
  140. node_box = {
  141. type = "fixed",
  142. fixed = {
  143. { -.5, -.5, -.5, .5, .5, .5},
  144. { -.7, -.3, -.7, .7, .3, .7},
  145. },
  146. },
  147. selection_box = {
  148. type = "fixed",
  149. fixed = {
  150. {-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
  151. },
  152. },
  153. paramtype = "light",
  154. is_ground_content = false,
  155. tiles = { "default_meselamp.png" },
  156. walkable = true,
  157. groups = { cracky = 3, petroleum_fixture = 1 },
  158. on_construct = function(pos)
  159. local meta = minetest.get_meta(pos)
  160. local inv = meta:get_inventory()
  161. inv:set_size("main", 1)
  162. meta:set_string("formspec", rock_crusher_formspec);
  163. end,
  164. on_punch = function(pos)
  165. swap_node(pos, "bitumen:rock_crusher")
  166. -- destruct_light(pos)
  167. end,
  168. on_timer = crushtimer,
  169. })
  170. bitumen.register_blueprint({
  171. name="bitumen:rock_crusher",
  172. no_constructor_craft = true,
  173. })