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.

186 lines
4.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. // tactical_mission.cpp
  3. // Interface for managing player "missions"
  4. // Michael Booth, June 2009
  5. #include "cbase.h"
  6. #include "tactical_mission.h"
  7. /**
  8. * Global singleton accessor.
  9. */
  10. CTacticalMissionManager &TheTacticalMissions( void )
  11. {
  12. static CTacticalMissionManager *manager = g_pGameRules->TacticalMissionManagerFactory();
  13. return *manager;
  14. }
  15. //---------------------------------------------------------------------------------------------
  16. class CListMissions : public CTacticalMissionManager::IForEachMission
  17. {
  18. public:
  19. virtual bool Inspect( const CTacticalMission &mission )
  20. {
  21. Msg( "%s\n", mission.GetName() );
  22. return true;
  23. }
  24. };
  25. CON_COMMAND_F( mission_list, "List all available tactical missions", FCVAR_GAMEDLL )
  26. {
  27. if ( !UTIL_IsCommandIssuedByServerAdmin() )
  28. return;
  29. CListMissions list;
  30. TheTacticalMissions().ForEachMission( list );
  31. }
  32. //---------------------------------------------------------------------------------------------
  33. class CShowZone : public IForEachNavArea
  34. {
  35. public:
  36. virtual bool Inspect( const CNavArea *area )
  37. {
  38. area->DrawFilled( 255, 255, 0, 255, 9999.9f );
  39. return true;
  40. }
  41. };
  42. CON_COMMAND_F( mission_show, "Show the given mission", FCVAR_GAMEDLL )
  43. {
  44. if ( !UTIL_IsCommandIssuedByServerAdmin() )
  45. return;
  46. if ( args.ArgC() < 2 )
  47. {
  48. Msg( "%s <name of mission>\n", args.Arg(0) );
  49. return;
  50. }
  51. const CTacticalMission *mission = TheTacticalMissions().GetMission( args.Arg(1) );
  52. if ( mission )
  53. {
  54. const CTacticalMissionZone *zone = mission->GetDeployZone( NULL );
  55. if ( zone )
  56. {
  57. CShowZone show;
  58. zone->ForEachArea( show );
  59. }
  60. else
  61. {
  62. Msg( "No deploy zone\n" );
  63. }
  64. }
  65. else
  66. {
  67. Msg( "Unknown mission '%s'\n", args.Arg(1) );
  68. }
  69. }
  70. //---------------------------------------------------------------------------------------------
  71. CNavArea *CTacticalMissionZone::SelectArea( CBasePlayer *who ) const
  72. {
  73. if ( m_areaVector.Count() == 0 )
  74. return NULL;
  75. int which = RandomInt( 0, m_areaVector.Count()-1 );
  76. return m_areaVector[ which ];
  77. }
  78. //---------------------------------------------------------------------------------------------
  79. /**
  80. * Iterate each area in this zone.
  81. * If functor returns false, stop iterating and return false.
  82. */
  83. bool CTacticalMissionZone::ForEachArea( IForEachNavArea &func ) const
  84. {
  85. int i;
  86. for( i=0; i<m_areaVector.Count(); ++i )
  87. {
  88. if ( func.Inspect( m_areaVector[i] ) == false )
  89. break;
  90. }
  91. bool wasCompleteIteration = ( i == m_areaVector.Count() );
  92. func.PostIteration( wasCompleteIteration );
  93. return wasCompleteIteration;
  94. }
  95. //---------------------------------------------------------------------------------------------
  96. //---------------------------------------------------------------------------------------------
  97. CTacticalMissionManager::CTacticalMissionManager( void )
  98. {
  99. ListenForGameEvent( "round_start" );
  100. ListenForGameEvent( "teamplay_round_start" );
  101. }
  102. //---------------------------------------------------------------------------------------------
  103. void CTacticalMissionManager::FireGameEvent( IGameEvent *gameEvent )
  104. {
  105. if ( FStrEq( gameEvent->GetName(), "round_start" ) || FStrEq( gameEvent->GetName(), "teamplay_round_start" ) )
  106. {
  107. OnRoundRestart();
  108. }
  109. }
  110. //---------------------------------------------------------------------------------------------
  111. void CTacticalMissionManager::Register( CTacticalMission *mission )
  112. {
  113. if ( m_missionVector.Find( mission ) == m_missionVector.InvalidIndex() )
  114. {
  115. m_missionVector.AddToTail( mission );
  116. }
  117. }
  118. //---------------------------------------------------------------------------------------------
  119. void CTacticalMissionManager::Unregister( CTacticalMission *mission )
  120. {
  121. m_missionVector.FindAndRemove( mission );
  122. }
  123. //---------------------------------------------------------------------------------------------
  124. /**
  125. * Given a mission name, return the mission (or NULL)
  126. */
  127. const CTacticalMission *CTacticalMissionManager::GetMission( const char *name )
  128. {
  129. FOR_EACH_VEC( m_missionVector, it )
  130. {
  131. if ( FStrEq( m_missionVector[it]->GetName(), name ) )
  132. return m_missionVector[it];
  133. }
  134. return NULL;
  135. }
  136. //---------------------------------------------------------------------------------------------
  137. /**
  138. * Iterate each mission.
  139. * If functor returns false, stop iterating and return false.
  140. */
  141. bool CTacticalMissionManager::ForEachMission( CTacticalMissionManager::IForEachMission &func )
  142. {
  143. FOR_EACH_VEC( m_missionVector, it )
  144. {
  145. if ( !func.Inspect( *m_missionVector[it] ) )
  146. return false;
  147. }
  148. return true;
  149. }