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")
29 minetest
.log("action", "[playereffects] playereffects.mt opened.")
30 local string = file
:read()
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
))
44 function playereffects
.next_effect_id()
45 playereffects
.last_effect_id
= playereffects
.last_effect_id
+ 1
46 return playereffects
.last_effect_id
49 --[=[ API functions ]=]
50 function playereffects
.register_effect_type(name
, description
, icon
, groups
, apply
, cancel
, hidden
)
52 effect_type
.description
= description
53 effect_type
.apply
= apply
54 effect_type
.groups
= groups
55 effect_type
.icon
= icon
57 effect_type
.cancel
= cancel
59 effect_type
.cancel
= function() end
62 effect_type
.hidden
= hidden
64 effect_type
.hidden
= false
66 playereffects
.effect_types
[name
] = effect_type
67 minetest
.log("action", "[playereffects] Effect type "..name
.." registered!")
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
80 if(is_player
== false) then
81 minetest
.log("error", "[playereffects] Attempted to apply effect type "..effect_type_id
.." to a non-player!")
85 local status
= playereffects
.effect_types
[effect_type_id
].apply(player
)
86 local playername
= player
:get_player_name()
89 if(status
== false) then
90 minetest
.log("action", "[playereffects] Attempt to apply effect type "..effect_type_id
.." to player "..playername
.." failed!")
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
)
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
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
118 elseif(smallest_hudpos
>= 0) then
119 free_hudpos
= smallest_hudpos
- 1
121 free_hudpos
= biggest_hudpos
+ 1
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
)
128 hudids
= {text_id
=nil, icon_id
=nil}
132 playername
= playername
,
133 effect_id
= effect_id
,
134 effect_type_id
= effect_type_id
,
135 start_time
= start_time
,
136 time_left
= duration
,
138 hudpos
= free_hudpos
,
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
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
161 function playereffects
.cancel_effect_group(groupname
, playername
)
162 local effects
= playereffects
.get_player_effects(playername
)
164 local effect
= effects
[e
]
165 local thesegroups
= playereffects
.effect_types
[effect
.effect_type_id
].groups
167 for g
=1,#thesegroups
do
168 if(thesegroups
[g
] == groupname
) then
169 playereffects
.cancel_effect(effect
.effect_id
)
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
195 for k
,v
in pairs(playereffects
.effects
) do
196 if(v
.playername
== playername
) then
197 table.insert(effects
, v
)
206 --[=[ Saving all data to file ]=]
207 function playereffects
.save_to_file()
208 local save_time
= os
.time()
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
)
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")
244 file
:write(savestring
)
246 minetest
.log("action", "[playereffects] Wrote playereffects data into "..filepath
..".")
248 minetest
.log("error", "[playereffects] Failed to write playereffects data into "..filepath
..".")
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())
257 playereffects
.cancel_effect(effects
[e
].effect_id
)
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
] = {}
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
)
281 minetest
.register_on_shutdown(function()
282 minetest
.log("action", "[playereffects] Server shuts down. Rescuing data into playereffects.mt")
283 playereffects
.save_to_file()
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
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()
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()
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())
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
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 },
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 },
381 offset
= { x
= -186, y
= pos
*20 },
388 return { text_id
= text_id
, icon_id
= icon_id
}
393 dofile(minetest
.get_modpath(minetest
.get_current_modname()).."/examples.lua")