explosion.cpp 44 KB

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