explosion.cpp 44 KB

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