init.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. --minetest.chat_send_player("MustTest", "# Server: Torchmelt @ " .. minetest.pos_to_string(pos) .. "!")
  39. if torch_melt(pos) then
  40. minetest.get_node_timer(pos):start(math_random(torchmelt.min_time, torchmelt.max_time))
  41. else
  42. --minetest.chat_send_player("MustTest", "# Server: Removed node @ " .. minetest.pos_to_string(pos) .. "!")
  43. minetest.get_node_timer(pos):stop()
  44. minetest.remove_node(pos) -- Remove the utility node.
  45. end
  46. end
  47. if not torchmelt.registered then
  48. minetest.register_node("torchmelt:melter", {
  49. drawtype = "airlike",
  50. description = "Torch Heat Source (Please Report to Admin)",
  51. paramtype = "light",
  52. sunlight_propagates = true,
  53. walkable = false,
  54. pointable = false,
  55. groups = {immovable = 1},
  56. climbable = false,
  57. buildable_to = true,
  58. floodable = true,
  59. drop = "",
  60. on_construct = function(...)
  61. return torchmelt.on_construct(...)
  62. end,
  63. on_timer = function(...)
  64. return torchmelt.on_timer(...)
  65. end,
  66. on_finish_collapse = function(pos, node)
  67. minetest.remove_node(pos)
  68. end,
  69. on_collapse_to_entity = function(pos, node)
  70. -- Do nothing.
  71. end,
  72. })
  73. local c = "torchmelt:core"
  74. local f = torchmelt.modpath .. "/init.lua"
  75. reload.register_file(c, f, false)
  76. torchmelt.registered = true
  77. end