weapon.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. // weapon.cpp
  19. // Project: Nostril (aka Postal)
  20. //
  21. // This module implements the CWeapon class which is base class for the weapons
  22. //
  23. // History:
  24. // 02/27/97 BRH Started this file from CDoofus and modified it
  25. // to be a base class for the weapons.
  26. //
  27. // 03/13/97 JMI Load now takes a version number.
  28. //
  29. // 03/19/97 BRH Added virtual functions for processing messages and
  30. // virtual OnMessage handler functions so that it follows
  31. // the model of the CThing3d base class object.
  32. //
  33. // 06/25/97 BRH Added rendering of 2D shadow sprite to the Render
  34. // function. Also added PrepareShadow function to
  35. // load the default shadow resource if no resource is
  36. // loaded for the shadow and then make the shadow visible.
  37. //
  38. // 06/30/97 JMI Now maps the Z to 3D when loading fileversions previous to
  39. // 24.
  40. //
  41. // 07/09/97 JMI Now uses m_pRealm->Make2dResPath() to get the fullpath
  42. // for 2D image components.
  43. //
  44. // 07/21/97 JMI Now checks upper bound on m_sAlphaLevel of shadow sprite.
  45. //
  46. // 07/30/97 JMI Now hides shadow if mainsprite is hidden.
  47. //
  48. ////////////////////////////////////////////////////////////////////////////////
  49. #define WEAPON_CPP
  50. #include "RSPiX.h"
  51. #include "weapon.h"
  52. #include "reality.h"
  53. ////////////////////////////////////////////////////////////////////////////////
  54. // Macros/types/etc.
  55. ////////////////////////////////////////////////////////////////////////////////
  56. #define SHADOW_FILE "shadow.img"
  57. ////////////////////////////////////////////////////////////////////////////////
  58. // Variables/data
  59. ////////////////////////////////////////////////////////////////////////////////
  60. // Let this auto-init to 0
  61. short CWeapon::ms_sFileCount;
  62. ////////////////////////////////////////////////////////////////////////////////
  63. // Load object (should call base class version!)
  64. ////////////////////////////////////////////////////////////////////////////////
  65. short CWeapon::Load( // Returns 0 if successfull, non-zero otherwise
  66. RFile* pFile, // In: File to load from
  67. bool bEditMode, // In: True for edit mode, false otherwise
  68. short sFileCount, // In: File count (unique per file, never 0)
  69. ULONG ulFileVersion) // In: Version of file format to load.
  70. {
  71. short sResult = 0;
  72. // Call the CThing base class load to get the instance ID
  73. sResult = CThing::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  74. if (sResult == 0)
  75. {
  76. // Load common data just once per file (not with each object)
  77. if (ms_sFileCount != sFileCount)
  78. {
  79. ms_sFileCount = sFileCount;
  80. // Load static data.
  81. switch (ulFileVersion)
  82. {
  83. default:
  84. case 1:
  85. break;
  86. }
  87. }
  88. switch (ulFileVersion)
  89. {
  90. default:
  91. case 1:
  92. // Load object data
  93. pFile->Read(&m_dX);
  94. pFile->Read(&m_dY);
  95. pFile->Read(&m_dZ);
  96. pFile->Read(&m_dRot);
  97. pFile->Read(&m_dVertVel);
  98. pFile->Read(&m_dHorizVel);
  99. pFile->Read(&m_eState);
  100. break;
  101. }
  102. // If the file version is earlier than the change to real 3D coords . . .
  103. if (ulFileVersion < 24)
  104. {
  105. // Convert to 3D.
  106. m_pRealm->MapY2DtoZ3D(
  107. m_dZ,
  108. &m_dZ);
  109. }
  110. // Make sure there were no file errors or format errors . . .
  111. if (!pFile->Error() && sResult == 0)
  112. {
  113. // Get resources
  114. // sResult = GetResources();
  115. }
  116. else
  117. {
  118. sResult = -1;
  119. TRACE("CWeapon::Load(): Error reading from file!\n");
  120. }
  121. }
  122. else
  123. {
  124. TRACE("CWeapon::Load(): CThing::Load() failed.\n");
  125. }
  126. return sResult;
  127. }
  128. ////////////////////////////////////////////////////////////////////////////////
  129. // Save object (should call base class version!)
  130. ////////////////////////////////////////////////////////////////////////////////
  131. short CWeapon::Save( // Returns 0 if successfull, non-zero otherwise
  132. RFile* pFile, // In: File to save to
  133. short sFileCount) // In: File count (unique per file, never 0)
  134. {
  135. // Call the base class save to save the u16InstanceID
  136. CThing::Save(pFile, sFileCount);
  137. // Save common data just once per file (not with each object)
  138. if (ms_sFileCount != sFileCount)
  139. {
  140. ms_sFileCount = sFileCount;
  141. // Save static data
  142. }
  143. // Save object data
  144. pFile->Write(&m_dX);
  145. pFile->Write(&m_dY);
  146. pFile->Write(&m_dZ);
  147. pFile->Write(&m_dRot);
  148. pFile->Write(&m_dVertVel);
  149. pFile->Write(&m_dHorizVel);
  150. pFile->Write(&m_eState);
  151. return 0;
  152. }
  153. ////////////////////////////////////////////////////////////////////////////////
  154. // Startup object
  155. ////////////////////////////////////////////////////////////////////////////////
  156. short CWeapon::Startup(void) // Returns 0 if successfull, non-zero otherwise
  157. {
  158. // Init other stuff
  159. m_lPrevTime = m_pRealm->m_time.GetGameTime();
  160. return 0;
  161. }
  162. ////////////////////////////////////////////////////////////////////////////////
  163. // Setup object
  164. ////////////////////////////////////////////////////////////////////////////////
  165. short CWeapon::Setup(short sX, short sY, short sZ)
  166. {
  167. m_dX = sX;
  168. m_dY = sY;
  169. m_dZ = sZ;
  170. return 0;
  171. }
  172. ////////////////////////////////////////////////////////////////////////////////
  173. // Shutdown object
  174. ////////////////////////////////////////////////////////////////////////////////
  175. short CWeapon::Shutdown(void) // Returns 0 if successfull, non-zero otherwise
  176. {
  177. return 0;
  178. }
  179. ////////////////////////////////////////////////////////////////////////////////
  180. // Suspend object
  181. ////////////////////////////////////////////////////////////////////////////////
  182. void CWeapon::Suspend(void)
  183. {
  184. m_sSuspend++;
  185. }
  186. ////////////////////////////////////////////////////////////////////////////////
  187. // Resume object
  188. ////////////////////////////////////////////////////////////////////////////////
  189. void CWeapon::Resume(void)
  190. {
  191. m_sSuspend--;
  192. // If we're actually going to start updating again, we need to reset
  193. // the time so as to ignore any time that passed while we were suspended.
  194. // This method is far from precise, but I'm hoping it's good enough.
  195. if (m_sSuspend == 0)
  196. m_lPrevTime = m_pRealm->m_time.GetGameTime();
  197. }
  198. ////////////////////////////////////////////////////////////////////////////////
  199. // Update object
  200. ////////////////////////////////////////////////////////////////////////////////
  201. void CWeapon::Update(void)
  202. {
  203. }
  204. ////////////////////////////////////////////////////////////////////////////////
  205. // Render object
  206. ////////////////////////////////////////////////////////////////////////////////
  207. void CWeapon::Render(void)
  208. {
  209. CSprite* pSprite = GetSprite();
  210. // If the shadow is enabled and the main sprite is visible . . .
  211. if (m_spriteShadow.m_pImage && (pSprite->m_sInFlags & CSprite::InHidden) == 0)
  212. {
  213. // Get the height of the terrain from the attribute map
  214. short sY = m_pRealm->GetHeight((short) m_dX, (short) m_dZ);
  215. // Map from 3d to 2d coords
  216. Map3Dto2D(m_dX, (double) sY, m_dZ, &(m_spriteShadow.m_sX2), &(m_spriteShadow.m_sY2) );
  217. // Offset hotspot to center of image.
  218. m_spriteShadow.m_sX2 -= m_spriteShadow.m_pImage->m_sWidth / 2;
  219. m_spriteShadow.m_sY2 -= m_spriteShadow.m_pImage->m_sHeight / 2;
  220. // Priority is based on bottom edge of sprite on X/Z plane!
  221. m_spriteShadow.m_sPriority = MAX(pSprite->m_sPriority - 1, 0);//m_dZ;
  222. // Layer should be based on info we get from attribute map.
  223. m_spriteShadow.m_sLayer = pSprite->m_sLayer;
  224. // Set the alpha level based on the height difference
  225. m_spriteShadow.m_sAlphaLevel = 200 - ((short) m_dY - sY);
  226. // Check bounds . . .
  227. if (m_spriteShadow.m_sAlphaLevel < 0)
  228. {
  229. m_spriteShadow.m_sAlphaLevel = 0;
  230. }
  231. else if (m_spriteShadow.m_sAlphaLevel > 255)
  232. {
  233. m_spriteShadow.m_sAlphaLevel = 255;
  234. }
  235. // If the main sprite is on the ground, then hide the shadow.
  236. if ((short) m_dY - sY == 0)
  237. m_spriteShadow.m_sInFlags |= CSprite::InHidden;
  238. else
  239. m_spriteShadow.m_sInFlags &= ~CSprite::InHidden;
  240. // Update sprite in scene
  241. m_pRealm->m_scene.UpdateSprite(&m_spriteShadow);
  242. }
  243. else
  244. {
  245. m_spriteShadow.m_sInFlags |= CSprite::InHidden;
  246. }
  247. }
  248. ////////////////////////////////////////////////////////////////////////////////
  249. // Called by editor to init new object at specified position
  250. ////////////////////////////////////////////////////////////////////////////////
  251. short CWeapon::EditNew( // Returns 0 if successfull, non-zero otherwise
  252. short sX, // In: New x coord
  253. short sY, // In: New y coord
  254. short sZ) // In: New z coord
  255. {
  256. short sResult = 0;
  257. // Use specified position
  258. m_dX = (double)sX;
  259. m_dY = (double)sY;
  260. m_dZ = (double)sZ;
  261. return sResult;
  262. }
  263. ////////////////////////////////////////////////////////////////////////////////
  264. // Called by editor to modify object
  265. ////////////////////////////////////////////////////////////////////////////////
  266. short CWeapon::EditModify(void)
  267. {
  268. return 0;
  269. }
  270. ////////////////////////////////////////////////////////////////////////////////
  271. // Called by editor to move object to specified position
  272. ////////////////////////////////////////////////////////////////////////////////
  273. short CWeapon::EditMove( // Returns 0 if successfull, non-zero otherwise
  274. short sX, // In: New x coord
  275. short sY, // In: New y coord
  276. short sZ) // In: New z coord
  277. {
  278. m_dX = (double)sX;
  279. m_dY = (double)sY;
  280. m_dZ = (double)sZ;
  281. return 0;
  282. }
  283. ////////////////////////////////////////////////////////////////////////////////
  284. // Called by editor to update object
  285. ////////////////////////////////////////////////////////////////////////////////
  286. void CWeapon::EditUpdate(void)
  287. {
  288. }
  289. ////////////////////////////////////////////////////////////////////////////////
  290. // Called by editor to render object
  291. ////////////////////////////////////////////////////////////////////////////////
  292. void CWeapon::EditRender(void)
  293. {
  294. // In some cases, object's might need to do a special-case render in edit
  295. // mode because Startup() isn't called. In this case it doesn't matter, so
  296. // we can call the normal Render().
  297. Render();
  298. }
  299. ////////////////////////////////////////////////////////////////////////////////
  300. // Get all required resources
  301. ////////////////////////////////////////////////////////////////////////////////
  302. short CWeapon::GetResources(void) // Returns 0 if successfull, non-zero otherwise
  303. {
  304. return 0;
  305. }
  306. ////////////////////////////////////////////////////////////////////////////////
  307. // Free all resources
  308. ////////////////////////////////////////////////////////////////////////////////
  309. short CWeapon::FreeResources(void) // Returns 0 if successfull, non-zero otherwise
  310. {
  311. return 0;
  312. }
  313. ////////////////////////////////////////////////////////////////////////////////
  314. // BounceAngle
  315. ////////////////////////////////////////////////////////////////////////////////
  316. double CWeapon::BounceAngle(double dRot)
  317. {
  318. short sRot = (short) dRot;
  319. short sBounceAngle = (((((sRot / 90) + 1) * 180) - sRot) % 360);
  320. return (double) sBounceAngle;
  321. }
  322. ////////////////////////////////////////////////////////////////////////////////
  323. // Process all messages currently in the message queue through
  324. // ProcessMessage().
  325. // (virtual).
  326. ////////////////////////////////////////////////////////////////////////////////
  327. void CWeapon::ProcessMessages(void)
  328. {
  329. // Check queue of messages.
  330. GameMessage msg;
  331. while (m_MessageQueue.DeQ(&msg) == true)
  332. {
  333. ProcessMessage(&msg);
  334. }
  335. }
  336. ////////////////////////////////////////////////////////////////////////////////
  337. // Process the specified message. For most messages, this function
  338. // will call the equivalent On* function.
  339. // (virtual).
  340. ////////////////////////////////////////////////////////////////////////////////
  341. void CWeapon::ProcessMessage( // Returns nothing.
  342. GameMessage* pmsg) // Message to process.
  343. {
  344. switch (pmsg->msg_Generic.eType)
  345. {
  346. case typeShot:
  347. OnShotMsg(&(pmsg->msg_Shot) );
  348. break;
  349. case typeExplosion:
  350. OnExplosionMsg(&(pmsg->msg_Explosion) );
  351. break;
  352. case typeBurn:
  353. OnBurnMsg(&(pmsg->msg_Burn) );
  354. break;
  355. case typeObjectDelete:
  356. OnDeleteMsg(&(pmsg->msg_ObjectDelete) );
  357. break;
  358. case typeTrigger:
  359. OnTriggerMsg(&(pmsg->msg_Trigger) );
  360. break;
  361. default:
  362. // Should this complain when it doesn't know a message type?
  363. break;
  364. }
  365. }
  366. ////////////////////////////////////////////////////////////////////////////////
  367. // Handles a msg_Shot.
  368. // (virtual).
  369. ////////////////////////////////////////////////////////////////////////////////
  370. void CWeapon::OnShotMsg( // Returns nothing.
  371. Shot_Message* pshotmsg) // In: Message to handle.
  372. {
  373. }
  374. ////////////////////////////////////////////////////////////////////////////////
  375. // Handles an Explosion_Message.
  376. // (virtual).
  377. ////////////////////////////////////////////////////////////////////////////////
  378. void CWeapon::OnExplosionMsg( // Returns nothing.
  379. Explosion_Message* pexplosionmsg) // In: Message to handle.
  380. {
  381. }
  382. ////////////////////////////////////////////////////////////////////////////////
  383. // Handles a Burn_Message.
  384. // (virtual).
  385. ////////////////////////////////////////////////////////////////////////////////
  386. void CWeapon::OnBurnMsg( // Returns nothing.
  387. Burn_Message* pburnmsg) // In: Message to handle.
  388. {
  389. }
  390. ////////////////////////////////////////////////////////////////////////////////
  391. // Handles an ObjectDelete_Message.
  392. // (virtual).
  393. ////////////////////////////////////////////////////////////////////////////////
  394. void CWeapon::OnDeleteMsg( // Returns nothing.
  395. ObjectDelete_Message* pdeletemsg) // In: Message to handle.
  396. {
  397. }
  398. ////////////////////////////////////////////////////////////////////////////////
  399. // Handles a Trigger_Message
  400. // (virtual).
  401. ////////////////////////////////////////////////////////////////////////////////
  402. void CWeapon::OnTriggerMsg( // Returns nothing
  403. Trigger_Message* ptriggermsg) // In: Message to handle
  404. {
  405. }
  406. ////////////////////////////////////////////////////////////////////////////////
  407. // PrepareShadow
  408. ////////////////////////////////////////////////////////////////////////////////
  409. short CWeapon::PrepareShadow(void)
  410. {
  411. short sResult = SUCCESS;
  412. // If the shadow doesn't have resource loaded yet, load the default
  413. if (m_spriteShadow.m_pImage == NULL)
  414. {
  415. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(SHADOW_FILE), &(m_spriteShadow.m_pImage), RFile::LittleEndian);
  416. }
  417. // If a resource is available, set the shadow to visible.
  418. if (sResult == SUCCESS)
  419. m_spriteShadow.m_sInFlags &= ~CSprite::InHidden;
  420. return sResult;
  421. }
  422. ////////////////////////////////////////////////////////////////////////////////
  423. // EOF
  424. ////////////////////////////////////////////////////////////////////////////////