init.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ice = ice or {}
  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. --minetest.chat_send_player("MustTest", "# Server: Ice notify @ " .. minetest.pos_to_string(pos) .. "!")
  20. local timer = minetest.get_node_timer(pos)
  21. --if not timer:is_started() then
  22. timer:start(math_random(ice.minmax_time()))
  23. --end
  24. end
  25. -- Here is where we calculate the ice freeze/melt logic.
  26. function ice.on_ice_timer(pos, elapsed)
  27. --minetest.chat_send_player("MustTest", "# Server: Icemelt @ " .. minetest.pos_to_string(pos) .. "!")
  28. local nn = minetest.get_node(pos).name
  29. if nn == "default:ice" then
  30. -- Transform thick, opaque ice to thin ice.
  31. if minetest.find_node_near(pos, 1, "group:melt_around") then
  32. minetest.add_node(pos, {name="ice:thin_ice"})
  33. return
  34. end
  35. elseif nn == "ice:thin_ice" then
  36. -- Melt thin ice to liquid.
  37. local minp = {x=pos.x-1, y=pos.y-1, z=pos.z-1}
  38. local maxp = {x=pos.x+1, y=pos.y+1, z=pos.z+1}
  39. local warm = minetest.find_nodes_in_area(minp, maxp, "group:melt_around")
  40. local heat = minetest.find_nodes_in_area(minp, maxp, {"group:flame", "group:hot"})
  41. if #warm >= 3 or #heat > 0 then
  42. minetest.add_node(pos, {name="default:water_source"})
  43. minetest.check_for_falling(pos)
  44. return
  45. end
  46. -- Turn thin ice opaque again.
  47. local above = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name
  48. if snow.is_snow(above) or above == "default:snowblock" then
  49. minetest.add_node(pos, {name="default:ice"})
  50. return
  51. end
  52. end
  53. end
  54. if not ice.registered then
  55. minetest.register_node("ice:thin_ice", {
  56. drawtype = "glasslike",
  57. description = "Clear Ice\n\nThis can be made to melt.\nApply heat for results.",
  58. tiles = {"ice_thin_ice.png"},
  59. use_texture_alpha = true,
  60. is_ground_content = true, -- Don't interfere with cavegen.
  61. paramtype = "light",
  62. groups = utility.dig_groups("ice", {
  63. ice = 1, melts = 1, cold = 1,
  64. want_notify = 1,
  65. slippery = 5,
  66. }),
  67. --_melts_to = "default:water_source",
  68. sounds = default.node_sound_glass_defaults(),
  69. -- Can be called by lavamelt ABM.
  70. on_melt = function(pos, other)
  71. minetest.remove_node(pos)
  72. end,
  73. -- Hack to notify self.
  74. on_construct = function(pos)
  75. minetest.get_node_timer(pos):start(math_random(ice.minmax_time()))
  76. end,
  77. on_notify = function(...)
  78. return ice.on_ice_notify(...)
  79. end,
  80. on_timer = function(...)
  81. return ice.on_ice_timer(...)
  82. end,
  83. })
  84. ice.registered = true
  85. end