explosion.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/fx/explosion.h"
  24. #include "core/resourceManager.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/typeValidators.h"
  27. #include "sfx/sfxSystem.h"
  28. #include "sfx/sfxTrack.h"
  29. #include "sfx/sfxTypes.h"
  30. #include "scene/sceneManager.h"
  31. #include "scene/sceneRenderState.h"
  32. #include "lighting/lightInfo.h"
  33. #include "lighting/lightManager.h"
  34. #include "core/stream/bitStream.h"
  35. #include "sim/netConnection.h"
  36. #include "ts/tsShape.h"
  37. #include "ts/tsShapeInstance.h"
  38. #include "math/mRandom.h"
  39. #include "math/mathIO.h"
  40. #include "math/mathUtils.h"
  41. #include "T3D/debris.h"
  42. #include "T3D/gameBase/gameConnection.h"
  43. #include "T3D/fx/particleEmitter.h"
  44. #include "T3D/fx/cameraFXMgr.h"
  45. #include "T3D/debris.h"
  46. #include "T3D/shapeBase.h"
  47. #include "T3D/gameBase/gameProcess.h"
  48. #include "renderInstance/renderPassManager.h"
  49. #include "console/engineAPI.h"
  50. IMPLEMENT_CONOBJECT(Explosion);
  51. ConsoleDocClass( Explosion,
  52. "@brief The emitter for an explosion effect, with properties defined by a "
  53. "ExplosionData object.\n\n"
  54. "@ingroup FX\n"
  55. "The object will initiate the explosion effects automatically after being "
  56. "added to the simulation.\n"
  57. "@tsexample\n"
  58. "datablock ExplosionData( GrenadeSubExplosion )\n"
  59. "{\n"
  60. " offset = 0.25;\n"
  61. " emitter[0] = GrenadeExpSparkEmitter;\n\n"
  62. " lightStartRadius = 4.0;\n"
  63. " lightEndRadius = 0.0;\n"
  64. " lightStartColor = \"0.9 0.7 0.7\";\n"
  65. " lightEndColor = \"0.9 0.7 0.7\";\n"
  66. " lightStartBrightness = 2.0;\n"
  67. " lightEndBrightness = 0.0;\n"
  68. "};\n\n"
  69. "datablock ExplosionData( GrenadeLauncherExplosion )\n"
  70. "{\n"
  71. " soundProfile = GrenadeLauncherExplosionSound;\n"
  72. " lifeTimeMS = 400; // Quick flash, short burn, and moderate dispersal\n\n"
  73. " // Volume particles\n"
  74. " particleEmitter = GrenadeExpFireEmitter;\n"
  75. " particleDensity = 75;\n"
  76. " particleRadius = 2.25;\n\n"
  77. " // Point emission\n"
  78. " emitter[0] = GrenadeExpDustEmitter;\n"
  79. " emitter[1] = GrenadeExpSparksEmitter;\n"
  80. " emitter[2] = GrenadeExpSmokeEmitter;\n\n"
  81. " // Sub explosion objects\n"
  82. " subExplosion[0] = GrenadeSubExplosion;\n\n"
  83. " // Camera Shaking\n"
  84. " shakeCamera = true;\n"
  85. " camShakeFreq = \"10.0 11.0 9.0\";\n"
  86. " camShakeAmp = \"15.0 15.0 15.0\";\n"
  87. " camShakeDuration = 1.5;\n"
  88. " camShakeRadius = 20;\n\n"
  89. " // Exploding debris\n"
  90. " debris = GrenadeDebris;\n"
  91. " debrisThetaMin = 10;\n"
  92. " debrisThetaMax = 60;\n"
  93. " debrisNum = 4;\n"
  94. " debrisNumVariance = 2;\n"
  95. " debrisVelocity = 25;\n"
  96. " debrisVelocityVariance = 5;\n\n"
  97. " lightStartRadius = 4.0;\n"
  98. " lightEndRadius = 0.0;\n"
  99. " lightStartColor = \"1.0 1.0 1.0\";\n"
  100. " lightEndColor = \"1.0 1.0 1.0\";\n"
  101. " lightStartBrightness = 4.0;\n"
  102. " lightEndBrightness = 0.0;\n"
  103. " lightNormalOffset = 2.0;\n"
  104. "};\n\n"
  105. "function createExplosion()\n"
  106. "{\n"
  107. " // Create a new explosion - it will explode automatically\n"
  108. " %pos = \"0 0 100\";\n"
  109. " %obj = new Explosion()\n"
  110. " {\n"
  111. " position = %pos;\n"
  112. " dataBlock = GrenadeLauncherExplosion;\n"
  113. " };\n"
  114. "}\n\n"
  115. "// schedule an explosion\n"
  116. "schedule(1000, 0, createExplosion);\n"
  117. "@endtsexample"
  118. );
  119. #define MaxLightRadius 20
  120. MRandomLCG sgRandom(0xdeadbeef);
  121. DefineEngineFunction(calcExplosionCoverage, F32, (Point3F pos, S32 id, U32 covMask),(Point3F(0.0f,0.0f,0.0f), NULL, NULL),
  122. "@brief Calculates how much an explosion effects a specific object.\n\n"
  123. "Use this to determine how much damage to apply to objects based on their "
  124. "distance from the explosion's center point, and whether the explosion is "
  125. "blocked by other objects.\n\n"
  126. "@param pos Center position of the explosion.\n"
  127. "@param id Id of the object of which to check coverage.\n"
  128. "@param covMask Mask of object types that may block the explosion.\n"
  129. "@return Coverage value from 0 (not affected by the explosion) to 1 (fully affected)\n\n"
  130. "@tsexample\n"
  131. "// Get the position of the explosion.\n"
  132. "%position = %explosion.getPosition();\n\n"
  133. "// Set a list of TypeMasks (defined in gameFunctioncs.cpp), seperated by the | character.\n"
  134. "%TypeMasks = $TypeMasks::StaticObjectType | $TypeMasks::ItemObjectType\n\n"
  135. "// Acquire the damage value from 0.0f - 1.0f.\n"
  136. "%coverage = calcExplosionCoverage( %position, %sceneObject, %TypeMasks );\n\n"
  137. "// Apply damage to object\n"
  138. "%sceneObject.applyDamage( %coverage * 20 );\n"
  139. "@endtsexample\n"
  140. "@ingroup FX")
  141. {
  142. Point3F center;
  143. SceneObject* sceneObject = NULL;
  144. if (Sim::findObject(id, sceneObject) == false) {
  145. Con::warnf(ConsoleLogEntry::General, "calcExplosionCoverage: couldn't find object: %s", id);
  146. return 1.0f;
  147. }
  148. if (sceneObject->isClientObject() || sceneObject->getContainer() == NULL) {
  149. Con::warnf(ConsoleLogEntry::General, "calcExplosionCoverage: object is on the client, or not in the container system");
  150. return 1.0f;
  151. }
  152. sceneObject->getObjBox().getCenter(&center);
  153. center.convolve(sceneObject->getScale());
  154. sceneObject->getTransform().mulP(center);
  155. RayInfo rayInfo;
  156. sceneObject->disableCollision();
  157. if (sceneObject->getContainer()->castRay(pos, center, covMask, &rayInfo) == true) {
  158. // Try casting up and then out
  159. if (sceneObject->getContainer()->castRay(pos, pos + Point3F(0.0f, 0.0f, 1.0f), covMask, &rayInfo) == false)
  160. {
  161. if (sceneObject->getContainer()->castRay(pos + Point3F(0.0f, 0.0f, 1.0f), center, covMask, &rayInfo) == false)
  162. {
  163. sceneObject->enableCollision();
  164. return 1.0f;
  165. }
  166. }
  167. sceneObject->enableCollision();
  168. return 0.0f;
  169. } else {
  170. sceneObject->enableCollision();
  171. return 1.0f;
  172. }
  173. }
  174. //----------------------------------------------------------------------------
  175. //
  176. IMPLEMENT_CO_DATABLOCK_V1(ExplosionData);
  177. ConsoleDocClass( ExplosionData,
  178. "@brief Defines the attributes of an Explosion: particleEmitters, debris, "
  179. "lighting and camera shake effects.\n"
  180. "@ingroup FX\n"
  181. );
  182. ExplosionData::ExplosionData()
  183. {
  184. dtsFileName = NULL;
  185. particleDensity = 10;
  186. particleRadius = 1.0f;
  187. faceViewer = false;
  188. soundProfile = NULL;
  189. particleEmitter = NULL;
  190. particleEmitterId = 0;
  191. explosionScale.set(1.0f, 1.0f, 1.0f);
  192. playSpeed = 1.0f;
  193. dMemset( emitterList, 0, sizeof( emitterList ) );
  194. dMemset( emitterIDList, 0, sizeof( emitterIDList ) );
  195. dMemset( debrisList, 0, sizeof( debrisList ) );
  196. dMemset( debrisIDList, 0, sizeof( debrisIDList ) );
  197. debrisThetaMin = 0.0f;
  198. debrisThetaMax = 90.0f;
  199. debrisPhiMin = 0.0f;
  200. debrisPhiMax = 360.0f;
  201. debrisNum = 1;
  202. debrisNumVariance = 0;
  203. debrisVelocity = 2.0f;
  204. debrisVelocityVariance = 0.0f;
  205. dMemset( explosionList, 0, sizeof( explosionList ) );
  206. dMemset( explosionIDList, 0, sizeof( explosionIDList ) );
  207. delayMS = 0;
  208. delayVariance = 0;
  209. lifetimeMS = 1000;
  210. lifetimeVariance = 0;
  211. offset = 0.0f;
  212. shockwave = NULL;
  213. shockwaveID = 0;
  214. shockwaveOnTerrain = false;
  215. shakeCamera = false;
  216. camShakeFreq.set( 10.0f, 10.0f, 10.0f );
  217. camShakeAmp.set( 1.0f, 1.0f, 1.0f );
  218. camShakeDuration = 1.5f;
  219. camShakeRadius = 10.0f;
  220. camShakeFalloff = 10.0f;
  221. for( U32 i=0; i<EC_NUM_TIME_KEYS; i++ )
  222. {
  223. times[i] = 1.0f;
  224. }
  225. times[0] = 0.0f;
  226. for( U32 j=0; j<EC_NUM_TIME_KEYS; j++ )
  227. {
  228. sizes[j].set( 1.0f, 1.0f, 1.0f );
  229. }
  230. //
  231. lightStartRadius = lightEndRadius = 0.0f;
  232. lightStartColor.set(1.0f,1.0f,1.0f);
  233. lightEndColor.set(1.0f,1.0f,1.0f);
  234. lightStartBrightness = 1.0f;
  235. lightEndBrightness = 1.0f;
  236. lightNormalOffset = 0.1f;
  237. }
  238. void ExplosionData::initPersistFields()
  239. {
  240. addField( "explosionShape", TypeShapeFilename, Offset(dtsFileName, ExplosionData),
  241. "@brief Optional DTS or DAE shape to place at the center of the explosion.\n\n"
  242. "The <i>ambient</i> animation of this model will be played automatically at "
  243. "the start of the explosion." );
  244. addField( "explosionScale", TypePoint3F, Offset(explosionScale, ExplosionData),
  245. "\"X Y Z\" scale factor applied to the explosionShape model at the start "
  246. "of the explosion." );
  247. addField( "playSpeed", TypeF32, Offset(playSpeed, ExplosionData),
  248. "Time scale at which to play the explosionShape <i>ambient</i> sequence." );
  249. addField( "soundProfile", TYPEID< SFXTrack >(), Offset(soundProfile, ExplosionData),
  250. "Non-looping sound effect that will be played at the start of the explosion." );
  251. addField( "faceViewer", TypeBool, Offset(faceViewer, ExplosionData),
  252. "Controls whether the visual effects of the explosion always face the camera." );
  253. addField( "particleEmitter", TYPEID< ParticleEmitterData >(), Offset(particleEmitter, ExplosionData),
  254. "@brief Emitter used to generate a cloud of particles at the start of the explosion.\n\n"
  255. "Explosions can generate two different particle effects. The first is a "
  256. "single burst of particles at the start of the explosion emitted in a "
  257. "spherical cloud using particleEmitter.\n\n"
  258. "The second effect spawns the list of ParticleEmitters given by the emitter[] "
  259. "field. These emitters generate particles in the normal way throughout the "
  260. "lifetime of the explosion." );
  261. addField( "particleDensity", TypeS32, Offset(particleDensity, ExplosionData),
  262. "@brief Density of the particle cloud created at the start of the explosion.\n\n"
  263. "@see particleEmitter" );
  264. addField( "particleRadius", TypeF32, Offset(particleRadius, ExplosionData),
  265. "@brief Radial distance from the explosion center at which cloud particles "
  266. "are emitted.\n\n"
  267. "@see particleEmitter" );
  268. addField( "emitter", TYPEID< ParticleEmitterData >(), Offset(emitterList, ExplosionData), EC_NUM_EMITTERS,
  269. "@brief List of additional ParticleEmitterData objects to spawn with this "
  270. "explosion.\n\n"
  271. "@see particleEmitter" );
  272. // addField( "shockwave", TypeShockwaveDataPtr, Offset(shockwave, ExplosionData) );
  273. // addField( "shockwaveOnTerrain", TypeBool, Offset(shockwaveOnTerrain, ExplosionData) );
  274. addField( "debris", TYPEID< DebrisData >(), Offset(debrisList, ExplosionData), EC_NUM_DEBRIS_TYPES,
  275. "List of DebrisData objects to spawn with this explosion." );
  276. addField( "debrisThetaMin", TypeF32, Offset(debrisThetaMin, ExplosionData),
  277. "Minimum angle, from the horizontal plane, to eject debris from." );
  278. addField( "debrisThetaMax", TypeF32, Offset(debrisThetaMax, ExplosionData),
  279. "Maximum angle, from the horizontal plane, to eject debris from." );
  280. addField( "debrisPhiMin", TypeF32, Offset(debrisPhiMin, ExplosionData),
  281. "Minimum reference angle, from the vertical plane, to eject debris from." );
  282. addField( "debrisPhiMax", TypeF32, Offset(debrisPhiMax, ExplosionData),
  283. "Maximum reference angle, from the vertical plane, to eject debris from." );
  284. addField( "debrisNum", TypeS32, Offset(debrisNum, ExplosionData),
  285. "Number of debris objects to create." );
  286. addField( "debrisNumVariance", TypeS32, Offset(debrisNumVariance, ExplosionData),
  287. "Variance in the number of debris objects to create (must be from 0 - debrisNum)." );
  288. addField( "debrisVelocity", TypeF32, Offset(debrisVelocity, ExplosionData),
  289. "Velocity to toss debris at." );
  290. addField( "debrisVelocityVariance", TypeF32, Offset(debrisVelocityVariance, ExplosionData),
  291. "Variance in the debris initial velocity (must be >= 0)." );
  292. addField( "subExplosion", TYPEID< ExplosionData >(), Offset(explosionList, ExplosionData), EC_MAX_SUB_EXPLOSIONS,
  293. "List of additional ExplosionData objects to create at the start of the "
  294. "explosion." );
  295. addField( "delayMS", TypeS32, Offset(delayMS, ExplosionData),
  296. "Amount of time, in milliseconds, to delay the start of the explosion effect "
  297. "from the creation of the Explosion object." );
  298. addField( "delayVariance", TypeS32, Offset(delayVariance, ExplosionData),
  299. "Variance, in milliseconds, of delayMS." );
  300. addField( "lifetimeMS", TypeS32, Offset(lifetimeMS, ExplosionData),
  301. "@brief Lifetime, in milliseconds, of the Explosion object.\n\n"
  302. "@note If explosionShape is defined and contains an <i>ambient</i> animation, "
  303. "this field is ignored, and the playSpeed scaled duration of the animation "
  304. "is used instead." );
  305. addField( "lifetimeVariance", TypeS32, Offset(lifetimeVariance, ExplosionData),
  306. "Variance, in milliseconds, of the lifetimeMS of the Explosion object.\n" );
  307. addField( "offset", TypeF32, Offset(offset, ExplosionData),
  308. "@brief Offset distance (in a random direction) of the center of the explosion "
  309. "from the Explosion object position.\n\n"
  310. "Most often used to create some variance in position for subExplosion effects." );
  311. addField( "times", TypeF32, Offset(times, ExplosionData), EC_NUM_TIME_KEYS,
  312. "@brief Time keyframes used to scale the explosionShape model.\n\n"
  313. "Values should be in increasing order from 0.0 - 1.0, and correspond to "
  314. "the life of the Explosion where 0 is the beginning and 1 is the end of "
  315. "the explosion lifetime.\n"
  316. "@see lifetimeMS" );
  317. addField( "sizes", TypePoint3F, Offset(sizes, ExplosionData), EC_NUM_TIME_KEYS,
  318. "@brief \"X Y Z\" size keyframes used to scale the explosionShape model.\n\n"
  319. "The explosionShape (if defined) will be scaled using the times/sizes "
  320. "keyframes over the lifetime of the explosion.\n"
  321. "@see lifetimeMS" );
  322. addField( "shakeCamera", TypeBool, Offset(shakeCamera, ExplosionData),
  323. "Controls whether the camera shakes during this explosion." );
  324. addField( "camShakeFreq", TypePoint3F, Offset(camShakeFreq, ExplosionData),
  325. "Frequency of camera shaking, defined in the \"X Y Z\" axes." );
  326. addField( "camShakeAmp", TypePoint3F, Offset(camShakeAmp, ExplosionData),
  327. "@brief Amplitude of camera shaking, defined in the \"X Y Z\" axes.\n\n"
  328. "Set any value to 0 to disable shaking in that axis." );
  329. addField( "camShakeDuration", TypeF32, Offset(camShakeDuration, ExplosionData),
  330. "Duration (in seconds) to shake the camera." );
  331. addField( "camShakeRadius", TypeF32, Offset(camShakeRadius, ExplosionData),
  332. "Radial distance that a camera's position must be within relative to the "
  333. "center of the explosion to be shaken." );
  334. addField( "camShakeFalloff", TypeF32, Offset(camShakeFalloff, ExplosionData),
  335. "Falloff value for the camera shake." );
  336. addField( "lightStartRadius", TypeF32, Offset(lightStartRadius, ExplosionData),
  337. "@brief Initial radius of the PointLight created by this explosion.\n\n"
  338. "Radius is linearly interpolated from lightStartRadius to lightEndRadius "
  339. "over the lifetime of the explosion.\n"
  340. "@see lifetimeMS" );
  341. addField( "lightEndRadius", TypeF32, Offset(lightEndRadius, ExplosionData),
  342. "@brief Final radius of the PointLight created by this explosion.\n\n"
  343. "@see lightStartRadius" );
  344. addField( "lightStartColor", TypeColorF, Offset(lightStartColor, ExplosionData),
  345. "@brief Initial color of the PointLight created by this explosion.\n\n"
  346. "Color is linearly interpolated from lightStartColor to lightEndColor "
  347. "over the lifetime of the explosion.\n"
  348. "@see lifetimeMS" );
  349. addField( "lightEndColor", TypeColorF, Offset(lightEndColor, ExplosionData),
  350. "@brief Final color of the PointLight created by this explosion.\n\n"
  351. "@see lightStartColor" );
  352. addField( "lightStartBrightness", TypeF32, Offset(lightStartBrightness, ExplosionData),
  353. "@brief Initial brightness of the PointLight created by this explosion.\n\n"
  354. "Brightness is linearly interpolated from lightStartBrightness to "
  355. "lightEndBrightness over the lifetime of the explosion.\n"
  356. "@see lifetimeMS" );
  357. addField("lightEndBrightness", TypeF32, Offset(lightEndBrightness, ExplosionData),
  358. "@brief Final brightness of the PointLight created by this explosion.\n\n"
  359. "@see lightStartBrightness" );
  360. addField( "lightNormalOffset", TypeF32, Offset(lightNormalOffset, ExplosionData),
  361. "Distance (in the explosion normal direction) of the PointLight position "
  362. "from the explosion center." );
  363. Parent::initPersistFields();
  364. }
  365. bool ExplosionData::onAdd()
  366. {
  367. if (Parent::onAdd() == false)
  368. return false;
  369. if (explosionScale.x < 0.01f || explosionScale.y < 0.01f || explosionScale.z < 0.01f)
  370. {
  371. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s)::onAdd: ExplosionScale components must be >= 0.01", getName());
  372. explosionScale.x = explosionScale.x < 0.01f ? 0.01f : explosionScale.x;
  373. explosionScale.y = explosionScale.y < 0.01f ? 0.01f : explosionScale.y;
  374. explosionScale.z = explosionScale.z < 0.01f ? 0.01f : explosionScale.z;
  375. }
  376. if (debrisThetaMin < 0.0f)
  377. {
  378. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) debrisThetaMin < 0.0", getName());
  379. debrisThetaMin = 0.0f;
  380. }
  381. if (debrisThetaMax > 180.0f)
  382. {
  383. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) debrisThetaMax > 180.0", getName());
  384. debrisThetaMax = 180.0f;
  385. }
  386. if (debrisThetaMin > debrisThetaMax) {
  387. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) debrisThetaMin > debrisThetaMax", getName());
  388. debrisThetaMin = debrisThetaMax;
  389. }
  390. if (debrisPhiMin < 0.0f)
  391. {
  392. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) debrisPhiMin < 0.0", getName());
  393. debrisPhiMin = 0.0f;
  394. }
  395. if (debrisPhiMax > 360.0f)
  396. {
  397. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) debrisPhiMax > 360.0", getName());
  398. debrisPhiMax = 360.0f;
  399. }
  400. if (debrisPhiMin > debrisPhiMax) {
  401. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) debrisPhiMin > debrisPhiMax", getName());
  402. debrisPhiMin = debrisPhiMax;
  403. }
  404. if (debrisNum > 1000) {
  405. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) debrisNum > 1000", getName());
  406. debrisNum = 1000;
  407. }
  408. if (debrisNumVariance > 1000) {
  409. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) debrisNumVariance > 1000", getName());
  410. debrisNumVariance = 1000;
  411. }
  412. if (debrisVelocity < 0.1f)
  413. {
  414. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) debrisVelocity < 0.1", getName());
  415. debrisVelocity = 0.1f;
  416. }
  417. if (debrisVelocityVariance > 1000) {
  418. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) debrisVelocityVariance > 1000", getName());
  419. debrisVelocityVariance = 1000;
  420. }
  421. if (playSpeed < 0.05f)
  422. {
  423. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) playSpeed < 0.05", getName());
  424. playSpeed = 0.05f;
  425. }
  426. if (lifetimeMS < 1) {
  427. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) lifetimeMS < 1", getName());
  428. lifetimeMS = 1;
  429. }
  430. if (lifetimeVariance > lifetimeMS) {
  431. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) lifetimeVariance > lifetimeMS", getName());
  432. lifetimeVariance = lifetimeMS;
  433. }
  434. if (delayMS < 0) {
  435. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) delayMS < 0", getName());
  436. delayMS = 0;
  437. }
  438. if (delayVariance > delayMS) {
  439. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) delayVariance > delayMS", getName());
  440. delayVariance = delayMS;
  441. }
  442. if (offset < 0.0f)
  443. {
  444. Con::warnf(ConsoleLogEntry::General, "ExplosionData(%s) offset < 0.0", getName());
  445. offset = 0.0f;
  446. }
  447. S32 i;
  448. for( i=0; i<EC_NUM_DEBRIS_TYPES; i++ )
  449. {
  450. if( !debrisList[i] && debrisIDList[i] != 0 )
  451. {
  452. if( !Sim::findObject( debrisIDList[i], debrisList[i] ) )
  453. {
  454. Con::errorf( ConsoleLogEntry::General, "ExplosionData::onAdd: Invalid packet, bad datablockId(debris): 0x%x", debrisIDList[i] );
  455. }
  456. }
  457. }
  458. for( i=0; i<EC_NUM_EMITTERS; i++ )
  459. {
  460. if( !emitterList[i] && emitterIDList[i] != 0 )
  461. {
  462. if( Sim::findObject( emitterIDList[i], emitterList[i] ) == false)
  463. {
  464. Con::errorf( ConsoleLogEntry::General, "ExplosionData::onAdd: Invalid packet, bad datablockId(particle emitter): 0x%x", emitterIDList[i] );
  465. }
  466. }
  467. }
  468. for( S32 k=0; k<EC_MAX_SUB_EXPLOSIONS; k++ )
  469. {
  470. if( !explosionList[k] && explosionIDList[k] != 0 )
  471. {
  472. if( Sim::findObject( explosionIDList[k], explosionList[k] ) == false)
  473. {
  474. Con::errorf( ConsoleLogEntry::General, "ExplosionData::onAdd: Invalid packet, bad datablockId(explosion): 0x%x", explosionIDList[k] );
  475. }
  476. }
  477. }
  478. return true;
  479. }
  480. void ExplosionData::packData(BitStream* stream)
  481. {
  482. Parent::packData(stream);
  483. stream->writeString(dtsFileName);
  484. sfxWrite( stream, soundProfile );
  485. if (stream->writeFlag(particleEmitter))
  486. stream->writeRangedU32(particleEmitter->getId(),DataBlockObjectIdFirst,DataBlockObjectIdLast);
  487. stream->writeInt(particleDensity, 14);
  488. stream->write(particleRadius);
  489. stream->writeFlag(faceViewer);
  490. if(stream->writeFlag(explosionScale.x != 1 || explosionScale.y != 1 || explosionScale.z != 1))
  491. {
  492. stream->writeInt((S32)(explosionScale.x * 100), 16);
  493. stream->writeInt((S32)(explosionScale.y * 100), 16);
  494. stream->writeInt((S32)(explosionScale.z * 100), 16);
  495. }
  496. stream->writeInt((S32)(playSpeed * 20), 14);
  497. stream->writeRangedU32((U32)debrisThetaMin, 0, 180);
  498. stream->writeRangedU32((U32)debrisThetaMax, 0, 180);
  499. stream->writeRangedU32((U32)debrisPhiMin, 0, 360);
  500. stream->writeRangedU32((U32)debrisPhiMax, 0, 360);
  501. stream->writeRangedU32((U32)debrisNum, 0, 1000);
  502. stream->writeRangedU32(debrisNumVariance, 0, 1000);
  503. stream->writeInt((S32)(debrisVelocity * 10), 14);
  504. stream->writeRangedU32((U32)(debrisVelocityVariance * 10), 0, 10000);
  505. stream->writeInt(delayMS >> 5, 16);
  506. stream->writeInt(delayVariance >> 5, 16);
  507. stream->writeInt(lifetimeMS >> 5, 16);
  508. stream->writeInt(lifetimeVariance >> 5, 16);
  509. stream->write(offset);
  510. stream->writeFlag( shakeCamera );
  511. stream->write(camShakeFreq.x);
  512. stream->write(camShakeFreq.y);
  513. stream->write(camShakeFreq.z);
  514. stream->write(camShakeAmp.x);
  515. stream->write(camShakeAmp.y);
  516. stream->write(camShakeAmp.z);
  517. stream->write(camShakeDuration);
  518. stream->write(camShakeRadius);
  519. stream->write(camShakeFalloff);
  520. for( S32 j=0; j<EC_NUM_DEBRIS_TYPES; j++ )
  521. {
  522. if( stream->writeFlag( debrisList[j] ) )
  523. {
  524. stream->writeRangedU32( debrisList[j]->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  525. }
  526. }
  527. S32 i;
  528. for( i=0; i<EC_NUM_EMITTERS; i++ )
  529. {
  530. if( stream->writeFlag( emitterList[i] != NULL ) )
  531. {
  532. stream->writeRangedU32( emitterList[i]->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  533. }
  534. }
  535. for( i=0; i<EC_MAX_SUB_EXPLOSIONS; i++ )
  536. {
  537. if( stream->writeFlag( explosionList[i] != NULL ) )
  538. {
  539. stream->writeRangedU32( explosionList[i]->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  540. }
  541. }
  542. U32 count;
  543. for(count = 0; count < EC_NUM_TIME_KEYS; count++)
  544. if(times[i] >= 1)
  545. break;
  546. count++;
  547. if(count > EC_NUM_TIME_KEYS)
  548. count = EC_NUM_TIME_KEYS;
  549. stream->writeRangedU32(count, 0, EC_NUM_TIME_KEYS);
  550. for( i=0; i<count; i++ )
  551. stream->writeFloat( times[i], 8 );
  552. for( i=0; i<count; i++ )
  553. {
  554. stream->writeRangedU32((U32)(sizes[i].x * 100), 0, 16000);
  555. stream->writeRangedU32((U32)(sizes[i].y * 100), 0, 16000);
  556. stream->writeRangedU32((U32)(sizes[i].z * 100), 0, 16000);
  557. }
  558. // Dynamic light info
  559. stream->writeFloat(lightStartRadius/MaxLightRadius, 8);
  560. stream->writeFloat(lightEndRadius/MaxLightRadius, 8);
  561. stream->writeFloat(lightStartColor.red,7);
  562. stream->writeFloat(lightStartColor.green,7);
  563. stream->writeFloat(lightStartColor.blue,7);
  564. stream->writeFloat(lightEndColor.red,7);
  565. stream->writeFloat(lightEndColor.green,7);
  566. stream->writeFloat(lightEndColor.blue,7);
  567. stream->writeFloat(lightStartBrightness/MaxLightRadius, 8);
  568. stream->writeFloat(lightEndBrightness/MaxLightRadius, 8);
  569. stream->write(lightNormalOffset);
  570. }
  571. void ExplosionData::unpackData(BitStream* stream)
  572. {
  573. Parent::unpackData(stream);
  574. dtsFileName = stream->readSTString();
  575. sfxRead( stream, &soundProfile );
  576. if (stream->readFlag())
  577. particleEmitterId = stream->readRangedU32(DataBlockObjectIdFirst, DataBlockObjectIdLast);
  578. else
  579. particleEmitterId = 0;
  580. particleDensity = stream->readInt(14);
  581. stream->read(&particleRadius);
  582. faceViewer = stream->readFlag();
  583. if(stream->readFlag())
  584. {
  585. explosionScale.x = stream->readInt(16) / 100.0f;
  586. explosionScale.y = stream->readInt(16) / 100.0f;
  587. explosionScale.z = stream->readInt(16) / 100.0f;
  588. }
  589. else
  590. explosionScale.set(1,1,1);
  591. playSpeed = stream->readInt(14) / 20.0f;
  592. debrisThetaMin = stream->readRangedU32(0, 180);
  593. debrisThetaMax = stream->readRangedU32(0, 180);
  594. debrisPhiMin = stream->readRangedU32(0, 360);
  595. debrisPhiMax = stream->readRangedU32(0, 360);
  596. debrisNum = stream->readRangedU32(0, 1000);
  597. debrisNumVariance = stream->readRangedU32(0, 1000);
  598. debrisVelocity = stream->readInt(14) / 10.0f;
  599. debrisVelocityVariance = stream->readRangedU32(0, 10000) / 10.0f;
  600. delayMS = stream->readInt(16) << 5;
  601. delayVariance = stream->readInt(16) << 5;
  602. lifetimeMS = stream->readInt(16) << 5;
  603. lifetimeVariance = stream->readInt(16) << 5;
  604. stream->read(&offset);
  605. shakeCamera = stream->readFlag();
  606. stream->read(&camShakeFreq.x);
  607. stream->read(&camShakeFreq.y);
  608. stream->read(&camShakeFreq.z);
  609. stream->read(&camShakeAmp.x);
  610. stream->read(&camShakeAmp.y);
  611. stream->read(&camShakeAmp.z);
  612. stream->read(&camShakeDuration);
  613. stream->read(&camShakeRadius);
  614. stream->read(&camShakeFalloff);
  615. for( S32 j=0; j<EC_NUM_DEBRIS_TYPES; j++ )
  616. {
  617. if( stream->readFlag() )
  618. {
  619. debrisIDList[j] = (S32) stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  620. }
  621. }
  622. U32 i;
  623. for( i=0; i<EC_NUM_EMITTERS; i++ )
  624. {
  625. if( stream->readFlag() )
  626. {
  627. emitterIDList[i] = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  628. }
  629. }
  630. for( S32 k=0; k<EC_MAX_SUB_EXPLOSIONS; k++ )
  631. {
  632. if( stream->readFlag() )
  633. {
  634. explosionIDList[k] = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  635. }
  636. }
  637. U32 count = stream->readRangedU32(0, EC_NUM_TIME_KEYS);
  638. for( i=0; i<count; i++ )
  639. times[i] = stream->readFloat(8);
  640. for( i=0; i<count; i++ )
  641. {
  642. sizes[i].x = stream->readRangedU32(0, 16000) / 100.0f;
  643. sizes[i].y = stream->readRangedU32(0, 16000) / 100.0f;
  644. sizes[i].z = stream->readRangedU32(0, 16000) / 100.0f;
  645. }
  646. //
  647. lightStartRadius = stream->readFloat(8) * MaxLightRadius;
  648. lightEndRadius = stream->readFloat(8) * MaxLightRadius;
  649. lightStartColor.red = stream->readFloat(7);
  650. lightStartColor.green = stream->readFloat(7);
  651. lightStartColor.blue = stream->readFloat(7);
  652. lightEndColor.red = stream->readFloat(7);
  653. lightEndColor.green = stream->readFloat(7);
  654. lightEndColor.blue = stream->readFloat(7);
  655. lightStartBrightness = stream->readFloat(8) * MaxLightRadius;
  656. lightEndBrightness = stream->readFloat(8) * MaxLightRadius;
  657. stream->read( &lightNormalOffset );
  658. }
  659. bool ExplosionData::preload(bool server, String &errorStr)
  660. {
  661. if (Parent::preload(server, errorStr) == false)
  662. return false;
  663. if( !server )
  664. {
  665. String errorStr;
  666. if( !sfxResolve( &soundProfile, errorStr ) )
  667. Con::errorf(ConsoleLogEntry::General, "Error, unable to load sound profile for explosion datablock: %s", errorStr.c_str());
  668. if (!particleEmitter && particleEmitterId != 0)
  669. if (Sim::findObject(particleEmitterId, particleEmitter) == false)
  670. Con::errorf(ConsoleLogEntry::General, "Error, unable to load particle emitter for explosion datablock");
  671. }
  672. if (dtsFileName && dtsFileName[0]) {
  673. explosionShape = ResourceManager::get().load(dtsFileName);
  674. if (!bool(explosionShape)) {
  675. errorStr = String::ToString("ExplosionData: Couldn't load shape \"%s\"", dtsFileName);
  676. return false;
  677. }
  678. // Resolve animations
  679. explosionAnimation = explosionShape->findSequence("ambient");
  680. // Preload textures with a dummy instance...
  681. TSShapeInstance* pDummy = new TSShapeInstance(explosionShape, !server);
  682. delete pDummy;
  683. } else {
  684. explosionShape = NULL;
  685. explosionAnimation = -1;
  686. }
  687. return true;
  688. }
  689. //--------------------------------------------------------------------------
  690. //--------------------------------------
  691. //
  692. Explosion::Explosion()
  693. {
  694. mTypeMask |= ExplosionObjectType | LightObjectType;
  695. mExplosionInstance = NULL;
  696. mExplosionThread = NULL;
  697. dMemset( mEmitterList, 0, sizeof( mEmitterList ) );
  698. mMainEmitter = NULL;
  699. mFade = 1;
  700. mDelayMS = 0;
  701. mCurrMS = 0;
  702. mEndingMS = 1000;
  703. mActive = false;
  704. mCollideType = 0;
  705. mInitialNormal.set( 0.0f, 0.0f, 1.0f );
  706. mRandAngle = sgRandom.randF( 0.0f, 1.0f ) * M_PI_F * 2.0f;
  707. mLight = LIGHTMGR->createLightInfo();
  708. mNetFlags.set( IsGhost );
  709. }
  710. Explosion::~Explosion()
  711. {
  712. if( mExplosionInstance )
  713. {
  714. delete mExplosionInstance;
  715. mExplosionInstance = NULL;
  716. mExplosionThread = NULL;
  717. }
  718. SAFE_DELETE(mLight);
  719. }
  720. void Explosion::setInitialState(const Point3F& point, const Point3F& normal, const F32 fade)
  721. {
  722. setPosition(point);
  723. mInitialNormal = normal;
  724. mFade = fade;
  725. }
  726. //--------------------------------------------------------------------------
  727. void Explosion::initPersistFields()
  728. {
  729. Parent::initPersistFields();
  730. //
  731. }
  732. //--------------------------------------------------------------------------
  733. bool Explosion::onAdd()
  734. {
  735. // first check if we have a server connection, if we dont then this is on the server
  736. // and we should exit, then check if the parent fails to add the object
  737. GameConnection *conn = GameConnection::getConnectionToServer();
  738. if ( !conn || !Parent::onAdd() )
  739. return false;
  740. mDelayMS = mDataBlock->delayMS + sgRandom.randI( -mDataBlock->delayVariance, mDataBlock->delayVariance );
  741. mEndingMS = mDataBlock->lifetimeMS + sgRandom.randI( -mDataBlock->lifetimeVariance, mDataBlock->lifetimeVariance );
  742. if( mFabs( mDataBlock->offset ) > 0.001f )
  743. {
  744. MatrixF axisOrient = MathUtils::createOrientFromDir( mInitialNormal );
  745. MatrixF trans = getTransform();
  746. Point3F randVec;
  747. randVec.x = sgRandom.randF( -1.0f, 1.0f );
  748. randVec.y = sgRandom.randF( 0.0f, 1.0f );
  749. randVec.z = sgRandom.randF( -1.0f, 1.0f );
  750. randVec.normalize();
  751. randVec *= mDataBlock->offset;
  752. axisOrient.mulV( randVec );
  753. trans.setPosition( trans.getPosition() + randVec );
  754. setTransform( trans );
  755. }
  756. // shake camera
  757. if( mDataBlock->shakeCamera )
  758. {
  759. // first check if explosion is near player
  760. GameConnection* connection = GameConnection::getConnectionToServer();
  761. ShapeBase *obj = dynamic_cast<ShapeBase*>(connection->getControlObject());
  762. bool applyShake = true;
  763. if( obj )
  764. {
  765. ShapeBase* cObj = obj;
  766. while((cObj = cObj->getControlObject()) != 0)
  767. {
  768. if(cObj->useObjsEyePoint())
  769. {
  770. applyShake = false;
  771. break;
  772. }
  773. }
  774. }
  775. if( applyShake && obj )
  776. {
  777. VectorF diff = obj->getPosition() - getPosition();
  778. F32 dist = diff.len();
  779. if( dist < mDataBlock->camShakeRadius )
  780. {
  781. CameraShake *camShake = new CameraShake;
  782. camShake->setDuration( mDataBlock->camShakeDuration );
  783. camShake->setFrequency( mDataBlock->camShakeFreq );
  784. F32 falloff = dist / mDataBlock->camShakeRadius;
  785. falloff = 1.0f + falloff * 10.0f;
  786. falloff = 1.0f / (falloff * falloff);
  787. VectorF shakeAmp = mDataBlock->camShakeAmp * falloff;
  788. camShake->setAmplitude( shakeAmp );
  789. camShake->setFalloff( mDataBlock->camShakeFalloff );
  790. camShake->init();
  791. gCamFXMgr.addFX( camShake );
  792. }
  793. }
  794. }
  795. if( mDelayMS == 0 )
  796. {
  797. if( !explode() )
  798. {
  799. return false;
  800. }
  801. }
  802. gClientSceneGraph->addObjectToScene(this);
  803. removeFromProcessList();
  804. ClientProcessList::get()->addObject(this);
  805. mRandomVal = sgRandom.randF();
  806. NetConnection* pNC = NetConnection::getConnectionToServer();
  807. AssertFatal(pNC != NULL, "Error, must have a connection to the server!");
  808. pNC->addObject(this);
  809. // Initialize the light structure and register as a dynamic light
  810. if (mDataBlock->lightStartRadius != 0.0f || mDataBlock->lightEndRadius)
  811. {
  812. mLight->setType( LightInfo::Point );
  813. mLight->setRange( mDataBlock->lightStartRadius );
  814. mLight->setColor( mDataBlock->lightStartColor );
  815. }
  816. return true;
  817. }
  818. void Explosion::onRemove()
  819. {
  820. for( int i=0; i<ExplosionData::EC_NUM_EMITTERS; i++ )
  821. {
  822. if( mEmitterList[i] )
  823. {
  824. mEmitterList[i]->deleteWhenEmpty();
  825. mEmitterList[i] = NULL;
  826. }
  827. }
  828. if( mMainEmitter )
  829. {
  830. mMainEmitter->deleteWhenEmpty();
  831. mMainEmitter = NULL;
  832. }
  833. if (getSceneManager() != NULL)
  834. getSceneManager()->removeObjectFromScene(this);
  835. if (getContainer() != NULL)
  836. getContainer()->removeObject(this);
  837. Parent::onRemove();
  838. }
  839. bool Explosion::onNewDataBlock( GameBaseData *dptr, bool reload )
  840. {
  841. mDataBlock = dynamic_cast<ExplosionData*>( dptr );
  842. if (!mDataBlock || !Parent::onNewDataBlock( dptr, reload ))
  843. return false;
  844. scriptOnNewDataBlock();
  845. return true;
  846. }
  847. //--------------------------------------------------------------------------
  848. void Explosion::prepRenderImage( SceneRenderState* state )
  849. {
  850. prepBatchRender( state );
  851. }
  852. void Explosion::setCurrentScale()
  853. {
  854. F32 t = F32(mCurrMS) / F32(mEndingMS);
  855. for( U32 i = 1; i < ExplosionData::EC_NUM_TIME_KEYS; i++ )
  856. {
  857. if( mDataBlock->times[i] >= t )
  858. {
  859. F32 firstPart = t - mDataBlock->times[i-1];
  860. F32 total = mDataBlock->times[i] -
  861. mDataBlock->times[i-1];
  862. firstPart /= total;
  863. mObjScale = (mDataBlock->sizes[i-1] * (1.0f - firstPart)) +
  864. (mDataBlock->sizes[i] * firstPart);
  865. return;
  866. }
  867. }
  868. }
  869. //--------------------------------------------------------------------------
  870. // Make the explosion face the viewer (if desired)
  871. //--------------------------------------------------------------------------
  872. void Explosion::prepModelView(SceneRenderState* state)
  873. {
  874. MatrixF rotMatrix( true );
  875. Point3F targetVector;
  876. if( mDataBlock->faceViewer )
  877. {
  878. targetVector = getPosition() - state->getCameraPosition();
  879. targetVector.normalize();
  880. // rotate explosion each time so it's a little different
  881. rotMatrix.set( EulerF( 0.0f, mRandAngle, 0.0f ) );
  882. }
  883. else
  884. {
  885. targetVector = mInitialNormal;
  886. }
  887. MatrixF explOrient = MathUtils::createOrientFromDir( targetVector );
  888. explOrient.mul( rotMatrix );
  889. explOrient.setPosition( getPosition() );
  890. setCurrentScale();
  891. explOrient.scale( mObjScale );
  892. GFX->setWorldMatrix( explOrient );
  893. }
  894. //--------------------------------------------------------------------------
  895. // Render object
  896. //--------------------------------------------------------------------------
  897. void Explosion::prepBatchRender(SceneRenderState* state)
  898. {
  899. if ( !mExplosionInstance )
  900. return;
  901. MatrixF proj = GFX->getProjectionMatrix();
  902. RectI viewport = GFX->getViewport();
  903. // Set up our TS render state here.
  904. TSRenderState rdata;
  905. rdata.setSceneState( state );
  906. // We might have some forward lit materials
  907. // so pass down a query to gather lights.
  908. LightQuery query;
  909. query.init( getWorldSphere() );
  910. rdata.setLightQuery( &query );
  911. // render mesh
  912. GFX->pushWorldMatrix();
  913. prepModelView( state );
  914. mExplosionInstance->animate();
  915. mExplosionInstance->render( rdata );
  916. GFX->popWorldMatrix();
  917. GFX->setProjectionMatrix( proj );
  918. GFX->setViewport( viewport );
  919. }
  920. void Explosion::submitLights( LightManager *lm, bool staticLighting )
  921. {
  922. if ( staticLighting )
  923. return;
  924. // Update the light's info and add it to the scene, the light will
  925. // only be visible for this current frame.
  926. mLight->setPosition( getRenderTransform().getPosition() + mInitialNormal * mDataBlock->lightNormalOffset );
  927. F32 t = F32(mCurrMS) / F32(mEndingMS);
  928. mLight->setRange( mDataBlock->lightStartRadius +
  929. (mDataBlock->lightEndRadius - mDataBlock->lightStartRadius) * t );
  930. mLight->setColor( mDataBlock->lightStartColor +
  931. (mDataBlock->lightEndColor - mDataBlock->lightStartColor) * t );
  932. mLight->setBrightness( mDataBlock->lightStartBrightness +
  933. (mDataBlock->lightEndBrightness - mDataBlock->lightStartBrightness) * t );
  934. lm->registerGlobalLight( mLight, this );
  935. }
  936. //--------------------------------------------------------------------------
  937. void Explosion::processTick(const Move*)
  938. {
  939. mCurrMS += TickMs;
  940. if( mCurrMS >= mEndingMS )
  941. {
  942. deleteObject();
  943. return;
  944. }
  945. if( (mCurrMS > mDelayMS) && !mActive )
  946. explode();
  947. }
  948. void Explosion::advanceTime(F32 dt)
  949. {
  950. if (dt == 0.0f)
  951. return;
  952. GameConnection* conn = GameConnection::getConnectionToServer();
  953. if(!conn)
  954. return;
  955. updateEmitters( dt );
  956. if( mExplosionInstance )
  957. mExplosionInstance->advanceTime(dt, mExplosionThread);
  958. }
  959. //----------------------------------------------------------------------------
  960. // Update emitters
  961. //----------------------------------------------------------------------------
  962. void Explosion::updateEmitters( F32 dt )
  963. {
  964. Point3F pos = getPosition();
  965. for( int i=0; i<ExplosionData::EC_NUM_EMITTERS; i++ )
  966. {
  967. if( mEmitterList[i] )
  968. {
  969. mEmitterList[i]->emitParticles( pos, pos, mInitialNormal, Point3F( 0.0f, 0.0f, 0.0f ), (U32)(dt * 1000));
  970. }
  971. }
  972. }
  973. //----------------------------------------------------------------------------
  974. // Launch Debris
  975. //----------------------------------------------------------------------------
  976. void Explosion::launchDebris( Point3F &axis )
  977. {
  978. GameConnection* conn = GameConnection::getConnectionToServer();
  979. if(!conn)
  980. return;
  981. bool hasDebris = false;
  982. for( int j=0; j<ExplosionData::EC_NUM_DEBRIS_TYPES; j++ )
  983. {
  984. if( mDataBlock->debrisList[j] )
  985. {
  986. hasDebris = true;
  987. break;
  988. }
  989. }
  990. if( !hasDebris )
  991. {
  992. return;
  993. }
  994. Point3F axisx;
  995. if (mFabs(axis.z) < 0.999f)
  996. mCross(axis, Point3F(0.0f, 0.0f, 1.0f), &axisx);
  997. else
  998. mCross(axis, Point3F(0.0f, 1.0f, 0.0f), &axisx);
  999. axisx.normalize();
  1000. Point3F pos( 0.0f, 0.0f, 0.5f );
  1001. pos += getPosition();
  1002. U32 numDebris = mDataBlock->debrisNum + sgRandom.randI( -mDataBlock->debrisNumVariance, mDataBlock->debrisNumVariance );
  1003. for( int i=0; i<numDebris; i++ )
  1004. {
  1005. Point3F launchDir = MathUtils::randomDir( axis, mDataBlock->debrisThetaMin, mDataBlock->debrisThetaMax,
  1006. mDataBlock->debrisPhiMin, mDataBlock->debrisPhiMax );
  1007. F32 debrisVel = mDataBlock->debrisVelocity + mDataBlock->debrisVelocityVariance * sgRandom.randF( -1.0f, 1.0f );
  1008. launchDir *= debrisVel;
  1009. Debris *debris = new Debris;
  1010. debris->setDataBlock( mDataBlock->debrisList[0] );
  1011. debris->setTransform( getTransform() );
  1012. debris->init( pos, launchDir );
  1013. if( !debris->registerObject() )
  1014. {
  1015. Con::warnf( ConsoleLogEntry::General, "Could not register debris for class: %s", mDataBlock->getName() );
  1016. delete debris;
  1017. debris = NULL;
  1018. }
  1019. }
  1020. }
  1021. //----------------------------------------------------------------------------
  1022. // Spawn sub explosions
  1023. //----------------------------------------------------------------------------
  1024. void Explosion::spawnSubExplosions()
  1025. {
  1026. GameConnection* conn = GameConnection::getConnectionToServer();
  1027. if(!conn)
  1028. return;
  1029. for( S32 i=0; i<ExplosionData::EC_MAX_SUB_EXPLOSIONS; i++ )
  1030. {
  1031. if( mDataBlock->explosionList[i] )
  1032. {
  1033. MatrixF trans = getTransform();
  1034. Explosion* pExplosion = new Explosion;
  1035. pExplosion->setDataBlock( mDataBlock->explosionList[i] );
  1036. pExplosion->setTransform( trans );
  1037. pExplosion->setInitialState( trans.getPosition(), mInitialNormal, 1);
  1038. if (!pExplosion->registerObject())
  1039. delete pExplosion;
  1040. }
  1041. }
  1042. }
  1043. //----------------------------------------------------------------------------
  1044. // Explode
  1045. //----------------------------------------------------------------------------
  1046. bool Explosion::explode()
  1047. {
  1048. mActive = true;
  1049. GameConnection* conn = GameConnection::getConnectionToServer();
  1050. if(!conn)
  1051. return false;
  1052. launchDebris( mInitialNormal );
  1053. spawnSubExplosions();
  1054. if (bool(mDataBlock->explosionShape) && mDataBlock->explosionAnimation != -1) {
  1055. mExplosionInstance = new TSShapeInstance(mDataBlock->explosionShape, true);
  1056. mExplosionThread = mExplosionInstance->addThread();
  1057. mExplosionInstance->setSequence(mExplosionThread, mDataBlock->explosionAnimation, 0);
  1058. mExplosionInstance->setTimeScale(mExplosionThread, mDataBlock->playSpeed);
  1059. mCurrMS = 0;
  1060. mEndingMS = U32(mExplosionInstance->getScaledDuration(mExplosionThread) * 1000.0f);
  1061. mObjScale.convolve(mDataBlock->explosionScale);
  1062. mObjBox = mDataBlock->explosionShape->bounds;
  1063. resetWorldBox();
  1064. }
  1065. if (mDataBlock->soundProfile)
  1066. SFX->playOnce( mDataBlock->soundProfile, &getTransform() );
  1067. if (mDataBlock->particleEmitter) {
  1068. mMainEmitter = new ParticleEmitter;
  1069. mMainEmitter->setDataBlock(mDataBlock->particleEmitter);
  1070. mMainEmitter->registerObject();
  1071. mMainEmitter->emitParticles(getPosition(), mInitialNormal, mDataBlock->particleRadius,
  1072. Point3F::Zero, U32(mDataBlock->particleDensity * mFade));
  1073. }
  1074. for( int i=0; i<ExplosionData::EC_NUM_EMITTERS; i++ )
  1075. {
  1076. if( mDataBlock->emitterList[i] != NULL )
  1077. {
  1078. ParticleEmitter * pEmitter = new ParticleEmitter;
  1079. pEmitter->setDataBlock( mDataBlock->emitterList[i] );
  1080. if( !pEmitter->registerObject() )
  1081. {
  1082. Con::warnf( ConsoleLogEntry::General, "Could not register emitter for particle of class: %s", mDataBlock->getName() );
  1083. SAFE_DELETE(pEmitter);
  1084. }
  1085. mEmitterList[i] = pEmitter;
  1086. }
  1087. }
  1088. return true;
  1089. }