mine.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. // mine.cpp
  19. // Project: Nostril (aka Postal)
  20. //
  21. // This module implements the CMine weapon class which is a hand
  22. // thrown grenade weapon.
  23. //
  24. //
  25. // History:
  26. // 03/19/97 BRH Started this weapon object.
  27. //
  28. // 03/19/97 BRH Added 4 types of mines to this file. Still need to
  29. // create a dialog in the gui editor to select which type
  30. // of mine to place in the editor. Also need to add motion
  31. // functions to the base class and use them for the
  32. // Bouncing Betty in Update.
  33. //
  34. // 03/20/97 BRH Added dialog box for selecting mine type. Still need to
  35. // do the bouncing betty mine.
  36. //
  37. // 03/21/97 BRH Added the Bouncing Betty functionality.
  38. //
  39. // 04/10/97 BRH Updated this to work with the new multi layer attribute
  40. // maps.
  41. //
  42. // 04/15/97 BRH Added fuse timer dialog item to edit modify to allow
  43. // the timed mine fuse to be set. Also added an overloaded
  44. // Setup() function to set the time for the fuse. Changed
  45. // Load and Save to deal with this new fuse value.
  46. //
  47. // 04/23/97 JMI Now sets its m_smash's bits to Mine instead of Item.
  48. //
  49. // 04/25/97 JMI Added angle Z (vertical angle adjustment) of 0 to the
  50. // CBulletFest::Fire(...) call.
  51. //
  52. // 04/29/97 JMI Changed State_Fire to merely jump to State_Idle. The
  53. // reason is that CCharacter uses State_Fire to notify things
  54. // that they should arm. Perhaps this state should be changed
  55. // to 'arm', but, technically, some things don't really even
  56. // arm until after that. Perhaps it should be State_Go or
  57. // something.
  58. // Anyways, in order to use State_Fire as the 'arm' trigger
  59. // from the placer, I had to change State_Go to do what State_Fire
  60. // used to for the bouncing betty; namely, wait for the time
  61. // to expire.
  62. // Also, Render() now subtracts half the width and half the
  63. // height from the 2D render location in an attempt to better
  64. // center the image. This should help especially when this
  65. // object is the child of another to make it appear in the
  66. // right spot.
  67. //
  68. // 04/30/97 JMI Changed the Setup() override of the CWeapon's Setup() to
  69. // pass the current mine type to the Setup() with eType.
  70. // Changed Construct() to take an ID as a parameter and added
  71. // ConstructProximity(), ConstructTimed(),
  72. // ConstructBouncingBetty(), and ConstructRemoteControl() to
  73. // allocate that type of mine.
  74. // Removed m_eMineType (now uses Class ID instead).
  75. // Removed Setup() that took an eType.
  76. // Filled in PreLoad() (but it still needs to convert each
  77. // RImage to whatever type is most efficient).
  78. // Also, GetResources() was new'ing m_pImage and then calling
  79. // rspGetResource() (which gives you an entirely new instance
  80. // of an image); so it was basically wasting an RImage worth
  81. // of memory.
  82. //
  83. // 05/28/97 BRH Increased arming time for Betty and Proximity mines to make
  84. // them easier to place and get away.
  85. //
  86. // 05/29/97 JMI Removed ASSERT on m_pRealm->m_pAttribMap which no longer
  87. // exists.
  88. //
  89. // 06/11/97 BRH Added shooter ID's to the shot messages and passed it
  90. // along to the explosion.
  91. //
  92. // 06/12/97 BRH Added shooter ID to the call to Setup for the explosion.
  93. //
  94. // 06/13/97 JMI Now obeys State_Hide.
  95. //
  96. // 06/30/97 BRH Added cache sound effects to Preload function.
  97. //
  98. // 07/09/97 JMI Now uses m_pRealm->Make2dResPath() to get the fullpath
  99. // for 2D image components.
  100. //
  101. // 07/09/97 JMI Changed Preload() to take a pointer to the calling realm
  102. // as a parameter.
  103. //
  104. // 07/18/97 JMI Got rid of bogus immitation PlaySample functions.
  105. // Now there is one PlaySample() function. Also, you now
  106. // MUST specify a category and you don't have to specify a
  107. // SoundInstance ptr to specify a volume.
  108. //
  109. // 07/21/97 JMI Now handles delete messages.
  110. //
  111. // 08/05/97 JMI Changed priority to use Z position rather than 2D
  112. // projected Y position.
  113. //
  114. // 08/16/97 BRH Added an arming beep sound and a click sound when the
  115. // mine is armed.
  116. //
  117. // 08/17/97 JMI Now, instead of aborting the sample, when the mine arms,
  118. // it stops the sample's loopage.
  119. // Now sets the volume every iteration b/c, although the mine
  120. // stays still, the distance to the local dude varies as the
  121. // local dude moves.
  122. // Also, changed m_pthingParent to m_idParent.
  123. //
  124. // 08/28/97 BRH Added preload function to cache the sounds and images.
  125. //
  126. ////////////////////////////////////////////////////////////////////////////////
  127. #define MINE_CPP
  128. #include "RSPiX.h"
  129. #include <math.h>
  130. #include "mine.h"
  131. #include "explode.h"
  132. #include "SampleMaster.h"
  133. #include "reality.h"
  134. ////////////////////////////////////////////////////////////////////////////////
  135. // Macros/types/etc.
  136. ////////////////////////////////////////////////////////////////////////////////
  137. #define TIMEDMINE_FILE "TimeMine.img"
  138. #define PROXIMITYMINE_FILE "ProxMine.img"
  139. #define BOUNCINGBETTYMINE_FILE "BettyMine.img"
  140. #define REMOTEMINE_FILE "RemoteMine.img"
  141. ////////////////////////////////////////////////////////////////////////////////
  142. // Variables/data
  143. ////////////////////////////////////////////////////////////////////////////////
  144. // These are default values -- actually values are set using the editor!
  145. short CMine::ms_sProximityRadius = 10;
  146. short CMine::ms_sBettyRadius = 60;
  147. short CMine::ms_sBettyRange = 1000;
  148. long CMine::ms_lFuseTime = 6000;
  149. long CMine::ms_lArmingTime = 5000;
  150. long CMine::ms_lExplosionDelay = 150;
  151. double CMine::ms_dInitialBounceVelocity = 80.0;
  152. // Let this auto-init to 0
  153. short CMine::ms_sFileCount;
  154. ////////////////////////////////////////////////////////////////////////////////
  155. // Load object (should call base class version!)
  156. ////////////////////////////////////////////////////////////////////////////////
  157. short CMine::Load( // Returns 0 if successfull, non-zero otherwise
  158. RFile* pFile, // In: File to load from
  159. bool bEditMode, // In: True for edit mode, false otherwise
  160. short sFileCount, // In: File count (unique per file, never 0)
  161. ULONG ulFileVersion) // In: Version of file format to load.
  162. {
  163. short sResult = 0;
  164. sResult = CWeapon::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  165. if (sResult == SUCCESS)
  166. {
  167. // Load common data just once per file (not with each object)
  168. if (ms_sFileCount != sFileCount)
  169. {
  170. ms_sFileCount = sFileCount;
  171. // Load static data
  172. switch (ulFileVersion)
  173. {
  174. default:
  175. case 1:
  176. pFile->Read(&ms_sProximityRadius);
  177. pFile->Read(&ms_sBettyRadius);
  178. pFile->Read(&ms_sBettyRange);
  179. pFile->Read(&ms_lFuseTime);
  180. pFile->Read(&ms_lArmingTime);
  181. pFile->Read(&ms_lExplosionDelay);
  182. pFile->Read(&ms_dInitialBounceVelocity);
  183. break;
  184. }
  185. }
  186. // Load object data
  187. MineType type;
  188. switch (ulFileVersion)
  189. {
  190. case 1:
  191. case 2:
  192. case 3:
  193. case 4:
  194. pFile->Read(&type);
  195. break;
  196. case 5:
  197. case 6:
  198. case 7:
  199. case 8:
  200. case 9:
  201. case 10:
  202. pFile->Read(&type);
  203. case 11:
  204. default:
  205. pFile->Read(&m_lFuseTime);
  206. break;
  207. }
  208. // Make sure there were no file errors or format errors . . .
  209. if (!pFile->Error() && sResult == 0)
  210. {
  211. // Get resources
  212. sResult = GetResources();
  213. }
  214. else
  215. {
  216. sResult = -1;
  217. TRACE("CMine::Load(): Error reading from file!\n");
  218. }
  219. }
  220. return sResult;
  221. }
  222. ////////////////////////////////////////////////////////////////////////////////
  223. // Save object (should call base class version!)
  224. ////////////////////////////////////////////////////////////////////////////////
  225. short CMine::Save( // Returns 0 if successfull, non-zero otherwise
  226. RFile* pFile, // In: File to save to
  227. short sFileCount) // In: File count (unique per file, never 0)
  228. {
  229. // In most cases, the base class Save() should be called. In this case it
  230. // isn't because the base class doesn't have a Save()!
  231. CWeapon::Save(pFile, sFileCount);
  232. // Save common data just once per file (not with each object)
  233. if (ms_sFileCount != sFileCount)
  234. {
  235. ms_sFileCount = sFileCount;
  236. // Save static data
  237. pFile->Write(&ms_sProximityRadius);
  238. pFile->Write(&ms_sBettyRadius);
  239. pFile->Write(&ms_sBettyRange);
  240. pFile->Write(&ms_lFuseTime);
  241. pFile->Write(&ms_lArmingTime);
  242. pFile->Write(&ms_lExplosionDelay);
  243. pFile->Write(&ms_dInitialBounceVelocity);
  244. }
  245. // Save object data
  246. pFile->Write(&m_lFuseTime);
  247. return 0;
  248. }
  249. ////////////////////////////////////////////////////////////////////////////////
  250. // Startup
  251. ////////////////////////////////////////////////////////////////////////////////
  252. short CMine::Startup(void)
  253. {
  254. return Init();
  255. }
  256. ////////////////////////////////////////////////////////////////////////////////
  257. // Init
  258. ////////////////////////////////////////////////////////////////////////////////
  259. short CMine::Init(void)
  260. {
  261. short sResult = SUCCESS;
  262. m_eState = State_Idle;
  263. long lThisTime = m_pRealm->m_time.GetGameTime();
  264. if (m_lFuseTime == 0)
  265. m_lFuseTime = ms_lFuseTime;
  266. switch (m_id)
  267. {
  268. case CTimedMineID:
  269. m_lTimer = m_lFuseTime + lThisTime;
  270. //This is needed to fix the crash when dropping timed mine
  271. //Bug is something to do with collision detection later on in smash.cpp
  272. m_sCurRadius = ms_sProximityRadius;
  273. break;
  274. case CProximityMineID:
  275. m_lTimer = ms_lArmingTime + lThisTime;
  276. m_sCurRadius = ms_sProximityRadius;
  277. break;
  278. case CBouncingBettyMineID:
  279. m_lTimer = ms_lArmingTime + lThisTime;
  280. m_sCurRadius = ms_sBettyRadius;
  281. break;
  282. case CRemoteControlMineID:
  283. m_lTimer = lThisTime;
  284. break;
  285. }
  286. // Set up collision object
  287. m_smash.m_bits = CSmash::Mine;
  288. m_smash.m_pThing = this;
  289. // Load resources
  290. sResult = GetResources();
  291. return sResult;
  292. }
  293. ////////////////////////////////////////////////////////////////////////////////
  294. // This inline updates a specified velocity with a specified drag over a
  295. // specified time.
  296. ////////////////////////////////////////////////////////////////////////////////
  297. inline
  298. bool UpdateVelocity( // Returns true if velocity reaches zero because of the
  299. // supplied accelration, false otherwise.
  300. double* pdVel, // In: Initial velocity.
  301. // Out: New velocity.
  302. double* pdDeltaVel, // Out: Delta velocity.
  303. double dAcc, // In: Acceleration.
  304. double dSeconds) // In: Elapsed time in seconds.
  305. {
  306. bool bAcceleratedToZero = false;
  307. double dVelPrev = *pdVel;
  308. *pdDeltaVel = dAcc * dSeconds;
  309. *pdVel += *pdDeltaVel;
  310. // I think this can be consdensed into a subtraction and one or two comparisons,
  311. // but I'm not sure that's really faster than the max 3 comparisons here.
  312. // If previously traveling forward . . .
  313. if (dVelPrev > 0.0)
  314. {
  315. // Passing 0 is considered at rest . . .
  316. if (*pdVel < 0.0)
  317. {
  318. // Update delta.
  319. *pdDeltaVel -= *pdVel;
  320. // Zero velocity.
  321. *pdVel = 0.0;
  322. }
  323. }
  324. else
  325. {
  326. // If previously traveling backward . . .
  327. if (dVelPrev < 0.0)
  328. {
  329. // Passing 0 is considered at rest . . .
  330. if (*pdVel > 0.0)
  331. {
  332. // Update delta.
  333. *pdDeltaVel -= *pdVel;
  334. // Zero velocity.
  335. *pdVel = 0.0;
  336. }
  337. }
  338. }
  339. // If velocity is now zero . . .
  340. if (*pdVel == 0.0)
  341. {
  342. // If drag opposed the previous velocity . . .
  343. if ((dVelPrev > 0.0 && dAcc < 0.0) || (dVelPrev < 0.0 && dAcc > 0.0))
  344. {
  345. // Drag has achieved its goal.
  346. bAcceleratedToZero = true;
  347. }
  348. }
  349. return bAcceleratedToZero;
  350. }
  351. ////////////////////////////////////////////////////////////////////////////////
  352. // Update object
  353. ////////////////////////////////////////////////////////////////////////////////
  354. void CMine::Update(void)
  355. {
  356. CSmash* pSmashed = NULL;
  357. double dDistance;
  358. short sShootAngle;
  359. short sShotX;
  360. short sShotY;
  361. short sShotZ;
  362. CThing* pShotThing = NULL;
  363. GameMessage msg;
  364. if (!m_sSuspend)
  365. {
  366. // Get new time
  367. long lThisTime = m_pRealm->m_time.GetGameTime();
  368. ProcessMessages();
  369. if (m_eState == State_Deleted)
  370. {
  371. delete this;
  372. return;
  373. }
  374. // Calculate elapsed time in seconds
  375. double dSeconds = (double)(lThisTime - m_lPrevTime) / 1000.0;
  376. // Check the current state
  377. switch (m_eState)
  378. {
  379. //-----------------------------------------------------------------------
  380. // Idle - waiting to arm
  381. //-----------------------------------------------------------------------
  382. case CWeapon::State_Idle:
  383. if (lThisTime > m_lTimer)
  384. {
  385. // End the looping arming sound
  386. if (m_siMineBeep != 0)
  387. {
  388. StopLoopingSample(m_siMineBeep);
  389. m_siMineBeep = 0;
  390. }
  391. switch (m_id)
  392. {
  393. case CTimedMineID:
  394. m_eState = State_Explode;
  395. break;
  396. case CProximityMineID:
  397. case CBouncingBettyMineID:
  398. m_eState = State_Armed;
  399. PlaySample(
  400. g_smidMineSet, // Sample to play
  401. SampleMaster::Weapon, // Category for user sound adjustment
  402. DistanceToVolume(m_dX, m_dY, m_dZ, MineSndHalfLife) ); //Pos
  403. break;
  404. case CRemoteControlMineID:
  405. default:
  406. break;
  407. }
  408. }
  409. else
  410. {
  411. short sX = m_dX;
  412. short sY = m_dY;
  413. short sZ = m_dZ;
  414. // If we have a parent . . .
  415. CThing* pthing = NULL; // Initialized for safety.
  416. if (m_pRealm->m_idbank.GetThingByID(&pthing, m_idParent) == 0)
  417. {
  418. // Add in its position.
  419. sX += pthing->GetX();
  420. sY += pthing->GetY();
  421. sZ += pthing->GetZ();
  422. }
  423. // Update sound position.
  424. SetInstanceVolume(m_siMineBeep, DistanceToVolume(sX, sY, sZ, MineSndHalfLife) );
  425. }
  426. break;
  427. //-----------------------------------------------------------------------
  428. // Armed - State for Proximity mine & bouncing betty where they check
  429. // for collisions with other characters and react.
  430. //-----------------------------------------------------------------------
  431. case CWeapon::State_Armed:
  432. if (m_pRealm->m_smashatorium.QuickCheck(&m_smash,
  433. CSmash::Character,
  434. CSmash::Good | CSmash::Bad,
  435. 0, &pSmashed))
  436. {
  437. switch (m_id)
  438. {
  439. case CBouncingBettyMineID:
  440. m_eState = State_Go;
  441. m_dVertVel = ms_dInitialBounceVelocity;
  442. // Make it go off right away
  443. m_lTimer = lThisTime;
  444. break;
  445. case CProximityMineID:
  446. m_eState = State_Explode;
  447. // Make it go off right away
  448. m_lTimer = lThisTime;
  449. break;
  450. }
  451. }
  452. break;
  453. //-----------------------------------------------------------------------
  454. // Fire - Initial triggering notification from character (if placed by
  455. // character).
  456. //-----------------------------------------------------------------------
  457. case CWeapon::State_Fire:
  458. // Go back to waiting to arm.
  459. m_eState = State_Idle;
  460. break;
  461. //-----------------------------------------------------------------------
  462. // Go - used for bouncing betty when it is bouncing up
  463. //-----------------------------------------------------------------------
  464. case CWeapon::State_Go:
  465. if (lThisTime > m_lTimer)
  466. {
  467. PlaySample(
  468. g_smidBounceLaunch,
  469. SampleMaster::Weapon,
  470. DistanceToVolume(m_dX, m_dY, m_dZ, LaunchSndHalfLife) ); // In: Initial Sound Volume (0 - 255)
  471. // Do motion
  472. ///////////////////////// Vertical Velocity /////////////////////////////////
  473. UpdateVelocity(&m_dVertVel, &m_dVertDeltaVel, g_dAccelerationDueToGravity, dSeconds);
  474. // Apply external vertical velocity.
  475. dDistance = (m_dVertVel - m_dVertDeltaVel / 2) * dSeconds;
  476. m_dY = m_dY + dDistance;
  477. // If velocity is negative, then explode and shoot in
  478. // several directions using deluxe shot or something.
  479. if (m_dVertVel <= 0.0)
  480. {
  481. // Make a small explosion noise
  482. PlaySample(
  483. g_smidBounceExplode,
  484. SampleMaster::Weapon,
  485. DistanceToVolume(m_dX, m_dY, m_dZ, ExplosionSndHalfLife) ); // In: Initial Sound Volume (0 - 255)
  486. // Draw some kind of flash on the mine
  487. m_bulletfest.Impact(0, m_dX, m_dY, m_dZ, m_pRealm);
  488. m_bulletfest.Impact(0, m_dX + 5, m_dY, m_dZ, m_pRealm);
  489. m_bulletfest.Impact(0, m_dX - 5, m_dY, m_dZ, m_pRealm);
  490. m_bulletfest.Impact(0, m_dX, m_dY + 5, m_dZ, m_pRealm);
  491. m_bulletfest.Impact(0, m_dX, m_dY - 5, m_dZ, m_pRealm);
  492. // Shoot in all directions
  493. for (sShootAngle = 0; sShootAngle < 360; sShootAngle += 20)
  494. {
  495. m_bulletfest.Fire(sShootAngle,
  496. 0,
  497. (short) m_dX,
  498. (short) m_dY,
  499. (short) m_dZ,
  500. ms_sBettyRange,
  501. m_pRealm,
  502. CSmash::Character,
  503. CSmash::Good | CSmash::Bad,
  504. 0,
  505. &sShotX,
  506. &sShotY,
  507. &sShotZ,
  508. &pShotThing);
  509. if (pShotThing != NULL)
  510. {
  511. msg.msg_Shot.eType = typeShot;
  512. msg.msg_Shot.sPriority = 0;
  513. msg.msg_Shot.sDamage = 50;
  514. msg.msg_Shot.sAngle = rspATan(m_dZ - sShotZ, sShotX - m_dX);
  515. msg.msg_Shot.u16ShooterID = m_u16ShooterID;
  516. // Tell this thing that it got shot
  517. SendThingMessage(&msg, pShotThing);
  518. }
  519. }
  520. // Get rid of the mine
  521. delete this;
  522. return;
  523. }
  524. }
  525. break;
  526. //-----------------------------------------------------------------------
  527. // Explode
  528. //-----------------------------------------------------------------------
  529. case CWeapon::State_Explode:
  530. if (lThisTime > m_lTimer)
  531. {
  532. // Start an explosion object and then kill rocket
  533. // object
  534. CExplode* pExplosion;
  535. if (CThing::Construct(CThing::CExplodeID, m_pRealm, (CThing**) &pExplosion) == 0)
  536. {
  537. pExplosion->Setup(m_dX, m_dY, m_dZ, m_u16ShooterID);
  538. PlaySample(
  539. g_smidGrenadeExplode,
  540. SampleMaster::Destruction,
  541. DistanceToVolume(m_dX, m_dY, m_dZ, ExplosionSndHalfLife) ); // In: Initial Sound Volume (0 - 255)
  542. }
  543. delete this;
  544. return;
  545. }
  546. break;
  547. }
  548. // Save time for next time
  549. m_lPrevTime = lThisTime;
  550. // Update sphere.
  551. m_smash.m_sphere.sphere.X = m_dX;
  552. m_smash.m_sphere.sphere.Y = m_dY;
  553. m_smash.m_sphere.sphere.Z = m_dZ;
  554. m_smash.m_sphere.sphere.lRadius = m_sCurRadius;
  555. // Update the smash.
  556. m_pRealm->m_smashatorium.Update(&m_smash);
  557. }
  558. }
  559. ////////////////////////////////////////////////////////////////////////////////
  560. // Render object
  561. ////////////////////////////////////////////////////////////////////////////////
  562. void CMine::Render(void)
  563. {
  564. if (m_pImage)
  565. {
  566. // Image would normally animate, but doesn't for now
  567. m_sprite.m_pImage = m_pImage;
  568. if (m_eState == State_Hide)
  569. {
  570. // Hide.
  571. m_sprite.m_sInFlags = CSprite::InHidden;
  572. }
  573. else
  574. {
  575. // No special flags
  576. m_sprite.m_sInFlags = 0;
  577. }
  578. // Map from 3d to 2d coords
  579. Map3Dto2D(
  580. (short) m_dX,
  581. (short) m_dY,
  582. (short) m_dZ,
  583. &m_sprite.m_sX2,
  584. &m_sprite.m_sY2);
  585. // Center on image.
  586. m_sprite.m_sX2 -= m_pImage->m_sWidth / 2;
  587. m_sprite.m_sY2 -= m_pImage->m_sHeight / 2;
  588. // Priority is based on bottom edge of sprite
  589. m_sprite.m_sPriority = m_dZ;
  590. // Layer should be based on info we get from attribute map.
  591. m_sprite.m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer((short) m_dX, (short) m_dZ));
  592. // Update sprite in scene
  593. m_pRealm->m_scene.UpdateSprite(&m_sprite);
  594. }
  595. }
  596. ////////////////////////////////////////////////////////////////////////////////
  597. // Setup new object - called by object that created this object
  598. // This version is meant for the timed mine so that
  599. // the fuse time can be set.
  600. ////////////////////////////////////////////////////////////////////////////////
  601. short CMine::Setup( // Returns 0 if successful, non-zero otherwise
  602. short sX, // In: X coord placement
  603. short sY, // In: Y coord placement
  604. short sZ, // In: Z coord placement
  605. long lFuseTime) // In: ms before mine goes off (timed mine only)
  606. {
  607. m_lFuseTime = lFuseTime;
  608. return Setup(sX, sY, sZ);
  609. }
  610. ////////////////////////////////////////////////////////////////////////////////
  611. // Setup new object - called by object that created this object
  612. ////////////////////////////////////////////////////////////////////////////////
  613. short CMine::Setup( // Returns 0 if successfull, non-zero otherwise
  614. short sX, // In: New x coord
  615. short sY, // In: New y coord
  616. short sZ) // In: New z coord
  617. {
  618. // Loop the Arming sound
  619. PlaySample(
  620. g_smidMineBeep, // In: sound to play
  621. SampleMaster::Weapon, // In: user volume adjustment category
  622. DistanceToVolume(sX, sY, sZ, MineSndHalfLife), // Position
  623. &m_siMineBeep, // Out: Handle to sound so it can be stopped
  624. NULL, // Out: Sample duration in ms
  625. 0, // In: Where to loop to
  626. -1, // In: Where to loop from
  627. // In: If less than 1, the end + lLoopEndTime is used.
  628. false); // In: Call ReleaseAndPurge rather than Release at end
  629. short sResult = CWeapon::Setup(sX, sY, sZ);
  630. if (sResult == 0)
  631. {
  632. sResult = Init();
  633. }
  634. return sResult;
  635. }
  636. ////////////////////////////////////////////////////////////////////////////////
  637. // Get all required resources
  638. ////////////////////////////////////////////////////////////////////////////////
  639. short CMine::GetResources(void) // Returns 0 if successfull, non-zero otherwise
  640. {
  641. short sResult = 0;
  642. if (m_pImage == 0)
  643. {
  644. switch (m_id)
  645. {
  646. case CTimedMineID:
  647. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(TIMEDMINE_FILE), &m_pImage, RFile::LittleEndian);
  648. break;
  649. case CProximityMineID:
  650. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(PROXIMITYMINE_FILE), &m_pImage, RFile::LittleEndian);
  651. break;
  652. case CBouncingBettyMineID:
  653. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(BOUNCINGBETTYMINE_FILE), &m_pImage, RFile::LittleEndian);
  654. break;
  655. case CRemoteControlMineID:
  656. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(REMOTEMINE_FILE), &m_pImage, RFile::LittleEndian);
  657. break;
  658. }
  659. }
  660. return sResult;
  661. }
  662. ////////////////////////////////////////////////////////////////////////////////
  663. // Free all resources
  664. ////////////////////////////////////////////////////////////////////////////////
  665. short CMine::FreeResources(void) // Returns 0 if successfull, non-zero otherwise
  666. {
  667. rspReleaseResource(&g_resmgrGame, &m_pImage);
  668. return 0;
  669. }
  670. ////////////////////////////////////////////////////////////////////////////////
  671. // Helper inline to get a GUI, set its text to the value, and recompose it.
  672. ////////////////////////////////////////////////////////////////////////////////
  673. inline
  674. void SetText( // Returns nothing.
  675. RGuiItem* pguiRoot, // In: Root GUI.
  676. long lId, // In: ID of GUI to set text.
  677. long lVal) // In: Value to set text to.
  678. {
  679. RGuiItem* pgui = pguiRoot->GetItemFromId(lId);
  680. if (pgui != NULL)
  681. {
  682. pgui->SetText("%ld", lVal);
  683. pgui->Compose();
  684. }
  685. }
  686. ////////////////////////////////////////////////////////////////////////////////
  687. // Edit Modify
  688. ////////////////////////////////////////////////////////////////////////////////
  689. short CMine::EditModify(void)
  690. {
  691. short sResult = 0;
  692. RGuiItem* pGui = RGuiItem::LoadInstantiate(FullPathVD("res/editor/mine.gui"));
  693. if (pGui)
  694. {
  695. SetText(pGui, 7, m_lFuseTime);
  696. sResult = DoGui(pGui);
  697. if (sResult == 1)
  698. {
  699. m_lFuseTime = pGui->GetVal(7);
  700. }
  701. }
  702. delete pGui;
  703. return 0;
  704. }
  705. ////////////////////////////////////////////////////////////////////////////////
  706. // Called by editor to init new object at specified position
  707. ////////////////////////////////////////////////////////////////////////////////
  708. short CMine::EditNew( // Returns 0 if successfull, non-zero otherwise
  709. short sX, // In: New x coord
  710. short sY, // In: New y coord
  711. short sZ) // In: New z coord
  712. {
  713. short sResult = 0;
  714. if (sResult == SUCCESS)
  715. {
  716. // Load resources
  717. sResult = Setup(sX, sY, sZ);
  718. }
  719. else
  720. {
  721. sResult = -1;
  722. }
  723. return sResult;
  724. }
  725. ////////////////////////////////////////////////////////////////////////////////
  726. // Preload - basically trick the resource manager into caching resources
  727. // for this object so there won't be a delay the first time it is
  728. // created.
  729. ////////////////////////////////////////////////////////////////////////////////
  730. short CMine::Preload(
  731. CRealm* prealm) // In: Calling realm.
  732. {
  733. short sResult;
  734. RImage* pim;
  735. sResult = rspGetResource(&g_resmgrGame, prealm->Make2dResPath(TIMEDMINE_FILE), &pim, RFile::LittleEndian);
  736. if (sResult == 0)
  737. {
  738. rspReleaseResource(&g_resmgrGame, &pim);
  739. sResult = rspGetResource(&g_resmgrGame, prealm->Make2dResPath(PROXIMITYMINE_FILE), &pim, RFile::LittleEndian);
  740. if (sResult == 0)
  741. {
  742. rspReleaseResource(&g_resmgrGame, &pim);
  743. sResult = rspGetResource(&g_resmgrGame, prealm->Make2dResPath(BOUNCINGBETTYMINE_FILE), &pim, RFile::LittleEndian);
  744. if (sResult == 0)
  745. {
  746. rspReleaseResource(&g_resmgrGame, &pim);
  747. }
  748. }
  749. }
  750. CacheSample(g_smidBounceLaunch);
  751. CacheSample(g_smidBounceExplode);
  752. CacheSample(g_smidGrenadeExplode);
  753. CacheSample(g_smidMineBeep);
  754. CacheSample(g_smidMineSet);
  755. // This should convert each one of these images.
  756. return sResult;
  757. }
  758. ////////////////////////////////////////////////////////////////////////////////
  759. // Explosion Message handler
  760. ////////////////////////////////////////////////////////////////////////////////
  761. void CMine::OnExplosionMsg(Explosion_Message* pMessage)
  762. {
  763. // If we got blown up, go off in whatever manner this type
  764. // of mine would normally go off.
  765. switch (m_id)
  766. {
  767. // If its a bouncing betty, and hasn't already been
  768. // triggered, then trigger it to bounce up.
  769. case CBouncingBettyMineID:
  770. if (m_eState == State_Idle || m_eState == State_Armed)
  771. {
  772. m_eState = State_Go;
  773. m_dVertVel = ms_dInitialBounceVelocity;
  774. m_lTimer = m_pRealm->m_time.GetGameTime() + ms_lExplosionDelay;
  775. }
  776. break;
  777. // If its any other type, just make it explode if it
  778. // is not exploding already
  779. default:
  780. if (m_eState != State_Explode)
  781. {
  782. m_eState = State_Explode;
  783. m_lTimer = m_pRealm->m_time.GetGameTime() + ms_lExplosionDelay;
  784. }
  785. break;
  786. }
  787. }
  788. ////////////////////////////////////////////////////////////////////////////////
  789. // Trigger Message handler
  790. ////////////////////////////////////////////////////////////////////////////////
  791. void CMine::OnTriggerMsg(Trigger_Message* pMessage)
  792. {
  793. // If we are a remote control mine & we got the trigger message,
  794. // then blow up.
  795. if (m_id == CRemoteControlMineID)
  796. m_eState = State_Explode;
  797. }
  798. ////////////////////////////////////////////////////////////////////////////////
  799. // Handles an ObjectDelete_Message.
  800. ////////////////////////////////////////////////////////////////////////////////
  801. void CMine::OnDeleteMsg( // Returns nothing.
  802. ObjectDelete_Message* pdeletemsg) // In: Message to handle.
  803. {
  804. // Go to deleted state. Update() will delete us.
  805. m_eState = State_Deleted;
  806. }
  807. ////////////////////////////////////////////////////////////////////////////////
  808. // EOF
  809. ////////////////////////////////////////////////////////////////////////////////