1 local S
= minetest
.get_translator("mcl_weather")
3 -- weather states, 'none' is default, other states depends from active mods
4 mcl_weather
.state
= "none"
6 -- player list for saving player meta info
7 mcl_weather
.players
= {}
9 -- default weather recalculation interval
10 mcl_weather
.check_interval
= 300
12 -- weather min duration
13 mcl_weather
.min_duration
= 600
15 -- weather max duration
16 mcl_weather
.max_duration
= 9000
18 -- weather calculated end time
19 mcl_weather
.end_time
= nil
21 -- registered weathers
22 mcl_weather
.reg_weathers
= {}
24 -- global flag to disable/enable ABM logic.
25 mcl_weather
.allow_abm
= true
27 mcl_weather
.reg_weathers
["none"] = {
28 min_duration
= mcl_weather
.min_duration
,
29 max_duration
= mcl_weather
.max_duration
,
35 clear
= function() end,
38 local storage
= minetest
.get_mod_storage()
39 -- Save weather into mod storage, so it can be loaded after restarting the server
40 local save_weather
= function()
41 storage
:set_string("mcl_weather_state", mcl_weather
.state
)
42 storage
:set_int("mcl_weather_end_time", mcl_weather
.end_time
)
43 minetest
.log("verbose", "[mcl_weather] Weather data saved: state="..mcl_weather
.state
.." end_time="..mcl_weather
.end_time
)
45 minetest
.register_on_shutdown(save_weather
)
47 mcl_weather
.get_rand_end_time
= function(min_duration
, max_duration
)
49 if min_duration
~= nil and max_duration
~= nil then
50 r
= math
.random(min_duration
, max_duration
);
52 r
= math
.random(mcl_weather
.min_duration
, mcl_weather
.max_duration
);
54 return minetest
.get_gametime() + r
57 mcl_weather
.get_current_light_factor
= function()
58 if mcl_weather
.state
== "none" then
61 return mcl_weather
.reg_weathers
[mcl_weather
.state
].light_factor
65 -- Returns true if pos is outdoor.
66 -- Outdoor is defined as any node in the Overworld under open sky.
67 -- FIXME: Nodes below glass also count as “outdoor”, this should not be the case.
68 mcl_weather
.is_outdoor
= function(pos
)
69 local cpos
= {x
=pos
.x
, y
=pos
.y
+1, z
=pos
.z
}
70 local dim
= mcl_worlds
.pos_to_dimension(cpos
)
71 if minetest
.get_node_light(cpos
, 0.5) == 15 and dim
== "overworld" then
77 -- checks if player is undewater. This is needed in order to
78 -- turn off weather particles generation.
79 mcl_weather
.is_underwater
= function(player
)
80 local ppos
= player
:get_pos()
81 local offset
= player
:get_eye_offset()
82 local player_eye_pos
= {x
= ppos
.x
+ offset
.x
,
83 y
= ppos
.y
+ offset
.y
+ 1.5,
84 z
= ppos
.z
+ offset
.z
}
85 local node_level
= minetest
.get_node_level(player_eye_pos
)
86 if node_level
== 8 or node_level
== 7 then
92 -- trying to locate position for particles by player look direction for performance reason.
93 -- it is costly to generate many particles around player so goal is focus mainly on front view.
94 mcl_weather
.get_random_pos_by_player_look_dir
= function(player
)
95 local look_dir
= player
:get_look_dir()
96 local player_pos
= player
:get_pos()
98 local random_pos_x
= 0
99 local random_pos_y
= 0
100 local random_pos_z
= 0
102 if look_dir
.x
> 0 then
103 if look_dir
.z
> 0 then
104 random_pos_x
= math
.random() + math
.random(player_pos
.x
- 2.5, player_pos
.x
+ 5)
105 random_pos_z
= math
.random() + math
.random(player_pos
.z
- 2.5, player_pos
.z
+ 5)
107 random_pos_x
= math
.random() + math
.random(player_pos
.x
- 2.5, player_pos
.x
+ 5)
108 random_pos_z
= math
.random() + math
.random(player_pos
.z
- 5, player_pos
.z
+ 2.5)
111 if look_dir
.z
> 0 then
112 random_pos_x
= math
.random() + math
.random(player_pos
.x
- 5, player_pos
.x
+ 2.5)
113 random_pos_z
= math
.random() + math
.random(player_pos
.z
- 2.5, player_pos
.z
+ 5)
115 random_pos_x
= math
.random() + math
.random(player_pos
.x
- 5, player_pos
.x
+ 2.5)
116 random_pos_z
= math
.random() + math
.random(player_pos
.z
- 5, player_pos
.z
+ 2.5)
120 random_pos_y
= math
.random() + math
.random(player_pos
.y
+ 10, player_pos
.y
+ 15)
121 return random_pos_x
, random_pos_y
, random_pos_z
124 minetest
.register_globalstep(function(dtime
)
125 if mcl_weather
.end_time
== nil then
126 mcl_weather
.end_time
= mcl_weather
.get_rand_end_time()
128 -- recalculate weather
129 if mcl_weather
.end_time
<= minetest
.get_gametime() then
130 local changeWeather
= minetest
.settings
:get_bool("mcl_doWeatherCycle")
131 if changeWeather
== nil then
134 if changeWeather
then
135 mcl_weather
.set_random_weather(mcl_weather
.state
, mcl_weather
.reg_weathers
[mcl_weather
.state
])
137 mcl_weather
.end_time
= mcl_weather
.get_rand_end_time()
142 -- Sets random weather (which could be 'none' (no weather)).
143 mcl_weather
.set_random_weather
= function(weather_name
, weather_meta
)
144 if (weather_meta
~= nil) then
145 local transitions
= weather_meta
.transitions
146 local random_roll
= math
.random(0,100)
148 for v
, weather
in pairs(transitions
) do
149 if random_roll
< v
then
150 new_weather
= weather
155 mcl_weather
.change_weather(new_weather
)
160 -- Change weather to new_weather.
161 -- * explicit_end_time is OPTIONAL. If specified, explicitly set the
162 -- gametime (minetest.get_gametime) in which the weather ends.
163 mcl_weather
.change_weather
= function(new_weather
, explicit_end_time
)
164 if (mcl_weather
.reg_weathers
~= nil and mcl_weather
.reg_weathers
[new_weather
] ~= nil) then
165 if (mcl_weather
.state
~= nil and mcl_weather
.reg_weathers
[mcl_weather
.state
] ~= nil) then
166 mcl_weather
.reg_weathers
[mcl_weather
.state
].clear()
168 mcl_weather
.state
= new_weather
169 local weather_meta
= mcl_weather
.reg_weathers
[mcl_weather
.state
]
170 if explicit_end_time
then
171 mcl_weather
.end_time
= explicit_end_time
173 mcl_weather
.end_time
= mcl_weather
.get_rand_end_time(weather_meta
.min_duration
, weather_meta
.max_duration
)
175 mcl_weather
.skycolor
.update_sky_color()
182 mcl_weather
.get_weather
= function()
183 return mcl_weather
.state
186 minetest
.register_privilege("weather_manager", {
187 description
= S("Gives ability to control weather"),
188 give_to_singleplayer
= false
191 -- Weather command definition. Set
192 minetest
.register_chatcommand("weather", {
193 params
= "(clear | rain | snow | thunder) [<duration>]",
194 description
= S("Changes the weather to the specified parameter."),
195 privs
= {weather_manager
= true},
196 func
= function(name
, param
)
197 if (param
== "") then
198 return false, S("Error: No weather specified.")
200 local new_weather
, end_time
201 local parse1
, parse2
= string.match(param
, "(%w+) ?(%d*)")
203 if parse1
== "clear" then
209 return false, S("Error: Invalid parameters.")
212 if type(tonumber(parse2
)) == "number" then
213 local duration
= tonumber(parse2
)
215 return false, S("Error: Duration can't be less than 1 second.")
217 end_time
= minetest
.get_gametime() + duration
221 local success
= mcl_weather
.change_weather(new_weather
, end_time
)
225 return false, S("Error: Invalid weather specified. Use “clear”, “rain”, “snow” or “thunder”.")
230 minetest
.register_chatcommand("toggledownfall", {
232 description
= S("Toggles between clear weather and weather with downfall (randomly rain, thunderstorm or snow)"),
233 privs
= {weather_manager
= true},
234 func
= function(name
, param
)
235 -- Currently rain/thunder/snow: Set weather to clear
236 if mcl_weather
.state
~= "none" then
237 return mcl_weather
.change_weather("none")
239 -- Currently clear: Set weather randomly to rain/thunder/snow
241 local new
= { "rain", "thunder", "snow" }
242 local r
= math
.random(1, #new
)
243 return mcl_weather
.change_weather(new
[r
])
248 -- Configuration setting which allows user to disable ABM for weathers (if they use it).
249 -- Weather mods expected to be use this flag before registering ABM.
250 local weather_allow_abm
= minetest
.settings
:get_bool("weather_allow_abm")
251 if weather_allow_abm
~= nil and weather_allow_abm
== false then
252 mcl_weather
.allow_abm
= false
256 local load_weather
= function()
257 local weather
= storage
:get_string("mcl_weather_state")
258 if weather
and weather
~= "" then
259 mcl_weather
.state
= weather
260 mcl_weather
.end_time
= storage
:get_int("mcl_weather_end_time")
261 mcl_weather
.change_weather(weather
, mcl_weather
.end_time
)
262 if type(mcl_weather
.end_time
) ~= "number" then
263 -- Fallback in case of corrupted end time
264 mcl_weather
.end_time
= mcl_weather
.min_duration
266 minetest
.log("action", "[mcl_weather] Weather restored.")
268 minetest
.log("action", "[mcl_weather] No weather data found. Starting with clear weather.")