doominterface.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include "Precompiled.h"
  21. #include <stdio.h>
  22. #include "globaldata.h"
  23. #include "doominterface.h"
  24. #include "Main.h"
  25. #include "m_menu.h"
  26. #include "g_game.h"
  27. extern void I_SetTime( int );
  28. bool waitingForWipe;
  29. static const int dargc = 7;
  30. static char* dargv[4][7] =
  31. {
  32. { "doomlauncher", "-net", "0", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1" },
  33. { "doomlauncher", "-net", "1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1" },
  34. { "doomlauncher", "-net", "2", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1" },
  35. { "doomlauncher", "-net", "3", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1" },
  36. };
  37. static int mpArgc[4];
  38. static char mpArgV[4][10][32];
  39. static char* mpArgVPtr[4][10];
  40. static bool drawFullScreen = false;
  41. DoomInterface::DoomInterface() {
  42. numplayers = 0;
  43. bFinished[0] = bFinished[1] = bFinished[2] = bFinished[3] = false;
  44. lastTicRun = 0;
  45. }
  46. DoomInterface::~DoomInterface() {
  47. }
  48. void DoomInterface::Startup( int playerscount, bool multiplayer )
  49. {
  50. int i;
  51. int localdargc = 1; // for the commandline
  52. numplayers = playerscount;
  53. globalNetworking = multiplayer;
  54. lastTicRun = 0;
  55. if (DoomLib::Z_Malloc == NULL) {
  56. DoomLib::Z_Malloc = Z_Malloc;
  57. }
  58. // Splitscreen
  59. if ( !multiplayer && playerscount > 1 ) {
  60. localdargc += 2; // for the '-net' and the console number
  61. localdargc += playerscount;
  62. }
  63. if ( multiplayer ) {
  64. // Force online games to 1 local player for now.
  65. // TODO: We should support local splitscreen and online.
  66. numplayers = 1;
  67. }
  68. // Start up DooM Classic
  69. for ( i = 0; i < numplayers; ++i)
  70. {
  71. DoomLib::SetPlayer(i);
  72. bFinished[i] = false;
  73. DoomLib::InitGlobals( NULL );
  74. if ( globalNetworking ) {
  75. printf( "Starting mulitplayer game, argv = " );
  76. for ( int j = 0; j < mpArgc[0]; ++j ) {
  77. printf( " %s", mpArgVPtr[0][j] );
  78. }
  79. printf( "\n" );
  80. DoomLib::InitGame(mpArgc[i], mpArgVPtr[i] );
  81. } else {
  82. DoomLib::InitGame(localdargc, dargv[i] );
  83. }
  84. if( DoomLib::skipToLoad ) {
  85. G_LoadGame( DoomLib::loadGamePath );
  86. DoomLib::skipToLoad = false;
  87. ::g->menuactive = 0;
  88. }
  89. if( DoomLib::skipToNew ) {
  90. static int startLevel = 1;
  91. G_DeferedInitNew((skill_t)DoomLib::chosenSkill,DoomLib::chosenEpisode+1, startLevel);
  92. DoomLib::skipToNew = false;
  93. ::g->menuactive = 0;
  94. }
  95. DoomLib::SetPlayer(-1);
  96. }
  97. }
  98. bool DoomInterface::Frame( int iTime, idUserCmdMgr * userCmdMgr )
  99. {
  100. int i;
  101. bool bAllFinished = true;
  102. if ( !globalNetworking || ( lastTicRun < iTime ) ) {
  103. drawFullScreen = false;
  104. DoomLib::SetPlayer( 0 );
  105. DoomLib::PollNetwork();
  106. for (i = 0; i < numplayers; ++i)
  107. {
  108. DoomLib::SetPlayer( i );
  109. I_SetTime( iTime );
  110. if (bFinished[i] == false) {
  111. bAllFinished = false;
  112. bFinished[i] = DoomLib::Poll();
  113. } else {
  114. if (::g->wipedone) {
  115. if ( !waitingForWipe ) {
  116. const bool didRunTic = DoomLib::Tic( userCmdMgr );
  117. if ( didRunTic == false ) {
  118. //printf( "Skipping tic and yielding because not enough time has passed.\n" );
  119. // Give lower priority threads a chance to run.
  120. Sys_Yield();
  121. }
  122. }
  123. DoomLib::Frame();
  124. }
  125. if (::g->wipe) {
  126. DoomLib::Wipe();
  127. // Draw the menus over the wipe.
  128. M_Drawer();
  129. }
  130. if( ::g->gamestate != GS_LEVEL && GetNumPlayers() > 2 ) {
  131. drawFullScreen = true;
  132. }
  133. }
  134. DoomLib::SetPlayer(-1);
  135. }
  136. DoomLib::SetPlayer( 0 );
  137. DoomLib::SendNetwork();
  138. DoomLib::RunSound();
  139. DoomLib::SetPlayer( -1 );
  140. lastTicRun = iTime;
  141. } else {
  142. printf( "Skipping this frame becase it's not time to run a tic yet.\n" );
  143. }
  144. return bAllFinished;
  145. }
  146. void I_ShutdownNetwork();
  147. void DoomInterface::Shutdown() {
  148. int i;
  149. for ( i=0; i < numplayers; i++ ) {
  150. DoomLib::SetPlayer( i );
  151. D_QuitNetGame();
  152. }
  153. // Shutdown local network state
  154. I_ShutdownNetwork();
  155. for ( i=0; i < numplayers; i++ ) {
  156. DoomLib::SetPlayer( i );
  157. DoomLib::Shutdown();
  158. }
  159. DoomLib::SetPlayer( -1 );
  160. numplayers = 0;
  161. lastTicRun = 0;
  162. }
  163. qboolean G_CheckDemoStatus( void );
  164. void DoomInterface::QuitCurrentGame() {
  165. for ( int i = 0; i < numplayers; i++ ) {
  166. DoomLib::SetPlayer( i );
  167. if(::g->netgame) {
  168. // Shut down networking
  169. D_QuitNetGame();
  170. }
  171. G_CheckDemoStatus();
  172. globalPauseTime = false;
  173. ::g->menuactive = false;
  174. ::g->usergame = false;
  175. ::g->netgame = false;
  176. lastTicRun = 0;
  177. //if ( !gameLocal->IsSplitscreen() ) {
  178. // Start background demos
  179. D_StartTitle();
  180. //}
  181. }
  182. // Shutdown local network state
  183. I_ShutdownNetwork();
  184. }
  185. void DoomInterface::EndDMGame() {
  186. for ( int i = 0; i < numplayers; i++ ) {
  187. DoomLib::SetPlayer( i );
  188. if(::g->netgame) {
  189. D_QuitNetGame();
  190. }
  191. G_CheckDemoStatus();
  192. globalPauseTime = false;
  193. ::g->menuactive = false;
  194. ::g->usergame = false;
  195. ::g->netgame = false;
  196. lastTicRun = 0;
  197. D_StartTitle();
  198. }
  199. }
  200. //static
  201. int DoomInterface::CurrentPlayer() {
  202. return DoomLib::GetPlayer();
  203. }
  204. int DoomInterface::GetNumPlayers() const {
  205. return numplayers;
  206. }
  207. #ifdef ID_ENABLE_DOOM_CLASSIC_NETWORKING
  208. void DoomInterface::SetNetworking( DoomLib::RecvFunc recv, DoomLib::SendFunc send, DoomLib::SendRemoteFunc sendRemote ) {
  209. DoomLib::SetNetworking( recv, send, sendRemote );
  210. }
  211. #endif
  212. void DoomInterface::SetMultiplayerPlayers(int localPlayerIndex, int playerCount, int localPlayer, std::vector<std::string> playerAddresses) {
  213. for(int i = 0; i < 10; i++) {
  214. mpArgVPtr[localPlayerIndex][i] = mpArgV[localPlayerIndex][i];
  215. }
  216. mpArgc[localPlayerIndex] = playerCount+5;
  217. strcpy(mpArgV[localPlayerIndex][0], "doomlauncher");
  218. strcpy(mpArgV[localPlayerIndex][1], "-dup");
  219. strcpy(mpArgV[localPlayerIndex][2], "1");
  220. strcpy(mpArgV[localPlayerIndex][3], "-net");
  221. sprintf(mpArgV[localPlayerIndex][4], "%d", localPlayer);
  222. strcpy(mpArgV[localPlayerIndex][5], playerAddresses[localPlayer].c_str());
  223. int currentArg = 6;
  224. for(int i = 0; i < playerCount; i++) {
  225. if(i != localPlayer) {
  226. strcpy(mpArgV[localPlayerIndex][currentArg], playerAddresses[i].c_str());
  227. currentArg++;
  228. }
  229. }
  230. }