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")
39 minetest
.log("action", "[playereffects] playereffects.mt opened.")
40 local string = file
:read()
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
))
54 function playereffects
.next_effect_id()
55 playereffects
.last_effect_id
= playereffects
.last_effect_id
+ 1
56 return playereffects
.last_effect_id
59 --[=[ API functions ]=]
60 function playereffects
.register_effect_type(effect_type_id
, description
, icon
, groups
, apply
, cancel
, hidden
, cancel_on_death
)
62 effect_type
.description
= description
63 effect_type
.apply
= apply
64 effect_type
.groups
= groups
65 effect_type
.icon
= icon
67 effect_type
.cancel
= cancel
69 effect_type
.cancel
= function() end
72 effect_type
.hidden
= hidden
74 effect_type
.hidden
= false
76 if cancel_on_death
~= nil then
77 effect_type
.cancel_on_death
= cancel_on_death
79 effect_type
.cancel_on_death
= true
82 playereffects
.effect_types
[effect_type_id
] = effect_type
83 minetest
.log("action", "[playereffects] Effect type "..effect_type_id
.." registered!")
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
96 if(is_player
== false) then
97 minetest
.log("error", "[playereffects] Attempted to apply effect type "..effect_type_id
.." to a non-player!")
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
)
110 if(status
== false) then
111 minetest
.log("action", "[playereffects] Attempt to apply effect type "..effect_type_id
.." to player "..playername
.." failed!")
117 local effect_id
= playereffects
.next_effect_id()
118 local smallest_hudpos
119 local biggest_hudpos
= -1
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
138 elseif(smallest_hudpos
>= 0) then
139 free_hudpos
= smallest_hudpos
- 1
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
)
152 playereffects
.hudinfos
[playername
][effect_id
] = hudinfo
154 text_id
, icon_id
= nil, nil
158 playername
= playername
,
159 effect_id
= effect_id
,
160 effect_type_id
= effect_type_id
,
161 start_time
= start_time
,
162 time_left
= duration
,
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
)
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
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
187 function playereffects
.cancel_effect_group(groupname
, playername
)
188 local effects
= playereffects
.get_player_effects(playername
)
190 local effect
= effects
[e
]
191 local thesegroups
= playereffects
.effect_types
[effect
.effect_type_id
].groups
193 for g
=1,#thesegroups
do
194 if(thesegroups
[g
] == groupname
) then
195 playereffects
.cancel_effect(effect
.effect_id
)
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
225 for k
,v
in pairs(playereffects
.effects
) do
226 if(v
.playername
== playername
) then
227 table.insert(effects
, v
)
236 --[=[ Saving all data to file ]=]
237 function playereffects
.save_to_file()
238 local save_time
= os
.time()
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
)
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")
274 file
:write(savestring
)
276 minetest
.log("action", "[playereffects] Wrote playereffects data into "..filepath
..".")
278 minetest
.log("error", "[playereffects] Failed to write playereffects data into "..filepath
..".")
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())
287 if(playereffects
.effect_types
[effects
[e
].effect_type_id
].cancel_on_death
== true) then
288 playereffects
.cancel_effect(effects
[e
].effect_id
)
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
] = {}
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
)
313 minetest
.register_on_shutdown(function()
314 minetest
.log("action", "[playereffects] Server shuts down. Rescuing data into playereffects.mt")
315 playereffects
.save_to_file()
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
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()
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()
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
399 if(playereffects
.effect_types
[effect_type_id
].cancel_on_death
== true) then
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 },
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 },
424 offset
= { x
= -186, y
= pos
*20 },
431 return text_id
, icon_id
436 dofile(minetest
.get_modpath(minetest
.get_current_modname()).."/examples.lua")