game.cpp 8.8 KB

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