afxEffectron.cpp 28 KB

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