firebomb.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. // firebomb.cpp
  19. // Project: Postal
  20. //
  21. // This module implements the CFirebomb weapon class which is a hand
  22. // thrown grenade weapon that explodes into a ring of fire..
  23. //
  24. //
  25. // History:
  26. // 01/17/97 BRH Started this weapon object.
  27. //
  28. // 02/04/97 JMI Changed LoadDib() call to Load() (which now supports
  29. // loading of DIBs).
  30. //
  31. // 02/11/97 BRH Started the firebomb object from the grenade file
  32. //
  33. // 02/14/97 BRH Now uses resource manager to get the image.
  34. //
  35. // 02/19/97 BRH Changed the main part of the weapon to 3D and left
  36. // the fire fragments as 2D. Also added some randomness
  37. // to the pattern and increased the burn times.
  38. //
  39. // 02/19/97 BRH Added ProcessMessages function to check for
  40. // ObjectDeleted messages.
  41. //
  42. // 02/21/97 BRH Changed fragments to be invisible controllers of the
  43. // small fire animation rather than an 2d sprite. This
  44. // sort of hides the bounce effect which is bad so we may
  45. // want to put it back. Also uses the small fire for
  46. // the fire fragments.
  47. //
  48. // 02/23/97 BRH Changed the coordinate system to x,-z
  49. //
  50. // 02/23/97 BRH Added a static Preload() function which will be called
  51. // before play begins to cache the resources needed for this
  52. // object.
  53. //
  54. // 02/24/97 JMI No longer sets the m_type member of the m_sprite b/c it
  55. // is set by m_sprite's constructor.
  56. //
  57. // 02/24/97 BRH Changed to using reality.h motion templates. Using new
  58. // algorithm for detecting ground and walls. Made the
  59. // center fire more than 1 sprite and changed to thin
  60. // fire. Added sound effect for initial impact.
  61. //
  62. // 02/28/97 BRH Derived this from the CWeapon base class.
  63. //
  64. // 03/03/97 JMI Changed reference to CGrenade::State_Deleted to
  65. // CWeapon::State_Deleted.
  66. //
  67. // 03/06/97 BRH Changed to using ID's for keeping track of the file
  68. // and gettting the pointer from the ID each time.
  69. //
  70. // 03/13/97 JMI Load()s now take a version number.
  71. //
  72. // 03/21/97 BRH Changed this to ignore ATTRIBUTE_NOT_WALKABLE so that
  73. // the cocktails won't bounce off of the edge of the world.
  74. //
  75. // 04/10/97 BRH Updated this to work with the new multi layer attribute
  76. // maps.
  77. //
  78. // 05/04/97 BRH Took out an old unused reference to an STL iterator.
  79. //
  80. // 05/13/97 JMI CFireBomb was using a formula to compute the direction of
  81. // 8 firefrags such that one appeared in a random position
  82. // in all of 8 octants. The problem was the formula subtract-
  83. // ed 25 which, in the case of the first octant, if the random
  84. // position 24 or less were chosen, would result in a negative
  85. // value. But, to simply remove this could cause values 360
  86. // or over since the random portion was based on 50.0 and not
  87. // the size of an octant. So I removed the -25 and changed the
  88. // 50.0 to (360/8) which, as far as I can tell, makes the
  89. // inclusive extents of the formula [0..359].
  90. //
  91. // 05/29/97 JMI Removed ASSERT on m_pRealm->m_pAttribMap which no longer
  92. // exists.
  93. //
  94. // 06/03/97 BRH Changed the cocktails so they don't bounce off of
  95. // walls, they just fall to the ground and break.
  96. //
  97. // 06/11/97 BRH Added shooter ID passing to the fires that it creates.
  98. //
  99. // 06/12/97 JMI Now handles State_Hide by setting m_sprite's InHidden flag.
  100. //
  101. // 06/12/97 BRH Fixed order of passing the shooter ID.
  102. //
  103. // 06/16/97 BRH Fixed starting condition in not walkable area.
  104. //
  105. // 06/18/97 BRH Changed over to using GetRandom()
  106. //
  107. // 06/25/97 BRH Added use of base class 2D shadow on the ground, but loaded
  108. // a smaller shadow resource.
  109. //
  110. // 06/30/97 BRH Added cache of sound effects in Preload function.
  111. //
  112. // 07/09/97 JMI Now uses m_pRealm->Make2dResPath() to get the fullpath
  113. // for 2D image components.
  114. //
  115. // 07/09/97 JMI Changed Preload() to take a pointer to the calling realm
  116. // as a parameter.
  117. //
  118. // 07/18/97 JMI Got rid of bogus immitation PlaySample functions.
  119. // Now there is one PlaySample() function. Also, you now
  120. // MUST specify a category and you don't have to specify a
  121. // SoundInstance ptr to specify a volume.
  122. //
  123. // 07/27/97 JMI Changed to use Z position (i.e., X/Z plane) instead of
  124. // Y2 position (i.e., viewing plane) position for draw
  125. // priority.
  126. //
  127. // 07/30/97 JMI Same old delete error showed up on Alpha.
  128. // ProcessMessages() was deleting the firebomb on a delete msg
  129. // but, once returned to Update(), it was checking the
  130. // m_eState member to see if it should return. Unfortunately,
  131. // since 'this' had already been deallocated, it was too late
  132. // to do such a thing.
  133. //
  134. // 08/17/97 JMI Changed m_pthingParent to m_idParent.
  135. //
  136. // 08/20/97 BRH Moved firebomb sound to Destruction volume control.
  137. //
  138. // 08/27/97 BRH Added large fire sound which had not been used until now.
  139. //
  140. // 08/28/97 JMI Added a explode counter so we can cap the number of
  141. // explosions a firefrag can make.
  142. //
  143. // 08/28/97 BRH Added cache of large fire sound.
  144. //
  145. ////////////////////////////////////////////////////////////////////////////////
  146. #define FIREBOMB_CPP
  147. #include "RSPiX.h"
  148. #include <math.h>
  149. #include "firebomb.h"
  150. #include "dude.h"
  151. #include "fire.h"
  152. #include "SampleMaster.h"
  153. #include "game.h"
  154. #include "reality.h"
  155. ////////////////////////////////////////////////////////////////////////////////
  156. // Macros/types/etc.
  157. ////////////////////////////////////////////////////////////////////////////////
  158. #define PRIMARY_BURN_TIME 10000
  159. #define SECONDARY_BURN_TIME 7000
  160. #define SMALL_SHADOW_FILE "smallshadow.img"
  161. ////////////////////////////////////////////////////////////////////////////////
  162. // Variables/data
  163. ////////////////////////////////////////////////////////////////////////////////
  164. // These are default values -- actually values are set using the editor!
  165. double CFirebomb::ms_dCloseDistance = 30.0; // Close enough to hit CDude
  166. double CFirebomb::ms_dThrowVertVel = 40.0; // Throw up at this velocity
  167. double CFirebomb::ms_dThrowHorizVel = 250; // Throw out at this velocity
  168. // Let this auto-init to 0
  169. short CFirebomb::ms_sFileCount;
  170. /// Grenade Animation Files
  171. // An array of pointers to res names (one for each animatino component)
  172. static char* ms_apszResNames[] =
  173. {
  174. "3d/grenade.sop",
  175. "3d/grenade.mesh",
  176. "3d/grenade.tex",
  177. "3d/grenade.hot",
  178. "3d/grenade.bounds",
  179. "3d/grenade.floor",
  180. NULL,
  181. NULL
  182. };
  183. ////////////////////////////////////////////////////////////////////////////////
  184. // Load object (should call base class version!)
  185. ////////////////////////////////////////////////////////////////////////////////
  186. short CFirebomb::Load( // Returns 0 if successfull, non-zero otherwise
  187. RFile* pFile, // In: File to load from
  188. bool bEditMode, // In: True for edit mode, false otherwise
  189. short sFileCount, // In: File count (unique per file, never 0)
  190. ULONG ulFileVersion) // In: Version of file format to load.
  191. {
  192. short sResult = 0;
  193. sResult = CWeapon::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  194. if (sResult == SUCCESS)
  195. {
  196. // Load common data just once per file (not with each object)
  197. if (ms_sFileCount != sFileCount)
  198. {
  199. ms_sFileCount = sFileCount;
  200. // Load static data
  201. switch (ulFileVersion)
  202. {
  203. default:
  204. case 1:
  205. break;
  206. }
  207. }
  208. // Load object data
  209. switch (ulFileVersion)
  210. {
  211. default:
  212. case 1:
  213. break;
  214. }
  215. // Make sure there were no file errors or format errors . . .
  216. if (!pFile->Error() && sResult == 0)
  217. {
  218. // Get resources
  219. sResult = GetResources();
  220. }
  221. else
  222. {
  223. sResult = -1;
  224. TRACE("CFirebomb::Load(): Error reading from file!\n");
  225. }
  226. }
  227. return sResult;
  228. }
  229. ////////////////////////////////////////////////////////////////////////////////
  230. // Save object (should call base class version!)
  231. ////////////////////////////////////////////////////////////////////////////////
  232. short CFirebomb::Save( // Returns 0 if successfull, non-zero otherwise
  233. RFile* pFile, // In: File to save to
  234. short sFileCount) // In: File count (unique per file, never 0)
  235. {
  236. CWeapon::Save(pFile, sFileCount);
  237. // Save common data just once per file (not with each object)
  238. if (ms_sFileCount != sFileCount)
  239. {
  240. ms_sFileCount = sFileCount;
  241. // Save static data
  242. }
  243. // Save object data
  244. return 0;
  245. }
  246. ////////////////////////////////////////////////////////////////////////////////
  247. // Update object
  248. ////////////////////////////////////////////////////////////////////////////////
  249. void CFirebomb::Update(void)
  250. {
  251. USHORT usAttrib;
  252. short sHeight = m_sPrevHeight;
  253. double dNewX;
  254. double dNewY;
  255. double dNewZ;
  256. if (!m_sSuspend)
  257. {
  258. // Get new time
  259. long lThisTime = m_pRealm->m_time.GetGameTime();
  260. // Calculate elapsed time in seconds
  261. double dSeconds = (double)(lThisTime - m_lPrevTime) / 1000.0;
  262. ProcessMessages();
  263. if (m_eState == State_Deleted)
  264. {
  265. delete this;
  266. return;
  267. }
  268. // Check the current state
  269. switch (m_eState)
  270. {
  271. case CFirebomb::State_Idle:
  272. break;
  273. case CFirebomb::State_Fire:
  274. // Make sure we start in a valid position. If we are staring
  275. // inside a wall, just delete this object now.
  276. usAttrib = m_pRealm->GetFloorAttribute((short) m_dX, (short) m_dZ);
  277. sHeight = m_pRealm->GetHeight((short) m_dX, (short) m_dZ);
  278. if (m_dY < sHeight)
  279. {
  280. delete this;
  281. return;
  282. }
  283. m_eState = State_Go;
  284. // m_lTimer = lThisTime + ms_lGrenadeFuseTime;
  285. break;
  286. //-----------------------------------------------------------------------
  287. // Go - fly through the air until hit the ground, change directions on
  288. // obstacle collision.
  289. //-----------------------------------------------------------------------
  290. case CFirebomb::State_Go:
  291. // Do horizontal velocity
  292. dNewX = m_dX + COSQ[(short)m_dRot] * (m_dHorizVel * dSeconds);
  293. dNewZ = m_dZ - SINQ[(short)m_dRot] * (m_dHorizVel * dSeconds);
  294. // Do vertical velocity
  295. dNewY = m_dY;
  296. AdjustPosVel(&dNewY, &m_dVertVel, dSeconds);
  297. // Check the height to see if it hit the ground
  298. sHeight = m_pRealm->GetHeight((short) dNewX, (short) dNewZ);
  299. // If its lower than the last and current height, assume it
  300. // hit the ground.
  301. if (dNewY < sHeight && m_dY >= sHeight)
  302. {
  303. m_dY = sHeight;
  304. m_eState = CFirebomb::State_Explode;
  305. }
  306. else
  307. {
  308. // If it is above the last known ground and is now lower
  309. // than the height at its new position, assume it hit
  310. // a wall and should fall (this is where it used to bounce)
  311. if (dNewY < sHeight && m_dY < sHeight)
  312. {
  313. dNewX = m_dX; // Restore last x position
  314. dNewZ = m_dZ; // Restore last z position
  315. m_dRot = BounceAngle(m_dRot); // Change directions
  316. m_dHorizVel = 0.5;
  317. }
  318. else
  319. m_dY = dNewY;
  320. }
  321. m_dX = dNewX;
  322. m_dZ = dNewZ;
  323. break;
  324. //-----------------------------------------------------------------------
  325. // Explode - Once it hits the ground, break into fire fragments that
  326. // bounce out from this point.
  327. //-----------------------------------------------------------------------
  328. case CFirebomb::State_Explode:
  329. CFire* pFire;
  330. if (CThing::Construct(CThing::CFireID, m_pRealm, (CThing**) &pFire) == 0)
  331. {
  332. pFire->Setup(m_dX, m_dY, m_dZ, PRIMARY_BURN_TIME, true, CFire::LargeFire);
  333. pFire->m_u16ShooterID = m_u16ShooterID;
  334. PlaySample(
  335. g_smidFirebomb,
  336. SampleMaster::Destruction,
  337. DistanceToVolume(m_dX, m_dY, m_dZ, FireBombSndHalfLife) ); // In: Initial Sound Volume (0 - 255)
  338. PlaySample(
  339. g_smidFireLarge,
  340. SampleMaster::Destruction,
  341. DistanceToVolume(m_dX, m_dY, m_dZ, FireBombSndHalfLife) );
  342. }
  343. // Loop to create 8 fragments in a circular pattern
  344. short i;
  345. CFirefrag* pFrag;
  346. for (i = 0; i < 8; i++)
  347. {
  348. if (CThing::Construct(CThing::CFirefragID, m_pRealm, (CThing**) &pFrag) == 0)
  349. {
  350. pFrag->m_u16ShooterID = m_u16ShooterID;
  351. pFrag->Setup(m_dX, m_dY, m_dZ);
  352. pFrag->m_dVertVel = m_dVertVel * -0.5;
  353. pFrag->m_dHorizVel = 60.0;
  354. // pFrag->m_dRot = (i * (360/8)) - 25 + (GetRandom() % 50);
  355. pFrag->m_dRot = (i * (360/8)) + (GetRandom() % (360 / 8));
  356. pFrag->m_eState = CWeapon::State_Go;
  357. }
  358. }
  359. delete this;
  360. return;
  361. break;
  362. }
  363. // Save height for next time
  364. m_sPrevHeight = sHeight;
  365. // Save time for next time
  366. m_lPrevTime = lThisTime;
  367. }
  368. }
  369. ////////////////////////////////////////////////////////////////////////////////
  370. // Render object
  371. ////////////////////////////////////////////////////////////////////////////////
  372. void CFirebomb::Render(void)
  373. {
  374. // Animate
  375. long lThisTime = m_pRealm->m_time.GetGameTime();
  376. m_sprite.m_pmesh = (RMesh*) m_anim.m_pmeshes->GetAtTime(lThisTime);
  377. m_sprite.m_psop = (RSop*) m_anim.m_psops->GetAtTime(lThisTime);
  378. m_sprite.m_ptex = (RTexture*) m_anim.m_ptextures->GetAtTime(lThisTime);
  379. m_sprite.m_psphere = (RP3d*) m_anim.m_pbounds->GetAtTime(lThisTime);
  380. // Eventually this should be channel driven also
  381. m_sprite.m_sRadius = m_sCurRadius;
  382. if (m_eState == State_Hide)
  383. {
  384. // Hide.
  385. m_sprite.m_sInFlags = CSprite::InHidden;
  386. }
  387. else
  388. {
  389. // No special flags
  390. m_sprite.m_sInFlags = 0;
  391. }
  392. // If we're not a child of someone else...
  393. if (m_idParent == CIdBank::IdNil)
  394. {
  395. // Map from 3d to 2d coords
  396. Map3Dto2D((short) m_dX, (short) m_dY, (short) m_dZ, &m_sprite.m_sX2, &m_sprite.m_sY2);
  397. // Priority is based on our Z position.
  398. m_sprite.m_sPriority = m_dZ;
  399. // Layer should be based on info we get from attribute map
  400. m_sprite.m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer((short) m_dX, (short) m_dZ));
  401. m_sprite.m_ptrans = &m_trans;
  402. // Update sprite in scene
  403. m_pRealm->m_scene.UpdateSprite(&m_sprite);
  404. // Draw the 2D shadow
  405. CWeapon::Render();
  406. }
  407. else
  408. {
  409. // m_idParent is setting out transform relative to its position
  410. // and we are drawn by the scene with the parent.
  411. }
  412. }
  413. ////////////////////////////////////////////////////////////////////////////////
  414. // Setup new object - called by object that created this object
  415. ////////////////////////////////////////////////////////////////////////////////
  416. short CFirebomb::Setup( // Returns 0 if successfull, non-zero otherwise
  417. short sX, // In: New x coord
  418. short sY, // In: New y coord
  419. short sZ) // In: New z coord
  420. {
  421. short sResult = 0;
  422. // Use specified position
  423. m_dX = (double)sX;
  424. m_dY = (double)sY;
  425. m_dZ = (double)sZ;
  426. m_dHorizVel = ms_dThrowHorizVel;
  427. m_dVertVel = ms_dThrowVertVel;
  428. // HARD-WIRED CODE ALERT!
  429. // Eventually, this should be set via the bounding sphere radius.
  430. m_sCurRadius = 22; // FOR NOW, always half of scene.cpp:SCREEN_DIAMETER_FOR_3D.
  431. // Load resources
  432. sResult = GetResources();
  433. PrepareShadow();
  434. return sResult;
  435. }
  436. ////////////////////////////////////////////////////////////////////////////////
  437. // Get all required resources
  438. ////////////////////////////////////////////////////////////////////////////////
  439. short CFirebomb::GetResources(void) // Returns 0 if successfull, non-zero otherwise
  440. {
  441. short sResult = 0;
  442. sResult = m_anim.Get(ms_apszResNames);
  443. if (sResult == 0)
  444. {
  445. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(SMALL_SHADOW_FILE), &(m_spriteShadow.m_pImage), RFile::LittleEndian);
  446. if (sResult == 0)
  447. {
  448. // add more gets
  449. }
  450. else
  451. {
  452. TRACE("CGrenade::GetResources - Failed to open 2D shadow image\n");
  453. }
  454. }
  455. else
  456. {
  457. TRACE("CFirebomb::GetResources - Failed to open 3D animation for firebomb\n");
  458. }
  459. return sResult;
  460. }
  461. ////////////////////////////////////////////////////////////////////////////////
  462. // Free all resources
  463. ////////////////////////////////////////////////////////////////////////////////
  464. short CFirebomb::FreeResources(void) // Returns 0 if successfull, non-zero otherwise
  465. {
  466. m_anim.Release();
  467. return SUCCESS;
  468. }
  469. ////////////////////////////////////////////////////////////////////////////////
  470. // Preload - basically trick the resource manager into caching resources
  471. // for this object so there won't be a delay the first time it is
  472. // created.
  473. ////////////////////////////////////////////////////////////////////////////////
  474. short CFirebomb::Preload(
  475. CRealm* prealm) // In: Calling realm.
  476. {
  477. CAnim3D anim;
  478. RImage* pimage;
  479. short sResult = anim.Get(ms_apszResNames);
  480. anim.Release();
  481. rspGetResource(&g_resmgrGame, prealm->Make2dResPath(SMALL_SHADOW_FILE), &pimage, RFile::LittleEndian);
  482. rspReleaseResource(&g_resmgrGame, &pimage);
  483. CacheSample(g_smidFirebomb);
  484. CacheSample(g_smidFireLarge);
  485. return sResult;
  486. }
  487. ////////////////////////////////////////////////////////////////////////////////
  488. // ProcessMessages
  489. ////////////////////////////////////////////////////////////////////////////////
  490. void CFirebomb::ProcessMessages(void)
  491. {
  492. GameMessage msg;
  493. if (m_MessageQueue.DeQ(&msg) == true)
  494. {
  495. switch(msg.msg_Generic.eType)
  496. {
  497. case typeObjectDelete:
  498. m_MessageQueue.Empty();
  499. m_eState = State_Deleted;
  500. return;
  501. break;
  502. }
  503. }
  504. // Dump the rest of the messages
  505. m_MessageQueue.Empty();
  506. return;
  507. }
  508. ////////////////////////////////////////////////////////////////////////////////
  509. // Firefrag
  510. ////////////////////////////////////////////////////////////////////////////////
  511. ////////////////////////////////////////////////////////////////////////////////
  512. // Macros/types/etc.
  513. ////////////////////////////////////////////////////////////////////////////////
  514. #define FRAG_IMAGE_FILE "res\\grenade.bmp"
  515. // Minimum elapsed time (in milliseconds)
  516. ////////////////////////////////////////////////////////////////////////////////
  517. // Variables/data
  518. ////////////////////////////////////////////////////////////////////////////////
  519. // These are default values -- actually values are set using the editor!
  520. double CFirefrag::ms_dAccUser = 250.0; // Acceleration due to user
  521. double CFirefrag::ms_dAccDrag = 300.0; // Acceleration due to drag
  522. double CFirefrag::ms_dGravity = -9.5; // acceleration due to gravity
  523. double CFirefrag::ms_dThrowVertVel = 10.0; // Throw up at this velocity
  524. double CFirefrag::ms_dThrowHorizVel = 60; // Throw out at this velocity
  525. double CFirefrag::ms_dMinBounceVel = 30.0; // Min amount needed to bounce up
  526. double CFirefrag::ms_dVelTransferFract = -0.4; // Amount of velocity to bounce back up
  527. short CFirefrag::ms_sMaxExplosions = 4; // Maximum explosions before death.
  528. // Let this auto-init to 0
  529. short CFirefrag::ms_sFileCount;
  530. ////////////////////////////////////////////////////////////////////////////////
  531. // Load object (should call base class version!)
  532. ////////////////////////////////////////////////////////////////////////////////
  533. short CFirefrag::Load( // Returns 0 if successfull, non-zero otherwise
  534. RFile* pFile, // In: File to load from
  535. bool bEditMode, // In: True for edit mode, false otherwise
  536. short sFileCount, // In: File count (unique per file, never 0)
  537. ULONG ulFileVersion) // In: Version of file format to load.
  538. {
  539. short sResult = 0;
  540. sResult = CWeapon::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  541. if (sResult == SUCCESS)
  542. {
  543. // Load common data just once per file (not with each object)
  544. if (ms_sFileCount != sFileCount)
  545. {
  546. ms_sFileCount = sFileCount;
  547. // Load static data
  548. switch (ulFileVersion)
  549. {
  550. default:
  551. case 1:
  552. pFile->Read(&ms_dAccUser);
  553. pFile->Read(&ms_dAccDrag);
  554. break;
  555. }
  556. }
  557. // Load object data
  558. switch (ulFileVersion)
  559. {
  560. default:
  561. case 1:
  562. break;
  563. }
  564. // Make sure there were no file errors or format errors . . .
  565. if (!pFile->Error() && sResult == 0)
  566. {
  567. // Get resources
  568. sResult = GetResources();
  569. }
  570. else
  571. {
  572. sResult = -1;
  573. TRACE("CFirefrag::Load(): Error reading from file!\n");
  574. }
  575. }
  576. return sResult;
  577. }
  578. ////////////////////////////////////////////////////////////////////////////////
  579. // Save object (should call base class version!)
  580. ////////////////////////////////////////////////////////////////////////////////
  581. short CFirefrag::Save( // Returns 0 if successfull, non-zero otherwise
  582. RFile* pFile, // In: File to save to
  583. short sFileCount) // In: File count (unique per file, never 0)
  584. {
  585. CWeapon::Save(pFile, sFileCount);
  586. // Save common data just once per file (not with each object)
  587. if (ms_sFileCount != sFileCount)
  588. {
  589. ms_sFileCount = sFileCount;
  590. // Save static data
  591. pFile->Write(&ms_dAccUser);
  592. pFile->Write(&ms_dAccDrag);
  593. }
  594. // Save object data
  595. return 0;
  596. }
  597. ////////////////////////////////////////////////////////////////////////////////
  598. // Update object
  599. ////////////////////////////////////////////////////////////////////////////////
  600. void CFirefrag::Update(void)
  601. {
  602. short sHeight = m_sPrevHeight;
  603. double dPrevVertVel;
  604. double dNewX;
  605. double dNewY;
  606. double dNewZ;
  607. if (!m_sSuspend)
  608. {
  609. // Get new time
  610. long lThisTime = m_pRealm->m_time.GetGameTime();
  611. // Calculate elapsed time in seconds
  612. double dSeconds = (double)(lThisTime - m_lPrevTime) / 1000.0;
  613. // Check the current state
  614. switch (m_eState)
  615. {
  616. case CWeapon::State_Idle:
  617. break;
  618. case CWeapon::State_Fire:
  619. m_eState = State_Go;
  620. break;
  621. //-----------------------------------------------------------------------
  622. // Go - fly through the air until hit the ground, change directions on
  623. // obstacle collision.
  624. //-----------------------------------------------------------------------
  625. case CWeapon::State_Go:
  626. // Do horizontal velocity
  627. dNewX = m_dX + COSQ[(short)m_dRot] * (m_dHorizVel * dSeconds);
  628. dNewZ = m_dZ - SINQ[(short)m_dRot] * (m_dHorizVel * dSeconds);
  629. // Do vertical velocity
  630. // m_dVertVel += ms_dGravity;
  631. // m_dY += m_dVertVel * dSeconds;
  632. dPrevVertVel = m_dVertVel;
  633. dNewY = m_dY;
  634. AdjustPosVel(&dNewY, &m_dVertVel, dSeconds);
  635. // Check the height to see if it hit the ground
  636. sHeight = m_pRealm->GetHeight((short) dNewX, (short) dNewZ);
  637. // If its lower than the last and current height, assume it
  638. // hit the ground.
  639. // if (m_dY <= m_sPrevHeight && m_dY <= sHeight)
  640. if (dNewY < sHeight && m_dY >= sHeight)
  641. {
  642. m_dY = sHeight;
  643. m_eState = CWeapon::State_Explode;
  644. m_dVertVel = dPrevVertVel;
  645. }
  646. else
  647. {
  648. // If it is above the last known ground and is now lower
  649. // than the height at its new position, assume it hit
  650. // a wall and should bounce.
  651. if (dNewY < sHeight && m_dY < sHeight)
  652. {
  653. dNewX = m_dX; // Restore last x position
  654. dNewZ = m_dZ; // Restore last z position
  655. m_dRot = BounceAngle(m_dRot); // Change directions
  656. ASSERT(m_dRot >= 0.0 && m_dRot < 360.0);
  657. }
  658. else
  659. m_dY = dNewY;
  660. }
  661. m_dX = dNewX;
  662. m_dZ = dNewZ;
  663. break;
  664. //-----------------------------------------------------------------------
  665. // Explode - Once it hits the ground, break into fire fragments that
  666. // bounce out from this point.
  667. //-----------------------------------------------------------------------
  668. case CWeapon::State_Explode:
  669. CFire* pFire;
  670. if (CThing::Construct(CThing::CFireID, m_pRealm, (CThing**) &pFire) == 0)
  671. {
  672. pFire->Setup(m_dX, m_dY, m_dZ, SECONDARY_BURN_TIME, true, CFire::SmallFire);
  673. pFire->m_u16ShooterID = m_u16ShooterID;
  674. // PlaySample(g_smidGrenadeExplode);
  675. }
  676. m_sNumExplosions++;
  677. // If you have enough velocity to bounce and we've not exceeded
  678. // the maximum number of explosions, redirect velocity
  679. // upward and bounce, else kill yourself off.
  680. if (-m_dVertVel > ms_dMinBounceVel && m_sNumExplosions <= ms_sMaxExplosions)
  681. {
  682. m_dVertVel = m_dVertVel * ms_dVelTransferFract;
  683. m_eState = CWeapon::State_Go;
  684. }
  685. else
  686. {
  687. m_pRealm->m_idbank.GetThingByID((CThing**) &m_pFire, m_u16FireID);
  688. if (m_pFire)
  689. {
  690. GameMessage msg;
  691. msg.msg_ObjectDelete.eType = typeObjectDelete;
  692. msg.msg_ObjectDelete.sPriority = 0;
  693. SendThingMessage(&msg, m_pFire);
  694. }
  695. delete this;
  696. return;
  697. }
  698. break;
  699. }
  700. m_pRealm->m_idbank.GetThingByID((CThing**) &m_pFire, m_u16FireID);
  701. if (m_pFire)
  702. {
  703. m_pFire->m_dX = m_dX;
  704. m_pFire->m_dY = m_dY;
  705. m_pFire->m_dZ = m_dZ;
  706. }
  707. // Save height for next time
  708. m_sPrevHeight = sHeight;
  709. // Save time for next time
  710. m_lPrevTime = lThisTime;
  711. }
  712. }
  713. ////////////////////////////////////////////////////////////////////////////////
  714. // Render object
  715. ////////////////////////////////////////////////////////////////////////////////
  716. void CFirefrag::Render(void)
  717. {
  718. /*
  719. // This is a standard 2d sprite
  720. m_sprite.m_type = CSprite::Standard2d;
  721. // No special flags
  722. m_sprite.m_sInFlags = 0;
  723. // Map from 3d to 2d coords
  724. m_sprite.m_sX2 = m_dX - (m_pImage->m_sWidth / 2);
  725. m_sprite.m_sY2 = (m_dZ - (m_pImage->m_sHeight)) - m_dY;
  726. // Priority is based on bottom edge of sprite
  727. m_sprite.m_sPriority = m_sprite.m_sY2 + m_pImage->m_sHeight;
  728. // Layer should be based on info we get from attribute map, but is hardwired for now
  729. // m_sprite.m_sLayer = 0;
  730. ASSERT(m_pRealm != NULL);
  731. ASSERT(m_pRealm->m_pAttribMap != NULL);
  732. // Layer should be based on info we get from attribute map.
  733. m_sprite.m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer((short) m_dX, (short) m_dZ));
  734. // Image would normally animate, but doesn't for now
  735. m_sprite.m_pImage = m_pImage;
  736. // Update sprite in scene
  737. m_pRealm->m_scene.UpdateSprite(&m_sprite);
  738. */
  739. }
  740. ////////////////////////////////////////////////////////////////////////////////
  741. // Setup new object - called by object that created this object
  742. ////////////////////////////////////////////////////////////////////////////////
  743. short CFirefrag::Setup( // Returns 0 if successfull, non-zero otherwise
  744. short sX, // In: New x coord
  745. short sY, // In: New y coord
  746. short sZ) // In: New z coord
  747. {
  748. short sResult = 0;
  749. // Use specified position
  750. m_dX = (double)sX;
  751. m_dY = (double)sY;
  752. m_dZ = (double)sZ;
  753. m_lPrevTime = m_pRealm->m_time.GetGameTime();
  754. m_dVertVel = ms_dThrowVertVel;
  755. m_dHorizVel = ms_dThrowHorizVel;
  756. // Load resources
  757. // sResult = GetResources();
  758. if (CThing::Construct(CThing::CFireID, m_pRealm, (CThing**) &m_pFire) == 0)
  759. {
  760. m_pFire->Setup(m_dX, m_dY, m_dZ, SECONDARY_BURN_TIME, true, CFire::SmallFire);
  761. m_pFire->m_u16ShooterID = m_u16ShooterID;
  762. }
  763. return sResult;
  764. }
  765. ////////////////////////////////////////////////////////////////////////////////
  766. // Get all required resources
  767. ////////////////////////////////////////////////////////////////////////////////
  768. short CFirefrag::GetResources(void) // Returns 0 if successfull, non-zero otherwise
  769. {
  770. short sResult = 0;
  771. if (m_pImage == 0)
  772. {
  773. m_pImage = new RImage;
  774. if (m_pImage)
  775. {
  776. sResult = m_pImage->Load(FRAG_IMAGE_FILE);
  777. if (sResult == 0)
  778. {
  779. if (m_pImage->Convert(RImage::FSPR8) != RImage::FSPR8)
  780. {
  781. sResult = -1;
  782. TRACE("CFirefrag::GetResource(): Couldn't convert to FSPR8!\n");
  783. }
  784. }
  785. }
  786. else
  787. {
  788. sResult = -1;
  789. TRACE("CFirefrag::GetResources(): Couldn't allocate RImage!\n");
  790. }
  791. }
  792. return sResult;
  793. }
  794. ////////////////////////////////////////////////////////////////////////////////
  795. // Free all resources
  796. ////////////////////////////////////////////////////////////////////////////////
  797. short CFirefrag::FreeResources(void) // Returns 0 if successfull, non-zero otherwise
  798. {
  799. short sResult = 0;
  800. if (m_pImage != 0)
  801. {
  802. delete m_pImage;
  803. m_pImage = 0;
  804. }
  805. return sResult;
  806. }
  807. ////////////////////////////////////////////////////////////////////////////////
  808. // EOF
  809. ////////////////////////////////////////////////////////////////////////////////