This repository has been archived by the owner on Oct 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.lua
297 lines (248 loc) · 7.27 KB
/
api.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
---------------------------
-- Happy Weather API
-- License: MIT
-- Credits: xeranas
---------------------------
-- Main object which will be used in Weather API lifecycle
happy_weather = {}
-- Local variables which helps organize active and deactive weahers
local registered_weathers = {}
local active_weathers = {}
------------------------------------
-- Local helper / utility methods --
------------------------------------
-- Adds weather to active_weathers table
local add_active_weather = function(weather_obj)
table.insert(active_weathers, weather_obj)
end
-- Remove weather from active_weathers table
local remove_active_weather = function(weather_code)
if #active_weathers == 0 then
return
end
for k, weather_ in ipairs(active_weathers) do
if weather_.code == weather_code then
table.remove(active_weathers, k)
return
end
end
end
-- Returns active weather
local get_active_weather = function(weather_code)
if #active_weathers == 0 then
return nil
end
for k, weather_ in ipairs(active_weathers) do
if weather_.code == weather_code then
return weather_
end
end
end
-- adds player to affected_players table
local add_player = function(affected_players, player)
table.insert(affected_players, player)
end
-- remove player from affected_players table
local remove_player = function(affected_players, player_name)
if #affected_players == 0 then
return
end
for k, player_ in ipairs(affected_players) do
if player_:get_player_name() == player_name then
table.remove(affected_players, k)
return
end
end
end
local is_player_affected = function(affected_players, player_name)
if #affected_players == 0 then
return false
end
for k, player_ in ipairs(affected_players) do
if player_:get_player_name() == player_name then
return true
end
end
end
---------------------------
-- Weather API functions --
---------------------------
-- Adds weather to register_weathers table
happy_weather.register_weather = function(weather_obj)
table.insert(registered_weathers, weather_obj)
end
-- Returns true if weather is active right now, false otherwise
happy_weather.is_weather_active = function(weather_code)
if #active_weathers == 0 then
return false
end
for k, weather_ in ipairs(active_weathers) do
if weather_.code == weather_code then
return true
end
end
return false
end
-- Requests weaher to start
happy_weather.request_to_start = function(weather_code, position)
if #registered_weathers == 0 then
return
end
for k, weather_ in ipairs(registered_weathers) do
if weather_.code == weather_code and weather_.start ~= nil then
weather_.start(position)
return
end
end
end
-- Requests weaher to end
happy_weather.request_to_end = function(weather_code)
if #active_weathers == 0 then
return
end
for k, weather_ in ipairs(active_weathers) do
if weather_.code == weather_code and weather_.stop ~= nil then
weather_.stop()
return
end
end
end
happy_weather.is_player_in_weather_area = function(player_name, weather_code)
if #active_weathers == 0 then
return false
end
local active_weather = get_active_weather(weather_code)
if active_weather == nil then
return false
end
return is_player_affected(active_weather.affected_players, player_name)
end
-----------------------------------------------------------------------------
-- Weather object callback wrappers to avoid issues from undefined methods --
-----------------------------------------------------------------------------
-- Weather is_starting method nil-safe wrapper
local weather_is_starting = function(weather_obj, dtime, position)
if weather_obj.is_starting == nil then
return false
end
return weather_obj.is_starting(dtime, position)
end
-- Weather is_starting method nil-safe wrapper
local weather_is_ending = function(weather_obj, dtime)
if weather_obj.is_ending == nil then
return false
end
return weather_obj.is_ending(dtime)
end
-- Weather add_player method nil-safe wrapper
local weather_add_player = function(weather_obj, player)
if weather_obj.add_player == nil then
return
end
weather_obj.add_player(player)
end
-- Weather remove_player method nil-safe wrapper
local weather_remove_player = function(weather_obj, player)
if weather_obj.remove_player == nil then
return
end
weather_obj.remove_player(player)
end
-- Weather remove_player method nil-safe wrapper
local weather_in_area = function(weather_obj, position)
if weather_obj.in_area == nil then
return true
end
return weather_obj.in_area(position)
end
-- Weather render method nil-safe wrapper
local weather_render = function(weather_obj, dtime, player)
if weather_obj.render == nil then
return
end
weather_obj.render(dtime, player)
end
-- Weather start method nil-safe wrapper
local weather_start = function(weather_obj, player)
if weather_obj.start == nil then
return
end
weather_obj.start(player)
end
-- Weather stop method nil-safe wrapper
local weather_stop = function(weather_obj, player)
if weather_obj.stop == nil then
return
end
weather_obj.stop(player)
end
-- Perform clean-up callbacks calls sets flags upon weaher end
local prepare_ending = function(weather_obj)
weather_obj.active = false
remove_active_weather(weather_obj.code)
end
-- Perform weather setup for certain player
local prepare_starting = function(weather_obj)
weather_obj.active = true
weather_obj.affected_players = {}
add_active_weather(weather_obj)
end
-- While still active weather can or can not affect players based on area they are
local render_if_in_area = function(weather_obj, dtime, player)
if is_player_affected(weather_obj.affected_players, player:get_player_name()) then
if weather_in_area(weather_obj, player:getpos()) then
weather_render(weather_obj, dtime, player)
else
weather_remove_player(weather_obj, player)
remove_player(weather_obj.affected_players, player:get_player_name())
end
else
if weather_in_area(weather_obj, player:getpos()) then
add_player(weather_obj.affected_players, player)
weather_add_player(weather_obj, player)
end
end
end
--------------------------
-- Global step function --
--------------------------
minetest.register_globalstep(function(dtime)
if #registered_weathers == 0 then
-- no registered weathers, do nothing.
return
end
if #minetest.get_connected_players() == 0 then
-- no actual players, do nothing.
return
end
-- Loop through registered weathers
for i, weather_ in ipairs(registered_weathers) do
local deactivate_weather = false
local activate_weather = false
-- Loop through connected players
for ii, player in ipairs(minetest.get_connected_players()) do
-- Weaher is active checking if it about to end
if weather_.active then
if weather_is_ending(weather_, dtime) or deactivate_weather then
weather_remove_player(weather_, player)
remove_player(weather_.affected_players, player:get_player_name())
deactivate_weather = true -- should remain true until all players will be removed from weather
-- Weather still active updating it
else
render_if_in_area(weather_, dtime, player)
end
-- Weaher is not active checking if it about to start
else
if weather_.is_starting(dtime, player:getpos()) then
activate_weather = true
end
end
end
if deactivate_weather then
prepare_ending(weather_)
end
if activate_weather then
prepare_starting(weather_)
end
end
end)