afxEffectron.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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*)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, addEffect, 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::addEffect() -- 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. const char* result = Con::executef(datablock, "onPreactivate",
  819. Con::getIntArg(param_holder->getId()),
  820. (extra) ? Con::getIntArg(extra->getId()) : "");
  821. if (result && result[0] != '\0' && !dAtob(result))
  822. {
  823. #if defined(TORQUE_DEBUG)
  824. Con::warnf("afxEffectron: onPreactivate() returned false, effect aborted.");
  825. #endif
  826. Sim::postEvent(param_holder, new ObjectDeleteEvent, Sim::getCurrentTime());
  827. return 0;
  828. }
  829. // make a temp datablock clone if there are substitutions
  830. if (datablock->getSubstitutionCount() > 0)
  831. {
  832. datablock = new afxEffectronData(*exeblock, true);
  833. exeblock->performSubstitutions(datablock, param_holder);
  834. }
  835. // create a new effectron instance
  836. afxEffectron* eff = new afxEffectron(true);
  837. eff->setDataBlock(datablock);
  838. eff->exeblock = exeblock;
  839. eff->setExtra(extra);
  840. // copy dynamic fields from the param holder to the effectron
  841. eff->assignDynamicFieldsFrom(param_holder, arcaneFX::sParameterFieldPrefix);
  842. Sim::postEvent(param_holder, new ObjectDeleteEvent, Sim::getCurrentTime());
  843. // register
  844. if (!eff->registerObject())
  845. {
  846. Con::errorf("afxEffectron: failed to register effectron instance.");
  847. Sim::postEvent(eff, new ObjectDeleteEvent, Sim::getCurrentTime());
  848. return 0;
  849. }
  850. registerForCleanup(eff);
  851. eff->activate();
  852. return eff;
  853. }
  854. bool afxEffectron::activationCallInit(bool postponed)
  855. {
  856. if (postponed && (!started_with_newop || !postpone_activation))
  857. {
  858. Con::errorf("afxEffectron::activate() -- activate() is only required when creating an effectron with the \"new\" operator "
  859. "and the postponeActivation field is set to \"true\".");
  860. return false;
  861. }
  862. return true;
  863. }
  864. void afxEffectron::activate()
  865. {
  866. // separating the final part of startup allows the calling script
  867. // to make certain types of calls on the returned effectron that
  868. // need to happen prior to constraint initialization.
  869. Sim::postEvent(this, new EffectronFinishStartupEvent, Sim::getCurrentTime());
  870. // CALL SCRIPT afxEffectronData::onActivate(%eff)
  871. Con::executef(exeblock, "onActivate", getIdString());
  872. }
  873. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  874. // console functions
  875. DefineEngineMethod(afxEffectron, setTimeFactor, void, (float factor),,
  876. "Sets the time-factor for the effectron.\n\n"
  877. "@ingroup AFX")
  878. {
  879. object->setTimeFactor(factor);
  880. }
  881. DefineEngineMethod(afxEffectron, interrupt, void, (),,
  882. "Interrupts and deletes a running effectron.\n\n"
  883. "@ingroup AFX")
  884. {
  885. object->postEvent(afxEffectron::INTERRUPT_EVENT);
  886. }
  887. DefineEngineMethod(afxEffectron, activate, void, (),,
  888. "Activates an effectron that was started with postponeActivation=true.\n\n"
  889. "@ingroup AFX")
  890. {
  891. if (object->activationCallInit(true))
  892. object->activate();
  893. }
  894. DefineEngineFunction(startEffectron, S32, (afxEffectronData* datablock, const char* constraintSource, const char* constraintName, SimObject* extra),
  895. (nullAsType<afxEffectronData*>(), nullAsType<const char*>(), nullAsType<const char*>(), nullAsType<SimObject*>()),
  896. "Instantiates the effectron defined by datablock.\n\n"
  897. "@ingroup AFX")
  898. {
  899. if (!datablock)
  900. {
  901. Con::errorf("startEffectron() -- missing valid datablock.");
  902. return 0;
  903. }
  904. //
  905. // Start the Effectron
  906. //
  907. afxEffectron* eff = afxEffectron::start_effect(datablock, extra);
  908. //
  909. // Create a constraint from arguments (if specified).
  910. //
  911. if (eff)
  912. {
  913. if (constraintSource && constraintName)
  914. {
  915. if (!eff->addConstraint(constraintSource, constraintName))
  916. Con::errorf("startEffectron() -- failed to find constraint object [%s].", constraintSource);
  917. }
  918. }
  919. //
  920. // Return the ID (or 0 if start failed).
  921. //
  922. return (eff) ? eff->getId() : 0;
  923. }
  924. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//