Common_local.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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. static const int MAX_USERCMD_BACKUP = 256;
  21. static const int NUM_USERCMD_RELAY = 10;
  22. static const int NUM_USERCMD_SEND = 8;
  23. static const int initialHz = 60;
  24. static const int initialBaseTicks = 1000 / initialHz;
  25. static const int initialBaseTicksPerSec = initialHz * initialBaseTicks;
  26. static const int LOAD_TIP_CHANGE_INTERVAL = 12000;
  27. static const int LOAD_TIP_COUNT = 26;
  28. class idGameThread : public idSysThread {
  29. public:
  30. idGameThread() :
  31. gameTime(),
  32. drawTime(),
  33. threadTime(),
  34. threadGameTime(),
  35. threadRenderTime(),
  36. userCmdMgr( NULL ),
  37. ret(),
  38. numGameFrames(),
  39. isClient()
  40. {}
  41. // the gameReturn_t is from the previous frame, the
  42. // new frame will be running in parallel on exit
  43. gameReturn_t RunGameAndDraw( int numGameFrames, idUserCmdMgr & userCmdMgr_, bool isClient_, int startGameFrame );
  44. // Accessors to the stored frame/thread time information
  45. void SetThreadTotalTime( const int inTime ) { threadTime = inTime; }
  46. int GetThreadTotalTime() const { return threadTime; }
  47. void SetThreadGameTime( const int time ) { threadGameTime = time; }
  48. int GetThreadGameTime() const { return threadGameTime; }
  49. void SetThreadRenderTime( const int time ) { threadRenderTime = time; }
  50. int GetThreadRenderTime() const { return threadRenderTime; }
  51. private:
  52. virtual int Run();
  53. int gameTime;
  54. int drawTime;
  55. int threadTime; // total time : game time + foreground render time
  56. int threadGameTime; // game time only
  57. int threadRenderTime; // render fg time only
  58. idUserCmdMgr * userCmdMgr;
  59. gameReturn_t ret;
  60. int numGameFrames;
  61. bool isClient;
  62. };
  63. enum errorParm_t {
  64. ERP_NONE,
  65. ERP_FATAL, // exit the entire game with a popup window
  66. ERP_DROP, // print to console and disconnect from game
  67. ERP_DISCONNECT // don't kill server
  68. };
  69. enum gameLaunch_t {
  70. LAUNCH_TITLE_DOOM = 0,
  71. LAUNCH_TITLE_DOOM2,
  72. };
  73. struct netTimes_t {
  74. int localTime;
  75. int serverTime;
  76. };
  77. struct frameTiming_t {
  78. uint64 startSyncTime;
  79. uint64 finishSyncTime;
  80. uint64 startGameTime;
  81. uint64 finishGameTime;
  82. uint64 finishDrawTime;
  83. uint64 startRenderTime;
  84. uint64 finishRenderTime;
  85. };
  86. #define MAX_PRINT_MSG_SIZE 4096
  87. #define MAX_WARNING_LIST 256
  88. #define SAVEGAME_CHECKPOINT_FILENAME "gamedata.save"
  89. #define SAVEGAME_DESCRIPTION_FILENAME "gamedata.txt"
  90. #define SAVEGAME_STRINGS_FILENAME "gamedata.strings"
  91. class idCommonLocal : public idCommon {
  92. public:
  93. idCommonLocal();
  94. virtual void Init( int argc, const char * const * argv, const char *cmdline );
  95. virtual void Shutdown();
  96. virtual void CreateMainMenu();
  97. virtual void Quit();
  98. virtual bool IsInitialized() const;
  99. virtual void Frame();
  100. virtual void UpdateScreen( bool captureToImage );
  101. virtual void UpdateLevelLoadPacifier();
  102. virtual void StartupVariable( const char * match );
  103. virtual void WriteConfigToFile( const char *filename );
  104. virtual void BeginRedirect( char *buffer, int buffersize, void (*flush)( const char * ) );
  105. virtual void EndRedirect();
  106. virtual void SetRefreshOnPrint( bool set );
  107. virtual void Printf( VERIFY_FORMAT_STRING const char *fmt, ... );
  108. virtual void VPrintf( const char *fmt, va_list arg );
  109. virtual void DPrintf( VERIFY_FORMAT_STRING const char *fmt, ... );
  110. virtual void Warning( VERIFY_FORMAT_STRING const char *fmt, ... );
  111. virtual void DWarning( VERIFY_FORMAT_STRING const char *fmt, ...);
  112. virtual void PrintWarnings();
  113. virtual void ClearWarnings( const char *reason );
  114. virtual void Error( VERIFY_FORMAT_STRING const char *fmt, ... );
  115. virtual void FatalError( VERIFY_FORMAT_STRING const char *fmt, ... );
  116. virtual bool IsShuttingDown() const { return com_shuttingDown; }
  117. virtual const char * KeysFromBinding( const char *bind );
  118. virtual const char * BindingFromKey( const char *key );
  119. virtual bool IsMultiplayer();
  120. virtual bool IsServer();
  121. virtual bool IsClient();
  122. virtual bool GetConsoleUsed() { return consoleUsed; }
  123. virtual int GetSnapRate();
  124. virtual void NetReceiveReliable( int peer, int type, idBitMsg & msg );
  125. virtual void NetReceiveSnapshot( class idSnapShot & ss );
  126. virtual void NetReceiveUsercmds( int peer, idBitMsg & msg );
  127. void NetReadUsercmds( int clientNum, idBitMsg & msg );
  128. virtual bool ProcessEvent( const sysEvent_t *event );
  129. virtual bool LoadGame( const char * saveName );
  130. virtual bool SaveGame( const char * saveName );
  131. virtual int ButtonState( int key );
  132. virtual int KeyState( int key );
  133. virtual idDemoFile * ReadDemo() { return readDemo; }
  134. virtual idDemoFile * WriteDemo() { return writeDemo; }
  135. virtual idGame * Game() { return game; }
  136. virtual idRenderWorld * RW() { return renderWorld; }
  137. virtual idSoundWorld * SW() { return soundWorld; }
  138. virtual idSoundWorld * MenuSW() { return menuSoundWorld; }
  139. virtual idSession * Session() { return session; }
  140. virtual idCommonDialog & Dialog() { return commonDialog; }
  141. virtual void OnSaveCompleted( idSaveLoadParms & parms );
  142. virtual void OnLoadCompleted( idSaveLoadParms & parms );
  143. virtual void OnLoadFilesCompleted( idSaveLoadParms & parms );
  144. virtual void OnEnumerationCompleted( idSaveLoadParms & parms );
  145. virtual void OnDeleteCompleted( idSaveLoadParms & parms );
  146. virtual void TriggerScreenWipe( const char * _wipeMaterial, bool hold );
  147. virtual void OnStartHosting( idMatchParameters & parms );
  148. virtual int GetGameFrame() { return gameFrame; }
  149. virtual void LaunchExternalTitle( int titleIndex,
  150. int device,
  151. const lobbyConnectInfo_t * const connectInfo ); // For handling invitations. NULL if no invitation used.
  152. virtual void InitializeMPMapsModes();
  153. virtual const idStrList & GetModeList() const { return mpGameModes; }
  154. virtual const idStrList & GetModeDisplayList() const { return mpDisplayGameModes; }
  155. virtual const idList<mpMap_t> & GetMapList() const { return mpGameMaps; }
  156. virtual void ResetPlayerInput( int playerIndex );
  157. virtual bool JapaneseCensorship() const;
  158. virtual void QueueShowShell() { showShellRequested = true; }
  159. virtual currentGame_t GetCurrentGame() const { return currentGame; }
  160. virtual void SwitchToGame( currentGame_t newGame );
  161. public:
  162. void Draw(); // called by gameThread
  163. int GetGameThreadTotalTime() const { return gameThread.GetThreadTotalTime(); }
  164. int GetGameThreadGameTime() const { return gameThread.GetThreadGameTime(); }
  165. int GetGameThreadRenderTime() const { return gameThread.GetThreadRenderTime(); }
  166. int GetRendererBackEndMicroseconds() const { return time_backend; }
  167. int GetRendererShadowsMicroseconds() const { return time_shadows; }
  168. int GetRendererIdleMicroseconds() const { return mainFrameTiming.startRenderTime - mainFrameTiming.finishSyncTime; }
  169. int GetRendererGPUMicroseconds() const { return time_gpu; }
  170. frameTiming_t frameTiming;
  171. frameTiming_t mainFrameTiming;
  172. public: // These are public because they are called directly by static functions in this file
  173. const char * GetCurrentMapName() { return currentMapName.c_str(); }
  174. // loads a map and starts a new game on it
  175. void StartNewGame( const char * mapName, bool devmap, int gameMode );
  176. void LeaveGame();
  177. void DemoShot( const char *name );
  178. void StartRecordingRenderDemo( const char *name );
  179. void StopRecordingRenderDemo();
  180. void StartPlayingRenderDemo( idStr name );
  181. void StopPlayingRenderDemo();
  182. void CompressDemoFile( const char *scheme, const char *name );
  183. void TimeRenderDemo( const char *name, bool twice = false, bool quit = false );
  184. void AVIRenderDemo( const char *name );
  185. void AVIGame( const char *name );
  186. // localization
  187. void InitLanguageDict();
  188. void LocalizeGui( const char *fileName, idLangDict &langDict );
  189. void LocalizeMapData( const char *fileName, idLangDict &langDict );
  190. void LocalizeSpecificMapData( const char *fileName, idLangDict &langDict, const idLangDict &replaceArgs );
  191. idUserCmdMgr & GetUCmdMgr() { return userCmdMgr; }
  192. private:
  193. bool com_fullyInitialized;
  194. bool com_refreshOnPrint; // update the screen every print for dmap
  195. errorParm_t com_errorEntered;
  196. bool com_shuttingDown;
  197. bool com_isJapaneseSKU;
  198. idFile * logFile;
  199. char errorMessage[MAX_PRINT_MSG_SIZE];
  200. char * rd_buffer;
  201. int rd_buffersize;
  202. void (*rd_flush)( const char *buffer );
  203. idStr warningCaption;
  204. idStrList warningList;
  205. idStrList errorList;
  206. int gameDLL;
  207. idCommonDialog commonDialog;
  208. idFile_SaveGame saveFile;
  209. idFile_SaveGame stringsFile;
  210. idFile_SaveGamePipelined *pipelineFile;
  211. // The main render world and sound world
  212. idRenderWorld * renderWorld;
  213. idSoundWorld * soundWorld;
  214. // The renderer and sound system will write changes to writeDemo.
  215. // Demos can be recorded and played at the same time when splicing.
  216. idDemoFile * readDemo;
  217. idDemoFile * writeDemo;
  218. bool menuActive;
  219. idSoundWorld * menuSoundWorld; // so the game soundWorld can be muted
  220. bool insideExecuteMapChange; // Enable Pacifier Updates
  221. // This is set if the player enables the console, which disables achievements
  222. bool consoleUsed;
  223. // This additional information is required for ExecuteMapChange for SP games ONLY
  224. // This data is cleared after ExecuteMapChange
  225. struct mapSpawnData_t {
  226. idFile_SaveGame * savegameFile; // Used for loading a save game
  227. idFile_SaveGame * stringTableFile; // String table read from save game loaded
  228. idFile_SaveGamePipelined *pipelineFile;
  229. int savegameVersion; // Version of the save game we're loading
  230. idDict persistentPlayerInfo; // Used for transitioning from map to map
  231. };
  232. mapSpawnData_t mapSpawnData;
  233. idStr currentMapName; // for checking reload on same level
  234. bool mapSpawned; // cleared on Stop()
  235. bool insideUpdateScreen; // true while inside ::UpdateScreen()
  236. idUserCmdMgr userCmdMgr;
  237. int nextUsercmdSendTime; // Next time to send usercmds
  238. int nextSnapshotSendTime; // Next time to send a snapshot
  239. idSnapShot lastSnapShot; // last snapshot we received from the server
  240. struct reliableMsg_t {
  241. int client;
  242. int type;
  243. int dataSize;
  244. byte * data;
  245. };
  246. idList<reliableMsg_t> reliableQueue;
  247. // Snapshot interpolation
  248. idSnapShot oldss; // last local snapshot
  249. // (ie on server this is the last "master" snapshot we created)
  250. // (on clients this is the last received snapshot)
  251. // used for comparisons with the new snapshot for com_drawSnapshot
  252. // This is ultimately controlled by net_maxBufferedSnapshots by running double speed, but this is the hard max before seeing visual popping
  253. static const int RECEIVE_SNAPSHOT_BUFFER_SIZE = 16;
  254. int readSnapshotIndex;
  255. int writeSnapshotIndex;
  256. idArray<idSnapShot,RECEIVE_SNAPSHOT_BUFFER_SIZE> receivedSnaps;
  257. float optimalPCTBuffer;
  258. float optimalTimeBuffered;
  259. float optimalTimeBufferedWindow;
  260. uint64 snapRate;
  261. uint64 actualRate;
  262. uint64 snapTime; // time we got the most recent snapshot
  263. uint64 snapTimeDelta; // time interval that current ss was sent in
  264. uint64 snapTimeWrite;
  265. uint64 snapCurrentTime; // realtime playback time
  266. netTimes_t snapCurrent; // current snapshot
  267. netTimes_t snapPrevious; // previous snapshot
  268. float snapCurrentResidual;
  269. float snapTimeBuffered;
  270. float effectiveSnapRate;
  271. int totalBufferedTime;
  272. int totalRecvTime;
  273. int clientPrediction;
  274. int gameFrame; // Frame number of the local game
  275. double gameTimeResidual; // left over msec from the last game frame
  276. bool syncNextGameFrame;
  277. bool aviCaptureMode; // if true, screenshots will be taken and sound captured
  278. idStr aviDemoShortName; //
  279. int aviDemoFrameCount;
  280. enum timeDemo_t {
  281. TD_NO,
  282. TD_YES,
  283. TD_YES_THEN_QUIT
  284. };
  285. timeDemo_t timeDemo;
  286. int timeDemoStartTime;
  287. int numDemoFrames; // for timeDemo and demoShot
  288. int demoTimeOffset;
  289. renderView_t currentDemoRenderView;
  290. idStrList mpGameModes;
  291. idStrList mpDisplayGameModes;
  292. idList<mpMap_t> mpGameMaps;
  293. idSWF * loadGUI;
  294. int nextLoadTip;
  295. bool isHellMap;
  296. bool defaultLoadscreen;
  297. idStaticList<int, LOAD_TIP_COUNT> loadTipList;
  298. const idMaterial * splashScreen;
  299. const idMaterial * whiteMaterial;
  300. const idMaterial * wipeMaterial;
  301. int wipeStartTime;
  302. int wipeStopTime;
  303. bool wipeHold;
  304. bool wipeForced; // used for the PS3 to start an early wipe while we are accessing saved game data
  305. idGameThread gameThread; // the game and draw code can be run in parallel
  306. // com_speeds times
  307. int count_numGameFrames; // total number of game frames that were run
  308. int time_gameFrame; // game logic time
  309. int time_maxGameFrame; // maximum single frame game logic time
  310. int time_gameDraw; // game present time
  311. uint64 time_frontend; // renderer frontend time
  312. uint64 time_backend; // renderer backend time
  313. uint64 time_shadows; // renderer backend waiting for shadow volumes to be created
  314. uint64 time_gpu; // total gpu time, at least for PC
  315. // Used during loading screens
  316. int lastPacifierSessionTime;
  317. int lastPacifierGuiTime;
  318. bool lastPacifierDialogState;
  319. bool showShellRequested;
  320. currentGame_t currentGame;
  321. currentGame_t idealCurrentGame; // Defer game switching so that bad things don't happen in the middle of the frame.
  322. const idMaterial * doomClassicMaterial;
  323. static const int DOOMCLASSIC_RENDERWIDTH = 320 * 3;
  324. static const int DOOMCLASSIC_RENDERHEIGHT = 200 * 3;
  325. static const int DOOMCLASSIC_BYTES_PER_PIXEL = 4;
  326. static const int DOOMCLASSIC_IMAGE_SIZE_IN_BYTES = DOOMCLASSIC_RENDERWIDTH * DOOMCLASSIC_RENDERHEIGHT * DOOMCLASSIC_BYTES_PER_PIXEL;
  327. idArray< byte, DOOMCLASSIC_IMAGE_SIZE_IN_BYTES > doomClassicImageData;
  328. private:
  329. void InitCommands();
  330. void InitSIMD();
  331. void AddStartupCommands();
  332. void ParseCommandLine( int argc, const char * const * argv );
  333. bool SafeMode();
  334. void CloseLogFile();
  335. void WriteConfiguration();
  336. void DumpWarnings();
  337. void LoadGameDLL();
  338. void UnloadGameDLL();
  339. void CleanupShell();
  340. void RenderBink( const char * path );
  341. void RenderSplash();
  342. void FilterLangList( idStrList* list, idStr lang );
  343. void CheckStartupStorageRequirements();
  344. void ExitMenu();
  345. bool MenuEvent( const sysEvent_t * event );
  346. void StartMenu( bool playIntro = false );
  347. void GuiFrameEvents();
  348. void BeginAVICapture( const char *name );
  349. void EndAVICapture();
  350. void AdvanceRenderDemo( bool singleFrameOnly );
  351. void ProcessGameReturn( const gameReturn_t & ret );
  352. void RunNetworkSnapshotFrame();
  353. void ExecuteReliableMessages();
  354. // Snapshot interpolation
  355. void ProcessSnapshot( idSnapShot & ss );
  356. int CalcSnapTimeBuffered( int & totalBufferedTime, int & totalRecvTime );
  357. void ProcessNextSnapshot();
  358. void InterpolateSnapshot( netTimes_t & prev, netTimes_t & next, float fraction, bool predict );
  359. void ResetNetworkingState();
  360. int NetworkFrame();
  361. void SendSnapshots();
  362. void SendUsercmds( int localClientNum );
  363. void LoadLoadingGui(const char *mapName, bool & hellMap );
  364. // Meant to be used like:
  365. // while ( waiting ) { BusyWait(); }
  366. void BusyWait();
  367. bool WaitForSessionState( idSession::sessionState_t desiredState );
  368. void ExecuteMapChange();
  369. void UnloadMap();
  370. void Stop( bool resetSession = true );
  371. // called by Draw when the scene to scene wipe is still running
  372. void DrawWipeModel();
  373. void StartWipe( const char *materialName, bool hold = false);
  374. void CompleteWipe();
  375. void ClearWipe();
  376. void MoveToNewMap( const char * mapName, bool devmap );
  377. void PlayIntroGui();
  378. void ScrubSaveGameFileName( idStr &saveFileName ) const;
  379. // Doom classic support
  380. void RunDoomClassicFrame();
  381. void RenderDoomClassic();
  382. bool IsPlayingDoomClassic() const { return GetCurrentGame() != DOOM3_BFG; }
  383. void PerformGameSwitch();
  384. };
  385. extern idCommonLocal commonLocal;