MultiplayerGame.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. typedef enum {
  29. GAME_SP,
  30. GAME_DM,
  31. GAME_TOURNEY,
  32. GAME_TDM,
  33. GAME_LASTMAN
  34. } gameType_t;
  35. typedef enum {
  36. PLAYER_VOTE_NONE,
  37. PLAYER_VOTE_NO,
  38. PLAYER_VOTE_YES,
  39. PLAYER_VOTE_WAIT // mark a player allowed to vote
  40. } playerVote_t;
  41. typedef struct mpPlayerState_s {
  42. int ping; // player ping
  43. int fragCount; // kills
  44. int teamFragCount; // team kills
  45. int wins; // wins
  46. playerVote_t vote; // player's vote
  47. bool scoreBoardUp; // toggle based on player scoreboard button, used to activate de-activate the scoreboard gui
  48. bool ingame;
  49. } mpPlayerState_t;
  50. const int NUM_CHAT_NOTIFY = 5;
  51. const int CHAT_FADE_TIME = 400;
  52. const int FRAGLIMIT_DELAY = 2000;
  53. const int MP_PLAYER_MINFRAGS = -100;
  54. const int MP_PLAYER_MAXFRAGS = 100;
  55. const int MP_PLAYER_MAXWINS = 100;
  56. const int MP_PLAYER_MAXPING = 999;
  57. typedef struct mpChatLine_s {
  58. idStr line;
  59. short fade; // starts high and decreases, line is removed once reached 0
  60. } mpChatLine_t;
  61. typedef enum {
  62. SND_YOUWIN = 0,
  63. SND_YOULOSE,
  64. SND_FIGHT,
  65. SND_VOTE,
  66. SND_VOTE_PASSED,
  67. SND_VOTE_FAILED,
  68. SND_THREE,
  69. SND_TWO,
  70. SND_ONE,
  71. SND_SUDDENDEATH,
  72. SND_COUNT
  73. } snd_evt_t;
  74. class idMultiplayerGame {
  75. public:
  76. idMultiplayerGame();
  77. void Shutdown( void );
  78. // resets everything and prepares for a match
  79. void Reset( void );
  80. // setup local data for a new player
  81. void SpawnPlayer( int clientNum );
  82. // checks rules and updates state of the mp game
  83. void Run( void );
  84. // draws mp hud, scoredboard, etc..
  85. bool Draw( int clientNum );
  86. // updates a player vote
  87. void PlayerVote( int clientNum, playerVote_t vote );
  88. // updates frag counts and potentially ends the match in sudden death
  89. void PlayerDeath( idPlayer *dead, idPlayer *killer, bool telefrag );
  90. void AddChatLine( const char *fmt, ... ) id_attribute((format(printf,2,3)));
  91. void UpdateMainGui( void );
  92. idUserInterface*StartMenu( void );
  93. const char* HandleGuiCommands( const char *menuCommand );
  94. void SetMenuSkin( void );
  95. void WriteToSnapshot( idBitMsgDelta &msg ) const;
  96. void ReadFromSnapshot( const idBitMsgDelta &msg );
  97. // game state
  98. typedef enum {
  99. INACTIVE = 0, // not running
  100. WARMUP, // warming up
  101. COUNTDOWN, // post warmup pre-game
  102. GAMEON, // game is on
  103. SUDDENDEATH, // game is on but in sudden death, first frag wins
  104. GAMEREVIEW, // game is over, scoreboard is up. we wait si_gameReviewPause seconds (which has a min value)
  105. NEXTGAME,
  106. STATE_COUNT
  107. } gameState_t;
  108. static const char *GameStateStrings[ STATE_COUNT ];
  109. idMultiplayerGame::gameState_t GetGameState( void ) const;
  110. static const char *GlobalSoundStrings[ SND_COUNT ];
  111. void PlayGlobalSound( int to, snd_evt_t evt, const char *shader = NULL );
  112. // more compact than a chat line
  113. typedef enum {
  114. MSG_SUICIDE = 0,
  115. MSG_KILLED,
  116. MSG_KILLEDTEAM,
  117. MSG_DIED,
  118. MSG_VOTE,
  119. MSG_VOTEPASSED,
  120. MSG_VOTEFAILED,
  121. MSG_SUDDENDEATH,
  122. MSG_FORCEREADY,
  123. MSG_JOINEDSPEC,
  124. MSG_TIMELIMIT,
  125. MSG_FRAGLIMIT,
  126. MSG_TELEFRAGGED,
  127. MSG_JOINTEAM,
  128. MSG_HOLYSHIT,
  129. MSG_COUNT
  130. } msg_evt_t;
  131. void PrintMessageEvent( int to, msg_evt_t evt, int parm1 = -1, int parm2 = -1 );
  132. void DisconnectClient( int clientNum );
  133. static void ForceReady_f( const idCmdArgs &args );
  134. static void DropWeapon_f( const idCmdArgs &args );
  135. static void MessageMode_f( const idCmdArgs &args );
  136. static void VoiceChat_f( const idCmdArgs &args );
  137. static void VoiceChatTeam_f( const idCmdArgs &args );
  138. typedef enum {
  139. VOTE_RESTART = 0,
  140. VOTE_TIMELIMIT,
  141. VOTE_FRAGLIMIT,
  142. VOTE_GAMETYPE,
  143. VOTE_KICK,
  144. VOTE_MAP,
  145. VOTE_SPECTATORS,
  146. VOTE_NEXTMAP,
  147. VOTE_COUNT,
  148. VOTE_NONE
  149. } vote_flags_t;
  150. typedef enum {
  151. VOTE_UPDATE,
  152. VOTE_FAILED,
  153. VOTE_PASSED, // passed, but no reset yet
  154. VOTE_ABORTED,
  155. VOTE_RESET // tell clients to reset vote state
  156. } vote_result_t;
  157. static void Vote_f( const idCmdArgs &args );
  158. static void CallVote_f( const idCmdArgs &args );
  159. void ClientCallVote( vote_flags_t voteIndex, const char *voteValue );
  160. void ServerCallVote( int clientNum, const idBitMsg &msg );
  161. void ClientStartVote( int clientNum, const char *voteString );
  162. void ServerStartVote( int clientNum, vote_flags_t voteIndex, const char *voteValue );
  163. void ClientUpdateVote( vote_result_t result, int yesCount, int noCount );
  164. void CastVote( int clientNum, bool vote );
  165. void ExecuteVote( void );
  166. void WantKilled( int clientNum );
  167. int NumActualClients( bool countSpectators, int *teamcount = NULL );
  168. void DropWeapon( int clientNum );
  169. void MapRestart( void );
  170. // called by idPlayer whenever it detects a team change (init or switch)
  171. void SwitchToTeam( int clientNum, int oldteam, int newteam );
  172. bool IsPureReady( void ) const;
  173. void ProcessChatMessage( int clientNum, bool team, const char *name, const char *text, const char *sound );
  174. void ProcessVoiceChat( int clientNum, bool team, int index );
  175. void Precache( void );
  176. // throttle UI switch rates
  177. void ThrottleUserInfo( void );
  178. void ToggleSpectate( void );
  179. void ToggleReady( void );
  180. void ToggleTeam( void );
  181. void ClearFrags( int clientNum );
  182. void EnterGame( int clientNum );
  183. bool CanPlay( idPlayer *p );
  184. bool IsInGame( int clientNum );
  185. bool WantRespawn( idPlayer *p );
  186. void ServerWriteInitialReliableMessages( int clientNum );
  187. void ClientReadStartState( const idBitMsg &msg );
  188. void ClientReadWarmupTime( const idBitMsg &msg );
  189. void ServerClientConnect( int clientNum );
  190. void PlayerStats( int clientNum, char *data, const int len );
  191. private:
  192. static const char *MPGuis[];
  193. static const char *ThrottleVars[];
  194. static const char *ThrottleVarsInEnglish[];
  195. static const int ThrottleDelay[];
  196. // state vars
  197. gameState_t gameState; // what state the current game is in
  198. gameState_t nextState; // state to switch to when nextStateSwitch is hit
  199. int pingUpdateTime; // time to update ping
  200. mpPlayerState_t playerState[ MAX_CLIENTS ];
  201. // keep track of clients which are willingly in spectator mode
  202. // vote vars
  203. vote_flags_t vote; // active vote or VOTE_NONE
  204. int voteTimeOut; // when the current vote expires
  205. int voteExecTime; // delay between vote passed msg and execute
  206. float yesVotes; // counter for yes votes
  207. float noVotes; // and for no votes
  208. idStr voteValue; // the data voted upon ( server )
  209. idStr voteString; // the vote string ( client )
  210. bool voted; // hide vote box ( client )
  211. int kickVoteMap[ MAX_CLIENTS ];
  212. // time related
  213. int nextStateSwitch; // time next state switch
  214. int warmupEndTime; // warmup till..
  215. int matchStartedTime; // time current match started
  216. // tourney
  217. int currentTourneyPlayer[2];// our current set of players
  218. int lastWinner; // plays again
  219. // warmup
  220. idStr warmupText; // text shown in warmup area of screen
  221. bool one, two, three; // keeps count down voice from repeating
  222. // guis
  223. idUserInterface *scoreBoard; // scoreboard
  224. idUserInterface *spectateGui; // spectate info
  225. idUserInterface *guiChat; // chat text
  226. idUserInterface *mainGui; // ready / nick / votes etc.
  227. idListGUI *mapList;
  228. idUserInterface *msgmodeGui; // message mode
  229. int currentMenu; // 0 - none, 1 - mainGui, 2 - msgmodeGui
  230. int nextMenu; // if 0, will do mainGui
  231. bool bCurrentMenuMsg; // send menu state updates to server
  232. // chat data
  233. mpChatLine_t chatHistory[ NUM_CHAT_NOTIFY ];
  234. int chatHistoryIndex;
  235. int chatHistorySize; // 0 <= x < NUM_CHAT_NOTIFY
  236. bool chatDataUpdated;
  237. int lastChatLineTime;
  238. // rankings are used by UpdateScoreboard and UpdateHud
  239. int numRankedPlayers; // ranked players, others may be empty slots or spectators
  240. idPlayer * rankedPlayers[MAX_CLIENTS];
  241. bool pureReady; // defaults to false, set to true once server game is running with pure checksums
  242. int fragLimitTimeout;
  243. int switchThrottle[ 3 ];
  244. int voiceChatThrottle;
  245. gameType_t lastGameType; // for restarts
  246. int startFragLimit; // synchronize to clients in initial state, set on -> GAMEON
  247. private:
  248. void UpdatePlayerRanks();
  249. // updates the passed gui with current score information
  250. void UpdateRankColor( idUserInterface *gui, const char *mask, int i, const idVec3 &vec );
  251. void UpdateScoreboard( idUserInterface *scoreBoard, idPlayer *player );
  252. void ClearGuis( void );
  253. void DrawScoreBoard( idPlayer *player );
  254. void UpdateHud( idPlayer *player, idUserInterface *hud );
  255. bool Warmup( void );
  256. void CheckVote( void );
  257. bool AllPlayersReady( void );
  258. idPlayer * FragLimitHit( void );
  259. idPlayer * FragLeader( void );
  260. bool TimeLimitHit( void );
  261. void NewState( gameState_t news, idPlayer *player = NULL );
  262. void UpdateWinsLosses( idPlayer *winner );
  263. // fill any empty tourney slots based on the current tourney ranks
  264. void FillTourneySlots( void );
  265. void CycleTourneyPlayers( void );
  266. // walk through the tourneyRank to build a wait list for the clients
  267. void UpdateTourneyLine( void );
  268. const char * GameTime( void );
  269. void Clear( void );
  270. bool EnoughClientsToPlay( void );
  271. void ClearChatData( void );
  272. void DrawChat( void );
  273. // go through the clients, and see if they want to be respawned, and if the game allows it
  274. // called during normal gameplay for death -> respawn cycles
  275. // and for a spectator who want back in the game (see param)
  276. void CheckRespawns( idPlayer *spectator = NULL );
  277. void ForceReady();
  278. // when clients disconnect or join spectate during game, check if we need to end the game
  279. void CheckAbortGame( void );
  280. void MessageMode( const idCmdArgs &args );
  281. void DisableMenu( void );
  282. void SetMapShot( void );
  283. // scores in TDM
  284. void TeamScore( int entityNumber, int team, int delta );
  285. void VoiceChat( const idCmdArgs &args, bool team );
  286. void DumpTourneyLine( void );
  287. void SuddenRespawn( void );
  288. };
  289. ID_INLINE idMultiplayerGame::gameState_t idMultiplayerGame::GetGameState( void ) const {
  290. return gameState;
  291. }
  292. ID_INLINE bool idMultiplayerGame::IsPureReady( void ) const {
  293. return pureReady;
  294. }
  295. ID_INLINE void idMultiplayerGame::ClearFrags( int clientNum ) {
  296. playerState[ clientNum ].fragCount = 0;
  297. }
  298. ID_INLINE bool idMultiplayerGame::IsInGame( int clientNum ) {
  299. return playerState[ clientNum ].ingame;
  300. }
  301. #endif /* !__MULTIPLAYERGAME_H__ */