afxMagicMissile.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. // afxMagicMissile is a heavily modified variation of the stock Projectile class. In
  27. // addition to numerous AFX customizations, it also incorporates functionality based on
  28. // the following TGE resources:
  29. //
  30. // Guided or Seeker Projectiles by Derk Adams
  31. // http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=6778
  32. //
  33. // Projectile Ballistic Coefficients (drag factors) by Mark Owen
  34. // http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=5128
  35. //
  36. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  37. #ifndef _AFX_MAGIC_MISSILE_H_
  38. #define _AFX_MAGIC_MISSILE_H_
  39. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  40. #include "T3D/lightDescription.h"
  41. #include "T3D/fx/particleEmitter.h"
  42. #include "afx/afxConstraint.h"
  43. class SplashData;
  44. class ShapeBase;
  45. class TSShapeInstance;
  46. class PhysicsWorld;
  47. class SFXTrack;
  48. class SFXSource;
  49. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  50. // afxMagicMissileData
  51. class afxMagicMissileData : public GameBaseData, protected AssetPtrCallback
  52. {
  53. typedef GameBaseData Parent;
  54. protected:
  55. bool onAdd() override;
  56. public:
  57. enum { MaxLifetimeTicks = 4095 };
  58. public:
  59. // variables set in datablock definition:
  60. // Shape related
  61. DECLARE_SHAPEASSET_REFACTOR(afxMagicMissileData, ProjectileShape)
  62. //StringTableEntry projectileShapeName;
  63. //bool hasLight;
  64. //F32 lightRadius;
  65. //LinearColorF lightColor;
  66. //bool hasWaterLight;
  67. //LinearColorF waterLightColor;
  68. /*
  69. /// Set to true if it is a billboard and want it to always face the viewer, false otherwise
  70. bool faceViewer;
  71. */
  72. Point3F scale;
  73. /*
  74. /// [0,1] scale of how much velocity should be inherited from the parent object
  75. F32 velInheritFactor;
  76. /// Speed of the projectile when fired
  77. */
  78. F32 muzzleVelocity;
  79. /// Should it arc?
  80. bool isBallistic;
  81. /*
  82. /// How HIGH should it bounce (parallel to normal), [0,1]
  83. F32 bounceElasticity;
  84. /// How much momentum should be lost when it bounces (perpendicular to normal), [0,1]
  85. F32 bounceFriction;
  86. */
  87. /// Should this projectile fall/rise different than a default object?
  88. F32 gravityMod;
  89. /// How long the projectile should exist before deleting itself
  90. U32 lifetime; // ticks
  91. /*
  92. /// How long it should not detonate on impact
  93. S32 armingDelay; // the values are converted on initialization with
  94. */
  95. S32 fadeDelay; // ticks
  96. /*
  97. ExplosionData* explosion; // Explosion Datablock
  98. S32 explosionId; // Explosion ID
  99. ExplosionData* waterExplosion; // Water Explosion Datablock
  100. S32 waterExplosionId; // Water Explosion ID
  101. */
  102. SplashData* splash; // Water Splash Datablock
  103. S32 splashId; // Water splash ID
  104. DECLARE_SOUNDASSET(afxMagicMissileData, ProjectileSound);
  105. DECLARE_ASSET_SETGET(afxMagicMissileData, ProjectileSound);
  106. LightDescription *lightDesc;
  107. S32 lightDescId;
  108. /*
  109. enum DecalConstants { // Number of decals constant
  110. NumDecals = 6,
  111. };
  112. DecalData* decals[NumDecals]; // Decal Datablocks
  113. S32 decalId[NumDecals]; // Decal IDs
  114. U32 decalCount; // # of loaded Decal Datablocks
  115. */
  116. // variables set on preload:
  117. TSShape* projectileShape;
  118. /*
  119. S32 activateSeq;
  120. S32 maintainSeq;
  121. */
  122. ParticleEmitterData* particleEmitter;
  123. S32 particleEmitterId;
  124. ParticleEmitterData* particleWaterEmitter;
  125. S32 particleWaterEmitterId;
  126. U32 collision_mask;
  127. Point3F starting_vel_vec;
  128. // guidance behavior
  129. bool isGuided;
  130. F32 precision;
  131. S32 trackDelay;
  132. // simple physics
  133. F32 ballisticCoefficient;
  134. // terrain following
  135. bool followTerrain;
  136. F32 followTerrainHeight;
  137. F32 followTerrainAdjustRate;
  138. S32 followTerrainAdjustDelay;
  139. F32 acceleration;
  140. S32 accelDelay;
  141. U32 accelLifetime;
  142. StringTableEntry launch_node;
  143. Point3F launch_offset;
  144. Point3F launch_offset_server;
  145. Point3F launch_offset_client;
  146. Point3F launch_node_offset;
  147. F32 launch_pitch;
  148. F32 launch_pan;
  149. bool echo_launch_offset;
  150. StringTableEntry launch_cons_s_spec;
  151. afxConstraintDef launch_cons_s_def;
  152. StringTableEntry launch_cons_c_spec;
  153. afxConstraintDef launch_cons_c_def;
  154. // wiggle behavior
  155. Vector<F32> wiggle_magnitudes;
  156. Vector<F32> wiggle_speeds;
  157. StringTableEntry wiggle_axis_string;
  158. Point3F* wiggle_axis;
  159. U32 wiggle_num_axis;
  160. // hover behavior
  161. F32 hover_altitude;
  162. F32 hover_attack_distance;
  163. F32 hover_attack_gradient;
  164. U32 hover_time;
  165. bool reverse_targeting;
  166. U32 caster_safety_time;
  167. public:
  168. /*C*/ afxMagicMissileData();
  169. /*D*/ ~afxMagicMissileData();
  170. void packData(BitStream*) override;
  171. void unpackData(BitStream*) override;
  172. bool preload(bool server, String &errorStr) override;
  173. static void initPersistFields();
  174. DECLARE_CONOBJECT(afxMagicMissileData);
  175. public:
  176. /*C*/ afxMagicMissileData(const afxMagicMissileData&, bool = false);
  177. afxMagicMissileData* cloneAndPerformSubstitutions(const SimObject*, S32 index=0);
  178. bool allowSubstitutions() const override { return true; }
  179. void gather_cons_defs(Vector<afxConstraintDef>& defs);
  180. protected:
  181. void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override
  182. {
  183. reloadOnLocalClient();
  184. }
  185. };
  186. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  187. // afxMagicMissile
  188. //class afxMagicSpell;
  189. class afxChoreographer;
  190. class afxMagicMissile : public GameBase, public ISceneLight
  191. {
  192. typedef GameBase Parent;
  193. public:
  194. /*
  195. // Initial conditions
  196. enum ProjectileConstants {
  197. SourceIdTimeoutTicks = 7, // = 231 ms
  198. DeleteWaitTime = 500, ///< 500 ms delete timeout (for network transmission delays)
  199. ExcessVelDirBits = 7,
  200. MaxLivingTicks = 4095,
  201. };
  202. */
  203. enum UpdateMasks {
  204. /*
  205. BounceMask = Parent::NextFreeMask,
  206. ExplosionMask = Parent::NextFreeMask << 1,
  207. */
  208. GuideMask = Parent::NextFreeMask << 0,
  209. LaunchMask = Parent::NextFreeMask << 1,
  210. ImpactMask = Parent::NextFreeMask << 2,
  211. NextFreeMask = Parent::NextFreeMask << 3
  212. };
  213. protected:
  214. PhysicsWorld *mPhysicsWorld;
  215. afxMagicMissileData* mDataBlock;
  216. ParticleEmitter* mParticleEmitter;
  217. ParticleEmitter* mParticleWaterEmitter;
  218. SFXSource* mSound;
  219. Point3F mCurrPosition;
  220. Point3F mCurrVelocity;
  221. /*
  222. S32 mSourceObjectId;
  223. S32 mSourceObjectSlot;
  224. */
  225. // Time related variables common to all projectiles, managed by processTick
  226. U32 mCurrTick; ///< Current time in ticks
  227. /*
  228. SimObjectPtr<ShapeBase> mSourceObject; ///< Actual pointer to the source object, times out after SourceIdTimeoutTicks
  229. */
  230. // Rendering related variables
  231. TSShapeInstance* mProjectileShape;
  232. /*
  233. TSThread* mActivateThread;
  234. TSThread* mMaintainThread;
  235. Point3F mLastRenderPos;
  236. */
  237. // ISceneLight
  238. void submitLights( LightManager *lm, bool staticLighting ) override;
  239. LightInfo* getLight() override { return mLight; }
  240. LightInfo *mLight;
  241. LightState mLightState;
  242. /*
  243. bool mHidden; ///< set by the derived class, if true, projectile doesn't render
  244. F32 mFadeValue; ///< set in processTick, interpolation between fadeDelay and lifetime
  245. ///< in data block
  246. */
  247. /*
  248. // Warping and back delta variables. Only valid on the client
  249. //
  250. Point3F mWarpStart;
  251. Point3F mWarpEnd;
  252. U32 mWarpTicksRemaining;
  253. */
  254. Point3F mCurrDeltaBase;
  255. Point3F mCurrBackDelta;
  256. /*
  257. Point3F mExplosionPosition;
  258. Point3F mExplosionNormal;
  259. U32 mCollideHitType;
  260. */
  261. bool onAdd() override;
  262. void onRemove() override;
  263. bool onNewDataBlock(GameBaseData *dptr, bool reload) override;
  264. // Rendering
  265. void prepRenderImage(SceneRenderState*) override;
  266. void prepBatchRender( SceneRenderState *state);
  267. void processTick(const Move *move) override;
  268. /*
  269. void advanceTime(F32 dt);
  270. */
  271. void interpolateTick(F32 delta) override;
  272. /*
  273. /// What to do once this projectile collides with something
  274. virtual void onCollision(const Point3F& p, const Point3F& n, SceneObject*);
  275. /// What to do when this projectile explodes
  276. virtual void explode(const Point3F& p, const Point3F& n, const U32 collideType );
  277. /// Returns the velocity of the projectile
  278. Point3F getVelocity() const;
  279. */
  280. void emitParticles(const Point3F&, const Point3F&, const Point3F&, const U32);
  281. void updateSound();
  282. // Rendering
  283. /*
  284. void prepModelView ( SceneRenderState *state);
  285. */
  286. // These are stolen from the player class ..
  287. bool pointInWater(const Point3F &point);
  288. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream) override;
  289. void unpackUpdate(NetConnection *conn, BitStream *stream) override;
  290. afxChoreographer* choreographer;
  291. bool client_only;
  292. bool server_only;
  293. bool use_accel;
  294. U32 collision_mask;
  295. F32 prec_inc;
  296. bool did_launch;
  297. bool did_impact;
  298. SceneObject* missile_target;
  299. SceneObject* collide_exempt;
  300. bool hover_attack_go;
  301. U32 hover_attack_tick;
  302. F32 starting_velocity;
  303. Point3F starting_vel_vec;
  304. SimObject* ss_object;
  305. S32 ss_index;
  306. private:
  307. void init(bool on_server, bool on_client);
  308. void create_splash(const Point3F& pos);
  309. SceneObject* get_default_launcher() const;
  310. void get_launch_constraint_data(Point3F& pos, Point3F& vel);
  311. void get_launch_data(Point3F& pos, Point3F& vel);
  312. bool is_active() const { return (did_launch && !did_impact); }
  313. public:
  314. /*
  315. F32 getUpdatePriority(CameraScopeQuery *focusObject, U32 updateMask, S32 updateSkips);
  316. */
  317. /*C*/ afxMagicMissile();
  318. /*C*/ afxMagicMissile(bool on_server, bool on_client);
  319. /*D*/ ~afxMagicMissile();
  320. void onDeleteNotify(SimObject*) override;
  321. DECLARE_CONOBJECT(afxMagicMissile);
  322. DECLARE_CATEGORY("UNLISTED");
  323. static void initPersistFields();
  324. /*
  325. virtual bool calculateImpact(float simTime,
  326. Point3F& pointOfImpact,
  327. float& impactTime);
  328. static U32 smProjectileWarpTicks;
  329. protected:
  330. static const U32 csmStaticCollisionMask;
  331. static const U32 csmDynamicCollisionMask;
  332. static const U32 csmDamageableMask;
  333. */
  334. void launch();
  335. void setChoreographer(afxChoreographer*);
  336. void setStartingVelocityVector(const Point3F& vel_vec);
  337. void setStartingVelocity(const F32 vel);
  338. void getStartingVelocityValues(F32& vel, Point3F& vel_vec);
  339. void setSubstitutionData(SimObject* obj, S32 idx=0) { ss_object = obj; ss_index = idx; }
  340. };
  341. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  342. // afxMagicMissileCallback
  343. class afxMagicMissileCallback
  344. {
  345. public:
  346. virtual void impactNotify(const Point3F& p, const Point3F& n, SceneObject*)=0;
  347. };
  348. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  349. #endif // _AFX_MAGIC_MISSILE_H_