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.

222 lines
5.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef DOD_CONTROL_POINT_MASTER_H
  7. #define DOD_CONTROL_POINT_MASTER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "utlmap.h"
  12. #include "dod_shareddefs.h"
  13. #include "dod_team.h"
  14. #include "dod_gamerules.h"
  15. #include "dod_control_point.h"
  16. #include "dod_area_capture.h"
  17. #include "dod_objective_resource.h"
  18. #include "GameEventListener.h"
  19. // ====================
  20. // Control Point Master
  21. // ====================
  22. // One ControlPointMaster is spawned per level. Shortly after spawning it detects all the Control
  23. // points in the map and puts them into the m_ControlPoints. From there it detects the state
  24. // where all points are captured and resets them if necessary It gives points every time interval to
  25. // the owners of the points
  26. // ====================
  27. #define TIMER_CONTEXT "TIMER_CONTEXT"
  28. #define FLAGS_CONTEXT "FLAGS_CONTEXT"
  29. class CControlPointMaster : public CBaseEntity
  30. {
  31. public:
  32. DECLARE_CLASS( CControlPointMaster, CBaseEntity );
  33. CControlPointMaster();
  34. virtual void Spawn( void );
  35. virtual bool KeyValue( const char *szKeyName, const char *szValue );
  36. int GetNumPoints( void );
  37. int GetPointOwner( int point );
  38. void Reset( void );
  39. void RoundRespawn( void );
  40. int CountAdvantageFlags( int team );
  41. bool IsActive( void ) { return ( m_bDisabled == false ); }
  42. bool IsUsingRoundTimer( void ) { return m_bUseTimer; }
  43. void GetTimerData( int &iTimerSeconds, int &iTimerWinTeam );
  44. void FireTeamWinOutput( int iWinningTeam );
  45. void CheckWinConditions( void );
  46. bool WouldNewCPOwnerWinGame( CControlPoint *pPoint, int iNewOwner );
  47. CControlPoint *GetCPByIndex( int index );
  48. private:
  49. void BecomeActive( void );
  50. void BecomeInactive( void );
  51. void EXPORT CPMThink( void );
  52. int TeamOwnsAllPoints( CControlPoint *pOverridePoint = NULL, int iOverrideNewTeam = TEAM_UNASSIGNED );
  53. void TeamWins( int team );
  54. bool FindControlPoints( void ); //look in the map to find active control points
  55. void InputAddTimerSeconds( inputdata_t &inputdata );
  56. CUtlMap<int, CControlPoint *> m_ControlPoints;
  57. bool m_bFoundPoints; //true when the control points have been found and the array is initialized
  58. float m_fGivePointsTime; //the time at which we give points for holding control points
  59. DECLARE_DATADESC();
  60. bool m_bDisabled; //is this CPM active or not
  61. void InputEnable( inputdata_t &inputdata ) { BecomeInactive(); }
  62. void InputDisable( inputdata_t &inputdata ) { BecomeActive(); }
  63. void InputRoundInit( inputdata_t &inputdata );
  64. void InputRoundStart( inputdata_t &inputdata );
  65. bool m_bUseTimer;
  66. int m_iTimerTeam;
  67. int m_iTimerLength;
  68. COutputEvent m_AlliesWinOutput;
  69. COutputEvent m_AxisWinOutput;
  70. };
  71. class CDODCustomScoring : public CBaseEntity, public CGameEventListener
  72. {
  73. public:
  74. DECLARE_CLASS( CDODCustomScoring, CBaseEntity );
  75. DECLARE_DATADESC();
  76. CDODCustomScoring()
  77. {
  78. ListenForGameEvent( "dod_round_win" );
  79. ListenForGameEvent( "dod_round_active" );
  80. }
  81. virtual void Spawn( void )
  82. {
  83. Assert( m_iPointTeam == TEAM_ALLIES || m_iPointTeam == TEAM_AXIS );
  84. }
  85. virtual void FireGameEvent( IGameEvent *event )
  86. {
  87. const char *eventName = event->GetName();
  88. if ( !Q_strcmp( eventName, "dod_round_win" ) )
  89. {
  90. int team = event->GetInt( "team" );
  91. if ( team == m_iPointTeam )
  92. {
  93. GiveRemainingPoints();
  94. }
  95. // stop giving points, round is over
  96. SetThink( NULL ); // think no more!
  97. }
  98. else if ( !Q_strcmp( eventName, "dod_round_active" ) )
  99. {
  100. StartGivingPoints();
  101. }
  102. }
  103. // Needs to be activated by gamerules
  104. void StartGivingPoints( void )
  105. {
  106. if ( m_iNumPointGives <= 0 )
  107. return;
  108. if ( m_iPointsToGive <= 0 )
  109. return;
  110. m_iRemainingPoints = m_iNumPointGives * m_iPointsToGive;
  111. SetThink( &CDODCustomScoring::PointsThink );
  112. SetNextThink( gpGlobals->curtime + m_iTickLength );
  113. }
  114. // Give this team all the points that they would have gotten had we continued
  115. void GiveRemainingPoints( void )
  116. {
  117. GivePoints( m_iRemainingPoints );
  118. }
  119. // input to give tick points to our team
  120. void InputGivePoints( inputdata_t &inputdata )
  121. {
  122. int iPoints = inputdata.value.Int();
  123. GetGlobalTeam(m_iPointTeam)->AddScore( MAX( iPoints, 0 ) );
  124. }
  125. // accessor for our point team
  126. int GetPointTeam( void )
  127. {
  128. return m_iPointTeam;
  129. }
  130. private:
  131. void GivePoints( int points )
  132. {
  133. GetGlobalTeam(m_iPointTeam)->AddScore( MAX( points, 0 ) );
  134. m_iRemainingPoints -= points;
  135. if ( points > 0 )
  136. {
  137. if ( points == 1 )
  138. {
  139. UTIL_ClientPrintAll( HUD_PRINTTALK, "#game_score_allie_point" );
  140. }
  141. else
  142. {
  143. char buf[8];
  144. Q_snprintf( buf, sizeof(buf), "%d", points );
  145. UTIL_ClientPrintAll( HUD_PRINTTALK, "#game_score_allie_points", buf );
  146. }
  147. IGameEvent *event = gameeventmanager->CreateEvent( "dod_tick_points" );
  148. if ( event )
  149. {
  150. event->SetInt( "team", m_iPointTeam );
  151. event->SetInt( "score", points );
  152. event->SetInt( "totalscore", GetGlobalTeam(m_iPointTeam)->GetScore() );
  153. gameeventmanager->FireEvent( event );
  154. }
  155. }
  156. }
  157. void PointsThink( void )
  158. {
  159. GivePoints( m_iPointsToGive );
  160. SetNextThink( gpGlobals->curtime + m_iTickLength );
  161. }
  162. int m_iPointTeam; // team to give points to
  163. int m_iPointsToGive; // points to give per tick
  164. int m_iRemainingPoints; // total number of points we have left to give
  165. int m_iTickLength; // time between point gives
  166. int m_iNumPointGives; // number of times we're planning on giving out points
  167. };
  168. #endif //DOD_CONTROL_POINT_MASTER_H