afxEffectron.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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 "console/engineAPI.h"
  26. #include "T3D/gameBase/gameConnection.h"
  27. #include "sfx/sfxSystem.h"
  28. #include "math/mathIO.h"
  29. #include "afx/afxChoreographer.h"
  30. #include "afx/afxEffectron.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. // afxEffectronData::ewValidator
  33. //
  34. // When an effect is added using "addEffect", this validator intercepts the value
  35. // and adds it to the dynamic effects list.
  36. //
  37. void afxEffectronData::ewValidator::validateType(SimObject* object, void* typePtr)
  38. {
  39. afxEffectronData* eff_data = dynamic_cast<afxEffectronData*>(object);
  40. afxEffectBaseData** ew = (afxEffectBaseData**)(typePtr);
  41. if (eff_data && ew)
  42. {
  43. eff_data->fx_list.push_back(*ew);
  44. *ew = 0;
  45. }
  46. }
  47. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  48. class EffectronFinishStartupEvent : public SimEvent
  49. {
  50. public:
  51. void process(SimObject* obj)
  52. {
  53. afxEffectron* eff = dynamic_cast<afxEffectron*>(obj);
  54. if (eff)
  55. eff->finish_startup();
  56. }
  57. };
  58. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  59. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  60. // afxEffectronData
  61. IMPLEMENT_CO_DATABLOCK_V1(afxEffectronData);
  62. ConsoleDocClass( afxEffectronData,
  63. "@brief Defines the properties of an afxEffectron.\n\n"
  64. "@ingroup afxChoreographers\n"
  65. "@ingroup AFX\n"
  66. "@ingroup Datablocks\n"
  67. );
  68. afxEffectronData::afxEffectronData()
  69. {
  70. duration = 0.0f;
  71. n_loops = 1;
  72. // dummy entry holds effect-wrapper pointer while a special validator
  73. // grabs it and adds it to an appropriate effects list
  74. dummy_fx_entry = NULL;
  75. // marked true if datablock ids need to
  76. // be converted into pointers
  77. do_id_convert = false;
  78. }
  79. afxEffectronData::afxEffectronData(const afxEffectronData& other, bool temp_clone) : afxChoreographerData(other, temp_clone)
  80. {
  81. duration = other.duration;
  82. n_loops = other.n_loops;
  83. dummy_fx_entry = other.dummy_fx_entry;
  84. do_id_convert = other.do_id_convert;
  85. fx_list = other.fx_list;
  86. }
  87. void afxEffectronData::reloadReset()
  88. {
  89. fx_list.clear();
  90. }
  91. #define myOffset(field) Offset(field, afxEffectronData)
  92. void afxEffectronData::initPersistFields()
  93. {
  94. addField("duration", TypeF32, myOffset(duration),
  95. "...");
  96. addField("numLoops", TypeS32, myOffset(n_loops),
  97. "...");
  98. // effect lists
  99. // for each of these, dummy_fx_entry is set and then a validator adds it to the appropriate effects list
  100. static ewValidator emptyValidator(0);
  101. addFieldV("addEffect", TYPEID<afxEffectBaseData>(), myOffset(dummy_fx_entry), &emptyValidator,
  102. "...");
  103. Parent::initPersistFields();
  104. // disallow some field substitutions
  105. disableFieldSubstitutions("addEffect");
  106. }
  107. bool afxEffectronData::onAdd()
  108. {
  109. if (Parent::onAdd() == false)
  110. return false;
  111. return true;
  112. }
  113. void afxEffectronData::pack_fx(BitStream* stream, const afxEffectList& fx, bool packed)
  114. {
  115. stream->writeInt(fx.size(), EFFECTS_PER_PHRASE_BITS);
  116. for (int i = 0; i < fx.size(); i++)
  117. writeDatablockID(stream, fx[i], packed);
  118. }
  119. void afxEffectronData::unpack_fx(BitStream* stream, afxEffectList& fx)
  120. {
  121. fx.clear();
  122. S32 n_fx = stream->readInt(EFFECTS_PER_PHRASE_BITS);
  123. for (int i = 0; i < n_fx; i++)
  124. fx.push_back((afxEffectWrapperData*)(uintptr_t)readDatablockID(stream));
  125. }
  126. void afxEffectronData::packData(BitStream* stream)
  127. {
  128. Parent::packData(stream);
  129. stream->write(duration);
  130. stream->write(n_loops);
  131. pack_fx(stream, fx_list, mPacked);
  132. }
  133. void afxEffectronData::unpackData(BitStream* stream)
  134. {
  135. Parent::unpackData(stream);
  136. stream->read(&duration);
  137. stream->read(&n_loops);
  138. do_id_convert = true;
  139. unpack_fx(stream, fx_list);
  140. }
  141. bool afxEffectronData::preload(bool server, String &errorStr)
  142. {
  143. if (!Parent::preload(server, errorStr))
  144. return false;
  145. // Resolve objects transmitted from server
  146. if (!server)
  147. {
  148. if (do_id_convert)
  149. {
  150. for (S32 i = 0; i < fx_list.size(); i++)
  151. {
  152. SimObjectId db_id = SimObjectId((uintptr_t)fx_list[i]);
  153. if (db_id != 0)
  154. {
  155. // try to convert id to pointer
  156. if (!Sim::findObject(db_id, fx_list[i]))
  157. {
  158. Con::errorf(ConsoleLogEntry::General,
  159. "afxEffectronData::preload() -- bad datablockId: 0x%x",
  160. db_id);
  161. }
  162. }
  163. }
  164. do_id_convert = false;
  165. }
  166. }
  167. return true;
  168. }
  169. void afxEffectronData::gatherConstraintDefs(Vector<afxConstraintDef>& defs)
  170. {
  171. afxConstraintDef::gather_cons_defs(defs, fx_list);
  172. }
  173. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  174. DefineEngineMethod(afxEffectronData, reset, void, (),,
  175. "Resets an effectron datablock during reload.\n\n"
  176. "@ingroup AFX")
  177. {
  178. object->reloadReset();
  179. }
  180. DefineEngineMethod(afxEffectronData, pushEffect, void, (afxEffectBaseData* effect),,
  181. "Adds an effect (wrapper or group) to an effectron's phase.\n\n"
  182. "@ingroup AFX")
  183. {
  184. if (!effect)
  185. {
  186. Con::errorf("afxEffectronData::pushEffect() -- missing afxEffectWrapperData.");
  187. return;
  188. }
  189. object->fx_list.push_back(effect);
  190. }
  191. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  192. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  193. // afxEffectron
  194. IMPLEMENT_CO_NETOBJECT_V1(afxEffectron);
  195. ConsoleDocClass( afxEffectron,
  196. "@brief A basic effects choreographer.\n\n"
  197. "@ingroup afxChoreographers\n"
  198. "@ingroup AFX\n"
  199. );
  200. StringTableEntry afxEffectron::CAMERA_CONS;
  201. StringTableEntry afxEffectron::LISTENER_CONS;
  202. void afxEffectron::init()
  203. {
  204. // setup static predefined constraint names
  205. if (CAMERA_CONS == 0)
  206. {
  207. CAMERA_CONS = StringTable->insert("camera");
  208. LISTENER_CONS = StringTable->insert("listener");
  209. }
  210. // afxEffectron is always in scope, however the effects used
  211. // do their own scoping in that they will shut off if their
  212. // position constraint leaves scope.
  213. //
  214. // note -- ghosting is delayed until constraint
  215. // initialization is done.
  216. //
  217. //mNetFlags.set(Ghostable | ScopeAlways);
  218. mNetFlags.clear(Ghostable | ScopeAlways);
  219. datablock = NULL;
  220. exeblock = NULL;
  221. constraints_initialized = false;
  222. scoping_initialized = false;
  223. effect_state = (U8) INACTIVE_STATE;
  224. effect_elapsed = 0;
  225. // define named constraints
  226. constraint_mgr->defineConstraint(CAMERA_CONSTRAINT, CAMERA_CONS);
  227. constraint_mgr->defineConstraint(POINT_CONSTRAINT, LISTENER_CONS);
  228. active_phrase = NULL;
  229. time_factor = 1.0f;
  230. camera_cons_obj = 0;
  231. marks_mask = 0;
  232. }
  233. afxEffectron::afxEffectron()
  234. {
  235. started_with_newop = true;
  236. init();
  237. }
  238. afxEffectron::afxEffectron(bool not_default)
  239. {
  240. started_with_newop = false;
  241. init();
  242. }
  243. afxEffectron::~afxEffectron()
  244. {
  245. delete active_phrase;
  246. if (datablock && datablock->isTempClone())
  247. {
  248. delete datablock;
  249. datablock = 0;
  250. }
  251. }
  252. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  253. // STANDARD OVERLOADED METHODS //
  254. bool afxEffectron::onNewDataBlock(GameBaseData* dptr, bool reload)
  255. {
  256. datablock = dynamic_cast<afxEffectronData*>(dptr);
  257. if (!datablock || !Parent::onNewDataBlock(dptr, reload))
  258. return false;
  259. if (isServerObject() && started_with_newop)
  260. {
  261. // copy dynamic fields from the datablock but
  262. // don't replace fields with a value
  263. assignDynamicFieldsFrom(dptr, arcaneFX::sParameterFieldPrefix, true);
  264. }
  265. exeblock = datablock;
  266. if (isClientObject())
  267. {
  268. // make a temp datablock clone if there are substitutions
  269. if (datablock->getSubstitutionCount() > 0)
  270. {
  271. afxEffectronData* orig_db = datablock;
  272. datablock = new afxEffectronData(*orig_db, true);
  273. exeblock = orig_db;
  274. // Don't perform substitutions yet, the effectrons's dynamic fields haven't
  275. // arrived yet and the substitutions may refer to them. Hold off and do
  276. // in in the onAdd() method.
  277. }
  278. }
  279. else if (started_with_newop)
  280. {
  281. // make a temp datablock clone if there are substitutions
  282. if (datablock->getSubstitutionCount() > 0)
  283. {
  284. afxEffectronData* orig_db = datablock;
  285. datablock = new afxEffectronData(*orig_db, true);
  286. exeblock = orig_db;
  287. orig_db->performSubstitutions(datablock, this, ranking);
  288. }
  289. }
  290. return true;
  291. }
  292. void afxEffectron::processTick(const Move* m)
  293. {
  294. Parent::processTick(m);
  295. // don't process moves or client ticks
  296. if (m != 0 || isClientObject())
  297. return;
  298. process_server();
  299. }
  300. void afxEffectron::advanceTime(F32 dt)
  301. {
  302. Parent::advanceTime(dt);
  303. process_client(dt);
  304. }
  305. bool afxEffectron::onAdd()
  306. {
  307. if (!Parent::onAdd())
  308. return false;
  309. if (isClientObject())
  310. {
  311. if (datablock->isTempClone())
  312. {
  313. afxEffectronData* orig_db = (afxEffectronData*)exeblock;
  314. orig_db->performSubstitutions(datablock, this, ranking);
  315. }
  316. }
  317. else if (started_with_newop && !postpone_activation)
  318. {
  319. if (!activationCallInit())
  320. return false;
  321. activate();
  322. }
  323. return true;
  324. }
  325. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  326. U32 afxEffectron::packUpdate(NetConnection* conn, U32 mask, BitStream* stream)
  327. {
  328. S32 mark_stream_pos = stream->getCurPos();
  329. U32 retMask = Parent::packUpdate(conn, mask, stream);
  330. // InitialUpdate
  331. if (stream->writeFlag(mask & InitialUpdateMask))
  332. {
  333. // pack extra object's ghost index or scope id if not yet ghosted
  334. if (stream->writeFlag(dynamic_cast<NetObject*>(mExtra) != 0))
  335. {
  336. NetObject* net_extra = (NetObject*)mExtra;
  337. S32 ghost_idx = conn->getGhostIndex(net_extra);
  338. if (stream->writeFlag(ghost_idx != -1))
  339. stream->writeRangedU32(U32(ghost_idx), 0, NetConnection::MaxGhostCount);
  340. else
  341. {
  342. if (stream->writeFlag(net_extra->getScopeId() > 0))
  343. {
  344. stream->writeInt(net_extra->getScopeId(), NetObject::SCOPE_ID_BITS);
  345. }
  346. }
  347. }
  348. stream->write(time_factor);
  349. GameConnection* gconn = dynamic_cast<GameConnection*>(conn);
  350. bool zoned_in = (gconn) ? gconn->isZonedIn() : false;
  351. if (stream->writeFlag(zoned_in))
  352. pack_constraint_info(conn, stream);
  353. }
  354. // StateEvent or SyncEvent
  355. if (stream->writeFlag((mask & StateEventMask) || (mask & SyncEventMask)))
  356. {
  357. stream->write(marks_mask);
  358. stream->write(effect_state);
  359. stream->write(effect_elapsed);
  360. }
  361. // SyncEvent
  362. bool do_sync_event = ((mask & SyncEventMask) && !(mask & InitialUpdateMask));
  363. if (stream->writeFlag(do_sync_event))
  364. {
  365. pack_constraint_info(conn, stream);
  366. }
  367. check_packet_usage(conn, stream, mark_stream_pos, "afxEffectron:");
  368. AssertISV(stream->isValid(), "afxEffectron::packUpdate(): write failure occurred, possibly caused by packet-size overrun.");
  369. return retMask;
  370. }
  371. //~~~~~~~~~~~~~~~~~~~~//
  372. void afxEffectron::unpackUpdate(NetConnection * conn, BitStream * stream)
  373. {
  374. Parent::unpackUpdate(conn, stream);
  375. bool initial_update = false;
  376. bool zoned_in = true;
  377. bool do_sync_event = false;
  378. U8 new_marks_mask = 0;
  379. U8 new_state = INACTIVE_STATE;
  380. F32 new_elapsed = 0;
  381. // InitialUpdate Only
  382. if (stream->readFlag())
  383. {
  384. initial_update = true;
  385. // extra sent
  386. if (stream->readFlag())
  387. {
  388. // cleanup?
  389. if (stream->readFlag()) // is ghost_idx
  390. {
  391. S32 ghost_idx = stream->readRangedU32(0, NetConnection::MaxGhostCount);
  392. mExtra = dynamic_cast<SimObject*>(conn->resolveGhost(ghost_idx));
  393. }
  394. else
  395. {
  396. if (stream->readFlag()) // has scope_id
  397. {
  398. // JTF NOTE: U16 extra_scope_id = stream->readInt(NetObject::SCOPE_ID_BITS);
  399. stream->readInt(NetObject::SCOPE_ID_BITS);
  400. }
  401. }
  402. }
  403. stream->read(&time_factor);
  404. // if client is marked as fully zoned in
  405. if ((zoned_in = stream->readFlag()) == true)
  406. {
  407. unpack_constraint_info(conn, stream);
  408. init_constraints();
  409. }
  410. }
  411. // StateEvent or SyncEvent
  412. // this state data is sent for both state-events and
  413. // sync-events
  414. if (stream->readFlag())
  415. {
  416. stream->read(&new_marks_mask);
  417. stream->read(&new_state);
  418. stream->read(&new_elapsed);
  419. marks_mask = new_marks_mask;
  420. }
  421. // SyncEvent
  422. do_sync_event = stream->readFlag();
  423. if (do_sync_event)
  424. {
  425. unpack_constraint_info(conn, stream);
  426. init_constraints();
  427. }
  428. //~~~~~~~~~~~~~~~~~~~~//
  429. if (!zoned_in)
  430. effect_state = LATE_STATE;
  431. // need to adjust state info to get all synced up with spell on server
  432. if (do_sync_event && !initial_update)
  433. sync_client(new_marks_mask, new_state, new_elapsed);
  434. }
  435. void afxEffectron::sync_with_clients()
  436. {
  437. setMaskBits(SyncEventMask);
  438. }
  439. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  440. // private
  441. bool afxEffectron::state_expired()
  442. {
  443. afxPhrase* phrase = (effect_state == ACTIVE_STATE) ? active_phrase : NULL;
  444. if (phrase)
  445. {
  446. if (phrase->expired(effect_elapsed))
  447. return (!phrase->recycle(effect_elapsed));
  448. return false;
  449. }
  450. return true;
  451. }
  452. void afxEffectron::init_constraints()
  453. {
  454. if (constraints_initialized)
  455. {
  456. //Con::printf("CONSTRAINTS ALREADY INITIALIZED");
  457. return;
  458. }
  459. Vector<afxConstraintDef> defs;
  460. datablock->gatherConstraintDefs(defs);
  461. constraint_mgr->initConstraintDefs(defs, isServerObject());
  462. if (isServerObject())
  463. {
  464. // find local camera
  465. camera_cons_obj = get_camera();
  466. if (camera_cons_obj)
  467. camera_cons_id = constraint_mgr->setReferenceObject(CAMERA_CONS, camera_cons_obj);
  468. }
  469. else // if (isClientObject())
  470. {
  471. // find local camera
  472. camera_cons_obj = get_camera();
  473. if (camera_cons_obj)
  474. camera_cons_id = constraint_mgr->setReferenceObject(CAMERA_CONS, camera_cons_obj);
  475. // find local listener
  476. Point3F listener_pos;
  477. listener_pos = SFX->getListener().getTransform().getPosition();
  478. listener_cons_id = constraint_mgr->setReferencePoint(LISTENER_CONS, listener_pos);
  479. }
  480. constraint_mgr->adjustProcessOrdering(this);
  481. constraints_initialized = true;
  482. }
  483. void afxEffectron::init_scoping()
  484. {
  485. if (scoping_initialized)
  486. {
  487. //Con::printf("SCOPING ALREADY INITIALIZED");
  488. return;
  489. }
  490. if (isServerObject())
  491. {
  492. if (explicit_clients.size() > 0)
  493. {
  494. for (U32 i = 0; i < explicit_clients.size(); i++)
  495. explicit_clients[i]->objectLocalScopeAlways(this);
  496. }
  497. else
  498. {
  499. mNetFlags.set(Ghostable);
  500. setScopeAlways();
  501. }
  502. scoping_initialized = true;
  503. }
  504. }
  505. void afxEffectron::setup_active_fx()
  506. {
  507. active_phrase = new afxPhrase(isServerObject(), /*willStop=*/true);
  508. if (active_phrase)
  509. {
  510. active_phrase->init(datablock->fx_list, datablock->duration, this, time_factor, datablock->n_loops);
  511. }
  512. }
  513. bool afxEffectron::cleanup_over()
  514. {
  515. if (active_phrase && !active_phrase->isEmpty())
  516. return false;
  517. return true;
  518. }
  519. void afxEffectron::inflictDamage(const char * label, const char* flavor, SimObjectId target_id,
  520. F32 amount, U8 n, F32 ad_amount, F32 radius, Point3F pos, F32 impulse)
  521. {
  522. char *posArg = Con::getArgBuffer(64);
  523. dSprintf(posArg, 64, "%f %f %f", pos.x, pos.y, pos.z);
  524. Con::executef(exeblock, "onDamage",
  525. getIdString(),
  526. label,
  527. flavor,
  528. Con::getIntArg(target_id),
  529. Con::getFloatArg(amount),
  530. Con::getIntArg(n),
  531. posArg,
  532. Con::getFloatArg(ad_amount),
  533. Con::getFloatArg(radius),
  534. Con::getFloatArg(impulse));
  535. }
  536. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  537. // private
  538. void afxEffectron::process_server()
  539. {
  540. if (effect_state != INACTIVE_STATE)
  541. effect_elapsed += TickSec;
  542. U8 pending_state = effect_state;
  543. // check for state changes
  544. switch (effect_state)
  545. {
  546. case INACTIVE_STATE:
  547. if (marks_mask & MARK_ACTIVATE)
  548. pending_state = ACTIVE_STATE;
  549. break;
  550. case ACTIVE_STATE:
  551. if (marks_mask & MARK_INTERRUPT)
  552. pending_state = CLEANUP_STATE;
  553. else if (marks_mask & MARK_SHUTDOWN)
  554. pending_state = CLEANUP_STATE;
  555. else if (state_expired())
  556. pending_state = CLEANUP_STATE;
  557. break;
  558. case CLEANUP_STATE:
  559. if (cleanup_over())
  560. pending_state = DONE_STATE;
  561. break;
  562. }
  563. if (effect_state != pending_state)
  564. change_state_s(pending_state);
  565. if (effect_state == INACTIVE_STATE)
  566. return;
  567. //--------------------------//
  568. // sample the constraints
  569. constraint_mgr->sample(TickSec, Platform::getVirtualMilliseconds());
  570. if (active_phrase)
  571. active_phrase->update(TickSec, effect_elapsed);
  572. }
  573. void afxEffectron::change_state_s(U8 pending_state)
  574. {
  575. if (effect_state == pending_state)
  576. return;
  577. switch (effect_state)
  578. {
  579. case INACTIVE_STATE:
  580. break;
  581. case ACTIVE_STATE:
  582. leave_active_state_s();
  583. break;
  584. case CLEANUP_STATE:
  585. break;
  586. case DONE_STATE:
  587. break;
  588. }
  589. effect_state = pending_state;
  590. switch (pending_state)
  591. {
  592. case INACTIVE_STATE:
  593. break;
  594. case ACTIVE_STATE:
  595. enter_active_state_s();
  596. break;
  597. case CLEANUP_STATE:
  598. enter_cleanup_state_s();
  599. break;
  600. case DONE_STATE:
  601. enter_done_state_s();
  602. break;
  603. }
  604. }
  605. void afxEffectron::enter_done_state_s()
  606. {
  607. postEvent(DEACTIVATE_EVENT);
  608. F32 done_time = effect_elapsed;
  609. if (active_phrase)
  610. {
  611. F32 phrase_done;
  612. if (active_phrase->willStop() && active_phrase->isInfinite())
  613. phrase_done = effect_elapsed + active_phrase->calcAfterLife();
  614. else
  615. phrase_done = active_phrase->calcDoneTime();
  616. if (phrase_done > done_time)
  617. done_time = phrase_done;
  618. }
  619. F32 time_left = done_time - effect_elapsed;
  620. if (time_left < 0)
  621. time_left = 0;
  622. Sim::postEvent(this, new ObjectDeleteEvent, Sim::getCurrentTime() + time_left*1000 + 500);
  623. // CALL SCRIPT afxEffectronData::onDeactivate(%eff)
  624. Con::executef(exeblock, "onDeactivate", getIdString());
  625. }
  626. void afxEffectron::enter_active_state_s()
  627. {
  628. // stamp constraint-mgr starting time
  629. constraint_mgr->setStartTime(Platform::getVirtualMilliseconds());
  630. effect_elapsed = 0;
  631. setup_dynamic_constraints();
  632. // start casting effects
  633. setup_active_fx();
  634. if (active_phrase)
  635. active_phrase->start(effect_elapsed, effect_elapsed);
  636. }
  637. void afxEffectron::leave_active_state_s()
  638. {
  639. if (active_phrase)
  640. active_phrase->stop(effect_elapsed);
  641. }
  642. void afxEffectron::enter_cleanup_state_s()
  643. {
  644. postEvent(SHUTDOWN_EVENT);
  645. }
  646. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  647. // private
  648. void afxEffectron::process_client(F32 dt)
  649. {
  650. effect_elapsed += dt;
  651. U8 pending_state = effect_state;
  652. // check for state changes
  653. switch (effect_state)
  654. {
  655. case INACTIVE_STATE:
  656. if (marks_mask & MARK_ACTIVATE)
  657. pending_state = ACTIVE_STATE;
  658. break;
  659. case ACTIVE_STATE:
  660. if (marks_mask & MARK_INTERRUPT)
  661. pending_state = CLEANUP_STATE;
  662. else if (marks_mask & MARK_SHUTDOWN)
  663. pending_state = CLEANUP_STATE;
  664. else if (state_expired())
  665. pending_state = CLEANUP_STATE;
  666. break;
  667. case CLEANUP_STATE:
  668. if (cleanup_over())
  669. pending_state = DONE_STATE;
  670. break;
  671. }
  672. if (effect_state != pending_state)
  673. change_state_c(pending_state);
  674. if (effect_state == INACTIVE_STATE)
  675. return;
  676. //--------------------------//
  677. // update the listener constraint position
  678. if (!listener_cons_id.undefined())
  679. {
  680. Point3F listener_pos;
  681. listener_pos = SFX->getListener().getTransform().getPosition();
  682. constraint_mgr->setReferencePoint(listener_cons_id, listener_pos);
  683. }
  684. // find local camera position
  685. Point3F cam_pos;
  686. SceneObject* current_cam = get_camera(&cam_pos);
  687. // detect camera changes
  688. if (!camera_cons_id.undefined() && current_cam != camera_cons_obj)
  689. {
  690. constraint_mgr->setReferenceObject(camera_cons_id, current_cam);
  691. camera_cons_obj = current_cam;
  692. }
  693. // sample the constraints
  694. constraint_mgr->sample(dt, Platform::getVirtualMilliseconds(), (current_cam) ? &cam_pos : 0);
  695. // update active effects lists
  696. if (active_phrase)
  697. active_phrase->update(dt, effect_elapsed);
  698. }
  699. void afxEffectron::change_state_c(U8 pending_state)
  700. {
  701. if (effect_state == pending_state)
  702. return;
  703. switch (effect_state)
  704. {
  705. case INACTIVE_STATE:
  706. break;
  707. case ACTIVE_STATE:
  708. leave_active_state_c();
  709. break;
  710. case CLEANUP_STATE:
  711. break;
  712. case DONE_STATE:
  713. break;
  714. }
  715. effect_state = pending_state;
  716. switch (pending_state)
  717. {
  718. case INACTIVE_STATE:
  719. break;
  720. case ACTIVE_STATE:
  721. enter_active_state_c(effect_elapsed);
  722. break;
  723. case CLEANUP_STATE:
  724. break;
  725. case DONE_STATE:
  726. break;
  727. }
  728. }
  729. void afxEffectron::enter_active_state_c(F32 starttime)
  730. {
  731. // stamp constraint-mgr starting time
  732. constraint_mgr->setStartTime(Platform::getVirtualMilliseconds() - (U32)(effect_elapsed*1000));
  733. ///effect_elapsed = 0;
  734. setup_dynamic_constraints();
  735. setup_active_fx();
  736. if (active_phrase)
  737. active_phrase->start(starttime, effect_elapsed);
  738. }
  739. void afxEffectron::leave_active_state_c()
  740. {
  741. if (active_phrase)
  742. active_phrase->stop(effect_elapsed);
  743. }
  744. void afxEffectron::sync_client(U16 marks, U8 state, F32 elapsed)
  745. {
  746. //Con::printf("SYNC marks=%d old_state=%d state=%d elapsed=%g",
  747. // marks, effect_state, state, elapsed);
  748. if (effect_state != LATE_STATE)
  749. return;
  750. marks_mask = marks;
  751. // don't want to be started on late zoning clients
  752. if (!datablock->exec_on_new_clients)
  753. {
  754. effect_state = DONE_STATE;
  755. }
  756. // it looks like we're ghosting pretty late and
  757. // should just return to the inactive state.
  758. else if (marks & (MARK_INTERRUPT | MARK_DEACTIVATE | MARK_SHUTDOWN))
  759. {
  760. effect_state = DONE_STATE;
  761. }
  762. // it looks like we should be in the active state.
  763. else if (marks & MARK_ACTIVATE)
  764. {
  765. effect_state = ACTIVE_STATE;
  766. effect_elapsed = elapsed;
  767. enter_active_state_c(0.0);
  768. }
  769. }
  770. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  771. // public:
  772. void afxEffectron::postEvent(U8 event)
  773. {
  774. setMaskBits(StateEventMask);
  775. switch (event)
  776. {
  777. case ACTIVATE_EVENT:
  778. marks_mask |= MARK_ACTIVATE;
  779. break;
  780. case SHUTDOWN_EVENT:
  781. marks_mask |= MARK_SHUTDOWN;
  782. break;
  783. case DEACTIVATE_EVENT:
  784. marks_mask |= MARK_DEACTIVATE;
  785. break;
  786. case INTERRUPT_EVENT:
  787. marks_mask |= MARK_INTERRUPT;
  788. break;
  789. }
  790. }
  791. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  792. void afxEffectron::finish_startup()
  793. {
  794. init_constraints();
  795. init_scoping();
  796. postEvent(afxEffectron::ACTIVATE_EVENT);
  797. }
  798. // static
  799. afxEffectron*
  800. afxEffectron::start_effect(afxEffectronData* datablock, SimObject* extra)
  801. {
  802. AssertFatal(datablock != NULL, "Datablock is missing.");
  803. afxEffectronData* exeblock = datablock;
  804. SimObject* param_holder = new SimObject();
  805. if (!param_holder->registerObject())
  806. {
  807. Con::errorf("afxEffectron: failed to register parameter object.");
  808. delete param_holder;
  809. return 0;
  810. }
  811. param_holder->assignDynamicFieldsFrom(datablock, arcaneFX::sParameterFieldPrefix);
  812. if (extra)
  813. {
  814. // copy dynamic fields from the extra object to the param holder
  815. param_holder->assignDynamicFieldsFrom(extra, arcaneFX::sParameterFieldPrefix);
  816. }
  817. // CALL SCRIPT afxEffectronData::onPreactivate(%params, %extra)
  818. ConsoleValue cValue = Con::executef(datablock, "onPreactivate",
  819. Con::getIntArg(param_holder->getId()),
  820. (extra) ? Con::getIntArg(extra->getId()) : "");
  821. const char* result = cValue.getString();
  822. if (result && result[0] != '\0' && !dAtob(result))
  823. {
  824. #if defined(TORQUE_DEBUG)
  825. Con::warnf("afxEffectron: onPreactivate() returned false, effect aborted.");
  826. #endif
  827. Sim::postEvent(param_holder, new ObjectDeleteEvent, Sim::getCurrentTime());
  828. return 0;
  829. }
  830. // make a temp datablock clone if there are substitutions
  831. if (datablock->getSubstitutionCount() > 0)
  832. {
  833. datablock = new afxEffectronData(*exeblock, true);
  834. exeblock->performSubstitutions(datablock, param_holder);
  835. }
  836. // create a new effectron instance
  837. afxEffectron* eff = new afxEffectron(true);
  838. eff->setDataBlock(datablock);
  839. eff->exeblock = exeblock;
  840. eff->setExtra(extra);
  841. // copy dynamic fields from the param holder to the effectron
  842. eff->assignDynamicFieldsFrom(param_holder, arcaneFX::sParameterFieldPrefix);
  843. Sim::postEvent(param_holder, new ObjectDeleteEvent, Sim::getCurrentTime());
  844. // register
  845. if (!eff->registerObject())
  846. {
  847. Con::errorf("afxEffectron: failed to register effectron instance.");
  848. Sim::postEvent(eff, new ObjectDeleteEvent, Sim::getCurrentTime());
  849. return 0;
  850. }
  851. registerForCleanup(eff);
  852. eff->activate();
  853. return eff;
  854. }
  855. bool afxEffectron::activationCallInit(bool postponed)
  856. {
  857. if (postponed && (!started_with_newop || !postpone_activation))
  858. {
  859. Con::errorf("afxEffectron::activate() -- activate() is only required when creating an effectron with the \"new\" operator "
  860. "and the postponeActivation field is set to \"true\".");
  861. return false;
  862. }
  863. return true;
  864. }
  865. void afxEffectron::activate()
  866. {
  867. // separating the final part of startup allows the calling script
  868. // to make certain types of calls on the returned effectron that
  869. // need to happen prior to constraint initialization.
  870. Sim::postEvent(this, new EffectronFinishStartupEvent, Sim::getCurrentTime());
  871. // CALL SCRIPT afxEffectronData::onActivate(%eff)
  872. Con::executef(exeblock, "onActivate", getIdString());
  873. }
  874. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  875. // console functions
  876. DefineEngineMethod(afxEffectron, setTimeFactor, void, (float factor),,
  877. "Sets the time-factor for the effectron.\n\n"
  878. "@ingroup AFX")
  879. {
  880. object->setTimeFactor(factor);
  881. }
  882. DefineEngineMethod(afxEffectron, interrupt, void, (),,
  883. "Interrupts and deletes a running effectron.\n\n"
  884. "@ingroup AFX")
  885. {
  886. object->postEvent(afxEffectron::INTERRUPT_EVENT);
  887. }
  888. DefineEngineMethod(afxEffectron, activate, void, (),,
  889. "Activates an effectron that was started with postponeActivation=true.\n\n"
  890. "@ingroup AFX")
  891. {
  892. if (object->activationCallInit(true))
  893. object->activate();
  894. }
  895. DefineEngineFunction(startEffectron, S32, (afxEffectronData* datablock, const char* constraintSource, const char* constraintName, SimObject* extra),
  896. (nullAsType<afxEffectronData*>(), nullAsType<const char*>(), nullAsType<const char*>(), nullAsType<SimObject*>()),
  897. "Instantiates the effectron defined by datablock.\n\n"
  898. "@ingroup AFX")
  899. {
  900. if (!datablock)
  901. {
  902. Con::errorf("startEffectron() -- missing valid datablock.");
  903. return 0;
  904. }
  905. //
  906. // Start the Effectron
  907. //
  908. afxEffectron* eff = afxEffectron::start_effect(datablock, extra);
  909. //
  910. // Create a constraint from arguments (if specified).
  911. //
  912. if (eff)
  913. {
  914. if (constraintSource && constraintName)
  915. {
  916. if (!eff->addConstraint(constraintSource, constraintName))
  917. Con::errorf("startEffectron() -- failed to find constraint object [%s].", constraintSource);
  918. }
  919. }
  920. //
  921. // Return the ID (or 0 if start failed).
  922. //
  923. return (eff) ? eff->getId() : 0;
  924. }
  925. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//