From 5750284e8f26418672a08394d29891f9ccd4db15 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Tue, 10 Oct 2023 01:07:26 +0200 Subject: [PATCH] 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 --- mods/ITEMS/mcl_potions/commands.lua | 62 ++++++++++-------- mods/ITEMS/mcl_potions/functions.lua | 31 +++++++-- mods/ITEMS/mcl_potions/init.lua | 23 +++---- ...=> mcl_potions_effect_fire_resistance.png} | Bin ...ng => mcl_potions_effect_invisibility.png} | Bin ...oned.png => mcl_potions_effect_poison.png} | Bin ...ng => mcl_potions_effect_regeneration.png} | Bin ...ow.png => mcl_potions_effect_slowness.png} | Bin ...ng.png => mcl_potions_effect_strength.png} | Bin ...t.png => mcl_potions_effect_swiftness.png} | Bin ...ak.png => mcl_potions_effect_weakness.png} | Bin 11 files changed, 74 insertions(+), 42 deletions(-) rename textures/{mcl_potions_effect_fire_proof.png => mcl_potions_effect_fire_resistance.png} (100%) rename textures/{mcl_potions_effect_invisible.png => mcl_potions_effect_invisibility.png} (100%) rename textures/{mcl_potions_effect_poisoned.png => mcl_potions_effect_poison.png} (100%) rename textures/{mcl_potions_effect_regenerating.png => mcl_potions_effect_regeneration.png} (100%) rename textures/{mcl_potions_effect_slow.png => mcl_potions_effect_slowness.png} (100%) rename textures/{mcl_potions_effect_strong.png => mcl_potions_effect_strength.png} (100%) rename textures/{mcl_potions_effect_swift.png => mcl_potions_effect_swiftness.png} (100%) rename textures/{mcl_potions_effect_weak.png => mcl_potions_effect_weakness.png} (100%) diff --git a/mods/ITEMS/mcl_potions/commands.lua b/mods/ITEMS/mcl_potions/commands.lua index 10bf86311..16b3ec01e 100644 --- a/mods/ITEMS/mcl_potions/commands.lua +++ b/mods/ITEMS/mcl_potions/commands.lua @@ -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",{ - params = S(" []"), - description = S("Add a status effect to yourself. Arguments: : name of status effect, e.g. poison. : duration in seconds. : effect strength multiplier (1 = 100%)"), + params = S(" [] []"), + description = S("Add a status effect to yourself. Arguments: : name of status effect, e.g. poison. : duration in seconds. : 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. : effect strength modifier, can mean different things depending on the effect."), privs = {server = true}, func = function(name, params) @@ -37,22 +23,46 @@ minetest.register_chatcommand("effect",{ if not P[1] then 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!") - elseif P[3] and not tonumber(P[3]) then - return false, S("Invalid factor parameter!") - end - -- Default factor = 1 - if not P[3] then - P[3] = 1.0 + elseif P[3] and not tonumber(P[3]) and P[3] ~= "F" then + return false, S("Invalid level parameter!") + elseif P[3] and P[3] == "F" and not P[4] then + return false, S("Missing or invalid factor parameter when level is F!") end - if get_chat_function[P[1]] then - get_chat_function[P[1]](minetest.get_player_by_name(name), tonumber(P[3]), tonumber(P[2])) - return true + -- Default level = 1 + if not P[3] then + 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 return false, S("@1 is not an available status effect.", P[1]) end end, }) + diff --git a/mods/ITEMS/mcl_potions/functions.lua b/mods/ITEMS/mcl_potions/functions.lua index 6f0255f16..31c811e0b 100644 --- a/mods/ITEMS/mcl_potions/functions.lua +++ b/mods/ITEMS/mcl_potions/functions.lua @@ -48,7 +48,7 @@ local function generate_linear_lvl_to_fac(l1, l2) end 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 return function(level) if level == 0 then return 0 end @@ -89,6 +89,15 @@ function mcl_potions.register_effect(def) if def.name == nil then error("Unable to register effect: name is nil") 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 pdef = {} if not def.icon then @@ -141,6 +150,18 @@ function mcl_potions.register_effect(def) EF[name] = {} 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({ name = "invisibility", 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 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 EF[name][player] and effect.on_load then effect.on_load(player, EF[name][player].factor) @@ -1133,10 +1154,8 @@ local function target_valid(object, name) end function mcl_potions.give_effect(name, object, factor, duration) - if not target_valid(object, name) then return false end - 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 local vals = {dur = duration, timer = 0,} if edef.uses_factor then vals.factor = factor end @@ -1160,6 +1179,8 @@ function mcl_potions.give_effect(name, object, factor, duration) end if object:is_player() then potions_set_hud(object) end + + return true end function mcl_potions.give_effect_by_level(name, object, level, duration) diff --git a/mods/ITEMS/mcl_potions/init.lua b/mods/ITEMS/mcl_potions/init.lua index 8e4c53cad..8c308f1fb 100644 --- a/mods/ITEMS/mcl_potions/init.lua +++ b/mods/ITEMS/mcl_potions/init.lua @@ -462,17 +462,18 @@ function mcl_potions.get_alchemy(ingr, pot) return false end -mcl_mobs.effect_functions["poison"] = mcl_potions.poison_func -mcl_mobs.effect_functions["regeneration"] = mcl_potions.regeneration_func -mcl_mobs.effect_functions["invisibility"] = mcl_potions.invisiblility_func -mcl_mobs.effect_functions["fire_resistance"] = mcl_potions.fire_resistance_func -mcl_mobs.effect_functions["night_vision"] = mcl_potions.night_vision_func -mcl_mobs.effect_functions["water_breathing"] = mcl_potions.water_breathing_func -mcl_mobs.effect_functions["leaping"] = mcl_potions.leaping_func -mcl_mobs.effect_functions["swiftness"] = mcl_potions.swiftness_func -mcl_mobs.effect_functions["heal"] = mcl_potions.healing_func -mcl_mobs.effect_functions["bad_omen"] = mcl_potions.bad_omen_func -mcl_mobs.effect_functions["withering"] = mcl_potions.withering_func +-- TODO replace all calls to the old API with new API calls in other mods +-- mcl_mobs.effect_functions["poison"] = mcl_potions.poison_func +-- mcl_mobs.effect_functions["regeneration"] = mcl_potions.regeneration_func +-- mcl_mobs.effect_functions["invisibility"] = mcl_potions.invisiblility_func +-- mcl_mobs.effect_functions["fire_resistance"] = mcl_potions.fire_resistance_func +-- mcl_mobs.effect_functions["night_vision"] = mcl_potions.night_vision_func +-- mcl_mobs.effect_functions["water_breathing"] = mcl_potions.water_breathing_func +-- mcl_mobs.effect_functions["leaping"] = mcl_potions.leaping_func +-- mcl_mobs.effect_functions["swiftness"] = mcl_potions.swiftness_func +-- mcl_mobs.effect_functions["heal"] = mcl_potions.healing_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 local etime = 0 diff --git a/textures/mcl_potions_effect_fire_proof.png b/textures/mcl_potions_effect_fire_resistance.png similarity index 100% rename from textures/mcl_potions_effect_fire_proof.png rename to textures/mcl_potions_effect_fire_resistance.png diff --git a/textures/mcl_potions_effect_invisible.png b/textures/mcl_potions_effect_invisibility.png similarity index 100% rename from textures/mcl_potions_effect_invisible.png rename to textures/mcl_potions_effect_invisibility.png diff --git a/textures/mcl_potions_effect_poisoned.png b/textures/mcl_potions_effect_poison.png similarity index 100% rename from textures/mcl_potions_effect_poisoned.png rename to textures/mcl_potions_effect_poison.png diff --git a/textures/mcl_potions_effect_regenerating.png b/textures/mcl_potions_effect_regeneration.png similarity index 100% rename from textures/mcl_potions_effect_regenerating.png rename to textures/mcl_potions_effect_regeneration.png diff --git a/textures/mcl_potions_effect_slow.png b/textures/mcl_potions_effect_slowness.png similarity index 100% rename from textures/mcl_potions_effect_slow.png rename to textures/mcl_potions_effect_slowness.png diff --git a/textures/mcl_potions_effect_strong.png b/textures/mcl_potions_effect_strength.png similarity index 100% rename from textures/mcl_potions_effect_strong.png rename to textures/mcl_potions_effect_strength.png diff --git a/textures/mcl_potions_effect_swift.png b/textures/mcl_potions_effect_swiftness.png similarity index 100% rename from textures/mcl_potions_effect_swift.png rename to textures/mcl_potions_effect_swiftness.png diff --git a/textures/mcl_potions_effect_weak.png b/textures/mcl_potions_effect_weakness.png similarity index 100% rename from textures/mcl_potions_effect_weak.png rename to textures/mcl_potions_effect_weakness.png