StructureToppleUpdate.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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: StructureToppleUpdate.cpp ///////////////////////////////////////////////////////////////////////
  24. // Author:
  25. // Desc:
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/Thing.h"
  30. #include "Common/ThingTemplate.h"
  31. #include "Common/INI.h"
  32. #include "Common/RandomValue.h"
  33. #include "Common/Xfer.h"
  34. #include "GameClient/FXList.h"
  35. #include "GameLogic/GameLogic.h"
  36. #include "GameLogic/Module/BoneFXUpdate.h"
  37. #include "GameLogic/Module/StructureToppleUpdate.h"
  38. #include "GameLogic/Module/AIUpdate.h"
  39. #include "GameLogic/Object.h"
  40. #include "GameLogic/ObjectCreationList.h"
  41. #include "GameLogic/ScriptEngine.h"
  42. #include "GameLogic/Weapon.h"
  43. #include "GameClient/Drawable.h"
  44. #include "GameClient/InGameUI.h"
  45. #ifdef _INTERNAL
  46. // for occasional debugging...
  47. //#pragma optimize("", off)
  48. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  49. #endif
  50. const Int MAX_IDX = 32;
  51. //-------------------------------------------------------------------------------------------------
  52. //-------------------------------------------------------------------------------------------------
  53. StructureToppleUpdate::StructureToppleUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  54. {
  55. //Added By Sadullah Nader
  56. //Initialization(s) inserted
  57. m_delayBurstLocation.zero();
  58. m_structuralIntegrity = 0.0f;
  59. m_toppleDirection.x = m_toppleDirection.y = 0;
  60. //
  61. m_toppleFrame = 0;
  62. m_toppleState = TOPPLESTATE_STANDING;
  63. m_toppleVelocity = 0.0f;
  64. m_accumulatedAngle = 0.001f; // Need to give it a little nudge in the right direction
  65. m_lastCrushedLocation = 0.0f;
  66. m_nextBurstFrame = -1;
  67. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  68. //Get the extent height here, rather than after it dies -- when it switches to dead state
  69. //the rubble state has a tiny height.
  70. Object *building = getObject();
  71. m_buildingHeight = building->getGeometryInfo().getMaxHeightAbovePosition();
  72. }
  73. //-------------------------------------------------------------------------------------------------
  74. //-------------------------------------------------------------------------------------------------
  75. StructureToppleUpdate::~StructureToppleUpdate( void )
  76. {
  77. }
  78. //-------------------------------------------------------------------------------------------------
  79. static void parseOCL( INI* ini, void *instance, void * /*store*/, const void* /*userData*/ )
  80. {
  81. StructureToppleUpdateModuleData* self = (StructureToppleUpdateModuleData*)instance;
  82. StructureTopplePhaseType stphase = (StructureTopplePhaseType)INI::scanIndexList(ini->getNextToken(), TheStructureTopplePhaseNames);
  83. for (const char* token = ini->getNextToken(); token != NULL; token = ini->getNextTokenOrNull())
  84. {
  85. const ObjectCreationList *ocl = TheObjectCreationListStore->findObjectCreationList(token); // could be null! this is OK!
  86. self->m_ocls[stphase].push_back(ocl);
  87. }
  88. }
  89. //-------------------------------------------------------------------------------------------------
  90. static void parseAngleFX(INI* ini, void *instance, void * /* store */, const void * /*userData*/)
  91. {
  92. StructureToppleUpdateModuleData* self = (StructureToppleUpdateModuleData*)instance;
  93. AngleFXInfo info;
  94. INI::parseReal(ini, instance, &(info.angle), NULL);
  95. info.angle = info.angle * PI / 180.0f; // convert from degrees to radians.
  96. INI::parseFXList(ini, instance, &(info.fxList), NULL);
  97. self->angleFX.push_back(info);
  98. }
  99. //-------------------------------------------------------------------------------------------------
  100. /*static*/ void StructureToppleUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  101. {
  102. UpdateModuleData::buildFieldParse(p);
  103. static const FieldParse dataFieldParse[] =
  104. {
  105. { "MinToppleDelay", INI::parseDurationUnsignedInt, NULL, offsetof( StructureToppleUpdateModuleData, m_minToppleDelay ) },
  106. { "MaxToppleDelay", INI::parseDurationUnsignedInt, NULL, offsetof( StructureToppleUpdateModuleData, m_maxToppleDelay ) },
  107. { "MinToppleBurstDelay", INI::parseDurationUnsignedInt, NULL, offsetof( StructureToppleUpdateModuleData, m_minToppleBurstDelay ) },
  108. { "MaxToppleBurstDelay", INI::parseDurationUnsignedInt, NULL, offsetof( StructureToppleUpdateModuleData, m_maxToppleBurstDelay ) },
  109. { "StructuralIntegrity", INI::parseReal, NULL, offsetof( StructureToppleUpdateModuleData, m_structuralIntegrity ) },
  110. { "StructuralDecay", INI::parseReal, NULL, offsetof( StructureToppleUpdateModuleData, m_structuralDecay ) },
  111. { "DamageFXTypes", INI::parseDamageTypeFlags, NULL, offsetof( StructureToppleUpdateModuleData, m_damageFXTypes ) },
  112. { "TopplingFX", INI::parseFXList, NULL, offsetof( StructureToppleUpdateModuleData, m_toppleFXList ) },
  113. { "ToppleDelayFX", INI::parseFXList, NULL, offsetof( StructureToppleUpdateModuleData, m_toppleDelayFXList ) },
  114. { "ToppleStartFX", INI::parseFXList, NULL, offsetof( StructureToppleUpdateModuleData, m_toppleStartFXList ) },
  115. { "ToppleDoneFX", INI::parseFXList, NULL, offsetof( StructureToppleUpdateModuleData, m_toppleDoneFXList ) },
  116. { "CrushingFX", INI::parseFXList, NULL, offsetof( StructureToppleUpdateModuleData, m_crushingFXList ) },
  117. { "CrushingWeaponName", INI::parseAsciiString, NULL, offsetof( StructureToppleUpdateModuleData, m_crushingWeaponName ) },
  118. { "OCL", parseOCL, NULL, 0 },
  119. { "AngleFX", parseAngleFX, NULL, 0 },
  120. { 0, 0, 0, 0 }
  121. };
  122. p.add(dataFieldParse);
  123. p.add(DieMuxData::getFieldParse(), offsetof( StructureToppleUpdateModuleData, m_dieMuxData ));
  124. }
  125. //-------------------------------------------------------------------------------------------------
  126. //-------------------------------------------------------------------------------------------------
  127. void StructureToppleUpdate::beginStructureTopple(const DamageInfo *damageInfo)
  128. {
  129. const StructureToppleUpdateModuleData *d = getStructureToppleUpdateModuleData();
  130. if (d)
  131. {
  132. UnsignedInt now = TheGameLogic->getFrame();
  133. m_toppleFrame = now + GameLogicRandomValue(d->m_minToppleDelay, d->m_maxToppleDelay);
  134. Object *attacker = TheGameLogic->findObjectByID(damageInfo->in.m_sourceID);
  135. Object *building = getObject();
  136. Real toppleAngle = 0.0;
  137. if (attacker == NULL) {
  138. toppleAngle = GameLogicRandomValueReal(0.0, 2*PI);
  139. } else {
  140. const Coord3D *attackerPos = attacker->getPosition();
  141. const Coord3D *buildingPos = building->getPosition();
  142. // Calculate the topple direction to be the opposite of the direction fired from.
  143. m_toppleDirection.x = buildingPos->x - attackerPos->x;
  144. m_toppleDirection.y = buildingPos->y - attackerPos->y;
  145. // Give it a little randomness...
  146. toppleAngle = m_toppleDirection.toAngle();
  147. toppleAngle += GameLogicRandomValueReal(-PI/8, PI/8);
  148. }
  149. m_toppleDirection.x = Cos(toppleAngle);
  150. m_toppleDirection.y = Sin(toppleAngle);
  151. TheScriptEngine->adjustToppleDirection(getObject(), &m_toppleDirection);
  152. Real averageRadius = (building->getGeometryInfo().getMajorRadius() + building->getGeometryInfo().getMinorRadius()) / 2;
  153. Real explosionRadius = averageRadius * 0.90;
  154. m_delayBurstLocation.x = building->getPosition()->x + explosionRadius * Cos(toppleAngle);
  155. m_delayBurstLocation.y = building->getPosition()->y + explosionRadius * Sin(toppleAngle);
  156. m_delayBurstLocation.z = TheTerrainLogic->getGroundHeight(m_delayBurstLocation.x, m_delayBurstLocation.y);
  157. doToppleStartFX(building, damageInfo);
  158. m_nextBurstFrame = now + GameClientRandomValue(d->m_minToppleBurstDelay, d->m_maxToppleBurstDelay);
  159. m_toppleState = TOPPLESTATE_WAITINGFORTOPPLESTART;
  160. setWakeFrame(getObject(), UPDATE_SLEEP_NONE);
  161. }
  162. }
  163. //-------------------------------------------------------------------------------------------------
  164. //-------------------------------------------------------------------------------------------------
  165. void StructureToppleUpdate::onDie( const DamageInfo *damageInfo )
  166. {
  167. const StructureToppleUpdateModuleData* d = getStructureToppleUpdateModuleData();
  168. if (!d->m_dieMuxData.isDieApplicable(getObject(), damageInfo))
  169. return;
  170. AIUpdateInterface *ai = getObject()->getAIUpdateInterface();
  171. if (ai)
  172. ai->markAsDead();
  173. // Deselect the object for all players.
  174. TheGameLogic->deselectObject(getObject(), PLAYERMASK_ALL, TRUE);
  175. beginStructureTopple(damageInfo);
  176. }
  177. //-------------------------------------------------------------------------------------------------
  178. //-------------------------------------------------------------------------------------------------
  179. UpdateSleepTime StructureToppleUpdate::update( void )
  180. {
  181. static const Real TOPPLE_ACCELERATION_FACTOR = 0.02f;
  182. const StructureToppleUpdateModuleData *d = getStructureToppleUpdateModuleData();
  183. if (m_toppleState == TOPPLESTATE_STANDING)
  184. {
  185. DEBUG_CRASH(("hmm, what?"));
  186. return UPDATE_SLEEP_FOREVER;
  187. }
  188. // get last damage info
  189. const DamageInfo *lastDamageInfo = getObject()->getBodyModule()->getLastDamageInfo();
  190. // We are in the dramatic pause between when the building has lost all its hit points and
  191. // when it starts toppling over.
  192. if (m_toppleState == TOPPLESTATE_WAITINGFORTOPPLESTART) {
  193. UnsignedInt now = TheGameLogic->getFrame();
  194. if (now >= m_nextBurstFrame) {
  195. doToppleDelayBurstFX();
  196. // This uses a game client random value because the delay bursts are purely visual and aural effects.
  197. m_nextBurstFrame = now + GameClientRandomValue(d->m_minToppleBurstDelay, d->m_maxToppleBurstDelay);
  198. }
  199. if (now >= m_toppleFrame) {
  200. m_toppleState = TOPPLESTATE_TOPPLING;
  201. m_structuralIntegrity = d->m_structuralIntegrity;
  202. }
  203. }
  204. // The building is in the process of falling over.
  205. if (m_toppleState == TOPPLESTATE_TOPPLING) {
  206. UnsignedInt now = TheGameLogic->getFrame();
  207. Real toppleAcceleration = TOPPLE_ACCELERATION_FACTOR * (Sin(m_accumulatedAngle) * (1.0 - m_structuralIntegrity));
  208. // DEBUG_LOG(("toppleAcceleration = %f\n", toppleAcceleration));
  209. m_toppleVelocity += toppleAcceleration;
  210. // DEBUG_LOG(("m_toppleVelocity = %f\n", m_toppleVelocity));
  211. // doesn't make sense to have a structural integrity less than zero.
  212. if (m_structuralIntegrity > 0.0f) {
  213. m_structuralIntegrity *= d->m_structuralDecay;
  214. if (m_structuralIntegrity < 0.0f) {
  215. m_structuralIntegrity = 0.0f;
  216. }
  217. }
  218. // DEBUG_LOG(("m_structuralIntegrity = %f\n\n", m_structuralIntegrity));
  219. doAngleFX(m_accumulatedAngle, m_accumulatedAngle + m_toppleVelocity);
  220. m_accumulatedAngle += m_toppleVelocity;
  221. applyCrushingDamage(PI/2 - m_accumulatedAngle);
  222. if (m_accumulatedAngle >= PI/2) {
  223. m_toppleVelocity -= m_accumulatedAngle - PI/2;
  224. m_accumulatedAngle = PI/2;
  225. m_toppleState = TOPPLESTATE_WAITINGFORDONE;
  226. applyCrushingDamage(0.0f);
  227. doPhaseStuff(STPHASE_FINAL, getObject()->getPosition());
  228. if( lastDamageInfo == NULL || getDamageTypeFlag( d->m_damageFXTypes, lastDamageInfo->in.m_damageType ) )
  229. FXList::doFXObj(d->m_toppleDoneFXList, getObject());
  230. m_toppleFrame = TheGameLogic->getFrame();
  231. }
  232. if (now >= m_nextBurstFrame) {
  233. doToppleDelayBurstFX();
  234. // This uses a game client random value because the delay bursts are purely visual and aural effects.
  235. m_nextBurstFrame = now + GameClientRandomValue(d->m_minToppleBurstDelay, d->m_maxToppleBurstDelay);
  236. }
  237. Object *building = getObject();
  238. Matrix3D xfrm = *building->getTransformMatrix();
  239. xfrm.In_Place_Pre_Rotate_X(-m_toppleVelocity * m_toppleDirection.y);
  240. xfrm.In_Place_Pre_Rotate_Y(m_toppleVelocity * m_toppleDirection.x);
  241. building->setTransformMatrix(&xfrm);
  242. }
  243. // The building is now flat on the ground and done with all the crushing and all that.
  244. if (m_toppleState == TOPPLESTATE_WAITINGFORDONE)
  245. {
  246. if (m_toppleFrame <= TheGameLogic->getFrame())
  247. {
  248. Object *building = getObject();
  249. Drawable *drawable = building->getDrawable();
  250. drawable->clearModelConditionState(MODELCONDITION_RUBBLE);
  251. drawable->setModelConditionState(MODELCONDITION_POST_COLLAPSE);
  252. // Need to update body particle systems, now
  253. BodyModuleInterface *body = building->getBodyModule();
  254. body->updateBodyParticleSystems();
  255. doToppleDoneStuff();
  256. m_toppleState = TOPPLESTATE_DONE;
  257. return UPDATE_SLEEP_FOREVER;
  258. }
  259. }
  260. return UPDATE_SLEEP_NONE;
  261. }
  262. //-------------------------------------------------------------------------------------------------
  263. //-------------------------------------------------------------------------------------------------
  264. void StructureToppleUpdate::doToppleDoneStuff()
  265. {
  266. static NameKeyType key_BoneFXUpdate = NAMEKEY("BoneFXUpdate");
  267. BoneFXUpdate *bfxu = (BoneFXUpdate *)getObject()->findUpdateModule(key_BoneFXUpdate);
  268. if (bfxu != NULL) {
  269. bfxu->stopAllBoneFX();
  270. }
  271. Object *building = getObject();
  272. Real origAngle = building->getOrientation();
  273. building->setOrientation(origAngle);
  274. Real toppleAngle = m_toppleDirection.toAngle();
  275. Matrix3D xfrm = *building->getTransformMatrix();
  276. xfrm.In_Place_Pre_Rotate_Z(toppleAngle-origAngle);
  277. building->setTransformMatrix(&xfrm);
  278. }
  279. //-------------------------------------------------------------------------------------------------
  280. //-------------------------------------------------------------------------------------------------
  281. void StructureToppleUpdate::doAngleFX(Real curAngle, Real newAngle)
  282. {
  283. const StructureToppleUpdateModuleData *d = getStructureToppleUpdateModuleData();
  284. const DamageInfo *lastDamageInfo = getObject()->getBodyModule()->getLastDamageInfo();
  285. for (std::vector<AngleFXInfo>::const_iterator it = d->angleFX.begin(); it != d->angleFX.end(); ++it)
  286. {
  287. if ((it->angle > curAngle) && (it->angle <= newAngle))
  288. {
  289. if( lastDamageInfo == NULL || getDamageTypeFlag( d->m_damageFXTypes, lastDamageInfo->in.m_damageType ) )
  290. FXList::doFXObj(it->fxList, getObject());
  291. }
  292. }
  293. }
  294. //-------------------------------------------------------------------------------------------------
  295. //-------------------------------------------------------------------------------------------------
  296. // theta is the angle of the building with respect to the ground.
  297. void StructureToppleUpdate::applyCrushingDamage(Real theta)
  298. {
  299. // static const Real THETA_CEILING = PI/8; // This weapon won't do any damage until theta is less than this value.
  300. static const Real THETA_CEILING = PI/6; // This weapon won't do any damage until theta is less than this value.
  301. static const Real WEAPON_SPACING_PERPENDICULAR = 25; // The spacing between weapon firing locations,
  302. // distance is perpendicular to the direction the
  303. // building is falling in.
  304. const StructureToppleUpdateModuleData *d = getStructureToppleUpdateModuleData();
  305. if (theta > THETA_CEILING) {
  306. return;
  307. }
  308. Object *building = getObject();
  309. Real orientationAngle = building->getOrientation();
  310. Real toppleAngle = m_toppleDirection.toAngle();
  311. // Figure out the width of the projection of the boundary of the building along the topple direction.
  312. // Do this because the amount of ground that is affected will be different if the building falls
  313. // in different orientations.
  314. Real angle = orientationAngle - toppleAngle;
  315. Real minorComponent = building->getGeometryInfo().getMinorRadius() * Cos(angle);
  316. Real majorComponent = building->getGeometryInfo().getMajorRadius() * Sin(angle);
  317. Coord3D temp3D;
  318. temp3D.x = majorComponent;
  319. temp3D.y = minorComponent;
  320. temp3D.z = 0.0f;
  321. Real facingWidth = temp3D.length() / 2;
  322. // Get the crushing weapon.
  323. const WeaponTemplate* wt = TheWeaponStore->findWeaponTemplate(d->m_crushingWeaponName);
  324. if (wt == NULL) {
  325. return;
  326. }
  327. // The furthest away from the base of the building to explode on.
  328. Real maxDistance = m_buildingHeight * (1.0 - Sin(theta));
  329. /*
  330. * Fire explosions at regular intervals across the area that the building is currently
  331. * crushing. The explosions occur across the face and along the length of the building.
  332. */
  333. Real jcos;
  334. Real jsin;
  335. // Coord3D target;
  336. for (Real j = m_lastCrushedLocation; j < maxDistance; j += WEAPON_SPACING_PERPENDICULAR) {
  337. jcos = j * Cos(toppleAngle);
  338. jsin = j * Sin(toppleAngle);
  339. doDamageLine(building, wt, jcos, jsin, facingWidth, toppleAngle);
  340. }
  341. jcos = maxDistance * Cos(toppleAngle);
  342. jsin = maxDistance * Sin(toppleAngle);
  343. doDamageLine(building, wt, jcos, jsin, facingWidth, toppleAngle);
  344. m_lastCrushedLocation = j;
  345. }
  346. //-------------------------------------------------------------------------------------------------
  347. //-------------------------------------------------------------------------------------------------
  348. void StructureToppleUpdate::doDamageLine(Object *building, const WeaponTemplate* wt, Real jcos, Real jsin, Real facingWidth, Real toppleAngle)
  349. {
  350. const DamageInfo *lastDamageInfo = getObject()->getBodyModule()->getLastDamageInfo();
  351. static const Real WEAPON_SPACING_PARALLEL = 25; // The spacing between weapon firing locations,
  352. // distance is parallel to the direction the building
  353. // is falling in.
  354. const StructureToppleUpdateModuleData *d = getStructureToppleUpdateModuleData();
  355. Coord3D target;
  356. for (Real i = -facingWidth; i < facingWidth; i += WEAPON_SPACING_PARALLEL)
  357. {
  358. target.x = building->getPosition()->x + jcos + (i * Sin(toppleAngle));
  359. target.y = building->getPosition()->y + jsin + (i * Cos(toppleAngle));
  360. target.z = TheTerrainLogic->getGroundHeight(target.x, target.y);
  361. TheWeaponStore->createAndFireTempWeapon(wt, building, &target);
  362. // do the crushing particle effects
  363. if( lastDamageInfo == NULL || getDamageTypeFlag( d->m_damageFXTypes, lastDamageInfo->in.m_damageType ) )
  364. FXList::doFXPos(d->m_crushingFXList, &target);
  365. }
  366. // Make sure there are weapons fired and FX done on the edge of the building.
  367. target.x = building->getPosition()->x + jcos + (facingWidth * Sin(toppleAngle));
  368. target.y = building->getPosition()->y + jsin + (facingWidth * Cos(toppleAngle));
  369. target.z = TheTerrainLogic->getGroundHeight(target.x, target.y);
  370. TheWeaponStore->createAndFireTempWeapon(wt, building, &target);
  371. // do the crushing particle effects
  372. if( lastDamageInfo == NULL || getDamageTypeFlag( d->m_damageFXTypes, lastDamageInfo->in.m_damageType ) )
  373. FXList::doFXPos(d->m_crushingFXList, &target);
  374. // Do the flying debris for this line.
  375. target.x = building->getPosition()->x + jcos;
  376. target.y = building->getPosition()->y + jsin;
  377. target.z = TheTerrainLogic->getGroundHeight(target.x, target.y);
  378. doPhaseStuff(STPHASE_FINAL, &target);
  379. }
  380. //-------------------------------------------------------------------------------------------------
  381. //-------------------------------------------------------------------------------------------------
  382. void StructureToppleUpdate::doToppleStartFX(Object *building, const DamageInfo *damageInfo)
  383. {
  384. const StructureToppleUpdateModuleData *d = getStructureToppleUpdateModuleData();
  385. const DamageInfo *lastDamageInfo = getObject()->getBodyModule()->getLastDamageInfo();
  386. if( lastDamageInfo == NULL || getDamageTypeFlag( d->m_damageFXTypes, lastDamageInfo->in.m_damageType ) )
  387. FXList::doFXPos(d->m_toppleStartFXList, building->getPosition());
  388. doPhaseStuff(STPHASE_INITIAL, building->getPosition());
  389. }
  390. //-------------------------------------------------------------------------------------------------
  391. //-------------------------------------------------------------------------------------------------
  392. void StructureToppleUpdate::doToppleDelayBurstFX()
  393. {
  394. const StructureToppleUpdateModuleData *d = getStructureToppleUpdateModuleData();
  395. const DamageInfo *lastDamageInfo = getObject()->getBodyModule()->getLastDamageInfo();
  396. DEBUG_LOG(("Doing topple delay burst on frame %d\n", TheGameLogic->getFrame()));
  397. if( lastDamageInfo == NULL || getDamageTypeFlag( d->m_damageFXTypes, lastDamageInfo->in.m_damageType ) )
  398. FXList::doFXPos(d->m_toppleDelayFXList, &m_delayBurstLocation);
  399. Object *building = getObject();
  400. Drawable *drawable = building->getDrawable();
  401. if( lastDamageInfo == NULL || getDamageTypeFlag( d->m_damageFXTypes, lastDamageInfo->in.m_damageType ) )
  402. {
  403. for (std::vector<FXBoneInfo>::const_iterator it = d->fxbones.begin(); it != d->fxbones.end(); ++it)
  404. {
  405. ParticleSystem *sys = TheParticleSystemManager->createParticleSystem(it->particleSystemTemplate);
  406. if (sys != NULL)
  407. {
  408. Coord3D pos;
  409. if (drawable->getPristineBonePositions(it->boneName.str(), 0, &pos, NULL, 1) == 1)
  410. {
  411. // got the bone position...
  412. sys->setPosition(&pos);
  413. // Attatch it to the object...
  414. sys->attachToDrawable(drawable);
  415. }
  416. }
  417. }
  418. }
  419. doPhaseStuff(STPHASE_DELAY, &m_delayBurstLocation);
  420. }
  421. //-------------------------------------------------------------------------------------------------
  422. //-------------------------------------------------------------------------------------------------
  423. inline Bool inList(Int value, Int count, const Int idxList[])
  424. {
  425. for (Int j = 0; j < count; ++j)
  426. {
  427. if (idxList[j] == value)
  428. return true;
  429. }
  430. return false;
  431. }
  432. //-------------------------------------------------------------------------------------------------
  433. //-------------------------------------------------------------------------------------------------
  434. static void buildNonDupRandomIndexList(Int range, Int count, Int idxList[])
  435. {
  436. for (Int i = 0; i < count; ++i)
  437. {
  438. Int idx;
  439. do
  440. {
  441. idx = GameLogicRandomValue(0, range-1);
  442. }
  443. while (inList(idx, i, idxList));
  444. idxList[i] = idx;
  445. }
  446. }
  447. //-------------------------------------------------------------------------------------------------
  448. //-------------------------------------------------------------------------------------------------
  449. void StructureToppleUpdate::doPhaseStuff(StructureTopplePhaseType stphase, const Coord3D *target)
  450. {
  451. const StructureToppleUpdateModuleData* d = getStructureToppleUpdateModuleData();
  452. Int i, idx, count, listSize;
  453. Int idxList[MAX_IDX];
  454. listSize = d->m_ocls[stphase].size();
  455. if (listSize > 0)
  456. {
  457. count = d->m_oclCount[stphase];
  458. buildNonDupRandomIndexList(listSize, count, idxList);
  459. for (i = 0; i < count; ++i)
  460. {
  461. idx = idxList[i];
  462. const OCLVec& v = d->m_ocls[stphase];
  463. DEBUG_ASSERTCRASH(idx>=0&&idx<v.size(),("bad idx"));
  464. const ObjectCreationList* ocl = v[idx];
  465. ObjectCreationList::create(ocl, getObject(), target, NULL, INVALID_ANGLE );
  466. }
  467. }
  468. }
  469. // ------------------------------------------------------------------------------------------------
  470. /** CRC */
  471. // ------------------------------------------------------------------------------------------------
  472. void StructureToppleUpdate::crc( Xfer *xfer )
  473. {
  474. // extend base class
  475. UpdateModule::crc( xfer );
  476. } // end crc
  477. // ------------------------------------------------------------------------------------------------
  478. /** Xfer method
  479. * Version Info:
  480. * 1: Initial version */
  481. // ------------------------------------------------------------------------------------------------
  482. void StructureToppleUpdate::xfer( Xfer *xfer )
  483. {
  484. // version
  485. XferVersion currentVersion = 1;
  486. XferVersion version = currentVersion;
  487. xfer->xferVersion( &version, currentVersion );
  488. // extend base class
  489. UpdateModule::xfer( xfer );
  490. // topple frame
  491. xfer->xferUnsignedInt( &m_toppleFrame );
  492. // topple direction
  493. xfer->xferCoord2D( &m_toppleDirection );
  494. // topple state
  495. xfer->xferUser( &m_toppleState, sizeof( StructureToppleStateType ) );
  496. // topple velocity
  497. xfer->xferReal( &m_toppleVelocity );
  498. // accumulated angle
  499. xfer->xferReal( &m_accumulatedAngle );
  500. // structural integrity
  501. xfer->xferReal( &m_structuralIntegrity );
  502. // last crushed location
  503. xfer->xferReal( &m_lastCrushedLocation );
  504. // next burst frame
  505. xfer->xferInt( &m_nextBurstFrame );
  506. // delay burst location
  507. xfer->xferCoord3D( &m_delayBurstLocation );
  508. } // end xfer
  509. // ------------------------------------------------------------------------------------------------
  510. /** Load post process */
  511. // ------------------------------------------------------------------------------------------------
  512. void StructureToppleUpdate::loadPostProcess( void )
  513. {
  514. // extend base class
  515. UpdateModule::loadPostProcess();
  516. } // end loadPostProcess