pylon.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. // pylon.cpp
  19. // Project: Nostril (aka Postal)
  20. //
  21. // This module implements the bouy marker for use with the network navagation
  22. // system that will help the enemy guys get around the world.
  23. //
  24. // History:
  25. //
  26. // 05/01/97 BRH Started this object from the Bouy. It will take over the
  27. // responsibility of logic suggestions and locations for the
  28. // enemy AI and the bouys will go back to dealing only with
  29. // navigation.
  30. //
  31. // 05/02/97 BRH Added message processing and changed the Popout and
  32. // ShootCycle messages to be similar in their parameters.
  33. // Stores the information about the other pylon and the
  34. // dude when they become available.
  35. //
  36. // 05/05/97 BRH Fixed problems in the EditModify and the EditNew. Now
  37. // properly sets the image for the sprite.
  38. //
  39. // 05/06/97 JMI GetThingByID() was being passed a pylon pointer instead of
  40. // a pointer to a pylon pointer.
  41. //
  42. // 05/17/97 BRH Moved the clearing of the triggered flag to the top of
  43. // Update so that it will stay alive an entire interation.
  44. //
  45. // 05/29/97 JMI Removed ASSERT on m_pRealm->m_pAttribMap which no longer
  46. // exists.
  47. //
  48. // 06/17/97 MJR Moved some vars that were CPylon statics into the realm
  49. // so they could be instantiated on a realm-by-realm basis.
  50. //
  51. // 06/30/97 JMI Moved EditRect() and EditHotSpot() from pylon.h to
  52. // pylon.cpp.
  53. // Also, converted EditRect(), EditRender(), and/or Render()
  54. // to use Map3Dto2D().
  55. //
  56. // 06/30/97 JMI Now maps the Z to 3D when loading fileversions previous to
  57. // 24.
  58. //
  59. // 07/09/97 JMI Now uses m_pRealm->Make2dResPath() to get the fullpath
  60. // for 2D image components.
  61. //
  62. // 07/14/97 JMI Fixed two spots that did not check the return value from
  63. // GetThingById() before using the passed thing pointer.
  64. // Now EditModify() sets m_msg.msg_Popout.ucIDNext to 0, if
  65. // the user's specified pylon ID did not exist.
  66. // A dilemna existed for GetPylon(), if the user entered an
  67. // invalid pylon ID in the editor OR, more likely, the user
  68. // entered a valid pylon ID and then deleted the pylon, what
  69. // did GetPylon() return as the end pylon? It currently
  70. // returns itself since that pylon is guaranteed to exist.
  71. // /shrug.
  72. //
  73. // 08/05/97 JMI Changed priority to use Z position rather than 2D
  74. // projected Y position.
  75. //
  76. ////////////////////////////////////////////////////////////////////////////////
  77. #define PYLON_CPP
  78. #include "RSPiX.h"
  79. #include <math.h>
  80. #include "pylon.h"
  81. ////////////////////////////////////////////////////////////////////////////////
  82. // Macros/types/etc.
  83. ////////////////////////////////////////////////////////////////////////////////
  84. #define IMAGE_FILE "pylon.bmp"
  85. ////////////////////////////////////////////////////////////////////////////////
  86. // Variables/data
  87. ////////////////////////////////////////////////////////////////////////////////
  88. // These are default values -- actually values are set using the editor!
  89. // Let this auto-init to 0
  90. short CPylon::ms_sFileCount;
  91. ////////////////////////////////////////////////////////////////////////////////
  92. // Load object (should call base class version!)
  93. ////////////////////////////////////////////////////////////////////////////////
  94. short CPylon::Load( // Returns 0 if successfull, non-zero otherwise
  95. RFile* pFile, // In: File to load from
  96. bool bEditMode, // In: True for edit mode, false otherwise
  97. short sFileCount, // In: File count (unique per file, never 0)
  98. ULONG ulFileVersion) // In: Version of file format to load.
  99. {
  100. // Call the base load to get the u16InstanceID
  101. short sResult = CThing::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  102. if (sResult == 0)
  103. {
  104. // Load common data just once per file (not with each object)
  105. if (ms_sFileCount != sFileCount)
  106. {
  107. ms_sFileCount = sFileCount;
  108. // Load static data
  109. switch (ulFileVersion)
  110. {
  111. default:
  112. case 1:
  113. break;
  114. }
  115. }
  116. // Load object data
  117. pFile->Read(&m_dX);
  118. pFile->Read(&m_dY);
  119. pFile->Read(&m_dZ);
  120. // Switch on the parts that have changed
  121. switch (ulFileVersion)
  122. {
  123. default:
  124. case 6:
  125. pFile->Read(&m_ucID);
  126. m_msg.Load(pFile);
  127. break;
  128. case 5:
  129. case 4:
  130. case 3:
  131. case 2:
  132. case 1:
  133. case 0:
  134. break;
  135. }
  136. // If the file version is earlier than the change to real 3D coords . . .
  137. if (ulFileVersion < 24)
  138. {
  139. // Convert to 3D.
  140. m_pRealm->MapY2DtoZ3D(
  141. m_dZ,
  142. &m_dZ);
  143. }
  144. // Make sure there were no file errors or format errors . . .
  145. if (!pFile->Error() && sResult == 0)
  146. {
  147. // ONLY IN EDIT MODE . . .
  148. if (bEditMode == true)
  149. {
  150. // Get resources
  151. // sResult = GetResources();
  152. }
  153. }
  154. else
  155. {
  156. sResult = -1;
  157. TRACE("CPylon::Load(): Error reading from file!\n");
  158. }
  159. }
  160. else
  161. {
  162. TRACE("CPylon::Load(): CThing::Load() failed.\n");
  163. }
  164. return sResult;
  165. }
  166. ////////////////////////////////////////////////////////////////////////////////
  167. // Save object (should call base class version!)
  168. ////////////////////////////////////////////////////////////////////////////////
  169. short CPylon::Save( // Returns 0 if successfull, non-zero otherwise
  170. RFile* pFile, // In: File to save to
  171. short sFileCount) // In: File count (unique per file, never 0)
  172. {
  173. // Call the base class save to save the u16InstanceID
  174. CThing::Save(pFile, sFileCount);
  175. // Save common data just once per file (not with each object)
  176. if (ms_sFileCount != sFileCount)
  177. {
  178. ms_sFileCount = sFileCount;
  179. // Save static data
  180. }
  181. // Save object data
  182. pFile->Write(&m_dX);
  183. pFile->Write(&m_dY);
  184. pFile->Write(&m_dZ);
  185. pFile->Write(&m_ucID);
  186. m_msg.Save(pFile);
  187. if (pFile->Error())
  188. return FAILURE;
  189. else
  190. return SUCCESS;
  191. }
  192. ////////////////////////////////////////////////////////////////////////////////
  193. // Startup object
  194. ////////////////////////////////////////////////////////////////////////////////
  195. short CPylon::Startup(void) // Returns 0 if successfull, non-zero otherwise
  196. {
  197. short sResult = 0;
  198. // At this point we can assume the CHood was loaded, so we init our height
  199. m_dY = m_pRealm->GetHeight((short) m_dX, (short) m_dZ);
  200. // Init other stuff
  201. sResult = GetResources();
  202. // Update sphere.
  203. m_smash.m_sphere.sphere.X = m_dX;
  204. m_smash.m_sphere.sphere.Y = m_dY;
  205. m_smash.m_sphere.sphere.Z = m_dZ;
  206. m_smash.m_sphere.sphere.lRadius = m_sprite.m_pImage->m_sWidth;
  207. m_smash.m_bits = CSmash::Pylon;
  208. m_smash.m_pThing = this;
  209. // Update the smash.
  210. m_pRealm->m_smashatorium.Update(&m_smash);
  211. return sResult;
  212. }
  213. ////////////////////////////////////////////////////////////////////////////////
  214. // Shutdown object
  215. ////////////////////////////////////////////////////////////////////////////////
  216. short CPylon::Shutdown(void) // Returns 0 if successfull, non-zero otherwise
  217. {
  218. return 0;
  219. }
  220. ////////////////////////////////////////////////////////////////////////////////
  221. // Suspend object
  222. ////////////////////////////////////////////////////////////////////////////////
  223. void CPylon::Suspend(void)
  224. {
  225. m_sSuspend++;
  226. }
  227. ////////////////////////////////////////////////////////////////////////////////
  228. // Resume object
  229. ////////////////////////////////////////////////////////////////////////////////
  230. void CPylon::Resume(void)
  231. {
  232. m_sSuspend--;
  233. // If we're actually going to start updating again, we need to reset
  234. // the time so as to ignore any time that passed while we were suspended.
  235. // This method is far from precise, but I'm hoping it's good enough.
  236. }
  237. ////////////////////////////////////////////////////////////////////////////////
  238. // Update object
  239. ////////////////////////////////////////////////////////////////////////////////
  240. void CPylon::Update(void)
  241. {
  242. // Clear the target dude.
  243. m_u16TargetDudeID = 0;
  244. // Check for Dude trigger messages and set the flag
  245. ProcessMessages();
  246. }
  247. ////////////////////////////////////////////////////////////////////////////////
  248. // Render object
  249. ////////////////////////////////////////////////////////////////////////////////
  250. void CPylon::Render(void)
  251. {
  252. }
  253. ////////////////////////////////////////////////////////////////////////////////
  254. // Called by editor to init new object at specified position
  255. ////////////////////////////////////////////////////////////////////////////////
  256. short CPylon::EditNew( // Returns 0 if successfull, non-zero otherwise
  257. short sX, // In: New x coord
  258. short sY, // In: New y coord
  259. short sZ) // In: New z coord
  260. {
  261. short sResult = 0;
  262. // Use specified position
  263. m_dX = (double)sX;
  264. m_dY = (double)sY;
  265. m_dZ = (double)sZ;
  266. // Get Pylon ID
  267. m_ucID = GetFreePylonID();
  268. // Load resources
  269. sResult = GetResources();
  270. // Update sphere.
  271. m_smash.m_sphere.sphere.X = m_dX;
  272. m_smash.m_sphere.sphere.Y = m_dY;
  273. m_smash.m_sphere.sphere.Z = m_dZ;
  274. m_smash.m_sphere.sphere.lRadius = m_sprite.m_pImage->m_sWidth;
  275. m_smash.m_bits = CSmash::Pylon;
  276. m_smash.m_pThing = this;
  277. // Update the smash.
  278. m_pRealm->m_smashatorium.Update(&m_smash);
  279. return sResult;
  280. }
  281. inline
  282. void SetText( // Returns nothing.
  283. RGuiItem* pguiRoot, // In: Root GUI.
  284. long lId, // In: ID of GUI to set text.
  285. long lVal) // In: Value to set text to.
  286. {
  287. RGuiItem* pgui = pguiRoot->GetItemFromId(lId);
  288. if (pgui != NULL)
  289. {
  290. pgui->SetText("%ld", lVal);
  291. pgui->Compose();
  292. }
  293. }
  294. ////////////////////////////////////////////////////////////////////////////////
  295. // Called by editor to modify object
  296. ////////////////////////////////////////////////////////////////////////////////
  297. short CPylon::EditModify(void)
  298. {
  299. short sResult = 0;
  300. RGuiItem* pGui = RGuiItem::LoadInstantiate(FullPathVD("res/editor/bouy.gui"));
  301. RGuiItem* pSecondaryGui = NULL;
  302. if (pGui)
  303. {
  304. RListBox* pList = (RListBox*) pGui->GetItemFromId(3);
  305. if (pList)
  306. {
  307. switch(m_msg.msg_Generic.eType)
  308. {
  309. case typePopout:
  310. pList->SetSel(pGui->GetItemFromId(21));
  311. break;
  312. case typeShootCycle:
  313. pList->SetSel(pGui->GetItemFromId(23));
  314. break;
  315. case typeSafeSpot:
  316. pList->SetSel(pGui->GetItemFromId(22));
  317. break;
  318. default:
  319. pList->SetSel(pGui->GetItemFromId(20));
  320. break;
  321. }
  322. sResult = DoGui(pGui);
  323. if (sResult == 1)
  324. {
  325. {
  326. RGuiItem* pSelection = pList->GetSel();
  327. if (pSelection)
  328. {
  329. switch (pSelection->m_lId)
  330. {
  331. /*
  332. // Start Popout dialog
  333. case 21:
  334. pSecondaryGui = RGuiItem::LoadInstantiate(FullPathVD("res/editor/bouy_pop.gui"));
  335. if (pSecondaryGui)
  336. {
  337. SetText(pSecondaryGui, 3, m_msg.msg_Popout.sAngle);
  338. SetText(pSecondaryGui, 4, m_msg.msg_Popout.sDistance);
  339. sResult = DoGui(pSecondaryGui);
  340. if (sResult == 1)
  341. {
  342. // Get results back and store in message
  343. m_msg.msg_Popout.sAngle = pSecondaryGui->GetVal(3);
  344. m_msg.msg_Popout.sDistance = pSecondaryGui->GetVal(4);
  345. }
  346. }
  347. break;
  348. */
  349. // SafeSpot
  350. case 20:
  351. m_msg.msg_SafeSpot.eType = typeSafeSpot;
  352. break;
  353. // Popout dialog (same as run and shoot)
  354. case 21:
  355. pSecondaryGui = RGuiItem::LoadInstantiate(FullPathVD("res/editor/bouy_rs.gui"));
  356. if (pSecondaryGui)
  357. {
  358. SetText(pSecondaryGui, 3, m_msg.msg_Popout.ucIDNext);
  359. sResult = DoGui(pSecondaryGui);
  360. if (sResult == 1)
  361. {
  362. // Get results back and store in message
  363. m_msg.msg_Popout.eType = typePopout;
  364. m_msg.msg_Popout.ucIDNext = pSecondaryGui->GetVal(3);
  365. m_msg.msg_Popout.u16UniquePylonID = GetPylonUniqueID(m_msg.msg_Popout.ucIDNext);
  366. CPylon* pPylon;
  367. if (m_pRealm->m_idbank.GetThingByID((CThing**) &pPylon, m_msg.msg_Popout.u16UniquePylonID) == 0)
  368. {
  369. m_msg.msg_Popout.sNextPylonX = pPylon->GetX();
  370. m_msg.msg_Popout.sNextPylonZ = pPylon->GetZ();
  371. }
  372. else
  373. {
  374. m_msg.msg_Popout.ucIDNext = 0;
  375. }
  376. }
  377. }
  378. break;
  379. // Run & Shoot Cycle dialog box
  380. case 23:
  381. pSecondaryGui = RGuiItem::LoadInstantiate(FullPathVD("res/editor/bouy_rs.gui"));
  382. if (pSecondaryGui)
  383. {
  384. SetText(pSecondaryGui, 3, m_msg.msg_ShootCycle.ucIDNext);
  385. sResult = DoGui(pSecondaryGui);
  386. if (sResult == 1)
  387. {
  388. // Get results back and store in message
  389. m_msg.msg_ShootCycle.eType = typeShootCycle;
  390. m_msg.msg_ShootCycle.ucIDNext = pSecondaryGui->GetVal(3);
  391. m_msg.msg_ShootCycle.u16UniquePylonID = GetPylonUniqueID(m_msg.msg_ShootCycle.ucIDNext);
  392. CPylon* pPylon;
  393. if (m_pRealm->m_idbank.GetThingByID((CThing**) &pPylon, m_msg.msg_ShootCycle.u16UniquePylonID) == 0)
  394. {
  395. m_msg.msg_ShootCycle.sNextPylonX = pPylon->GetX();
  396. m_msg.msg_ShootCycle.sNextPylonZ = pPylon->GetZ();
  397. }
  398. else
  399. {
  400. m_msg.msg_Popout.ucIDNext = 0;
  401. }
  402. }
  403. }
  404. break;
  405. }
  406. }
  407. }
  408. }
  409. }
  410. }
  411. delete pGui;
  412. delete pSecondaryGui;
  413. return 0;
  414. }
  415. ////////////////////////////////////////////////////////////////////////////////
  416. // Called by editor to move object to specified position
  417. ////////////////////////////////////////////////////////////////////////////////
  418. short CPylon::EditMove( // Returns 0 if successfull, non-zero otherwise
  419. short sX, // In: New x coord
  420. short sY, // In: New y coord
  421. short sZ) // In: New z coord
  422. {
  423. m_dX = (double)sX;
  424. m_dY = (double)sY;
  425. m_dZ = (double)sZ;
  426. // Update sphere.
  427. m_smash.m_sphere.sphere.X = m_dX;
  428. m_smash.m_sphere.sphere.Y = m_dY;
  429. m_smash.m_sphere.sphere.Z = m_dZ;
  430. m_smash.m_sphere.sphere.lRadius = m_sprite.m_pImage->m_sWidth;
  431. m_smash.m_bits = CSmash::Pylon;
  432. m_smash.m_pThing = this;
  433. // Update the smash.
  434. m_pRealm->m_smashatorium.Update(&m_smash);
  435. return 0;
  436. }
  437. ////////////////////////////////////////////////////////////////////////////////
  438. // Called by editor to update object
  439. ////////////////////////////////////////////////////////////////////////////////
  440. void CPylon::EditUpdate(void)
  441. {
  442. }
  443. ////////////////////////////////////////////////////////////////////////////////
  444. // Called by editor to render object
  445. ////////////////////////////////////////////////////////////////////////////////
  446. void CPylon::EditRender(void)
  447. {
  448. // No special flags
  449. m_sprite.m_sInFlags = 0;
  450. // Map from 3d to 2d coords
  451. Map3Dto2D(
  452. (short) m_dX,
  453. (short) m_dY,
  454. (short) m_dZ,
  455. &m_sprite.m_sX2,
  456. &m_sprite.m_sY2);
  457. // Priority is based on bottom edge of sprite
  458. m_sprite.m_sPriority = m_dZ;
  459. // Center on image.
  460. m_sprite.m_sX2 -= m_pImage->m_sWidth / 2;
  461. m_sprite.m_sY2 -= m_pImage->m_sHeight;
  462. // Layer should be based on info we get from attribute map.
  463. m_sprite.m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer((short) m_dX, (short) m_dZ));
  464. // Image would normally animate, but doesn't for now
  465. m_sprite.m_pImage = m_pImage;
  466. // Update sprite in scene
  467. m_pRealm->m_scene.UpdateSprite(&m_sprite);
  468. }
  469. ////////////////////////////////////////////////////////////////////////////////
  470. // Give Edit a rectangle around this object
  471. ////////////////////////////////////////////////////////////////////////////////
  472. void CPylon::EditRect(RRect* pRect)
  473. {
  474. Map3Dto2D(
  475. m_dX,
  476. m_dY,
  477. m_dZ,
  478. &(pRect->sX),
  479. &(pRect->sY) );
  480. pRect->sW = 10; // Safety.
  481. pRect->sH = 10; // Safety.
  482. if (m_pImage != NULL)
  483. {
  484. pRect->sW = m_pImage->m_sWidth;
  485. pRect->sH = m_pImage->m_sHeight;
  486. }
  487. pRect->sX -= pRect->sW / 2;
  488. pRect->sY -= pRect->sH;
  489. }
  490. ////////////////////////////////////////////////////////////////////////////////
  491. // Called by editor to get the hotspot of an object in 2D.
  492. ////////////////////////////////////////////////////////////////////////////////
  493. void CPylon::EditHotSpot( // Returns nothiing.
  494. short* psX, // Out: X coord of 2D hotspot relative to
  495. // EditRect() pos.
  496. short* psY) // Out: Y coord of 2D hotspot relative to
  497. // EditRect() pos.
  498. {
  499. // Base of pylon is hotspot.
  500. *psX = (m_pImage->m_sWidth / 2);
  501. *psY = m_pImage->m_sHeight;
  502. }
  503. ////////////////////////////////////////////////////////////////////////////////
  504. // Get all required resources
  505. ////////////////////////////////////////////////////////////////////////////////
  506. short CPylon::GetResources(void) // Returns 0 if successfull, non-zero otherwise
  507. {
  508. short sResult = 0;
  509. if (m_pImage == 0)
  510. {
  511. RImage* pimBouyRes;
  512. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(IMAGE_FILE), &pimBouyRes);
  513. if (sResult == 0)
  514. {
  515. // Allocate image . . .
  516. m_pImage = new RImage;
  517. if (m_pImage != NULL)
  518. {
  519. // Allocate image data . . .
  520. if (m_pImage->CreateImage(
  521. pimBouyRes->m_sWidth,
  522. pimBouyRes->m_sHeight,
  523. RImage::BMP8) == 0)
  524. {
  525. // Blt bouy res.
  526. rspBlit(
  527. pimBouyRes, // Src.
  528. m_pImage, // Dst.
  529. 0, // Dst.
  530. 0, // Dst.
  531. NULL); // Dst clip.
  532. // Put in ID.
  533. RPrint print;
  534. print.SetFont(19, &g_fontBig);
  535. print.SetColor(1);
  536. print.SetJustifyCenter();
  537. print.print(
  538. m_pImage, // Dst.
  539. 0, // Dst.
  540. 18, // Dst.
  541. "%d", // Format.
  542. (short)m_ucID); // Src.
  543. // Convert to efficient transparent blit format . . .
  544. if (m_pImage->Convert(RImage::FSPR8) != RImage::FSPR8)
  545. {
  546. sResult = -3;
  547. TRACE("CPylon::GetResource() - Couldn't convert to FSPR8\n");
  548. }
  549. else
  550. {
  551. m_sprite.m_pImage = m_pImage;
  552. }
  553. }
  554. else
  555. {
  556. sResult = -2;
  557. TRACE("CPylon::GetResource() - m_pImage->CreateImage() failed.\n");
  558. }
  559. // If an error occurred after allocation . . .
  560. if (sResult != 0)
  561. {
  562. delete m_pImage;
  563. m_pImage = NULL;
  564. }
  565. }
  566. else
  567. {
  568. sResult = -1;
  569. TRACE("CPylon::GetResource(): Failed to allocate RImage.\n");
  570. }
  571. rspReleaseResource(&g_resmgrGame, &pimBouyRes);
  572. }
  573. }
  574. return sResult;
  575. }
  576. ////////////////////////////////////////////////////////////////////////////////
  577. // Free all resources
  578. ////////////////////////////////////////////////////////////////////////////////
  579. short CPylon::FreeResources(void) // Returns 0 if successfull, non-zero otherwise
  580. {
  581. if (m_pImage != NULL)
  582. {
  583. delete m_pImage;
  584. m_pImage = NULL;
  585. }
  586. return 0;
  587. }
  588. ////////////////////////////////////////////////////////////////////////////////
  589. // MessageRequest - Other Things can ask for the hint message from this bouy
  590. ////////////////////////////////////////////////////////////////////////////////
  591. void CPylon::MessageRequest(CThing* pRequestingThing)
  592. {
  593. if (pRequestingThing)
  594. {
  595. SendThingMessage(&m_msg, pRequestingThing);
  596. }
  597. }
  598. ////////////////////////////////////////////////////////////////////////////////
  599. // GetFreePylonID - Scan the list of CPylons and make sure that the ID is
  600. // not used before it is given out.
  601. ////////////////////////////////////////////////////////////////////////////////
  602. UCHAR CPylon::GetFreePylonID(void)
  603. {
  604. UCHAR id = m_pRealm->m_ucNextPylonID;
  605. if (m_pRealm->m_sNumPylons >= PYLON_MAX_PYLONS)
  606. return 0;
  607. CListNode<CThing>* pNext = NULL;
  608. bool bIdInUse = false;
  609. do
  610. {
  611. bIdInUse = false;
  612. // Loop through list of CPylons and see if they already have this ID
  613. pNext = m_pRealm->m_aclassHeads[CThing::CPylonID].m_pnNext;
  614. while (pNext && pNext->m_powner && !bIdInUse)
  615. {
  616. if (((CPylon*) pNext->m_powner)->m_ucID == id)
  617. bIdInUse = true;
  618. else
  619. pNext = pNext->m_pnNext;
  620. }
  621. if (bIdInUse)
  622. {
  623. if (id == 255)
  624. id = 1;
  625. else
  626. id++;
  627. }
  628. } while (bIdInUse);
  629. // Will wrap when it goes past 255. Even though number of pylons is
  630. // limited to PYLON_MAX_PYLONS, this var just keeps counting up, so if
  631. // you delete a bunch and then create a bunch more, this could wrap past
  632. // PYLON_MAX_PYLONS. The point is that it's safe.
  633. m_pRealm->m_ucNextPylonID = id + 1;
  634. return id;
  635. }
  636. ////////////////////////////////////////////////////////////////////////////////
  637. // GetPylon
  638. ////////////////////////////////////////////////////////////////////////////////
  639. CPylon* CPylon::GetPylon(UCHAR ucPylonID)
  640. {
  641. CPylon* pPylon = NULL;;
  642. if (m_pRealm->m_idbank.GetThingByID((CThing**) &pPylon, GetPylonUniqueID(ucPylonID)) != SUCCESS)
  643. pPylon = this;
  644. return pPylon;
  645. }
  646. ////////////////////////////////////////////////////////////////////////////////
  647. // GetPylonUniqueID - loop through list of pylons to get Unique ID
  648. ////////////////////////////////////////////////////////////////////////////////
  649. U16 CPylon::GetPylonUniqueID(UCHAR ucPylonID)
  650. {
  651. U16 u16UniqueID = CIdBank::IdNil;
  652. CListNode<CThing>* pNext = m_pRealm->m_aclassHeads[CThing::CPylonID].m_pnNext;
  653. bool bSearching = true;
  654. while (pNext && pNext->m_powner && bSearching)
  655. {
  656. if (((CPylon*) pNext->m_powner)->m_ucID == ucPylonID)
  657. {
  658. u16UniqueID = ((CThing*) pNext->m_powner)->GetInstanceID();
  659. bSearching = false;
  660. }
  661. else
  662. pNext = pNext->m_pnNext;
  663. }
  664. return u16UniqueID;
  665. }
  666. ////////////////////////////////////////////////////////////////////////////////
  667. // ProcessMessages - Similar to the base class version but handles a few more
  668. ////////////////////////////////////////////////////////////////////////////////
  669. void CPylon::ProcessMessages(void)
  670. {
  671. double dMinSqDistance = 1.0e200;
  672. double dTempSqDistance = 0;
  673. // Check queue of messages.
  674. GameMessage msg;
  675. while (m_MessageQueue.DeQ(&msg) == true)
  676. {
  677. switch(msg.msg_Generic.eType)
  678. {
  679. case typeDudeTrigger:
  680. dTempSqDistance = (m_dX-msg.msg_DudeTrigger.dX) * (m_dX-msg.msg_DudeTrigger.dX) +
  681. (m_dZ-msg.msg_DudeTrigger.dZ) * (m_dZ-msg.msg_DudeTrigger.dZ);
  682. if (dTempSqDistance < dMinSqDistance)
  683. {
  684. m_msg.msg_Popout.u16UniqueDudeID = m_u16TargetDudeID = msg.msg_DudeTrigger.u16DudeUniqueID;
  685. dMinSqDistance = dTempSqDistance;
  686. }
  687. break;
  688. }
  689. }
  690. }
  691. ////////////////////////////////////////////////////////////////////////////////
  692. // EOF
  693. ////////////////////////////////////////////////////////////////////////////////