From f493e4aae0a9821222410bc139a71f94a07725b2 Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Sat, 12 Aug 2023 20:28:16 -0300 Subject: [PATCH] Add G_FindPlayerStart, G_FindMatchStart, and G_FindTeamStart to Lua --- src/g_game.c | 75 +++++++++++++++++++++++++++++++++++++---------- src/g_game.h | 8 ++--- src/lua_baselib.c | 47 +++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 19 deletions(-) diff --git a/src/g_game.c b/src/g_game.c index a79a2321d..0764f5669 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -2885,10 +2885,7 @@ static boolean G_AreTeamStartsAvailableForPlayer(INT32 playernum) mapthing_t *G_FindTeamStart(INT32 playernum) { UINT8 team = players[playernum].ctfteam; - if (team == TEAM_NONE || team >= numteams) - return G_FindMatchStart(playernum); - - if (G_AreTeamStartsAvailable(team)) + if (team != TEAM_NONE && G_AreTeamStartsAvailable(team)) { for (INT32 j = 0; j < MAXPLAYERS; j++) { @@ -2940,10 +2937,12 @@ static mapthing_t *G_FindBestStart(INT32 playernum, INT32 type) { case PLAYER_START_TYPE_COOP: return G_FindCoopStart(playernum); + case PLAYER_START_TYPE_TEAM: + if (players[playernum].ctfteam > TEAM_NONE && players[playernum].ctfteam < numteams) + return G_FindTeamStart(playernum); + /* FALLTHRU */ case PLAYER_START_TYPE_MATCH: return G_FindMatchStart(playernum); - case PLAYER_START_TYPE_TEAM: - return G_FindTeamStart(playernum); } return NULL; } @@ -3072,8 +3071,8 @@ mapthing_t *G_FindMapStart(INT32 playernum) mapthing_t *spawnpoint = NULL; // -- Spectators -- - // Order in platform gametypes: Coop->DM->CTF - // Otherwise: DM->CTF->Coop + // Order in platform gametypes: Coop->DM->Team + // Otherwise: DM->Team->Coop if (player->spectator) { // In platform gametypes, spawn in Co-op starts first @@ -3085,17 +3084,17 @@ mapthing_t *G_FindMapStart(INT32 playernum) } // -- CTF -- - // Order: CTF->DM->Coop - else if (gametyperules & GTR_TEAMFLAGS) + // Order: Team->DM->Coop + else if (gametyperules & GTR_TEAMFLAGS && G_GametypeHasTeams()) { - if (player->ctfteam) + if (player->ctfteam > TEAM_NONE && player->ctfteam < numteams) spawnpoint = G_GetTeamStartForSpawning(playernum); else spawnpoint = G_GetMatchStartForSpawning(playernum); } // -- DM/Tag/etc -- - // Order: DM->CTF->Coop + // Order: DM->Team->Coop else if (gametyperules & GTR_DEATHMATCHSTARTS) { // If the current gametype has teams, but isn't CTF, then this looks for a team start first @@ -3104,20 +3103,20 @@ mapthing_t *G_FindMapStart(INT32 playernum) { spawnpoint = G_FindTeamStart(playernum); - // If no spawn point was found, but team starts are available, this means there is no good location + // If no spawn point was returned, but team starts are available, this means there is no good location // for the player to spawn at. In that situation, we display a message telling the player so. if (spawnpoint == NULL && G_AreTeamStartsAvailable(player->ctfteam) && P_IsLocalPlayer(player)) CONS_Alert(CONS_WARNING, M_GetText("Could not spawn at any %s starts!\n"), G_GetTeamName(player->ctfteam)); } // If that failed, no warning is shown. Instead, this will look for a match start, which may - // then display a warning if no suitable map starts were found. + // then display a warning if no suitable map start was found. if (spawnpoint == NULL) spawnpoint = G_GetMatchStartForSpawning(playernum); } // -- Other game modes -- - // Order: Coop->DM->CTF + // Order: Coop->DM->Team else spawnpoint = G_GetCoopStartForSpawning(playernum); @@ -3140,6 +3139,52 @@ mapthing_t *G_FindMapStart(INT32 playernum) return spawnpoint; } +mapthing_t *G_FindBestPlayerStart(INT32 playernum) +{ + if (!playeringame[playernum]) + return NULL; + + player_t *player = &players[playernum]; + + // Spectator + if (player->spectator) + { + if (G_PlatformGametype() && !(gametyperules & GTR_DEATHMATCHSTARTS)) + return G_FindCoopStart(playernum); + else + return G_FindMatchStart(playernum); + } + // CTF + else if (gametyperules & GTR_TEAMFLAGS && G_GametypeHasTeams()) + { + mapthing_t *mapthing = NULL; + + if (player->ctfteam > TEAM_NONE && player->ctfteam < numteams) + mapthing = G_FindTeamStart(playernum); + + if (mapthing) + return mapthing; + else + return G_FindMatchStart(playernum); + } + // Gametypes that use match starts + else if (gametyperules & GTR_DEATHMATCHSTARTS) + { + mapthing_t *mapthing = NULL; + + if (G_GametypeHasTeams() && player->ctfteam > TEAM_NONE && player->ctfteam < numteams) + mapthing = G_FindTeamStart(playernum); + + if (mapthing) + return mapthing; + else + return G_FindMatchStart(playernum); + } + + // Everything else (just uses Co-Op) + return G_FindCoopStart(playernum); +} + // Go back through all the projectiles and remove all references to the old // player mobj, replacing them with the new one. void G_ChangePlayerReferences(mobj_t *oldmo, mobj_t *newmo) diff --git a/src/g_game.h b/src/g_game.h index fe22ef10c..3cba4901c 100644 --- a/src/g_game.h +++ b/src/g_game.h @@ -164,11 +164,11 @@ void G_FreeMapSearch(mapsearchfreq_t *freq, INT32 freqc); /* Match map name by search + 2 digit map code or map number. */ INT32 G_FindMapByNameOrCode(const char *query, char **foundmapnamep); -// XMOD spawning -mapthing_t *G_FindTeamStart(INT32 playernum); -mapthing_t *G_FindMatchStart(INT32 playernum); -mapthing_t *G_FindCoopStart(INT32 playernum); mapthing_t *G_FindMapStart(INT32 playernum); +mapthing_t *G_FindBestPlayerStart(INT32 playernum); +mapthing_t *G_FindCoopStart(INT32 playernum); +mapthing_t *G_FindMatchStart(INT32 playernum); +mapthing_t *G_FindTeamStart(INT32 playernum); void G_MovePlayerToSpawnOrStarpost(INT32 playernum); void G_SpawnPlayer(INT32 playernum); diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 7f9afefd9..ef440beea 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -4096,6 +4096,50 @@ static int lib_gGetMostDisadvantagedTeam(lua_State *L) return 1; } +static int lib_gFindPlayerStart(lua_State *L) +{ + player_t *plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); + INLEVEL + if (!plr) + return LUA_ErrInvalid(L, "player_t"); + mapthing_t *mapthing = G_FindBestPlayerStart(plr - players); + if (mapthing) + LUA_PushUserdata(L, mapthing, META_MAPTHING); + else + lua_pushnil(L); + return 1; +} + +static int lib_gFindMatchStart(lua_State *L) +{ + player_t *plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); + INLEVEL + if (!plr) + return LUA_ErrInvalid(L, "player_t"); + mapthing_t *mapthing = G_FindMatchStart(plr - players); + if (mapthing) + LUA_PushUserdata(L, mapthing, META_MAPTHING); + else + lua_pushnil(L); + return 1; +} + +static int lib_gFindTeamStart(lua_State *L) +{ + player_t *plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); + INLEVEL + if (!plr) + return LUA_ErrInvalid(L, "player_t"); + mapthing_t *mapthing = NULL; + if (plr->ctfteam > TEAM_NONE && plr->ctfteam < numteams) + mapthing = G_FindTeamStart(plr - players); + if (mapthing) + LUA_PushUserdata(L, mapthing, META_MAPTHING); + else + lua_pushnil(L); + return 1; +} + static int lib_gTicsToHours(lua_State *L) { tic_t rtic = luaL_checkinteger(L, 1); @@ -4429,6 +4473,9 @@ static luaL_Reg lib[] = { {"G_GetWorstPerformingTeam",lib_gGetWorstPerformingTeam}, {"G_GetMostAdvantagedTeam",lib_gGetMostAdvantagedTeam}, {"G_GetMostDisadvantagedTeam",lib_gGetMostDisadvantagedTeam}, + {"G_FindPlayerStart",lib_gFindPlayerStart}, + {"G_FindMatchStart",lib_gFindMatchStart}, + {"G_FindTeamStart",lib_gFindTeamStart}, {"G_TicsToHours",lib_gTicsToHours}, {"G_TicsToMinutes",lib_gTicsToMinutes}, {"G_TicsToSeconds",lib_gTicsToSeconds},