explosion.cpp 44 KB

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