ParticleSys.cpp 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // ParticleSys.cpp ////////////////////////////////////////////////////////////////////////////////
  24. // Particle System implementation
  25. // Author: Michael S. Booth, November 2001
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #define DEFINE_PARTICLE_SYSTEM_NAMES
  29. #include "Common/GameState.h"
  30. #include "Common/INI.h"
  31. #include "Common/PerfTimer.h"
  32. #include "Common/ThingFactory.h"
  33. #include "Common/GameLOD.h"
  34. #include "Common/Xfer.h"
  35. #include "GameClient/Drawable.h"
  36. #include "GameClient/DebugDisplay.h"
  37. #include "GameClient/Display.h"
  38. #include "GameClient/GameClient.h"
  39. #include "GameClient/InGameUI.h"
  40. #include "GameClient/ParticleSys.h"
  41. #include "GameLogic/GameLogic.h"
  42. #include "GameLogic/Object.h"
  43. #include "GameLogic/TerrainLogic.h"
  44. #ifdef _INTERNAL
  45. // for occasional debugging...
  46. //#pragma optimize("", off)
  47. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  48. #endif
  49. //------------------------------------------------------------------------------ Performance Timers
  50. //#include "Common/PerfMetrics.h"
  51. //#include "Common/PerfTimer.h"
  52. //static PerfTimer s_particleSys("ParticleSys::update", false, PERFMETRICS_LOGIC_STARTFRAME, PERFMETRICS_LOGIC_STOPFRAME);
  53. //-------------------------------------------------------------------------------------------------
  54. // the singleton
  55. ParticleSystemManager *TheParticleSystemManager = NULL;
  56. ///////////////////////////////////////////////////////////////////////////////////////////////////
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////
  58. ///////////////////////////////////////////////////////////////////////////////////////////////////
  59. // ------------------------------------------------------------------------------------------------
  60. // ------------------------------------------------------------------------------------------------
  61. ParticleInfo::ParticleInfo( void )
  62. {
  63. //Added By Sadullah Nader
  64. //Initializations inserted
  65. m_angleX = m_angleY = m_angleZ = 0.0f;
  66. m_angularDamping = 0.0f;
  67. m_angularRateX = m_angularRateY = m_angularRateZ = 0.0f;
  68. m_colorScale =0.0f;
  69. m_size = 0.0f;
  70. m_sizeRate = 0.0f;
  71. m_sizeRateDamping = 0.0f;
  72. m_velDamping = 0.0f;
  73. m_windRandomness = 0.0f;
  74. m_emitterPos.zero();
  75. m_pos.zero();
  76. m_vel.zero();
  77. m_lifetime = 0;
  78. m_particleUpTowardsEmitter = FALSE;
  79. //
  80. } // end ParticleInfo
  81. // ------------------------------------------------------------------------------------------------
  82. /** CRC */
  83. // ------------------------------------------------------------------------------------------------
  84. void ParticleInfo::crc( Xfer *xfer )
  85. {
  86. } // end crc
  87. // ------------------------------------------------------------------------------------------------
  88. /** Xfer method
  89. * Version Info:
  90. * 1: Initial version */
  91. // ------------------------------------------------------------------------------------------------
  92. void ParticleInfo::xfer( Xfer *xfer )
  93. {
  94. Int i;
  95. // version
  96. XferVersion currentVersion = 1;
  97. XferVersion version = currentVersion;
  98. xfer->xferVersion( &version, currentVersion );
  99. // velocity
  100. xfer->xferCoord3D( &m_vel );
  101. // position
  102. xfer->xferCoord3D( &m_pos );
  103. // emitter position
  104. xfer->xferCoord3D( &m_emitterPos );
  105. // velocity damping
  106. xfer->xferReal( &m_velDamping );
  107. // angle
  108. xfer->xferReal( &m_angleX );
  109. xfer->xferReal( &m_angleY );
  110. xfer->xferReal( &m_angleZ );
  111. // angular rate
  112. xfer->xferReal( &m_angularRateX );
  113. xfer->xferReal( &m_angularRateY );
  114. xfer->xferReal( &m_angularRateZ );
  115. // lifetime
  116. xfer->xferUnsignedInt( &m_lifetime );
  117. // size
  118. xfer->xferReal( &m_size );
  119. // size rate
  120. xfer->xferReal( &m_sizeRate );
  121. // size rate damping
  122. xfer->xferReal( &m_sizeRateDamping );
  123. // alpha keys
  124. for( i = 0; i < MAX_KEYFRAMES; ++i )
  125. {
  126. xfer->xferReal( &m_alphaKey[ i ].value );
  127. xfer->xferUnsignedInt( &m_alphaKey[ i ].frame );
  128. } // end for, i
  129. // color keys
  130. for( i = 0; i < MAX_KEYFRAMES; ++i )
  131. {
  132. xfer->xferRGBColor( &m_colorKey[ i ].color );
  133. xfer->xferUnsignedInt( &m_colorKey[ i ].frame );
  134. } // end for, i
  135. // color scale
  136. xfer->xferReal( &m_colorScale );
  137. // particle up towards emitter
  138. xfer->xferBool( &m_particleUpTowardsEmitter );
  139. // wind randomness
  140. xfer->xferReal( &m_windRandomness );
  141. } // end xfer
  142. // ------------------------------------------------------------------------------------------------
  143. /** Load post process */
  144. // ------------------------------------------------------------------------------------------------
  145. void ParticleInfo::loadPostProcess( void )
  146. {
  147. } // end loadPostProcess
  148. /** Load post process */
  149. // ------------------------------------------------------------------------------------------------
  150. ///////////////////////////////////////////////////////////////////////////////////////////////////
  151. ///////////////////////////////////////////////////////////////////////////////////////////////////
  152. ///////////////////////////////////////////////////////////////////////////////////////////////////
  153. // ------------------------------------------------------------------------------------------------
  154. enum
  155. {
  156. MAX_SIZE_BONUS = 50
  157. };
  158. //todo move this somewhere more useful.
  159. // ------------------------------------------------------------------------------------------------
  160. #if 0
  161. static Real angleBetween(const Coord2D &vecA, const Coord2D &vecB)
  162. {
  163. Real lengthA = vecA->length();
  164. if (lengthA < FLT_EPSILON) {
  165. return 0.0f;
  166. }
  167. Real lengthB = vecB->length();
  168. if (lengthB < FLT_EPSILON) {
  169. return 0.0f;
  170. }
  171. Real dotProduct = (vecA->x * vecB->x + vecA->y * vecB->y);
  172. // If the dotproduct is 0.0, then they are orthogonal
  173. if (dotProduct < FLT_EPSILON && dotProduct > -FLT_EPSILON) {
  174. return vecB->x > 0.0f ? PI : 0.0f;
  175. }
  176. Real cosTheta = dotProduct / (lengthA * lengthB);
  177. Real theta = ACos(cosTheta);
  178. return vecB->x > 0.0f ? theta : -theta;
  179. }
  180. #endif
  181. ///////////////////////////////////////////////////////////////////////////////////////////////////
  182. // Particle ///////////////////////////////////////////////////////////////////////////////////////
  183. ///////////////////////////////////////////////////////////////////////////////////////////////////
  184. // ------------------------------------------------------------------------------------------------
  185. /** Compute alpha rate to get to next key on given frame */
  186. // ------------------------------------------------------------------------------------------------
  187. void Particle::computeAlphaRate( void )
  188. {
  189. if (m_alphaKey[ m_alphaTargetKey ].frame == 0)
  190. {
  191. m_alphaRate = 0.0f;
  192. return;
  193. }
  194. Real delta = m_alphaKey[ m_alphaTargetKey ].value - m_alphaKey[ m_alphaTargetKey-1 ].value;
  195. UnsignedInt time = m_alphaKey[ m_alphaTargetKey ].frame - m_alphaKey[ m_alphaTargetKey-1 ].frame;
  196. m_alphaRate = delta/time;
  197. }
  198. // ------------------------------------------------------------------------------------------------
  199. /** Compute color rate to get to next key on given frame */
  200. // ------------------------------------------------------------------------------------------------
  201. void Particle::computeColorRate( void )
  202. {
  203. if (m_colorKey[ m_colorTargetKey ].frame == 0)
  204. {
  205. m_colorRate.red = 0.0f;
  206. m_colorRate.green = 0.0f;
  207. m_colorRate.blue = 0.0f;
  208. return;
  209. }
  210. UnsignedInt time = m_colorKey[ m_colorTargetKey ].frame - m_colorKey[ m_colorTargetKey-1 ].frame;
  211. Real delta = m_colorKey[ m_colorTargetKey ].color.red - m_colorKey[ m_colorTargetKey-1 ].color.red;
  212. m_colorRate.red = delta/time;
  213. delta = m_colorKey[ m_colorTargetKey ].color.green - m_colorKey[ m_colorTargetKey-1 ].color.green;
  214. m_colorRate.green = delta/time;
  215. delta = m_colorKey[ m_colorTargetKey ].color.blue - m_colorKey[ m_colorTargetKey-1 ].color.blue;
  216. m_colorRate.blue = delta/time;
  217. }
  218. // ------------------------------------------------------------------------------------------------
  219. /** Construct a particle from a particle template */
  220. // ------------------------------------------------------------------------------------------------
  221. Particle::Particle( ParticleSystem *system, const ParticleInfo *info )
  222. {
  223. m_system = system;
  224. m_isCulled = FALSE;
  225. m_accel.x = 0.0f;
  226. m_accel.y = 0.0f;
  227. m_accel.z = 0.0f;
  228. m_vel = info->m_vel;
  229. m_pos = info->m_pos;
  230. m_angleX = info->m_angleX;
  231. m_angleY = info->m_angleY;
  232. m_angleZ = info->m_angleZ;
  233. //Added By Sadullah Nader
  234. //Initializations inserted
  235. m_lastPos.zero();
  236. //
  237. m_windRandomness = info->m_windRandomness;
  238. m_particleUpTowardsEmitter = info->m_particleUpTowardsEmitter;
  239. m_emitterPos = info->m_emitterPos;
  240. m_angularRateX = info->m_angularRateX;
  241. m_angularRateY = info->m_angularRateY;
  242. m_angularRateZ = info->m_angularRateZ;
  243. m_angularDamping = info->m_angularDamping;
  244. m_velDamping = info->m_velDamping;
  245. m_lifetime = info->m_lifetime;
  246. m_lifetimeLeft = info->m_lifetime;
  247. m_createTimestamp = TheGameClient->getFrame();
  248. m_personality = 0;
  249. m_size = info->m_size;
  250. m_sizeRate = info->m_sizeRate;
  251. m_sizeRateDamping = info->m_sizeRateDamping;
  252. // set up alpha
  253. for( int i=0; i<MAX_KEYFRAMES; i++ )
  254. m_alphaKey[i] = info->m_alphaKey[i];
  255. m_alpha = m_alphaKey[0].value;
  256. m_alphaTargetKey = 1;
  257. computeAlphaRate();
  258. // set up colors
  259. for( i=0; i<MAX_KEYFRAMES; i++ )
  260. m_colorKey[i] = info->m_colorKey[i];
  261. m_color = m_colorKey[0].color;
  262. m_colorTargetKey = 1;
  263. computeColorRate();
  264. m_colorScale = info->m_colorScale;
  265. if (system->isUsingDrawables())
  266. {
  267. const ThingTemplate *tmpl = TheThingFactory->findTemplate(system->getParticleTypeName());
  268. DEBUG_ASSERTCRASH(tmpl, ("Drawable %s not found\n",system->getParticleTypeName().str()));
  269. if (tmpl)
  270. {
  271. m_drawable = TheThingFactory->newDrawable(tmpl);
  272. if (m_drawable)
  273. m_drawable->attachToParticleSystem( this );
  274. }
  275. }
  276. m_inSystemList = m_inOverallList = FALSE;
  277. m_systemPrev = m_systemNext = m_overallPrev = m_overallNext = NULL;
  278. // add this particle to the global list, retaining particle creation order
  279. TheParticleSystemManager->addParticle(this, system->getPriority() );
  280. // add this particle to the Particle System list, retaining local creation order
  281. m_system->addParticle(this);
  282. //DEBUG_ASSERTLOG(!(totalParticleCount % 100 == 0), ( "TotalParticleCount = %d\n", m_totalParticleCount ));
  283. }
  284. // ------------------------------------------------------------------------------------------------
  285. /** Destructor */
  286. // ------------------------------------------------------------------------------------------------
  287. Particle::~Particle()
  288. {
  289. // tell the particle system that this particle is gone
  290. m_system->removeParticle( this );
  291. if (m_drawable)
  292. TheGameClient->destroyDrawable( m_drawable );
  293. m_drawable = NULL;
  294. // if this particle was controlling another particle system, destroy that system
  295. if (m_systemUnderControl)
  296. {
  297. m_systemUnderControl->detachControlParticle( this );
  298. m_systemUnderControl->destroy();
  299. }
  300. m_systemUnderControl = NULL;
  301. // remove from the global list
  302. TheParticleSystemManager->removeParticle(this);
  303. //DEBUG_ASSERTLOG(!(totalParticleCount % 100 == 0), ( "TotalParticleCount = %d\n", m_totalParticleCount ));
  304. }
  305. // ------------------------------------------------------------------------------------------------
  306. /** Add the given acceleration */
  307. // ------------------------------------------------------------------------------------------------
  308. void Particle::applyForce( const Coord3D *force )
  309. {
  310. m_accel.x += force->x;
  311. m_accel.y += force->y;
  312. m_accel.z += force->z;
  313. }
  314. // ------------------------------------------------------------------------------------------------
  315. /** Update the behavior of an individual particle */
  316. // ------------------------------------------------------------------------------------------------
  317. Bool Particle::update( void )
  318. {
  319. // integrate acceleration into velocity
  320. m_vel.x += m_accel.x;
  321. m_vel.y += m_accel.y;
  322. m_vel.z += m_accel.z;
  323. m_vel.x *= m_velDamping;
  324. m_vel.y *= m_velDamping;
  325. m_vel.z *= m_velDamping;
  326. // integrate velocity into position
  327. const Coord3D *driftVel = m_system->getDriftVelocity();
  328. m_pos.x += m_vel.x + driftVel->x;
  329. m_pos.y += m_vel.y + driftVel->y;
  330. m_pos.z += m_vel.z + driftVel->z;
  331. // integrate the wind (if specified) into position
  332. doWindMotion();
  333. // update orientation
  334. m_angleX += m_angularRateX;
  335. m_angleY += m_angularRateY;
  336. m_angleZ += m_angularRateZ;
  337. m_angularRateX *= m_angularDamping;
  338. m_angularRateY *= m_angularDamping;
  339. m_angularRateZ *= m_angularDamping;
  340. if (m_particleUpTowardsEmitter) {
  341. // adjust the up position back towards the particle
  342. Coord2D emitterDir;
  343. emitterDir.x = m_pos.x - m_emitterPos.x;
  344. emitterDir.y = m_pos.y - m_emitterPos.y;
  345. #if 0
  346. static const Coord2D upVec = { 0.0f, 1.0f };
  347. m_angleZ = angleBetween(upVec, emitterDir) + PI;
  348. #else
  349. if (emitterDir.y < FLT_EPSILON && emitterDir.y > -FLT_EPSILON) {
  350. m_angleZ = emitterDir.x > 0.0f ? PI + PI : PI;
  351. } else {
  352. Real emitterDirLength = emitterDir.length();
  353. if (emitterDirLength < FLT_EPSILON) {
  354. m_angleZ = PI;
  355. } else {
  356. Real theta = ACos(emitterDir.y/emitterDirLength);
  357. m_angleZ = emitterDir.x > 0.0f ? PI + theta : PI - theta;
  358. }
  359. }
  360. #endif
  361. }
  362. // update size
  363. m_size += m_sizeRate;
  364. m_sizeRate *= m_sizeRateDamping;
  365. //
  366. // Update alpha
  367. //
  368. m_alpha += m_alphaRate;
  369. if (m_alphaTargetKey < MAX_KEYFRAMES && m_alphaKey[ m_alphaTargetKey ].frame)
  370. {
  371. if (TheGameClient->getFrame() - m_createTimestamp >= m_alphaKey[ m_alphaTargetKey ].frame)
  372. {
  373. m_alpha = m_alphaKey[ m_alphaTargetKey ].value;
  374. m_alphaTargetKey++;
  375. computeAlphaRate();
  376. }
  377. }
  378. else
  379. m_alphaRate = 0.0f;
  380. if (m_alpha < 0.0f)
  381. m_alpha = 0.0f;
  382. else if (m_alpha > 1.0f)
  383. m_alpha = 1.0f;
  384. //
  385. // Update color
  386. //
  387. m_color.red += m_colorRate.red;
  388. m_color.green += m_colorRate.green;
  389. m_color.blue += m_colorRate.blue;
  390. if (m_colorTargetKey < MAX_KEYFRAMES && m_colorKey[ m_colorTargetKey ].frame)
  391. {
  392. if (TheGameClient->getFrame() - m_createTimestamp >= m_colorKey[ m_colorTargetKey ].frame)
  393. {
  394. // can't set, because of colorscale
  395. // m_color = m_colorKey[ m_colorTargetKey ].color;
  396. m_colorTargetKey++;
  397. computeColorRate();
  398. }
  399. }
  400. else
  401. {
  402. m_colorRate.red = 0.0f;
  403. m_colorRate.green = 0.0f;
  404. m_colorRate.blue = 0.0f;
  405. }
  406. /// @todo Rethink this - at least its name
  407. m_color.red += m_colorScale;
  408. m_color.green += m_colorScale;
  409. m_color.blue += m_colorScale;
  410. if (m_color.red < 0.0f)
  411. m_color.red = 0.0f;
  412. else if (m_color.red > 1.0f)
  413. m_color.red = 1.0f;
  414. if (m_color.red < 0.0f)
  415. m_color.green = 0.0f;
  416. else if (m_color.green > 1.0f)
  417. m_color.green = 1.0f;
  418. if (m_color.blue < 0.0f)
  419. m_color.blue = 0.0f;
  420. else if (m_color.blue > 1.0f)
  421. m_color.blue = 1.0f;
  422. // reset the acceleration for accumulation next frame
  423. m_accel.x = 0.0f;
  424. m_accel.y = 0.0f;
  425. m_accel.z = 0.0f;
  426. if (m_drawable)
  427. {
  428. Matrix3D rot;
  429. rot.Make_Identity();
  430. rot.Rotate_X( m_angleX );
  431. rot.Rotate_Y( m_angleY );
  432. rot.Rotate_Z( m_angleZ );
  433. m_drawable->setInstanceMatrix( &rot );
  434. m_drawable->setPosition( &m_pos );
  435. }
  436. // monitor lifetime
  437. if (m_lifetimeLeft && --m_lifetimeLeft == 0)
  438. return false;
  439. DEBUG_ASSERTCRASH( m_lifetimeLeft, ( "A particle has an infinite lifetime..." ));
  440. // if we've gone totally invisible, destroy ourselves
  441. if (isInvisible())
  442. return false;
  443. return true;
  444. }
  445. // ------------------------------------------------------------------------------------------------
  446. /** Do wind motion as specified by the particle system template, if present */
  447. // ------------------------------------------------------------------------------------------------
  448. void Particle::doWindMotion( void )
  449. {
  450. ParticleSystemInfo::WindMotion windMotion = m_system->getWindMotion();
  451. // see if we should even do anything
  452. if( windMotion == ParticleSystemInfo::WIND_MOTION_NOT_USED )
  453. return;
  454. // get the angle of the wind
  455. Real windAngle = m_system->getWindAngle();
  456. // get the system position
  457. Coord3D systemPos;
  458. m_system->getPosition( &systemPos );
  459. // when we're attached objects and drawables we offset by that position as well
  460. if( ObjectID attachedObj = m_system->getAttachedObject() )
  461. {
  462. Object *obj = TheGameLogic->findObjectByID( attachedObj );
  463. if( obj )
  464. {
  465. const Coord3D *objPos = obj->getPosition();
  466. systemPos.x += objPos->x;
  467. systemPos.y += objPos->y;
  468. systemPos.z += objPos->z;
  469. } // end if
  470. } // end if
  471. else if( DrawableID attachedDraw = m_system->getAttachedDrawable() )
  472. {
  473. Drawable *draw = TheGameClient->findDrawableByID( attachedDraw );
  474. if( draw )
  475. {
  476. const Coord3D *drawPos = draw->getPosition();
  477. systemPos.x += drawPos->x;
  478. systemPos.y += drawPos->y;
  479. systemPos.z += drawPos->z;
  480. } // end if
  481. } // end else if
  482. //
  483. // compute a vector from the system position in the world to the particle ... we will use
  484. // this to compute how much force we apply
  485. //
  486. Coord3D v;
  487. v.x = m_pos.x - systemPos.x;
  488. v.y = m_pos.y - systemPos.y;
  489. v.z = m_pos.z - systemPos.z;
  490. // distance amounts for full force from wind and no force at all
  491. Real fullForceDistance = 75.0f;
  492. Real noForceDistance = 200.0f;
  493. //
  494. // given the distance from the wind position to the particle ... figure out how much
  495. // force we're going to apply to it. When it's further away (outside of the full force
  496. // distance) we will apply only a fraction of the force
  497. //
  498. Real distFromWind = v.length();
  499. if( distFromWind < noForceDistance )
  500. {
  501. Real windForceStrength = 2.0f * m_windRandomness;
  502. // only apply force if still within the circle of influence
  503. if( distFromWind > fullForceDistance )
  504. windForceStrength *= (1.0f - ((distFromWind - fullForceDistance) /
  505. (noForceDistance - fullForceDistance)));
  506. // integate the wind motion into the position
  507. m_pos.x += (Cos( windAngle ) * windForceStrength);
  508. m_pos.y += (Sin( windAngle ) * windForceStrength);
  509. } // end if
  510. } // doWindMotion
  511. // ------------------------------------------------------------------------------------------------
  512. /** Get priority of a particle ... which is the priority of it's attached system */
  513. // ------------------------------------------------------------------------------------------------
  514. ParticlePriorityType Particle::getPriority( void )
  515. {
  516. return m_system->getPriority();
  517. }
  518. // ------------------------------------------------------------------------------------------------
  519. /** Return true if this particle is invisible */
  520. // ------------------------------------------------------------------------------------------------
  521. Bool Particle::isInvisible( void )
  522. {
  523. // Drawables are never invisible (yet)
  524. /// @todo Allow Drawables to fade via alpha (MSB)
  525. if (m_drawable)
  526. return false;
  527. switch (m_system->getShaderType())
  528. {
  529. case ParticleSystemInfo::ADDITIVE:
  530. // if color is black, this particle is invisible
  531. // check that we're not in the process of going to another color
  532. if (m_colorKey[ m_colorTargetKey ].frame == 0)
  533. {
  534. if (m_color.red < 0.01f && m_color.green < 0.01f && m_color.blue < 0.01f)
  535. return true;
  536. }
  537. return false;
  538. case ParticleSystemInfo::ALPHA:
  539. // if alpha is zero, this particle is invisible
  540. if (m_alpha < 0.01f)
  541. return true;
  542. return false;
  543. case ParticleSystemInfo::ALPHA_TEST:
  544. // hmm... assume these particles are never invisible
  545. return false;
  546. case ParticleSystemInfo::MULTIPLY:
  547. // if color is white, this particle is invisible
  548. // check that we're not in the process of going to another color
  549. if (m_colorKey[ m_colorTargetKey ].frame == 0)
  550. {
  551. if (m_color.red > 0.99f && m_color.green > 0.99f && m_color.blue > 0.99f)
  552. return true;
  553. }
  554. return false;
  555. }
  556. // should never get here - if we do, data is incorrect
  557. return true;
  558. }
  559. // ------------------------------------------------------------------------------------------------
  560. /** CRC */
  561. // ------------------------------------------------------------------------------------------------
  562. void Particle::crc( Xfer *xfer )
  563. {
  564. } // end crc
  565. // ------------------------------------------------------------------------------------------------
  566. /** Xfer method
  567. * Version Info:
  568. * 1: Initial version */
  569. // ------------------------------------------------------------------------------------------------
  570. void Particle::xfer( Xfer *xfer )
  571. {
  572. // version
  573. XferVersion currentVersion = 1;
  574. XferVersion version = currentVersion;
  575. xfer->xferVersion( &version, currentVersion );
  576. // base class particle info
  577. ParticleInfo::xfer( xfer );
  578. // personality
  579. xfer->xferUnsignedInt( &m_personality );
  580. // acceleration
  581. xfer->xferCoord3D( &m_accel );
  582. // last position
  583. xfer->xferCoord3D( &m_lastPos );
  584. // lifetime left
  585. xfer->xferUnsignedInt( &m_lifetimeLeft );
  586. // creation timestamp
  587. xfer->xferUnsignedInt( &m_createTimestamp );
  588. // alpha
  589. xfer->xferReal( &m_alpha );
  590. // alpha rate
  591. xfer->xferReal( &m_alphaRate );
  592. // alpha target key
  593. xfer->xferInt( &m_alphaTargetKey );
  594. // color
  595. xfer->xferRGBColor( &m_color );
  596. // color rate
  597. xfer->xferRGBColor( &m_colorRate );
  598. // color target key
  599. xfer->xferInt( &m_colorTargetKey );
  600. // drawable
  601. DrawableID drawableID = m_drawable ? m_drawable->getID() : INVALID_DRAWABLE_ID;
  602. xfer->xferDrawableID( &drawableID );
  603. if( xfer->getXferMode() == XFER_LOAD && drawableID != INVALID_DRAWABLE_ID )
  604. {
  605. //
  606. // save drawable pointer, note that this xfer block is after all the drawable stuff
  607. // so we don't need to post process anything all the correct drawables are loaded
  608. // and available to us
  609. //
  610. m_drawable = TheGameClient->findDrawableByID( drawableID );
  611. // sanity
  612. if( m_drawable == NULL )
  613. {
  614. DEBUG_CRASH(( "Particle::xfer - Unable to find matching drawable id for particle\n" ));
  615. throw SC_INVALID_DATA;
  616. } // end if
  617. // set the particle pointer in the drawable
  618. m_drawable->friend_setParticle( this );
  619. } // end if
  620. // system under control as an id
  621. ParticleSystemID systemUnderControlID = m_systemUnderControl ? m_systemUnderControl->getSystemID() : INVALID_PARTICLE_SYSTEM_ID;
  622. xfer->xferUser( &systemUnderControlID, sizeof( ParticleSystemID ) );
  623. } // end xfer
  624. // ------------------------------------------------------------------------------------------------
  625. /** Load post process */
  626. // ------------------------------------------------------------------------------------------------
  627. void Particle::loadPostProcess( void )
  628. {
  629. // call base class post process
  630. ParticleInfo::loadPostProcess();
  631. // tidy up the m_systemUnderControl pointer
  632. if( m_systemUnderControlID != INVALID_PARTICLE_SYSTEM_ID )
  633. {
  634. ParticleSystem *system;
  635. // find system
  636. system = TheParticleSystemManager->findParticleSystem( m_systemUnderControlID );
  637. // set us as the control particle for this system
  638. system->setControlParticle( this );
  639. controlParticleSystem( system );
  640. // sanity
  641. if( m_systemUnderControlID == NULL )
  642. {
  643. DEBUG_CRASH(( "Particle::loadPostProcess - Unable to find system under control pointer\n" ));
  644. throw SC_INVALID_DATA;
  645. } // end if
  646. } // end if
  647. } // end loadPostProcess
  648. ///////////////////////////////////////////////////////////////////////////////////////////////////
  649. ///////////////////////////////////////////////////////////////////////////////////////////////////
  650. ///////////////////////////////////////////////////////////////////////////////////////////////////
  651. // ------------------------------------------------------------------------------------------------
  652. // ------------------------------------------------------------------------------------------------
  653. ParticleSystemInfo::ParticleSystemInfo()
  654. {
  655. m_priority = PARTICLE_PRIORITY_LOWEST;
  656. m_isGroundAligned = false;
  657. m_isEmitAboveGroundOnly = false;
  658. m_isParticleUpTowardsEmitter = false;
  659. //Added By Sadullah Nader
  660. //Initializations inserted
  661. m_driftVelocity.zero();
  662. m_gravity = 0.0f;
  663. m_isEmissionVolumeHollow = FALSE;
  664. m_isOneShot = FALSE;
  665. m_slavePosOffset.zero();
  666. m_systemLifetime = 0;
  667. //
  668. // some default values for the wind motion values
  669. m_windMotion = WIND_MOTION_NOT_USED;
  670. m_windAngle = 0.0f;
  671. m_windAngleChange = 0.15f; // higher is ping pong faster
  672. m_windAngleChangeMin = 0.15f;
  673. m_windAngleChangeMax = 0.45f;
  674. m_windMotionStartAngleMin = 0.0f;
  675. m_windMotionStartAngleMax = PI / 4.0f;
  676. m_windMotionStartAngle = m_windMotionStartAngleMin;
  677. m_windMotionEndAngleMin = TWO_PI - (PI / 4.0f);
  678. m_windMotionEndAngleMax = TWO_PI;
  679. m_windMotionEndAngle = m_windMotionEndAngleMin;
  680. m_windMotionMovingToEndAngle = TRUE;
  681. m_volumeParticleDepth = DEFAULT_VOLUME_PARTICLE_DEPTH;
  682. }
  683. void ParticleSystemInfo::tintAllColors( Color tintColor )
  684. {
  685. RGBColor rgb;
  686. rgb.setFromInt(tintColor);
  687. //This tints all but the first colorKey!!!
  688. for (int key = 1; key < MAX_KEYFRAMES; ++key )
  689. {
  690. m_colorKey[ key ].color.red *= (Real)(rgb.red ) / 255.0f;
  691. m_colorKey[ key ].color.green *= (Real)(rgb.green) / 255.0f;
  692. m_colorKey[ key ].color.blue *= (Real)(rgb.blue ) / 255.0f;
  693. }
  694. } // end loadPostProcess
  695. // ------------------------------------------------------------------------------------------------
  696. /** CRC */
  697. // ------------------------------------------------------------------------------------------------
  698. void ParticleSystemInfo::crc( Xfer *xfer )
  699. {
  700. } // end crc
  701. // ------------------------------------------------------------------------------------------------
  702. /** Xfer method
  703. * Version Info:
  704. * 1: Initial version */
  705. // ------------------------------------------------------------------------------------------------
  706. void ParticleSystemInfo::xfer( Xfer *xfer )
  707. {
  708. Int i;
  709. // version
  710. XferVersion currentVersion = 1;
  711. XferVersion version = currentVersion;
  712. xfer->xferVersion( &version, currentVersion );
  713. // is one shot
  714. xfer->xferBool( &m_isOneShot );
  715. // shader type
  716. xfer->xferUser( &m_shaderType, sizeof( ParticleShaderType ) );
  717. // particle type
  718. xfer->xferUser( &m_particleType, sizeof( ParticleType ) );
  719. // particle type name
  720. xfer->xferAsciiString( &m_particleTypeName );
  721. // angles
  722. xfer->xferUser( &m_angleX, sizeof( GameClientRandomVariable ) );
  723. xfer->xferUser( &m_angleY, sizeof( GameClientRandomVariable ) );
  724. xfer->xferUser( &m_angleZ, sizeof( GameClientRandomVariable ) );
  725. // angular rate
  726. xfer->xferUser( &m_angularRateX, sizeof( GameClientRandomVariable ) );
  727. xfer->xferUser( &m_angularRateY, sizeof( GameClientRandomVariable ) );
  728. xfer->xferUser( &m_angularRateZ, sizeof( GameClientRandomVariable ) );
  729. // angular damping
  730. xfer->xferUser( &m_angularDamping, sizeof( GameClientRandomVariable ) );
  731. // velocity damping
  732. xfer->xferUser( &m_velDamping, sizeof( GameClientRandomVariable ) );
  733. // lifetime
  734. xfer->xferUser( &m_lifetime, sizeof( GameClientRandomVariable ) );
  735. // system lifetime
  736. xfer->xferUnsignedInt( &m_systemLifetime );
  737. // start size
  738. xfer->xferUser( &m_startSize, sizeof( GameClientRandomVariable ) );
  739. // start size rate
  740. xfer->xferUser( &m_startSizeRate, sizeof( GameClientRandomVariable ) );
  741. // size rate
  742. xfer->xferUser( &m_sizeRate, sizeof( GameClientRandomVariable ) );
  743. // size rate damping
  744. xfer->xferUser( &m_sizeRateDamping, sizeof( GameClientRandomVariable ) );
  745. // alpha keys
  746. for( i = 0; i < MAX_KEYFRAMES; ++i )
  747. {
  748. xfer->xferUser( &m_alphaKey[ i ].var, sizeof( GameClientRandomVariable ) );
  749. xfer->xferUnsignedInt( &m_alphaKey[ i ].frame );
  750. } // end for, i
  751. // color keys
  752. for( i = 0; i < MAX_KEYFRAMES; ++i )
  753. {
  754. xfer->xferRGBColor( &m_colorKey[ i ].color );
  755. xfer->xferUnsignedInt( &m_colorKey[ i ].frame );
  756. } // end for, i
  757. // color scale
  758. xfer->xferUser( &m_colorScale, sizeof( GameClientRandomVariable ) );
  759. // burst delay
  760. xfer->xferUser( &m_burstDelay, sizeof( GameClientRandomVariable ) );
  761. // burst count
  762. xfer->xferUser( &m_burstCount, sizeof( GameClientRandomVariable ) );
  763. // initial delay
  764. xfer->xferUser( &m_initialDelay, sizeof( GameClientRandomVariable ) );
  765. // drift velocity
  766. xfer->xferCoord3D( &m_driftVelocity );
  767. // gravity
  768. xfer->xferReal( &m_gravity );
  769. // slave system name
  770. xfer->xferAsciiString( &m_slaveSystemName );
  771. // slave position offset
  772. xfer->xferCoord3D( &m_slavePosOffset );
  773. // attached system name
  774. xfer->xferAsciiString( &m_attachedSystemName );
  775. // emission velocity type, this must come before m_emissionVelocity
  776. xfer->xferUser( &m_emissionVelocityType, sizeof( EmissionVelocityType ) );
  777. // particle priority
  778. xfer->xferUser( &m_priority, sizeof( ParticlePriorityType ) );
  779. // emission velocity
  780. switch( m_emissionVelocityType )
  781. {
  782. // --------------------------------------------------------------------------------------------
  783. case ORTHO:
  784. xfer->xferUser( &m_emissionVelocity.ortho.x, sizeof( GameClientRandomVariable ) );
  785. xfer->xferUser( &m_emissionVelocity.ortho.y, sizeof( GameClientRandomVariable ) );
  786. xfer->xferUser( &m_emissionVelocity.ortho.z, sizeof( GameClientRandomVariable ) );
  787. break;
  788. // --------------------------------------------------------------------------------------------
  789. case SPHERICAL:
  790. xfer->xferUser( &m_emissionVelocity.spherical.speed, sizeof( GameClientRandomVariable ) );
  791. break;
  792. // --------------------------------------------------------------------------------------------
  793. case HEMISPHERICAL:
  794. xfer->xferUser( &m_emissionVelocity.hemispherical.speed, sizeof( GameClientRandomVariable ) );
  795. break;
  796. // --------------------------------------------------------------------------------------------
  797. case CYLINDRICAL:
  798. xfer->xferUser( &m_emissionVelocity.cylindrical.radial, sizeof( GameClientRandomVariable ) );
  799. xfer->xferUser( &m_emissionVelocity.cylindrical.normal, sizeof( GameClientRandomVariable ) );
  800. break;
  801. // --------------------------------------------------------------------------------------------
  802. case OUTWARD:
  803. xfer->xferUser( &m_emissionVelocity.outward.speed, sizeof( GameClientRandomVariable ) );
  804. xfer->xferUser( &m_emissionVelocity.outward.otherSpeed, sizeof( GameClientRandomVariable ) );
  805. break;
  806. } // end switch, m_emissionVelocityType
  807. // emission volume type
  808. xfer->xferUser( &m_emissionVolumeType, sizeof( EmissionVolumeType ) );
  809. // emission volume
  810. switch( m_emissionVolumeType )
  811. {
  812. // --------------------------------------------------------------------------------------------
  813. case POINT:
  814. // point has no data, it uses the systems position
  815. break;
  816. // --------------------------------------------------------------------------------------------
  817. case LINE:
  818. xfer->xferCoord3D( &m_emissionVolume.line.start );
  819. xfer->xferCoord3D( &m_emissionVolume.line.end );
  820. break;
  821. // --------------------------------------------------------------------------------------------
  822. case BOX:
  823. xfer->xferCoord3D( &m_emissionVolume.box.halfSize );
  824. break;
  825. // --------------------------------------------------------------------------------------------
  826. case SPHERE:
  827. xfer->xferReal( &m_emissionVolume.sphere.radius );
  828. break;
  829. // --------------------------------------------------------------------------------------------
  830. case CYLINDER:
  831. xfer->xferReal( &m_emissionVolume.cylinder.radius );
  832. xfer->xferReal( &m_emissionVolume.cylinder.length );
  833. break;
  834. } // end switch, m_emissionVolumeType
  835. // is emission volume hollow
  836. xfer->xferBool( &m_isEmissionVolumeHollow );
  837. // is ground aligned
  838. xfer->xferBool( &m_isGroundAligned );
  839. // emit above ground only
  840. xfer->xferBool( &m_isEmitAboveGroundOnly );
  841. // is particle up towards emitter
  842. xfer->xferBool( &m_isParticleUpTowardsEmitter );
  843. // wind motion
  844. xfer->xferUser( &m_windMotion, sizeof( WindMotion ) );
  845. // wind angle
  846. xfer->xferReal( &m_windAngle );
  847. // wind angle change
  848. xfer->xferReal( &m_windAngleChange );
  849. // wind angle change min
  850. xfer->xferReal( &m_windAngleChangeMin );
  851. // wind angle change max
  852. xfer->xferReal( &m_windAngleChangeMax );
  853. // wind motion start angle
  854. xfer->xferReal( &m_windMotionStartAngle );
  855. // wind motion start angle min
  856. xfer->xferReal( &m_windMotionStartAngleMin );
  857. // wind motion start angle max
  858. xfer->xferReal( &m_windMotionStartAngleMax );
  859. // wind motion end angle
  860. xfer->xferReal( &m_windMotionEndAngle );
  861. // wind motion end angle min
  862. xfer->xferReal( &m_windMotionEndAngleMin );
  863. // wind motion end angle max
  864. xfer->xferReal( &m_windMotionEndAngleMax );
  865. // wind motion moving to end angle
  866. xfer->xferByte( &m_windMotionMovingToEndAngle );
  867. } // end xfer
  868. // ------------------------------------------------------------------------------------------------
  869. /** Load post process */
  870. // ------------------------------------------------------------------------------------------------
  871. void ParticleSystemInfo::loadPostProcess( void )
  872. {
  873. } // end loadPostProcess
  874. ///////////////////////////////////////////////////////////////////////////////////////////////////
  875. // ParticleSystem /////////////////////////////////////////////////////////////////////////////////
  876. ///////////////////////////////////////////////////////////////////////////////////////////////////
  877. // ------------------------------------------------------------------------------------------------
  878. /** Read particle system properties from given file */
  879. // ------------------------------------------------------------------------------------------------
  880. ParticleSystem::ParticleSystem( const ParticleSystemTemplate *sysTemplate,
  881. ParticleSystemID id,
  882. Bool createSlaves )
  883. {
  884. m_systemParticlesHead = m_systemParticlesTail = NULL;
  885. m_isFirstPos = true;
  886. m_template = sysTemplate;
  887. m_systemID = id;
  888. //Added By Sadullah Nader
  889. //Initializations inserted
  890. m_lastPos.zero();
  891. m_pos.zero();
  892. m_velCoeff.zero();
  893. //
  894. m_attachedToDrawableID = INVALID_DRAWABLE_ID;
  895. m_attachedToObjectID = INVALID_ID;
  896. m_isLocalIdentity = true;
  897. m_localTransform.Make_Identity();
  898. m_isIdentity = true;
  899. m_transform.Make_Identity();
  900. m_isStopped = false;
  901. m_isDestroyed = false;
  902. m_isSaveable = true;
  903. m_slavePosOffset = sysTemplate->m_slavePosOffset;
  904. ///@todo: further formalize this parameter with an UnsignedInt field in the editor
  905. m_volumeParticleDepth = DEFAULT_VOLUME_PARTICLE_DEPTH;
  906. m_driftVelocity = sysTemplate->m_driftVelocity;
  907. m_velCoeff.x = 1.0f;
  908. m_velCoeff.y = 1.0f;
  909. m_velCoeff.z = 1.0f;
  910. m_countCoeff = 1.0f;
  911. m_delayCoeff = 1.0f;
  912. m_sizeCoeff = 1.0f;
  913. m_gravity = sysTemplate->m_gravity;
  914. m_lifetime = sysTemplate->m_lifetime;
  915. m_startSize = sysTemplate->m_startSize;
  916. m_startSizeRate = sysTemplate->m_startSizeRate;
  917. m_sizeRate = sysTemplate->m_sizeRate;
  918. m_sizeRateDamping = sysTemplate->m_sizeRateDamping;
  919. for( int i=0; i<MAX_KEYFRAMES; i++ )
  920. m_alphaKey[i] = sysTemplate->m_alphaKey[i];
  921. for( i=0; i<MAX_KEYFRAMES; i++ )
  922. m_colorKey[i] = sysTemplate->m_colorKey[i];
  923. /// @todo It is confusing to do this conversion here...
  924. Real low = sysTemplate->m_colorScale.getMinimumValue();
  925. Real hi = sysTemplate->m_colorScale.getMaximumValue();
  926. m_colorScale.setRange( low / 255.0f, hi / 255.0f );
  927. m_burstDelay = sysTemplate->m_burstDelay;
  928. m_burstDelayLeft = 0;
  929. m_burstCount = sysTemplate->m_burstCount;
  930. m_isOneShot = sysTemplate->m_isOneShot;
  931. m_delayLeft = (UnsignedInt)sysTemplate->m_initialDelay.getValue();
  932. m_startTimestamp = TheGameClient->getFrame();
  933. m_systemLifetimeLeft = sysTemplate->m_systemLifetime;
  934. if (sysTemplate->m_systemLifetime)
  935. m_isForever = false;
  936. else
  937. m_isForever = true;
  938. m_accumulatedSizeBonus = 0;
  939. m_velDamping = sysTemplate->m_velDamping;
  940. m_angleX = sysTemplate->m_angleX;
  941. m_angleY = sysTemplate->m_angleY;
  942. m_angleZ = sysTemplate->m_angleZ;
  943. m_angularRateX = sysTemplate->m_angularRateX;
  944. m_angularRateY = sysTemplate->m_angularRateY;
  945. m_angularRateZ = sysTemplate->m_angularRateZ;
  946. m_angularDamping = sysTemplate->m_angularDamping;
  947. m_priority = sysTemplate->m_priority;
  948. m_emissionVelocityType = sysTemplate->m_emissionVelocityType;
  949. m_emissionVelocity = sysTemplate->m_emissionVelocity;
  950. m_emissionVolumeType = sysTemplate->m_emissionVolumeType;
  951. m_emissionVolume = sysTemplate->m_emissionVolume;
  952. m_isEmissionVolumeHollow = sysTemplate->m_isEmissionVolumeHollow;
  953. m_isGroundAligned = sysTemplate->m_isGroundAligned;
  954. m_isEmitAboveGroundOnly = sysTemplate->m_isEmitAboveGroundOnly;
  955. m_isParticleUpTowardsEmitter = sysTemplate->m_isParticleUpTowardsEmitter;
  956. m_windMotion = sysTemplate->m_windMotion;
  957. m_windAngleChange = sysTemplate->m_windAngleChange;
  958. m_windAngleChangeMin = sysTemplate->m_windAngleChangeMin;
  959. m_windAngleChangeMax = sysTemplate->m_windAngleChangeMax;
  960. m_windMotionStartAngleMin = sysTemplate->m_windMotionStartAngleMin;
  961. m_windMotionStartAngleMax = sysTemplate->m_windMotionStartAngleMax;
  962. m_windMotionEndAngleMin = sysTemplate->m_windMotionEndAngleMin;
  963. m_windMotionEndAngleMax = sysTemplate->m_windMotionEndAngleMax;
  964. m_windMotionMovingToEndAngle = sysTemplate->m_windMotionMovingToEndAngle;
  965. m_windMotionStartAngle = GameClientRandomValueReal( m_windMotionStartAngleMin, m_windMotionStartAngleMax );
  966. m_windMotionEndAngle = GameClientRandomValueReal( m_windMotionEndAngleMin, m_windMotionEndAngleMax );
  967. m_windAngle = GameClientRandomValueReal( m_windMotionStartAngle, m_windMotionEndAngle );
  968. m_shaderType = sysTemplate->m_shaderType;
  969. m_particleType = sysTemplate->m_particleType;
  970. m_particleTypeName = sysTemplate->m_particleTypeName;
  971. m_isStopped = false;
  972. // set up slave particle system, if any
  973. m_masterSystemID = INVALID_PARTICLE_SYSTEM_ID;
  974. m_slaveSystemID = INVALID_PARTICLE_SYSTEM_ID;
  975. m_masterSystem = NULL;
  976. m_slaveSystem = NULL;
  977. if( createSlaves )
  978. {
  979. ParticleSystem *slaveSystem = sysTemplate->createSlaveSystem();
  980. if( slaveSystem )
  981. {
  982. setSlave( slaveSystem );
  983. m_slaveSystem->setMaster( this );
  984. } // end if
  985. } // end if
  986. m_attachedSystemName = sysTemplate->m_attachedSystemName;
  987. m_particleCount = 0;
  988. m_personalityStore = 0;
  989. m_controlParticle = NULL;
  990. TheParticleSystemManager->friend_addParticleSystem(this);
  991. //DEBUG_ASSERTLOG(!(m_totalParticleSystemCount % 10 == 0), ( "TotalParticleSystemCount = %d\n", m_totalParticleSystemCount ));
  992. }
  993. // ------------------------------------------------------------------------------------------------
  994. /** Destroy particle system and all of its particles */
  995. // ------------------------------------------------------------------------------------------------
  996. ParticleSystem::~ParticleSystem()
  997. {
  998. // tell any of our slave systems that we are going away
  999. if( m_slaveSystem )
  1000. {
  1001. DEBUG_ASSERTCRASH( m_slaveSystem->getMaster() == this, ("~ParticleSystem: Our slave doesn't have us as a master!\n") );
  1002. m_slaveSystem->setMaster( NULL );
  1003. setSlave( NULL );
  1004. } // end if
  1005. // tell any master system that *we* are going away
  1006. if( m_masterSystem )
  1007. {
  1008. DEBUG_ASSERTCRASH( m_masterSystem->getSlave() == this, ("~ParticleSystem: Our master doesn't have us as a slave!\n") );
  1009. m_masterSystem->setSlave( NULL );
  1010. setMaster( NULL );
  1011. } // end if
  1012. // destroy all particles "in the air"
  1013. while (m_systemParticlesHead)
  1014. m_systemParticlesHead->deleteInstance();
  1015. m_attachedToDrawableID = INVALID_DRAWABLE_ID;
  1016. m_attachedToObjectID = INVALID_ID;
  1017. // if this system was controlled by a particle, detach
  1018. if (m_controlParticle)
  1019. m_controlParticle->detachControlledParticleSystem();
  1020. m_controlParticle = NULL;
  1021. TheParticleSystemManager->friend_removeParticleSystem(this);
  1022. //DEBUG_ASSERTLOG(!(m_totalParticleSystemCount % 10 == 0), ( "TotalParticleSystemCount = %d\n", m_totalParticleSystemCount ));
  1023. }
  1024. // ------------------------------------------------------------------------------------------------
  1025. // ------------------------------------------------------------------------------------------------
  1026. void ParticleSystem::setMaster( ParticleSystem *master )
  1027. {
  1028. m_masterSystem = master;
  1029. m_masterSystemID = master ? master->getSystemID() : INVALID_PARTICLE_SYSTEM_ID;
  1030. } // end set Master
  1031. // ------------------------------------------------------------------------------------------------
  1032. // ------------------------------------------------------------------------------------------------
  1033. void ParticleSystem::setSlave( ParticleSystem *slave )
  1034. {
  1035. m_slaveSystem = slave;
  1036. m_slaveSystemID = slave ? slave->getSystemID() : INVALID_PARTICLE_SYSTEM_ID;
  1037. } // end setSlave
  1038. // ------------------------------------------------------------------------------------------------
  1039. // ------------------------------------------------------------------------------------------------
  1040. void ParticleSystem::setSaveable(Bool b)
  1041. {
  1042. m_isSaveable = b;
  1043. if (m_slaveSystem)
  1044. m_slaveSystem->setSaveable(b);
  1045. }
  1046. // ------------------------------------------------------------------------------------------------
  1047. /** (Re)start a stopped particle system */
  1048. // ------------------------------------------------------------------------------------------------
  1049. void ParticleSystem::start( void )
  1050. {
  1051. m_isStopped = false;
  1052. }
  1053. // ------------------------------------------------------------------------------------------------
  1054. /** Stop a particle system from emitting */
  1055. // ------------------------------------------------------------------------------------------------
  1056. void ParticleSystem::stop( void )
  1057. {
  1058. m_isStopped = true;
  1059. }
  1060. // ------------------------------------------------------------------------------------------------
  1061. /** Stop emitting, wait for all of our particles to die, then destroy self. */
  1062. // ------------------------------------------------------------------------------------------------
  1063. void ParticleSystem::destroy( void )
  1064. {
  1065. m_isDestroyed = true;
  1066. if( m_slaveSystem )
  1067. {
  1068. m_slaveSystem->destroy(); // If we don't it will leak forever. We are solely responsible for it.
  1069. }
  1070. }
  1071. // ------------------------------------------------------------------------------------------------
  1072. /** Get the position of the particle system */
  1073. // ------------------------------------------------------------------------------------------------
  1074. void ParticleSystem::getPosition( Coord3D *pos )
  1075. {
  1076. Vector3 vec;
  1077. m_localTransform.Get_Translation(&vec);
  1078. if (pos)
  1079. { pos->x=vec.X;
  1080. pos->y=vec.Y;
  1081. pos->z=vec.Z;
  1082. }
  1083. }
  1084. // ------------------------------------------------------------------------------------------------
  1085. /** Set the position of the particle system */
  1086. // ------------------------------------------------------------------------------------------------
  1087. void ParticleSystem::setPosition( const Coord3D *pos )
  1088. {
  1089. m_localTransform.Set_X_Translation( pos->x );
  1090. m_localTransform.Set_Y_Translation( pos->y );
  1091. m_localTransform.Set_Z_Translation( pos->z );
  1092. m_isLocalIdentity = false;
  1093. }
  1094. // ------------------------------------------------------------------------------------------------
  1095. /** Set the system's local transform */
  1096. // ------------------------------------------------------------------------------------------------
  1097. void ParticleSystem::setLocalTransform( const Matrix3D *matrix )
  1098. {
  1099. m_localTransform = *matrix;
  1100. m_isLocalIdentity = false;
  1101. }
  1102. // ------------------------------------------------------------------------------------------------
  1103. /** Rotate local transform matrix */
  1104. // ------------------------------------------------------------------------------------------------
  1105. void ParticleSystem::rotateLocalTransformX( Real x )
  1106. {
  1107. m_localTransform.Rotate_X( x );
  1108. m_isLocalIdentity = false;
  1109. }
  1110. // ------------------------------------------------------------------------------------------------
  1111. /** Rotate local transform matrix */
  1112. // ------------------------------------------------------------------------------------------------
  1113. void ParticleSystem::rotateLocalTransformY( Real y )
  1114. {
  1115. m_localTransform.Rotate_Y( y );
  1116. m_isLocalIdentity = false;
  1117. }
  1118. // ------------------------------------------------------------------------------------------------
  1119. /** Rotate local transform matrix */
  1120. // ------------------------------------------------------------------------------------------------
  1121. void ParticleSystem::rotateLocalTransformZ( Real z )
  1122. {
  1123. m_localTransform.Rotate_Z( z );
  1124. m_isLocalIdentity = false;
  1125. }
  1126. // ------------------------------------------------------------------------------------------------
  1127. /** Attach this particle system to a Drawable */
  1128. // ------------------------------------------------------------------------------------------------
  1129. void ParticleSystem::attachToDrawable( const Drawable *draw )
  1130. {
  1131. if (draw)
  1132. m_attachedToDrawableID = draw->getID();
  1133. else
  1134. m_attachedToDrawableID = INVALID_DRAWABLE_ID;
  1135. }
  1136. // ------------------------------------------------------------------------------------------------
  1137. /** Attach this particle system to a Drawable */
  1138. // ------------------------------------------------------------------------------------------------
  1139. void ParticleSystem::attachToObject( const Object *obj )
  1140. {
  1141. if (obj)
  1142. m_attachedToObjectID = obj->getID();
  1143. else
  1144. m_attachedToObjectID = INVALID_ID;
  1145. }
  1146. // ------------------------------------------------------------------------------------------------
  1147. /** Compute a random point on a unit sphere
  1148. * @todo The density of random points generated is not uniform within the sphere */
  1149. // ------------------------------------------------------------------------------------------------
  1150. const Coord3D *ParticleSystem::computePointOnUnitSphere( void )
  1151. {
  1152. static Coord3D point;
  1153. do
  1154. {
  1155. point.x = GameClientRandomValueReal( -1.0f, 1.0f );
  1156. point.y = GameClientRandomValueReal( -1.0f, 1.0f );
  1157. point.z = GameClientRandomValueReal( -1.0f, 1.0f );
  1158. }
  1159. while (point.x == 0.0f && point.y == 0.0f && point.z == 0.0f);
  1160. point.normalize();
  1161. return &point;
  1162. }
  1163. // ------------------------------------------------------------------------------------------------
  1164. /** Compute a velocity vector based on emission properties */
  1165. // ------------------------------------------------------------------------------------------------
  1166. const Coord3D *ParticleSystem::computeParticleVelocity( const Coord3D *pos )
  1167. {
  1168. static Coord3D newVel;
  1169. switch( m_emissionVelocityType )
  1170. {
  1171. case ORTHO:
  1172. newVel.x = m_emissionVelocity.ortho.x.getValue();
  1173. newVel.y = m_emissionVelocity.ortho.y.getValue();
  1174. newVel.z = m_emissionVelocity.ortho.z.getValue();
  1175. break;
  1176. case CYLINDRICAL:
  1177. {
  1178. Real radialSpeed, angle;
  1179. radialSpeed = m_emissionVelocity.cylindrical.radial.getValue();
  1180. angle = GameClientRandomValueReal( 0, 2.0f*PI );
  1181. newVel.x = radialSpeed * cos( angle );
  1182. newVel.y = radialSpeed * sin( angle );
  1183. newVel.z = m_emissionVelocity.cylindrical.normal.getValue();
  1184. break;
  1185. }
  1186. // "outward" velocity is directed along the surface normal of the emission volume
  1187. case OUTWARD:
  1188. {
  1189. Real speed = m_emissionVelocity.outward.speed.getValue();
  1190. Real otherSpeed = m_emissionVelocity.outward.otherSpeed.getValue();
  1191. Coord3D sysPos;
  1192. /*
  1193. sysPos.x = m_localTransform.Get_X_Translation();
  1194. sysPos.y = m_localTransform.Get_Y_Translation();
  1195. sysPos.z = m_localTransform.Get_Z_Translation();
  1196. */
  1197. sysPos.x = 0.0f;
  1198. sysPos.y = 0.0f;
  1199. sysPos.z = 0.0f;
  1200. switch( m_emissionVolumeType )
  1201. {
  1202. case CYLINDER:
  1203. Coord2D disk;
  1204. disk.x = pos->x - sysPos.x;
  1205. disk.y = pos->y - sysPos.y;
  1206. disk.normalize();
  1207. newVel.x = speed * disk.x;
  1208. newVel.y = speed * disk.y;
  1209. newVel.z = otherSpeed;
  1210. break;
  1211. case BOX: ///< @todo Implement BOX OUTWARD velocity
  1212. case SPHERE:
  1213. {
  1214. newVel.x = pos->x - sysPos.x;
  1215. newVel.y = pos->y - sysPos.y;
  1216. newVel.z = pos->z - sysPos.z;
  1217. newVel.normalize();
  1218. newVel.x *= speed;
  1219. newVel.y *= speed;
  1220. newVel.z *= speed;
  1221. break;
  1222. }
  1223. case LINE:
  1224. {
  1225. Coord3D along; // unit vector along line direction
  1226. along.x = m_emissionVolume.line.end.x - m_emissionVolume.line.start.x;
  1227. along.y = m_emissionVolume.line.end.y - m_emissionVolume.line.start.y;
  1228. along.z = m_emissionVolume.line.end.z - m_emissionVolume.line.start.z;
  1229. along.normalize();
  1230. Coord3D perp; // unit vector perpendicular to the along/up plane
  1231. Coord3D up; // unit vector in the up direction (Z)
  1232. up.x = 0.0;
  1233. up.y = 0.0;
  1234. up.z = 1.0;
  1235. perp.crossProduct( &up, &along, &perp );
  1236. up.crossProduct( &along, &perp, &up );
  1237. // "speed" is in 'horizontal' plane, and "otherSpeed" is 'vertical'
  1238. newVel.x = speed * perp.x + otherSpeed * up.x;
  1239. newVel.y = speed * perp.y + otherSpeed * up.y;
  1240. newVel.z = speed * perp.z + otherSpeed * up.z;
  1241. break;
  1242. }
  1243. case POINT:
  1244. {
  1245. Coord3D vel = *computePointOnUnitSphere();
  1246. newVel.x = speed * vel.x;
  1247. newVel.y = speed * vel.y;
  1248. newVel.z = speed * vel.z;
  1249. break;
  1250. }
  1251. }
  1252. break;
  1253. }
  1254. case SPHERICAL:
  1255. {
  1256. Real speed = m_emissionVelocity.spherical.speed.getValue();
  1257. Coord3D vel = *computePointOnUnitSphere();
  1258. newVel.x = speed * vel.x;
  1259. newVel.y = speed * vel.y;
  1260. newVel.z = speed * vel.z;
  1261. break;
  1262. }
  1263. case HEMISPHERICAL:
  1264. {
  1265. Coord3D vel;
  1266. Real speed = m_emissionVelocity.spherical.speed.getValue();
  1267. do
  1268. {
  1269. vel.x = GameClientRandomValueReal( -1.0f, 1.0f );
  1270. vel.y = GameClientRandomValueReal( -1.0f, 1.0f );
  1271. vel.z = GameClientRandomValueReal( 0.0f, 1.0f );
  1272. }
  1273. while (vel.x == 0.0f && vel.y == 0.0f && vel.z == 0.0f);
  1274. vel.normalize();
  1275. newVel.x = speed * vel.x;
  1276. newVel.y = speed * vel.y;
  1277. newVel.z = speed * vel.z;
  1278. break;
  1279. }
  1280. default:
  1281. newVel.x = 0.0f;
  1282. newVel.y = 0.0f;
  1283. newVel.z = 0.0f;
  1284. break;
  1285. }
  1286. // scale the velocity by the velocity multiplier
  1287. newVel.x *= m_velCoeff.x*(0.5f+TheGlobalData->m_particleScale/2.0f);
  1288. newVel.y *= m_velCoeff.y*(0.5f+TheGlobalData->m_particleScale/2.0f);
  1289. newVel.z *= m_velCoeff.z*(0.5f+TheGlobalData->m_particleScale/2.0f);
  1290. return &newVel;
  1291. }
  1292. // ------------------------------------------------------------------------------------------------
  1293. /** Compute a position based on emission properties */
  1294. // ------------------------------------------------------------------------------------------------
  1295. const Coord3D *ParticleSystem::computeParticlePosition( void )
  1296. {
  1297. static Coord3D newPos;
  1298. switch( m_emissionVolumeType )
  1299. {
  1300. case CYLINDER:
  1301. {
  1302. Real angle = GameClientRandomValueReal( 0, 2.0f*PI );
  1303. Real radius;
  1304. if (m_isEmissionVolumeHollow)
  1305. radius = m_emissionVolume.cylinder.radius;
  1306. else
  1307. radius = GameClientRandomValueReal( 0.0f, m_emissionVolume.cylinder.radius );
  1308. newPos.x = radius * cos( angle );
  1309. newPos.y = radius * sin( angle );
  1310. Real halfLength = m_emissionVolume.cylinder.length/2.0f;
  1311. newPos.z = GameClientRandomValueReal( -halfLength, halfLength );
  1312. break;
  1313. }
  1314. case SPHERE:
  1315. {
  1316. Real radius;
  1317. if (m_isEmissionVolumeHollow)
  1318. radius = m_emissionVolume.sphere.radius;
  1319. else
  1320. radius = GameClientRandomValueReal( 0.0f, m_emissionVolume.sphere.radius );
  1321. newPos = *computePointOnUnitSphere();
  1322. newPos.x *= radius;
  1323. newPos.y *= radius;
  1324. newPos.z *= radius;
  1325. break;
  1326. }
  1327. case BOX:
  1328. {
  1329. if (m_isEmissionVolumeHollow) {
  1330. // determine which side to generate on.
  1331. // 0 is bottom, 3 is top,
  1332. // 1 is left , 4 is right
  1333. // 2 is front, 5 is right back
  1334. int side = GameClientRandomValue(0, 6);
  1335. if (side % 3 == 0) {
  1336. // generate X, Y
  1337. newPos.x = GameClientRandomValueReal( -m_emissionVolume.box.halfSize.x, m_emissionVolume.box.halfSize.x );
  1338. newPos.y = GameClientRandomValueReal( -m_emissionVolume.box.halfSize.y, m_emissionVolume.box.halfSize.y );
  1339. if (side == 0) {
  1340. newPos.z = -m_emissionVolume.box.halfSize.z;
  1341. } else {
  1342. newPos.z = m_emissionVolume.box.halfSize.z;
  1343. }
  1344. } else if (side % 3 == 1) {
  1345. // generate Y, Z
  1346. newPos.y = GameClientRandomValueReal( -m_emissionVolume.box.halfSize.y, m_emissionVolume.box.halfSize.y );
  1347. newPos.z = GameClientRandomValueReal( -m_emissionVolume.box.halfSize.z, m_emissionVolume.box.halfSize.z );
  1348. if (side == 1) {
  1349. newPos.x = -m_emissionVolume.box.halfSize.x;
  1350. } else {
  1351. newPos.x = m_emissionVolume.box.halfSize.y;
  1352. }
  1353. } else if (side % 3 == 2) {
  1354. // generate X, Z
  1355. newPos.x = GameClientRandomValueReal( -m_emissionVolume.box.halfSize.x, m_emissionVolume.box.halfSize.x );
  1356. newPos.z = GameClientRandomValueReal( -m_emissionVolume.box.halfSize.z, m_emissionVolume.box.halfSize.z );
  1357. if (side == 2) {
  1358. newPos.y = -m_emissionVolume.box.halfSize.y;
  1359. } else {
  1360. newPos.y = m_emissionVolume.box.halfSize.y;
  1361. }
  1362. }
  1363. } else {
  1364. newPos.x = GameClientRandomValueReal( -m_emissionVolume.box.halfSize.x, m_emissionVolume.box.halfSize.x );
  1365. newPos.y = GameClientRandomValueReal( -m_emissionVolume.box.halfSize.y, m_emissionVolume.box.halfSize.y );
  1366. newPos.z = GameClientRandomValueReal( -m_emissionVolume.box.halfSize.z, m_emissionVolume.box.halfSize.z );
  1367. break;
  1368. }
  1369. }
  1370. case LINE:
  1371. {
  1372. Coord3D delta, start, end;
  1373. start = m_emissionVolume.line.start;
  1374. end = m_emissionVolume.line.end;
  1375. delta.x = end.x - start.x;
  1376. delta.y = end.y - start.y;
  1377. delta.z = end.z - start.z;
  1378. Real t = GameClientRandomValueReal( 0.0f, 1.0f );
  1379. newPos.x = start.x + t * delta.x;
  1380. newPos.y = start.y + t * delta.y;
  1381. newPos.z = start.z + t * delta.z;
  1382. break;
  1383. }
  1384. case POINT:
  1385. default:
  1386. newPos.x = 0.0f;
  1387. newPos.y = 0.0f;
  1388. newPos.z = 0.0f;
  1389. break;
  1390. }
  1391. newPos.x *= (0.5f+TheGlobalData->m_particleScale/2.0f);
  1392. newPos.y *= (0.5f+TheGlobalData->m_particleScale/2.0f);
  1393. newPos.z *= (0.5f+TheGlobalData->m_particleScale/2.0f);
  1394. return &newPos;
  1395. }
  1396. // ------------------------------------------------------------------------------------------------
  1397. /** Factory method for particles. */
  1398. // ------------------------------------------------------------------------------------------------
  1399. Particle *ParticleSystem::createParticle( const ParticleInfo *info,
  1400. ParticlePriorityType priority,
  1401. Bool forceCreate )
  1402. {
  1403. //
  1404. // if we aren't absolutely forcing this particle to be created (which is needed when
  1405. // loading and creating particle systems from the save games) we need to check a few
  1406. // restrictions before this particle can really be created
  1407. //
  1408. if( forceCreate == FALSE )
  1409. {
  1410. if (TheGlobalData->m_useFX == FALSE)
  1411. return NULL;
  1412. //
  1413. // Enforce particle limit.
  1414. // If we are at the limit, destroy the oldest particle in order
  1415. // to make room for this one.
  1416. //
  1417. //
  1418. // Check if particle is below priorities we allow for this FPS or if it being skipped because
  1419. // all particesl are being skipped (excluding special fps independent particles at
  1420. // getMinDynamicParticleSkipPriority())
  1421. //
  1422. if( priority < TheGameLODManager->getMinDynamicParticlePriority() ||
  1423. (priority < TheGameLODManager->getMinDynamicParticleSkipPriority() &&
  1424. TheGameLODManager->isParticleSkipped()) )
  1425. return NULL;
  1426. if ( getParticleCount() > 0 && priority == AREA_EFFECT && m_isGroundAligned && TheParticleSystemManager->getFieldParticleCount() > (UnsignedInt)TheGlobalData->m_maxFieldParticleCount )
  1427. return NULL;
  1428. // ALWAYS_RENDER particles are exempt from all count limits, and are always created, regardless of LOD issues.
  1429. if (priority != ALWAYS_RENDER)
  1430. {
  1431. int numInExcess = TheParticleSystemManager->getParticleCount() - (UnsignedInt)TheGlobalData->m_maxParticleCount;
  1432. if ( numInExcess > 0)
  1433. {
  1434. if( TheParticleSystemManager->removeOldestParticles((UnsignedInt) numInExcess, priority) != numInExcess )
  1435. return NULL; // could not remove enough particles, don't create new stuff
  1436. }
  1437. if (TheGlobalData->m_maxParticleCount == 0)
  1438. return NULL;
  1439. }
  1440. } // end if
  1441. Particle *p = newInstance(Particle)( this, info );
  1442. return p;
  1443. }
  1444. // ------------------------------------------------------------------------------------------------
  1445. /** Generate a new, random set of ParticleInfo
  1446. * particleNum and particleCount are used to get 'tween frame particles emitted in the correct
  1447. * place. (jkmcd) */
  1448. // ------------------------------------------------------------------------------------------------
  1449. const ParticleInfo *ParticleSystem::generateParticleInfo( Int particleNum, Int particleCount )
  1450. {
  1451. static ParticleInfo info;
  1452. if (particleCount == 0) {
  1453. DEBUG_CRASH(("particleCount must NOT be 0. Set to 1 or greater."));
  1454. return &info;
  1455. }
  1456. // NOTE: position MUST be computed before velocity, in case OUTWARD velocity is
  1457. // specified, which must know where the particle is in space.
  1458. info.m_pos = *computeParticlePosition();
  1459. info.m_vel = *computeParticleVelocity( &info.m_pos );
  1460. // transform the position and velocity, if necessary
  1461. /// @todo Avoid conversion from Coord3D to Vector3 somehow
  1462. if (m_isIdentity == false)
  1463. {
  1464. // transform particle position to world coordinates
  1465. Vector3 p, pr;
  1466. Coord3D emissionAdjustment; // this is the adjustment for inter-frame emission
  1467. // @todo : This should work, if m_lastPos = m_pos is removed from here but it doesn't.
  1468. // @todo : Investigate why. jkmcd
  1469. if (m_isFirstPos) {
  1470. m_lastPos = m_pos;
  1471. m_isFirstPos = false;
  1472. }
  1473. emissionAdjustment.x = (1 - (INT_TO_REAL(particleNum) / particleCount)) * (m_pos.x - m_lastPos.x);
  1474. emissionAdjustment.y = (1 - (INT_TO_REAL(particleNum) / particleCount)) * (m_pos.y - m_lastPos.y);
  1475. emissionAdjustment.z = (1 - (INT_TO_REAL(particleNum) / particleCount)) * (m_pos.z - m_lastPos.z);
  1476. p.X = info.m_pos.x;
  1477. p.Y = info.m_pos.y;
  1478. p.Z = info.m_pos.z;
  1479. #ifdef ALLOW_TEMPORARIES
  1480. pr = m_transform * p;
  1481. #else
  1482. m_transform.mulVector3(p, pr);
  1483. #endif
  1484. info.m_pos.x = pr.X - emissionAdjustment.x;
  1485. info.m_pos.y = pr.Y - emissionAdjustment.y;
  1486. info.m_pos.z = pr.Z - emissionAdjustment.z;
  1487. // transform particle velocity to world coordinates
  1488. Vector3 v, vr;
  1489. v.X = info.m_vel.x;
  1490. v.Y = info.m_vel.y;
  1491. v.Z = info.m_vel.z;
  1492. Matrix3D::Rotate_Vector( m_transform, v, &vr );
  1493. info.m_vel.x = vr.X;
  1494. info.m_vel.y = vr.Y;
  1495. info.m_vel.z = vr.Z;
  1496. }
  1497. info.m_velDamping = m_velDamping.getValue();
  1498. info.m_angularDamping = m_angularDamping.getValue();
  1499. info.m_angleX = m_angleX.getValue();
  1500. info.m_angleY = m_angleY.getValue();
  1501. info.m_angleZ = m_angleZ.getValue();
  1502. info.m_angularRateX = m_angularRateX.getValue();
  1503. info.m_angularRateY = m_angularRateY.getValue();
  1504. info.m_angularRateZ = m_angularRateZ.getValue();
  1505. info.m_lifetime = (UnsignedInt)m_lifetime.getValue();
  1506. info.m_size = m_startSize.getValue()*m_sizeCoeff*TheGlobalData->m_particleScale;
  1507. info.m_sizeRate = m_sizeRate.getValue()*m_sizeCoeff*TheGlobalData->m_particleScale;
  1508. info.m_sizeRateDamping = m_sizeRateDamping.getValue();
  1509. // Keeping a running tally makes each successive particle spawned start a bit bigger (or smaller).
  1510. info.m_size += m_accumulatedSizeBonus;
  1511. m_accumulatedSizeBonus += m_startSizeRate.getValue();
  1512. if( m_accumulatedSizeBonus )
  1513. m_accumulatedSizeBonus = min( m_accumulatedSizeBonus, (float)MAX_SIZE_BONUS );
  1514. for( int i=0; i<MAX_KEYFRAMES; i++ )
  1515. {
  1516. info.m_alphaKey[i].value = m_alphaKey[i].var.getValue();
  1517. info.m_alphaKey[i].frame = m_alphaKey[i].frame;
  1518. }
  1519. /*
  1520. info.m_color.red = m_color.red.getValue();
  1521. info.m_color.green = m_color.green.getValue();
  1522. info.m_color.blue = m_color.blue.getValue();
  1523. */
  1524. for( i=0; i<MAX_KEYFRAMES; i++ )
  1525. {
  1526. info.m_colorKey[i] = m_colorKey[i];
  1527. }
  1528. info.m_colorScale = m_colorScale.getValue();
  1529. #ifdef ALLOW_TEMPORARIES
  1530. Vector3 pos = m_transform * Vector3(0, 0, 0);
  1531. #else
  1532. Vector3 pos;
  1533. m_transform.mulVector3(Vector3(0, 0, 0), pos);
  1534. #endif
  1535. info.m_emitterPos.x = pos.X;
  1536. info.m_emitterPos.y = pos.Y;
  1537. info.m_emitterPos.z = pos.Z;
  1538. info.m_particleUpTowardsEmitter = m_isParticleUpTowardsEmitter;
  1539. info.m_windRandomness = GameClientRandomValueReal( 0.7f, 1.3f );
  1540. return &info;
  1541. }
  1542. // ------------------------------------------------------------------------------------------------
  1543. /** Update this particle system, potentially generating new particles */
  1544. // ------------------------------------------------------------------------------------------------
  1545. Bool ParticleSystem::update( Int localPlayerIndex )
  1546. {
  1547. if (TheGlobalData->m_useFX == FALSE)
  1548. return false;
  1549. // do initial delay ... note, this currently delays the lifetime
  1550. if (m_delayLeft)
  1551. {
  1552. --m_delayLeft;
  1553. // system actually "starts" once initial delay is over
  1554. /// @todo reset start time when system is stopped/started
  1555. if (m_delayLeft == 0)
  1556. m_startTimestamp = TheGameClient->getFrame();
  1557. return true;
  1558. }
  1559. // update the wind motion
  1560. updateWindMotion();
  1561. // if this system is attached to a Drawable/Object, update the current transform
  1562. // matrix so generated particles' are relative to the parent Drawable's
  1563. // position and orientation
  1564. Bool transformSet = false;
  1565. const Matrix3D *parentXfrm = NULL;
  1566. Bool isShrouded = false;
  1567. if (m_attachedToDrawableID)
  1568. {
  1569. Drawable *attachedTo = TheGameClient->findDrawableByID( m_attachedToDrawableID );
  1570. if (attachedTo)
  1571. {
  1572. if (attachedTo->getFullyObscuredByShroud())
  1573. isShrouded = true;
  1574. parentXfrm = attachedTo->getTransformMatrix();
  1575. m_lastPos = m_pos;
  1576. m_pos = *attachedTo->getPosition();
  1577. }
  1578. else
  1579. {
  1580. // Drawable has been destroyed - lose our attachment to it
  1581. m_attachedToDrawableID = INVALID_DRAWABLE_ID;
  1582. // destroy ourselves
  1583. destroy();
  1584. }
  1585. }
  1586. if (m_attachedToObjectID)
  1587. {
  1588. Object *attachedTo = TheGameLogic->findObjectByID( m_attachedToObjectID );
  1589. if (attachedTo)
  1590. {
  1591. if (!isShrouded)
  1592. isShrouded = (attachedTo->getShroudedStatus(localPlayerIndex) >= OBJECTSHROUD_FOGGED);
  1593. parentXfrm = attachedTo->getTransformMatrix();
  1594. m_lastPos = m_pos;
  1595. m_pos = *attachedTo->getPosition();
  1596. }
  1597. else
  1598. {
  1599. // Drawable has been destroyed - lose our attachment to it
  1600. m_attachedToObjectID = INVALID_ID;
  1601. // destroy ourselves
  1602. destroy();
  1603. }
  1604. }
  1605. if (parentXfrm)
  1606. {
  1607. // if system has its own local transform, concatenate them
  1608. if (m_isLocalIdentity == false)
  1609. #ifdef ALLOW_TEMPORARIES
  1610. m_transform = (*parentXfrm) * m_localTransform;
  1611. #else
  1612. m_transform.mul(*parentXfrm, m_localTransform);
  1613. #endif
  1614. else
  1615. m_transform = *parentXfrm;
  1616. m_isIdentity = false;
  1617. transformSet = true;
  1618. }
  1619. if (transformSet == false)
  1620. {
  1621. if (m_isLocalIdentity == false)
  1622. {
  1623. m_transform = m_localTransform;
  1624. m_isIdentity = false;
  1625. }
  1626. else
  1627. {
  1628. m_isIdentity = true;
  1629. }
  1630. }
  1631. // if we are controlled by a particle, its position is local origin
  1632. if (m_controlParticle)
  1633. {
  1634. const Coord3D *controlPos = m_controlParticle->getPosition();
  1635. /// @todo Concatenate this, instead of overriding (MSB)
  1636. m_transform.Set_X_Translation( controlPos->x );
  1637. m_transform.Set_Y_Translation( controlPos->y );
  1638. m_transform.Set_Z_Translation( controlPos->z );
  1639. m_isIdentity = false;
  1640. m_lastPos = m_pos;
  1641. m_pos = *controlPos;
  1642. }
  1643. //
  1644. // Generate new particles if the system hasn't been 'stopped' or 'destroyed'
  1645. // If we are a slave system, do not generate particles ourselves - our master will force us to
  1646. //
  1647. if (m_isDestroyed == false)
  1648. {
  1649. if (m_isForever || (m_isForever == false && m_systemLifetimeLeft > 0))
  1650. {
  1651. if (!isShrouded && m_isStopped == false && m_masterSystem == NULL)
  1652. {
  1653. if (m_burstDelayLeft == 0)
  1654. {
  1655. ParticlePriorityType priority = getPriority();
  1656. // emit a burst of particles
  1657. Int count = REAL_TO_INT(m_burstCount.getValue());
  1658. count *= m_countCoeff;
  1659. for( Int i=0; i<count; i++ )
  1660. {
  1661. // generate this particle's unique attributes
  1662. const ParticleInfo *info = generateParticleInfo(i, count);
  1663. if (!m_isEmitAboveGroundOnly || (info->m_pos.z >= TheTerrainLogic->getGroundHeight(info->m_pos.x, info->m_pos.y)))
  1664. {
  1665. // actually create a particle
  1666. Particle *p = createParticle( info, priority );
  1667. if (p == NULL)
  1668. continue;
  1669. if (m_attachedSystemName.isEmpty() == false)
  1670. {
  1671. const ParticleSystemTemplate *tmp = TheParticleSystemManager->findTemplate( m_attachedSystemName );
  1672. if (tmp)
  1673. {
  1674. ParticleSystem *sys = TheParticleSystemManager->createParticleSystem( tmp, TRUE );
  1675. sys->setControlParticle( p );
  1676. p->controlParticleSystem( sys );
  1677. }
  1678. }
  1679. // create a slave particle, if necessary
  1680. if (m_slaveSystem)
  1681. {
  1682. ParticleInfo mergeInfo = ParticleSystem::mergeRelatedParticleSystems(this, m_slaveSystem, false);
  1683. // create slaved particle
  1684. m_slaveSystem->createParticle( &mergeInfo, priority );
  1685. }
  1686. }
  1687. }
  1688. // compute next burst delay
  1689. m_burstDelayLeft = (UnsignedInt)m_burstDelay.getValue();
  1690. m_burstDelayLeft *= m_delayCoeff;
  1691. }
  1692. else
  1693. {
  1694. m_burstDelayLeft--;
  1695. }
  1696. } // end if stopped check
  1697. } // end if system lifetime check
  1698. } // end if is destroyed
  1699. //
  1700. // Update all particles in the system
  1701. //
  1702. Particle *p = m_systemParticlesHead;
  1703. Particle *oldParticle;
  1704. while (p)
  1705. {
  1706. // apply 'gravity' force
  1707. if (m_gravity != 0.0f)
  1708. {
  1709. Coord3D force;
  1710. force.x = 0.0f;
  1711. force.y = 0.0f;
  1712. force.z = m_gravity;
  1713. p->applyForce( &force );
  1714. }
  1715. if (p->update() == false)
  1716. {
  1717. oldParticle = p;
  1718. p = p->m_systemNext;
  1719. oldParticle->deleteInstance();
  1720. } else {
  1721. p = p->m_systemNext;
  1722. }
  1723. }
  1724. //
  1725. // If we have been "destroyed", wait for all of our particles to die off,
  1726. // then destroy ourselves (return false).
  1727. //
  1728. if (m_isDestroyed && !m_systemParticlesHead)
  1729. return false;
  1730. // monitor particle system lifetime
  1731. if (m_isForever == false)
  1732. {
  1733. // decrement lifetime if not zero
  1734. if (m_systemLifetimeLeft)
  1735. m_systemLifetimeLeft--;
  1736. // if there are still particles "in the air", don't destroy yet
  1737. if (getParticleCount())
  1738. return true;
  1739. // check if time is up
  1740. if (m_systemLifetimeLeft == 0)
  1741. return false;
  1742. }
  1743. return true;
  1744. }
  1745. // ------------------------------------------------------------------------------------------------
  1746. /** Update the wind motion */
  1747. // ------------------------------------------------------------------------------------------------
  1748. void ParticleSystem::updateWindMotion( void )
  1749. {
  1750. switch( m_windMotion )
  1751. {
  1752. // --------------------------------------------------------------------------------------------
  1753. case ParticleSystemInfo::WIND_MOTION_PING_PONG:
  1754. {
  1755. Real startAngle = m_windMotionStartAngle;
  1756. Real endAngle = m_windMotionEndAngle;
  1757. // this only works when start angle is less than end angle
  1758. DEBUG_ASSERTCRASH( startAngle < endAngle, ("updateWindMotion: startAngle must be < endAngle\n") );
  1759. // how big is the total angle span
  1760. Real totalSpan = endAngle - startAngle;
  1761. Real halfSpan = totalSpan / 2.0f;
  1762. // given our current angle ... how far away from the "center" of the span are we
  1763. Real diffFromCenter = fabs( halfSpan - m_windAngle + startAngle );
  1764. //
  1765. // given our distance from the center ... we need to compute how much we will change
  1766. // the angle. When we are closer to the center we change it faster (more), and when
  1767. // we are near the edges we change is slower (less)
  1768. //
  1769. Real change = (1.0f - (diffFromCenter / halfSpan)) * m_windAngleChange;
  1770. // we will always change a little bit
  1771. #define MINIMUM_CHANGE 0.005f // lower #'s have softer swings at the edge angles
  1772. if( change < MINIMUM_CHANGE )
  1773. change = MINIMUM_CHANGE;
  1774. //
  1775. // if we are moving toward the end angle we add the change, if we're moving away
  1776. // from it we subtract it
  1777. //
  1778. if( m_windMotionMovingToEndAngle )
  1779. {
  1780. // add angle
  1781. m_windAngle += change;
  1782. // see if we're at the end and should switch directions
  1783. if( m_windAngle >= endAngle )
  1784. {
  1785. // change directions
  1786. m_windMotionMovingToEndAngle = FALSE;
  1787. // pick a new change delta
  1788. m_windAngleChange =
  1789. GameClientRandomValueReal( m_windAngleChangeMin, m_windAngleChangeMax );
  1790. // pick new start and end angles
  1791. m_windMotionStartAngle =
  1792. GameClientRandomValueReal( m_windMotionStartAngleMin,
  1793. m_windMotionStartAngleMax );
  1794. m_windMotionEndAngle =
  1795. GameClientRandomValueReal( m_windMotionEndAngleMin,
  1796. m_windMotionEndAngleMax );
  1797. } // end if
  1798. } // end if
  1799. else
  1800. {
  1801. // subtract angle
  1802. m_windAngle -= change;
  1803. // see if we're at the end and should switch directions
  1804. if( m_windAngle <= startAngle )
  1805. {
  1806. // change directions
  1807. m_windMotionMovingToEndAngle = TRUE;
  1808. // pick a new change delta
  1809. m_windAngleChange =
  1810. GameClientRandomValueReal( m_windAngleChangeMin, m_windAngleChangeMax );
  1811. // pick new start and end angles
  1812. m_windMotionStartAngle =
  1813. GameClientRandomValueReal( m_windMotionStartAngleMin,
  1814. m_windMotionStartAngleMax );
  1815. m_windMotionEndAngle =
  1816. GameClientRandomValueReal( m_windMotionEndAngleMin,
  1817. m_windMotionEndAngleMax );
  1818. } // end if
  1819. } // end else
  1820. break;
  1821. } // end case
  1822. // --------------------------------------------------------------------------------------------
  1823. case ParticleSystemInfo::WIND_MOTION_CIRCULAR:
  1824. {
  1825. // give us a wind angle change if one hasn't been specifed (this plays nice with the particle editor)
  1826. if( m_windAngleChange == 0.0f )
  1827. m_windAngleChange = GameClientRandomValueReal( m_windAngleChangeMin, m_windAngleChangeMax );
  1828. // add to our wind angle
  1829. m_windAngle += m_windAngleChange;
  1830. // keep in 0 to 2PI range just to keep the numbers safe and sane
  1831. if( m_windAngle > TWO_PI )
  1832. m_windAngle -= TWO_PI;
  1833. else if( m_windAngle < 0.0f )
  1834. m_windAngle += TWO_PI;
  1835. break;
  1836. } // end case
  1837. // ---------------------------------------------------------------------------------------------
  1838. default:
  1839. {
  1840. break;
  1841. } // end default
  1842. } // end if
  1843. } // end updateWindMotion
  1844. // ------------------------------------------------------------------------------------------------
  1845. // ------------------------------------------------------------------------------------------------
  1846. void ParticleSystem::addParticle( Particle *particleToAdd )
  1847. {
  1848. if (particleToAdd->m_inSystemList)
  1849. return;
  1850. if (!m_systemParticlesHead)
  1851. {
  1852. m_systemParticlesHead = particleToAdd;
  1853. }
  1854. if (m_systemParticlesTail)
  1855. {
  1856. m_systemParticlesTail->m_systemNext = particleToAdd;
  1857. particleToAdd->m_systemPrev = m_systemParticlesTail;
  1858. }
  1859. else
  1860. {
  1861. particleToAdd->m_systemPrev = NULL;
  1862. }
  1863. m_systemParticlesTail = particleToAdd;
  1864. particleToAdd->m_systemNext = NULL;
  1865. particleToAdd->m_inSystemList = TRUE;
  1866. ++m_particleCount;
  1867. particleToAdd->setPersonality( m_personalityStore++ );
  1868. }
  1869. // ------------------------------------------------------------------------------------------------
  1870. /** Remove given particle from the list - ONLY FOR USE BY PARTICLE */
  1871. // ------------------------------------------------------------------------------------------------
  1872. void ParticleSystem::removeParticle( Particle *particleToRemove )
  1873. {
  1874. if (!particleToRemove->m_inSystemList)
  1875. return;
  1876. // remove links from prev & next objs
  1877. if (particleToRemove->m_systemNext)
  1878. particleToRemove->m_systemNext->m_systemPrev = particleToRemove->m_systemPrev;
  1879. if (particleToRemove->m_systemPrev)
  1880. particleToRemove->m_systemPrev->m_systemNext = particleToRemove->m_systemNext;
  1881. // update head & tail if neccessary
  1882. if (particleToRemove == m_systemParticlesHead)
  1883. m_systemParticlesHead = particleToRemove->m_systemNext;
  1884. if (particleToRemove == m_systemParticlesTail)
  1885. m_systemParticlesTail = particleToRemove->m_systemPrev;
  1886. particleToRemove->m_systemNext = particleToRemove->m_systemPrev = NULL;
  1887. particleToRemove->m_inSystemList = FALSE;
  1888. --m_particleCount;
  1889. }
  1890. // ------------------------------------------------------------------------------------------------
  1891. // ------------------------------------------------------------------------------------------------
  1892. ParticleInfo ParticleSystem::mergeRelatedParticleSystems( ParticleSystem *masterParticleSystem, ParticleSystem *slaveParticleSystem, Bool slaveNeedsFullPromotion)
  1893. {
  1894. if (!masterParticleSystem || !slaveParticleSystem) {
  1895. DEBUG_CRASH(("masterParticleSystem or slaveParticleSystem was NULL. Should not happen. JKMCD"));
  1896. ParticleInfo bogus;
  1897. memset( &bogus, 0, sizeof( bogus ) );
  1898. return bogus;
  1899. }
  1900. // copy info
  1901. ParticleInfo mergeInfo = *masterParticleSystem->generateParticleInfo(1, 1);
  1902. // generate one from the slave system
  1903. const ParticleInfo *info = slaveParticleSystem->generateParticleInfo(1, 1);
  1904. // override unique attributes of slave particle
  1905. mergeInfo.m_lifetime = info->m_lifetime;
  1906. // size becomes a scale factor of master's particles
  1907. mergeInfo.m_size *= info->m_size;
  1908. mergeInfo.m_sizeRate *= info->m_sizeRate;
  1909. mergeInfo.m_sizeRateDamping *= info->m_sizeRateDamping;
  1910. mergeInfo.m_angleX = info->m_angleX;
  1911. mergeInfo.m_angleY = info->m_angleY;
  1912. mergeInfo.m_angleZ = info->m_angleZ;
  1913. mergeInfo.m_angularRateX = info->m_angularRateX;
  1914. mergeInfo.m_angularRateY = info->m_angularRateY;
  1915. mergeInfo.m_angularRateZ = info->m_angularRateZ;
  1916. mergeInfo.m_angularDamping = info->m_angularDamping;
  1917. for( int i=0; i<MAX_KEYFRAMES; i++ )
  1918. mergeInfo.m_alphaKey[i] = info->m_alphaKey[i];
  1919. for( i=0; i<MAX_KEYFRAMES; i++ )
  1920. mergeInfo.m_colorKey[i] = info->m_colorKey[i];
  1921. mergeInfo.m_colorScale = info->m_colorScale;
  1922. // offset slave's position relative to master's
  1923. const Coord3D *offset = slaveParticleSystem->getSlavePositionOffset();
  1924. mergeInfo.m_pos.x += offset->x;
  1925. mergeInfo.m_pos.y += offset->y;
  1926. mergeInfo.m_pos.z += offset->z;
  1927. if (slaveNeedsFullPromotion) {
  1928. slaveParticleSystem->m_burstCount = masterParticleSystem->m_burstCount;
  1929. slaveParticleSystem->m_burstDelay = masterParticleSystem->m_burstDelay;
  1930. slaveParticleSystem->m_priority = masterParticleSystem->m_priority;
  1931. slaveParticleSystem->m_emissionVelocity = masterParticleSystem->m_emissionVelocity;
  1932. slaveParticleSystem->m_emissionVelocityType = masterParticleSystem->m_emissionVelocityType;
  1933. slaveParticleSystem->m_emissionVolume = masterParticleSystem->m_emissionVolume;
  1934. slaveParticleSystem->m_emissionVolumeType = masterParticleSystem->m_emissionVolumeType;
  1935. slaveParticleSystem->m_isEmissionVolumeHollow = masterParticleSystem->m_isEmissionVolumeHollow;
  1936. slaveParticleSystem->m_startSize.setRange(masterParticleSystem->m_startSize.getMinimumValue() * slaveParticleSystem->m_startSize.getMinimumValue(),
  1937. masterParticleSystem->m_startSize.getMaximumValue() * slaveParticleSystem->m_startSize.getMaximumValue(),
  1938. masterParticleSystem->m_startSize.getDistributionType());
  1939. slaveParticleSystem->m_sizeRate.setRange(masterParticleSystem->m_sizeRate.getMinimumValue() * slaveParticleSystem->m_sizeRate.getMinimumValue(),
  1940. masterParticleSystem->m_sizeRate.getMaximumValue() * slaveParticleSystem->m_sizeRate.getMaximumValue(),
  1941. masterParticleSystem->m_sizeRate.getDistributionType());
  1942. slaveParticleSystem->m_sizeRateDamping.setRange(masterParticleSystem->m_sizeRateDamping.getMinimumValue() * slaveParticleSystem->m_sizeRateDamping.getMinimumValue(),
  1943. masterParticleSystem->m_sizeRateDamping.getMaximumValue() * slaveParticleSystem->m_sizeRateDamping.getMaximumValue(),
  1944. masterParticleSystem->m_sizeRateDamping.getDistributionType());
  1945. // slaveParticleSystem->m_burstCount.setRange(masterParticleSystem->m_burstCount.getMinimumValue() / 2,
  1946. // masterParticleSystem->m_burstCount.getMaximumValue() / 2,
  1947. // masterParticleSystem->m_burstCount.getDistributionType());
  1948. }
  1949. return mergeInfo;
  1950. }
  1951. // ------------------------------------------------------------------------------------------------
  1952. // ------------------------------------------------------------------------------------------------
  1953. void ParticleSystem::setLifetimeRange( Real min, Real max )
  1954. {
  1955. m_lifetime.setRange( min, max );
  1956. }
  1957. // ------------------------------------------------------------------------------------------------
  1958. // ------------------------------------------------------------------------------------------------
  1959. void ParticleSystem::setControlParticle( Particle *p )
  1960. {
  1961. m_controlParticle = p;
  1962. }
  1963. // ------------------------------------------------------------------------------------------------
  1964. /** CRC */
  1965. // ------------------------------------------------------------------------------------------------
  1966. void ParticleSystem::crc( Xfer *xfer )
  1967. {
  1968. } // end crc
  1969. // ------------------------------------------------------------------------------------------------
  1970. /** Xfer method
  1971. * Version Info:
  1972. * 1: Initial version */
  1973. // ------------------------------------------------------------------------------------------------
  1974. void ParticleSystem::xfer( Xfer *xfer )
  1975. {
  1976. // version
  1977. XferVersion currentVersion = 1;
  1978. XferVersion version = currentVersion;
  1979. xfer->xferVersion( &version, currentVersion );
  1980. // base class info
  1981. ParticleSystemInfo::xfer( xfer );
  1982. // particle system ID
  1983. xfer->xferUser( &m_systemID, sizeof( ParticleSystemID ) );
  1984. // attached to drawable id
  1985. xfer->xferDrawableID( &m_attachedToDrawableID );
  1986. // attached to object id
  1987. xfer->xferObjectID( &m_attachedToObjectID );
  1988. // is local identity
  1989. xfer->xferBool( &m_isLocalIdentity );
  1990. // local transform
  1991. xfer->xferUser( &m_localTransform, sizeof( Matrix3D ) );
  1992. // is identity
  1993. xfer->xferBool( &m_isIdentity );
  1994. // transform
  1995. xfer->xferUser( &m_transform, sizeof( Matrix3D ) );
  1996. // burst delay left
  1997. xfer->xferUnsignedInt( &m_burstDelayLeft );
  1998. // delay left
  1999. xfer->xferUnsignedInt( &m_delayLeft );
  2000. // start timestamp
  2001. xfer->xferUnsignedInt( &m_startTimestamp );
  2002. // system lifetime left
  2003. xfer->xferUnsignedInt( &m_systemLifetimeLeft );
  2004. // personality store
  2005. xfer->xferUnsignedInt( &m_personalityStore );
  2006. // is forever
  2007. xfer->xferBool( &m_isForever );
  2008. // accumulated size bonus
  2009. xfer->xferReal( &m_accumulatedSizeBonus );
  2010. // is stopped
  2011. xfer->xferBool( &m_isStopped );
  2012. // we never save destroyed particle systems so there is no need to consider m_isDestroyed
  2013. // m_isDestroyed <-- do nothing with me
  2014. // ditto for m_isSaveable
  2015. // m_isSaveable <-- do nothing with me
  2016. // velCoeff
  2017. xfer->xferCoord3D( &m_velCoeff );
  2018. // count coeff
  2019. xfer->xferReal( &m_countCoeff );
  2020. // delay coeff
  2021. xfer->xferReal( &m_delayCoeff );
  2022. // size coeff
  2023. xfer->xferReal( &m_sizeCoeff );
  2024. // position
  2025. xfer->xferCoord3D( &m_pos );
  2026. // last position
  2027. xfer->xferCoord3D( &m_lastPos );
  2028. // is first pos
  2029. xfer->xferBool( &m_isFirstPos );
  2030. // slave system id
  2031. xfer->xferUser( &m_slaveSystemID, sizeof( ParticleSystemID ) );
  2032. // master system
  2033. xfer->xferUser( &m_masterSystemID, sizeof( ParticleSystemID ) );
  2034. // particle count
  2035. UnsignedInt particleCount = m_particleCount;
  2036. xfer->xferUnsignedInt( &particleCount );
  2037. // particles
  2038. if( xfer->getXferMode() == XFER_SAVE )
  2039. {
  2040. Particle *particle;
  2041. // go through all particles in this system
  2042. for( particle = m_systemParticlesHead; particle; particle = particle->m_systemNext )
  2043. {
  2044. // write particle information
  2045. xfer->xferSnapshot( particle );
  2046. } // end for, particle
  2047. } // end if, save
  2048. else
  2049. {
  2050. ParticlePriorityType priority = getPriority();
  2051. const ParticleInfo *info = generateParticleInfo( 0, 1 );
  2052. Particle *particle;
  2053. // read each particle data block
  2054. for( UnsignedInt i = 0; i < particleCount; ++i )
  2055. {
  2056. // create a new particle
  2057. particle = createParticle( info, priority, TRUE );
  2058. // sanity
  2059. DEBUG_ASSERTCRASH( particle, ("ParticleSyste::xfer - Unable to create particle for loading\n") );
  2060. // read in the particle data
  2061. xfer->xferSnapshot( particle );
  2062. } // end for i
  2063. } // end else, load
  2064. } // end xfer
  2065. // ------------------------------------------------------------------------------------------------
  2066. /** Load post process */
  2067. // ------------------------------------------------------------------------------------------------
  2068. void ParticleSystem::loadPostProcess( void )
  2069. {
  2070. // call base class post process
  2071. ParticleSystemInfo::loadPostProcess();
  2072. // reconnect slave pointers if needed
  2073. if( m_slaveSystemID != INVALID_PARTICLE_SYSTEM_ID )
  2074. {
  2075. // sanity
  2076. if( m_slaveSystem != NULL )
  2077. {
  2078. DEBUG_CRASH(( "ParticleSystem::loadPostProcess - m_slaveSystem is not NULL but should be\n" ));
  2079. throw SC_INVALID_DATA;
  2080. } // end if
  2081. // assign system
  2082. m_slaveSystem = TheParticleSystemManager->findParticleSystem( m_slaveSystemID );
  2083. // sanity
  2084. if( m_slaveSystem == NULL || m_slaveSystem->isDestroyed() == TRUE )
  2085. {
  2086. DEBUG_CRASH(( "ParticleSystem::loadPostProcess - m_slaveSystem is NULL or destroyed\n" ));
  2087. throw SC_INVALID_DATA;
  2088. } // end if
  2089. } // end if
  2090. // reconnect master pointers if needed
  2091. if( m_masterSystemID != INVALID_PARTICLE_SYSTEM_ID )
  2092. {
  2093. // sanity
  2094. if( m_masterSystem != NULL )
  2095. {
  2096. DEBUG_CRASH(( "ParticleSystem::loadPostProcess - m_masterSystem is not NULL but should be\n" ));
  2097. throw SC_INVALID_DATA;
  2098. } // end if
  2099. // assign system
  2100. m_masterSystem = TheParticleSystemManager->findParticleSystem( m_masterSystemID );
  2101. // sanity
  2102. if( m_masterSystem == NULL || m_masterSystem->isDestroyed() == TRUE )
  2103. {
  2104. DEBUG_CRASH(( "ParticleSystem::loadPostProcess - m_masterSystem is NULL or destroyed\n" ));
  2105. throw SC_INVALID_DATA;
  2106. } // end if
  2107. } // end if
  2108. } // end loadPostProcess
  2109. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2110. // ParticleSystemTemplate /////////////////////////////////////////////////////////////////////////
  2111. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2112. // ------------------------------------------------------------------------------------------------
  2113. /** INI parse data */
  2114. // ------------------------------------------------------------------------------------------------
  2115. const FieldParse ParticleSystemTemplate::m_fieldParseTable[] =
  2116. {
  2117. { "Priority", INI::parseIndexList, ParticlePriorityNames, offsetof( ParticleSystemTemplate, m_priority ) },
  2118. { "IsOneShot", INI::parseBool, NULL, offsetof( ParticleSystemTemplate, m_isOneShot ) },
  2119. { "Shader", INI::parseIndexList, ParticleShaderTypeNames, offsetof( ParticleSystemTemplate, m_shaderType ) },
  2120. { "Type", INI::parseIndexList, ParticleTypeNames, offsetof( ParticleSystemTemplate, m_particleType ) },
  2121. { "ParticleName", INI::parseAsciiString, NULL, offsetof( ParticleSystemTemplate, m_particleTypeName ) },
  2122. { "AngleX", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_angleX ) },
  2123. { "AngleY", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_angleY ) },
  2124. { "AngleZ", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_angleZ ) },
  2125. { "AngularRateX", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_angularRateX ) },
  2126. { "AngularRateY", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_angularRateY ) },
  2127. { "AngularRateZ", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_angularRateZ ) },
  2128. { "AngularDamping", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_angularDamping ) },
  2129. { "VelocityDamping", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_velDamping ) },
  2130. { "Gravity", INI::parseReal, NULL, offsetof( ParticleSystemTemplate, m_gravity ) },
  2131. { "SlaveSystem", INI::parseAsciiString, NULL, offsetof( ParticleSystemTemplate, m_slaveSystemName ) },
  2132. { "SlavePosOffset", INI::parseCoord3D, NULL, offsetof( ParticleSystemTemplate, m_slavePosOffset ) },
  2133. { "PerParticleAttachedSystem", INI::parseAsciiString, NULL, offsetof( ParticleSystemTemplate, m_attachedSystemName ) },
  2134. { "Lifetime", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_lifetime ) },
  2135. { "SystemLifetime", INI::parseUnsignedInt, NULL, offsetof( ParticleSystemTemplate, m_systemLifetime ) },
  2136. { "Size", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_startSize ) },
  2137. { "StartSizeRate", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_startSizeRate ) },
  2138. { "SizeRate", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_sizeRate ) },
  2139. { "SizeRateDamping", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_sizeRateDamping ) },
  2140. { "Alpha1", ParticleSystemTemplate::parseRandomKeyframe, NULL, offsetof( ParticleSystemTemplate, m_alphaKey[0] ) },
  2141. { "Alpha2", ParticleSystemTemplate::parseRandomKeyframe, NULL, offsetof( ParticleSystemTemplate, m_alphaKey[1] ) },
  2142. { "Alpha3", ParticleSystemTemplate::parseRandomKeyframe, NULL, offsetof( ParticleSystemTemplate, m_alphaKey[2] ) },
  2143. { "Alpha4", ParticleSystemTemplate::parseRandomKeyframe, NULL, offsetof( ParticleSystemTemplate, m_alphaKey[3] ) },
  2144. { "Alpha5", ParticleSystemTemplate::parseRandomKeyframe, NULL, offsetof( ParticleSystemTemplate, m_alphaKey[4] ) },
  2145. { "Alpha6", ParticleSystemTemplate::parseRandomKeyframe, NULL, offsetof( ParticleSystemTemplate, m_alphaKey[5] ) },
  2146. { "Alpha7", ParticleSystemTemplate::parseRandomKeyframe, NULL, offsetof( ParticleSystemTemplate, m_alphaKey[6] ) },
  2147. { "Alpha8", ParticleSystemTemplate::parseRandomKeyframe, NULL, offsetof( ParticleSystemTemplate, m_alphaKey[7] ) },
  2148. { "Color1", ParticleSystemTemplate::parseRGBColorKeyframe,NULL, offsetof( ParticleSystemTemplate, m_colorKey[0] ) },
  2149. { "Color2", ParticleSystemTemplate::parseRGBColorKeyframe,NULL, offsetof( ParticleSystemTemplate, m_colorKey[1] ) },
  2150. { "Color3", ParticleSystemTemplate::parseRGBColorKeyframe,NULL, offsetof( ParticleSystemTemplate, m_colorKey[2] ) },
  2151. { "Color4", ParticleSystemTemplate::parseRGBColorKeyframe,NULL, offsetof( ParticleSystemTemplate, m_colorKey[3] ) },
  2152. { "Color5", ParticleSystemTemplate::parseRGBColorKeyframe,NULL, offsetof( ParticleSystemTemplate, m_colorKey[4] ) },
  2153. { "Color6", ParticleSystemTemplate::parseRGBColorKeyframe,NULL, offsetof( ParticleSystemTemplate, m_colorKey[5] ) },
  2154. { "Color7", ParticleSystemTemplate::parseRGBColorKeyframe,NULL, offsetof( ParticleSystemTemplate, m_colorKey[6] ) },
  2155. { "Color8", ParticleSystemTemplate::parseRGBColorKeyframe,NULL, offsetof( ParticleSystemTemplate, m_colorKey[7] ) },
  2156. // { "COLOR", ParticleSystemTemplate::parseRandomRGBColor, NULL, offsetof( ParticleSystemTemplate, m_color ) },
  2157. { "ColorScale", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_colorScale ) },
  2158. { "BurstDelay", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_burstDelay ) },
  2159. { "BurstCount", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_burstCount ) },
  2160. { "InitialDelay", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_initialDelay ) },
  2161. { "DriftVelocity", INI::parseCoord3D, NULL, offsetof( ParticleSystemTemplate, m_driftVelocity ) },
  2162. { "VelocityType", INI::parseIndexList, EmissionVelocityTypeNames, offsetof( ParticleSystemTemplate, m_emissionVelocityType ) },
  2163. { "VelOrthoX", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_emissionVelocity.ortho.x ) },
  2164. { "VelOrthoY", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_emissionVelocity.ortho.y ) },
  2165. { "VelOrthoZ", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_emissionVelocity.ortho.z ) },
  2166. { "VelSpherical", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_emissionVelocity.spherical.speed ) },
  2167. { "VelHemispherical", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_emissionVelocity.hemispherical.speed ) },
  2168. { "VelCylindricalRadial", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_emissionVelocity.cylindrical.radial ) },
  2169. { "VelCylindricalNormal", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_emissionVelocity.cylindrical.normal ) },
  2170. { "VelOutward", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_emissionVelocity.outward.speed ) },
  2171. { "VelOutwardOther", INI::parseGameClientRandomVariable, NULL, offsetof( ParticleSystemTemplate, m_emissionVelocity.outward.otherSpeed ) },
  2172. { "VolumeType", INI::parseIndexList, EmissionVolumeTypeNames, offsetof( ParticleSystemTemplate, m_emissionVolumeType ) },
  2173. { "VolLineStart", INI::parseCoord3D, NULL, offsetof( ParticleSystemTemplate, m_emissionVolume.line.start ) },
  2174. { "VolLineEnd", INI::parseCoord3D, NULL, offsetof( ParticleSystemTemplate, m_emissionVolume.line.end ) },
  2175. { "VolBoxHalfSize", INI::parseCoord3D, NULL, offsetof( ParticleSystemTemplate, m_emissionVolume.box.halfSize ) },
  2176. { "VolSphereRadius", INI::parseReal, NULL, offsetof( ParticleSystemTemplate, m_emissionVolume.sphere.radius ) },
  2177. { "VolCylinderRadius", INI::parseReal, NULL, offsetof( ParticleSystemTemplate, m_emissionVolume.cylinder.radius ) },
  2178. { "VolCylinderLength", INI::parseReal, NULL, offsetof( ParticleSystemTemplate, m_emissionVolume.cylinder.length ) },
  2179. { "IsHollow", INI::parseBool, NULL, offsetof( ParticleSystemTemplate, m_isEmissionVolumeHollow ) },
  2180. { "IsGroundAligned", INI::parseBool, NULL, offsetof( ParticleSystemTemplate, m_isGroundAligned ) },
  2181. { "IsEmitAboveGroundOnly", INI::parseBool, NULL, offsetof( ParticleSystemTemplate, m_isEmitAboveGroundOnly) },
  2182. { "IsParticleUpTowardsEmitter", INI::parseBool, NULL, offsetof( ParticleSystemTemplate, m_isParticleUpTowardsEmitter) },
  2183. { "WindMotion", INI::parseIndexList, WindMotionNames, offsetof( ParticleSystemTemplate, m_windMotion ) },
  2184. { "WindAngleChangeMin", INI::parseReal, NULL, offsetof( ParticleSystemTemplate, m_windAngleChangeMin ) },
  2185. { "WindAngleChangeMax", INI::parseReal, NULL, offsetof( ParticleSystemTemplate, m_windAngleChangeMax ) },
  2186. { "WindPingPongStartAngleMin", INI::parseReal, NULL, offsetof( ParticleSystemTemplate, m_windMotionStartAngleMin ) },
  2187. { "WindPingPongStartAngleMax", INI::parseReal, NULL, offsetof( ParticleSystemTemplate, m_windMotionStartAngleMax ) },
  2188. { "WindPingPongEndAngleMin", INI::parseReal, NULL, offsetof( ParticleSystemTemplate, m_windMotionEndAngleMin ) },
  2189. { "WindPingPongEndAngleMax", INI::parseReal, NULL, offsetof( ParticleSystemTemplate, m_windMotionEndAngleMax ) },
  2190. { NULL, NULL, NULL, 0 },
  2191. };
  2192. // ------------------------------------------------------------------------------------------------
  2193. /** Parse a "random keyframe".
  2194. * The format is "FIELD = low high frame". */
  2195. // ------------------------------------------------------------------------------------------------
  2196. void ParticleSystemTemplate::parseRandomKeyframe( INI* ini, void *instance,
  2197. void *store, const void* /*userData*/ )
  2198. {
  2199. RandomKeyframe *key = static_cast<RandomKeyframe *>(store);
  2200. Real low = ini->scanReal(ini->getNextToken());
  2201. Real high = ini->scanReal(ini->getNextToken());
  2202. key->frame = ini->scanUnsignedInt(ini->getNextToken());
  2203. // set the range of the random variable
  2204. key->var.setRange( low, high );
  2205. }
  2206. // ------------------------------------------------------------------------------------------------
  2207. /** Parse a "color keyframe".
  2208. * The format is "FIELD = R:r G:g B:b frame". */
  2209. // ------------------------------------------------------------------------------------------------
  2210. void ParticleSystemTemplate::parseRGBColorKeyframe( INI* ini, void *instance,
  2211. void *store, const void* /*userData*/ )
  2212. {
  2213. RGBColorKeyframe *key = static_cast<RGBColorKeyframe *>(store);
  2214. INI::parseRGBColor( ini, instance, &key->color, NULL );
  2215. INI::parseUnsignedInt( ini, instance, &key->frame, NULL );
  2216. }
  2217. // ------------------------------------------------------------------------------------------------
  2218. /** Parse a RandomVariable RGB color.
  2219. * Note that the components may be negative, as this is used for rates, as well. */
  2220. // ------------------------------------------------------------------------------------------------
  2221. void ParticleSystemTemplate::parseRandomRGBColor( INI* ini, void *instance,
  2222. void *store, const void* /*userData*/ )
  2223. {
  2224. #if 0
  2225. char seps[] = " \n\r\t=:RGB,";
  2226. const char *token;
  2227. Int colors[2][3];
  2228. Int result;
  2229. enum { LO = 0, HI = 1 };
  2230. enum { RED = 0, GREEN = 1, BLUE = 2 };
  2231. // initialize to invalid values
  2232. colors[ LO ][ RED ] = -1;
  2233. colors[ LO ][ GREEN ] = -1;
  2234. colors[ LO ][ BLUE ] = -1;
  2235. colors[ HI ][ RED ] = -1;
  2236. colors[ HI ][ GREEN ] = -1;
  2237. colors[ HI ][ BLUE ] = -1;
  2238. // do each color part
  2239. for( Int i = 0; i < 3; i++ )
  2240. {
  2241. for( Int j = 0; j < 2; j++ )
  2242. {
  2243. // get the color number
  2244. token = ini->getNextToken(seps);
  2245. // convert to number
  2246. colors[j][i] = ini->scanInt(token);
  2247. // check to see if it's within range
  2248. if( colors[j][i] < -255 || colors[j][i] > 255 )
  2249. throw INI_INVALID_DATA;
  2250. } // end for i
  2251. }
  2252. // assign the color components to the "RGBColor" pointer at 'store'
  2253. ParticleSystemInfo::RandomRGBColor *theColor = (ParticleSystemInfo::RandomRGBColor *)store;
  2254. theColor->red.setRange( (Real)colors[ LO ][ RED ] / 255.0f, (Real)colors[ HI ][ RED ] / 255.0f );
  2255. theColor->green.setRange( (Real)colors[ LO ][ GREEN ] / 255.0f, (Real)colors[ HI ][ GREEN ] / 255.0f );
  2256. theColor->blue.setRange( (Real)colors[ LO ][ BLUE ] / 255.0f, (Real)colors[ HI ][ BLUE ] / 255.0f );
  2257. #endif
  2258. }
  2259. // ------------------------------------------------------------------------------------------------
  2260. // ------------------------------------------------------------------------------------------------
  2261. ParticleSystemTemplate::ParticleSystemTemplate( const AsciiString &name ) :
  2262. m_name(name)
  2263. {
  2264. //Added By Sadullah Nader
  2265. //Initializations inserted
  2266. m_slaveTemplate = NULL;
  2267. //
  2268. }
  2269. // ------------------------------------------------------------------------------------------------
  2270. // ------------------------------------------------------------------------------------------------
  2271. ParticleSystemTemplate::~ParticleSystemTemplate()
  2272. {
  2273. }
  2274. // ------------------------------------------------------------------------------------------------
  2275. /** If returns non-NULL, it is a slave system for use ... the create slaves parameter
  2276. * tells *this* slave system whether or not it should create any slaves itself
  2277. * automatically during its own constructor */
  2278. // ------------------------------------------------------------------------------------------------
  2279. ParticleSystem *ParticleSystemTemplate::createSlaveSystem( Bool createSlaves ) const
  2280. {
  2281. if (m_slaveTemplate == NULL && m_slaveSystemName.isEmpty() == false)
  2282. m_slaveTemplate = TheParticleSystemManager->findTemplate( m_slaveSystemName );
  2283. ParticleSystem *slave = NULL;
  2284. if (m_slaveTemplate)
  2285. slave = TheParticleSystemManager->createParticleSystem( m_slaveTemplate, createSlaves );
  2286. return slave;
  2287. }
  2288. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2289. // ParticleSystemManager //////////////////////////////////////////////////////////////////////////
  2290. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2291. // ------------------------------------------------------------------------------------------------
  2292. // ------------------------------------------------------------------------------------------------
  2293. ParticleSystemManager::ParticleSystemManager( void )
  2294. {
  2295. m_uniqueSystemID = INVALID_PARTICLE_SYSTEM_ID;
  2296. m_onScreenParticleCount = 0;
  2297. m_localPlayerIndex = 0;
  2298. //Added By Sadullah Nader
  2299. //Initializations inserted
  2300. m_lastLogicFrameUpdate = 0;
  2301. m_particleCount = 0;
  2302. m_fieldParticleCount = 0;
  2303. m_particleSystemCount = 0;
  2304. //
  2305. for( Int i = 0; i < NUM_PARTICLE_PRIORITIES; ++i )
  2306. {
  2307. m_allParticlesHead[ i ] = NULL;
  2308. m_allParticlesTail[ i ] = NULL;
  2309. } // end for, i
  2310. }
  2311. // ------------------------------------------------------------------------------------------------
  2312. // ------------------------------------------------------------------------------------------------
  2313. ParticleSystemManager::~ParticleSystemManager()
  2314. {
  2315. reset();
  2316. TemplateMap::iterator begin(m_templateMap.begin());
  2317. TemplateMap::iterator end(m_templateMap.end());
  2318. for (; begin != end; ++begin) {
  2319. (*begin).second->deleteInstance();
  2320. }
  2321. }
  2322. // ------------------------------------------------------------------------------------------------
  2323. /** Initialize the manager */
  2324. // ------------------------------------------------------------------------------------------------
  2325. void ParticleSystemManager::init( void )
  2326. {
  2327. /// Read INI data and build templates
  2328. INI ini;
  2329. ini.load( AsciiString( "Data\\INI\\ParticleSystem.ini" ), INI_LOAD_OVERWRITE, NULL );
  2330. // sanity, our lists must be empty!!
  2331. for( Int i = 0; i < NUM_PARTICLE_PRIORITIES; ++i )
  2332. {
  2333. // sanity
  2334. DEBUG_ASSERTCRASH( m_allParticlesHead[ i ] == NULL, ("INIT: ParticleSystem all particles head[%d] is not NULL!\n", i) );
  2335. DEBUG_ASSERTCRASH( m_allParticlesTail[ i ] == NULL, ("INIT: ParticleSystem all particles tail[%d] is not NULL!\n", i) );
  2336. // just to be clean set them to NULL
  2337. m_allParticlesHead[ i ] = NULL;
  2338. m_allParticlesTail[ i ] = NULL;
  2339. } // end for, i
  2340. }
  2341. // ------------------------------------------------------------------------------------------------
  2342. /** Reset the manager and all particle systems */
  2343. // ------------------------------------------------------------------------------------------------
  2344. void ParticleSystemManager::reset( void )
  2345. {
  2346. while (getParticleSystemCount()) {
  2347. if (m_allParticleSystemList.front()) {
  2348. m_allParticleSystemList.front()->deleteInstance();
  2349. }
  2350. }
  2351. // sanity, our lists must be empty!!
  2352. for( Int i = 0; i < NUM_PARTICLE_PRIORITIES; ++i )
  2353. {
  2354. // sanity
  2355. DEBUG_ASSERTCRASH( m_allParticlesHead[ i ] == NULL, ("RESET: ParticleSystem all particles head[%d] is not NULL!\n", i) );
  2356. DEBUG_ASSERTCRASH( m_allParticlesTail[ i ] == NULL, ("RESET: ParticleSystem all particles tail[%d] is not NULL!\n", i) );
  2357. // just to be clean set them to NULL
  2358. m_allParticlesHead[ i ] = NULL;
  2359. m_allParticlesTail[ i ] = NULL;
  2360. } // end for, i
  2361. m_particleCount = 0;
  2362. m_fieldParticleCount = 0;
  2363. m_particleSystemCount = 0;
  2364. m_uniqueSystemID = INVALID_PARTICLE_SYSTEM_ID;
  2365. m_lastLogicFrameUpdate = -1;
  2366. // leave templates as-is
  2367. }
  2368. // ------------------------------------------------------------------------------------------------
  2369. /** Update all particle systems */
  2370. // ------------------------------------------------------------------------------------------------
  2371. //DECLARE_PERF_TIMER(ParticleSystemManager)
  2372. void ParticleSystemManager::update( void )
  2373. {
  2374. if (m_lastLogicFrameUpdate == TheGameLogic->getFrame()) {
  2375. return;
  2376. }
  2377. // update the last logic frame.
  2378. m_lastLogicFrameUpdate = TheGameLogic->getFrame();
  2379. //USE_PERF_TIMER(ParticleSystemManager)
  2380. ParticleSystem *sys;
  2381. for(ParticleSystemListIt it = m_allParticleSystemList.begin(); it != m_allParticleSystemList.end();)
  2382. {
  2383. sys = (*it);
  2384. if (!sys) {
  2385. continue;
  2386. }
  2387. if (sys->update(m_localPlayerIndex) == false)
  2388. {
  2389. ++it;
  2390. sys->deleteInstance();
  2391. } else {
  2392. ++it;
  2393. }
  2394. }
  2395. }
  2396. // ------------------------------------------------------------------------------------------------
  2397. /** sets the count of the particles on screen after each frame */
  2398. // ------------------------------------------------------------------------------------------------
  2399. void ParticleSystemManager::setOnScreenParticleCount(int count)
  2400. {
  2401. m_onScreenParticleCount = count;
  2402. }
  2403. // ------------------------------------------------------------------------------------------------
  2404. /** Given a file containing particle system properties, create a new instance of it */
  2405. // ------------------------------------------------------------------------------------------------
  2406. ParticleSystem *ParticleSystemManager::createParticleSystem( const ParticleSystemTemplate *sysTemplate, Bool createSlaves )
  2407. {
  2408. // sanity
  2409. if (sysTemplate == NULL)
  2410. return NULL;
  2411. m_uniqueSystemID = (ParticleSystemID)((UnsignedInt)m_uniqueSystemID + 1);
  2412. ParticleSystem *sys = newInstance(ParticleSystem)( sysTemplate, m_uniqueSystemID, createSlaves );
  2413. return sys;
  2414. }
  2415. // ------------------------------------------------------------------------------------------------
  2416. /// given a template, instantiate a particle system attached to the given object, and return its ID
  2417. // ------------------------------------------------------------------------------------------------
  2418. ParticleSystemID ParticleSystemManager::createAttachedParticleSystemID(
  2419. const ParticleSystemTemplate *sysTemplate,
  2420. Object* attachTo,
  2421. Bool createSlaves )
  2422. {
  2423. ParticleSystem* pSystem = TheParticleSystemManager->createParticleSystem(sysTemplate, createSlaves);
  2424. if (pSystem && attachTo)
  2425. pSystem->attachToObject(attachTo);
  2426. return pSystem ? pSystem->getSystemID() : INVALID_PARTICLE_SYSTEM_ID;
  2427. }
  2428. // ------------------------------------------------------------------------------------------------
  2429. /** Find a particle system with the matching system id */
  2430. // ------------------------------------------------------------------------------------------------
  2431. ParticleSystem *ParticleSystemManager::findParticleSystem( ParticleSystemID id )
  2432. {
  2433. if (id == INVALID_PARTICLE_SYSTEM_ID)
  2434. return NULL; // my, that was easy
  2435. ParticleSystem *system = NULL;
  2436. for( ParticleSystemListIt it = m_allParticleSystemList.begin(); it != m_allParticleSystemList.end(); ++it ) {
  2437. system = *it;
  2438. if (!system) {
  2439. continue;
  2440. }
  2441. if( system->getSystemID() == id ) {
  2442. return system;
  2443. }
  2444. }
  2445. return NULL;
  2446. } // end findParticleSystem
  2447. // ------------------------------------------------------------------------------------------------
  2448. /** destroy the particle system with the given id (if it still exists) */
  2449. // ------------------------------------------------------------------------------------------------
  2450. void ParticleSystemManager::destroyParticleSystemByID(ParticleSystemID id)
  2451. {
  2452. ParticleSystem* pSystem = findParticleSystem(id);
  2453. if( pSystem )
  2454. pSystem->destroy();
  2455. }
  2456. // ------------------------------------------------------------------------------------------------
  2457. /** Locate an existing ParticleSystemTemplate */
  2458. // ------------------------------------------------------------------------------------------------
  2459. ParticleSystemTemplate *ParticleSystemManager::findTemplate( const AsciiString &name ) const
  2460. {
  2461. ParticleSystemTemplate *sysTemplate = NULL;
  2462. TemplateMap::const_iterator find(m_templateMap.find(name));
  2463. if (find != m_templateMap.end()) {
  2464. sysTemplate = (*find).second;
  2465. }
  2466. return sysTemplate;
  2467. }
  2468. // ------------------------------------------------------------------------------------------------
  2469. /** Create a new ParticleSystemTemplate */
  2470. // ------------------------------------------------------------------------------------------------
  2471. ParticleSystemTemplate *ParticleSystemManager::newTemplate( const AsciiString &name )
  2472. {
  2473. ParticleSystemTemplate *sysTemplate = findTemplate(name);
  2474. if (sysTemplate == NULL) {
  2475. sysTemplate = newInstance(ParticleSystemTemplate)( name );
  2476. if (! m_templateMap.insert(std::make_pair(name, sysTemplate)).second) {
  2477. sysTemplate->deleteInstance();
  2478. sysTemplate = NULL;
  2479. }
  2480. }
  2481. return sysTemplate;
  2482. }
  2483. // ------------------------------------------------------------------------------------------------
  2484. /** Find a particle system's parent. Should really only be called by TheScriptEngine */
  2485. // ------------------------------------------------------------------------------------------------
  2486. ParticleSystemTemplate *ParticleSystemManager::findParentTemplate( const AsciiString &name, Int parentNum ) const
  2487. {
  2488. if (name.isEmpty()) {
  2489. return NULL;
  2490. }
  2491. TemplateMap::const_iterator begin(m_templateMap.begin());
  2492. TemplateMap::const_iterator end(m_templateMap.end());
  2493. for(; begin != end; ++begin) {
  2494. ParticleSystemTemplate *sysTemplate = (*begin).second;
  2495. if (name.compare(sysTemplate->m_slaveSystemName) == 0) {
  2496. if (! parentNum--) {
  2497. return sysTemplate;
  2498. }
  2499. }
  2500. }
  2501. return NULL;
  2502. }
  2503. // ------------------------------------------------------------------------------------------------
  2504. /** Destroy any particle systems that are attached to this object */
  2505. // ------------------------------------------------------------------------------------------------
  2506. void ParticleSystemManager::destroyAttachedSystems( Object *obj )
  2507. {
  2508. // sanity
  2509. if( obj == NULL )
  2510. return;
  2511. // iterate through all systems
  2512. ParticleSystem *system = NULL;
  2513. for( ParticleSystemListIt it = m_allParticleSystemList.begin();
  2514. it != m_allParticleSystemList.end();
  2515. ++it )
  2516. {
  2517. system = *it;
  2518. if( system == NULL )
  2519. continue;
  2520. if( system->getAttachedObject() == obj->getID() )
  2521. system->destroy();
  2522. }
  2523. }
  2524. // ------------------------------------------------------------------------------------------------
  2525. /** Add a particle to the global particle list. */
  2526. // ------------------------------------------------------------------------------------------------
  2527. void ParticleSystemManager::addParticle( Particle *particleToAdd, ParticlePriorityType priority )
  2528. {
  2529. if (particleToAdd->m_inOverallList)
  2530. return;
  2531. if (!m_allParticlesHead[ priority ])
  2532. {
  2533. m_allParticlesHead[ priority ] = particleToAdd;
  2534. }
  2535. if (m_allParticlesTail[ priority ])
  2536. {
  2537. m_allParticlesTail[ priority ]->m_overallNext = particleToAdd;
  2538. particleToAdd->m_overallPrev = m_allParticlesTail[ priority ];
  2539. }
  2540. else
  2541. {
  2542. particleToAdd->m_overallPrev = NULL;
  2543. }
  2544. m_allParticlesTail[ priority ] = particleToAdd;
  2545. particleToAdd->m_overallNext = NULL;
  2546. particleToAdd->m_inOverallList = TRUE;
  2547. ++m_particleCount;
  2548. }
  2549. // ------------------------------------------------------------------------------------------------
  2550. /** Remove a particle from the global particle list. */
  2551. // ------------------------------------------------------------------------------------------------
  2552. void ParticleSystemManager::removeParticle( Particle *particleToRemove)
  2553. {
  2554. if (!particleToRemove->m_inOverallList)
  2555. return;
  2556. // get priority of the particle we're removing
  2557. ParticlePriorityType priority = particleToRemove->getPriority();
  2558. // remove links from prev & next objs
  2559. if (particleToRemove->m_overallNext)
  2560. particleToRemove->m_overallNext->m_overallPrev = particleToRemove->m_overallPrev;
  2561. if (particleToRemove->m_overallPrev)
  2562. particleToRemove->m_overallPrev->m_overallNext = particleToRemove->m_overallNext;
  2563. // update head & tail if neccessary
  2564. if (particleToRemove == m_allParticlesHead[ priority ])
  2565. m_allParticlesHead[ priority ] = particleToRemove->m_overallNext;
  2566. if (particleToRemove == m_allParticlesTail[ priority ])
  2567. m_allParticlesTail[ priority ] = particleToRemove->m_overallPrev;
  2568. particleToRemove->m_overallNext = particleToRemove->m_overallPrev = NULL;
  2569. particleToRemove->m_inOverallList = FALSE;
  2570. --m_particleCount;
  2571. }
  2572. // ------------------------------------------------------------------------------------------------
  2573. /** Add a particle system to the master particle system list. */
  2574. // ------------------------------------------------------------------------------------------------
  2575. void ParticleSystemManager::friend_addParticleSystem( ParticleSystem *particleSystemToAdd )
  2576. {
  2577. m_allParticleSystemList.push_back(particleSystemToAdd);
  2578. ++m_particleSystemCount;
  2579. }
  2580. // ------------------------------------------------------------------------------------------------
  2581. /** Remove a particle system from the master particle system list. */
  2582. // ------------------------------------------------------------------------------------------------
  2583. void ParticleSystemManager::friend_removeParticleSystem( ParticleSystem *particleSystemToRemove )
  2584. {
  2585. ParticleSystemListIt it = std::find(m_allParticleSystemList.begin(), m_allParticleSystemList.end(), particleSystemToRemove);
  2586. if (it != m_allParticleSystemList.end()) {
  2587. m_allParticleSystemList.erase(it);
  2588. --m_particleSystemCount;
  2589. }
  2590. }
  2591. // ------------------------------------------------------------------------------------------------
  2592. /** Remove the oldest N number of particles from the lowest priority lists first. We will
  2593. * not remove particles from any priorities higher or equal to the priorityCap parameter. */
  2594. // ------------------------------------------------------------------------------------------------
  2595. Int ParticleSystemManager::removeOldestParticles( UnsignedInt count,
  2596. ParticlePriorityType priorityCap )
  2597. {
  2598. Int countToRemove = count;
  2599. while (count-- && getParticleCount())
  2600. {
  2601. for( Int i = PARTICLE_PRIORITY_LOWEST;
  2602. i < priorityCap;
  2603. ++i )
  2604. {
  2605. if( m_allParticlesHead[ i ] )
  2606. {
  2607. m_allParticlesHead[ i ]->deleteInstance();
  2608. break; // exit for
  2609. }
  2610. }
  2611. }
  2612. // return the number of particles actually removed
  2613. return countToRemove - count;
  2614. }
  2615. // ------------------------------------------------------------------------------------------------
  2616. /** Preload particle system textures */
  2617. // ------------------------------------------------------------------------------------------------
  2618. void ParticleSystemManager::preloadAssets( TimeOfDay timeOfDay )
  2619. {
  2620. TemplateMap::iterator begin(m_templateMap.begin());
  2621. TemplateMap::iterator end(m_templateMap.end());
  2622. for (; begin != end; ++begin) {
  2623. const ParticleSystemTemplate *tmplate = (*begin).second;
  2624. if (tmplate->m_particleType == ParticleSystemInfo::PARTICLE &&
  2625. (! tmplate->m_particleTypeName.isEmpty()))
  2626. {
  2627. TheDisplay->preloadTextureAssets(tmplate->m_particleTypeName);
  2628. }
  2629. }
  2630. }
  2631. // ------------------------------------------------------------------------------------------------
  2632. /** CRC */
  2633. // ------------------------------------------------------------------------------------------------
  2634. void ParticleSystemManager::crc( Xfer *xfer )
  2635. {
  2636. } // end crc
  2637. // ------------------------------------------------------------------------------------------------
  2638. /** Xfer method
  2639. * Version Info:
  2640. * 1: Initial version */
  2641. // ------------------------------------------------------------------------------------------------
  2642. void ParticleSystemManager::xfer( Xfer *xfer )
  2643. {
  2644. // version
  2645. XferVersion currentVersion = 1;
  2646. XferVersion version = currentVersion;
  2647. xfer->xferVersion( &version, currentVersion );
  2648. // unique system ID counter
  2649. xfer->xferUser( &m_uniqueSystemID, sizeof( ParticleSystemID ) );
  2650. // count of particle systems in the world
  2651. UnsignedInt systemCount = m_particleSystemCount;
  2652. xfer->xferUnsignedInt( &systemCount );
  2653. // particle systems data
  2654. AsciiString systemName;
  2655. ParticleSystem *system;
  2656. if( xfer->getXferMode() == XFER_SAVE )
  2657. {
  2658. // iterate each particle system
  2659. ParticleSystemListIt it;
  2660. for( it = m_allParticleSystemList.begin(); it != m_allParticleSystemList.end(); ++it )
  2661. {
  2662. systemCount--;
  2663. // get system
  2664. system = *it;
  2665. // ignore destroyed systems and non-saveable systems
  2666. if( system->isDestroyed() == TRUE || system->isSaveable() == FALSE ) {
  2667. AsciiString mtString = "";
  2668. xfer->xferAsciiString(&mtString); // write null string as key for destroyed system.
  2669. continue;
  2670. }
  2671. // write template name
  2672. systemName = system->getTemplate()->getName();
  2673. xfer->xferAsciiString( &systemName );
  2674. // write system data
  2675. xfer->xferSnapshot( system );
  2676. } // end for, it
  2677. DEBUG_ASSERTCRASH(systemCount==0, ("Mismatch in write count."));
  2678. } // end if, save
  2679. else
  2680. {
  2681. const ParticleSystemTemplate *systemTemplate;
  2682. // read each particle system
  2683. for( UnsignedInt i = 0; i < systemCount; ++i )
  2684. {
  2685. // read system name and find template
  2686. xfer->xferAsciiString( &systemName );
  2687. if (systemName.isEmpty()) {
  2688. continue; // destroyed particle system.
  2689. }
  2690. systemTemplate = findTemplate( systemName );
  2691. // sanity
  2692. if( systemTemplate == NULL )
  2693. {
  2694. DEBUG_CRASH(( "ParticleSystemManager::xfer - Unknown particle system template '%s'\n",
  2695. systemName.str() ));
  2696. throw SC_INVALID_DATA;
  2697. } // end if
  2698. // create system
  2699. system = createParticleSystem( systemTemplate, FALSE );
  2700. if( system == NULL )
  2701. {
  2702. DEBUG_CRASH(( "ParticleSystemManager::xfer - Unable to allocate particle system '%s'\n",
  2703. systemName.str() ));
  2704. throw SC_INVALID_DATA;
  2705. } // end if
  2706. // read system data
  2707. xfer->xferSnapshot( system );
  2708. } // end for, i
  2709. } // end else, load
  2710. } // end particleSystemManager
  2711. // ------------------------------------------------------------------------------------------------
  2712. /** Load post process */
  2713. // ------------------------------------------------------------------------------------------------
  2714. void ParticleSystemManager::loadPostProcess( void )
  2715. {
  2716. } // end loadPostProcess
  2717. // ------------------------------------------------------------------------------------------------
  2718. /** Output particle system statistics to the screen
  2719. * @todo Implement a real console (MSB) */
  2720. // ------------------------------------------------------------------------------------------------
  2721. void ParticleSystemDebugDisplay( DebugDisplayInterface *dd, void *, FILE *fp )
  2722. {
  2723. if (!dd)
  2724. return;
  2725. dd->setCursorPos( 0, 0 );
  2726. dd->setRightMargin( 2 );
  2727. dd->printf( "Total Particles: %d\n", TheParticleSystemManager->getParticleCount() );
  2728. dd->printf( "Total Particles (On Screen): %d\n", TheParticleSystemManager->getOnScreenParticleCount());
  2729. dd->printf( "Total Particle Systems: %d\n", TheParticleSystemManager->getParticleSystemCount() );
  2730. ParticleSystemManager::ParticleSystemList list = TheParticleSystemManager->getAllParticleSystems();
  2731. ParticleSystemManager::ParticleSystemList::iterator it;
  2732. std::map<AsciiString, Int> templateMap;
  2733. std::map<AsciiString, Int> templateMapParticleCount;
  2734. std::map<AsciiString, Int>::iterator templateMapIt;
  2735. std::map<AsciiString, Int>::iterator templateMapParticleCountIt;
  2736. for ( it = list.begin(); it != list.end(); ++it )
  2737. {
  2738. AsciiString templateName = (*it)->getTemplate()->getName();
  2739. templateMapIt = templateMap.find(templateName);
  2740. if (templateMapIt == templateMap.end())
  2741. {
  2742. templateMap.insert(std::make_pair(templateName, 1));
  2743. templateMapParticleCount.insert(std::make_pair(templateName, (*it)->getParticleCount()));
  2744. }
  2745. else
  2746. {
  2747. ++templateMapIt->second;
  2748. templateMapParticleCountIt = templateMapParticleCount.find(templateName);
  2749. if (templateMapParticleCountIt != templateMapParticleCount.end())
  2750. templateMapParticleCountIt->second += (*it)->getParticleCount();
  2751. }
  2752. }
  2753. for (templateMapIt = templateMap.begin(); templateMapIt != templateMap.end(); ++templateMapIt)
  2754. {
  2755. templateMapParticleCountIt = templateMapParticleCount.find(templateMapIt->first);
  2756. dd->printf(" %s: %d instances", templateMapIt->first.str(), templateMapIt->second);
  2757. if (templateMapParticleCountIt != templateMapParticleCount.end())
  2758. dd->printf(" (Avg per system %.2f)", INT_TO_REAL(templateMapParticleCountIt->second) / templateMapIt->second);
  2759. dd->printf("\n");
  2760. }
  2761. }
  2762. // ------------------------------------------------------------------------------------------------