MultiplayerGame.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __MULTIPLAYERGAME_H__
  21. #define __MULTIPLAYERGAME_H__
  22. /*
  23. ===============================================================================
  24. Basic DOOM multiplayer
  25. ===============================================================================
  26. */
  27. class idPlayer;
  28. #ifdef CTF
  29. class idItemTeam;
  30. #endif
  31. typedef enum {
  32. GAME_SP = 0,
  33. GAME_DM,
  34. GAME_TOURNEY,
  35. GAME_TDM,
  36. GAME_LASTMAN,
  37. #ifdef CTF
  38. GAME_CTF,
  39. GAME_COUNT,
  40. #endif
  41. } gameType_t;
  42. #ifdef CTF
  43. // Used by the UI
  44. typedef enum {
  45. FLAGSTATUS_INBASE = 0,
  46. FLAGSTATUS_TAKEN = 1,
  47. FLAGSTATUS_STRAY = 2,
  48. FLAGSTATUS_NONE = 3
  49. } flagStatus_t;
  50. #endif
  51. typedef enum {
  52. PLAYER_VOTE_NONE,
  53. PLAYER_VOTE_NO,
  54. PLAYER_VOTE_YES,
  55. PLAYER_VOTE_WAIT // mark a player allowed to vote
  56. } playerVote_t;
  57. typedef struct mpPlayerState_s {
  58. int ping; // player ping
  59. int fragCount; // kills
  60. int teamFragCount; // team kills
  61. int wins; // wins
  62. playerVote_t vote; // player's vote
  63. bool scoreBoardUp; // toggle based on player scoreboard button, used to activate de-activate the scoreboard gui
  64. bool ingame;
  65. } mpPlayerState_t;
  66. const int NUM_CHAT_NOTIFY = 5;
  67. const int CHAT_FADE_TIME = 400;
  68. const int FRAGLIMIT_DELAY = 2000;
  69. const int MP_PLAYER_MINFRAGS = -100;
  70. #ifdef CTF
  71. const int MP_PLAYER_MAXFRAGS = 400; // in CTF frags are player points
  72. #else
  73. const int MP_PLAYER_MAXFRAGS = 100;
  74. #endif
  75. const int MP_PLAYER_MAXWINS = 100;
  76. const int MP_PLAYER_MAXPING = 999;
  77. #ifdef CTF
  78. const int MP_CTF_MAXPOINTS = 25;
  79. #endif
  80. typedef struct mpChatLine_s {
  81. idStr line;
  82. short fade; // starts high and decreases, line is removed once reached 0
  83. } mpChatLine_t;
  84. typedef enum {
  85. SND_YOUWIN = 0,
  86. SND_YOULOSE,
  87. SND_FIGHT,
  88. SND_VOTE,
  89. SND_VOTE_PASSED,
  90. SND_VOTE_FAILED,
  91. SND_THREE,
  92. SND_TWO,
  93. SND_ONE,
  94. SND_SUDDENDEATH,
  95. #ifdef CTF
  96. SND_FLAG_CAPTURED_YOURS,
  97. SND_FLAG_CAPTURED_THEIRS,
  98. SND_FLAG_RETURN,
  99. SND_FLAG_TAKEN_YOURS,
  100. SND_FLAG_TAKEN_THEIRS,
  101. SND_FLAG_DROPPED_YOURS,
  102. SND_FLAG_DROPPED_THEIRS,
  103. #endif
  104. SND_COUNT
  105. } snd_evt_t;
  106. class idMultiplayerGame {
  107. public:
  108. idMultiplayerGame();
  109. void Shutdown( void );
  110. // resets everything and prepares for a match
  111. void Reset( void );
  112. // setup local data for a new player
  113. void SpawnPlayer( int clientNum );
  114. // checks rules and updates state of the mp game
  115. void Run( void );
  116. // draws mp hud, scoredboard, etc..
  117. bool Draw( int clientNum );
  118. // updates a player vote
  119. void PlayerVote( int clientNum, playerVote_t vote );
  120. // updates frag counts and potentially ends the match in sudden death
  121. void PlayerDeath( idPlayer *dead, idPlayer *killer, bool telefrag );
  122. void AddChatLine( const char *fmt, ... ) id_attribute((format(printf,2,3)));
  123. void UpdateMainGui( void );
  124. idUserInterface*StartMenu( void );
  125. const char* HandleGuiCommands( const char *menuCommand );
  126. void SetMenuSkin( void );
  127. void WriteToSnapshot( idBitMsgDelta &msg ) const;
  128. void ReadFromSnapshot( const idBitMsgDelta &msg );
  129. // game state
  130. typedef enum {
  131. INACTIVE = 0, // not running
  132. WARMUP, // warming up
  133. COUNTDOWN, // post warmup pre-game
  134. GAMEON, // game is on
  135. SUDDENDEATH, // game is on but in sudden death, first frag wins
  136. GAMEREVIEW, // game is over, scoreboard is up. we wait si_gameReviewPause seconds (which has a min value)
  137. NEXTGAME,
  138. STATE_COUNT
  139. } gameState_t;
  140. static const char *GameStateStrings[ STATE_COUNT ];
  141. idMultiplayerGame::gameState_t GetGameState( void ) const;
  142. static const char *GlobalSoundStrings[ SND_COUNT ];
  143. void PlayGlobalSound( int to, snd_evt_t evt, const char *shader = NULL );
  144. #ifdef CTF
  145. void PlayTeamSound( int toTeam, snd_evt_t evt, const char *shader = NULL ); // sound that's sent only to member of toTeam team
  146. #endif
  147. // more compact than a chat line
  148. typedef enum {
  149. MSG_SUICIDE = 0,
  150. MSG_KILLED,
  151. MSG_KILLEDTEAM,
  152. MSG_DIED,
  153. MSG_VOTE,
  154. MSG_VOTEPASSED,
  155. MSG_VOTEFAILED,
  156. MSG_SUDDENDEATH,
  157. MSG_FORCEREADY,
  158. MSG_JOINEDSPEC,
  159. MSG_TIMELIMIT,
  160. MSG_FRAGLIMIT,
  161. MSG_TELEFRAGGED,
  162. MSG_JOINTEAM,
  163. MSG_HOLYSHIT,
  164. #ifdef CTF
  165. MSG_POINTLIMIT,
  166. MSG_FLAGTAKEN,
  167. MSG_FLAGDROP,
  168. MSG_FLAGRETURN,
  169. MSG_FLAGCAPTURE,
  170. MSG_SCOREUPDATE,
  171. #endif
  172. MSG_COUNT
  173. } msg_evt_t;
  174. void PrintMessageEvent( int to, msg_evt_t evt, int parm1 = -1, int parm2 = -1 );
  175. void DisconnectClient( int clientNum );
  176. static void ForceReady_f( const idCmdArgs &args );
  177. static void DropWeapon_f( const idCmdArgs &args );
  178. static void MessageMode_f( const idCmdArgs &args );
  179. static void VoiceChat_f( const idCmdArgs &args );
  180. static void VoiceChatTeam_f( const idCmdArgs &args );
  181. typedef enum {
  182. VOTE_RESTART = 0,
  183. VOTE_TIMELIMIT,
  184. VOTE_FRAGLIMIT,
  185. VOTE_GAMETYPE,
  186. VOTE_KICK,
  187. VOTE_MAP,
  188. VOTE_SPECTATORS,
  189. VOTE_NEXTMAP,
  190. VOTE_COUNT,
  191. VOTE_NONE
  192. } vote_flags_t;
  193. typedef enum {
  194. VOTE_UPDATE,
  195. VOTE_FAILED,
  196. VOTE_PASSED, // passed, but no reset yet
  197. VOTE_ABORTED,
  198. VOTE_RESET // tell clients to reset vote state
  199. } vote_result_t;
  200. static void Vote_f( const idCmdArgs &args );
  201. static void CallVote_f( const idCmdArgs &args );
  202. void ClientCallVote( vote_flags_t voteIndex, const char *voteValue );
  203. void ServerCallVote( int clientNum, const idBitMsg &msg );
  204. void ClientStartVote( int clientNum, const char *voteString );
  205. void ServerStartVote( int clientNum, vote_flags_t voteIndex, const char *voteValue );
  206. void ClientUpdateVote( vote_result_t result, int yesCount, int noCount );
  207. void CastVote( int clientNum, bool vote );
  208. void ExecuteVote( void );
  209. void WantKilled( int clientNum );
  210. int NumActualClients( bool countSpectators, int *teamcount = NULL );
  211. void DropWeapon( int clientNum );
  212. void MapRestart( void );
  213. // called by idPlayer whenever it detects a team change (init or switch)
  214. void SwitchToTeam( int clientNum, int oldteam, int newteam );
  215. bool IsPureReady( void ) const;
  216. void ProcessChatMessage( int clientNum, bool team, const char *name, const char *text, const char *sound );
  217. void ProcessVoiceChat( int clientNum, bool team, int index );
  218. void Precache( void );
  219. // throttle UI switch rates
  220. void ThrottleUserInfo( void );
  221. void ToggleSpectate( void );
  222. void ToggleReady( void );
  223. void ToggleTeam( void );
  224. void ClearFrags( int clientNum );
  225. void EnterGame( int clientNum );
  226. bool CanPlay( idPlayer *p );
  227. bool IsInGame( int clientNum );
  228. bool WantRespawn( idPlayer *p );
  229. void ServerWriteInitialReliableMessages( int clientNum );
  230. void ClientReadStartState( const idBitMsg &msg );
  231. void ClientReadWarmupTime( const idBitMsg &msg );
  232. void ServerClientConnect( int clientNum );
  233. #ifdef CTF
  234. void ClearHUDStatus( void );
  235. int GetFlagPoints( int team ); // Team points in CTF
  236. void SetFlagMsg( bool b ); // allow flag event messages to be sent
  237. bool IsFlagMsgOn( void ); // should flag event messages go through?
  238. void ClearGuis( void );
  239. int player_red_flag; // Ent num of red flag carrier for HUD
  240. int player_blue_flag; // Ent num of blue flag carrier for HUD
  241. #endif
  242. void PlayerStats( int clientNum, char *data, const int len );
  243. private:
  244. static const char *MPGuis[];
  245. static const char *ThrottleVars[];
  246. static const char *ThrottleVarsInEnglish[];
  247. static const int ThrottleDelay[];
  248. // state vars
  249. gameState_t gameState; // what state the current game is in
  250. gameState_t nextState; // state to switch to when nextStateSwitch is hit
  251. int pingUpdateTime; // time to update ping
  252. mpPlayerState_t playerState[ MAX_CLIENTS ];
  253. // keep track of clients which are willingly in spectator mode
  254. // vote vars
  255. vote_flags_t vote; // active vote or VOTE_NONE
  256. int voteTimeOut; // when the current vote expires
  257. int voteExecTime; // delay between vote passed msg and execute
  258. float yesVotes; // counter for yes votes
  259. float noVotes; // and for no votes
  260. idStr voteValue; // the data voted upon ( server )
  261. idStr voteString; // the vote string ( client )
  262. bool voted; // hide vote box ( client )
  263. int kickVoteMap[ MAX_CLIENTS ];
  264. // time related
  265. int nextStateSwitch; // time next state switch
  266. int warmupEndTime; // warmup till..
  267. int matchStartedTime; // time current match started
  268. // tourney
  269. int currentTourneyPlayer[2];// our current set of players
  270. int lastWinner; // plays again
  271. // warmup
  272. idStr warmupText; // text shown in warmup area of screen
  273. bool one, two, three; // keeps count down voice from repeating
  274. // guis
  275. idUserInterface *scoreBoard; // scoreboard
  276. idUserInterface *spectateGui; // spectate info
  277. idUserInterface *guiChat; // chat text
  278. idUserInterface *mainGui; // ready / nick / votes etc.
  279. idListGUI *mapList;
  280. idUserInterface *msgmodeGui; // message mode
  281. int currentMenu; // 0 - none, 1 - mainGui, 2 - msgmodeGui
  282. int nextMenu; // if 0, will do mainGui
  283. bool bCurrentMenuMsg; // send menu state updates to server
  284. // chat data
  285. mpChatLine_t chatHistory[ NUM_CHAT_NOTIFY ];
  286. int chatHistoryIndex;
  287. int chatHistorySize; // 0 <= x < NUM_CHAT_NOTIFY
  288. bool chatDataUpdated;
  289. int lastChatLineTime;
  290. // rankings are used by UpdateScoreboard and UpdateHud
  291. int numRankedPlayers; // ranked players, others may be empty slots or spectators
  292. idPlayer * rankedPlayers[MAX_CLIENTS];
  293. bool pureReady; // defaults to false, set to true once server game is running with pure checksums
  294. int fragLimitTimeout;
  295. int switchThrottle[ 3 ];
  296. int voiceChatThrottle;
  297. gameType_t lastGameType; // for restarts
  298. int startFragLimit; // synchronize to clients in initial state, set on -> GAMEON
  299. #ifdef CTF
  300. idItemTeam * teamFlags[ 2 ];
  301. int teamPoints[ 2 ];
  302. bool flagMsgOn;
  303. const char * gameTypeVoteMap[ GAME_COUNT ];
  304. #endif
  305. private:
  306. void UpdatePlayerRanks();
  307. // updates the passed gui with current score information
  308. void UpdateRankColor( idUserInterface *gui, const char *mask, int i, const idVec3 &vec );
  309. void UpdateScoreboard( idUserInterface *scoreBoard, idPlayer *player );
  310. #ifdef CTF
  311. void UpdateCTFScoreboard( idUserInterface *scoreBoard, idPlayer *player );
  312. #endif
  313. #ifndef CTF
  314. // We declare this publically above so we can call it during a map restart.
  315. void ClearGuis( void );
  316. #endif
  317. void DrawScoreBoard( idPlayer *player );
  318. void UpdateHud( idPlayer *player, idUserInterface *hud );
  319. bool Warmup( void );
  320. void CheckVote( void );
  321. bool AllPlayersReady( void );
  322. idPlayer * FragLimitHit( void );
  323. idPlayer * FragLeader( void );
  324. bool TimeLimitHit( void );
  325. #ifdef CTF
  326. bool PointLimitHit( void );
  327. // return team with most points
  328. int WinningTeam( void );
  329. #endif
  330. void NewState( gameState_t news, idPlayer *player = NULL );
  331. void UpdateWinsLosses( idPlayer *winner );
  332. // fill any empty tourney slots based on the current tourney ranks
  333. void FillTourneySlots( void );
  334. void CycleTourneyPlayers( void );
  335. // walk through the tourneyRank to build a wait list for the clients
  336. void UpdateTourneyLine( void );
  337. const char * GameTime( void );
  338. void Clear( void );
  339. bool EnoughClientsToPlay( void );
  340. void ClearChatData( void );
  341. void DrawChat( void );
  342. // go through the clients, and see if they want to be respawned, and if the game allows it
  343. // called during normal gameplay for death -> respawn cycles
  344. // and for a spectator who want back in the game (see param)
  345. void CheckRespawns( idPlayer *spectator = NULL );
  346. void ForceReady();
  347. // when clients disconnect or join spectate during game, check if we need to end the game
  348. void CheckAbortGame( void );
  349. void MessageMode( const idCmdArgs &args );
  350. void DisableMenu( void );
  351. void SetMapShot( void );
  352. // scores in TDM
  353. void TeamScore( int entityNumber, int team, int delta );
  354. void VoiceChat( const idCmdArgs &args, bool team );
  355. void DumpTourneyLine( void );
  356. void SuddenRespawn( void );
  357. #ifdef CTF
  358. void FindTeamFlags( void );
  359. #endif
  360. public:
  361. #ifdef CTF
  362. idItemTeam * GetTeamFlag( int team );
  363. flagStatus_t GetFlagStatus( int team );
  364. void TeamScoreCTF( int team, int delta );
  365. void PlayerScoreCTF( int playerIdx, int delta );
  366. // returns entityNum to team flag carrier, -1 if no flag carrier
  367. int GetFlagCarrier( int team );
  368. void UpdateScoreboardFlagStatus( void );
  369. void SetBestGametype( const char * map );
  370. void ReloadScoreboard();
  371. #endif
  372. #ifdef _D3XP
  373. idStr GetBestGametype( const char* map, const char* gametype );
  374. #endif
  375. /* #ifdef CTF ... merge the below IsGametypeFlagBased */
  376. bool IsGametypeFlagBased( void );
  377. bool IsGametypeTeamBased( void );
  378. /* #endif CTF */
  379. };
  380. ID_INLINE idMultiplayerGame::gameState_t idMultiplayerGame::GetGameState( void ) const {
  381. return gameState;
  382. }
  383. ID_INLINE bool idMultiplayerGame::IsPureReady( void ) const {
  384. return pureReady;
  385. }
  386. ID_INLINE void idMultiplayerGame::ClearFrags( int clientNum ) {
  387. playerState[ clientNum ].fragCount = 0;
  388. }
  389. ID_INLINE bool idMultiplayerGame::IsInGame( int clientNum ) {
  390. return playerState[ clientNum ].ingame;
  391. }
  392. #endif /* !__MULTIPLAYERGAME_H__ */