player.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #ifndef _PLAYER_H_
  27. #define _PLAYER_H_
  28. #ifndef _SHAPEBASE_H_
  29. #include "T3D/shapeBase.h"
  30. #endif
  31. #ifndef _BOXCONVEX_H_
  32. #include "collision/boxConvex.h"
  33. #endif
  34. #include "T3D/assets/SoundAsset.h"
  35. #include "T3D/gameBase/gameProcess.h"
  36. class Material;
  37. class ParticleEmitter;
  38. class ParticleEmitterData;
  39. class DecalData;
  40. class SplashData;
  41. class PhysicsPlayer;
  42. class Player;
  43. #ifdef TORQUE_OPENVR
  44. class OpenVRTrackedObject;
  45. #endif
  46. #ifdef TORQUE_NAVIGATION_ENABLED
  47. #include "navigation/navPath.h"
  48. #include "navigation/navMesh.h"
  49. #include "navigation/coverPoint.h"
  50. #endif // TORQUE_NAVIGATION_ENABLED
  51. //----------------------------------------------------------------------------
  52. struct PlayerData: public ShapeBaseData /*protected AssetPtrCallback < already in shapebasedata. */ {
  53. typedef ShapeBaseData Parent;
  54. enum Constants {
  55. RecoverDelayBits = 7,
  56. JumpDelayBits = 7,
  57. NumSpineNodes = 6,
  58. ImpactBits = 3,
  59. NUM_SPLASH_EMITTERS = 3,
  60. BUBBLE_EMITTER = 2,
  61. };
  62. bool renderFirstPerson; ///< Render the player shape in first person
  63. /// Render shadows while in first person when
  64. /// renderFirstPerson is disabled.
  65. bool firstPersonShadows;
  66. StringTableEntry imageAnimPrefix; ///< Passed along to mounted images to modify
  67. /// animation sequences played in third person. [optional]
  68. bool allowImageStateAnimation; ///< When true a new thread is added to the player to allow for
  69. /// mounted images to request a sequence be played on the player
  70. /// through the image's state machine. It is only optional so
  71. /// that we don't create a TSThread on the player if we don't
  72. /// need to.
  73. DECLARE_SHAPEASSET_ARRAY_REFACTOR(PlayerData, ShapeFP, ShapeBase::MaxMountedImages)
  74. StringTableEntry imageAnimPrefixFP; ///< Passed along to mounted images to modify
  75. /// animation sequences played in first person. [optional]
  76. U32 mCRCFP[ShapeBase::MaxMountedImages]; ///< Computed CRC values for the first person mounted image shapes
  77. /// Depends on the ShapeBaseData computeCRC field.
  78. bool mValidShapeFP[ShapeBase::MaxMountedImages]; ///< Indicates that there is a valid first person mounted image shape
  79. F32 pickupRadius; ///< Radius around player for items (on server)
  80. F32 maxTimeScale; ///< Max timeScale for action animations
  81. F32 minLookAngle; ///< Lowest angle (radians) the player can look
  82. F32 maxLookAngle; ///< Highest angle (radians) the player can look
  83. F32 maxFreelookAngle; ///< Max left/right angle the player can look
  84. /// @name Physics constants
  85. /// @{
  86. F32 maxStepHeight; ///< Maximum height the player can step up
  87. F32 runSurfaceAngle; ///< Maximum angle from vertical in degrees the player can run up
  88. F32 horizMaxSpeed; ///< Max speed attainable in the horizontal
  89. F32 horizResistSpeed; ///< Speed at which resistance will take place
  90. F32 horizResistFactor; ///< Factor of resistance once horizResistSpeed has been reached
  91. F32 upMaxSpeed; ///< Max vertical speed attainable
  92. F32 upResistSpeed; ///< Speed at which resistance will take place
  93. F32 upResistFactor; ///< Factor of resistance once upResistSpeed has been reached
  94. F32 fallingSpeedThreshold; ///< Downward speed at which we consider the player falling
  95. S32 recoverDelay; ///< # tick
  96. F32 recoverRunForceScale; ///< RunForce multiplier in recover state
  97. F32 landSequenceTime; ///< If greater than 0 then the legacy fall recovery system will be bypassed
  98. /// in favour of just playing the player's land sequence. The time to
  99. /// recover from a fall then becomes this parameter's time and the land
  100. /// sequence's playback will be scaled to match.
  101. bool transitionToLand; ///< When going from a fall to a land, should we transition between the two?
  102. // Running/Walking
  103. F32 runForce; ///< Force used to accelerate player
  104. F32 runEnergyDrain; ///< Energy drain/tick
  105. F32 minRunEnergy; ///< Minimum energy required to run
  106. F32 maxForwardSpeed; ///< Maximum forward speed when running
  107. F32 maxBackwardSpeed; ///< Maximum backward speed when running
  108. F32 maxSideSpeed; ///< Maximum side speed when running
  109. // Jumping
  110. F32 jumpForce; ///< Force exerted per jump
  111. F32 jumpEnergyDrain; ///< Energy drained per jump
  112. F32 minJumpEnergy; ///< Minimum energy required to jump
  113. F32 minJumpSpeed; ///< Minimum speed needed to jump
  114. F32 maxJumpSpeed; ///< Maximum speed before the player can no longer jump
  115. F32 jumpSurfaceAngle; ///< Angle from vertical in degrees where the player can jump
  116. S32 jumpDelay; ///< Delay time in ticks between jumps
  117. // Sprinting
  118. F32 sprintForce; ///< Force used to accelerate player
  119. F32 sprintEnergyDrain; ///< Energy drain/tick
  120. F32 minSprintEnergy; ///< Minimum energy required to sprint
  121. F32 maxSprintForwardSpeed; ///< Maximum forward speed when sprinting
  122. F32 maxSprintBackwardSpeed; ///< Maximum backward speed when sprinting
  123. F32 maxSprintSideSpeed; ///< Maximum side speed when sprinting
  124. F32 sprintStrafeScale; ///< Amount to scale strafing motion vector while sprinting
  125. F32 sprintYawScale; ///< Amount to scale yaw motion while sprinting
  126. F32 sprintPitchScale; ///< Amount to scale pitch motion while sprinting
  127. bool sprintCanJump; ///< Can the player jump while sprinting
  128. // Swimming
  129. F32 swimForce; ///< Force used to accelerate player while swimming
  130. F32 maxUnderwaterForwardSpeed; ///< Maximum underwater forward speed when running
  131. F32 maxUnderwaterBackwardSpeed; ///< Maximum underwater backward speed when running
  132. F32 maxUnderwaterSideSpeed; ///< Maximum underwater side speed when running
  133. // Crouching
  134. F32 crouchForce; ///< Force used to accelerate player while crouching
  135. F32 maxCrouchForwardSpeed; ///< Maximum forward speed when crouching
  136. F32 maxCrouchBackwardSpeed; ///< Maximum backward speed when crouching
  137. F32 maxCrouchSideSpeed; ///< Maximum side speed when crouching
  138. // Prone
  139. F32 proneForce; ///< Force used to accelerate player while prone
  140. F32 maxProneForwardSpeed; ///< Maximum forward speed when prone
  141. F32 maxProneBackwardSpeed; ///< Maximum backward speed when prone
  142. F32 maxProneSideSpeed; ///< Maximum side speed when prone
  143. // Jetting
  144. F32 jetJumpForce;
  145. F32 jetJumpEnergyDrain; ///< Energy per jump
  146. F32 jetMinJumpEnergy;
  147. F32 jetMinJumpSpeed;
  148. F32 jetMaxJumpSpeed;
  149. F32 jetJumpSurfaceAngle; ///< Angle vertical degrees
  150. /// @}
  151. /// @name Hitboxes
  152. /// @{
  153. F32 boxHeadPercentage;
  154. F32 boxTorsoPercentage;
  155. F32 boxHeadLeftPercentage;
  156. F32 boxHeadRightPercentage;
  157. F32 boxHeadBackPercentage;
  158. F32 boxHeadFrontPercentage;
  159. /// @}
  160. F32 minImpactSpeed; ///< Minimum impact speed required to apply fall damage
  161. F32 minLateralImpactSpeed; ///< Minimum impact speed required to apply non-falling damage.
  162. F32 decalOffset;
  163. F32 groundImpactMinSpeed; ///< Minimum impact speed required to apply fall damage with the ground
  164. VectorF groundImpactShakeFreq; ///< Frequency in each direction for the camera to shake
  165. VectorF groundImpactShakeAmp; ///< How much to shake
  166. F32 groundImpactShakeDuration; ///< How long to shake
  167. F32 groundImpactShakeFalloff; ///< How fast the shake disapates
  168. /// Zounds!
  169. enum Sounds {
  170. FootSoft,
  171. FootHard,
  172. FootMetal,
  173. FootSnow,
  174. WaterStart,
  175. FootShallowSplash,
  176. FootWading,
  177. FootUnderWater,
  178. FootBubbles,
  179. MoveBubbles,
  180. WaterBreath,
  181. ImpactStart,
  182. ImpactSoft,
  183. ImpactHard,
  184. ImpactMetal,
  185. ImpactSnow,
  186. ImpactWaterEasy,
  187. ImpactWaterMedium,
  188. ImpactWaterHard,
  189. ExitWater,
  190. MaxSounds
  191. };
  192. DECLARE_SOUNDASSET_ARRAY(PlayerData, PlayerSound, Sounds::MaxSounds);
  193. Point3F boxSize; ///< Width, depth, height
  194. Point3F crouchBoxSize;
  195. Point3F proneBoxSize;
  196. Point3F swimBoxSize;
  197. /// Animation and other data initialized in onAdd
  198. struct ActionAnimationDef {
  199. const char* name; ///< Sequence name
  200. struct Vector {
  201. F32 x,y,z;
  202. } dir; ///< Default direction
  203. };
  204. struct ActionAnimation {
  205. const char* name; ///< Sequence name
  206. S32 sequence; ///< Sequence index
  207. VectorF dir; ///< Dir of animation ground transform
  208. F32 speed; ///< Speed in m/s
  209. bool velocityScale; ///< Scale animation by velocity
  210. bool death; ///< Are we dying?
  211. };
  212. enum {
  213. // *** WARNING ***
  214. // These enum values are used to index the ActionAnimationList
  215. // array instantiated in player.cc
  216. // The first several are selected in the move state based on velocity
  217. RootAnim,
  218. RunForwardAnim,
  219. BackBackwardAnim,
  220. SideLeftAnim,
  221. SideRightAnim,
  222. SprintRootAnim,
  223. SprintForwardAnim,
  224. SprintBackwardAnim,
  225. SprintLeftAnim,
  226. SprintRightAnim,
  227. CrouchRootAnim,
  228. CrouchForwardAnim,
  229. CrouchBackwardAnim,
  230. CrouchLeftAnim,
  231. CrouchRightAnim,
  232. ProneRootAnim,
  233. ProneForwardAnim,
  234. ProneBackwardAnim,
  235. SwimRootAnim,
  236. SwimForwardAnim,
  237. SwimBackwardAnim,
  238. SwimLeftAnim,
  239. SwimRightAnim,
  240. // These are set explicitly based on player actions
  241. FallAnim,
  242. JumpAnim,
  243. StandJumpAnim,
  244. LandAnim,
  245. JetAnim,
  246. //
  247. NumTableActionAnims = JetAnim + 1,
  248. NumExtraActionAnims = 512 - NumTableActionAnims,
  249. NumActionAnims = NumTableActionAnims + NumExtraActionAnims,
  250. ActionAnimBits = 9,
  251. NullAnimation = (1 << ActionAnimBits) - 1
  252. };
  253. static ActionAnimationDef ActionAnimationList[NumTableActionAnims];
  254. ActionAnimation actionList[NumActionAnims];
  255. U32 actionCount;
  256. U32 lookAction;
  257. S32 spineNode[NumSpineNodes];
  258. S32 pickupDelta; ///< Base off of pcikupRadius
  259. F32 runSurfaceCos; ///< Angle from vertical in cos(runSurfaceAngle)
  260. F32 jumpSurfaceCos; ///< Angle from vertical in cos(jumpSurfaceAngle)
  261. enum Impacts {
  262. ImpactNone,
  263. ImpactNormal,
  264. };
  265. enum Recoil {
  266. LightRecoil,
  267. MediumRecoil,
  268. HeavyRecoil,
  269. NumRecoilSequences
  270. };
  271. S32 recoilSequence[NumRecoilSequences];
  272. /// @name Particles
  273. /// All of the data relating to environmental effects
  274. /// @{
  275. ParticleEmitterData * footPuffEmitter;
  276. S32 footPuffID;
  277. S32 footPuffNumParts;
  278. F32 footPuffRadius;
  279. DecalData* decalData;
  280. S32 decalID;
  281. ParticleEmitterData * dustEmitter;
  282. S32 dustID;
  283. SplashData* splash;
  284. S32 splashId;
  285. F32 splashVelocity;
  286. F32 splashAngle;
  287. F32 splashFreqMod;
  288. F32 splashVelEpsilon;
  289. F32 bubbleEmitTime;
  290. F32 medSplashSoundVel;
  291. F32 hardSplashSoundVel;
  292. F32 exitSplashSoundVel;
  293. F32 footSplashHeight;
  294. // Air control
  295. F32 airControl;
  296. // Jump off surfaces at their normal rather than straight up
  297. bool jumpTowardsNormal;
  298. StringTableEntry mControlMap;
  299. // For use if/when mPhysicsPlayer is created
  300. StringTableEntry physicsPlayerType;
  301. ParticleEmitterData* splashEmitterList[NUM_SPLASH_EMITTERS];
  302. S32 splashEmitterIDList[NUM_SPLASH_EMITTERS];
  303. /// @}
  304. //
  305. DECLARE_CONOBJECT(PlayerData);
  306. PlayerData();
  307. bool preload(bool server, String &errorStr) override;
  308. void getGroundInfo(TSShapeInstance*,TSThread*,ActionAnimation*);
  309. bool isTableSequence(S32 seq);
  310. bool isJumpAction(U32 action);
  311. static void initPersistFields();
  312. void packData(BitStream* stream) override;
  313. void unpackData(BitStream* stream) override;
  314. void onShapeChanged()
  315. {
  316. reloadOnLocalClient();
  317. }
  318. /// @name Callbacks
  319. /// @{
  320. DECLARE_CALLBACK( void, onPoseChange, ( Player* obj, const char* oldPose, const char* newPose ) );
  321. DECLARE_CALLBACK( void, onStartSwim, ( Player* obj ) );
  322. DECLARE_CALLBACK( void, onStopSwim, ( Player* obj ) );
  323. DECLARE_CALLBACK( void, onStartSprintMotion, ( Player* obj ) );
  324. DECLARE_CALLBACK( void, onStopSprintMotion, ( Player* obj ) );
  325. DECLARE_CALLBACK( void, doDismount, ( Player* obj ) );
  326. DECLARE_CALLBACK( void, onEnterLiquid, ( Player* obj, F32 coverage, const char* type ) );
  327. DECLARE_CALLBACK( void, onLeaveLiquid, ( Player* obj, const char* type ) );
  328. DECLARE_CALLBACK( void, animationDone, ( Player* obj, const char* animName) );
  329. DECLARE_CALLBACK( void, onEnterMissionArea, ( Player* obj ) );
  330. DECLARE_CALLBACK( void, onLeaveMissionArea, ( Player* obj ) );
  331. /// @}
  332. protected:
  333. void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override
  334. {
  335. reloadOnLocalClient();
  336. }
  337. };
  338. //----------------------------------------------------------------------------
  339. class Player: public ShapeBase
  340. {
  341. typedef ShapeBase Parent;
  342. public:
  343. enum Pose {
  344. StandPose = 0,
  345. SprintPose,
  346. CrouchPose,
  347. PronePose,
  348. SwimPose,
  349. NumPoseBits = 3
  350. };
  351. /// The ExtendedMove position/rotation index used for head movements
  352. static S32 smExtendedMoveHeadPosRotIndex;
  353. protected:
  354. /// Bit masks for different types of events
  355. enum MaskBits {
  356. ActionMask = Parent::NextFreeMask << 0,
  357. MoveMask = Parent::NextFreeMask << 1,
  358. ImpactMask = Parent::NextFreeMask << 2,
  359. TriggerMask = Parent::NextFreeMask << 3,
  360. NextFreeMask = Parent::NextFreeMask << 4
  361. };
  362. SimObjectPtr<ParticleEmitter> mSplashEmitter[PlayerData::NUM_SPLASH_EMITTERS];
  363. F32 mBubbleEmitterTime;
  364. /// Client interpolation/warp data
  365. struct StateDelta {
  366. Move move; ///< Last move from server
  367. F32 dt; ///< Last interpolation time
  368. /// @name Interpolation data
  369. /// @{
  370. Point3F pos;
  371. Point3F rot;
  372. Point3F head;
  373. VectorF posVec;
  374. VectorF rotVec;
  375. VectorF headVec;
  376. /// @}
  377. /// @name Warp data
  378. /// @{
  379. S32 warpTicks;
  380. Point3F warpOffset;
  381. Point3F rotOffset;
  382. /// @}
  383. };
  384. StateDelta mDelta; ///< Used for interpolation on the client. @see StateDelta
  385. S32 mPredictionCount; ///< Number of ticks to predict
  386. // Current pos, vel etc.
  387. Point3F mHead; ///< Head rotation, uses only x & z
  388. Point3F mRot; ///< Body rotation, uses only z
  389. VectorF mVelocity; ///< Velocity
  390. Point3F mAnchorPoint; ///< Pos compression anchor
  391. S32 mImpactSound;
  392. bool mUseHeadZCalc; ///< Including mHead.z in transform calculations
  393. F32 mLastAbsoluteYaw; ///< Stores that last absolute yaw value as passed in by ExtendedMove
  394. F32 mLastAbsolutePitch; ///< Stores that last absolute pitch value as passed in by ExtendedMove
  395. F32 mLastAbsoluteRoll; ///< Stores that last absolute roll value as passed in by ExtendedMove
  396. S32 mMountPending; ///< mMountPending suppresses tickDelay countdown so players will sit until
  397. ///< their mount, or another animation, comes through (or 13 seconds elapses).
  398. /// Main player state
  399. enum ActionState {
  400. NullState,
  401. MoveState,
  402. RecoverState,
  403. NumStateBits = 3
  404. };
  405. ActionState mState; ///< What is the player doing? @see ActionState
  406. bool mFalling; ///< Falling in mid-air?
  407. S32 mJumpDelay; ///< Delay till next jump
  408. Pose mPose;
  409. bool mAllowJumping;
  410. bool mAllowJetJumping;
  411. bool mAllowSprinting;
  412. bool mAllowCrouching;
  413. bool mAllowProne;
  414. bool mAllowSwimming;
  415. S32 mContactTimer; ///< Ticks since last contact
  416. Point3F mJumpSurfaceNormal; ///< Normal of the surface the player last jumped on
  417. U32 mJumpSurfaceLastContact; ///< How long it's been since the player landed (ticks)
  418. F32 mWeaponBackFraction; ///< Amount to slide the weapon back (if it's up against something)
  419. SFXSource* mMoveBubbleSound; ///< Sound for moving bubbles
  420. SFXSource* mWaterBreathSound; ///< Sound for underwater breath
  421. SimObjectPtr<ShapeBase> mControlObject; ///< Controlling object
  422. /// @name Animation threads & data
  423. /// @{
  424. struct ActionAnimation {
  425. S32 action;
  426. TSThread* thread;
  427. S32 delayTicks; // before picking another.
  428. bool forward;
  429. bool firstPerson;
  430. bool waitForEnd;
  431. bool holdAtEnd;
  432. bool animateOnServer;
  433. bool atEnd;
  434. bool callbackTripped;
  435. bool useSynchedPos;
  436. } mActionAnimation;
  437. struct ArmAnimation {
  438. U32 action;
  439. TSThread* thread;
  440. } mArmAnimation;
  441. TSThread* mArmThread;
  442. TSThread* mHeadVThread;
  443. TSThread* mHeadHThread;
  444. TSThread* mRecoilThread;
  445. TSThread* mImageStateThread;
  446. /// @}
  447. bool mInMissionArea; ///< Are we in the mission area?
  448. //
  449. S32 mRecoverTicks; ///< same as recoverTicks in the player datablock
  450. U32 mReversePending;
  451. F32 mRecoverDelay; ///< When bypassing the legacy recover system and only using the land sequence,
  452. /// this is how long the player will be in the land sequence.
  453. bool mInWater; ///< Is true if WaterCoverage is greater than zero
  454. bool mSwimming; ///< Is true if WaterCoverage is above the swimming threshold
  455. //
  456. PlayerData* mDataBlock; ///< MMmmmmm...datablock...
  457. Point3F mLastPos; ///< Holds the last position for physics updates
  458. Point3F mLastWaterPos; ///< Same as mLastPos, but for water
  459. #ifdef TORQUE_OPENVR
  460. SimObjectPtr<OpenVRTrackedObject> mControllers[2];
  461. #endif
  462. struct ContactInfo
  463. {
  464. bool contacted, jump, run;
  465. SceneObject *contactObject;
  466. VectorF contactNormal;
  467. void clear()
  468. {
  469. contacted=jump=run=false;
  470. contactObject = NULL;
  471. contactNormal.set(1,1,1);
  472. }
  473. ContactInfo() { clear(); }
  474. } mContactInfo;
  475. struct Death {
  476. F32 lastPos;
  477. Point3F posAdd;
  478. VectorF rotate;
  479. VectorF curNormal;
  480. F32 curSink;
  481. void clear() {dMemset(this, 0, sizeof(*this)); initFall();}
  482. VectorF getPosAdd() {VectorF ret(posAdd); posAdd.set(0,0,0); return ret;}
  483. bool haveVelocity() {return posAdd.x != 0 || posAdd.y != 0;}
  484. void initFall() {curNormal.set(0,0,1); curSink = 0;}
  485. Death() {clear();}
  486. MatrixF* fallToGround(F32 adjust, const Point3F& pos, F32 zrot, F32 boxRad);
  487. } mDeath;
  488. PhysicsPlayer *mPhysicsRep;
  489. // First person mounted image shapes
  490. TSShapeInstance* mShapeFPInstance[ShapeBase::MaxMountedImages];
  491. TSThread *mShapeFPAmbientThread[ShapeBase::MaxMountedImages];
  492. TSThread *mShapeFPVisThread[ShapeBase::MaxMountedImages];
  493. TSThread *mShapeFPAnimThread[ShapeBase::MaxMountedImages];
  494. TSThread *mShapeFPFlashThread[ShapeBase::MaxMountedImages];
  495. TSThread *mShapeFPSpinThread[ShapeBase::MaxMountedImages];
  496. public:
  497. // New collision
  498. OrthoBoxConvex mConvex;
  499. Box3F mWorkingQueryBox;
  500. /// Standing / Crouched / Prone or Swimming
  501. Pose getPose() const { return mPose; }
  502. virtual const char* getPoseName() const;
  503. /// Setting this from script directly might not actually work,
  504. /// This is really just a helper for the player class so that its bounding box
  505. /// will get resized appropriately when the pose changes
  506. void setPose( Pose pose );
  507. PhysicsPlayer* getPhysicsRep() const { return mPhysicsRep; }
  508. #ifdef TORQUE_OPENVR
  509. void setControllers(Vector<OpenVRTrackedObject*> controllerList);
  510. #endif
  511. protected:
  512. void setState(ActionState state, U32 ticks=0);
  513. void updateState();
  514. // Jetting
  515. bool mJetting;
  516. ///Update the movement
  517. virtual void updateMove(const Move *move);
  518. ///Interpolate movement
  519. Point3F _move( const F32 travelTime, Collision *outCol );
  520. F32 _doCollisionImpact( const Collision *collision, bool fallingCollision);
  521. void _handleCollision( const Collision &collision );
  522. virtual bool updatePos(const F32 travelTime = TickSec);
  523. // PATHSHAPE
  524. virtual void updateAttachment();
  525. // PATHSHAPE END
  526. ///Update head animation
  527. void updateLookAnimation(F32 dT = 0.f);
  528. ///Update other animations
  529. void updateAnimation(F32 dt);
  530. void updateAnimationTree(bool firstPerson);
  531. bool step(Point3F *pos,F32 *maxStep,F32 time);
  532. ///See if the player is still in the mission area
  533. void checkMissionArea();
  534. virtual U32 getArmAction() const { return mArmAnimation.action; }
  535. virtual bool setArmThread(U32 action);
  536. virtual void setActionThread(U32 action,bool forward,bool hold = false,bool wait = false,bool fsp = false, bool forceSet = false);
  537. virtual void updateActionThread();
  538. virtual void pickBestMoveAction(U32 startAnim, U32 endAnim, U32 * action, bool * forward) const;
  539. virtual void pickActionAnimation();
  540. /// @name Mounted objects
  541. /// @{
  542. void onUnmount( SceneObject *obj, S32 node ) override;
  543. void unmount() override;
  544. /// @}
  545. void setPosition(const Point3F& pos,const Point3F& viewRot);
  546. void setRenderPosition(const Point3F& pos,const Point3F& viewRot,F32 dt=-1);
  547. void _findContact( SceneObject **contactObject, VectorF *contactNormal, Vector<SceneObject*> *outOverlapObjects );
  548. void findContact( bool *run, bool *jump, VectorF *contactNormal );
  549. void buildImagePrefixPaths(String* prefixPaths);
  550. S32 findPrefixSequence(String* prefixPaths, const String& baseSeq);
  551. S32 convertActionToImagePrefix(U32 action);
  552. void onImage(U32 imageSlot, bool unmount) override;
  553. void onImageRecoil(U32 imageSlot,ShapeBaseImageData::StateData::RecoilState) override;
  554. void onImageStateAnimation(U32 imageSlot, const char* seqName, bool direction, bool scaleToState, F32 stateTimeOutValue) override;
  555. const char* getImageAnimPrefix(U32 imageSlot, S32 imageShapeIndex) override;
  556. void onImageAnimThreadChange(U32 imageSlot, S32 imageShapeIndex, ShapeBaseImageData::StateData* lastState, const char* anim, F32 pos, F32 timeScale, bool reset=false) override;
  557. void onImageAnimThreadUpdate(U32 imageSlot, S32 imageShapeIndex, F32 dt) override;
  558. void updateDamageLevel() override;
  559. void updateDamageState() override;
  560. /// Set which client is controlling this player
  561. void setControllingClient(GameConnection* client) override;
  562. void calcClassRenderData() override;
  563. /// Play sound for foot contact.
  564. ///
  565. /// @param triggeredLeft If true, left foot hit; right otherwise.
  566. /// @param contactMaterial Material onto which the player stepped; may be NULL.
  567. /// @param contactObject Object onto which the player stepped; may be NULL.
  568. void playFootstepSound( bool triggeredLeft, Material* contactMaterial, SceneObject* contactObject );
  569. /// Play an impact sound.
  570. void playImpactSound();
  571. /// Are we in the process of dying?
  572. bool inDeathAnim();
  573. F32 deathDelta(Point3F &delta);
  574. void updateDeathOffsets();
  575. bool inSittingAnim();
  576. /// @name Water
  577. /// @{
  578. void updateSplash(); ///< Update the splash effect
  579. void updateFroth( F32 dt ); ///< Update any froth
  580. void updateWaterSounds( F32 dt ); ///< Update water sounds
  581. void createSplash( Point3F &pos, F32 speed ); ///< Creates a splash
  582. bool collidingWithWater( Point3F &waterHeight ); ///< Are we colliding with water?
  583. /// @}
  584. void disableHeadZCalc() { mUseHeadZCalc = false; }
  585. void enableHeadZCalc() { mUseHeadZCalc = true; }
  586. public:
  587. DECLARE_CONOBJECT(Player);
  588. DECLARE_CATEGORY("Actor \t Controllable");
  589. Player();
  590. ~Player();
  591. static void consoleInit();
  592. /// @name Transforms
  593. /// @{
  594. void setTransform(const MatrixF &mat) override;
  595. void getEyeTransform(MatrixF* mat) override;
  596. void getEyeBaseTransform(MatrixF* mat, bool includeBank) override;
  597. void getRenderEyeTransform(MatrixF* mat) override;
  598. void getRenderEyeBaseTransform(MatrixF* mat, bool includeBank) override;
  599. void getCameraParameters(F32 *min, F32 *max, Point3F *offset, MatrixF *rot) override;
  600. void getMuzzleTransform(U32 imageSlot,MatrixF* mat) override;
  601. void getRenderMuzzleTransform(U32 imageSlot,MatrixF* mat) override;
  602. void getMuzzleVector(U32 imageSlot,VectorF* vec) override;
  603. /// @}
  604. F32 getSpeed() const;
  605. Point3F getVelocity() const override;
  606. void setVelocity(const VectorF& vel) override;
  607. /// Apply an impulse at the given point, with magnitude/direction of vec
  608. void applyImpulse(const Point3F& pos,const VectorF& vec) override;
  609. /// Get the rotation of the player
  610. const Point3F& getRotation() { return mRot; }
  611. /// Get the rotation of the head of the player
  612. const Point3F& getHeadRotation() { return mHead; }
  613. void getDamageLocation(const Point3F& in_rPos, const char *&out_rpVert, const char *&out_rpQuad);
  614. void allowAllPoses();
  615. void allowJumping(bool state) { mAllowJumping = state; }
  616. void allowJetJumping(bool state) { mAllowJetJumping = state; }
  617. void allowSprinting(bool state) { mAllowSprinting = state; }
  618. void allowCrouching(bool state) { mAllowCrouching = state; }
  619. void allowProne(bool state) { mAllowProne = state; }
  620. void allowSwimming(bool state) { mAllowSwimming = state; }
  621. bool canJump(); ///< Can the player jump?
  622. bool canJetJump(); ///< Can the player jet?
  623. bool canSwim(); ///< Can the player swim?
  624. bool canCrouch();
  625. bool canStand();
  626. bool canProne();
  627. bool canSprint();
  628. bool haveContact() const { return !mContactTimer; } ///< Is it in contact with something
  629. void getMuzzlePointAI( U32 imageSlot, Point3F *point );
  630. F32 getMaxForwardVelocity() const { return (mDataBlock != NULL ? mDataBlock->maxForwardSpeed : 0); }
  631. bool isDisplacable() const override;
  632. Point3F getMomentum() const override;
  633. void setMomentum(const Point3F &momentum) override;
  634. bool displaceObject(const Point3F& displaceVector) override;
  635. bool checkDismountPosition(const MatrixF& oldPos, const MatrixF& newPos); ///< Is it safe to dismount here?
  636. //
  637. bool onAdd() override;
  638. void onRemove() override;
  639. bool onNewDataBlock( GameBaseData *dptr, bool reload ) override;
  640. void onScaleChanged() override;
  641. Box3F mScaledBox;
  642. // Animation
  643. const char* getStateName();
  644. bool setActionThread(const char* sequence,bool hold,bool wait,bool fsp = false);
  645. const String& getArmThread() const;
  646. bool setArmThread(const char* sequence);
  647. // Object control
  648. void setControlObject(ShapeBase *obj) override;
  649. ShapeBase* getControlObject() override;
  650. //
  651. void updateWorkingCollisionSet();
  652. void processTick(const Move *move) override;
  653. void interpolateTick(F32 delta) override;
  654. void advanceTime(F32 dt) override;
  655. bool castRay(const Point3F &start, const Point3F &end, RayInfo* info) override;
  656. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere) override;
  657. void buildConvex(const Box3F& box, Convex* convex) override;
  658. bool isControlObject();
  659. void onCameraScopeQuery(NetConnection *cr, CameraScopeQuery *) override;
  660. void writePacketData(GameConnection *conn, BitStream *stream) override;
  661. void readPacketData (GameConnection *conn, BitStream *stream) override;
  662. U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream) override;
  663. void unpackUpdate(NetConnection *conn, BitStream *stream) override;
  664. void prepRenderImage( SceneRenderState* state ) override;
  665. virtual void renderConvex( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  666. void renderMountedImage( U32 imageSlot, TSRenderState &rstate, SceneRenderState *state ) override;
  667. protected:
  668. static void afx_consoleInit();
  669. void afx_init();
  670. U32 afx_packUpdate(NetConnection*, U32 mask, BitStream*, U32 retMask);
  671. void afx_unpackUpdate(NetConnection*, BitStream*);
  672. private:
  673. static bool sCorpsesHiddenFromRayCast;
  674. public:
  675. void restoreAnimation(U32 tag) override;
  676. U32 getAnimationID(const char* name) override;
  677. U32 playAnimationByID(U32 anim_id, F32 pos, F32 rate, F32 trans, bool hold, bool wait, bool is_death_anim) override;
  678. F32 getAnimationDurationByID(U32 anim_id) override;
  679. bool isBlendAnimation(const char* name) override;
  680. const char* getLastClipName(U32 clip_tag) override;
  681. void unlockAnimation(U32 tag, bool force=false) override;
  682. U32 lockAnimation() override;
  683. bool isAnimationLocked() const override { return ((anim_clip_flags & BLOCK_USER_CONTROL) != 0); }
  684. protected:
  685. bool overrideLookAnimation;
  686. F32 armLookOverridePos;
  687. F32 headVLookOverridePos;
  688. F32 headHLookOverridePos;
  689. public:
  690. void setLookAnimationOverride(bool flag);
  691. void copyHeadRotation(const Player* p) { mHead = p->mHead; }
  692. public:
  693. bool ignore_updates;
  694. void resetContactTimer() { mContactTimer = 0; }
  695. protected:
  696. U8 move_trigger_states;
  697. U32 fx_s_triggers;
  698. U32 mark_fx_c_triggers;
  699. U32 fx_c_triggers;
  700. F32 z_velocity;
  701. bool mark_idle;
  702. F32 idle_timer;
  703. bool mark_s_landing;
  704. void process_client_triggers(bool triggeredLeft, bool triggeredRight);
  705. public:
  706. enum {
  707. // server events
  708. PLAYER_MOVE_TRIGGER_0 = BIT(0),
  709. PLAYER_MOVE_TRIGGER_1 = BIT(1),
  710. PLAYER_MOVE_TRIGGER_2 = BIT(2),
  711. PLAYER_MOVE_TRIGGER_3 = BIT(3),
  712. PLAYER_MOVE_TRIGGER_4 = BIT(4),
  713. PLAYER_MOVE_TRIGGER_5 = BIT(5),
  714. PLAYER_LANDING_S_TRIGGER = BIT(6),
  715. PLAYER_FIRE_S_TRIGGER = PLAYER_MOVE_TRIGGER_0,
  716. PLAYER_FIRE_ALT_S_TRIGGER = PLAYER_MOVE_TRIGGER_1,
  717. PLAYER_JUMP_S_TRIGGER = BIT(7),
  718. // client events
  719. PLAYER_LF_FOOT_C_TRIGGER = BIT(16),
  720. PLAYER_RT_FOOT_C_TRIGGER = BIT(17),
  721. PLAYER_LANDING_C_TRIGGER = BIT(18),
  722. PLAYER_IDLE_C_TRIGGER = BIT(19),
  723. };
  724. U32 getClientEventTriggers() const { return fx_c_triggers; }
  725. U32 getServerEventTriggers() const { return fx_s_triggers; }
  726. protected:
  727. F32 speed_bias;
  728. F32 speed_bias_goal;
  729. bool override_movement;
  730. Point3F movement_data;
  731. U8 movement_op;
  732. U32 last_movement_tag;
  733. static U32 unique_movement_tag_counter;
  734. public:
  735. void setMovementSpeedBias(F32 bias);
  736. U32 setMovementOverride(F32 bias, const Point3F* mov=0, U32 op=1);
  737. void restoreMovement(U32 tag);
  738. protected:
  739. S32 footfallDecalOverride;
  740. S32 footfallSoundOverride;
  741. S32 footfallDustOverride;
  742. bool noFootfallFX;
  743. public:
  744. void overrideFootfallFX(bool decals=true, bool sounds=true, bool dust=true);
  745. void restoreFootfallFX(bool decals=true, bool sounds=true, bool dust=true);
  746. };
  747. typedef Player::Pose PlayerPose;
  748. DefineEnumType( PlayerPose );
  749. #endif