ladder.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. // Ladder.cpp
  19. // Project: Nostril (aka Postal)
  20. //
  21. // History:
  22. // 06/02/97 JMI Started.
  23. //
  24. // 06/02/97 JMI Was not previously setting the position of the smashes'
  25. // spheres. Fixed.
  26. //
  27. // 07/09/97 JMI Now uses m_pRealm->Make2dResPath() to get the fullpath
  28. // for 2D image components.
  29. //
  30. //////////////////////////////////////////////////////////////////////////////
  31. //
  32. // This CThing-derived class will represent ladders that the CDudes, and
  33. // perhaps other characters, can use to climb to heights they cannot step up
  34. // to.
  35. //
  36. //////////////////////////////////////////////////////////////////////////////
  37. #define LADDER_CPP
  38. #include "RSPiX.h"
  39. #include <math.h>
  40. #include "Ladder.h"
  41. #include "game.h"
  42. #include "reality.h"
  43. ////////////////////////////////////////////////////////////////////////////////
  44. // Macros/types/etc.
  45. ////////////////////////////////////////////////////////////////////////////////
  46. #define GUI_FILE_NAME "res/editor/Ladder.gui"
  47. // This is for edit mode only.
  48. #define LADDER_IMAGE_FILENAME "ladder.bmp"
  49. #define GUI_ID_LEN 3
  50. #define GUI_ID_HEIGHT 4
  51. #define GUI_ID_ROT 5
  52. #define GUI_ID_TYPE_BASE 10
  53. #define LADDER_ENDS_RADIUS 10
  54. ////////////////////////////////////////////////////////////////////////////////
  55. // Variables/data
  56. ////////////////////////////////////////////////////////////////////////////////
  57. ////////////////////////////////////////////////////////////////////////////////
  58. // Load object (should call base class version!)
  59. ////////////////////////////////////////////////////////////////////////////////
  60. short CLadder::Load( // Returns 0 if successfull, non-zero otherwise
  61. RFile* pFile, // In: File to load from
  62. bool bEditMode, // In: True for edit mode, false otherwise
  63. short sFileCount, // In: File count (unique per file, never 0)
  64. ULONG ulFileVersion) // In: Version of file format to load.
  65. {
  66. short sResult = CThing::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  67. if (sResult == 0)
  68. {
  69. switch (ulFileVersion)
  70. {
  71. default:
  72. case 15:
  73. case 14:
  74. case 13:
  75. case 12:
  76. case 11:
  77. case 10:
  78. case 9:
  79. case 8:
  80. case 7:
  81. case 6:
  82. case 5:
  83. case 4:
  84. case 3:
  85. case 2:
  86. case 1:
  87. pFile->Read(&m_dX);
  88. pFile->Read(&m_dY);
  89. pFile->Read(&m_dZ);
  90. pFile->Read(&m_sLen);
  91. pFile->Read(&m_sHeight);
  92. pFile->Read(&m_sRotY);
  93. break;
  94. }
  95. // Make sure there were no file errors or format errors . . .
  96. if (!pFile->Error() && sResult == 0)
  97. {
  98. // Get resources and initialize.
  99. sResult = Init();
  100. }
  101. else
  102. {
  103. sResult = -1;
  104. TRACE("CLadder::Load(): Error reading from file!\n");
  105. }
  106. }
  107. return sResult;
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////
  110. // Save object (should call base class version!)
  111. ////////////////////////////////////////////////////////////////////////////////
  112. short CLadder::Save( // Returns 0 if successfull, non-zero otherwise
  113. RFile* pFile, // In: File to save to
  114. short sFileCount) // In: File count (unique per file, never 0)
  115. {
  116. short sResult = CThing::Save(pFile, sFileCount);
  117. if (sResult == 0)
  118. {
  119. pFile->Write(&m_dX);
  120. pFile->Write(&m_dY);
  121. pFile->Write(&m_dZ);
  122. pFile->Write(&m_sLen);
  123. pFile->Write(&m_sHeight);
  124. pFile->Write(&m_sRotY);
  125. // Make sure there were no file errors
  126. sResult = pFile->Error();
  127. }
  128. return sResult;
  129. }
  130. ////////////////////////////////////////////////////////////////////////////////
  131. // Startup object
  132. ////////////////////////////////////////////////////////////////////////////////
  133. short CLadder::Startup(void) // Returns 0 if successfull, non-zero otherwise
  134. {
  135. return 0;
  136. }
  137. ////////////////////////////////////////////////////////////////////////////////
  138. // Shutdown object
  139. ////////////////////////////////////////////////////////////////////////////////
  140. short CLadder::Shutdown(void) // Returns 0 if successfull, non-zero otherwise
  141. {
  142. return 0;
  143. }
  144. ////////////////////////////////////////////////////////////////////////////////
  145. // Suspend object
  146. ////////////////////////////////////////////////////////////////////////////////
  147. void CLadder::Suspend(void)
  148. {
  149. m_sSuspend++;
  150. }
  151. ////////////////////////////////////////////////////////////////////////////////
  152. // Resume object
  153. ////////////////////////////////////////////////////////////////////////////////
  154. void CLadder::Resume(void)
  155. {
  156. m_sSuspend--;
  157. }
  158. ////////////////////////////////////////////////////////////////////////////////
  159. // Update object
  160. ////////////////////////////////////////////////////////////////////////////////
  161. void CLadder::Update(void)
  162. {
  163. // Do schtuff.
  164. }
  165. ////////////////////////////////////////////////////////////////////////////////
  166. // Render object
  167. ////////////////////////////////////////////////////////////////////////////////
  168. void CLadder::Render(void)
  169. {
  170. // Doesn't normally draw anything (see EditRender() for render during
  171. // edit mode).
  172. }
  173. ////////////////////////////////////////////////////////////////////////////////
  174. // Called by editor to init new object at specified position
  175. ////////////////////////////////////////////////////////////////////////////////
  176. short CLadder::EditNew( // Returns 0 if successfull, non-zero otherwise
  177. short sX, // In: New x coord
  178. short sY, // In: New y coord
  179. short sZ) // In: New z coord
  180. {
  181. short sResult = 0;
  182. // Use specified position
  183. m_dX = (double)sX;
  184. m_dY = (double)sY;
  185. m_dZ = (double)sZ;
  186. sResult = Init();
  187. return sResult;
  188. }
  189. ////////////////////////////////////////////////////////////////////////////////
  190. // Called by editor to modify object
  191. ////////////////////////////////////////////////////////////////////////////////
  192. short CLadder::EditModify(void)
  193. {
  194. short sResult = 0;
  195. RGuiItem* pgui = RGuiItem::LoadInstantiate(FullPathVD(GUI_FILE_NAME));
  196. if (pgui != NULL)
  197. {
  198. RGuiItem* pguiLen = pgui->GetItemFromId(GUI_ID_LEN);
  199. RGuiItem* pguiHeight = pgui->GetItemFromId(GUI_ID_HEIGHT);
  200. RGuiItem* pguiRot = pgui->GetItemFromId(GUI_ID_ROT);
  201. if (pguiLen != NULL && pguiHeight != NULL && pguiRot != NULL)
  202. {
  203. // Set text.
  204. pguiLen->SetText("%hd", m_sLen);
  205. // Realize text.
  206. pguiLen->Compose();
  207. // Set text.
  208. pguiHeight->SetText("%hd", m_sHeight);
  209. // Realize text.
  210. pguiHeight->Compose();
  211. // Set text.
  212. pguiRot->SetText("%hd", m_sRotY);
  213. // Realize text.
  214. pguiRot->Compose();
  215. if (DoGui(pgui) == 1)
  216. {
  217. // Get new values.
  218. m_sLen = pguiLen->GetVal();
  219. m_sHeight = pguiHeight->GetVal();
  220. m_sRotY = rspMod360(pguiRot->GetVal() );
  221. }
  222. else
  223. {
  224. sResult = 1;
  225. }
  226. }
  227. else
  228. {
  229. sResult = -2;
  230. }
  231. // Done with GUI.
  232. delete pgui;
  233. }
  234. else
  235. {
  236. sResult = -1;
  237. }
  238. // If successful so far . . .
  239. if (sResult == 0)
  240. {
  241. // Load resources and initialize.
  242. sResult = Init();
  243. }
  244. return sResult;
  245. }
  246. ////////////////////////////////////////////////////////////////////////////////
  247. // Called by editor to move object to specified position
  248. ////////////////////////////////////////////////////////////////////////////////
  249. short CLadder::EditMove( // Returns 0 if successfull, non-zero otherwise
  250. short sX, // In: New x coord
  251. short sY, // In: New y coord
  252. short sZ) // In: New z coord
  253. {
  254. m_dX = (double)sX;
  255. m_dY = (double)sY;
  256. m_dZ = (double)sZ;
  257. return 0;
  258. }
  259. ////////////////////////////////////////////////////////////////////////////////
  260. // Called by editor to get the clickable pos/area of an object in 2D.
  261. // (virtual (Overridden here)).
  262. ////////////////////////////////////////////////////////////////////////////////
  263. void CLadder::EditRect( // Returns nothiing.
  264. RRect* prc) // Out: Clickable pos/area of object.
  265. {
  266. prc->sX = m_dX;
  267. prc->sY = m_dZ - m_dY;
  268. prc->sW = 10; // Safety.
  269. prc->sH = 10; // Safety.
  270. if (m_sprite.m_pImage != NULL)
  271. {
  272. prc->sW = m_sprite.m_pImage->m_sWidth;
  273. prc->sH = m_sprite.m_pImage->m_sHeight;
  274. }
  275. prc->sX -= prc->sW / 2;
  276. prc->sY -= prc->sH / 2;
  277. }
  278. ////////////////////////////////////////////////////////////////////////////////
  279. // Called by editor to get the hotspot of an object in 2D.
  280. // (virtual (Overridden here)).
  281. ////////////////////////////////////////////////////////////////////////////////
  282. void CLadder::EditHotSpot( // Returns nothiing.
  283. short* psX, // Out: X coord of 2D hotspot relative to
  284. // EditRect() pos.
  285. short* psY) // Out: Y coord of 2D hotspot relative to
  286. // EditRect() pos.
  287. {
  288. *psX = 0; // Safety.
  289. *psY = 0; // Safety.
  290. if (m_sprite.m_pImage != NULL)
  291. {
  292. *psX = m_sprite.m_pImage->m_sWidth / 2;
  293. *psY = m_sprite.m_pImage->m_sHeight / 2;
  294. }
  295. }
  296. ////////////////////////////////////////////////////////////////////////////////
  297. // Called by editor to update object
  298. ////////////////////////////////////////////////////////////////////////////////
  299. void CLadder::EditUpdate(void)
  300. {
  301. // The editor schtuff.
  302. }
  303. ////////////////////////////////////////////////////////////////////////////////
  304. // Called by editor to render object
  305. ////////////////////////////////////////////////////////////////////////////////
  306. void CLadder::EditRender(void)
  307. {
  308. // Map from 3d to 2d coords
  309. Map3Dto2D(
  310. (short) m_dX,
  311. (short) m_dY,
  312. (short) m_dZ,
  313. &m_sprite.m_sX2,
  314. &m_sprite.m_sY2);
  315. // Center on image.
  316. m_sprite.m_sX2 -= m_sprite.m_pImage->m_sWidth / 2;
  317. m_sprite.m_sY2 -= m_sprite.m_pImage->m_sHeight / 2;
  318. // Priority is based on bottom edge of sprite on X/Z plane.
  319. m_sprite.m_sPriority = m_dZ + m_sprite.m_pImage->m_sHeight / 2;
  320. // Layer should be based on info we get from attribute map.
  321. m_sprite.m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer((short) m_dX, (short) m_dZ));
  322. // Update sprite in scene
  323. m_pRealm->m_scene.UpdateSprite(&m_sprite);
  324. }
  325. ////////////////////////////////////////////////////////////////////////////////
  326. // Initialize object.
  327. ////////////////////////////////////////////////////////////////////////////////
  328. short CLadder::Init(void) // Returns 0 on success.
  329. {
  330. short sRes = GetResources();
  331. // Set up collision object.
  332. m_smashTop.m_bits = CSmash::Ladder;
  333. m_smashTop.m_sphere.sphere.lRadius = LADDER_ENDS_RADIUS;
  334. m_smashTop.m_sphere.sphere.X = m_dX + COSQ[m_sRotY] * m_sLen;
  335. m_smashTop.m_sphere.sphere.Y = m_dY + m_sHeight;
  336. m_smashTop.m_sphere.sphere.Z = m_dZ - SINQ[m_sRotY] * m_sLen;
  337. m_smashBottom.m_bits = CSmash::Ladder;
  338. m_smashBottom.m_sphere.sphere.lRadius = LADDER_ENDS_RADIUS;
  339. m_smashBottom.m_sphere.sphere.X = m_dX;
  340. m_smashBottom.m_sphere.sphere.Y = m_dY;
  341. m_smashBottom.m_sphere.sphere.Z = m_dZ;
  342. // Update the smash.
  343. m_pRealm->m_smashatorium.Update(&m_smashTop);
  344. m_pRealm->m_smashatorium.Update(&m_smashBottom);
  345. return sRes;
  346. }
  347. ////////////////////////////////////////////////////////////////////////////////
  348. // Get all required resources
  349. ////////////////////////////////////////////////////////////////////////////////
  350. short CLadder::GetResources(void) // Returns 0 if successfull, non-zero otherwise
  351. {
  352. short sResult = 0;
  353. // Safe to call even if no resource.
  354. FreeResources();
  355. sResult = rspGetResource(
  356. &g_resmgrGame,
  357. m_pRealm->Make2dResPath(LADDER_IMAGE_FILENAME),
  358. &m_sprite.m_pImage);
  359. return sResult;
  360. }
  361. ////////////////////////////////////////////////////////////////////////////////
  362. // Free all resources
  363. ////////////////////////////////////////////////////////////////////////////////
  364. short CLadder::FreeResources(void) // Returns 0 if successfull, non-zero otherwise
  365. {
  366. short sResult = 0;
  367. if (m_sprite.m_pImage != NULL)
  368. {
  369. rspReleaseResource(&g_resmgrGame, &m_sprite.m_pImage);
  370. }
  371. return sResult;
  372. }
  373. ////////////////////////////////////////////////////////////////////////////////
  374. // Call to to try to get on the ladder.
  375. // This function will return false if there is already someone on the
  376. // ladder.
  377. ////////////////////////////////////////////////////////////////////////////////
  378. bool CLadder::GetOn( // Returns true, if able to get on,
  379. // false otherwise.
  380. CCharacter* pchar) // In: Character attempting to get onto ladder.
  381. {
  382. bool bGotOn = true; // Assume success.
  383. if (m_pcharLadderBoy == NULL)
  384. {
  385. m_pcharLadderBoy = pchar;
  386. }
  387. else
  388. {
  389. bGotOn = false;
  390. }
  391. return bGotOn;
  392. }
  393. ////////////////////////////////////////////////////////////////////////////////
  394. // Call when you get off the ladder.
  395. // Only call this function, if you have made a successful call to GetOn()
  396. // and have not, since, made a call to this function.
  397. ////////////////////////////////////////////////////////////////////////////////
  398. void CLadder::GetOff(void) // Returns nothing.
  399. {
  400. m_pcharLadderBoy = NULL;
  401. }
  402. ////////////////////////////////////////////////////////////////////////////////
  403. // Get the next position on the ladder.
  404. ////////////////////////////////////////////////////////////////////////////////
  405. void CLadder::GetNextPos( // Returns nothing.
  406. double* pdX, // In: Current x position.
  407. // Out: New x position.
  408. double* pdY, // In: Current y position.
  409. // Out: New y position.
  410. double* pdZ, // In: Current z position.
  411. // Out: New z position.
  412. double dDistance) // In: Distance to travel. Positive is up.
  413. {
  414. TRACE("GetNextPos(): NYI!!\n");
  415. }
  416. ////////////////////////////////////////////////////////////////////////////////
  417. // EOF
  418. ////////////////////////////////////////////////////////////////////////////////