diff --git a/Client/mods/deathmatch/logic/CClientTeam.cpp b/Client/mods/deathmatch/logic/CClientTeam.cpp index 3813986695..28dd562e27 100644 --- a/Client/mods/deathmatch/logic/CClientTeam.cpp +++ b/Client/mods/deathmatch/logic/CClientTeam.cpp @@ -64,6 +64,19 @@ void CClientTeam::RemoveAll() m_List.clear(); } +std::vector CClientTeam::GetPlayers() const +{ + std::vector players; + + for (auto iter = IterBegin(); iter != IterEnd(); ++iter) + { + if (!(*iter)->IsBeingDeleted()) + players.push_back(*iter); + } + + return players; +} + bool CClientTeam::Exists(CClientPlayer* pPlayer) { list::const_iterator iter = m_List.begin(); @@ -96,4 +109,4 @@ void CClientTeam::SetColor(unsigned char ucRed, unsigned char ucGreen, unsigned m_ucRed = ucRed; m_ucGreen = ucGreen; m_ucBlue = ucBlue; -} \ No newline at end of file +} diff --git a/Client/mods/deathmatch/logic/CClientTeam.h b/Client/mods/deathmatch/logic/CClientTeam.h index 05e4468481..11e5dc71e1 100644 --- a/Client/mods/deathmatch/logic/CClientTeam.h +++ b/Client/mods/deathmatch/logic/CClientTeam.h @@ -44,11 +44,12 @@ class CClientTeam final : public CClientEntity void AddPlayer(CClientPlayer* pPlayer, bool bChangePlayer = false); void RemovePlayer(CClientPlayer* pPlayer, bool bChangePlayer = false); void RemoveAll(); + std::vector GetPlayers() const; bool Exists(CClientPlayer* pPlayer); - std::list::const_iterator IterBegin() { return m_List.begin(); } - std::list::const_iterator IterEnd() { return m_List.end(); } + std::list::const_iterator IterBegin() const noexcept { return m_List.begin(); } + std::list::const_iterator IterEnd() const noexcept { return m_List.end(); } void GetPosition(CVector& vecPosition) const { vecPosition = m_vecPosition; } void SetPosition(const CVector& vecPosition) { m_vecPosition = vecPosition; } diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaTeamDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaTeamDefs.cpp index e06cbeb260..b8772b4b88 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaTeamDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaTeamDefs.cpp @@ -2,21 +2,23 @@ * * PROJECT: Multi Theft Auto * LICENSE: See LICENSE in the top level directory - * FILE: mods/shared_logic/luadefs/CLuaTeamDefs.cpp - * PURPOSE: Lua team definitions class * * Multi Theft Auto is available from http://www.multitheftauto.com/ * *****************************************************************************/ #include "StdInc.h" -using std::list; +#include void CLuaTeamDefs::LoadFunctions() { constexpr static const std::pair functions[]{ - {"getTeamFromName", GetTeamFromName}, {"getTeamName", GetTeamName}, {"getTeamColor", GetTeamColor}, - {"getTeamFriendlyFire", GetTeamFriendlyFire}, {"getPlayersInTeam", GetPlayersInTeam}, {"countPlayersInTeam", CountPlayersInTeam}, + {"getTeamFromName", ArgumentParserWarn}, + {"getTeamName", ArgumentParserWarn}, + {"getTeamColor", ArgumentParserWarn}, + {"getTeamFriendlyFire", ArgumentParserWarn}, + {"getPlayersInTeam", ArgumentParserWarn}, + {"countPlayersInTeam", ArgumentParserWarn} }; // Add functions @@ -28,7 +30,6 @@ void CLuaTeamDefs::AddClass(lua_State* luaVM) { lua_newclass(luaVM); - lua_classfunction(luaVM, "create", "getTeamFromName"); lua_classfunction(luaVM, "getFromName", "getTeamFromName"); lua_classfunction(luaVM, "countPlayers", "countPlayersInTeam"); lua_classfunction(luaVM, "getFriendlyFire", "getTeamFriendlyFire"); @@ -36,148 +37,51 @@ void CLuaTeamDefs::AddClass(lua_State* luaVM) lua_classfunction(luaVM, "getColor", "getTeamColor"); lua_classfunction(luaVM, "getPlayers", "getPlayersInTeam"); - lua_classvariable(luaVM, "playerCount", NULL, "countPlayersInTeam"); - lua_classvariable(luaVM, "friendlyFire", NULL, "getTeamFriendlyFire"); - lua_classvariable(luaVM, "players", NULL, "getPlayersInTeam"); - lua_classvariable(luaVM, "name", NULL, "getTeamName"); + lua_classvariable(luaVM, "playerCount", nullptr, "countPlayersInTeam"); + lua_classvariable(luaVM, "friendlyFire", nullptr, "getTeamFriendlyFire"); + lua_classvariable(luaVM, "players", nullptr, "getPlayersInTeam"); + lua_classvariable(luaVM, "name", nullptr, "getTeamName"); lua_registerclass(luaVM, "Team", "Element"); } -int CLuaTeamDefs::GetTeamFromName(lua_State* luaVM) +std::variant CLuaTeamDefs::GetTeamFromName(const std::string name) noexcept { - SString strName = ""; - CScriptArgReader argStream(luaVM); - argStream.ReadString(strName); - - if (!argStream.HasErrors()) - { - CClientTeam* pTeam = m_pTeamManager->GetTeam(strName); - if (pTeam) - { - lua_pushelement(luaVM, pTeam); - return 1; - } - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + CClientTeam* team = m_pTeamManager->GetTeam(name.c_str()); + + if (!team) + return false; + + return team; } -int CLuaTeamDefs::GetTeamName(lua_State* luaVM) +std::string CLuaTeamDefs::GetTeamName(CClientTeam* team) { - CClientTeam* pTeam = NULL; - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pTeam); - - if (!argStream.HasErrors()) - { - const char* szName = pTeam->GetTeamName(); - if (szName) - { - lua_pushstring(luaVM, szName); - return 1; - } - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + return std::string(team->GetTeamName()); } -int CLuaTeamDefs::GetTeamColor(lua_State* luaVM) +CLuaMultiReturn CLuaTeamDefs::GetTeamColor(CClientTeam* team) noexcept { - CClientTeam* pTeam = NULL; - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pTeam); - - if (!argStream.HasErrors()) - { - unsigned char ucRed, ucGreen, ucBlue; - pTeam->GetColor(ucRed, ucGreen, ucBlue); - - lua_pushnumber(luaVM, ucRed); - lua_pushnumber(luaVM, ucGreen); - lua_pushnumber(luaVM, ucBlue); - return 3; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + std::uint8_t red; + std::uint8_t green; + std::uint8_t blue; + + team->GetColor(red, green, blue); + + return {red, green, blue}; } -int CLuaTeamDefs::GetTeamFriendlyFire(lua_State* luaVM) +bool CLuaTeamDefs::GetTeamFriendlyFire(CClientTeam* team) noexcept { - CClientTeam* pTeam = NULL; - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pTeam); - - if (!argStream.HasErrors()) - { - bool bFriendlyFire = pTeam->GetFriendlyFire(); - lua_pushboolean(luaVM, bFriendlyFire); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + return team->GetFriendlyFire(); } -int CLuaTeamDefs::GetPlayersInTeam(lua_State* luaVM) +std::vector CLuaTeamDefs::GetPlayersInTeam(CClientTeam* team) { - CClientTeam* pTeam = NULL; - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pTeam); - - if (!argStream.HasErrors()) - { - lua_newtable(luaVM); - - unsigned int uiIndex = 0; - - list::const_iterator iter = pTeam->IterBegin(); - for (; iter != pTeam->IterEnd(); iter++) - { - CClientPlayer* pPlayer = *iter; - if (!pPlayer->IsBeingDeleted()) - { - lua_pushnumber(luaVM, ++uiIndex); - lua_pushelement(luaVM, pPlayer); - lua_settable(luaVM, -3); - } - } - - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + return team->GetPlayers(); } -int CLuaTeamDefs::CountPlayersInTeam(lua_State* luaVM) +std::uint32_t CLuaTeamDefs::CountPlayersInTeam(CClientTeam* team) noexcept { - CClientTeam* pTeam = NULL; - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pTeam); - - if (!argStream.HasErrors()) - { - unsigned int uiCount = pTeam->CountPlayers(); - lua_pushnumber(luaVM, uiCount); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + return team->CountPlayers(); } diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaTeamDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaTeamDefs.h index 815801bac8..6dddee7aa7 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaTeamDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaTeamDefs.h @@ -2,8 +2,6 @@ * * PROJECT: Multi Theft Auto * LICENSE: See LICENSE in the top level directory - * FILE: mods/shared_logic/luadefs/CLuaTeamDefs.cpp - * PURPOSE: Lua team definitions class * * Multi Theft Auto is available from http://www.multitheftauto.com/ * @@ -11,6 +9,7 @@ #pragma once #include "CLuaDefs.h" +#include class CLuaTeamDefs : public CLuaDefs { @@ -18,10 +17,11 @@ class CLuaTeamDefs : public CLuaDefs static void LoadFunctions(); static void AddClass(lua_State* luaVM); - LUA_DECLARE(GetTeamFromName); - LUA_DECLARE(GetTeamName); - LUA_DECLARE(GetTeamColor); - LUA_DECLARE(GetTeamFriendlyFire); - LUA_DECLARE(GetPlayersInTeam); - LUA_DECLARE(CountPlayersInTeam); +private: + static std::variant GetTeamFromName(const std::string name) noexcept; + static std::string GetTeamName(CClientTeam* team); + static CLuaMultiReturn GetTeamColor(CClientTeam* team) noexcept; + static bool GetTeamFriendlyFire(CClientTeam* team) noexcept; + static std::vector GetPlayersInTeam(CClientTeam* team); + static std::uint32_t CountPlayersInTeam(CClientTeam* team) noexcept; };