gameFunctions.cpp 18 KB

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