init.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. -- Always load the API
  2. dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/api.lua")
  3. -- Disable by mapgen or setting
  4. if minetest.settings:get_bool("enable_weather") == false then
  5. return
  6. end
  7. local mg_name = minetest.get_mapgen_setting("mg_name")
  8. if mg_name == "v6" or mg_name == "singlenode" then
  9. -- set a default shadow intensity for mgv6 and singlenode
  10. minetest.register_on_joinplayer(function(player)
  11. player:set_lighting({ shadows = { intensity = 0.33 } })
  12. end)
  13. return
  14. end
  15. -- Default implementation for noise based cloud appearance
  16. local TSCALE = 600 -- Time scale of noise variation in seconds
  17. local np_density = {
  18. offset = 0.5,
  19. scale = 0.5,
  20. spread = {x = TSCALE, y = TSCALE, z = TSCALE},
  21. seed = 813,
  22. octaves = 1,
  23. persist = 0,
  24. lacunarity = 2,
  25. }
  26. local np_thickness = {
  27. offset = 0.5,
  28. scale = 0.5,
  29. spread = {x = TSCALE, y = TSCALE, z = TSCALE},
  30. seed = 96,
  31. octaves = 1,
  32. persist = 0,
  33. lacunarity = 2,
  34. }
  35. local np_speedx = {
  36. offset = 0,
  37. scale = 1,
  38. spread = {x = TSCALE, y = TSCALE, z = TSCALE},
  39. seed = 911923,
  40. octaves = 1,
  41. persist = 0,
  42. lacunarity = 2,
  43. }
  44. local np_speedz = {
  45. offset = 0,
  46. scale = 1,
  47. spread = {x = TSCALE, y = TSCALE, z = TSCALE},
  48. seed = 5728,
  49. octaves = 1,
  50. persist = 0,
  51. lacunarity = 2,
  52. }
  53. local nobj_density = nil
  54. local nobj_thickness = nil
  55. local nobj_speedx = nil
  56. local nobj_speedz = nil
  57. local function rangelim(value, lower, upper)
  58. return math.min(math.max(value, lower), upper)
  59. end
  60. local t_offset
  61. do
  62. local meta = minetest.get_mod_storage()
  63. if meta:contains("time_offset") then
  64. t_offset = meta:get_int("time_offset")
  65. else
  66. -- Use random offset so not each new world behaves the same.
  67. t_offset = math.random(0, 300000)
  68. meta:set_int("time_offset", t_offset)
  69. end
  70. end
  71. function weather.get(player)
  72. -- Adjusted time in seconds
  73. local time = math.floor(minetest.get_gametime() - t_offset)
  74. nobj_density = nobj_density or minetest.get_perlin(np_density)
  75. nobj_thickness = nobj_thickness or minetest.get_perlin(np_thickness)
  76. nobj_speedx = nobj_speedx or minetest.get_perlin(np_speedx)
  77. nobj_speedz = nobj_speedz or minetest.get_perlin(np_speedz)
  78. local n_density = nobj_density:get_2d({x = time, y = 0}) -- 0 to 1
  79. local n_thickness = nobj_thickness:get_2d({x = time, y = 0}) -- 0 to 1
  80. local n_speedx = nobj_speedx:get_2d({x = time, y = 0}) -- -1 to 1
  81. local n_speedz = nobj_speedz:get_2d({x = time, y = 0}) -- -1 to 1
  82. -- Fallback to mid-value 50 for very old worlds
  83. local humid = minetest.get_humidity(player:get_pos()) or 50
  84. -- Default and classic density value is 0.4, make this happen
  85. -- at humidity midvalue 50 when n_density is at midvalue 0.5.
  86. -- density_max = 0.25 at humid = 0.
  87. -- density_max = 0.8 at humid = 50.
  88. -- density_max = 1.35 at humid = 100.
  89. local density_max = 0.8 + ((humid - 50) / 50) * 0.55
  90. -- Range limit density_max to always have occasional
  91. -- small scattered clouds at extreme low humidity.
  92. local density = rangelim(density_max, 0.2, 1.0) * n_density
  93. return {
  94. clouds = {
  95. density = density,
  96. thickness = math.max(math.floor(
  97. rangelim(32 * humid / 100, 8, 32) * n_thickness
  98. ), 2),
  99. speed = {x = n_speedx * 4, z = n_speedz * 4},
  100. },
  101. lighting = {
  102. shadows = { intensity = 0.7 * (1 - density) }
  103. }
  104. }
  105. end