game.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "platform/platformInput.h"
  28. #include "app/game.h"
  29. #include "math/mMath.h"
  30. #include "core/dnet.h"
  31. #include "core/stream/fileStream.h"
  32. #include "core/frameAllocator.h"
  33. #include "core/iTickable.h"
  34. #include "core/strings/findMatch.h"
  35. #include "console/simBase.h"
  36. #include "console/console.h"
  37. #include "console/consoleTypes.h"
  38. #include "console/engineAPI.h"
  39. #include "gui/controls/guiMLTextCtrl.h"
  40. #ifdef TORQUE_TGB_ONLY
  41. #include "T2D/oldModel/networking/t2dGameConnection.h"
  42. #include "T2D/oldModel/networking/t2dNetworkServerSceneProcess.h"
  43. #include "T2D/oldModel/networking/t2dNetworkClientSceneProcess.h"
  44. #else
  45. #include "T3D/gameBase/gameConnection.h"
  46. #include "T3D/gameFunctions.h"
  47. #include "T3D/gameBase/gameProcess.h"
  48. #endif
  49. #include "platform/profiler.h"
  50. #include "gfx/gfxCubemap.h"
  51. #include "gfx/gfxTextureManager.h"
  52. #include "sfx/sfxSystem.h"
  53. // Including this header provides access to certain system-level AFX methods.
  54. #include "afx/arcaneFX.h"
  55. #ifdef TORQUE_PLAYER
  56. // See matching #ifdef in editor/editor.cpp
  57. bool gEditingMission = false;
  58. #endif
  59. //--------------------------------------------------------------------------
  60. ConsoleFunctionGroupBegin( InputManagement, "Functions that let you deal with input from scripts" );
  61. DefineConsoleFunction( deactivateDirectInput, void, (), ,
  62. "()"
  63. "@brief Disables DirectInput.\n\n"
  64. "Also deactivates any connected joysticks.\n\n"
  65. "@ingroup Input" )
  66. {
  67. if ( Input::isActive() )
  68. Input::deactivate();
  69. }
  70. DefineConsoleFunction( activateDirectInput, void, (), ,
  71. "()"
  72. "@brief Activates DirectInput.\n\n"
  73. "Also activates any connected joysticks."
  74. "@ingroup Input")
  75. {
  76. if ( !Input::isActive() )
  77. Input::activate();
  78. }
  79. ConsoleFunctionGroupEnd( InputManagement );
  80. //--------------------------------------------------------------------------
  81. static const U32 MaxPlayerNameLength = 16;
  82. DefineConsoleFunction( strToPlayerName, const char*, (const char* ptr ), , "strToPlayerName(string);" )
  83. {
  84. // Strip leading spaces and underscores:
  85. while ( *ptr == ' ' || *ptr == '_' )
  86. ptr++;
  87. U32 len = dStrlen( ptr );
  88. if ( len )
  89. {
  90. char* ret = Con::getReturnBuffer( MaxPlayerNameLength + 1 );
  91. char* rptr = ret;
  92. ret[MaxPlayerNameLength - 1] = '\0';
  93. ret[MaxPlayerNameLength] = '\0';
  94. bool space = false;
  95. U8 ch;
  96. while ( *ptr && dStrlen( ret ) < MaxPlayerNameLength )
  97. {
  98. ch = (U8) *ptr;
  99. // Strip all illegal characters:
  100. if ( ch < 32 || ch == ',' || ch == '.' || ch == '\'' || ch == '`' )
  101. {
  102. ptr++;
  103. continue;
  104. }
  105. // Don't allow double spaces or space-underline combinations:
  106. if ( ch == ' ' || ch == '_' )
  107. {
  108. if ( space )
  109. {
  110. ptr++;
  111. continue;
  112. }
  113. else
  114. space = true;
  115. }
  116. else
  117. space = false;
  118. *rptr++ = *ptr;
  119. ptr++;
  120. }
  121. *rptr = '\0';
  122. //finally, strip out the ML text control chars...
  123. return GuiMLTextCtrl::stripControlChars(ret);
  124. }
  125. return( "" );
  126. }
  127. ConsoleFunctionGroupBegin( Platform , "General platform functions.");
  128. DefineConsoleFunction( lockMouse, void, (bool isLocked ), , "(bool isLocked)"
  129. "@brief Lock or unlock the mouse to the window.\n\n"
  130. "When true, prevents the mouse from leaving the bounds of the game window.\n\n"
  131. "@ingroup Input")
  132. {
  133. Platform::setWindowLocked(isLocked);
  134. }
  135. DefineConsoleFunction( setNetPort, bool, (int port, bool bind), (true), "(int port, bool bind=true)"
  136. "@brief Set the network port for the game to use.\n\n"
  137. "@param port The port to use.\n"
  138. "@param bind True if bind() should be called on the port.\n"
  139. "@returns True if the port was successfully opened.\n"
  140. "This will trigger a windows firewall prompt. "
  141. "If you don't have firewall tunneling tech you can set this to false to avoid the prompt.\n\n"
  142. "@ingroup Networking")
  143. {
  144. return Net::openPort((S32)port, bind);
  145. }
  146. DefineConsoleFunction(isAddressTypeAvailable, bool, (int addressType), , "(protocol id)"
  147. "@brief Determines if a specified address type can be reached.\n\n"
  148. "@ingroup Networking")
  149. {
  150. return Net::isAddressTypeAvailable((NetAddress::Type)addressType);
  151. }
  152. DefineConsoleFunction( closeNetPort, void, (), , "()"
  153. "@brief Closes the current network port\n\n"
  154. "@ingroup Networking")
  155. {
  156. Net::closePort();
  157. }
  158. DefineConsoleFunction( saveJournal, void, (const char * filename), , "(string filename)"
  159. "Save the journal to the specified file.\n\n"
  160. "@ingroup Platform")
  161. {
  162. Journal::Record(filename);
  163. }
  164. DefineConsoleFunction( playJournal, void, (const char * filename), , "(string filename)"
  165. "@brief Begin playback of a journal from a specified field.\n\n"
  166. "@param filename Name and path of file journal file\n"
  167. "@ingroup Platform")
  168. {
  169. // CodeReview - BJG 4/24/2007 - The break flag needs to be wired back in.
  170. // bool jBreak = (argc > 2)? dAtob(argv[2]): false;
  171. Journal::Play(filename);
  172. }
  173. DefineConsoleFunction( getSimTime, S32, (), , "()"
  174. "Return the current sim time in milliseconds.\n\n"
  175. "@brief Sim time is time since the game started.\n\n"
  176. "@ingroup Platform")
  177. {
  178. return Sim::getCurrentTime();
  179. }
  180. DefineConsoleFunction( getRealTime, S32, (), , "()"
  181. "@brief Return the current real time in milliseconds.\n\n"
  182. "Real time is platform defined; typically time since the computer booted.\n\n"
  183. "@ingroup Platform")
  184. {
  185. return Platform::getRealMilliseconds();
  186. }
  187. ConsoleFunction( getLocalTime, const char *, 1, 1, "Return the current local time as: weekday month day year hour min sec.\n\n"
  188. "Local time is platform defined.")
  189. {
  190. Platform::LocalTime lt;
  191. Platform::getLocalTime(lt);
  192. static const U32 bufSize = 128;
  193. char *retBuffer = Con::getReturnBuffer(bufSize);
  194. dSprintf(retBuffer, bufSize, "%d %d %d %d %02d %02d %02d",
  195. lt.weekday,
  196. lt.month + 1,
  197. lt.monthday,
  198. lt.year + 1900,
  199. lt.hour,
  200. lt.min,
  201. lt.sec);
  202. return retBuffer;
  203. }
  204. ConsoleFunctionGroupEnd(Platform);
  205. //-----------------------------------------------------------------------------
  206. bool clientProcess(U32 timeDelta)
  207. {
  208. // Required heartbeat call on the client side which must come
  209. // before the advanceTime() calls are made to the scene objects.
  210. arcaneFX::advanceTime(timeDelta);
  211. bool ret = true;
  212. #ifndef TORQUE_TGB_ONLY
  213. ret = ClientProcessList::get()->advanceTime(timeDelta);
  214. #else
  215. ret = gt2dNetworkClientProcess.advanceTime( timeDelta );
  216. #endif
  217. ITickable::advanceTime(timeDelta);
  218. #ifndef TORQUE_TGB_ONLY
  219. // Determine if we're lagging
  220. GameConnection* connection = GameConnection::getConnectionToServer();
  221. if(connection)
  222. {
  223. connection->detectLag();
  224. }
  225. #else
  226. // Determine if we're lagging
  227. t2dGameConnection* connection = t2dGameConnection::getConnectionToServer();
  228. if(connection)
  229. {
  230. connection->detectLag();
  231. }
  232. #endif
  233. // Let SFX process.
  234. SFX->_update();
  235. return ret;
  236. }
  237. bool serverProcess(U32 timeDelta)
  238. {
  239. bool ret = true;
  240. #ifndef TORQUE_TGB_ONLY
  241. ret = ServerProcessList::get()->advanceTime(timeDelta);
  242. #else
  243. ret = gt2dNetworkServerProcess.advanceTime( timeDelta );
  244. #endif
  245. return ret;
  246. }