Thing3d.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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. // thing3d.cpp
  19. // Project: Postal
  20. //
  21. // This module implements the CThing3d class which is the class of generic
  22. // thing3d functionality for game thing3ds.
  23. //
  24. // History:
  25. //
  26. // 03/03/97 BRH,JMI Started this generic thing3d object to reduce the
  27. // amount of redundant code.
  28. //
  29. // 03/17/97 JMI Turned most of the CCharacter class into this CThing3d
  30. // class.
  31. //
  32. // 03/18/97 BRH Tuned the OnExplosionMsg to add more external velocity
  33. // on explosion. The way it was, the object were barely
  34. // reacting to an explosion.
  35. //
  36. // 03/18/97 JMI Now OnExplosionMsg, if there's an internal velocity,
  37. // sets the internal drag to the air drag.
  38. // Also, the external drag is now set to the air drag instead
  39. // of the surface drag in OnExplosionMsg.
  40. //
  41. // 03/18/97 JMI Now Render() does only the necessities, if there is a
  42. // parent. Also, added DetachChild().
  43. //
  44. // 03/19/97 JMI Added m_dExtRotVelY, m_dExtRotVelZ, and m_dRotZ.
  45. // GetNewPosition() now updates m_dRot and m_dRotZ using
  46. // velocities m_dExtRotVelY and m_dExtRotVelZ.
  47. // Also, DetachChild() now returns a pointer to the detached
  48. // child.
  49. // Render() now uses m_dRotZ through m_trans.Rz().
  50. // WhileBlownUp() now sets the m_dExtHorzDrag to 0 when it
  51. // detects a horizontal collision with terrain.
  52. //
  53. // 04/02/97 JMI Added #include for fire.h.
  54. //
  55. // 04/10/97 BRH Changed GetAttributes to two functions for the new
  56. // multi layred attribute maps. Now there is a
  57. // GetFloorAttributes and GetEffectAttributes that do
  58. // lookups on the two different attribute maps.
  59. //
  60. // 04/21/97 JMI Made MakeValidPosition() virtual.
  61. //
  62. // 04/22/97 BRH Changed default return for GetFloorAttribute to return
  63. // zero height rather than max height when off of the map.
  64. //
  65. // 05/14/97 JMI Added generic form of DetachChild(...).
  66. // Also, added generic PositionChild(...).
  67. //
  68. // 05/19/97 BRH Added StateNames array so that the names of the states
  69. // can be shown for thought balloons and refernced for
  70. // the logic tables.
  71. //
  72. // 05/20/97 BRH Added strings for the new victim states.
  73. //
  74. // 05/21/97 BRH Added ShootRun state for an alternative to the shoot
  75. // animation. They will use this state when shooting while
  76. // running.
  77. //
  78. // 05/29/97 JMI GetNewPosition() was doing Z backwards for external
  79. // forces. Fixed.
  80. // OnShotMsg() now provides momentum from the bullets.
  81. //
  82. // 05/29/97 JMI Changed m_pRealm->m_pHeightMap->GetVal() calls to
  83. // m_pRealm->GetFloorMapValue() and
  84. // m_pRealm->m_pAttrMap->GetVal() calls to
  85. // m_pRealm->GetEffectAttribute().
  86. // Removed ASSERT on m_pRealm->m_pAttribMap which no longer
  87. // exists.
  88. // Also, added a GetLayer().
  89. //
  90. // 05/30/97 JMI Added a generic function for adding a force vector to
  91. // the external force.
  92. //
  93. // 06/05/97 JMI Removed m_sHitPoints and added a CStockPile, m_stockpile,
  94. // instead.
  95. //
  96. // 06/06/97 BRH Added additional state descriptions for the new states
  97. // that had been added a while ago.
  98. //
  99. // 06/12/97 BRH Set the small fire that burns on the guy when he is
  100. // on fire to use his ID for the shooter.
  101. //
  102. // 06/13/97 JMI Added State_ObjectReleased.
  103. //
  104. // 06/15/97 JMI Now AddForceVector() makes sure there's a force before
  105. // setting the drag.
  106. //
  107. // 06/17/97 JMI Forgot to zero m_dDrag in WhileBlownUp(). Fixed.
  108. //
  109. // 06/17/97 JMI Converted all occurrences of rand() to GetRand() and
  110. // srand() to SeedRand().
  111. //
  112. // 06/25/97 BRH Added shadow sprite rendering to the Render() function.
  113. // Also added PrepareShadow function to turn on the shadow
  114. // sprite and load the default shadow resource if it doesn't
  115. // already have a resource loaded.
  116. //
  117. // 06/25/97 BRH Set the shadow sprite to hidden when the main item is
  118. // on the ground since the characters looked funny when
  119. // they were walking around. Also set the layer of the
  120. // shadow to the same layer as the main item rather than
  121. // having it check its own points for layer information.
  122. //
  123. // 06/25/97 JMI Now WhileBlownUp() sets m_bAboveTerrain appropriately.
  124. //
  125. // 06/27/97 JMI Now uses TransformPtsToRealm() in EditRect().
  126. //
  127. // 06/29/97 JMI Now EditHotSpot() merely gets the difference between the
  128. // EditRect() and the 2D mapping of the 3D Realm position.
  129. //
  130. // 06/30/97 JMI Now maps the Z to 3D when loading fileversions previous to
  131. // 24.
  132. //
  133. // 07/01/97 JMI Now GetFloorAttributes() uses MapY2DtoY3D() to map the
  134. // height into the realm coord system.
  135. //
  136. // 07/01/97 JMI Changed DetachChild() and PositionChild() to receive the
  137. // rigid body transform as a parameter rather than assume it
  138. // is m_panimCur->m_transRigid.
  139. // Also, added GetLinkPoint().
  140. //
  141. // 07/01/79 JMI Now GetFloorAttributes() uses MapAttribHeight() instead
  142. // of MapY2DtoY3D().
  143. //
  144. // 08/09/97 JMI Converted from macro MAX_STEPUP_THRESHOLD to enum
  145. // MaxStepUpThreshold macro.
  146. //
  147. // 07/09/97 JMI Now uses m_pRealm->Make2dResPath() to get the fullpath
  148. // for 2D image components.
  149. //
  150. // 07/12/97 BRH Added additional text strings for the new states.
  151. //
  152. // 07/17/97 BRH Added DelayShoot state description.
  153. //
  154. // 07/18/97 JMI Added PlaySample() that will hook existing PlaySample()
  155. // calls in CThing3d derived classes and map them to
  156. // PlaySample() and, if they don't specify a volume, it will
  157. // use this object's distance to the ear.
  158. //
  159. // 07/21/97 BRH Fixed bug in WhileShot() that caused the state to never
  160. // exit. When the ostrish started using this function,
  161. // it never got out of the shot state. Most other objects
  162. // never use this function.
  163. //
  164. // 07/21/97 JMI Now checks upper bound on m_sAlphaLevel of shadow sprite.
  165. //
  166. // 07/27/97 JMI Changed to use Z position (i.e., X/Z plane) instead of
  167. // Y2 position (i.e., viewing plane) position for draw
  168. // priority.
  169. //
  170. // 08/01/97 BRH Added description for new state AvoidFire and DangerNear
  171. //
  172. // 08/02/97 BRH Added virtual OnHelpMsg function and added the case to
  173. // ProcessMessage.
  174. //
  175. // 08/06/97 JRD Added local scaling to render process
  176. //
  177. // 08/08/97 BRH Added march state description.
  178. //
  179. // 08/11/97 BRH Added Walk next state description.
  180. //
  181. // 08/18/97 JMI Moved StartAnim() from CDude to CThing3d so more things
  182. // could use it.
  183. //
  184. // 08/18/97 JMI Added m_sLayerOverride which directs Render() to use the
  185. // specified layer rather than the one based on the
  186. // attributes.
  187. //
  188. // 08/24/97 JMI Changed ms_apt3dAttribCheck to ms_apt2dAttribCheckMedium[]
  189. // and added ms_apt2dAttribCheckSmall[],
  190. // ms_apt2dAttribCheckLarge[], and ms_apt2dAttribCheckHuge[].
  191. // Also, added pointer so that each object can choose one,
  192. // m_pap2dAttribCheckPoints.
  193. // Also, changed type of these arrays to local type Point2D.
  194. // Also, added two additional points to each of the arrays.
  195. //
  196. // 08/28/97 JMI Now EditRect() checks to make sure the current animation
  197. // actually has its components.
  198. //
  199. // 08/28/97 BRH Added virtual put me down message handler.
  200. //
  201. // 09/02/97 JMI Now sets the fire's starter ID so it can tell us correctly
  202. // who to credit for our burn damage in the case that it is
  203. // our internal flame.
  204. //
  205. ////////////////////////////////////////////////////////////////////////////////
  206. #define THING3D_CPP
  207. #include "RSPiX.h"
  208. #include <math.h>
  209. #include "Thing3d.h"
  210. #include "reality.h"
  211. #include "fire.h"
  212. ////////////////////////////////////////////////////////////////////////////////
  213. // Macros/types/etc.
  214. ////////////////////////////////////////////////////////////////////////////////
  215. #define SHADOW_FILE "shadow.img"
  216. #define MaxForeVel 80.0
  217. #define MaxBackVel -60.0
  218. // Determines the number of elements in the passed array at compile time.
  219. #define NUM_ELEMENTS(a) (sizeof(a) / sizeof(a[0]) )
  220. // Gets a random between -range / 2 and range / 2.
  221. #define RAND_SWAY(sway) ((GetRand() % sway) - sway / 2)
  222. // Amount of time fire will last.
  223. #define BURN_DURATION 5000 // In ms.
  224. // Light level for burnt thing3d.
  225. #define BURNT_BRIGHTNESS -40 // -128 to 127.
  226. // Sets a value pointed to if ptr is not NULL.
  227. #define SET(pval, val) ((pval != NULL) ? *pval = val : val)
  228. // Multiply damage by this to get velocity (for absorbing momentum of
  229. // damage cause (e.g., bullets, etc.) ).
  230. #define DAMAGE2VEL_RATIO 1.0
  231. // Terminates arrays of attrib check points.
  232. #define ATTRIB_CHECK_TERMINATOR -32768
  233. ////////////////////////////////////////////////////////////////////////////////
  234. // Variables/data
  235. ////////////////////////////////////////////////////////////////////////////////
  236. double CThing3d::ms_dDefaultSurfaceDrag = 220.0; // Default drag along surfaces.
  237. double CThing3d::ms_dDefaultAirDrag = 30.0; // Default drag due to air friction.
  238. short CThing3d::ms_sBurntBrightness = -40; // Brightness level after being burnt
  239. /// These are the points that are checked on the attrib map /////////////////////
  240. // These points are relative to the thing's origin.
  241. // These are arrays of pts to be checked on the attribute map for various
  242. // size of CThing3d derived things.
  243. const CThing3d::Point2D CThing3d::ms_apt2dAttribCheckSmall[] =
  244. {
  245. // + + +
  246. // + x +
  247. // + + +
  248. { -2, -2, },
  249. { 0, -2, },
  250. { 2, -2, },
  251. { -2, 0, },
  252. { 2, 0, },
  253. { -2, 2, },
  254. { 0, 2, },
  255. { 2, 2, },
  256. // Add more points above this one.
  257. { ATTRIB_CHECK_TERMINATOR, }
  258. };
  259. const CThing3d::Point2D CThing3d::ms_apt2dAttribCheckMedium[] =
  260. {
  261. // + + +
  262. // + x +
  263. // + + +
  264. { -6, -6, },
  265. { 0, -6, },
  266. { 6, -6, },
  267. { -6, 0, },
  268. { 6, 0, },
  269. { -6, 6, },
  270. { 0, 6, },
  271. { 6, 6, },
  272. // Add more points above this one.
  273. { ATTRIB_CHECK_TERMINATOR, }
  274. };
  275. const CThing3d::Point2D CThing3d::ms_apt2dAttribCheckLarge[] =
  276. {
  277. // + + +
  278. // + x +
  279. // + + +
  280. { -12, -12, },
  281. { 0, -12, },
  282. { 12, -12, },
  283. { -12, 0, },
  284. { 12, 0, },
  285. { -12, 12, },
  286. { 0, 12, },
  287. { 12, 12, },
  288. // Add more points above this one.
  289. { ATTRIB_CHECK_TERMINATOR, }
  290. };
  291. const CThing3d::Point2D CThing3d::ms_apt2dAttribCheckHuge[] =
  292. {
  293. // + + +
  294. // + x +
  295. // + + +
  296. { -48, -48, },
  297. { 0, -48, },
  298. { 48, -48, },
  299. { -48, 0, },
  300. { 48, 0, },
  301. { -48, 48, },
  302. { 0, 48, },
  303. { 48, 48, },
  304. // Add more points above this one.
  305. { ATTRIB_CHECK_TERMINATOR, }
  306. };
  307. char* CThing3d::ms_apszStateNames[] =
  308. {
  309. "State_Idle",
  310. "State_Shot",
  311. "State_Blownup",
  312. "State_Burning",
  313. "State_Die",
  314. "State_Dead",
  315. "State_RunOver",
  316. "State_Vomit",
  317. "State_Suicide",
  318. "State_Persistent",
  319. "State_Stand",
  320. "State_Throw",
  321. "State_ThrowRelease",
  322. "State_ThrowFinish",
  323. "State_ThrowDone",
  324. "State_Run",
  325. "State_Shooting",
  326. "State_RunAndShoot",
  327. "State_Strafe",
  328. "State_StrafeAndShoot",
  329. "State_Launch",
  330. "State_LaunchRelease",
  331. "State_LaunchFinish",
  332. "State_LaunchDone",
  333. "State_GetUp",
  334. "State_Duck",
  335. "State_Rise",
  336. "State_Jump",
  337. "State_JumpForward",
  338. "State_Land",
  339. "State_LandForward",
  340. "State_Fall",
  341. "State_March",
  342. "State_Mingle",
  343. "State_Panic",
  344. "State_Load",
  345. "State_Patrol",
  346. "State_Shoot",
  347. "State_Wait",
  348. "State_Stop",
  349. "State_Hunt",
  350. "State_HuntNext",
  351. "State_Engage",
  352. "State_Guard",
  353. "State_Reposition",
  354. "State_Retreat",
  355. "State_PopBegin",
  356. "State_PopWait",
  357. "State_Popout",
  358. "State_PopShoot",
  359. "State_RunShootBegin",
  360. "State_RunShootRun",
  361. "State_RunShootWait",
  362. "State_RunShoot",
  363. "State_Delete",
  364. "State_Writhing",
  365. "State_Execute",
  366. "State_PutDown",
  367. "State_PickUp",
  368. "State_PutOutFire",
  369. "State_Walk",
  370. "State_Hide",
  371. "State_MoveNext",
  372. "State_PositionSet",
  373. "State_PositionMove",
  374. "State_HideBegin",
  375. "State_ShootRun",
  376. "State_HuntHold",
  377. "State_Climb",
  378. "State_ObjectReleased",
  379. "PanicBegin",
  380. "PanicContinue",
  381. "WalkBegin",
  382. "WalkContinue",
  383. "DelayShoot",
  384. "AvoidFire",
  385. "Helping",
  386. "MarchNext",
  387. "WalkNext",
  388. "Please Add State Description",
  389. "Please Add State Description",
  390. "Please Add State Description",
  391. "Please Add State Description",
  392. "Please Add State Description",
  393. "Please Add State Description",
  394. "Please Add State Description",
  395. "Please Add State Description",
  396. "Please Add State Description",
  397. };
  398. ////////////////////////////////////////////////////////////////////////////////
  399. // Load object (should call base class version!)
  400. ////////////////////////////////////////////////////////////////////////////////
  401. short CThing3d::Load( // Returns 0 if successfull, non-zero otherwise
  402. RFile* pFile, // In: File to load from
  403. bool bEditMode, // In: True for edit mode, false otherwise
  404. short sFileCount, // In: File count (unique per file, never 0)
  405. ULONG ulFileVersion) // In: Version of file format to load.
  406. {
  407. // Call the CThing base class load to get the instance ID
  408. short sResult = CThing::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  409. if (sResult == 0)
  410. {
  411. // Load object data
  412. switch (ulFileVersion)
  413. {
  414. default:
  415. case 16:
  416. pFile->Read(&m_dX);
  417. pFile->Read(&m_dY);
  418. pFile->Read(&m_dZ);
  419. pFile->Read(&m_dRot);
  420. sResult = m_stockpile.Load(pFile, ulFileVersion);
  421. break;
  422. case 15:
  423. case 14:
  424. case 13:
  425. case 12:
  426. case 11:
  427. case 10:
  428. case 9:
  429. case 8:
  430. case 7:
  431. case 6:
  432. case 5:
  433. case 4:
  434. case 3:
  435. pFile->Read(&m_dX);
  436. pFile->Read(&m_dY);
  437. pFile->Read(&m_dZ);
  438. pFile->Read(&m_stockpile.m_sHitPoints);
  439. pFile->Read(&m_dRot);
  440. break;
  441. case 2:
  442. case 1:
  443. pFile->Read(&m_dX);
  444. pFile->Read(&m_dY);
  445. pFile->Read(&m_dZ);
  446. // PATCH: To make the CCharacter continue to work. We need to
  447. // read the space that is occupied by the weapon type for CCharacter.
  448. // In file format versions 3 and above, this is written later in
  449. // the file.
  450. CThing::ClassIDType idWeaponDummy;
  451. pFile->Read(&idWeaponDummy);
  452. // END PATCH.
  453. pFile->Read(&m_stockpile.m_sHitPoints);
  454. pFile->Read(&m_dRot);
  455. break;
  456. }
  457. // If the file version is earlier than the change to real 3D coords . . .
  458. if (ulFileVersion < 24)
  459. {
  460. // Convert to 3D.
  461. m_pRealm->MapY2DtoZ3D(
  462. m_dZ,
  463. &m_dZ);
  464. }
  465. // Make sure there were no file errors or format errors . . .
  466. if (!pFile->Error() && sResult == 0)
  467. {
  468. // Success.
  469. }
  470. else
  471. {
  472. sResult = -1;
  473. TRACE("CThing3d::Load(): Error reading from file!\n");
  474. }
  475. }
  476. return sResult;
  477. }
  478. ////////////////////////////////////////////////////////////////////////////////
  479. // Save object (should call base class version!)
  480. ////////////////////////////////////////////////////////////////////////////////
  481. short CThing3d::Save( // Returns 0 if successfull, non-zero otherwise
  482. RFile* pFile, // In: File to save to
  483. short sFileCount) // In: File count (unique per file, never 0)
  484. {
  485. // Call the base class save to save the u16InstanceID
  486. short sResult = CThing::Save(pFile, sFileCount);
  487. if (sResult == 0)
  488. {
  489. // Save object data
  490. pFile->Write(&m_dX);
  491. pFile->Write(&m_dY);
  492. pFile->Write(&m_dZ);
  493. pFile->Write(&m_dRot);
  494. sResult = m_stockpile.Save(pFile);
  495. }
  496. return sResult;
  497. }
  498. ////////////////////////////////////////////////////////////////////////////////
  499. // Startup object
  500. ////////////////////////////////////////////////////////////////////////////////
  501. short CThing3d::Startup(void) // Returns 0 if successfull, non-zero otherwise
  502. {
  503. // Init other stuff
  504. m_lPrevTime = m_pRealm->m_time.GetGameTime();
  505. m_lAnimPrevUpdateTime = m_lPrevTime;
  506. // No special flags
  507. m_sprite.m_sInFlags = 0;
  508. return 0;
  509. }
  510. ////////////////////////////////////////////////////////////////////////////////
  511. // Shutdown object
  512. ////////////////////////////////////////////////////////////////////////////////
  513. short CThing3d::Shutdown(void) // Returns 0 if successfull, non-zero otherwise
  514. {
  515. return 0;
  516. }
  517. ////////////////////////////////////////////////////////////////////////////////
  518. // Suspend object
  519. ////////////////////////////////////////////////////////////////////////////////
  520. void CThing3d::Suspend(void)
  521. {
  522. if (m_sSuspend == 0)
  523. {
  524. // Store current delta so we can restore it.
  525. long lCurTime = m_pRealm->m_time.GetGameTime();
  526. m_lPrevTime = lCurTime - m_lPrevTime;
  527. m_lAnimPrevUpdateTime = lCurTime - m_lAnimPrevUpdateTime;
  528. }
  529. m_sSuspend++;
  530. }
  531. ////////////////////////////////////////////////////////////////////////////////
  532. // Resume object
  533. ////////////////////////////////////////////////////////////////////////////////
  534. void CThing3d::Resume(void)
  535. {
  536. m_sSuspend--;
  537. // If we're actually going to start updating again, we need to reset
  538. // the time so as to ignore any time that passed while we were suspended.
  539. // This method is far from precise, but I'm hoping it's good enough.
  540. if (m_sSuspend == 0)
  541. {
  542. long lCurTime = m_pRealm->m_time.GetGameTime();
  543. m_lPrevTime = lCurTime - m_lPrevTime;
  544. m_lAnimPrevUpdateTime = lCurTime - m_lAnimPrevUpdateTime;
  545. }
  546. }
  547. ////////////////////////////////////////////////////////////////////////////////
  548. // Render object
  549. ////////////////////////////////////////////////////////////////////////////////
  550. void CThing3d::Render(void)
  551. {
  552. U16 u16CombinedAttributes;
  553. short sLightTally;
  554. GetEffectAttributes(m_dX, m_dZ, &u16CombinedAttributes, &sLightTally);
  555. // Brightness.
  556. m_sprite.m_sBrightness = m_sBrightness + sLightTally * gsGlobalBrightnessPerLightAttribute;
  557. // If no parent . . .
  558. if (m_u16IdParent == CIdBank::IdNil)
  559. {
  560. // Reset transform back to start to set absolute rather than cummulative rotation
  561. m_trans.Make1();
  562. m_trans.Scale(m_dScaleX,m_dScaleY,m_dScaleZ);
  563. m_trans.Ry(rspMod360(m_dRot) );
  564. m_trans.Rz(rspMod360(m_dRotZ) );
  565. // Map from 3d to 2d coords
  566. Map3Dto2D((short) m_dX, (short) m_dY, (short) m_dZ, &m_sprite.m_sX2, &m_sprite.m_sY2);
  567. // If no layer override . . .
  568. if (m_sLayerOverride < 0)
  569. {
  570. // Layer should be based on info from attribute map.
  571. GetLayer(m_dX, m_dZ, &(m_sprite.m_sLayer) );
  572. }
  573. else
  574. {
  575. // Use the layer override.
  576. m_sprite.m_sLayer = m_sLayerOverride;
  577. }
  578. // Priority is based on our Z position.
  579. m_sprite.m_sPriority = m_dZ;
  580. // Update sprite in scene
  581. m_pRealm->m_scene.UpdateSprite(&m_sprite);
  582. // Set transform.
  583. m_sprite.m_ptrans = &m_trans;
  584. // If the item is above the ground, show the shadow sprite, else hide it.
  585. if (m_bAboveTerrain)
  586. m_spriteShadow.m_sInFlags &= ~CSprite::InHidden;
  587. else
  588. m_spriteShadow.m_sInFlags |= CSprite::InHidden;
  589. // If the shadow is enabled
  590. if (!(m_spriteShadow.m_sInFlags & CSprite::InHidden) && m_spriteShadow.m_pImage != NULL)
  591. {
  592. // Get the height of the terrain from the attribute map
  593. short sY = m_pRealm->GetHeight((short) m_dX, (short) m_dZ);
  594. // Map from 3d to 2d coords
  595. Map3Dto2D(m_dX, (double) sY, m_dZ, &(m_spriteShadow.m_sX2), &(m_spriteShadow.m_sY2) );
  596. // Offset hotspot to center of image.
  597. m_spriteShadow.m_sX2 -= m_spriteShadow.m_pImage->m_sWidth / 2;
  598. m_spriteShadow.m_sY2 -= m_spriteShadow.m_pImage->m_sHeight / 2;
  599. // Priority is based on bottom edge of sprite on X/Z plane!
  600. m_spriteShadow.m_sPriority = MAX(m_sprite.m_sPriority - 1, 0);//m_dZ;
  601. // Layer should be based on info we get from attribute map.
  602. m_spriteShadow.m_sLayer = m_sprite.m_sLayer; //CRealm::GetLayerViaAttrib(m_pRealm->GetLayer((short) m_dX, (short) m_dZ));
  603. // Set the alpha level based on the height difference
  604. m_spriteShadow.m_sAlphaLevel = 200 - ((short) m_dY - sY);
  605. // Check bounds . . .
  606. if (m_spriteShadow.m_sAlphaLevel < 0)
  607. {
  608. m_spriteShadow.m_sAlphaLevel = 0;
  609. }
  610. else if (m_spriteShadow.m_sAlphaLevel > 255)
  611. {
  612. m_spriteShadow.m_sAlphaLevel = 255;
  613. }
  614. // Update sprite in scene
  615. m_pRealm->m_scene.UpdateSprite(&m_spriteShadow);
  616. }
  617. }
  618. ASSERT(m_panimCur != NULL);
  619. m_sprite.m_pmesh = (RMesh*) m_panimCur->m_pmeshes->GetAtTime(m_lAnimTime);
  620. m_sprite.m_psop = (RSop*) m_panimCur->m_psops->GetAtTime(m_lAnimTime);
  621. m_sprite.m_ptex = (RTexture*) m_panimCur->m_ptextures->GetAtTime(m_lAnimTime);
  622. m_sprite.m_psphere = (RP3d*) m_panimCur->m_pbounds->GetAtTime(m_lAnimTime);
  623. }
  624. ////////////////////////////////////////////////////////////////////////////////
  625. // Called by editor to render object
  626. ////////////////////////////////////////////////////////////////////////////////
  627. void CThing3d::EditRender(void)
  628. {
  629. // In some cases, object's might need to do a special-case render in edit
  630. // mode because Startup() isn't called. In this case it doesn't matter, so
  631. // we can call the normal Render().
  632. Render();
  633. }
  634. ////////////////////////////////////////////////////////////////////////////////
  635. // Called by editor to init new object at specified position
  636. ////////////////////////////////////////////////////////////////////////////////
  637. short CThing3d::EditNew( // Returns 0 if successfull, non-zero otherwise
  638. short sX, // In: New x coord
  639. short sY, // In: New y coord
  640. short sZ) // In: New z coord
  641. {
  642. short sResult = 0;
  643. // Use specified position
  644. m_dX = (double)sX;
  645. m_dY = (double)sY;
  646. m_dZ = (double)sZ;
  647. return sResult;
  648. }
  649. ////////////////////////////////////////////////////////////////////////////////
  650. // Called by editor to modify object
  651. ////////////////////////////////////////////////////////////////////////////////
  652. short CThing3d::EditModify(void)
  653. {
  654. return 0;
  655. }
  656. ////////////////////////////////////////////////////////////////////////////////
  657. // Called by editor to move object to specified position
  658. ////////////////////////////////////////////////////////////////////////////////
  659. short CThing3d::EditMove( // Returns 0 if successfull, non-zero otherwise
  660. short sX, // In: New x coord
  661. short sY, // In: New y coord
  662. short sZ) // In: New z coord
  663. {
  664. m_dX = (double)sX;
  665. m_dY = (double)sY;
  666. m_dZ = (double)sZ;
  667. return 0;
  668. }
  669. ////////////////////////////////////////////////////////////////////////////////
  670. // Give Edit a rectangle around this object
  671. ////////////////////////////////////////////////////////////////////////////////
  672. void CThing3d::EditRect(RRect* pRect)
  673. {
  674. if (m_panimCur != NULL)
  675. {
  676. if (m_panimCur->m_psops)
  677. {
  678. // Get current sphere.
  679. RP3d apt3dSrc[2]; // Center and point on outside of
  680. // bounding sphere in "Randy" coords.
  681. RP3d apt3dDst[2]; // Dst for above in Postal coords.
  682. RTransform trans;
  683. apt3dSrc[0] = *((RP3d*) m_panimCur->m_pbounds->GetAtTime(0));
  684. apt3dSrc[1].x = apt3dSrc[0].x + apt3dSrc[0].w;
  685. apt3dSrc[1].y = apt3dSrc[0].y + apt3dSrc[0].w;
  686. apt3dSrc[1].z = apt3dSrc[0].z + apt3dSrc[0].w;
  687. apt3dSrc[0].w = 1;
  688. apt3dSrc[1].w = 1;
  689. m_pRealm->m_scene.TransformPtsToRealm(&trans, apt3dSrc, apt3dDst, 2);
  690. m_sprite.m_sRadius = sqrt(ABS2(
  691. apt3dDst[1].x - apt3dDst[0].x,
  692. apt3dDst[1].y - apt3dDst[0].y,
  693. apt3dDst[1].z - apt3dDst[0].z
  694. ) );
  695. Map3Dto2D(
  696. m_dX + apt3dDst[0].x,
  697. m_dY + apt3dDst[0].y,
  698. m_dZ + apt3dDst[0].z,
  699. &(pRect->sX), &(pRect->sY) );
  700. }
  701. }
  702. else
  703. {
  704. Map3Dto2D(m_dX, m_dY, m_dZ, &(pRect->sX), &(pRect->sY) );
  705. m_sprite.m_sRadius = 10; // **FUDGE.
  706. }
  707. pRect->sX -= m_sprite.m_sRadius;
  708. pRect->sY -= m_sprite.m_sRadius;
  709. pRect->sW = m_sprite.m_sRadius * 2;
  710. pRect->sH = m_sprite.m_sRadius * 2;
  711. }
  712. ////////////////////////////////////////////////////////////////////////////////
  713. // Called by editor to get the hotspot of an object in 2D.
  714. // (virtual (Overridden here)).
  715. ////////////////////////////////////////////////////////////////////////////////
  716. void CThing3d::EditHotSpot( // Returns nothiing.
  717. short* psX, // Out: X coord of 2D hotspot relative to
  718. // EditRect() pos.
  719. short* psY) // Out: Y coord of 2D hotspot relative to
  720. // EditRect() pos.
  721. {
  722. // Get rectangle.
  723. RRect rc;
  724. EditRect(&rc);
  725. // Get 2D hotspot.
  726. short sX;
  727. short sY;
  728. Map3Dto2D(
  729. m_dX,
  730. m_dY,
  731. m_dZ,
  732. &sX,
  733. &sY);
  734. // Get relation.
  735. *psX = sX - rc.sX;
  736. *psY = sY - rc.sY;
  737. }
  738. //---------------------------------------------------------------------------
  739. // Useful generic thing3d state-specific functionality.
  740. //---------------------------------------------------------------------------
  741. ////////////////////////////////////////////////////////////////////////////////
  742. // Implements basic one-time functionality for each time State_Shot is
  743. // entered.
  744. ////////////////////////////////////////////////////////////////////////////////
  745. void CThing3d::OnShot(void)
  746. {
  747. }
  748. ////////////////////////////////////////////////////////////////////////////////
  749. // Implements basic functionality while shot and returns true
  750. // until the state is completed.
  751. ////////////////////////////////////////////////////////////////////////////////
  752. bool CThing3d::WhileShot(void) // Returns true until state is complete
  753. {
  754. bool bStatePersists = true;
  755. if (m_lAnimTime > m_panimCur->m_psops->TotalTime() || m_stockpile.m_sHitPoints <= 0)
  756. bStatePersists = false;
  757. return bStatePersists;
  758. }
  759. ////////////////////////////////////////////////////////////////////////////////
  760. // Implements basic functionality while being blown up and returns true
  761. // until the state is completed.
  762. ////////////////////////////////////////////////////////////////////////////////
  763. bool CThing3d::WhileBlownUp(void) // Returns true until state is complete.
  764. {
  765. bool bStatePersists = true; // Assume not done.
  766. double dNewX, dNewY, dNewZ;
  767. // Get time from last call in seconds.
  768. long lCurTime = m_pRealm->m_time.GetGameTime();
  769. double dSeconds = double(lCurTime - m_lPrevTime) / 1000.0;
  770. // Update Velocities ////////////////////////////////////////////////////////
  771. UpdateVelocities(dSeconds, MaxForeVel, MaxBackVel);
  772. // Get New Position /////////////////////////////////////////////////////////
  773. GetNewPosition(&dNewX, &dNewY, &dNewZ, dSeconds);
  774. // Validate New Position ////////////////////////////////////////////////////
  775. // Get attribute at new location.
  776. // Get height at new position.
  777. USHORT usAttrib;
  778. short sHeight;
  779. GetFloorAttributes(dNewX, dNewZ, &usAttrib, &sHeight);
  780. // If it was above the ground last time and is now below the ground, it must have
  781. // hit the ground and the blown up state is complete
  782. if (dNewY < sHeight && m_dY >= sHeight)
  783. {
  784. dNewY = sHeight;
  785. // Make sure its done with current animation also
  786. if (m_lAnimTime > m_panimCur->m_psops->TotalTime())
  787. bStatePersists = false;
  788. // No longer above the terrain.
  789. m_bAboveTerrain = false;
  790. }
  791. else
  792. {
  793. // We should be above the terrain.
  794. m_bAboveTerrain = true;
  795. // If the new height is greater than the current and previous height, then it must
  796. // have hit a wall and should continue to fall against the wall (update Y but not
  797. // X or Z - which is kind of cheating since it may be free to move in X or Z unless
  798. // it is in a corner
  799. if (usAttrib & REALM_ATTR_NOT_WALKABLE || (dNewY < sHeight && m_dY < sHeight))
  800. {
  801. // Reset x and z to previous positiion
  802. dNewX = m_dX;
  803. dNewZ = m_dZ;
  804. // Stop moving horizontally.
  805. m_dVel = 0.0;
  806. m_dAcc = 0.0;
  807. m_dExtHorzVel = 0.0;
  808. m_dExtHorzDrag = 0.0;
  809. m_dDrag = 0.0;
  810. // Make sure it's not underground at this position with the new y position.
  811. GetFloorAttributes(dNewX, dNewZ, &usAttrib, &sHeight);
  812. if (dNewY <= sHeight)
  813. {
  814. // Get out of the ground.
  815. dNewY = sHeight;
  816. // We are not above the terrain.
  817. m_bAboveTerrain = false;
  818. }
  819. }
  820. }
  821. m_dX = dNewX;
  822. m_dY = dNewY;
  823. m_dZ = dNewZ;
  824. return bStatePersists;
  825. }
  826. ////////////////////////////////////////////////////////////////////////////////
  827. // Implements basic functionality while being on fire and returns true
  828. // until the state is completed.
  829. ////////////////////////////////////////////////////////////////////////////////
  830. bool CThing3d::WhileBurning(void) // Returns true until state is complete.
  831. {
  832. bool bStatePersists = true; // Assume not done.
  833. // See if it is destroyed/dead yet.
  834. if (m_stockpile.m_sHitPoints < 0)
  835. bStatePersists = false;
  836. return bStatePersists;
  837. }
  838. ////////////////////////////////////////////////////////////////////////////////
  839. // Implements basic functionality while being run over and returns true
  840. // until the state is completed.
  841. ////////////////////////////////////////////////////////////////////////////////
  842. bool CThing3d::WhileRunOver(void) // Returns true until state is complete.
  843. {
  844. bool bStatePersists = true; // Assume not done.
  845. return bStatePersists;
  846. }
  847. //---------------------------------------------------------------------------
  848. // Useful generic thing3d functionality.
  849. //---------------------------------------------------------------------------
  850. ////////////////////////////////////////////////////////////////////////////////
  851. // This inline updates a specified velocity with a specified drag over a
  852. // specified time.
  853. ////////////////////////////////////////////////////////////////////////////////
  854. inline
  855. bool UpdateVelocity( // Returns true if velocity reaches zero because of the
  856. // supplied accelration, false otherwise.
  857. double* pdVel, // In: Initial velocity.
  858. // Out: New velocity.
  859. double* pdDeltaVel, // Out: Delta velocity.
  860. double dAcc, // In: Acceleration.
  861. double dSeconds) // In: Elapsed time in seconds.
  862. {
  863. bool bAcceleratedToZero = false;
  864. double dVelPrev = *pdVel;
  865. *pdDeltaVel = dAcc * dSeconds;
  866. *pdVel += *pdDeltaVel;
  867. // I think this can be consdensed into a subtraction and one or two comparisons,
  868. // but I'm not sure that's really faster than the max 3 comparisons here.
  869. // If previously traveling forward . . .
  870. if (dVelPrev > 0.0)
  871. {
  872. // Passing 0 is considered at rest . . .
  873. if (*pdVel < 0.0)
  874. {
  875. // Update delta.
  876. *pdDeltaVel -= *pdVel;
  877. // Zero velocity.
  878. *pdVel = 0.0;
  879. }
  880. }
  881. else
  882. {
  883. // If previously traveling backward . . .
  884. if (dVelPrev < 0.0)
  885. {
  886. // Passing 0 is considered at rest . . .
  887. if (*pdVel > 0.0)
  888. {
  889. // Update delta.
  890. *pdDeltaVel -= *pdVel;
  891. // Zero velocity.
  892. *pdVel = 0.0;
  893. }
  894. }
  895. }
  896. // If velocity is now zero . . .
  897. if (*pdVel == 0.0)
  898. {
  899. // If drag opposed the previous velocity . . .
  900. if ((dVelPrev > 0.0 && dAcc < 0.0) || (dVelPrev < 0.0 && dAcc > 0.0))
  901. {
  902. // Drag has achieved its goal.
  903. bAcceleratedToZero = true;
  904. }
  905. }
  906. return bAcceleratedToZero;
  907. }
  908. ////////////////////////////////////////////////////////////////////////////////
  909. // Applies accelerations to velocities keeping them within specified
  910. // limits.
  911. ////////////////////////////////////////////////////////////////////////////////
  912. void CThing3d::UpdateVelocities(
  913. double dSeconds, // Seconds since last update.
  914. double dMaxForeVel, // Maximum forward velocity.
  915. double dMaxBackVel) // Maximum backward velocity.
  916. {
  917. double dVelPrev;
  918. ///////////////////////// Internal Velocity /////////////////////////////////
  919. // Note: This does not use UpdateVelocity() b/c there are two accelerations
  920. // acting on the one velocity. Have to investigate what problems could
  921. // arise.
  922. dVelPrev = m_dVel;
  923. m_dDeltaVel = (m_dAcc + m_dDrag) * dSeconds;
  924. m_dVel += m_dDeltaVel;
  925. // If previously traveling forward . . .
  926. if (dVelPrev > 0.0)
  927. {
  928. // Passing 0 is considered at rest . . .
  929. if (m_dVel < 0.0)
  930. {
  931. m_dVel = 0.0;
  932. }
  933. else
  934. {
  935. if (m_dVel > dMaxForeVel)
  936. {
  937. m_dVel = dMaxForeVel;
  938. }
  939. }
  940. }
  941. else
  942. {
  943. // If previously traveling backward . . .
  944. if (dVelPrev < 0.0)
  945. {
  946. // Passing 0 is considered at rest . . .
  947. if (m_dVel > 0.0)
  948. {
  949. m_dVel = 0.0;
  950. }
  951. else
  952. {
  953. if (m_dVel < dMaxBackVel)
  954. {
  955. m_dVel = dMaxBackVel;
  956. }
  957. }
  958. }
  959. }
  960. // If velocity is now zero . . .
  961. if (m_dVel == 0.0)
  962. {
  963. // If drag opposed the previous velocity . . .
  964. if ((dVelPrev > 0.0 && m_dDrag < 0.0) || (dVelPrev < 0.0 && m_dDrag > 0.0))
  965. {
  966. // Drag has achieved its goal.
  967. m_dDrag = 0.0;
  968. }
  969. }
  970. // Update delta (we may have capped or trimmed velocity against dMax*Vel).
  971. m_dDeltaVel = m_dVel - dVelPrev;
  972. ///////////////////////// External Velocity /////////////////////////////////
  973. // If velocity is now zero b/c of drag . . .
  974. if (UpdateVelocity(&m_dExtHorzVel, &m_dExtHorzDeltaVel, m_dExtHorzDrag, dSeconds) == true)
  975. {
  976. // Drag has achieved its goal.
  977. m_dExtHorzDrag = 0.0;
  978. }
  979. ///////////////////////// Vertical Velocity /////////////////////////////////
  980. UpdateVelocity(&m_dExtVertVel, &m_dExtVertDeltaVel, g_dAccelerationDueToGravity, dSeconds);
  981. }
  982. ////////////////////////////////////////////////////////////////////////////////
  983. // Applies velocities to positions.
  984. ////////////////////////////////////////////////////////////////////////////////
  985. void CThing3d::GetNewPosition( // Returns nothing.
  986. double* pdNewX, // Out: New x position.
  987. double* pdNewY, // Out: New y position.
  988. double* pdNewZ, // Out: New z position.
  989. double dSeconds) // Seconds since last update.
  990. {
  991. // Couldn't decide whether this should be in UpdateVelocities() or
  992. // GetNewPosition().
  993. ///////////////////////////// Rotations /////////////////////////////////////
  994. m_dRot = rspMod360(m_dRot + m_dExtRotVelY * dSeconds);
  995. m_dRotZ = rspMod360(m_dRotZ + m_dExtRotVelZ * dSeconds);
  996. // Make sure rotation is w/i bounds for COSQ and SINQ arrays.
  997. // Note: If we keep the above adjustments on m_dRot, we won't
  998. // need this rspMod360().
  999. short sRot = rspMod360(m_dRot);
  1000. m_dRot = sRot;
  1001. // Make sure external force rotation is w/i bounds for COSQ and SINQ arrays.
  1002. short sExtHorzRot = rspMod360(m_dExtHorzRot);
  1003. m_dExtHorzRot = sExtHorzRot;
  1004. double dDistance;
  1005. // Apply internal velocity.
  1006. dDistance = (m_dVel - m_dDeltaVel / 2) * dSeconds;
  1007. *pdNewX = m_dX + COSQ[sRot] * dDistance;
  1008. *pdNewZ = m_dZ - SINQ[sRot] * dDistance;
  1009. // Apply external velocity.
  1010. dDistance = (m_dExtHorzVel - m_dExtHorzDeltaVel / 2) * dSeconds;
  1011. *pdNewX += COSQ[sExtHorzRot] * dDistance;
  1012. *pdNewZ += -SINQ[sExtHorzRot] * dDistance;
  1013. // Apply external vertical velocity.
  1014. dDistance = (m_dExtVertVel - m_dExtVertDeltaVel / 2) * dSeconds;
  1015. *pdNewY = m_dY + dDistance;
  1016. }
  1017. //#ifdef MOBILE
  1018. ////////////////////////////////////////////////////////////////////////////////
  1019. // Applies velocities to positions with angle
  1020. ////////////////////////////////////////////////////////////////////////////////
  1021. void CThing3d::GetNewPositionAngle( // Returns nothing.
  1022. double* pdNewX, // Out: New x position.
  1023. double* pdNewY, // Out: New y position.
  1024. double* pdNewZ, // Out: New z position.
  1025. double dSeconds, // Seconds since last update.
  1026. double dAngle
  1027. )
  1028. {
  1029. // Couldn't decide whether this should be in UpdateVelocities() or
  1030. // GetNewPosition().
  1031. ///////////////////////////// Rotations /////////////////////////////////////
  1032. //dAngle = rspMod360(dAngle + m_dExtRotVelY * dSeconds);
  1033. m_dRot = rspMod360(m_dRot + m_dExtRotVelY * dSeconds);
  1034. m_dRotZ = rspMod360(m_dRotZ + m_dExtRotVelZ * dSeconds);
  1035. // Make sure rotation is w/i bounds for COSQ and SINQ arrays.
  1036. // Note: If we keep the above adjustments on m_dRot, we won't
  1037. // need this rspMod360().
  1038. short sRot = rspMod360(m_dRot);
  1039. m_dRot = sRot;
  1040. sRot = rspMod360(dAngle);
  1041. // Make sure external force rotation is w/i bounds for COSQ and SINQ arrays.
  1042. short sExtHorzRot = rspMod360(m_dExtHorzRot);
  1043. m_dExtHorzRot = sExtHorzRot;
  1044. double dDistance;
  1045. // Apply internal velocity.
  1046. dDistance = (m_dVel - m_dDeltaVel / 2) * dSeconds;
  1047. *pdNewX = m_dX + COSQ[sRot] * dDistance;
  1048. *pdNewZ = m_dZ - SINQ[sRot] * dDistance;
  1049. // Apply external velocity.
  1050. dDistance = (m_dExtHorzVel - m_dExtHorzDeltaVel / 2) * dSeconds;
  1051. *pdNewX += COSQ[sExtHorzRot] * dDistance;
  1052. *pdNewZ += -SINQ[sExtHorzRot] * dDistance;
  1053. // Apply external vertical velocity.
  1054. dDistance = (m_dExtVertVel - m_dExtVertDeltaVel / 2) * dSeconds;
  1055. *pdNewY = m_dY + dDistance;
  1056. }
  1057. //#endif
  1058. ////////////////////////////////////////////////////////////////////////////////
  1059. // Determines if supplied position is valid tweaking it if necessary.
  1060. // (virtual).
  1061. ////////////////////////////////////////////////////////////////////////////////
  1062. bool CThing3d::MakeValidPosition( // Returns true, if new position was valid.
  1063. // Returns false, if could not reach new position.
  1064. double* pdNewX, // In: x position to validate.
  1065. // Out: New x position.
  1066. double* pdNewY, // In: y position to validate.
  1067. // Out: New y position.
  1068. double* pdNewZ, // In: z position to validate.
  1069. // Out: New z position.
  1070. short sVertTolerance /*= 0*/) // Vertical tolerance.
  1071. {
  1072. bool bValidatedPosition = false; // Assume failure.
  1073. // Get attribute at new location.
  1074. // Get height at new position.
  1075. USHORT usAttrib;
  1076. short sHeight;
  1077. GetFloorAttributes(*pdNewX, *pdNewZ, &usAttrib, &sHeight);
  1078. // If too big a height difference or completely not walkable . . .
  1079. if (usAttrib & REALM_ATTR_NOT_WALKABLE
  1080. || (sHeight - *pdNewY > sVertTolerance) )// && m_bAboveTerrain == false && m_dExtHorzVel == 0.0))
  1081. {
  1082. // Restore previous X/Z position.
  1083. *pdNewX = m_dX;
  1084. *pdNewZ = m_dZ;
  1085. bValidatedPosition = true;
  1086. // Get height in that spot.
  1087. GetFloorAttributes(*pdNewX, *pdNewZ, &usAttrib, &sHeight);
  1088. }
  1089. else
  1090. {
  1091. // Note that we succeeded in making new position valid.
  1092. bValidatedPosition = true;
  1093. }
  1094. // If we're gonna be at or below ground level . . .
  1095. if (sHeight >= *pdNewY)
  1096. {
  1097. // Get outta there!
  1098. *pdNewY = sHeight;
  1099. // Update vertical delta.
  1100. m_dExtVertDeltaVel += -m_dExtVertVel;
  1101. // Reset vertical velocity.
  1102. m_dExtVertVel = 0.0;
  1103. m_bAboveTerrain = false;
  1104. }
  1105. else
  1106. {
  1107. m_bAboveTerrain = true;
  1108. }
  1109. return bValidatedPosition;
  1110. }
  1111. ////////////////////////////////////////////////////////////////////////////////
  1112. // Deluxe does all for updating position.
  1113. ////////////////////////////////////////////////////////////////////////////////
  1114. void CThing3d::DeluxeUpdatePosVel( // Returns nothing.
  1115. double dSeconds) // In: Duration since last update in seconds.
  1116. {
  1117. double dNewX, dNewY, dNewZ;
  1118. // Update Velocities ////////////////////////////////////////////////////////
  1119. UpdateVelocities(dSeconds, MaxForeVel, MaxBackVel);
  1120. // Get New Position /////////////////////////////////////////////////////////
  1121. GetNewPosition(&dNewX, &dNewY, &dNewZ, dSeconds);
  1122. // Validate New Position ////////////////////////////////////////////////////
  1123. if (MakeValidPosition(&dNewX, &dNewY, &dNewZ, MaxStepUpThreshold) == true)
  1124. {
  1125. // Update Values /////////////////////////////////////////////////////////
  1126. m_dX = dNewX;
  1127. m_dY = dNewY;
  1128. m_dZ = dNewZ;
  1129. UpdateFirePosition();
  1130. }
  1131. else
  1132. {
  1133. // Restore Values ////////////////////////////////////////////////////////
  1134. // Didn't actually move and, therefore, did not actually accelerate.
  1135. // Restore velocities.
  1136. m_dVel -= m_dDeltaVel;
  1137. // m_dExtHorzVel -= m_dExtHorzDeltaVel;
  1138. // m_dExtVertVel -= m_dExtVertDeltaVel;
  1139. }
  1140. }
  1141. ////////////////////////////////////////////////////////////////////////////////
  1142. // Process all messages currently in the message queue through
  1143. // ProcessMessage().
  1144. // (virtual).
  1145. ////////////////////////////////////////////////////////////////////////////////
  1146. void CThing3d::ProcessMessages(void)
  1147. {
  1148. // Check queue of messages.
  1149. GameMessage msg;
  1150. while (m_MessageQueue.DeQ(&msg) == true)
  1151. {
  1152. ProcessMessage(&msg);
  1153. }
  1154. }
  1155. ////////////////////////////////////////////////////////////////////////////////
  1156. // Process the specified message. For most messages, this function
  1157. // will call the equivalent On* function.
  1158. // (virtual).
  1159. ////////////////////////////////////////////////////////////////////////////////
  1160. void CThing3d::ProcessMessage( // Returns nothing.
  1161. GameMessage* pmsg) // Message to process.
  1162. {
  1163. switch (pmsg->msg_Generic.eType)
  1164. {
  1165. case typeShot:
  1166. OnShotMsg(&(pmsg->msg_Shot) );
  1167. break;
  1168. case typeExplosion:
  1169. OnExplosionMsg(&(pmsg->msg_Explosion) );
  1170. break;
  1171. case typeBurn:
  1172. OnBurnMsg(&(pmsg->msg_Burn) );
  1173. break;
  1174. case typeObjectDelete:
  1175. OnDeleteMsg(&(pmsg->msg_ObjectDelete) );
  1176. break;
  1177. case typeHelp:
  1178. OnHelpMsg(&(pmsg->msg_Help) );
  1179. break;
  1180. case typePutMeDown:
  1181. OnPutMeDownMsg(&(pmsg->msg_PutMeDown) );
  1182. break;
  1183. default:
  1184. // Should this complain when it doesn't know a message type?
  1185. break;
  1186. }
  1187. }
  1188. ////////////////////////////////////////////////////////////////////////////////
  1189. // Handles a msg_Shot.
  1190. // (virtual).
  1191. ////////////////////////////////////////////////////////////////////////////////
  1192. void CThing3d::OnShotMsg( // Returns nothing.
  1193. Shot_Message* pshotmsg) // In: Message to handle.
  1194. {
  1195. if (pshotmsg->sDamage > 0)
  1196. {
  1197. // Receive bullet's momentum.
  1198. AddForceVector(
  1199. pshotmsg->sDamage * DAMAGE2VEL_RATIO, // In: Magnitude of additional vector.
  1200. pshotmsg->sAngle); // In: Direction (in degrees) of additional vector.
  1201. }
  1202. }
  1203. ////////////////////////////////////////////////////////////////////////////////
  1204. // Handles an Explosion_Message.
  1205. // (virtual).
  1206. ////////////////////////////////////////////////////////////////////////////////
  1207. void CThing3d::OnExplosionMsg( // Returns nothing.
  1208. Explosion_Message* pexplosionmsg) // In: Message to handle.
  1209. {
  1210. m_dAcc = 0;
  1211. double dX = m_dX - pexplosionmsg->sX;
  1212. double dZ = m_dZ - pexplosionmsg->sZ;
  1213. double dSqDist = (dX*dX) + (dZ*dZ);
  1214. double dMulVert = 0.7;
  1215. double dMulHorz = 0.2;
  1216. if (dSqDist < 1.0)
  1217. dSqDist = 1.0;
  1218. m_dExtHorzRot = rspATan(pexplosionmsg->sZ - m_dZ, m_dX - pexplosionmsg->sX);
  1219. if (dSqDist <= 900)
  1220. {
  1221. dMulVert = 0.8;
  1222. dMulHorz = 0.3;
  1223. }
  1224. if (dSqDist <= 400)
  1225. {
  1226. dMulVert = 0.9;
  1227. dMulHorz = 0.4;
  1228. }
  1229. if (dSqDist <= 100)
  1230. {
  1231. dMulVert = 1.0;
  1232. dMulHorz = 0.5;
  1233. }
  1234. m_dExtHorzVel = pexplosionmsg->sVelocity * dMulHorz;
  1235. m_dExtVertVel = pexplosionmsg->sVelocity * dMulVert;
  1236. m_dExtHorzDrag = -ms_dDefaultAirDrag;
  1237. // If there's an internal velocity . . .
  1238. if (m_dVel != 0.0)
  1239. {
  1240. m_dDrag = -ms_dDefaultAirDrag;
  1241. }
  1242. }
  1243. ////////////////////////////////////////////////////////////////////////////////
  1244. // Handles a Burn_Message.
  1245. // (virtual).
  1246. ////////////////////////////////////////////////////////////////////////////////
  1247. void CThing3d::OnBurnMsg( // Returns nothing.
  1248. Burn_Message* pburnmsg) // In: Message to handle.
  1249. {
  1250. CFire* pfire;
  1251. // If we don't already have a fire . . .
  1252. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pfire, m_u16IdFire) != 0)
  1253. {
  1254. // Make a fire and remember its ID.
  1255. if (CThing::ConstructWithID(CThing::CFireID, m_pRealm, (CThing**) &pfire) == 0)
  1256. {
  1257. // Store its ID.
  1258. m_u16IdFire = pfire->GetInstanceID();
  1259. // Put it in the thing3d's midsection.
  1260. pfire->Setup(
  1261. m_dX, // In: New x coord
  1262. m_dY + m_sprite.m_sRadius, // In: New y coord
  1263. m_dZ, // In: New z coord
  1264. BURN_DURATION, // In: Number of milliseconds to burn, default 1sec
  1265. false, // In: Use thick fire (more opaque) default = true
  1266. CFire::SmallFire); // In: Animation type to use default = LargeFire
  1267. // We use this object as a weapon against others so we are the shooter.
  1268. pfire->m_u16ShooterID = m_u16InstanceId;
  1269. // Note though who caused the creation of this fire so we know who's
  1270. // responsible for this thing's damage.
  1271. pfire->m_u16FireStarterID = pburnmsg->u16ShooterID;
  1272. pfire->m_bIsBurningDude = (GetClassID() == CDudeID);
  1273. // pfire->MessagesOff();
  1274. }
  1275. }
  1276. }
  1277. ////////////////////////////////////////////////////////////////////////////////
  1278. // Handles an ObjectDelete_Message.
  1279. // (virtual).
  1280. ////////////////////////////////////////////////////////////////////////////////
  1281. void CThing3d::OnDeleteMsg( // Returns nothing.
  1282. ObjectDelete_Message* pdeletemsg) // In: Message to handle.
  1283. {
  1284. }
  1285. ////////////////////////////////////////////////////////////////////////////////
  1286. // Handles a Help_Message
  1287. // (virtual)
  1288. ////////////////////////////////////////////////////////////////////////////////
  1289. void CThing3d::OnHelpMsg( // Returns nothing
  1290. Help_Message* phelpmsg) // In: Message to handle
  1291. {
  1292. }
  1293. ////////////////////////////////////////////////////////////////////////////////
  1294. // Handles a PutMeDown_Message
  1295. // (virtual)
  1296. ////////////////////////////////////////////////////////////////////////////////
  1297. void CThing3d::OnPutMeDownMsg( // Returns nothing
  1298. PutMeDown_Message* pputmedownmsg)
  1299. {
  1300. }
  1301. ////////////////////////////////////////////////////////////////////////////////
  1302. // Update fire animation's position.
  1303. ////////////////////////////////////////////////////////////////////////////////
  1304. void CThing3d::UpdateFirePosition(void)
  1305. {
  1306. CFire* pfire;
  1307. // If there is a fire . . .
  1308. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pfire, m_u16IdFire) == 0)
  1309. {
  1310. // Update its position.
  1311. pfire->m_dX = m_dX;
  1312. pfire->m_dY = m_dY;
  1313. // Always put fire slightly in front of thing3d so we can see alpha
  1314. // effect.
  1315. pfire->m_dZ = m_dZ + 1.0;
  1316. // If dead or dying . . .
  1317. if (m_state == State_Die || m_state == State_Dead)
  1318. {
  1319. // Char the guy.
  1320. m_sBrightness = BURNT_BRIGHTNESS;
  1321. }
  1322. }
  1323. else
  1324. {
  1325. m_u16IdFire = CIdBank::IdNil;
  1326. }
  1327. }
  1328. ////////////////////////////////////////////////////////////////////////////////
  1329. // Get attributes at supplied position (uses m_pap2dAttribCheckPoints).
  1330. ////////////////////////////////////////////////////////////////////////////////
  1331. void CThing3d::GetFloorAttributes( // Returns nothing.
  1332. short sX, // In: X coord.
  1333. short sZ, // In: Z coord.
  1334. U16* pu16Attrib, // Out: Combined attribs, if not NULL.
  1335. short* psHeight) // Out: Max height, if not NULL.
  1336. {
  1337. U16 u16CurAttrib;
  1338. U16 u16CombinedAttrib = 0;
  1339. short sLightTally = 0;
  1340. short sMaxHeight = -32767;
  1341. short sCurHeight;
  1342. const Point2D* p2d;
  1343. for (p2d = m_pap2dAttribCheckPoints; p2d->sX != ATTRIB_CHECK_TERMINATOR; p2d++)
  1344. {
  1345. u16CurAttrib = m_pRealm->GetFloorMapValue((short) sX + p2d->sX,
  1346. (short) sZ + p2d->sZ,
  1347. REALM_ATTR_NOT_WALKABLE | 0x0000);
  1348. // Combine attributes - and the height for now (since it will be masked out at end)
  1349. u16CombinedAttrib |= u16CurAttrib;
  1350. // Get height.
  1351. sCurHeight = short(u16CurAttrib & REALM_ATTR_HEIGHT_MASK);
  1352. if (sCurHeight > sMaxHeight)
  1353. {
  1354. sMaxHeight = sCurHeight;
  1355. }
  1356. }
  1357. // Strip off the height information from the attributes
  1358. u16CombinedAttrib &= REALM_ATTR_FLOOR_MASK;
  1359. // If concerned with the height . . .
  1360. if (psHeight)
  1361. {
  1362. // Map into realm.
  1363. m_pRealm->MapAttribHeight(sMaxHeight * 4, psHeight);
  1364. }
  1365. SET(pu16Attrib, u16CombinedAttrib);
  1366. }
  1367. ////////////////////////////////////////////////////////////////////////////////
  1368. // Get attributes at supplied position (uses m_pap2dAttribCheckPoints).
  1369. ////////////////////////////////////////////////////////////////////////////////
  1370. void CThing3d::GetEffectAttributes( // Returns nothing.
  1371. short sX, // In: X coord.
  1372. short sZ, // In: Z coord.
  1373. U16* pu16Attrib, // Out: Combined attribs, if not NULL.
  1374. short* psLightBits) // Out: Tally of light bits set, if not NULL.
  1375. {
  1376. U16 u16CurAttrib;
  1377. U16 u16CombinedAttrib = 0;
  1378. short sLightTally = 0;
  1379. const Point2D* p2d;
  1380. for (p2d = m_pap2dAttribCheckPoints; p2d->sX != ATTRIB_CHECK_TERMINATOR; p2d++)
  1381. {
  1382. u16CurAttrib = m_pRealm->GetEffectAttribute(
  1383. (short) sX + p2d->sX,
  1384. (short) sZ + p2d->sZ
  1385. );
  1386. // Combine attributes other than height.
  1387. u16CombinedAttrib |= u16CurAttrib;
  1388. // Get light effect
  1389. if (u16CurAttrib & REALM_ATTR_LIGHT_BIT)
  1390. sLightTally++;
  1391. }
  1392. SET(pu16Attrib, u16CombinedAttrib);
  1393. SET(psLightBits, sLightTally);
  1394. }
  1395. ////////////////////////////////////////////////////////////////////////////////
  1396. // Get the layer based on the attribute points array (uses
  1397. // m_pap2dAttribCheckPoints).
  1398. ////////////////////////////////////////////////////////////////////////////////
  1399. void CThing3d::GetLayer( // Returns nothing.
  1400. short sX, // In: X coord.
  1401. short sZ, // In: Z coord.
  1402. short* psLayer) // Out: Combined layer.
  1403. {
  1404. U16 u16CombinedLayer = 0;
  1405. const Point2D* p2d;
  1406. for (p2d = m_pap2dAttribCheckPoints; p2d->sX != ATTRIB_CHECK_TERMINATOR; p2d++)
  1407. {
  1408. u16CombinedLayer |= m_pRealm->GetLayer(
  1409. (short) sX + p2d->sX,
  1410. (short) sZ + p2d->sZ
  1411. );
  1412. }
  1413. SET(psLayer, m_pRealm->GetLayerViaAttrib(u16CombinedLayer) );
  1414. }
  1415. ////////////////////////////////////////////////////////////////////////////////
  1416. // Get the link point specified by the provided transform.
  1417. ////////////////////////////////////////////////////////////////////////////////
  1418. void CThing3d::GetLinkPoint( // Returns nothing.
  1419. RTransform* ptrans, // In: Transform specifying point.
  1420. double* pdX, // Out: Point speicfied.
  1421. double* pdY, // Out: Point speicfied.
  1422. double* pdZ) // Out: Point speicfied.
  1423. {
  1424. // Set up translation based on the combined character and rigid body transforms.
  1425. RTransform transChildAbsolute;
  1426. // Apply child and parent to transChildAbs.
  1427. transChildAbsolute.Mul(m_sprite.m_ptrans->T, ptrans->T);
  1428. // Set up pt at origin for weapon.
  1429. RP3d pt3Src = {0, 0, 0, 1};
  1430. RP3d pt3Dst;
  1431. // Get last transition position by mapping origin.
  1432. m_pRealm->m_scene.TransformPtsToRealm(&transChildAbsolute, &pt3Src, &pt3Dst, 1);
  1433. // Output link point.
  1434. *pdX = pt3Dst.x;
  1435. *pdY = pt3Dst.y;
  1436. *pdZ = pt3Dst.z;
  1437. }
  1438. ////////////////////////////////////////////////////////////////////////////////
  1439. // Detach the specified Thing3d.
  1440. // (virtual).
  1441. ////////////////////////////////////////////////////////////////////////////////
  1442. CThing3d* CThing3d::DetachChild( // Returns ptr to the child or NULL, if none.
  1443. U16* pu16InstanceId, // In: Instance ID of child to detach.
  1444. // Out: CIdBank::IdNil.
  1445. RTransform* ptrans) // In: Transform for positioning child.
  1446. {
  1447. CThing3d* pthing3d;
  1448. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pthing3d, *pu16InstanceId) == 0)
  1449. {
  1450. DetachChild(
  1451. &(pthing3d->m_sprite), // In: Child sprite to detach.
  1452. ptrans, // In: Transform for positioning child.
  1453. &(pthing3d->m_dX), // Out: Position of child.
  1454. &(pthing3d->m_dY), // Out: Position of child.
  1455. &(pthing3d->m_dZ) ); // Out: Position of child.
  1456. // Done with child.
  1457. *pu16InstanceId = CIdBank::IdNil;
  1458. // Child is done with us.
  1459. pthing3d->m_u16IdParent = CIdBank::IdNil;
  1460. }
  1461. else
  1462. {
  1463. // No longer exists.
  1464. *pu16InstanceId = CIdBank::IdNil;
  1465. pthing3d = NULL;
  1466. }
  1467. return pthing3d;
  1468. }
  1469. ////////////////////////////////////////////////////////////////////////////////
  1470. // Detach the specified child sprite (can be any sprite type).
  1471. // (virtual)
  1472. ////////////////////////////////////////////////////////////////////////////////
  1473. void CThing3d::DetachChild( // Returns nothing.
  1474. CSprite* psprite, // In: Child sprite to detach.
  1475. RTransform* ptrans, // In: Transform for positioning child.
  1476. double* pdX, // Out: New position of child.
  1477. double* pdY, // Out: New position of child.
  1478. double* pdZ) // Out: New position of child.
  1479. {
  1480. // Get the link point via the transform.
  1481. GetLinkPoint(ptrans, pdX, pdY, pdZ);
  1482. // Set child position to character's position offset by rigid body's realm offset.
  1483. *pdX += m_dX;
  1484. *pdY += m_dY;
  1485. *pdZ += m_dZ;
  1486. // Detatch child's sprite
  1487. m_sprite.RemoveChild(psprite);
  1488. }
  1489. ////////////////////////////////////////////////////////////////////////////////
  1490. // Position the specified child sprite (can be any sprite type).
  1491. // (virtual)
  1492. ////////////////////////////////////////////////////////////////////////////////
  1493. void CThing3d::PositionChild( // Returns nothing.
  1494. CSprite* psprite, // In: Child sprite to position.
  1495. RTransform* ptrans, // In: Transform for positioning child.
  1496. double* pdX, // Out: New position of child.
  1497. double* pdY, // Out: New position of child.
  1498. double* pdZ) // Out: New position of child.
  1499. {
  1500. switch (psprite->GetType())
  1501. {
  1502. case CSprite::Standard3d:
  1503. // Set transform from our rigid body transfanimation for the child
  1504. // sprite.
  1505. ((CSprite3*)psprite)->m_ptrans = ptrans;
  1506. break;
  1507. case CSprite::Standard2d: // This only works for 1st level children.
  1508. {
  1509. // NOTE: This relies on the child having its Render() call after
  1510. // this object's Render() call.
  1511. // Since we know this object allocated the child (if we know that (it
  1512. // is true in the case of a weapon)), we know that this object was
  1513. // allocated first. If it was allocated first, it was added to the
  1514. // realm list first. Since the realm always adds to the end of its
  1515. // list, we know this object's Render() will occur before the child's.
  1516. // It's reliable enough for me, barely.
  1517. // In the case that the Render() for the child occurs before this
  1518. // object's, the child will lag by, at most, 1 frame.
  1519. // Convert the rigid body transform to some relative coordinates.
  1520. // Note that we don't need to try to extract the rotations from the
  1521. // transform since CSprite2's cannot rotate currently.
  1522. GetLinkPoint(ptrans, pdX, pdY, pdZ);
  1523. break;
  1524. }
  1525. default:
  1526. TRACE("PositionChild(): Don't know how to handle this child's sprite type.\n");
  1527. break;
  1528. }
  1529. }
  1530. ////////////////////////////////////////////////////////////////////////////////
  1531. // Add a force vector to this thing's external horizontal velocity vector.
  1532. ////////////////////////////////////////////////////////////////////////////////
  1533. void CThing3d::AddForceVector( // Returns nothing.
  1534. double dAddVel, // In: Magnitude of additional vector.
  1535. short sRot) // In: Direction (in degrees) of additional vector.
  1536. {
  1537. // Maybe we should make this an inline to add two vectors so we can use
  1538. // this in other places.
  1539. short sAddAngle = rspMod360(sRot);
  1540. short sCurAngle = rspMod360(m_dExtHorzRot);
  1541. // Get our current X and Z component vectors.
  1542. double dCurVectX = COSQ[sCurAngle] * m_dExtHorzVel;
  1543. double dCurVectZ = -SINQ[sCurAngle] * m_dExtHorzVel;
  1544. double dAddVectX = COSQ[sAddAngle] * dAddVel;
  1545. double dAddVectZ = -SINQ[sAddAngle] * dAddVel;
  1546. // Determine new combined component vectors.
  1547. dCurVectX += dAddVectX;
  1548. dCurVectZ += dAddVectZ;
  1549. // Determine new vector and angle.
  1550. m_dExtHorzVel = rspSqrt(ABS2(dCurVectX, dCurVectZ) );
  1551. m_dExtHorzRot = rspATan(-dCurVectZ, dCurVectX);
  1552. // Limit the magnitude of the vector.
  1553. if (m_dExtHorzVel > 0.0)
  1554. {
  1555. if (m_dExtHorzVel > MaxForeVel)
  1556. m_dExtHorzVel = MaxForeVel;
  1557. }
  1558. else
  1559. {
  1560. if (m_dExtHorzVel < MaxBackVel)
  1561. m_dExtHorzVel = MaxBackVel;
  1562. }
  1563. // Make sure there's some surface drag, if there's a force.
  1564. if (m_dExtHorzDrag == 0.0 && m_dExtHorzVel != 0.0)
  1565. {
  1566. // If above the terrain . . .
  1567. if (m_bAboveTerrain == true)
  1568. {
  1569. m_dExtHorzDrag = -ms_dDefaultAirDrag;
  1570. }
  1571. else
  1572. {
  1573. m_dExtHorzDrag = -ms_dDefaultSurfaceDrag;
  1574. }
  1575. // If vector is negative magnitude for some reason . . .
  1576. if (m_dExtHorzVel < 0.0)
  1577. {
  1578. // Negate the drag.
  1579. m_dExtHorzDrag = -m_dExtHorzDrag;
  1580. }
  1581. }
  1582. }
  1583. ////////////////////////////////////////////////////////////////////////////////
  1584. // PrepareShadow
  1585. ////////////////////////////////////////////////////////////////////////////////
  1586. short CThing3d::PrepareShadow(void)
  1587. {
  1588. short sResult = SUCCESS;
  1589. // If the shadow doesn't have resource loaded yet, load the default
  1590. if (m_spriteShadow.m_pImage == NULL)
  1591. {
  1592. sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(SHADOW_FILE), &(m_spriteShadow.m_pImage), RFile::LittleEndian);
  1593. }
  1594. // If a resource is available, set the shadow to visible.
  1595. if (sResult == SUCCESS)
  1596. m_spriteShadow.m_sInFlags &= ~CSprite::InHidden;
  1597. return sResult;
  1598. }
  1599. ////////////////////////////////////////////////////////////////////////////////
  1600. // Plays a sample and purges the resource after playing (as long as nobody
  1601. // else has used the same sample resource).
  1602. // Plays a sample with volume adjustment. This may require load from disk.
  1603. // Also, if no initial volume is specified, the distance to the ear is used.
  1604. ////////////////////////////////////////////////////////////////////////////////
  1605. void CThing3d::PlaySample( // Returns nothing.
  1606. // Does not fail.
  1607. SampleMasterID id, // In: Identifier of sample you want played.
  1608. SampleMaster::SoundCategory eType, // In: Sound Volume Category for user adjustment
  1609. short sInitialVolume /*= -1*/, // In: Initial Sound Volume (0 - 255)
  1610. // Negative indicates to use the distance to the
  1611. // ear to determine the volume.
  1612. SampleMaster::SoundInstance* psi /*= NULL*/, // Out: Handle for adjusting sound volume
  1613. long* plSampleDuration /*= NULL*/, // Out: Sample duration in ms, if not NULL.
  1614. long lLoopStartTime /*= -1*/, // In: Where to loop back to in milliseconds.
  1615. // -1 indicates no looping (unless m_sLoop is
  1616. // explicitly set).
  1617. long lLoopEndTime /*= 0*/, // In: Where to loop back from in milliseconds.
  1618. // In: If less than 1, the end + lLoopEndTime is used.
  1619. bool bPurgeSample /*= false*/) // In: Call ReleaseAndPurge rather than Release after playing
  1620. {
  1621. // If negative volume . . .
  1622. if (sInitialVolume < 0)
  1623. {
  1624. // Determine volume based on distance to ear.
  1625. sInitialVolume = DistanceToVolume(m_dX, m_dY, m_dZ, SoundHalfLife);
  1626. }
  1627. ::PlaySample( // Returns nothing.
  1628. // Does not fail.
  1629. id, // In: Identifier of sample you want played.
  1630. eType, // In: Sound Volume Category for user adjustment
  1631. sInitialVolume, // In: Initial Sound Volume (0 - 255)
  1632. // Negative indicates to use the distance to the
  1633. // ear to determine the volume.
  1634. psi, // Out: Handle for adjusting sound volume
  1635. plSampleDuration, // Out: Sample duration in ms, if not NULL.
  1636. lLoopStartTime, // In: Where to loop back to in milliseconds.
  1637. // -1 indicates no looping (unless m_sLoop is
  1638. // explicitly set).
  1639. lLoopEndTime, // In: Where to loop back from in milliseconds.
  1640. // In: If less than 1, the end + lLoopEndTime is used.
  1641. bPurgeSample); // In: Call ReleaseAndPurge rather than Release after playing
  1642. }
  1643. ////////////////////////////////////////////////////////////////////////////////
  1644. // Start a CAnimThing.
  1645. ////////////////////////////////////////////////////////////////////////////////
  1646. CAnimThing* CThing3d::StartAnim( // Returns ptr to CAnimThing on success; NULL otherwise.
  1647. char* pszAnimResName, // In: Animation's resource name.
  1648. short sX, // In: Position.
  1649. short sY, // In: Position.
  1650. short sZ, // In: Position.
  1651. bool bLoop) // In: true to loop animation.
  1652. {
  1653. // Create the animator . . .
  1654. CAnimThing* pat = NULL;
  1655. if (ConstructWithID(CAnimThingID, m_pRealm, (CThing**)&pat) == 0)
  1656. {
  1657. strcpy(pat->m_szResName, pszAnimResName);
  1658. // Start it up:
  1659. // No looping.
  1660. pat->m_sLoop = (bLoop == true) ? TRUE : FALSE;
  1661. // No notification necessary.
  1662. pat->Setup(sX, sY, sZ);
  1663. }
  1664. else
  1665. {
  1666. TRACE("StartAnim(): Failed to construct new CAnimThing.\n");
  1667. }
  1668. return pat;
  1669. }
  1670. ////////////////////////////////////////////////////////////////////////////////
  1671. // EOF
  1672. ////////////////////////////////////////////////////////////////////////////////