fire.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // fire.cpp
  19. // Project: Postal
  20. //
  21. // This module implements the CFire weapon class which is a burning flame
  22. // for several different effects and weapons.
  23. //
  24. //
  25. // History:
  26. // 01/17/97 BRH Started this weapon object.
  27. //
  28. // 01/23/97 BRH Updated the time to GetGameTime rather than using
  29. // real time..
  30. //
  31. // 02/04/97 JMI Changed LoadDib() call to Load() (which now supports
  32. // loading of DIBs).
  33. //
  34. // 02/06/97 BRH Added RAnimSprite animation of the explosion for now.
  35. // We are going to do an Alpha effect on the explosion, so
  36. // there are two animations, one of the image and one of
  37. // the Alpha information stored as a BMP8 animation. When
  38. // the Alpha effect is ready, we will pass a frame from
  39. // each animation to a function to draw it.
  40. //
  41. // 02/06/97 BRH Fixed problem with timer. Since all Explosion objects
  42. // are using the same resource managed animation, they cannot
  43. // use the animation timer, they have to do the timing
  44. // themselves.
  45. //
  46. // 02/07/97 BRH Changed the sprite from CSprite2 to CSpriteAlpha2 for
  47. // the Alpha Blit effect.
  48. //
  49. // 02/09/97 BRH Started the Fire from Explode file since they are
  50. // similar.
  51. //
  52. // 02/10/97 JMI rspReleaseResource() now takes a ptr to a ptr.
  53. //
  54. // 02/11/97 BRH Changed the fire to start on a random frame number
  55. // so if you have many fires, they don't pulsate or all
  56. // burn in sync with each other.
  57. //
  58. // 02/14/97 BRH Changed from using the RAnimSprite to channel data.
  59. //
  60. // 02/17/97 BRH Now uses the resource manager to get the assets and starts
  61. // at a random time interval so the fire will be random again.
  62. //
  63. // 02/17/97 BRH Changed the lifetime to be time based rather than frame
  64. // based which was causing the fire to live on forever
  65. // since being switched from RAnimSprite to RChannel1.
  66. //
  67. // 02/18/97 BRH Now the fire changes to different Alpha channels as it
  68. // burns out during its time to live.
  69. //
  70. // 02/19/97 BRH Checks for collisions and sends messages.
  71. //
  72. // 02/19/97 BRH Added the ability to run both small and large fire
  73. // animations. Change the duration on the alpha layers
  74. // so that the initial alpha channel gets played for 80%
  75. // of the burning time. Also added bThick parameter to startup
  76. // which will start using the 0th Alpha channel which is
  77. // more opaque. If you want more Alpha, set to false which
  78. // will start on the next Alpha level down.
  79. //
  80. // 02/23/97 BRH Added static Preload() funciton which will be called
  81. // before play begins to cache a resource for this object.
  82. //
  83. // 02/24/97 JMI No longer sets the m_type member of the m_sprite b/c it
  84. // is set by m_sprite's constructor.
  85. //
  86. // 02/24/97 BRH Set the default state in ProcessMessages
  87. //
  88. // 02/24/97 BRH Added a timer for checkin collisions so it doesn't have
  89. // to check each time, but it was checking only when changing
  90. // alpha levels which was too long.
  91. //
  92. // 03/05/97 JMI Render()'s mapping from 3D to 2D had a typo (was adding m_dY
  93. // instead of subtracting). Now uses Map3Dto2D().
  94. //
  95. // 03/13/97 JMI Load now takes a version number.
  96. //
  97. // 04/10/97 BRH Updated this to work with the new multi layer attribute
  98. // maps.
  99. //
  100. // 04/14/97 BRH Added CSmash::Item to the collide bits so that the fire
  101. // will send messages to barrels and other items.
  102. //
  103. // 04/21/97 BRH Added Smoke animation to the fire and the ability of the
  104. // fire to change to smoke.
  105. //
  106. // 02/22/97 BRH Adjusted the timer for the smoke effect to eliminate some
  107. // of the final frames so that the smoke wouldn't pulsate
  108. // like it did.
  109. //
  110. // 04/23/97 JMI Changed this item's m_smash bits from CSmash::Item to
  111. // CSmash::Fire.
  112. // Now affects Characters, Miscs, Mines, and Barrels.
  113. //
  114. // 04/24/97 BRH Added static wind direction variable that will get
  115. // adjusted slightly by each new creation of smoke which
  116. // calls WindDirectionUpdate() to randomly vary the wind
  117. // direction.
  118. //
  119. // 04/25/97 BRH Fixed problem with smoke that was created as smoke,
  120. // setting people on fire. Also fixed wall detection
  121. // and added an individual direction variable to each
  122. // instance of smoke that initially copies the wind
  123. // direction and uses it until it hits a wall, then it
  124. // rotates in one direction or the other until it is
  125. // free to move again.
  126. //
  127. // 05/09/97 JMI Update() now moves the smashatorium object when the CFire
  128. // is not Smoke.
  129. //
  130. // 05/29/97 JMI Removed ASSERT on m_pRealm->m_pAttribMap which no longer
  131. // exists.
  132. //
  133. // 06/11/97 BRH Pass along the m_u16ShooterID value in the Burn message.
  134. //
  135. // 06/15/97 BRH Fixed Smoke going past animation by 1 frame.
  136. //
  137. // 06/16/97 BRH Fixed smoke init of static wind direction. Now it
  138. // inits the wind direction on class load so that it doesn't
  139. // cause problems for the demo mode.
  140. //
  141. // 06/17/97 MJR Same as previous one for wind velocity.
  142. //
  143. // MJR Moved resetting of statics to Preload(), since in most
  144. // cases, fire or smoke are not Load()'ed.
  145. //
  146. // 06/18/97 BRH Changed over to using GetRandom()
  147. //
  148. // 06/26/97 BRH Added CSmash::AlmostDead to the include bits for fire so
  149. // that writhing guys can be killed by fire.
  150. //
  151. // 07/01/97 BRH Added small smoke animation.
  152. //
  153. // 07/04/97 BRH Added an auto alpha blend on the small smoke for the
  154. // rocket trails so they can blend into alpha based on
  155. // their time to live. May need to disable the
  156. // alpha channel for it to work correctly.
  157. //
  158. // 07/08/97 JMI Fixed Render() to distribute the homogeneous alpha level
  159. // better. Still needs tuning.
  160. //
  161. // 07/09/97 JMI Now uses m_pRealm->Make2dResPath() to get the fullpath
  162. // for 2D image components.
  163. //
  164. // 07/09/97 JMI Changed Preload() to take a pointer to the calling realm
  165. // as a parameter.
  166. //
  167. // 07/10/97 JMI Now uses alpha mask and level for animation.
  168. //
  169. // 07/13/97 BRH Changed the animations to use only 1 alpha mask and change
  170. // the alpha level based on time.
  171. //
  172. // 07/20/97 JMI Added some ASSERTs.
  173. //
  174. // 07/23/97 BRH Changed small fires to create small smokes rather than
  175. // large which slows down the game quite a bit.
  176. //
  177. // 07/27/97 JMI Changed to use Z position (i.e., X/Z plane) instead of
  178. // Y2 position (i.e., viewing plane) position for draw
  179. // priority.
  180. //
  181. // 08/11/97 BRH If alpha blending is turned off, as a performance option,
  182. // then don't even blit the smoke since without the alpha
  183. // effect, you can't see through it at all.
  184. //
  185. // 08/20/97 JMI Now does a range check on m_sCurrentAlphaLevel after
  186. // decrementing.
  187. //
  188. // 09/02/97 JMI Added m_u16FireStarterID. This is used for a special case
  189. // when the starter of the fire is not the thing using the
  190. // fire as a weapon (e.g., when a guy catches fire he can
  191. // use the fire on other people by running into them causing
  192. // them to catch on fire; however, if his own fire kills him
  193. // it is to the creator of the fire's credit that he dies).
  194. //
  195. ////////////////////////////////////////////////////////////////////////////////
  196. #define FIRE_CPP
  197. #include "RSPiX.h"
  198. #include <math.h>
  199. #include "fire.h"
  200. #include "dude.h"
  201. #include "game.h"
  202. #include "reality.h"
  203. ////////////////////////////////////////////////////////////////////////////////
  204. // Macros/types/etc.
  205. ////////////////////////////////////////////////////////////////////////////////
  206. #define AA_FILE "fire.aan"
  207. #define LARGE_FILE "fire.aan"
  208. #define SMALL_FILE "smallfire.aan"
  209. #define SMOKE_FILE "smoke.aan"
  210. #define SMALL_SMOKE_FILE "tinysmoke.aan"
  211. #define INIT_WIND_DIR 30
  212. #define INIT_WIND_VEL 30
  213. #define MAX_ALPHA 200 // Used for smoke trails
  214. #define THICK_ALPHA 255 // Start alpha level for thick fire
  215. #define THIN_ALPHA 200 // Start alpha level for thin fire
  216. #define DIEDOWN_ALPHA 100 // Point at which it looks like its dying down
  217. #define SMOLDER_ALPHA 30 // Point at which it is too weak to burn anyone
  218. #define BRIGHT_PERCENT 0.80 // Amount of the time it should be more opaque
  219. ////////////////////////////////////////////////////////////////////////////////
  220. // Variables/data
  221. ////////////////////////////////////////////////////////////////////////////////
  222. // Let this auto-init to 0
  223. short CFire::ms_sFileCount;
  224. short CFire::ms_sLargeRadius = 20;
  225. short CFire::ms_sSmallRadius = 8;
  226. short CFire::ms_sWindDirection = INIT_WIND_DIR; // Start wind in this direction
  227. long CFire::ms_lCollisionTime = 250; // Check for collisions this often
  228. long CFire::ms_lSmokeTime = 10000; // Time to let smoke run
  229. double CFire::ms_dWindVelocity = INIT_WIND_VEL; // Pixels per second drift due to wind
  230. ////////////////////////////////////////////////////////////////////////////////
  231. // Load object (should call base class version!)
  232. ////////////////////////////////////////////////////////////////////////////////
  233. short CFire::Load( // Returns 0 if successfull, non-zero otherwise
  234. RFile* pFile, // In: File to load from
  235. bool bEditMode, // In: True for edit mode, false otherwise
  236. short sFileCount, // In: File count (unique per file, never 0)
  237. ULONG ulFileVersion) // In: Version of file format to load.
  238. {
  239. short sResult = CThing::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  240. if (sResult == 0)
  241. {
  242. // Load common data just once per file (not with each object)
  243. if (ms_sFileCount != sFileCount)
  244. {
  245. ms_sFileCount = sFileCount;
  246. // Init the static wind direction and velocity when the class loads.
  247. ms_sWindDirection = INIT_WIND_DIR;
  248. ms_dWindVelocity = INIT_WIND_VEL;
  249. // Load static data.
  250. switch (ulFileVersion)
  251. {
  252. default:
  253. case 1:
  254. break;
  255. }
  256. }
  257. // Load instance data.
  258. switch (ulFileVersion)
  259. {
  260. default:
  261. case 1:
  262. pFile->Read(&m_eFireAnim);
  263. break;
  264. }
  265. // Make sure there were no file errors or format errors . . .
  266. if (!pFile->Error() && sResult == 0)
  267. {
  268. // Get resources
  269. sResult = GetResources();
  270. }
  271. else
  272. {
  273. sResult = -1;
  274. TRACE("CFire::Load(): Error reading from file!\n");
  275. }
  276. }
  277. else
  278. {
  279. TRACE("CFire::Load(): CThing::Load() failed.\n");
  280. }
  281. return sResult;
  282. }
  283. ////////////////////////////////////////////////////////////////////////////////
  284. // Save object (should call base class version!)
  285. ////////////////////////////////////////////////////////////////////////////////
  286. short CFire::Save( // Returns 0 if successfull, non-zero otherwise
  287. RFile* pFile, // In: File to save to
  288. short sFileCount) // In: File count (unique per file, never 0)
  289. {
  290. short sResult = CThing::Save(pFile, sFileCount);
  291. if (sResult == 0)
  292. {
  293. // Save common data just once per file (not with each object)
  294. if (ms_sFileCount != sFileCount)
  295. {
  296. ms_sFileCount = sFileCount;
  297. // Save static data
  298. }
  299. pFile->Write(&m_eFireAnim);
  300. }
  301. else
  302. {
  303. TRACE("CFire::Save(): CThing::Save() failed.\n");
  304. }
  305. return sResult;
  306. }
  307. ////////////////////////////////////////////////////////////////////////////////
  308. // Startup object
  309. ////////////////////////////////////////////////////////////////////////////////
  310. short CFire::Startup(void) // Returns 0 if successfull, non-zero otherwise
  311. {
  312. return Init();
  313. }
  314. ////////////////////////////////////////////////////////////////////////////////
  315. // Shutdown object
  316. ////////////////////////////////////////////////////////////////////////////////
  317. short CFire::Shutdown(void) // Returns 0 if successfull, non-zero otherwise
  318. {
  319. return 0;
  320. }
  321. ////////////////////////////////////////////////////////////////////////////////
  322. // Suspend object
  323. ////////////////////////////////////////////////////////////////////////////////
  324. void CFire::Suspend(void)
  325. {
  326. m_sSuspend++;
  327. }
  328. ////////////////////////////////////////////////////////////////////////////////
  329. // Resume object
  330. ////////////////////////////////////////////////////////////////////////////////
  331. void CFire::Resume(void)
  332. {
  333. m_sSuspend--;
  334. // If we're actually going to start updating again, we need to reset
  335. // the time so as to ignore any time that passed while we were suspended.
  336. // This method is far from precise, but I'm hoping it's good enough.
  337. if (m_sSuspend == 0)
  338. m_lPrevTime = m_pRealm->m_time.GetGameTime();
  339. }
  340. ////////////////////////////////////////////////////////////////////////////////
  341. // Update object
  342. ////////////////////////////////////////////////////////////////////////////////
  343. void CFire::Update(void)
  344. {
  345. long lThisTime;
  346. double dSeconds;
  347. double dDistance;
  348. double dNewX;
  349. double dNewZ;
  350. if (!m_sSuspend)
  351. {
  352. // See if we killed ourselves
  353. if (ProcessMessages() == State_Deleted)
  354. return;
  355. if (m_lTimer < m_lBurnUntil)
  356. {
  357. lThisTime = m_pRealm->m_time.GetGameTime();
  358. m_lTimer += lThisTime - m_lPrevTime;
  359. // See if its time to change to the next alpha channel
  360. if (m_lTimer > m_lCurrentAlphaTimeout)
  361. {
  362. m_sCurrentAlphaLevel--;
  363. // Range check.
  364. if (m_sCurrentAlphaLevel < 0)
  365. m_sCurrentAlphaLevel = 0;
  366. else if (m_sCurrentAlphaLevel > 255)
  367. m_sCurrentAlphaLevel = 255;
  368. if (m_lTimer < m_lAlphaBreakPoint)
  369. m_lCurrentAlphaTimeout += m_lBrightAlphaInterval;
  370. else
  371. m_lCurrentAlphaTimeout += m_lDimAlphaInterval;
  372. }
  373. if (lThisTime > m_lCollisionTimer)
  374. {
  375. // If the fire is not smoldering out, then it has the ability
  376. // to set other things on fire and should check collisions
  377. // to see which things it should tell to burn.
  378. if (m_bSendMessages && m_sCurrentAlphaLevel > SMOLDER_ALPHA && m_eFireAnim != Smoke && m_eFireAnim != SmallSmoke)
  379. {
  380. CSmash* pSmashed = NULL;
  381. GameMessage msg;
  382. msg.msg_Burn.eType = typeBurn;
  383. msg.msg_Burn.sPriority = 0;
  384. msg.msg_Burn.sDamage = 10;
  385. msg.msg_Burn.u16ShooterID = m_u16ShooterID;
  386. m_pRealm->m_smashatorium.QuickCheckReset(&m_smash, m_u32CollideIncludeBits,
  387. m_u32CollideDontcareBits,
  388. m_u32CollideExcludeBits);
  389. while (m_pRealm->m_smashatorium.QuickCheckNext(&pSmashed))
  390. {
  391. // Default to the standard case where credit is given to the
  392. // shooter.
  393. msg.msg_Burn.u16ShooterID = m_u16ShooterID;
  394. if ((m_bIsBurningDude) && (pSmashed->m_pThing->GetClassID() != CDudeID))
  395. UnlockAchievement(ACHIEVEMENT_TOUCH_SOMEONE_WHILE_BURNING);
  396. // If the fire starter ID is set . . .
  397. if (m_u16FireStarterID != CIdBank::IdNil)
  398. {
  399. // If this is the shooter . . .
  400. if (pSmashed->m_pThing->GetInstanceID() == m_u16ShooterID)
  401. {
  402. // The shooter is damaged by his own fire with credit
  403. // given to the fire starter.
  404. msg.msg_Burn.u16ShooterID = m_u16FireStarterID;
  405. }
  406. }
  407. // Burn.
  408. SendThingMessage(&msg, pSmashed->m_pThing);
  409. }
  410. }
  411. // Reset collision timer for next time
  412. m_lCollisionTimer = lThisTime + ms_lCollisionTime;
  413. }
  414. // If this is smoke, make it drift in the wind direction
  415. if (m_eFireAnim == Smoke || m_eFireAnim == SmallSmoke)
  416. {
  417. // Update position using wind direction and velocity
  418. dSeconds = ((double) lThisTime - (double) m_lPrevTime) / 1000.0;
  419. // Apply internal velocity.
  420. dDistance = ms_dWindVelocity * dSeconds;
  421. dNewX = m_dX + COSQ[(short) m_sRot] * dDistance;
  422. dNewZ = m_dZ - SINQ[(short) m_sRot] * dDistance;
  423. // Check attribute map for walls, and if you hit a wall,
  424. // set the timer so you will die off next time around.
  425. short sHeight = m_pRealm->GetHeight((short) dNewX, (short) dNewZ);
  426. // If it hits a wall taller than itself, then it will rotate in the
  427. // predetermined direction until it is free to move.
  428. if ((short) m_dY < sHeight)
  429. {
  430. if (m_bTurnRight)
  431. m_sRot = rspMod360(m_sRot - 20);
  432. else
  433. m_sRot = rspMod360(m_sRot + 20);
  434. }
  435. else
  436. // else it is ok, so update its new position
  437. {
  438. m_dX = dNewX;
  439. m_dZ = dNewZ;
  440. }
  441. }
  442. else
  443. {
  444. // Update our smashatorium location.
  445. m_smash.m_sphere.sphere.X = m_dX;
  446. m_smash.m_sphere.sphere.Y = m_dY;
  447. m_smash.m_sphere.sphere.Z = m_dZ;
  448. // Update the smash.
  449. m_pRealm->m_smashatorium.Update(&m_smash);
  450. }
  451. m_lPrevTime = lThisTime;
  452. }
  453. else
  454. {
  455. // If its done smoking, then delete it
  456. if (m_eFireAnim == Smoke || m_eFireAnim == SmallSmoke)
  457. {
  458. delete this;
  459. }
  460. // Else change the fire to smoke
  461. else
  462. {
  463. if (Smokeout() != SUCCESS)
  464. delete this;
  465. }
  466. }
  467. }
  468. }
  469. ////////////////////////////////////////////////////////////////////////////////
  470. // Render object
  471. ////////////////////////////////////////////////////////////////////////////////
  472. void CFire::Render(void)
  473. {
  474. CAlphaAnim* pAnim;
  475. if (m_eFireAnim == Smoke || m_eFireAnim == SmallSmoke)
  476. {
  477. pAnim = (CAlphaAnim*) m_pAnimChannel->GetAtTime(m_lTimer);
  478. // For a performance gain, don't blit the smoke at all if alpha
  479. // blending is turned off - its impossible to see through when
  480. // alpha blending is off anyway.
  481. if (g_GameSettings.m_sAlphaBlend)
  482. m_sprite.m_sInFlags = 0;
  483. else
  484. m_sprite.m_sInFlags = CSprite::InHidden;
  485. }
  486. else
  487. {
  488. pAnim = (CAlphaAnim*) m_pAnimChannel->GetAtTime(m_lTimer % m_pAnimChannel->TotalTime());
  489. }
  490. if (pAnim)
  491. {
  492. // Map from 3d to 2d coords
  493. Map3Dto2D(m_dX, m_dY, m_dZ, &(m_sprite.m_sX2), &(m_sprite.m_sY2) );
  494. // Offset by animations 2D offsets.
  495. m_sprite.m_sX2 += pAnim->m_sX;
  496. m_sprite.m_sY2 += pAnim->m_sY;
  497. // Priority is based on our Z position.
  498. m_sprite.m_sPriority = m_dZ;
  499. // Layer should be based on info we get from attribute map.
  500. m_sprite.m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer((short) m_dX, (short) m_dZ));
  501. // Copy the color info and the alpha channel to the Alpha Sprite
  502. m_sprite.m_pImage = &(pAnim->m_imColor);
  503. // If its the tiny smoke (for trails)
  504. // Do the alpha based on the time to live.
  505. if (m_eFireAnim == SmallSmoke)
  506. {
  507. // Set the alpha level so it gets more translucent over time.
  508. m_sprite.m_sAlphaLevel = MAX_ALPHA - (MAX_ALPHA * (m_lTimer - m_lStartTime) ) / m_lTimeToLive ;
  509. // Do a range check.
  510. if (m_sprite.m_sAlphaLevel < 0)
  511. m_sprite.m_sAlphaLevel = 0;
  512. else if (m_sprite.m_sAlphaLevel > MAX_ALPHA)
  513. m_sprite.m_sAlphaLevel = MAX_ALPHA;
  514. }
  515. else
  516. {
  517. m_sprite.m_sAlphaLevel = m_sCurrentAlphaLevel;
  518. }
  519. // Now there is only one alpha mask
  520. m_sprite.m_pimAlpha = &(pAnim->m_pimAlphaArray[0]);
  521. ASSERT(m_sprite.m_sAlphaLevel <= 255);
  522. ASSERT(m_sprite.m_sAlphaLevel >= 0);
  523. // Update sprite in scene
  524. m_pRealm->m_scene.UpdateSprite(&m_sprite);
  525. }
  526. }
  527. ////////////////////////////////////////////////////////////////////////////////
  528. // Setup
  529. ////////////////////////////////////////////////////////////////////////////////
  530. short CFire::Setup( // Returns 0 if successfull, non-zero otherwise
  531. short sX, // In: New x coord
  532. short sY, // In: New y coord
  533. short sZ, // In: New z coord
  534. long lTimeToLive, // In: Number of milliseconds to burn, default 1sec
  535. bool bThick, // In: Use thick fire (more opaque) default = true
  536. FireAnim eAnimType) // In: Animation type to use default = LargeFire
  537. {
  538. short sResult = 0;
  539. // Use specified position
  540. m_dX = (double)sX;
  541. m_dY = (double)sY;
  542. m_dZ = (double)sZ;
  543. m_lPrevTime = m_pRealm->m_time.GetGameTime();
  544. m_lCollisionTimer = m_lPrevTime + ms_lCollisionTime;
  545. m_eFireAnim = eAnimType;
  546. m_lTimeToLive = lTimeToLive;
  547. if (bThick)
  548. m_sCurrentAlphaLevel = THICK_ALPHA;
  549. else
  550. m_sCurrentAlphaLevel = THIN_ALPHA;
  551. // Load resources
  552. sResult = GetResources();
  553. if (sResult == SUCCESS)
  554. sResult = Init();
  555. m_sRot = ms_sWindDirection;
  556. m_bTurnRight = (GetRandom() & 0x01);
  557. return sResult;
  558. }
  559. ////////////////////////////////////////////////////////////////////////////////
  560. // Init
  561. ////////////////////////////////////////////////////////////////////////////////
  562. short CFire::Init(void)
  563. {
  564. short sResult = SUCCESS;
  565. CAlphaAnim* pAnim = NULL;
  566. if (m_pAnimChannel != NULL)
  567. {
  568. m_lTimer = GetRandom() % m_pAnimChannel->TotalTime();
  569. m_lStartTime = m_lTimer;
  570. m_lBurnUntil = m_lTimer + m_lTimeToLive;
  571. m_lAlphaBreakPoint = m_lTimer + (m_lTimeToLive * BRIGHT_PERCENT);
  572. pAnim = (CAlphaAnim*) m_pAnimChannel->GetAtTime(0);
  573. ASSERT(pAnim != NULL);
  574. m_lBrightAlphaInterval = (m_lTimeToLive * BRIGHT_PERCENT) / MAX(1, m_sCurrentAlphaLevel - DIEDOWN_ALPHA);
  575. m_lDimAlphaInterval = (m_lTimeToLive * (100.0 - BRIGHT_PERCENT)) / MAX(1, DIEDOWN_ALPHA);
  576. m_lCurrentAlphaTimeout = m_lTimer + m_lBrightAlphaInterval;
  577. m_sTotalAlphaChannels = 1;
  578. }
  579. switch (m_eFireAnim)
  580. {
  581. case LargeFire:
  582. // Update sphere
  583. m_smash.m_sphere.sphere.X = m_dX;
  584. m_smash.m_sphere.sphere.Y = m_dY;
  585. m_smash.m_sphere.sphere.Z = m_dZ;
  586. m_smash.m_sphere.sphere.lRadius = ms_sLargeRadius;
  587. m_smash.m_bits = CSmash::Fire;
  588. m_smash.m_pThing = this;
  589. // Update the smash
  590. m_pRealm->m_smashatorium.Update(&m_smash);
  591. break;
  592. case SmallFire:
  593. // Update sphere
  594. m_smash.m_sphere.sphere.X = m_dX;
  595. m_smash.m_sphere.sphere.Y = m_dY;
  596. m_smash.m_sphere.sphere.Z = m_dZ;
  597. m_smash.m_sphere.sphere.lRadius = ms_sSmallRadius;
  598. m_smash.m_bits = CSmash::Fire;
  599. m_smash.m_pThing = this;
  600. // Update the smash
  601. m_pRealm->m_smashatorium.Update(&m_smash);
  602. break;
  603. case Smoke:
  604. m_smash.m_pThing = NULL;
  605. m_bSendMessages = false;
  606. break;
  607. case SmallSmoke:
  608. m_smash.m_pThing = NULL;
  609. m_bSendMessages = false;
  610. m_lStartTime = m_lTimer = GetRandom() % m_pAnimChannel->TotalTime() / 3;
  611. m_lBurnUntil = m_lTimer + m_lTimeToLive;
  612. break;
  613. }
  614. // Set the collision bits
  615. m_u32CollideIncludeBits = CSmash::Character | CSmash::Barrel | CSmash::Mine | CSmash::Misc | CSmash::AlmostDead;
  616. m_u32CollideDontcareBits = CSmash::Good | CSmash::Bad;
  617. m_u32CollideExcludeBits = 0;
  618. return sResult;
  619. }
  620. ////////////////////////////////////////////////////////////////////////////////
  621. // Smokeout - Change from fire to smoke
  622. ////////////////////////////////////////////////////////////////////////////////
  623. short CFire::Smokeout(void)
  624. {
  625. short sResult = SUCCESS;
  626. // Modify the wind direction slightly
  627. WindDirectionUpdate();
  628. // Remove smash from the smashatorium if it was being used
  629. if (m_smash.m_pThing)
  630. m_pRealm->m_smashatorium.Remove(&m_smash);
  631. m_bSendMessages = false;
  632. // Release the fire animation and get the smoke animation
  633. FreeResources();
  634. if (m_eFireAnim == SmallFire)
  635. m_eFireAnim = SmallSmoke;
  636. else
  637. m_eFireAnim = Smoke;
  638. sResult = GetResources();
  639. // Reset timers
  640. CAlphaAnim* pAnim = NULL;
  641. if (m_pAnimChannel != NULL)
  642. {
  643. // Reset alpha level
  644. m_sCurrentAlphaLevel = THICK_ALPHA;
  645. pAnim = (CAlphaAnim*) m_pAnimChannel->GetAtTime(0);
  646. ASSERT(pAnim != NULL);
  647. // m_lTimeToLive = m_pAnimChannel->TotalTime();
  648. // use same time to live as the original
  649. m_lStartTime = m_lTimer = 0;
  650. m_lBurnUntil = m_lTimer + m_lTimeToLive;
  651. m_lAlphaBreakPoint = m_lTimer + (m_lTimeToLive * BRIGHT_PERCENT);
  652. m_lBrightAlphaInterval = (m_lTimeToLive * BRIGHT_PERCENT) / MAX(1, m_sCurrentAlphaLevel - DIEDOWN_ALPHA);
  653. m_lDimAlphaInterval = (m_lTimeToLive * (1.0 - BRIGHT_PERCENT)) / MAX(1, DIEDOWN_ALPHA);
  654. m_lCurrentAlphaTimeout = m_lTimer + m_lBrightAlphaInterval;
  655. m_sTotalAlphaChannels = 1;
  656. }
  657. return sResult;
  658. }
  659. ////////////////////////////////////////////////////////////////////////////////
  660. // Called by editor to init new object at specified position
  661. ////////////////////////////////////////////////////////////////////////////////
  662. short CFire::EditNew( // Returns 0 if successfull, non-zero otherwise
  663. short sX, // In: New x coord
  664. short sY, // In: New y coord
  665. short sZ) // In: New z coord
  666. {
  667. short sResult = 0;
  668. // Use specified position
  669. m_dX = (double)sX;
  670. m_dY = (double)sY;
  671. m_dZ = (double)sZ;
  672. m_lTimer = GetRandom(); //m_pRealm->m_time.GetGameTime() + 1000;
  673. m_lPrevTime = m_pRealm->m_time.GetGameTime();
  674. // Load resources
  675. sResult = GetResources();
  676. return sResult;
  677. }
  678. ////////////////////////////////////////////////////////////////////////////////
  679. // Called by editor to modify object
  680. ////////////////////////////////////////////////////////////////////////////////
  681. short CFire::EditModify(void)
  682. {
  683. return 0;
  684. }
  685. ////////////////////////////////////////////////////////////////////////////////
  686. // Called by editor to move object to specified position
  687. ////////////////////////////////////////////////////////////////////////////////
  688. short CFire::EditMove( // Returns 0 if successfull, non-zero otherwise
  689. short sX, // In: New x coord
  690. short sY, // In: New y coord
  691. short sZ) // In: New z coord
  692. {
  693. m_dX = (double)sX;
  694. m_dY = (double)sY;
  695. m_dZ = (double)sZ;
  696. return 0;
  697. }
  698. ////////////////////////////////////////////////////////////////////////////////
  699. // Called by editor to update object
  700. ////////////////////////////////////////////////////////////////////////////////
  701. void CFire::EditUpdate(void)
  702. {
  703. }
  704. ////////////////////////////////////////////////////////////////////////////////
  705. // Called by editor to render object
  706. ////////////////////////////////////////////////////////////////////////////////
  707. void CFire::EditRender(void)
  708. {
  709. // In some cases, object's might need to do a special-case render in edit
  710. // mode because Startup() isn't called. In this case it doesn't matter, so
  711. // we can call the normal Render().
  712. Render();
  713. }
  714. ////////////////////////////////////////////////////////////////////////////////
  715. // Get all required resources
  716. ////////////////////////////////////////////////////////////////////////////////
  717. short CFire::GetResources(void) // Returns 0 if successfull, non-zero otherwise
  718. {
  719. short sResult = SUCCESS;
  720. switch (m_eFireAnim)
  721. {
  722. case LargeFire:
  723. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(LARGE_FILE), &m_pAnimChannel, RFile::LittleEndian);
  724. break;
  725. case SmallFire:
  726. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(SMALL_FILE), &m_pAnimChannel, RFile::LittleEndian);
  727. break;
  728. case Smoke:
  729. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(SMOKE_FILE), &m_pAnimChannel, RFile::LittleEndian);
  730. break;
  731. case SmallSmoke:
  732. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(SMALL_SMOKE_FILE), &m_pAnimChannel, RFile::LittleEndian);
  733. break;
  734. }
  735. if (sResult != 0)
  736. TRACE("CFire::GetResources - Error getting fire animation resource\n");
  737. return sResult;
  738. }
  739. ////////////////////////////////////////////////////////////////////////////////
  740. // Free all resources
  741. ////////////////////////////////////////////////////////////////////////////////
  742. short CFire::FreeResources(void) // Returns 0 if successfull, non-zero otherwise
  743. {
  744. short sResult = 0;
  745. rspReleaseResource(&g_resmgrGame, &m_pAnimChannel);
  746. return sResult;
  747. }
  748. ////////////////////////////////////////////////////////////////////////////////
  749. // Preload - basically trick the resource manager into caching resources for fire
  750. // animations before play begins so that when a fire is set for
  751. // the first time, there won't be a delay while it loads.
  752. ////////////////////////////////////////////////////////////////////////////////
  753. short CFire::Preload(
  754. CRealm* prealm) // In: Calling realm.
  755. {
  756. // Init the static wind direction and velocity when the class loads.
  757. ms_sWindDirection = INIT_WIND_DIR;
  758. ms_dWindVelocity = INIT_WIND_VEL;
  759. ChannelAA* pRes;
  760. short sResult = rspGetResource(&g_resmgrGame, prealm->Make2dResPath(LARGE_FILE), &pRes, RFile::LittleEndian);
  761. rspReleaseResource(&g_resmgrGame, &pRes);
  762. sResult |= rspGetResource(&g_resmgrGame, prealm->Make2dResPath(SMALL_FILE), &pRes, RFile::LittleEndian);
  763. rspReleaseResource(&g_resmgrGame, &pRes);
  764. sResult |= rspGetResource(&g_resmgrGame, prealm->Make2dResPath(SMOKE_FILE), &pRes, RFile::LittleEndian);
  765. rspReleaseResource(&g_resmgrGame, &pRes);
  766. sResult |= rspGetResource(&g_resmgrGame, prealm->Make2dResPath(SMALL_SMOKE_FILE), &pRes, RFile::LittleEndian);
  767. rspReleaseResource(&g_resmgrGame, &pRes);
  768. return sResult;
  769. }
  770. ////////////////////////////////////////////////////////////////////////////////
  771. // ProcessMessages
  772. ////////////////////////////////////////////////////////////////////////////////
  773. CFire::CFireState CFire::ProcessMessages(void)
  774. {
  775. CFireState eNewState = State_Idle;
  776. GameMessage msg;
  777. if (m_MessageQueue.DeQ(&msg) == true)
  778. {
  779. switch(msg.msg_Generic.eType)
  780. {
  781. case typeObjectDelete:
  782. m_MessageQueue.Empty();
  783. delete this;
  784. return CFire::State_Deleted;
  785. break;
  786. }
  787. }
  788. // Dump the rest of the messages
  789. m_MessageQueue.Empty();
  790. return eNewState;
  791. }
  792. ////////////////////////////////////////////////////////////////////////////////
  793. // EOF
  794. ////////////////////////////////////////////////////////////////////////////////