game.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platform/platformInput.h"
  24. #include "app/game.h"
  25. #include "math/mMath.h"
  26. #include "core/dnet.h"
  27. #include "core/stream/fileStream.h"
  28. #include "core/frameAllocator.h"
  29. #include "core/iTickable.h"
  30. #include "core/strings/findMatch.h"
  31. #include "console/simBase.h"
  32. #include "console/console.h"
  33. #include "console/consoleTypes.h"
  34. #include "gui/controls/guiMLTextCtrl.h"
  35. #ifdef TORQUE_TGB_ONLY
  36. #include "T2D/oldModel/networking/t2dGameConnection.h"
  37. #include "T2D/oldModel/networking/t2dNetworkServerSceneProcess.h"
  38. #include "T2D/oldModel/networking/t2dNetworkClientSceneProcess.h"
  39. #else
  40. #include "T3D/gameBase/gameConnection.h"
  41. #include "T3D/gameFunctions.h"
  42. #include "T3D/gameBase/gameProcess.h"
  43. #endif
  44. #include "platform/profiler.h"
  45. #include "gfx/gfxCubemap.h"
  46. #include "gfx/gfxTextureManager.h"
  47. #include "sfx/sfxSystem.h"
  48. #ifdef TORQUE_PLAYER
  49. // See matching #ifdef in editor/editor.cpp
  50. bool gEditingMission = false;
  51. #endif
  52. //--------------------------------------------------------------------------
  53. ConsoleFunctionGroupBegin( InputManagement, "Functions that let you deal with input from scripts" );
  54. ConsoleFunction( deactivateDirectInput, void, 1, 1, "()"
  55. "@brief Disables DirectInput.\n\n"
  56. "Also deactivates any connected joysticks.\n\n"
  57. "@ingroup Input" )
  58. {
  59. TORQUE_UNUSED(argc); TORQUE_UNUSED(argv);
  60. if ( Input::isActive() )
  61. Input::deactivate();
  62. }
  63. ConsoleFunction( activateDirectInput, void, 1, 1,"()"
  64. "@brief Activates DirectInput.\n\n"
  65. "Also activates any connected joysticks."
  66. "@ingroup Input")
  67. {
  68. TORQUE_UNUSED(argc); TORQUE_UNUSED(argv);
  69. if ( !Input::isActive() )
  70. Input::activate();
  71. }
  72. ConsoleFunctionGroupEnd( InputManagement );
  73. //--------------------------------------------------------------------------
  74. static const U32 MaxPlayerNameLength = 16;
  75. ConsoleFunction( strToPlayerName, const char*, 2, 2, "strToPlayerName( string )" )
  76. {
  77. TORQUE_UNUSED(argc);
  78. const char* ptr = argv[1];
  79. // Strip leading spaces and underscores:
  80. while ( *ptr == ' ' || *ptr == '_' )
  81. ptr++;
  82. U32 len = dStrlen( ptr );
  83. if ( len )
  84. {
  85. char* ret = Con::getReturnBuffer( MaxPlayerNameLength + 1 );
  86. char* rptr = ret;
  87. ret[MaxPlayerNameLength - 1] = '\0';
  88. ret[MaxPlayerNameLength] = '\0';
  89. bool space = false;
  90. U8 ch;
  91. while ( *ptr && dStrlen( ret ) < MaxPlayerNameLength )
  92. {
  93. ch = (U8) *ptr;
  94. // Strip all illegal characters:
  95. if ( ch < 32 || ch == ',' || ch == '.' || ch == '\'' || ch == '`' )
  96. {
  97. ptr++;
  98. continue;
  99. }
  100. // Don't allow double spaces or space-underline combinations:
  101. if ( ch == ' ' || ch == '_' )
  102. {
  103. if ( space )
  104. {
  105. ptr++;
  106. continue;
  107. }
  108. else
  109. space = true;
  110. }
  111. else
  112. space = false;
  113. *rptr++ = *ptr;
  114. ptr++;
  115. }
  116. *rptr = '\0';
  117. //finally, strip out the ML text control chars...
  118. return GuiMLTextCtrl::stripControlChars(ret);
  119. }
  120. return( "" );
  121. }
  122. ConsoleFunctionGroupBegin( Platform , "General platform functions.");
  123. ConsoleFunction( lockMouse, void, 2, 2, "(bool isLocked)"
  124. "@brief Lock or unlock the mouse to the window.\n\n"
  125. "When true, prevents the mouse from leaving the bounds of the game window.\n\n"
  126. "@ingroup Input")
  127. {
  128. Platform::setWindowLocked(dAtob(argv[1]));
  129. }
  130. ConsoleFunction( setNetPort, bool, 2, 3, "(int port, bool bind=true)"
  131. "@brief Set the network port for the game to use.\n\n"
  132. "@param port The port to use.\n"
  133. "@param bind True if bind() should be called on the port.\n"
  134. "@returns True if the port was successfully opened.\n"
  135. "This will trigger a windows firewall prompt. "
  136. "If you don't have firewall tunneling tech you can set this to false to avoid the prompt.\n\n"
  137. "@ingroup Networking")
  138. {
  139. bool bind = true;
  140. if (argc == 3)
  141. bind = dAtob(argv[2]);
  142. return Net::openPort(dAtoi(argv[1]), bind);
  143. }
  144. ConsoleFunction( closeNetPort, void, 1, 1, "()"
  145. "@brief Closes the current network port\n\n"
  146. "@ingroup Networking")
  147. {
  148. Net::closePort();
  149. }
  150. ConsoleFunction( saveJournal, void, 2, 2, "(string filename)"
  151. "Save the journal to the specified file.\n\n"
  152. "@ingroup Platform")
  153. {
  154. Journal::Record(argv[1]);
  155. }
  156. ConsoleFunction( playJournal, void, 2, 3, "(string filename)"
  157. "@brief Begin playback of a journal from a specified field.\n\n"
  158. "@param filename Name and path of file journal file\n"
  159. "@ingroup Platform")
  160. {
  161. // CodeReview - BJG 4/24/2007 - The break flag needs to be wired back in.
  162. // bool jBreak = (argc > 2)? dAtob(argv[2]): false;
  163. Journal::Play(argv[1]);
  164. }
  165. ConsoleFunction( getSimTime, S32, 1, 1, "()"
  166. "Return the current sim time in milliseconds.\n\n"
  167. "@brief Sim time is time since the game started.\n\n"
  168. "@ingroup Platform")
  169. {
  170. return Sim::getCurrentTime();
  171. }
  172. ConsoleFunction( getRealTime, S32, 1, 1, "()"
  173. "@brief Return the current real time in milliseconds.\n\n"
  174. "Real time is platform defined; typically time since the computer booted.\n\n"
  175. "@ingroup Platform")
  176. {
  177. return Platform::getRealMilliseconds();
  178. }
  179. ConsoleFunctionGroupEnd(Platform);
  180. //-----------------------------------------------------------------------------
  181. bool clientProcess(U32 timeDelta)
  182. {
  183. bool ret = true;
  184. #ifndef TORQUE_TGB_ONLY
  185. ret = ClientProcessList::get()->advanceTime(timeDelta);
  186. #else
  187. ret = gt2dNetworkClientProcess.advanceTime( timeDelta );
  188. #endif
  189. ITickable::advanceTime(timeDelta);
  190. #ifndef TORQUE_TGB_ONLY
  191. // Determine if we're lagging
  192. GameConnection* connection = GameConnection::getConnectionToServer();
  193. if(connection)
  194. {
  195. connection->detectLag();
  196. }
  197. #else
  198. // Determine if we're lagging
  199. t2dGameConnection* connection = t2dGameConnection::getConnectionToServer();
  200. if(connection)
  201. {
  202. connection->detectLag();
  203. }
  204. #endif
  205. // Let SFX process.
  206. SFX->_update();
  207. return ret;
  208. }
  209. bool serverProcess(U32 timeDelta)
  210. {
  211. bool ret = true;
  212. #ifndef TORQUE_TGB_ONLY
  213. ret = ServerProcessList::get()->advanceTime(timeDelta);
  214. #else
  215. ret = gt2dNetworkServerProcess.advanceTime( timeDelta );
  216. #endif
  217. return ret;
  218. }