init.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. if not minetest.global_exists("ice") then ice = {} end
  2. ice.modpath = minetest.get_modpath("ice")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. if minetest.get_modpath("reload") then
  6. local c = "ice:core"
  7. local f = ice.modpath .. "/init.lua"
  8. if not reload.file_registered(c) then
  9. reload.register_file(c, f, false)
  10. end
  11. end
  12. -- May be used as argument to math_random().
  13. function ice.minmax_time()
  14. return ice.min_time, ice.max_time
  15. end
  16. ice.min_time = 40
  17. ice.max_time = 300
  18. function ice.on_ice_notify(pos, other)
  19. local timer = minetest.get_node_timer(pos)
  20. --if not timer:is_started() then
  21. timer:start(math_random(ice.minmax_time()))
  22. --end
  23. end
  24. -- Here is where we calculate the ice freeze/melt logic.
  25. function ice.on_ice_timer(pos, elapsed)
  26. local nn = minetest.get_node(pos).name
  27. if nn == "default:ice" then
  28. -- Transform thick, opaque ice to thin ice.
  29. if minetest.find_node_near(pos, 1, "group:melt_around") then
  30. minetest.add_node(pos, {name="ice:thin_ice"})
  31. return
  32. end
  33. elseif nn == "ice:thin_ice" then
  34. -- Melt thin ice to liquid.
  35. local minp = {x=pos.x-1, y=pos.y-1, z=pos.z-1}
  36. local maxp = {x=pos.x+1, y=pos.y+1, z=pos.z+1}
  37. local warm = minetest.find_nodes_in_area(minp, maxp, "group:melt_around")
  38. local heat = minetest.find_nodes_in_area(minp, maxp, {"group:flame", "group:hot"})
  39. if #warm >= 3 or #heat > 0 then
  40. minetest.add_node(pos, {name="default:water_source"})
  41. minetest.check_for_falling(pos)
  42. return
  43. end
  44. -- Turn thin ice opaque again.
  45. local above = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name
  46. if snow.is_snow(above) or above == "default:snowblock" then
  47. minetest.add_node(pos, {name="default:ice"})
  48. return
  49. end
  50. end
  51. end
  52. if not ice.registered then
  53. minetest.register_node("ice:thin_ice", {
  54. drawtype = "glasslike",
  55. description = "Clear Ice\n\nThis can be made to melt.\nApply heat for results.",
  56. tiles = {"ice_thin_ice.png"},
  57. use_texture_alpha = "blend",
  58. is_ground_content = true, -- Don't interfere with cavegen.
  59. paramtype = "light",
  60. groups = utility.dig_groups("ice", {
  61. ice = 1, melts = 1, cold = 1,
  62. want_notify = 1,
  63. slippery = 5,
  64. }),
  65. --_melts_to = "default:water_source",
  66. _is_bulk_mapgen_stone = true,
  67. sounds = default.node_sound_glass_defaults(),
  68. -- Can be called by lavamelt ABM.
  69. on_melt = function(pos, other)
  70. minetest.remove_node(pos)
  71. end,
  72. -- Hack to notify self.
  73. on_construct = function(pos)
  74. minetest.get_node_timer(pos):start(math_random(ice.minmax_time()))
  75. end,
  76. on_notify = function(...)
  77. return ice.on_ice_notify(...)
  78. end,
  79. on_timer = function(...)
  80. return ice.on_ice_timer(...)
  81. end,
  82. })
  83. ice.registered = true
  84. end