turretShape.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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 "T3D/turret/turretShape.h"
  23. #include "console/console.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/engineAPI.h"
  26. #include "core/stream/bitStream.h"
  27. #include "math/mMath.h"
  28. #include "math/mathIO.h"
  29. #include "ts/tsShapeInstance.h"
  30. #include "T3D/fx/cameraFXMgr.h"
  31. #include "T3D/gameBase/gameConnection.h"
  32. #include "T3D/physics/physicsBody.h"
  33. //----------------------------------------------------------------------------
  34. // Client prediction
  35. // Trigger objects that are not normally collided with.
  36. static U32 sTriggerMask = ItemObjectType |
  37. TriggerObjectType |
  38. CorpseObjectType;
  39. //----------------------------------------------------------------------------
  40. ImplementEnumType( TurretShapeFireLinkType,
  41. "@brief How the weapons are linked to triggers for this TurretShape.\n\n"
  42. "@ingroup gameObjects\n\n")
  43. { TurretShapeData::FireTogether, "FireTogether", "All weapons fire under trigger 0.\n" },
  44. { TurretShapeData::GroupedFire, "GroupedFire", "Weapon mounts 0,2 fire under trigger 0, mounts 1,3 fire under trigger 1.\n" },
  45. { TurretShapeData::IndividualFire, "IndividualFire", "Each weapon mount fires under its own trigger 0-3.\n" },
  46. EndImplementEnumType;
  47. IMPLEMENT_CO_DATABLOCK_V1(TurretShapeData);
  48. ConsoleDocClass( TurretShapeData,
  49. "@brief Defines properties for a TurretShape object.\n\n"
  50. "@see TurretShape\n"
  51. "@see TurretShapeData\n"
  52. "@ingroup gameObjects\n"
  53. );
  54. IMPLEMENT_CALLBACK( TurretShapeData, onMountObject, void, ( SceneObject* turret, SceneObject* obj, S32 node ),( turret, obj, node ),
  55. "@brief Informs the TurretShapeData object that a player is mounting it.\n\n"
  56. "@param turret The TurretShape object.\n"
  57. "@param obj The player that is mounting.\n"
  58. "@param node The node the player is mounting to.\n"
  59. "@note Server side only.\n"
  60. );
  61. IMPLEMENT_CALLBACK( TurretShapeData, onUnmountObject, void, ( SceneObject* turret, SceneObject* obj ),( turret, obj ),
  62. "@brief Informs the TurretShapeData object that a player is unmounting it.\n\n"
  63. "@param turret The TurretShape object.\n"
  64. "@param obj The player that is unmounting.\n"
  65. "@note Server side only.\n"
  66. );
  67. IMPLEMENT_CALLBACK( TurretShapeData, onStickyCollision, void, ( TurretShape* obj ),( obj ),
  68. "@brief Informs the TurretData object that it is now sticking to another object.\n\n"
  69. "This callback is only called if the TurretData::sticky property for this Turret is true.\n"
  70. "@param obj The Turret object that is colliding.\n"
  71. "@note Server side only.\n"
  72. "@see TurretShape, TurretData\n"
  73. );
  74. TurretShapeData::TurretShapeData()
  75. {
  76. weaponLinkType = FireTogether;
  77. zRotOnly = false;
  78. startLoaded = true;
  79. friction = 0;
  80. elasticity = 0;
  81. sticky = false;
  82. gravityMod = 1.0;
  83. maxVelocity = 25.0f;
  84. density = 2;
  85. drag = 0.5;
  86. cameraOffset = 0;
  87. maxHeading = 180.0f;
  88. minPitch = 90.0f;
  89. maxPitch = 90.0f;
  90. headingRate = -1;
  91. pitchRate = -1;
  92. headingNode = -1;
  93. pitchNode = -1;
  94. U32 i = 0;
  95. for (i=0; i<NumMirrorDirectionNodes; ++i)
  96. {
  97. pitchNodes[i] = -1;
  98. headingNodes[i] = -1;
  99. }
  100. for (i=0; i<ShapeBase::MaxMountedImages; ++i)
  101. {
  102. weaponMountNode[i] = -1;
  103. }
  104. for (i = 0; i < NumRecoilSequences;i++)
  105. recoilSequence[i] = -1;
  106. pitchSequence = -1;
  107. headingSequence = -1;
  108. }
  109. void TurretShapeData::initPersistFields()
  110. {
  111. docsURL;
  112. Parent::initPersistFields();
  113. addGroup("Steering");
  114. addField("zRotOnly", TypeBool, Offset(zRotOnly, TurretShapeData),
  115. "@brief Should the turret allow only z rotations.\n\n"
  116. "True indicates that the turret may only be rotated on its z axis, just like the Item class. "
  117. "This keeps the turret always upright regardless of the surface it lands on.\n");
  118. addField("maxHeading", TypeF32, Offset(maxHeading, TurretShapeData),
  119. "@brief Maximum number of degrees to rotate from center.\n\n"
  120. "A value of 180 or more degrees indicates the turret may rotate completely around.\n");
  121. addField("minPitch", TypeF32, Offset(minPitch, TurretShapeData),
  122. "@brief Minimum number of degrees to rotate down from straight ahead.\n\n");
  123. addField("maxPitch", TypeF32, Offset(maxPitch, TurretShapeData),
  124. "@brief Maximum number of degrees to rotate up from straight ahead.\n\n");
  125. addField("headingRate", TypeF32, Offset(headingRate, TurretShapeData),
  126. "@brief Degrees per second rotation.\n\n"
  127. "A value of 0 means no rotation is allowed. A value less than 0 means the rotation is instantaneous.\n");
  128. addField("pitchRate", TypeF32, Offset(pitchRate, TurretShapeData),
  129. "@brief Degrees per second rotation.\n\n"
  130. "A value of 0 means no rotation is allowed. A value less than 0 means the rotation is instantaneous.\n");
  131. endGroup("Steering");
  132. addGroup("Weapon State");
  133. addField( "weaponLinkType", TYPEID< TurretShapeData::FireLinkType >(), Offset(weaponLinkType, TurretShapeData),
  134. "@brief Set how the mounted weapons are linked and triggered.\n\n"
  135. "<ul><li>FireTogether: All weapons fire under trigger 0.</li>"
  136. "<li>GroupedFire: Weapon mounts 0,2 fire under trigger 0, mounts 1,3 fire under trigger 1.</li>"
  137. "<li>IndividualFire: Each weapon mount fires under its own trigger 0-3.</li></ul>\n"
  138. "@see TurretShapeFireLinkType");
  139. addField("startLoaded", TypeBool, Offset(startLoaded, TurretShapeData),
  140. "@brief Does the turret's mounted weapon(s) start in a loaded state.\n\n"
  141. "True indicates that all mounted weapons start in a loaded state.\n"
  142. "@see ShapeBase::setImageLoaded()");
  143. endGroup("Weapon State");
  144. addGroup("Camera", "The settings used by the shape when it is the camera.");
  145. addField("cameraOffset", TypeF32, Offset(cameraOffset, TurretShapeData),
  146. "Vertical (Z axis) height of the camera above the turret." );
  147. endGroup("Camera");
  148. }
  149. void TurretShapeData::packData(BitStream* stream)
  150. {
  151. Parent::packData(stream);
  152. stream->writeFlag(zRotOnly);
  153. stream->writeInt(weaponLinkType,NumFireLinkTypeBits);
  154. stream->write(cameraOffset);
  155. stream->write(maxHeading);
  156. stream->write(minPitch);
  157. stream->write(maxPitch);
  158. stream->write(headingRate);
  159. stream->write(pitchRate);
  160. }
  161. void TurretShapeData::unpackData(BitStream* stream)
  162. {
  163. Parent::unpackData(stream);
  164. zRotOnly = stream->readFlag();
  165. weaponLinkType = (FireLinkType)stream->readInt(NumFireLinkTypeBits);
  166. stream->read(&cameraOffset);
  167. stream->read(&maxHeading);
  168. stream->read(&minPitch);
  169. stream->read(&maxPitch);
  170. stream->read(&headingRate);
  171. stream->read(&pitchRate);
  172. }
  173. bool TurretShapeData::preload(bool server, String &errorStr)
  174. {
  175. if (!Parent::preload(server, errorStr))
  176. return false;
  177. // We have mShape at this point. Resolve nodes.
  178. headingNode = mShape->findNode("heading");
  179. pitchNode = mShape->findNode("pitch");
  180. // Find any mirror pitch nodes
  181. for (U32 i = 0; i < NumMirrorDirectionNodes; ++i)
  182. {
  183. char name[32];
  184. dSprintf(name, 31, "pitch%d", i+1);
  185. pitchNodes[i] = mShape->findNode(name);
  186. dSprintf(name, 31, "heading%d", i+1);
  187. headingNodes[i] = mShape->findNode(name);
  188. }
  189. // Resolve weapon mount point node indexes
  190. for (U32 i = 0; i < ShapeBase::MaxMountedImages; i++) {
  191. char fullName[256];
  192. dSprintf(fullName,sizeof(fullName),"weaponMount%d",i);
  193. weaponMountNode[i] = mShape->findNode(fullName);
  194. }
  195. // Recoil animations
  196. recoilSequence[0] = mShape->findSequence("light_recoil");
  197. recoilSequence[1] = mShape->findSequence("medium_recoil");
  198. recoilSequence[2] = mShape->findSequence("heavy_recoil");
  199. // Optional sequences used when the turret rotates
  200. pitchSequence = mShape->findSequence("pitch");
  201. headingSequence = mShape->findSequence("heading");
  202. return true;
  203. }
  204. //----------------------------------------------------------------------------
  205. IMPLEMENT_CO_NETOBJECT_V1(TurretShape);
  206. ConsoleDocClass( TurretShape,
  207. "@ingroup gameObjects\n"
  208. );
  209. TurretShape::TurretShape()
  210. {
  211. mTypeMask |= VehicleObjectType | DynamicShapeObjectType | TurretObjectType;
  212. mDataBlock = 0;
  213. allowManualRotation = true;
  214. allowManualFire = true;
  215. mTurretDelta.rot = Point3F(0.0f, 0.0f, 0.0f);
  216. mTurretDelta.rotVec = VectorF(0.0f, 0.0f, 0.0f);
  217. mTurretDelta.dt = 1;
  218. mRot = mTurretDelta.rot;
  219. mPitchAllowed = true;
  220. mHeadingAllowed = true;
  221. mPitchRate = -1;
  222. mHeadingRate = -1;
  223. mPitchUp = 0;
  224. mPitchDown = 0;
  225. mHeadingMax = mDegToRad(180.0f);
  226. mRespawn = false;
  227. mPitchThread = 0;
  228. mHeadingThread = 0;
  229. mSubclassTurretShapeHandlesScene = false;
  230. // For the Item class
  231. mSubclassItemHandlesScene = true;
  232. mRecoilThread = NULL;
  233. mImageStateThread = NULL;
  234. }
  235. TurretShape::~TurretShape()
  236. {
  237. }
  238. //----------------------------------------------------------------------------
  239. void TurretShape::initPersistFields()
  240. {
  241. docsURL;
  242. addField("respawn", TypeBool, Offset(mRespawn, TurretShape),
  243. "@brief Respawn the turret after it has been destroyed.\n\n"
  244. "If true, the turret will respawn after it is destroyed.\n");
  245. Parent::initPersistFields();
  246. }
  247. bool TurretShape::onAdd()
  248. {
  249. if( !Parent::onAdd() )
  250. return false;
  251. // Add this object to the scene
  252. if (!mSubclassTurretShapeHandlesScene)
  253. {
  254. addToScene();
  255. }
  256. if (isServerObject() && !mSubclassTurretShapeHandlesScene)
  257. {
  258. scriptOnAdd();
  259. }
  260. return true;
  261. }
  262. void TurretShape::onRemove()
  263. {
  264. Parent::onRemove();
  265. if (!mSubclassTurretShapeHandlesScene)
  266. {
  267. scriptOnRemove();
  268. // Remove this object from the scene
  269. removeFromScene();
  270. }
  271. }
  272. bool TurretShape::onNewDataBlock(GameBaseData* dptr, bool reload)
  273. {
  274. mDataBlock = dynamic_cast<TurretShapeData*>(dptr);
  275. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  276. return false;
  277. // Mark these nodes for control by code only (will not animate in a sequence)
  278. if (mDataBlock->headingNode != -1)
  279. mShapeInstance->setNodeAnimationState(mDataBlock->headingNode, TSShapeInstance::MaskNodeHandsOff);
  280. if (mDataBlock->pitchNode != -1)
  281. mShapeInstance->setNodeAnimationState(mDataBlock->pitchNode, TSShapeInstance::MaskNodeHandsOff);
  282. for (U32 i=0; i<TurretShapeData::NumMirrorDirectionNodes; ++i)
  283. {
  284. if (mDataBlock->pitchNodes[i] != -1)
  285. {
  286. mShapeInstance->setNodeAnimationState(mDataBlock->pitchNodes[i], TSShapeInstance::MaskNodeHandsOff);
  287. }
  288. if (mDataBlock->headingNodes[i] != -1)
  289. {
  290. mShapeInstance->setNodeAnimationState(mDataBlock->headingNodes[i], TSShapeInstance::MaskNodeHandsOff);
  291. }
  292. }
  293. if (mIsZero(mDataBlock->pitchRate))
  294. {
  295. mPitchAllowed = false;
  296. }
  297. else
  298. {
  299. mPitchAllowed = true;
  300. if (mDataBlock->pitchRate > 0)
  301. {
  302. mPitchRate = mDegToRad(mDataBlock->pitchRate);
  303. }
  304. else
  305. {
  306. mPitchRate = -1;
  307. }
  308. }
  309. if (mIsZero(mDataBlock->headingRate))
  310. {
  311. mHeadingAllowed = false;
  312. }
  313. else
  314. {
  315. mHeadingAllowed = true;
  316. if (mDataBlock->headingRate > 0)
  317. {
  318. mHeadingRate = mDegToRad(mDataBlock->headingRate);
  319. }
  320. else
  321. {
  322. mHeadingRate = -1;
  323. }
  324. }
  325. mPitchUp = -mDegToRad(mDataBlock->maxPitch);
  326. mPitchDown = mDegToRad(mDataBlock->minPitch);
  327. mHeadingMax = mDegToRad(mDataBlock->maxHeading);
  328. // Create Recoil thread if any recoil sequences are specified.
  329. // Note that the server player does not play this animation.
  330. mRecoilThread = 0;
  331. if (isGhost())
  332. for (U32 s = 0; s < TurretShapeData::NumRecoilSequences; s++)
  333. if (mDataBlock->recoilSequence[s] != -1) {
  334. mRecoilThread = mShapeInstance->addThread();
  335. mShapeInstance->setSequence(mRecoilThread, mDataBlock->recoilSequence[s], 0);
  336. mShapeInstance->setTimeScale(mRecoilThread, 0);
  337. break;
  338. }
  339. // Reset the image state driven animation thread. This will be properly built
  340. // in onImageStateAnimation() when needed.
  341. mImageStateThread = 0;
  342. // Optional rotation threads. These only play on the client.
  343. mPitchThread = 0;
  344. mHeadingThread = 0;
  345. if (isGhost())
  346. {
  347. if (mDataBlock->pitchSequence != -1)
  348. {
  349. mPitchThread = mShapeInstance->addThread();
  350. mShapeInstance->setSequence(mPitchThread, mDataBlock->pitchSequence, 0);
  351. mShapeInstance->setTimeScale(mPitchThread, 0);
  352. }
  353. if (mDataBlock->headingSequence != -1)
  354. {
  355. mHeadingThread = mShapeInstance->addThread();
  356. mShapeInstance->setSequence(mHeadingThread, mDataBlock->headingSequence, 0);
  357. mShapeInstance->setTimeScale(mHeadingThread, 0);
  358. }
  359. }
  360. if (!mSubclassTurretShapeHandlesScene)
  361. {
  362. scriptOnNewDataBlock();
  363. }
  364. return true;
  365. }
  366. //----------------------------------------------------------------------------
  367. void TurretShape::updateAnimation(F32 dt)
  368. {
  369. if (mRecoilThread)
  370. mShapeInstance->advanceTime(dt,mRecoilThread);
  371. if (mImageStateThread)
  372. mShapeInstance->advanceTime(dt,mImageStateThread);
  373. // Update any pitch and heading threads
  374. if (mPitchThread)
  375. {
  376. F32 d = mPitchDown - mPitchUp;
  377. if (!mIsZero(d))
  378. {
  379. F32 pos = (mRot.x - mPitchUp) / d;
  380. mShapeInstance->setPos(mPitchThread, mClampF(pos, 0.0f, 1.0f));
  381. }
  382. }
  383. if (mHeadingThread)
  384. {
  385. F32 pos = 0.0f;
  386. if (mHeadingMax < mDegToRad(180.0f))
  387. {
  388. F32 d = mHeadingMax * 2.0f;
  389. if (!mIsZero(d))
  390. {
  391. pos = (mRot.z + mHeadingMax) / d;
  392. }
  393. }
  394. else
  395. {
  396. pos = mRot.z / M_2PI;
  397. if (pos < 0.0f)
  398. {
  399. // We don't want negative rotations to simply mirror the
  400. // positive rotations but to animate into them as if -0.0
  401. // is equivalent to 1.0.
  402. pos = mFmod(pos, 1.0f) + 1.0f;
  403. }
  404. if (pos > 1.0f)
  405. {
  406. pos = mFmod(pos, 1.0f);
  407. }
  408. }
  409. mShapeInstance->setPos(mHeadingThread, mClampF(pos, 0.0f, 1.0f));
  410. }
  411. }
  412. //----------------------------------------------------------------------------
  413. void TurretShape::onImage(U32 imageSlot, bool unmount)
  414. {
  415. // Clear out any previous image state animation
  416. if (mImageStateThread)
  417. {
  418. mShapeInstance->destroyThread(mImageStateThread);
  419. mImageStateThread = 0;
  420. }
  421. }
  422. void TurretShape::onImageRecoil( U32, ShapeBaseImageData::StateData::RecoilState state )
  423. {
  424. if ( mRecoilThread )
  425. {
  426. if ( state != ShapeBaseImageData::StateData::NoRecoil )
  427. {
  428. S32 stateIndex = state - ShapeBaseImageData::StateData::LightRecoil;
  429. if ( mDataBlock->recoilSequence[stateIndex] != -1 )
  430. {
  431. mShapeInstance->setSequence( mRecoilThread, mDataBlock->recoilSequence[stateIndex], 0 );
  432. mShapeInstance->setTimeScale( mRecoilThread, 1 );
  433. }
  434. }
  435. }
  436. }
  437. void TurretShape::onImageStateAnimation(U32 imageSlot, const char* seqName, bool direction, bool scaleToState, F32 stateTimeOutValue)
  438. {
  439. if (isGhost())
  440. {
  441. S32 seqIndex = mShapeInstance->getShape()->findSequence(seqName);
  442. if (seqIndex != -1)
  443. {
  444. if (!mImageStateThread)
  445. {
  446. mImageStateThread = mShapeInstance->addThread();
  447. }
  448. mShapeInstance->setSequence( mImageStateThread, seqIndex, 0 );
  449. F32 timeScale = (scaleToState && stateTimeOutValue) ?
  450. mShapeInstance->getDuration(mImageStateThread) / stateTimeOutValue : 1.0f;
  451. mShapeInstance->setTimeScale( mImageStateThread, direction ? timeScale : -timeScale );
  452. }
  453. }
  454. }
  455. //----------------------------------------------------------------------------
  456. const char* TurretShape::getStateName()
  457. {
  458. if (mDamageState != Enabled)
  459. return "Dead";
  460. if (isMounted())
  461. return "Mounted";
  462. return "Ready";
  463. }
  464. void TurretShape::updateDamageLevel()
  465. {
  466. if (!isGhost())
  467. setDamageState((mDamage >= mDataBlock->maxDamage)? Destroyed: Enabled);
  468. if (mDamageThread)
  469. mShapeInstance->setPos(mDamageThread, mDamage / mDataBlock->destroyedLevel);
  470. }
  471. //----------------------------------------------------------------------------
  472. void TurretShape::processTick(const Move* move)
  473. {
  474. // Image Triggers
  475. if (getAllowManualFire() && move && mDamageState == Enabled)
  476. {
  477. switch(mDataBlock->weaponLinkType)
  478. {
  479. case TurretShapeData::FireTogether:
  480. {
  481. setImageTriggerState(0,move->trigger[0]);
  482. setImageTriggerState(1,move->trigger[0]);
  483. setImageTriggerState(2,move->trigger[0]);
  484. setImageTriggerState(3,move->trigger[0]);
  485. setImageAltTriggerState(0,move->trigger[1]);
  486. setImageAltTriggerState(1,move->trigger[1]);
  487. setImageAltTriggerState(2,move->trigger[1]);
  488. setImageAltTriggerState(3,move->trigger[1]);
  489. break;
  490. }
  491. case TurretShapeData::GroupedFire:
  492. {
  493. setImageTriggerState(0,move->trigger[0]);
  494. setImageTriggerState(1,move->trigger[1]);
  495. setImageTriggerState(2,move->trigger[0]);
  496. setImageTriggerState(3,move->trigger[1]);
  497. break;
  498. }
  499. case TurretShapeData::IndividualFire:
  500. {
  501. setImageTriggerState(0,move->trigger[0]);
  502. setImageTriggerState(1,move->trigger[1]);
  503. setImageTriggerState(2,move->trigger[2]);
  504. setImageTriggerState(3,move->trigger[3]);
  505. break;
  506. }
  507. }
  508. }
  509. Parent::processTick(move);
  510. // Change our type based on our rest state
  511. if (mAtRest)
  512. {
  513. // At rest so we're static
  514. mTypeMask &= ~DynamicShapeObjectType;
  515. mTypeMask |= StaticObjectType | StaticShapeObjectType;
  516. }
  517. else
  518. {
  519. // Not at rest so we're dynamic
  520. mTypeMask &= ~StaticObjectType;
  521. mTypeMask &= ~StaticShapeObjectType;
  522. mTypeMask |= DynamicShapeObjectType;
  523. }
  524. if (!isGhost())
  525. updateAnimation(TickSec);
  526. updateMove(move);
  527. }
  528. void TurretShape::interpolateTick(F32 dt)
  529. {
  530. Parent::interpolateTick(dt);
  531. if (isMounted()) {
  532. MatrixF mat;
  533. mMount.object->getRenderMountTransform( dt, mMount.node, mMount.xfm, &mat );
  534. ShapeBase::setRenderTransform(mat);
  535. }
  536. // Orientation
  537. Point3F rot = mTurretDelta.rot + mTurretDelta.rotVec * dt;
  538. // Make sure we don't interpolate past the limits
  539. _applyLimits(rot);
  540. _setRotation(rot);
  541. }
  542. void TurretShape::advanceTime(F32 dt)
  543. {
  544. // If there were any ShapeBase script threads that
  545. // have played, then we need to update all code
  546. // controlled nodes. This is done before the Parent
  547. // call as script threads may play and be destroyed
  548. // before our code is called.
  549. bool updateNodes = false;
  550. for (U32 i = 0; i < MaxScriptThreads; i++)
  551. {
  552. Thread& st = mScriptThread[i];
  553. if (st.thread)
  554. {
  555. updateNodes = true;
  556. break;
  557. }
  558. }
  559. Parent::advanceTime(dt);
  560. updateAnimation(dt);
  561. _setRotation(mRot);
  562. }
  563. void TurretShape::setTransform( const MatrixF& mat )
  564. {
  565. if (mDataBlock && mDataBlock->zRotOnly)
  566. {
  567. // Allow Item::setTransform() to do the work
  568. Parent::setTransform( mat );
  569. }
  570. else
  571. {
  572. // Do the transform work here to avoid Item's restriction on rotation
  573. ShapeBase::setTransform( mat );
  574. if ( !mStatic )
  575. {
  576. mAtRest = false;
  577. mAtRestCounter = 0;
  578. }
  579. if ( mPhysicsRep )
  580. mPhysicsRep->setTransform( getTransform() );
  581. setMaskBits( Item::RotationMask | Item::PositionMask | Item::NoWarpMask );
  582. }
  583. }
  584. void TurretShape::updateMove(const Move* move)
  585. {
  586. PROFILE_SCOPE( TurretShape_UpdateMove );
  587. if (!move)
  588. return;
  589. Point3F vec, pos;
  590. // Update orientation
  591. mTurretDelta.rotVec = mRot;
  592. VectorF rotVec(0, 0, 0);
  593. if (getAllowManualRotation())
  594. {
  595. if (mPitchAllowed)
  596. {
  597. rotVec.x = move->pitch * 2.0f; // Assume that our -2PI to 2PI range was clamped to -PI to PI in script;
  598. if (mPitchRate > 0)
  599. {
  600. rotVec.x *= mPitchRate * TickSec;
  601. }
  602. }
  603. if (mHeadingAllowed)
  604. {
  605. rotVec.z = move->yaw * 2.0f; // Assume that our -2PI to 2PI range was clamped to -PI to PI in script
  606. if (mHeadingRate > 0)
  607. {
  608. rotVec.z *= mHeadingRate * TickSec;
  609. }
  610. }
  611. }
  612. mRot.x += rotVec.x;
  613. mRot.z += rotVec.z;
  614. _applyLimits(mRot);
  615. if (isServerObject())
  616. {
  617. // As this ends up animating shape nodes, we have no sense of a transform and
  618. // render transform. Therefore we treat this as the true transform and leave the
  619. // client shape node changes to interpolateTick() as the render transform. Otherwise
  620. // on the client we'll have this node change from processTick() and then backstepping
  621. // and catching up to the true node change in interpolateTick(), which causes the
  622. // turret to stutter.
  623. _setRotation( mRot );
  624. }
  625. else
  626. {
  627. // If on the client, calc delta for backstepping
  628. mTurretDelta.rot = mRot;
  629. mTurretDelta.rotVec = mTurretDelta.rotVec - mTurretDelta.rot;
  630. }
  631. setMaskBits(TurretUpdateMask);
  632. }
  633. bool TurretShape::getNodeTransform(S32 node, MatrixF& mat)
  634. {
  635. if (node == -1)
  636. return false;
  637. MatrixF nodeTransform = mShapeInstance->mNodeTransforms[node];
  638. const Point3F& scale = getScale();
  639. // The position of the node needs to be scaled.
  640. Point3F position = nodeTransform.getPosition();
  641. position.convolve( scale );
  642. nodeTransform.setPosition( position );
  643. mat.mul(mObjToWorld, nodeTransform);
  644. return true;
  645. }
  646. bool TurretShape::getWorldNodeTransform(S32 node, MatrixF& mat)
  647. {
  648. MatrixF nodeMat;
  649. if (!getNodeTransform(node, nodeMat))
  650. return false;
  651. nodeMat.affineInverse();
  652. mat = nodeMat;
  653. return true;
  654. }
  655. void TurretShape::_setRotation(const Point3F& rot)
  656. {
  657. _updateNodes(rot);
  658. mShapeInstance->animate();
  659. mRot = rot;
  660. }
  661. void TurretShape::_updateNodes(const Point3F& rot)
  662. {
  663. EulerF xRot(rot.x, 0.0f, 0.0f);
  664. EulerF zRot(0.0f, 0.0f, rot.z);
  665. // Set heading
  666. S32 node = mDataBlock->headingNode;
  667. if (node != -1)
  668. {
  669. MatrixF* mat = &mShapeInstance->mNodeTransforms[node];
  670. Point3F defaultPos = mShapeInstance->getShape()->defaultTranslations[node];
  671. Quat16 defaultRot = mShapeInstance->getShape()->defaultRotations[node];
  672. QuatF qrot(zRot);
  673. qrot *= defaultRot.getQuatF();
  674. qrot.setMatrix( mat );
  675. mat->setColumn(3, defaultPos);
  676. }
  677. // Set pitch
  678. node = mDataBlock->pitchNode;
  679. if (node != -1)
  680. {
  681. MatrixF* mat = &mShapeInstance->mNodeTransforms[node];
  682. Point3F defaultPos = mShapeInstance->getShape()->defaultTranslations[node];
  683. Quat16 defaultRot = mShapeInstance->getShape()->defaultRotations[node];
  684. QuatF qrot(xRot);
  685. qrot *= defaultRot.getQuatF();
  686. qrot.setMatrix( mat );
  687. mat->setColumn(3, defaultPos);
  688. }
  689. // Now the mirror direction nodes, if any
  690. for (U32 i=0; i<TurretShapeData::NumMirrorDirectionNodes; ++i)
  691. {
  692. node = mDataBlock->pitchNodes[i];
  693. if (node != -1)
  694. {
  695. MatrixF* mat = &mShapeInstance->mNodeTransforms[node];
  696. Point3F defaultPos = mShapeInstance->getShape()->defaultTranslations[node];
  697. Quat16 defaultRot = mShapeInstance->getShape()->defaultRotations[node];
  698. QuatF qrot(xRot);
  699. qrot *= defaultRot.getQuatF();
  700. qrot.setMatrix( mat );
  701. mat->setColumn(3, defaultPos);
  702. }
  703. node = mDataBlock->headingNodes[i];
  704. if (node != -1)
  705. {
  706. MatrixF* mat = &mShapeInstance->mNodeTransforms[node];
  707. Point3F defaultPos = mShapeInstance->getShape()->defaultTranslations[node];
  708. Quat16 defaultRot = mShapeInstance->getShape()->defaultRotations[node];
  709. QuatF qrot(zRot);
  710. qrot *= defaultRot.getQuatF();
  711. qrot.setMatrix( mat );
  712. mat->setColumn(3, defaultPos);
  713. }
  714. }
  715. mShapeInstance->setDirty(TSShapeInstance::TransformDirty);
  716. }
  717. void TurretShape::_applyLimits(Point3F& rot)
  718. {
  719. rot.x = mClampF(rot.x, mPitchUp, mPitchDown);
  720. if (mHeadingMax < mDegToRad(180.0f))
  721. {
  722. rot.z = mClampF(rot.z, -mHeadingMax, mHeadingMax);
  723. }
  724. }
  725. bool TurretShape::_outsideLimits(Point3F& rot)
  726. {
  727. if (rot.x < mPitchUp || rot.x > mPitchDown)
  728. return true;
  729. if (mHeadingMax < mDegToRad(180.0f))
  730. {
  731. if (rot.z < -mHeadingMax || rot.z > mHeadingMax)
  732. return true;
  733. }
  734. return false;
  735. }
  736. //----------------------------------------------------------------------------
  737. void TurretShape::mountObject( SceneObject *obj, S32 node, const MatrixF &xfm )
  738. {
  739. Parent::mountObject(obj, node, xfm);
  740. if (isClientObject())
  741. {
  742. if (obj)
  743. {
  744. GameConnection* conn = obj->getControllingClient();
  745. if (conn)
  746. {
  747. // Allow the client to set up any action maps, HUD, etc.
  748. Con::executef("turretMountCallback", Con::getIntArg(getId()), Con::getIntArg(obj->getId()), Con::getIntArg(true));
  749. }
  750. }
  751. }
  752. else
  753. {
  754. mDataBlock->onMountObject_callback( this, obj, node );
  755. }
  756. }
  757. void TurretShape::unmountObject( SceneObject *obj )
  758. {
  759. Parent::unmountObject(obj);
  760. if (isClientObject())
  761. {
  762. if (obj)
  763. {
  764. GameConnection* conn = obj->getControllingClient();
  765. if (conn)
  766. {
  767. // Allow the client to set up any action maps, HUD, etc.
  768. Con::executef("turretMountCallback", Con::getIntArg(getId()), Con::getIntArg(obj->getId()), Con::getIntArg(false));
  769. }
  770. }
  771. }
  772. else
  773. {
  774. mDataBlock->onUnmountObject_callback( this, obj );
  775. }
  776. }
  777. void TurretShape::onUnmount(SceneObject*,S32)
  778. {
  779. // Make sure the client get's the final server pos of this turret.
  780. setMaskBits(PositionMask);
  781. }
  782. //----------------------------------------------------------------------------
  783. void TurretShape::getCameraParameters(F32 *min,F32* max,Point3F* off,MatrixF* rot)
  784. {
  785. *min = mDataBlock->cameraMinDist;
  786. *max = mDataBlock->cameraMaxDist;
  787. off->set(0,0,mDataBlock->cameraOffset);
  788. rot->identity();
  789. }
  790. void TurretShape::getCameraTransform(F32* pos,MatrixF* mat)
  791. {
  792. // Returns camera to world space transform
  793. // Handles first person / third person camera position
  794. if (isServerObject() && mShapeInstance)
  795. mShapeInstance->animateNodeSubtrees(true);
  796. if (*pos == 0) {
  797. getRenderEyeTransform(mat);
  798. return;
  799. }
  800. // Get the shape's camera parameters.
  801. F32 min,max;
  802. MatrixF rot;
  803. Point3F offset;
  804. getCameraParameters(&min,&max,&offset,&rot);
  805. // Start with the current eye position
  806. MatrixF eye;
  807. getRenderEyeTransform(&eye);
  808. // Build a transform that points along the eye axis
  809. // but where the Z axis is always up.
  810. {
  811. MatrixF cam(1);
  812. VectorF x,y,z(0,0,1);
  813. eye.getColumn(1, &y);
  814. mCross(y, z, &x);
  815. x.normalize();
  816. mCross(x, y, &z);
  817. z.normalize();
  818. cam.setColumn(0,x);
  819. cam.setColumn(1,y);
  820. cam.setColumn(2,z);
  821. mat->mul(cam,rot);
  822. }
  823. // Camera is positioned straight back along the eye's -Y axis.
  824. // A ray is cast to make sure the camera doesn't go through
  825. // anything solid.
  826. VectorF vp,vec;
  827. vp.x = vp.z = 0;
  828. vp.y = -(max - min) * *pos;
  829. eye.mulV(vp,&vec);
  830. // Use the camera node as the starting position if it exists.
  831. Point3F osp,sp;
  832. if (mDataBlock->cameraNode != -1)
  833. {
  834. mShapeInstance->mNodeTransforms[mDataBlock->cameraNode].getColumn(3,&osp);
  835. getRenderTransform().mulP(osp,&sp);
  836. }
  837. else
  838. eye.getColumn(3,&sp);
  839. // Make sure we don't hit ourself...
  840. disableCollision();
  841. if (isMounted())
  842. getObjectMount()->disableCollision();
  843. // Cast the ray into the container database to see if we're going
  844. // to hit anything.
  845. RayInfo collision;
  846. Point3F ep = sp + vec + offset;
  847. if (mContainer->castRay(sp, ep,
  848. ~(WaterObjectType | GameBaseObjectType | DefaultObjectType | sTriggerMask),
  849. &collision) == true) {
  850. // Shift the collision point back a little to try and
  851. // avoid clipping against the front camera plane.
  852. F32 t = collision.t - (-mDot(vec, collision.normal) / vec.len()) * 0.1;
  853. if (t > 0.0f)
  854. ep = sp + offset + (vec * t);
  855. else
  856. eye.getColumn(3,&ep);
  857. }
  858. mat->setColumn(3,ep);
  859. // Re-enable our collision.
  860. if (isMounted())
  861. getObjectMount()->enableCollision();
  862. enableCollision();
  863. // Apply Camera FX.
  864. mat->mul( gCamFXMgr.getTrans() );
  865. }
  866. //----------------------------------------------------------------------------
  867. void TurretShape::writePacketData(GameConnection *connection, BitStream *stream)
  868. {
  869. // Update client regardless of status flags.
  870. Parent::writePacketData(connection, stream);
  871. stream->writeSignedFloat(mRot.x / M_2PI_F, 7);
  872. stream->writeSignedFloat(mRot.z / M_2PI_F, 7);
  873. }
  874. void TurretShape::readPacketData(GameConnection *connection, BitStream *stream)
  875. {
  876. Parent::readPacketData(connection, stream);
  877. Point3F rot(0.0f, 0.0f, 0.0f);
  878. rot.x = stream->readSignedFloat(7) * M_2PI_F;
  879. rot.z = stream->readSignedFloat(7) * M_2PI_F;
  880. _setRotation(rot);
  881. mTurretDelta.rot = rot;
  882. mTurretDelta.rotVec.set(0.0f, 0.0f, 0.0f);
  883. }
  884. U32 TurretShape::packUpdate(NetConnection *connection, U32 mask, BitStream *stream)
  885. {
  886. // Handle rotation ourselves (so it is not locked to the Z axis like for Items)
  887. U32 retMask = Parent::packUpdate( connection, mask & (~Item::RotationMask), stream );
  888. if (stream->writeFlag(mask & InitialUpdateMask)) {
  889. stream->writeFlag(mRespawn);
  890. }
  891. if ( stream->writeFlag( mask & Item::RotationMask ) )
  892. {
  893. QuatF rot( mObjToWorld );
  894. mathWrite( *stream, rot );
  895. }
  896. // The rest of the data is part of the control object packet update.
  897. // If we're controlled by this client, we don't need to send it.
  898. if(stream->writeFlag((NetConnection*)getControllingClient() == connection && !(mask & InitialUpdateMask)))
  899. return 0;
  900. if (stream->writeFlag(mask & TurretUpdateMask))
  901. {
  902. stream->writeSignedFloat(mRot.x / M_2PI_F, 7);
  903. stream->writeSignedFloat(mRot.z / M_2PI_F, 7);
  904. stream->write(allowManualRotation);
  905. stream->write(allowManualFire);
  906. }
  907. return retMask;
  908. }
  909. void TurretShape::unpackUpdate(NetConnection *connection, BitStream *stream)
  910. {
  911. Parent::unpackUpdate(connection,stream);
  912. // InitialUpdateMask
  913. if (stream->readFlag()) {
  914. mRespawn = stream->readFlag();
  915. }
  916. // Item::RotationMask
  917. if ( stream->readFlag() )
  918. {
  919. QuatF rot;
  920. mathRead( *stream, &rot );
  921. Point3F pos = mObjToWorld.getPosition();
  922. rot.setMatrix( &mObjToWorld );
  923. mObjToWorld.setPosition( pos );
  924. }
  925. // controlled by the client?
  926. if(stream->readFlag())
  927. return;
  928. // TurretUpdateMask
  929. if (stream->readFlag())
  930. {
  931. Point3F rot(0.0f, 0.0f, 0.0f);
  932. rot.x = stream->readSignedFloat(7) * M_2PI_F;
  933. rot.z = stream->readSignedFloat(7) * M_2PI_F;
  934. _setRotation(rot);
  935. // New delta for client side interpolation
  936. mTurretDelta.rot = rot;
  937. mTurretDelta.rotVec = VectorF(0.0f, 0.0f, 0.0f);
  938. stream->read(&allowManualRotation);
  939. stream->read(&allowManualFire);
  940. }
  941. }
  942. //----------------------------------------------------------------------------
  943. void TurretShape::getWeaponMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat )
  944. {
  945. // Returns mount point to world space transform
  946. if ( index >= 0 && index < ShapeBase::MaxMountedImages) {
  947. S32 ni = mDataBlock->weaponMountNode[index];
  948. if (ni != -1) {
  949. MatrixF mountTransform = mShapeInstance->mNodeTransforms[ni];
  950. mountTransform.mul( xfm );
  951. const Point3F& scale = getScale();
  952. // The position of the mount point needs to be scaled.
  953. Point3F position = mountTransform.getPosition();
  954. position.convolve( scale );
  955. mountTransform.setPosition( position );
  956. // Also we would like the object to be scaled to the model.
  957. outMat->mul(mObjToWorld, mountTransform);
  958. return;
  959. }
  960. }
  961. // Then let SceneObject handle it.
  962. GrandParent::getMountTransform( index, xfm, outMat );
  963. }
  964. void TurretShape::getRenderWeaponMountTransform( F32 delta, S32 mountPoint, const MatrixF &xfm, MatrixF *outMat )
  965. {
  966. // Returns mount point to world space transform
  967. if ( mountPoint >= 0 && mountPoint < ShapeBase::MaxMountedImages) {
  968. S32 ni = mDataBlock->weaponMountNode[mountPoint];
  969. if (ni != -1) {
  970. MatrixF mountTransform = mShapeInstance->mNodeTransforms[ni];
  971. mountTransform.mul( xfm );
  972. const Point3F& scale = getScale();
  973. // The position of the mount point needs to be scaled.
  974. Point3F position = mountTransform.getPosition();
  975. position.convolve( scale );
  976. mountTransform.setPosition( position );
  977. // Also we would like the object to be scaled to the model.
  978. mountTransform.scale( scale );
  979. outMat->mul(getRenderTransform(), mountTransform);
  980. return;
  981. }
  982. }
  983. // Then let SceneObject handle it.
  984. GrandParent::getRenderMountTransform( delta, mountPoint, xfm, outMat );
  985. }
  986. void TurretShape::getImageTransform(U32 imageSlot,MatrixF* mat)
  987. {
  988. // Image transform in world space
  989. MountedImage& image = mMountedImageList[imageSlot];
  990. if (image.dataBlock) {
  991. ShapeBaseImageData& data = *image.dataBlock;
  992. MatrixF nmat;
  993. if (data.useEyeOffset && isFirstPerson()) {
  994. getEyeTransform(&nmat);
  995. mat->mul(nmat,data.eyeOffset);
  996. }
  997. else {
  998. getWeaponMountTransform( imageSlot, MatrixF::Identity, &nmat );
  999. mat->mul(nmat,data.mountTransform[getImageShapeIndex(image)]);
  1000. }
  1001. }
  1002. else
  1003. *mat = mObjToWorld;
  1004. }
  1005. void TurretShape::getRenderImageTransform( U32 imageSlot, MatrixF* mat, bool noEyeOffset )
  1006. {
  1007. // Image transform in world space
  1008. MountedImage& image = mMountedImageList[imageSlot];
  1009. if (image.dataBlock)
  1010. {
  1011. ShapeBaseImageData& data = *image.dataBlock;
  1012. MatrixF nmat;
  1013. if ( !noEyeOffset && data.useEyeOffset && isFirstPerson() )
  1014. {
  1015. getRenderEyeTransform(&nmat);
  1016. mat->mul(nmat,data.eyeOffset);
  1017. }
  1018. else
  1019. {
  1020. getRenderWeaponMountTransform( 0.0f, imageSlot, MatrixF::Identity, &nmat );
  1021. mat->mul(nmat,data.mountTransform[getImageShapeIndex(image)]);
  1022. }
  1023. }
  1024. else
  1025. *mat = getRenderTransform();
  1026. }
  1027. void TurretShape::getImageTransform(U32 imageSlot,S32 node,MatrixF* mat)
  1028. {
  1029. // Same as ShapeBase::getImageTransform() other than getRenderWeaponMountTransform() below
  1030. // Image transform in world space
  1031. MountedImage& image = mMountedImageList[imageSlot];
  1032. if (image.dataBlock)
  1033. {
  1034. if (node != -1)
  1035. {
  1036. ShapeBaseImageData& data = *image.dataBlock;
  1037. U32 shapeIndex = getImageShapeIndex(image);
  1038. MatrixF nmat = image.shapeInstance[shapeIndex]->mNodeTransforms[node];
  1039. MatrixF mmat;
  1040. if (data.useEyeNode && isFirstPerson() && data.eyeMountNode[shapeIndex] != -1)
  1041. {
  1042. // We need to animate, even on the server, to make sure the nodes are in the correct location.
  1043. image.shapeInstance[shapeIndex]->animate();
  1044. MatrixF emat;
  1045. getEyeBaseTransform(&emat, mDataBlock->mountedImagesBank);
  1046. MatrixF mountTransform = image.shapeInstance[shapeIndex]->mNodeTransforms[data.eyeMountNode[shapeIndex]];
  1047. mountTransform.affineInverse();
  1048. mmat.mul(emat, mountTransform);
  1049. }
  1050. else if (data.useEyeOffset && isFirstPerson())
  1051. {
  1052. MatrixF emat;
  1053. getEyeTransform(&emat);
  1054. mmat.mul(emat,data.eyeOffset);
  1055. }
  1056. else
  1057. {
  1058. MatrixF emat;
  1059. getWeaponMountTransform( imageSlot, MatrixF::Identity, &emat );
  1060. mmat.mul(emat,data.mountTransform[shapeIndex]);
  1061. }
  1062. mat->mul(mmat, nmat);
  1063. }
  1064. else
  1065. getImageTransform(imageSlot,mat);
  1066. }
  1067. else
  1068. *mat = mObjToWorld;
  1069. }
  1070. void TurretShape::getRenderImageTransform(U32 imageSlot,S32 node,MatrixF* mat)
  1071. {
  1072. // Same as ShapeBase::getRenderImageTransform() other than getRenderWeaponMountTransform() below
  1073. // Image transform in world space
  1074. MountedImage& image = mMountedImageList[imageSlot];
  1075. if (image.dataBlock)
  1076. {
  1077. if (node != -1)
  1078. {
  1079. ShapeBaseImageData& data = *image.dataBlock;
  1080. U32 shapeIndex = getImageShapeIndex(image);
  1081. MatrixF nmat = image.shapeInstance[shapeIndex]->mNodeTransforms[node];
  1082. MatrixF mmat;
  1083. if ( data.useEyeNode && isFirstPerson() && data.eyeMountNode[shapeIndex] != -1 )
  1084. {
  1085. MatrixF emat;
  1086. getRenderEyeBaseTransform(&emat, mDataBlock->mountedImagesBank);
  1087. MatrixF mountTransform = image.shapeInstance[shapeIndex]->mNodeTransforms[data.eyeMountNode[shapeIndex]];
  1088. mountTransform.affineInverse();
  1089. mmat.mul(emat, mountTransform);
  1090. }
  1091. else if ( data.useEyeOffset && isFirstPerson() )
  1092. {
  1093. MatrixF emat;
  1094. getRenderEyeTransform(&emat);
  1095. mmat.mul(emat,data.eyeOffset);
  1096. }
  1097. else
  1098. {
  1099. MatrixF emat;
  1100. getRenderWeaponMountTransform( 0.0f, imageSlot, MatrixF::Identity, &emat );
  1101. mmat.mul(emat,data.mountTransform[shapeIndex]);
  1102. }
  1103. mat->mul(mmat, nmat);
  1104. }
  1105. else
  1106. getRenderImageTransform(imageSlot,mat);
  1107. }
  1108. else
  1109. *mat = getRenderTransform();
  1110. }
  1111. //----------------------------------------------------------------------------
  1112. void TurretShape::prepRenderImage( SceneRenderState *state )
  1113. {
  1114. // Skip the Item class rendering
  1115. _prepRenderImage( state, true, true );
  1116. }
  1117. void TurretShape::prepBatchRender( SceneRenderState *state, S32 mountedImageIndex )
  1118. {
  1119. Parent::prepBatchRender( state, mountedImageIndex );
  1120. if ( !gShowBoundingBox )
  1121. return;
  1122. //if ( mountedImageIndex != -1 )
  1123. //{
  1124. // ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1125. // ri->renderDelegate.bind( this, &Vehicle::_renderMuzzleVector );
  1126. // ri->objectIndex = mountedImageIndex;
  1127. // ri->type = RenderPassManager::RIT_Editor;
  1128. // state->getRenderPass()->addInst( ri );
  1129. // return;
  1130. //}
  1131. //ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1132. //ri->renderDelegate.bind( this, &Vehicle::_renderMassAndContacts );
  1133. //ri->type = RenderPassManager::RIT_Editor;
  1134. //state->getRenderPass()->addInst( ri );
  1135. }
  1136. //----------------------------------------------------------------------------
  1137. DefineEngineMethod( TurretShape, getAllowManualRotation, bool, (),,
  1138. "@brief Get if the turret is allowed to rotate through moves.\n\n"
  1139. "@return True if the turret is allowed to rotate through moves.\n" )
  1140. {
  1141. return object->getAllowManualRotation();
  1142. }
  1143. DefineEngineMethod( TurretShape, setAllowManualRotation, void, (bool allow),,
  1144. "@brief Set if the turret is allowed to rotate through moves.\n\n"
  1145. "@param allow If true then the turret may be rotated through moves.\n")
  1146. {
  1147. return object->setAllowManualRotation(allow);
  1148. }
  1149. DefineEngineMethod( TurretShape, getAllowManualFire, bool, (),,
  1150. "@brief Get if the turret is allowed to fire through moves.\n\n"
  1151. "@return True if the turret is allowed to fire through moves.\n" )
  1152. {
  1153. return object->getAllowManualFire();
  1154. }
  1155. DefineEngineMethod( TurretShape, setAllowManualFire, void, (bool allow),,
  1156. "@brief Set if the turret is allowed to fire through moves.\n\n"
  1157. "@param allow If true then the turret may be fired through moves.\n")
  1158. {
  1159. return object->setAllowManualFire(allow);
  1160. }
  1161. DefineEngineMethod( TurretShape, getState, const char*, (),,
  1162. "@brief Get the name of the turret's current state.\n\n"
  1163. "The state is one of the following:\n\n<ul>"
  1164. "<li>Dead - The TurretShape is destroyed.</li>"
  1165. "<li>Mounted - The TurretShape is mounted to an object such as a vehicle.</li>"
  1166. "<li>Ready - The TurretShape is free to move. The usual state.</li></ul>\n"
  1167. "@return The current state; one of: \"Dead\", \"Mounted\", \"Ready\"\n" )
  1168. {
  1169. return object->getStateName();
  1170. }
  1171. DefineEngineMethod( TurretShape, getTurretEulerRotation, Point3F, (),,
  1172. "@brief Get Euler rotation of this turret's heading and pitch nodes.\n\n"
  1173. "@return the orientation of the turret's heading and pitch nodes in the "
  1174. "form of rotations around the X, Y and Z axes in degrees.\n" )
  1175. {
  1176. Point3F euler = object->getTurretRotation();
  1177. // Convert to degrees.
  1178. euler.x = mRadToDeg( euler.x );
  1179. euler.y = mRadToDeg( euler.y );
  1180. euler.z = mRadToDeg( euler.z );
  1181. return euler;
  1182. }
  1183. DefineEngineMethod( TurretShape, setTurretEulerRotation, void, ( Point3F rot ),,
  1184. "@brief Set Euler rotation of this turret's heading and pitch nodes in degrees.\n\n"
  1185. "@param rot The rotation in degrees. The pitch is the X component and the "
  1186. "heading is the Z component. The Y component is ignored.\n")
  1187. {
  1188. object->setTurretRotation( rot );
  1189. }
  1190. DefineEngineMethod( TurretShape, doRespawn, bool, (),,
  1191. "@brief Does the turret respawn after it has been destroyed.\n\n"
  1192. "@returns True if the turret respawns.\n")
  1193. {
  1194. return object->doRespawn();
  1195. }