2014-07-06 01:04:03 +02:00
|
|
|
--[=[ Main tables ]=]
|
|
|
|
|
|
|
|
playereffects = {}
|
|
|
|
|
|
|
|
--[[ table containing the groups (experimental) ]]
|
|
|
|
playereffects.groups = {}
|
|
|
|
|
|
|
|
--[[ table containing all the effect types ]]
|
|
|
|
playereffects.effect_types = {}
|
|
|
|
|
|
|
|
--[[ table containing all the active effects ]]
|
|
|
|
playereffects.effects = {}
|
|
|
|
|
2014-07-06 08:01:09 +02:00
|
|
|
--[[ table containing all the inactive effects.
|
|
|
|
Effects become inactive if a player leaves an become active again if they join again. ]]
|
|
|
|
playereffects.inactive_effects = {}
|
|
|
|
|
2014-07-06 01:04:03 +02:00
|
|
|
-- Variable for counting the effect_id
|
|
|
|
playereffects.last_effect_id = 0
|
|
|
|
|
2014-07-13 21:57:45 +02:00
|
|
|
--[=[ Include settings ]=]
|
|
|
|
dofile(minetest.get_modpath("playereffects").."/settings.lua")
|
|
|
|
|
2014-07-06 21:16:25 +02:00
|
|
|
--[=[ Load inactive_effects and last_effect_id from playereffects.mt, if this file exists ]=]
|
|
|
|
do
|
|
|
|
local filepath = minetest.get_worldpath().."/playereffects.mt"
|
|
|
|
local file = io.open(filepath, "r")
|
|
|
|
local string
|
|
|
|
if file then
|
|
|
|
minetest.log("action", "[playereffects] playereffects.mt opened.")
|
|
|
|
local string = file:read()
|
|
|
|
io.close(file)
|
|
|
|
if(string ~= nil) then
|
|
|
|
savetable = minetest.deserialize(string)
|
|
|
|
playereffects.inactive_effects = savetable.inactive_effects
|
|
|
|
minetest.debug("[playereffects] playereffects.mt successfully read.")
|
|
|
|
minetest.debug("[playereffects] inactive_effects = "..dump(playereffects.inactive_effects))
|
|
|
|
playereffects.last_effect_id = savetable.last_effect_id
|
|
|
|
minetest.debug("[playereffects] last_effect_id = "..dump(playereffects.last_effect_id))
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-06 01:04:03 +02:00
|
|
|
function playereffects.next_effect_id()
|
|
|
|
playereffects.last_effect_id = playereffects.last_effect_id + 1
|
|
|
|
return playereffects.last_effect_id
|
|
|
|
end
|
|
|
|
|
|
|
|
--[=[ API functions ]=]
|
2014-07-07 09:38:55 +02:00
|
|
|
function playereffects.register_effect_type(name, description, icon, groups, apply, cancel)
|
2014-07-06 01:04:03 +02:00
|
|
|
effect_type = {}
|
|
|
|
effect_type.description = description
|
|
|
|
effect_type.apply = apply
|
|
|
|
effect_type.groups = groups
|
2014-07-07 09:38:55 +02:00
|
|
|
effect_type.icon = icon
|
2014-07-06 01:04:03 +02:00
|
|
|
if cancel ~= nil then
|
|
|
|
effect_type.cancel = cancel
|
|
|
|
else
|
|
|
|
effect_type.cancel = function() end
|
|
|
|
end
|
|
|
|
playereffects.effect_types[name] = effect_type
|
|
|
|
minetest.log("action", "Effect type "..name.." registered!")
|
|
|
|
end
|
|
|
|
|
|
|
|
function playereffects.apply_effect_type(effect_type_id, duration, player)
|
|
|
|
local start_time = os.time()
|
|
|
|
local playername = player:get_player_name()
|
|
|
|
local groups = playereffects.effect_types[effect_type_id].groups
|
|
|
|
for k,v in pairs(groups) do
|
|
|
|
playereffects.cancel_effect_group(v, playername)
|
|
|
|
end
|
|
|
|
local effect_id = playereffects.next_effect_id()
|
2014-07-06 18:04:29 +02:00
|
|
|
local effects = playereffects.get_player_effects(playername)
|
|
|
|
local smallest_hudpos
|
|
|
|
local biggest_hudpos = -1
|
|
|
|
local free_hudpos
|
|
|
|
for e=1,#effects do
|
|
|
|
local hudpos = effects[e].hudpos
|
|
|
|
if(hudpos > biggest_hudpos) then
|
|
|
|
biggest_hudpos = hudpos
|
|
|
|
end
|
|
|
|
if(smallest_hudpos == nil) then
|
|
|
|
smallest_hudpos = hudpos
|
2014-07-06 18:30:32 +02:00
|
|
|
elseif(hudpos < smallest_hudpos) then
|
2014-07-06 18:04:29 +02:00
|
|
|
smallest_hudpos = hudpos
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if(smallest_hudpos == nil) then
|
|
|
|
free_hudpos = 0
|
|
|
|
elseif(smallest_hudpos >= 0) then
|
|
|
|
free_hudpos = smallest_hudpos - 1
|
|
|
|
else
|
|
|
|
free_hudpos = biggest_hudpos + 1
|
|
|
|
end
|
2014-07-07 10:55:18 +02:00
|
|
|
local hudids
|
|
|
|
--[[ show no more than 20 effects on the screen, so that hud_update does not need to be called so often ]]
|
|
|
|
if(free_hudpos <= 20) then
|
|
|
|
hudids = playereffects.hud_effect(effect_type_id, player, free_hudpos, duration)
|
|
|
|
else
|
|
|
|
hudids = {text_id=nil, icon_id=nil}
|
|
|
|
end
|
2014-07-06 18:04:29 +02:00
|
|
|
|
2014-07-06 01:04:03 +02:00
|
|
|
local effect = {
|
|
|
|
playername = playername,
|
|
|
|
effect_id = effect_id,
|
|
|
|
effect_type_id = effect_type_id,
|
|
|
|
start_time = start_time,
|
|
|
|
time_left = duration,
|
2014-07-07 09:38:55 +02:00
|
|
|
hudids = hudids,
|
2014-07-06 18:04:29 +02:00
|
|
|
hudpos = free_hudpos,
|
2014-07-06 01:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
playereffects.effects[effect_id] = effect
|
|
|
|
|
|
|
|
playereffects.effect_types[effect_type_id].apply(player)
|
|
|
|
minetest.log("action", "Effect type "..effect_type_id.." applied to player "..playername.."!")
|
|
|
|
minetest.after(duration, function(effect_id) playereffects.cancel_effect(effect_id) end, effect_id)
|
|
|
|
end
|
|
|
|
|
2014-07-13 21:27:59 +02:00
|
|
|
function playereffects.cancel_effect_type(effect_type_id, cancel_all, playername)
|
|
|
|
local effects = playereffects.get_player_effects(playername)
|
|
|
|
if(cancel_all==nil) then all = false end
|
|
|
|
for e=1, #effects do
|
|
|
|
if(effects[e].effect_type_id == effect_type_id) then
|
|
|
|
playereffects.cancel_effect(effects[e].effect_id)
|
|
|
|
if(cancel_all==false) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-07-06 01:04:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function playereffects.cancel_effect_group(groupname, playername)
|
|
|
|
local effects = playereffects.get_player_effects(playername)
|
|
|
|
for e=1,#effects do
|
|
|
|
local effect = effects[e]
|
|
|
|
local thesegroups = playereffects.effect_types[effect.effect_type_id].groups
|
|
|
|
minetest.log("action", "thesegroups = "..dump(thesegroups))
|
|
|
|
minetest.log("action", "groupname = "..dump(groupname))
|
|
|
|
local delete = false
|
|
|
|
for g=1,#thesegroups do
|
|
|
|
if(thesegroups[g] == groupname) then
|
|
|
|
playereffects.cancel_effect(effect.effect_id)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function playereffects.cancel_effect(effect_id)
|
|
|
|
local effect = playereffects.effects[effect_id]
|
|
|
|
if(effect ~= nil) then
|
|
|
|
local player = minetest.get_player_by_name(effect.playername)
|
2014-07-07 10:55:18 +02:00
|
|
|
if(effect.hudids.text_id~=nil) then
|
|
|
|
player:hud_remove(effect.hudids.text_id)
|
|
|
|
end
|
2014-07-07 09:38:55 +02:00
|
|
|
if(effect.hudids.icon_id~=nil) then
|
|
|
|
player:hud_remove(effect.hudids.icon_id)
|
|
|
|
end
|
2014-07-06 01:04:03 +02:00
|
|
|
playereffects.effect_types[effect.effect_type_id].cancel(effect)
|
|
|
|
playereffects.effects[effect_id] = nil
|
2014-07-06 08:01:09 +02:00
|
|
|
minetest.log("action", "Effect type "..effect.effect_type_id.." cancelled from player "..effect.playername.."!")
|
2014-07-06 01:04:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function playereffects.get_player_effects(playername)
|
|
|
|
if(minetest.get_player_by_name(playername) ~= nil) then
|
|
|
|
local effects = {}
|
|
|
|
for k,v in pairs(playereffects.effects) do
|
|
|
|
if(v.playername == playername) then
|
|
|
|
table.insert(effects, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return effects
|
|
|
|
else
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--[=[ Callbacks ]=]
|
|
|
|
--[[ Cancel all effects on player death ]]
|
|
|
|
minetest.register_on_dieplayer(function(player)
|
|
|
|
local effects = playereffects.get_player_effects(player:get_player_name())
|
|
|
|
for e=1,#effects do
|
|
|
|
playereffects.cancel_effect(effects[e].effect_id)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2014-07-06 08:01:09 +02:00
|
|
|
|
2014-07-06 01:04:03 +02:00
|
|
|
minetest.register_on_leaveplayer(function(player)
|
2014-07-06 08:01:09 +02:00
|
|
|
local leave_time = os.time()
|
|
|
|
local playername = player:get_player_name()
|
|
|
|
local effects = playereffects.get_player_effects(playername)
|
|
|
|
|
2014-07-06 01:04:03 +02:00
|
|
|
playereffects.hud_clear(player)
|
2014-07-06 08:01:09 +02:00
|
|
|
|
|
|
|
if(playereffects.inactive_effects[playername] == nil) then
|
|
|
|
playereffects.inactive_effects[playername] = {}
|
|
|
|
end
|
|
|
|
for e=1,#effects do
|
2014-07-06 21:16:25 +02:00
|
|
|
local new_duration = effects[e].time_left - os.difftime(leave_time, effects[e].start_time)
|
2014-07-06 08:01:09 +02:00
|
|
|
local new_effect = effects[e]
|
|
|
|
new_effect.time_left = new_duration
|
|
|
|
table.insert(playereffects.inactive_effects[playername], new_effect)
|
|
|
|
playereffects.cancel_effect(effects[e].effect_id)
|
|
|
|
end
|
2014-07-06 01:04:03 +02:00
|
|
|
end)
|
|
|
|
|
2014-07-06 21:16:25 +02:00
|
|
|
minetest.register_on_shutdown(function()
|
|
|
|
minetest.log("action", "[playereffects] Server shuts down. Rescuing data into playereffects.mt")
|
|
|
|
local shutdown_time = os.time()
|
|
|
|
local savetable = {}
|
|
|
|
local effects = playereffects.effects
|
|
|
|
local inactive_effects = playereffects.inactive_effects
|
|
|
|
for id,effect in pairs(effects) do
|
|
|
|
local new_duration = effect.time_left - os.difftime(shutdown_time, effect.start_time)
|
|
|
|
local new_effect = effect
|
|
|
|
new_effect.time_left = new_duration
|
|
|
|
if(inactive_effects[effect.playername] == nil) then
|
|
|
|
inactive_effects[effect.playername] = {}
|
|
|
|
end
|
|
|
|
table.insert(inactive_effects[effect.playername], new_effect)
|
|
|
|
playereffects.cancel_effect(effect.effect_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
savetable.inactive_effects = inactive_effects
|
|
|
|
savetable.last_effect_id = playereffects.last_effect_id
|
|
|
|
|
|
|
|
savestring = minetest.serialize(savetable)
|
|
|
|
|
|
|
|
local filepath = minetest.get_worldpath().."/playereffects.mt"
|
|
|
|
local file = io.open(filepath, "w")
|
|
|
|
if file then
|
|
|
|
file:write(savestring)
|
|
|
|
io.close(file)
|
|
|
|
minetest.log("action", "[playereffects] Wrote playereffects data into "..filepath..".")
|
|
|
|
else
|
|
|
|
minetest.log("error", "[playereffects] Failed to write playereffects data into "..filepath..".")
|
|
|
|
end
|
|
|
|
|
|
|
|
end)
|
2014-07-06 08:01:09 +02:00
|
|
|
|
2014-07-06 01:04:03 +02:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
2014-07-06 08:01:09 +02:00
|
|
|
local playername = player:get_player_name()
|
|
|
|
|
|
|
|
-- load all the effects again (if any)
|
|
|
|
if(playereffects.inactive_effects[playername] ~= nil) then
|
|
|
|
for i=1,#playereffects.inactive_effects[playername] do
|
|
|
|
local effect = playereffects.inactive_effects[playername][i]
|
|
|
|
playereffects.apply_effect_type(effect.effect_type_id, effect.time_left, player)
|
|
|
|
end
|
|
|
|
playereffects.inactive_effects[playername] = nil
|
|
|
|
end
|
2014-07-06 23:17:53 +02:00
|
|
|
end)
|
2014-07-06 01:04:03 +02:00
|
|
|
|
2014-07-07 10:39:21 +02:00
|
|
|
playereffects.globalstep_timer = 0
|
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
playereffects.globalstep_timer = playereffects.globalstep_timer + dtime
|
|
|
|
if(playereffects.globalstep_timer < 1) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
playereffects.globalstep_timer = 0
|
|
|
|
|
2014-07-06 18:04:29 +02:00
|
|
|
local players = minetest.get_connected_players()
|
|
|
|
for p=1,#players do
|
|
|
|
playereffects.hud_update(players[p])
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2014-07-06 21:16:25 +02:00
|
|
|
|
|
|
|
|
2014-07-06 01:04:03 +02:00
|
|
|
|
|
|
|
--[=[ HUD ]=]
|
|
|
|
function playereffects.hud_update(player)
|
2014-07-13 21:57:45 +02:00
|
|
|
if(playereffects.use_hud == true) then
|
|
|
|
local now = os.time()
|
|
|
|
local effects = playereffects.get_player_effects(player:get_player_name())
|
|
|
|
for e=1,#effects do
|
|
|
|
local effect = effects[e]
|
|
|
|
if(effect.hudids.text_id ~= nil) then
|
|
|
|
local description = playereffects.effect_types[effect.effect_type_id].description
|
|
|
|
local time_left = os.difftime(effect.start_time + effect.time_left, now)
|
|
|
|
player:hud_change(effect.hudids.text_id, "text", description .. " ("..tostring(time_left).." s)")
|
|
|
|
end
|
2014-07-07 10:55:18 +02:00
|
|
|
end
|
2014-07-06 01:04:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function playereffects.hud_clear(player)
|
2014-07-13 21:57:45 +02:00
|
|
|
if(playereffects.use_hud == true) then
|
|
|
|
local playername = player:get_player_name()
|
|
|
|
local effects = playereffects.get_player_effects(playername)
|
|
|
|
if(effects ~= nil) then
|
|
|
|
for e=1,#effects do
|
|
|
|
if(effects[e].hudids.text_id ~= nil) then
|
|
|
|
player:hud_remove(effects[e].hudids.text_id)
|
|
|
|
end
|
|
|
|
if(effects[e].hudids.icon_id ~= nil) then
|
|
|
|
player:hud_remove(effects[e].hudids.icon_id)
|
|
|
|
end
|
2014-07-07 09:38:55 +02:00
|
|
|
end
|
2014-07-06 01:04:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-06 22:52:40 +02:00
|
|
|
function playereffects.hud_effect(effect_type_id, player, pos, time_left)
|
2014-07-07 09:38:55 +02:00
|
|
|
local text_id, icon_id
|
2014-07-13 21:57:45 +02:00
|
|
|
if(playereffects.use_hud == true) then
|
|
|
|
text_id = player:hud_add({
|
|
|
|
hud_elem_type = "text",
|
2014-07-07 09:38:55 +02:00
|
|
|
position = { x = 1, y = 0.3 },
|
2014-07-13 21:57:45 +02:00
|
|
|
name = "effect_"..effect_type_id,
|
|
|
|
text = playereffects.effect_types[effect_type_id].description .. " ("..tostring(time_left).." s)",
|
|
|
|
scale = { x = 170, y = 20},
|
|
|
|
alignment = { x = -1, y = 0 },
|
|
|
|
direction = 1,
|
|
|
|
number = 0xFFFFFF,
|
|
|
|
offset = { x = -5, y = pos*20 }
|
2014-07-07 09:38:55 +02:00
|
|
|
})
|
2014-07-13 21:57:45 +02:00
|
|
|
if(playereffects.effect_types[effect_type_id].icon ~= nil) then
|
|
|
|
icon_id = player:hud_add({
|
|
|
|
hud_elem_type = "image",
|
|
|
|
scale = { x = 1, y = 1 },
|
|
|
|
position = { x = 1, y = 0.3 },
|
|
|
|
name = "effect_icon_"..effect_type_id,
|
|
|
|
text = playereffects.effect_types[effect_type_id].icon,
|
|
|
|
alignment = { x = -1, y=0 },
|
|
|
|
direction = 0,
|
|
|
|
offset = { x = -186, y = pos*20 },
|
|
|
|
})
|
|
|
|
end
|
|
|
|
else
|
|
|
|
text_id = nil
|
|
|
|
icon_id = nil
|
|
|
|
end
|
2014-07-07 09:38:55 +02:00
|
|
|
return { text_id = text_id, icon_id = icon_id }
|
2014-07-06 01:04:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-07-06 23:17:53 +02:00
|
|
|
-- LOAD EXAMPLES
|
2014-07-06 21:16:25 +02:00
|
|
|
dofile(minetest.get_modpath(minetest.get_current_modname()).."/examples.lua")
|