|
57 | 57 | #include "missionchooser/iasw_mission_chooser_source.h" |
58 | 58 | #include "rd_vgui_vscript_shared.h" |
59 | 59 | #include "rd_crafting_defs.h" |
| 60 | +#include <algorithm> |
60 | 61 |
|
61 | 62 | // memdbgon must be the last include file in a .cpp file!!! |
62 | 63 | #include "tier0/memdbgon.h" |
@@ -2246,8 +2247,87 @@ void CASW_Player::SetSpectatingOrder( const int* iProfiles, int nProfiles ) |
2246 | 2247 | m_bSpectatingInOrder = true; |
2247 | 2248 | } |
2248 | 2249 |
|
| 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 | + |
2249 | 2323 | void CASW_Player::SpectateNextMarine() |
2250 | 2324 | { |
| 2325 | + if ( m_bSpectatingInOrder ) |
| 2326 | + { |
| 2327 | + SpectateNextMarineInOrder(); |
| 2328 | + return; |
| 2329 | + } |
| 2330 | + |
2251 | 2331 | CASW_Game_Resource* pGameResource = ASWGameResource(); |
2252 | 2332 | if (!pGameResource) |
2253 | 2333 | return; |
@@ -2282,7 +2362,7 @@ void CASW_Player::SpectateNextMarine() |
2282 | 2362 | { |
2283 | 2363 | //Msg(" we're spectating this dude, so clearing our current spectator\n"); |
2284 | 2364 | SetSpectatingNPC(NULL); |
2285 | | - } |
| 2365 | + } |
2286 | 2366 | } |
2287 | 2367 | //Msg("end\n"); |
2288 | 2368 | // 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 |
|
0 commit comments