init.lua 3.4 KB

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