DisplayCover.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. #ifdef PRECOMPILEDHEADERS
  2. #include "AI All.h"
  3. #include "DisplayCover.h"
  4. #include "Interface.h"
  5. #include "_Ja25Englishtext.h"
  6. //#include "Ja25 Strategic Ai.h"
  7. #endif
  8. //******* Local Defines **************************************************
  9. #define DC_MAX_COVER_RANGE 31
  10. #define DC__SOLDIER_VISIBLE_RANGE 31
  11. #define DC__MIN_SIZE 4
  12. #define DC__MAX_SIZE 11
  13. typedef struct
  14. {
  15. INT16 sGridNo;
  16. INT8 bCover; //% chance that the gridno is fully covered. ie 100 if safe, 0 is has no cover
  17. // BOOLEAN fRoof;
  18. } BEST_COVER_STRUCT;
  19. typedef struct
  20. {
  21. INT16 sGridNo;
  22. INT8 bVisibleToSoldier;
  23. BOOLEAN fRoof;
  24. } VISIBLE_TO_SOLDIER_STRUCT;
  25. /*
  26. #define DC__PRONE (INT8)( 0x01 )
  27. #define DC__CROUCH (INT8)( 0x02 )
  28. #define DC__STAND (INT8)( 0x04 )
  29. */
  30. enum
  31. {
  32. DC__SEE_NO_STANCES,
  33. DC__SEE_1_STANCE,
  34. DC__SEE_2_STANCE,
  35. DC__SEE_3_STANCE,
  36. };
  37. //****** Global Variables *****************************************
  38. BEST_COVER_STRUCT gCoverRadius[ DC_MAX_COVER_RANGE ][ DC_MAX_COVER_RANGE ];
  39. INT16 gsLastCoverGridNo=NOWHERE;
  40. INT16 gsLastSoldierGridNo=NOWHERE;
  41. INT8 gbLastStance=-1;
  42. VISIBLE_TO_SOLDIER_STRUCT gVisibleToSoldierStruct[ DC__SOLDIER_VISIBLE_RANGE ][ DC__SOLDIER_VISIBLE_RANGE ];
  43. INT16 gsLastVisibleToSoldierGridNo=NOWHERE;
  44. //******* Function Prototypes ***************************************
  45. INT8 CalcCoverForGridNoBasedOnTeamKnownEnemies( SOLDIERTYPE *pSoldier, INT16 sTargetGridno, INT8 bStance );
  46. void CalculateCoverInRadiusAroundGridno( INT16 sTargetGridNo, INT8 bSearchRange );
  47. void AddCoverTileToEachGridNo();
  48. void AddCoverObjectToWorld( INT16 sGridNo, UINT16 usGraphic, BOOLEAN fRoof );
  49. void RemoveCoverObjectFromWorld( INT16 sGridNo, UINT16 usGraphic, BOOLEAN fRoof );
  50. INT8 GetCurrentMercForDisplayCoverStance();
  51. SOLDIERTYPE *GetCurrentMercForDisplayCover();
  52. void CalculateVisibleToSoldierAroundGridno( INT16 sGridNo, INT8 bSearchRange );
  53. void AddVisibleToSoldierToEachGridNo();
  54. INT8 CalcIfSoldierCanSeeGridNo( SOLDIERTYPE *pSoldier, INT16 sTargetGridNo, BOOLEAN fRoof );
  55. BOOLEAN IsTheRoofVisible( INT16 sGridNo );
  56. //ppp
  57. //******* Functions **************************************************
  58. void DisplayCoverOfSelectedGridNo( )
  59. {
  60. INT16 sGridNo;
  61. INT8 bStance;
  62. GetMouseMapPos( &sGridNo );
  63. //Only allowed in if there is someone selected
  64. if( gusSelectedSoldier == NOBODY )
  65. {
  66. return;
  67. }
  68. //if the cursor is in a the tactical map
  69. if( sGridNo != NOWHERE && sGridNo != 0 )
  70. {
  71. bStance = GetCurrentMercForDisplayCoverStance();
  72. //if the gridno is different then the last one that was displayed
  73. if( sGridNo != gsLastCoverGridNo ||
  74. gbLastStance != bStance ||
  75. MercPtrs[ gusSelectedSoldier ]->sGridNo != gsLastSoldierGridNo )
  76. {
  77. //if the cover is currently being displayed
  78. if( gsLastCoverGridNo != NOWHERE || gbLastStance != -1 || gsLastSoldierGridNo != NOWHERE )
  79. {
  80. //remove the gridnos
  81. RemoveCoverOfSelectedGridNo();
  82. }
  83. else
  84. {
  85. //if it is the first time in here
  86. //pop up a message to say we are in the display cover routine
  87. #ifdef JA2TESTVERSION
  88. {
  89. CHAR16 zString[512];
  90. swprintf( zString, L"%s, (%d)", zNewTacticalMessages[ TCTL_MSG__DISPLAY_COVER ], gGameSettings.ubSizeOfDisplayCover );
  91. ScreenMsg( FONT_MCOLOR_LTYELLOW, MSG_INTERFACE, zString );
  92. }
  93. #else
  94. ScreenMsg( FONT_MCOLOR_LTYELLOW, MSG_INTERFACE, zNewTacticalMessages[ TCTL_MSG__DISPLAY_COVER ] );
  95. #endif
  96. //increment the display cover counter ( just seeing how many times people use it )
  97. //gJa25SaveStruct.uiDisplayCoverCounter++;
  98. }
  99. gbLastStance = bStance;
  100. gsLastCoverGridNo = sGridNo;
  101. gsLastSoldierGridNo = MercPtrs[ gusSelectedSoldier ]->sGridNo;
  102. //Fill the array of gridno and cover values
  103. CalculateCoverInRadiusAroundGridno( sGridNo, gGameSettings.ubSizeOfDisplayCover );
  104. //Add the graphics to each gridno
  105. AddCoverTileToEachGridNo();
  106. // Re-render the scene!
  107. SetRenderFlags( RENDER_FLAG_FULL );
  108. }
  109. }
  110. }
  111. void AddCoverTileToEachGridNo()
  112. {
  113. UINT32 uiCntX, uiCntY;
  114. BOOLEAN fRoof = ( gsInterfaceLevel != I_GROUND_LEVEL );
  115. //loop through all the gridnos
  116. for(uiCntY=0; uiCntY<DC_MAX_COVER_RANGE ;uiCntY++)
  117. {
  118. for(uiCntX=0; uiCntX<DC_MAX_COVER_RANGE ;uiCntX++)
  119. {
  120. //if there is a valid cover at this gridno
  121. if( gCoverRadius[ uiCntX ][ uiCntY ].bCover != -1 )
  122. {
  123. //if the tile provides 80-100% cover
  124. if( gCoverRadius[ uiCntX ][ uiCntY ].bCover <= 100 &&
  125. gCoverRadius[ uiCntX ][ uiCntY ].bCover > 80 )
  126. {
  127. AddCoverObjectToWorld( gCoverRadius[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_5, fRoof );
  128. }
  129. //else if the tile provides 60-80% cover
  130. else if( gCoverRadius[ uiCntX ][ uiCntY ].bCover <= 80 &&
  131. gCoverRadius[ uiCntX ][ uiCntY ].bCover > 60 )
  132. {
  133. AddCoverObjectToWorld( gCoverRadius[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_4, fRoof );
  134. }
  135. //else if the tile provides 40-60% cover
  136. else if( gCoverRadius[ uiCntX ][ uiCntY ].bCover <= 60 &&
  137. gCoverRadius[ uiCntX ][ uiCntY ].bCover > 40 )
  138. {
  139. AddCoverObjectToWorld( gCoverRadius[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_3, fRoof );
  140. }
  141. //else if the tile provides 20-40% cover
  142. else if( gCoverRadius[ uiCntX ][ uiCntY ].bCover <= 40 &&
  143. gCoverRadius[ uiCntX ][ uiCntY ].bCover > 20 )
  144. {
  145. AddCoverObjectToWorld( gCoverRadius[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_2, fRoof );
  146. }
  147. //else if the tile provides 0-20% cover
  148. else if( gCoverRadius[ uiCntX ][ uiCntY ].bCover <= 20 &&
  149. gCoverRadius[ uiCntX ][ uiCntY ].bCover >= 0 )
  150. {
  151. AddCoverObjectToWorld( gCoverRadius[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_1, fRoof );
  152. }
  153. //should never get in here
  154. else
  155. {
  156. Assert( 0 );
  157. }
  158. }
  159. }
  160. }
  161. }
  162. void RemoveCoverOfSelectedGridNo()
  163. {
  164. UINT32 uiCntX, uiCntY;
  165. BOOLEAN fRoof = ( gsInterfaceLevel != I_GROUND_LEVEL );
  166. if( gsLastCoverGridNo == NOWHERE )
  167. {
  168. return;
  169. }
  170. //loop through all the gridnos
  171. for(uiCntY=0; uiCntY<DC_MAX_COVER_RANGE ;uiCntY++)
  172. {
  173. for(uiCntX=0; uiCntX<DC_MAX_COVER_RANGE ;uiCntX++)
  174. {
  175. //if there is a valid cover at this gridno
  176. if( gCoverRadius[ uiCntX ][ uiCntY ].bCover != -1 )
  177. {
  178. // fRoof = gCoverRadius[ uiCntX ][ uiCntY ].fRoof;
  179. //if the tile provides 80-100% cover
  180. if( gCoverRadius[ uiCntX ][ uiCntY ].bCover <= 100 &&
  181. gCoverRadius[ uiCntX ][ uiCntY ].bCover > 80 )
  182. {
  183. RemoveCoverObjectFromWorld( gCoverRadius[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_5, fRoof );
  184. }
  185. //else if the tile provides 60-80% cover
  186. else if( gCoverRadius[ uiCntX ][ uiCntY ].bCover <= 80 &&
  187. gCoverRadius[ uiCntX ][ uiCntY ].bCover > 60 )
  188. {
  189. RemoveCoverObjectFromWorld( gCoverRadius[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_4, fRoof );
  190. }
  191. //else if the tile provides 40-60% cover
  192. else if( gCoverRadius[ uiCntX ][ uiCntY ].bCover <= 60 &&
  193. gCoverRadius[ uiCntX ][ uiCntY ].bCover > 40 )
  194. {
  195. RemoveCoverObjectFromWorld( gCoverRadius[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_3, fRoof );
  196. }
  197. //else if the tile provides 20-40% cover
  198. else if( gCoverRadius[ uiCntX ][ uiCntY ].bCover <= 40 &&
  199. gCoverRadius[ uiCntX ][ uiCntY ].bCover > 20 )
  200. {
  201. RemoveCoverObjectFromWorld( gCoverRadius[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_2, fRoof );
  202. }
  203. //else if the tile provides 0-20% cover
  204. else if( gCoverRadius[ uiCntX ][ uiCntY ].bCover <= 20 &&
  205. gCoverRadius[ uiCntX ][ uiCntY ].bCover >= 0 )
  206. {
  207. RemoveCoverObjectFromWorld( gCoverRadius[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_1, fRoof );
  208. }
  209. //should never get in here
  210. else
  211. {
  212. Assert( 0 );
  213. }
  214. }
  215. }
  216. }
  217. // Re-render the scene!
  218. SetRenderFlags( RENDER_FLAG_FULL );
  219. gsLastCoverGridNo = NOWHERE;
  220. gbLastStance = -1;
  221. gsLastSoldierGridNo = NOWHERE;
  222. }
  223. void CalculateCoverInRadiusAroundGridno( INT16 sTargetGridNo, INT8 bSearchRange )
  224. {
  225. INT16 sMaxLeft, sMaxRight, sMaxUp, sMaxDown, sXOffset, sYOffset;
  226. SOLDIERTYPE *pSoldier=NULL;
  227. INT16 sGridNo;
  228. INT16 sCounterX, sCounterY;
  229. UINT8 ubID;
  230. INT8 bStance;
  231. // BOOLEAN fRoof;
  232. //clear out the array first
  233. // memset( gCoverRadius, -1, DC_MAX_COVER_RANGE * DC_MAX_COVER_RANGE );
  234. //loop through all the gridnos that we are interested in
  235. for (sCounterY = 0; sCounterY < DC_MAX_COVER_RANGE; sCounterY++)
  236. {
  237. for (sCounterX = 0; sCounterX < DC_MAX_COVER_RANGE; sCounterX++)
  238. {
  239. gCoverRadius[ sCounterX ][ sCounterY ].sGridNo = -1;
  240. gCoverRadius[ sCounterX ][ sCounterY ].bCover = -1;
  241. }
  242. }
  243. if( bSearchRange > ( DC_MAX_COVER_RANGE / 2 ) )
  244. bSearchRange = ( DC_MAX_COVER_RANGE / 2 );
  245. // determine maximum horizontal limits
  246. sMaxLeft = min( bSearchRange,( sTargetGridNo % MAXCOL ));
  247. sMaxRight = min( bSearchRange,MAXCOL - (( sTargetGridNo % MAXCOL ) + 1));
  248. // determine maximum vertical limits
  249. sMaxUp = min( bSearchRange,( sTargetGridNo / MAXROW ));
  250. sMaxDown = min( bSearchRange,MAXROW - (( sTargetGridNo / MAXROW ) + 1));
  251. //Find out which tiles around the location are reachable
  252. LocalReachableTest( sTargetGridNo, bSearchRange );
  253. pSoldier = GetCurrentMercForDisplayCover();
  254. sCounterX = sCounterY = 0;
  255. //Determine the stance to use
  256. bStance = GetCurrentMercForDisplayCoverStance();
  257. //loop through all the gridnos that we are interested in
  258. for (sYOffset = -sMaxUp; sYOffset <= sMaxDown; sYOffset++)
  259. {
  260. for (sXOffset = -sMaxLeft; sXOffset <= sMaxRight; sXOffset++)
  261. {
  262. sGridNo = sTargetGridNo + sXOffset + (MAXCOL * sYOffset);
  263. //record the gridno
  264. gCoverRadius[ sCounterX ][ sCounterY ].sGridNo = sGridNo;
  265. /*
  266. fRoof = FALSE;
  267. //is there a roof above this gridno
  268. if( FlatRoofAboveGridNo( sGridNo ) )
  269. {
  270. if( IsTheRoofVisible( sGridNo ) )
  271. {
  272. fRoof = TRUE;
  273. }
  274. }
  275. */
  276. //if the gridno is NOT on screen
  277. if( !GridNoOnScreen( sGridNo ) )
  278. {
  279. continue;
  280. }
  281. //if we are to display cover for the roofs, and there is a roof above us
  282. if( gsInterfaceLevel == I_ROOF_LEVEL && !FlatRoofAboveGridNo( sGridNo ) )
  283. {
  284. continue;
  285. }
  286. //if the gridno cant be reached
  287. if ( !(gpWorldLevelData[sGridNo].uiFlags & MAPELEMENT_REACHABLE) )
  288. {
  289. //skip to the next gridno
  290. sCounterX++;
  291. continue;
  292. }
  293. // if someone (visible) is there, skip
  294. //Check both bottom level, and top level
  295. ubID = WhoIsThere2( sGridNo, 0 );
  296. if( ubID == NOBODY )
  297. {
  298. ubID = WhoIsThere2( sGridNo, 1 );
  299. }
  300. //if someone is here, and they are an enemy, skip over them
  301. if ( ubID != NOBODY && Menptr[ ubID ].bVisible == TRUE && Menptr[ ubID ].bTeam != pSoldier->bTeam )
  302. {
  303. continue;
  304. }
  305. //Calculate the cover for this gridno
  306. gCoverRadius[ sCounterX ][ sCounterY ].bCover = CalcCoverForGridNoBasedOnTeamKnownEnemies( pSoldier, sGridNo, bStance );
  307. // gCoverRadius[ sCounterX ][ sCounterY ].fRoof = fRoof;
  308. sCounterX++;
  309. }
  310. sCounterY++;
  311. sCounterX = 0;
  312. }
  313. }
  314. INT8 CalcCoverForGridNoBasedOnTeamKnownEnemies( SOLDIERTYPE *pSoldier, INT16 sTargetGridNo, INT8 bStance )
  315. {
  316. INT32 iTotalCoverPoints=0;
  317. INT8 bNumEnemies=0;
  318. INT8 bPercentCoverForGridno=0;
  319. UINT32 uiLoop;
  320. SOLDIERTYPE *pOpponent;
  321. INT8 *pbPersOL;
  322. INT8 *pbPublOL;
  323. INT32 iGetThrough=0;
  324. INT32 iBulletGetThrough=0;
  325. INT32 iHighestValue=0;
  326. INT32 iCover=0;
  327. UINT16 usMaxRange;
  328. UINT16 usRange;
  329. UINT16 usSightLimit;
  330. //loop through all the enemies and determine the cover
  331. for (uiLoop = 0; uiLoop < guiNumMercSlots; uiLoop++)
  332. {
  333. pOpponent = MercSlots[ uiLoop ];
  334. // if this merc is inactive, at base, on assignment, dead, unconscious
  335. if (!pOpponent || pOpponent->bLife < OKLIFE)
  336. {
  337. continue; // next merc
  338. }
  339. // if this man is neutral / on the same side, he's not an opponent
  340. if( CONSIDERED_NEUTRAL( pSoldier, pOpponent ) || (pSoldier->bSide == pOpponent->bSide))
  341. {
  342. continue; // next merc
  343. }
  344. pbPersOL = pSoldier->bOppList + pOpponent->ubID;
  345. pbPublOL = gbPublicOpplist[ OUR_TEAM ] + pOpponent->ubID;
  346. // if this opponent is unknown personally and publicly
  347. if( *pbPersOL != SEEN_CURRENTLY &&
  348. *pbPersOL != SEEN_THIS_TURN &&
  349. *pbPublOL != SEEN_CURRENTLY &&
  350. *pbPublOL != SEEN_THIS_TURN )
  351. {
  352. continue; // next merc
  353. }
  354. usRange = (UINT16)GetRangeInCellCoordsFromGridNoDiff( pOpponent->sGridNo, sTargetGridNo );
  355. usSightLimit = DistanceVisible( pOpponent, DIRECTION_IRRELEVANT, DIRECTION_IRRELEVANT, sTargetGridNo, pSoldier->bLevel );
  356. if( usRange > ( usSightLimit * CELL_X_SIZE ) )
  357. {
  358. continue;
  359. }
  360. // if actual LOS check fails, then chance to hit is 0, ignore this guy
  361. if( SoldierToVirtualSoldierLineOfSightTest( pOpponent, sTargetGridNo, pSoldier->bLevel, bStance, (UINT8)usSightLimit, TRUE ) == 0 )
  362. {
  363. continue;
  364. }
  365. iGetThrough = SoldierToLocationChanceToGetThrough( pOpponent, sTargetGridNo, pSoldier->bLevel, bStance, NOBODY );
  366. // iBulletGetThrough = CalcChanceToHitGun( pOpponent, sTargetGridNo, AP_MAX_AIM_ATTACK, AIM_SHOT_TORSO );
  367. if( WeaponInHand( pOpponent ) )
  368. {
  369. usMaxRange = GunRange( &pOpponent->inv[ HANDPOS ] );
  370. }
  371. else
  372. {
  373. usMaxRange = Weapon[ GLOCK_18 ].usRange;
  374. }
  375. iBulletGetThrough = __min( __max( (INT32)( ( ( ( ( usMaxRange - usRange ) / (FLOAT)( usMaxRange ) ) + .3 ) * 100 ) ), 0 ), 100 );
  376. if( iBulletGetThrough > 5 && iGetThrough > 0 )
  377. {
  378. iCover = (iGetThrough * iBulletGetThrough / 100);
  379. if( iCover > iHighestValue )
  380. iHighestValue = iCover;
  381. iTotalCoverPoints += iCover;
  382. bNumEnemies++;
  383. }
  384. }
  385. if( bNumEnemies == 0 )
  386. {
  387. bPercentCoverForGridno = 100;
  388. }
  389. else
  390. {
  391. INT32 iTemp;
  392. bPercentCoverForGridno = ( iTotalCoverPoints / bNumEnemies );
  393. iTemp = bPercentCoverForGridno - ( iHighestValue / bNumEnemies );
  394. iTemp = iTemp + iHighestValue;
  395. bPercentCoverForGridno = 100 - ( __min( iTemp, 100 ) );
  396. }
  397. return( bPercentCoverForGridno );
  398. }
  399. void AddCoverObjectToWorld( INT16 sGridNo, UINT16 usGraphic, BOOLEAN fRoof )
  400. {
  401. LEVELNODE *pNode;
  402. if( fRoof )
  403. {
  404. AddOnRoofToHead( sGridNo, usGraphic );
  405. pNode = gpWorldLevelData[ sGridNo ].pOnRoofHead;
  406. }
  407. else
  408. {
  409. AddObjectToHead( sGridNo, usGraphic );
  410. pNode = gpWorldLevelData[ sGridNo ].pObjectHead;
  411. }
  412. pNode->uiFlags |= LEVELNODE_REVEAL;
  413. if( NightTime() )
  414. {
  415. pNode->ubShadeLevel=DEFAULT_SHADE_LEVEL;
  416. pNode->ubNaturalShadeLevel=DEFAULT_SHADE_LEVEL;
  417. }
  418. }
  419. void RemoveCoverObjectFromWorld( INT16 sGridNo, UINT16 usGraphic, BOOLEAN fRoof )
  420. {
  421. if( fRoof )
  422. {
  423. RemoveOnRoof( sGridNo, usGraphic );
  424. }
  425. else
  426. {
  427. RemoveObject( sGridNo, usGraphic );
  428. }
  429. }
  430. SOLDIERTYPE *GetCurrentMercForDisplayCover()
  431. {
  432. SOLDIERTYPE *pSoldier=NULL;
  433. //Get a soldier that is on the player team
  434. if( gusSelectedSoldier != NOBODY )
  435. {
  436. GetSoldier( &pSoldier, gusSelectedSoldier );
  437. }
  438. else
  439. {
  440. Assert( 0 );
  441. }
  442. return( pSoldier );
  443. }
  444. INT8 GetCurrentMercForDisplayCoverStance()
  445. {
  446. INT8 bStance;
  447. SOLDIERTYPE *pSoldier = NULL;
  448. pSoldier = GetCurrentMercForDisplayCover();
  449. switch( pSoldier->usUIMovementMode )
  450. {
  451. case PRONE:
  452. case CRAWLING:
  453. bStance = ANIM_PRONE;
  454. break;
  455. case KNEEL_DOWN:
  456. case SWATTING:
  457. case CROUCHING:
  458. bStance = ANIM_CROUCH;
  459. break;
  460. case WALKING:
  461. case RUNNING:
  462. case STANDING:
  463. bStance = ANIM_STAND;
  464. break;
  465. default:
  466. bStance = ANIM_CROUCH;
  467. break;
  468. }
  469. return( bStance );
  470. }
  471. void DisplayRangeToTarget( SOLDIERTYPE *pSoldier, INT16 sTargetGridNo )
  472. {
  473. UINT16 usRange=0;
  474. CHAR16 zOutputString[512];
  475. if( sTargetGridNo == NOWHERE || sTargetGridNo == 0 )
  476. {
  477. return;
  478. }
  479. //Get the range to the target location
  480. usRange = GetRangeInCellCoordsFromGridNoDiff( pSoldier->sGridNo, sTargetGridNo );
  481. usRange = usRange / 10;
  482. //if the soldier has a weapon in hand,
  483. if( WeaponInHand( pSoldier ) )
  484. {
  485. //display a string with the weapons range, then range to target
  486. swprintf( zOutputString, zNewTacticalMessages[ TCTL_MSG__RANGE_TO_TARGET_AND_GUN_RANGE ], Weapon[ pSoldier->inv[HANDPOS].usItem ].usRange / 10, usRange );
  487. }
  488. else
  489. {
  490. //display a string with the range to target
  491. swprintf( zOutputString, zNewTacticalMessages[ TCTL_MSG__RANGE_TO_TARGET ], usRange );
  492. }
  493. //Display the msg
  494. ScreenMsg( FONT_MCOLOR_LTYELLOW, MSG_INTERFACE, zOutputString );
  495. //if the target is out of the mercs gun range or knife
  496. if( !InRange( pSoldier, sTargetGridNo ) &&
  497. ( Item[ pSoldier->inv[HANDPOS].usItem ].usItemClass == IC_GUN || Item[ pSoldier->inv[HANDPOS].usItem ].usItemClass == IC_THROWING_KNIFE ) )
  498. {
  499. // Display a warning saying so
  500. ScreenMsg( FONT_MCOLOR_LTYELLOW, MSG_INTERFACE, TacticalStr[ OUT_OF_RANGE_STRING ] );
  501. }
  502. //increment the display gun range counter ( just seeing how many times people use it )
  503. //gJa25SaveStruct.uiDisplayGunRangeCounter++;
  504. }
  505. void DisplayGridNoVisibleToSoldierGrid( )
  506. {
  507. INT16 sGridNo;
  508. // INT8 bStance;
  509. GetMouseMapPos( &sGridNo );
  510. //Only allowed in if there is someone selected
  511. if( gusSelectedSoldier == NOBODY )
  512. {
  513. return;
  514. }
  515. //if the cursor is in a the tactical map
  516. if( sGridNo != NOWHERE && sGridNo != 0 )
  517. {
  518. //if the gridno is different then the last one that was displayed
  519. if( sGridNo != gsLastVisibleToSoldierGridNo || MercPtrs[ gusSelectedSoldier ]->sGridNo != gsLastSoldierGridNo )
  520. {
  521. //if the cover is currently being displayed
  522. if( gsLastVisibleToSoldierGridNo != NOWHERE || gsLastSoldierGridNo != NOWHERE )
  523. {
  524. //remove the gridnos
  525. RemoveVisibleGridNoAtSelectedGridNo();
  526. }
  527. else
  528. {
  529. #ifdef JA2TESTVERSION
  530. {
  531. CHAR16 zString[512];
  532. swprintf( zString, L"%s, (%d)", zNewTacticalMessages[ TCTL_MSG__LOS ], gGameSettings.ubSizeOfLOS );
  533. ScreenMsg( FONT_MCOLOR_LTYELLOW, MSG_INTERFACE, zString );
  534. }
  535. #else
  536. //pop up a message to say we are in the display cover routine
  537. ScreenMsg( FONT_MCOLOR_LTYELLOW, MSG_INTERFACE, zNewTacticalMessages[ TCTL_MSG__LOS ] );
  538. #endif
  539. //increment the display LOS counter ( just seeing how many times people use it )
  540. //gJa25SaveStruct.uiDisplayLosCounter++;
  541. }
  542. gsLastVisibleToSoldierGridNo = sGridNo;
  543. gsLastSoldierGridNo = MercPtrs[ gusSelectedSoldier ]->sGridNo;
  544. //Fill the array of gridno and cover values
  545. CalculateVisibleToSoldierAroundGridno( sGridNo, gGameSettings.ubSizeOfLOS );
  546. //Add the graphics to each gridno
  547. AddVisibleToSoldierToEachGridNo();
  548. // Re-render the scene!
  549. SetRenderFlags( RENDER_FLAG_FULL );
  550. }
  551. }
  552. }
  553. void CalculateVisibleToSoldierAroundGridno( INT16 sTargetGridNo, INT8 bSearchRange )
  554. {
  555. INT16 sMaxLeft, sMaxRight, sMaxUp, sMaxDown, sXOffset, sYOffset;
  556. SOLDIERTYPE *pSoldier=NULL;
  557. INT16 sGridNo;
  558. INT16 sCounterX, sCounterY;
  559. BOOLEAN fRoof=FALSE;
  560. //clear out the struct
  561. memset( gVisibleToSoldierStruct, 0, sizeof( VISIBLE_TO_SOLDIER_STRUCT ) * DC__SOLDIER_VISIBLE_RANGE * DC__SOLDIER_VISIBLE_RANGE );
  562. if( bSearchRange > ( DC_MAX_COVER_RANGE / 2 ) )
  563. bSearchRange = ( DC_MAX_COVER_RANGE / 2 );
  564. // determine maximum horizontal limits
  565. sMaxLeft = min( bSearchRange,( sTargetGridNo % MAXCOL ));
  566. sMaxRight = min( bSearchRange,MAXCOL - (( sTargetGridNo % MAXCOL ) + 1));
  567. // determine maximum vertical limits
  568. sMaxUp = min( bSearchRange,( sTargetGridNo / MAXROW ));
  569. sMaxDown = min( bSearchRange,MAXROW - (( sTargetGridNo / MAXROW ) + 1));
  570. pSoldier = GetCurrentMercForDisplayCover();
  571. sCounterX=0;
  572. sCounterY=0;
  573. //loop through all the gridnos that we are interested in
  574. for (sYOffset = -sMaxUp; sYOffset <= sMaxDown; sYOffset++)
  575. {
  576. sCounterX = 0;
  577. for (sXOffset = -sMaxLeft; sXOffset <= sMaxRight; sXOffset++)
  578. {
  579. sGridNo = sTargetGridNo + sXOffset + (MAXCOL * sYOffset);
  580. fRoof = FALSE;
  581. //record the gridno
  582. gVisibleToSoldierStruct[ sCounterX ][ sCounterY ].sGridNo = sGridNo;
  583. //if the gridno is NOT on screen
  584. if( !GridNoOnScreen( sGridNo ) )
  585. {
  586. continue;
  587. }
  588. //is there a roof above this gridno
  589. if( FlatRoofAboveGridNo( sGridNo ) )
  590. {
  591. if( IsTheRoofVisible( sGridNo ) && gbWorldSectorZ == 0 )
  592. {
  593. fRoof = TRUE;
  594. }
  595. //if wer havent explored the area yet and we are underground, dont show cover
  596. else if( !( gpWorldLevelData[ sGridNo ].uiFlags & MAPELEMENT_REVEALED ) && gbWorldSectorZ != 0 )
  597. {
  598. continue;
  599. }
  600. }
  601. /*
  602. //if we are to display cover for the roofs, and there is a roof above us
  603. if( gsInterfaceLevel == I_ROOF_LEVEL && !FlatRoofAboveGridNo( sGridNo ) )
  604. {
  605. continue;
  606. }
  607. */
  608. /*
  609. // if someone (visible) is there, skip
  610. //Check both bottom level, and top level
  611. ubID = WhoIsThere2( sGridNo, 0 );
  612. if( ubID == NOBODY )
  613. {
  614. ubID = WhoIsThere2( sGridNo, 1 );
  615. }
  616. //if someone is here, and they are an enemy, skip over them
  617. if ( ubID != NOBODY && Menptr[ ubID ].bVisible == TRUE && Menptr[ ubID ].bTeam != pSoldier->bTeam )
  618. {
  619. continue;
  620. }
  621. //Calculate the cover for this gridno
  622. gCoverRadius[ sCounterX ][ sCounterY ].bCover = CalcCoverForGridNoBasedOnTeamKnownEnemies( pSoldier, sGridNo, bStance );
  623. */
  624. gVisibleToSoldierStruct[ sCounterX ][ sCounterY ].bVisibleToSoldier = CalcIfSoldierCanSeeGridNo( pSoldier, sGridNo, fRoof );
  625. gVisibleToSoldierStruct[ sCounterX ][ sCounterY ].fRoof = fRoof;
  626. sCounterX++;
  627. }
  628. sCounterY++;
  629. }
  630. }
  631. void AddVisibleToSoldierToEachGridNo()
  632. {
  633. UINT32 uiCntX, uiCntY;
  634. INT8 bVisibleToSoldier=0;
  635. BOOLEAN fRoof;
  636. INT16 sGridNo;
  637. //loop through all the gridnos
  638. for(uiCntY=0; uiCntY<DC_MAX_COVER_RANGE ;uiCntY++)
  639. {
  640. for(uiCntX=0; uiCntX<DC_MAX_COVER_RANGE ;uiCntX++)
  641. {
  642. bVisibleToSoldier = gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].bVisibleToSoldier;
  643. if( bVisibleToSoldier == -1 )
  644. {
  645. continue;
  646. }
  647. fRoof = gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].fRoof;
  648. sGridNo = gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].sGridNo;
  649. //if the soldier can easily see this gridno. Can see all 3 positions
  650. if( bVisibleToSoldier == DC__SEE_3_STANCE )
  651. {
  652. AddCoverObjectToWorld( sGridNo, SPECIALTILE_COVER_5, fRoof );
  653. }
  654. //cant see a thing
  655. else if( bVisibleToSoldier == DC__SEE_NO_STANCES )
  656. {
  657. AddCoverObjectToWorld( gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_1, fRoof );
  658. }
  659. //can only see prone
  660. else if( bVisibleToSoldier == DC__SEE_1_STANCE )
  661. {
  662. AddCoverObjectToWorld( gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_2, fRoof );
  663. }
  664. //can see crouch or prone
  665. else if( bVisibleToSoldier == DC__SEE_2_STANCE )
  666. {
  667. AddCoverObjectToWorld( gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_3, fRoof );
  668. }
  669. else
  670. {
  671. Assert( 0 );
  672. }
  673. }
  674. }
  675. }
  676. void RemoveVisibleGridNoAtSelectedGridNo()
  677. {
  678. UINT32 uiCntX, uiCntY;
  679. INT8 bVisibleToSoldier;
  680. INT16 sGridNo;
  681. BOOLEAN fRoof;
  682. //make sure to only remove it when its right
  683. if( gsLastVisibleToSoldierGridNo == NOWHERE )
  684. {
  685. return;
  686. }
  687. //loop through all the gridnos
  688. for(uiCntY=0; uiCntY<DC_MAX_COVER_RANGE ;uiCntY++)
  689. {
  690. for(uiCntX=0; uiCntX<DC_MAX_COVER_RANGE ;uiCntX++)
  691. {
  692. bVisibleToSoldier = gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].bVisibleToSoldier;
  693. fRoof = gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].fRoof;
  694. sGridNo = gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].sGridNo;
  695. //if there is a valid cover at this gridno
  696. if( bVisibleToSoldier == DC__SEE_3_STANCE )
  697. {
  698. RemoveCoverObjectFromWorld( gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_5, fRoof );
  699. }
  700. //cant see a thing
  701. else if( bVisibleToSoldier == DC__SEE_NO_STANCES )
  702. {
  703. RemoveCoverObjectFromWorld( gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_1, fRoof );
  704. }
  705. //can only see prone
  706. else if( bVisibleToSoldier == DC__SEE_1_STANCE )
  707. {
  708. RemoveCoverObjectFromWorld( gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_2, fRoof );
  709. }
  710. //can see crouch or prone
  711. else if( bVisibleToSoldier == DC__SEE_2_STANCE )
  712. {
  713. RemoveCoverObjectFromWorld( gVisibleToSoldierStruct[ uiCntX ][ uiCntY ].sGridNo, SPECIALTILE_COVER_3, fRoof );
  714. }
  715. else
  716. {
  717. Assert( 0 );
  718. }
  719. }
  720. }
  721. // Re-render the scene!
  722. SetRenderFlags( RENDER_FLAG_FULL );
  723. gsLastVisibleToSoldierGridNo = NOWHERE;
  724. gsLastSoldierGridNo = NOWHERE;
  725. }
  726. INT8 CalcIfSoldierCanSeeGridNo( SOLDIERTYPE *pSoldier, INT16 sTargetGridNo, BOOLEAN fRoof )
  727. {
  728. INT8 bRetVal=0;
  729. INT32 iLosForGridNo=0;
  730. UINT16 usSightLimit=0;
  731. INT8 *pPersOL,*pbPublOL;
  732. UINT8 ubID;
  733. BOOLEAN bAware=FALSE;
  734. if( fRoof )
  735. {
  736. ubID = WhoIsThere2( sTargetGridNo, 1 );
  737. }
  738. else
  739. {
  740. ubID = WhoIsThere2( sTargetGridNo, 0 );
  741. }
  742. if( ubID != NOBODY )
  743. {
  744. pPersOL = &(pSoldier->bOppList[ubID]);
  745. pbPublOL = &(gbPublicOpplist[pSoldier->bTeam][ubID]);
  746. // if soldier is known about (SEEN or HEARD within last few turns)
  747. if (*pPersOL || *pbPublOL)
  748. {
  749. bAware = TRUE;
  750. }
  751. }
  752. usSightLimit = DistanceVisible( pSoldier, DIRECTION_IRRELEVANT, DIRECTION_IRRELEVANT, sTargetGridNo, fRoof );
  753. //
  754. // Prone
  755. //
  756. iLosForGridNo = SoldierToVirtualSoldierLineOfSightTest( pSoldier, sTargetGridNo, fRoof, ANIM_PRONE, (UINT8)usSightLimit, bAware );
  757. if( iLosForGridNo != 0 )
  758. {
  759. bRetVal++;
  760. }
  761. //
  762. // Crouch
  763. //
  764. iLosForGridNo = SoldierToVirtualSoldierLineOfSightTest( pSoldier, sTargetGridNo, fRoof, ANIM_CROUCH, (UINT8)usSightLimit, bAware );
  765. if( iLosForGridNo != 0 )
  766. {
  767. bRetVal++;
  768. }
  769. //
  770. // Standing
  771. //
  772. iLosForGridNo = SoldierToVirtualSoldierLineOfSightTest( pSoldier, sTargetGridNo, fRoof, ANIM_STAND, (UINT8)usSightLimit, bAware );
  773. if( iLosForGridNo != 0 )
  774. {
  775. bRetVal++;
  776. }
  777. return( bRetVal );
  778. }
  779. BOOLEAN IsTheRoofVisible( INT16 sGridNo )
  780. {
  781. UINT8 ubRoom;
  782. if( InARoom( sGridNo, &ubRoom ) )
  783. {
  784. if( gpWorldLevelData[ sGridNo ].uiFlags & MAPELEMENT_REVEALED )
  785. {
  786. if( gTacticalStatus.uiFlags & SHOW_ALL_ROOFS )
  787. return( TRUE );
  788. else
  789. return( FALSE );
  790. }
  791. else
  792. {
  793. return( TRUE );
  794. }
  795. }
  796. return( FALSE );
  797. }
  798. #ifdef JA2TESTVERSION
  799. /*
  800. void DisplayLosAndDisplayCoverUsageScreenMsg()
  801. {
  802. CHAR16 zString[512];
  803. swprintf( zString, L"Display Cover: %d", gJa25SaveStruct.uiDisplayCoverCounter );
  804. ScreenMsg( FONT_MCOLOR_LTYELLOW, MSG_INTERFACE, zString );
  805. swprintf( zString, L"LOS: %d", gJa25SaveStruct.uiDisplayLosCounter );
  806. ScreenMsg( FONT_MCOLOR_LTYELLOW, MSG_INTERFACE, zString );
  807. swprintf( zString, L"Gun Range: %d", gJa25SaveStruct.uiDisplayGunRangeCounter );
  808. ScreenMsg( FONT_MCOLOR_LTYELLOW, MSG_INTERFACE, zString );
  809. }
  810. */
  811. #endif
  812. void ChangeSizeOfDisplayCover( INT32 iNewSize )
  813. {
  814. //if the new size is smaller or greater, scale it
  815. if( iNewSize < DC__MIN_SIZE )
  816. {
  817. iNewSize = DC__MIN_SIZE;
  818. }
  819. else if( iNewSize > DC__MAX_SIZE )
  820. {
  821. iNewSize = DC__MAX_SIZE;
  822. }
  823. //Set new size
  824. gGameSettings.ubSizeOfDisplayCover = (UINT8)iNewSize;
  825. //redisplay the cover
  826. RemoveCoverOfSelectedGridNo();
  827. DisplayCoverOfSelectedGridNo( );
  828. }
  829. void ChangeSizeOfLOS( INT32 iNewSize )
  830. {
  831. //if the new size is smaller or greater, scale it
  832. if( iNewSize < DC__MIN_SIZE )
  833. {
  834. iNewSize = DC__MIN_SIZE;
  835. }
  836. else if( iNewSize > DC__MAX_SIZE )
  837. {
  838. iNewSize = DC__MAX_SIZE;
  839. }
  840. //Set new size
  841. gGameSettings.ubSizeOfLOS = (UINT8)iNewSize;
  842. //ReDisplay the los
  843. RemoveVisibleGridNoAtSelectedGridNo();
  844. DisplayGridNoVisibleToSoldierGrid( );
  845. }