turretShape.cpp 42 KB

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