Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "tf/tf_shareddefs.h"
  8. #include "entity_forcerespawn.h"
  9. #include "tf_player.h"
  10. #include "tf_gamerules.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. //=============================================================================
  14. //
  15. // CTFReset tables.
  16. //
  17. BEGIN_DATADESC( CTFForceRespawn )
  18. // Inputs.
  19. DEFINE_INPUTFUNC( FIELD_VOID, "ForceRespawn", InputForceRespawn ),
  20. DEFINE_INPUTFUNC( FIELD_VOID, "ForceRespawnSwitchTeams", InputForceRespawnSwitchTeams ),
  21. DEFINE_INPUTFUNC( FIELD_INTEGER, "ForceTeamRespawn", InputForceTeamRespawn ),
  22. // Outputs.
  23. DEFINE_OUTPUT( m_outputOnForceRespawn, "OnForceRespawn" ),
  24. END_DATADESC()
  25. LINK_ENTITY_TO_CLASS( game_forcerespawn, CTFForceRespawn );
  26. //-----------------------------------------------------------------------------
  27. // Purpose: Constructor.
  28. //-----------------------------------------------------------------------------
  29. CTFForceRespawn::CTFForceRespawn()
  30. {
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Purpose:
  34. //-----------------------------------------------------------------------------
  35. void CTFForceRespawn::ForceRespawn( bool bSwitchTeams, int nTeam /* = TEAM_UNASSIGNED */, bool bRemoveEverything /* = true */ )
  36. {
  37. int i = 0;
  38. if ( bRemoveEverything && TFGameRules() )
  39. {
  40. TFGameRules()->RemoveAllProjectilesAndBuildings();
  41. }
  42. // respawn the players
  43. for ( i = 1 ; i <= gpGlobals->maxClients ; i++ )
  44. {
  45. CTFPlayer *pPlayer = ToTFPlayer( UTIL_PlayerByIndex( i ) );
  46. if ( pPlayer )
  47. {
  48. // Ignore players who aren't on an active team
  49. if ( pPlayer->GetTeamNumber() != TF_TEAM_RED && pPlayer->GetTeamNumber() != TF_TEAM_BLUE )
  50. {
  51. // Let the player spawn immediately when they do pick a class
  52. pPlayer->AllowInstantSpawn();
  53. continue;
  54. }
  55. if ( bSwitchTeams )
  56. {
  57. if ( pPlayer->GetTeamNumber() == TF_TEAM_RED )
  58. {
  59. pPlayer->ForceChangeTeam( TF_TEAM_BLUE, true );
  60. }
  61. else if ( pPlayer->GetTeamNumber() == TF_TEAM_BLUE )
  62. {
  63. pPlayer->ForceChangeTeam( TF_TEAM_RED, true );
  64. }
  65. }
  66. // Ignore players who haven't picked a class yet
  67. if ( !pPlayer->GetPlayerClass() || pPlayer->GetPlayerClass()->GetClassIndex() == TF_CLASS_UNDEFINED )
  68. {
  69. // Allow them to spawn instantly when they do choose
  70. pPlayer->AllowInstantSpawn();
  71. continue;
  72. }
  73. if ( nTeam != TEAM_UNASSIGNED )
  74. {
  75. // Ignore players who aren't on the team we're trying to respawn
  76. if ( pPlayer->GetTeamNumber() != nTeam )
  77. {
  78. continue;
  79. }
  80. else
  81. {
  82. // Ignore players on the team that aren't dead
  83. if ( pPlayer->IsAlive() )
  84. continue;
  85. }
  86. }
  87. pPlayer->ForceRespawn();
  88. }
  89. }
  90. // remove any dropped weapons/ammo packs
  91. CBaseEntity *pEnt = NULL;
  92. while ( (pEnt = gEntList.FindEntityByClassname( pEnt, "tf_ammo_pack" )) != NULL )
  93. {
  94. UTIL_Remove( pEnt );
  95. }
  96. // Output.
  97. m_outputOnForceRespawn.FireOutput( this, this );
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose:
  101. //-----------------------------------------------------------------------------
  102. void CTFForceRespawn::InputForceRespawn( inputdata_t &inputdata )
  103. {
  104. ForceRespawn( false );
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose:
  108. //-----------------------------------------------------------------------------
  109. void CTFForceRespawn::InputForceRespawnSwitchTeams( inputdata_t &inputdata )
  110. {
  111. ForceRespawn( true );
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Purpose:
  115. //-----------------------------------------------------------------------------
  116. void CTFForceRespawn::InputForceTeamRespawn( inputdata_t &inputdata )
  117. {
  118. int nTeam = inputdata.value.Int();
  119. ForceRespawn( false, nTeam, false );
  120. }