explosion.cpp 44 KB

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