init.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. -- Maximum number of light nodes above beacon (or 0 for no max)
  2. local beacon_height = tonumber(minetest.settings:get("beacon_height")) or 0
  3. local function get_node_under_light(pos)
  4. pos = table.copy(pos)
  5. local node = minetest.get_node(pos)
  6. local y_dist = 0
  7. while minetest.get_item_group(node.name, "beacon_light") ~= 0 and (beacon_height == 0 or y_dist <= beacon_height) do
  8. node = minetest.get_node(vector.subtract(pos, {x = 0, y = y_dist, z = 0}))
  9. y_dist = y_dist + 1
  10. end
  11. return node
  12. end
  13. local function beacon_on_construct(pos, color)
  14. local above = vector.add(pos, {x = 0, y = 1, z = 0})
  15. if get_node_under_light(pos).name == ("beacons:base_" .. color) and minetest.get_node(above).name == "air" then
  16. minetest.set_node(above, {name = "beacons:light_" .. color})
  17. end
  18. end
  19. local function beacon_on_destruct(pos, color)
  20. local above = vector.add(pos, {x = 0, y = 1, z = 0})
  21. if minetest.get_node(above).name == ("beacons:light_" .. color) then
  22. minetest.remove_node(above)
  23. end
  24. end
  25. local function beacon_on_timer(pos, color)
  26. beacon_on_construct(pos, color)
  27. if get_node_under_light(pos).name ~= ("beacons:base_" .. color) then
  28. minetest.remove_node(pos)
  29. end
  30. return true
  31. end
  32. local function make_on_construct(color)
  33. return function(pos)
  34. minetest.get_node_timer(pos):start(1)
  35. return beacon_on_construct(pos, color)
  36. end
  37. end
  38. local function make_on_destruct(color)
  39. return function(pos)
  40. return beacon_on_destruct(pos, color)
  41. end
  42. end
  43. local function make_on_timer(color)
  44. return function(pos)
  45. return beacon_on_timer(pos, color)
  46. end
  47. end
  48. local color_descs = {
  49. white = "White",
  50. red = "Red",
  51. yellow = "Yellow",
  52. green = "Green",
  53. cyan = "Cyan",
  54. blue = "Blue",
  55. magenta = "Magenta",
  56. orange = "Orange",
  57. violet = "Violet",
  58. }
  59. for color, color_desc in pairs(color_descs) do
  60. minetest.register_node("beacons:base_" .. color, {
  61. description = color_desc .. " Beacon",
  62. tiles = {"beacons_base_top.png", "beacons_base_bottom.png", "beacons_base_side.png"},
  63. color = color:gsub("_", ""),
  64. groups = {beacon_base = 1, cracky = 2},
  65. is_ground_content = false,
  66. paramtype = "light",
  67. on_rotate = function() end,
  68. on_construct = make_on_construct(color),
  69. on_destruct = make_on_destruct(color),
  70. on_timer = make_on_timer(color),
  71. })
  72. if minetest.get_modpath("default") then
  73. minetest.override_item("beacons:base_" .. color, {sounds = default.node_sound_metal_defaults()})
  74. if minetest.get_modpath("dye") then
  75. local color_group
  76. if color == "magenta" then
  77. color_group = "excolor_red_violet"
  78. else
  79. color_group = "excolor_" .. color
  80. end
  81. minetest.register_craft({
  82. output = "beacons:base_" .. color,
  83. recipe = {
  84. {"default:steel_ingot", "group:" .. color_group, "default:steel_ingot",},
  85. {"default:steel_ingot", "default:meselamp", "default:steel_ingot",},
  86. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot",},
  87. },
  88. })
  89. minetest.register_craft({
  90. type = "shapeless",
  91. output = "beacons:base_" .. color,
  92. recipe = {"group:beacon_base", "group:" .. color_group},
  93. })
  94. end
  95. end
  96. minetest.register_node("beacons:light_" .. color, {
  97. description = color_desc .. " Beacon Light",
  98. drawtype = "plantlike",
  99. tiles = {"beacons_light.png"},
  100. color = color:gsub("_", ""),
  101. paramtype = "light",
  102. sunlight_propagates = true,
  103. light_source = minetest.LIGHT_MAX,
  104. walkable = false,
  105. pointable = false,
  106. diggable = false,
  107. buildable_to = true,
  108. floodable = false,
  109. drop = "",
  110. groups = {beacon_light = 1, not_in_creative_inventory = 1},
  111. damage_per_second = 1,
  112. post_effect_color = color,
  113. on_blast = function() end,
  114. on_construct = make_on_construct(color),
  115. on_destruct = make_on_destruct(color),
  116. on_timer = make_on_timer(color),
  117. })
  118. end