init.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 t_offset
  63. do
  64. local meta = minetest.get_mod_storage()
  65. if meta:contains("time_offset") then
  66. t_offset = meta:get_int("time_offset")
  67. else
  68. -- Use random offset so not each new world behaves the same.
  69. t_offset = math.random(0, 300000)
  70. meta:set_int("time_offset", t_offset)
  71. end
  72. end
  73. local function update_clouds()
  74. -- Adjusted time in seconds
  75. local time = math.floor(minetest.get_gametime() - t_offset)
  76. nobj_density = nobj_density or minetest.get_perlin(np_density)
  77. nobj_thickness = nobj_thickness or minetest.get_perlin(np_thickness)
  78. nobj_speedx = nobj_speedx or minetest.get_perlin(np_speedx)
  79. nobj_speedz = nobj_speedz or minetest.get_perlin(np_speedz)
  80. local n_density = nobj_density:get_2d({x = time, y = 0}) -- 0 to 1
  81. local n_thickness = nobj_thickness:get_2d({x = time, y = 0}) -- 0 to 1
  82. local n_speedx = nobj_speedx:get_2d({x = time, y = 0}) -- -1 to 1
  83. local n_speedz = nobj_speedz:get_2d({x = time, y = 0}) -- -1 to 1
  84. for _, player in ipairs(minetest.get_connected_players()) do
  85. -- Fallback to mid-value 50 for very old worlds
  86. local humid = minetest.get_humidity(player:get_pos()) or 50
  87. -- Default and classic density value is 0.4, make this happen
  88. -- at humidity midvalue 50 when n_density is at midvalue 0.5.
  89. -- density_max = 0.25 at humid = 0.
  90. -- density_max = 0.8 at humid = 50.
  91. -- density_max = 1.35 at humid = 100.
  92. local density_max = 0.8 + ((humid - 50) / 50) * 0.55
  93. -- Range limit density_max to always have occasional
  94. -- small scattered clouds at extreme low humidity.
  95. local density = rangelim(density_max, 0.2, 1.0) * n_density
  96. player:set_clouds({
  97. density = density,
  98. thickness = math.max(math.floor(
  99. rangelim(32 * humid / 100, 8, 32) * n_thickness
  100. ), 2),
  101. speed = {x = n_speedx * 4, z = n_speedz * 4},
  102. })
  103. -- now adjust the shadow intensity
  104. player:set_lighting({ shadows = { intensity = 0.7 * (1 - density) } })
  105. end
  106. end
  107. local function cyclic_update()
  108. update_clouds()
  109. minetest.after(CYCLE, cyclic_update)
  110. end
  111. minetest.after(0, cyclic_update)
  112. -- Update on player join to instantly alter clouds from the default
  113. minetest.register_on_joinplayer(function(player)
  114. update_clouds()
  115. end)