gameobj.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  1. //***************************************************************************
  2. //
  3. // gameobj.cpp -- File contains the Game Object class code
  4. //
  5. // MechCommander 2
  6. //
  7. //---------------------------------------------------------------------------//
  8. // Copyright (C) Microsoft Corporation. All rights reserved. //
  9. //===========================================================================//
  10. #ifndef MCLIB_H
  11. #include "mclib.h"
  12. #endif
  13. #ifndef GAMEOBJ_H
  14. #include "gameobj.h"
  15. #endif
  16. #ifndef CARNAGE_H
  17. #include "carnage.h"
  18. #endif
  19. #ifndef TEAM_H
  20. #include "team.h"
  21. #endif
  22. #ifndef OBJTYPE_H
  23. #include "objtype.h"
  24. #endif
  25. #ifndef OBJMGR_H
  26. #include "objmgr.h"
  27. #endif
  28. #ifndef DOBJNUM_H
  29. #include "dobjnum.h"
  30. #endif
  31. #ifndef MOVE_H
  32. #include "move.h"
  33. #endif
  34. #ifndef CMPONENT_H
  35. #include "cmponent.h"
  36. #endif
  37. #ifndef CONTACT_H
  38. #include "contact.h"
  39. #endif
  40. #ifndef UNITDESG_H
  41. #include "unitdesg.h"
  42. #endif
  43. #ifndef MULTPLYR_H
  44. #include "multplyr.h"
  45. #endif
  46. #ifndef MOVER_H
  47. #include "mover.h"
  48. #endif
  49. #ifndef TURRET_H
  50. #include "turret.h"
  51. #endif
  52. #ifndef GAMELOG_H
  53. #include "gamelog.h"
  54. #endif
  55. #ifndef COMNDR_H
  56. #include "comndr.h"
  57. #endif
  58. //---------------------------------------------------------------------------
  59. extern GameLog* CombatLog;
  60. extern MissionMapPtr GameMap;
  61. extern float metersPerWorldUnit;
  62. extern float worldUnitsPerMeter;
  63. extern TeamPtr homeTeam;
  64. extern float MechClassWeights[NUM_MECH_CLASSES];
  65. extern char* ExceptionGameMsg;
  66. char ChunkDebugMsg[5120];
  67. unsigned long GameObject::spanMask = 0;
  68. float GameObject::blockCaptureRange = 0.0;
  69. bool GameObject::initialize = false;
  70. extern float maxVisualRange;
  71. extern long visualRangeTable[];
  72. float applyDifficultyWeapon (float dmg, bool isPlayer);
  73. //***************************************************************************
  74. // WEAPONFIRECHUNK class
  75. //***************************************************************************
  76. #define WEAPONFIRECHUNK_HIT_BITS 1
  77. #define WEAPONFIRECHUNK_WEAPON_BITS 5
  78. #define WEAPONFIRECHUNK_TARGETTYPE_BITS 2
  79. #define WEAPONFIRECHUNK_ENTRYQUAD_BITS 2
  80. #define WEAPONFIRECHUNK_HITLOCATION_BITS 4
  81. #define WEAPONFIRECHUNK_MOVERINDEX_BITS 7
  82. #define WEAPONFIRECHUNK_CELLPOS_BITS 10
  83. #define WEAPONFIRECHUNK_MISSILES_BITS 4
  84. #define WEAPONFIRECHUNK_TARGETID_BITS 20
  85. #define WEAPONFIRECHUNK_SPECIALTYPE_BITS 2
  86. #define WEAPONFIRECHUNK_SPECIALID_BITS 8
  87. #define WEAPONFIRECHUNK_HIT_MASK 0x00000001
  88. #define WEAPONFIRECHUNK_WEAPON_MASK 0x0000001F
  89. #define WEAPONFIRECHUNK_TARGETTYPE_MASK 0x00000003
  90. #define WEAPONFIRECHUNK_ENTRYQUAD_MASK 0x00000003
  91. #define WEAPONFIRECHUNK_HITLOCATION_MASK 0x0000000F
  92. #define WEAPONFIRECHUNK_MOVERINDEX_MASK 0x0000007F
  93. #define WEAPONFIRECHUNK_TERRAINBLOCK_MASK 0x000000FF
  94. #define WEAPONFIRECHUNK_TERRAINVERTEX_MASK 0x000001FF
  95. #define WEAPONFIRECHUNK_TERRAINITEM_MASK 0x00000007
  96. #define WEAPONFIRECHUNK_TRAIN_MASK 0x000000FF
  97. #define WEAPONFIRECHUNK_TRAINCAR_MASK 0x000000FF
  98. #define WEAPONFIRECHUNK_CELLPOS_MASK 0x000003FF
  99. #define WEAPONFIRECHUNK_MISSILES_MASK 0x0000000F
  100. #define WEAPONFIRECHUNK_TARGETID_MASK 0x000FFFFF
  101. #define WEAPONFIRECHUNK_SPECIALTYPE_MASK 0x00000003
  102. #define WEAPONFIRECHUNK_SPECIALID_MASK 0x000000FF
  103. #define WEAPONFIRECHUNK_TARGET_MOVER 0
  104. #define WEAPONFIRECHUNK_TARGET_TERRAIN 1
  105. #define WEAPONFIRECHUNK_TARGET_SPECIAL 2
  106. #define WEAPONFIRECHUNK_TARGET_LOCATION 3
  107. #define WEAPONFIRECHUNK_SPECIAL_CAMERADRONE 0
  108. //---------------------------------------------------------------------------
  109. void WeaponShotInfo::init (GameObjectWatchID _attackerWID, long _masterId, float _damage, long _hitLocation, float _entryAngle) {
  110. attackerWID = _attackerWID;
  111. masterId = _masterId;
  112. damage = _damage;
  113. hitLocation = _hitLocation;
  114. entryAngle = _entryAngle;
  115. if (!MPlayer && _attackerWID) //No Multiplayer skill levels
  116. {
  117. //---------------------------------------------------
  118. // SKill Levels -- Need Attacker Alignment and Class
  119. GameObjectPtr _attacker = ObjectManager->getByWatchID(_attackerWID);
  120. if (_attacker)
  121. {
  122. ObjectClass objClass = _attacker->getObjectClass();
  123. long commanderId = _attacker->getCommanderId();
  124. bool isPlayer = commanderId == Commander::home->getId();
  125. if (!isPlayer) //Only enemy Mechs, vehicles, elementals and turrets get modified
  126. {
  127. if ((objClass == BATTLEMECH) ||
  128. (objClass == GROUNDVEHICLE) ||
  129. (objClass == TURRET))
  130. {
  131. damage = applyDifficultyWeapon(_damage,isPlayer);
  132. }
  133. }
  134. else
  135. {
  136. damage = applyDifficultyWeapon(_damage,isPlayer);
  137. }
  138. }
  139. }
  140. Assert((damage >= 0.0) && (damage <= 255.0), (long)damage, " WeaponShotInfo.init: damage out of range ");
  141. if (MPlayer && MPlayer->isServer()) {
  142. damage = (float)((unsigned long)(damage * 4.0)) * 0.25;
  143. if ((entryAngle >= -45.0) && (entryAngle <= 45.0))
  144. entryAngle = 0.0; //MECH_HIT_ARC_FRONT;
  145. else if ((entryAngle > -135.0) && (entryAngle < -45.0))
  146. entryAngle = -90.0; //MECH_HIT_ARC_LEFT;
  147. else if ((entryAngle > 45.0) && (_entryAngle < 135))
  148. entryAngle = 90.0; //MECH_HIT_ARC_RIGHT;
  149. else
  150. entryAngle = 180.0; //MECH_HIT_ARC_REAR;
  151. }
  152. }
  153. //---------------------------------------------------------------------------
  154. void WeaponShotInfo::setDamage (float _damage) {
  155. damage = _damage;
  156. if (MPlayer && MPlayer->isServer())
  157. damage = (float)((unsigned long)(damage * 4.0)) * 0.25;
  158. }
  159. //---------------------------------------------------------------------------
  160. void WeaponShotInfo::setEntryAngle (float _entryAngle) {
  161. entryAngle = _entryAngle;
  162. if (MPlayer && MPlayer->isServer()) {
  163. if ((entryAngle >= -45.0) && (entryAngle <= 45.0))
  164. entryAngle = 0.0; //MECH_HIT_ARC_FRONT;
  165. else if ((entryAngle > -135.0) && (entryAngle < -45.0))
  166. entryAngle = -90.0; //MECH_HIT_ARC_LEFT;
  167. else if ((entryAngle > 45.0) && (_entryAngle < 135))
  168. entryAngle = 90.0; //MECH_HIT_ARC_RIGHT;
  169. else
  170. entryAngle = 180.0; //MECH_HIT_ARC_REAR;
  171. }
  172. }
  173. //---------------------------------------------------------------------------
  174. void* WeaponFireChunk::operator new (size_t ourSize) {
  175. void* result;
  176. result = systemHeap->Malloc(ourSize);
  177. return(result);
  178. }
  179. //---------------------------------------------------------------------------
  180. void WeaponFireChunk::operator delete (void* us) {
  181. systemHeap->Free(us);
  182. }
  183. //---------------------------------------------------------------------------
  184. void DebugWeaponFireChunk (WeaponFireChunkPtr chunk1, WeaponFireChunkPtr chunk2, GameObjectPtr attacker) {
  185. ChunkDebugMsg[0] = NULL;
  186. char outString[512];
  187. if (attacker) {
  188. if (attacker->isMover()) {
  189. sprintf(outString, "attacker = %s (%d)\n", attacker->getName(), attacker->getPartId());
  190. strcat(ChunkDebugMsg, outString);
  191. }
  192. else {
  193. sprintf(outString, "attacker = objClass %d (%d)\n", attacker->getObjectClass(), attacker->getPartId());
  194. strcat(ChunkDebugMsg, outString);
  195. }
  196. }
  197. else
  198. strcat(ChunkDebugMsg, "attacker = ???\n");
  199. if (chunk1) {
  200. strcat(ChunkDebugMsg, "\nCHUNK1\n");
  201. GameObjectPtr target = NULL;
  202. Stuff::Vector3D targetPoint;
  203. targetPoint.Zero();
  204. bool isTargetPoint = false;
  205. if (chunk1->targetType == WEAPONFIRECHUNK_TARGET_MOVER)
  206. target = (GameObjectPtr)MPlayer->moverRoster[chunk1->targetId];
  207. else if (chunk1->targetType == WEAPONFIRECHUNK_TARGET_TERRAIN)
  208. target = ObjectManager->findByPartId(chunk1->targetId);
  209. else if (chunk1->targetType == WEAPONFIRECHUNK_TARGET_SPECIAL)
  210. target = ObjectManager->findByPartId(chunk1->targetId);
  211. else if (chunk1->targetType == WEAPONFIRECHUNK_TARGET_LOCATION) {
  212. targetPoint.x = (float)chunk1->targetCell[1] * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  213. targetPoint.y = (Terrain::worldUnitsMapSide / 2) - ((float)chunk1->targetCell[0] * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  214. targetPoint.z = (float)land->getTerrainElevation(targetPoint);
  215. isTargetPoint = true;
  216. }
  217. if (target) {
  218. if (target->isMover()) {
  219. sprintf(outString, "target = %s (%d)\n", target->getName(), target->getPartId());
  220. strcat(ChunkDebugMsg, outString);
  221. }
  222. else {
  223. sprintf(outString, "target = objClass %d (%d)\n", target->getObjectClass(), target->getPartId());
  224. strcat(ChunkDebugMsg, outString);
  225. }
  226. }
  227. else if (isTargetPoint) {
  228. sprintf(outString, "target point = (%f, %f, %f)\n", targetPoint.x, targetPoint.y, targetPoint.z);
  229. strcat(ChunkDebugMsg, outString);
  230. }
  231. else {
  232. strcat(ChunkDebugMsg, "target = ???\n");
  233. }
  234. sprintf(outString, "targetType = %d\n", chunk1->targetType);
  235. strcat(ChunkDebugMsg, outString);
  236. sprintf(outString, "targetId = %d\n", chunk1->targetId);
  237. strcat(ChunkDebugMsg, outString);
  238. sprintf(outString, "specialType = %d\n", chunk1->specialType);
  239. strcat(ChunkDebugMsg, outString);
  240. sprintf(outString, "specialId = %d\n", chunk1->specialId);
  241. strcat(ChunkDebugMsg, outString);
  242. sprintf(outString, "targetCell = [%d, %d]\n", chunk1->targetCell[0], chunk1->targetCell[1]);
  243. strcat(ChunkDebugMsg, outString);
  244. sprintf(outString, "weaponIndex = %d\n", chunk1->weaponIndex);
  245. strcat(ChunkDebugMsg, outString);
  246. sprintf(outString, "hit = %c\n", chunk1->hit ? 'T' : 'N');
  247. strcat(ChunkDebugMsg, outString);
  248. sprintf(outString, "entryAngle = %d\n", chunk1->entryAngle);
  249. strcat(ChunkDebugMsg, outString);
  250. sprintf(outString, "numMissiles = %d\n", chunk1->numMissiles);
  251. strcat(ChunkDebugMsg, outString);
  252. sprintf(outString, "hitLocation = %d\n", chunk1->hitLocation);
  253. strcat(ChunkDebugMsg, outString);
  254. sprintf(outString, "data = %x\n", chunk1->data);
  255. strcat(ChunkDebugMsg, outString);
  256. }
  257. if (chunk2) {
  258. strcat(ChunkDebugMsg, "\nCHUNK2\n");
  259. GameObjectPtr target = NULL;
  260. Stuff::Vector3D targetPoint;
  261. targetPoint.Zero();
  262. bool isTargetPoint = false;
  263. if (chunk2->targetType == WEAPONFIRECHUNK_TARGET_MOVER)
  264. target = (GameObjectPtr)MPlayer->moverRoster[chunk2->targetId];
  265. else if (chunk2->targetType == WEAPONFIRECHUNK_TARGET_TERRAIN)
  266. target = ObjectManager->findByPartId(chunk2->targetId);
  267. else if (chunk2->targetType == WEAPONFIRECHUNK_TARGET_SPECIAL)
  268. target = ObjectManager->findByPartId(chunk2->targetId);
  269. else if (chunk2->targetType == WEAPONFIRECHUNK_TARGET_LOCATION) {
  270. targetPoint.x = (float)chunk2->targetCell[1] * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  271. targetPoint.y = (Terrain::worldUnitsMapSide / 2) - ((float)chunk2->targetCell[0] * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  272. targetPoint.z = (float)land->getTerrainElevation(targetPoint);
  273. isTargetPoint = true;
  274. }
  275. if (target) {
  276. if (target->isMover()) {
  277. sprintf(outString, "target = %s (%d)\n", target->getName(), target->getPartId());
  278. strcat(ChunkDebugMsg, outString);
  279. }
  280. else {
  281. sprintf(outString, "target = objClass %d (%d)\n", target->getObjectClass(), target->getPartId());
  282. strcat(ChunkDebugMsg, outString);
  283. }
  284. }
  285. else if (isTargetPoint) {
  286. sprintf(outString, "target point = (%f, %f, %f)\n", targetPoint.x, targetPoint.y, targetPoint.z);
  287. strcat(ChunkDebugMsg, outString);
  288. }
  289. else {
  290. strcat(ChunkDebugMsg, "target = ???\n");
  291. }
  292. sprintf(outString, "targetType = %d\n", chunk2->targetType);
  293. strcat(ChunkDebugMsg, outString);
  294. sprintf(outString, "targetId = %d\n", chunk2->targetId);
  295. strcat(ChunkDebugMsg, outString);
  296. sprintf(outString, "specialType = %d\n", chunk2->specialType);
  297. strcat(ChunkDebugMsg, outString);
  298. sprintf(outString, "specialId = %d\n", chunk2->specialId);
  299. strcat(ChunkDebugMsg, outString);
  300. sprintf(outString, "targetCell = [%d, %d]\n", chunk2->targetCell[0], chunk1->targetCell[1]);
  301. strcat(ChunkDebugMsg, outString);
  302. sprintf(outString, "weaponIndex = %d\n", chunk2->weaponIndex);
  303. strcat(ChunkDebugMsg, outString);
  304. sprintf(outString, "hit = %c\n", chunk2->hit ? 'T' : 'N');
  305. strcat(ChunkDebugMsg, outString);
  306. sprintf(outString, "entryAngle = %d\n", chunk2->entryAngle);
  307. strcat(ChunkDebugMsg, outString);
  308. sprintf(outString, "numMissiles = %d\n", chunk2->numMissiles);
  309. strcat(ChunkDebugMsg, outString);
  310. sprintf(outString, "hitLocation = %d\n", chunk2->hitLocation);
  311. strcat(ChunkDebugMsg, outString);
  312. sprintf(outString, "data = %x\n", chunk2->data);
  313. strcat(ChunkDebugMsg, outString);
  314. }
  315. File* debugFile = new File;
  316. debugFile->create("wfchunk.dbg");
  317. debugFile->writeString(ChunkDebugMsg);
  318. debugFile->close();
  319. delete debugFile;
  320. debugFile = NULL;
  321. ExceptionGameMsg = ChunkDebugMsg;
  322. }
  323. //---------------------------------------------------------------------------
  324. #ifdef _DEBUG
  325. #define LOGWEAPONFIRECHUNKS
  326. #define WEAPONFIRELOG_SIZE 256
  327. long NumWeaponFiresInLog = 0;
  328. long NumWeaponFiresInLogQueue = 0;
  329. GameObjectPtr WeaponFireAttackerLog[WEAPONFIRELOG_SIZE];
  330. GameObjectPtr WeaponFireTargetLog[WEAPONFIRELOG_SIZE];
  331. unsigned long WeaponFireDataLog[WEAPONFIRELOG_SIZE];
  332. File* WeaponFireLog = NULL;
  333. #endif
  334. //---------------------------------------------------------------------------
  335. #ifdef LOGWEAPONFIRECHUNKS
  336. void DumpWeaponFireLog (void) {
  337. //----------------
  338. // Dump to file...
  339. for (long i = 0; i < NumWeaponFiresInLogQueue; i++) {
  340. GameObjectPtr attacker = WeaponFireAttackerLog[i];
  341. GameObjectPtr target = WeaponFireTargetLog[i];
  342. unsigned long data = WeaponFireDataLog[i];
  343. WeaponFireChunk chunk;
  344. chunk.init();
  345. chunk.data = data;
  346. chunk.unpack(attacker);
  347. char s[512];
  348. char attackerName[128];
  349. if (attacker->isMover())
  350. sprintf(attackerName, "%s (%d)", attacker->getName(), attacker->getPartId());
  351. else
  352. sprintf(attackerName, "objClass %d (%d)", attacker->getObjectClass(), attacker->getPartId());
  353. char targetName[128];
  354. if (target)
  355. if (target->isMover())
  356. sprintf(targetName, "%s (%d)", target->getName(), target->getPartId());
  357. else
  358. sprintf(targetName, "objClass %d (%d)", target->getObjectClass(), target->getPartId());
  359. else
  360. sprintf(targetName, "NA/LOCATION");
  361. sprintf(s, "attacker = %s, target = %s, data = %x, hit = %d, numMissiles = %d\n", attackerName, targetName, data, chunk.hit, chunk.numMissiles);
  362. WeaponFireLog->writeString(s);
  363. }
  364. NumWeaponFiresInLogQueue = 0;
  365. }
  366. //---------------------------------------------------------------------------
  367. void CloseWeaponFireLog (void) {
  368. if (WeaponFireLog) {
  369. DumpWeaponFireLog();
  370. char s[512];
  371. sprintf(s, "\nNum Total WeaponFires = %d\n", NumWeaponFiresInLog);
  372. WeaponFireLog->writeString(s);
  373. WeaponFireLog->close();
  374. delete WeaponFireLog;
  375. WeaponFireLog = NULL;
  376. NumWeaponFiresInLog = 0;
  377. NumWeaponFiresInLogQueue = 0;
  378. }
  379. }
  380. #endif
  381. //---------------------------------------------------------------------------
  382. void OpenWeaponFireLog (void) {
  383. #ifdef LOGWEAPONFIRECHUNKS
  384. if (WeaponFireLog)
  385. CloseWeaponFireLog();
  386. NumWeaponFiresInLog = 0;
  387. NumWeaponFiresInLogQueue = 0;
  388. WeaponFireLog = new File;
  389. if (!WeaponFireLog)
  390. Fatal(0, " unable to malloc WeaponFireLog ");
  391. if (WeaponFireLog->create("wf.log") != NO_ERR)
  392. Fatal(0, " unable to create WeaponFireLog ");
  393. #endif
  394. }
  395. //---------------------------------------------------------------------------
  396. void LogWeaponFireChunk (WeaponFireChunkPtr chunk, GameObjectPtr attacker, GameObjectPtr target) {
  397. #ifdef LOGWEAPONFIRECHUNKS
  398. if (!WeaponFireLog)
  399. return;
  400. if (NumWeaponFiresInLogQueue == WEAPONFIRELOG_SIZE)
  401. DumpWeaponFireLog();
  402. WeaponFireAttackerLog[NumWeaponFiresInLogQueue] = attacker;
  403. WeaponFireTargetLog[NumWeaponFiresInLogQueue] = target;
  404. WeaponFireDataLog[NumWeaponFiresInLogQueue] = chunk->data;
  405. NumWeaponFiresInLog++;
  406. NumWeaponFiresInLogQueue++;
  407. #endif
  408. }
  409. //---------------------------------------------------------------------------
  410. void WeaponFireChunk::buildMoverTarget (GameObjectPtr target,
  411. long _weaponIndex,
  412. bool _hit,
  413. float _entryAngle,
  414. long _numMissiles,
  415. long _hitLocation) {
  416. targetType = WEAPONFIRECHUNK_TARGET_MOVER;
  417. targetId = ((MoverPtr)target)->getNetRosterIndex();
  418. weaponIndex = _weaponIndex;
  419. hit = _hit;
  420. if ((_entryAngle >= -45.0) && (_entryAngle <= 45.0))
  421. entryAngle = 0; //MECH_HIT_ARC_FRONT;
  422. else if ((_entryAngle > -135.0) && (_entryAngle < -45.0))
  423. entryAngle = 2; //MECH_HIT_ARC_LEFT;
  424. else if ((_entryAngle > 45.0) && (_entryAngle < 135))
  425. entryAngle = 3; //MECH_HIT_ARC_RIGHT;
  426. else
  427. entryAngle = 1; //MECH_HIT_ARC_REAR;
  428. numMissiles = _numMissiles;
  429. hitLocation = _hitLocation;
  430. Assert((targetId > -1) && (targetId < MAX_MULTIPLAYER_MOVERS), targetId, " WeaponFireChunk.buildMoverTarget: bad targetId ");
  431. Assert((weaponIndex > -1) && (weaponIndex < 32), weaponIndex, " WeaponFireChunk.buildMoverTarget: bad weaponIndex ");
  432. Assert((numMissiles > -1) && (numMissiles < 16), numMissiles, " WeaponFireChunk.buildMoverTarget: bad numMissiles ");
  433. Assert((hitLocation > -2) && (hitLocation < 12), hitLocation, " WeaponFireChunk.buildMoverTarget: bad hitLocation ");
  434. data = 0;
  435. }
  436. //---------------------------------------------------------------------------
  437. void WeaponFireChunk::buildTerrainTarget (GameObjectPtr target,
  438. long _weaponIndex,
  439. bool _hit,
  440. long _numMissiles) {
  441. targetType = WEAPONFIRECHUNK_TARGET_TERRAIN;
  442. targetId = target->getPartId();
  443. //target->getCellPosition(targetCell[0], targetCell[1]);
  444. weaponIndex = _weaponIndex;
  445. hit = _hit;
  446. numMissiles = _numMissiles;
  447. Assert(target->getPartId() != -1, target->getObjectClass(), " WeaponFireChunk.buildTerrainTarget: -1 partId ");
  448. data = 0;
  449. }
  450. //---------------------------------------------------------------------------
  451. void WeaponFireChunk::buildCameraDroneTarget (GameObjectPtr target,
  452. long _weaponIndex,
  453. bool _hit,
  454. float _entryAngle,
  455. long _numMissiles) {
  456. targetType = WEAPONFIRECHUNK_TARGET_SPECIAL;
  457. targetId = target->getPartId();
  458. targetCell[0] = 128;
  459. targetCell[1] = targetId - MIN_CAMERA_DRONE_ID;
  460. weaponIndex = _weaponIndex;
  461. hit = _hit;
  462. if ((_entryAngle >= -45.0) && (_entryAngle <= 45.0))
  463. entryAngle = 0; //MECH_HIT_ARC_FRONT;
  464. else if ((_entryAngle > -135.0) && (_entryAngle < -45.0))
  465. entryAngle = 2; //MECH_HIT_ARC_LEFT;
  466. else if ((_entryAngle > 45.0) && (_entryAngle < 135))
  467. entryAngle = 3; //MECH_HIT_ARC_RIGHT;
  468. else
  469. entryAngle = 1; //MECH_HIT_ARC_REAR;
  470. numMissiles = _numMissiles;
  471. data = 0;
  472. }
  473. //---------------------------------------------------------------------------
  474. void WeaponFireChunk::buildLocationTarget (Stuff::Vector3D location,
  475. long _weaponIndex,
  476. bool _hit,
  477. long _numMissiles) {
  478. targetType = WEAPONFIRECHUNK_TARGET_LOCATION;
  479. land->worldToCell(location, targetCell[0], targetCell[1]);
  480. weaponIndex = _weaponIndex;
  481. hit = _hit;
  482. numMissiles = _numMissiles;
  483. data = 0;
  484. }
  485. //---------------------------------------------------------------------------
  486. #ifdef _DEBUG
  487. #define DEBUG_WEAPONFIRECHUNK
  488. #endif
  489. void WeaponFireChunk::pack (GameObjectPtr attacker) {
  490. data = 0;
  491. switch (targetType) {
  492. case WEAPONFIRECHUNK_TARGET_MOVER:
  493. //------------------------
  494. // Mover Target...
  495. // current size is 23 bits
  496. data |= entryAngle;
  497. data <<= WEAPONFIRECHUNK_HITLOCATION_BITS;
  498. data |= (hitLocation + 2);
  499. data <<= WEAPONFIRECHUNK_MOVERINDEX_BITS;
  500. data |= targetId;
  501. if (numMissiles > 0) {
  502. //------------------------------
  503. // Weapon is a missile weapon...
  504. data <<= WEAPONFIRECHUNK_MISSILES_BITS;
  505. data |= numMissiles;
  506. }
  507. data <<= WEAPONFIRECHUNK_HIT_BITS;
  508. break;
  509. case WEAPONFIRECHUNK_TARGET_TERRAIN:
  510. //-------------------------
  511. // Terrain Object Target...
  512. // current size is
  513. data |= (targetId - MIN_TERRAIN_PART_ID);
  514. if (numMissiles > 0) {
  515. //------------------------------
  516. // Weapon is a missile weapon...
  517. data <<= WEAPONFIRECHUNK_MISSILES_BITS;
  518. data |= numMissiles;
  519. }
  520. data <<= WEAPONFIRECHUNK_HIT_BITS;
  521. break;
  522. case WEAPONFIRECHUNK_TARGET_SPECIAL:
  523. data |= entryAngle;
  524. data <<= WEAPONFIRECHUNK_CELLPOS_BITS;
  525. data |= targetCell[0];
  526. data <<= WEAPONFIRECHUNK_CELLPOS_BITS;
  527. data |= targetCell[1];
  528. if (numMissiles > 0) {
  529. //------------------------------
  530. // Weapon is a missile weapon...
  531. data <<= WEAPONFIRECHUNK_MISSILES_BITS;
  532. data |= numMissiles;
  533. }
  534. data <<= WEAPONFIRECHUNK_HIT_BITS;
  535. break;
  536. case WEAPONFIRECHUNK_TARGET_LOCATION:
  537. //-----------------------------------------
  538. // Must be a Location Target (or a Miss)...
  539. data |= targetCell[0];
  540. data <<= WEAPONFIRECHUNK_CELLPOS_BITS;
  541. data |= targetCell[1];
  542. if (numMissiles > 0) {
  543. //------------------------------
  544. // Weapon is a missile weapon...
  545. data <<= WEAPONFIRECHUNK_MISSILES_BITS;
  546. data |= numMissiles;
  547. }
  548. data <<= WEAPONFIRECHUNK_HIT_BITS;
  549. break;
  550. }
  551. if (hit)
  552. data |= 1;
  553. data <<= WEAPONFIRECHUNK_WEAPON_BITS;
  554. data |= weaponIndex;
  555. data <<= WEAPONFIRECHUNK_TARGETTYPE_BITS;
  556. data |= targetType;
  557. #ifdef DEBUG_WEAPONFIRECHUNK
  558. //-------------------------
  559. // Lots'a error checking...
  560. if ((targetType < 0) || (targetType > 3)) {
  561. DebugWeaponFireChunk(this, NULL, attacker);
  562. char errMsg[1024];
  563. sprintf(errMsg, " WeaponFireChunk.pack: bad targetType %d (save wfchunk.dbg file) ", targetType);
  564. Assert(false, targetType, errMsg);
  565. }
  566. if ((weaponIndex < 0) || (weaponIndex > 31)) {
  567. DebugWeaponFireChunk(this, NULL, attacker);
  568. char errMsg[1024];
  569. sprintf(errMsg, " WeaponFireChunk.pack: bad weaponIndex %d (save wfchunk.dbg file) ", weaponIndex);
  570. Assert(false, weaponIndex, errMsg);
  571. }
  572. if ((hitLocation < -1) || (hitLocation >= 12)) {
  573. DebugWeaponFireChunk(this, NULL, attacker);
  574. char errMsg[1024];
  575. sprintf(errMsg, " WeaponFireChunk.pack: bad hitLocation %d (save wfchunk.dbg file) ", hitLocation);
  576. Assert(false, hitLocation, errMsg);
  577. }
  578. if ((entryAngle < 0) || (entryAngle > 3)) {
  579. DebugWeaponFireChunk(this, NULL, attacker);
  580. char errMsg[1024];
  581. sprintf(errMsg, " WeaponFireChunk.pack: bad entryAngle %d (save wfchunk.dbg file) ", entryAngle);
  582. Assert(false, entryAngle, errMsg);
  583. }
  584. if ((numMissiles < 0) || (numMissiles > 15)) {
  585. DebugWeaponFireChunk(this, NULL, attacker);
  586. char errMsg[1024];
  587. sprintf(errMsg, " WeaponFireChunk.pack: bad numMissiles %d (save wfchunk.dbg file) ", numMissiles);
  588. Assert(false, numMissiles, errMsg);
  589. }
  590. #endif
  591. }
  592. //---------------------------------------------------------------------------
  593. void WeaponFireChunk::unpack (GameObjectPtr attacker) {
  594. unsigned long tempData = data;
  595. targetType = (tempData & WEAPONFIRECHUNK_TARGETTYPE_MASK);
  596. tempData >>= WEAPONFIRECHUNK_TARGETTYPE_BITS;
  597. weaponIndex = (tempData & WEAPONFIRECHUNK_WEAPON_MASK);
  598. tempData >>= WEAPONFIRECHUNK_WEAPON_BITS;
  599. hit = ((tempData & WEAPONFIRECHUNK_HIT_MASK) != 0) ? true : false;
  600. tempData >>= WEAPONFIRECHUNK_HIT_BITS;
  601. bool isMissileWeapon = false;
  602. if (attacker->isMover()) {
  603. long itemIndex = ((MoverPtr)attacker)->numOther + weaponIndex;
  604. isMissileWeapon = ((MoverPtr)attacker)->isWeaponMissile(itemIndex);
  605. }
  606. else if (attacker->getObjectClass() == TURRET) {
  607. //----------------------
  608. // Attacker is turret...
  609. isMissileWeapon = ((Turret*)attacker)->isWeaponMissile(weaponIndex);
  610. }
  611. switch (targetType) {
  612. case WEAPONFIRECHUNK_TARGET_MOVER:
  613. //----------------
  614. // Mover Target...
  615. if (isMissileWeapon) {
  616. numMissiles = (tempData & WEAPONFIRECHUNK_MISSILES_MASK);
  617. tempData >>= WEAPONFIRECHUNK_MISSILES_BITS;
  618. }
  619. targetId = (tempData & WEAPONFIRECHUNK_MOVERINDEX_MASK);
  620. tempData >>= WEAPONFIRECHUNK_MOVERINDEX_BITS;
  621. hitLocation = ((tempData & WEAPONFIRECHUNK_HITLOCATION_MASK) - 2);
  622. tempData >>= WEAPONFIRECHUNK_HITLOCATION_BITS;
  623. entryAngle = (tempData & WEAPONFIRECHUNK_ENTRYQUAD_MASK);
  624. break;
  625. case WEAPONFIRECHUNK_TARGET_TERRAIN:
  626. //-------------------------
  627. // Terrain Object Target...
  628. if (isMissileWeapon) {
  629. numMissiles = (tempData & WEAPONFIRECHUNK_MISSILES_MASK);
  630. tempData >>= WEAPONFIRECHUNK_MISSILES_BITS;
  631. }
  632. targetId = MIN_TERRAIN_PART_ID + (tempData & WEAPONFIRECHUNK_TARGETID_MASK);
  633. tempData >>= WEAPONFIRECHUNK_TARGETID_BITS;
  634. break;
  635. case WEAPONFIRECHUNK_TARGET_SPECIAL:
  636. //-----------------------
  637. // Special (Train, CameraDrone) Object Target...
  638. if (isMissileWeapon) {
  639. numMissiles = (tempData & WEAPONFIRECHUNK_MISSILES_MASK);
  640. tempData >>= WEAPONFIRECHUNK_MISSILES_BITS;
  641. }
  642. specialId = (tempData & WEAPONFIRECHUNK_SPECIALID_MASK);
  643. tempData >>= WEAPONFIRECHUNK_SPECIALID_BITS;
  644. specialType = (tempData & WEAPONFIRECHUNK_SPECIALTYPE_MASK);
  645. tempData >>= WEAPONFIRECHUNK_SPECIALTYPE_BITS;
  646. switch (specialType) {
  647. case WEAPONFIRECHUNK_SPECIAL_CAMERADRONE:
  648. targetId = MIN_CAMERA_DRONE_ID + specialId;
  649. break;
  650. default:
  651. Fatal(specialType, " WeaponFireChunk.unpack: bad specialType ");
  652. }
  653. entryAngle = (tempData & WEAPONFIRECHUNK_ENTRYQUAD_MASK);
  654. break;
  655. case WEAPONFIRECHUNK_TARGET_LOCATION:
  656. //-----------------------------------------
  657. // Must be a Location Target (or a Miss)...
  658. if (isMissileWeapon) {
  659. numMissiles = (tempData & WEAPONFIRECHUNK_MISSILES_MASK);
  660. tempData >>= WEAPONFIRECHUNK_MISSILES_BITS;
  661. }
  662. targetCell[1] = (tempData & WEAPONFIRECHUNK_CELLPOS_MASK);
  663. tempData >>= WEAPONFIRECHUNK_CELLPOS_BITS;
  664. targetCell[0] = (tempData & WEAPONFIRECHUNK_CELLPOS_MASK);
  665. break;
  666. }
  667. //-------------------------
  668. // Lots'a error checking...
  669. #ifdef DEBUG_WEAPONFIRECHUNK
  670. //-------------------------
  671. // Lots'a error checking...
  672. if ((targetType < 0) || (targetType > 3)) {
  673. DebugWeaponFireChunk(this, NULL, attacker);
  674. char errMsg[1024];
  675. sprintf(errMsg, " WeaponFireChunk.unpack: bad targetType %d (save wfchunk.dbg file) ", targetType);
  676. Assert(false, targetType, errMsg);
  677. }
  678. if ((weaponIndex < 0) || (weaponIndex > 31)) {
  679. DebugWeaponFireChunk(this, NULL, attacker);
  680. char errMsg[1024];
  681. sprintf(errMsg, " WeaponFireChunk.unpack: bad weaponIndex %d (save wfchunk.dbg file) ", weaponIndex);
  682. Assert(false, weaponIndex, errMsg);
  683. }
  684. if ((hitLocation < -1) || (hitLocation >= 12)) {
  685. DebugWeaponFireChunk(this, NULL, attacker);
  686. char errMsg[1024];
  687. sprintf(errMsg, " WeaponFireChunk.unpack: bad hitLocation %d (save wfchunk.dbg file) ", hitLocation);
  688. Assert(false, hitLocation, errMsg);
  689. }
  690. if ((entryAngle < 0) || (entryAngle > 3)) {
  691. DebugWeaponFireChunk(this, NULL, attacker);
  692. char errMsg[1024];
  693. sprintf(errMsg, " WeaponFireChunk.unpack: bad entryAngle %d (save wfchunk.dbg file) ", entryAngle);
  694. Assert(false, entryAngle, errMsg);
  695. }
  696. if ((numMissiles < 0) || (numMissiles > 15)) {
  697. DebugWeaponFireChunk(this, NULL, attacker);
  698. char errMsg[1024];
  699. sprintf(errMsg, " WeaponFireChunk.unpack: bad numMissiles %d (save wfchunk.dbg file) ", numMissiles);
  700. Assert(false, numMissiles, errMsg);
  701. }
  702. if (!hit) {
  703. bool isStreakMissile = false;
  704. if (attacker->isMover()) {
  705. long itemIndex = ((MoverPtr)attacker)->numOther + weaponIndex;
  706. isStreakMissile = MasterComponent::masterList[((MoverPtr)attacker)->inventory[itemIndex].masterID].getWeaponStreak();
  707. }
  708. else if (attacker->getObjectClass() == TURRET) {
  709. //----------------------
  710. // Attacker is turret...
  711. isStreakMissile = ((TurretPtr)attacker)->isWeaponStreak(weaponIndex);
  712. }
  713. if (isStreakMissile) {
  714. DebugWeaponFireChunk(this, NULL, attacker);
  715. Assert(false, 0, " WeaponFireChunk.unpack: streak missile missed (save wfchunk.dbg file) ");
  716. }
  717. }
  718. GameObjectPtr target = NULL;
  719. if (targetType == 0 /*WEAPONFIRECHUNK_TARGET_MOVER*/) {
  720. target = (GameObjectPtr)MPlayer->moverRoster[targetId];
  721. //----------------------------------------------------------------------------
  722. // Mover targets could be NULL now, since we free them when they're destroyed.
  723. //if (target == NULL) {
  724. // DebugWeaponFireChunk (this, NULL, attacker);
  725. // Assert(false, 0, " WeaponFireChunk.unpack: NULL Mover Target (save wfchunk.dbg file) ");
  726. //}
  727. }
  728. else if (targetType == 1 /*WEAPONFIRECHUNK_TARGET_TERRAIN*/) {
  729. target = ObjectManager->findByPartId(targetId);
  730. if (target == NULL) {
  731. DebugWeaponFireChunk (this, NULL, attacker);
  732. Assert(false, 0, " WeaponFireChunk.unpack: NULL Terrain Target (save wfchunk.dbg file) ");
  733. }
  734. }
  735. else if (targetType == 2 /*WEAPONFIRECHUNK_TARGET_TRAIN*/) {
  736. target = ObjectManager->findByPartId(targetId);
  737. if (target == NULL) {
  738. DebugWeaponFireChunk (this, NULL, attacker);
  739. Assert(false, 0, " WeaponFireChunk.unpack: NULL Special Target (save wfchunk.dbg file) ");
  740. }
  741. }
  742. else if (targetType == 3 /*WEAPONFIRECHUNK_TARGET_LOCATION*/) {
  743. // Do nothing...
  744. }
  745. #endif
  746. }
  747. //---------------------------------------------------------------------------
  748. bool WeaponFireChunk::equalTo (WeaponFireChunkPtr chunk) {
  749. if (targetType != chunk->targetType) {
  750. DebugWeaponFireChunk(this, chunk, NULL);
  751. return(false);
  752. }
  753. if (targetId != chunk->targetId) {
  754. DebugWeaponFireChunk(this, chunk, NULL);
  755. return(false);
  756. }
  757. if (targetCell[0] != chunk->targetCell[0]) {
  758. DebugWeaponFireChunk(this, chunk, NULL);
  759. return(false);
  760. }
  761. if (targetCell[1] != chunk->targetCell[1]) {
  762. DebugWeaponFireChunk(this, chunk, NULL);
  763. return(false);
  764. }
  765. if (specialType != chunk->specialType) {
  766. DebugWeaponFireChunk(this, chunk, NULL);
  767. return(false);
  768. }
  769. if (specialId != chunk->specialId) {
  770. DebugWeaponFireChunk(this, chunk, NULL);
  771. return(false);
  772. }
  773. if (weaponIndex != chunk->weaponIndex) {
  774. DebugWeaponFireChunk(this, chunk, NULL);
  775. return(false);
  776. }
  777. if (hit != chunk->hit) {
  778. DebugWeaponFireChunk(this, chunk, NULL);
  779. return(false);
  780. }
  781. if (entryAngle != chunk->entryAngle) {
  782. DebugWeaponFireChunk(this, chunk, NULL);
  783. return(false);
  784. }
  785. if (numMissiles != chunk->numMissiles) {
  786. DebugWeaponFireChunk(this, chunk, NULL);
  787. return(false);
  788. }
  789. if (hitLocation != chunk->hitLocation) {
  790. DebugWeaponFireChunk(this, chunk, NULL);
  791. return(false);
  792. }
  793. return(true);
  794. }
  795. //***************************************************************************
  796. // WEAPONHITCHUNK class
  797. //***************************************************************************
  798. void DebugWeaponHitChunk (WeaponHitChunkPtr chunk1, WeaponHitChunkPtr chunk2) {
  799. ChunkDebugMsg[0] = NULL;
  800. char outString[512];
  801. if (chunk1) {
  802. strcat(ChunkDebugMsg, "\nCHUNK1\n");
  803. GameObjectPtr target = NULL;
  804. bool isTargetPoint = false;
  805. if (chunk1->targetType == WEAPONHITCHUNK_TARGET_MOVER)
  806. target = (GameObjectPtr)MPlayer->moverRoster[chunk1->targetId];
  807. else if (chunk1->targetType == WEAPONHITCHUNK_TARGET_TERRAIN)
  808. target = ObjectManager->findByPartId(chunk1->targetId);
  809. else if (chunk1->targetType == WEAPONHITCHUNK_TARGET_SPECIAL)
  810. target = ObjectManager->findByPartId(chunk1->targetId);
  811. else if (chunk1->targetType == WEAPONHITCHUNK_TARGET_LOCATION)
  812. isTargetPoint = true;
  813. if (target) {
  814. if (target->isMover()) {
  815. sprintf(outString, "target = %s (%d)\n", target->getName(), target->getPartId());
  816. strcat(ChunkDebugMsg, outString);
  817. }
  818. else {
  819. sprintf(outString, "target = objClass %d (%d)\n", target->getObjectClass(), target->getPartId());
  820. strcat(ChunkDebugMsg, outString);
  821. }
  822. }
  823. else if (isTargetPoint)
  824. strcat(ChunkDebugMsg, "target point\n");
  825. else
  826. strcat(ChunkDebugMsg, "target = ???\n");
  827. sprintf(outString, "targetType = %d\n", chunk1->targetType);
  828. strcat(ChunkDebugMsg, outString);
  829. sprintf(outString, "targetId = %d\n", chunk1->targetId);
  830. strcat(ChunkDebugMsg, outString);
  831. sprintf(outString, "specialType = %d\n", chunk1->specialType);
  832. strcat(ChunkDebugMsg, outString);
  833. sprintf(outString, "specialId = %d\n", chunk1->specialId);
  834. strcat(ChunkDebugMsg, outString);
  835. sprintf(outString, "cause = %d\n", chunk1->cause);
  836. strcat(ChunkDebugMsg, outString);
  837. sprintf(outString, "damage = %f\n", chunk1->damage);
  838. strcat(ChunkDebugMsg, outString);
  839. sprintf(outString, "hitLocation = %d\n", chunk1->hitLocation);
  840. strcat(ChunkDebugMsg, outString);
  841. sprintf(outString, "entryAngle = %d\n", chunk1->entryAngle);
  842. strcat(ChunkDebugMsg, outString);
  843. sprintf(outString, "refit = %s\n", chunk1->refit ? "true" : "false");
  844. strcat(ChunkDebugMsg, outString);
  845. }
  846. if (chunk2) {
  847. strcat(ChunkDebugMsg, "\nCHUNK2\n");
  848. GameObjectPtr target = NULL;
  849. bool isTargetPoint = false;
  850. if (chunk2->targetType == WEAPONHITCHUNK_TARGET_MOVER)
  851. target = (GameObjectPtr)MPlayer->moverRoster[chunk2->targetId];
  852. else if (chunk2->targetType == WEAPONHITCHUNK_TARGET_TERRAIN)
  853. target = ObjectManager->findByPartId(chunk2->targetId);
  854. else if (chunk2->targetType == WEAPONHITCHUNK_TARGET_SPECIAL)
  855. target = ObjectManager->findByPartId(chunk2->targetId);
  856. else if (chunk2->targetType == WEAPONHITCHUNK_TARGET_LOCATION)
  857. isTargetPoint = true;
  858. if (target) {
  859. if (target->isMover()) {
  860. sprintf(outString, "target = %s (%d)\n", target->getName(), target->getPartId());
  861. strcat(ChunkDebugMsg, outString);
  862. }
  863. else {
  864. sprintf(outString, "target = objClass %d (%d)\n", target->getObjectClass(), target->getPartId());
  865. strcat(ChunkDebugMsg, outString);
  866. }
  867. }
  868. else if (isTargetPoint)
  869. strcat(ChunkDebugMsg, "target point");
  870. else
  871. strcat(ChunkDebugMsg, "target = ???\n");
  872. sprintf(outString, "targetType = %d\n", chunk2->targetType);
  873. strcat(ChunkDebugMsg, outString);
  874. sprintf(outString, "targetId = %d\n", chunk2->targetId);
  875. strcat(ChunkDebugMsg, outString);
  876. sprintf(outString, "specialType = %d\n", chunk2->specialType);
  877. strcat(ChunkDebugMsg, outString);
  878. sprintf(outString, "specialId = %d\n", chunk2->specialId);
  879. strcat(ChunkDebugMsg, outString);
  880. sprintf(outString, "cause = %d\n", chunk2->cause);
  881. strcat(ChunkDebugMsg, outString);
  882. sprintf(outString, "damage = %f\n", chunk2->damage);
  883. strcat(ChunkDebugMsg, outString);
  884. sprintf(outString, "hitLocation = %d\n", chunk2->hitLocation);
  885. strcat(ChunkDebugMsg, outString);
  886. sprintf(outString, "entryAngle = %d\n", chunk2->entryAngle);
  887. strcat(ChunkDebugMsg, outString);
  888. sprintf(outString, "refit = %s\n", chunk2->refit ? "true" : "false");
  889. strcat(ChunkDebugMsg, outString);
  890. }
  891. File* debugFile = new File;
  892. debugFile->create("whchunk.dbg");
  893. debugFile->writeString(ChunkDebugMsg);
  894. debugFile->close();
  895. delete debugFile;
  896. debugFile = NULL;
  897. ExceptionGameMsg = ChunkDebugMsg;
  898. }
  899. //---------------------------------------------------------------------------
  900. void* WeaponHitChunk::operator new (size_t ourSize) {
  901. void* result = systemHeap->Malloc(ourSize);
  902. return(result);
  903. }
  904. //---------------------------------------------------------------------------
  905. void WeaponHitChunk::operator delete (void* us) {
  906. systemHeap->Free(us);
  907. }
  908. //---------------------------------------------------------------------------
  909. void WeaponHitChunk::buildMoverTarget (GameObjectPtr target,
  910. long _cause,
  911. float _damage,
  912. long _hitLocation,
  913. float _entryAngle,
  914. bool isRefit) {
  915. targetType = WEAPONHITCHUNK_TARGET_MOVER;
  916. targetId = ((MoverPtr)target)->getNetRosterIndex();
  917. //Assert((targetId > -1) && (targetId < MPlayer->numMovers), targetId, " WeaponHitChunk.buildMoverTarget: bad targetId ");
  918. cause = _cause;
  919. //Assert((cause
  920. damage = _damage;
  921. //Assert((damage >= ) && (damage < 1024), damage, " WeaponHitChunk.buildMoverTarget: bad damage ");
  922. hitLocation = _hitLocation;
  923. //Assert((hitLocation > -2) && (hitLocation < 12), hitLocation, " WeaponHitChunk.buildMoverTarget: bad hitLocation ");
  924. if ((_entryAngle >= -45.0) && (_entryAngle <= 45.0))
  925. entryAngle = 0; //MECH_HIT_ARC_FRONT;
  926. else if ((_entryAngle > -135.0) && (_entryAngle < -45.0))
  927. entryAngle = 2; //MECH_HIT_ARC_LEFT;
  928. else if ((_entryAngle > 45.0) && (_entryAngle < 135))
  929. entryAngle = 3; //MECH_HIT_ARC_RIGHT;
  930. else
  931. entryAngle = 1; //MECH_HIT_ARC_REAR;
  932. refit = isRefit;
  933. data = 0;
  934. }
  935. //---------------------------------------------------------------------------
  936. void WeaponHitChunk::buildTerrainTarget (GameObjectPtr target,
  937. float _damage) {
  938. targetType = WEAPONHITCHUNK_TARGET_TERRAIN;
  939. targetId = target->getPartId();
  940. damage = _damage;
  941. data = 0;
  942. }
  943. //---------------------------------------------------------------------------
  944. void WeaponHitChunk::buildCameraDroneTarget (GameObjectPtr target,
  945. float _damage,
  946. float _entryAngle) {
  947. targetType = WEAPONHITCHUNK_TARGET_SPECIAL;
  948. targetId = target->getPartId();
  949. specialType = WEAPONFIRECHUNK_SPECIAL_CAMERADRONE;
  950. specialId = targetId - MIN_CAMERA_DRONE_ID;
  951. damage = _damage;
  952. //Assert((damage >= ) && (damage < 1024), damage, " WeaponHitChunk.buildMoverTarget: bad damage ");
  953. if ((_entryAngle >= -45.0) && (_entryAngle <= 45.0))
  954. entryAngle = 0; //MECH_HIT_ARC_FRONT;
  955. else if ((_entryAngle > -135.0) && (_entryAngle < -45.0))
  956. entryAngle = 2; //MECH_HIT_ARC_LEFT;
  957. else if ((_entryAngle > 45.0) && (_entryAngle < 135))
  958. entryAngle = 3; //MECH_HIT_ARC_RIGHT;
  959. else
  960. entryAngle = 1; //MECH_HIT_ARC_REAR;
  961. data = 0;
  962. }
  963. //---------------------------------------------------------------------------
  964. void WeaponHitChunk::build (GameObjectPtr target, WeaponShotInfoPtr shotInfo, bool isRefit) {
  965. if (!target)
  966. Fatal(0, " WeaponHitChunk.build: NULL target ");
  967. Assert(((float)((unsigned long)(shotInfo->damage * 4.0)) * 0.25) == shotInfo->damage, 0, " WeaponHitChunk.build: damage round error ");
  968. if (target->isMover()) {
  969. //---------------------------------------------------------------
  970. // HACK fix for ammoExplosions without needing to save the weapon
  971. // master ID...
  972. if (shotInfo->masterId > 0) {
  973. if (MasterComponent::masterList[shotInfo->masterId].getForm() == COMPONENT_FORM_AMMO)
  974. shotInfo->masterId = -4;
  975. else
  976. shotInfo->masterId = 0;
  977. }
  978. buildMoverTarget(target,
  979. shotInfo->masterId,
  980. shotInfo->damage,
  981. shotInfo->hitLocation,
  982. shotInfo->entryAngle,
  983. isRefit);
  984. }
  985. else {
  986. switch (target->getObjectClass()) {
  987. case BUILDING:
  988. case TREEBUILDING:
  989. case TREE:
  990. case GATE:
  991. case TURRET:
  992. case BRIDGE:
  993. buildTerrainTarget(target, shotInfo->damage);
  994. break;
  995. case CAMERADRONE:
  996. buildCameraDroneTarget(target, shotInfo->damage, shotInfo->entryAngle);
  997. break;
  998. default:
  999. //Fatal(0, " WeaponHitChunk.build: bad target type ");
  1000. break;
  1001. }
  1002. }
  1003. }
  1004. //---------------------------------------------------------------------------
  1005. void WeaponHitChunk::pack (void) {
  1006. data = 0;
  1007. switch (targetType) {
  1008. case WEAPONHITCHUNK_TARGET_MOVER:
  1009. //----------------
  1010. // Mover Target...
  1011. if (refit)
  1012. data |= 1;
  1013. data <<= WEAPONHITCHUNK_ENTRYQUAD_BITS;
  1014. data |= entryAngle;
  1015. data <<= WEAPONHITCHUNK_HITLOCATION_BITS;
  1016. data |= (hitLocation + 2);
  1017. data <<= WEAPONHITCHUNK_CAUSE_BITS;
  1018. data |= (cause + 7);
  1019. data <<= WEAPONHITCHUNK_MOVERINDEX_BITS;
  1020. data |= targetId;
  1021. data <<= WEAPONHITCHUNK_DAMAGE_BITS;
  1022. break;
  1023. case WEAPONHITCHUNK_TARGET_TERRAIN:
  1024. //-------------------------
  1025. // Terrain Object Target...
  1026. data |= (targetId - MIN_TERRAIN_PART_ID);
  1027. data <<= WEAPONHITCHUNK_DAMAGE_BITS;
  1028. break;
  1029. case WEAPONHITCHUNK_TARGET_SPECIAL:
  1030. data |= entryAngle;
  1031. data <<= WEAPONHITCHUNK_SPECIALTYPE_BITS;
  1032. data |= specialType;
  1033. data <<= WEAPONHITCHUNK_SPECIALID_BITS;
  1034. data |= specialId;
  1035. data <<= WEAPONHITCHUNK_DAMAGE_BITS;
  1036. break;
  1037. default:
  1038. Fatal(0, " Bad WeaponHitChunk Target Type ");
  1039. }
  1040. data |= (unsigned long)(damage * 4.0);
  1041. data <<= WEAPONHITCHUNK_TARGETTYPE_BITS;
  1042. data |= targetType;
  1043. }
  1044. //---------------------------------------------------------------------------
  1045. void WeaponHitChunk::unpack (void) {
  1046. unsigned long tempData = data;
  1047. targetType = (tempData & WEAPONHITCHUNK_TARGETTYPE_MASK);
  1048. tempData >>= WEAPONHITCHUNK_TARGETTYPE_BITS;
  1049. damage = (float)(tempData & WEAPONHITCHUNK_DAMAGE_MASK) * 0.25;
  1050. tempData >>= WEAPONHITCHUNK_DAMAGE_BITS;
  1051. Assert((damage >= 0.0) && (damage <= 255.0), 0, " WeaponHitChunk.unpack: bad damage ");
  1052. switch (targetType) {
  1053. case WEAPONHITCHUNK_TARGET_MOVER:
  1054. //----------------
  1055. // Mover Target...
  1056. targetId = (tempData & WEAPONHITCHUNK_MOVERINDEX_MASK);
  1057. tempData >>= WEAPONHITCHUNK_MOVERINDEX_BITS;
  1058. Assert((targetId > -1) && (targetId < MAX_MULTIPLAYER_MOVERS), targetId, " WeaponHitChunk.unpack: bad targetId ");
  1059. cause = ((tempData & WEAPONHITCHUNK_CAUSE_MASK) - 7);
  1060. tempData >>= WEAPONHITCHUNK_CAUSE_BITS;
  1061. Assert((cause >= -7) && (cause <= 0), cause, " WeaponHitChunk.unpack: bad cause ");
  1062. hitLocation = ((tempData & WEAPONHITCHUNK_HITLOCATION_MASK) - 2);
  1063. tempData >>= WEAPONHITCHUNK_HITLOCATION_BITS;
  1064. Assert((hitLocation > -2) && (hitLocation < 12), hitLocation, " WeaponHitChunk.unpack: bad hitLocation ");
  1065. entryAngle = (tempData & WEAPONHITCHUNK_ENTRYQUAD_MASK);
  1066. tempData >>= WEAPONHITCHUNK_ENTRYQUAD_BITS;
  1067. refit = (tempData & WEAPONHITCHUNK_REFIT_MASK) ? true : false;
  1068. break;
  1069. case WEAPONHITCHUNK_TARGET_TERRAIN:
  1070. //-------------------------
  1071. // Terrain Object Target...
  1072. targetId = MIN_TERRAIN_PART_ID + (tempData & WEAPONHITCHUNK_TARGETID_MASK);
  1073. tempData >>= WEAPONHITCHUNK_TARGETID_BITS;
  1074. break;
  1075. case WEAPONHITCHUNK_TARGET_SPECIAL:
  1076. //-----------------------
  1077. // Train Object Target...
  1078. specialId = (tempData & WEAPONHITCHUNK_SPECIALID_MASK);
  1079. tempData >>= WEAPONHITCHUNK_SPECIALID_BITS;
  1080. specialType = (tempData & WEAPONHITCHUNK_SPECIALTYPE_MASK);
  1081. tempData >>= WEAPONHITCHUNK_SPECIALTYPE_BITS;
  1082. switch (specialType) {
  1083. case WEAPONHITCHUNK_SPECIAL_CAMERADRONE:
  1084. targetId = MIN_CAMERA_DRONE_ID + specialId;
  1085. break;
  1086. default:
  1087. Fatal(specialType, " WeaponHitChunk.unpack: bad specialType ");
  1088. }
  1089. entryAngle = (tempData & WEAPONHITCHUNK_ENTRYQUAD_MASK);
  1090. break;
  1091. default:
  1092. DebugWeaponHitChunk(this, NULL);
  1093. Fatal(0, " Bad WeaponHitChunk Target Type ");
  1094. }
  1095. }
  1096. //---------------------------------------------------------------------------
  1097. bool WeaponHitChunk::equalTo (WeaponHitChunkPtr chunk) {
  1098. if (targetType != chunk->targetType) {
  1099. DebugWeaponHitChunk(this, chunk);
  1100. return(false);
  1101. }
  1102. if (targetId != chunk->targetId) {
  1103. DebugWeaponHitChunk(this, chunk);
  1104. return(false);
  1105. }
  1106. if (specialType != chunk->specialType) {
  1107. DebugWeaponHitChunk(this, chunk);
  1108. return(false);
  1109. }
  1110. if (specialId != chunk->specialId) {
  1111. DebugWeaponHitChunk(this, chunk);
  1112. return(false);
  1113. }
  1114. if (cause != chunk->cause) {
  1115. DebugWeaponHitChunk(this, chunk);
  1116. return(false);
  1117. }
  1118. if (damage != chunk->damage) {
  1119. DebugWeaponHitChunk(this, chunk);
  1120. return(false);
  1121. }
  1122. if (entryAngle != chunk->entryAngle) {
  1123. DebugWeaponHitChunk(this, chunk);
  1124. return(false);
  1125. }
  1126. if (refit != chunk->refit) {
  1127. DebugWeaponHitChunk(this, chunk);
  1128. return(false);
  1129. }
  1130. if (hitLocation != chunk->hitLocation) {
  1131. DebugWeaponHitChunk(this, chunk);
  1132. return(false);
  1133. }
  1134. return(true);
  1135. }
  1136. //---------------------------------------------------------------------------
  1137. bool WeaponHitChunk::valid (long from) {
  1138. if (refit) {
  1139. if (targetType != 0)
  1140. Fatal(0, " Multiplayer.handleAppWeaponHitUpdate: bad targetType for refit ");
  1141. MoverPtr target = MPlayer->moverRoster[targetId];
  1142. if (!target) {
  1143. if (CombatLog)
  1144. CombatLog->write("WeaponHitChunk INVALID: refit has NULL target");
  1145. DebugWeaponHitChunk(this, NULL);
  1146. return(false);
  1147. }
  1148. }
  1149. else {
  1150. WeaponShotInfo shotInfo;
  1151. shotInfo.attackerWID = 0;
  1152. shotInfo.masterId = cause;
  1153. shotInfo.damage = damage;
  1154. shotInfo.hitLocation = hitLocation;
  1155. //shotInfo.entryAngle = entryQuadTable[entryAngle];
  1156. switch (targetType) {
  1157. case WEAPONHITCHUNK_TARGET_MOVER:
  1158. #ifdef _DEBUG
  1159. //if (WeaponHitLog)
  1160. // WeaponHitLog->writeString("==>handleAppWeaponHit\n");
  1161. #endif
  1162. //FromMP = true;
  1163. //if (moverRoster[chunk.targetId])
  1164. // moverRoster[chunk.targetId]->handleWeaponHit(&shotInfo);
  1165. //FromMP = false;
  1166. break;
  1167. case WEAPONHITCHUNK_TARGET_TERRAIN:
  1168. case WEAPONHITCHUNK_TARGET_SPECIAL: {
  1169. GameObjectPtr target = ObjectManager->findByPartId(targetId);
  1170. if (!target) {
  1171. char s[512];
  1172. long r = (targetId - MIN_TERRAIN_PART_ID) / MAX_MAP_CELL_WIDTH;
  1173. long c = targetId - MIN_TERRAIN_PART_ID - (MAX_MAP_CELL_WIDTH * r);
  1174. sprintf(s, "WeaponHitChunk INVALID: NULL terrain target (%d), rc = %d,%d, type = %d, damage = %.2f, data=%d", targetId, r, c, targetType, damage, data);
  1175. if (CombatLog) {
  1176. CombatLog->write(s);
  1177. CombatLog->dump();
  1178. //CombatLog->close();
  1179. }
  1180. DebugWeaponHitChunk(this, NULL);
  1181. return(false);
  1182. }
  1183. }
  1184. break;
  1185. default:
  1186. if (CombatLog) {
  1187. CombatLog->write("WeaponHitChunk INVALID: bad targetType");
  1188. CombatLog->dump();
  1189. }
  1190. DebugWeaponHitChunk(this, NULL);
  1191. return(false);
  1192. }
  1193. }
  1194. return(true);
  1195. }
  1196. //---------------------------------------------------------------------------
  1197. // class GameObject
  1198. //---------------------------------------------------------------------------
  1199. void* GameObject::operator new (size_t ourSize) {
  1200. void* result = ObjectTypeManager::objectCache->Malloc(ourSize);
  1201. return(result);
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. void GameObject::operator delete (void* us) {
  1205. ObjectTypeManager::objectCache->Free(us);
  1206. }
  1207. //---------------------------------------------------------------------------
  1208. void GameObject::init (bool create) {
  1209. objectClass = GAMEOBJECT;
  1210. if (initialize) {
  1211. handle = 0;
  1212. appearance = NULL;
  1213. threatRating = 0;
  1214. }
  1215. partId = 0;
  1216. watchID = 0;
  1217. typeHandle = 0;
  1218. position.Zero();
  1219. cellPositionRow = 0;
  1220. cellPositionCol = 0;
  1221. flags = OBJECT_FLAG_USEME | OBJECT_FLAG_AWAKE;
  1222. debugFlags = 0;
  1223. status = OBJECT_STATUS_NORMAL;
  1224. tonnage = 0.0;
  1225. d_vertexNum = -1;
  1226. //team = 255;
  1227. collisionFreeFromWID = 0;
  1228. collisionFreeTime = 0.0;
  1229. screenPos.x = 0.0f;
  1230. screenPos.y = 0.0f;
  1231. screenPos.z = 0.0f;
  1232. screenPos.w = 0.0f;
  1233. windowsVisible = 0;
  1234. explDamage = 0.0;
  1235. explRadius = 0.0;
  1236. maxCV = 0;
  1237. curCV = 0;
  1238. lastFrameTime = 0.0;
  1239. blipFrame = 0;
  1240. numAttackers = 0;
  1241. rotation = 0.0;
  1242. drawFlags = 0;
  1243. }
  1244. //---------------------------------------------------------------------------
  1245. void GameObject::set (GameObject copy) {
  1246. objectClass = copy.objectClass;
  1247. handle = copy.handle;
  1248. partId = copy.partId;
  1249. watchID = copy.watchID;
  1250. typeHandle = copy.typeHandle;
  1251. position = copy.position;
  1252. cellPositionRow = copy.cellPositionRow;
  1253. cellPositionCol = copy.cellPositionCol;
  1254. flags = copy.flags;
  1255. debugFlags = copy.debugFlags;
  1256. status = copy.status;
  1257. tonnage = copy.tonnage;
  1258. appearance = copy.appearance;
  1259. d_vertexNum = copy.d_vertexNum;
  1260. collisionFreeFromWID = copy.collisionFreeFromWID;
  1261. collisionFreeTime = copy.collisionFreeTime;
  1262. screenPos = copy.screenPos;
  1263. windowsVisible = copy.windowsVisible;
  1264. explRadius = copy.explRadius;
  1265. explDamage = copy.explDamage;
  1266. maxCV = copy.maxCV;
  1267. curCV = copy.curCV;
  1268. lastFrameTime = copy.lastFrameTime;
  1269. blipFrame = copy.blipFrame;
  1270. numAttackers = copy.numAttackers;
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. ObjectTypePtr GameObject::getObjectType (void) {
  1274. return(ObjectManager->getObjectType(typeHandle));
  1275. }
  1276. //---------------------------------------------------------------------------
  1277. void GameObject::init (bool create, ObjectTypePtr _type) {
  1278. typeHandle = _type->whatAmI();
  1279. #ifdef _DEBUG
  1280. // id = _type->getName();
  1281. #endif
  1282. objectClass = GAMEOBJECT;
  1283. }
  1284. //---------------------------------------------------------------------------
  1285. unsigned long GameObject::getWatchID (bool assign) {
  1286. if ((watchID == 0) && assign)
  1287. ObjectManager->setWatchID(this);
  1288. return(watchID);
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. void GameObject::getBlockAndVertexNumber (long &blockNum, long &vertexNum) {
  1292. Assert(Terrain::worldUnitsPerVertex==128,0," Optimizations now broken ");
  1293. // What is our block and vertex number?
  1294. long mx = (float2long(position.x) >> 7) + Terrain::halfVerticesMapSide;
  1295. long blockX = float2long(mx * Terrain::oneOverVerticesBlockSide);
  1296. long my = Terrain::halfVerticesMapSide - ((float2long(position.y) >> 7) + 1);
  1297. long blockY = float2long(my * Terrain::oneOverVerticesBlockSide);
  1298. blockNum = blockX + (blockY * Terrain::blocksMapSide);
  1299. long vertexX = mx - (blockX * Terrain::verticesBlockSide);
  1300. long vertexY = my - (blockY * Terrain::verticesBlockSide);
  1301. vertexNum = vertexX + (vertexY * Terrain::verticesBlockSide);
  1302. }
  1303. //---------------------------------------------------------------------------
  1304. long GameObject::kill (void)
  1305. {
  1306. //------------------------------------------------------------
  1307. //Once new MC II ObjMgr is up and running, put this back in...
  1308. // DO NOT DO THIS ANYMORE. EACH OBJECT MUST HAVE ITS OWN DESTRUCTION NOW>
  1309. // GOSFX Change 12/15/99 -fs Code Deleted
  1310. return(OBJECT_DEAD);
  1311. }
  1312. //---------------------------------------------------------------------------
  1313. float GameObject::relFacingTo (Stuff::Vector3D goal, long bodyLocation) {
  1314. Stuff::Vector3D facingVec = getRotationVector();
  1315. Stuff::Vector3D goalVec;
  1316. goalVec.Subtract(goal, position);
  1317. float angle = angle_from(facingVec, goalVec);
  1318. //--------------------------------
  1319. // Get sign of relative angle.
  1320. float z = (facingVec.x * goalVec.y) - (facingVec.y * goalVec.x);
  1321. if (z > 0.0f)
  1322. angle = -angle;
  1323. return(angle);
  1324. }
  1325. //---------------------------------------------------------------------------
  1326. Stuff::Vector3D GameObject::relativePosition (float angle, float distance, unsigned long flags) {
  1327. //--------------------------------------------------------
  1328. // Note that the angle should be -180 <= angle <= 180, and
  1329. // the distance is in meters...
  1330. #ifdef USE_ROTATION
  1331. Stuff::Vector2DOf<float> curPos;
  1332. curPos.x = position.x;
  1333. curPos.y = position.y;
  1334. distance *= -worldUnitsPerMeter;
  1335. Stuff::Vector2DOf<float> shiftVect;
  1336. //--------------------------------------------
  1337. // Absolute facing, based upon north facing...
  1338. shiftVect.x = 0.0;
  1339. shiftVect.y = 1.0;
  1340. float tx = shiftVect.x;
  1341. float sine = sin(angle);
  1342. float cosine = cos(angle);
  1343. shiftVect.x *= cosine;
  1344. shiftVect.x += (shiftVect.y * sine);
  1345. shiftVect.y *= cosine;
  1346. shiftVect.y -= (tx * sine);
  1347. //shiftVect.rotate(angle);
  1348. shiftVect *= distance;
  1349. Stuff::Vector2DOf<float> relPos;
  1350. relPos.x = curPos.x + shiftVect.x;
  1351. relPos.y = curPos.y + shiftVect.y;
  1352. Stuff::Vector2DOf<float> start2d;
  1353. Stuff::Vector2DOf<float> goal2d;
  1354. Stuff::Vector2DOf<float> deltaVector;
  1355. if (flags & RELPOS_FLAG_PASSABLE_START) {
  1356. start2d = curPos;
  1357. goal2d = relPos;
  1358. }
  1359. else {
  1360. start2d = relPos;
  1361. goal2d = curPos;
  1362. }
  1363. deltaVector.x= goal2d.x - start2d.x;
  1364. deltaVector.y = goal2d.y - start2d.y;
  1365. //-------------------------------------------------------------
  1366. // First, we need to calc the delta vector--how much we extend
  1367. // the ray everytime we check the map cell for clear placement.
  1368. deltaVector.normalize();
  1369. float cellLength = Terrain::metersPerVertex / (float)MAPCELL_DIM;
  1370. cellLength *= 0.5;
  1371. deltaVector *= cellLength;
  1372. if (deltaVector.magnitude() == 0.0)
  1373. return(curPos);
  1374. //-------------------------------------------------
  1375. // Determine the max length the ray must be cast...
  1376. float maxLength = start2d.distance_from(goal2d);
  1377. //------------------------------------------------------------
  1378. // We'll start at the target, and if it's blocked, we'll move
  1379. // toward our start location, looking for the first valid/open
  1380. // cell...
  1381. Stuff::Vector2DOf<float> curPoint = start2d;
  1382. Stuff::Vector2DOf<float> curRay;
  1383. curRay.zero();
  1384. float rayLength = 0.0;
  1385. long tileR, tileC, cellR, cellC;
  1386. Stuff::Vector3D curPoint3d;
  1387. curPoint3d.init(curPoint.x, curPoint.y, 0.0);
  1388. GameMap->worldToMapPos(curPoint3d, tileR, tileC, cellR, cellC);
  1389. bool cellClear = GameMap->cellPassable(tileR, tileC, cellR, cellC);
  1390. Stuff::Vector2DOf<float> lastGoodPoint = curPoint;
  1391. if (flags & RELPOS_FLAG_PASSABLE_START)
  1392. while (cellClear && (rayLength < maxLength)) {
  1393. lastGoodPoint = curPoint;
  1394. curPoint += deltaVector;
  1395. curRay = curPoint - start2d;
  1396. rayLength = curRay.magnitude();
  1397. curPoint3d.init(curPoint.x, curPoint.y, 0.0);
  1398. GameMap->worldToMapPos(curPoint3d, tileR, tileC, cellR, cellC);
  1399. cellClear = GameMap->cellPassable(tileR, tileC, cellR, cellC);
  1400. }
  1401. else
  1402. while (!cellClear && (rayLength < maxLength)) {
  1403. lastGoodPoint = curPoint;
  1404. curPoint += deltaVector;
  1405. curRay = curPoint - start2d;
  1406. rayLength = curRay.magnitude();
  1407. curPoint3d.init(curPoint.x, curPoint.y, 0.0);
  1408. GameMap->worldToMapPos(curPoint3d, tileR, tileC, cellR, cellC);
  1409. cellClear = GameMap->cellPassable(tileR, tileC, cellR, cellC);
  1410. }
  1411. curPoint3d.init(lastGoodPoint.x, lastGoodPoint.y, 0.0);
  1412. curPoint3d.z = GameMap->getTerrainElevation(curPoint3d);
  1413. return(curPoint3d);
  1414. #else
  1415. return(position);
  1416. #endif
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. void GameObject::setPosition (const Stuff::Vector3D& newPosition, bool calcPositions) {
  1420. position = newPosition;
  1421. if (calcPositions) {
  1422. long newCellRow = 0;
  1423. long newCellCol = 0;
  1424. long tileRow = 0;
  1425. long tileCol = 0;
  1426. land->worldToTileCell(position, tileRow, tileCol, newCellRow, newCellCol);
  1427. cellPositionRow = newCellRow + tileRow * MAPCELL_DIM;
  1428. cellPositionCol = newCellCol + tileCol * MAPCELL_DIM;
  1429. d_vertexNum = tileRow * Terrain::realVerticesMapSide + tileCol;
  1430. }
  1431. Assert((cellPositionRow >= 0) && (cellPositionRow < GameMap->getHeight()), 0, " Object moved off map ");
  1432. Assert((cellPositionCol >= 0) && (cellPositionCol < GameMap->getWidth()), 0, " Object moved off map ");
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. float GameObject::distanceFrom (Stuff::Vector3D goal) {
  1436. Stuff::Vector3D result;
  1437. result.x = position.x - goal.x;
  1438. result.y = position.y - goal.y;
  1439. result.z = 0.0;
  1440. return((result.GetLength() * metersPerWorldUnit));
  1441. }
  1442. //---------------------------------------------------------------------------
  1443. long GameObject::cellDistanceFrom (Stuff::Vector3D goal) {
  1444. long cellRow = 0;
  1445. long cellCol = 0;
  1446. land->worldToCell(goal, cellRow, cellCol);
  1447. long rowDelta = 0;
  1448. if (cellPositionRow > cellRow)
  1449. rowDelta = cellPositionRow - cellRow;
  1450. else
  1451. rowDelta = cellRow - cellPositionRow;
  1452. long colDelta = 0;
  1453. if (cellPositionCol > cellCol)
  1454. colDelta = cellPositionCol - cellCol;
  1455. else
  1456. colDelta = cellCol - cellPositionCol;
  1457. return(rowDelta > colDelta ? rowDelta : colDelta);
  1458. }
  1459. //---------------------------------------------------------------------------
  1460. long GameObject::cellDistanceFrom (GameObjectPtr obj) {
  1461. long rowDelta = 0;
  1462. if (cellPositionRow > obj->cellPositionRow)
  1463. rowDelta = cellPositionRow - obj->cellPositionRow;
  1464. else
  1465. rowDelta = obj->cellPositionRow - cellPositionRow;
  1466. long colDelta = 0;
  1467. if (cellPositionCol > obj->cellPositionCol)
  1468. colDelta = cellPositionCol - obj->cellPositionCol;
  1469. else
  1470. colDelta = obj->cellPositionCol - cellPositionCol;
  1471. return(rowDelta > colDelta ? rowDelta : colDelta);
  1472. }
  1473. //---------------------------------------------------------------------------
  1474. long GameObject::getLineOfSightNodes (long eyeCellRow, long eyeCellCol, long* cells) {
  1475. cells[0] = cellPositionRow;
  1476. cells[1] = cellPositionCol;
  1477. return(1);
  1478. }
  1479. //---------------------------------------------------------------------------
  1480. bool GameObject::lineOfSight (long cellRow, long cellCol, bool checkVisibleBits) {
  1481. bool LOSclear = Team::lineOfSight(0.0f,cellPositionRow, cellPositionCol, cellRow, cellCol, getTeamId(), 15.0, checkVisibleBits);
  1482. return(LOSclear);
  1483. }
  1484. //---------------------------------------------------------------------------
  1485. bool GameObject::lineOfSight (Stuff::Vector3D point, bool checkVisibleBits) {
  1486. Stuff::Vector3D firingPosition = getLOSPosition();
  1487. Stuff::Vector3D targetPosition = point;
  1488. bool LOSclear = false;
  1489. if (land->IsGameSelectTerrainPosition(point))
  1490. LOSclear = Team::lineOfSight(firingPosition, targetPosition, getTeamId(), 15.0, checkVisibleBits);
  1491. return(LOSclear);
  1492. }
  1493. //---------------------------------------------------------------------------
  1494. #ifdef LAB_ONLY
  1495. extern __int64 MCTimeLOSUpdate;
  1496. #endif
  1497. inline bool GameObject::lineOfSight (GameObjectPtr target, float startExtRad, bool checkVisibleBits)
  1498. {
  1499. __int64 timeStart = GetCycles();
  1500. //If we call this without a target, we have no LOS!!
  1501. // Keeps it from crashing, too.
  1502. // Not sure where all of the calls Glenn makes to this are, but I'm looking!
  1503. if (!target) {
  1504. #ifdef LAB_ONLY
  1505. MCTimeLOSUpdate += (GetCycles() - timeStart);
  1506. #endif
  1507. return false;
  1508. }
  1509. Stuff::Vector3D distance;
  1510. distance.Subtract(target->getPosition(),getPosition());
  1511. float dist = distance.GetApproximateLength();
  1512. //Figure out altitude above minimum terrain altitude and look up in table.
  1513. float baseElevation = MapData::waterDepth;
  1514. if (MapData::waterDepth < Terrain::userMin)
  1515. baseElevation = Terrain::userMin;
  1516. float altitude = position.z - baseElevation;
  1517. float altitudeIntegerRange = (Terrain::userMax - baseElevation) * 0.00390625f;
  1518. long altLevel = 0;
  1519. if (altitudeIntegerRange > Stuff::SMALL)
  1520. altLevel = altitude / altitudeIntegerRange;
  1521. if (altLevel < 0)
  1522. altLevel = 0;
  1523. if (altLevel > 255)
  1524. altLevel = 255;
  1525. float radius = visualRangeTable[altLevel];
  1526. //Scouting specialty skill.
  1527. if (isMover())
  1528. {
  1529. MoverPtr mover = (MoverPtr)this;
  1530. if (mover->pilot && mover->pilot->isScout())
  1531. radius += (radius * 0.2f);
  1532. radius *= mover->getLOSFactor();
  1533. }
  1534. if (dist > (radius * 25.0f * worldUnitsPerMeter))
  1535. {
  1536. #ifdef LAB_ONLY
  1537. MCTimeLOSUpdate += (GetCycles() - timeStart);
  1538. #endif
  1539. return false;
  1540. }
  1541. //--------------------------------------------------------------------------
  1542. // For now, we hardcode the "height" of the firer and target (to 10 meters).
  1543. // Easily changed when we add a height field to the object class...
  1544. if (target->isMover())
  1545. {
  1546. if (isMover() && ObjectManager->useMoverLineOfSightTable)
  1547. {
  1548. long index = (handle * ObjectManager->maxMovers) + target->handle;
  1549. char losStatus = ObjectManager->moverLineOfSightTable[index];
  1550. if (losStatus == -1)
  1551. {
  1552. bool los = Team::lineOfSight(getLOSPosition(), target->getLOSPosition(), getTeamId(), target->getAppearRadius(), startExtRad, checkVisibleBits);
  1553. if (los)
  1554. {
  1555. ObjectManager->moverLineOfSightTable[index] = 1;
  1556. //Inverse is NOT always true!!!!
  1557. // I can demonstrate a case!!
  1558. // -fs
  1559. // ObjectManager->moverLineOfSightTable[(target->handle * ObjectManager->maxMovers) + handle] = 1;
  1560. }
  1561. else
  1562. {
  1563. ObjectManager->moverLineOfSightTable[index] = 0;
  1564. // ObjectManager->moverLineOfSightTable[(target->handle * ObjectManager->maxMovers) + handle] = 0;
  1565. }
  1566. }
  1567. if (ObjectManager->moverLineOfSightTable[index])
  1568. {
  1569. #ifdef LAB_ONLY
  1570. MCTimeLOSUpdate += (GetCycles() - timeStart);
  1571. #endif
  1572. return true;
  1573. }
  1574. else
  1575. {
  1576. #ifdef LAB_ONLY
  1577. MCTimeLOSUpdate += (GetCycles() - timeStart);
  1578. #endif
  1579. return false;
  1580. }
  1581. }
  1582. if (Team::lineOfSight(getLOSPosition(), target->getLOSPosition(), getTeamId(), target->getAppearRadius(), startExtRad, checkVisibleBits))
  1583. {
  1584. #ifdef LAB_ONLY
  1585. MCTimeLOSUpdate += (GetCycles() - timeStart);
  1586. #endif
  1587. return(true);
  1588. }
  1589. else
  1590. {
  1591. #ifdef LAB_ONLY
  1592. MCTimeLOSUpdate += (GetCycles() - timeStart);
  1593. #endif
  1594. return(false);
  1595. }
  1596. }
  1597. //Try yanking these and just use the location the weapon fire is going to.
  1598. // With the extent radius, like I originally did.
  1599. // -fs
  1600. //long lineOfSightNodes[20];
  1601. //long numNodes = target->getLineOfSightNodes(cellPositionRow, cellPositionCol, lineOfSightNodes);
  1602. // for (long i = 0; i < numNodes; i++)
  1603. // {
  1604. // if (land->IsGameSelectTerrainPosition(getLOSPosition()))
  1605. // {
  1606. float elev = land->getTerrainElevation(getLOSPosition());
  1607. float localStart = getLOSPosition().z - elev;
  1608. Stuff::Vector3D targetPosition = target->getLOSPosition();
  1609. if (Team::lineOfSight(getLOSPosition(), target->getLOSPosition(), getTeamId(), target->getAppearRadius(), startExtRad, checkVisibleBits))
  1610. {
  1611. #ifdef LAB_ONLY
  1612. MCTimeLOSUpdate += (GetCycles() - timeStart);
  1613. #endif
  1614. return(true);
  1615. }
  1616. // }
  1617. // }
  1618. #ifdef LAB_ONLY
  1619. MCTimeLOSUpdate += (GetCycles() - timeStart);
  1620. #endif
  1621. return(false);
  1622. }
  1623. //---------------------------------------------------------------------------
  1624. void GameObject::destroy (void) {
  1625. //Never need to call this. Heap destruct will get this!!
  1626. // ObjectManager->removeObjectType(typeHandle);
  1627. }
  1628. //---------------------------------------------------------------------------
  1629. bool GameObject::onScreen (void) {
  1630. //----------------------------------------------------------------------
  1631. // This function is the meat and potatoes of the object cull system.
  1632. // Its job is to determine if the object is on screen or not.
  1633. // It does this by transforming the position for each active camera to
  1634. // its screen coords and saving them. It then checks each set of coords
  1635. // to see if they are in the viewport of each camera. Returned value
  1636. // is number of windows that object can be seen in.
  1637. long isVisible = 0;
  1638. if (eye) {
  1639. Stuff::Vector3D objPosition = position;
  1640. isVisible = eye->projectZ(objPosition, screenPos);
  1641. }
  1642. if (isVisible) {
  1643. windowsVisible = turn;
  1644. return(true);
  1645. }
  1646. return(false);
  1647. }
  1648. //---------------------------------------------------------------------------
  1649. float GameObject::getExtentRadius (void) {
  1650. //---------------------------------------------------------------------
  1651. // Can be overridden by explosions to return an instance based value.
  1652. ObjectTypePtr objType = ObjectManager->getObjectType(typeHandle);
  1653. if (objType)
  1654. return(objType->getExtentRadius());
  1655. return(-1.0);
  1656. }
  1657. //---------------------------------------------------------------------------
  1658. void GameObject::setExtentRadius (float newRadius) {
  1659. //---------------------------------------------------------------------
  1660. // Can be overridden by explosions to set an instance based value.
  1661. ObjectTypePtr objType = ObjectManager->getObjectType(typeHandle);
  1662. if (objType)
  1663. objType->setExtentRadius(newRadius);
  1664. }
  1665. //---------------------------------------------------------------------------
  1666. MechClass GameObject::getMechClass(void) {
  1667. if (getObjectClass() != BATTLEMECH)
  1668. return(MECH_CLASS_NONE);
  1669. if (tonnage < MechClassWeights[MECH_CLASS_LIGHT])
  1670. return(MECH_CLASS_LIGHT);
  1671. if (tonnage < MechClassWeights[MECH_CLASS_MEDIUM])
  1672. return(MECH_CLASS_MEDIUM);
  1673. if (tonnage < MechClassWeights[MECH_CLASS_HEAVY])
  1674. return(MECH_CLASS_HEAVY);
  1675. return(MECH_CLASS_ASSAULT);
  1676. }
  1677. //---------------------------------------------------------------------------
  1678. long GameObject::getCaptureBlocker (GameObjectPtr capturingMover, GameObjectPtr* blockerList) {
  1679. long numBlockers = 0;
  1680. TeamPtr capturingTeam = capturingMover->getTeam();
  1681. if (!capturingTeam)
  1682. STOP(("GameObject.getCaptureBlocker: NULL capturingTeam"));
  1683. if (distanceFrom(capturingMover->getPosition()) <= 30.0) {
  1684. for (long i = 0; i < ObjectManager->getNumMovers(); i++) {
  1685. MoverPtr mover = ObjectManager->getMover(i);
  1686. if (capturingTeam->isEnemy(mover->getTeam()))
  1687. if (!mover->isMarine() && (mover->numWeapons > 0))
  1688. if ((distanceFrom(mover->getPosition()) < blockCaptureRange) && !mover->isDisabled() && mover->getAwake())
  1689. if (blockerList)
  1690. blockerList[numBlockers++] = mover;
  1691. else
  1692. return(1);
  1693. }
  1694. }
  1695. else {
  1696. for (long i = 0; i < ObjectManager->getNumMovers(); i++) {
  1697. MoverPtr mover = ObjectManager->getMover(i);
  1698. if (!mover->getTeam() || capturingTeam->isEnemy(mover->getTeam()))
  1699. if (!mover->isMarine() && (mover->numWeapons > 0))
  1700. if ((distanceFrom(mover->getPosition()) < blockCaptureRange) && !mover->isDisabled() && mover->getAwake())
  1701. if (capturingMover->getTeam()->isContact(capturingMover, mover, CONTACT_CRITERIA_VISUAL_OR_SENSOR + CONTACT_CRITERIA_ENEMY + CONTACT_CRITERIA_NOT_DISABLED))
  1702. if (blockerList)
  1703. blockerList[numBlockers++] = mover;
  1704. else
  1705. return(1);
  1706. }
  1707. }
  1708. return(numBlockers);
  1709. }
  1710. //---------------------------------------------------------------------------
  1711. bool GameObject::isFriendly (GameObjectPtr obj) {
  1712. TeamPtr myTeam = getTeam();
  1713. TeamPtr objTeam = obj->getTeam();
  1714. if (myTeam && objTeam)
  1715. return(myTeam->isFriendly(objTeam));
  1716. return(false);
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. bool GameObject::isEnemy (GameObjectPtr obj) {
  1720. TeamPtr myTeam = getTeam();
  1721. TeamPtr objTeam = obj->getTeam();
  1722. if (myTeam && objTeam)
  1723. return(myTeam->isEnemy(objTeam));
  1724. return(false);
  1725. }
  1726. //---------------------------------------------------------------------------
  1727. bool GameObject::isNeutral (GameObjectPtr obj) {
  1728. TeamPtr myTeam = getTeam();
  1729. TeamPtr objTeam = obj->getTeam();
  1730. if (myTeam && objTeam)
  1731. return(myTeam->isNeutral(objTeam));
  1732. return(false);
  1733. }
  1734. //---------------------------------------------------------------------------
  1735. void GameObject::CopyTo (GameObjectData *data)
  1736. {
  1737. if (getObjectType())
  1738. data->objectTypeNum = getObjectType()->getObjTypeNum();
  1739. else
  1740. data->objectTypeNum = 0;
  1741. data->objectClass = objectClass;
  1742. data->handle = handle;
  1743. data->partId = partId;
  1744. data->watchID = watchID;
  1745. data->typeHandle = typeHandle;
  1746. data->position = position;
  1747. data->cellPositionRow = cellPositionRow;
  1748. data->cellPositionCol = cellPositionCol;
  1749. data->d_vertexNum = d_vertexNum;
  1750. data->flags = flags;
  1751. data->debugFlags = debugFlags;
  1752. data->status = status;
  1753. data->tonnage = tonnage;
  1754. data->rotation = rotation;
  1755. if (getObjectType() && getObjectType()->getAppearanceTypeName())
  1756. {
  1757. if (strlen(getObjectType()->getAppearanceTypeName()) <= 255)
  1758. strcpy(data->appearanceTypeID,getObjectType()->getAppearanceTypeName());
  1759. else
  1760. STOP(("Object Appearance name too long for Save. %s",getObjectType()->getAppearanceTypeName()));
  1761. }
  1762. else
  1763. {
  1764. strcpy(data->appearanceTypeID,"NONE");
  1765. }
  1766. data->collisionFreeFromWID = collisionFreeFromWID;
  1767. data->collisionFreeTime = collisionFreeTime;
  1768. data->screenPos = screenPos;
  1769. data->windowsVisible = windowsVisible;
  1770. data->explRadius = explRadius;
  1771. data->explDamage = explDamage;
  1772. data->maxCV = maxCV;
  1773. data->curCV = curCV;
  1774. data->threatRating = threatRating;
  1775. data->lastFrameTime = lastFrameTime;
  1776. data->blipFrame = blipFrame;
  1777. data->numAttackers = numAttackers;
  1778. data->drawFlags = drawFlags;
  1779. }
  1780. //---------------------------------------------------------------------------
  1781. void GameObject::Save (PacketFilePtr file, long packetNum)
  1782. {
  1783. STOP(("Should never save a gameObj!!"));
  1784. }
  1785. //---------------------------------------------------------------------------
  1786. void GameObject::Load (GameObjectData *data)
  1787. {
  1788. objectClass = data->objectClass;
  1789. handle = data->handle;
  1790. partId = data->partId;
  1791. watchID = data->watchID;
  1792. position = data->position;
  1793. cellPositionRow = data->cellPositionRow;
  1794. cellPositionCol = data->cellPositionCol;
  1795. d_vertexNum = data->d_vertexNum;
  1796. flags = data->flags;
  1797. debugFlags = data->debugFlags;
  1798. status = data->status;
  1799. tonnage = data->tonnage;
  1800. rotation = data->rotation;
  1801. collisionFreeFromWID = data->collisionFreeFromWID;
  1802. collisionFreeTime = data->collisionFreeTime;
  1803. screenPos = data->screenPos;
  1804. windowsVisible = 0; //Force back to zero so I don't have to save the turn.
  1805. explRadius = data->explRadius;
  1806. explDamage = data->explDamage;
  1807. maxCV = data->maxCV;
  1808. curCV = data->curCV;
  1809. threatRating = data->threatRating;
  1810. lastFrameTime = data->lastFrameTime;
  1811. blipFrame = data->blipFrame;
  1812. numAttackers = data->numAttackers;
  1813. drawFlags = data->drawFlags;
  1814. }
  1815. //***************************************************************************