afxEffectVector.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. #include "afx/arcaneFX.h"
  25. #include "afxChoreographer.h"
  26. #include "afxEffectVector.h"
  27. #include "afxConstraint.h"
  28. #include "afxEffectWrapper.h"
  29. #include "afxEffectGroup.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. void afxEffectVector::filter_client_server()
  32. {
  33. if (empty())
  34. return;
  35. for (S32 i = 0; i < mFX_v->size(); i++)
  36. {
  37. if ((*mFX_v)[i]->mDatablock->runsHere(mOn_server))
  38. mFX_v2->push_back((*mFX_v)[i]);
  39. else
  40. {
  41. delete (*mFX_v)[i];
  42. (*mFX_v)[i] = 0;
  43. }
  44. }
  45. swap_vecs();
  46. mFX_v2->clear();
  47. }
  48. void afxEffectVector::calc_fx_dur_and_afterlife()
  49. {
  50. mTotal_fx_dur = 0.0f;
  51. mAfter_life = 0.0f;
  52. if (empty())
  53. return;
  54. for (S32 i = 0; i < mFX_v->size(); i++)
  55. {
  56. afxEffectWrapper* ew = (*mFX_v)[i];
  57. if (ew)
  58. {
  59. F32 ew_dur;
  60. if (ew->mEW_timing.lifetime < 0)
  61. {
  62. if (mPhrase_dur > ew->mEW_timing.delay)
  63. ew_dur = mPhrase_dur + ew->afterStopTime();
  64. else
  65. ew_dur = ew->mEW_timing.delay + ew->afterStopTime();
  66. }
  67. else
  68. ew_dur = ew->mEW_timing.delay + ew->mEW_timing.lifetime + ew->mEW_timing.fade_out_time;
  69. if (ew_dur > mTotal_fx_dur)
  70. mTotal_fx_dur = ew_dur;
  71. F32 after = ew->afterStopTime();
  72. if (after > mAfter_life)
  73. mAfter_life = after;
  74. }
  75. }
  76. }
  77. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  78. afxEffectVector::afxEffectVector()
  79. {
  80. mFX_v = 0;
  81. mFX_v2 = 0;
  82. mActive = false;
  83. mOn_server = false;
  84. mTotal_fx_dur = 0;
  85. mAfter_life = 0;
  86. }
  87. afxEffectVector::~afxEffectVector()
  88. {
  89. stop(true);
  90. delete mFX_v;
  91. delete mFX_v2;
  92. }
  93. void afxEffectVector::effects_init(afxChoreographer* chor, afxEffectList& effects, bool will_stop, F32 time_factor,
  94. S32 group_index, const afxGroupTimingData* group_timing)
  95. {
  96. afxConstraintMgr* cons_mgr = chor->getConstraintMgr();
  97. for (S32 i = 0; i < effects.size(); i++)
  98. {
  99. if (dynamic_cast<afxEffectGroupData*>(effects[i]))
  100. {
  101. afxEffectGroupData* eg = (afxEffectGroupData*)effects[i];
  102. if (eg->getSubstitutionCount() > 0)
  103. {
  104. // clone the datablock and perform substitutions
  105. afxEffectGroupData* orig_db = eg;
  106. eg = new afxEffectGroupData(*orig_db, true);
  107. orig_db->performSubstitutions(eg, chor, group_index);
  108. }
  109. if (eg->group_enabled)
  110. {
  111. if (eg->assign_idx)
  112. {
  113. for (S32 j = 0; j < eg->group_count; j++)
  114. effects_init(chor, eg->fx_list, will_stop, time_factor, j+eg->idx_offset, &eg->timing);
  115. }
  116. else
  117. {
  118. for (S32 j = 0; j < eg->group_count; j++)
  119. effects_init(chor, eg->fx_list, will_stop, time_factor, group_index, &eg->timing);
  120. }
  121. }
  122. if (eg->isTempClone())
  123. delete eg;
  124. }
  125. else if (dynamic_cast<afxEffectWrapperData*>(effects[i]))
  126. {
  127. afxEffectWrapperData* ewd = (afxEffectWrapperData*)effects[i];
  128. if (ewd->getSubstitutionCount() > 0)
  129. {
  130. // clone the ewd and perform substitutions
  131. afxEffectWrapperData* orig_db = ewd;
  132. ewd = new afxEffectWrapperData(*orig_db, true);
  133. orig_db->performSubstitutions(ewd, chor, group_index);
  134. }
  135. if (ewd->effect_enabled)
  136. {
  137. static afxEffectTimingData inherited_timing;
  138. bool use_inherited_timing = false;
  139. if (ewd->inherit_timing != 0)
  140. {
  141. if (group_timing)
  142. {
  143. inherited_timing = ewd->ewd_timing;
  144. if ((ewd->inherit_timing & afxEffectDefs::TIMING_DELAY) != 0)
  145. inherited_timing.delay = group_timing->delay;
  146. if ((ewd->inherit_timing & afxEffectDefs::TIMING_LIFETIME) != 0)
  147. inherited_timing.lifetime = group_timing->lifetime;
  148. if ((ewd->inherit_timing & afxEffectDefs::TIMING_FADE_IN) != 0)
  149. inherited_timing.fade_in_time = group_timing->fade_in_time;
  150. if ((ewd->inherit_timing & afxEffectDefs::TIMING_FADE_OUT) != 0)
  151. inherited_timing.fade_out_time = group_timing->fade_out_time;
  152. }
  153. else
  154. {
  155. Con::warnf("afxEffectVector::effects_init() -- %s::inheritGroupTiming is non-zero but wrapper is not in a group.");
  156. }
  157. }
  158. const afxEffectTimingData& timing = (use_inherited_timing) ? inherited_timing : ewd->ewd_timing;
  159. if ( (will_stop || !ewd->requiresStop(timing)) &&
  160. (chor->testRanking(ewd->ranking_range.low, ewd->ranking_range.high)) &&
  161. (chor->testLevelOfDetail(ewd->lod_range.low, ewd->lod_range.high)) &&
  162. (ewd->testExecConditions(chor->getExecConditions()))
  163. )
  164. {
  165. afxEffectWrapper* effect;
  166. effect = afxEffectWrapper::ew_create(chor, ewd, cons_mgr, time_factor, group_index);
  167. if (effect)
  168. mFX_v->push_back(effect);
  169. }
  170. }
  171. else
  172. {
  173. if (ewd->isTempClone())
  174. delete ewd;
  175. }
  176. }
  177. }
  178. }
  179. void afxEffectVector::ev_init(afxChoreographer* chor, afxEffectList& effects, bool on_server,
  180. bool will_stop, F32 time_factor, F32 phrase_dur, S32 group_index)
  181. {
  182. mOn_server = on_server;
  183. mPhrase_dur = phrase_dur;
  184. mFX_v = new Vector<afxEffectWrapper*>;
  185. effects_init(chor, effects, will_stop, time_factor, group_index);
  186. mFX_v2 = new Vector<afxEffectWrapper*>(mFX_v->size());
  187. }
  188. void afxEffectVector::start(F32 timestamp)
  189. {
  190. if (empty())
  191. return;
  192. // At this point both client and server effects are in the list.
  193. // Timing adjustments are made during prestart().
  194. for (S32 i = 0; i < mFX_v->size(); i++)
  195. (*mFX_v)[i]->prestart();
  196. // duration and afterlife values are pre-calculated here
  197. calc_fx_dur_and_afterlife();
  198. // now we filter out client-only or server-only effects that
  199. // don't belong here,
  200. filter_client_server();
  201. mActive = true;
  202. for (S32 j = 0; j < mFX_v->size(); j++)
  203. {
  204. if ((*mFX_v)[j]->start(timestamp))
  205. mFX_v2->push_back((*mFX_v)[j]);
  206. else
  207. {
  208. delete (*mFX_v)[j];
  209. (*mFX_v)[j] = 0;
  210. }
  211. }
  212. swap_vecs();
  213. mFX_v2->clear();
  214. }
  215. void afxEffectVector::update(F32 dt)
  216. {
  217. if (empty())
  218. {
  219. mActive = false;
  220. return;
  221. }
  222. for (int i = 0; i < mFX_v->size(); i++)
  223. {
  224. (*mFX_v)[i]->update(dt);
  225. if ((*mFX_v)[i]->isDone() || (*mFX_v)[i]->isAborted())
  226. {
  227. // effect has ended, cleanup and delete
  228. (*mFX_v)[i]->cleanup();
  229. delete (*mFX_v)[i];
  230. (*mFX_v)[i] = 0;
  231. }
  232. else
  233. {
  234. // effect is still going, so keep it around
  235. mFX_v2->push_back((*mFX_v)[i]);
  236. }
  237. }
  238. swap_vecs();
  239. mFX_v2->clear();
  240. if (empty())
  241. {
  242. mActive = false;
  243. delete mFX_v; mFX_v =0;
  244. delete mFX_v2; mFX_v2 = 0;
  245. }
  246. }
  247. void afxEffectVector::stop(bool force_cleanup)
  248. {
  249. if (empty())
  250. {
  251. mActive = false;
  252. return;
  253. }
  254. for (int i = 0; i < mFX_v->size(); i++)
  255. {
  256. (*mFX_v)[i]->stop();
  257. if (force_cleanup || (*mFX_v)[i]->deleteWhenStopped())
  258. {
  259. // effect is over when stopped, cleanup and delete
  260. (*mFX_v)[i]->cleanup();
  261. delete (*mFX_v)[i];
  262. (*mFX_v)[i] = 0;
  263. }
  264. else
  265. {
  266. // effect needs to fadeout or something, so keep it around
  267. mFX_v2->push_back((*mFX_v)[i]);
  268. }
  269. }
  270. swap_vecs();
  271. mFX_v2->clear();
  272. if (empty())
  273. {
  274. mActive = false;
  275. delete mFX_v; mFX_v =0;
  276. delete mFX_v2; mFX_v2 = 0;
  277. }
  278. }
  279. void afxEffectVector::interrupt()
  280. {
  281. if (empty())
  282. {
  283. mActive = false;
  284. return;
  285. }
  286. for (int i = 0; i < mFX_v->size(); i++)
  287. {
  288. (*mFX_v)[i]->stop();
  289. (*mFX_v)[i]->cleanup();
  290. delete (*mFX_v)[i];
  291. (*mFX_v)[i] = 0;
  292. }
  293. swap_vecs();
  294. mFX_v2->clear();
  295. if (empty())
  296. {
  297. mActive = false;
  298. delete mFX_v; mFX_v =0;
  299. delete mFX_v2; mFX_v2 = 0;
  300. }
  301. }
  302. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//