turretShape.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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. updateMove(move);
  519. }
  520. void TurretShape::interpolateTick(F32 dt)
  521. {
  522. Parent::interpolateTick(dt);
  523. if (isMounted()) {
  524. MatrixF mat;
  525. mMount.object->getRenderMountTransform( dt, mMount.node, mMount.xfm, &mat );
  526. ShapeBase::setRenderTransform(mat);
  527. }
  528. // Orientation
  529. Point3F rot = mTurretDelta.rot + mTurretDelta.rotVec * dt;
  530. // Make sure we don't interpolate past the limits
  531. _applyLimits(rot);
  532. _setRotation(rot);
  533. }
  534. void TurretShape::advanceTime(F32 dt)
  535. {
  536. // If there were any ShapeBase script threads that
  537. // have played, then we need to update all code
  538. // controlled nodes. This is done before the Parent
  539. // call as script threads may play and be destroyed
  540. // before our code is called.
  541. bool updateNodes = false;
  542. for (U32 i = 0; i < MaxScriptThreads; i++)
  543. {
  544. Thread& st = mScriptThread[i];
  545. if (st.thread)
  546. {
  547. updateNodes = true;
  548. break;
  549. }
  550. }
  551. Parent::advanceTime(dt);
  552. updateAnimation(dt);
  553. _setRotation(mRot);
  554. }
  555. void TurretShape::setTransform( const MatrixF& mat )
  556. {
  557. if (mDataBlock && mDataBlock->zRotOnly)
  558. {
  559. // Allow Item::setTransform() to do the work
  560. Parent::setTransform( mat );
  561. }
  562. else
  563. {
  564. // Do the transform work here to avoid Item's restriction on rotation
  565. ShapeBase::setTransform( mat );
  566. if ( !mStatic )
  567. {
  568. mAtRest = false;
  569. mAtRestCounter = 0;
  570. }
  571. if ( mPhysicsRep )
  572. mPhysicsRep->setTransform( getTransform() );
  573. setMaskBits( Item::RotationMask | Item::PositionMask | Item::NoWarpMask );
  574. }
  575. }
  576. void TurretShape::updateMove(const Move* move)
  577. {
  578. PROFILE_SCOPE( TurretShape_UpdateMove );
  579. if (!move)
  580. return;
  581. Point3F vec, pos;
  582. // Update orientation
  583. mTurretDelta.rotVec = mRot;
  584. VectorF rotVec(0, 0, 0);
  585. if (getAllowManualRotation())
  586. {
  587. if (mPitchAllowed)
  588. {
  589. rotVec.x = move->pitch * 2.0f; // Assume that our -2PI to 2PI range was clamped to -PI to PI in script;
  590. if (mPitchRate > 0)
  591. {
  592. rotVec.x *= mPitchRate * TickSec;
  593. }
  594. }
  595. if (mHeadingAllowed)
  596. {
  597. rotVec.z = move->yaw * 2.0f; // Assume that our -2PI to 2PI range was clamped to -PI to PI in script
  598. if (mHeadingRate > 0)
  599. {
  600. rotVec.z *= mHeadingRate * TickSec;
  601. }
  602. }
  603. }
  604. mRot.x += rotVec.x;
  605. mRot.z += rotVec.z;
  606. _applyLimits(mRot);
  607. if (isServerObject())
  608. {
  609. // As this ends up animating shape nodes, we have no sense of a transform and
  610. // render transform. Therefore we treat this as the true transform and leave the
  611. // client shape node changes to interpolateTick() as the render transform. Otherwise
  612. // on the client we'll have this node change from processTick() and then backstepping
  613. // and catching up to the true node change in interpolateTick(), which causes the
  614. // turret to stutter.
  615. _setRotation( mRot );
  616. }
  617. else
  618. {
  619. // If on the client, calc delta for backstepping
  620. mTurretDelta.rot = mRot;
  621. mTurretDelta.rotVec = mTurretDelta.rotVec - mTurretDelta.rot;
  622. }
  623. setMaskBits(TurretUpdateMask);
  624. }
  625. bool TurretShape::getNodeTransform(S32 node, MatrixF& mat)
  626. {
  627. if (node == -1)
  628. return false;
  629. MatrixF nodeTransform = mShapeInstance->mNodeTransforms[node];
  630. const Point3F& scale = getScale();
  631. // The position of the node needs to be scaled.
  632. Point3F position = nodeTransform.getPosition();
  633. position.convolve( scale );
  634. nodeTransform.setPosition( position );
  635. mat.mul(mObjToWorld, nodeTransform);
  636. return true;
  637. }
  638. bool TurretShape::getWorldNodeTransform(S32 node, MatrixF& mat)
  639. {
  640. MatrixF nodeMat;
  641. if (!getNodeTransform(node, nodeMat))
  642. return false;
  643. nodeMat.affineInverse();
  644. mat = nodeMat;
  645. return true;
  646. }
  647. void TurretShape::_setRotation(const Point3F& rot)
  648. {
  649. _updateNodes(rot);
  650. mShapeInstance->animate();
  651. mRot = rot;
  652. }
  653. void TurretShape::_updateNodes(const Point3F& rot)
  654. {
  655. EulerF xRot(rot.x, 0.0f, 0.0f);
  656. EulerF zRot(0.0f, 0.0f, rot.z);
  657. // Set heading
  658. S32 node = mDataBlock->headingNode;
  659. if (node != -1)
  660. {
  661. MatrixF* mat = &mShapeInstance->mNodeTransforms[node];
  662. Point3F defaultPos = mShapeInstance->getShape()->defaultTranslations[node];
  663. Quat16 defaultRot = mShapeInstance->getShape()->defaultRotations[node];
  664. QuatF qrot(zRot);
  665. qrot *= defaultRot.getQuatF();
  666. qrot.setMatrix( mat );
  667. mat->setColumn(3, defaultPos);
  668. }
  669. // Set pitch
  670. node = mDataBlock->pitchNode;
  671. if (node != -1)
  672. {
  673. MatrixF* mat = &mShapeInstance->mNodeTransforms[node];
  674. Point3F defaultPos = mShapeInstance->getShape()->defaultTranslations[node];
  675. Quat16 defaultRot = mShapeInstance->getShape()->defaultRotations[node];
  676. QuatF qrot(xRot);
  677. qrot *= defaultRot.getQuatF();
  678. qrot.setMatrix( mat );
  679. mat->setColumn(3, defaultPos);
  680. }
  681. // Now the mirror direction nodes, if any
  682. for (U32 i=0; i<TurretShapeData::NumMirrorDirectionNodes; ++i)
  683. {
  684. node = mDataBlock->pitchNodes[i];
  685. if (node != -1)
  686. {
  687. MatrixF* mat = &mShapeInstance->mNodeTransforms[node];
  688. Point3F defaultPos = mShapeInstance->getShape()->defaultTranslations[node];
  689. Quat16 defaultRot = mShapeInstance->getShape()->defaultRotations[node];
  690. QuatF qrot(xRot);
  691. qrot *= defaultRot.getQuatF();
  692. qrot.setMatrix( mat );
  693. mat->setColumn(3, defaultPos);
  694. }
  695. node = mDataBlock->headingNodes[i];
  696. if (node != -1)
  697. {
  698. MatrixF* mat = &mShapeInstance->mNodeTransforms[node];
  699. Point3F defaultPos = mShapeInstance->getShape()->defaultTranslations[node];
  700. Quat16 defaultRot = mShapeInstance->getShape()->defaultRotations[node];
  701. QuatF qrot(zRot);
  702. qrot *= defaultRot.getQuatF();
  703. qrot.setMatrix( mat );
  704. mat->setColumn(3, defaultPos);
  705. }
  706. }
  707. mShapeInstance->setDirty(TSShapeInstance::TransformDirty);
  708. }
  709. void TurretShape::_applyLimits(Point3F& rot)
  710. {
  711. rot.x = mClampF(rot.x, mPitchUp, mPitchDown);
  712. if (mHeadingMax < mDegToRad(180.0f))
  713. {
  714. rot.z = mClampF(rot.z, -mHeadingMax, mHeadingMax);
  715. }
  716. }
  717. bool TurretShape::_outsideLimits(Point3F& rot)
  718. {
  719. if (rot.x < mPitchUp || rot.x > mPitchDown)
  720. return true;
  721. if (mHeadingMax < mDegToRad(180.0f))
  722. {
  723. if (rot.z < -mHeadingMax || rot.z > mHeadingMax)
  724. return true;
  725. }
  726. return false;
  727. }
  728. //----------------------------------------------------------------------------
  729. void TurretShape::mountObject( SceneObject *obj, S32 node, const MatrixF &xfm )
  730. {
  731. Parent::mountObject(obj, node, xfm);
  732. if (isClientObject())
  733. {
  734. if (obj)
  735. {
  736. GameConnection* conn = obj->getControllingClient();
  737. if (conn)
  738. {
  739. // Allow the client to set up any action maps, HUD, etc.
  740. Con::executef("turretMountCallback", Con::getIntArg(getId()), Con::getIntArg(obj->getId()), Con::getIntArg(true));
  741. }
  742. }
  743. }
  744. else
  745. {
  746. mDataBlock->onMountObject_callback( this, obj, node );
  747. }
  748. }
  749. void TurretShape::unmountObject( SceneObject *obj )
  750. {
  751. Parent::unmountObject(obj);
  752. if (isClientObject())
  753. {
  754. if (obj)
  755. {
  756. GameConnection* conn = obj->getControllingClient();
  757. if (conn)
  758. {
  759. // Allow the client to set up any action maps, HUD, etc.
  760. Con::executef("turretMountCallback", Con::getIntArg(getId()), Con::getIntArg(obj->getId()), Con::getIntArg(false));
  761. }
  762. }
  763. }
  764. else
  765. {
  766. mDataBlock->onUnmountObject_callback( this, obj );
  767. }
  768. }
  769. void TurretShape::onUnmount(ShapeBase*,S32)
  770. {
  771. // Make sure the client get's the final server pos of this turret.
  772. setMaskBits(PositionMask);
  773. }
  774. //----------------------------------------------------------------------------
  775. void TurretShape::getCameraParameters(F32 *min,F32* max,Point3F* off,MatrixF* rot)
  776. {
  777. *min = mDataBlock->cameraMinDist;
  778. *max = mDataBlock->cameraMaxDist;
  779. off->set(0,0,mDataBlock->cameraOffset);
  780. rot->identity();
  781. }
  782. void TurretShape::getCameraTransform(F32* pos,MatrixF* mat)
  783. {
  784. // Returns camera to world space transform
  785. // Handles first person / third person camera position
  786. if (isServerObject() && mShapeInstance)
  787. mShapeInstance->animateNodeSubtrees(true);
  788. if (*pos == 0) {
  789. getRenderEyeTransform(mat);
  790. return;
  791. }
  792. // Get the shape's camera parameters.
  793. F32 min,max;
  794. MatrixF rot;
  795. Point3F offset;
  796. getCameraParameters(&min,&max,&offset,&rot);
  797. // Start with the current eye position
  798. MatrixF eye;
  799. getRenderEyeTransform(&eye);
  800. // Build a transform that points along the eye axis
  801. // but where the Z axis is always up.
  802. {
  803. MatrixF cam(1);
  804. VectorF x,y,z(0,0,1);
  805. eye.getColumn(1, &y);
  806. mCross(y, z, &x);
  807. x.normalize();
  808. mCross(x, y, &z);
  809. z.normalize();
  810. cam.setColumn(0,x);
  811. cam.setColumn(1,y);
  812. cam.setColumn(2,z);
  813. mat->mul(cam,rot);
  814. }
  815. // Camera is positioned straight back along the eye's -Y axis.
  816. // A ray is cast to make sure the camera doesn't go through
  817. // anything solid.
  818. VectorF vp,vec;
  819. vp.x = vp.z = 0;
  820. vp.y = -(max - min) * *pos;
  821. eye.mulV(vp,&vec);
  822. // Use the camera node as the starting position if it exists.
  823. Point3F osp,sp;
  824. if (mDataBlock->cameraNode != -1)
  825. {
  826. mShapeInstance->mNodeTransforms[mDataBlock->cameraNode].getColumn(3,&osp);
  827. getRenderTransform().mulP(osp,&sp);
  828. }
  829. else
  830. eye.getColumn(3,&sp);
  831. // Make sure we don't hit ourself...
  832. disableCollision();
  833. if (isMounted())
  834. getObjectMount()->disableCollision();
  835. // Cast the ray into the container database to see if we're going
  836. // to hit anything.
  837. RayInfo collision;
  838. Point3F ep = sp + vec + offset;
  839. if (mContainer->castRay(sp, ep,
  840. ~(WaterObjectType | GameBaseObjectType | DefaultObjectType | sTriggerMask),
  841. &collision) == true) {
  842. // Shift the collision point back a little to try and
  843. // avoid clipping against the front camera plane.
  844. F32 t = collision.t - (-mDot(vec, collision.normal) / vec.len()) * 0.1;
  845. if (t > 0.0f)
  846. ep = sp + offset + (vec * t);
  847. else
  848. eye.getColumn(3,&ep);
  849. }
  850. mat->setColumn(3,ep);
  851. // Re-enable our collision.
  852. if (isMounted())
  853. getObjectMount()->enableCollision();
  854. enableCollision();
  855. // Apply Camera FX.
  856. mat->mul( gCamFXMgr.getTrans() );
  857. }
  858. //----------------------------------------------------------------------------
  859. void TurretShape::writePacketData(GameConnection *connection, BitStream *stream)
  860. {
  861. // Update client regardless of status flags.
  862. Parent::writePacketData(connection, stream);
  863. stream->writeSignedFloat(mRot.x / M_2PI_F, 7);
  864. stream->writeSignedFloat(mRot.z / M_2PI_F, 7);
  865. }
  866. void TurretShape::readPacketData(GameConnection *connection, BitStream *stream)
  867. {
  868. Parent::readPacketData(connection, stream);
  869. Point3F rot(0.0f, 0.0f, 0.0f);
  870. rot.x = stream->readSignedFloat(7) * M_2PI_F;
  871. rot.z = stream->readSignedFloat(7) * M_2PI_F;
  872. _setRotation(rot);
  873. mTurretDelta.rot = rot;
  874. mTurretDelta.rotVec.set(0.0f, 0.0f, 0.0f);
  875. }
  876. U32 TurretShape::packUpdate(NetConnection *connection, U32 mask, BitStream *stream)
  877. {
  878. // Handle rotation ourselves (so it is not locked to the Z axis like for Items)
  879. U32 retMask = Parent::packUpdate( connection, mask & (~Item::RotationMask), stream );
  880. if (stream->writeFlag(mask & InitialUpdateMask)) {
  881. stream->writeFlag(mRespawn);
  882. }
  883. if ( stream->writeFlag( mask & Item::RotationMask ) )
  884. {
  885. QuatF rot( mObjToWorld );
  886. mathWrite( *stream, rot );
  887. }
  888. // The rest of the data is part of the control object packet update.
  889. // If we're controlled by this client, we don't need to send it.
  890. if(stream->writeFlag((NetConnection*)getControllingClient() == connection && !(mask & InitialUpdateMask)))
  891. return 0;
  892. if (stream->writeFlag(mask & TurretUpdateMask))
  893. {
  894. stream->writeSignedFloat(mRot.x / M_2PI_F, 7);
  895. stream->writeSignedFloat(mRot.z / M_2PI_F, 7);
  896. stream->write(allowManualRotation);
  897. stream->write(allowManualFire);
  898. }
  899. return retMask;
  900. }
  901. void TurretShape::unpackUpdate(NetConnection *connection, BitStream *stream)
  902. {
  903. Parent::unpackUpdate(connection,stream);
  904. // InitialUpdateMask
  905. if (stream->readFlag()) {
  906. mRespawn = stream->readFlag();
  907. }
  908. // Item::RotationMask
  909. if ( stream->readFlag() )
  910. {
  911. QuatF rot;
  912. mathRead( *stream, &rot );
  913. Point3F pos = mObjToWorld.getPosition();
  914. rot.setMatrix( &mObjToWorld );
  915. mObjToWorld.setPosition( pos );
  916. }
  917. // controlled by the client?
  918. if(stream->readFlag())
  919. return;
  920. // TurretUpdateMask
  921. if (stream->readFlag())
  922. {
  923. Point3F rot(0.0f, 0.0f, 0.0f);
  924. rot.x = stream->readSignedFloat(7) * M_2PI_F;
  925. rot.z = stream->readSignedFloat(7) * M_2PI_F;
  926. _setRotation(rot);
  927. // New delta for client side interpolation
  928. mTurretDelta.rot = rot;
  929. mTurretDelta.rotVec = VectorF(0.0f, 0.0f, 0.0f);
  930. stream->read(&allowManualRotation);
  931. stream->read(&allowManualFire);
  932. }
  933. }
  934. //----------------------------------------------------------------------------
  935. void TurretShape::getWeaponMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat )
  936. {
  937. // Returns mount point to world space transform
  938. if ( index >= 0 && index < ShapeBase::MaxMountedImages) {
  939. S32 ni = mDataBlock->weaponMountNode[index];
  940. if (ni != -1) {
  941. MatrixF mountTransform = mShapeInstance->mNodeTransforms[ni];
  942. mountTransform.mul( xfm );
  943. const Point3F& scale = getScale();
  944. // The position of the mount point needs to be scaled.
  945. Point3F position = mountTransform.getPosition();
  946. position.convolve( scale );
  947. mountTransform.setPosition( position );
  948. // Also we would like the object to be scaled to the model.
  949. outMat->mul(mObjToWorld, mountTransform);
  950. return;
  951. }
  952. }
  953. // Then let SceneObject handle it.
  954. GrandParent::getMountTransform( index, xfm, outMat );
  955. }
  956. void TurretShape::getRenderWeaponMountTransform( F32 delta, S32 mountPoint, const MatrixF &xfm, MatrixF *outMat )
  957. {
  958. // Returns mount point to world space transform
  959. if ( mountPoint >= 0 && mountPoint < ShapeBase::MaxMountedImages) {
  960. S32 ni = mDataBlock->weaponMountNode[mountPoint];
  961. if (ni != -1) {
  962. MatrixF mountTransform = mShapeInstance->mNodeTransforms[ni];
  963. mountTransform.mul( xfm );
  964. const Point3F& scale = getScale();
  965. // The position of the mount point needs to be scaled.
  966. Point3F position = mountTransform.getPosition();
  967. position.convolve( scale );
  968. mountTransform.setPosition( position );
  969. // Also we would like the object to be scaled to the model.
  970. mountTransform.scale( scale );
  971. outMat->mul(getRenderTransform(), mountTransform);
  972. return;
  973. }
  974. }
  975. // Then let SceneObject handle it.
  976. GrandParent::getRenderMountTransform( delta, mountPoint, xfm, outMat );
  977. }
  978. void TurretShape::getImageTransform(U32 imageSlot,MatrixF* mat)
  979. {
  980. // Image transform in world space
  981. MountedImage& image = mMountedImageList[imageSlot];
  982. if (image.dataBlock) {
  983. ShapeBaseImageData& data = *image.dataBlock;
  984. MatrixF nmat;
  985. if (data.useEyeOffset && isFirstPerson()) {
  986. getEyeTransform(&nmat);
  987. mat->mul(nmat,data.eyeOffset);
  988. }
  989. else {
  990. getWeaponMountTransform( imageSlot, MatrixF::Identity, &nmat );
  991. mat->mul(nmat,data.mountTransform[getImageShapeIndex(image)]);
  992. }
  993. }
  994. else
  995. *mat = mObjToWorld;
  996. }
  997. void TurretShape::getRenderImageTransform( U32 imageSlot, MatrixF* mat, bool noEyeOffset )
  998. {
  999. // Image transform in world space
  1000. MountedImage& image = mMountedImageList[imageSlot];
  1001. if (image.dataBlock)
  1002. {
  1003. ShapeBaseImageData& data = *image.dataBlock;
  1004. MatrixF nmat;
  1005. if ( !noEyeOffset && data.useEyeOffset && isFirstPerson() )
  1006. {
  1007. getRenderEyeTransform(&nmat);
  1008. mat->mul(nmat,data.eyeOffset);
  1009. }
  1010. else
  1011. {
  1012. getRenderWeaponMountTransform( 0.0f, imageSlot, MatrixF::Identity, &nmat );
  1013. mat->mul(nmat,data.mountTransform[getImageShapeIndex(image)]);
  1014. }
  1015. }
  1016. else
  1017. *mat = getRenderTransform();
  1018. }
  1019. void TurretShape::getImageTransform(U32 imageSlot,S32 node,MatrixF* mat)
  1020. {
  1021. // Same as ShapeBase::getImageTransform() other than getRenderWeaponMountTransform() below
  1022. // Image transform in world space
  1023. MountedImage& image = mMountedImageList[imageSlot];
  1024. if (image.dataBlock)
  1025. {
  1026. if (node != -1)
  1027. {
  1028. ShapeBaseImageData& data = *image.dataBlock;
  1029. U32 shapeIndex = getImageShapeIndex(image);
  1030. MatrixF nmat = image.shapeInstance[shapeIndex]->mNodeTransforms[node];
  1031. MatrixF mmat;
  1032. if (data.useEyeNode && isFirstPerson() && data.eyeMountNode[shapeIndex] != -1)
  1033. {
  1034. // We need to animate, even on the server, to make sure the nodes are in the correct location.
  1035. image.shapeInstance[shapeIndex]->animate();
  1036. MatrixF emat;
  1037. getEyeBaseTransform(&emat, mDataBlock->mountedImagesBank);
  1038. MatrixF mountTransform = image.shapeInstance[shapeIndex]->mNodeTransforms[data.eyeMountNode[shapeIndex]];
  1039. mountTransform.affineInverse();
  1040. mmat.mul(emat, mountTransform);
  1041. }
  1042. else if (data.useEyeOffset && isFirstPerson())
  1043. {
  1044. MatrixF emat;
  1045. getEyeTransform(&emat);
  1046. mmat.mul(emat,data.eyeOffset);
  1047. }
  1048. else
  1049. {
  1050. MatrixF emat;
  1051. getWeaponMountTransform( imageSlot, MatrixF::Identity, &emat );
  1052. mmat.mul(emat,data.mountTransform[shapeIndex]);
  1053. }
  1054. mat->mul(mmat, nmat);
  1055. }
  1056. else
  1057. getImageTransform(imageSlot,mat);
  1058. }
  1059. else
  1060. *mat = mObjToWorld;
  1061. }
  1062. void TurretShape::getRenderImageTransform(U32 imageSlot,S32 node,MatrixF* mat)
  1063. {
  1064. // Same as ShapeBase::getRenderImageTransform() other than getRenderWeaponMountTransform() below
  1065. // Image transform in world space
  1066. MountedImage& image = mMountedImageList[imageSlot];
  1067. if (image.dataBlock)
  1068. {
  1069. if (node != -1)
  1070. {
  1071. ShapeBaseImageData& data = *image.dataBlock;
  1072. U32 shapeIndex = getImageShapeIndex(image);
  1073. MatrixF nmat = image.shapeInstance[shapeIndex]->mNodeTransforms[node];
  1074. MatrixF mmat;
  1075. if ( data.useEyeNode && isFirstPerson() && data.eyeMountNode[shapeIndex] != -1 )
  1076. {
  1077. MatrixF emat;
  1078. getRenderEyeBaseTransform(&emat, mDataBlock->mountedImagesBank);
  1079. MatrixF mountTransform = image.shapeInstance[shapeIndex]->mNodeTransforms[data.eyeMountNode[shapeIndex]];
  1080. mountTransform.affineInverse();
  1081. mmat.mul(emat, mountTransform);
  1082. }
  1083. else if ( data.useEyeOffset && isFirstPerson() )
  1084. {
  1085. MatrixF emat;
  1086. getRenderEyeTransform(&emat);
  1087. mmat.mul(emat,data.eyeOffset);
  1088. }
  1089. else
  1090. {
  1091. MatrixF emat;
  1092. getRenderWeaponMountTransform( 0.0f, imageSlot, MatrixF::Identity, &emat );
  1093. mmat.mul(emat,data.mountTransform[shapeIndex]);
  1094. }
  1095. mat->mul(mmat, nmat);
  1096. }
  1097. else
  1098. getRenderImageTransform(imageSlot,mat);
  1099. }
  1100. else
  1101. *mat = getRenderTransform();
  1102. }
  1103. //----------------------------------------------------------------------------
  1104. void TurretShape::prepRenderImage( SceneRenderState *state )
  1105. {
  1106. // Skip the Item class rendering
  1107. _prepRenderImage( state, true, true );
  1108. }
  1109. void TurretShape::prepBatchRender( SceneRenderState *state, S32 mountedImageIndex )
  1110. {
  1111. Parent::prepBatchRender( state, mountedImageIndex );
  1112. if ( !gShowBoundingBox )
  1113. return;
  1114. //if ( mountedImageIndex != -1 )
  1115. //{
  1116. // ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1117. // ri->renderDelegate.bind( this, &Vehicle::_renderMuzzleVector );
  1118. // ri->objectIndex = mountedImageIndex;
  1119. // ri->type = RenderPassManager::RIT_Editor;
  1120. // state->getRenderPass()->addInst( ri );
  1121. // return;
  1122. //}
  1123. //ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1124. //ri->renderDelegate.bind( this, &Vehicle::_renderMassAndContacts );
  1125. //ri->type = RenderPassManager::RIT_Editor;
  1126. //state->getRenderPass()->addInst( ri );
  1127. }
  1128. //----------------------------------------------------------------------------
  1129. DefineEngineMethod( TurretShape, getAllowManualRotation, bool, (),,
  1130. "@brief Get if the turret is allowed to rotate through moves.\n\n"
  1131. "@return True if the turret is allowed to rotate through moves.\n" )
  1132. {
  1133. return object->getAllowManualRotation();
  1134. }
  1135. DefineEngineMethod( TurretShape, setAllowManualRotation, void, (bool allow),,
  1136. "@brief Set if the turret is allowed to rotate through moves.\n\n"
  1137. "@param allow If true then the turret may be rotated through moves.\n")
  1138. {
  1139. return object->setAllowManualRotation(allow);
  1140. }
  1141. DefineEngineMethod( TurretShape, getAllowManualFire, bool, (),,
  1142. "@brief Get if the turret is allowed to fire through moves.\n\n"
  1143. "@return True if the turret is allowed to fire through moves.\n" )
  1144. {
  1145. return object->getAllowManualFire();
  1146. }
  1147. DefineEngineMethod( TurretShape, setAllowManualFire, void, (bool allow),,
  1148. "@brief Set if the turret is allowed to fire through moves.\n\n"
  1149. "@param allow If true then the turret may be fired through moves.\n")
  1150. {
  1151. return object->setAllowManualFire(allow);
  1152. }
  1153. DefineEngineMethod( TurretShape, getState, const char*, (),,
  1154. "@brief Get the name of the turret's current state.\n\n"
  1155. "The state is one of the following:\n\n<ul>"
  1156. "<li>Dead - The TurretShape is destroyed.</li>"
  1157. "<li>Mounted - The TurretShape is mounted to an object such as a vehicle.</li>"
  1158. "<li>Ready - The TurretShape is free to move. The usual state.</li></ul>\n"
  1159. "@return The current state; one of: \"Dead\", \"Mounted\", \"Ready\"\n" )
  1160. {
  1161. return object->getStateName();
  1162. }
  1163. DefineEngineMethod( TurretShape, getTurretEulerRotation, Point3F, (),,
  1164. "@brief Get Euler rotation of this turret's heading and pitch nodes.\n\n"
  1165. "@return the orientation of the turret's heading and pitch nodes in the "
  1166. "form of rotations around the X, Y and Z axes in degrees.\n" )
  1167. {
  1168. Point3F euler = object->getTurretRotation();
  1169. // Convert to degrees.
  1170. euler.x = mRadToDeg( euler.x );
  1171. euler.y = mRadToDeg( euler.y );
  1172. euler.z = mRadToDeg( euler.z );
  1173. return euler;
  1174. }
  1175. DefineEngineMethod( TurretShape, setTurretEulerRotation, void, ( Point3F rot ),,
  1176. "@brief Set Euler rotation of this turret's heading and pitch nodes in degrees.\n\n"
  1177. "@param rot The rotation in degrees. The pitch is the X component and the "
  1178. "heading is the Z component. The Y component is ignored.\n")
  1179. {
  1180. object->setTurretRotation( rot );
  1181. }
  1182. DefineEngineMethod( TurretShape, doRespawn, bool, (),,
  1183. "@brief Does the turret respawn after it has been destroyed.\n\n"
  1184. "@returns True if the turret respawns.\n")
  1185. {
  1186. return object->doRespawn();
  1187. }