lights.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. local loffs = {
  10. {x=0, y=4, z=4},
  11. {x=0, y=4, z=-4},
  12. {x=4, y=4, z=0},
  13. {x=-4, y=4, z=0},
  14. {x=7, y=8, z=7},
  15. {x=7, y=8, z=-7},
  16. {x=-7, y=8, z=7},
  17. {x=-7, y=8, z=-7},
  18. {x=9, y=8, z=0},
  19. {x=-9, y=8, z=0},
  20. {x=0, y=8, z=-9},
  21. {x=0, y=8, z=-9},
  22. {x=0, y=16, z=0},
  23. }
  24. local function destruct_light(pos)
  25. for _,v in ipairs(loffs) do
  26. local p = vector.add(pos, v)
  27. local n = minetest.get_node(p)
  28. if n.name == "bitumen:magic_light" then
  29. minetest.set_node(p, {name="air"})
  30. end
  31. end
  32. end
  33. local function try_turn_on(pos)
  34. local bpos = {x=pos.x, y=pos.y - 1, z=pos.z}
  35. local bnode = minetest.get_node(bpos)
  36. local bmeta = minetest.env:get_meta(bpos)
  37. if not bmeta or bnode.name ~= "bitumen:oil_drum" then
  38. swap_node(pos, "bitumen:kerosene_light")
  39. destruct_light(pos)
  40. return
  41. end
  42. local fluid = bmeta:get_string("fluid")
  43. local fill = bmeta:get_int("fill")
  44. local max_fill = bmeta:get_int("maxfill")
  45. if not fill or fill == 0 then
  46. swap_node(pos, "bitumen:kerosene_light")
  47. destruct_light(pos)
  48. return
  49. end
  50. local taken = 1
  51. -- turn on
  52. for _,v in ipairs(loffs) do
  53. local p = vector.add(pos, v)
  54. local n = minetest.get_node(p)
  55. if n.name == "air" then
  56. minetest.set_node(p, {name="bitumen:magic_light"})
  57. -- else
  58. end
  59. end
  60. bmeta:set_float("fill", math.max(fill - taken, 0))
  61. bmeta:set_string("infotext", fluid .." (".. math.floor(((fill-taken)*100/max_fill)+0.5) .."%)")
  62. end
  63. minetest.register_node("bitumen:kerosene_light", {
  64. description = "Kerosene Light",
  65. drawtype = "nodebox",
  66. node_box = {
  67. type = "fixed",
  68. fixed = {
  69. { -.1, -.4, -.1, .1, .5, .1},
  70. { -.4, -.5, -.4, .4, -.3, .4},
  71. { -.4, .5, -.4, .41, .51, .41},
  72. },
  73. },
  74. selection_box = {
  75. type = "fixed",
  76. fixed = {
  77. {-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
  78. },
  79. },
  80. paramtype = "light",
  81. is_ground_content = false,
  82. tiles = { "default_wood.png" },
  83. walkable = true,
  84. groups = { cracky = 3, petroleum_fixture = 1 },
  85. light_source = 1,
  86. on_punch = function(pos)
  87. swap_node(pos, "bitumen:kerosene_light_on")
  88. try_turn_on(pos)
  89. end,
  90. on_destruct = destruct_light,
  91. })
  92. minetest.register_node("bitumen:kerosene_light_on", {
  93. description = "Kerosene Light",
  94. drawtype = "nodebox",
  95. node_box = {
  96. type = "fixed",
  97. fixed = {
  98. { -.1, -.4, -.1, .1, .5, .1},
  99. { -.4, -.5, -.4, .4, -.3, .4},
  100. },
  101. },
  102. selection_box = {
  103. type = "fixed",
  104. fixed = {
  105. {-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
  106. },
  107. },
  108. paramtype = "light",
  109. is_ground_content = false,
  110. tiles = { "default_meselamp.png" },
  111. walkable = true,
  112. groups = { cracky = 3, petroleum_fixture = 1 },
  113. light_source = default.LIGHT_MAX,
  114. on_punch = function(pos)
  115. swap_node(pos, "bitumen:kerosene_light")
  116. destruct_light(pos)
  117. end,
  118. on_destruct = destruct_light,
  119. })
  120. minetest.register_node("bitumen:magic_light", {
  121. description = "Hidden Magic Light",
  122. drawtype = "airlike",
  123. -- tiles = {"default_mese.png"},
  124. paramtype = "light",
  125. sunlight_propagates = true,
  126. is_ground_content = false,
  127. light_source = default.LIGHT_MAX,
  128. })
  129. minetest.register_abm({
  130. nodenames = {"bitumen:kerosene_light_on"},
  131. -- neighbors = {"bitumen:oil_drum"},
  132. interval = 60,
  133. chance = 1,
  134. action = function(pos, node)
  135. try_turn_on(pos)
  136. end,
  137. })
  138. --[[ cleanup failsafe
  139. minetest.register_abm({
  140. nodenames = {"bitumen:magic_light"},
  141. interval = 30,
  142. chance = 1,
  143. action = function(pos, node)
  144. minetest.set_node(pos, {name="air"})
  145. end,
  146. })
  147. ]]