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")
35 if(playereffects
.use_hud
== nil) then
36 playereffects
.use_hud
= true
38 if(playereffects
.use_autosave
== nil) then
39 playereffects
.use_autosave
= true
41 if(playereffects
.autosave_time
== nil) then
42 playereffects
.autosave_time
= 10
44 if(playereffects
.use_examples
== nil) then
45 playereffects
.use_examples
= false
48 --[=[ Load inactive_effects and last_effect_id from playereffects.mt, if this file exists ]=]
50 local filepath
= minetest
.get_worldpath().."/playereffects.mt"
51 local file
= io
.open(filepath
, "r")
53 minetest
.log("action", "[playereffects] playereffects.mt opened.")
54 local string = file
:read()
56 if(string ~= nil) then
57 local savetable
= minetest
.deserialize(string)
58 playereffects
.inactive_effects
= savetable
.inactive_effects
59 minetest
.debug("[playereffects] playereffects.mt successfully read.")
60 minetest
.debug("[playereffects] inactive_effects = "..dump(playereffects
.inactive_effects
))
61 playereffects
.last_effect_id
= savetable
.last_effect_id
62 minetest
.debug("[playereffects] last_effect_id = "..dump(playereffects
.last_effect_id
))
68 function playereffects
.next_effect_id()
69 playereffects
.last_effect_id
= playereffects
.last_effect_id
+ 1
70 return playereffects
.last_effect_id
73 --[=[ API functions ]=]
74 function playereffects
.register_effect_type(effect_type_id
, description
, icon
, groups
, apply
, cancel
, hidden
, cancel_on_death
, repeat_interval
)
75 local effect_type
= {}
76 effect_type
.description
= description
77 effect_type
.apply
= apply
78 effect_type
.groups
= groups
79 effect_type
.icon
= icon
81 effect_type
.cancel
= cancel
83 effect_type
.cancel
= function() end
86 effect_type
.hidden
= hidden
88 effect_type
.hidden
= false
90 if cancel_on_death
~= nil then
91 effect_type
.cancel_on_death
= cancel_on_death
93 effect_type
.cancel_on_death
= true
95 effect_type
.repeat_interval
= repeat_interval
97 playereffects
.effect_types
[effect_type_id
] = effect_type
98 minetest
.log("action", "[playereffects] Effect type "..effect_type_id
.." registered!")
101 function playereffects
.apply_effect_type(effect_type_id
, duration
, player
, repeat_interval_time_left
)
102 local start_time
= os
.time()
103 local is_player
= false
104 if(type(player
)=="userdata") then
105 if(player
.is_player
~= nil) then
106 if(player
:is_player() == true) then
111 if(is_player
== false) then
112 minetest
.log("error", "[playereffects] Attempted to apply effect type "..effect_type_id
.." to a non-player!")
116 local playername
= player
:get_player_name()
117 local groups
= playereffects
.effect_types
[effect_type_id
].groups
118 for k
,v
in pairs(groups
) do
119 playereffects
.cancel_effect_group(v
, playername
)
123 if(playereffects
.effect_types
[effect_type_id
].repeat_interval
== nil) then
124 local status
= playereffects
.effect_types
[effect_type_id
].apply(player
)
125 if(status
== false) then
126 minetest
.log("action", "[playereffects] Attempt to apply effect type "..effect_type_id
.." to player "..playername
.." failed!")
134 local effect_id
= playereffects
.next_effect_id()
135 local smallest_hudpos
136 local biggest_hudpos
= -1
138 if(playereffects
.hudinfos
[playername
] == nil) then
139 playereffects
.hudinfos
[playername
] = {}
141 local hudinfos
= playereffects
.hudinfos
[playername
]
142 for effect_id
, hudinfo
in pairs(hudinfos
) do
143 local hudpos
= hudinfo
.pos
144 if(hudpos
> biggest_hudpos
) then
145 biggest_hudpos
= hudpos
147 if(smallest_hudpos
== nil) then
148 smallest_hudpos
= hudpos
149 elseif(hudpos
< smallest_hudpos
) then
150 smallest_hudpos
= hudpos
153 if(smallest_hudpos
== nil) then
155 elseif(smallest_hudpos
>= 0) then
156 free_hudpos
= smallest_hudpos
- 1
158 free_hudpos
= biggest_hudpos
+ 1
161 local repeat_interval
= playereffects
.effect_types
[effect_type_id
].repeat_interval
162 if(repeat_interval
~= nil) then
163 if(repeat_interval_time_left
== nil) then
164 repeat_interval_time_left
= repeat_interval
168 --[[ show no more than 20 effects on the screen, so that hud_update does not need to be called so often ]]
169 local text_id
, icon_id
170 if(free_hudpos
<= 20) then
171 text_id
, icon_id
= playereffects
.hud_effect(effect_type_id
, player
, free_hudpos
, duration
, repeat_interval_time_left
)
177 playereffects
.hudinfos
[playername
][effect_id
] = hudinfo
179 text_id
, icon_id
= nil, nil
183 playername
= playername
,
184 effect_id
= effect_id
,
185 effect_type_id
= effect_type_id
,
186 start_time
= start_time
,
187 repeat_interval_start_time
= start_time
,
188 time_left
= duration
,
189 repeat_interval_time_left
= repeat_interval_time_left
,
193 playereffects
.effects
[effect_id
] = effect
195 if(repeat_interval
~= nil) then
196 minetest
.after(repeat_interval_time_left
, playereffects
.repeater
, effect_id
, duration
, player
, playereffects
.effect_types
[effect_type_id
].apply
)
198 minetest
.after(duration
, function(effect_id
) playereffects
.cancel_effect(effect_id
) end, effect_id
)
204 function playereffects
.repeater(effect_id
, repetitions
, player
, apply
)
205 local effect
= playereffects
.effects
[effect_id
]
206 if(effect
~= nil) then
207 local repetitions
= effect
.time_left
209 repetitions
= repetitions
- 1
210 effect
.time_left
= repetitions
211 if(repetitions
<= 0) then
212 playereffects
.cancel_effect(effect_id
)
214 local repeat_interval
= playereffects
.effect_types
[effect
.effect_type_id
].repeat_interval
215 effect
.repeat_interval_time_left
= repeat_interval
216 effect
.repeat_interval_start_time
= os
.time()
219 playereffects
.repeater
,
229 function playereffects
.cancel_effect_type(effect_type_id
, cancel_all
, playername
)
230 local effects
= playereffects
.get_player_effects(playername
)
231 if(cancel_all
==nil) then cancel_all
= false end
233 if(effects
[e
].effect_type_id
== effect_type_id
) then
234 playereffects
.cancel_effect(effects
[e
].effect_id
)
235 if(cancel_all
==false) then
242 function playereffects
.cancel_effect_group(groupname
, playername
)
243 local effects
= playereffects
.get_player_effects(playername
)
245 local effect
= effects
[e
]
246 local thesegroups
= playereffects
.effect_types
[effect
.effect_type_id
].groups
248 for g
=1,#thesegroups
do
249 if(thesegroups
[g
] == groupname
) then
250 playereffects
.cancel_effect(effect
.effect_id
)
257 function playereffects
.get_remaining_effect_time(effect_id
)
258 local now
= os
.time()
259 local effect
= playereffects
.effects
[effect_id
]
260 if(effect
~= nil) then
261 return (effect
.time_left
- os
.difftime(now
, effect
.start_time
))
267 function playereffects
.cancel_effect(effect_id
)
268 local effect
= playereffects
.effects
[effect_id
]
269 if(effect
~= nil) then
270 local player
= minetest
.get_player_by_name(effect
.playername
)
271 local hudinfo
= playereffects
.hudinfos
[effect
.playername
][effect_id
]
272 if(hudinfo
~= nil) then
273 if(hudinfo
.text_id
~=nil) then
274 player
:hud_remove(hudinfo
.text_id
)
276 if(hudinfo
.icon_id
~=nil) then
277 player
:hud_remove(hudinfo
.icon_id
)
279 playereffects
.hudinfos
[effect
.playername
][effect_id
] = nil
281 playereffects
.effect_types
[effect
.effect_type_id
].cancel(effect
, player
)
282 playereffects
.effects
[effect_id
] = nil
286 function playereffects
.get_player_effects(playername
)
287 if(minetest
.get_player_by_name(playername
) ~= nil) then
289 for k
,v
in pairs(playereffects
.effects
) do
290 if(v
.playername
== playername
) then
291 table.insert(effects
, v
)
300 function playereffects
.has_effect_type(playername
, effect_type_id
)
301 local pe
= playereffects
.get_player_effects(playername
)
303 if pe
[i
].effect_type_id
== effect_type_id
then
310 --[=[ Saving all data to file ]=]
311 function playereffects
.save_to_file()
312 local save_time
= os
.time()
314 local inactive_effects
= {}
315 for id
,effecttable
in pairs(playereffects
.inactive_effects
) do
316 local playername
= id
317 if(inactive_effects
[playername
] == nil) then
318 inactive_effects
[playername
] = {}
320 for i
=1,#effecttable
do
321 table.insert(inactive_effects
[playername
], effecttable
[i
])
324 for id
,effect
in pairs(playereffects
.effects
) do
325 local new_duration
, new_repeat_duration
326 if(playereffects
.effect_types
[effect
.effect_type_id
].repeat_interval
~= nil) then
327 new_duration
= effect
.time_left
328 new_repeat_duration
= effect
.repeat_interval_time_left
- os
.difftime(save_time
, effect
.repeat_interval_start_time
)
330 new_duration
= effect
.time_left
- os
.difftime(save_time
, effect
.start_time
)
333 effect_id
= effect
.effect_id
,
334 effect_type_id
= effect
.effect_type_id
,
335 time_left
= new_duration
,
336 repeat_interval_time_left
= new_repeat_duration
,
337 start_time
= effect
.start_time
,
338 repeat_interval_start_time
= effect
.repeat_interval_start_time
,
339 playername
= effect
.playername
,
340 metadata
= effect
.metadata
342 if(inactive_effects
[effect
.playername
] == nil) then
343 inactive_effects
[effect
.playername
] = {}
345 table.insert(inactive_effects
[effect
.playername
], new_effect
)
348 savetable
.inactive_effects
= inactive_effects
349 savetable
.last_effect_id
= playereffects
.last_effect_id
351 local savestring
= minetest
.serialize(savetable
)
353 local filepath
= minetest
.get_worldpath().."/playereffects.mt"
354 local file
= io
.open(filepath
, "w")
356 file
:write(savestring
)
358 minetest
.log("info", "[playereffects] Wrote playereffects data into "..filepath
..".")
360 minetest
.log("error", "[playereffects] Failed to write playereffects data into "..filepath
..".")
365 --[[ Cancel all effects on player death ]]
366 minetest
.register_on_dieplayer(function(player
)
367 local effects
= playereffects
.get_player_effects(player
:get_player_name())
369 if(playereffects
.effect_types
[effects
[e
].effect_type_id
].cancel_on_death
== true) then
370 playereffects
.cancel_effect(effects
[e
].effect_id
)
376 minetest
.register_on_leaveplayer(function(player
)
377 local leave_time
= os
.time()
378 local playername
= player
:get_player_name()
379 local effects
= playereffects
.get_player_effects(playername
)
381 playereffects
.hud_clear(player
)
383 if(playereffects
.inactive_effects
[playername
] == nil) then
384 playereffects
.inactive_effects
[playername
] = {}
387 local new_duration
= effects
[e
].time_left
- os
.difftime(leave_time
, effects
[e
].start_time
)
388 local new_effect
= effects
[e
]
389 new_effect
.time_left
= new_duration
390 table.insert(playereffects
.inactive_effects
[playername
], new_effect
)
391 playereffects
.cancel_effect(effects
[e
].effect_id
)
395 minetest
.register_on_shutdown(function()
396 minetest
.log("info", "[playereffects] Server shuts down. Rescuing data into playereffects.mt")
397 playereffects
.save_to_file()
400 minetest
.register_on_joinplayer(function(player
)
401 local playername
= player
:get_player_name()
403 -- load all the effects again (if any)
404 if(playereffects
.inactive_effects
[playername
] ~= nil) then
405 for i
=1,#playereffects
.inactive_effects
[playername
] do
406 local effect
= playereffects
.inactive_effects
[playername
][i
]
407 playereffects
.apply_effect_type(effect
.effect_type_id
, effect
.time_left
, player
, effect
.repeat_interval_time_left
)
409 playereffects
.inactive_effects
[playername
] = nil
413 playereffects
.globalstep_timer
= 0
414 playereffects
.autosave_timer
= 0
415 minetest
.register_globalstep(function(dtime
)
416 playereffects
.globalstep_timer
= playereffects
.globalstep_timer
+ dtime
417 playereffects
.autosave_timer
= playereffects
.autosave_timer
+ dtime
418 -- Update HUDs of all players
419 if(playereffects
.globalstep_timer
>= 1) then
420 playereffects
.globalstep_timer
= 0
422 local players
= minetest
.get_connected_players()
424 playereffects
.hud_update(players
[p
])
427 -- Autosave into file
428 if(playereffects
.use_autosave
== true and playereffects
.autosave_timer
>= playereffects
.autosave_time
) then
429 playereffects
.autosave_timer
= 0
430 minetest
.log("info", "[playereffects] Autosaving mod data to playereffects.mt ...")
431 playereffects
.save_to_file()
439 function playereffects
.hud_update(player
)
440 if(playereffects
.use_hud
== true) then
441 local now
= os
.time()
442 local playername
= player
:get_player_name()
443 local hudinfos
= playereffects
.hudinfos
[playername
]
444 if(hudinfos
~= nil) then
445 for effect_id
, hudinfo
in pairs(hudinfos
) do
446 local effect
= playereffects
.effects
[effect_id
]
447 if(effect
~= nil and hudinfo
.text_id
~= nil) then
448 local description
= playereffects
.effect_types
[effect
.effect_type_id
].description
449 local repeat_interval
= playereffects
.effect_types
[effect
.effect_type_id
].repeat_interval
450 if(repeat_interval
~= nil) then
451 local repeat_interval_time_left
= os
.difftime(effect
.repeat_interval_start_time
+ effect
.repeat_interval_time_left
, now
)
452 player
:hud_change(hudinfo
.text_id
, "text", description
.. " ("..tostring(effect
.time_left
).."/"..tostring(repeat_interval_time_left
) .. "s )")
454 local time_left
= os
.difftime(effect
.start_time
+ effect
.time_left
, now
)
455 player
:hud_change(hudinfo
.text_id
, "text", description
.. " ("..tostring(time_left
).." s)")
463 function playereffects
.hud_clear(player
)
464 if(playereffects
.use_hud
== true) then
465 local playername
= player
:get_player_name()
466 local hudinfos
= playereffects
.hudinfos
[playername
]
467 if(hudinfos
~= nil) then
468 for effect_id
, hudinfo
in pairs(hudinfos
) do
469 local effect
= playereffects
.effects
[effect_id
]
470 if(hudinfo
.text_id
~= nil) then
471 player
:hud_remove(hudinfo
.text_id
)
473 if(hudinfo
.icon_id
~= nil) then
474 player
:hud_remove(hudinfo
.icon_id
)
476 playereffects
.hudinfos
[playername
][effect_id
] = nil
482 function playereffects
.hud_effect(effect_type_id
, player
, pos
, time_left
, repeat_interval_time_left
)
483 local text_id
, icon_id
484 local effect_type
= playereffects
.effect_types
[effect_type_id
]
485 if(playereffects
.use_hud
== true and effect_type
.hidden
== false) then
487 if(playereffects
.effect_types
[effect_type_id
].cancel_on_death
== true) then
492 local description
= playereffects
.effect_types
[effect_type_id
].description
494 if(repeat_interval_time_left
~= nil) then
495 text
= description
.. " ("..tostring(time_left
).."/"..tostring(repeat_interval_time_left
) .. "s )"
497 text
= description
.. " ("..tostring(time_left
).." s)"
499 text_id
= player
:hud_add({
500 hud_elem_type
= "text",
501 position
= { x
= 1, y
= 0.3 },
502 name
= "effect_"..effect_type_id
,
504 scale
= { x
= 170, y
= 20},
505 alignment
= { x
= -1, y
= 0 },
508 offset
= { x
= -5, y
= pos
*20 }
510 if(playereffects
.effect_types
[effect_type_id
].icon
~= nil) then
511 icon_id
= player
:hud_add({
512 hud_elem_type
= "image",
513 scale
= { x
= 1, y
= 1 },
514 position
= { x
= 1, y
= 0.3 },
515 name
= "effect_icon_"..effect_type_id
,
516 text
= playereffects
.effect_types
[effect_type_id
].icon
,
517 alignment
= { x
= -1, y
=0 },
519 offset
= { x
= -186, y
= pos
*20 },
526 return text_id
, icon_id
531 if(playereffects
.use_examples
== true) then
532 dofile(minetest
.get_modpath(minetest
.get_current_modname()).."/examples.lua")