2015-10-24 02:03:12 +02:00
|
|
|
--
|
|
|
|
-- Player effects mod
|
|
|
|
-- By Kaadmy, for Pixture
|
|
|
|
--
|
|
|
|
|
2019-08-28 17:31:41 +02:00
|
|
|
local S = minetest.get_translator("player_effects")
|
2020-01-31 02:17:31 +01:00
|
|
|
local DISPLAY_ICONS = false
|
2019-08-28 17:31:41 +02:00
|
|
|
|
2015-10-24 02:03:12 +02:00
|
|
|
player_effects = {}
|
|
|
|
|
|
|
|
player_effects.effects = {}
|
|
|
|
player_effects.registered_effects = {}
|
|
|
|
|
2017-05-12 06:24:54 +02:00
|
|
|
local effects_file = minetest.get_worldpath() .. "/player_effects.dat"
|
2017-05-15 01:06:34 +02:00
|
|
|
local timer_interval = 1 -- update every second
|
|
|
|
local timer = 10
|
2015-10-24 02:03:12 +02:00
|
|
|
|
|
|
|
local function save_effects()
|
|
|
|
local f = io.open(effects_file, "w")
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
f:write(minetest.serialize(player_effects.effects))
|
2015-10-24 02:03:12 +02:00
|
|
|
|
|
|
|
io.close(f)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function load_effects()
|
|
|
|
local f = io.open(effects_file, "r")
|
|
|
|
|
|
|
|
if f then
|
2017-05-12 04:29:55 +02:00
|
|
|
player_effects.effects = minetest.deserialize(f:read("*all"))
|
2015-10-24 02:03:12 +02:00
|
|
|
|
|
|
|
io.close(f)
|
|
|
|
else
|
|
|
|
save_effects()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-31 01:14:59 +01:00
|
|
|
local huds = {}
|
|
|
|
|
|
|
|
local function display_effect_icons(player)
|
|
|
|
if not DISPLAY_ICONS then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local name = player:get_player_name()
|
|
|
|
for _,h in pairs(huds[name]) do
|
|
|
|
player:hud_remove(h)
|
|
|
|
end
|
|
|
|
huds[name] = {}
|
|
|
|
local i = 0
|
|
|
|
for en, _ in pairs(player_effects.effects[name]) do
|
|
|
|
local effect = player_effects.get_registered_effect(en)
|
|
|
|
if effect.icon then
|
|
|
|
local id = player:hud_add({
|
|
|
|
hud_elem_type = "image",
|
|
|
|
position = { x = 1, y = 0 },
|
|
|
|
offset = { x = -52 - i*52, y = 270 },
|
|
|
|
text = effect.icon,
|
|
|
|
scale = { x = 3, y = 3 },
|
|
|
|
size = { x = 16, y = 16 },
|
|
|
|
alignment = { x = 1, y = 1 },
|
2020-04-07 14:39:05 +02:00
|
|
|
z_index = 10,
|
2020-01-31 01:14:59 +01:00
|
|
|
})
|
|
|
|
table.insert(huds[name], id)
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-24 02:03:12 +02:00
|
|
|
function player_effects.register_effect(name, def)
|
|
|
|
local rd = {
|
|
|
|
title = def.title or name, -- good-looking name of the effect
|
2019-08-28 17:31:41 +02:00
|
|
|
description = def.description or S("The @1 effect", name), -- description of what the effect does
|
2015-10-24 02:03:12 +02:00
|
|
|
duration = def.duration or 1, -- how long the effect lasts, <0 is infinite and has to be disabled manually
|
2020-01-31 01:14:59 +01:00
|
|
|
physics = def.physics or {}, -- physics overrides for the player
|
2020-01-31 01:33:33 +01:00
|
|
|
icon = def.icon, -- effect icon for HUD (optional)
|
|
|
|
save = def.save, -- if true, effect will be preserved after server shutdown (default: true)
|
2015-10-24 02:03:12 +02:00
|
|
|
}
|
2020-01-31 01:33:33 +01:00
|
|
|
if rd.save == nil then
|
|
|
|
rd.save = true
|
|
|
|
end
|
2015-10-24 02:03:12 +02:00
|
|
|
|
|
|
|
player_effects.registered_effects[name] = rd
|
|
|
|
end
|
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
function player_effects.get_registered_effect(ename)
|
|
|
|
local e = player_effects.registered_effects[ename]
|
2017-05-12 06:24:54 +02:00
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
if not e then
|
|
|
|
default.log("[mod:player_effects] Cannot find registered player effect " .. ename, "error")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return e
|
|
|
|
end
|
|
|
|
|
2015-10-24 02:03:12 +02:00
|
|
|
function player_effects.apply_effect(player, ename)
|
2015-10-24 20:36:31 +02:00
|
|
|
local effect = player_effects.get_registered_effect(ename)
|
|
|
|
|
|
|
|
if effect.duration >= 0 then
|
2017-05-12 04:29:55 +02:00
|
|
|
player_effects.effects[player:get_player_name()][ename] = minetest.get_gametime() + effect.duration
|
2015-10-24 20:36:31 +02:00
|
|
|
else
|
|
|
|
player_effects.effects[player:get_player_name()][ename] = -1
|
|
|
|
end
|
|
|
|
|
|
|
|
local phys = {speed = 1, jump = 1, gravity = 1}
|
|
|
|
|
|
|
|
for en, _ in pairs(player_effects.effects[player:get_player_name()]) do
|
|
|
|
local effect = player_effects.get_registered_effect(en)
|
2015-10-24 02:03:12 +02:00
|
|
|
|
|
|
|
if effect.physics.speed ~= nil then
|
|
|
|
phys.speed = phys.speed * effect.physics.speed
|
|
|
|
end
|
|
|
|
|
|
|
|
if effect.physics.jump ~= nil then
|
|
|
|
phys.jump = phys.jump * effect.physics.jump
|
|
|
|
end
|
|
|
|
|
|
|
|
if effect.physics.gravity ~= nil then
|
|
|
|
phys.gravity = phys.gravity * effect.physics.gravity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
player:set_physics_override(phys)
|
2020-01-31 01:14:59 +01:00
|
|
|
display_effect_icons(player)
|
2015-10-24 02:03:12 +02:00
|
|
|
|
|
|
|
save_effects()
|
|
|
|
end
|
|
|
|
|
|
|
|
function player_effects.remove_effect(player, ename)
|
|
|
|
if player_effects.effects[player:get_player_name()][ename] == nil then return end
|
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
local phys = {speed = 1, jump = 1, gravity = 1}
|
2015-10-24 02:03:12 +02:00
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
for en, _ in pairs(player_effects.effects[player:get_player_name()]) do
|
|
|
|
if en ~= ename then
|
|
|
|
local effect = player_effects.get_registered_effect(en)
|
|
|
|
|
|
|
|
if effect.physics.speed ~= nil then
|
|
|
|
phys.speed = phys.speed * effect.physics.speed
|
|
|
|
end
|
|
|
|
|
|
|
|
if effect.physics.jump ~= nil then
|
|
|
|
phys.jump = phys.jump * effect.physics.jump
|
|
|
|
end
|
2015-10-24 02:03:12 +02:00
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
if effect.physics.gravity ~= nil then
|
|
|
|
phys.gravity = phys.gravity * effect.physics.gravity
|
|
|
|
end
|
|
|
|
end
|
2015-10-24 02:03:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
player:set_physics_override(phys)
|
|
|
|
|
|
|
|
player_effects.effects[player:get_player_name()][ename] = nil
|
2020-01-31 01:14:59 +01:00
|
|
|
display_effect_icons(player)
|
2015-10-24 02:03:12 +02:00
|
|
|
|
|
|
|
save_effects()
|
|
|
|
end
|
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
function player_effects.refresh_effects(player)
|
2015-10-25 22:18:54 +01:00
|
|
|
local phys = {speed = 1, jump = 1, gravity = 1}
|
2015-10-24 20:36:31 +02:00
|
|
|
|
2020-01-31 01:33:33 +01:00
|
|
|
local clear = {}
|
|
|
|
local name = player:get_player_name()
|
|
|
|
for en, _ in pairs(player_effects.effects[name]) do
|
2015-11-15 17:10:48 +01:00
|
|
|
local effect = player_effects.get_registered_effect(en)
|
2020-01-31 01:33:33 +01:00
|
|
|
if effect.save == false then
|
|
|
|
table.insert(clear, en)
|
|
|
|
else
|
|
|
|
if effect.physics.speed ~= nil then
|
|
|
|
phys.speed = phys.speed * effect.physics.speed
|
|
|
|
end
|
|
|
|
|
|
|
|
if effect.physics.jump ~= nil then
|
|
|
|
phys.jump = phys.jump * effect.physics.jump
|
|
|
|
end
|
|
|
|
|
|
|
|
if effect.physics.gravity ~= nil then
|
|
|
|
phys.gravity = phys.gravity * effect.physics.gravity
|
|
|
|
end
|
2015-10-24 20:36:31 +02:00
|
|
|
end
|
|
|
|
end
|
2020-01-31 01:33:33 +01:00
|
|
|
for e=1, #clear do
|
|
|
|
player_effects.effects[name][clear[e]] = nil
|
|
|
|
end
|
2015-10-24 20:36:31 +02:00
|
|
|
|
2015-10-25 22:18:54 +01:00
|
|
|
player:set_physics_override(phys)
|
2015-10-24 20:36:31 +02:00
|
|
|
|
2020-01-31 01:14:59 +01:00
|
|
|
display_effect_icons(player)
|
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
save_effects()
|
|
|
|
end
|
|
|
|
|
2015-10-24 02:03:12 +02:00
|
|
|
function player_effects.clear_effects(player)
|
|
|
|
-- call this if you want to clear all effects, it's faster and more efficient
|
|
|
|
player:set_physics_override({speed = 1, jump = 1, gravity = 1})
|
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
player_effects.effects[player:get_player_name()] = {}
|
2020-01-31 01:14:59 +01:00
|
|
|
display_effect_icons(player)
|
2015-10-24 02:03:12 +02:00
|
|
|
|
|
|
|
save_effects()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function step(dtime)
|
|
|
|
timer = timer + dtime
|
2017-05-15 01:06:34 +02:00
|
|
|
|
|
|
|
if timer < timer_interval then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
timer = 0
|
|
|
|
|
|
|
|
local gt = minetest.get_gametime()
|
|
|
|
|
2019-08-30 20:32:18 +02:00
|
|
|
if player_effects.effects == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-05-15 01:06:34 +02:00
|
|
|
for _, player in pairs(minetest.get_connected_players()) do
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
for ename, endtime in pairs(player_effects.effects[name]) do
|
|
|
|
if endtime > 0 then
|
|
|
|
local timeleft = endtime - gt
|
|
|
|
if timeleft <= 0 then
|
|
|
|
player_effects.remove_effect(player, ename)
|
|
|
|
end
|
|
|
|
end
|
2015-10-24 02:03:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_joinplayer(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
load_effects()
|
|
|
|
|
|
|
|
if player_effects.effects[name] == nil then
|
|
|
|
player_effects.effects[name] = {}
|
|
|
|
end
|
2020-01-31 01:14:59 +01:00
|
|
|
huds[name] = {}
|
2015-10-24 02:03:12 +02:00
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
player_effects.refresh_effects(player)
|
2015-10-24 02:03:12 +02:00
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
save_effects()
|
2015-10-24 02:03:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_leaveplayer(player)
|
|
|
|
save_effects()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_dieplayer(player)
|
|
|
|
player_effects.clear_effects(player)
|
|
|
|
end
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_globalstep(step)
|
|
|
|
minetest.register_on_joinplayer(on_joinplayer)
|
|
|
|
minetest.register_on_leaveplayer(on_leaveplayer)
|
|
|
|
minetest.register_on_dieplayer(on_dieplayer)
|
2015-10-24 02:03:12 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_chatcommand(
|
2015-10-24 02:03:12 +02:00
|
|
|
"player_effects",
|
|
|
|
{
|
2019-08-28 17:31:41 +02:00
|
|
|
description = S("Show your current player effects"),
|
2015-10-24 02:03:12 +02:00
|
|
|
func = function(name, param)
|
2019-08-28 17:31:41 +02:00
|
|
|
local s = S("Current player effects:").."\n"
|
2017-05-15 01:06:34 +02:00
|
|
|
local ea = 0
|
|
|
|
|
|
|
|
for ename, endtime in pairs(player_effects.effects[name]) do
|
|
|
|
if endtime < 0 then
|
2019-08-28 17:31:41 +02:00
|
|
|
s = s .. " " .. S("@1: unlimited", player_effects.registered_effects[ename].title) .. "\n"
|
2017-05-15 01:06:34 +02:00
|
|
|
else
|
2019-08-28 17:31:41 +02:00
|
|
|
s = s .. " " .. S("@1: @2 s remaining", player_effects.registered_effects[ename].title, (endtime - minetest.get_gametime())) .. "\n"
|
2017-05-15 01:06:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
ea = ea + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if ea > 0 then
|
2019-06-23 21:05:52 +02:00
|
|
|
return true, s
|
2017-05-15 01:06:34 +02:00
|
|
|
else
|
2019-08-28 17:31:41 +02:00
|
|
|
return true, S("You currently have no effects.")
|
2017-05-15 01:06:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
2015-10-24 02:03:12 +02:00
|
|
|
|
2017-05-12 06:24:54 +02:00
|
|
|
default.log("mod:player_effects", "loaded")
|