Skip to content

Commit 019499d

Browse files
committed
feat(asw_player): add spectate in order
1 parent ec1984d commit 019499d

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

src/game/server/swarm/asw_player.cpp

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "missionchooser/iasw_mission_chooser_source.h"
5858
#include "rd_vgui_vscript_shared.h"
5959
#include "rd_crafting_defs.h"
60+
#include <algorithm>
6061

6162
// memdbgon must be the last include file in a .cpp file!!!
6263
#include "tier0/memdbgon.h"
@@ -2246,8 +2247,87 @@ void CASW_Player::SetSpectatingOrder( const int* iProfiles, int nProfiles )
22462247
m_bSpectatingInOrder = true;
22472248
}
22482249

2250+
bool CASW_Player::CompareMarinesSpectatingPriority( CASW_Marine* pM1, CASW_Marine* pM2 ) const
2251+
{
2252+
// Assign worst priority if no marine or no profile index
2253+
const int iProfile1 = pM1 ? pM1->GetMarineProfile()->m_ProfileIndex : INT_MAX;
2254+
const int iProfile2 = pM2 ? pM2->GetMarineProfile()->m_ProfileIndex : INT_MAX;
2255+
2256+
const int iPriority1 = ( iProfile1 >= 0 && iProfile1 < ASW_NUM_MARINE_PROFILES ) ? m_iSpectatingMapping[iProfile1] : INT_MAX;
2257+
const int iPriority2 = ( iProfile2 >= 0 && iProfile2 < ASW_NUM_MARINE_PROFILES ) ? m_iSpectatingMapping[iProfile2] : INT_MAX;
2258+
2259+
return iPriority1 < iPriority2;
2260+
}
2261+
2262+
void CASW_Player::SpectateNextMarineInOrder()
2263+
{
2264+
CASW_Game_Resource* pGameResource = ASWGameResource();
2265+
if ( !pGameResource )
2266+
return;
2267+
2268+
int iSortedCount = 0;
2269+
CASW_Marine* pMarinesSorted[ASW_MAX_MARINE_RESOURCES] = { NULL };
2270+
2271+
// Gather all valid marines
2272+
for ( int i = 0; i < pGameResource->GetMaxMarineResources(); i++ )
2273+
{
2274+
CASW_Marine_Resource* pMR = pGameResource->GetMarineResource( i );
2275+
CASW_Marine *pMarine = pMR ? pMR->GetMarineEntity() : NULL;
2276+
if ( pMarine && pMarine->IsAlive() && pMarine->GetHealth() > 0 )
2277+
{
2278+
pMarinesSorted[iSortedCount] = pMarine;
2279+
iSortedCount++;
2280+
}
2281+
}
2282+
2283+
std::sort(
2284+
pMarinesSorted,
2285+
pMarinesSorted + iSortedCount,
2286+
[this](CASW_Marine* a, CASW_Marine* b) {
2287+
return CompareMarinesSpectatingPriority(a, b);
2288+
}
2289+
);
2290+
2291+
//Msg("CASW_Player::SpectateNextMarineInOrder\n");
2292+
2293+
//Msg(" set first guy as our first\n");
2294+
CASW_Marine *pFirst = pMarinesSorted[0];
2295+
2296+
// loop through all valid marines
2297+
for ( int i = 0; i < iSortedCount; i++ )
2298+
{
2299+
//Msg("Checking pMR %d\n", i);
2300+
CASW_Marine *pMarine = pMarinesSorted[i]; // Alive marine
2301+
2302+
if (GetSpectatingNPC() == NULL) // if we're not spectating anything yet, then spectate the first one we find
2303+
{
2304+
//Msg(" We're not spectating anyone, so we're gonna spec this dude\n");
2305+
SetSpectatingNPC(pMarine);
2306+
break;
2307+
}
2308+
if (GetSpectatingNPC() == pMarine) // if we're spectating this one, then clear it, so the next one we find will get set
2309+
{
2310+
//Msg(" we're spectating this dude, so clearing our current spectator\n");
2311+
SetSpectatingNPC(NULL);
2312+
}
2313+
}
2314+
//Msg("end\n");
2315+
// if we're still not spectating anything but we found at least marine, then that means we were spectating the last one in the list and need to set this
2316+
if (GetSpectatingNPC() == NULL && pFirst)
2317+
{
2318+
//Msg(" but we're still not speccing anyone and we have a first set, so speccing that dude\n");
2319+
SetSpectatingNPC(pFirst);
2320+
}
2321+
}
2322+
22492323
void CASW_Player::SpectateNextMarine()
22502324
{
2325+
if ( m_bSpectatingInOrder )
2326+
{
2327+
SpectateNextMarineInOrder();
2328+
return;
2329+
}
2330+
22512331
CASW_Game_Resource* pGameResource = ASWGameResource();
22522332
if (!pGameResource)
22532333
return;
@@ -2282,7 +2362,7 @@ void CASW_Player::SpectateNextMarine()
22822362
{
22832363
//Msg(" we're spectating this dude, so clearing our current spectator\n");
22842364
SetSpectatingNPC(NULL);
2285-
}
2365+
}
22862366
}
22872367
//Msg("end\n");
22882368
// if we're still not spectating anything but we found at least marine, then that means we were spectating the last one in the list and need to set this

src/game/server/swarm/asw_player.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ class CASW_Player : public CBaseMultiplayerPlayer, public IASWPlayerAnimStateHel
9090
bool m_bSpectatingInOrder = false;
9191
int m_iSpectatingMapping[ASW_NUM_MARINE_PROFILES];
9292
public:
93+
bool CompareMarinesSpectatingPriority( CASW_Marine* pM1, CASW_Marine* pM2 ) const;
9394
void UnsetSpectatingOrder() { m_bSpectatingInOrder = false; }
9495
void SetSpectatingOrder( const int* iProfiles, int nProfiles );
96+
void SpectateNextMarineInOrder();
9597
void SpectateNextMarine();
9698
void SetSpectatingNPC( CASW_Inhabitable_NPC *pSpectating );
9799
CASW_Inhabitable_NPC *GetSpectatingNPC() const;

0 commit comments

Comments
 (0)