From 48c8bf1e2ea924ecb148a48cdeacef87b9893722 Mon Sep 17 00:00:00 2001 From: Zach Toogood Date: Mon, 27 Jan 2025 13:51:00 +0000 Subject: [PATCH] Core: Add magic_enum dep and printAllMods() --- ext/CMakeLists.txt | 7 +++++ scripts/specs/core/CBaseEntity.lua | 4 +++ src/map/lua/lua_baseentity.cpp | 42 ++++++++++++++++++++++++++++++ src/map/lua/lua_baseentity.h | 1 + 4 files changed, 54 insertions(+) diff --git a/ext/CMakeLists.txt b/ext/CMakeLists.txt index 3a7a6700afb..6f6456d9a2c 100644 --- a/ext/CMakeLists.txt +++ b/ext/CMakeLists.txt @@ -237,6 +237,12 @@ CPMAddPackage( GIT_TAG 83a592f0c3807500f1aaf3b07fd48105a01e2780 ) # defines: alpaca +CPMAddPackage( + NAME magic_enum + GITHUB_REPOSITORY Neargye/magic_enum + GIT_TAG 1a1824df7ac798177a521eed952720681b0bf482 +) # defines: magic_enum + set(EXTERNAL_LIBS fmt::fmt spdlog @@ -258,6 +264,7 @@ set(EXTERNAL_LIBS asio bcrypt alpaca + magic_enum ) if(WIN32) diff --git a/scripts/specs/core/CBaseEntity.lua b/scripts/specs/core/CBaseEntity.lua index f1e7a7291d0..11aea4b4261 100644 --- a/scripts/specs/core/CBaseEntity.lua +++ b/scripts/specs/core/CBaseEntity.lua @@ -2931,6 +2931,10 @@ end function CBaseEntity:delMod(modID, value) end +---@return nil +function CBaseEntity:printAllMods(modID, value) +end + ---@nodiscard ---@param modId integer ---@return integer diff --git a/src/map/lua/lua_baseentity.cpp b/src/map/lua/lua_baseentity.cpp index 16c8fd84324..6f7ef4d121b 100644 --- a/src/map/lua/lua_baseentity.cpp +++ b/src/map/lua/lua_baseentity.cpp @@ -168,6 +168,8 @@ #include "utils/trustutils.h" #include "utils/zoneutils.h" +#include + extern std::unordered_map>>> PacketMods; //======================================================// @@ -13723,6 +13725,45 @@ void CLuaBaseEntity::delMod(uint16 modID, int16 value) static_cast(m_PBaseEntity)->delModifier(static_cast(modID), value); } +/************************************************************************ + * Function: printAllMods() + * Purpose : Prints all mods that have a non-zero value + * Example : target:printAllMods() + * Notes : + ************************************************************************/ + +void CLuaBaseEntity::printAllMods() +{ + if (m_PBaseEntity->objtype == TYPE_NPC) + { + ShowWarning("Invalid Entity (NPC: %s) calling function.", m_PBaseEntity->getName()); + return; + } + + const auto longestEnumLength = [&]() + { + std::size_t longest = 0U; + for (auto modId : magic_enum::enum_values()) + { + if (auto length = magic_enum::enum_name(modId).size(); length > longest) + { + longest = length; + } + } + return longest; + }(); + + auto* PEntity = static_cast(m_PBaseEntity); + ShowInfo(fmt::format("{}'s ({}) mods:", PEntity->getName(), PEntity->id).c_str()); + for (const auto& modId : magic_enum::enum_values()) + { + if (const auto& value = PEntity->getMod(modId); value != 0) + { + ShowInfo(fmt::format("| {:<{}} | {:<8} |", magic_enum::enum_name(modId), longestEnumLength, value).c_str()); + } + }; +} + /************************************************************************ * Function: addLatent() * Purpose : Adds the specified latent to the player @@ -19239,6 +19280,7 @@ void CLuaBaseEntity::Register() SOL_REGISTER("getMod", CLuaBaseEntity::getMod); SOL_REGISTER("setMod", CLuaBaseEntity::setMod); SOL_REGISTER("delMod", CLuaBaseEntity::delMod); + SOL_REGISTER("printAllMods", CLuaBaseEntity::printAllMods); SOL_REGISTER("getMaxGearMod", CLuaBaseEntity::getMaxGearMod); SOL_REGISTER("addLatent", CLuaBaseEntity::addLatent); diff --git a/src/map/lua/lua_baseentity.h b/src/map/lua/lua_baseentity.h index a504a96ded7..477b4915821 100644 --- a/src/map/lua/lua_baseentity.h +++ b/src/map/lua/lua_baseentity.h @@ -676,6 +676,7 @@ class CLuaBaseEntity int16 getMod(uint16 modID); void setMod(uint16 modID, int16 value); void delMod(uint16 modID, int16 value); + void printAllMods(); int16 getMaxGearMod(Mod modId); void addLatent(uint16 condID, uint16 conditionValue, uint16 mID, int16 modValue);