afxEffectWrapper.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #ifndef _AFX_EFFECT_WRAPPER_H_
  25. #define _AFX_EFFECT_WRAPPER_H_
  26. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  27. #include "afx/arcaneFX.h"
  28. #include "afxEffectDefs.h"
  29. #include "afxConstraint.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. struct afxEffectTimingData
  32. {
  33. F32 delay;
  34. F32 lifetime;
  35. F32 fade_in_time;
  36. F32 fade_out_time;
  37. F32 residue_lifetime;
  38. F32 residue_fadetime;
  39. F32 life_bias;
  40. Point2F fadein_ease;
  41. Point2F fadeout_ease;
  42. afxEffectTimingData()
  43. {
  44. delay = 0.0f;
  45. lifetime = 0.0f;
  46. fade_in_time = 0.0f;
  47. fade_out_time = 0.0f;
  48. residue_lifetime = 0.0f;
  49. residue_fadetime = 0.0f;
  50. life_bias = 1.0f;
  51. fadein_ease.set(0.0f, 1.0f);
  52. fadeout_ease.set(0.0f, 1.0f);
  53. }
  54. };
  55. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  56. class afxEffectWrapperData;
  57. class afxAnimCurve;
  58. class afxEffectAdapterDesc
  59. {
  60. private:
  61. static Vector<afxEffectAdapterDesc*>* adapters;
  62. public:
  63. /*C*/ afxEffectAdapterDesc();
  64. virtual bool testEffectType(const SimDataBlock*) const=0;
  65. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const=0;
  66. virtual bool runsOnServer(const afxEffectWrapperData*) const=0;
  67. virtual bool runsOnClient(const afxEffectWrapperData*) const=0;
  68. virtual bool isPositional(const afxEffectWrapperData*) const { return true; }
  69. virtual void prepEffect(afxEffectWrapperData*) const { }
  70. virtual afxEffectWrapper* create() const=0;
  71. static bool identifyEffect(afxEffectWrapperData*);
  72. };
  73. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  74. class afxXM_BaseData;
  75. class afxEffectBaseData : public GameBaseData, public afxEffectDefs
  76. {
  77. typedef GameBaseData Parent;
  78. public:
  79. /*C*/ afxEffectBaseData() { }
  80. /*C*/ afxEffectBaseData(const afxEffectBaseData& other, bool temp=false) : GameBaseData(other, temp){ }
  81. virtual void gather_cons_defs(Vector<afxConstraintDef>& defs) { };
  82. DECLARE_CONOBJECT(afxEffectBaseData);
  83. DECLARE_CATEGORY("AFX");
  84. };
  85. //class afxEffectWrapperData : public GameBaseData, public afxEffectDefs
  86. class afxEffectWrapperData : public afxEffectBaseData
  87. {
  88. //typedef GameBaseData Parent;
  89. typedef afxEffectBaseData Parent;
  90. bool do_id_convert;
  91. public:
  92. enum { MAX_CONDITION_STATES = 4 };
  93. public:
  94. StringTableEntry effect_name;
  95. bool use_as_cons_obj;
  96. bool use_ghost_as_cons_obj;
  97. StringTableEntry cons_spec;
  98. StringTableEntry pos_cons_spec;
  99. StringTableEntry orient_cons_spec;
  100. StringTableEntry aim_cons_spec;
  101. StringTableEntry life_cons_spec;
  102. //
  103. afxConstraintDef cons_def;
  104. afxConstraintDef pos_cons_def;
  105. afxConstraintDef orient_cons_def;
  106. afxConstraintDef aim_cons_def;
  107. afxConstraintDef life_cons_def;
  108. afxEffectTimingData ewd_timing;
  109. U32 inherit_timing;
  110. F32 scale_factor; // scale size if applicable
  111. F32 rate_factor; // scale rate if applicable
  112. F32 user_fade_out_time;
  113. bool is_looping;
  114. U32 n_loops;
  115. F32 loop_gap_time;
  116. bool ignore_time_factor;
  117. bool propagate_time_factor;
  118. ByteRange ranking_range;
  119. ByteRange lod_range;
  120. S32 life_conds;
  121. bool effect_enabled;
  122. U32 exec_cond_on_bits[MAX_CONDITION_STATES];
  123. U32 exec_cond_off_bits[MAX_CONDITION_STATES];
  124. U32 exec_cond_bitmasks[MAX_CONDITION_STATES];
  125. S32 data_ID;
  126. afxXM_BaseData* xfm_modifiers[MAX_XFM_MODIFIERS];
  127. Box3F forced_bbox;
  128. bool update_forced_bbox;
  129. S8 sort_priority;
  130. Point3F direction;
  131. F32 speed;
  132. F32 mass;
  133. bool borrow_altitudes;
  134. StringTableEntry vis_keys_spec;
  135. afxAnimCurve* vis_keys;
  136. SimDataBlock* effect_data;
  137. afxEffectAdapterDesc* effect_desc;
  138. S32 group_index;
  139. void parse_cons_specs();
  140. void parse_vis_keys();
  141. void gather_cons_defs(Vector<afxConstraintDef>& defs);
  142. void pack_mods(BitStream*, afxXM_BaseData* mods[], bool packed);
  143. void unpack_mods(BitStream*, afxXM_BaseData* mods[]);
  144. public:
  145. /*C*/ afxEffectWrapperData();
  146. /*C*/ afxEffectWrapperData(const afxEffectWrapperData&, bool = false);
  147. /*D*/ ~afxEffectWrapperData();
  148. virtual bool onAdd();
  149. virtual void packData(BitStream*);
  150. virtual void unpackData(BitStream*);
  151. bool preload(bool server, String &errorStr);
  152. virtual void onPerformSubstitutions();
  153. bool requiresStop(const afxEffectTimingData& timing) { return effect_desc->requiresStop(this, timing); }
  154. bool runsOnServer() { return effect_desc->runsOnServer(this); }
  155. bool runsOnClient() { return effect_desc->runsOnClient(this); }
  156. bool runsHere(bool server_here) { return (server_here) ? runsOnServer() : runsOnClient(); }
  157. bool isPositional() { return effect_desc->isPositional(this); }
  158. bool testExecConditions(U32 conditions);
  159. F32 afterStopTime() { return ewd_timing.fade_out_time; }
  160. virtual bool allowSubstitutions() const { return true; }
  161. static void initPersistFields();
  162. DECLARE_CONOBJECT(afxEffectWrapperData);
  163. DECLARE_CATEGORY("AFX");
  164. };
  165. inline bool afxEffectWrapperData::testExecConditions(U32 conditions)
  166. {
  167. if (exec_cond_bitmasks[0] == 0)
  168. return true;
  169. if ((exec_cond_bitmasks[0] & conditions) == exec_cond_on_bits[0])
  170. return true;
  171. for (S32 i = 1; i < MAX_CONDITION_STATES; i++)
  172. {
  173. if (exec_cond_bitmasks[i] == 0)
  174. return false;
  175. if ((exec_cond_bitmasks[i] & conditions) == exec_cond_on_bits[i])
  176. return true;
  177. }
  178. return false;
  179. }
  180. typedef Vector<afxEffectBaseData*> afxEffectList;
  181. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  182. // afxEffectWrapper
  183. //
  184. // NOTE -- this not a subclass of GameBase... it is only meant to exist on
  185. // the client-side.
  186. class ShapeBase;
  187. class GameBase;
  188. class TSShape;
  189. class TSShapeInstance;
  190. class SceneObject;
  191. class afxConstraint;
  192. class afxConstraintMgr;
  193. class afxChoreographer;
  194. class afxXM_Base;
  195. class afxEffectWrapper : public SimObject, public afxEffectDefs
  196. {
  197. typedef SimObject Parent;
  198. friend class afxEffectVector;
  199. private:
  200. bool test_life_conds();
  201. protected:
  202. afxEffectWrapperData* mDatablock;
  203. afxEffectTimingData mEW_timing;
  204. F32 mFade_in_end;
  205. F32 mFade_out_start;
  206. F32 mFull_lifetime;
  207. F32 mTime_factor;
  208. F32 mProp_time_factor;
  209. afxChoreographer* mChoreographer;
  210. afxConstraintMgr* mCons_mgr;
  211. afxConstraintID mPos_cons_id;
  212. afxConstraintID mOrient_cons_id;
  213. afxConstraintID mAim_cons_id;
  214. afxConstraintID mLife_cons_id;
  215. afxConstraintID mEffect_cons_id;
  216. F32 mElapsed;
  217. F32 mLife_elapsed;
  218. F32 mLife_end;
  219. bool mStopped;
  220. bool mCond_alive;
  221. U32 mNum_updates;
  222. MatrixF mUpdated_xfm;
  223. Point3F mUpdated_pos;
  224. Point3F mUpdated_aim;
  225. Point3F mUpdated_scale;
  226. LinearColorF mUpdated_color;
  227. F32 mFade_value;
  228. F32 mLast_fade_value;
  229. bool mDo_fade_inout;
  230. bool mDo_fades;
  231. bool mIn_scope;
  232. bool mIs_aborted;
  233. afxXM_Base* mXfm_modifiers[MAX_XFM_MODIFIERS];
  234. F32 mLive_scale_factor;
  235. F32 mLive_fade_factor;
  236. F32 mTerrain_altitude;
  237. F32 mInterior_altitude;
  238. S32 mGroup_index;
  239. public:
  240. /*C*/ afxEffectWrapper();
  241. virtual ~afxEffectWrapper();
  242. void ew_init(afxChoreographer*, afxEffectWrapperData*, afxConstraintMgr*,
  243. F32 time_factor);
  244. F32 getFullLifetime() { return mEW_timing.lifetime + mEW_timing.fade_out_time; }
  245. F32 getTimeFactor() { return mTime_factor; }
  246. afxConstraint* getPosConstraint() { return mCons_mgr->getConstraint(mPos_cons_id); }
  247. afxConstraint* getOrientConstraint() { return mCons_mgr->getConstraint(mOrient_cons_id); }
  248. afxConstraint* getAimConstraint() { return mCons_mgr->getConstraint(mAim_cons_id); }
  249. afxConstraint* getLifeConstraint() { return mCons_mgr->getConstraint(mLife_cons_id); }
  250. afxChoreographer* getChoreographer() { return mChoreographer; }
  251. virtual bool isDone();
  252. virtual bool deleteWhenStopped() { return false; }
  253. F32 afterStopTime() { return mEW_timing.fade_out_time; }
  254. bool isAborted() const { return mIs_aborted; }
  255. void prestart();
  256. bool start(F32 timestamp);
  257. bool update(F32 dt);
  258. void stop();
  259. void cleanup(bool was_stopped=false);
  260. void setScopeStatus(bool flag);
  261. virtual void ea_set_datablock(SimDataBlock*) { }
  262. virtual bool ea_start() { return true; }
  263. virtual bool ea_update(F32 dt) { return true; }
  264. virtual void ea_finish(bool was_stopped) { }
  265. virtual void ea_set_scope_status(bool flag) { }
  266. virtual bool ea_is_enabled() { return true; }
  267. virtual SceneObject* ea_get_scene_object() const { return 0; }
  268. U32 ea_get_triggers() const { return 0; }
  269. void getUpdatedPosition(Point3F& pos) { pos = mUpdated_pos;}
  270. void getUpdatedTransform(MatrixF& xfm) { xfm = mUpdated_xfm; }
  271. void getUpdatedScale(Point3F& scale) { scale = mUpdated_scale; }
  272. void getUpdatedColor(LinearColorF& color) { color = mUpdated_color; }
  273. virtual void getUpdatedBoxCenter(Point3F& pos) { pos = mUpdated_pos;}
  274. virtual void getUnconstrainedPosition(Point3F& pos) { pos.zero();}
  275. virtual void getUnconstrainedTransform(MatrixF& xfm) { xfm.identity(); }
  276. virtual void getBaseColor(LinearColorF& color) { color.set(1.0f, 1.0f, 1.0f, 1.0f); }
  277. SceneObject* getSceneObject() const { return ea_get_scene_object(); }
  278. U32 getTriggers() const { return ea_get_triggers(); }
  279. F32 getMass() { return mDatablock->mass; }
  280. Point3F getDirection() { return mDatablock->direction; }
  281. F32 getSpeed() { return mDatablock->speed; }
  282. virtual TSShape* getTSShape() { return 0; }
  283. virtual TSShapeInstance* getTSShapeInstance() { return 0; }
  284. virtual U32 setAnimClip(const char* clip, F32 pos, F32 rate, F32 trans) { return 0; }
  285. virtual void resetAnimation(U32 tag) { }
  286. virtual F32 getAnimClipDuration(const char* clip) { return 0.0f; }
  287. void setTerrainAltitude(F32 alt) { mTerrain_altitude = alt; }
  288. void setInteriorAltitude(F32 alt) { mInterior_altitude = alt; }
  289. void getAltitudes(F32& terr_alt, F32& inter_alt) const { terr_alt = mTerrain_altitude; inter_alt = mInterior_altitude; }
  290. void setGroupIndex(S32 idx) { mGroup_index = idx; }
  291. S32 getGroupIndex() const { return mGroup_index; }
  292. bool inScope() const { return mIn_scope; }
  293. public:
  294. static void initPersistFields();
  295. static afxEffectWrapper* ew_create(afxChoreographer*, afxEffectWrapperData*, afxConstraintMgr*, F32 time_factor, S32 group_index=0);
  296. DECLARE_CONOBJECT(afxEffectWrapper);
  297. DECLARE_CATEGORY("AFX");
  298. };
  299. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  300. #endif // _AFX_EFFECT_WRAPPER_H_