Improved /effect command and fixes

-improved the /effect command, allowing to use effect levels
-fixed a bug in level-to-factor conversions
-renamed effect icons to follow the new convention
This commit is contained in:
the-real-herowl 2023-10-10 01:07:26 +02:00
parent 8477d13c79
commit 5750284e8f
11 changed files with 74 additions and 42 deletions

View file

@ -8,23 +8,9 @@ local S = minetest.get_translator(minetest.get_current_modname())
-- ░╚════╝░╚═╝░░╚═╝╚═╝░░╚═╝░░░╚═╝░░░  ░╚════╝░░╚════╝░╚═╝░░░░░╚═╝╚═╝░░░░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝╚═════╝░╚═════╝░ -- ░╚════╝░╚═╝░░╚═╝╚═╝░░╚═╝░░░╚═╝░░░  ░╚════╝░░╚════╝░╚═╝░░░░░╚═╝╚═╝░░░░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝╚═════╝░╚═════╝░
local get_chat_function = {}
get_chat_function["poison"] = mcl_potions.poison_func
get_chat_function["regeneration"] = mcl_potions.regeneration_func
get_chat_function["invisibility"] = mcl_potions.invisiblility_func
get_chat_function["fire_resistance"] = mcl_potions.fire_resistance_func
get_chat_function["night_vision"] = mcl_potions.night_vision_func
get_chat_function["water_breathing"] = mcl_potions.water_breathing_func
get_chat_function["leaping"] = mcl_potions.leaping_func
get_chat_function["swiftness"] = mcl_potions.swiftness_func
get_chat_function["heal"] = mcl_potions.healing_func
get_chat_function["bad_omen"] = mcl_potions.bad_omen_func
get_chat_function["withering"] = mcl_potions.withering_func
minetest.register_chatcommand("effect",{ minetest.register_chatcommand("effect",{
params = S("<effect> <duration> [<factor>]"), params = S("<effect> <duration> [<level>] [<factor>]"),
description = S("Add a status effect to yourself. Arguments: <effect>: name of status effect, e.g. poison. <duration>: duration in seconds. <factor>: effect strength multiplier (1 = 100%)"), description = S("Add a status effect to yourself. Arguments: <effect>: name of status effect, e.g. poison. <duration>: duration in seconds. <level>: effect power determinant, bigger level results in more powerful effect for effects that depend on the level, defaults to 1, pass F to use low-level factor instead. <factor>: effect strength modifier, can mean different things depending on the effect."),
privs = {server = true}, privs = {server = true},
func = function(name, params) func = function(name, params)
@ -37,22 +23,46 @@ minetest.register_chatcommand("effect",{
if not P[1] then if not P[1] then
return false, S("Missing effect parameter!") return false, S("Missing effect parameter!")
elseif not tonumber(P[2]) then elseif P[1] == "list" then
local regs = mcl_potions.get_registered_effects()
local effects = "heal"
for name, _ in pairs(regs) do
effects = effects .. ", " .. name
end
return true, effects
elseif not tonumber(P[2])then
return false, S("Missing or invalid duration parameter!") return false, S("Missing or invalid duration parameter!")
elseif P[3] and not tonumber(P[3]) then elseif P[3] and not tonumber(P[3]) and P[3] ~= "F" then
return false, S("Invalid factor parameter!") return false, S("Invalid level parameter!")
end elseif P[3] and P[3] == "F" and not P[4] then
-- Default factor = 1 return false, S("Missing or invalid factor parameter when level is F!")
if not P[3] then
P[3] = 1.0
end end
if get_chat_function[P[1]] then -- Default level = 1
get_chat_function[P[1]](minetest.get_player_by_name(name), tonumber(P[3]), tonumber(P[2])) if not P[3] then
return true P[3] = 1
end
if mcl_potions.is_effect_registered(P[1]) then
if P[3] == "F" then
local given = mcl_potions.give_effect(P[1], minetest.get_player_by_name(name), tonumber(P[4]), tonumber(P[2]))
if given then
return true, S("@1 effect given to player @2 for @3 seconds with factor of @4.", P[1], name, P[2], P[4])
else
return false, S("Giving effect @1 to player @2 failed.", P[1], name)
end
else
local given = mcl_potions.give_effect_by_level(P[1], minetest.get_player_by_name(name), tonumber(P[3]), tonumber(P[2]))
if given then
return true, S("@1 effect on level @2 given to player @3 for @4 seconds.", P[1], P[3], name, P[2])
else
return false, S("Giving effect @1 to player @2 failed.", P[1], name)
end
end
else else
return false, S("@1 is not an available status effect.", P[1]) return false, S("@1 is not an available status effect.", P[1])
end end
end, end,
}) })

View file

@ -48,7 +48,7 @@ local function generate_linear_lvl_to_fac(l1, l2)
end end
local function generate_rational_lvl_to_fac(l1, l2) local function generate_rational_lvl_to_fac(l1, l2)
local a = (l1 - l2) / 1.5 local a = (l1 - l2) * 2
local b = 2*l2 - l1 local b = 2*l2 - l1
return function(level) return function(level)
if level == 0 then return 0 end if level == 0 then return 0 end
@ -89,6 +89,15 @@ function mcl_potions.register_effect(def)
if def.name == nil then if def.name == nil then
error("Unable to register effect: name is nil") error("Unable to register effect: name is nil")
end end
if def.name == "list" then
error("Unable to register effect: list is a reserved word")
end
if def.name == "heal" then
error("Unable to register effect: heal is a reserved word")
end
if registered_effects[name] then
error("Effect named "..name.." already registered!")
end
local name = def.name local name = def.name
local pdef = {} local pdef = {}
if not def.icon then if not def.icon then
@ -141,6 +150,18 @@ function mcl_potions.register_effect(def)
EF[name] = {} EF[name] = {}
end end
function mcl_potions.get_registered_effects()
return table.copy(registered_effects)
end
function mcl_potions.is_effect_registered(name)
if registered_effects[name] then
return true
else
return false
end
end
mcl_potions.register_effect({ mcl_potions.register_effect({
name = "invisibility", name = "invisibility",
on_start = function(object, factor) on_start = function(object, factor)
@ -891,7 +912,7 @@ function mcl_potions._load_player_effects(player)
-- new API effects + on_load for loaded legacy effects -- new API effects + on_load for loaded legacy effects
for name, effect in pairs(registered_effects) do for name, effect in pairs(registered_effects) do
local loaded = minetest.deserialize(meta:get_string("mcl_potions:"..name)) local loaded = minetest.deserialize(meta:get_string("mcl_potions:_EF_"..name))
if loaded then EF[name][player] = loaded end if loaded then EF[name][player] = loaded end
if EF[name][player] and effect.on_load then if EF[name][player] and effect.on_load then
effect.on_load(player, EF[name][player].factor) effect.on_load(player, EF[name][player].factor)
@ -1133,10 +1154,8 @@ local function target_valid(object, name)
end end
function mcl_potions.give_effect(name, object, factor, duration) function mcl_potions.give_effect(name, object, factor, duration)
if not target_valid(object, name) then return false end
local edef = registered_effects[name] local edef = registered_effects[name]
if not edef then return false end if not edef or not target_valid(object, name) then return false end
if not EF[name][object] then if not EF[name][object] then
local vals = {dur = duration, timer = 0,} local vals = {dur = duration, timer = 0,}
if edef.uses_factor then vals.factor = factor end if edef.uses_factor then vals.factor = factor end
@ -1160,6 +1179,8 @@ function mcl_potions.give_effect(name, object, factor, duration)
end end
if object:is_player() then potions_set_hud(object) end if object:is_player() then potions_set_hud(object) end
return true
end end
function mcl_potions.give_effect_by_level(name, object, level, duration) function mcl_potions.give_effect_by_level(name, object, level, duration)

View file

@ -462,17 +462,18 @@ function mcl_potions.get_alchemy(ingr, pot)
return false return false
end end
mcl_mobs.effect_functions["poison"] = mcl_potions.poison_func -- TODO replace all calls to the old API with new API calls in other mods
mcl_mobs.effect_functions["regeneration"] = mcl_potions.regeneration_func -- mcl_mobs.effect_functions["poison"] = mcl_potions.poison_func
mcl_mobs.effect_functions["invisibility"] = mcl_potions.invisiblility_func -- mcl_mobs.effect_functions["regeneration"] = mcl_potions.regeneration_func
mcl_mobs.effect_functions["fire_resistance"] = mcl_potions.fire_resistance_func -- mcl_mobs.effect_functions["invisibility"] = mcl_potions.invisiblility_func
mcl_mobs.effect_functions["night_vision"] = mcl_potions.night_vision_func -- mcl_mobs.effect_functions["fire_resistance"] = mcl_potions.fire_resistance_func
mcl_mobs.effect_functions["water_breathing"] = mcl_potions.water_breathing_func -- mcl_mobs.effect_functions["night_vision"] = mcl_potions.night_vision_func
mcl_mobs.effect_functions["leaping"] = mcl_potions.leaping_func -- mcl_mobs.effect_functions["water_breathing"] = mcl_potions.water_breathing_func
mcl_mobs.effect_functions["swiftness"] = mcl_potions.swiftness_func -- mcl_mobs.effect_functions["leaping"] = mcl_potions.leaping_func
mcl_mobs.effect_functions["heal"] = mcl_potions.healing_func -- mcl_mobs.effect_functions["swiftness"] = mcl_potions.swiftness_func
mcl_mobs.effect_functions["bad_omen"] = mcl_potions.bad_omen_func -- mcl_mobs.effect_functions["heal"] = mcl_potions.healing_func
mcl_mobs.effect_functions["withering"] = mcl_potions.withering_func -- mcl_mobs.effect_functions["bad_omen"] = mcl_potions.bad_omen_func
-- mcl_mobs.effect_functions["withering"] = mcl_potions.withering_func
-- give withering to players in a wither rose -- give withering to players in a wither rose
local etime = 0 local etime = 0

View file

Before

Width:  |  Height:  |  Size: 121 B

After

Width:  |  Height:  |  Size: 121 B

View file

Before

Width:  |  Height:  |  Size: 124 B

After

Width:  |  Height:  |  Size: 124 B

View file

Before

Width:  |  Height:  |  Size: 123 B

After

Width:  |  Height:  |  Size: 123 B

View file

Before

Width:  |  Height:  |  Size: 138 B

After

Width:  |  Height:  |  Size: 138 B

View file

Before

Width:  |  Height:  |  Size: 118 B

After

Width:  |  Height:  |  Size: 118 B

View file

Before

Width:  |  Height:  |  Size: 114 B

After

Width:  |  Height:  |  Size: 114 B

View file

Before

Width:  |  Height:  |  Size: 122 B

After

Width:  |  Height:  |  Size: 122 B

View file

Before

Width:  |  Height:  |  Size: 129 B

After

Width:  |  Height:  |  Size: 129 B