22 *
33 * PROJECT: Multi Theft Auto
44 * LICENSE: See LICENSE in the top level directory
5- * FILE: mods/shared_logic/luadefs/CLuaTeamDefs.cpp
6- * PURPOSE: Lua team definitions class
75 *
86 * Multi Theft Auto is available from http://www.multitheftauto.com/
97 *
108 *****************************************************************************/
119
1210#include " StdInc.h"
13- using std::list;
11+ # include < lua/CLuaFunctionParser.h >
1412
1513void CLuaTeamDefs::LoadFunctions ()
1614{
1715 constexpr static const std::pair<const char *, lua_CFunction> functions[]{
18- {" getTeamFromName" , GetTeamFromName}, {" getTeamName" , GetTeamName}, {" getTeamColor" , GetTeamColor},
19- {" getTeamFriendlyFire" , GetTeamFriendlyFire}, {" getPlayersInTeam" , GetPlayersInTeam}, {" countPlayersInTeam" , CountPlayersInTeam},
16+ {" getTeamFromName" , ArgumentParserWarn<false , GetTeamFromName>},
17+ {" getTeamName" , ArgumentParserWarn<false , GetTeamName>},
18+ {" getTeamColor" , ArgumentParserWarn<false , GetTeamColor>},
19+ {" getTeamFriendlyFire" , ArgumentParserWarn<false , GetTeamFriendlyFire>},
20+ {" getPlayersInTeam" , ArgumentParserWarn<false , GetPlayersInTeam>},
21+ {" countPlayersInTeam" , ArgumentParserWarn<false , CountPlayersInTeam>}
2022 };
2123
2224 // Add functions
@@ -28,156 +30,58 @@ void CLuaTeamDefs::AddClass(lua_State* luaVM)
2830{
2931 lua_newclass (luaVM);
3032
31- lua_classfunction (luaVM, " create" , " getTeamFromName" );
3233 lua_classfunction (luaVM, " getFromName" , " getTeamFromName" );
3334 lua_classfunction (luaVM, " countPlayers" , " countPlayersInTeam" );
3435 lua_classfunction (luaVM, " getFriendlyFire" , " getTeamFriendlyFire" );
3536 lua_classfunction (luaVM, " getName" , " getTeamName" );
3637 lua_classfunction (luaVM, " getColor" , " getTeamColor" );
3738 lua_classfunction (luaVM, " getPlayers" , " getPlayersInTeam" );
3839
39- lua_classvariable (luaVM, " playerCount" , NULL , " countPlayersInTeam" );
40- lua_classvariable (luaVM, " friendlyFire" , NULL , " getTeamFriendlyFire" );
41- lua_classvariable (luaVM, " players" , NULL , " getPlayersInTeam" );
42- lua_classvariable (luaVM, " name" , NULL , " getTeamName" );
40+ lua_classvariable (luaVM, " playerCount" , nullptr , " countPlayersInTeam" );
41+ lua_classvariable (luaVM, " friendlyFire" , nullptr , " getTeamFriendlyFire" );
42+ lua_classvariable (luaVM, " players" , nullptr , " getPlayersInTeam" );
43+ lua_classvariable (luaVM, " name" , nullptr , " getTeamName" );
4344
4445 lua_registerclass (luaVM, " Team" , " Element" );
4546}
4647
47- int CLuaTeamDefs::GetTeamFromName (lua_State* luaVM)
48+ std::variant<CClientTeam*, bool > CLuaTeamDefs::GetTeamFromName (const std::string name) noexcept
4849{
49- SString strName = " " ;
50- CScriptArgReader argStream (luaVM);
51- argStream.ReadString (strName);
52- 53- if (!argStream.HasErrors ())
54- {
55- CClientTeam* pTeam = m_pTeamManager->GetTeam (strName);
56- if (pTeam)
57- {
58- lua_pushelement (luaVM, pTeam);
59- return 1 ;
60- }
61- }
62- else
63- m_pScriptDebugging->LogCustom (luaVM, argStream.GetFullErrorMessage ());
64- 65- lua_pushboolean (luaVM, false );
66- return 1 ;
50+ CClientTeam* team = m_pTeamManager->GetTeam (name.c_str ());
51+ 52+ if (!team)
53+ return false ;
54+ 55+ return team;
6756}
6857
69- int CLuaTeamDefs::GetTeamName (lua_State* luaVM )
58+ std::string CLuaTeamDefs::GetTeamName (CClientTeam* team )
7059{
71- CClientTeam* pTeam = NULL ;
72- CScriptArgReader argStream (luaVM);
73- argStream.ReadUserData (pTeam);
74- 75- if (!argStream.HasErrors ())
76- {
77- const char * szName = pTeam->GetTeamName ();
78- if (szName)
79- {
80- lua_pushstring (luaVM, szName);
81- return 1 ;
82- }
83- }
84- else
85- m_pScriptDebugging->LogCustom (luaVM, argStream.GetFullErrorMessage ());
86- 87- lua_pushboolean (luaVM, false );
88- return 1 ;
60+ return std::string (team->GetTeamName ());
8961}
9062
91- int CLuaTeamDefs::GetTeamColor (lua_State* luaVM)
63+ CLuaMultiReturn<std:: uint8_t , std:: uint8_t , std:: uint8_t > CLuaTeamDefs::GetTeamColor (CClientTeam* team) noexcept
9264{
93- CClientTeam* pTeam = NULL ;
94- CScriptArgReader argStream (luaVM);
95- argStream.ReadUserData (pTeam);
96- 97- if (!argStream.HasErrors ())
98- {
99- unsigned char ucRed, ucGreen, ucBlue;
100- pTeam->GetColor (ucRed, ucGreen, ucBlue);
101- 102- lua_pushnumber (luaVM, ucRed);
103- lua_pushnumber (luaVM, ucGreen);
104- lua_pushnumber (luaVM, ucBlue);
105- return 3 ;
106- }
107- else
108- m_pScriptDebugging->LogCustom (luaVM, argStream.GetFullErrorMessage ());
109- 110- lua_pushboolean (luaVM, false );
111- return 1 ;
65+ std::uint8_t red;
66+ std::uint8_t green;
67+ std::uint8_t blue;
68+ 69+ team->GetColor (red, green, blue);
70+ 71+ return {red, green, blue};
11272}
11373
114- int CLuaTeamDefs::GetTeamFriendlyFire (lua_State* luaVM)
74+ bool CLuaTeamDefs::GetTeamFriendlyFire (CClientTeam* team) noexcept
11575{
116- CClientTeam* pTeam = NULL ;
117- CScriptArgReader argStream (luaVM);
118- argStream.ReadUserData (pTeam);
119- 120- if (!argStream.HasErrors ())
121- {
122- bool bFriendlyFire = pTeam->GetFriendlyFire ();
123- lua_pushboolean (luaVM, bFriendlyFire);
124- return 1 ;
125- }
126- else
127- m_pScriptDebugging->LogCustom (luaVM, argStream.GetFullErrorMessage ());
128- 129- lua_pushboolean (luaVM, false );
130- return 1 ;
76+ return team->GetFriendlyFire ();
13177}
13278
133- int CLuaTeamDefs::GetPlayersInTeam (lua_State* luaVM )
79+ std::vector<CClientPlayer*> CLuaTeamDefs::GetPlayersInTeam (CClientTeam* team )
13480{
135- CClientTeam* pTeam = NULL ;
136- CScriptArgReader argStream (luaVM);
137- argStream.ReadUserData (pTeam);
138- 139- if (!argStream.HasErrors ())
140- {
141- lua_newtable (luaVM);
142- 143- unsigned int uiIndex = 0 ;
144- 145- list<CClientPlayer*>::const_iterator iter = pTeam->IterBegin ();
146- for (; iter != pTeam->IterEnd (); iter++)
147- {
148- CClientPlayer* pPlayer = *iter;
149- if (!pPlayer->IsBeingDeleted ())
150- {
151- lua_pushnumber (luaVM, ++uiIndex);
152- lua_pushelement (luaVM, pPlayer);
153- lua_settable (luaVM, -3 );
154- }
155- }
156- 157- return 1 ;
158- }
159- else
160- m_pScriptDebugging->LogCustom (luaVM, argStream.GetFullErrorMessage ());
161- 162- lua_pushboolean (luaVM, false );
163- return 1 ;
81+ return team->GetPlayers ();
16482}
16583
166- int CLuaTeamDefs::CountPlayersInTeam (lua_State* luaVM)
84+ std:: uint32_t CLuaTeamDefs::CountPlayersInTeam (CClientTeam* team) noexcept
16785{
168- CClientTeam* pTeam = NULL ;
169- CScriptArgReader argStream (luaVM);
170- argStream.ReadUserData (pTeam);
171- 172- if (!argStream.HasErrors ())
173- {
174- unsigned int uiCount = pTeam->CountPlayers ();
175- lua_pushnumber (luaVM, uiCount);
176- return 1 ;
177- }
178- else
179- m_pScriptDebugging->LogCustom (luaVM, argStream.GetFullErrorMessage ());
180- 181- lua_pushboolean (luaVM, false );
182- return 1 ;
86+ return team->CountPlayers ();
18387}
0 commit comments