bouy.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. // bouy.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. // 01/28/97 BRH Added bouy's to the editor which will help with navagation
  26. //
  27. // 02/02/97 BRH Added NextRouteNode function which will tell you which
  28. // bouy you should go to next in order to get to your
  29. // destination.
  30. //
  31. // 02/03/97 BRH Added info to the Load and Save functions to save
  32. // information needed to reconnect the Bouy network after
  33. // loading it.
  34. //
  35. // 02/04/97 BRH Added GetRouteTableEntry() function that can be called
  36. // from the CNavigationNet's ping function which will safely
  37. // return an entry from the routing table. If the table
  38. // was not large enough, it expands the the current
  39. // number of nodes and initializes the data.
  40. //
  41. // 02/04/97 JMI Changed LoadDib() call to Load() (which now supports
  42. // loading of DIBs).
  43. //
  44. // 02/23/97 BRH Changed bouy resource to be under Resource Manager control.
  45. // Also changed Render to do nothing and moved its
  46. // functionality to EditRender so that the Bouys are not
  47. // drawn during game play but only in the editor.
  48. //
  49. // 02/24/97 JMI No longer sets the m_type member of the m_sprite b/c it
  50. // is set by m_sprite's constructor.
  51. //
  52. // 03/06/97 BRH Changed to the new calling of Ping which doesn't have the
  53. // maxdepth parameter. Also added a commented out version of
  54. // saving the route table but didn't want to include it yet
  55. // since it would change the format of the realm files.
  56. //
  57. // 03/07/97 JMI Now draws the bouy number into it's m_pImage.
  58. //
  59. // 03/07/97 JMI Now sets the color of the text to be safe.
  60. //
  61. // 03/07/97 BRH Played some with the font for the bouys so it would
  62. // be easier to see and so it would show 2 digits.
  63. //
  64. // 03/13/97 BRH Made a few changes to update the routing tables
  65. // correctly. I will also be adding a hops table
  66. // in addition to the route table to cut down on the
  67. // number of pings required to fill in the tables. Then
  68. // the hops tables can be freed since they aren't needed for
  69. // gameplay.
  70. //
  71. // 03/13/97 JMI Load now takes a version number.
  72. //
  73. // 04/10/97 BRH Updated this to work with the new multi layer attribute
  74. // maps.
  75. //
  76. // 04/11/97 BRH Adding BuildRoutingTable function which will use a
  77. // a Breadth-First search of the tree to determine the
  78. // shortest route to all reachable nodes, and will then
  79. // use the temporary BSF tree to fill in the routing table.
  80. //
  81. // 04/15/97 BRH Taking out the old routing method leaving just the new
  82. // which seems to be working.
  83. //
  84. // 04/20/97 BRH Added MessageRequest function that will send the
  85. // bouy's function if it has one. Also now loads and saves
  86. // the message using the message's Load and Save functions.
  87. //
  88. // 04/21/97 BRH Changed to multiple dialogs for each type of message to
  89. // avoid special code to display the correct fields for each
  90. // different type of message.
  91. //
  92. // 04/24/97 BRH Fixed problem in Load with new version number.
  93. //
  94. // 05/01/97 BRH Removed messages for logic suggestions and put those
  95. // into the CPylon class instead.
  96. //
  97. // 05/29/97 JMI Removed ASSERT on m_pRealm->m_pAttribMap which no longer
  98. // exists.
  99. //
  100. // 06/06/97 BRH Freed three arrays used in BuildRouteTable that had
  101. // previously been a source of memory leaks.
  102. //
  103. // 06/25/97 BRH Took out the STL set "linkset" because it was causing
  104. // sync problems with the game. Apparently, the set uses
  105. // a tree to store its data, and when building that tree,
  106. // uses some kind of random function to balance the tree, but
  107. // not the standard library rand() function, but a different
  108. // random function that we don't reset from realm to realm.
  109. // This caused the routing tables to be build differently
  110. // each time the game was played, and so the demo mode and
  111. // network mode were out of sync.
  112. //
  113. // 06/29/97 MJR Removed last trace of STL, replacing vector with RFList.
  114. //
  115. // 06/29/97 JMI Converted EditRect(), EditRender(), and/or Render() to
  116. // use Map3Dto2D().
  117. // Also, moved definitions of EditRect() and EditHotSpot() to
  118. // here from bouy.h.
  119. //
  120. // 06/30/97 JMI Now maps the Z to 3D when loading fileversions previous to
  121. // 24.
  122. //
  123. // 07/07/97 BRH Fixed bug when saving bouy networks where bouys had been
  124. // deleted. The Unlink was not correctly decrementing the
  125. // number of direct links so it was expecting more direct
  126. // links when the file was reloaded.
  127. //
  128. // 07/09/97 JMI Now uses m_pRealm->Make2dResPath() to get the fullpath
  129. // for 2D image components.
  130. //
  131. // 07/25/97 BRH Fixed the problem of bouys greater than 254 being
  132. // created in the editor.
  133. //
  134. // 07/30/97 BRH Added a flag to indicate whether the bouys should be shown
  135. // or not so that they can be turned off in the editor.
  136. //
  137. // 08/02/97 JMI Made bouy font smaller (was 22, now 15), made bouy font
  138. // brighter (was 1 (dark red), now 249 (bright red) ), and
  139. // widened bouy graphic in an attempt to make IDs more read-
  140. // able.
  141. //
  142. // 08/05/97 JMI Changed priority to use Z position rather than 2D
  143. // projected Y position.
  144. //
  145. // 08/05/97 BRH Defaulted the bouy network to ON.
  146. //
  147. // 08/08/97 BRH Only the bouys of the current Nav Net are displayed now.
  148. // (and their hots are disabled when they are not shown).
  149. // This will help cut down the confusion and prevent
  150. // users from joining the networks which would be bad.
  151. //
  152. // 08/08/97 JMI Now calls GetResources() on Load() and only in edit mode.
  153. //
  154. // 08/08/97 JMI Now calls GetResources() in Startup() but checks the realm
  155. // flag indicating whether we're in edit mode first to make
  156. // sure we are.
  157. //
  158. ////////////////////////////////////////////////////////////////////////////////
  159. #define BOUY_CPP
  160. #include "RSPiX.h"
  161. #include <math.h>
  162. #include "bouy.h"
  163. #include "navnet.h"
  164. ////////////////////////////////////////////////////////////////////////////////
  165. // Macros/types/etc.
  166. ////////////////////////////////////////////////////////////////////////////////
  167. #define IMAGE_FILE "bouy.bmp"
  168. #define BOUY_ID_FONT_HEIGHT 15
  169. #define BOUY_ID_FONT_COLOR 249
  170. ////////////////////////////////////////////////////////////////////////////////
  171. // Variables/data
  172. ////////////////////////////////////////////////////////////////////////////////
  173. // These are default values -- actually values are set using the editor!
  174. // Let this auto-init to 0
  175. short CBouy::ms_sFileCount;
  176. bool CBouy::ms_bShowBouys = true;
  177. ////////////////////////////////////////////////////////////////////////////////
  178. // Load object (should call base class version!)
  179. ////////////////////////////////////////////////////////////////////////////////
  180. short CBouy::Load( // Returns 0 if successfull, non-zero otherwise
  181. RFile* pFile, // In: File to load from
  182. bool bEditMode, // In: True for edit mode, false otherwise
  183. short sFileCount, // In: File count (unique per file, never 0)
  184. ULONG ulFileVersion) // In: Version of file format to load.
  185. {
  186. GameMessage msg;
  187. // Call the base load to get the u16InstanceID
  188. short sResult = CThing::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  189. if (sResult == 0)
  190. {
  191. // Load common data just once per file (not with each object)
  192. if (ms_sFileCount != sFileCount)
  193. {
  194. ms_sFileCount = sFileCount;
  195. // Load static data
  196. switch (ulFileVersion)
  197. {
  198. default:
  199. case 1:
  200. break;
  201. }
  202. }
  203. // Load object data
  204. pFile->Read(&m_dX);
  205. pFile->Read(&m_dY);
  206. pFile->Read(&m_dZ);
  207. U16 u16Data;
  208. U16 u16NumLinks;
  209. U16 i;
  210. // Get number of links to be read
  211. pFile->Read(&u16NumLinks);
  212. for (i = 0; i < u16NumLinks; i++)
  213. {
  214. pFile->Read(&u16Data);
  215. m_LinkInstanceID.InsertTail(u16Data);
  216. }
  217. // Get the instance ID for the NavNet
  218. pFile->Read(&u16Data);
  219. m_u16ParentInstanceID = u16Data;
  220. // Switch on the parts that have changed
  221. switch (ulFileVersion)
  222. {
  223. case 6:
  224. case 7:
  225. case 8:
  226. case 9:
  227. case 10:
  228. case 11:
  229. msg.Load(pFile);
  230. break;
  231. default:
  232. break;
  233. }
  234. // If the file version is earlier than the change to real 3D coords . . .
  235. if (ulFileVersion < 24)
  236. {
  237. // Convert to 3D.
  238. m_pRealm->MapY2DtoZ3D(
  239. m_dZ,
  240. &m_dZ);
  241. }
  242. // Make sure there were no file errors or format errors . . .
  243. if (!pFile->Error() && sResult == 0)
  244. {
  245. #if 0
  246. // If you were thinking of doing something like this, stop.
  247. // It is too early to create the image for displaying this
  248. // bouy (we don't yet have the bouy ID).
  249. // ONLY IN EDIT MODE . . .
  250. if (bEditMode == true)
  251. {
  252. // Get resources
  253. sResult = GetResources();
  254. }
  255. #endif
  256. }
  257. else
  258. {
  259. sResult = -1;
  260. TRACE("CBouy::Load(): Error reading from file!\n");
  261. }
  262. }
  263. else
  264. {
  265. TRACE("CBouy::Load(): CThing::Load() failed.\n");
  266. }
  267. return sResult;
  268. }
  269. ////////////////////////////////////////////////////////////////////////////////
  270. // Save object (should call base class version!)
  271. ////////////////////////////////////////////////////////////////////////////////
  272. short CBouy::Save( // Returns 0 if successfull, non-zero otherwise
  273. RFile* pFile, // In: File to save to
  274. short sFileCount) // In: File count (unique per file, never 0)
  275. {
  276. // Call the base class save to save the u16InstanceID
  277. CThing::Save(pFile, sFileCount);
  278. // Save common data just once per file (not with each object)
  279. if (ms_sFileCount != sFileCount)
  280. {
  281. ms_sFileCount = sFileCount;
  282. // Save static data
  283. }
  284. // Save object data
  285. pFile->Write(&m_dX);
  286. pFile->Write(&m_dY);
  287. pFile->Write(&m_dZ);
  288. U16 u16Data = m_sNumDirectLinks;
  289. // Save the number of links that will follow in the file
  290. pFile->Write(&u16Data);
  291. CBouy* pLinkedBouy = m_aplDirectLinks.GetHead();
  292. while (pLinkedBouy != NULL)
  293. {
  294. u16Data = pLinkedBouy->GetInstanceID();
  295. pFile->Write(&u16Data);
  296. pLinkedBouy = m_aplDirectLinks.GetNext();
  297. }
  298. // Save the instance ID for the parent NavNet so it can be connected
  299. // again after load
  300. u16Data = m_pParentNavNet->GetInstanceID();
  301. pFile->Write(&u16Data);
  302. if (pFile->Error())
  303. return FAILURE;
  304. else
  305. return SUCCESS;
  306. }
  307. ////////////////////////////////////////////////////////////////////////////////
  308. // Startup object
  309. ////////////////////////////////////////////////////////////////////////////////
  310. short CBouy::Startup(void) // Returns 0 if successfull, non-zero otherwise
  311. {
  312. short sResult = 0;
  313. // At this point we can assume the CHood was loaded, so we init our height
  314. m_dY = m_pRealm->GetHeight((short) m_dX, (short) m_dZ);
  315. // Init other stuff
  316. // Get pointer to Navigation Net
  317. // If we don't have a pointer to the nav net yet, get it from the ID
  318. if (m_pParentNavNet == NULL)
  319. {
  320. m_pRealm->m_idbank.GetThingByID((CThing**) &m_pParentNavNet, m_u16ParentInstanceID);
  321. // Re-register yourself with the network.
  322. m_pParentNavNet->AddBouy(this);
  323. linkinstanceid::Pointer i;
  324. CBouy* pBouy = NULL;
  325. for (i = m_LinkInstanceID.GetHead(); i != 0; i = m_LinkInstanceID.GetNext(i))
  326. {
  327. m_pRealm->m_idbank.GetThingByID((CThing**) &pBouy, m_LinkInstanceID.GetData(i));
  328. // If its not already linked, then add it.
  329. if (!m_aplDirectLinks.Find(pBouy))
  330. {
  331. m_aplDirectLinks.AddTail(pBouy);
  332. m_sNumDirectLinks++;
  333. }
  334. }
  335. // Only in edit mode . . .
  336. if (m_pRealm->m_flags.bEditing == true)
  337. {
  338. sResult = GetResources();
  339. }
  340. }
  341. return sResult;
  342. }
  343. ////////////////////////////////////////////////////////////////////////////////
  344. // Shutdown object
  345. ////////////////////////////////////////////////////////////////////////////////
  346. short CBouy::Shutdown(void) // Returns 0 if successfull, non-zero otherwise
  347. {
  348. return 0;
  349. }
  350. ////////////////////////////////////////////////////////////////////////////////
  351. // Suspend object
  352. ////////////////////////////////////////////////////////////////////////////////
  353. void CBouy::Suspend(void)
  354. {
  355. m_sSuspend++;
  356. }
  357. ////////////////////////////////////////////////////////////////////////////////
  358. // Resume object
  359. ////////////////////////////////////////////////////////////////////////////////
  360. void CBouy::Resume(void)
  361. {
  362. m_sSuspend--;
  363. // If we're actually going to start updating again, we need to reset
  364. // the time so as to ignore any time that passed while we were suspended.
  365. // This method is far from precise, but I'm hoping it's good enough.
  366. }
  367. ////////////////////////////////////////////////////////////////////////////////
  368. // Update object
  369. ////////////////////////////////////////////////////////////////////////////////
  370. void CBouy::Update(void)
  371. {
  372. }
  373. ////////////////////////////////////////////////////////////////////////////////
  374. // AddLink - Add a 1 hop direct link to the routing table
  375. ////////////////////////////////////////////////////////////////////////////////
  376. short CBouy::AddLink(CBouy* pBouy)
  377. {
  378. short sReturn = SUCCESS;
  379. if (!m_aplDirectLinks.Find(pBouy))
  380. {
  381. m_aplDirectLinks.AddTail(pBouy);
  382. m_sNumDirectLinks++;
  383. }
  384. return sReturn;
  385. }
  386. ////////////////////////////////////////////////////////////////////////////////
  387. // Render object
  388. ////////////////////////////////////////////////////////////////////////////////
  389. void CBouy::Render(void)
  390. {
  391. }
  392. ////////////////////////////////////////////////////////////////////////////////
  393. // Called by editor to init new object at specified position
  394. ////////////////////////////////////////////////////////////////////////////////
  395. short CBouy::EditNew( // Returns 0 if successfull, non-zero otherwise
  396. short sX, // In: New x coord
  397. short sY, // In: New y coord
  398. short sZ) // In: New z coord
  399. {
  400. short sResult = 0;
  401. // Use specified position
  402. m_dX = (double)sX;
  403. m_dY = (double)sY;
  404. m_dZ = (double)sZ;
  405. // Since we were created in the editor, set our Nav Net
  406. m_pParentNavNet = m_pRealm->GetCurrentNavNet();
  407. if (m_pParentNavNet->AddBouy(this) == 0)
  408. sResult = FAILURE;
  409. else
  410. // Load resources
  411. sResult = GetResources();
  412. return sResult;
  413. }
  414. ////////////////////////////////////////////////////////////////////////////////
  415. // Called by editor to modify object
  416. ////////////////////////////////////////////////////////////////////////////////
  417. short CBouy::EditModify(void)
  418. {
  419. return 0;
  420. }
  421. ////////////////////////////////////////////////////////////////////////////////
  422. // Called by editor to move object to specified position
  423. ////////////////////////////////////////////////////////////////////////////////
  424. short CBouy::EditMove( // Returns 0 if successfull, non-zero otherwise
  425. short sX, // In: New x coord
  426. short sY, // In: New y coord
  427. short sZ) // In: New z coord
  428. {
  429. m_dX = (double)sX;
  430. m_dY = (double)sY;
  431. m_dZ = (double)sZ;
  432. return 0;
  433. }
  434. ////////////////////////////////////////////////////////////////////////////////
  435. // Called by editor to update object
  436. ////////////////////////////////////////////////////////////////////////////////
  437. void CBouy::EditUpdate(void)
  438. {
  439. }
  440. ////////////////////////////////////////////////////////////////////////////////
  441. // Called by editor to render object
  442. ////////////////////////////////////////////////////////////////////////////////
  443. void CBouy::EditRender(void)
  444. {
  445. // No special flags
  446. if (ms_bShowBouys)
  447. {
  448. if (m_pParentNavNet == m_pRealm->GetCurrentNavNet())
  449. {
  450. m_sprite.m_sInFlags = 0;
  451. m_phot->SetActive(TRUE);
  452. }
  453. else
  454. {
  455. m_sprite.m_sInFlags = CSprite::InHidden;
  456. m_phot->SetActive(FALSE);
  457. }
  458. }
  459. else
  460. m_sprite.m_sInFlags = CSprite::InHidden;
  461. // Map from 3d to 2d coords
  462. Map3Dto2D(
  463. (short) m_dX,
  464. (short) m_dY,
  465. (short) m_dZ,
  466. &m_sprite.m_sX2,
  467. &m_sprite.m_sY2);
  468. // Center on image.
  469. m_sprite.m_sX2 -= m_pImage->m_sWidth / 2;
  470. m_sprite.m_sY2 -= m_pImage->m_sHeight;
  471. // Priority is based on bottom edge of sprite
  472. m_sprite.m_sPriority = m_dZ;
  473. // Layer should be based on info we get from attribute map.
  474. m_sprite.m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer((short) m_dX, (short) m_dZ));
  475. // Image would normally animate, but doesn't for now
  476. m_sprite.m_pImage = m_pImage;
  477. // Update sprite in scene
  478. m_pRealm->m_scene.UpdateSprite(&m_sprite);
  479. }
  480. ////////////////////////////////////////////////////////////////////////////////
  481. // Give Edit a rectangle around this object
  482. ////////////////////////////////////////////////////////////////////////////////
  483. void CBouy::EditRect(RRect* pRect)
  484. {
  485. Map3Dto2D(
  486. m_dX,
  487. m_dY,
  488. m_dZ,
  489. &(pRect->sX),
  490. &(pRect->sY) );
  491. pRect->sW = 10; // Safety.
  492. pRect->sH = 10; // Safety.
  493. if (m_pImage != NULL)
  494. {
  495. pRect->sW = m_pImage->m_sWidth;
  496. pRect->sH = m_pImage->m_sHeight;
  497. }
  498. pRect->sX -= pRect->sW / 2;
  499. pRect->sY -= pRect->sH;
  500. }
  501. ////////////////////////////////////////////////////////////////////////////////
  502. // Called by editor to get the hotspot of an object in 2D.
  503. ////////////////////////////////////////////////////////////////////////////////
  504. void CBouy::EditHotSpot( // Returns nothiing.
  505. short* psX, // Out: X coord of 2D hotspot relative to
  506. // EditRect() pos.
  507. short* psY) // Out: Y coord of 2D hotspot relative to
  508. // EditRect() pos.
  509. {
  510. // Base of bouy is hotspot.
  511. *psX = (m_pImage->m_sWidth / 2);
  512. *psY = m_pImage->m_sHeight;
  513. }
  514. ////////////////////////////////////////////////////////////////////////////////
  515. // Get all required resources
  516. ////////////////////////////////////////////////////////////////////////////////
  517. short CBouy::GetResources(void) // Returns 0 if successfull, non-zero otherwise
  518. {
  519. short sResult = 0;
  520. if (m_pImage == 0)
  521. {
  522. RImage* pimBouyRes;
  523. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(IMAGE_FILE), &pimBouyRes);
  524. if (sResult == 0)
  525. {
  526. // Allocate image . . .
  527. m_pImage = new RImage;
  528. if (m_pImage != NULL)
  529. {
  530. // Allocate image data . . .
  531. if (m_pImage->CreateImage(
  532. pimBouyRes->m_sWidth,
  533. pimBouyRes->m_sHeight,
  534. RImage::BMP8) == 0)
  535. {
  536. // Blt bouy res.
  537. rspBlit(
  538. pimBouyRes, // Src.
  539. m_pImage, // Dst.
  540. 0, // Dst.
  541. 0, // Dst.
  542. NULL); // Dst clip.
  543. // Put in ID.
  544. RPrint print;
  545. print.SetFont(BOUY_ID_FONT_HEIGHT, &g_fontBig);
  546. print.SetColor(BOUY_ID_FONT_COLOR);
  547. print.SetJustifyCenter();
  548. print.print(
  549. m_pImage, // Dst.
  550. 0, // Dst.
  551. m_pImage->m_sHeight - BOUY_ID_FONT_HEIGHT, // Dst.
  552. "%d", // Format.
  553. (short)m_ucID); // Src.
  554. // Convert to efficient transparent blit format . . .
  555. if (m_pImage->Convert(RImage::FSPR8) != RImage::FSPR8)
  556. {
  557. sResult = -3;
  558. TRACE("CBouy::GetResource() - Couldn't convert to FSPR8\n");
  559. }
  560. }
  561. else
  562. {
  563. sResult = -2;
  564. TRACE("CBouy::GetResource() - m_pImage->CreateImage() failed.\n");
  565. }
  566. // If an error occurred after allocation . . .
  567. if (sResult != 0)
  568. {
  569. delete m_pImage;
  570. m_pImage = NULL;
  571. }
  572. }
  573. else
  574. {
  575. sResult = -1;
  576. TRACE("CBouy::GetResource(): Failed to allocate RImage.\n");
  577. }
  578. rspReleaseResource(&g_resmgrGame, &pimBouyRes);
  579. }
  580. }
  581. return sResult;
  582. }
  583. ////////////////////////////////////////////////////////////////////////////////
  584. // Free all resources
  585. ////////////////////////////////////////////////////////////////////////////////
  586. short CBouy::FreeResources(void) // Returns 0 if successfull, non-zero otherwise
  587. {
  588. if (m_pImage != NULL)
  589. {
  590. delete m_pImage;
  591. m_pImage = NULL;
  592. }
  593. return 0;
  594. }
  595. ////////////////////////////////////////////////////////////////////////////////
  596. // Unlink - Visit your direct links and unlink yourself from them, then free
  597. // your own links.
  598. ////////////////////////////////////////////////////////////////////////////////
  599. void CBouy::Unlink(void)
  600. {
  601. // Follow all of this bouy's direct links and unlink this bouy
  602. CBouy* pLinkedBouy = m_aplDirectLinks.GetHead();
  603. while (pLinkedBouy)
  604. {
  605. pLinkedBouy->m_aplDirectLinks.Remove(this);
  606. pLinkedBouy->m_sNumDirectLinks--;
  607. pLinkedBouy = m_aplDirectLinks.GetNext();
  608. }
  609. // Erase all of your own links to other bouys
  610. m_aplDirectLinks.Reset();
  611. m_sNumDirectLinks = 0;
  612. // Remove this bouy from the network
  613. m_pParentNavNet->RemoveBouy(m_ucID);
  614. }
  615. ////////////////////////////////////////////////////////////////////////////////
  616. // BuildRoutingTable - Fills in the routing table by building a BSF tree and
  617. // using the hop counts and parent tree, fills in the
  618. // routing table.
  619. ////////////////////////////////////////////////////////////////////////////////
  620. short CBouy::BuildRoutingTable(void)
  621. {
  622. short sResult = SUCCESS;
  623. UCHAR* aVisited = NULL;
  624. UCHAR* aDistance = NULL;
  625. UCHAR* aParent = NULL;
  626. UCHAR* pucCurrentNode = NULL;
  627. UCHAR* pucAdjNode = NULL;
  628. CBouy* pTraverseBouy = NULL;
  629. ASSERT(m_pParentNavNet != NULL);
  630. short sCurrentNumNodes = m_pParentNavNet->GetNumNodes();
  631. RQueue <UCHAR, 256> bfsQueue;
  632. // Make sure there is enough space in the routing table, or
  633. // reallocate it if there isn't enough.
  634. if (m_sRouteTableSize < sCurrentNumNodes)
  635. {
  636. if (m_paucRouteTable != NULL)
  637. free(m_paucRouteTable);
  638. m_paucRouteTable = (UCHAR*) malloc(sCurrentNumNodes);
  639. m_sRouteTableSize = sCurrentNumNodes;
  640. }
  641. // Allocate memory for use in building the BSF tree
  642. aVisited = (UCHAR*) malloc(sCurrentNumNodes);
  643. aDistance = (UCHAR*) malloc(sCurrentNumNodes);
  644. aParent = (UCHAR*) malloc(sCurrentNumNodes);
  645. if (m_paucRouteTable != NULL &&
  646. aVisited != NULL &&
  647. aDistance != NULL &&
  648. aParent != NULL)
  649. {
  650. // Initialize the table to unreachable and initialize the
  651. // BSF data structures.
  652. memset(m_paucRouteTable, 255, m_sRouteTableSize);
  653. memset(aVisited, 0, sCurrentNumNodes);
  654. memset(aDistance, 255, sCurrentNumNodes);
  655. memset(aParent, 0, sCurrentNumNodes);
  656. // Breadth-First Search
  657. aVisited[m_ucID] = TRUE;
  658. aDistance[m_ucID] = 0;
  659. bfsQueue.EnQ(&m_ucID);
  660. while (!bfsQueue.IsEmpty())
  661. {
  662. pucCurrentNode = bfsQueue.DeQ();
  663. pTraverseBouy = m_pParentNavNet->GetBouy(*pucCurrentNode);
  664. CBouy* pLinkedBouy = pTraverseBouy->m_aplDirectLinks.GetHead();
  665. while (pLinkedBouy)
  666. {
  667. pucAdjNode = &(pLinkedBouy->m_ucID);
  668. if (aVisited[*pucAdjNode] == FALSE)
  669. {
  670. aVisited[*pucAdjNode] = TRUE;
  671. aParent[*pucAdjNode] = *pucCurrentNode;
  672. aDistance[*pucAdjNode] = aDistance[*pucCurrentNode] + 1;
  673. bfsQueue.EnQ(pucAdjNode);
  674. }
  675. pLinkedBouy = pTraverseBouy->m_aplDirectLinks.GetNext();
  676. }
  677. }
  678. // Breadth-First Search complete.
  679. // Now the aDistance contains the hop count to all other connected nodes
  680. // and aParent provides a way to build the routing table by traversing
  681. // backwards.
  682. UCHAR ucCurrentDistance;
  683. UCHAR curr;
  684. short j;
  685. for (j = 1; j < sCurrentNumNodes; j++)
  686. {
  687. ucCurrentDistance = aDistance[j];
  688. // If distance is 255 (infinite flag) then mark that node
  689. // as unreachable in the routing table using 255 as the flag
  690. if (ucCurrentDistance == 255)
  691. {
  692. m_paucRouteTable[j] = 255;
  693. }
  694. else
  695. {
  696. // If distance is 0, then this is the node, and mark it in the
  697. // routing table as 0, meaning you have reached your final destination
  698. if (j == m_ucID)
  699. {
  700. m_paucRouteTable[j] = 0;
  701. }
  702. else
  703. {
  704. // If the distance is 1, it is a directly connected node, so put
  705. // its ID in the routing table as the node to go to.
  706. if (ucCurrentDistance == 1)
  707. {
  708. m_paucRouteTable[j] = j;
  709. }
  710. else
  711. {
  712. // Else, its a child of a directly connected node, so find out which
  713. // one by tracing back along the parent tree.
  714. curr = j;
  715. while (aParent[curr] != m_ucID)
  716. curr = aParent[curr];
  717. m_paucRouteTable[j] = curr;
  718. }
  719. }
  720. }
  721. }
  722. }
  723. else
  724. {
  725. TRACE("CBouy::BuildRoutingTable: Error allocating memory for tables for bouy %d\n", m_ucID);
  726. sResult = -1;
  727. }
  728. if (aVisited)
  729. free(aVisited);
  730. if (aDistance)
  731. free(aDistance);
  732. if (aParent)
  733. free(aParent);
  734. return sResult;
  735. }
  736. ////////////////////////////////////////////////////////////////////////////////
  737. // NextRouteNode - Tells you which node to go to next to get to your destination
  738. ////////////////////////////////////////////////////////////////////////////////
  739. UCHAR CBouy::NextRouteNode(UCHAR dst)
  740. {
  741. if (dst >= m_pParentNavNet->GetNumNodes())
  742. return 255;
  743. else
  744. return m_paucRouteTable[dst];
  745. }
  746. ////////////////////////////////////////////////////////////////////////////////
  747. // EOF
  748. ////////////////////////////////////////////////////////////////////////////////