gameFunctions.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 "T3D/gameFunctions.h"
  24. #include "T3D/gameBase/gameConnection.h"
  25. #include "T3D/camera.h"
  26. #include "T3D/sfx/sfx3DWorld.h"
  27. #include "console/consoleTypes.h"
  28. #include "gui/3d/guiTSControl.h"
  29. #include "core/util/journal/process.h"
  30. #include "materials/materialManager.h"
  31. #include "math/mEase.h"
  32. #include "core/module.h"
  33. #include "console/engineAPI.h"
  34. static void RegisterGameFunctions();
  35. static void Process3D();
  36. MODULE_BEGIN( 3D )
  37. MODULE_INIT_AFTER( Process )
  38. MODULE_INIT_AFTER( Scene )
  39. MODULE_SHUTDOWN_BEFORE( Process )
  40. MODULE_SHUTDOWN_BEFORE( Sim )
  41. MODULE_SHUTDOWN_AFTER( Scene )
  42. MODULE_INIT
  43. {
  44. Process::notify(Process3D, PROCESS_TIME_ORDER);
  45. GameConnection::smFovUpdate.notify(GameSetCameraFov);
  46. RegisterGameFunctions();
  47. }
  48. MODULE_SHUTDOWN
  49. {
  50. GameConnection::smFovUpdate.remove(GameSetCameraFov);
  51. Process::remove(Process3D);
  52. }
  53. MODULE_END;
  54. static S32 gEaseInOut = Ease::InOut;
  55. static S32 gEaseIn = Ease::In;
  56. static S32 gEaseOut = Ease::Out;
  57. static S32 gEaseLinear = Ease::Linear;
  58. static S32 gEaseQuadratic= Ease::Quadratic;
  59. static S32 gEaseCubic= Ease::Cubic;
  60. static S32 gEaseQuartic = Ease::Quartic;
  61. static S32 gEaseQuintic = Ease::Quintic;
  62. static S32 gEaseSinusoidal= Ease::Sinusoidal;
  63. static S32 gEaseExponential = Ease::Exponential;
  64. static S32 gEaseCircular = Ease::Circular;
  65. static S32 gEaseElastic = Ease::Elastic;
  66. static S32 gEaseBack = Ease::Back;
  67. static S32 gEaseBounce = Ease::Bounce;
  68. extern void ShowInit();
  69. //------------------------------------------------------------------------------
  70. /// Camera and FOV info
  71. namespace {
  72. const U32 MaxZoomSpeed = 2000; ///< max number of ms to reach target FOV
  73. static F32 sConsoleCameraFov = 90.f; ///< updated to camera FOV each frame
  74. static F32 sDefaultFov = 90.f; ///< normal FOV
  75. static F32 sCameraFov = 90.f; ///< current camera FOV
  76. static F32 sTargetFov = 90.f; ///< the desired FOV
  77. static F32 sLastCameraUpdateTime = 0; ///< last time camera was updated
  78. static S32 sZoomSpeed = 500; ///< ms per 90deg fov change
  79. /// A scale to apply to the normal visible distance
  80. /// typically used for tuning performance.
  81. static F32 sVisDistanceScale = 1.0f;
  82. } // namespace {}
  83. // query
  84. static SimpleQueryList sgServerQueryList;
  85. static U32 sgServerQueryIndex = 0;
  86. //SERVER FUNCTIONS ONLY
  87. ConsoleFunctionGroupBegin( Containers, "Spatial query functions. <b>Server side only!</b>");
  88. ConsoleFunction(containerFindFirst, const char*, 6, 6, "(int mask, Point3F point, float x, float y, float z)"
  89. "@brief Find objects matching the bitmask type within a box centered at point, with extents x, y, z.\n\n"
  90. "@returns The first object found, or an empty string if nothing was found. Thereafter, you can get more "
  91. "results using containerFindNext()."
  92. "@see containerFindNext\n"
  93. "@ingroup Game")
  94. {
  95. //find out what we're looking for
  96. U32 typeMask = U32(dAtoi(argv[1]));
  97. //find the center of the container volume
  98. Point3F origin(0.0f, 0.0f, 0.0f);
  99. dSscanf(argv[2], "%g %g %g", &origin.x, &origin.y, &origin.z);
  100. //find the box dimensions
  101. Point3F size(0.0f, 0.0f, 0.0f);
  102. size.x = mFabs(dAtof(argv[3]));
  103. size.y = mFabs(dAtof(argv[4]));
  104. size.z = mFabs(dAtof(argv[5]));
  105. //build the container volume
  106. Box3F queryBox;
  107. queryBox.minExtents = origin;
  108. queryBox.maxExtents = origin;
  109. queryBox.minExtents -= size;
  110. queryBox.maxExtents += size;
  111. //initialize the list, and do the query
  112. sgServerQueryList.mList.clear();
  113. gServerContainer.findObjects(queryBox, typeMask, SimpleQueryList::insertionCallback, &sgServerQueryList);
  114. //return the first element
  115. sgServerQueryIndex = 0;
  116. char *buff = Con::getReturnBuffer(100);
  117. if (sgServerQueryList.mList.size())
  118. dSprintf(buff, 100, "%d", sgServerQueryList.mList[sgServerQueryIndex++]->getId());
  119. else
  120. buff[0] = '\0';
  121. return buff;
  122. }
  123. ConsoleFunction( containerFindNext, const char*, 1, 1, "()"
  124. "@brief Get more results from a previous call to containerFindFirst().\n\n"
  125. "@note You must call containerFindFirst() to begin the search.\n"
  126. "@returns The next object found, or an empty string if nothing else was found.\n"
  127. "@see containerFindFirst()\n"
  128. "@ingroup Game")
  129. {
  130. //return the next element
  131. char *buff = Con::getReturnBuffer(100);
  132. if (sgServerQueryIndex < sgServerQueryList.mList.size())
  133. dSprintf(buff, 100, "%d", sgServerQueryList.mList[sgServerQueryIndex++]->getId());
  134. else
  135. buff[0] = '\0';
  136. return buff;
  137. }
  138. ConsoleFunctionGroupEnd( Containers );
  139. //------------------------------------------------------------------------------
  140. bool GameGetCameraTransform(MatrixF *mat, Point3F *velocity)
  141. {
  142. // Return the position and velocity of the control object
  143. GameConnection* connection = GameConnection::getConnectionToServer();
  144. return connection && connection->getControlCameraTransform(0, mat) &&
  145. connection->getControlCameraVelocity(velocity);
  146. }
  147. //------------------------------------------------------------------------------
  148. DefineEngineFunction( setDefaultFov, void, ( F32 defaultFOV ),,
  149. "@brief Set the default FOV for a camera.\n"
  150. "@param defaultFOV The default field of view in degrees\n"
  151. "@ingroup CameraSystem")
  152. {
  153. sDefaultFov = mClampF(defaultFOV, MinCameraFov, MaxCameraFov);
  154. if(sCameraFov == sTargetFov)
  155. sTargetFov = sDefaultFov;
  156. }
  157. DefineEngineFunction( setZoomSpeed, void, ( S32 speed ),,
  158. "@brief Set the zoom speed of the camera.\n"
  159. "This affects how quickly the camera changes from one field of view "
  160. "to another.\n"
  161. "@param speed The camera's zoom speed in ms per 90deg FOV change\n"
  162. "@ingroup CameraSystem")
  163. {
  164. sZoomSpeed = mClamp(speed, 0, MaxZoomSpeed);
  165. }
  166. DefineEngineFunction( setFov, void, ( F32 FOV ),,
  167. "@brief Set the FOV of the camera.\n"
  168. "@param FOV The camera's new FOV in degrees\n"
  169. "@ingroup CameraSystem")
  170. {
  171. sTargetFov = mClampF(FOV, MinCameraFov, MaxCameraFov);
  172. }
  173. F32 GameGetCameraFov()
  174. {
  175. return(sCameraFov);
  176. }
  177. void GameSetCameraFov(F32 fov)
  178. {
  179. sTargetFov = sCameraFov = fov;
  180. }
  181. void GameSetCameraTargetFov(F32 fov)
  182. {
  183. sTargetFov = fov;
  184. }
  185. void GameUpdateCameraFov()
  186. {
  187. F32 time = F32(Platform::getVirtualMilliseconds());
  188. // need to update fov?
  189. if(sTargetFov != sCameraFov)
  190. {
  191. F32 delta = time - sLastCameraUpdateTime;
  192. // snap zoom?
  193. if((sZoomSpeed == 0) || (delta <= 0.f))
  194. sCameraFov = sTargetFov;
  195. else
  196. {
  197. // gZoomSpeed is time in ms to zoom 90deg
  198. F32 step = 90.f * (delta / F32(sZoomSpeed));
  199. if(sCameraFov > sTargetFov)
  200. {
  201. sCameraFov -= step;
  202. if(sCameraFov < sTargetFov)
  203. sCameraFov = sTargetFov;
  204. }
  205. else
  206. {
  207. sCameraFov += step;
  208. if(sCameraFov > sTargetFov)
  209. sCameraFov = sTargetFov;
  210. }
  211. }
  212. }
  213. // the game connection controls the vertical and the horizontal
  214. GameConnection * connection = GameConnection::getConnectionToServer();
  215. if(connection)
  216. {
  217. // check if fov is valid on control object
  218. if(connection->isValidControlCameraFov(sCameraFov))
  219. connection->setControlCameraFov(sCameraFov);
  220. else
  221. {
  222. // will set to the closest fov (fails only on invalid control object)
  223. if(connection->setControlCameraFov(sCameraFov))
  224. {
  225. F32 setFov = sCameraFov;
  226. connection->getControlCameraFov(&setFov);
  227. sTargetFov = sCameraFov = setFov;
  228. }
  229. }
  230. }
  231. // update the console variable
  232. sConsoleCameraFov = sCameraFov;
  233. sLastCameraUpdateTime = time;
  234. }
  235. //--------------------------------------------------------------------------
  236. #ifdef TORQUE_DEBUG
  237. // ConsoleFunction(dumpTSShapes, void, 1, 1, "dumpTSShapes();")
  238. // {
  239. // argc, argv;
  240. // FindMatch match("*.dts", 4096);
  241. // gResourceManager->findMatches(&match);
  242. // for (U32 i = 0; i < match.numMatches(); i++)
  243. // {
  244. // U32 j;
  245. // Resource<TSShape> shape = ResourceManager::get().load(match.matchList[i]);
  246. // if (bool(shape) == false)
  247. // Con::errorf(" aaa Couldn't load: %s", match.matchList[i]);
  248. // U32 numMeshes = 0, numSkins = 0;
  249. // for (j = 0; j < shape->meshes.size(); j++)
  250. // if (shape->meshes[j])
  251. // numMeshes++;
  252. // for (j = 0; j < shape->skins.size(); j++)
  253. // if (shape->skins[j])
  254. // numSkins++;
  255. // Con::printf(" aaa Shape: %s (%d meshes, %d skins)", match.matchList[i], numMeshes, numSkins);
  256. // Con::printf(" aaa Meshes");
  257. // for (j = 0; j < shape->meshes.size(); j++)
  258. // {
  259. // if (shape->meshes[j])
  260. // Con::printf(" aaa %d -> nf: %d, nmf: %d, nvpf: %d (%d, %d, %d, %d, %d)",
  261. // shape->meshes[j]->meshType & TSMesh::TypeMask,
  262. // shape->meshes[j]->numFrames,
  263. // shape->meshes[j]->numMatFrames,
  264. // shape->meshes[j]->vertsPerFrame,
  265. // shape->meshes[j]->verts.size(),
  266. // shape->meshes[j]->norms.size(),
  267. // shape->meshes[j]->tverts.size(),
  268. // shape->meshes[j]->primitives.size(),
  269. // shape->meshes[j]->indices.size());
  270. // }
  271. // Con::printf(" aaa Skins");
  272. // for (j = 0; j < shape->skins.size(); j++)
  273. // {
  274. // if (shape->skins[j])
  275. // Con::printf(" aaa %d -> nf: %d, nmf: %d, nvpf: %d (%d, %d, %d, %d, %d)",
  276. // shape->skins[j]->meshType & TSMesh::TypeMask,
  277. // shape->skins[j]->numFrames,
  278. // shape->skins[j]->numMatFrames,
  279. // shape->skins[j]->vertsPerFrame,
  280. // shape->skins[j]->verts.size(),
  281. // shape->skins[j]->norms.size(),
  282. // shape->skins[j]->tverts.size(),
  283. // shape->skins[j]->primitives.size(),
  284. // shape->skins[j]->indices.size());
  285. // }
  286. // }
  287. // }
  288. #endif
  289. bool GameProcessCameraQuery(CameraQuery *query)
  290. {
  291. GameConnection* connection = GameConnection::getConnectionToServer();
  292. if (connection && connection->getControlCameraTransform(0.032f, &query->cameraMatrix))
  293. {
  294. query->object = dynamic_cast<ShapeBase*>(connection->getControlObject());
  295. query->nearPlane = gClientSceneGraph->getNearClip();
  296. // Scale the normal visible distance by the performance
  297. // tuning scale which we never let over 1.
  298. sVisDistanceScale = mClampF( sVisDistanceScale, 0.01f, 1.0f );
  299. query->farPlane = gClientSceneGraph->getVisibleDistance() * sVisDistanceScale;
  300. F32 cameraFov;
  301. if(!connection->getControlCameraFov(&cameraFov))
  302. return false;
  303. query->fov = mDegToRad(cameraFov);
  304. return true;
  305. }
  306. return false;
  307. }
  308. void GameRenderWorld()
  309. {
  310. PROFILE_START(GameRenderWorld);
  311. FrameAllocator::setWaterMark(0);
  312. gClientSceneGraph->renderScene( SPT_Diffuse );
  313. // renderScene leaves some states dirty, which causes problems if GameTSCtrl is the last Gui object rendered
  314. GFX->updateStates();
  315. AssertFatal(FrameAllocator::getWaterMark() == 0,
  316. "Error, someone didn't reset the water mark on the frame allocator!");
  317. FrameAllocator::setWaterMark(0);
  318. PROFILE_END();
  319. }
  320. static void Process3D()
  321. {
  322. MATMGR->updateTime();
  323. // Update the SFX world, if there is one.
  324. if( gSFX3DWorld )
  325. gSFX3DWorld->update();
  326. }
  327. static void RegisterGameFunctions()
  328. {
  329. Con::addVariable( "$pref::Camera::distanceScale", TypeF32, &sVisDistanceScale,
  330. "A scale to apply to the normal visible distance, typically used for tuning performance.\n"
  331. "@ingroup Game");
  332. Con::addVariable( "$cameraFov", TypeF32, &sConsoleCameraFov,
  333. "The camera's Field of View.\n\n"
  334. "@ingroup Game" );
  335. // Stuff game types into the console
  336. Con::setIntVariable("$TypeMasks::StaticObjectType", StaticObjectType);
  337. Con::setIntVariable("$TypeMasks::EnvironmentObjectType", EnvironmentObjectType);
  338. Con::setIntVariable("$TypeMasks::TerrainObjectType", TerrainObjectType);
  339. Con::setIntVariable("$TypeMasks::WaterObjectType", WaterObjectType);
  340. Con::setIntVariable("$TypeMasks::TriggerObjectType", TriggerObjectType);
  341. Con::setIntVariable("$TypeMasks::MarkerObjectType", MarkerObjectType);
  342. Con::setIntVariable("$TypeMasks::GameBaseObjectType", GameBaseObjectType);
  343. Con::setIntVariable("$TypeMasks::ShapeBaseObjectType", ShapeBaseObjectType);
  344. Con::setIntVariable("$TypeMasks::CameraObjectType", CameraObjectType);
  345. Con::setIntVariable("$TypeMasks::StaticShapeObjectType", StaticShapeObjectType);
  346. Con::setIntVariable("$TypeMasks::DynamicShapeObjectType", DynamicShapeObjectType);
  347. Con::setIntVariable("$TypeMasks::PlayerObjectType", PlayerObjectType);
  348. Con::setIntVariable("$TypeMasks::ItemObjectType", ItemObjectType);
  349. Con::setIntVariable("$TypeMasks::VehicleObjectType", VehicleObjectType);
  350. Con::setIntVariable("$TypeMasks::VehicleBlockerObjectType", VehicleBlockerObjectType);
  351. Con::setIntVariable("$TypeMasks::ProjectileObjectType", ProjectileObjectType);
  352. Con::setIntVariable("$TypeMasks::ExplosionObjectType", ExplosionObjectType);
  353. Con::setIntVariable("$TypeMasks::CorpseObjectType", CorpseObjectType);
  354. Con::setIntVariable("$TypeMasks::DebrisObjectType", DebrisObjectType);
  355. Con::setIntVariable("$TypeMasks::PhysicalZoneObjectType", PhysicalZoneObjectType);
  356. Con::setIntVariable("$TypeMasks::LightObjectType", LightObjectType);
  357. Con::addVariable("Ease::InOut", TypeS32, &gEaseInOut,
  358. "InOut ease for curve movement.\n"
  359. "@ingroup Game");
  360. Con::addVariable("Ease::In", TypeS32, &gEaseIn,
  361. "In ease for curve movement.\n"
  362. "@ingroup Game");
  363. Con::addVariable("Ease::Out", TypeS32, &gEaseOut,
  364. "Out ease for curve movement.\n"
  365. "@ingroup Game");
  366. Con::addVariable("Ease::Linear", TypeS32, &gEaseLinear,
  367. "Linear ease for curve movement.\n"
  368. "@ingroup Game");
  369. Con::addVariable("Ease::Quadratic", TypeS32, &gEaseQuadratic,
  370. "Quadratic ease for curve movement.\n"
  371. "@ingroup Game");
  372. Con::addVariable("Ease::Cubic", TypeS32, &gEaseCubic,
  373. "Cubic ease for curve movement.\n"
  374. "@ingroup Game");
  375. Con::addVariable("Ease::Quartic", TypeS32, &gEaseQuartic,
  376. "Quartic ease for curve movement.\n"
  377. "@ingroup Game");
  378. Con::addVariable("Ease::Quintic", TypeS32, &gEaseQuintic,
  379. "Quintic ease for curve movement.\n"
  380. "@ingroup Game");
  381. Con::addVariable("Ease::Sinusoidal", TypeS32, &gEaseSinusoidal,
  382. "Sinusoidal ease for curve movement.\n"
  383. "@ingroup Game");
  384. Con::addVariable("Ease::Exponential", TypeS32, &gEaseExponential,
  385. "Exponential ease for curve movement.\n"
  386. "@ingroup Game");
  387. Con::addVariable("Ease::Circular", TypeS32, &gEaseCircular,
  388. "Circular ease for curve movement.\n"
  389. "@ingroup Game");
  390. Con::addVariable("Ease::Elastic", TypeS32, &gEaseElastic,
  391. "Elastic ease for curve movement.\n"
  392. "@ingroup Game");
  393. Con::addVariable("Ease::Back", TypeS32, &gEaseBack,
  394. "Backwards ease for curve movement.\n"
  395. "@ingroup Game");
  396. Con::addVariable("Ease::Bounce", TypeS32, &gEaseBounce,
  397. "Bounce ease for curve movement.\n"
  398. "@ingroup Game");
  399. }