From 87e18d410c3a0901c689e536369d2eb53eb90bf2 Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Sun, 6 Aug 2023 00:56:40 -0300 Subject: [PATCH] Add P_GetTeamFlag and P_GetTeamFlagMapthing --- src/lua_baselib.c | 24 ++++++++++++++++++++++++ src/lua_infolib.c | 2 +- src/p_inter.c | 4 ++-- src/p_local.h | 3 +++ src/p_mobj.c | 24 ++++++++++++++++++++---- src/p_saveg.c | 4 ++-- src/p_setup.c | 2 +- src/st_stuff.c | 11 +++++++---- 8 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 04a6fe49c..af64d0823 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -852,6 +852,28 @@ static int lib_pWeaponOrPanel(lua_State *L) return 1; } +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 + INLEVEL + LUA_PushUserdata(L, P_GetTeamFlag(team), META_MOBJ); + return 1; +} + +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 + INLEVEL + LUA_PushUserdata(L, P_GetTeamFlagMapthing(team), META_MAPTHING); + return 1; +} + static int lib_pFlashPal(lua_State *L) { player_t *pl = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); @@ -4114,6 +4136,8 @@ static luaL_Reg lib[] = { {"P_MobjFlip",lib_pMobjFlip}, {"P_GetMobjGravity",lib_pGetMobjGravity}, {"P_WeaponOrPanel",lib_pWeaponOrPanel}, + {"P_GetTeamFlag",lib_pGetTeamFlag}, + {"P_GetTeamFlagMapthing",lib_pGetTeamFlagMapthing}, {"P_FlashPal",lib_pFlashPal}, {"P_GetClosestAxis",lib_pGetClosestAxis}, {"P_SpawnParaloop",lib_pSpawnParaloop}, diff --git a/src/lua_infolib.c b/src/lua_infolib.c index 140392158..0b4ae4735 100644 --- a/src/lua_infolib.c +++ b/src/lua_infolib.c @@ -2386,7 +2386,7 @@ static int teamlist_set(lua_State *L) 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); + return luaL_error(L, "team index %d out of range (0 - %d)", team, numteams - 1); teamlist->list[i - 1] = (UINT8)team; return 0; } diff --git a/src/p_inter.c b/src/p_inter.c index 0aa829d9e..ecd3e5aa0 100644 --- a/src/p_inter.c +++ b/src/p_inter.c @@ -888,7 +888,7 @@ void P_TouchSpecialThing(mobj_t *special, mobj_t *toucher, boolean heightcheck) player->gotflag |= teams[flagteam].flag; CONS_Printf(M_GetText("%s picked up the %s%s%c!\n"), plname, flagcolor, flagtext, 0x80); - flagmobjs[flagteam] = NULL; + P_SetTarget(&flagmobjs[flagteam], NULL); // code for dealing with abilities is handled elsewhere now break; } @@ -4443,7 +4443,7 @@ void P_PlayerFlagBurst(player_t *player, boolean toss) } // Pointers set for displaying time value and for consistency restoration. - flagmobjs[team] = flag; + P_SetTarget(&flagmobjs[team], flag); } player->gotflag = 0; diff --git a/src/p_local.h b/src/p_local.h index 563e257d8..fb8626b64 100644 --- a/src/p_local.h +++ b/src/p_local.h @@ -336,6 +336,9 @@ SINT8 P_MobjFlip(mobj_t *mobj); fixed_t P_GetMobjGravity(mobj_t *mo); FUNCMATH boolean P_WeaponOrPanel(mobjtype_t type); +mobj_t *P_GetTeamFlag(UINT8 team); +mapthing_t *P_GetTeamFlagMapthing(UINT8 team); + void P_CalcChasePostImg(player_t *player, camera_t *thiscam); boolean P_CameraThinker(player_t *player, camera_t *thiscam, boolean resetcalled); diff --git a/src/p_mobj.c b/src/p_mobj.c index 5532bb34b..ff8dd7c63 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -10037,7 +10037,7 @@ static void P_FlagFuseThink(mobj_t *mobj) else if (players[consoleplayer].ctfteam != 0) S_StartSound(NULL, sfx_hoop3); - flagmobjs[team] = flagmo; + P_SetTarget(&flagmobjs[team], flagmo); } } @@ -12069,7 +12069,7 @@ static boolean P_AllowMobjSpawn(mapthing_t* mthing, mobjtype_t i) UINT8 team = mobjinfo[i].mass; if (team >= numteams) return false; - else if (flagmobjs[team]) + else if (flagmobjs[team] && !P_MobjWasRemoved(flagmobjs[team])) { CONS_Alert(CONS_ERROR, M_GetText("Only one flag per team allowed in CTF!\n")); return false; @@ -13171,11 +13171,11 @@ static boolean P_SetupSpawnedMapThing(mapthing_t *mthing, mobj_t *mobj, boolean } break; case MT_REDFLAG: - flagmobjs[TEAM_RED] = mobj; + P_SetTarget(&flagmobjs[TEAM_RED], mobj); flagpoints[TEAM_RED] = mobj->spawnpoint; break; case MT_BLUEFLAG: - flagmobjs[TEAM_BLUE] = mobj; + P_SetTarget(&flagmobjs[TEAM_BLUE], mobj); flagpoints[TEAM_BLUE] = mobj->spawnpoint; break; case MT_NIGHTSSTAR: @@ -14185,3 +14185,19 @@ mobj_t *P_SpawnMobjFromMobj(mobj_t *mobj, fixed_t xofs, fixed_t yofs, fixed_t zo return newmobj; } + +mobj_t *P_GetTeamFlag(UINT8 team) +{ + if (team >= teamsingame || P_MobjWasRemoved(flagmobjs[team])) + return NULL; + + return flagmobjs[team]; +} + +mapthing_t *P_GetTeamFlagMapthing(UINT8 team) +{ + if (team >= teamsingame) + return NULL; + + return flagpoints[team]; +} diff --git a/src/p_saveg.c b/src/p_saveg.c index 231e9e616..0812f6c3a 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -3065,12 +3065,12 @@ static thinker_t* LoadMobjThinker(actionf_p1 thinker) if (diff & MD_REDFLAG) { - flagmobjs[TEAM_RED] = mobj; + P_SetTarget(&flagmobjs[TEAM_RED], mobj); flagpoints[TEAM_RED] = mobj->spawnpoint; } if (diff & MD_BLUEFLAG) { - flagmobjs[TEAM_BLUE] = mobj; + P_SetTarget(&flagmobjs[TEAM_BLUE], mobj); flagpoints[TEAM_BLUE] = mobj->spawnpoint; } diff --git a/src/p_setup.c b/src/p_setup.c index bd1264a83..89b8ff769 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -7040,7 +7040,7 @@ static void P_InitLevelSettings(void) // clear ctf pointers for (i = 0; i < MAXTEAMS; i++) { - flagmobjs[i] = NULL; + P_SetTarget(&flagmobjs[i], NULL); flagpoints[i] = NULL; } diff --git a/src/st_stuff.c b/src/st_stuff.c index bd1be0225..42bfda9d1 100644 --- a/src/st_stuff.c +++ b/src/st_stuff.c @@ -2480,12 +2480,15 @@ static void ST_drawTeamHUD(void) } // Display a countdown timer showing how much time left until the flag returns to base. + if (LUA_HudEnabled(hud_teamscores)) { - if (flagmobjs[G_GetTeam(2)] && flagmobjs[G_GetTeam(2)]->fuse > 1 && LUA_HudEnabled(hud_teamscores)) - V_DrawCenteredString(BASEVIDWIDTH/2 - SEP, 8, V_YELLOWMAP|V_HUDTRANS|V_PERPLAYER|V_SNAPTOTOP, va("%u", (flagmobjs[G_GetTeam(2)]->fuse / TICRATE))); + mobj_t *flagmobj = flagmobjs[G_GetTeam(2)]; + if (flagmobj && !P_MobjWasRemoved(flagmobj) && flagmobj->fuse > 1) + V_DrawCenteredString(BASEVIDWIDTH/2 - SEP, 8, V_YELLOWMAP|V_HUDTRANS|V_PERPLAYER|V_SNAPTOTOP, va("%u", (flagmobj->fuse / TICRATE))); - if (flagmobjs[G_GetTeam(1)] && flagmobjs[G_GetTeam(1)]->fuse > 1 && LUA_HudEnabled(hud_teamscores)) - V_DrawCenteredString(BASEVIDWIDTH/2 + SEP, 8, V_YELLOWMAP|V_HUDTRANS|V_PERPLAYER|V_SNAPTOTOP, va("%u", (flagmobjs[G_GetTeam(1)]->fuse / TICRATE))); + flagmobj = flagmobjs[G_GetTeam(1)]; + if (flagmobj && !P_MobjWasRemoved(flagmobj) && flagmobj->fuse > 1) + V_DrawCenteredString(BASEVIDWIDTH/2 + SEP, 8, V_YELLOWMAP|V_HUDTRANS|V_PERPLAYER|V_SNAPTOTOP, va("%u", (flagmobj->fuse / TICRATE))); } }