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 playername
= player
:get_player_name()
73 local groups
= playereffects
.effect_types
[effect_type_id
].groups
74 for k
,v
in pairs(groups
) do
75 playereffects
.cancel_effect_group(v
, playername
)
77 local effect_id
= playereffects
.next_effect_id()
78 local effects
= playereffects
.get_player_effects(playername
)
80 local biggest_hudpos
= -1
83 local hudpos
= effects
[e
].hudpos
84 if(hudpos
> biggest_hudpos
) then
85 biggest_hudpos
= hudpos
87 if(smallest_hudpos
== nil) then
88 smallest_hudpos
= hudpos
89 elseif(hudpos
< smallest_hudpos
) then
90 smallest_hudpos
= hudpos
93 if(smallest_hudpos
== nil) then
95 elseif(smallest_hudpos
>= 0) then
96 free_hudpos
= smallest_hudpos
- 1
98 free_hudpos
= biggest_hudpos
+ 1
101 --[[ show no more than 20 effects on the screen, so that hud_update does not need to be called so often ]]
102 if(free_hudpos
<= 20) then
103 hudids
= playereffects
.hud_effect(effect_type_id
, player
, free_hudpos
, duration
)
105 hudids
= {text_id
=nil, icon_id
=nil}
109 playername
= playername
,
110 effect_id
= effect_id
,
111 effect_type_id
= effect_type_id
,
112 start_time
= start_time
,
113 time_left
= duration
,
115 hudpos
= free_hudpos
,
118 playereffects
.effects
[effect_id
] = effect
120 playereffects
.effect_types
[effect_type_id
].apply(player
)
121 minetest
.log("action", "[playereffects] Effect type "..effect_type_id
.." applied to player "..playername
.."!")
122 minetest
.after(duration
, function(effect_id
) playereffects
.cancel_effect(effect_id
) end, effect_id
)
125 function playereffects
.cancel_effect_type(effect_type_id
, cancel_all
, playername
)
126 local effects
= playereffects
.get_player_effects(playername
)
127 if(cancel_all
==nil) then all
= false end
129 if(effects
[e
].effect_type_id
== effect_type_id
) then
130 playereffects
.cancel_effect(effects
[e
].effect_id
)
131 if(cancel_all
==false) then
138 function playereffects
.cancel_effect_group(groupname
, playername
)
139 local effects
= playereffects
.get_player_effects(playername
)
141 local effect
= effects
[e
]
142 local thesegroups
= playereffects
.effect_types
[effect
.effect_type_id
].groups
144 for g
=1,#thesegroups
do
145 if(thesegroups
[g
] == groupname
) then
146 playereffects
.cancel_effect(effect
.effect_id
)
153 function playereffects
.cancel_effect(effect_id
)
154 local effect
= playereffects
.effects
[effect_id
]
155 if(effect
~= nil) then
156 local player
= minetest
.get_player_by_name(effect
.playername
)
157 if(effect
.hudids
.text_id
~=nil) then
158 player
:hud_remove(effect
.hudids
.text_id
)
160 if(effect
.hudids
.icon_id
~=nil) then
161 player
:hud_remove(effect
.hudids
.icon_id
)
163 playereffects
.effect_types
[effect
.effect_type_id
].cancel(effect
)
164 playereffects
.effects
[effect_id
] = nil
165 minetest
.log("action", "[playereffects] Effect type "..effect
.effect_type_id
.." cancelled from player "..effect
.playername
.."!")
169 function playereffects
.get_player_effects(playername
)
170 if(minetest
.get_player_by_name(playername
) ~= nil) then
172 for k
,v
in pairs(playereffects
.effects
) do
173 if(v
.playername
== playername
) then
174 table.insert(effects
, v
)
183 --[=[ Saving all data to file ]=]
184 function playereffects
.save_to_file()
185 local save_time
= os
.time()
187 local inactive_effects
= {}
188 for id
,effecttable
in pairs(playereffects
.inactive_effects
) do
189 local playername
= id
190 if(inactive_effects
[playername
] == nil) then
191 inactive_effects
[playername
] = {}
193 for i
=1,#effecttable
do
194 table.insert(inactive_effects
[playername
], effecttable
[i
])
197 for id
,effect
in pairs(playereffects
.effects
) do
198 local new_duration
= effect
.time_left
- os
.difftime(save_time
, effect
.start_time
)
200 effect_id
= effect
.effect_id
,
201 effect_type_id
= effect
.effect_type_id
,
202 time_left
= new_duration
,
203 start_time
= effect
.start_time
,
204 playername
= effect
.playername
,
206 if(inactive_effects
[effect
.playername
] == nil) then
207 inactive_effects
[effect
.playername
] = {}
209 table.insert(inactive_effects
[effect
.playername
], new_effect
)
212 savetable
.inactive_effects
= inactive_effects
213 savetable
.last_effect_id
= playereffects
.last_effect_id
215 local savestring
= minetest
.serialize(savetable
)
217 local filepath
= minetest
.get_worldpath().."/playereffects.mt"
218 local file
= io
.open(filepath
, "w")
220 file
:write(savestring
)
222 minetest
.log("action", "[playereffects] Wrote playereffects data into "..filepath
..".")
224 minetest
.log("error", "[playereffects] Failed to write playereffects data into "..filepath
..".")
229 --[[ Cancel all effects on player death ]]
230 minetest
.register_on_dieplayer(function(player
)
231 local effects
= playereffects
.get_player_effects(player
:get_player_name())
233 playereffects
.cancel_effect(effects
[e
].effect_id
)
238 minetest
.register_on_leaveplayer(function(player
)
239 local leave_time
= os
.time()
240 local playername
= player
:get_player_name()
241 local effects
= playereffects
.get_player_effects(playername
)
243 playereffects
.hud_clear(player
)
245 if(playereffects
.inactive_effects
[playername
] == nil) then
246 playereffects
.inactive_effects
[playername
] = {}
249 local new_duration
= effects
[e
].time_left
- os
.difftime(leave_time
, effects
[e
].start_time
)
250 local new_effect
= effects
[e
]
251 new_effect
.time_left
= new_duration
252 table.insert(playereffects
.inactive_effects
[playername
], new_effect
)
253 playereffects
.cancel_effect(effects
[e
].effect_id
)
257 minetest
.register_on_shutdown(function()
258 minetest
.log("action", "[playereffects] Server shuts down. Rescuing data into playereffects.mt")
259 playereffects
.save_to_file()
262 minetest
.register_on_joinplayer(function(player
)
263 local playername
= player
:get_player_name()
265 -- load all the effects again (if any)
266 if(playereffects
.inactive_effects
[playername
] ~= nil) then
267 for i
=1,#playereffects
.inactive_effects
[playername
] do
268 local effect
= playereffects
.inactive_effects
[playername
][i
]
269 playereffects
.apply_effect_type(effect
.effect_type_id
, effect
.time_left
, player
)
271 playereffects
.inactive_effects
[playername
] = nil
275 playereffects
.globalstep_timer
= 0
276 playereffects
.autosave_timer
= 0
277 minetest
.register_globalstep(function(dtime
)
278 playereffects
.globalstep_timer
= playereffects
.globalstep_timer
+ dtime
279 playereffects
.autosave_timer
= playereffects
.autosave_timer
+ dtime
280 -- Update HUDs of all players
281 if(playereffects
.globalstep_timer
>= 1) then
282 playereffects
.globalstep_timer
= 0
284 local players
= minetest
.get_connected_players()
286 playereffects
.hud_update(players
[p
])
289 -- Autosave into file
290 if(playereffects
.use_autosave
== true and playereffects
.autosave_timer
>= playereffects
.autosave_time
) then
291 playereffects
.autosave_timer
= 0
292 minetest
.log("action", "[playereffects] Autosaving mod data to playereffects.mt ...")
293 playereffects
.save_to_file()
301 function playereffects
.hud_update(player
)
302 if(playereffects
.use_hud
== true) then
303 local now
= os
.time()
304 local effects
= playereffects
.get_player_effects(player
:get_player_name())
306 local effect
= effects
[e
]
307 if(effect
.hudids
.text_id
~= nil) then
308 local description
= playereffects
.effect_types
[effect
.effect_type_id
].description
309 local time_left
= os
.difftime(effect
.start_time
+ effect
.time_left
, now
)
310 player
:hud_change(effect
.hudids
.text_id
, "text", description
.. " ("..tostring(time_left
).." s)")
316 function playereffects
.hud_clear(player
)
317 if(playereffects
.use_hud
== true) then
318 local playername
= player
:get_player_name()
319 local effects
= playereffects
.get_player_effects(playername
)
320 if(effects
~= nil) then
322 if(effects
[e
].hudids
.text_id
~= nil) then
323 player
:hud_remove(effects
[e
].hudids
.text_id
)
325 if(effects
[e
].hudids
.icon_id
~= nil) then
326 player
:hud_remove(effects
[e
].hudids
.icon_id
)
333 function playereffects
.hud_effect(effect_type_id
, player
, pos
, time_left
)
334 local text_id
, icon_id
335 local effect_type
= playereffects
.effect_types
[effect_type_id
]
336 if(playereffects
.use_hud
== true and effect_type
.hidden
== false) then
337 text_id
= player
:hud_add({
338 hud_elem_type
= "text",
339 position
= { x
= 1, y
= 0.3 },
340 name
= "effect_"..effect_type_id
,
341 text
= playereffects
.effect_types
[effect_type_id
].description
.. " ("..tostring(time_left
).." s)",
342 scale
= { x
= 170, y
= 20},
343 alignment
= { x
= -1, y
= 0 },
346 offset
= { x
= -5, y
= pos
*20 }
348 if(playereffects
.effect_types
[effect_type_id
].icon
~= nil) then
349 icon_id
= player
:hud_add({
350 hud_elem_type
= "image",
351 scale
= { x
= 1, y
= 1 },
352 position
= { x
= 1, y
= 0.3 },
353 name
= "effect_icon_"..effect_type_id
,
354 text
= playereffects
.effect_types
[effect_type_id
].icon
,
355 alignment
= { x
= -1, y
=0 },
357 offset
= { x
= -186, y
= pos
*20 },
364 return { text_id
= text_id
, icon_id
= icon_id
}
369 dofile(minetest
.get_modpath(minetest
.get_current_modname()).."/examples.lua")