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 --[=[ Load inactive_effects and last_effect_id from playereffects.mt, if this file exists ]=]
23 local filepath
= minetest
.get_worldpath().."/playereffects.mt"
24 local file
= io
.open(filepath
, "r")
27 minetest
.log("action", "[playereffects] playereffects.mt opened.")
28 local string = file
:read()
30 if(string ~= nil) then
31 savetable
= minetest
.deserialize(string)
32 playereffects
.inactive_effects
= savetable
.inactive_effects
33 minetest
.debug("[playereffects] playereffects.mt successfully read.")
34 minetest
.debug("[playereffects] inactive_effects = "..dump(playereffects
.inactive_effects
))
35 playereffects
.last_effect_id
= savetable
.last_effect_id
36 minetest
.debug("[playereffects] last_effect_id = "..dump(playereffects
.last_effect_id
))
42 function playereffects
.next_effect_id()
43 playereffects
.last_effect_id
= playereffects
.last_effect_id
+ 1
44 return playereffects
.last_effect_id
47 --[=[ API functions ]=]
48 function playereffects
.register_effect_type(name
, description
, icon
, groups
, apply
, cancel
)
50 effect_type
.description
= description
51 effect_type
.apply
= apply
52 effect_type
.groups
= groups
53 effect_type
.icon
= icon
55 effect_type
.cancel
= cancel
57 effect_type
.cancel
= function() end
59 playereffects
.effect_types
[name
] = effect_type
60 minetest
.log("action", "Effect type "..name
.." registered!")
63 function playereffects
.apply_effect_type(effect_type_id
, duration
, player
)
64 local start_time
= os
.time()
65 local playername
= player
:get_player_name()
66 local groups
= playereffects
.effect_types
[effect_type_id
].groups
67 for k
,v
in pairs(groups
) do
68 playereffects
.cancel_effect_group(v
, playername
)
70 local effect_id
= playereffects
.next_effect_id()
71 local effects
= playereffects
.get_player_effects(playername
)
73 local biggest_hudpos
= -1
76 local hudpos
= effects
[e
].hudpos
77 if(hudpos
> biggest_hudpos
) then
78 biggest_hudpos
= hudpos
80 if(smallest_hudpos
== nil) then
81 smallest_hudpos
= hudpos
82 elseif(hudpos
< smallest_hudpos
) then
83 smallest_hudpos
= hudpos
86 if(smallest_hudpos
== nil) then
88 elseif(smallest_hudpos
>= 0) then
89 free_hudpos
= smallest_hudpos
- 1
91 free_hudpos
= biggest_hudpos
+ 1
94 --[[ show no more than 20 effects on the screen, so that hud_update does not need to be called so often ]]
95 if(free_hudpos
<= 20) then
96 hudids
= playereffects
.hud_effect(effect_type_id
, player
, free_hudpos
, duration
)
98 hudids
= {text_id
=nil, icon_id
=nil}
102 playername
= playername
,
103 effect_id
= effect_id
,
104 effect_type_id
= effect_type_id
,
105 start_time
= start_time
,
106 time_left
= duration
,
108 hudpos
= free_hudpos
,
111 playereffects
.effects
[effect_id
] = effect
113 playereffects
.effect_types
[effect_type_id
].apply(player
)
114 minetest
.log("action", "Effect type "..effect_type_id
.." applied to player "..playername
.."!")
115 minetest
.after(duration
, function(effect_id
) playereffects
.cancel_effect(effect_id
) end, effect_id
)
118 function playereffects
.cancel_effect_type(effect_type_id
, cancel_all
, playername
)
119 local effects
= playereffects
.get_player_effects(playername
)
120 if(cancel_all
==nil) then all
= false end
122 if(effects
[e
].effect_type_id
== effect_type_id
) then
123 playereffects
.cancel_effect(effects
[e
].effect_id
)
124 if(cancel_all
==false) then
131 function playereffects
.cancel_effect_group(groupname
, playername
)
132 local effects
= playereffects
.get_player_effects(playername
)
134 local effect
= effects
[e
]
135 local thesegroups
= playereffects
.effect_types
[effect
.effect_type_id
].groups
136 minetest
.log("action", "thesegroups = "..dump(thesegroups
))
137 minetest
.log("action", "groupname = "..dump(groupname
))
139 for g
=1,#thesegroups
do
140 if(thesegroups
[g
] == groupname
) then
141 playereffects
.cancel_effect(effect
.effect_id
)
148 function playereffects
.cancel_effect(effect_id
)
149 local effect
= playereffects
.effects
[effect_id
]
150 if(effect
~= nil) then
151 local player
= minetest
.get_player_by_name(effect
.playername
)
152 if(effect
.hudids
.text_id
~=nil) then
153 player
:hud_remove(effect
.hudids
.text_id
)
155 if(effect
.hudids
.icon_id
~=nil) then
156 player
:hud_remove(effect
.hudids
.icon_id
)
158 playereffects
.effect_types
[effect
.effect_type_id
].cancel(effect
)
159 playereffects
.effects
[effect_id
] = nil
160 minetest
.log("action", "Effect type "..effect
.effect_type_id
.." cancelled from player "..effect
.playername
.."!")
164 function playereffects
.get_player_effects(playername
)
165 if(minetest
.get_player_by_name(playername
) ~= nil) then
167 for k
,v
in pairs(playereffects
.effects
) do
168 if(v
.playername
== playername
) then
169 table.insert(effects
, v
)
179 --[[ Cancel all effects on player death ]]
180 minetest
.register_on_dieplayer(function(player
)
181 local effects
= playereffects
.get_player_effects(player
:get_player_name())
183 playereffects
.cancel_effect(effects
[e
].effect_id
)
188 minetest
.register_on_leaveplayer(function(player
)
189 local leave_time
= os
.time()
190 local playername
= player
:get_player_name()
191 local effects
= playereffects
.get_player_effects(playername
)
193 playereffects
.hud_clear(player
)
195 if(playereffects
.inactive_effects
[playername
] == nil) then
196 playereffects
.inactive_effects
[playername
] = {}
199 local new_duration
= effects
[e
].time_left
- os
.difftime(leave_time
, effects
[e
].start_time
)
200 local new_effect
= effects
[e
]
201 new_effect
.time_left
= new_duration
202 table.insert(playereffects
.inactive_effects
[playername
], new_effect
)
203 playereffects
.cancel_effect(effects
[e
].effect_id
)
207 minetest
.register_on_shutdown(function()
208 minetest
.log("action", "[playereffects] Server shuts down. Rescuing data into playereffects.mt")
209 local shutdown_time
= os
.time()
211 local effects
= playereffects
.effects
212 local inactive_effects
= playereffects
.inactive_effects
213 for id
,effect
in pairs(effects
) do
214 local new_duration
= effect
.time_left
- os
.difftime(shutdown_time
, effect
.start_time
)
215 local new_effect
= effect
216 new_effect
.time_left
= new_duration
217 if(inactive_effects
[effect
.playername
] == nil) then
218 inactive_effects
[effect
.playername
] = {}
220 table.insert(inactive_effects
[effect
.playername
], new_effect
)
221 playereffects
.cancel_effect(effect
.effect_id
)
224 savetable
.inactive_effects
= inactive_effects
225 savetable
.last_effect_id
= playereffects
.last_effect_id
227 savestring
= minetest
.serialize(savetable
)
229 local filepath
= minetest
.get_worldpath().."/playereffects.mt"
230 local file
= io
.open(filepath
, "w")
232 file
:write(savestring
)
234 minetest
.log("action", "[playereffects] Wrote playereffects data into "..filepath
..".")
236 minetest
.log("error", "[playereffects] Failed to write playereffects data into "..filepath
..".")
241 minetest
.register_on_joinplayer(function(player
)
242 local playername
= player
:get_player_name()
244 -- load all the effects again (if any)
245 if(playereffects
.inactive_effects
[playername
] ~= nil) then
246 for i
=1,#playereffects
.inactive_effects
[playername
] do
247 local effect
= playereffects
.inactive_effects
[playername
][i
]
248 playereffects
.apply_effect_type(effect
.effect_type_id
, effect
.time_left
, player
)
250 playereffects
.inactive_effects
[playername
] = nil
254 playereffects
.globalstep_timer
= 0
255 minetest
.register_globalstep(function(dtime
)
256 playereffects
.globalstep_timer
= playereffects
.globalstep_timer
+ dtime
257 if(playereffects
.globalstep_timer
< 1) then
260 playereffects
.globalstep_timer
= 0
262 local players
= minetest
.get_connected_players()
264 playereffects
.hud_update(players
[p
])
272 function playereffects
.hud_update(player
)
273 local now
= os
.time()
274 local effects
= playereffects
.get_player_effects(player
:get_player_name())
276 local effect
= effects
[e
]
277 if(effect
.hudids
.text_id
~= nil) then
278 local description
= playereffects
.effect_types
[effect
.effect_type_id
].description
279 local time_left
= os
.difftime(effect
.start_time
+ effect
.time_left
, now
)
280 player
:hud_change(effect
.hudids
.text_id
, "text", description
.. " ("..tostring(time_left
).." s)")
285 function playereffects
.hud_clear(player
)
286 local playername
= player
:get_player_name()
287 local effects
= playereffects
.get_player_effects(playername
)
288 if(effects
~= nil) then
290 if(effects
[e
].hudids
.text_id
~= nil) then
291 player
:hud_remove(effects
[e
].hudids
.text_id
)
293 if(effects
[e
].hudids
.icon_id
~= nil) then
294 player
:hud_remove(effects
[e
].hudids
.icon_id
)
300 function playereffects
.hud_effect(effect_type_id
, player
, pos
, time_left
)
301 local text_id
, icon_id
302 text_id
= player
:hud_add({
303 hud_elem_type
= "text",
304 position
= { x
= 1, y
= 0.3 },
305 name
= "effect_"..effect_type_id
,
306 text
= playereffects
.effect_types
[effect_type_id
].description
.. " ("..tostring(time_left
).." s)",
307 scale
= { x
= 170, y
= 20},
308 alignment
= { x
= -1, y
= 0 },
311 offset
= { x
= -5, y
= pos
*20 }
313 if(playereffects
.effect_types
[effect_type_id
].icon
~= nil) then
314 icon_id
= player
:hud_add({
315 hud_elem_type
= "image",
316 scale
= { x
= 1, y
= 1 },
317 position
= { x
= 1, y
= 0.3 },
318 name
= "effect_icon_"..effect_type_id
,
319 text
= playereffects
.effect_types
[effect_type_id
].icon
,
320 alignment
= { x
= -1, y
=0 },
322 offset
= { x
= -186, y
= pos
*20 },
325 return { text_id
= text_id
, icon_id
= icon_id
}
330 dofile(minetest
.get_modpath(minetest
.get_current_modname()).."/examples.lua")