From 1fb178dd6628ba47509d505ec9c9d4d6fea2107d Mon Sep 17 00:00:00 2001
From: Lactozilla <jp6781615@gmail.com>
Date: Sun, 6 Aug 2023 23:51:52 -0300
Subject: [PATCH] Add P_PlayerHasTeamFlag

---
 src/lua_baselib.c | 37 ++++++++++++++++++++++++-------------
 src/lua_infolib.c |  8 ++++----
 src/p_local.h     |  1 +
 src/p_user.c      |  8 ++++++++
 4 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index 99c070879a..ac3d081eec 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -845,7 +845,6 @@ static int lib_pGetMobjGravity(lua_State *L)
 static int lib_pWeaponOrPanel(lua_State *L)
 {
 	mobjtype_t type = luaL_checkinteger(L, 1);
-	//HUDSAFE
 	if (type >= NUMMOBJTYPES)
 		return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
 	lua_pushboolean(L, P_WeaponOrPanel(type));
@@ -855,9 +854,8 @@ static int lib_pWeaponOrPanel(lua_State *L)
 static int lib_pGetTeamFlag(lua_State *L)
 {
 	int team = luaL_checkinteger(L, 1);
-	if (team < 0 || team >= numteams)
-		return luaL_error(L, "team index %d out of range (0 - %d)", team, numteams - 1);
-	NOHUD
+	if (team <= 0 || team >= numteams)
+		return luaL_error(L, "team index %d out of range (1 - %d)", team, numteams - 1);
 	INLEVEL
 	LUA_PushUserdata(L, P_GetTeamFlag(team), META_MOBJ);
 	return 1;
@@ -866,9 +864,8 @@ static int lib_pGetTeamFlag(lua_State *L)
 static int lib_pGetTeamFlagMapthing(lua_State *L)
 {
 	int team = luaL_checkinteger(L, 1);
-	if (team < 0 || team >= numteams)
-		return luaL_error(L, "team index %d out of range (0 - %d)", team, numteams - 1);
-	NOHUD
+	if (team <= 0 || team >= numteams)
+		return luaL_error(L, "team index %d out of range (1 - %d)", team, numteams - 1);
 	INLEVEL
 	LUA_PushUserdata(L, P_GetTeamFlagMapthing(team), META_MAPTHING);
 	return 1;
@@ -1854,6 +1851,19 @@ static int lib_pPlayerShouldUseSpinHeight(lua_State *L)
 	return 1;
 }
 
+static int lib_pPlayerHasTeamFlag(lua_State *L)
+{
+	player_t *player = *((player_t **)luaL_checkudata(L, 1, META_PLAYER));
+	INT32 team = (INT32)luaL_checkinteger(L, 2);
+	INLEVEL
+	if (!player)
+		return LUA_ErrInvalid(L, "player_t");
+	if (team <= 0 || team >= numteams)
+		luaL_error(L, "team index %d out of range (1 - %d)", team, numteams-1);
+	lua_pushboolean(L, P_PlayerHasTeamFlag(player, team));
+	return 1;
+}
+
 // P_MAP
 ///////////
 
@@ -2490,8 +2500,8 @@ static int lib_pMobjTouchingTeamBase(lua_State *L)
 	INLEVEL
 	if (!mo)
 		return LUA_ErrInvalid(L, "mobj_t");
-	if (team <= 0 || team >= teamsingame)
-		luaL_error(L, "team index %d out of range (1 - %d)", team, teamsingame-1);
+	if (team <= 0 || team >= numteams)
+		luaL_error(L, "team index %d out of range (1 - %d)", team, numteams-1);
 	LUA_PushUserdata(L, P_MobjTouchingTeamBase(mo, team), META_SECTOR);
 	return 1;
 }
@@ -2657,8 +2667,8 @@ static int lib_pTeamHasFlagAtBase(lua_State *L)
 {
 	INT32 team = luaL_checkinteger(L, 1);
 	INLEVEL
-	if (team <= 0 || team >= teamsingame)
-		luaL_error(L, "team index %d out of range (1 - %d)", team, teamsingame-1);
+	if (team <= 0 || team >= numteams)
+		luaL_error(L, "team index %d out of range (1 - %d)", team, numteams-1);
 	lua_pushboolean(L, P_TeamHasFlagAtBase(team));
 	return 1;
 }
@@ -3697,8 +3707,8 @@ static int lib_gAddGametype(lua_State *L)
 					if (idx >= 0 && idx < MAXTEAMS)
 					{
 						int team_index = luaL_checkinteger(L, -1);
-						if (team_index < 0 || team_index >= numteams)
-							luaL_error(L, "team index %d out of range (0 - %d)", team_index, numteams-1);
+						if (team_index <= 0 || team_index >= numteams)
+							luaL_error(L, "team index %d out of range (1 - %d)", team_index, numteams-1);
 
 						teamlist[idx] = (UINT8)team_index;
 
@@ -4415,6 +4425,7 @@ static luaL_Reg lib[] = {
 	{"P_DoFollowMobj",lib_pDoFollowMobj},
 	{"P_PlayerCanEnterSpinGaps",lib_pPlayerCanEnterSpinGaps},
 	{"P_PlayerShouldUseSpinHeight",lib_pPlayerShouldUseSpinHeight},
+	{"P_PlayerHasTeamFlag",lib_pPlayerHasTeamFlag},
 
 	// p_map
 	{"P_CheckPosition",lib_pCheckPosition},
diff --git a/src/lua_infolib.c b/src/lua_infolib.c
index 5ae0c2ddb7..0e5b75d141 100644
--- a/src/lua_infolib.c
+++ b/src/lua_infolib.c
@@ -2046,8 +2046,8 @@ static int gametype_set(lua_State *L)
 				if (i >= 0 && i < MAXTEAMS)
 				{
 					int team_index = luaL_checkinteger(L, -1);
-					if (team_index < 0 || team_index >= numteams)
-						luaL_error(L, "team index %d out of range (0 - %d)", team_index, numteams-1);
+					if (team_index <= 0 || team_index >= numteams)
+						luaL_error(L, "team index %d out of range (1 - %d)", team_index, numteams-1);
 
 					gt->teams.list[i] = (UINT8)team_index;
 
@@ -2418,8 +2418,8 @@ static int teamlist_set(lua_State *L)
 	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)", team, numteams - 1);
+	if (team <= 0 || team >= numteams)
+		return luaL_error(L, "team index %d out of range (1 - %d)", team, numteams - 1);
 	teamlist->list[i - 1] = (UINT8)team;
 	return 0;
 }
diff --git a/src/p_local.h b/src/p_local.h
index 5ee5dfd5d2..e6c90b0d1f 100644
--- a/src/p_local.h
+++ b/src/p_local.h
@@ -146,6 +146,7 @@ void P_ForceLocalAngle(player_t *player, angle_t angle);
 boolean P_PlayerFullbright(player_t *player);
 boolean P_PlayerCanEnterSpinGaps(player_t *player);
 boolean P_PlayerShouldUseSpinHeight(player_t *player);
+boolean P_PlayerHasTeamFlag(player_t *player, UINT8 team);
 UINT16 P_GetPlayerColor(player_t *player);
 
 boolean P_IsObjectInGoop(mobj_t *mo);
diff --git a/src/p_user.c b/src/p_user.c
index 72b098c149..9ded986167 100644
--- a/src/p_user.c
+++ b/src/p_user.c
@@ -13097,6 +13097,14 @@ boolean P_PlayerShouldUseSpinHeight(player_t *player)
 		|| JUMPCURLED(player));
 }
 
+boolean P_PlayerHasTeamFlag(player_t *player, UINT8 team)
+{
+	if (!G_GametypeHasTeams() || team >= numteams)
+		return false;
+
+	return player->gotflag & teams[team].flag;
+}
+
 UINT16 P_GetPlayerColor(player_t *player)
 {
 	if (G_GametypeHasTeams() && player->ctfteam)
-- 
GitLab