From 073e86684241a04beac73ba3221a0c8cc35bebe0 Mon Sep 17 00:00:00 2001
From: Lactozilla <jp6781615@gmail.com>
Date: Sun, 6 Aug 2023 00:34:01 -0300
Subject: [PATCH] Add teamscores to Lua

---
 src/lua_infolib.c | 51 ++++++++++++++++++++++++++++++++++++-----------
 src/lua_libs.h    |  1 +
 src/lua_script.c  | 13 ++++++++++++
 src/st_stuff.c    |  8 ++++----
 4 files changed, 57 insertions(+), 16 deletions(-)

diff --git a/src/lua_infolib.c b/src/lua_infolib.c
index 4f62e50cb8..4e69678aba 100644
--- a/src/lua_infolib.c
+++ b/src/lua_infolib.c
@@ -2371,19 +2371,12 @@ static int team_num(lua_State *L)
 	return 1;
 }
 
-static int teamlist_len(lua_State *L)
-{
-	teamlist_t *teamlist = *((teamlist_t **)luaL_checkudata(L, 1, META_TEAMLIST));
-	lua_pushinteger(L, teamlist->num);
-	return 1;
-}
-
 static int teamlist_get(lua_State *L)
 {
 	teamlist_t *teamlist = *((teamlist_t **)luaL_checkudata(L, 1, META_TEAMLIST));
 	int i = luaL_checkinteger(L, 2);
-	if (i < 0 || i > teamlist->num)
-		return luaL_error(L, "list index %d out of range (1 - %d)", i, teamlist->num);
+	if (i <= 0 || i > teamlist->num)
+		return luaL_error(L, "array index %d out of range (1 - %d)", i, teamlist->num);
 	lua_pushinteger(L, teamlist->list[i - 1]);
 	return 1;
 }
@@ -2392,15 +2385,49 @@ static int teamlist_set(lua_State *L)
 {
 	teamlist_t *teamlist = *((teamlist_t **)luaL_checkudata(L, 1, META_TEAMLIST));
 	int i = luaL_checkinteger(L, 2);
-	if (i < 0 || i > teamlist->num)
-		return luaL_error(L, "list index %d out of range (1 - %d)", i, teamlist->num);
 	int team = luaL_checkinteger(L, 3);
+	if (i <= 0 || i > teamlist->num)
+		return luaL_error(L, "array index %d out of range (1 - %d)", i, teamlist->num);
 	if (team < 0 || team >= numteams)
 		return luaL_error(L, "team index %d out of range (0 - %d)", i, numteams - 1);
 	teamlist->list[i - 1] = (UINT8)team;
 	return 0;
 }
 
+static int teamlist_len(lua_State *L)
+{
+	teamlist_t *teamlist = *((teamlist_t **)luaL_checkudata(L, 1, META_TEAMLIST));
+	lua_pushinteger(L, teamlist->num);
+	return 1;
+}
+
+static int teamscores_get(lua_State *L)
+{
+	UINT32 *scoreslist = *((UINT32 **)luaL_checkudata(L, 1, META_TEAMSCORES));
+	int i = luaL_checkinteger(L, 2);
+	if (i < 0 || i >= numteams)
+		return luaL_error(L, "array index %d out of range (0 - %d)", i, numteams - 1);
+	lua_pushinteger(L, scoreslist[i]);
+	return 1;
+}
+
+static int teamscores_set(lua_State *L)
+{
+	UINT32 *scoreslist = *((UINT32 **)luaL_checkudata(L, 1, META_TEAMSCORES));
+	int i = luaL_checkinteger(L, 2);
+	UINT32 score = (UINT32)luaL_checkinteger(L, 3);
+	if (i < 0 || i >= numteams)
+		return luaL_error(L, "array index %d out of range (0 - %d)", i, numteams - 1);
+	scoreslist[i] = score;
+	return 0;
+}
+
+static int teamscores_len(lua_State *L)
+{
+	lua_pushinteger(L, numteams);
+	return 1;
+}
+
 //////////////////////////////
 //
 // Now push all these functions into the Lua state!
@@ -2421,6 +2448,7 @@ int LUA_InfoLib(lua_State *L)
 	LUA_RegisterUserdataMetatable(L, META_GAMETYPE, gametype_get, gametype_set, gametype_num);
 	LUA_RegisterUserdataMetatable(L, META_TEAM, team_get, team_set, team_num);
 	LUA_RegisterUserdataMetatable(L, META_TEAMLIST, teamlist_get, teamlist_set, teamlist_len);
+	LUA_RegisterUserdataMetatable(L, META_TEAMSCORES, teamscores_get, teamscores_set, teamscores_len);
 	LUA_RegisterUserdataMetatable(L, META_SKINCOLOR, skincolor_get, skincolor_set, skincolor_num);
 	LUA_RegisterUserdataMetatable(L, META_COLORRAMP, colorramp_get, colorramp_set, colorramp_len);
 	LUA_RegisterUserdataMetatable(L, META_SFXINFO, sfxinfo_get, sfxinfo_set, sfxinfo_num);
@@ -2439,7 +2467,6 @@ int LUA_InfoLib(lua_State *L)
 	LUA_RegisterGlobalUserdata(L, "mobjinfo", lib_getMobjInfo, lib_setMobjInfo, lib_mobjinfolen);
 	LUA_RegisterGlobalUserdata(L, "gametypes", lib_getGametypes, NULL, lib_gametypeslen);
 	LUA_RegisterGlobalUserdata(L, "teams", lib_getTeams, lib_setTeams, lib_teamslen);
-	// LUA_RegisterGlobalUserdata(L, "gametypes", lib_getGametypes, NULL, lib_gametypeslen);
 	LUA_RegisterGlobalUserdata(L, "skincolors", lib_getSkinColor, lib_setSkinColor, lib_skincolorslen);
 	LUA_RegisterGlobalUserdata(L, "spriteinfo", lib_getSpriteInfo, lib_setSpriteInfo, lib_spriteinfolen);
 	LUA_RegisterGlobalUserdata(L, "sfxinfo", lib_getSfxInfo, lib_setSfxInfo, lib_sfxlen);
diff --git a/src/lua_libs.h b/src/lua_libs.h
index 8a6af9f8b4..f611019df9 100644
--- a/src/lua_libs.h
+++ b/src/lua_libs.h
@@ -32,6 +32,7 @@ extern boolean ignoregameinputs;
 #define META_GAMETYPE "GAMETYPE_T*"
 #define META_TEAM "TEAM_T*"
 #define META_TEAMLIST "TEAMLIST_T*"
+#define META_TEAMSCORES "TEAMSCORES"
 #define META_PIVOTLIST "SPRITEFRAMEPIVOT_T[]"
 #define META_FRAMEPIVOT "SPRITEFRAMEPIVOT_T*"
 
diff --git a/src/lua_script.c b/src/lua_script.c
index 084b9ce6d4..bca8197709 100644
--- a/src/lua_script.c
+++ b/src/lua_script.c
@@ -219,6 +219,9 @@ int LUA_PushGlobals(lua_State *L, const char *word)
 	} else if (fastcmp(word,"redscore")) {
 		lua_pushinteger(L, teamscores[G_GetTeam(1)]);
 		return 1;
+	} else if (fastcmp(word,"teamscores")) {
+		LUA_PushUserdata(L, teamscores, META_TEAMSCORES);
+		return 1;
 	} else if (fastcmp(word,"timelimit")) {
 		lua_pushinteger(L, cv_timelimit.value);
 		return 1;
@@ -999,6 +1002,7 @@ enum
 	ARCH_SKIN,
 	ARCH_GAMETYPE,
 	ARCH_TEAM,
+	ARCH_TEAMSCORES,
 
 	ARCH_TEND=0xFF,
 };
@@ -1030,6 +1034,7 @@ static const struct {
 	{META_SKIN,     ARCH_SKIN},
 	{META_GAMETYPE, ARCH_GAMETYPE},
 	{META_TEAM,     ARCH_TEAM},
+	{META_TEAMSCORES, ARCH_TEAMSCORES},
 	{NULL,          ARCH_NULL}
 };
 
@@ -1372,6 +1377,11 @@ static UINT8 ArchiveValue(int TABLESINDEX, int myindex)
 			WRITEUINT8(save_p, team - teams);
 			break;
 		}
+		case ARCH_TEAMSCORES:
+		{
+			WRITEUINT8(save_p, ARCH_TEAMSCORES);
+			break;
+		}
 		default:
 			WRITEUINT8(save_p, ARCH_NULL);
 			return 2;
@@ -1627,6 +1637,9 @@ static UINT8 UnArchiveValue(int TABLESINDEX)
 	case ARCH_TEAM:
 		LUA_PushUserdata(gL, &teams[READUINT8(save_p)], META_TEAM);
 		break;
+	case ARCH_TEAMSCORES:
+		LUA_PushUserdata(gL, teamscores, META_TEAMSCORES);
+		break;
 	case ARCH_TEND:
 		return 1;
 	}
diff --git a/src/st_stuff.c b/src/st_stuff.c
index 957e5ac311..1ae3972345 100644
--- a/src/st_stuff.c
+++ b/src/st_stuff.c
@@ -2439,13 +2439,13 @@ static void ST_drawTeamHUD(void)
 
 		if (gametyperules & GTR_TEAMFLAGS)
 		{
-			bmatcico = ST_GetCurrentTeamIconImage(1, TEAM_ICON_FLAG);
-			rmatcico = ST_GetCurrentTeamIconImage(2, TEAM_ICON_FLAG);
+			rmatcico = ST_GetCurrentTeamIconImage(1, TEAM_ICON_FLAG);
+			bmatcico = ST_GetCurrentTeamIconImage(2, TEAM_ICON_FLAG);
 		}
 		else
 		{
-			bmatcico = ST_GetCurrentTeamIconImage(1, TEAM_ICON);
-			rmatcico = ST_GetCurrentTeamIconImage(2, TEAM_ICON);
+			rmatcico = ST_GetCurrentTeamIconImage(1, TEAM_ICON);
+			bmatcico = ST_GetCurrentTeamIconImage(2, TEAM_ICON);
 		}
 
 		V_DrawSmallScaledPatch(BASEVIDWIDTH/2 - SEP - (bmatcico->width / 4), 4, V_HUDTRANS|V_PERPLAYER|V_SNAPTOTOP, bmatcico);
-- 
GitLab