turretShape.cpp 42 KB

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