FXList.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: FXList.cpp ///////////////////////////////////////////////////////////////////////////////
  24. // Author: Steven Johnson, December 2001
  25. // Desc: FXList descriptions
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "GameClient/FXList.h"
  30. #include "Common/DrawModule.h"
  31. #include "Common/GameAudio.h"
  32. #include "Common/INI.h"
  33. #include "Common/Player.h"
  34. #include "Common/PlayerList.h"
  35. #include "Common/RandomValue.h"
  36. #include "Common/ThingTemplate.h"
  37. #include "Common/ThingFactory.h"
  38. #include "GameLogic/Object.h"
  39. #include "GameLogic/GameLogic.h"
  40. #include "GameLogic/TerrainLogic.h"
  41. #include "GameClient/Display.h"
  42. #include "GameClient/GameClient.h"
  43. #include "GameClient/Drawable.h"
  44. #include "GameClient/ParticleSys.h"
  45. #include "GameLogic/PartitionManager.h"
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////
  47. // PUBLIC DATA ////////////////////////////////////////////////////////////////////////////////////
  48. ///////////////////////////////////////////////////////////////////////////////////////////////////
  49. #ifdef _INTERNAL
  50. // for occasional debugging...
  51. //#pragma optimize("", off)
  52. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  53. #endif
  54. FXListStore *TheFXListStore = NULL; ///< the FXList store definition
  55. //-------------------------------------------------------------------------------------------------
  56. static void adjustVector(Coord3D *vec, const Matrix3D* mtx)
  57. {
  58. if (mtx)
  59. {
  60. Vector3 vectmp;
  61. vectmp.X = vec->x;
  62. vectmp.Y = vec->y;
  63. vectmp.Z = vec->z;
  64. vectmp = mtx->Rotate_Vector(vectmp);
  65. vec->x = vectmp.X;
  66. vec->y = vectmp.Y;
  67. vec->z = vectmp.Z;
  68. }
  69. }
  70. ///////////////////////////////////////////////////////////////////////////////////////////////////
  71. // PRIVATE CLASSES ///////////////////////////////////////////////////////////////////////////////////
  72. ///////////////////////////////////////////////////////////////////////////////////////////////////
  73. //-------------------------------------------------------------------------------------------------
  74. void FXNugget::doFXObj(const Object* primary, const Object* secondary) const
  75. {
  76. const Coord3D* p = primary ? primary->getPosition() : NULL;
  77. const Matrix3D* mtx = primary ? primary->getTransformMatrix() : NULL;
  78. const Real speed = 0.0f; // yes, that's right -- NOT the object's speed.
  79. const Coord3D* s = secondary ? secondary->getPosition() : NULL;
  80. doFXPos(p, mtx, speed, s);
  81. }
  82. //-------------------------------------------------------------------------------------------------
  83. class SoundFXNugget : public FXNugget
  84. {
  85. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(SoundFXNugget, "SoundFXNugget")
  86. public:
  87. virtual void doFXPos(const Coord3D *primary, const Matrix3D* /*primaryMtx*/, const Real /*primarySpeed*/, const Coord3D * /*secondary*/, const Real /*overrideRadius*/ ) const
  88. {
  89. AudioEventRTS sound(m_soundName);
  90. if (primary)
  91. {
  92. sound.setPosition(primary);
  93. }
  94. TheAudio->addAudioEvent(&sound);
  95. }
  96. virtual void doFXObj(const Object* primary, const Object* secondary = NULL) const
  97. {
  98. AudioEventRTS sound(m_soundName);
  99. if (primary)
  100. {
  101. sound.setPlayerIndex(primary->getControllingPlayer()->getPlayerIndex());
  102. sound.setPosition(primary->getPosition());
  103. }
  104. TheAudio->addAudioEvent(&sound);
  105. }
  106. static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
  107. {
  108. static const FieldParse myFieldParse[] =
  109. {
  110. { "Name", INI::parseAsciiString, NULL, offsetof( SoundFXNugget, m_soundName ) },
  111. { 0, 0, 0, 0 }
  112. };
  113. SoundFXNugget* nugget = newInstance(SoundFXNugget);
  114. ini->initFromINI(nugget, myFieldParse);
  115. ((FXList*)instance)->addFXNugget(nugget);
  116. }
  117. private:
  118. AsciiString m_soundName;
  119. };
  120. EMPTY_DTOR(SoundFXNugget)
  121. //-------------------------------------------------------------------------------------------------
  122. static Real calcDist(const Coord3D& src, const Coord3D& dst)
  123. {
  124. Real dx = dst.x - src.x;
  125. Real dy = dst.y - src.y;
  126. Real dz = dst.z - src.z;
  127. return sqrt(dx*dx + dy*dy + dz*dz);
  128. }
  129. //-------------------------------------------------------------------------------------------------
  130. class TracerFXNugget : public FXNugget
  131. {
  132. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(TracerFXNugget, "TracerFXNugget")
  133. public:
  134. TracerFXNugget()
  135. {
  136. m_tracerName.set("GenericTracer");
  137. m_boneName.clear();
  138. m_speed = 0.0f; // means "use passed-in speed"
  139. m_decayAt = 1.0f;
  140. m_length = 10.0f;
  141. m_width = 1.0f;
  142. m_color.red = m_color.green = m_color.blue = 1.0f;
  143. m_probability = 1.0f;
  144. }
  145. virtual void doFXPos(const Coord3D *primary, const Matrix3D* primaryMtx, const Real primarySpeed, const Coord3D *secondary, const Real /*overrideRadius*/ ) const
  146. {
  147. if (m_probability <= GameClientRandomValueReal(0, 1))
  148. return;
  149. if (primary && secondary)
  150. {
  151. Drawable *tracer = TheThingFactory->newDrawable(TheThingFactory->findTemplate(m_tracerName));
  152. if(!tracer)
  153. return;
  154. //Kris -- Redid this section Sept 18, 2002
  155. //Calculate tracer orientations to face from primary to secondary position. This
  156. //should be the direction that the projectile is being fired towards. It doesn't make
  157. //sense that the old stuff made use of the muzzle fx bone orientation (because it's a
  158. //subobject). It had other problems because of elevation variations the tracers would
  159. //stay on the ground.
  160. //tracer->setTransformMatrix(primaryMtx);
  161. Matrix3D tracerMtx;
  162. Vector3 pos( primary->x, primary->y, primary->z );
  163. Vector3 dir( secondary->x - primary->x, secondary->y - primary->y, secondary->z - primary->z );
  164. dir.Normalize(); //This is fantastically crucial for calling buildTransformMatrix!!!!!
  165. tracerMtx.buildTransformMatrix( pos, dir );
  166. tracer->setTransformMatrix( &tracerMtx );
  167. tracer->setPosition(primary);
  168. Real speed = m_speed;
  169. if (speed == 0.0f)
  170. {
  171. speed = primarySpeed;
  172. }
  173. TracerDrawInterface* tdi = NULL;
  174. for (DrawModule** d = tracer->getDrawModules(); *d; ++d)
  175. {
  176. if ((tdi = (*d)->getTracerDrawInterface()) != NULL)
  177. {
  178. tdi->setTracerParms(speed, m_length, m_width, m_color, 1.0f);
  179. }
  180. }
  181. // estimate how long it will take us to get to the destination
  182. Real dist = calcDist(*primary, *secondary) - m_length;
  183. Real frames = (dist >= 0.0f && speed >= 0.0f) ? (dist / speed) : 1;
  184. Int framesAdjusted = REAL_TO_INT_CEIL(frames * m_decayAt);
  185. tracer->setExpirationDate(TheGameLogic->getFrame() + framesAdjusted);
  186. }
  187. else
  188. {
  189. DEBUG_CRASH(("You must have a primary and secondary source for this effect"));
  190. }
  191. }
  192. static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
  193. {
  194. static const FieldParse myFieldParse[] =
  195. {
  196. { "TracerName", INI::parseAsciiString, NULL, offsetof( TracerFXNugget, m_tracerName ) },
  197. { "BoneName", INI::parseAsciiString, NULL, offsetof( TracerFXNugget, m_boneName ) },
  198. { "Speed", INI::parseVelocityReal, NULL, offsetof( TracerFXNugget, m_speed ) },
  199. { "DecayAt", INI::parseReal, NULL, offsetof( TracerFXNugget, m_decayAt ) },
  200. { "Length", INI::parseReal, NULL, offsetof( TracerFXNugget, m_length ) },
  201. { "Width", INI::parseReal, NULL, offsetof( TracerFXNugget, m_width ) },
  202. { "Color", INI::parseRGBColor, NULL, offsetof( TracerFXNugget, m_color ) },
  203. { "Probability", INI::parseReal, NULL, offsetof( TracerFXNugget, m_probability ) },
  204. { 0, 0, 0, 0 }
  205. };
  206. TracerFXNugget* nugget = newInstance( TracerFXNugget );
  207. ini->initFromINI(nugget, myFieldParse);
  208. ((FXList*)instance)->addFXNugget(nugget);
  209. }
  210. private:
  211. AsciiString m_tracerName;
  212. AsciiString m_boneName;
  213. Real m_speed;
  214. Real m_decayAt;
  215. Real m_length;
  216. Real m_width;
  217. RGBColor m_color;
  218. Real m_probability;
  219. };
  220. EMPTY_DTOR(TracerFXNugget)
  221. //-------------------------------------------------------------------------------------------------
  222. class RayEffectFXNugget : public FXNugget
  223. {
  224. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(RayEffectFXNugget, "RayEffectFXNugget")
  225. public:
  226. RayEffectFXNugget()
  227. {
  228. m_templateName.clear();
  229. m_primaryOffset.x = m_primaryOffset.y = m_primaryOffset.z = 0;
  230. m_secondaryOffset.x = m_secondaryOffset.y = m_secondaryOffset.z = 0;
  231. }
  232. virtual void doFXPos(const Coord3D *primary, const Matrix3D* /*primaryMtx*/, const Real /*primarySpeed*/, const Coord3D * secondary, const Real /*overrideRadius*/ ) const
  233. {
  234. const ThingTemplate* tmpl = TheThingFactory->findTemplate(m_templateName);
  235. DEBUG_ASSERTCRASH(tmpl, ("RayEffect %s not found\n",m_templateName.str()));
  236. if (primary && secondary && tmpl)
  237. {
  238. Coord3D sourcePos = *primary;
  239. sourcePos.x += m_primaryOffset.x;
  240. sourcePos.y += m_primaryOffset.y;
  241. sourcePos.z += m_primaryOffset.z;
  242. Coord3D targetPos = *secondary;
  243. targetPos.x += m_secondaryOffset.x;
  244. targetPos.y += m_secondaryOffset.y;
  245. targetPos.z += m_secondaryOffset.z;
  246. TheGameClient->createRayEffectByTemplate(&sourcePos, &targetPos, tmpl);
  247. }
  248. else
  249. {
  250. DEBUG_CRASH(("You must have a primary AND secondary source for this effect"));
  251. }
  252. }
  253. static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
  254. {
  255. static const FieldParse myFieldParse[] =
  256. {
  257. { "Name", INI::parseAsciiString, NULL, offsetof( RayEffectFXNugget, m_templateName ) },
  258. { "PrimaryOffset", INI::parseCoord3D, NULL, offsetof( RayEffectFXNugget, m_primaryOffset ) },
  259. { "SecondaryOffset", INI::parseCoord3D, NULL, offsetof( RayEffectFXNugget, m_secondaryOffset ) },
  260. { 0, 0, 0, 0 }
  261. };
  262. RayEffectFXNugget* nugget = newInstance( RayEffectFXNugget );
  263. ini->initFromINI(nugget, myFieldParse);
  264. ((FXList*)instance)->addFXNugget(nugget);
  265. }
  266. private:
  267. AsciiString m_templateName;
  268. Coord3D m_primaryOffset;
  269. Coord3D m_secondaryOffset;
  270. };
  271. EMPTY_DTOR(RayEffectFXNugget)
  272. //-------------------------------------------------------------------------------------------------
  273. class LightPulseFXNugget : public FXNugget
  274. {
  275. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(LightPulseFXNugget, "LightPulseFXNugget")
  276. public:
  277. LightPulseFXNugget() : m_radius(0), m_increaseFrames(0), m_decreaseFrames(0), m_boundingCirclePct(0)
  278. {
  279. m_color.red = m_color.green = m_color.blue = 0;
  280. }
  281. virtual void doFXObj(const Object* primary, const Object* /*secondary*/) const
  282. {
  283. if (primary)
  284. {
  285. Real radius = m_radius;
  286. if (m_boundingCirclePct > 0)
  287. radius = (primary->getGeometryInfo().getBoundingCircleRadius() * m_boundingCirclePct);
  288. TheDisplay->createLightPulse(primary->getPosition(), &m_color, 1, radius, m_increaseFrames, m_decreaseFrames);
  289. }
  290. else
  291. {
  292. DEBUG_CRASH(("You must have a primary source for this effect"));
  293. }
  294. }
  295. virtual void doFXPos(const Coord3D *primary, const Matrix3D* /*primaryMtx*/, const Real /*primarySpeed*/, const Coord3D * /*secondary*/, const Real /*overrideRadius*/ ) const
  296. {
  297. if (primary)
  298. {
  299. TheDisplay->createLightPulse(primary, &m_color, 1, m_radius, m_increaseFrames, m_decreaseFrames);
  300. }
  301. else
  302. {
  303. DEBUG_CRASH(("You must have a primary source for this effect"));
  304. }
  305. }
  306. static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
  307. {
  308. static const FieldParse myFieldParse[] =
  309. {
  310. { "Color", INI::parseRGBColor, NULL, offsetof( LightPulseFXNugget, m_color ) },
  311. { "Radius", INI::parseReal, NULL, offsetof( LightPulseFXNugget, m_radius ) },
  312. { "RadiusAsPercentOfObjectSize", INI::parsePercentToReal, NULL, offsetof( LightPulseFXNugget, m_boundingCirclePct ) },
  313. { "IncreaseTime", INI::parseDurationUnsignedInt, NULL, offsetof( LightPulseFXNugget, m_increaseFrames ) },
  314. { "DecreaseTime", INI::parseDurationUnsignedInt, NULL, offsetof( LightPulseFXNugget, m_decreaseFrames ) },
  315. { 0, 0, 0, 0 }
  316. };
  317. LightPulseFXNugget* nugget = newInstance( LightPulseFXNugget );
  318. ini->initFromINI(nugget, myFieldParse);
  319. ((FXList*)instance)->addFXNugget(nugget);
  320. }
  321. private:
  322. RGBColor m_color;
  323. Real m_radius;
  324. Real m_boundingCirclePct;
  325. UnsignedInt m_increaseFrames;
  326. UnsignedInt m_decreaseFrames;
  327. };
  328. EMPTY_DTOR(LightPulseFXNugget)
  329. //-------------------------------------------------------------------------------------------------
  330. class ViewShakeFXNugget : public FXNugget
  331. {
  332. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ViewShakeFXNugget, "ViewShakeFXNugget")
  333. public:
  334. ViewShakeFXNugget() : m_shake(View::SHAKE_NORMAL)
  335. {
  336. }
  337. virtual void doFXPos(const Coord3D *primary, const Matrix3D* /*primaryMtx*/, const Real /*primarySpeed*/, const Coord3D * /*secondary*/, const Real /*overrideRadius*/ ) const
  338. {
  339. if (primary)
  340. {
  341. if (TheTacticalView)
  342. TheTacticalView->shake(primary, m_shake);
  343. }
  344. else
  345. {
  346. DEBUG_CRASH(("You must have a primary source for this effect"));
  347. }
  348. }
  349. static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
  350. {
  351. static const FieldParse myFieldParse[] =
  352. {
  353. { "Type", parseShakeType, NULL, offsetof( ViewShakeFXNugget, m_shake ) },
  354. { 0, 0, 0, 0 }
  355. };
  356. ViewShakeFXNugget* nugget = newInstance( ViewShakeFXNugget );
  357. ini->initFromINI(nugget, myFieldParse);
  358. ((FXList*)instance)->addFXNugget(nugget);
  359. }
  360. protected:
  361. static void parseShakeType( INI* ini, void *instance, void *store, const void* /*userData*/ )
  362. {
  363. static const LookupListRec shakeTypeNames[] =
  364. {
  365. { "SUBTLE", View::SHAKE_SUBTLE },
  366. { "NORMAL", View::SHAKE_NORMAL },
  367. { "STRONG", View::SHAKE_STRONG },
  368. { "SEVERE", View::SHAKE_SEVERE },
  369. { "CINE_EXTREME", View::SHAKE_CINE_EXTREME },
  370. { "CINE_INSANE", View::SHAKE_CINE_INSANE },
  371. { 0, 0 }
  372. };
  373. *(Int *)store = INI::scanLookupList(ini->getNextToken(), shakeTypeNames);
  374. }
  375. private:
  376. View::CameraShakeType m_shake;
  377. };
  378. EMPTY_DTOR(ViewShakeFXNugget)
  379. //-------------------------------------------------------------------------------------------------
  380. class TerrainScorchFXNugget : public FXNugget
  381. {
  382. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(TerrainScorchFXNugget, "TerrainScorchFXNugget")
  383. public:
  384. TerrainScorchFXNugget() : m_scorch(-1), m_radius(0)
  385. {
  386. }
  387. virtual void doFXPos(const Coord3D *primary, const Matrix3D* /*primaryMtx*/, const Real /*primarySpeed*/, const Coord3D * /*secondary*/, const Real /*overrideRadius*/ ) const
  388. {
  389. if (primary)
  390. {
  391. Int scorch = m_scorch;
  392. if (scorch < 0)
  393. {
  394. scorch = GameClientRandomValue( SCORCH_1, SCORCH_4 );
  395. }
  396. TheGameClient->addScorch(primary, m_radius, (Scorches)scorch);
  397. }
  398. else
  399. {
  400. DEBUG_CRASH(("You must have a primary source for this effect"));
  401. }
  402. }
  403. static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
  404. {
  405. static const FieldParse myFieldParse[] =
  406. {
  407. { "Type", parseScorchType, NULL, offsetof( TerrainScorchFXNugget, m_scorch ) },
  408. { "Radius", INI::parseReal, NULL, offsetof( TerrainScorchFXNugget, m_radius ) },
  409. { 0, 0, 0, 0 }
  410. };
  411. TerrainScorchFXNugget* nugget = newInstance( TerrainScorchFXNugget );
  412. ini->initFromINI(nugget, myFieldParse);
  413. ((FXList*)instance)->addFXNugget(nugget);
  414. }
  415. protected:
  416. static void parseScorchType( INI* ini, void *instance, void *store, const void* /*userData*/ )
  417. {
  418. static const LookupListRec scorchTypeNames[] =
  419. {
  420. { "SCORCH_1", SCORCH_1 },
  421. { "SCORCH_2", SCORCH_2 },
  422. { "SCORCH_3", SCORCH_3 },
  423. { "SCORCH_4", SCORCH_4 },
  424. { "SHADOW_SCORCH", SHADOW_SCORCH },
  425. { "RANDOM", -1 },
  426. { 0, 0 }
  427. };
  428. *(Int *)store = INI::scanLookupList(ini->getNextToken(), scorchTypeNames);
  429. }
  430. private:
  431. Int m_scorch;
  432. Real m_radius;
  433. };
  434. EMPTY_DTOR(TerrainScorchFXNugget)
  435. //-------------------------------------------------------------------------------------------------
  436. class ParticleSystemFXNugget : public FXNugget
  437. {
  438. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ParticleSystemFXNugget, "ParticleSystemFXNugget")
  439. public:
  440. ParticleSystemFXNugget()
  441. {
  442. m_name.clear();
  443. m_count = 1;
  444. m_radius.setRange(0, 0, GameClientRandomVariable::CONSTANT);
  445. m_height.setRange(0, 0, GameClientRandomVariable::CONSTANT);
  446. // -1 means "don't mess with it, just accept the particle-system's defaults"
  447. m_delay.setRange(-1, -1, GameClientRandomVariable::CONSTANT);
  448. m_offset.x = m_offset.y = m_offset.z = 0;
  449. m_orientToObject = false;
  450. m_attachToObject = false;
  451. m_createAtGroundHeight = FALSE;
  452. m_useCallersRadius = FALSE;
  453. m_rotateX = m_rotateY = m_rotateZ = 0;
  454. }
  455. virtual void doFXPos(const Coord3D *primary, const Matrix3D* primaryMtx, const Real /*primarySpeed*/, const Coord3D * /*secondary*/, const Real overrideRadius ) const
  456. {
  457. if (primary)
  458. {
  459. reallyDoFX(primary, primaryMtx, NULL, overrideRadius);
  460. }
  461. else
  462. {
  463. DEBUG_CRASH(("You must have a primary source for this effect"));
  464. }
  465. }
  466. virtual void doFXObj(const Object* primary, const Object* secondary) const
  467. {
  468. if (primary)
  469. {
  470. if (m_ricochet && secondary)
  471. {
  472. // HERE WE MUST BUILD A MATRIX WHICH WILL ORIENT THE NEW PARTICLE SYSTEM TO FACE AWAY FROM THE SECONDARY OBJECT
  473. // THE RESULT SHOULD LOOK LIKE THE DIRECTION OF THE "ATTACK" IS CARRIED THROUGH LIKE A RICOCHET
  474. Real deltaX = primary->getPosition()->x - secondary->getPosition()->x;
  475. Real deltaY = primary->getPosition()->y - secondary->getPosition()->y;
  476. Real aimingAngle = atan2(deltaY, deltaX);
  477. Matrix3D aimingMatrix(1);
  478. aimingMatrix.Rotate_Z( aimingAngle );
  479. reallyDoFX(primary->getPosition(), &aimingMatrix, primary, 0.0f);
  480. }
  481. else
  482. // if we have an object, then adjust the offset and direction by the object's transformation
  483. // matrix, so that (say) an offset of +10 in the z axis "follows" the orientation of the object.
  484. reallyDoFX(primary->getPosition(), primary->getTransformMatrix(), primary, 0.0f);
  485. }
  486. else
  487. {
  488. DEBUG_CRASH(("You must have a primary source for this effect"));
  489. }
  490. }
  491. static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
  492. {
  493. static const FieldParse myFieldParse[] =
  494. {
  495. { "Name", INI::parseAsciiString, NULL, offsetof( ParticleSystemFXNugget, m_name ) },
  496. { "Count", INI::parseInt, NULL, offsetof( ParticleSystemFXNugget, m_count ) },
  497. { "Offset", INI::parseCoord3D, NULL, offsetof( ParticleSystemFXNugget, m_offset ) },
  498. { "Radius", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemFXNugget, m_radius ) },
  499. { "Height", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemFXNugget, m_height ) },
  500. { "InitialDelay", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemFXNugget, m_delay ) },
  501. { "RotateX", INI::parseAngleReal, NULL, offsetof( ParticleSystemFXNugget, m_rotateX ) },
  502. { "RotateY", INI::parseAngleReal, NULL, offsetof( ParticleSystemFXNugget, m_rotateY ) },
  503. { "RotateZ", INI::parseAngleReal, NULL, offsetof( ParticleSystemFXNugget, m_rotateZ ) },
  504. { "OrientToObject", INI::parseBool, NULL, offsetof( ParticleSystemFXNugget, m_orientToObject ) },
  505. { "Ricochet", INI::parseBool, NULL, offsetof( ParticleSystemFXNugget, m_ricochet ) },
  506. { "AttachToObject", INI::parseBool, NULL, offsetof( ParticleSystemFXNugget, m_attachToObject ) },
  507. { "CreateAtGroundHeight", INI::parseBool, NULL, offsetof( ParticleSystemFXNugget, m_createAtGroundHeight ) },
  508. { "UseCallersRadius", INI::parseBool, NULL, offsetof( ParticleSystemFXNugget, m_useCallersRadius ) },
  509. { 0, 0, 0, 0 }
  510. };
  511. ParticleSystemFXNugget* nugget = newInstance( ParticleSystemFXNugget );
  512. ini->initFromINI(nugget, myFieldParse);
  513. ((FXList*)instance)->addFXNugget(nugget);
  514. }
  515. protected:
  516. void reallyDoFX(const Coord3D *primary, const Matrix3D* mtx, const Object* thingToAttachTo, Real overrideRadius ) const
  517. {
  518. Coord3D offset = m_offset;
  519. if (mtx)
  520. {
  521. adjustVector(&offset, mtx);
  522. }
  523. const ParticleSystemTemplate *tmp = TheParticleSystemManager->findTemplate(m_name);
  524. DEBUG_ASSERTCRASH(tmp, ("ParticleSystem %s not found\n",m_name.str()));
  525. if (tmp)
  526. {
  527. for (Int i = 0; i < m_count; i++ )
  528. {
  529. ParticleSystem *sys = TheParticleSystemManager->createParticleSystem(tmp);
  530. if (sys)
  531. {
  532. Coord3D newPos;
  533. Real radius = m_radius.getValue();
  534. Real angle = GameClientRandomValueReal( 0.0f, 2.0f * PI );
  535. newPos.x = primary->x + offset.x + radius * cos(angle);
  536. newPos.y = primary->y + offset.y + radius * sin(angle);
  537. if( m_createAtGroundHeight && TheTerrainLogic )
  538. {
  539. //old way:
  540. //newPos.z = TheTerrainLogic->getGrsoundHeight( newPos.x, newPos.y ) + 1;// The plus one prevents scissoring with terrain
  541. //new way: now we allow bridges in the GroundHeight.
  542. PathfindLayerEnum layer = TheTerrainLogic->getLayerForDestination(&newPos);
  543. newPos.z = TheTerrainLogic->getLayerHeight( newPos.x, newPos.y, layer );
  544. }
  545. else
  546. newPos.z = primary->z + offset.z + m_height.getValue();
  547. if (m_orientToObject && mtx)
  548. {
  549. sys->setLocalTransform(mtx);
  550. }
  551. if (m_rotateX != 0.0f)
  552. sys->rotateLocalTransformX(m_rotateX);
  553. if (m_rotateY != 0.0f)
  554. sys->rotateLocalTransformY(m_rotateY);
  555. if (m_rotateZ != 0.0f)
  556. sys->rotateLocalTransformZ(m_rotateZ);
  557. if (m_attachToObject && thingToAttachTo)
  558. sys->attachToObject(thingToAttachTo);
  559. else
  560. sys->setPosition( &newPos );
  561. Real delayInMsec = m_delay.getValue();
  562. if (delayInMsec >= 0.0f)
  563. {
  564. UnsignedInt delayInFrames = REAL_TO_INT_CEIL(ConvertDurationFromMsecsToFrames(delayInMsec));
  565. sys->setInitialDelay(delayInFrames);
  566. }
  567. if( m_useCallersRadius && overrideRadius )
  568. {
  569. ParticleSystemInfo::EmissionVolumeType type = sys->getEmisionVolumeType();
  570. if( type == ParticleSystemInfo::EmissionVolumeType::SPHERE )
  571. sys->setEmissionVolumeSphereRadius( overrideRadius );
  572. else if( type == ParticleSystemInfo::EmissionVolumeType::CYLINDER )
  573. sys->setEmissionVolumeCylinderRadius( overrideRadius );
  574. }
  575. }
  576. }
  577. }
  578. }
  579. private:
  580. AsciiString m_name;
  581. Int m_count;
  582. Coord3D m_offset;
  583. GameClientRandomVariable m_radius;
  584. GameClientRandomVariable m_height;
  585. GameClientRandomVariable m_delay;
  586. Real m_rotateX, m_rotateY, m_rotateZ;
  587. Bool m_orientToObject;
  588. Bool m_attachToObject;
  589. Bool m_createAtGroundHeight;
  590. Bool m_useCallersRadius;
  591. Bool m_ricochet;
  592. };
  593. EMPTY_DTOR(ParticleSystemFXNugget)
  594. //-------------------------------------------------------------------------------------------------
  595. class FXListAtBonePosFXNugget : public FXNugget
  596. {
  597. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(FXListAtBonePosFXNugget, "FXListAtBonePosFXNugget")
  598. public:
  599. FXListAtBonePosFXNugget()
  600. {
  601. m_fx = NULL;
  602. m_boneName.clear();
  603. m_orientToBone = true;
  604. }
  605. virtual void doFXPos(const Coord3D *primary, const Matrix3D* primaryMtx, const Real /*primarySpeed*/, const Coord3D * /*secondary*/, const Real /*overrideRadius*/ ) const
  606. {
  607. DEBUG_CRASH(("You must use the object form for this effect"));
  608. }
  609. virtual void doFXObj(const Object* primary, const Object* /*secondary*/) const
  610. {
  611. if (primary)
  612. {
  613. // first, try the unadorned name.
  614. doFxAtBones(primary, 0);
  615. // then, try the 01,02,03...etc names.
  616. doFxAtBones(primary, 1);
  617. }
  618. else
  619. {
  620. DEBUG_CRASH(("You must have a primary source for this effect"));
  621. }
  622. }
  623. static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
  624. {
  625. static const FieldParse myFieldParse[] =
  626. {
  627. { "FX", INI::parseFXList, NULL, offsetof( FXListAtBonePosFXNugget, m_fx ) },
  628. { "BoneName", INI::parseAsciiString, NULL, offsetof( FXListAtBonePosFXNugget, m_boneName ) },
  629. { "OrientToBone", INI::parseBool, NULL, offsetof( FXListAtBonePosFXNugget, m_orientToBone ) },
  630. { 0, 0, 0, 0 }
  631. };
  632. FXListAtBonePosFXNugget* nugget = newInstance( FXListAtBonePosFXNugget );
  633. ini->initFromINI(nugget, myFieldParse);
  634. ((FXList*)instance)->addFXNugget(nugget);
  635. }
  636. protected:
  637. void doFxAtBones(const Object* obj, Int start) const
  638. {
  639. Coord3D bonePos[MAX_BONE_POINTS];
  640. Matrix3D boneMtx[MAX_BONE_POINTS];
  641. Drawable* draw = obj->getDrawable();
  642. if (draw)
  643. {
  644. // yes, BONEPOS_CURRENT_CLIENT_ONLY -- this is client-only, so should be safe to do.
  645. Int count = draw->getCurrentClientBonePositions(m_boneName.str(), start, bonePos, boneMtx, MAX_BONE_POINTS);
  646. for (Int i = 0; i < count; ++i)
  647. {
  648. Coord3D p;
  649. Matrix3D m;
  650. obj->convertBonePosToWorldPos(&bonePos[i], &boneMtx[i], &p, &m);
  651. FXList::doFXPos(m_fx, &p, &m, 0.0f, NULL, 0.0f);
  652. }
  653. }
  654. }
  655. private:
  656. enum { MAX_BONE_POINTS = 40 };
  657. const FXList* m_fx;
  658. AsciiString m_boneName;
  659. Bool m_orientToBone;
  660. };
  661. EMPTY_DTOR(FXListAtBonePosFXNugget)
  662. //-------------------------------------------------------------------------------------------------
  663. //-------------------------------------------------------------------------------------------------
  664. //-------------------------------------------------------------------------------------------------
  665. //-------------------------------------------------------------------------------------------------
  666. static const FieldParse TheFXListFieldParse[] =
  667. {
  668. { "Sound", SoundFXNugget::parse, 0, 0},
  669. { "RayEffect", RayEffectFXNugget::parse, 0, 0},
  670. { "Tracer", TracerFXNugget::parse, 0, 0},
  671. { "LightPulse", LightPulseFXNugget::parse, 0, 0},
  672. { "ViewShake", ViewShakeFXNugget::parse, 0, 0},
  673. { "TerrainScorch", TerrainScorchFXNugget::parse, 0, 0},
  674. { "ParticleSystem", ParticleSystemFXNugget::parse, 0, 0},
  675. { "FXListAtBonePos", FXListAtBonePosFXNugget::parse, 0, 0},
  676. { NULL, NULL, 0, 0 } // keep this last
  677. };
  678. //-------------------------------------------------------------------------------------------------
  679. FXList::FXList()
  680. {
  681. }
  682. //-------------------------------------------------------------------------------------------------
  683. FXList::~FXList()
  684. {
  685. clear();
  686. }
  687. //-------------------------------------------------------------------------------------------------
  688. void FXList::clear()
  689. {
  690. for (FXNuggetList::iterator it = m_nuggets.begin(); it != m_nuggets.end(); ++it)
  691. {
  692. if (*it)
  693. (*it)->deleteInstance();
  694. }
  695. m_nuggets.clear();
  696. }
  697. //-------------------------------------------------------------------------------------------------
  698. void FXList::doFXPos(const Coord3D *primary, const Matrix3D* primaryMtx, const Real primarySpeed, const Coord3D *secondary, const Real overrideRadius ) const
  699. {
  700. if (ThePartitionManager->getShroudStatusForPlayer(ThePlayerList->getLocalPlayer()->getPlayerIndex(), primary) != CELLSHROUD_CLEAR)
  701. return;
  702. for (FXNuggetList::const_iterator it = m_nuggets.begin(); it != m_nuggets.end(); ++it)
  703. {
  704. (*it)->doFXPos(primary, primaryMtx, primarySpeed, secondary, overrideRadius);
  705. }
  706. }
  707. //-------------------------------------------------------------------------------------------------
  708. void FXList::doFXObj(const Object* primary, const Object* secondary) const
  709. {
  710. if (primary && primary->getShroudedStatus(ThePlayerList->getLocalPlayer()->getPlayerIndex()) > OBJECTSHROUD_PARTIAL_CLEAR)
  711. return; //the primary object is fogged or shrouded so don't bother with the effect.
  712. for (FXNuggetList::const_iterator it = m_nuggets.begin(); it != m_nuggets.end(); ++it)
  713. {
  714. // HERE THE PRIMARY IS THE GUY RECEIVING THE FX, AND SECONDARY MIGHT BE THE GUY DEALING IT
  715. (*it)->doFXObj(primary, secondary);
  716. }
  717. }
  718. //-------------------------------------------------------------------------------------------------
  719. //-------------------------------------------------------------------------------------------------
  720. //-------------------------------------------------------------------------------------------------
  721. //-------------------------------------------------------------------------------------------------
  722. FXListStore::FXListStore()
  723. {
  724. }
  725. //-------------------------------------------------------------------------------------------------
  726. FXListStore::~FXListStore()
  727. {
  728. m_fxmap.clear();
  729. }
  730. //-------------------------------------------------------------------------------------------------
  731. const FXList *FXListStore::findFXList(const char* name) const
  732. {
  733. if (stricmp(name, "None") == 0)
  734. return NULL;
  735. FXListMap::const_iterator it = m_fxmap.find(NAMEKEY(name));
  736. if (it != m_fxmap.end())
  737. {
  738. return &(*it).second;
  739. }
  740. return NULL;
  741. }
  742. //-------------------------------------------------------------------------------------------------
  743. /*static */ void FXListStore::parseFXListDefinition(INI *ini)
  744. {
  745. // read the FXList name
  746. const char *c = ini->getNextToken();
  747. NameKeyType key = TheNameKeyGenerator->nameToKey(c);
  748. FXList& fxl = TheFXListStore->m_fxmap[key];
  749. fxl.clear();
  750. ini->initFromINI(&fxl, TheFXListFieldParse);
  751. }
  752. //-------------------------------------------------------------------------------------------------
  753. /*static*/ void INI::parseFXListDefinition(INI *ini)
  754. {
  755. FXListStore::parseFXListDefinition(ini);
  756. }