Skip to content

Commit

Permalink
Refactor client CLuaTeamDefs (PR #3944)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico8340 authored Jan 10, 2025
1 parent ed1301b commit f37bbad
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 141 deletions.
15 changes: 14 additions & 1 deletion Client/mods/deathmatch/logic/CClientTeam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ void CClientTeam::RemoveAll()
m_List.clear();
}

std::vector<CClientPlayer*> CClientTeam::GetPlayers() const
{
std::vector<CClientPlayer*> players;

for (auto iter = IterBegin(); iter != IterEnd(); ++iter)
{
if (!(*iter)->IsBeingDeleted())
players.push_back(*iter);
}

return players;
}

bool CClientTeam::Exists(CClientPlayer* pPlayer)
{
list<CClientPlayer*>::const_iterator iter = m_List.begin();
Expand Down Expand Up @@ -96,4 +109,4 @@ void CClientTeam::SetColor(unsigned char ucRed, unsigned char ucGreen, unsigned
m_ucRed = ucRed;
m_ucGreen = ucGreen;
m_ucBlue = ucBlue;
}
}
5 changes: 3 additions & 2 deletions Client/mods/deathmatch/logic/CClientTeam.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<CClientPlayer*> GetPlayers() const;

bool Exists(CClientPlayer* pPlayer);

std::list<CClientPlayer*>::const_iterator IterBegin() { return m_List.begin(); }
std::list<CClientPlayer*>::const_iterator IterEnd() { return m_List.end(); }
std::list<CClientPlayer*>::const_iterator IterBegin() const noexcept { return m_List.begin(); }
std::list<CClientPlayer*>::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; }
Expand Down
164 changes: 34 additions & 130 deletions Client/mods/deathmatch/logic/luadefs/CLuaTeamDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <lua/CLuaFunctionParser.h>

void CLuaTeamDefs::LoadFunctions()
{
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
{"getTeamFromName", GetTeamFromName}, {"getTeamName", GetTeamName}, {"getTeamColor", GetTeamColor},
{"getTeamFriendlyFire", GetTeamFriendlyFire}, {"getPlayersInTeam", GetPlayersInTeam}, {"countPlayersInTeam", CountPlayersInTeam},
{"getTeamFromName", ArgumentParserWarn<false, GetTeamFromName>},
{"getTeamName", ArgumentParserWarn<false, GetTeamName>},
{"getTeamColor", ArgumentParserWarn<false, GetTeamColor>},
{"getTeamFriendlyFire", ArgumentParserWarn<false, GetTeamFriendlyFire>},
{"getPlayersInTeam", ArgumentParserWarn<false, GetPlayersInTeam>},
{"countPlayersInTeam", ArgumentParserWarn<false, CountPlayersInTeam>}
};

// Add functions
Expand All @@ -28,156 +30,58 @@ 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");
lua_classfunction(luaVM, "getName", "getTeamName");
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<CClientTeam*, bool> 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<std::uint8_t, std::uint8_t, std::uint8_t> 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<CClientPlayer*> CLuaTeamDefs::GetPlayersInTeam(CClientTeam* team)
{
CClientTeam* pTeam = NULL;
CScriptArgReader argStream(luaVM);
argStream.ReadUserData(pTeam);

if (!argStream.HasErrors())
{
lua_newtable(luaVM);

unsigned int uiIndex = 0;

list<CClientPlayer*>::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();
}
16 changes: 8 additions & 8 deletions Client/mods/deathmatch/logic/luadefs/CLuaTeamDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
*
* 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/
*
*****************************************************************************/

#pragma once
#include "CLuaDefs.h"
#include <lua/CLuaMultiReturn.h>

class CLuaTeamDefs : public CLuaDefs
{
public:
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<CClientTeam*, bool> GetTeamFromName(const std::string name) noexcept;
static std::string GetTeamName(CClientTeam* team);
static CLuaMultiReturn<std::uint8_t, std::uint8_t, std::uint8_t> GetTeamColor(CClientTeam* team) noexcept;
static bool GetTeamFriendlyFire(CClientTeam* team) noexcept;
static std::vector<CClientPlayer*> GetPlayersInTeam(CClientTeam* team);
static std::uint32_t CountPlayersInTeam(CClientTeam* team) noexcept;
};

0 comments on commit f37bbad

Please sign in to comment.