character.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  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. // character.cpp
  19. // Project: Postal
  20. //
  21. // This module implements the CCharacter class which is the class of generic
  22. // character functionality for game characters.
  23. //
  24. // History:
  25. //
  26. // 03/03/97 BRH,JMI Started this generic character object to reduce the
  27. // amount of redundant code.
  28. //
  29. // 03/04/97 JMI Changed calling convention and naming for velocities and
  30. // position functions and added a Deluxe updater.
  31. //
  32. // 03/04/97 JMI The m_sprite.m_sInFlags are now only initialized in
  33. // Startup() (instead of in Render()). If it was in Render(),
  34. // it overwrites any other initializations that take place on
  35. // these bit flags (such as XRay).
  36. //
  37. // 03/04/97 JMI UpdateVelocities() now makes sure the delta value is cor-
  38. // rect even when m_dVel is capped or trimmed.
  39. // Drags are now added (was subtracting them).
  40. // Commented out code to restore old velocities (as if accel
  41. // did not take place) when no new position was obtained.
  42. //
  43. // 03/04/97 JMI Added support for messages.
  44. //
  45. // 03/05/97 JMI Converted all rspMod360() calls to new calling convention.
  46. // Added fire response in OnBurnMsg().
  47. // Added UpdateFirePosition().
  48. //
  49. // 03/05/97 JMI Added PrepareWeapon() and ShootWeapon() that can handle
  50. // any current CThing weapon (i.e., cannot handle bullets).
  51. //
  52. // 03/05/97 BRH Added code to WhileShot, WhileBurning, WhileBlownUp,
  53. // WhileDying for default functionality for most of the
  54. // victims and enemy guys.
  55. //
  56. // 03/05/97 JMI Now OnDead() draws current animation frame into the back-
  57. // ground.
  58. //
  59. // 03/05/97 JMI Added handler for suicide message.
  60. // WhileBlownUp() now stops all horizontal movement when the
  61. // character encounters the 'no walk' attrib.
  62. // Added UpdateVelocity() inline in attempt to unify the way
  63. // velocity is updated. Works for two of three cases, haven't
  64. // tried third for fear of hosedness.
  65. // Currently vertical tolerance is not considered in
  66. // ValidatePosition().
  67. // Made external horizontal drag in OnExplosionMsg() negative.
  68. //
  69. // 03/06/97 JMI Re-enabled vertical tolerance utilizing new m_bAboveTerrain
  70. // boolean.
  71. //
  72. // 03/06/97 JMI Increased horizontal surface drag.
  73. // Now Render() uses the combined attributes for the layer.
  74. //
  75. // 03/06/97 JMI No longer relies on m_pWeapon to store weapon ptr. But,
  76. // if a derived class sets m_pWeapon to point to a weapon,
  77. // Render() will still update its transform.
  78. // This should be removed as soon as no one uses m_pWeapon.
  79. //
  80. // 03/06/97 JMI Now, if EditRect() does not have enough info to create
  81. // a rectangle, it tries a couple ways.
  82. //
  83. // 03/06/97 JMI Now ShootWeapon() gets the rigid body transform from the
  84. // current animation instead of from the child's sprite.
  85. //
  86. // 03/06/97 JMI Now allows a character to fall off of things but not onto
  87. // unless the height difference is within tolerance.
  88. //
  89. // 03/06/97 JMI Added a GetAttributes() function.
  90. // Now uses GetAttributes() instead of manually doing it.
  91. //
  92. // 03/12/97 JMI Actually, mistakenly, last time I put GetAttributes() in
  93. // WhileBlowingUp() instead of in DeluxeUpdatePosition().
  94. // Now DeluxeUpdatePosition() utilizes GetAttributes().
  95. // DeluxeUpdatePosition() now takes the duration in seconds
  96. // as a parameter instead of calculating it itself (and over-
  97. // writing m_lPrevTime).
  98. //
  99. // 03/13/97 JMI GetAttributes() was combining ONLY the layer bits. What
  100. // I really wanted it to do was combine all but the height
  101. // bits. Now it uses ~ATTRIBUTE_HEIGHT_MASK to combined the
  102. // bits.
  103. //
  104. // 03/13/97 JMI Load now takes a version number.
  105. //
  106. // 03/17/97 JMI Now most of CCharacter's functionality is implemented in
  107. // its base class, CThing3d.
  108. //
  109. // 03/21/97 JMI Removed m_pWeapon.
  110. //
  111. // 03/21/97 JMI ShootWeapon() now sets the rotation of the weapon beforing
  112. // detaching it.
  113. //
  114. // 03/21/97 JMI ShootWeapon() now returns a ptr to the weapon.
  115. //
  116. // 04/02/97 JMI OnDead() now destroys any weapon the character still has.
  117. // The idea is that, if the weapon was just rendered as a
  118. // child object onto the background, it should no longer exist
  119. // in CThing form. Also, undeleted child weapons will clutter
  120. // memory until the realm they're in is destroyed.
  121. // Removed m_pCurrentAnim.
  122. //
  123. // 04/02/97 JMI PrepareWeapon() now, also, returns a ptr to the weapon.
  124. //
  125. // 04/15/97 BRH We were noticing that the guys no longer turned dark after
  126. // being burnt. I added the change to m_sBrightness in
  127. // WhileBurning() so that he turns dark when dead.
  128. //
  129. // 04/23/97 JMI Added IsPathClear(), a rather deluxe function.
  130. //
  131. // 04/24/97 JMI #if 0'd out the debug portion of IsPathClear() which
  132. // displayed the check path as red or green line depending on
  133. // whether or not the path was entirely clear.
  134. //
  135. // 04/25/97 JMI Broke blood pool creation out of MakeBloody() and into
  136. // MakeBloodPool().
  137. //
  138. // 04/28/97 BRH Changed PrepareWeapon to use the new fake class ID's to
  139. // identify weapons that don't have an object associated with
  140. // them like guns. Now this function is generic enough that
  141. // the CDude doesn't have to override its own version of
  142. // PrepareWeapon.
  143. //
  144. // 04/28/97 JMI Changed ShootWeapon() to work for CShotGunID, CPistolID,
  145. // CMachineGunID, and CFireballID.
  146. // Also, added FireBullets() to simplify firing of bullets.
  147. //
  148. // 04/29/97 JMI Added case to ShootWeapon() to handle mines.
  149. //
  150. // 04/29/97 JMI Changed references to child weapon's m_sprite to
  151. // GetSprite() calls and took out special cases for
  152. // CFireballID and CMineID.
  153. //
  154. // 05/02/97 JMI FireBullets() now returns type bool indicating whether or
  155. // not someone/thing was hit by the bullets.
  156. //
  157. // 05/02/97 JMI Added timer explicitly for CCharacter::While/On* functions.
  158. // Utilized the timer in WhileBurning() to make sure dude dies
  159. // within a certain amount of burning and to make the dude
  160. // darken while he burns instead of just at the end.
  161. //
  162. // 05/13/97 JMI Now MakeBloody() creates chunks based on the passed damage.
  163. //
  164. // 05/14/97 JMI Now Render() uses PositionChild() to position the weapon.
  165. //
  166. // 05/16/97 JMI Added directionality to blood.
  167. //
  168. // 05/22/97 JMI Added bullet casings and shells.
  169. // Also, made all particle effects (Blood, Casings, and
  170. // Shells) obey g_GameSettings.m_sParticleEffects.
  171. //
  172. // 05/23/97 JMI Lowered velocity of shells and casings generated by Fire-
  173. // Bullets().
  174. //
  175. // 05/26/97 JMI Lowered vertical velocity of shells and casing generated
  176. // by FireBullets().
  177. //
  178. // 05/26/97 BRH Added CAssaultWeapon which is just the shot gun allowed
  179. // to rapid fire.
  180. //
  181. // 05/29/97 JMI Removed references to m_pRealm->m_pAttribMap which no longer
  182. // exists.
  183. //
  184. // 05/30/97 JMI Changed FireBullets() to only play the sample once (through
  185. // FireDeluxe(...) ).
  186. //
  187. // 06/02/97 JMI Added an WhileOnLadder().
  188. //
  189. // 06/02/97 JMI Removed ladder stuff.
  190. //
  191. // 06/05/97 JMI Changed m_sHitPoints to m_stockpile.m_sHitPoints to
  192. // accommodate new m_stockpile in base class, CThing3d (which
  193. // used to contain the m_sHitPoints).
  194. //
  195. // 06/08/97 BRH Added IlluminateTarget() function to check for targets within
  196. // a cone in the direction you specify. This will be used by the
  197. // CDude for targeting feedback. He will use it to place a
  198. // target sprite on the target he is aiming at.
  199. //
  200. // 06/10/97 JMI Removed CSmash::Misc from the default bits to collide with
  201. // when using FireBullets().
  202. //
  203. // 06/11/97 BRH Added passing of Shooter ID down into the Shot message and
  204. // to the other weapons in ShootWeapon(). Also added the
  205. // bullets damage chart to give tunability to different
  206. // weapons and target vs shooter differences.
  207. //
  208. // 06/11/97 JMI Added Preload() for loading assets used during play.
  209. //
  210. // 06/11/97 BRH Added the ID of the killer and used that to report to
  211. // the score module at time of death.
  212. //
  213. // 06/12/97 JMI 3D weapons are now initially (in PrepareWeapon() ) set to
  214. // CWeapon::State_Hide.
  215. //
  216. // 06/13/97 JMI Added WhileHoldingWeapon().
  217. //
  218. // 06/15/97 JMI Now OnShotMsg() checks to make sure we're receiving damage
  219. // before calling MakeBloody() (if someone's wearing a kevlar
  220. // vest, they may receive the impact forces of the bullet but
  221. // little or no damage).
  222. //
  223. // 06/17/97 JMI Converted all occurrences of rand() to GetRand() and
  224. // srand() to SeedRand().
  225. //
  226. // 06/24/97 JMI Now intializes animthing's m_msg's priority to 0 on in
  227. // MakeBloodPool() so we don't end up with randomly
  228. // prioritized messages.
  229. //
  230. // 06/30/97 JMI Now uses CRealm's new GetRealmWidth() and *Height()
  231. // for dimensions of realm's X/Z plane.
  232. //
  233. // 07/01/97 JMI Now passes rigid body transform to PositionChild().
  234. //
  235. // 07/09/97 JMI Now uses m_pRealm->Make2dResPath() to get the fullpath
  236. // for 2D image components.
  237. //
  238. // 07/09/97 JMI Changed Preload() to take a pointer to the calling realm
  239. // as a parameter.
  240. //
  241. // 07/21/97 JMI Now has an Update() which updates the weapon's position.
  242. // Also, added ValidateWeaponPosition().
  243. //
  244. // 07/21/97 JMI Now ValidateWeaponPosition() checks to make sure the weapon
  245. // state is not State_Hide before checking the position.
  246. //
  247. // 07/27/97 JMI No longer calls CRealm::Make2dResPath() on res names
  248. // given to CAnimThing as it already does that
  249. // automatically.
  250. // Also, now appropriately places blood splats on outside of
  251. // character's body based on opposite angle of impact.
  252. //
  253. // 07/30/97 JMI Now copies necessary ammo from ccharacter's stockpile to
  254. // the ammo, if required.
  255. //
  256. // 08/02/97 BRH Added virtual OnHelpMsg function.
  257. //
  258. // 08/07/97 JMI Added ShootWeapon()/FireBullets() support for the double
  259. // barrel.
  260. //
  261. // 08/07/97 JMI Now blood splat lands on ground (at terrrain height)
  262. // instead of at the character's Y.
  263. // Changed RAND_SWAY to an inline that considers the scene
  264. // scale.
  265. //
  266. // 08/07/97 JMI In last version did not put the blood pool on the ground
  267. // while animating.
  268. //
  269. // 08/08/97 JMI Now this class plays the flamer sound and handles its
  270. // loopage.
  271. //
  272. // 08/08/97 JMI Upgraded PrepareWeapon(), ShootWeapon(), and FireBullets()
  273. // to handle AutoRifle, Uzi, and SmallPistol.
  274. //
  275. // 08/09/97 JMI Now MakeBloody() tries to keep shots low for characters
  276. // that are in the writhing state.
  277. //
  278. // 08/09/97 JMI Converted a use of TransformPtsToRealm() to GetLinkPoint().
  279. //
  280. // 08/11/97 JMI Changed DoubleBarrel sound to g_smidDeathWadLaunch (was the
  281. // ol' wimpy g_smidShotGun).
  282. //
  283. // 08/13/97 JMI Temporarily shows particle effects regardless of user
  284. // setting in multiplayer mode.
  285. //
  286. // 08/17/97 JMI Now FireBullets() will never shoot writhers.
  287. //
  288. // 08/17/97 JMI Changed m_pthingParent to m_idParent.
  289. //
  290. // 08/18/97 JMI Now applies no randomization to CChunks and instead allows
  291. // CChunk::Setup() to apply its own randomization based on
  292. // sway values passed to it. Also, CChunk::Construct() will
  293. // now fail if particles are disabled so we don't need to
  294. // check for that anymore.
  295. //
  296. // 08/18/97 JMI FireBullets() now includes the random variation in angle
  297. // in the bullet message (before it used the UNswayed angle).
  298. //
  299. // 08/18/97 JMI Changed OnDead() to call DeadRender3D() (which used to be
  300. // known/called as just another Render() overload).
  301. //
  302. // 08/24/97 JMI Adjusted shotgun pellets to be 8 when shot by a dude into
  303. // a non dude. Also, adjusted machine gun bullets to be 15
  304. // when shot by a dude into a non dude.
  305. //
  306. // 08/28/97 BRH Added a virtual put me down message handler.
  307. //
  308. ////////////////////////////////////////////////////////////////////////////////
  309. #define CHARACTER_CPP
  310. #include "RSPiX.h"
  311. #include <math.h>
  312. #include "character.h"
  313. #include "reality.h"
  314. #include "AnimThing.h"
  315. #include "fireball.h"
  316. #include "mine.h"
  317. #include "fire.h"
  318. #include "chunk.h"
  319. #include "score.h"
  320. #include "deathWad.h"
  321. ////////////////////////////////////////////////////////////////////////////////
  322. // Macros/types/etc.
  323. ////////////////////////////////////////////////////////////////////////////////
  324. #define MAX_FORE_VEL 80.0
  325. #define MAX_BACK_VEL -60.0
  326. #define MAX_STEPUP_THRESHOLD 10.0
  327. // Determines the number of elements in the passed array at compile time.
  328. #define NUM_ELEMENTS(a) (sizeof(a) / sizeof(a[0]) )
  329. // Random amount the fire angle can adjust for bullets.
  330. #define FIRE_ANGLE_Y_SWAY 15
  331. #define FIRE_ANGLE_Z_SWAY 10
  332. #define MAX_BULLET_RANGE 400
  333. #define MAX_SHELL_RANGE 200
  334. #define NUM_SHOTS_PER_SHELL 7
  335. #define FIREBALL_LIVE_MS 500
  336. #define MAX_TIME_WITHOUT_FLAMAGE 500
  337. // Random amount the blood splat can adjust.
  338. #define BLOOD_SPLAT_SWAY 10
  339. // This is the distance in realm units from the characters' centers
  340. // to the outsides of their bodies. This is used to adjust things
  341. // like blood splats around the outside of their torsos.
  342. #define TORSO_RADIUS 5
  343. #define BLOOD_SPLAT_RES_NAME "BloodFront.aan"
  344. #define BLOOD_POOL_RES_NAME "BloodPool.aan"
  345. // Light level for burnt character.
  346. #define BURNT_BRIGHTNESS -40 // -128 to 127.
  347. // Amount of time for character to burn.
  348. #define BURN_DURATION 2500 // In ms.
  349. // Sets a value pointed to if ptr is not NULL.
  350. #define SET(pval, val) ((pval != NULL) ? *pval = val : val)
  351. // Maximum chunks that can be generated from one MakeBloody().
  352. #define MAX_CHUNKS 10
  353. // Amount chunks can vary from the direction of damage.
  354. #define CHUNKS_DAMAGE_DIR_SWAY 135
  355. #define SHOOTER_IS_DUDE 0x0008
  356. #define TARGET_IS_DUDE 0x0004
  357. #define WEAPON_IS_PISTOL 0x0000
  358. #define WEAPON_IS_MACHINEGUN 0x0001
  359. #define WEAPON_IS_SHOTGUN 0x0002
  360. #define WEAPON_IS_ASSAULT 0x0003
  361. ////////////////////////////////////////////////////////////////////////////////
  362. // Variables/data
  363. ////////////////////////////////////////////////////////////////////////////////
  364. static short ms_asBulletDamageChart[16] =
  365. {
  366. // Damage Shooter Target Weapon
  367. 10, // 0000 NonDude NonDude Pistol
  368. 10, // 0001 NonDude NonDude MachineGun
  369. 10, // 0010 NonDude NonDude ShotGun
  370. 10, // 0011 NonDude NonDude AssaultWeapon
  371. 10, // 0100 NonDude Dude Pistol
  372. 10, // 0101 NonDude Dude MachineGun
  373. 10, // 0110 NonDude Dude ShotGun
  374. 7, // 0111 NonDude Dude AssaultWeapon
  375. 10, // 1000 Dude NonDude Pistol
  376. 15, // 1001 Dude NonDude MachineGun
  377. 8, // 1010 Dude NonDude ShotGun
  378. 10, // 1011 Dude NonDude AssaultWeapon
  379. 10, // 1100 Dude Dude Pistol
  380. 10, // 1101 Dude Dude MachineGun
  381. 10, // 1110 Dude Dude ShotGun
  382. 10, // 1111 Dude Dude AssaultWeapon
  383. };
  384. ////////////////////////////////////////////////////////////////////////////////
  385. // Gets a random value that is +/- lRange with sway scaled by the current
  386. // scene scaling.
  387. ////////////////////////////////////////////////////////////////////////////////
  388. inline
  389. long GetRandSway( // Returns sway value.
  390. long lRange, // In: Total range of output value.
  391. double dScale) // In: Scaling.
  392. {
  393. // There's two approaches possible here:
  394. // 1) Scale lRange by current scene scale.
  395. // 2) Scale result by current scene scale.
  396. // I think I like the first option better because it allows for more
  397. // granularity in the result. But, since that is variable on a per level
  398. // basis, I'm not sure it's worth worrying about.
  399. // Going with option 1.
  400. lRange *= dScale;
  401. return (GetRand() % lRange) - lRange / 2;
  402. }
  403. ////////////////////////////////////////////////////////////////////////////////
  404. // Load object (should call base class version!)
  405. ////////////////////////////////////////////////////////////////////////////////
  406. short CCharacter::Load( // Returns 0 if successfull, non-zero otherwise
  407. RFile* pFile, // In: File to load from
  408. bool bEditMode, // In: True for edit mode, false otherwise
  409. short sFileCount, // In: File count (unique per file, never 0)
  410. ULONG ulFileVersion) // In: Version of file format to load.
  411. {
  412. // Call the CThing base class load to get the instance ID
  413. short sResult = CThing3d::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  414. if (sResult == 0)
  415. {
  416. switch (ulFileVersion)
  417. {
  418. default:
  419. case 3:
  420. pFile->Read(&m_eWeaponType);
  421. break;
  422. case 2: // Versions 1 and 2, when CCharacter was not descended from
  423. case 1: // CThing3d, loaded the weapon type amidst all the other data.
  424. // Load object data
  425. // **FUDGE.
  426. m_eWeaponType = CThing::CRocketID;
  427. break;
  428. }
  429. // Make sure there were no file errors or format errors . . .
  430. if (!pFile->Error() && sResult == 0)
  431. {
  432. // Success.
  433. }
  434. else
  435. {
  436. sResult = -1;
  437. TRACE("CCharacter::Load(): Error reading from file!\n");
  438. }
  439. }
  440. return sResult;
  441. }
  442. ////////////////////////////////////////////////////////////////////////////////
  443. // Save object (should call base class version!)
  444. ////////////////////////////////////////////////////////////////////////////////
  445. short CCharacter::Save( // Returns 0 if successfull, non-zero otherwise
  446. RFile* pFile, // In: File to save to
  447. short sFileCount) // In: File count (unique per file, never 0)
  448. {
  449. // Call the base class save to save the u16InstanceID
  450. short sResult = CThing3d::Save(pFile, sFileCount);
  451. if (sResult == 0)
  452. {
  453. // Save object data
  454. pFile->Write(&m_eWeaponType);
  455. sResult = pFile->Error();
  456. }
  457. return sResult;
  458. }
  459. ////////////////////////////////////////////////////////////////////////////////
  460. // Update object.
  461. // (virtual).
  462. ////////////////////////////////////////////////////////////////////////////////
  463. void CCharacter::Update(void) // Returns nothing.
  464. {
  465. if (m_u16IdWeapon != CIdBank::IdNil)
  466. {
  467. CWeapon* pweapon;
  468. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pweapon, m_u16IdWeapon) == 0)
  469. {
  470. RTransform* ptransWeapon = m_panimCur->m_ptransRigid->GetAtTime(m_lAnimTime);
  471. // Position weapon.
  472. PositionChild(
  473. pweapon->GetSprite(), // In: Child sprite to position.
  474. ptransWeapon, // In: Transform specifying position.
  475. &(pweapon->m_dX), // Out: New position of child.
  476. &(pweapon->m_dY), // Out: New position of child.
  477. &(pweapon->m_dZ) ); // Out: New position of child.
  478. }
  479. }
  480. // If we have a weapon sound play instance . . .
  481. if (m_siLastWeaponPlayInstance)
  482. {
  483. // If time has expired . . .
  484. if (m_pRealm->m_time.GetGameTime() > m_lStopLoopingWeaponSoundTime)
  485. {
  486. // Stop looping the sound.
  487. StopLoopingSample(m_siLastWeaponPlayInstance);
  488. // Forget about it.
  489. m_siLastWeaponPlayInstance = 0;
  490. }
  491. }
  492. // Call base class.
  493. CThing3d::Update();
  494. }
  495. ////////////////////////////////////////////////////////////////////////////////
  496. // Render object
  497. ////////////////////////////////////////////////////////////////////////////////
  498. void CCharacter::Render(void)
  499. {
  500. // Call base class.
  501. CThing3d::Render();
  502. }
  503. //---------------------------------------------------------------------------
  504. // Useful generic character state-specific functionality.
  505. //---------------------------------------------------------------------------
  506. ////////////////////////////////////////////////////////////////////////////////
  507. // Implements basic one-time functionality for each time State_Shot is
  508. // entered.
  509. ////////////////////////////////////////////////////////////////////////////////
  510. void CCharacter::OnShot(void)
  511. {
  512. CThing3d::OnShot();
  513. }
  514. ////////////////////////////////////////////////////////////////////////////////
  515. // Implements basic functionality while shot and returns true
  516. // until the state is completed.
  517. ////////////////////////////////////////////////////////////////////////////////
  518. bool CCharacter::WhileShot(void) // Returns true until state is complete
  519. {
  520. return CThing3d::WhileShot();
  521. }
  522. ////////////////////////////////////////////////////////////////////////////////
  523. // Implements basic functionality while being blown up and returns true
  524. // until the state is completed.
  525. ////////////////////////////////////////////////////////////////////////////////
  526. bool CCharacter::WhileBlownUp(void) // Returns true until state is complete.
  527. {
  528. return CThing3d::WhileBlownUp();
  529. }
  530. ////////////////////////////////////////////////////////////////////////////////
  531. // Implements basic functionality while being on fire and returns true
  532. // until the state is completed.
  533. ////////////////////////////////////////////////////////////////////////////////
  534. bool CCharacter::WhileBurning(void) // Returns true until state is complete.
  535. {
  536. bool bStatePersists = true; // Assume not done.
  537. // Get time from last call in seconds. Should this be passed in so we don't
  538. // update m_lPrevTime???
  539. long lCurTime = m_pRealm->m_time.GetGameTime();
  540. double dSeconds = double(lCurTime - m_lPrevTime) / 1000.0;
  541. m_lPrevTime = lCurTime;
  542. // Make character run around while on fire, varying in direction.
  543. m_dAcc = 150;
  544. m_dRot = rspMod360(m_dRot + (GetRand() % 30) - 15);
  545. DeluxeUpdatePosVel(dSeconds);
  546. // If the fire still exists . . .
  547. CFire* pfire;
  548. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pfire, m_u16IdFire) == 0)
  549. {
  550. // If the fire is still burning . . .
  551. if (pfire->IsBurning() != FALSE)
  552. {
  553. // Brightness is the ratio of the amount of time expired to the
  554. // total time multiplied by the destination brightness.
  555. long lTimeExpired = MIN(lCurTime + BURN_DURATION - m_lCharacterTimer, (long)BURN_DURATION);
  556. m_sBrightness = lTimeExpired * BURNT_BRIGHTNESS / BURN_DURATION;
  557. // If time has expired . . .
  558. if (lTimeExpired >= BURN_DURATION)
  559. {
  560. bStatePersists = false;
  561. }
  562. }
  563. else
  564. {
  565. // We're done.
  566. bStatePersists = false;
  567. }
  568. }
  569. else
  570. {
  571. // We're done.
  572. bStatePersists = false;
  573. }
  574. // If we're done . . .
  575. if (bStatePersists == false)
  576. {
  577. // Let's make sure we're dead.
  578. m_stockpile.m_sHitPoints = 0;
  579. }
  580. return bStatePersists;
  581. }
  582. ////////////////////////////////////////////////////////////////////////////////
  583. // Implements basic functionality while dying and returns true
  584. // until the state is completed.
  585. ////////////////////////////////////////////////////////////////////////////////
  586. bool CCharacter::WhileDying(void) // Returns true until state is complete.
  587. {
  588. bool bStatePersists = true; // Assume not done.
  589. // When he finishes his current animation (assuming dying anim) then he is
  590. // officially dead.
  591. if (m_lAnimTime > m_panimCur->m_psops->TotalTime())
  592. bStatePersists = false;
  593. return bStatePersists;
  594. }
  595. ////////////////////////////////////////////////////////////////////////////////
  596. // Implements basic functionality while holding and preparing to release
  597. // a weapon. Shows the weapon when the event hits 1 and releases the
  598. // weapon via ShootWeapon() when the event hits 2.
  599. ////////////////////////////////////////////////////////////////////////////////
  600. bool CCharacter::WhileHoldingWeapon( // Returns true when weapon is released.
  601. U32 u32BitsInclude, // In: Collision bits to pass to ShootWeapon
  602. U32 u32BitsDontcare, // In: Collision bits to pass to ShootWeapon
  603. U32 u32BitsExclude) // In: Collision bits to pass to ShootWeapon
  604. {
  605. bool bReleased = false; // Assume not released.
  606. U8 u8Event = *( (U8*)(m_panimCur->m_pevent->GetAtTime(m_lAnimTime) ) );
  607. // Check for show point in animation . . .
  608. if (u8Event > 0)
  609. {
  610. CWeapon* pweapon;
  611. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pweapon, m_u16IdWeapon) == 0)
  612. {
  613. // If hidden . . .
  614. if (pweapon->m_eState == CWeapon::State_Hide)
  615. {
  616. // Show.
  617. pweapon->m_eState = CWeapon::State_Idle;
  618. }
  619. // Check for release point . . .
  620. else if (u8Event > 1)
  621. {
  622. // Release.
  623. ShootWeapon(u32BitsInclude, u32BitsDontcare, u32BitsExclude);
  624. bReleased = true;
  625. }
  626. }
  627. else
  628. {
  629. // Check for release point . . .
  630. if (u8Event > 1)
  631. {
  632. // Release.
  633. ShootWeapon(u32BitsInclude, u32BitsDontcare, u32BitsExclude);
  634. bReleased = true;
  635. }
  636. }
  637. }
  638. return bReleased;
  639. }
  640. ////////////////////////////////////////////////////////////////////////////////
  641. // Implements basic one-time functionality for each time State_Dead is
  642. // entered.
  643. ////////////////////////////////////////////////////////////////////////////////
  644. void CCharacter::OnDead(void)
  645. {
  646. CHood* phood = m_pRealm->m_phood;
  647. // Render current dead frame into background to stay.
  648. m_pRealm->m_scene.DeadRender3D(
  649. phood->m_pimBackground, // Destination image.
  650. &m_sprite, // Tree of 3D sprites to render.
  651. phood); // Dst clip rect.
  652. // If we just rendered a child weapon into the background . . .
  653. CThing* pthing;
  654. if (m_pRealm->m_idbank.GetThingByID(&pthing, m_u16IdWeapon) == 0)
  655. {
  656. // It has a permanent place in the background and, therefore, is no
  657. // longer needed.
  658. // "Shoot" it (release it).
  659. ShootWeapon();
  660. // Delete it!
  661. GameMessage msg;
  662. msg.msg_ObjectDelete.eType = typeObjectDelete;
  663. msg.msg_ObjectDelete.sPriority = 0;
  664. SendThingMessage(&msg, pthing);
  665. }
  666. // Register death with the score module
  667. ScoreRegisterKill(m_pRealm, m_u16InstanceId, m_u16KillerId);
  668. }
  669. ////////////////////////////////////////////////////////////////////////////////
  670. // Implements basic functionality while being run over and returns true
  671. // until the state is completed.
  672. ////////////////////////////////////////////////////////////////////////////////
  673. bool CCharacter::WhileRunOver(void) // Returns true until state is complete.
  674. {
  675. return CThing3d::WhileRunOver();
  676. }
  677. ////////////////////////////////////////////////////////////////////////////////
  678. // Implements one-time functionality for when a weapon is destroyed while
  679. // we were moving it (i.e., before we let go or ShootWeapon()'ed it).
  680. // This can occur when a weapon, while traveling along our rigid body,
  681. // enters terrain.
  682. ////////////////////////////////////////////////////////////////////////////////
  683. // virtual.
  684. void CCharacter::OnWeaponDestroyed(void)
  685. {
  686. // Each higher object should implement this as needed for that type of object
  687. // or not at all. Please call this base though, in case we ever add something
  688. // here.
  689. }
  690. //---------------------------------------------------------------------------
  691. // Useful generic character functionality.
  692. //---------------------------------------------------------------------------
  693. ////////////////////////////////////////////////////////////////////////////////
  694. // Process the specified message. For most messages, this function
  695. // will call the equivalent On* function.
  696. // (virtual).
  697. ////////////////////////////////////////////////////////////////////////////////
  698. void CCharacter::ProcessMessage( // Returns nothing.
  699. GameMessage* pmsg) // Message to process.
  700. {
  701. // Call base class.
  702. CThing3d::ProcessMessage(pmsg);
  703. // Process character specific messages.
  704. switch (pmsg->msg_Generic.eType)
  705. {
  706. case typeDrawBlood:
  707. OnDrawBloodMsg(&(pmsg->msg_DrawBlood) );
  708. break;
  709. case typeSuicide:
  710. OnSuicideMsg(&(pmsg->msg_Suicide) );
  711. break;
  712. default:
  713. // Should this complain when it doesn't know a message type?
  714. break;
  715. }
  716. }
  717. ////////////////////////////////////////////////////////////////////////////////
  718. // Handles a msg_Shot.
  719. // (virtual).
  720. ////////////////////////////////////////////////////////////////////////////////
  721. void CCharacter::OnShotMsg( // Returns nothing.
  722. Shot_Message* pshotmsg) // In: Message to handle.
  723. {
  724. ASSERT(pshotmsg->u16ShooterID != 0xebeb);
  725. m_u16KillerId = pshotmsg->u16ShooterID;
  726. CThing3d::OnShotMsg(pshotmsg);
  727. // If we're receiving any damage from the shot . . .
  728. if (pshotmsg->sDamage > 0)
  729. {
  730. MakeBloody(
  731. pshotmsg->sDamage,
  732. pshotmsg->sAngle,
  733. CHUNKS_DAMAGE_DIR_SWAY);
  734. }
  735. }
  736. ////////////////////////////////////////////////////////////////////////////////
  737. // Handles an Explosion_Message.
  738. // (virtual).
  739. ////////////////////////////////////////////////////////////////////////////////
  740. void CCharacter::OnExplosionMsg( // Returns nothing.
  741. Explosion_Message* pexplosionmsg) // In: Message to handle.
  742. {
  743. ASSERT(pexplosionmsg->u16ShooterID != 0xebeb);
  744. m_u16KillerId = pexplosionmsg->u16ShooterID;
  745. MakeBloody(
  746. pexplosionmsg->sDamage,
  747. 180, // Slightly more efficient than 0 (speeds up mod op).
  748. 360);
  749. CThing3d::OnExplosionMsg(pexplosionmsg);
  750. }
  751. ////////////////////////////////////////////////////////////////////////////////
  752. // Handles a Burn_Message.
  753. // (virtual).
  754. ////////////////////////////////////////////////////////////////////////////////
  755. void CCharacter::OnBurnMsg( // Returns nothing.
  756. Burn_Message* pburnmsg) // In: Message to handle.
  757. {
  758. ASSERT(pburnmsg->u16ShooterID != 0xebeb);
  759. m_u16KillerId = pburnmsg->u16ShooterID;
  760. CThing3d::OnBurnMsg(pburnmsg);
  761. // If we are not yet in the burn state . . .
  762. if (m_state != State_Burning)
  763. {
  764. if (StatsAreAllowed) Stat_Burns++;
  765. // End of burn, if we don't die of 'natural' (being on fire) causes first.
  766. m_lCharacterTimer = m_pRealm->m_time.GetGameTime() + BURN_DURATION;
  767. }
  768. }
  769. ////////////////////////////////////////////////////////////////////////////////
  770. // Handles an ObjectDelete_Message.
  771. // (virtual).
  772. ////////////////////////////////////////////////////////////////////////////////
  773. void CCharacter::OnDeleteMsg( // Returns nothing.
  774. ObjectDelete_Message* pdeletemsg) // In: Message to handle.
  775. {
  776. CThing3d::OnDeleteMsg(pdeletemsg);
  777. }
  778. ////////////////////////////////////////////////////////////////////////////////
  779. // Handles a DrawBlood_Message.
  780. // (virtual).
  781. ////////////////////////////////////////////////////////////////////////////////
  782. void CCharacter::OnDrawBloodMsg( // Returns nothing.
  783. DrawBlood_Message* pdrawbloodmsg) // In: Message to handle.
  784. {
  785. BloodToBackground(pdrawbloodmsg->s2dX, pdrawbloodmsg->s2dY);
  786. }
  787. ////////////////////////////////////////////////////////////////////////////////
  788. // Handles a Suicide_Message.
  789. // (virtual).
  790. ////////////////////////////////////////////////////////////////////////////////
  791. void CCharacter::OnSuicideMsg( // Returns nothing.
  792. Suicide_Message* psuicidemsg) // In: Message to handle.
  793. {
  794. }
  795. ////////////////////////////////////////////////////////////////////////////////
  796. // Handles a Help_Message
  797. // (virtual)
  798. ////////////////////////////////////////////////////////////////////////////////
  799. void CCharacter::OnHelpMsg( // Returns nothing
  800. Help_Message* phelpmsg)
  801. {
  802. }
  803. ////////////////////////////////////////////////////////////////////////////////
  804. // Handles a PutMeDown_Message
  805. // (virtual)
  806. ////////////////////////////////////////////////////////////////////////////////
  807. void CCharacter::OnPutMeDownMsg( // Returns nothing
  808. PutMeDown_Message* pputmedownmsg)
  809. {
  810. }
  811. ////////////////////////////////////////////////////////////////////////////////
  812. // Creates blood splat and pool animations.
  813. ////////////////////////////////////////////////////////////////////////////////
  814. void CCharacter::MakeBloody(
  815. short sDamage, // In: Damage to base carnage on.
  816. short sDamageAngle, // In: Angle in which (NOT from which) damage was
  817. // applied.
  818. short sSwayRange) // In: Random amount chunks can sway from the
  819. // sDamageAngle (If 360, there'll be no noticeable
  820. // damage direction for the chunks).
  821. {
  822. double dHitY = m_dY + GetRandSway(BLOOD_SPLAT_SWAY, m_pRealm->m_phood->m_dScale3d);
  823. double dHitX;
  824. double dHitZ;
  825. // If writhing on the ground . . .
  826. if (m_state == State_Writhing)
  827. {
  828. // Keep the shot low and on our X/Z execution point.
  829. dHitX = m_smash.m_sphere.sphere.X;
  830. dHitZ = m_smash.m_sphere.sphere.Z;
  831. }
  832. else
  833. {
  834. // X/Z position depends on angle of shot (it is opposite).
  835. // That is, the wound appears on a portion of the dude facing the damage
  836. // source (where the bullet came from, the epicenter of the explosion, etc.)
  837. // giving us a little more feedback and realism.
  838. short sDeflectionAngle = rspMod360(sDamageAngle + 180);
  839. dHitX = m_dX + COSQ[sDeflectionAngle] * TORSO_RADIUS;
  840. dHitZ = m_dZ - SINQ[sDeflectionAngle] * TORSO_RADIUS;
  841. // Put it up about half way up character's body.
  842. dHitY += m_sprite.m_sRadius;
  843. }
  844. // Create blood animation.
  845. CAnimThing* pat = new CAnimThing(m_pRealm);
  846. if (pat != NULL)
  847. {
  848. strcpy(pat->m_szResName, BLOOD_SPLAT_RES_NAME);
  849. // Start it up:
  850. // No looping.
  851. pat->m_sLoop = FALSE;
  852. // No notification necessary.
  853. // NOTE: sAngle is currently not utilized.
  854. pat->Setup(dHitX, dHitY, dHitZ + 1);
  855. }
  856. // Create some chunks.
  857. short sNumChunks = MIN(sDamage / 2, MAX_CHUNKS);
  858. short i;
  859. for (i = 0; i < sNumChunks; i++)
  860. {
  861. // Create blood particles . . .
  862. CChunk* pchunk = NULL; // Initialized for safety.
  863. // Note that this will fail if particles are disabled.
  864. if (Construct(CChunkID, m_pRealm, (CThing**)&pchunk) == 0)
  865. {
  866. pchunk->Setup(
  867. dHitX, // Source position.
  868. dHitY, // Source position.
  869. dHitZ, // Source position.
  870. sDamageAngle, // Angle of velocity.
  871. sSwayRange, // Angle sway.
  872. 40, // Velocity (X/Z plane).
  873. 80, // Velocity (X/Z plane) sway.
  874. 50, // Velocity (Vertical).
  875. 100, // Velocity (Vertical) sway.
  876. CChunk::Blood); // Type of chunk.
  877. }
  878. }
  879. // Let's go ahead and create the pool at the same time.
  880. // If it gets bigger it might look like the above anim
  881. // created the pool which, of course, is the desired
  882. // effect.
  883. MakeBloodPool();
  884. }
  885. ////////////////////////////////////////////////////////////////////////////////
  886. // Creates blood pool animation.
  887. ////////////////////////////////////////////////////////////////////////////////
  888. void CCharacter::MakeBloodPool(void)
  889. {
  890. CAnimThing* pat = new CAnimThing(m_pRealm);
  891. if (pat != NULL)
  892. {
  893. strcpy(pat->m_szResName, BLOOD_POOL_RES_NAME);
  894. // Start it up:
  895. // No looping.
  896. pat->m_sLoop = FALSE;
  897. // Need notification to tell us when to put the animation
  898. // in the background.
  899. pat->m_msg.msg_DrawBlood.eType = typeDrawBlood;
  900. pat->m_msg.msg_DrawBlood.sPriority = 0;
  901. pat->m_u16IdSendMsg = m_u16InstanceId;
  902. double dHitX = m_dX + GetRandSway(BLOOD_SPLAT_SWAY, m_pRealm->m_phood->m_dScale3d);
  903. double dHitZ = m_dZ + GetRandSway(BLOOD_SPLAT_SWAY, m_pRealm->m_phood->m_dScale3d);
  904. // Make sure blood lands on the ground (terrain).
  905. double dTerrainH = m_pRealm->GetHeight(dHitX, dHitZ);
  906. Map3Dto2D(dHitX, dTerrainH, dHitZ,
  907. &(pat->m_msg.msg_DrawBlood.s2dX),
  908. &(pat->m_msg.msg_DrawBlood.s2dY) );
  909. // NOTE: sAngle is currently not utilized.
  910. pat->Setup(dHitX, dTerrainH, dHitZ);
  911. }
  912. }
  913. ////////////////////////////////////////////////////////////////////////////////
  914. // Draws last frame of blood pool into background.
  915. ////////////////////////////////////////////////////////////////////////////////
  916. void CCharacter::BloodToBackground(
  917. short sAnimX2d, // Position of animation in 2d.
  918. short sAnimY2d) // Position of animation in 2d.
  919. {
  920. // Draw last frame of pool directly into background.
  921. CAnimThing::ChannelAA* paachannel;
  922. if (rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(BLOOD_POOL_RES_NAME), &paachannel) == 0)
  923. {
  924. // Get last frame.
  925. CAlphaAnim* paa = paachannel->GetItem(paachannel->NumItems() - 1);
  926. ASSERT(paa != NULL);
  927. short sX = sAnimX2d + paa->m_sX;
  928. short sY = sAnimY2d + paa->m_sY;
  929. // Note this does not handle alpha case yet.
  930. if (paa->m_pimAlphaArray != NULL)
  931. {
  932. RRect rcClip(0, 0,
  933. m_pRealm->m_phood->m_pimBackground->m_sWidth,
  934. m_pRealm->m_phood->m_pimBackground->m_sHeight);
  935. // Do the Alpha blit.
  936. rspGeneralAlphaBlit(
  937. m_pRealm->m_phood->m_pmaTransparency, // Levels multialpha table.
  938. paa->m_pimAlphaArray, // Alpha mask.
  939. &(paa->m_imColor), // Src.
  940. m_pRealm->m_phood->m_pimBackground, // Dst.
  941. sX, // 2D Dst coord.
  942. sY, // 2D Dst coord.
  943. rcClip); // Dst.
  944. }
  945. else
  946. {
  947. // Regular transparency blit.
  948. rspBlit(
  949. &(paa->m_imColor), // Src.
  950. m_pRealm->m_phood->m_pimBackground, // Dst.
  951. sX, // 2D Dst coord.
  952. sY, // 2D Dst coord.
  953. NULL); // Dst.
  954. }
  955. rspReleaseResource(&g_resmgrGame, &paachannel);
  956. }
  957. }
  958. ////////////////////////////////////////////////////////////////////////////////
  959. // Prepare current weapon (ammo).
  960. // This should be done when the character starts its shoot animation.
  961. // (virtual).
  962. ////////////////////////////////////////////////////////////////////////////////
  963. CWeapon* CCharacter::PrepareWeapon(void) // Returns the weapon ptr or NULL.
  964. {
  965. CWeapon* pweapon = NULL;
  966. switch (m_eWeaponType)
  967. {
  968. case CPistolID:
  969. case CMachineGunID:
  970. case CShotGunID:
  971. case CAssaultWeaponID:
  972. case CDoubleBarrelID:
  973. case CUziID:
  974. case CAutoRifleID:
  975. case CSmallPistolID:
  976. break;
  977. default:
  978. if (ConstructWithID(m_eWeaponType, m_pRealm, (CThing**) &pweapon) == 0)
  979. {
  980. // Set its parent.
  981. pweapon->m_idParent = GetInstanceID();
  982. // Set it up.
  983. pweapon->Setup(0, 0, 0);
  984. pweapon->m_dRot = m_dRot;
  985. // Set its initial state to hidden.
  986. pweapon->m_eState = CWeapon::State_Hide;
  987. // Let the scene know to render the weapon as a child of this.
  988. m_sprite.AddChild(pweapon->GetSprite() );
  989. // Store ID.
  990. m_u16IdWeapon = pweapon->GetInstanceID();
  991. }
  992. else
  993. {
  994. TRACE("PrepareWeapon(): Failed to construct new %s.\n",
  995. ms_aClassInfo[m_eWeaponType].pszClassName);
  996. }
  997. break;
  998. }
  999. return pweapon;
  1000. }
  1001. ////////////////////////////////////////////////////////////////////////////////
  1002. // Shoot current weapon.
  1003. // This should be done when the character releases the weapon it's
  1004. // shooting.
  1005. // (virtual).
  1006. ////////////////////////////////////////////////////////////////////////////////
  1007. CWeapon* CCharacter::ShootWeapon( // Returns the weapon ptr or NULL
  1008. CSmash::Bits bitsInclude, // Bits to use for bullet collision (enemies can specify different bits)
  1009. CSmash::Bits bitsDontcare, // Bits to use for bullet colllsion
  1010. CSmash::Bits bitsExclude) // Bits to use for bullet collision
  1011. {
  1012. CWeapon* pweapon = NULL;
  1013. // Detatch the weapon.
  1014. ASSERT(m_panimCur != NULL);
  1015. // Get weapon's position relative to this character.
  1016. double dWeaponRelX, dWeaponRelY, dWeaponRelZ;
  1017. GetLinkPoint(
  1018. m_panimCur->m_ptransRigid->GetAtTime(m_lAnimTime),
  1019. &dWeaponRelX,
  1020. &dWeaponRelY,
  1021. &dWeaponRelZ);
  1022. RP3d pt3WeaponRel = { static_cast<float>(dWeaponRelX), static_cast<float>(dWeaponRelY), static_cast<float>(dWeaponRelZ), 1 };
  1023. switch (m_eWeaponType)
  1024. {
  1025. case CPistolID:
  1026. case CMachineGunID:
  1027. case CUziID:
  1028. case CAutoRifleID:
  1029. case CSmallPistolID:
  1030. FireBullets(&pt3WeaponRel, 1, MAX_BULLET_RANGE, g_smidBulletFire, bitsInclude, bitsDontcare, bitsExclude);
  1031. break;
  1032. case CShotGunID:
  1033. FireBullets(&pt3WeaponRel, NUM_SHOTS_PER_SHELL, MAX_SHELL_RANGE, g_smidShotgun, bitsInclude, bitsDontcare, bitsExclude);
  1034. break;
  1035. case CAssaultWeaponID:
  1036. FireBullets(&pt3WeaponRel, NUM_SHOTS_PER_SHELL, MAX_SHELL_RANGE, g_smidSprayCannon, bitsInclude, bitsDontcare, bitsExclude);
  1037. break;
  1038. case CDoubleBarrelID:
  1039. FireBullets(&pt3WeaponRel, 2 * NUM_SHOTS_PER_SHELL, MAX_SHELL_RANGE, g_smidDeathWadLaunch, bitsInclude, bitsDontcare, bitsExclude);
  1040. // Double it.
  1041. PlaySample(g_smidShotgun, SampleMaster::Weapon);
  1042. break;
  1043. case CFirestreamID:
  1044. // If there's no sound going on already . . .
  1045. if (m_siLastWeaponPlayInstance == 0)
  1046. {
  1047. PlaySample( // Returns nothing.
  1048. // Does not fail.
  1049. g_smidFlameThrower3, // In: Identifier of sample you want played.
  1050. SampleMaster::Destruction, // In: Sound Volume Category for user adjustment
  1051. 255, // In: Initial Sound Volume (0 - 255)
  1052. &m_siLastWeaponPlayInstance, // Out: Handle for adjusting sound volume
  1053. NULL, // Out: Sample duration in ms, if not NULL.
  1054. 250, // In: Where to loop back to in milliseconds.
  1055. // -1 indicates no looping (unless m_sLoop is
  1056. // explicitly set).
  1057. 750, // In: Where to loop back from in milliseconds.
  1058. // In: If less than 1, the end + lLoopEndTime is used.
  1059. false); // In: Call ReleaseAndPurge rather than Release after playing
  1060. }
  1061. m_lStopLoopingWeaponSoundTime = m_pRealm->m_time.GetGameTime() + MAX_TIME_WITHOUT_FLAMAGE;
  1062. // Intentional fall through.
  1063. default:
  1064. {
  1065. ASSERT(m_u16IdWeapon != CIdBank::IdNil);
  1066. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pweapon, m_u16IdWeapon) == 0)
  1067. {
  1068. // Set weapon position to character's position offset by rigid body's realm offset.
  1069. pweapon->m_dX = m_dX + pt3WeaponRel.x;
  1070. pweapon->m_dY = m_dY + pt3WeaponRel.y;
  1071. pweapon->m_dZ = m_dZ + pt3WeaponRel.z;
  1072. // I guess we usually want to launch in the direction we are _currently_ facing and
  1073. // not the direction we were in when we prepared the weapon.
  1074. pweapon->m_dRot = m_dRot;
  1075. // Set the collision bits for the weapon
  1076. pweapon->SetCollideBits(bitsInclude, bitsDontcare, bitsExclude);
  1077. // Set Instance ID of the shooter
  1078. pweapon->m_u16ShooterID = m_u16InstanceId;
  1079. // Set the weapon in motion by changing its state.
  1080. pweapon->m_eState = CWeapon::State_Fire;
  1081. // Detach parent pointer
  1082. pweapon->m_idParent = CIdBank::IdNil;
  1083. // Detatch weapon's sprite
  1084. CSprite* pspriteWeapon = pweapon->GetSprite();
  1085. if (pspriteWeapon)
  1086. {
  1087. // Some weapons (one weapon, the flamer) are never parented.
  1088. if (pspriteWeapon->m_psprParent)
  1089. {
  1090. m_sprite.RemoveChild(pspriteWeapon);
  1091. }
  1092. }
  1093. // Done with weapon.
  1094. m_u16IdWeapon = CIdBank::IdNil;
  1095. // Specific behavior by type.
  1096. switch (pweapon->GetClassID() )
  1097. {
  1098. case CDeathWadID:
  1099. CDeathWad* pdw = (CDeathWad*)pweapon;
  1100. // Let it take what it needs.
  1101. pdw->FeedWad(&m_stockpile);
  1102. break;
  1103. }
  1104. }
  1105. break;
  1106. }
  1107. }
  1108. // No longer exists.
  1109. m_u16IdWeapon = CIdBank::IdNil;
  1110. return pweapon;
  1111. }
  1112. ////////////////////////////////////////////////////////////////////////////////
  1113. // Validate weapon position. If invalid, the weapon is destroyed and
  1114. // the notification function, OnWeaponDestroyed() is called.
  1115. ////////////////////////////////////////////////////////////////////////////////
  1116. bool CCharacter::ValidateWeaponPosition(void) // Returns true, if weapon is in a valid position.
  1117. // Returns false, if weapon destroyed because it
  1118. // it is not in a valid position.
  1119. {
  1120. bool bValid = true; // Assume valid.
  1121. if (m_u16IdWeapon != CIdBank::IdNil)
  1122. {
  1123. switch (m_eWeaponType)
  1124. {
  1125. case CGrenadeID:
  1126. case CFirebombID:
  1127. case CNapalmID:
  1128. case CRocketID:
  1129. case CHeatseekerID:
  1130. {
  1131. CWeapon* pweapon;
  1132. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pweapon, m_u16IdWeapon) == 0)
  1133. {
  1134. // If this weapon is not hidden . . .
  1135. if (pweapon->m_eState != CWeapon::State_Hide)
  1136. {
  1137. RTransform* ptransWeapon = m_panimCur->m_ptransRigid->GetAtTime(m_lAnimTime);
  1138. // Get the position to check for terrain obstacles.
  1139. // Remember this is an offset from our hotspot (origin).
  1140. double dX, dY, dZ;
  1141. GetLinkPoint(ptransWeapon, &dX, &dY, &dZ);
  1142. // Check position to make sure it's not inside terrain.
  1143. short sTerrainH = m_pRealm->GetHeight(m_dX + dX, m_dZ + dZ);
  1144. if (sTerrainH > m_dY + dY)
  1145. {
  1146. // Send the object a delete message.
  1147. GameMessage msg;
  1148. msg.msg_ObjectDelete.eType = typeObjectDelete;
  1149. msg.msg_ObjectDelete.sPriority = 0;
  1150. SendThingMessage(&msg, pweapon);
  1151. // Clear our instance ID.
  1152. m_u16IdWeapon = CIdBank::IdNil;
  1153. // Call the notify function.
  1154. OnWeaponDestroyed();
  1155. bValid = false;
  1156. }
  1157. }
  1158. }
  1159. else
  1160. {
  1161. // Clear our instance ID.
  1162. m_u16IdWeapon = CIdBank::IdNil;
  1163. }
  1164. break;
  1165. }
  1166. default:
  1167. break;
  1168. }
  1169. }
  1170. return bValid;
  1171. }
  1172. ////////////////////////////////////////////////////////////////////////////////
  1173. // Fire some bullets.
  1174. ////////////////////////////////////////////////////////////////////////////////
  1175. bool CCharacter::FireBullets( // Returns true, if we hit someone/thing.
  1176. RP3d* ppt3d, // In: Launch pt in Postal units.
  1177. short sNumShots, // In: Number of shots to fire.
  1178. short sRange, // In: Bullet range.
  1179. SampleMasterID smidAmmo, // In: Ammo noise.
  1180. CSmash::Bits bitsInclude,/*=0*/ // In: Optional bits we can hit
  1181. CSmash::Bits bitsDontcare, // In: Optional bits not involved in collision
  1182. CSmash::Bits bitsExclude) // In: Optional bits excluded from collision
  1183. {
  1184. bool bHit = false; // Assume no hit.
  1185. if (bitsInclude == 0)
  1186. {
  1187. bitsInclude = CSmash::Character | CSmash::Barrel | CSmash::Mine;
  1188. }
  1189. // Never shoot dead people or writhers.
  1190. bitsExclude |= (CSmash::Dead | CSmash::AlmostDead);
  1191. const bool isPlayer = (m_id == CDudeID); // !!! FIXME: make sure it's not a remote multiplayer dude.
  1192. if (isPlayer)
  1193. {
  1194. if (StatsAreAllowed)
  1195. {
  1196. Stat_BulletsFired += sNumShots;
  1197. if (Stat_BulletsFired >= 1000000)
  1198. UnlockAchievement(ACHIEVEMENT_FIRE_1000000_BULLETS);
  1199. }
  1200. }
  1201. short i;
  1202. for (i = 0; i < sNumShots; i ++)
  1203. {
  1204. short sX, sY, sZ;
  1205. short sFireAngle = m_dRot + GetRandSway(FIRE_ANGLE_Y_SWAY, m_pRealm->m_phood->m_dScale3d);
  1206. CThing* pthingTarget;
  1207. bool bResult = m_bullets.FireDeluxe(
  1208. sFireAngle, // In: Angle of launch in degrees (on X/Z plane).
  1209. GetRandSway(FIRE_ANGLE_Z_SWAY, m_pRealm->m_phood->m_dScale3d), // In: Angle of launch in degrees (on X/Y plane).
  1210. m_dX + ppt3d->x, // In: Launch position.
  1211. m_dY + ppt3d->y, // In: Launch position.
  1212. m_dZ + ppt3d->z, // In: Launch position.
  1213. sRange, // In: Maximum distance.
  1214. m_pRealm, // In: Realm in which to fire.
  1215. bitsInclude, // In: Mask of CSmash masks that this bullet can hit.
  1216. bitsDontcare, // In: Mask of CSmash masks that this bullet does not care to hit.
  1217. bitsExclude, // In: Mask of CSmash masks that this bullet cannot hit.
  1218. 20, // In: Maximum angle with terrain that can cause
  1219. // a ricochet (on X/Z plane).
  1220. 2, // In: The maximum number of ricochets.
  1221. &sX, // Out: Terrain hit position (i.e., where the bullet
  1222. // would stop if no CThing collisions occurred).
  1223. &sY, // Out: Terrain hit position (i.e., where the bullet
  1224. // would stop if no CThing collisions occurred).
  1225. &sZ, // Out: Terrain hit position (i.e., where the bullet
  1226. // would stop if no CThing collisions occurred).
  1227. &pthingTarget, // Out: Ptr to thing hit or NULL.
  1228. true, // In: Draw a tracer at random point along path.
  1229. (i == 0) ? smidAmmo : g_smidNil) // In: Use ammo sample.
  1230. ;
  1231. //!! FIXME: this should simply miss if we shoot ourself. Cheap hack for dude shooting himself in twinstick mode.
  1232. if (pthingTarget != this && bResult)
  1233. {
  1234. // Create a message.
  1235. // Note that right here we can do the tuning on the amount of bullet
  1236. // damage based on m_eWeaponType and the pthingTarget->m_id == CDudeID and
  1237. // this->m_id == CDudeID.
  1238. GameMessage msg;
  1239. msg.msg_Shot.eType = typeShot;
  1240. msg.msg_Shot.sPriority = 0;
  1241. msg.msg_Shot.sAngle = sFireAngle;
  1242. msg.msg_Shot.u16ShooterID = m_u16InstanceId;
  1243. short sIndex = 0;
  1244. // Figure out how much damage the bullet should give
  1245. switch (m_eWeaponType)
  1246. {
  1247. case CSmallPistolID:
  1248. case CPistolID:
  1249. sIndex = WEAPON_IS_PISTOL;
  1250. break;
  1251. case CUziID:
  1252. case CAutoRifleID:
  1253. case CMachineGunID:
  1254. sIndex = WEAPON_IS_MACHINEGUN;
  1255. break;
  1256. case CShotGunID:
  1257. sIndex = WEAPON_IS_SHOTGUN;
  1258. break;
  1259. case CAssaultWeaponID:
  1260. sIndex = WEAPON_IS_ASSAULT;
  1261. break;
  1262. case CDoubleBarrelID:
  1263. sIndex = WEAPON_IS_SHOTGUN;
  1264. // There's additional damage via other means.
  1265. break;
  1266. }
  1267. // See if the shooter is a CDude
  1268. if (m_id == CDudeID)
  1269. sIndex |= SHOOTER_IS_DUDE;
  1270. // See if the target is a CDude
  1271. if (pthingTarget->GetClassID() == CDudeID)
  1272. sIndex |= TARGET_IS_DUDE;
  1273. // Based on these parameters, look up the damage in the damage chart.
  1274. msg.msg_Shot.sDamage = ms_asBulletDamageChart[sIndex];
  1275. // Send it the message.
  1276. SendThingMessage(&msg, pthingTarget);
  1277. if (m_eWeaponType == CDoubleBarrelID)
  1278. {
  1279. // This is gonna hurt.
  1280. msg.msg_Explosion.eType = typeExplosion;
  1281. msg.msg_Explosion.sPriority = 0;
  1282. // Spread this out.
  1283. msg.msg_Explosion.sDamage = ms_asBulletDamageChart[sIndex] / sNumShots + 1;
  1284. // Just in front of where bullet hit.
  1285. short sAngle = rspMod360(m_dRot);
  1286. msg.msg_Explosion.sX = (short) pthingTarget->GetX() - COSQ[sAngle] * 10;
  1287. msg.msg_Explosion.sY = (short) pthingTarget->GetY();
  1288. msg.msg_Explosion.sZ = (short) pthingTarget->GetZ() + SINQ[sAngle] * 10;
  1289. msg.msg_Explosion.sVelocity = 100;
  1290. msg.msg_Explosion.u16ShooterID = GetInstanceID();
  1291. SendThingMessage(&msg, pthingTarget);
  1292. }
  1293. // Note we hit something.
  1294. bHit = true;
  1295. if ((isPlayer) && (StatsAreAllowed))
  1296. {
  1297. Stat_BulletsHit++;
  1298. if (Stat_BulletsHit >= 100000)
  1299. UnlockAchievement(ACHIEVEMENT_HIT_100000_TARGETS);
  1300. }
  1301. }
  1302. else
  1303. {
  1304. if ((isPlayer) && (StatsAreAllowed)) Stat_BulletsMissed++;
  1305. }
  1306. }
  1307. // Create shells/casings . . .
  1308. CChunk* pchunk = NULL; // Initialized for safety.
  1309. // Note that this will fail if particles are disabled.
  1310. if (Construct(CChunkID, m_pRealm, (CThing**)&pchunk) == 0)
  1311. {
  1312. pchunk->Setup(
  1313. m_dX + ppt3d->x, // Source position.
  1314. m_dY + ppt3d->y, // Source position.
  1315. m_dZ + ppt3d->z, // Source position.
  1316. m_dRot + 90, // Angle of velocity.
  1317. 0, // Angle sway.
  1318. 30, // Velocity (X/Z plane).
  1319. 20, // Velocity sway.
  1320. 37, // Velocity (Vertical).
  1321. 25, // Velocity (Vertical) sway.
  1322. (sNumShots > 1) ? CChunk::Shell : CChunk::BulletCasing); // In: Type of chunk.
  1323. }
  1324. return bHit;
  1325. }
  1326. ////////////////////////////////////////////////////////////////////////////////
  1327. // Determine if a path is clear of items identified by the specified
  1328. // smash bits and terrain.
  1329. ////////////////////////////////////////////////////////////////////////////////
  1330. bool CCharacter::IsPathClear( // Returns true, if the entire path is clear.
  1331. // Returns false, if only a portion of the path is clear.
  1332. // (see *psX, *psY, *psZ, *ppthing).
  1333. short sX, // In: Starting X.
  1334. short sY, // In: Starting Y.
  1335. short sZ, // In: Starting Z.
  1336. short sRotY, // In: Rotation around y axis (direction on X/Z plane).
  1337. short sCrawlRate, // In: Rate at which to scan ('crawl') path in pixels per
  1338. // iteration.
  1339. // NOTE: We scan terrain using GetFloorAttributes()
  1340. // so small values of sCrawl are not necessary.
  1341. // NOTE: We could change this to a speed in pixels per second
  1342. // where we'd assume a certain frame rate.
  1343. short sRangeXZ, // In: Range on X/Z plane.
  1344. short sRadius, // In: Radius of path traverser.
  1345. short sVerticalTolerance, // In: Max traverser can step up.
  1346. CSmash::Bits bitsInclude, // In: Mask of CSmash bits that would terminate path.
  1347. CSmash::Bits bitsDontCare, // In: Mask of CSmash bits that would not affect path.
  1348. CSmash::Bits bitsExclude, // In: Mask of CSmash bits that cannot affect path.
  1349. short* psX, // Out: Point of intercept, if any, on path.
  1350. short* psY, // Out: Point of intercept, if any, on path.
  1351. short* psZ, // Out: Point of intercept, if any, on path.
  1352. CThing** ppthing, // Out: Thing that intercepted us or NULL, if none.
  1353. CSmash* psmashExclude/*= NULL*/) // In: Optional CSmash to exclude or NULL, if none.
  1354. {
  1355. bool bEntirelyClear = false; // Assume entire path is not clear.
  1356. /////////// Determine length of travel if no CThings hit ///////////////////
  1357. // Get most efficient increments that won't miss any attributes.
  1358. // For the rates we use trig with a hypotenuse of 1 which will give
  1359. // us a rate <= 1.0 and then multiply by the the crawl for
  1360. // a reasonable increase in the speed of this alg.
  1361. // sAngle must be between 0 and 359.
  1362. sRotY = rspMod360(sRotY);
  1363. float fRateX = COSQ[sRotY] * sCrawlRate;
  1364. float fRateZ = -SINQ[sRotY] * sCrawlRate;
  1365. float fRateY = 0.0; // If we ever want vertical movement . . .
  1366. // Set initial position to first point to check (NEVER checks original position).
  1367. float fPosX = sX + fRateX;
  1368. float fPosY = sY + fRateY;
  1369. float fPosZ = sZ + fRateZ;
  1370. // Determine amount traveled per iteration on X/Z plane just once.
  1371. float fIterDistXZ = sqrt(ABS2(fRateX, fRateZ) );
  1372. float fTotalDistXZ = 0.0F;
  1373. // Store extents.
  1374. short sMaxX = m_pRealm->GetRealmWidth();
  1375. short sMaxY = 512; // Robustness: Guard against infinite loop
  1376. // in the remote possibility that we shoot
  1377. // straight up (currently one can only shoot
  1378. // horizontally).
  1379. short sMaxZ = m_pRealm->GetRealmHeight();
  1380. short sMinX = 0;
  1381. short sMinY = -512;
  1382. short sMinZ = 0;
  1383. short sCurH;
  1384. U16 u16Attribute;
  1385. // Scan while in realm.
  1386. while (
  1387. fPosX > sMinX
  1388. && fPosY > sMinY
  1389. && fPosZ > sMinZ
  1390. && fPosX < sMaxX
  1391. && fPosY < sMaxY
  1392. && fPosZ < sMaxZ
  1393. && fTotalDistXZ < sRangeXZ)
  1394. {
  1395. GetFloorAttributes((short)fPosX, (short)fPosZ, &u16Attribute, &sCurH);
  1396. // If too big a height difference or completely not walkable . . .
  1397. if ( (u16Attribute & REALM_ATTR_NOT_WALKABLE)
  1398. || (sCurH - fPosY > sVerticalTolerance) )
  1399. {
  1400. break;
  1401. }
  1402. // Update position.
  1403. fPosX += fRateX;
  1404. fPosY = sCurH;
  1405. fPosZ += fRateZ;
  1406. // Update distance travelled on X/Z plane.
  1407. fTotalDistXZ += fIterDistXZ;
  1408. }
  1409. // Check 3D line segments outlining path.
  1410. R3DLine line1, line2;
  1411. if (fRateX > 0.0F)
  1412. {
  1413. line1.X1 = sX - sRadius;
  1414. line1.X2 = fPosX - sRadius;
  1415. line2.X1 = sX - sRadius;
  1416. line2.X2 = fPosX - sRadius;
  1417. }
  1418. else
  1419. {
  1420. line1.X1 = sX + sRadius;
  1421. line1.X2 = fPosX + sRadius;
  1422. line2.X1 = sX + sRadius;
  1423. line2.X2 = fPosX + sRadius;
  1424. }
  1425. if (fRateY > 0.0F)
  1426. {
  1427. line1.Y1 = sY - sRadius;
  1428. line1.Y2 = fPosY - sRadius;
  1429. line2.Y1 = sY - sRadius;
  1430. line2.Y2 = fPosY - sRadius;
  1431. }
  1432. else
  1433. {
  1434. line1.Y1 = sY + sRadius;
  1435. line1.Y2 = fPosY + sRadius;
  1436. line2.Y1 = sY + sRadius;
  1437. line2.Y2 = fPosY + sRadius;
  1438. }
  1439. if (fRateZ > 0.0F)
  1440. {
  1441. line1.Z1 = sZ - sRadius;
  1442. line1.Z2 = fPosZ - sRadius;
  1443. line2.Z1 = sZ - sRadius;
  1444. line2.Z2 = fPosZ - sRadius;
  1445. }
  1446. else
  1447. {
  1448. line1.Z1 = sZ + sRadius;
  1449. line1.Z2 = fPosZ + sRadius;
  1450. line2.Z1 = sZ + sRadius;
  1451. line2.Z2 = fPosZ + sRadius;
  1452. }
  1453. CSmash* psmashClosest = NULL;
  1454. // Determine if anything with specified smash description was hit on along each edge . . .
  1455. CSmash* psmash1;
  1456. if (m_pRealm->m_smashatorium.QuickCheckClosest(
  1457. &line1,
  1458. bitsInclude,
  1459. bitsDontCare,
  1460. bitsExclude,
  1461. &psmash1,
  1462. psmashExclude) == false)
  1463. {
  1464. psmash1 = NULL;
  1465. }
  1466. CSmash* psmash2;
  1467. if (m_pRealm->m_smashatorium.QuickCheckClosest(
  1468. &line2,
  1469. bitsInclude,
  1470. bitsDontCare,
  1471. bitsExclude,
  1472. &psmash2,
  1473. psmashExclude) == false)
  1474. {
  1475. psmash2 = NULL;
  1476. }
  1477. // If two smashes found . . .
  1478. if (psmash1 != NULL && psmash2 != NULL && psmash1 != psmash2)
  1479. {
  1480. // Determine closer on X/Z plane.
  1481. if ( ABS2(psmash1->m_sphere.sphere.X, psmash1->m_sphere.sphere.Y)
  1482. < ABS2(psmash2->m_sphere.sphere.X, psmash2->m_sphere.sphere.Y) )
  1483. {
  1484. psmashClosest = psmash1;
  1485. }
  1486. else
  1487. {
  1488. psmashClosest = psmash2;
  1489. }
  1490. }
  1491. else
  1492. {
  1493. // Whichever is not NULL.
  1494. if (psmash1 != NULL)
  1495. {
  1496. psmashClosest = psmash1;
  1497. }
  1498. else
  1499. {
  1500. psmashClosest = psmash2;
  1501. }
  1502. }
  1503. // If anything hit . . .
  1504. if (psmashClosest != NULL)
  1505. {
  1506. // Set *ppthing to thing hit.
  1507. *ppthing = psmashClosest->m_pThing;
  1508. // Set end pt.
  1509. *psX = psmashClosest->m_sphere.sphere.X;
  1510. *psY = psmashClosest->m_sphere.sphere.Y;
  1511. *psZ = psmashClosest->m_sphere.sphere.Z;
  1512. }
  1513. else
  1514. {
  1515. // Clear thing ptr.
  1516. *ppthing = NULL;
  1517. // Set end pt.
  1518. *psX = fPosX;
  1519. *psY = fPosY;
  1520. *psZ = fPosZ;
  1521. // If we made it the whole way . . .
  1522. if (fTotalDistXZ >= sRangeXZ)
  1523. {
  1524. bEntirelyClear = true;
  1525. }
  1526. }
  1527. #if 0
  1528. // FEEDBACK.
  1529. // Create a line sprite.
  1530. CSpriteLine2d* psl2d = new CSpriteLine2d;
  1531. if (psl2d != NULL)
  1532. {
  1533. Map3Dto2D(
  1534. sX,
  1535. sY,
  1536. sZ,
  1537. &(psl2d->m_sX2),
  1538. &(psl2d->m_sY2) );
  1539. Map3Dto2D(
  1540. *psX,
  1541. *psY,
  1542. *psZ,
  1543. &(psl2d->m_sX2End),
  1544. &(psl2d->m_sY2End) );
  1545. psl2d->m_sPriority = sZ;
  1546. psl2d->m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer(sX, sZ));
  1547. psl2d->m_u8Color = (bEntirelyClear == false) ? 249 : 250;
  1548. // Destroy when done.
  1549. psl2d->m_sInFlags = CSprite::InDeleteOnRender;
  1550. // Put 'er there.
  1551. m_pRealm->m_scene.UpdateSprite(psl2d);
  1552. }
  1553. #endif
  1554. return bEntirelyClear;
  1555. }
  1556. ////////////////////////////////////////////////////////////////////////////////
  1557. // Light up target if there is one within a cone in the given aiming direction.
  1558. ////////////////////////////////////////////////////////////////////////////////
  1559. bool CCharacter::IlluminateTarget( // Returns true if there is a target
  1560. short sX, // In: Starting x position
  1561. short sY, // In: Starting y position
  1562. short sZ, // In: Starting z position
  1563. short sRotY, // In: Aiming direction (rotation around y axis)
  1564. short sRangeXZ, // In: Range on X/Z plane
  1565. short sRadius, // In: Radius of path traverser.
  1566. CSmash::Bits bitsInclude, // In: Mask of CSmash bits that would count as a hit
  1567. CSmash::Bits bitsDontCare, // In: Mask of CSmash bits that would not affect path
  1568. CSmash::Bits bitsExclude, // In: Mask of CSmash bits that cannot affect path
  1569. CThing** hThing, // Out: Handle to thing that is the Target or NULL if none
  1570. CSmash* psmashExclude) // In: Optional CSmash to exclude or NULL, if none.
  1571. {
  1572. bool bTargetFound = false;
  1573. // sAngle must be between 0 and 359.
  1574. sRotY = rspMod360(sRotY);
  1575. float fRateX = COSQ[sRotY] * sRangeXZ;
  1576. float fRateZ = -SINQ[sRotY] * sRangeXZ;
  1577. float fRateY = 0.0; // If we ever want vertical movement . . .
  1578. // Set initial position to first point to check (NEVER checks original position).
  1579. float fPosX = sX + fRateX;
  1580. float fPosY = sY + fRateY;
  1581. float fPosZ = sZ + fRateZ;
  1582. // Check 3D line segments outlining path.
  1583. R3DLine line1, line2;
  1584. if (fRateX > 0.0F)
  1585. {
  1586. line1.X1 = sX - sRadius;
  1587. line1.X2 = fPosX - sRadius;
  1588. line2.X1 = sX - sRadius;
  1589. line2.X2 = fPosX - sRadius;
  1590. }
  1591. else
  1592. {
  1593. line1.X1 = sX + sRadius;
  1594. line1.X2 = fPosX + sRadius;
  1595. line2.X1 = sX + sRadius;
  1596. line2.X2 = fPosX + sRadius;
  1597. }
  1598. if (fRateY > 0.0F)
  1599. {
  1600. line1.Y1 = sY - sRadius;
  1601. line1.Y2 = fPosY - sRadius;
  1602. line2.Y1 = sY - sRadius;
  1603. line2.Y2 = fPosY - sRadius;
  1604. }
  1605. else
  1606. {
  1607. line1.Y1 = sY + sRadius;
  1608. line1.Y2 = fPosY + sRadius;
  1609. line2.Y1 = sY + sRadius;
  1610. line2.Y2 = fPosY + sRadius;
  1611. }
  1612. if (fRateZ > 0.0F)
  1613. {
  1614. line1.Z1 = sZ - sRadius;
  1615. line1.Z2 = fPosZ - sRadius;
  1616. line2.Z1 = sZ - sRadius;
  1617. line2.Z2 = fPosZ - sRadius;
  1618. }
  1619. else
  1620. {
  1621. line1.Z1 = sZ + sRadius;
  1622. line1.Z2 = fPosZ + sRadius;
  1623. line2.Z1 = sZ + sRadius;
  1624. line2.Z2 = fPosZ + sRadius;
  1625. }
  1626. CSmash* psmashClosest = NULL;
  1627. // Determine if anything with specified smash description was hit on along each edge . . .
  1628. CSmash* psmash1;
  1629. if (m_pRealm->m_smashatorium.QuickCheckClosest(
  1630. &line1,
  1631. bitsInclude,
  1632. bitsDontCare,
  1633. bitsExclude,
  1634. &psmash1,
  1635. psmashExclude) == false)
  1636. {
  1637. psmash1 = NULL;
  1638. }
  1639. CSmash* psmash2;
  1640. if (m_pRealm->m_smashatorium.QuickCheckClosest(
  1641. &line2,
  1642. bitsInclude,
  1643. bitsDontCare,
  1644. bitsExclude,
  1645. &psmash2,
  1646. psmashExclude) == false)
  1647. {
  1648. psmash2 = NULL;
  1649. }
  1650. // If two smashes found . . .
  1651. if (psmash1 != NULL && psmash2 != NULL && psmash1 != psmash2)
  1652. {
  1653. // Determine closer on X/Z plane.
  1654. if ( ABS2(psmash1->m_sphere.sphere.X, psmash1->m_sphere.sphere.Y)
  1655. < ABS2(psmash2->m_sphere.sphere.X, psmash2->m_sphere.sphere.Y) )
  1656. {
  1657. psmashClosest = psmash1;
  1658. }
  1659. else
  1660. {
  1661. psmashClosest = psmash2;
  1662. }
  1663. }
  1664. else
  1665. {
  1666. // Whichever is not NULL.
  1667. if (psmash1 != NULL)
  1668. {
  1669. psmashClosest = psmash1;
  1670. }
  1671. else
  1672. {
  1673. psmashClosest = psmash2;
  1674. }
  1675. }
  1676. // If anything hit . . .
  1677. if (psmashClosest != NULL)
  1678. {
  1679. // Set *ppthing to thing hit.
  1680. *hThing = psmashClosest->m_pThing;
  1681. bTargetFound = true;
  1682. // Set end pt.
  1683. // *psX = psmashClosest->m_sphere.sphere.X;
  1684. // *psY = psmashClosest->m_sphere.sphere.Y;
  1685. // *psZ = psmashClosest->m_sphere.sphere.Z;
  1686. }
  1687. else
  1688. {
  1689. // Clear thing ptr.
  1690. *hThing = NULL;
  1691. // Set end pt.
  1692. // *psX = fPosX;
  1693. // *psY = fPosY;
  1694. // *psZ = fPosZ;
  1695. }
  1696. #if 0
  1697. // FEEDBACK.
  1698. // Create a line sprite.
  1699. CSpriteLine2d* psl2d = new CSpriteLine2d;
  1700. if (psl2d != NULL)
  1701. {
  1702. Map3Dto2D(
  1703. sX,
  1704. sY,
  1705. sZ,
  1706. &(psl2d->m_sX2),
  1707. &(psl2d->m_sY2) );
  1708. Map3Dto2D(
  1709. *psX,
  1710. *psY,
  1711. *psZ,
  1712. &(psl2d->m_sX2End),
  1713. &(psl2d->m_sY2End) );
  1714. psl2d->m_sPriority = sZ;
  1715. psl2d->m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer(sX, sZ));
  1716. psl2d->m_u8Color = (bEntirelyClear == false) ? 249 : 250;
  1717. // Destroy when done.
  1718. psl2d->m_sInFlags = CSprite::InDeleteOnRender;
  1719. // Put 'er there.
  1720. m_pRealm->m_scene.UpdateSprite(psl2d);
  1721. }
  1722. #endif
  1723. return bTargetFound;
  1724. }
  1725. ////////////////////////////////////////////////////////////////////////////////
  1726. // Preload - cache the anims that may be used.
  1727. // (static).
  1728. ////////////////////////////////////////////////////////////////////////////////
  1729. short CCharacter::Preload(
  1730. CRealm* prealm) // In: Calling realm.
  1731. {
  1732. CAnimThing::ChannelAA* paaCache;
  1733. short sResult = 0;
  1734. if (rspGetResource(&g_resmgrGame, prealm->Make2dResPath(BLOOD_SPLAT_RES_NAME), &paaCache) == 0)
  1735. rspReleaseResource(&g_resmgrGame, &paaCache);
  1736. else
  1737. sResult |= 1;
  1738. if (rspGetResource(&g_resmgrGame, prealm->Make2dResPath(BLOOD_POOL_RES_NAME), &paaCache) == 0)
  1739. rspReleaseResource(&g_resmgrGame, &paaCache);
  1740. else
  1741. sResult |= 1;
  1742. // Tell samplemaster to cache (preload) these samples.
  1743. CacheSample(g_smidBulletFire);
  1744. CacheSample(g_smidShotgun);
  1745. CBulletFest::Preload(prealm);
  1746. return sResult;
  1747. }
  1748. ////////////////////////////////////////////////////////////////////////////////
  1749. // Called by destructor to clean up.
  1750. ////////////////////////////////////////////////////////////////////////////////
  1751. void CCharacter::Kill(void)
  1752. {
  1753. // If we have a weapon sound play instance . . .
  1754. if (m_siLastWeaponPlayInstance)
  1755. {
  1756. // Stop looping the sound.
  1757. StopLoopingSample(m_siLastWeaponPlayInstance);
  1758. // Forget about it.
  1759. m_siLastWeaponPlayInstance = 0;
  1760. }
  1761. }
  1762. ////////////////////////////////////////////////////////////////////////////////
  1763. // EOF
  1764. ////////////////////////////////////////////////////////////////////////////////