Slight performance improvements
[minetest_playereffects.git] / init.lua
blob42eefdf7d918760cb44379099a6fe33d7ef209f3
1 --[=[ Main tables ]=]
3 playereffects = {}
5 --[[ table containing the groups (experimental) ]]
6 playereffects.groups = {}
8 --[[ table containing all the HUD info tables, indexed by player names.
9 A single HUD info table is formatted like this: { text_id = 1, icon_id=2, pos = 0 }
10 Where: text_id: HUD ID of the textual effect description
11 icon_id: HUD ID of the effect icon (optional)
12 pos: Y offset factor (starts with 0)
13 Example of full table:
14 { ["player1"] = {{ text_id = 1, icon_id=4, pos = 0 }}, ["player2] = { { text_id = 5, icon_id=6, pos = 0 }, { text_id = 7, icon_id=8, pos = 1 } } }
16 playereffects.hudinfos = {}
18 --[[ table containing all the effect types ]]
19 playereffects.effect_types = {}
21 --[[ table containing all the active effects ]]
22 playereffects.effects = {}
24 --[[ table containing all the inactive effects.
25 Effects become inactive if a player leaves an become active again if they join again. ]]
26 playereffects.inactive_effects = {}
28 -- Variable for counting the effect_id
29 playereffects.last_effect_id = 0
31 --[=[ Include settings ]=]
32 dofile(minetest.get_modpath("playereffects").."/settings.lua")
34 --[=[ Load inactive_effects and last_effect_id from playereffects.mt, if this file exists ]=]
36 local filepath = minetest.get_worldpath().."/playereffects.mt"
37 local file = io.open(filepath, "r")
38 if file then
39 minetest.log("action", "[playereffects] playereffects.mt opened.")
40 local string = file:read()
41 io.close(file)
42 if(string ~= nil) then
43 local savetable = minetest.deserialize(string)
44 playereffects.inactive_effects = savetable.inactive_effects
45 minetest.debug("[playereffects] playereffects.mt successfully read.")
46 minetest.debug("[playereffects] inactive_effects = "..dump(playereffects.inactive_effects))
47 playereffects.last_effect_id = savetable.last_effect_id
48 minetest.debug("[playereffects] last_effect_id = "..dump(playereffects.last_effect_id))
50 end
51 end
52 end
54 function playereffects.next_effect_id()
55 playereffects.last_effect_id = playereffects.last_effect_id + 1
56 return playereffects.last_effect_id
57 end
59 --[=[ API functions ]=]
60 function playereffects.register_effect_type(effect_type_id, description, icon, groups, apply, cancel, hidden, cancel_on_death)
61 effect_type = {}
62 effect_type.description = description
63 effect_type.apply = apply
64 effect_type.groups = groups
65 effect_type.icon = icon
66 if cancel ~= nil then
67 effect_type.cancel = cancel
68 else
69 effect_type.cancel = function() end
70 end
71 if hidden ~= nil then
72 effect_type.hidden = hidden
73 else
74 effect_type.hidden = false
75 end
76 if cancel_on_death ~= nil then
77 effect_type.cancel_on_death = cancel_on_death
78 else
79 effect_type.cancel_on_death = true
80 end
82 playereffects.effect_types[effect_type_id] = effect_type
83 minetest.log("action", "[playereffects] Effect type "..effect_type_id.." registered!")
84 end
86 function playereffects.apply_effect_type(effect_type_id, duration, player)
87 local start_time = os.time()
88 local is_player = false
89 if(type(player)=="userdata") then
90 if(player.is_player ~= nil) then
91 if(player:is_player() == true) then
92 is_player = true
93 end
94 end
95 end
96 if(is_player == false) then
97 minetest.log("error", "[playereffects] Attempted to apply effect type "..effect_type_id.." to a non-player!")
98 return false
99 end
101 local playername = player:get_player_name()
102 local groups = playereffects.effect_types[effect_type_id].groups
103 for k,v in pairs(groups) do
104 playereffects.cancel_effect_group(v, playername)
107 local status = playereffects.effect_types[effect_type_id].apply(player)
108 local metadata
110 if(status == false) then
111 minetest.log("action", "[playereffects] Attempt to apply effect type "..effect_type_id.." to player "..playername.." failed!")
112 return false
113 else
114 metadata = status
117 local effect_id = playereffects.next_effect_id()
118 local smallest_hudpos
119 local biggest_hudpos = -1
120 local free_hudpos
121 if(playereffects.hudinfos[playername] == nil) then
122 playereffects.hudinfos[playername] = {}
124 local hudinfos = playereffects.hudinfos[playername]
125 for effect_id, hudinfo in pairs(hudinfos) do
126 local hudpos = hudinfo.pos
127 if(hudpos > biggest_hudpos) then
128 biggest_hudpos = hudpos
130 if(smallest_hudpos == nil) then
131 smallest_hudpos = hudpos
132 elseif(hudpos < smallest_hudpos) then
133 smallest_hudpos = hudpos
136 if(smallest_hudpos == nil) then
137 free_hudpos = 0
138 elseif(smallest_hudpos >= 0) then
139 free_hudpos = smallest_hudpos - 1
140 else
141 free_hudpos = biggest_hudpos + 1
143 --[[ show no more than 20 effects on the screen, so that hud_update does not need to be called so often ]]
144 local text_id, icon_id
145 if(free_hudpos <= 20) then
146 text_id, icon_id = playereffects.hud_effect(effect_type_id, player, free_hudpos, duration)
147 local hudinfo = {
148 text_id = text_id,
149 icon_id = icon_id,
150 pos = free_hudpos,
152 playereffects.hudinfos[playername][effect_id] = hudinfo
153 else
154 text_id, icon_id = nil, nil
157 local effect = {
158 playername = playername,
159 effect_id = effect_id,
160 effect_type_id = effect_type_id,
161 start_time = start_time,
162 time_left = duration,
163 metadata = metadata,
166 playereffects.effects[effect_id] = effect
168 -- minetest.log("action", "[playereffects] Effect type "..effect_type_id.." applied to player "..playername.." (effect_id = "..effect_id..").")
169 minetest.after(duration, function(effect_id) playereffects.cancel_effect(effect_id) end, effect_id)
171 return effect_id
174 function playereffects.cancel_effect_type(effect_type_id, cancel_all, playername)
175 local effects = playereffects.get_player_effects(playername)
176 if(cancel_all==nil) then all = false end
177 for e=1, #effects do
178 if(effects[e].effect_type_id == effect_type_id) then
179 playereffects.cancel_effect(effects[e].effect_id)
180 if(cancel_all==false) then
181 return
187 function playereffects.cancel_effect_group(groupname, playername)
188 local effects = playereffects.get_player_effects(playername)
189 for e=1,#effects do
190 local effect = effects[e]
191 local thesegroups = playereffects.effect_types[effect.effect_type_id].groups
192 local delete = false
193 for g=1,#thesegroups do
194 if(thesegroups[g] == groupname) then
195 playereffects.cancel_effect(effect.effect_id)
196 break
202 function playereffects.cancel_effect(effect_id)
203 local effect = playereffects.effects[effect_id]
204 if(effect ~= nil) then
205 local player = minetest.get_player_by_name(effect.playername)
206 local hudinfo = playereffects.hudinfos[effect.playername][effect_id]
207 if(hudinfo ~= nil) then
208 if(hudinfo.text_id~=nil) then
209 player:hud_remove(hudinfo.text_id)
211 if(hudinfo.icon_id~=nil) then
212 player:hud_remove(hudinfo.icon_id)
214 playereffects.hudinfos[effect.playername][effect_id] = nil
216 playereffects.effect_types[effect.effect_type_id].cancel(effect, player)
217 playereffects.effects[effect_id] = nil
218 minetest.log("action", "[playereffects] Effect type "..effect.effect_type_id.." cancelled from player "..effect.playername.."!")
222 function playereffects.get_player_effects(playername)
223 if(minetest.get_player_by_name(playername) ~= nil) then
224 local effects = {}
225 for k,v in pairs(playereffects.effects) do
226 if(v.playername == playername) then
227 table.insert(effects, v)
230 return effects
231 else
232 return {}
236 --[=[ Saving all data to file ]=]
237 function playereffects.save_to_file()
238 local save_time = os.time()
239 local savetable = {}
240 local inactive_effects = {}
241 for id,effecttable in pairs(playereffects.inactive_effects) do
242 local playername = id
243 if(inactive_effects[playername] == nil) then
244 inactive_effects[playername] = {}
246 for i=1,#effecttable do
247 table.insert(inactive_effects[playername], effecttable[i])
250 for id,effect in pairs(playereffects.effects) do
251 local new_duration = effect.time_left - os.difftime(save_time, effect.start_time)
252 local new_effect = {
253 effect_id = effect.effect_id,
254 effect_type_id = effect.effect_type_id,
255 time_left = new_duration,
256 start_time = effect.start_time,
257 playername = effect.playername,
258 metadata = effect.metadata
260 if(inactive_effects[effect.playername] == nil) then
261 inactive_effects[effect.playername] = {}
263 table.insert(inactive_effects[effect.playername], new_effect)
266 savetable.inactive_effects = inactive_effects
267 savetable.last_effect_id = playereffects.last_effect_id
269 local savestring = minetest.serialize(savetable)
271 local filepath = minetest.get_worldpath().."/playereffects.mt"
272 local file = io.open(filepath, "w")
273 if file then
274 file:write(savestring)
275 io.close(file)
276 minetest.log("action", "[playereffects] Wrote playereffects data into "..filepath..".")
277 else
278 minetest.log("error", "[playereffects] Failed to write playereffects data into "..filepath..".")
282 --[=[ Callbacks ]=]
283 --[[ Cancel all effects on player death ]]
284 minetest.register_on_dieplayer(function(player)
285 local effects = playereffects.get_player_effects(player:get_player_name())
286 for e=1,#effects do
287 if(playereffects.effect_types[effects[e].effect_type_id].cancel_on_death == true) then
288 playereffects.cancel_effect(effects[e].effect_id)
291 end)
294 minetest.register_on_leaveplayer(function(player)
295 local leave_time = os.time()
296 local playername = player:get_player_name()
297 local effects = playereffects.get_player_effects(playername)
299 playereffects.hud_clear(player)
301 if(playereffects.inactive_effects[playername] == nil) then
302 playereffects.inactive_effects[playername] = {}
304 for e=1,#effects do
305 local new_duration = effects[e].time_left - os.difftime(leave_time, effects[e].start_time)
306 local new_effect = effects[e]
307 new_effect.time_left = new_duration
308 table.insert(playereffects.inactive_effects[playername], new_effect)
309 playereffects.cancel_effect(effects[e].effect_id)
311 end)
313 minetest.register_on_shutdown(function()
314 minetest.log("action", "[playereffects] Server shuts down. Rescuing data into playereffects.mt")
315 playereffects.save_to_file()
316 end)
318 minetest.register_on_joinplayer(function(player)
319 local playername = player:get_player_name()
321 -- load all the effects again (if any)
322 if(playereffects.inactive_effects[playername] ~= nil) then
323 for i=1,#playereffects.inactive_effects[playername] do
324 local effect = playereffects.inactive_effects[playername][i]
325 playereffects.apply_effect_type(effect.effect_type_id, effect.time_left, player)
327 playereffects.inactive_effects[playername] = nil
329 end)
331 playereffects.globalstep_timer = 0
332 playereffects.autosave_timer = 0
333 minetest.register_globalstep(function(dtime)
334 playereffects.globalstep_timer = playereffects.globalstep_timer + dtime
335 playereffects.autosave_timer = playereffects.autosave_timer + dtime
336 -- Update HUDs of all players
337 if(playereffects.globalstep_timer >= 1) then
338 playereffects.globalstep_timer = 0
340 local players = minetest.get_connected_players()
341 for p=1,#players do
342 playereffects.hud_update(players[p])
345 -- Autosave into file
346 if(playereffects.use_autosave == true and playereffects.autosave_timer >= playereffects.autosave_time) then
347 playereffects.autosave_timer = 0
348 minetest.log("action", "[playereffects] Autosaving mod data to playereffects.mt ...")
349 playereffects.save_to_file()
351 end)
356 --[=[ HUD ]=]
357 function playereffects.hud_update(player)
358 if(playereffects.use_hud == true) then
359 local now = os.time()
360 local playername = player:get_player_name()
361 local hudinfos = playereffects.hudinfos[playername]
362 if(hudinfos ~= nil) then
363 for effect_id, hudinfo in pairs(hudinfos) do
364 local effect = playereffects.effects[effect_id]
365 if(effect ~= nil and hudinfo.text_id ~= nil) then
366 local description = playereffects.effect_types[effect.effect_type_id].description
367 local time_left = os.difftime(effect.start_time + effect.time_left, now)
368 player:hud_change(hudinfo.text_id, "text", description .. " ("..tostring(time_left).." s)")
375 function playereffects.hud_clear(player)
376 if(playereffects.use_hud == true) then
377 local playername = player:get_player_name()
378 local hudinfos = playereffects.hudinfos[playername]
379 if(hudinfos ~= nil) then
380 for effect_id, hudinfo in pairs(hudinfos) do
381 local effect = playereffects.effects[effect_id]
382 if(hudinfo.text_id ~= nil) then
383 player:hud_remove(hudinfo.text_id)
385 if(hudinfo.icon_id ~= nil) then
386 player:hud_remove(hudinfo.icon_id)
388 playereffects.hudinfos[playername][effect_id] = nil
394 function playereffects.hud_effect(effect_type_id, player, pos, time_left)
395 local text_id, icon_id
396 local effect_type = playereffects.effect_types[effect_type_id]
397 if(playereffects.use_hud == true and effect_type.hidden == false) then
398 local color
399 if(playereffects.effect_types[effect_type_id].cancel_on_death == true) then
400 color = 0xFFFFFF
401 else
402 color = 0xF0BAFF
404 text_id = player:hud_add({
405 hud_elem_type = "text",
406 position = { x = 1, y = 0.3 },
407 name = "effect_"..effect_type_id,
408 text = playereffects.effect_types[effect_type_id].description .. " ("..tostring(time_left).." s)",
409 scale = { x = 170, y = 20},
410 alignment = { x = -1, y = 0 },
411 direction = 1,
412 number = color,
413 offset = { x = -5, y = pos*20 }
415 if(playereffects.effect_types[effect_type_id].icon ~= nil) then
416 icon_id = player:hud_add({
417 hud_elem_type = "image",
418 scale = { x = 1, y = 1 },
419 position = { x = 1, y = 0.3 },
420 name = "effect_icon_"..effect_type_id,
421 text = playereffects.effect_types[effect_type_id].icon,
422 alignment = { x = -1, y=0 },
423 direction = 0,
424 offset = { x = -186, y = pos*20 },
426 end
427 else
428 text_id = nil
429 icon_id = nil
431 return text_id, icon_id
435 -- LOAD EXAMPLES
436 dofile(minetest.get_modpath(minetest.get_current_modname()).."/examples.lua")