afxEffectWrapper.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. };
  84. //class afxEffectWrapperData : public GameBaseData, public afxEffectDefs
  85. class afxEffectWrapperData : public afxEffectBaseData
  86. {
  87. //typedef GameBaseData Parent;
  88. typedef afxEffectBaseData Parent;
  89. bool do_id_convert;
  90. public:
  91. enum { MAX_CONDITION_STATES = 4 };
  92. public:
  93. StringTableEntry effect_name;
  94. bool use_as_cons_obj;
  95. bool use_ghost_as_cons_obj;
  96. StringTableEntry cons_spec;
  97. StringTableEntry pos_cons_spec;
  98. StringTableEntry orient_cons_spec;
  99. StringTableEntry aim_cons_spec;
  100. StringTableEntry life_cons_spec;
  101. //
  102. afxConstraintDef cons_def;
  103. afxConstraintDef pos_cons_def;
  104. afxConstraintDef orient_cons_def;
  105. afxConstraintDef aim_cons_def;
  106. afxConstraintDef life_cons_def;
  107. afxEffectTimingData ewd_timing;
  108. U32 inherit_timing;
  109. F32 scale_factor; // scale size if applicable
  110. F32 rate_factor; // scale rate if applicable
  111. F32 user_fade_out_time;
  112. bool is_looping;
  113. U32 n_loops;
  114. F32 loop_gap_time;
  115. bool ignore_time_factor;
  116. bool propagate_time_factor;
  117. ByteRange ranking_range;
  118. ByteRange lod_range;
  119. S32 life_conds;
  120. bool effect_enabled;
  121. U32 exec_cond_on_bits[MAX_CONDITION_STATES];
  122. U32 exec_cond_off_bits[MAX_CONDITION_STATES];
  123. U32 exec_cond_bitmasks[MAX_CONDITION_STATES];
  124. S32 data_ID;
  125. afxXM_BaseData* xfm_modifiers[MAX_XFM_MODIFIERS];
  126. Box3F forced_bbox;
  127. bool update_forced_bbox;
  128. S8 sort_priority;
  129. Point3F direction;
  130. F32 speed;
  131. F32 mass;
  132. bool borrow_altitudes;
  133. StringTableEntry vis_keys_spec;
  134. afxAnimCurve* vis_keys;
  135. SimDataBlock* effect_data;
  136. afxEffectAdapterDesc* effect_desc;
  137. S32 group_index;
  138. void parse_cons_specs();
  139. void parse_vis_keys();
  140. void gather_cons_defs(Vector<afxConstraintDef>& defs) override;
  141. void pack_mods(BitStream*, afxXM_BaseData* mods[], bool packed);
  142. void unpack_mods(BitStream*, afxXM_BaseData* mods[]);
  143. public:
  144. /*C*/ afxEffectWrapperData();
  145. /*C*/ afxEffectWrapperData(const afxEffectWrapperData&, bool = false);
  146. /*D*/ ~afxEffectWrapperData();
  147. bool onAdd() override;
  148. void packData(BitStream*) override;
  149. void unpackData(BitStream*) override;
  150. bool preload(bool server, String &errorStr) override;
  151. void onPerformSubstitutions() override;
  152. bool requiresStop(const afxEffectTimingData& timing) { return effect_desc->requiresStop(this, timing); }
  153. bool runsOnServer() { return effect_desc->runsOnServer(this); }
  154. bool runsOnClient() { return effect_desc->runsOnClient(this); }
  155. bool runsHere(bool server_here) { return (server_here) ? runsOnServer() : runsOnClient(); }
  156. bool isPositional() { return effect_desc->isPositional(this); }
  157. bool testExecConditions(U32 conditions);
  158. F32 afterStopTime() { return ewd_timing.fade_out_time; }
  159. bool allowSubstitutions() const override { return true; }
  160. static void initPersistFields();
  161. DECLARE_CONOBJECT(afxEffectWrapperData);
  162. };
  163. inline bool afxEffectWrapperData::testExecConditions(U32 conditions)
  164. {
  165. if (exec_cond_bitmasks[0] == 0)
  166. return true;
  167. if ((exec_cond_bitmasks[0] & conditions) == exec_cond_on_bits[0])
  168. return true;
  169. for (S32 i = 1; i < MAX_CONDITION_STATES; i++)
  170. {
  171. if (exec_cond_bitmasks[i] == 0)
  172. return false;
  173. if ((exec_cond_bitmasks[i] & conditions) == exec_cond_on_bits[i])
  174. return true;
  175. }
  176. return false;
  177. }
  178. typedef Vector<afxEffectBaseData*> afxEffectList;
  179. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  180. // afxEffectWrapper
  181. //
  182. // NOTE -- this not a subclass of GameBase... it is only meant to exist on
  183. // the client-side.
  184. class ShapeBase;
  185. class GameBase;
  186. class TSShape;
  187. class TSShapeInstance;
  188. class SceneObject;
  189. class afxConstraint;
  190. class afxConstraintMgr;
  191. class afxChoreographer;
  192. class afxXM_Base;
  193. class afxEffectWrapper : public SimObject, public afxEffectDefs
  194. {
  195. typedef SimObject Parent;
  196. friend class afxEffectVector;
  197. private:
  198. bool test_life_conds();
  199. protected:
  200. afxEffectWrapperData* mDatablock;
  201. afxEffectTimingData mEW_timing;
  202. F32 mFade_in_end;
  203. F32 mFade_out_start;
  204. F32 mFull_lifetime;
  205. F32 mTime_factor;
  206. F32 mProp_time_factor;
  207. afxChoreographer* mChoreographer;
  208. afxConstraintMgr* mCons_mgr;
  209. afxConstraintID mPos_cons_id;
  210. afxConstraintID mOrient_cons_id;
  211. afxConstraintID mAim_cons_id;
  212. afxConstraintID mLife_cons_id;
  213. afxConstraintID mEffect_cons_id;
  214. F32 mElapsed;
  215. F32 mLife_elapsed;
  216. F32 mLife_end;
  217. bool mStopped;
  218. bool mCond_alive;
  219. U32 mNum_updates;
  220. MatrixF mUpdated_xfm;
  221. Point3F mUpdated_pos;
  222. Point3F mUpdated_aim;
  223. Point3F mUpdated_scale;
  224. LinearColorF mUpdated_color;
  225. F32 mFade_value;
  226. F32 mLast_fade_value;
  227. bool mDo_fade_inout;
  228. bool mDo_fades;
  229. bool mIn_scope;
  230. bool mIs_aborted;
  231. afxXM_Base* mXfm_modifiers[MAX_XFM_MODIFIERS];
  232. F32 mLive_scale_factor;
  233. F32 mLive_fade_factor;
  234. F32 mTerrain_altitude;
  235. F32 mInterior_altitude;
  236. S32 mGroup_index;
  237. public:
  238. /*C*/ afxEffectWrapper();
  239. virtual ~afxEffectWrapper();
  240. void ew_init(afxChoreographer*, afxEffectWrapperData*, afxConstraintMgr*,
  241. F32 time_factor);
  242. F32 getFullLifetime() { return mEW_timing.lifetime + mEW_timing.fade_out_time; }
  243. F32 getTimeFactor() { return mTime_factor; }
  244. afxConstraint* getPosConstraint() { return mCons_mgr->getConstraint(mPos_cons_id); }
  245. afxConstraint* getOrientConstraint() { return mCons_mgr->getConstraint(mOrient_cons_id); }
  246. afxConstraint* getAimConstraint() { return mCons_mgr->getConstraint(mAim_cons_id); }
  247. afxConstraint* getLifeConstraint() { return mCons_mgr->getConstraint(mLife_cons_id); }
  248. afxChoreographer* getChoreographer() { return mChoreographer; }
  249. virtual bool isDone();
  250. virtual bool deleteWhenStopped() { return false; }
  251. F32 afterStopTime() { return mEW_timing.fade_out_time; }
  252. bool isAborted() const { return mIs_aborted; }
  253. void prestart();
  254. bool start(F32 timestamp);
  255. bool update(F32 dt);
  256. void stop();
  257. void cleanup(bool was_stopped=false);
  258. void setScopeStatus(bool flag);
  259. virtual void ea_set_datablock(SimDataBlock*) { }
  260. virtual bool ea_start() { return true; }
  261. virtual bool ea_update(F32 dt) { return true; }
  262. virtual void ea_finish(bool was_stopped) { }
  263. virtual void ea_set_scope_status(bool flag) { }
  264. virtual bool ea_is_enabled() { return true; }
  265. virtual SceneObject* ea_get_scene_object() const { return 0; }
  266. U32 ea_get_triggers() const { return 0; }
  267. void getUpdatedPosition(Point3F& pos) { pos = mUpdated_pos;}
  268. void getUpdatedTransform(MatrixF& xfm) { xfm = mUpdated_xfm; }
  269. void getUpdatedScale(Point3F& scale) { scale = mUpdated_scale; }
  270. void getUpdatedColor(LinearColorF& color) { color = mUpdated_color; }
  271. virtual void getUpdatedBoxCenter(Point3F& pos) { pos = mUpdated_pos;}
  272. virtual void getUnconstrainedPosition(Point3F& pos) { pos.zero();}
  273. virtual void getUnconstrainedTransform(MatrixF& xfm) { xfm.identity(); }
  274. virtual void getBaseColor(LinearColorF& color) { color.set(1.0f, 1.0f, 1.0f, 1.0f); }
  275. SceneObject* getSceneObject() const { return ea_get_scene_object(); }
  276. U32 getTriggers() const { return ea_get_triggers(); }
  277. F32 getMass() { return mDatablock->mass; }
  278. Point3F getDirection() { return mDatablock->direction; }
  279. F32 getSpeed() { return mDatablock->speed; }
  280. virtual TSShape* getTSShape() { return 0; }
  281. virtual TSShapeInstance* getTSShapeInstance() { return 0; }
  282. virtual U32 setAnimClip(const char* clip, F32 pos, F32 rate, F32 trans) { return 0; }
  283. virtual void resetAnimation(U32 tag) { }
  284. virtual F32 getAnimClipDuration(const char* clip) { return 0.0f; }
  285. void setTerrainAltitude(F32 alt) { mTerrain_altitude = alt; }
  286. void setInteriorAltitude(F32 alt) { mInterior_altitude = alt; }
  287. void getAltitudes(F32& terr_alt, F32& inter_alt) const { terr_alt = mTerrain_altitude; inter_alt = mInterior_altitude; }
  288. void setGroupIndex(S32 idx) { mGroup_index = idx; }
  289. S32 getGroupIndex() const { return mGroup_index; }
  290. bool inScope() const { return mIn_scope; }
  291. public:
  292. static void initPersistFields();
  293. static afxEffectWrapper* ew_create(afxChoreographer*, afxEffectWrapperData*, afxConstraintMgr*, F32 time_factor, S32 group_index=0);
  294. DECLARE_CONOBJECT(afxEffectWrapper);
  295. };
  296. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  297. #endif // _AFX_EFFECT_WRAPPER_H_