afxMagicMissile.h 13 KB

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