init.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. -- Disable by mapgen or setting
  2. local mg_name = minetest.get_mapgen_setting("mg_name")
  3. if mg_name == "v6" or mg_name == "singlenode" or
  4. minetest.settings:get_bool("enable_weather") == false then
  5. return
  6. end
  7. -- Parameters
  8. local TSCALE = 600 -- Time scale of noise variation in seconds
  9. local CYCLE = 8 -- Time period of cyclic clouds update in seconds
  10. local np_density = {
  11. offset = 0.5,
  12. scale = 0.5,
  13. spread = {x = TSCALE, y = TSCALE, z = TSCALE},
  14. seed = 813,
  15. octaves = 1,
  16. persist = 0,
  17. lacunarity = 2,
  18. }
  19. local np_thickness = {
  20. offset = 0.5,
  21. scale = 0.5,
  22. spread = {x = TSCALE, y = TSCALE, z = TSCALE},
  23. seed = 96,
  24. octaves = 1,
  25. persist = 0,
  26. lacunarity = 2,
  27. }
  28. local np_speedx = {
  29. offset = 0,
  30. scale = 1,
  31. spread = {x = TSCALE, y = TSCALE, z = TSCALE},
  32. seed = 911923,
  33. octaves = 1,
  34. persist = 0,
  35. lacunarity = 2,
  36. }
  37. local np_speedz = {
  38. offset = 0,
  39. scale = 1,
  40. spread = {x = TSCALE, y = TSCALE, z = TSCALE},
  41. seed = 5728,
  42. octaves = 1,
  43. persist = 0,
  44. lacunarity = 2,
  45. }
  46. -- End parameters
  47. -- Initialise noise objects to nil
  48. local nobj_density = nil
  49. local nobj_thickness = nil
  50. local nobj_speedx = nil
  51. local nobj_speedz = nil
  52. -- Update clouds function
  53. local function rangelim(value, lower, upper)
  54. return math.min(math.max(value, lower), upper)
  55. end
  56. local os_time_0 = os.time()
  57. local t_offset = math.random(0, 300000)
  58. local function update_clouds()
  59. -- Time in seconds.
  60. -- Add random time offset to avoid identical behaviour each server session.
  61. local time = os.difftime(os.time(), os_time_0) - t_offset
  62. nobj_density = nobj_density or minetest.get_perlin(np_density)
  63. nobj_thickness = nobj_thickness or minetest.get_perlin(np_thickness)
  64. nobj_speedx = nobj_speedx or minetest.get_perlin(np_speedx)
  65. nobj_speedz = nobj_speedz or minetest.get_perlin(np_speedz)
  66. local n_density = nobj_density:get_2d({x = time, y = 0}) -- 0 to 1
  67. local n_thickness = nobj_thickness:get_2d({x = time, y = 0}) -- 0 to 1
  68. local n_speedx = nobj_speedx:get_2d({x = time, y = 0}) -- -1 to 1
  69. local n_speedz = nobj_speedz:get_2d({x = time, y = 0}) -- -1 to 1
  70. for _, player in ipairs(minetest.get_connected_players()) do
  71. local humid = minetest.get_humidity(player:get_pos())
  72. -- Default and classic density value is 0.4, make this happen
  73. -- at humidity midvalue 50 when n_density is at midvalue 0.5.
  74. -- density_max = 0.25 at humid = 0.
  75. -- density_max = 0.8 at humid = 50.
  76. -- density_max = 1.35 at humid = 100.
  77. local density_max = 0.8 + ((humid - 50) / 50) * 0.55
  78. player:set_clouds({
  79. -- Range limit density_max to always have occasional
  80. -- small scattered clouds at extreme low humidity.
  81. density = rangelim(density_max, 0.2, 1.0) * n_density,
  82. thickness = math.max(math.floor(
  83. rangelim(32 * humid / 100, 8, 32) * n_thickness
  84. ), 2),
  85. speed = {x = n_speedx * 4, z = n_speedz * 4},
  86. })
  87. end
  88. end
  89. local function cyclic_update()
  90. update_clouds()
  91. minetest.after(CYCLE, cyclic_update)
  92. end
  93. minetest.after(0, cyclic_update)
  94. -- Update on player join to instantly alter clouds from the default
  95. minetest.register_on_joinplayer(function(player)
  96. update_clouds()
  97. end)