afxMagicMissile.h 13 KB

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