afxEffectWrapper.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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 "math/mathIO.h"
  26. #include "afx/ce/afxComponentEffect.h"
  27. #include "afx/afxResidueMgr.h"
  28. #include "afx/afxChoreographer.h"
  29. #include "afx/afxConstraint.h"
  30. #include "afx/xm/afxXfmMod.h"
  31. #include "afx/afxEffectWrapper.h"
  32. #include "afx/util/afxAnimCurve.h"
  33. #include "afx/util/afxEase.h"
  34. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  35. // afxEffectWrapperData
  36. IMPLEMENT_CO_DATABLOCK_V1(afxEffectBaseData);
  37. ConsoleDocClass( afxEffectBaseData,
  38. "@brief A datablock baseclass for afxEffectWrapperData and afxEffectGroupData.\n\n"
  39. "Not intended to be used directly, afxEffectBaseData exists to provide base member "
  40. "variables and generic functionality for the derived classes afxEffectWrapperData and "
  41. "afxEffectGroupData.\n\n"
  42. "@see afxEffectWrapperData\n\n"
  43. "@see afxEffectGroupData\n\n"
  44. "@ingroup afxEffects\n"
  45. "@ingroup AFX\n"
  46. "@ingroup Datablocks\n"
  47. );
  48. IMPLEMENT_CO_DATABLOCK_V1(afxEffectWrapperData);
  49. ConsoleDocClass( afxEffectWrapperData,
  50. "@brief A datablock that describes an Effect Wrapper.\n\n"
  51. "Conceptually an effect wrapper encloses a building-block effect and acts "
  52. "as a handle for adding the effect to a choreographer. Effect wrapper fields "
  53. "primarily deal with effect timing, constraints, and conditional effect execution.\n\n"
  54. "@see afxEffectBaseData\n\n"
  55. "@see afxEffectGroupData\n\n"
  56. "@ingroup afxEffects\n"
  57. "@ingroup AFX\n"
  58. "@ingroup Datablocks\n"
  59. );
  60. afxEffectWrapperData::afxEffectWrapperData()
  61. {
  62. effect_name = ST_NULLSTRING;
  63. effect_data = 0;
  64. effect_desc = 0;
  65. data_ID = 0;
  66. use_as_cons_obj = false;
  67. use_ghost_as_cons_obj = false;
  68. // constraint data
  69. cons_spec = ST_NULLSTRING;
  70. pos_cons_spec = ST_NULLSTRING;
  71. orient_cons_spec = ST_NULLSTRING;
  72. aim_cons_spec = StringTable->insert("camera");
  73. life_cons_spec = ST_NULLSTRING;
  74. // conditional execution flags
  75. effect_enabled = true;
  76. ranking_range.set(0,255);
  77. lod_range.set(0,255);
  78. life_conds = 0;
  79. for (S32 i = 0; i < MAX_CONDITION_STATES; i++)
  80. {
  81. exec_cond_on_bits[i] = 0;
  82. exec_cond_off_bits[i] = 0;
  83. exec_cond_bitmasks[i] = 0;
  84. }
  85. ewd_timing.lifetime = -1;
  86. user_fade_out_time = 0.0;
  87. is_looping = false;
  88. n_loops = 0;
  89. loop_gap_time = 0.0f;
  90. ignore_time_factor = false;
  91. propagate_time_factor = false;
  92. // residue settings
  93. // scaling factors
  94. rate_factor = 1.0f;
  95. scale_factor = 1.0f;
  96. dMemset(xfm_modifiers, 0, sizeof(xfm_modifiers));
  97. forced_bbox.minExtents.set(1,1,1);
  98. forced_bbox.maxExtents.set(-1,-1,-1);
  99. update_forced_bbox = false;
  100. // marked true if datablock ids need to
  101. // be converted into pointers
  102. do_id_convert = false;
  103. sort_priority = 0;
  104. direction.set(0,1,0);
  105. speed = 0.0f;
  106. mass = 1.0f;
  107. borrow_altitudes = false;
  108. vis_keys_spec = ST_NULLSTRING;
  109. vis_keys = 0;
  110. group_index = -1;
  111. inherit_timing = 0;
  112. }
  113. afxEffectWrapperData::afxEffectWrapperData(const afxEffectWrapperData& other, bool temp_clone) : afxEffectBaseData(other, temp_clone)
  114. {
  115. effect_name = other.effect_name;
  116. effect_data = other.effect_data;
  117. effect_desc = other.effect_desc;
  118. data_ID = other.data_ID;
  119. use_as_cons_obj = other.use_as_cons_obj;
  120. use_ghost_as_cons_obj = other.use_ghost_as_cons_obj;
  121. cons_spec = other.cons_spec;
  122. pos_cons_spec = other.pos_cons_spec;
  123. orient_cons_spec = other.orient_cons_spec;
  124. aim_cons_spec = other.aim_cons_spec;
  125. life_cons_spec = other.life_cons_spec;
  126. cons_def = other.cons_def;
  127. pos_cons_def = other.pos_cons_def;
  128. orient_cons_def = other.orient_cons_def;
  129. aim_cons_def = other.aim_cons_def;
  130. life_cons_def = other.life_cons_def;
  131. effect_enabled = other.effect_enabled;
  132. ranking_range = other.ranking_range;
  133. lod_range = other.lod_range;
  134. life_conds = other.life_conds;
  135. dMemcpy(exec_cond_on_bits, other.exec_cond_on_bits, sizeof(exec_cond_on_bits));
  136. dMemcpy(exec_cond_off_bits, other.exec_cond_off_bits, sizeof(exec_cond_off_bits));
  137. dMemcpy(exec_cond_bitmasks, other.exec_cond_bitmasks, sizeof(exec_cond_bitmasks));
  138. ewd_timing = other.ewd_timing;
  139. user_fade_out_time = other.user_fade_out_time;
  140. is_looping = other.is_looping;
  141. n_loops = other.n_loops;
  142. loop_gap_time = other.loop_gap_time;
  143. ignore_time_factor = other.ignore_time_factor;
  144. propagate_time_factor = other.propagate_time_factor;
  145. rate_factor = other.rate_factor;
  146. scale_factor = other.scale_factor;
  147. dMemcpy(xfm_modifiers, other.xfm_modifiers, sizeof(xfm_modifiers));
  148. forced_bbox = other.forced_bbox;
  149. update_forced_bbox = other.update_forced_bbox;
  150. do_id_convert = other.do_id_convert;
  151. sort_priority = other.sort_priority;
  152. direction = other.direction;
  153. speed = other.speed;
  154. mass = other.mass;
  155. borrow_altitudes = other.borrow_altitudes;
  156. vis_keys_spec = other.vis_keys_spec;
  157. vis_keys = other.vis_keys;
  158. if (other.vis_keys)
  159. {
  160. vis_keys = new afxAnimCurve();
  161. for (S32 i = 0; i < other.vis_keys->numKeys(); i++)
  162. {
  163. F32 when = other.vis_keys->getKeyTime(i);
  164. F32 what = other.vis_keys->getKeyValue(i);
  165. vis_keys->addKey(when, what);
  166. }
  167. }
  168. else
  169. vis_keys = 0;
  170. group_index = other.group_index;
  171. inherit_timing = other.inherit_timing;
  172. }
  173. afxEffectWrapperData::~afxEffectWrapperData()
  174. {
  175. if (vis_keys)
  176. delete vis_keys;
  177. }
  178. #define myOffset(field) Offset(field, afxEffectWrapperData)
  179. void afxEffectWrapperData::initPersistFields()
  180. {
  181. docsURL;
  182. // the wrapped effect
  183. addField("effect", TYPEID<SimDataBlock>(), myOffset(effect_data),
  184. "...");
  185. addField("effectName", TypeString, myOffset(effect_name),
  186. "...");
  187. // constraints
  188. addField("constraint", TypeString, myOffset(cons_spec),
  189. "...");
  190. addField("posConstraint", TypeString, myOffset(pos_cons_spec),
  191. "...");
  192. addField("posConstraint2", TypeString, myOffset(aim_cons_spec),
  193. "...");
  194. addField("orientConstraint", TypeString, myOffset(orient_cons_spec),
  195. "...");
  196. addField("lifeConstraint", TypeString, myOffset(life_cons_spec),
  197. "...");
  198. //
  199. addField("isConstraintSrc", TypeBool, myOffset(use_as_cons_obj),
  200. "...");
  201. addField("ghostIsConstraintSrc", TypeBool, myOffset(use_ghost_as_cons_obj),
  202. "...");
  203. addField("delay", TypeF32, myOffset(ewd_timing.delay),
  204. "...");
  205. addField("lifetime", TypeF32, myOffset(ewd_timing.lifetime),
  206. "...");
  207. addField("fadeInTime", TypeF32, myOffset(ewd_timing.fade_in_time),
  208. "...");
  209. addField("residueLifetime", TypeF32, myOffset(ewd_timing.residue_lifetime),
  210. "...");
  211. addField("fadeInEase", TypePoint2F, myOffset(ewd_timing.fadein_ease),
  212. "...");
  213. addField("fadeOutEase", TypePoint2F, myOffset(ewd_timing.fadeout_ease),
  214. "...");
  215. addField("lifetimeBias", TypeF32, myOffset(ewd_timing.life_bias),
  216. "...");
  217. addField("fadeOutTime", TypeF32, myOffset(user_fade_out_time),
  218. "...");
  219. addField("rateFactor", TypeF32, myOffset(rate_factor),
  220. "...");
  221. addField("scaleFactor", TypeF32, myOffset(scale_factor),
  222. "...");
  223. addField("isLooping", TypeBool, myOffset(is_looping),
  224. "...");
  225. addField("loopCount", TypeS32, myOffset(n_loops),
  226. "...");
  227. addField("loopGapTime", TypeF32, myOffset(loop_gap_time),
  228. "...");
  229. addField("ignoreTimeFactor", TypeBool, myOffset(ignore_time_factor),
  230. "...");
  231. addField("propagateTimeFactor", TypeBool, myOffset(propagate_time_factor),
  232. "...");
  233. addField("effectEnabled", TypeBool, myOffset(effect_enabled),
  234. "...");
  235. addField("rankingRange", TypeByteRange, myOffset(ranking_range),
  236. "...");
  237. addField("levelOfDetailRange", TypeByteRange, myOffset(lod_range),
  238. "...");
  239. addField("lifeConditions", TypeS32, myOffset(life_conds),
  240. "...");
  241. addField("execConditions", TypeS32, myOffset(exec_cond_on_bits), MAX_CONDITION_STATES,
  242. "...");
  243. addField("execOffConditions", TypeS32, myOffset(exec_cond_off_bits), MAX_CONDITION_STATES,
  244. "...");
  245. addField("xfmModifiers", TYPEID<afxXM_BaseData>(), myOffset(xfm_modifiers), MAX_XFM_MODIFIERS,
  246. "...");
  247. addField("forcedBBox", TypeBox3F, myOffset(forced_bbox),
  248. "...");
  249. addField("updateForcedBBox", TypeBool, myOffset(update_forced_bbox),
  250. "...");
  251. addField("sortPriority", TypeS8, myOffset(sort_priority),
  252. "...");
  253. addField("direction", TypePoint3F, myOffset(direction),
  254. "...");
  255. addField("speed", TypeF32, myOffset(speed),
  256. "...");
  257. addField("mass", TypeF32, myOffset(mass),
  258. "...");
  259. addField("borrowAltitudes", TypeBool, myOffset(borrow_altitudes),
  260. "...");
  261. addField("visibilityKeys", TypeString, myOffset(vis_keys_spec),
  262. "...");
  263. addField("groupIndex", TypeS32, myOffset(group_index),
  264. "...");
  265. addField("inheritGroupTiming", TypeS32, myOffset(inherit_timing),
  266. "...");
  267. Parent::initPersistFields();
  268. // disallow some field substitutions
  269. disableFieldSubstitutions("effect");
  270. onlyKeepClearSubstitutions("xfmModifiers"); // subs resolving to "~~", or "~0" are OK
  271. // Conditional Execution Flags
  272. Con::setIntVariable("$afx::DISABLED", DISABLED);
  273. Con::setIntVariable("$afx::ENABLED", ENABLED);
  274. Con::setIntVariable("$afx::FAILING", FAILING);
  275. Con::setIntVariable("$afx::DEAD", DEAD);
  276. Con::setIntVariable("$afx::ALIVE", ALIVE);
  277. Con::setIntVariable("$afx::DYING", DYING);
  278. }
  279. bool afxEffectWrapperData::onAdd()
  280. {
  281. if (Parent::onAdd() == false)
  282. return false;
  283. if (!effect_data)
  284. {
  285. if (!Sim::findObject((SimObjectId)data_ID, effect_data))
  286. {
  287. Con::errorf("afxEffectWrapperData::onAdd() -- bad datablockId: 0x%x", data_ID);
  288. return false;
  289. }
  290. }
  291. if (effect_data)
  292. {
  293. if (!afxEffectAdapterDesc::identifyEffect(this))
  294. {
  295. Con::errorf("afxEffectWrapperData::onAdd() -- unknown effect type.");
  296. return false;
  297. }
  298. }
  299. parse_cons_specs();
  300. parse_vis_keys();
  301. // figure out if fade-out is for effect of residue
  302. if (ewd_timing.residue_lifetime > 0)
  303. {
  304. ewd_timing.residue_fadetime = user_fade_out_time;
  305. ewd_timing.fade_out_time = 0.0f;
  306. }
  307. else
  308. {
  309. ewd_timing.residue_fadetime = 0.0f;
  310. ewd_timing.fade_out_time = user_fade_out_time;
  311. }
  312. // adjust fade-in time
  313. if (ewd_timing.lifetime >= 0)
  314. {
  315. ewd_timing.fade_in_time = getMin(ewd_timing.lifetime, ewd_timing.fade_in_time);
  316. }
  317. // adjust exec-conditions
  318. for (S32 i = 0; i < MAX_CONDITION_STATES; i++)
  319. exec_cond_bitmasks[i] = exec_cond_on_bits[i] | exec_cond_off_bits[i];
  320. return true;
  321. }
  322. void afxEffectWrapperData::packData(BitStream* stream)
  323. {
  324. Parent::packData(stream);
  325. writeDatablockID(stream, effect_data, mPacked);
  326. stream->writeString(effect_name);
  327. stream->writeString(cons_spec);
  328. stream->writeString(pos_cons_spec);
  329. stream->writeString(orient_cons_spec);
  330. stream->writeString(aim_cons_spec);
  331. stream->writeString(life_cons_spec);
  332. //
  333. stream->write(use_as_cons_obj);
  334. //stream->write(use_ghost_as_cons_obj);
  335. stream->writeFlag(effect_enabled);
  336. stream->write(ranking_range.low);
  337. stream->write(ranking_range.high);
  338. stream->write(lod_range.low);
  339. stream->write(lod_range.high);
  340. for (S32 i = 0; i < MAX_CONDITION_STATES; i++)
  341. {
  342. stream->write(exec_cond_on_bits[i]);
  343. stream->write(exec_cond_off_bits[i]);
  344. }
  345. stream->write(life_conds);
  346. stream->write(ewd_timing.delay);
  347. stream->write(ewd_timing.lifetime);
  348. stream->write(ewd_timing.fade_in_time);
  349. stream->write(user_fade_out_time);
  350. stream->write(is_looping);
  351. stream->write(n_loops);
  352. stream->write(loop_gap_time);
  353. stream->write(ignore_time_factor);
  354. stream->write(propagate_time_factor);
  355. stream->write(ewd_timing.residue_lifetime);
  356. stream->write(rate_factor);
  357. stream->write(scale_factor);
  358. // modifiers
  359. pack_mods(stream, xfm_modifiers, mPacked);
  360. mathWrite(*stream, forced_bbox);
  361. stream->write(update_forced_bbox);
  362. stream->write(sort_priority);
  363. mathWrite(*stream, direction);
  364. stream->write(speed);
  365. stream->write(mass);
  366. stream->write(borrow_altitudes);
  367. if (stream->writeFlag(vis_keys_spec != ST_NULLSTRING))
  368. stream->writeLongString(1023, vis_keys_spec);
  369. if (stream->writeFlag(group_index != -1))
  370. stream->write(group_index);
  371. stream->writeInt(inherit_timing, TIMING_BITS);
  372. }
  373. void afxEffectWrapperData::unpackData(BitStream* stream)
  374. {
  375. Parent::unpackData(stream);
  376. data_ID = readDatablockID(stream);
  377. effect_name = stream->readSTString();
  378. cons_spec = stream->readSTString();
  379. pos_cons_spec = stream->readSTString();
  380. orient_cons_spec = stream->readSTString();
  381. aim_cons_spec = stream->readSTString();
  382. life_cons_spec = stream->readSTString();
  383. //
  384. stream->read(&use_as_cons_obj);
  385. //stream->read(&use_ghost_as_cons_obj);
  386. effect_enabled = stream->readFlag();
  387. stream->read(&ranking_range.low);
  388. stream->read(&ranking_range.high);
  389. stream->read(&lod_range.low);
  390. stream->read(&lod_range.high);
  391. for (S32 i = 0; i < MAX_CONDITION_STATES; i++)
  392. {
  393. stream->read(&exec_cond_on_bits[i]);
  394. stream->read(&exec_cond_off_bits[i]);
  395. }
  396. stream->read(&life_conds);
  397. stream->read(&ewd_timing.delay);
  398. stream->read(&ewd_timing.lifetime);
  399. stream->read(&ewd_timing.fade_in_time);
  400. stream->read(&user_fade_out_time);
  401. stream->read(&is_looping);
  402. stream->read(&n_loops);
  403. stream->read(&loop_gap_time);
  404. stream->read(&ignore_time_factor);
  405. stream->read(&propagate_time_factor);
  406. stream->read(&ewd_timing.residue_lifetime);
  407. stream->read(&rate_factor);
  408. stream->read(&scale_factor);
  409. // modifiers
  410. do_id_convert = true;
  411. unpack_mods(stream, xfm_modifiers);
  412. mathRead(*stream, &forced_bbox);
  413. stream->read(&update_forced_bbox);
  414. stream->read(&sort_priority);
  415. mathRead(*stream, &direction);
  416. stream->read(&speed);
  417. stream->read(&mass);
  418. stream->read(&borrow_altitudes);
  419. if (stream->readFlag())
  420. {
  421. char buf[1024];
  422. stream->readLongString(1023, buf);
  423. vis_keys_spec = StringTable->insert(buf);
  424. }
  425. else
  426. vis_keys_spec = ST_NULLSTRING;
  427. if (stream->readFlag())
  428. stream->read(&group_index);
  429. else
  430. group_index = -1;
  431. inherit_timing = stream->readInt(TIMING_BITS);
  432. }
  433. /* static*/
  434. S32 num_modifiers(afxXM_BaseData* mods[])
  435. {
  436. S32 n_mods = 0;
  437. for (int i = 0; i < afxEffectDefs::MAX_XFM_MODIFIERS; i++)
  438. {
  439. if (mods[i])
  440. {
  441. if (i != n_mods)
  442. {
  443. mods[n_mods] = mods[i];
  444. mods[i] = 0;
  445. }
  446. n_mods++;
  447. }
  448. }
  449. return n_mods;
  450. }
  451. void afxEffectWrapperData::parse_cons_specs()
  452. {
  453. // parse the constraint specifications
  454. bool runs_on_s = runsOnServer();
  455. bool runs_on_c = runsOnClient();
  456. cons_def.parseSpec(cons_spec, runs_on_s, runs_on_c);
  457. pos_cons_def.parseSpec(pos_cons_spec, runs_on_s, runs_on_c);
  458. orient_cons_def.parseSpec(orient_cons_spec, runs_on_s, runs_on_c);
  459. aim_cons_def.parseSpec(aim_cons_spec, runs_on_s, runs_on_c);
  460. life_cons_def.parseSpec(life_cons_spec, runs_on_s, runs_on_c);
  461. if (cons_def.isDefined())
  462. {
  463. pos_cons_def = cons_def;
  464. if (!orient_cons_def.isDefined())
  465. orient_cons_def = cons_def;
  466. }
  467. }
  468. void afxEffectWrapperData::parse_vis_keys()
  469. {
  470. if (vis_keys_spec != ST_NULLSTRING)
  471. {
  472. if (vis_keys)
  473. delete vis_keys;
  474. vis_keys = new afxAnimCurve();
  475. char* keys_buffer = dStrdup(vis_keys_spec);
  476. char* key_token = dStrtok(keys_buffer, " \t");
  477. while (key_token != NULL)
  478. {
  479. char* colon = dStrchr(key_token, ':');
  480. if (colon)
  481. {
  482. *colon = '\0';
  483. F32 when = dAtof(key_token);
  484. F32 what = dAtof(colon+1);
  485. vis_keys->addKey(when, what);
  486. }
  487. key_token = dStrtok(NULL, " \t");
  488. }
  489. dFree(keys_buffer);
  490. vis_keys->sort();
  491. }
  492. }
  493. void afxEffectWrapperData::gather_cons_defs(Vector<afxConstraintDef>& defs)
  494. {
  495. if (pos_cons_def.isDefined())
  496. defs.push_back(pos_cons_def);
  497. if (orient_cons_def.isDefined())
  498. defs.push_back(orient_cons_def);
  499. if (aim_cons_def.isDefined())
  500. defs.push_back(aim_cons_def);
  501. if (life_cons_def.isDefined())
  502. defs.push_back(life_cons_def);
  503. afxComponentEffectData* ce_data = dynamic_cast<afxComponentEffectData*>(effect_data);
  504. if (ce_data)
  505. ce_data->gather_cons_defs(defs);
  506. }
  507. void afxEffectWrapperData::pack_mods(BitStream* stream, afxXM_BaseData* mods[], bool packed)
  508. {
  509. S32 n_mods = num_modifiers(mods);
  510. stream->writeInt(n_mods, 6);
  511. for (int i = 0; i < n_mods; i++)
  512. writeDatablockID(stream, mods[i], packed);
  513. }
  514. void afxEffectWrapperData::unpack_mods(BitStream* stream, afxXM_BaseData* mods[])
  515. {
  516. S32 n_mods = stream->readInt(6);
  517. for (int i = 0; i < n_mods; i++)
  518. mods[i] = (afxXM_BaseData*)(uintptr_t)readDatablockID(stream);
  519. }
  520. bool afxEffectWrapperData::preload(bool server, String &errorStr)
  521. {
  522. if (!Parent::preload(server, errorStr))
  523. return false;
  524. // Resolve objects transmitted from server
  525. if (!server)
  526. {
  527. if (do_id_convert)
  528. {
  529. for (int i = 0; i < MAX_XFM_MODIFIERS; i++)
  530. {
  531. SimObjectId db_id = SimObjectId((uintptr_t)xfm_modifiers[i]);
  532. if (db_id != 0)
  533. {
  534. // try to convert id to pointer
  535. if (!Sim::findObject(db_id, xfm_modifiers[i]))
  536. {
  537. Con::errorf("afxEffectWrapperData::preload() -- bad datablockId: 0x%x (xfm_modifiers[%d])",
  538. db_id, i);
  539. }
  540. }
  541. do_id_convert = false;
  542. }
  543. }
  544. }
  545. return true;
  546. }
  547. void afxEffectWrapperData::onPerformSubstitutions()
  548. {
  549. Parent::onPerformSubstitutions();
  550. parse_cons_specs();
  551. parse_vis_keys();
  552. if (ewd_timing.residue_lifetime > 0)
  553. {
  554. ewd_timing.residue_fadetime = user_fade_out_time;
  555. ewd_timing.fade_out_time = 0.0f;
  556. }
  557. else
  558. {
  559. ewd_timing.residue_fadetime = 0.0f;
  560. ewd_timing.fade_out_time = user_fade_out_time;
  561. }
  562. }
  563. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  564. // afxEffectWrapper
  565. IMPLEMENT_CONOBJECT(afxEffectWrapper);
  566. ConsoleDocClass( afxEffectWrapper,
  567. "@brief An Effect Wrapper as defined by an afxEffectWrapperData datablock.\n\n"
  568. "Conceptually an effect wrapper encloses a building-block effect and acts "
  569. "as a handle for adding the effect to a choreographer. Effect wrapper fields "
  570. "primarily deal with effect timing, constraints, and conditional effect execution.\n\n"
  571. "Not intended to be used directly, afxEffectWrapper is an internal baseclass used to "
  572. "implement effect-specific adapter classes.\n\n"
  573. "@ingroup afxEffects\n"
  574. "@ingroup AFX\n"
  575. );
  576. afxEffectWrapper::afxEffectWrapper()
  577. {
  578. mChoreographer = 0;
  579. mDatablock = 0;
  580. mCons_mgr = 0;
  581. mCond_alive = true;
  582. mElapsed = 0;
  583. mLife_end = 0;
  584. mLife_elapsed = 0;
  585. mStopped = false;
  586. mNum_updates = 0;
  587. mFade_value = 1.0f;
  588. mLast_fade_value = 0.0f;
  589. mFade_in_end = 0.0;
  590. mFade_out_start = 0.0f;
  591. mIn_scope = true;
  592. mIs_aborted = false;
  593. mDo_fade_inout = false;
  594. mDo_fades = false;
  595. mFull_lifetime = 0;
  596. mTime_factor = 1.0f;
  597. mProp_time_factor = 1.0f;
  598. mLive_scale_factor = 1.0f;
  599. mLive_fade_factor = 1.0f;
  600. mTerrain_altitude = -1.0f;
  601. mInterior_altitude = -1.0f;
  602. mGroup_index = 0;
  603. dMemset(mXfm_modifiers, 0, sizeof(mXfm_modifiers));
  604. }
  605. afxEffectWrapper::~afxEffectWrapper()
  606. {
  607. for (S32 i = 0; i < MAX_XFM_MODIFIERS; i++)
  608. if (mXfm_modifiers[i])
  609. delete mXfm_modifiers[i];
  610. if (mDatablock && mDatablock->effect_name != ST_NULLSTRING)
  611. {
  612. mChoreographer->removeNamedEffect(this);
  613. if (mDatablock->use_as_cons_obj && !mEffect_cons_id.undefined())
  614. mCons_mgr->setReferenceEffect(mEffect_cons_id, 0);
  615. }
  616. if (mDatablock && mDatablock->isTempClone())
  617. delete mDatablock;
  618. mDatablock = 0;
  619. }
  620. #undef myOffset
  621. #define myOffset(field) Offset(field, afxEffectWrapper)
  622. void afxEffectWrapper::initPersistFields()
  623. {
  624. docsURL;
  625. addField("liveScaleFactor", TypeF32, myOffset(mLive_scale_factor),
  626. "...");
  627. addField("liveFadeFactor", TypeF32, myOffset(mLive_fade_factor),
  628. "...");
  629. Parent::initPersistFields();
  630. }
  631. void afxEffectWrapper::ew_init(afxChoreographer* choreographer,
  632. afxEffectWrapperData* datablock,
  633. afxConstraintMgr* cons_mgr,
  634. F32 time_factor)
  635. {
  636. AssertFatal(choreographer != NULL, "Choreographer is missing.");
  637. AssertFatal(datablock != NULL, "Datablock is missing.");
  638. AssertFatal(cons_mgr != NULL, "Constraint manager is missing.");
  639. mChoreographer = choreographer;
  640. mDatablock = datablock;
  641. mCons_mgr = cons_mgr;
  642. ea_set_datablock(datablock->effect_data);
  643. mEW_timing = datablock->ewd_timing;
  644. if (mEW_timing.life_bias != 1.0f)
  645. {
  646. if (mEW_timing.lifetime > 0)
  647. mEW_timing.lifetime *= mEW_timing.life_bias;
  648. mEW_timing.fade_in_time *= mEW_timing.life_bias;
  649. mEW_timing.fade_out_time *= mEW_timing.life_bias;
  650. }
  651. mPos_cons_id = cons_mgr->getConstraintId(datablock->pos_cons_def);
  652. mOrient_cons_id = cons_mgr->getConstraintId(datablock->orient_cons_def);
  653. mAim_cons_id = cons_mgr->getConstraintId(datablock->aim_cons_def);
  654. mLife_cons_id = cons_mgr->getConstraintId(datablock->life_cons_def);
  655. mTime_factor = (datablock->ignore_time_factor) ? 1.0f : time_factor;
  656. if (datablock->propagate_time_factor)
  657. mProp_time_factor = time_factor;
  658. if (datablock->runsHere(choreographer->isServerObject()))
  659. {
  660. for (int i = 0; i < MAX_XFM_MODIFIERS && datablock->xfm_modifiers[i] != 0; i++)
  661. {
  662. mXfm_modifiers[i] = datablock->xfm_modifiers[i]->create(this, choreographer->isServerObject());
  663. AssertFatal(mXfm_modifiers[i] != 0, avar("Error, creation failed for xfm_modifiers[%d] of %s.", i, datablock->getName()));
  664. if (mXfm_modifiers[i] == 0)
  665. Con::errorf("Error, creation failed for xfm_modifiers[%d] of %s.", i, datablock->getName());
  666. }
  667. }
  668. if (datablock->effect_name != ST_NULLSTRING)
  669. {
  670. assignName(datablock->effect_name);
  671. choreographer->addNamedEffect(this);
  672. if (datablock->use_as_cons_obj)
  673. {
  674. mEffect_cons_id = cons_mgr->setReferenceEffect(datablock->effect_name, this);
  675. if (mEffect_cons_id.undefined() && datablock->isTempClone() && datablock->runsHere(choreographer->isServerObject()))
  676. mEffect_cons_id = cons_mgr->createReferenceEffect(datablock->effect_name, this);
  677. }
  678. }
  679. }
  680. void afxEffectWrapper::prestart()
  681. {
  682. // modify timing values by time_factor
  683. if (mEW_timing.lifetime > 0)
  684. mEW_timing.lifetime *= mTime_factor;
  685. mEW_timing.delay *= mTime_factor;
  686. mEW_timing.fade_in_time *= mTime_factor;
  687. mEW_timing.fade_out_time *= mTime_factor;
  688. if (mEW_timing.lifetime < 0)
  689. {
  690. mFull_lifetime = INFINITE_LIFETIME;
  691. mLife_end = INFINITE_LIFETIME;
  692. }
  693. else
  694. {
  695. mFull_lifetime = mEW_timing.lifetime + mEW_timing.fade_out_time;
  696. mLife_end = mEW_timing.delay + mEW_timing.lifetime;
  697. }
  698. if ((mEW_timing.fade_in_time + mEW_timing.fade_out_time) > 0.0f)
  699. {
  700. mFade_in_end = mEW_timing.delay + mEW_timing.fade_in_time;
  701. if (mFull_lifetime == INFINITE_LIFETIME)
  702. mFade_out_start = INFINITE_LIFETIME;
  703. else
  704. mFade_out_start = mEW_timing.delay + mEW_timing.lifetime;
  705. mDo_fade_inout = true;
  706. }
  707. if (!mDo_fade_inout && mDatablock->vis_keys != NULL && mDatablock->vis_keys->numKeys() > 0)
  708. {
  709. //do_fades = true;
  710. mFade_out_start = mEW_timing.delay + mEW_timing.lifetime;
  711. }
  712. }
  713. bool afxEffectWrapper::start(F32 timestamp)
  714. {
  715. if (!ea_is_enabled())
  716. {
  717. Con::warnf("afxEffectWrapper::start() -- effect type of %s is currently disabled.", mDatablock->getName());
  718. return false;
  719. }
  720. afxConstraint* life_constraint = getLifeConstraint();
  721. if (life_constraint)
  722. mCond_alive = life_constraint->getLivingState();
  723. mElapsed = timestamp;
  724. for (S32 i = 0; i < MAX_XFM_MODIFIERS; i++)
  725. {
  726. if (!mXfm_modifiers[i])
  727. break;
  728. else
  729. mXfm_modifiers[i]->start(timestamp);
  730. }
  731. if (!ea_start())
  732. {
  733. // subclass should print error message if applicable, so no message here.
  734. return false;
  735. }
  736. update(0.0f);
  737. return !isAborted();
  738. }
  739. bool afxEffectWrapper::test_life_conds()
  740. {
  741. afxConstraint* life_constraint = getLifeConstraint();
  742. if (!life_constraint || mDatablock->life_conds == 0)
  743. return true;
  744. S32 now_state = life_constraint->getDamageState();
  745. if ((mDatablock->life_conds & DEAD) != 0 && now_state == ShapeBase::Disabled)
  746. return true;
  747. if ((mDatablock->life_conds & ALIVE) != 0 && now_state == ShapeBase::Enabled)
  748. return true;
  749. if ((mDatablock->life_conds & DYING) != 0)
  750. return (mCond_alive && now_state == ShapeBase::Disabled);
  751. return false;
  752. }
  753. bool afxEffectWrapper::update(F32 dt)
  754. {
  755. mElapsed += dt;
  756. // life_elapsed won't exceed full_lifetime
  757. mLife_elapsed = getMin(mElapsed - mEW_timing.delay, mFull_lifetime);
  758. // update() returns early if elapsed is outside of active timing range
  759. // (delay <= elapsed <= delay+lifetime)
  760. // note: execution is always allowed beyond this point at least once,
  761. // even if elapsed exceeds the lifetime.
  762. if (mElapsed < mEW_timing.delay)
  763. {
  764. setScopeStatus(false);
  765. return false;
  766. }
  767. if (!mDatablock->requiresStop(mEW_timing) && mEW_timing.lifetime < 0)
  768. {
  769. F32 afterlife = mElapsed - mEW_timing.delay;
  770. if (afterlife > 1.0f || ((afterlife > 0.0f) && (mNum_updates > 0)))
  771. {
  772. setScopeStatus(mEW_timing.residue_lifetime > 0.0f);
  773. return false;
  774. }
  775. }
  776. else
  777. {
  778. F32 afterlife = mElapsed - (mFull_lifetime + mEW_timing.delay);
  779. if (afterlife > 1.0f || ((afterlife > 0.0f) && (mNum_updates > 0)))
  780. {
  781. setScopeStatus(mEW_timing.residue_lifetime > 0.0f);
  782. return false;
  783. }
  784. }
  785. // first time here, test if required conditions for effect are met
  786. if (mNum_updates == 0)
  787. {
  788. if (!test_life_conds())
  789. {
  790. mElapsed = mFull_lifetime + mEW_timing.delay;
  791. setScopeStatus(false);
  792. mNum_updates++;
  793. return false;
  794. }
  795. }
  796. setScopeStatus(true);
  797. mNum_updates++;
  798. // calculate current fade value if enabled
  799. if (mDo_fade_inout)
  800. {
  801. if (mEW_timing.fade_in_time > 0 && mElapsed <= mFade_in_end)
  802. {
  803. F32 t = mClampF((mElapsed - mEW_timing.delay)/ mEW_timing.fade_in_time, 0.0f, 1.0f);
  804. mFade_value = afxEase::t(t, mEW_timing.fadein_ease.x, mEW_timing.fadein_ease.y);
  805. mDo_fades = true;
  806. }
  807. else if (mElapsed > mFade_out_start)
  808. {
  809. if (mEW_timing.fade_out_time == 0)
  810. mFade_value = 0.0f;
  811. else
  812. {
  813. F32 t = mClampF(1.0f-(mElapsed - mFade_out_start)/ mEW_timing.fade_out_time, 0.0f, 1.0f);
  814. mFade_value = afxEase::t(t, mEW_timing.fadeout_ease.x, mEW_timing.fadeout_ease.y);
  815. }
  816. mDo_fades = true;
  817. }
  818. else
  819. {
  820. mFade_value = 1.0f;
  821. mDo_fades = false;
  822. }
  823. }
  824. else
  825. {
  826. mFade_value = 1.0;
  827. mDo_fades = false;
  828. }
  829. if (mDatablock->vis_keys && mDatablock->vis_keys->numKeys() > 0)
  830. {
  831. F32 vis = mDatablock->vis_keys->evaluate(mElapsed - mEW_timing.delay);
  832. mFade_value *= mClampF(vis, 0.0f, 1.0f);
  833. mDo_fades = (mFade_value < 1.0f);
  834. }
  835. // DEAL WITH CONSTRAINTS
  836. afxXM_Params params;
  837. Point3F& CONS_POS = params.pos;
  838. MatrixF& CONS_XFM = params.ori;
  839. Point3F& CONS_AIM = params.pos2;
  840. Point3F& CONS_SCALE = params.scale;
  841. LinearColorF& CONS_COLOR = params.color;
  842. afxConstraint* pos_constraint = getPosConstraint();
  843. if (pos_constraint)
  844. {
  845. bool valid = pos_constraint->getPosition(CONS_POS, mDatablock->pos_cons_def.mHistory_time);
  846. if (!valid)
  847. getUnconstrainedPosition(CONS_POS);
  848. setScopeStatus(valid);
  849. if (valid && mDatablock->borrow_altitudes)
  850. {
  851. F32 terr_alt, inter_alt;
  852. if (pos_constraint->getAltitudes(terr_alt, inter_alt))
  853. {
  854. mTerrain_altitude = terr_alt;
  855. mInterior_altitude = inter_alt;
  856. }
  857. }
  858. }
  859. else
  860. {
  861. getUnconstrainedPosition(CONS_POS);
  862. setScopeStatus(false);
  863. }
  864. afxConstraint* orient_constraint = getOrientConstraint();
  865. if (orient_constraint)
  866. {
  867. orient_constraint->getTransform(CONS_XFM, mDatablock->pos_cons_def.mHistory_time);
  868. }
  869. else
  870. {
  871. getUnconstrainedTransform(CONS_XFM);
  872. }
  873. afxConstraint* aim_constraint = getAimConstraint();
  874. if (aim_constraint)
  875. aim_constraint->getPosition(CONS_AIM, mDatablock->pos_cons_def.mHistory_time);
  876. else
  877. CONS_AIM.zero();
  878. CONS_SCALE.set(mDatablock->scale_factor, mDatablock->scale_factor, mDatablock->scale_factor);
  879. /*
  880. if (datablock->isPositional() && CONS_POS.isZero() && in_scope)
  881. Con::errorf("#EFFECT AT ORIGIN [%s] time=%g", datablock->getName(), dt);
  882. */
  883. getBaseColor(CONS_COLOR);
  884. params.vis = mFade_value;
  885. // apply modifiers
  886. for (int i = 0; i < MAX_XFM_MODIFIERS; i++)
  887. {
  888. if (!mXfm_modifiers[i])
  889. break;
  890. else
  891. mXfm_modifiers[i]->updateParams(dt, mLife_elapsed, params);
  892. }
  893. // final pos/orient is determined
  894. mUpdated_xfm = CONS_XFM;
  895. mUpdated_pos = CONS_POS;
  896. mUpdated_aim = CONS_AIM;
  897. mUpdated_xfm.setPosition(mUpdated_pos);
  898. mUpdated_scale = CONS_SCALE;
  899. mUpdated_color = CONS_COLOR;
  900. if (params.vis > 1.0f)
  901. mFade_value = 1.0f;
  902. else
  903. mFade_value = params.vis;
  904. if (mLast_fade_value != mFade_value)
  905. {
  906. mDo_fades = true;
  907. mLast_fade_value = mFade_value;
  908. }
  909. else
  910. {
  911. mDo_fades = (mFade_value < 1.0f);
  912. }
  913. if (!ea_update(dt))
  914. {
  915. mIs_aborted = true;
  916. Con::errorf("afxEffectWrapper::update() -- effect %s ended unexpectedly.", mDatablock->getName());
  917. }
  918. return true;
  919. }
  920. void afxEffectWrapper::stop()
  921. {
  922. if (!mDatablock->requiresStop(mEW_timing))
  923. return;
  924. mStopped = true;
  925. // this resets full_lifetime so it starts to shrink or fade
  926. if (mFull_lifetime == INFINITE_LIFETIME)
  927. {
  928. mFull_lifetime = (mElapsed - mEW_timing.delay) + afterStopTime();
  929. mLife_end = mElapsed;
  930. if (mEW_timing.fade_out_time > 0)
  931. mFade_out_start = mElapsed;
  932. }
  933. }
  934. void afxEffectWrapper::cleanup(bool was_stopped)
  935. {
  936. ea_finish(was_stopped);
  937. if (!mEffect_cons_id.undefined())
  938. {
  939. mCons_mgr->setReferenceEffect(mEffect_cons_id, 0);
  940. mEffect_cons_id = afxConstraintID();
  941. }
  942. }
  943. void afxEffectWrapper::setScopeStatus(bool in_scope)
  944. {
  945. if (mIn_scope != in_scope)
  946. {
  947. mIn_scope = in_scope;
  948. ea_set_scope_status(in_scope);
  949. }
  950. }
  951. bool afxEffectWrapper::isDone()
  952. {
  953. if (!mDatablock->is_looping)
  954. return (mElapsed >= (mLife_end + mEW_timing.fade_out_time));
  955. return false;
  956. }
  957. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  958. // static
  959. afxEffectWrapper* afxEffectWrapper::ew_create(afxChoreographer* choreographer,
  960. afxEffectWrapperData* datablock,
  961. afxConstraintMgr* cons_mgr,
  962. F32 time_factor,
  963. S32 group_index)
  964. {
  965. afxEffectWrapper* adapter = datablock->effect_desc->create();
  966. if (adapter)
  967. {
  968. adapter->mGroup_index = (datablock->group_index != -1) ? datablock->group_index : group_index;
  969. adapter->ew_init(choreographer, datablock, cons_mgr, time_factor);
  970. }
  971. return adapter;
  972. }
  973. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  974. Vector<afxEffectAdapterDesc*>* afxEffectAdapterDesc::adapters = 0;
  975. afxEffectAdapterDesc::afxEffectAdapterDesc()
  976. {
  977. if (!adapters)
  978. adapters = new Vector<afxEffectAdapterDesc*>;
  979. adapters->push_back(this);
  980. }
  981. bool afxEffectAdapterDesc::identifyEffect(afxEffectWrapperData* ew)
  982. {
  983. if (!ew || !ew->effect_data)
  984. {
  985. Con::errorf("afxEffectAdapterDesc::identifyEffect() -- effect datablock was not specified.");
  986. return false;
  987. }
  988. if (!adapters)
  989. {
  990. Con::errorf("afxEffectAdapterDesc::identifyEffect() -- adapter registration list has not been allocated.");
  991. return false;
  992. }
  993. if (adapters->size() == 0)
  994. {
  995. Con::errorf("afxEffectAdapterDesc::identifyEffect() -- no effect adapters have been registered.");
  996. return false;
  997. }
  998. for (S32 i = 0; i < adapters->size(); i++)
  999. {
  1000. if ((*adapters)[i]->testEffectType(ew->effect_data))
  1001. {
  1002. ew->effect_desc = (*adapters)[i];
  1003. (*adapters)[i]->prepEffect(ew);
  1004. return true;
  1005. }
  1006. }
  1007. Con::errorf("afxEffectAdapterDesc::identifyEffect() -- effect %s has an undefined type. -- %d",
  1008. ew->effect_data->getName(), adapters->size());
  1009. return false;
  1010. }
  1011. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//