init.lua 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. torchmelt = torchmelt or {}
  2. torchmelt.modpath = minetest.get_modpath("torchmelt")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. torchmelt.min_time = 15
  6. torchmelt.max_time = 80
  7. function torchmelt.start_melting(pos)
  8. local snow = minetest.find_node_near(pos, 2, snow.get_names())
  9. if snow then
  10. local air = minetest.find_node_near(pos, 1, "air")
  11. if air then
  12. minetest.set_node(air, {name="torchmelt:melter"})
  13. end
  14. end
  15. end
  16. function torchmelt.on_construct(pos)
  17. minetest.get_node_timer(pos):start(math_random(torchmelt.min_time, torchmelt.max_time))
  18. end
  19. local function torch_melt(pos)
  20. -- Perform the melting from the position of the heat source.
  21. local heat = minetest.find_node_near(pos, 1, "group:melt_around")
  22. if not heat then
  23. return false
  24. end
  25. local minp = vector.subtract(heat, 1)
  26. local maxp = vector.add(heat, 1)
  27. local snow = minetest.find_nodes_in_area(minp, maxp, snow.get_names())
  28. if #snow > 0 then
  29. -- Remove a random snow node.
  30. local p = snow[math_random(1, #snow)]
  31. minetest.remove_node(p)
  32. minetest.check_for_falling(p)
  33. return true
  34. end
  35. return false
  36. end
  37. function torchmelt.on_timer(pos, elapsed)
  38. if torch_melt(pos) then
  39. minetest.get_node_timer(pos):start(math_random(torchmelt.min_time, torchmelt.max_time))
  40. else
  41. minetest.get_node_timer(pos):stop()
  42. minetest.remove_node(pos) -- Remove the utility node.
  43. end
  44. end
  45. if not torchmelt.registered then
  46. minetest.register_node("torchmelt:melter", {
  47. drawtype = "airlike",
  48. description = "Torch Heat Source (Please Report to Admin)",
  49. paramtype = "light",
  50. sunlight_propagates = true,
  51. walkable = false,
  52. pointable = false,
  53. groups = {immovable = 1},
  54. climbable = false,
  55. buildable_to = true,
  56. floodable = true,
  57. drop = "",
  58. on_construct = function(...)
  59. return torchmelt.on_construct(...)
  60. end,
  61. on_timer = function(...)
  62. return torchmelt.on_timer(...)
  63. end,
  64. on_finish_collapse = function(pos, node)
  65. minetest.remove_node(pos)
  66. end,
  67. on_collapse_to_entity = function(pos, node)
  68. -- Do nothing.
  69. end,
  70. })
  71. local c = "torchmelt:core"
  72. local f = torchmelt.modpath .. "/init.lua"
  73. reload.register_file(c, f, false)
  74. torchmelt.registered = true
  75. end