dispenser.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  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. // dispenser.cpp
  19. // Project: Nostril (aka Postal)
  20. //
  21. // This module impliments the CDispenser class, which is a simple one animationed
  22. // 3D object.
  23. //
  24. // History:
  25. // 03/19/97 JMI Started this dispenser item class using CDispenser as a
  26. // template.
  27. // Will not hose you, but not yet functional. Not recommended
  28. // for saving with your realm yet. Might change loading
  29. // somewhat, but probably not.
  30. //
  31. // 03/20/97 JMI Now has VERY simple timer logic.
  32. // Has large problem with statics' sFileCount parameter to
  33. // Load().
  34. //
  35. // 03/25/97 JMI Changed EDIT_GUI_FILE to 8.3 name.
  36. //
  37. // 04/04/97 JMI No longer uses construct with ID in InstantiateDispensee().
  38. //
  39. // 04/08/97 JMI Although I was checking to make sure I did not mod by 0
  40. // when determining the next time for dispensage, if it was
  41. // the case that I would've mod'ed by 0, I was simply not
  42. // setting a new time causing the dispenser to just pump out
  43. // one dude per iteration and, indirectly, this run on sen-
  44. // tence. Fixed.
  45. //
  46. // 04/10/97 BRH Updated this to work with the new multi layer attribute
  47. // maps.
  48. //
  49. // 04/23/97 JMI Added new logic type Exists and added a max for the number
  50. // of dispensees and a current number of dispensees dispensed.
  51. //
  52. // 05/29/97 JMI Removed ASSERT on m_pRealm->m_pAttribMap which no longer
  53. // exists.
  54. //
  55. // 06/14/97 JMI Upgraded to use new DoGui() method for sub dialogs
  56. // involving GuiPressed().
  57. //
  58. // 06/17/97 JMI Converted all occurrences of rand() to GetRand() and
  59. // srand() to SeedRand().
  60. //
  61. // 06/27/97 JMI Now uses RListBox::EnsureVisible() to make sure the selected
  62. // dispensee type is visible in EditModify().
  63. // Also, displays dispensee type via text in EditRender().
  64. // Also, now updates the dispensee's position in Save()
  65. // instead of EditMove() to speed up dragging.
  66. // Also, now uses Map3Dto2D() in EditRender() and EditRect().
  67. //
  68. // 06/27/97 JMI Now shows actual dispensee in a deluxe way adding only an
  69. // icon indicating we're a dispenser.
  70. //
  71. // 06/27/97 JMI Added temp fix to cause the dispenser m_imRender to be
  72. // created on load. But it is very cheesy.
  73. //
  74. // 06/28/97 JMI Added a function to render the dispensee,
  75. // RenderDisipensee() which is now called in the various
  76. // places that need to update the dispensee's icon.
  77. //
  78. // 06/30/97 JMI Render() was using m_pim where it should had been using
  79. // m_imRender for determining the height for offseting the
  80. // priority.
  81. // Now uses the dispensee's actual EditRect() and
  82. // EditHotSpot().
  83. //
  84. // 06/30/97 JMI Now maps the Z to 3D when loading fileversions previous to
  85. // 24.
  86. //
  87. // 07/03/97 JMI Now uses SetGuiToNotify() to make a button able to end a
  88. // DoGui() session.
  89. //
  90. // 07/09/97 JMI Now uses m_pRealm->Make2dResPath() to get the fullpath
  91. // for 2D image components.
  92. //
  93. // 07/10/97 JMI Added GetClosestDude().
  94. // Also, added new logic type DistanceToDude.
  95. //
  96. // 07/14/97 JMI Was only instantiating the dispensee in edit mode. This
  97. // had the potential of causing the dispensee's ms_sFileCount
  98. // to be different when loading in edit mode than in non edit
  99. // mode and, therefore, could cause it to load its statics
  100. // different amounts of times between modes. Changed it to
  101. // be consistent and instantiate the dispensee even when
  102. // it doesn't need to in non-edit mode. It just only renders
  103. // the dispensee in edit mode loads.
  104. //
  105. // 07/21/97 JMI Now, if it has no thing ptr to use to create the editor
  106. // icon, it chooses a size of WIDTH_IF_NO_THING x
  107. // HEIGHT_IF_NO_THING.
  108. //
  109. // 07/27/97 JMI Now m_sMaxDispensees as a negative indicates infinite
  110. // dispensees. This is user selectable via a checkbox in the
  111. // dialog.
  112. //
  113. // 07/28/97 JMI Now sets m_u16IdDispensee to IdNil after instantiating a
  114. // thing for renderage. This was causing a problem with
  115. // 'Exists' logic.
  116. // Also, added initial delay for all logics.
  117. // Changed m_alLogicParms[0],2,3 to m_alLogicParms[4] (adding one
  118. // logic parm while changing the storage technique).
  119. // Made dialog more deluxe to handle new parm and
  120. // descriptions.
  121. // Checking in to work at home...may not be up to par.
  122. //
  123. // 07/28/97 JMI Now uses initial delay to set the next update time.
  124. // Also, now word wraps description in GUI.
  125. // Also, now displays a message when no dispensee type chosen.
  126. //
  127. // JMI Changed DestroyDispensee() to not ASSERT on a NULL ptr.
  128. //
  129. // 07/29/97 JMI Changed logic descriptions.
  130. //
  131. // 08/05/97 JMI Changed priority to use Z position rather than 2D
  132. // projected Y position.
  133. //
  134. // 08/10/97 JMI Now lets you jump right to editting the dispensee by
  135. // holding down ALT key when you choose EditModify().
  136. //
  137. ////////////////////////////////////////////////////////////////////////////////
  138. #define DISPENSER_CPP
  139. #include "RSPiX.h"
  140. #include <math.h>
  141. #include "dispenser.h"
  142. #include "reality.h"
  143. ////////////////////////////////////////////////////////////////////////////////
  144. // Macros/types/etc.
  145. ////////////////////////////////////////////////////////////////////////////////
  146. #define EDIT_GUI_FILE "res/editor/Dispense.gui"
  147. #define RES_FILE "dispenser.bmp"
  148. #define THINGS_LIST_BOX_ID 3
  149. #define LOGICS_LIST_BOX_ID 4
  150. #define LOGIC_PARMS_EDIT_ID_BASE 101
  151. #define LOGIC_PARMS_TEXT_ID_BASE 201
  152. #define BTN_MODIFY_DISPENSEE_ID 8
  153. #define MAX_DISPENSEES_EDIT_ID 9
  154. #define INFINITE_DISPENSEES_MB_ID 20
  155. #define DESCRIPTION_TEXT_ID 300
  156. // Font settings for displaying dispensee's that don't return a sprite.
  157. #define FONT_SIZE 15
  158. #define FONT_COLOR 249
  159. // Width and Height if no thing. Allow enough to read text.
  160. #define WIDTH_IF_NO_THING 60
  161. #define HEIGHT_IF_NO_THING 30
  162. ////////////////////////////////////////////////////////////////////////////////
  163. // Variables/data
  164. ////////////////////////////////////////////////////////////////////////////////
  165. // Let this auto-init to 0
  166. short CDispenser::ms_sFileCount;
  167. short CDispenser::ms_sDispenseeFileCount;
  168. // Descriptions of logic types and their parameters.
  169. CDispenser::LogicInfo CDispenser::ms_aliLogics[NumLogicTypes] =
  170. {
  171. ////////////////////////// Timed ////////////////////////////////////////
  172. {
  173. "Timed", // Logic Name.
  174. { // Parm descriptions. NULL for each entry that does not exist.
  175. "Min delay (ms):",
  176. "Max delay (ms):",
  177. "Initial delay (ms):",
  178. NULL,
  179. },
  180. // Description of logic.
  181. #if 1
  182. "\"Initial delay\" is the amount of time before dispensing begins.\n"
  183. "\"Min delay\" is the minimum amount of time before the next item is dispensed.\n"
  184. "\"Max delay\" is the maximum amount of time before the next item is dispensed.\n",
  185. #else
  186. "After the initial delay, dispenses a dispensee between \"Min delay\" "
  187. "and \"Max delay\" milliseconds.",
  188. #endif
  189. },
  190. ////////////////////////// Exists ///////////////////////////////////////
  191. {
  192. "Exists", // Logic Name
  193. { // Parm descriptions. NULL for each entry that does not exist.
  194. "Min delay (ms):",
  195. "Max delay (ms):",
  196. "Initial delay (ms):",
  197. NULL,
  198. },
  199. // Description of logic.
  200. #if 1
  201. "\"Initial delay\" is the amount of time before dispensing begins.\n"
  202. "\"Min delay\" is the minimum amount of time after the previously dispensed item "
  203. "is destroyed or killed that the next item will be dispensed.\n"
  204. "\"Max delay\" is the maximum amount of time after the previously dispensed item "
  205. "is destroyed or killed that the next item will be dispensed.\n",
  206. #else
  207. "After the initial delay, dispesenses a dispensee. Does not dispense the "
  208. "next dispensee until between \"Min delay\" and \"Max delay\" milliseconds "
  209. "after the previous one has been destroyed/killed.",
  210. #endif
  211. },
  212. ///////////////////// Distance To Dude ///////////////////////////////////
  213. {
  214. "Distance to Dude", // Logic Name.
  215. { // Parm descriptions. NULL for each entry that does not exist.
  216. "Min distance (0 = none):",
  217. "Max distance (0 = none):",
  218. "How often to check (ms):",
  219. "Initial delay (ms):",
  220. },
  221. // Description of logic.
  222. #if 1
  223. "\"Initial delay\" is the amount of time before dispensing begins.\n"
  224. "\"How often to check\" is the amount of time between checking the distance "
  225. "from the dispenser to the main dude.\n"
  226. "\"Min distance\" to \"Max distance\" is the range of distances to the closest "
  227. "main dude that will cause the next item to be dispensed.\n",
  228. #else
  229. "After the initial delay, checks the closest main dude every \"How often "
  230. "to check\" milliseconds and dispenses a new dispensee if the main dude "
  231. "is within \"Min distance\" to \"Max distance\" pixels at that time. Not "
  232. "for use with other toys.",
  233. #endif
  234. },
  235. };
  236. ////////////////////////////////////////////////////////////////////////////////
  237. // Local function prototypes
  238. ////////////////////////////////////////////////////////////////////////////////
  239. void LogicItemCall(
  240. RGuiItem* pguiLogicItem); // In: Logic item that was pressed.
  241. ////////////////////////////////////////////////////////////////////////////////
  242. // Load object (should call base class version!)
  243. ////////////////////////////////////////////////////////////////////////////////
  244. short CDispenser::Load( // Returns 0 if successfull, non-zero otherwise
  245. RFile* pFile, // In: File to load from
  246. bool bEditMode, // In: True for edit mode, false otherwise
  247. short sFileCount, // In: File count (unique per file, never 0)
  248. ULONG ulFileVersion) // In: Version of file format to load.
  249. {
  250. short sResult = 0;
  251. // In most cases, the base class Load() should be called.
  252. sResult = CThing::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  253. if (sResult == 0)
  254. {
  255. // Load common data just once per file (not with each object)
  256. if (ms_sFileCount != sFileCount)
  257. {
  258. ms_sFileCount = sFileCount;
  259. // Load static data
  260. switch (ulFileVersion)
  261. {
  262. default:
  263. case 1:
  264. break;
  265. }
  266. }
  267. // Load object data
  268. switch (ulFileVersion)
  269. {
  270. default:
  271. case 35:
  272. pFile->Read(&m_sMaxDispensees);
  273. pFile->Read(&m_sX);
  274. pFile->Read(&m_sY);
  275. pFile->Read(&m_sZ);
  276. pFile->Read(&m_idDispenseeType);
  277. U16 u16LogicType;
  278. pFile->Read(&u16LogicType);
  279. m_logictype = (LogicType)u16LogicType;
  280. pFile->Read(m_alLogicParms, 4);
  281. pFile->Read(&m_ulFileVersion);
  282. long lSize;
  283. if (pFile->Read(&lSize) == 1)
  284. {
  285. // Open memory file to receive the clone data . . .
  286. if (m_fileDispensee.Open(lSize, 1L, (RFile::Endian)pFile->GetEndian()) == 0)
  287. {
  288. // Put 'er there.
  289. pFile->Read(m_fileDispensee.GetMemory(), lSize);
  290. }
  291. }
  292. // Had to start the whole format over b/c the number of parms changed and I
  293. // didn't want to read the parms all over the place. The unfortunate thing
  294. // is that I end up with two versions of reading the dispensee which could
  295. // likely need to be changed at some point (so it'd have to be change in two
  296. // locations). Perhaps I could make that part a separate inline. Not sure
  297. // now though.
  298. // Break out here intentionally.
  299. break;
  300. // Older format support with less parms. Make sure to init unused new parms
  301. // to zero.
  302. case 34:
  303. case 33:
  304. case 32:
  305. case 31:
  306. case 30:
  307. case 29:
  308. case 28:
  309. case 27:
  310. case 26:
  311. case 25:
  312. case 24:
  313. case 23:
  314. case 22:
  315. case 21:
  316. case 20:
  317. case 19:
  318. case 18:
  319. case 17:
  320. case 16:
  321. case 15:
  322. case 14:
  323. case 13:
  324. case 12:
  325. case 11:
  326. case 10:
  327. case 9:
  328. case 8:
  329. case 7:
  330. pFile->Read(&m_sMaxDispensees);
  331. case 6:
  332. case 5:
  333. case 4:
  334. case 3:
  335. case 2:
  336. case 1:
  337. {
  338. pFile->Read(&m_sX);
  339. pFile->Read(&m_sY);
  340. pFile->Read(&m_sZ);
  341. pFile->Read(&m_idDispenseeType);
  342. U16 u16LogicType;
  343. pFile->Read(&u16LogicType);
  344. m_logictype = (LogicType)u16LogicType;
  345. pFile->Read(m_alLogicParms + 0);
  346. pFile->Read(m_alLogicParms + 1);
  347. pFile->Read(m_alLogicParms + 2);
  348. short i;
  349. for (i = 3; i < NumParms; i++)
  350. {
  351. m_alLogicParms[i] = 0;
  352. }
  353. pFile->Read(&m_ulFileVersion);
  354. long lSize;
  355. if (pFile->Read(&lSize) == 1)
  356. {
  357. // Open memory file to receive the clone data . . .
  358. if (m_fileDispensee.Open(lSize, 1L, (RFile::Endian)pFile->GetEndian()) == 0)
  359. {
  360. // Put 'er there.
  361. pFile->Read(m_fileDispensee.GetMemory(), lSize);
  362. }
  363. }
  364. break;
  365. }
  366. }
  367. // If the file version is earlier than the change to real 3D coords . . .
  368. if (ulFileVersion < 24)
  369. {
  370. // Convert to 3D.
  371. m_pRealm->MapY2DtoZ3D(
  372. m_sZ,
  373. &m_sZ);
  374. }
  375. // Make sure there were no file errors or format errors . . .
  376. if (!pFile->Error() && sResult == 0)
  377. {
  378. // Init dispenser
  379. sResult = Init(bEditMode);
  380. }
  381. else
  382. {
  383. sResult = -1;
  384. TRACE("CDispenser::Load(): Error reading from file!\n");
  385. }
  386. }
  387. else
  388. {
  389. TRACE("CDispenser::Load(): CThing::Load() failed.\n");
  390. }
  391. return sResult;
  392. }
  393. ////////////////////////////////////////////////////////////////////////////////
  394. // Save object (should call base class version!)
  395. ////////////////////////////////////////////////////////////////////////////////
  396. short CDispenser::Save( // Returns 0 if successfull, non-zero otherwise
  397. RFile* pFile, // In: File to save to
  398. short sFileCount) // In: File count (unique per file, never 0)
  399. {
  400. short sResult = 0;
  401. // In most cases, the base class Save() should be called.
  402. sResult = CThing::Save(pFile, sFileCount);
  403. if (sResult == 0)
  404. {
  405. // Save common data just once per file (not with each object)
  406. if (ms_sFileCount != sFileCount)
  407. {
  408. ms_sFileCount = sFileCount;
  409. // Save static data
  410. }
  411. pFile->Write(m_sMaxDispensees);
  412. pFile->Write(m_sX);
  413. pFile->Write(m_sY);
  414. pFile->Write(m_sZ);
  415. pFile->Write(m_idDispenseeType);
  416. pFile->Write((U16)m_logictype);
  417. pFile->Write(m_alLogicParms, 4);
  418. // We do this here instead of on EditMove() b/c EditMove() can
  419. // be slow when on every iteration, it allocates a CWhatever,
  420. // loads it, sets the new position, saves it and deletes it.
  421. // Update position . . .
  422. CThing* pthing;
  423. if (InstantiateDispensee(&pthing, false) == 0)
  424. {
  425. // Update position.
  426. pthing->EditMove(m_sX, m_sY, m_sZ);
  427. // Resave.
  428. SaveDispensee(pthing);
  429. // Get rid of.
  430. DestroyDispensee(&pthing);
  431. }
  432. pFile->Write(m_ulFileVersion);
  433. pFile->Write(m_fileDispensee.GetSize());
  434. pFile->Write(m_fileDispensee.GetMemory(), m_fileDispensee.GetSize());
  435. sResult = pFile->Error();
  436. }
  437. else
  438. {
  439. TRACE("CDispenser::Save(): CThing::Save() failed.\n");
  440. }
  441. return sResult;
  442. }
  443. ////////////////////////////////////////////////////////////////////////////////
  444. // Startup object
  445. ////////////////////////////////////////////////////////////////////////////////
  446. short CDispenser::Startup(void) // Returns 0 if successfull, non-zero otherwise
  447. {
  448. switch (m_logictype)
  449. {
  450. case Timed:
  451. case Exists:
  452. m_lNextUpdate = m_pRealm->m_time.GetGameTime() + m_alLogicParms[2];
  453. break;
  454. case DistanceToDude:
  455. m_lNextUpdate = m_pRealm->m_time.GetGameTime() + m_alLogicParms[3];
  456. break;
  457. default:
  458. m_lNextUpdate = m_pRealm->m_time.GetGameTime();
  459. break;
  460. }
  461. return 0;
  462. }
  463. ////////////////////////////////////////////////////////////////////////////////
  464. // Shutdown object
  465. ////////////////////////////////////////////////////////////////////////////////
  466. short CDispenser::Shutdown(void) // Returns 0 if successfull, non-zero otherwise
  467. {
  468. return 0;
  469. }
  470. ////////////////////////////////////////////////////////////////////////////////
  471. // Suspend object
  472. ////////////////////////////////////////////////////////////////////////////////
  473. void CDispenser::Suspend(void)
  474. {
  475. if (m_sSuspend == 0)
  476. {
  477. // Store current delta so we can restore it.
  478. long lCurTime = m_pRealm->m_time.GetGameTime();
  479. m_lNextUpdate = lCurTime - m_lNextUpdate;
  480. }
  481. m_sSuspend++;
  482. }
  483. ////////////////////////////////////////////////////////////////////////////////
  484. // Resume object
  485. ////////////////////////////////////////////////////////////////////////////////
  486. void CDispenser::Resume(void)
  487. {
  488. m_sSuspend--;
  489. // If we're actually going to start updating again, we need to reset
  490. // the time so as to ignore any time that passed while we were suspended.
  491. // This method is far from precise, but I'm hoping it's good enough.
  492. if (m_sSuspend == 0)
  493. {
  494. long lCurTime = m_pRealm->m_time.GetGameTime();
  495. m_lNextUpdate = lCurTime - m_lNextUpdate;
  496. }
  497. }
  498. ////////////////////////////////////////////////////////////////////////////////
  499. // Update object
  500. ////////////////////////////////////////////////////////////////////////////////
  501. void CDispenser::Update(void)
  502. {
  503. if (!m_sSuspend)
  504. {
  505. long lCurTime = m_pRealm->m_time.GetGameTime();
  506. if (m_sNumDispensees < m_sMaxDispensees || m_sMaxDispensees < 0)
  507. {
  508. // Logic on when to dispense next.
  509. switch (m_logictype)
  510. {
  511. case Timed:
  512. if (lCurTime >= m_lNextUpdate)
  513. {
  514. // Create a thing . . .
  515. CThing* pthing;
  516. if (InstantiateDispensee(&pthing, false) == 0)
  517. {
  518. // Wahoo.
  519. m_sNumDispensees++;
  520. }
  521. if (m_alLogicParms[1] - m_alLogicParms[0] > 0)
  522. {
  523. // Next update will be in a min of m_alLogicParms[0] and a max of m_alLogicParms[1]
  524. // milliseconds.
  525. m_lNextUpdate = lCurTime + m_alLogicParms[0] + (GetRand() % (m_alLogicParms[1] - m_alLogicParms[0]));
  526. }
  527. else
  528. {
  529. m_lNextUpdate = lCurTime + m_alLogicParms[0];
  530. }
  531. }
  532. break;
  533. case Exists:
  534. {
  535. // If we don't have one . . .
  536. if (m_u16IdDispensee == CIdBank::IdNil)
  537. {
  538. if (lCurTime >= m_lNextUpdate)
  539. {
  540. // Create a thing . . .
  541. CThing* pthing;
  542. if (InstantiateDispensee(&pthing, false) == 0)
  543. {
  544. // Wahoo.
  545. m_sNumDispensees++;
  546. }
  547. }
  548. }
  549. else
  550. {
  551. // If the last one no longer exists . . .
  552. CThing* pthing;
  553. if (m_pRealm->m_idbank.GetThingByID(&pthing, m_u16IdDispensee) != 0)
  554. {
  555. // Clear our ID.
  556. m_u16IdDispensee = CIdBank::IdNil;
  557. // Set the next update time.
  558. if (m_alLogicParms[1] - m_alLogicParms[0] > 0)
  559. {
  560. // Next update will be in a min of m_alLogicParms[0] and a max of m_alLogicParms[1]
  561. // milliseconds.
  562. m_lNextUpdate = lCurTime + m_alLogicParms[0] + (GetRand() % (m_alLogicParms[1] - m_alLogicParms[0]));
  563. }
  564. else
  565. {
  566. m_lNextUpdate = lCurTime + m_alLogicParms[0];
  567. }
  568. }
  569. }
  570. break;
  571. }
  572. case DistanceToDude:
  573. {
  574. if (lCurTime >= m_lNextUpdate)
  575. {
  576. // If in range . . .
  577. long lDudeDist;
  578. if (GetClosestDudeDistance(&lDudeDist) == 0)
  579. {
  580. if ( (lDudeDist >= m_alLogicParms[0] || m_alLogicParms[0] == 0) && (lDudeDist <= m_alLogicParms[1] || m_alLogicParms[1] == 0) )
  581. {
  582. // Create a thing . . .
  583. CThing* pthing;
  584. if (InstantiateDispensee(&pthing, false) == 0)
  585. {
  586. // Wahoo.
  587. m_sNumDispensees++;
  588. }
  589. }
  590. }
  591. m_lNextUpdate = lCurTime + m_alLogicParms[2];
  592. }
  593. break;
  594. }
  595. }
  596. }
  597. else
  598. {
  599. // Should it destroy itself?
  600. }
  601. }
  602. }
  603. ////////////////////////////////////////////////////////////////////////////////
  604. // Called by editor to init new object at specified position
  605. ////////////////////////////////////////////////////////////////////////////////
  606. short CDispenser::EditNew( // Returns 0 if successfull, non-zero otherwise
  607. short sX, // In: New x coord
  608. short sY, // In: New y coord
  609. short sZ) // In: New z coord
  610. {
  611. // Initialize for edit mode.
  612. short sResult = Init(true);
  613. if (sResult == 0)
  614. {
  615. sResult = EditModify();
  616. }
  617. return sResult;
  618. }
  619. ////////////////////////////////////////////////////////////////////////////////
  620. // Set text for and recompose item.
  621. ////////////////////////////////////////////////////////////////////////////////
  622. inline void SetLogicText( // Returns nothing.
  623. RGuiItem* pguiRoot, // In: Root item.
  624. long lId, // In: ID of item to update text.
  625. char* pszText, // In: New text or NULL for none and to disable lIdEdit.
  626. long lIdEdit) // In: Item to enable or disable.
  627. {
  628. RGuiItem* pguiEdit = pguiRoot->GetItemFromId(lIdEdit);
  629. RGuiItem* pguiText = pguiRoot->GetItemFromId(lId);
  630. if (pszText != NULL)
  631. {
  632. RSP_SAFE_GUI_REF_VOID(pguiText, SetText("%s", pszText) );
  633. RSP_SAFE_GUI_REF_VOID(pguiText, Compose() );
  634. RSP_SAFE_GUI_REF_VOID(pguiEdit, SetVisible(TRUE) );
  635. }
  636. else
  637. {
  638. RSP_SAFE_GUI_REF_VOID(pguiText, SetText("") );
  639. RSP_SAFE_GUI_REF_VOID(pguiText, Compose() );
  640. RSP_SAFE_GUI_REF_VOID(pguiEdit, SetVisible(FALSE) );
  641. }
  642. }
  643. ////////////////////////////////////////////////////////////////////////////////
  644. // Setup parms for specified logic type.
  645. ////////////////////////////////////////////////////////////////////////////////
  646. inline void SetupLogicParms( // Returns nothing.
  647. RGuiItem* pguiRoot, // In: Root item.
  648. short sType) // In: Logic type to setup parms for.
  649. {
  650. short i;
  651. for (i = 0; i < CDispenser::NumParms; i++)
  652. {
  653. SetLogicText(
  654. pguiRoot,
  655. LOGIC_PARMS_TEXT_ID_BASE + i,
  656. CDispenser::ms_aliLogics[sType].apszParms[i],
  657. LOGIC_PARMS_EDIT_ID_BASE + i);
  658. }
  659. // Update description.
  660. RGuiItem* pguiDescription = pguiRoot->GetItemFromId(DESCRIPTION_TEXT_ID);
  661. if (pguiDescription)
  662. {
  663. // Remember if word wrap was on . . .
  664. short sWasWordWrap = FALSE;
  665. if (pguiDescription->m_pprint->m_eModes & RPrint::WORD_WRAP)
  666. {
  667. sWasWordWrap = TRUE;
  668. }
  669. // Guarantee word wrap status.
  670. pguiDescription->m_pprint->SetWordWrap(TRUE);
  671. pguiDescription->SetText("%s", CDispenser::ms_aliLogics[sType].pszDescription);
  672. pguiDescription->Compose();
  673. // Restore word wrap status.
  674. pguiDescription->m_pprint->SetWordWrap(sWasWordWrap);
  675. }
  676. }
  677. ////////////////////////////////////////////////////////////////////////////////
  678. // Set selection and update parms via pressed callback from GUI.
  679. ////////////////////////////////////////////////////////////////////////////////
  680. void LogicItemCall(
  681. RGuiItem* pguiLogicItem) // In: Logic item that was pressed.
  682. {
  683. ASSERT(pguiLogicItem->m_ulUserInstance != NULL);
  684. RListBox* plb = (RListBox*)pguiLogicItem->m_ulUserInstance;
  685. ASSERT(plb->m_type == RGuiItem::ListBox);
  686. // Make item the selection.
  687. plb->SetSel(pguiLogicItem);
  688. short sType = pguiLogicItem->m_ulUserData;
  689. // Update parms.
  690. SetupLogicParms(plb->GetParent(), sType);
  691. }
  692. ////////////////////////////////////////////////////////////////////////////////
  693. // Updates the GUI items impacted by the current state of the max dispensees
  694. // 'Infinite' checkbox.
  695. ////////////////////////////////////////////////////////////////////////////////
  696. static void UpdateMaxDispensees(
  697. RGuiItem* pgui_pmb) // In: Multibtn that was pressed.
  698. {
  699. ASSERT(pgui_pmb->m_type == RGuiItem::MultiBtn);
  700. RMultiBtn* pmb = (RMultiBtn*)pgui_pmb;
  701. REdit* pedit = (REdit*)pmb->m_ulUserInstance;
  702. ASSERT(pedit->m_type == RGuiItem::Edit);
  703. // If infinite dispensees . . .
  704. if (pmb->m_sState == 2)
  705. {
  706. // No need for edit field, then.
  707. pedit->SetVisible(FALSE);
  708. }
  709. else
  710. {
  711. // We'll be needing edit field, then.
  712. pedit->SetVisible(TRUE);
  713. // If negative . . .
  714. if (pedit->GetVal() < 0)
  715. {
  716. // Set to default val.
  717. pedit->SetText("%d", 10);
  718. pedit->Compose();
  719. }
  720. }
  721. }
  722. ////////////////////////////////////////////////////////////////////////////////
  723. // Called by editor to modify object
  724. ////////////////////////////////////////////////////////////////////////////////
  725. short CDispenser::EditModify(void) // Returns 0 if successfull, non-zero otherwise
  726. {
  727. // Get key status array.
  728. U8* pau8KeyStatus = rspGetKeyStatusArray();
  729. short sResult = 0;
  730. ClassIDType idNewThingType;
  731. // Set up to modify dispensee.
  732. bool bModifyDispensee = false;
  733. // If not yet setup or key to jump straight to modifying dispensee is not held . . .
  734. if (m_idDispenseeType == TotalIDs || (pau8KeyStatus[RSP_SK_ALT] & 1) == 0)
  735. {
  736. idNewThingType = TotalIDs;
  737. RGuiItem* pguiRoot = RGuiItem::LoadInstantiate(FullPathVD(EDIT_GUI_FILE));
  738. if (pguiRoot != NULL)
  739. {
  740. // Get items.
  741. RListBox* plbThings = (RListBox*)pguiRoot->GetItemFromId(THINGS_LIST_BOX_ID);
  742. RListBox* plbLogics = (RListBox*)pguiRoot->GetItemFromId(LOGICS_LIST_BOX_ID);
  743. RBtn* pbtnModify = (RBtn*)pguiRoot->GetItemFromId(BTN_MODIFY_DISPENSEE_ID);
  744. REdit* peditMaxDispensees = (REdit*)pguiRoot->GetItemFromId(MAX_DISPENSEES_EDIT_ID);
  745. RMultiBtn* pmbInfiniteDispensees = (RMultiBtn*)pguiRoot->GetItemFromId(INFINITE_DISPENSEES_MB_ID);
  746. if ( plbThings
  747. && plbLogics
  748. && pbtnModify
  749. && peditMaxDispensees
  750. && pmbInfiniteDispensees)
  751. {
  752. ASSERT(plbThings->m_type == RGuiItem::ListBox);
  753. ASSERT(plbLogics->m_type == RGuiItem::ListBox);
  754. ASSERT(pbtnModify->m_type == RGuiItem::Btn);
  755. ASSERT(pmbInfiniteDispensees->m_type == RGuiItem::MultiBtn);
  756. short i;
  757. for (i = 0; i < NumParms; i++)
  758. {
  759. RGuiItem* pgui = pguiRoot->GetItemFromId(LOGIC_PARMS_EDIT_ID_BASE + i);
  760. if (pgui)
  761. {
  762. pgui->SetText("%ld", m_alLogicParms[i]);
  763. pgui->Compose();
  764. }
  765. }
  766. peditMaxDispensees->SetText("%ld", m_sMaxDispensees);
  767. peditMaxDispensees->Compose();
  768. // Point instance data at the max dispensees edit so it can show and
  769. // hide it.
  770. pmbInfiniteDispensees->m_ulUserInstance = (ULONG)peditMaxDispensees;
  771. pmbInfiniteDispensees->m_sState = (m_sMaxDispensees < 0) ? 2 : 1;
  772. pmbInfiniteDispensees->m_bcUser = UpdateMaxDispensees;
  773. pmbInfiniteDispensees->Compose();
  774. UpdateMaxDispensees(pmbInfiniteDispensees);
  775. // Set a callback for the button to end the DoGui().
  776. SetGuiToNotify(pbtnModify);
  777. RGuiItem* pguiItem;
  778. RGuiItem* pguiSel = NULL;
  779. for (i = 0; i < NumLogicTypes; i++)
  780. {
  781. pguiItem = plbLogics->AddString(ms_aliLogics[i].pszName);
  782. if (pguiItem != NULL)
  783. {
  784. // Set item number.
  785. pguiItem->m_ulUserData = i;
  786. // Set listbox ptr.
  787. pguiItem->m_ulUserInstance = (ULONG)plbLogics;
  788. // Set callback.
  789. pguiItem->m_bcUser = LogicItemCall;
  790. // If this item is the current logic type . . .
  791. if (m_logictype == i)
  792. {
  793. pguiSel = pguiItem;
  794. // Select it.
  795. plbLogics->SetSel(pguiItem);
  796. // Set up parms.
  797. SetupLogicParms(pguiRoot, i);
  798. }
  799. }
  800. }
  801. plbLogics->AdjustContents();
  802. // If there's a selected item (there should be) . . .
  803. if (pguiSel != NULL)
  804. {
  805. plbLogics->EnsureVisible(pguiSel);
  806. }
  807. pguiSel = NULL;
  808. // Add available objects to listbox.
  809. CThing::ClassIDType idCur;
  810. for (idCur = 0; idCur < CThing::TotalIDs; idCur++)
  811. {
  812. // If item is editor creatable . . .
  813. if (CThing::ms_aClassInfo[idCur].bEditorCreatable == true)
  814. {
  815. // Add string for each item to listbox.
  816. pguiItem = plbThings->AddString((char*)CThing::ms_aClassInfo[idCur].pszClassName);
  817. if (pguiItem != NULL)
  818. {
  819. pguiItem->m_ulUserData = (ULONG)idCur;
  820. // If this is the current type . . .
  821. if (m_idDispenseeType == idCur)
  822. {
  823. pguiSel = pguiItem;
  824. // Select it.
  825. plbThings->SetSel(pguiItem);
  826. }
  827. }
  828. }
  829. }
  830. // Format list items.
  831. plbThings->AdjustContents();
  832. // If there's a selected item (there may not be) . . .
  833. if (pguiSel != NULL)
  834. {
  835. plbThings->EnsureVisible(pguiSel);
  836. }
  837. while (sResult == 0 && idNewThingType == TotalIDs)
  838. {
  839. bModifyDispensee = false;
  840. switch (DoGui(pguiRoot) )
  841. {
  842. case BTN_MODIFY_DISPENSEE_ID:
  843. bModifyDispensee = true;
  844. // Intentional fall through.
  845. case 1:
  846. {
  847. // Get logic selection. Required.
  848. RGuiItem* pguiSel = plbLogics->GetSel();
  849. if (pguiSel != NULL)
  850. {
  851. m_logictype = (LogicType)pguiSel->m_ulUserData;
  852. }
  853. else
  854. {
  855. sResult = 1;
  856. }
  857. // Get dispensee type selection. Required.
  858. pguiSel = plbThings->GetSel();
  859. if (pguiSel != NULL)
  860. {
  861. idNewThingType = (ClassIDType)pguiSel->m_ulUserData;
  862. }
  863. else
  864. {
  865. rspMsgBox(
  866. RSP_MB_ICN_INFO | RSP_MB_BUT_OK,
  867. "Dispenser",
  868. g_pszDispenserNoDispenseeTypeChosen);
  869. }
  870. short i;
  871. for (i = 0; i < NumParms; i++)
  872. {
  873. m_alLogicParms[i] = pguiRoot->GetVal(LOGIC_PARMS_EDIT_ID_BASE + i);
  874. }
  875. if (pmbInfiniteDispensees->m_sState == 1)
  876. {
  877. m_sMaxDispensees = peditMaxDispensees->GetVal();
  878. }
  879. else
  880. {
  881. m_sMaxDispensees = -1;
  882. }
  883. break;
  884. }
  885. case 2:
  886. default:
  887. sResult = 1;
  888. break;
  889. }
  890. }
  891. }
  892. else
  893. {
  894. TRACE("EditModify(): Missing GUI items in %s.\n", EDIT_GUI_FILE);
  895. sResult = -2;
  896. }
  897. // Done with GUI.
  898. delete pguiRoot;
  899. pguiRoot = NULL;
  900. }
  901. else
  902. {
  903. TRACE("EditModify(): Failed to load %s.\n", EDIT_GUI_FILE);
  904. sResult = -1;
  905. }
  906. }
  907. else
  908. {
  909. // Go right to modifying dispensee.
  910. bModifyDispensee = true;
  911. // Use same thing type.
  912. idNewThingType = m_idDispenseeType;
  913. }
  914. // If successful so far . . .
  915. if (sResult == 0)
  916. {
  917. // If we have a dispensee . . .
  918. CThing* pthing = NULL;
  919. // Instantiate it so we get its current settings . . .
  920. if (InstantiateDispensee(&pthing, true) == 0)
  921. {
  922. // If the current dispensee has a different type than the desired one . . .
  923. if (pthing->GetClassID() != idNewThingType)
  924. {
  925. // Be done with this one.
  926. DestroyDispensee(&pthing);
  927. m_fileDispensee.Close();
  928. }
  929. }
  930. m_idDispenseeType = idNewThingType;
  931. // If no current thing . . .
  932. if (pthing == NULL)
  933. {
  934. // Allocate the desired thing . . .
  935. sResult = ConstructWithID(m_idDispenseeType, m_pRealm, &pthing);
  936. if (sResult == 0)
  937. {
  938. // New it in the correct location.
  939. sResult = pthing->EditNew(m_sX, m_sY, m_sZ);
  940. if (sResult == 0)
  941. {
  942. // Success.
  943. }
  944. else
  945. {
  946. TRACE("EditModify(): EditNew() failed for new dispensee.\n");
  947. }
  948. // If any errors occurred after allocation . . .
  949. if (sResult != 0)
  950. {
  951. DestroyDispensee(&pthing);
  952. }
  953. }
  954. else
  955. {
  956. TRACE("EditModify(): Failed to allocate new %s.\n",
  957. CThing::ms_aClassInfo[m_idDispenseeType].pszClassName);
  958. }
  959. }
  960. if (pthing != NULL)
  961. {
  962. // If editing was specified . . .
  963. if (bModifyDispensee == true)
  964. {
  965. // Modify it.
  966. pthing->EditModify();
  967. }
  968. // Render 'im:
  969. RenderDispensee(pthing);
  970. // Dump 'im:
  971. SaveDispensee(pthing);
  972. // Now that we have one in cold storage, we're done with this one.
  973. DestroyDispensee(&pthing);
  974. }
  975. }
  976. return sResult;
  977. }
  978. ////////////////////////////////////////////////////////////////////////////////
  979. // Called by editor to move object to specified position
  980. // (virtual (Overridden here)).
  981. ////////////////////////////////////////////////////////////////////////////////
  982. short CDispenser::EditMove( // Returns 0 if successfull, non-zero otherwise
  983. short sX, // In: New x coord
  984. short sY, // In: New y coord
  985. short sZ) // In: New z coord
  986. {
  987. short sResult = 0; // Assume success.
  988. m_sX = sX;
  989. m_sY = sY;
  990. m_sZ = sZ;
  991. return sResult;
  992. }
  993. ////////////////////////////////////////////////////////////////////////////////
  994. // Called by editor to render object
  995. // (virtual (Overridden here)).
  996. ////////////////////////////////////////////////////////////////////////////////
  997. void CDispenser::EditRender(void)
  998. {
  999. // Map from 3d to 2d coords
  1000. Map3Dto2D(
  1001. (short) m_sX,
  1002. (short) m_sY,
  1003. (short) m_sZ,
  1004. &m_sprite.m_sX2,
  1005. &m_sprite.m_sY2);
  1006. // Priority is based on hotspot of sprite
  1007. m_sprite.m_sPriority = m_sZ;
  1008. // Center on dispensee's hotspot.
  1009. m_sprite.m_sX2 -= m_sDispenseeHotSpotX;
  1010. m_sprite.m_sY2 -= m_sDispenseeHotSpotY;
  1011. // Layer should be based on info we get from attribute map.
  1012. m_sprite.m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer(m_sX, m_sZ));
  1013. // Image would normally animate, but doesn't for now
  1014. m_sprite.m_pImage = &m_imRender;
  1015. // Update sprite in scene
  1016. m_pRealm->m_scene.UpdateSprite(&m_sprite);
  1017. }
  1018. ////////////////////////////////////////////////////////////////////////////////
  1019. // Give Edit a rectangle around this object
  1020. // (virtual (Overridden here)).
  1021. ////////////////////////////////////////////////////////////////////////////////
  1022. void CDispenser::EditRect(RRect* prc)
  1023. {
  1024. // Map from 3d to 2d coords
  1025. Map3Dto2D(
  1026. (short) m_sX,
  1027. (short) m_sY,
  1028. (short) m_sZ,
  1029. &(prc->sX),
  1030. &(prc->sY) );
  1031. #if 0
  1032. // Center on image.
  1033. prc->sX -= m_imRender.m_sWidth / 2;
  1034. prc->sY -= m_imRender.m_sHeight;
  1035. prc->sW = m_imRender.m_sWidth;
  1036. prc->sH = m_imRender.m_sHeight;
  1037. #else
  1038. prc->sX -= m_sDispenseeHotSpotX;
  1039. prc->sY -= m_sDispenseeHotSpotY;
  1040. prc->sW = m_rcDispensee.sW;
  1041. prc->sH = m_rcDispensee.sH;
  1042. #endif
  1043. }
  1044. ////////////////////////////////////////////////////////////////////////////////
  1045. // Called by editor to get the hotspot of an object in 2D.
  1046. // (virtual (Overridden here)).
  1047. ////////////////////////////////////////////////////////////////////////////////
  1048. void CDispenser::EditHotSpot( // Returns nothiing.
  1049. short* psX, // Out: X coord of 2D hotspot relative to
  1050. // EditRect() pos.
  1051. short* psY) // Out: Y coord of 2D hotspot relative to
  1052. // EditRect() pos.
  1053. {
  1054. // Base of dispenser is hotspot.
  1055. *psX = m_sDispenseeHotSpotX;
  1056. *psY = m_sDispenseeHotSpotY;
  1057. }
  1058. ////////////////////////////////////////////////////////////////////////////////
  1059. // Init dispenser
  1060. ////////////////////////////////////////////////////////////////////////////////
  1061. short CDispenser::Init( // Returns 0 if successfull, non-zero otherwise
  1062. bool bEditMode) // true, if in edit mode; false, otherwise.
  1063. {
  1064. short sResult = 0;
  1065. // Remember.
  1066. m_bEditMode = bEditMode;
  1067. // Only need resources in edit mode . . .
  1068. if (bEditMode == true)
  1069. {
  1070. // Get resources
  1071. sResult = GetResources();
  1072. }
  1073. if (m_idDispenseeType < TotalIDs && sResult == 0)
  1074. {
  1075. // Instantiate dispensee so we can create its icon.
  1076. // NOTE: We MUST do this in both edit mode and non-edit mode
  1077. // b/c it affects the dispensee's ms_sFileCount which can affect
  1078. // the load process; therefore, we must be consistent.
  1079. CThing* pthing = NULL;
  1080. InstantiateDispensee(&pthing, false);
  1081. // If in edit mode . . .
  1082. if (bEditMode == true)
  1083. {
  1084. // Render 'im:
  1085. RenderDispensee(pthing);
  1086. }
  1087. DestroyDispensee(&pthing);
  1088. }
  1089. // No special flags.
  1090. m_sprite.m_sInFlags = 0;
  1091. return sResult;
  1092. }
  1093. ////////////////////////////////////////////////////////////////////////////////
  1094. // Kill dispenser
  1095. ////////////////////////////////////////////////////////////////////////////////
  1096. void CDispenser::Kill(void)
  1097. {
  1098. // Remove sprite from scene (this is safe even if it was already removed!)
  1099. m_pRealm->m_scene.RemoveSprite(&m_sprite);
  1100. // Free resources.
  1101. FreeResources();
  1102. if (m_fileDispensee.IsOpen() != FALSE)
  1103. {
  1104. m_fileDispensee.Close();
  1105. }
  1106. }
  1107. ////////////////////////////////////////////////////////////////////////////////
  1108. // Get all required resources
  1109. ////////////////////////////////////////////////////////////////////////////////
  1110. short CDispenser::GetResources(void) // Returns 0 if successfull, non-zero otherwise
  1111. {
  1112. short sResult = 0;
  1113. if (m_pim == NULL)
  1114. {
  1115. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(RES_FILE), &m_pim, RFile::LittleEndian);
  1116. if (sResult == 0)
  1117. {
  1118. if (m_pim->Convert(RImage::FSPR8) == RImage::FSPR8)
  1119. {
  1120. }
  1121. else
  1122. {
  1123. TRACE("GetResources(): Error converting to FSPR8.\n");
  1124. }
  1125. }
  1126. }
  1127. return sResult;
  1128. }
  1129. ////////////////////////////////////////////////////////////////////////////////
  1130. // Free all resources
  1131. ////////////////////////////////////////////////////////////////////////////////
  1132. void CDispenser::FreeResources(void)
  1133. {
  1134. if (m_pim != NULL)
  1135. {
  1136. // Release resources for animations.
  1137. rspReleaseResource(&g_resmgrGame, &m_pim);
  1138. }
  1139. }
  1140. ////////////////////////////////////////////////////////////////////////////////
  1141. // Create a dispensee from the memfile, if open.
  1142. ////////////////////////////////////////////////////////////////////////////////
  1143. short CDispenser::InstantiateDispensee( // Returns 0 on success.
  1144. CThing** ppthing, // Out: New thing loaded from m_fileDispensee.
  1145. bool bEditMode) // In: true if in edit mode.
  1146. {
  1147. short sResult = 0; // Assume success.
  1148. // If we even have a dispensee type . . .
  1149. if (m_idDispenseeType > 0 && m_idDispenseeType < TotalIDs)
  1150. {
  1151. // Allocate the desired thing . . .
  1152. if (CThing::Construct(m_idDispenseeType, m_pRealm, ppthing) == 0)
  1153. {
  1154. if (m_fileDispensee.IsOpen() != FALSE)
  1155. {
  1156. m_fileDispensee.Seek(0, SEEK_SET);
  1157. if ((*ppthing)->Load(
  1158. &m_fileDispensee,
  1159. bEditMode,
  1160. --ms_sDispenseeFileCount, // Always load statics for these.
  1161. m_ulFileVersion) == 0)
  1162. {
  1163. U16 idInstance;
  1164. if (m_pRealm->m_idbank.Get(*ppthing, &idInstance) == 0)
  1165. {
  1166. // Release file's ID (cannot have all the dispensee's
  1167. // using the same ID) and set new one.
  1168. (*ppthing)->SetInstanceID(idInstance);
  1169. // Success.
  1170. m_u16IdDispensee = idInstance;
  1171. // If in edit mode . . .
  1172. if (bEditMode == true)
  1173. {
  1174. // Update position.
  1175. (*ppthing)->EditMove(m_sX, m_sY, m_sZ);
  1176. }
  1177. // Startup, if requested. We only give one chance
  1178. // UNlike CRealm::Startup().
  1179. // if ((*ppthing)->m_sCallStartup != 0)
  1180. {
  1181. // (*ppthing)->m_sCallStartup = 0;
  1182. (*ppthing)->Startup();
  1183. }
  1184. }
  1185. else
  1186. {
  1187. TRACE("InstantiateDispensee(): Could not get an instance ID from the idbank.\n");
  1188. sResult = -3;
  1189. }
  1190. }
  1191. else
  1192. {
  1193. TRACE("InstantiateDispensee(): Load() failed for dispensee.\n");
  1194. sResult = -2;
  1195. }
  1196. }
  1197. else
  1198. {
  1199. sResult = 1;
  1200. }
  1201. // If any errors after allocation . . .
  1202. if (sResult != 0)
  1203. {
  1204. DestroyDispensee(ppthing);
  1205. }
  1206. }
  1207. else
  1208. {
  1209. TRACE("InstantiateDispensee(): Failed to allocate new %s.\n",
  1210. CThing::ms_aClassInfo[m_idDispenseeType].pszClassName);
  1211. sResult = -1;
  1212. }
  1213. }
  1214. else
  1215. {
  1216. sResult = 1;
  1217. }
  1218. return sResult;
  1219. }
  1220. ////////////////////////////////////////////////////////////////////////////////
  1221. // Write dispensee to the memfile.
  1222. ////////////////////////////////////////////////////////////////////////////////
  1223. short CDispenser::SaveDispensee( // Returns 0 on success.
  1224. CThing* pthing) // In: Instance of Dispensee to save.
  1225. {
  1226. short sResult = 0; // Assume success.
  1227. // If we already have a mem file . . .
  1228. if (m_fileDispensee.IsOpen() != FALSE)
  1229. {
  1230. m_fileDispensee.Close();
  1231. }
  1232. if (m_fileDispensee.Open(1, 1, RFile::LittleEndian) == 0)
  1233. {
  1234. if (pthing->Save(&m_fileDispensee, --ms_sDispenseeFileCount) == 0)
  1235. {
  1236. m_ulFileVersion = CRealm::FileVersion;
  1237. }
  1238. else
  1239. {
  1240. TRACE("SaveDispensee(): pthing->Save() failed.\n");
  1241. sResult = -2;
  1242. }
  1243. }
  1244. else
  1245. {
  1246. TRACE("SaveDispensee(): m_fileDispensee->Open() failed.\n");
  1247. sResult = -1;
  1248. }
  1249. return sResult;
  1250. }
  1251. ////////////////////////////////////////////////////////////////////////////////
  1252. // Render dispensee to m_imRender.
  1253. ////////////////////////////////////////////////////////////////////////////////
  1254. short CDispenser::RenderDispensee( // Returns 0 on success.
  1255. CThing* pthing) // In: Instance of Dispensee to render.
  1256. {
  1257. short sResult = 0; // Assume success.
  1258. // If in edit mode . . .
  1259. if (m_bEditMode == true)
  1260. {
  1261. // Redo display image. This is a waste if we're not in edit mode.
  1262. m_imRender.DestroyData();
  1263. CSprite* psprite = NULL;
  1264. // If there is an instance . . .
  1265. if (pthing)
  1266. {
  1267. // Prepare item to be rendered.
  1268. pthing->Render();
  1269. // Get size of dispensee.
  1270. pthing->EditRect(&m_rcDispensee);
  1271. pthing->EditHotSpot(&m_sDispenseeHotSpotX, &m_sDispenseeHotSpotY);
  1272. psprite = pthing->GetSprite();
  1273. }
  1274. else
  1275. {
  1276. // Map from 3d to 2d coords
  1277. Map3Dto2D(
  1278. (short) m_sX,
  1279. (short) m_sY,
  1280. (short) m_sZ,
  1281. &(m_rcDispensee.sX),
  1282. &(m_rcDispensee.sY) );
  1283. m_rcDispensee.sW = WIDTH_IF_NO_THING;
  1284. m_rcDispensee.sH = HEIGHT_IF_NO_THING;
  1285. m_sDispenseeHotSpotX = m_rcDispensee.sW / 2;
  1286. m_sDispenseeHotSpotY = m_rcDispensee.sH;
  1287. }
  1288. // Create image . . .
  1289. if (m_imRender.CreateImage( // Return 0 on success.
  1290. m_rcDispensee.sW, // Width of new buffer.
  1291. m_rcDispensee.sH, // Height of new buffer.
  1292. RImage::BMP8) // Type of new buffer.
  1293. == 0)
  1294. {
  1295. // Clear.
  1296. rspRect(
  1297. 0, // In: Not black, but transparent.
  1298. &m_imRender,
  1299. 0,
  1300. 0,
  1301. m_imRender.m_sWidth,
  1302. m_imRender.m_sHeight,
  1303. NULL);
  1304. // If we could get the thing's sprite . . .
  1305. if (psprite != NULL)
  1306. {
  1307. // Determine offset that would put the dispensee's hotspot in the center
  1308. // of our image.
  1309. short sOffX = -m_rcDispensee.sX;
  1310. short sOffY = -m_rcDispensee.sY;
  1311. RRect rcClip(0, 0, m_imRender.m_sWidth, m_imRender.m_sHeight);
  1312. // Render dispensee into image.
  1313. m_pRealm->m_scene.Render( // Returns nothing.
  1314. &m_imRender, // Destination image.
  1315. sOffX, // Destination 2D x coord.
  1316. sOffY, // Destination 2D y coord.
  1317. psprite, // Tree of sprites to render.
  1318. m_pRealm->m_phood, // Da hood, homey.
  1319. &rcClip, // Dst clip rect.
  1320. NULL); // XRayee, if not NULL.
  1321. }
  1322. else
  1323. {
  1324. // Use text alternative.
  1325. RPrint print;
  1326. print.SetFont(FONT_SIZE, &g_fontBig);
  1327. print.SetColor(FONT_COLOR, 0, 0);
  1328. print.print(
  1329. &m_imRender,
  1330. 0,
  1331. 0,
  1332. "%s",
  1333. ms_aClassInfo[m_idDispenseeType].pszClassName);
  1334. }
  1335. #if 1
  1336. // Draw dispenser icon on top.
  1337. rspBlit(
  1338. m_pim, // Src image.
  1339. &m_imRender, // Dst image.
  1340. m_imRender.m_sWidth / 2 - m_pim->m_sWidth / 2, // Dst x.
  1341. m_imRender.m_sHeight / 2 - m_pim->m_sHeight / 2, // Dst y.
  1342. NULL); // Dst clip.
  1343. #endif
  1344. }
  1345. else
  1346. {
  1347. TRACE("RenderDispensee(): m_imRender.CreateImage() failed.\n");
  1348. sResult = -1;
  1349. }
  1350. }
  1351. return sResult;
  1352. }
  1353. ////////////////////////////////////////////////////////////////////////////////
  1354. // Get the distance to the closest dude.
  1355. ////////////////////////////////////////////////////////////////////////////////
  1356. short CDispenser::GetClosestDudeDistance( // Returns 0 on success. Fails, if no dudes.
  1357. long* plClosestDistance) // Out: Distance to closest dude.
  1358. {
  1359. short sRes = 1; // Assume no dude found.
  1360. ULONG ulSqrDistance;
  1361. ULONG ulCurSqrDistance = 0xFFFFFFFF;
  1362. ULONG ulDistX;
  1363. ULONG ulDistZ;
  1364. CDude* pdude;
  1365. CListNode<CThing>* pDudeList = m_pRealm->m_aclassHeads[CThing::CDudeID].m_pnNext;
  1366. // While we have a node and that node is owned (the head and tail are not owned).
  1367. while (pDudeList && pDudeList->m_powner)
  1368. {
  1369. // Get current owner.
  1370. pdude = (CDude*) pDudeList->m_powner;
  1371. // Must be Dude.
  1372. ASSERT(pdude->GetClassID() == CDudeID);
  1373. // If this dude is not dead . . .
  1374. if (pdude->m_state != CThing3d::State_Dead)
  1375. {
  1376. // Determine square distance on X/Z plane.
  1377. ulDistX = pdude->m_dX - m_sX;
  1378. ulDistZ = pdude->m_dZ - m_sZ;
  1379. ulSqrDistance = ulDistX * ulDistX + ulDistZ * ulDistZ;
  1380. // If closer than the last guy . . .
  1381. if (ulSqrDistance < ulCurSqrDistance)
  1382. {
  1383. // This one is closer.
  1384. ulCurSqrDistance = ulSqrDistance;
  1385. // Definitely going to have a dude to return.
  1386. sRes = 0;
  1387. }
  1388. }
  1389. // Next node please.
  1390. pDudeList = pDudeList->m_pnNext;
  1391. }
  1392. if (sRes == 0)
  1393. {
  1394. *plClosestDistance = rspSqrt(ulSqrDistance);
  1395. }
  1396. else
  1397. {
  1398. *plClosestDistance = 0;
  1399. }
  1400. return sRes;
  1401. }
  1402. ////////////////////////////////////////////////////////////////////////////////
  1403. // Destroy an instantiated dispensee.
  1404. ////////////////////////////////////////////////////////////////////////////////
  1405. void CDispenser::DestroyDispensee( // Returns nothing.
  1406. CThing** ppthing) // In: Ptr to the instance.
  1407. {
  1408. ASSERT(ppthing);
  1409. if (*ppthing)
  1410. {
  1411. // If this one is the one indicated by the ID . . .
  1412. if ( (*ppthing)->GetInstanceID() == m_u16IdDispensee)
  1413. {
  1414. m_u16IdDispensee = CIdBank::IdNil;
  1415. }
  1416. // Destroy the dispensee.
  1417. delete *ppthing;
  1418. // Clear user's pointer.
  1419. *ppthing = NULL;
  1420. }
  1421. }
  1422. ////////////////////////////////////////////////////////////////////////////////
  1423. // EOF
  1424. ////////////////////////////////////////////////////////////////////////////////