Check for correct player in apply_effect_type
[minetest_playereffects.git] / init.lua
blob173a460b35867b45874c3f64cdc3b55a4b576745
1 --[=[ Main tables ]=]
3 playereffects = {}
5 --[[ table containing the groups (experimental) ]]
6 playereffects.groups = {}
8 --[[ table containing all the effect types ]]
9 playereffects.effect_types = {}
11 --[[ table containing all the active effects ]]
12 playereffects.effects = {}
14 --[[ table containing all the inactive effects.
15 Effects become inactive if a player leaves an become active again if they join again. ]]
16 playereffects.inactive_effects = {}
18 -- Variable for counting the effect_id
19 playereffects.last_effect_id = 0
21 --[=[ Include settings ]=]
22 dofile(minetest.get_modpath("playereffects").."/settings.lua")
24 --[=[ Load inactive_effects and last_effect_id from playereffects.mt, if this file exists ]=]
26 local filepath = minetest.get_worldpath().."/playereffects.mt"
27 local file = io.open(filepath, "r")
28 if file then
29 minetest.log("action", "[playereffects] playereffects.mt opened.")
30 local string = file:read()
31 io.close(file)
32 if(string ~= nil) then
33 local savetable = minetest.deserialize(string)
34 playereffects.inactive_effects = savetable.inactive_effects
35 minetest.debug("[playereffects] playereffects.mt successfully read.")
36 minetest.debug("[playereffects] inactive_effects = "..dump(playereffects.inactive_effects))
37 playereffects.last_effect_id = savetable.last_effect_id
38 minetest.debug("[playereffects] last_effect_id = "..dump(playereffects.last_effect_id))
40 end
41 end
42 end
44 function playereffects.next_effect_id()
45 playereffects.last_effect_id = playereffects.last_effect_id + 1
46 return playereffects.last_effect_id
47 end
49 --[=[ API functions ]=]
50 function playereffects.register_effect_type(name, description, icon, groups, apply, cancel, hidden)
51 effect_type = {}
52 effect_type.description = description
53 effect_type.apply = apply
54 effect_type.groups = groups
55 effect_type.icon = icon
56 if cancel ~= nil then
57 effect_type.cancel = cancel
58 else
59 effect_type.cancel = function() end
60 end
61 if hidden ~= nil then
62 effect_type.hidden = hidden
63 else
64 effect_type.hidden = false
65 end
66 playereffects.effect_types[name] = effect_type
67 minetest.log("action", "[playereffects] Effect type "..name.." registered!")
68 end
70 function playereffects.apply_effect_type(effect_type_id, duration, player)
71 local start_time = os.time()
72 local is_player = false
73 if(type(player)=="userdata") then
74 if(player.is_player ~= nil) then
75 if(player:is_player() == true) then
76 is_player = true
77 end
78 end
79 end
80 if(is_player == false) then
81 minetest.log("error", "[playereffects] Attempted to apply effect type "..effect_type_id.." to a non-player!")
82 return false
83 end
85 local status = playereffects.effect_types[effect_type_id].apply(player)
86 local playername = player:get_player_name()
87 local metadata
89 if(status == false) then
90 minetest.log("action", "[playereffects] Attempt to apply effect type "..effect_type_id.." to player "..playername.." failed!")
91 return false
92 else
93 metadata = status
94 end
96 local groups = playereffects.effect_types[effect_type_id].groups
97 for k,v in pairs(groups) do
98 playereffects.cancel_effect_group(v, playername)
99 end
100 local effect_id = playereffects.next_effect_id()
101 local effects = playereffects.get_player_effects(playername)
102 local smallest_hudpos
103 local biggest_hudpos = -1
104 local free_hudpos
105 for e=1,#effects do
106 local hudpos = effects[e].hudpos
107 if(hudpos > biggest_hudpos) then
108 biggest_hudpos = hudpos
110 if(smallest_hudpos == nil) then
111 smallest_hudpos = hudpos
112 elseif(hudpos < smallest_hudpos) then
113 smallest_hudpos = hudpos
116 if(smallest_hudpos == nil) then
117 free_hudpos = 0
118 elseif(smallest_hudpos >= 0) then
119 free_hudpos = smallest_hudpos - 1
120 else
121 free_hudpos = biggest_hudpos + 1
123 local hudids
124 --[[ show no more than 20 effects on the screen, so that hud_update does not need to be called so often ]]
125 if(free_hudpos <= 20) then
126 hudids = playereffects.hud_effect(effect_type_id, player, free_hudpos, duration)
127 else
128 hudids = {text_id=nil, icon_id=nil}
131 local effect = {
132 playername = playername,
133 effect_id = effect_id,
134 effect_type_id = effect_type_id,
135 start_time = start_time,
136 time_left = duration,
137 hudids = hudids,
138 hudpos = free_hudpos,
139 metadata = metadata,
142 playereffects.effects[effect_id] = effect
144 minetest.log("action", "[playereffects] Effect type "..effect_type_id.." applied to player "..playername.."!")
145 minetest.after(duration, function(effect_id) playereffects.cancel_effect(effect_id) end, effect_id)
148 function playereffects.cancel_effect_type(effect_type_id, cancel_all, playername)
149 local effects = playereffects.get_player_effects(playername)
150 if(cancel_all==nil) then all = false end
151 for e=1, #effects do
152 if(effects[e].effect_type_id == effect_type_id) then
153 playereffects.cancel_effect(effects[e].effect_id)
154 if(cancel_all==false) then
155 return
161 function playereffects.cancel_effect_group(groupname, playername)
162 local effects = playereffects.get_player_effects(playername)
163 for e=1,#effects do
164 local effect = effects[e]
165 local thesegroups = playereffects.effect_types[effect.effect_type_id].groups
166 local delete = false
167 for g=1,#thesegroups do
168 if(thesegroups[g] == groupname) then
169 playereffects.cancel_effect(effect.effect_id)
170 break
176 function playereffects.cancel_effect(effect_id)
177 local effect = playereffects.effects[effect_id]
178 if(effect ~= nil) then
179 local player = minetest.get_player_by_name(effect.playername)
180 if(effect.hudids.text_id~=nil) then
181 player:hud_remove(effect.hudids.text_id)
183 if(effect.hudids.icon_id~=nil) then
184 player:hud_remove(effect.hudids.icon_id)
186 playereffects.effect_types[effect.effect_type_id].cancel(effect)
187 playereffects.effects[effect_id] = nil
188 minetest.log("action", "[playereffects] Effect type "..effect.effect_type_id.." cancelled from player "..effect.playername.."!")
192 function playereffects.get_player_effects(playername)
193 if(minetest.get_player_by_name(playername) ~= nil) then
194 local effects = {}
195 for k,v in pairs(playereffects.effects) do
196 if(v.playername == playername) then
197 table.insert(effects, v)
200 return effects
201 else
202 return {}
206 --[=[ Saving all data to file ]=]
207 function playereffects.save_to_file()
208 local save_time = os.time()
209 local savetable = {}
210 local inactive_effects = {}
211 for id,effecttable in pairs(playereffects.inactive_effects) do
212 local playername = id
213 if(inactive_effects[playername] == nil) then
214 inactive_effects[playername] = {}
216 for i=1,#effecttable do
217 table.insert(inactive_effects[playername], effecttable[i])
220 for id,effect in pairs(playereffects.effects) do
221 local new_duration = effect.time_left - os.difftime(save_time, effect.start_time)
222 local new_effect = {
223 effect_id = effect.effect_id,
224 effect_type_id = effect.effect_type_id,
225 time_left = new_duration,
226 start_time = effect.start_time,
227 playername = effect.playername,
228 metadata = effect.metadata
230 if(inactive_effects[effect.playername] == nil) then
231 inactive_effects[effect.playername] = {}
233 table.insert(inactive_effects[effect.playername], new_effect)
236 savetable.inactive_effects = inactive_effects
237 savetable.last_effect_id = playereffects.last_effect_id
239 local savestring = minetest.serialize(savetable)
241 local filepath = minetest.get_worldpath().."/playereffects.mt"
242 local file = io.open(filepath, "w")
243 if file then
244 file:write(savestring)
245 io.close(file)
246 minetest.log("action", "[playereffects] Wrote playereffects data into "..filepath..".")
247 else
248 minetest.log("error", "[playereffects] Failed to write playereffects data into "..filepath..".")
252 --[=[ Callbacks ]=]
253 --[[ Cancel all effects on player death ]]
254 minetest.register_on_dieplayer(function(player)
255 local effects = playereffects.get_player_effects(player:get_player_name())
256 for e=1,#effects do
257 playereffects.cancel_effect(effects[e].effect_id)
259 end)
262 minetest.register_on_leaveplayer(function(player)
263 local leave_time = os.time()
264 local playername = player:get_player_name()
265 local effects = playereffects.get_player_effects(playername)
267 playereffects.hud_clear(player)
269 if(playereffects.inactive_effects[playername] == nil) then
270 playereffects.inactive_effects[playername] = {}
272 for e=1,#effects do
273 local new_duration = effects[e].time_left - os.difftime(leave_time, effects[e].start_time)
274 local new_effect = effects[e]
275 new_effect.time_left = new_duration
276 table.insert(playereffects.inactive_effects[playername], new_effect)
277 playereffects.cancel_effect(effects[e].effect_id)
279 end)
281 minetest.register_on_shutdown(function()
282 minetest.log("action", "[playereffects] Server shuts down. Rescuing data into playereffects.mt")
283 playereffects.save_to_file()
284 end)
286 minetest.register_on_joinplayer(function(player)
287 local playername = player:get_player_name()
289 -- load all the effects again (if any)
290 if(playereffects.inactive_effects[playername] ~= nil) then
291 for i=1,#playereffects.inactive_effects[playername] do
292 local effect = playereffects.inactive_effects[playername][i]
293 playereffects.apply_effect_type(effect.effect_type_id, effect.time_left, player)
295 playereffects.inactive_effects[playername] = nil
297 end)
299 playereffects.globalstep_timer = 0
300 playereffects.autosave_timer = 0
301 minetest.register_globalstep(function(dtime)
302 playereffects.globalstep_timer = playereffects.globalstep_timer + dtime
303 playereffects.autosave_timer = playereffects.autosave_timer + dtime
304 -- Update HUDs of all players
305 if(playereffects.globalstep_timer >= 1) then
306 playereffects.globalstep_timer = 0
308 local players = minetest.get_connected_players()
309 for p=1,#players do
310 playereffects.hud_update(players[p])
313 -- Autosave into file
314 if(playereffects.use_autosave == true and playereffects.autosave_timer >= playereffects.autosave_time) then
315 playereffects.autosave_timer = 0
316 minetest.log("action", "[playereffects] Autosaving mod data to playereffects.mt ...")
317 playereffects.save_to_file()
319 end)
324 --[=[ HUD ]=]
325 function playereffects.hud_update(player)
326 if(playereffects.use_hud == true) then
327 local now = os.time()
328 local effects = playereffects.get_player_effects(player:get_player_name())
329 for e=1,#effects do
330 local effect = effects[e]
331 if(effect.hudids.text_id ~= nil) then
332 local description = playereffects.effect_types[effect.effect_type_id].description
333 local time_left = os.difftime(effect.start_time + effect.time_left, now)
334 player:hud_change(effect.hudids.text_id, "text", description .. " ("..tostring(time_left).." s)")
340 function playereffects.hud_clear(player)
341 if(playereffects.use_hud == true) then
342 local playername = player:get_player_name()
343 local effects = playereffects.get_player_effects(playername)
344 if(effects ~= nil) then
345 for e=1,#effects do
346 if(effects[e].hudids.text_id ~= nil) then
347 player:hud_remove(effects[e].hudids.text_id)
349 if(effects[e].hudids.icon_id ~= nil) then
350 player:hud_remove(effects[e].hudids.icon_id)
357 function playereffects.hud_effect(effect_type_id, player, pos, time_left)
358 local text_id, icon_id
359 local effect_type = playereffects.effect_types[effect_type_id]
360 if(playereffects.use_hud == true and effect_type.hidden == false) then
361 text_id = player:hud_add({
362 hud_elem_type = "text",
363 position = { x = 1, y = 0.3 },
364 name = "effect_"..effect_type_id,
365 text = playereffects.effect_types[effect_type_id].description .. " ("..tostring(time_left).." s)",
366 scale = { x = 170, y = 20},
367 alignment = { x = -1, y = 0 },
368 direction = 1,
369 number = 0xFFFFFF,
370 offset = { x = -5, y = pos*20 }
372 if(playereffects.effect_types[effect_type_id].icon ~= nil) then
373 icon_id = player:hud_add({
374 hud_elem_type = "image",
375 scale = { x = 1, y = 1 },
376 position = { x = 1, y = 0.3 },
377 name = "effect_icon_"..effect_type_id,
378 text = playereffects.effect_types[effect_type_id].icon,
379 alignment = { x = -1, y=0 },
380 direction = 0,
381 offset = { x = -186, y = pos*20 },
383 end
384 else
385 text_id = nil
386 icon_id = nil
388 return { text_id = text_id, icon_id = icon_id }
392 -- LOAD EXAMPLES
393 dofile(minetest.get_modpath(minetest.get_current_modname()).."/examples.lua")