2
0

afxEffectVector.cpp 9.1 KB

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