MAP.CPP 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /CounterStrike/MAP.CPP 3 3/14/97 5:15p Joe_b $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : MAP.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : September 10, 1993 *
  30. * *
  31. * Last Update : October 5, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * MapClass::Base_Region -- Finds the owner and base zone for specified cell. *
  36. * MapClass::Cell_Region -- Determines the region from a specified cell number. *
  37. * MapClass::Cell_Threat -- Gets a houses threat value for a cell *
  38. * MapClass::Close_Object -- Finds a clickable close object to the specified coordinate. *
  39. * MapClass::Destroy_Bridge_At -- Destroyes the bridge at location specified. *
  40. * MapClass::Detach -- Remove specified object from map references. *
  41. * MapClass::In_Radar -- Is specified cell in the radar map? *
  42. * MapClass::Init -- clears all cells *
  43. * MapClass::Intact_Bridge_Count -- Determine the number of intact bridges. *
  44. * MapClass::Logic -- Handles map related logic functions. *
  45. * MapClass::Nearby_Location -- Finds a generally clear location near a specified cell. *
  46. * MapClass::One_Time -- Performs special one time initializations for the map. *
  47. * MapClass::Overlap_Down -- computes & marks object's overlap cells *
  48. * MapClass::Overlap_Up -- Computes & clears object's overlap cells *
  49. * MapClass::Overpass -- Performs any final cleanup to a freshly constructed map. *
  50. * MapClass::Pick_Up -- Removes specified object from the map. *
  51. * MapClass::Place_Down -- Places the specified object onto the map. *
  52. * MapClass::Place_Random_Crate -- Places a crate at random location on map. *
  53. * MapClass::Read_Binary -- Reads the binary data from the straw specified. *
  54. * MapClass::Remove_Crate -- Remove a crate from the specified cell. *
  55. * MapClass::Set_Map_Dimensions -- Initialize the map. *
  56. * MapClass::Sight_From -- Mark as visible the cells within a specified radius. *
  57. * MapClass::Validate -- validates every cell on the map *
  58. * MapClass::Write_Binary -- Pipes the map template data to the destination specified. *
  59. * MapClass::Zone_Reset -- Resets all zone numbers to match the map. *
  60. * MapClass::Zone_Span -- Flood fills the specified zone from the cell origin. *
  61. * MapClass::Pick_Random_Location -- Picks a random location on the map. *
  62. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  63. #include "function.h"
  64. #define MCW MAP_CELL_W
  65. int const MapClass::RadiusOffset[] = {
  66. /* 0 */ 0,
  67. /* 1 */ (-MCW*1)-1,(-MCW*1)+0,(-MCW*1)+1,-1,1,(MCW*1)-1,(MCW*1)+0,(MCW*1)+1,
  68. /* 2 */ (-MCW*2)-1,(-MCW*2)+0,(-MCW*2)+1,(-MCW*1)-2,(-MCW*1)+2,-2,2,(MCW*1)-2,(MCW*1)+2,(MCW*2)-1,(MCW*2)+0,(MCW*2)+1,
  69. /* 3 */ (-MCW*3)-1,(-MCW*3)+0,(-MCW*3)+1,(-MCW*2)-2,(-MCW*2)+2,(-MCW*1)-3,(-MCW*1)+3,-3,3,(MCW*1)-3,(MCW*1)+3,(MCW*2)-2,(MCW*2)+2,(MCW*3)-1,(MCW*3)+0,(MCW*3)+1,
  70. /* 4 */ (-MCW*4)-1,(-MCW*4)+0,(-MCW*4)+1,(-MCW*3)-3,(-MCW*3)-2,(-MCW*3)+2,(-MCW*3)+3,(-MCW*2)-3,(-MCW*2)+3,(-MCW*1)-4,(-MCW*1)+4,-4,4,(MCW*1)-4,(MCW*1)+4,(MCW*2)-3,(MCW*2)+3,(MCW*3)-3,(MCW*3)-2,(MCW*3)+2,(MCW*3)+3,(MCW*4)-1,(MCW*4)+0,(MCW*4)+1,
  71. /* 5 */ (-MCW*5)-1,(-MCW*5)+0,(-MCW*5)+1,(-MCW*4)-3,(-MCW*4)-2,(-MCW*4)+2,(-MCW*4)+3,(-MCW*3)-4,(-MCW*3)+4,(-MCW*2)-4,(-MCW*2)+4,(-MCW*1)-5,(-MCW*1)+5,-5,5,(MCW*1)-5,(MCW*1)+5,(MCW*2)-4,(MCW*2)+4,(MCW*3)-4,(MCW*3)+4,(MCW*4)-3,(MCW*4)-2,(MCW*4)+2,(MCW*4)+3,(MCW*5)-1,(MCW*5)+0,(MCW*5)+1,
  72. /* 6 */ (-MCW*6)-1,(-MCW*6)+0,(-MCW*6)+1,(-MCW*5)-3,(-MCW*5)-2,(-MCW*5)+2,(-MCW*5)+3,(-MCW*4)-4,(-MCW*4)+4,(-MCW*3)-5,(-MCW*3)+5,(-MCW*2)-5,(-MCW*2)+5,(-MCW*1)-6,(-MCW*1)+6,-6,6,(MCW*1)-6,(MCW*1)+6,(MCW*2)-5,(MCW*2)+5,(MCW*3)-5,(MCW*3)+5,(MCW*4)-4,(MCW*4)+4,(MCW*5)-3,(MCW*5)-2,(MCW*5)+2,(MCW*5)+3,(MCW*6)-1,(MCW*6)+0,(MCW*6)+1,
  73. /* 7 */ (-MCW*7)-1,(-MCW*7)+0,(-MCW*7)+1,(-MCW*6)-3,(-MCW*6)-2,(-MCW*6)+2,(-MCW*6)+3,(-MCW*5)-5,(-MCW*5)-4,(-MCW*5)+4,(-MCW*5)+5,(-MCW*4)-5,(-MCW*4)+5,(-MCW*3)-6,(-MCW*3)+6,(-MCW*2)-6,(-MCW*2)+6,(-MCW*1)-7,(-MCW*1)+7,-7,7,(MCW*1)-7,(MCW*1)+7,(MCW*2)-6,(MCW*2)+6,(MCW*3)-6,(MCW*3)+6,(MCW*4)-5,(MCW*4)+5,(MCW*5)-5,(MCW*5)-4,(MCW*5)+4,(MCW*5)+5,(MCW*6)-3,(MCW*6)-2,(MCW*6)+2,(MCW*6)+3,(MCW*7)-1,(MCW*7)+0,(MCW*7)+1,
  74. /* 8 */ (-MCW*8)-1,(-MCW*8)+0,(-MCW*8)+1,(-MCW*7)-3,(-MCW*7)-2,(-MCW*7)+2,(-MCW*7)+3,(-MCW*6)-5,(-MCW*6)-4,(-MCW*6)+4,(-MCW*6)+5,(-MCW*5)-6,(-MCW*5)+6,(-MCW*4)-6,(-MCW*4)+6,(-MCW*3)-7,(-MCW*3)+7,(-MCW*2)-7,(-MCW*2)+7,(-MCW*1)-8,(-MCW*1)+8,-8,8,(MCW*1)-8,(MCW*1)+8,(MCW*2)-7,(MCW*2)+7,(MCW*3)-7,(MCW*3)+7,(MCW*4)-6,(MCW*4)+6,(MCW*5)-6,(MCW*5)+6,(MCW*6)-5,(MCW*6)-4,(MCW*6)+4,(MCW*6)+5,(MCW*7)-3,(MCW*7)-2,(MCW*7)+2,(MCW*7)+3,(MCW*8)-1,(MCW*8)+0,(MCW*8)+1,
  75. /* 9 */ (-MCW*9)-1,(-MCW*9)+0,(-MCW*9)+1,(-MCW*8)-3,(-MCW*8)-2,(-MCW*8)+2,(-MCW*8)+3,(-MCW*7)-5,(-MCW*7)-4,(-MCW*7)+4,(-MCW*7)+5,(-MCW*6)-6,(-MCW*6)+6,(-MCW*5)-7,(-MCW*5)+7,(-MCW*4)-7,(-MCW*4)+7,(-MCW*3)-8,(-MCW*3)+8,(-MCW*2)-8,(-MCW*2)+8,(-MCW*1)-9,(-MCW*1)+9,-9,9,(MCW*1)-9,(MCW*1)+9,(MCW*2)-8,(MCW*2)+8,(MCW*3)-8,(MCW*3)+8,(MCW*4)-7,(MCW*4)+7,(MCW*5)-7,(MCW*5)+7,(MCW*6)-6,(MCW*6)+6,(MCW*7)-5,(MCW*7)-4,(MCW*7)+4,(MCW*7)+5,(MCW*8)-3,(MCW*8)-2,(MCW*8)+2,(MCW*8)+3,(MCW*9)-1,(MCW*9)+0,(MCW*9)+1,
  76. /* 10 */ (-MCW*10)-1,(-MCW*10)+0,(-MCW*10)+1,(-MCW*9)-3,(-MCW*9)-2,(-MCW*9)+2,(-MCW*9)+3,(-MCW*8)-5,(-MCW*8)-4,(-MCW*8)+4,(-MCW*8)+5,(-MCW*7)-7,(-MCW*7)-6,(-MCW*7)+6,(-MCW*7)+7,(-MCW*6)-7,(-MCW*6)+7,(-MCW*5)-8,(-MCW*5)+8,(-MCW*4)-8,(-MCW*4)+8,(-MCW*3)-9,(-MCW*3)+9,(-MCW*2)-9,(-MCW*2)+9,(-MCW*1)-10,(-MCW*1)+10,-10,10,(MCW*1)-10,(MCW*1)+10,(MCW*2)-9,(MCW*2)+9,(MCW*3)-9,(MCW*3)+9,(MCW*4)-8,(MCW*4)+8,(MCW*5)-8,(MCW*5)+8,(MCW*6)-7,(MCW*6)+7,(MCW*7)-7,(MCW*7)-6,(MCW*7)+6,(MCW*7)+7,(MCW*8)-5,(MCW*8)-4,
  77. (MCW*8)+4,(MCW*8)+5,(MCW*9)-3,(MCW*9)-2,(MCW*9)+2,(MCW*9)+3,(MCW*10)-1,(MCW*10)+0,(MCW*10)+1,
  78. };
  79. int const MapClass::RadiusCount[11] = {1,9,21,37,61,89,121,161,205,253,309};
  80. CellClass * BlubCell;
  81. /***********************************************************************************************
  82. * MapClass::One_Time -- Performs special one time initializations for the map. *
  83. * *
  84. * This routine is used by the game initialization function in order to perform any one *
  85. * time initializations required for the map. This includes allocation of the map and *
  86. * setting up its default dimensions. *
  87. * *
  88. * INPUT: none *
  89. * *
  90. * OUTPUT: none *
  91. * *
  92. * WARNINGS: This routine MUST be called once and only once. *
  93. * *
  94. * HISTORY: *
  95. * 05/31/1994 JLB : Created. *
  96. * 12/01/1994 BR : Added CellTriggers initialization *
  97. *=============================================================================================*/
  98. void MapClass::One_Time(void)
  99. {
  100. GScreenClass::One_Time();
  101. XSize = MAP_CELL_W;
  102. YSize = MAP_CELL_H;
  103. Size = XSize * YSize;
  104. /*
  105. ** Allocate the cell array.
  106. */
  107. Alloc_Cells();
  108. }
  109. /***********************************************************************************************
  110. * MapClass::Init_Clear -- clears the map & buffers to a known state *
  111. * *
  112. * INPUT: *
  113. * none. *
  114. * *
  115. * OUTPUT: *
  116. * none. *
  117. * *
  118. * WARNINGS: *
  119. * none. *
  120. * *
  121. * HISTORY: *
  122. * 03/17/1995 BRR : Created. *
  123. *=============================================================================================*/
  124. void MapClass::Init_Clear(void)
  125. {
  126. GScreenClass::Init_Clear();
  127. Init_Cells();
  128. TiberiumScan = 0;
  129. TiberiumGrowthCount = 0;
  130. TiberiumGrowthExcess = 0;
  131. TiberiumSpreadCount = 0;
  132. TiberiumSpreadExcess = 0;
  133. for (int index = 0; index < ARRAY_SIZE(Crates); index++) {
  134. Crates[index].Init();
  135. }
  136. }
  137. /***********************************************************************************************
  138. * MapClass::Alloc_Cells -- allocates the cell array *
  139. * *
  140. * This routine should be called at One_Time, and after loading the Map object from a save *
  141. * game, but prior to loading the cell objects. *
  142. * *
  143. * INPUT: *
  144. * none. *
  145. * *
  146. * OUTPUT: *
  147. * none. *
  148. * *
  149. * WARNINGS: *
  150. * none. *
  151. * *
  152. * HISTORY: *
  153. * 03/17/1995 BRR : Created. *
  154. *=============================================================================================*/
  155. void MapClass::Alloc_Cells(void)
  156. {
  157. /*
  158. ** Assume that whatever the contents of the VectorClass are is garbage
  159. ** (it may have been loaded from a save-game file), so zero it out first.
  160. */
  161. new (&Array) VectorClass<CellClass>;
  162. Array.Resize(Size);
  163. }
  164. /***********************************************************************************************
  165. * MapClass::Free_Cells -- frees the cell array *
  166. * *
  167. * This routine is used by the Load_Game routine to free the map's cell array before loading *
  168. * the map object from disk; the array is then re-allocated & cleared before the cell objects *
  169. * are loaded. *
  170. * *
  171. * INPUT: *
  172. * none. *
  173. * *
  174. * OUTPUT: *
  175. * none. *
  176. * *
  177. * WARNINGS: *
  178. * none. *
  179. * *
  180. * HISTORY: *
  181. * 03/17/1995 BRR : Created. *
  182. *=============================================================================================*/
  183. void MapClass::Free_Cells(void)
  184. {
  185. Array.Clear();
  186. }
  187. /***********************************************************************************************
  188. * MapClass::Init_Cells -- Initializes the cell array to a fresh state. *
  189. * *
  190. * This routine is used by Init_Clear to set the cells to a known state; it's also used by *
  191. * the Load_Game routine to init all cells before loading a set of cells from disk, so it *
  192. * needs to be called separately from the other Init_xxx() routines. *
  193. * *
  194. * INPUT: *
  195. * none. *
  196. * *
  197. * OUTPUT: *
  198. * none. *
  199. * *
  200. * WARNINGS: *
  201. * none. *
  202. * *
  203. * HISTORY: *
  204. * 03/17/1995 BRR : Created. *
  205. *=============================================================================================*/
  206. void MapClass::Init_Cells(void)
  207. {
  208. TotalValue = 0;
  209. for (int index = 0; index < MAP_CELL_TOTAL; index++) {
  210. new (&Array[index]) CellClass;
  211. }
  212. }
  213. /***********************************************************************************************
  214. * MapClass::Set_Map_Dimensions -- Set map dimensions. *
  215. * *
  216. * This routine is used to set the legal limits and position of the *
  217. * map as it relates to the overall map array. Typically, this is *
  218. * called by the scenario loading code. *
  219. * *
  220. * INPUT: x,y -- The X and Y coordinate of the "upper left" corner *
  221. * of the map. *
  222. * *
  223. * w,h -- The width and height of the legal map. *
  224. * *
  225. * OUTPUT: none *
  226. * *
  227. * WARNINGS: none *
  228. * *
  229. * HISTORY: *
  230. * 05/14/1994 JLB : Created. *
  231. *=============================================================================================*/
  232. void MapClass::Set_Map_Dimensions(int x, int y, int w, int h)
  233. {
  234. MapCellX = x;
  235. MapCellY = y;
  236. MapCellWidth = w;
  237. MapCellHeight = h;
  238. }
  239. /***********************************************************************************************
  240. * MapClass::Sight_From -- Mark as visible the cells within a specified radius. *
  241. * *
  242. * This routine is used to reveal the cells around a specific location. *
  243. * Typically, as a unit moves or is deployed, this routine will be *
  244. * called. Since it deals with MANY cells, it needs to be extremely *
  245. * fast. *
  246. * *
  247. * INPUT: cell -- The coordinate that the sighting originates from. *
  248. * *
  249. * sightrange-- The distance in cells that sighting extends. *
  250. * *
  251. * incremental-- Is this an incremental sighting. In other *
  252. * words, has this function been called before where *
  253. * the center coordinate is no more than one cell *
  254. * distant from the last time? *
  255. * *
  256. * OUTPUT: none *
  257. * *
  258. * WARNINGS: none *
  259. * *
  260. * HISTORY: *
  261. * 05/19/1992 JLB : Created. *
  262. * 03/08/1994 JLB : Updated to use sight table and incremental flag. *
  263. * 05/18/1994 JLB : Converted to member function. *
  264. *=============================================================================================*/
  265. void MapClass::Sight_From(CELL cell, int sightrange, HouseClass * house, bool incremental)
  266. {
  267. int xx; // Center cell X coordinate (bounds checking).
  268. int const * ptr; // Offset pointer.
  269. int count; // Counter for number of offsets to process.
  270. /*
  271. ** Units that are off-map cannot sight.
  272. */
  273. if (!In_Radar(cell)) return;
  274. if (!sightrange || sightrange > 10) return;
  275. /*
  276. ** Determine logical cell coordinate for center scan point.
  277. */
  278. xx = Cell_X(cell);
  279. /*
  280. ** Incremental scans only scan the outer rings. Full scans
  281. ** scan all internal cells as well.
  282. */
  283. count = RadiusCount[sightrange];
  284. ptr = &RadiusOffset[0];
  285. if (incremental) {
  286. if (sightrange > 2) {
  287. ptr += RadiusCount[sightrange-3];
  288. count -= RadiusCount[sightrange-3];
  289. }
  290. }
  291. /*
  292. ** Process all offsets required for the desired scan.
  293. */
  294. while (count--) {
  295. CELL newcell; // New cell with offset.
  296. int xdiff; // New cell's X coordinate distance from center.
  297. newcell = cell + *ptr++;
  298. /*
  299. ** Determine if the map edge has been wrapped. If so,
  300. ** then don't process the cell.
  301. */
  302. if ((unsigned)newcell >= MAP_CELL_TOTAL) continue;
  303. xdiff = Cell_X(newcell) - xx;
  304. xdiff = ABS(xdiff);
  305. if (xdiff > sightrange) continue;
  306. if (Distance(Cell_Coord(newcell), Cell_Coord(cell)) > (sightrange * CELL_LEPTON_W)) continue;
  307. /*
  308. ** Map the cell. For incremental scans, then update
  309. ** adjacent cells as well. For full scans, just update
  310. ** the cell itself.
  311. */
  312. if (!(*this)[newcell].IsMapped) {
  313. Map.Map_Cell(newcell, house);
  314. }
  315. }
  316. }
  317. /***********************************************************************************************
  318. * MapClass::Shroud_From -- cloak a radius of cells *
  319. * *
  320. * This routine is used to shroud the cells around a specific location. *
  321. * Typically, as a gap generator refreshes (when Encroach_Shadow() is called) this routine's*
  322. * called. Since it deals with MANY cells, it needs to be extremely *
  323. * fast. *
  324. * *
  325. * INPUT: cell -- The coordinate that the shrouding originates from. *
  326. * *
  327. * sightrange-- The distance in cells that sighting extends. *
  328. * *
  329. * OUTPUT: none *
  330. * *
  331. * WARNINGS: none *
  332. * *
  333. * HISTORY: *
  334. * 11/10/1995 BWG : Created. *
  335. *=============================================================================================*/
  336. void MapClass::Shroud_From(CELL cell, int sightrange)
  337. {
  338. int xx; // Center cell X coordinate (bounds checking).
  339. int const * ptr; // Offset pointer.
  340. int count; // Counter for number of offsets to process.
  341. /*
  342. ** Units that are off-map cannot sight.
  343. */
  344. if (!In_Radar(cell)) return;
  345. if (!sightrange || sightrange > Rule.GapShroudRadius) return;
  346. /*
  347. ** Determine logical cell coordinate for center scan point.
  348. */
  349. xx = Cell_X(cell);
  350. /*
  351. ** Incremental scans only scan the outer rings. Full scans
  352. ** scan all internal cells as well.
  353. */
  354. count = RadiusCount[sightrange];
  355. ptr = &RadiusOffset[0];
  356. /*
  357. ** Process all offsets required for the desired scan.
  358. */
  359. while (count--) {
  360. CELL newcell; // New cell with offset.
  361. int xdiff; // New cell's X coordinate distance from center.
  362. newcell = cell + *ptr++;
  363. /*
  364. ** Determine if the map edge has been wrapped. If so,
  365. ** then don't process the cell.
  366. */
  367. if ((unsigned)newcell >= MAP_CELL_TOTAL) continue;
  368. xdiff = Cell_X(newcell) - xx;
  369. xdiff = ABS(xdiff);
  370. if (xdiff > sightrange) continue;
  371. if (Distance(Cell_Coord(newcell), Cell_Coord(cell)) > (sightrange * CELL_LEPTON_W)) continue;
  372. /*
  373. ** Shroud the cell.
  374. */
  375. Map.Shroud_Cell(newcell);
  376. }
  377. }
  378. /***********************************************************************************************
  379. * MapClass::Jam_From -- Mark as jammed the cells within a specified radius. *
  380. * *
  381. * This routine is used to jam the cells around a specific location. *
  382. * Typically, as a gap generator structure is created, this routine will be *
  383. * called. Since it deals with MANY cells, it needs to be extremely *
  384. * fast. *
  385. * *
  386. * INPUT: cell -- The coordinate that the jamming originates from. *
  387. * *
  388. * jamrange -- The distance in cells that jamming extends. *
  389. * *
  390. * *
  391. * OUTPUT: none *
  392. * *
  393. * WARNINGS: none *
  394. * *
  395. * HISTORY: *
  396. * 11/09/1995 BWG : Created. *
  397. *=============================================================================================*/
  398. void MapClass::Jam_From(CELL cell, int jamrange, HouseClass * house)
  399. {
  400. int xx; // Center cell X coordinate (bounds checking).
  401. int const * ptr; // Offset pointer.
  402. int count; // Counter for number of offsets to process.
  403. /*
  404. ** Units that are off-map cannot jam.
  405. */
  406. if (!jamrange || jamrange > Rule.GapShroudRadius) return;
  407. /*
  408. ** Determine logical cell coordinate for center scan point.
  409. */
  410. xx = Cell_X(cell);
  411. /*
  412. ** Incremental scans only scan the outer rings. Full scans
  413. ** scan all internal cells as well.
  414. */
  415. count = RadiusCount[jamrange];
  416. ptr = &RadiusOffset[0];
  417. /*
  418. ** Process all offsets required for the desired scan.
  419. */
  420. while (count--) {
  421. CELL newcell; // New cell with offset.
  422. int xdiff; // New cell's X coordinate distance from center.
  423. newcell = cell + *ptr++;
  424. /*
  425. ** Determine if the map edge has been wrapped. If so,
  426. ** then don't process the cell.
  427. */
  428. if ((unsigned)newcell >= MAP_CELL_TOTAL) continue;
  429. xdiff = Cell_X(newcell) - xx;
  430. xdiff = ABS(xdiff);
  431. if (xdiff > jamrange) continue;
  432. if (Distance(Cell_Coord(newcell), Cell_Coord(cell)) > (jamrange * CELL_LEPTON_W)) continue;
  433. /*
  434. ** Jam the cell. For incremental scans, then update
  435. ** adjacent cells as well. For full scans, just update
  436. ** the cell itself.
  437. */
  438. Map.Jam_Cell(newcell, house/*KO, false*/);
  439. }
  440. // PlayerPtr->IsToLook = true;
  441. if (!house->IsPlayerControl) {
  442. Map.Constrained_Look(Cell_Coord(cell), Rule.GapShroudRadius * CELL_LEPTON_W);
  443. }
  444. #ifdef OBSOLETE
  445. /*
  446. ** The objects on the map need to perform a manual look operation if they happen
  447. ** to have their sight range overlap the gap radius.
  448. */
  449. if (!house->IsPlayerControl) {
  450. // if (house != PlayerPtr) {
  451. for (int index = 0; index < Map.Layer[LAYER_GROUND].Count(); index++) {
  452. ObjectClass * object = Map.Layer[LAYER_GROUND][index];
  453. if (object && object->Is_Techno()) {
  454. TechnoClass * tech = ((TechnoClass *)object);
  455. if (tech->IsDiscoveredByPlayer &&
  456. (tech->Distance(As_Target(cell)) / CELL_LEPTON_W) <= tech->Techno_Type_Class()->SightRange + Rule.GapShroudRadius &&
  457. (tech->House->IsPlayerControl ||
  458. (tech->What_Am_I() == RTTI_BUILDING && Rule.IsAllyReveal && tech->House->Is_Ally(PlayerPtr)))) {
  459. object->Look();
  460. }
  461. }
  462. }
  463. }
  464. #endif
  465. #ifdef OBSOLETE
  466. /*
  467. ** Here all the player's vehicles will perform a look if they're within
  468. ** the shadow.
  469. */
  470. for (int index = 0; index < Units.Count(); index++) {
  471. UnitClass * unit = Units.Ptr(index);
  472. if (unit && !unit->IsInLimbo && unit->House == PlayerPtr) {
  473. int dist = (unit->Distance(As_Target(cell))) / CELL_LEPTON_W;
  474. if (dist <= unit->Class->SightRange + Rule.GapShroudRadius /*gap generator sightrange*/) {
  475. unit->Look();
  476. }
  477. }
  478. }
  479. for (index = 0; index < Infantry.Count(); index++) {
  480. InfantryClass * unit = Infantry.Ptr(index);
  481. if (unit && !unit->IsInLimbo && unit->House == PlayerPtr) {
  482. int dist = (unit->Distance(As_Target(cell))) / CELL_LEPTON_W;
  483. if (dist <= unit->Class->SightRange + Rule.GapShroudRadius /*gap generator sightrange*/) {
  484. unit->Look();
  485. }
  486. }
  487. }
  488. for (index = 0; index < Vessels.Count(); index++) {
  489. VesselClass * unit = Vessels.Ptr(index);
  490. if (unit && !unit->IsInLimbo && unit->House == PlayerPtr) {
  491. int dist = (unit->Distance(As_Target(cell))) / CELL_LEPTON_W;
  492. if (dist <= unit->Class->SightRange + Rule.GapShroudRadius /*gap generator sightrange*/) {
  493. unit->Look();
  494. }
  495. }
  496. }
  497. }
  498. #endif
  499. }
  500. /***********************************************************************************************
  501. * MapClass::UnJam_From -- Remove jamming on the cells within a specified radius. *
  502. * *
  503. * This routine is used to jam the cells around a specific location. *
  504. * Typically, as a gap generator structure is created, this routine will be *
  505. * called. Since it deals with MANY cells, it needs to be extremely *
  506. * fast. *
  507. * *
  508. * INPUT: cell -- The coordinate that the jamming originates from. *
  509. * *
  510. * jamrange -- The distance in cells that jamming extends. *
  511. * *
  512. * OUTPUT: none *
  513. * *
  514. * WARNINGS: none *
  515. * *
  516. * HISTORY: *
  517. * 11/09/1995 BWG : Created. *
  518. *=============================================================================================*/
  519. void MapClass::UnJam_From(CELL cell, int jamrange, HouseClass * house)
  520. {
  521. int xx; // Center cell X coordinate (bounds checking).
  522. int const * ptr; // Offset pointer.
  523. int count; // Counter for number of offsets to process.
  524. /*
  525. ** Units that are off-map cannot jam.
  526. */
  527. if (!jamrange || jamrange > Rule.GapShroudRadius) return;
  528. /*
  529. ** Determine logical cell coordinate for center scan point.
  530. */
  531. xx = Cell_X(cell);
  532. /*
  533. ** Incremental scans only scan the outer rings. Full scans
  534. ** scan all internal cells as well.
  535. */
  536. count = RadiusCount[jamrange];
  537. ptr = &RadiusOffset[0];
  538. /*
  539. ** Process all offsets required for the desired scan.
  540. */
  541. while (count--) {
  542. CELL newcell; // New cell with offset.
  543. int xdiff; // New cell's X coordinate distance from center.
  544. newcell = cell + *ptr++;
  545. /*
  546. ** Determine if the map edge has been wrapped. If so,
  547. ** then don't process the cell.
  548. */
  549. if ((unsigned)newcell >= MAP_CELL_TOTAL) continue;
  550. xdiff = Cell_X(newcell) - xx;
  551. xdiff = ABS(xdiff);
  552. if (xdiff > jamrange) continue;
  553. if (Distance(Cell_Coord(newcell), Cell_Coord(cell)) > (jamrange * CELL_LEPTON_W)) continue;
  554. /*
  555. ** Jam the cell. For incremental scans, then update
  556. ** adjacent cells as well. For full scans, just update
  557. ** the cell itself.
  558. */
  559. Map.UnJam_Cell(newcell, house);
  560. }
  561. }
  562. /***********************************************************************************************
  563. * MapClass::In_Radar -- Is specified cell in the radar map? *
  564. * *
  565. * This determines if the specified cell can be within the navigable *
  566. * bounds of the map. Technically, this means, any cell that can be *
  567. * scanned by radar. If a cell returns false from this function, then *
  568. * the player could never move to or pass over this cell. *
  569. * *
  570. * INPUT: cell -- The cell to examine. *
  571. * *
  572. * OUTPUT: bool; Is this cell possible to be displayed on radar? *
  573. * *
  574. * WARNINGS: none *
  575. * *
  576. * HISTORY: *
  577. * 10/07/1992 JLB : Created. *
  578. * 04/30/1994 JLB : Converted to member function. *
  579. * 05/01/1994 JLB : Speeded up. *
  580. *=============================================================================================*/
  581. bool MapClass::In_Radar(CELL cell) const
  582. {
  583. /*
  584. ** If the cell value is WAY out of range, then it obviously can't be part of the game
  585. ** playfield.
  586. */
  587. if ((unsigned)cell > MAP_CELL_TOTAL) return(false);
  588. /*
  589. ** If the cell is off the left or right edge of the playfield, then return the "not in
  590. ** radar" flag.
  591. */
  592. if ((unsigned)(Cell_X(cell) - MapCellX) >= (unsigned)MapCellWidth) return(false);
  593. /*
  594. ** If the cell is off the top or bottom edge of the playfield, then return the "not in
  595. ** radar" flag.
  596. */
  597. if ((unsigned)(Cell_Y(cell) - MapCellY) >= (unsigned)MapCellHeight) return(false);
  598. return(true);
  599. }
  600. /***********************************************************************************************
  601. * MapClass::Place_Down -- Places the specified object onto the map. *
  602. * *
  603. * This routine is used to place an object onto the map. It updates the "occupier" of the *
  604. * cells that this object covers. The cells are determined from the Occupy_List function *
  605. * provided by the object. Only one cell can have an occupier and this routine is the only *
  606. * place that sets this condition. *
  607. * *
  608. * INPUT: cell -- The cell to base object occupation around. *
  609. * *
  610. * object -- The object to place onto the map. *
  611. * *
  612. * OUTPUT: none *
  613. * *
  614. * WARNINGS: none *
  615. * *
  616. * HISTORY: *
  617. * 07/31/1994 JLB : Created. *
  618. *=============================================================================================*/
  619. void MapClass::Place_Down(CELL cell, ObjectClass * object)
  620. {
  621. if (!object) return;
  622. if (object->Class_Of().IsFootprint && object->In_Which_Layer() == LAYER_GROUND) {
  623. short xlist[32];
  624. List_Copy(object->Occupy_List(), ARRAY_SIZE(xlist), xlist);
  625. short const * list = xlist;
  626. while (*list != REFRESH_EOL) {
  627. CELL newcell = cell + *list++;
  628. if ((unsigned)newcell < MAP_CELL_TOTAL) {
  629. (*this)[newcell].Occupy_Down(object);
  630. (*this)[newcell].Recalc_Attributes();
  631. (*this)[newcell].Redraw_Objects();
  632. }
  633. }
  634. List_Copy(object->Overlap_List(), ARRAY_SIZE(xlist), xlist);
  635. list = xlist;
  636. while (*list != REFRESH_EOL) {
  637. CELL newcell = cell + *list++;
  638. if ((unsigned)newcell < MAP_CELL_TOTAL) {
  639. (*this)[newcell].Overlap_Down(object);
  640. (*this)[newcell].Redraw_Objects();
  641. }
  642. }
  643. }
  644. }
  645. /***********************************************************************************************
  646. * MapClass::Pick_Up -- Removes specified object from the map. *
  647. * *
  648. * The object specified is removed from the map by this routine. This will remove the *
  649. * occupation flag for all the cells that the object covers. The cells that are covered *
  650. * are determined from the Occupy_List function. *
  651. * *
  652. * INPUT: cell -- The cell that the object is centered about. *
  653. * *
  654. * object -- Pointer to the object that will be removed. *
  655. * *
  656. * OUTPUT: none *
  657. * *
  658. * WARNINGS: none *
  659. * *
  660. * HISTORY: *
  661. * 07/31/1994 JLB : Created. *
  662. *=============================================================================================*/
  663. void MapClass::Pick_Up(CELL cell, ObjectClass * object)
  664. {
  665. if (!object) return;
  666. if (object->Class_Of().IsFootprint && object->In_Which_Layer() == LAYER_GROUND) {
  667. short xlist[32];
  668. List_Copy(object->Occupy_List(), ARRAY_SIZE(xlist), xlist);
  669. short const * list = xlist;
  670. while (*list != REFRESH_EOL) {
  671. CELL newcell = cell + *list++;
  672. if ((unsigned)newcell < MAP_CELL_TOTAL) {
  673. (*this)[newcell].Occupy_Up(object);
  674. (*this)[newcell].Recalc_Attributes();
  675. (*this)[newcell].Redraw_Objects();
  676. }
  677. }
  678. List_Copy(object->Overlap_List(), ARRAY_SIZE(xlist), xlist);
  679. list = xlist;
  680. while (*list != REFRESH_EOL) {
  681. CELL newcell = cell + *list++;
  682. if ((unsigned)newcell < MAP_CELL_TOTAL) {
  683. (*this)[newcell].Overlap_Up(object);
  684. (*this)[newcell].Redraw_Objects();
  685. }
  686. }
  687. }
  688. }
  689. /***********************************************************************************************
  690. * MapClass::Overlap_Down -- computes & marks object's overlap cells *
  691. * *
  692. * This routine is just like Place_Down, but it doesn't mark the cell's Occupier. *
  693. * This routine is used to implement MARK_OVERLAP_DOWN, which is useful for changing *
  694. * an object's render size, but not its logical size (ie when it's selected or an *
  695. * animation is attached to it). *
  696. * *
  697. * INPUT: *
  698. * cell -- The cell to base object overlap around. *
  699. * object -- The object to place onto the map. *
  700. * *
  701. * OUTPUT: *
  702. * none. *
  703. * *
  704. * WARNINGS: *
  705. * none. *
  706. * *
  707. * HISTORY: *
  708. * 07/12/1995 BRR : Created. *
  709. *=============================================================================================*/
  710. void MapClass::Overlap_Down(CELL cell, ObjectClass * object)
  711. {
  712. if (!object) return;
  713. if (object->Class_Of().IsFootprint && object->In_Which_Layer() == LAYER_GROUND) {
  714. short xlist[32];
  715. List_Copy(object->Overlap_List(), ARRAY_SIZE(xlist), xlist);
  716. short const * list = xlist;
  717. while (*list != REFRESH_EOL) {
  718. CELL newcell = cell + *list++;
  719. if ((unsigned)newcell < MAP_CELL_TOTAL) {
  720. (*this)[newcell].Overlap_Down(object);
  721. (*this)[newcell].Redraw_Objects();
  722. }
  723. }
  724. }
  725. }
  726. /***********************************************************************************************
  727. * MapClass::Overlap_Up -- Computes & clears object's overlap cells *
  728. * *
  729. * This routine is just like Pick_Up, but it doesn't mark the cell's Occupier. *
  730. * This routine is used to implement MARK_OVERLAP_UP, which is useful for changing *
  731. * an object's render size, but not its logical size (ie when it's selected or an *
  732. * animation is attached to it). *
  733. * *
  734. * INPUT: *
  735. * cell -- The cell to base object overlap around. *
  736. * object -- The object to place onto the map. *
  737. * *
  738. * OUTPUT: *
  739. * none. *
  740. * *
  741. * WARNINGS: *
  742. * none. *
  743. * *
  744. * HISTORY: *
  745. * 07/12/1995 BRR : Created. *
  746. *=============================================================================================*/
  747. void MapClass::Overlap_Up(CELL cell, ObjectClass * object)
  748. {
  749. if (!object) return;
  750. if (object->Class_Of().IsFootprint && object->In_Which_Layer() == LAYER_GROUND) {
  751. short xlist[32];
  752. List_Copy(object->Overlap_List(), ARRAY_SIZE(xlist), xlist);
  753. short const * list = xlist;
  754. while (*list != REFRESH_EOL) {
  755. CELL newcell = cell + *list++;
  756. if ((unsigned)newcell < MAP_CELL_TOTAL) {
  757. (*this)[newcell].Overlap_Up(object);
  758. (*this)[newcell].Redraw_Objects();
  759. }
  760. }
  761. }
  762. }
  763. /***********************************************************************************************
  764. * MapClass::Overpass -- Performs any final cleanup to a freshly constructed map. *
  765. * *
  766. * This routine will clean up anything necessary with the presumption that the map has *
  767. * been freshly created. Such things to clean up include various tiberium concentrations. *
  768. * *
  769. * INPUT: none *
  770. * *
  771. * OUTPUT: Returns the total credit value of the tiberium on the map. *
  772. * *
  773. * WARNINGS: none *
  774. * *
  775. * HISTORY: *
  776. * 09/19/1994 JLB : Created. *
  777. * 02/13/1995 JLB : Returns total tiberium worth. *
  778. * 02/15/1995 JLB : Optimal scan. *
  779. *=============================================================================================*/
  780. long MapClass::Overpass(void)
  781. {
  782. long value = 0;
  783. /*
  784. ** Smooth out Tiberium. Cells that are not surrounded by other tiberium
  785. ** will be reduced in density.
  786. */
  787. for (int y = 0; y < MapCellHeight; y++) {
  788. for (int x = 0; x < MapCellWidth; x++) {
  789. CELL cell = (MapCellY+y) * MAP_CELL_W + (MapCellX+x);
  790. value += (*this)[cell].Tiberium_Adjust(true);
  791. (*this)[cell].Recalc_Attributes();
  792. }
  793. }
  794. return(value);
  795. }
  796. /***********************************************************************************************
  797. * MapClass::Write_Binary -- Pipes the map template data to the destination specified. *
  798. * *
  799. * This stores the template data from the map to the output pipe specified. The template *
  800. * data consists of the template type number and template icon number for every cell on *
  801. * the map. The output is organized in such a way so as to get maximum compression. *
  802. * *
  803. * INPUT: pipe -- Reference to the output pipe that will receive the map template data. *
  804. * *
  805. * OUTPUT: Returns with the total number of bytes output to the pipe. *
  806. * *
  807. * WARNINGS: none *
  808. * *
  809. * HISTORY: *
  810. * 07/03/1996 JLB : Created. *
  811. *=============================================================================================*/
  812. int MapClass::Write_Binary(Pipe & pipe)
  813. {
  814. int total = 0;
  815. LCWPipe comp(LCWPipe::COMPRESS);
  816. comp.Put_To(&pipe);
  817. CellClass * cellptr = &Array[0];
  818. for (int i = 0; i < MAP_CELL_TOTAL; i++) {
  819. total += comp.Put(&cellptr->TType, sizeof(cellptr->TType));
  820. cellptr++;
  821. }
  822. cellptr = &Array[0];
  823. for (i = 0; i < MAP_CELL_TOTAL; i++) {
  824. total += comp.Put(&cellptr->TIcon, sizeof(cellptr->TIcon));
  825. cellptr++;
  826. }
  827. return(total);
  828. }
  829. /***********************************************************************************************
  830. * MapClass::Read_Binary -- Reads the binary data from the straw specified. *
  831. * *
  832. * This routine will retrieve the map template data from the straw specified. *
  833. * *
  834. * INPUT: straw -- Reference to the straw that will supply the map template data. *
  835. * *
  836. * OUTPUT: bool; Was the template data retrieved? *
  837. * *
  838. * WARNINGS: none *
  839. * *
  840. * HISTORY: *
  841. * 07/03/1996 JLB : Created. *
  842. *=============================================================================================*/
  843. bool MapClass::Read_Binary(Straw & straw)
  844. {
  845. LCWStraw decomp(LCWStraw::DECOMPRESS);
  846. decomp.Get_From(&straw);
  847. CELL cell;
  848. CellClass * cellptr;
  849. switch (NewINIFormat) {
  850. default:
  851. cellptr = &Array[0];
  852. for (cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  853. decomp.Get(&cellptr->TType, sizeof(cellptr->TType));
  854. cellptr++;
  855. }
  856. cellptr = &Array[0];
  857. for (cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  858. decomp.Get(&cellptr->TIcon, sizeof(cellptr->TIcon));
  859. cellptr->Recalc_Attributes();
  860. cellptr++;
  861. }
  862. break;
  863. case 0:
  864. case 1:
  865. case 2:
  866. cellptr = &Array[0];
  867. for (cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  868. decomp.Get(&cellptr->TType, sizeof(cellptr->TType));
  869. decomp.Get(&cellptr->TIcon, sizeof(cellptr->TIcon));
  870. cellptr->Recalc_Attributes();
  871. cellptr++;
  872. }
  873. break;
  874. }
  875. return(true);
  876. }
  877. /***********************************************************************************************
  878. * MapClass::Logic -- Handles map related logic functions. *
  879. * *
  880. * Manages tiberium growth and spread. *
  881. * *
  882. * INPUT: none *
  883. * *
  884. * OUTPUT: none *
  885. * *
  886. * WARNINGS: none *
  887. * *
  888. * HISTORY: *
  889. * 05/11/1995 JLB : Created. *
  890. * 07/09/1995 JLB : Handles two directional scan. *
  891. * 08/01/1995 JLB : Gives stronger weight to blossom trees. *
  892. *=============================================================================================*/
  893. void MapClass::Logic(void)
  894. {
  895. /*
  896. ** Crate regeneration is handled here.
  897. */
  898. if (Session.Type != GAME_NORMAL && Session.Options.Goodies) {
  899. /*
  900. ** Find any crate that has expired and then regenerate it at a new
  901. ** spot.
  902. */
  903. for (int index = 0; index < ARRAY_SIZE(Crates); index++) {
  904. if (Crates[index].Is_Expired()) {
  905. Crates[index].Remove_It();
  906. Place_Random_Crate();
  907. }
  908. }
  909. }
  910. /*
  911. ** Bail early if there is no allowed growth or spread of Tiberium.
  912. */
  913. if (!Rule.IsTGrowth && !Rule.IsTSpread) return;
  914. /*
  915. ** Scan another block of the map in order to accumulate the potential
  916. ** Tiberium cells that can grow or spread.
  917. */
  918. int subcount = MAP_CELL_TOTAL / (Rule.GrowthRate * TICKS_PER_MINUTE);
  919. subcount = max(subcount, 1);
  920. for (int index = TiberiumScan; index < MAP_CELL_TOTAL; index++) {
  921. CELL cell = index;
  922. if (In_Radar(cell)) {
  923. CellClass * ptr = &(*this)[cell];
  924. /*
  925. ** Tiberium cells can grow.
  926. */
  927. if (ptr->Can_Tiberium_Grow()) {
  928. /*
  929. ** Either replace an existing recorded cell value or add the new cell value to
  930. ** the list.
  931. */
  932. if (Random_Pick(0, TiberiumGrowthExcess) <= TiberiumGrowthCount) {
  933. if (TiberiumGrowthCount < sizeof(TiberiumGrowth)/sizeof(TiberiumGrowth[0])) {
  934. TiberiumGrowth[TiberiumGrowthCount++] = cell;
  935. } else {
  936. TiberiumGrowth[Random_Pick(0, TiberiumGrowthCount-1)] = cell;
  937. }
  938. }
  939. TiberiumGrowthExcess++;
  940. }
  941. /*
  942. ** Heavy Tiberium growth can spread.
  943. */
  944. if (ptr->Can_Tiberium_Spread()) {
  945. /*
  946. ** Either replace an existing recorded cell value or add the new cell value to
  947. ** the list.
  948. */
  949. if (Random_Pick(0, TiberiumSpreadExcess) <= TiberiumSpreadCount) {
  950. if (TiberiumSpreadCount < ARRAY_SIZE(TiberiumSpread)) {
  951. TiberiumSpread[TiberiumSpreadCount++] = cell;
  952. } else {
  953. TiberiumSpread[Random_Pick(0, TiberiumSpreadCount-1)] = cell;
  954. }
  955. }
  956. TiberiumSpreadExcess++;
  957. }
  958. }
  959. subcount--;
  960. if (subcount == 0) break;
  961. }
  962. TiberiumScan = index;
  963. /*
  964. ** When the entire map has been processed, proceed with tiberium (ore) growth
  965. ** and spread action.
  966. */
  967. if (TiberiumScan >= MAP_CELL_TOTAL) {
  968. TiberiumScan = 0;
  969. /*
  970. ** Growth logic.
  971. */
  972. if (TiberiumGrowthCount) {
  973. for (int i = 0; i < TiberiumGrowthCount; i++) {
  974. CELL cell = TiberiumGrowth[i];
  975. CellClass * newcell = &(*this)[cell];
  976. newcell->Grow_Tiberium();
  977. }
  978. }
  979. TiberiumGrowthCount = 0;
  980. TiberiumGrowthExcess = 0;
  981. /*
  982. ** Spread logic.
  983. */
  984. if (TiberiumSpreadCount) {
  985. for (int i = 0; i < TiberiumSpreadCount; i++) {
  986. Map[TiberiumSpread[i]].Spread_Tiberium();
  987. }
  988. }
  989. TiberiumSpreadCount = 0;
  990. TiberiumSpreadExcess = 0;
  991. }
  992. }
  993. /***********************************************************************************************
  994. * MapClass::Cell_Region -- Determines the region from a specified cell number. *
  995. * *
  996. * Use this routine to determine what region a particular cell lies in. *
  997. * *
  998. * INPUT: cell -- The cell number to examine. *
  999. * *
  1000. * OUTPUT: Returns with the region that the specified cell occupies. *
  1001. * *
  1002. * WARNINGS: none *
  1003. * *
  1004. * HISTORY: *
  1005. * 03/15/1995 JLB : Created. *
  1006. *=============================================================================================*/
  1007. int MapClass::Cell_Region(CELL cell)
  1008. {
  1009. return((Cell_X(cell) / REGION_WIDTH) + 1) + (((Cell_Y(cell) / REGION_HEIGHT) + 1) * MAP_REGION_WIDTH);
  1010. }
  1011. /***************************************************************************
  1012. * MapClass::Cell_Threat -- Gets a houses threat value for a cell *
  1013. * *
  1014. * INPUT: CELL cell - the cell number to check *
  1015. * HouseType house - the house to check *
  1016. * *
  1017. * OUTPUT: *
  1018. * *
  1019. * WARNINGS: *
  1020. * *
  1021. * HISTORY: *
  1022. * 04/25/1995 PWG : Created. *
  1023. *=========================================================================*/
  1024. int MapClass::Cell_Threat(CELL cell, HousesType house)
  1025. {
  1026. int threat = HouseClass::As_Pointer(house)->Regions[Map.Cell_Region(Map[cell].Cell_Number())].Threat_Value();
  1027. if (!threat && Map[cell].IsVisible) {
  1028. threat = 1;
  1029. }
  1030. return(threat);
  1031. }
  1032. /***********************************************************************************************
  1033. * MapClass::Place_Random_Crate -- Places a crate at random location on map. *
  1034. * *
  1035. * This routine will place a crate at a random location on the map. This routine will only *
  1036. * make a limited number of attempts to place and if unsuccessful, it will not place any. *
  1037. * *
  1038. * INPUT: none *
  1039. * *
  1040. * OUTPUT: Was a crate successfully placed? *
  1041. * *
  1042. * WARNINGS: none *
  1043. * *
  1044. * HISTORY: *
  1045. * 07/08/1995 JLB : Created. *
  1046. *=============================================================================================*/
  1047. bool MapClass::Place_Random_Crate(void)
  1048. {
  1049. /*
  1050. ** Find a crate index that is free for assignment. If there are
  1051. ** no free slots, then return with failure to place crate.
  1052. */
  1053. int crateindex = 0;
  1054. for (crateindex = 0; crateindex < ARRAY_SIZE(Crates); crateindex++) {
  1055. if (!Crates[crateindex].Is_Valid()) break;
  1056. }
  1057. if (crateindex == ARRAY_SIZE(Crates)) {
  1058. return(false);
  1059. }
  1060. /*
  1061. ** Give a good effort to scan for and place a crate down on the map.
  1062. */
  1063. for (int index = 0; index < 1000; index++) {
  1064. CELL cell = Map.Pick_Random_Location();
  1065. if (Crates[crateindex].Create_Crate(cell)) {
  1066. return(true);
  1067. }
  1068. }
  1069. return(false);
  1070. }
  1071. /***********************************************************************************************
  1072. * MapClass::Remove_Crate -- Remove a crate from the specified cell. *
  1073. * *
  1074. * This will examine the cell and remove any crates there. *
  1075. * *
  1076. * INPUT: cell -- The cell to examine for crates and remove from. *
  1077. * *
  1078. * OUTPUT: bool; Was a crate found at the location specified and was it removed? *
  1079. * *
  1080. * WARNINGS: none *
  1081. * *
  1082. * HISTORY: *
  1083. * 08/26/1996 JLB : Created. *
  1084. *=============================================================================================*/
  1085. bool MapClass::Remove_Crate(CELL cell)
  1086. {
  1087. if (Session.Type != GAME_NORMAL) {
  1088. for (int index = 0; index < ARRAY_SIZE(Crates); index++) {
  1089. if (Crates[index].Is_Here(cell)) {
  1090. return(Crates[index].Remove_It());
  1091. }
  1092. }
  1093. }
  1094. // if (Session.Type == GAME_NORMAL) {
  1095. CellClass * cellptr = &(*this)[cell];
  1096. if (cellptr->Overlay != OVERLAY_NONE && OverlayTypeClass::As_Reference(cellptr->Overlay).IsCrate) {
  1097. cellptr->Overlay = OVERLAY_NONE;
  1098. cellptr->OverlayData = 0;
  1099. return(true);
  1100. }
  1101. // } else {
  1102. // for (int index = 0; index < ARRAY_SIZE(Crates); index++) {
  1103. // if (Crates[index].Is_Here(cell)) {
  1104. // return(Crates[index].Remove_It());
  1105. // }
  1106. // }
  1107. // }
  1108. return(false);
  1109. }
  1110. /***************************************************************************
  1111. * MapClass::Validate -- validates every cell on the map *
  1112. * *
  1113. * This is a debugging routine, designed to detect memory trashers that *
  1114. * alter the map. This routine is slow, but thorough. *
  1115. * *
  1116. * INPUT: *
  1117. * none. *
  1118. * *
  1119. * OUTPUT: *
  1120. * true = map is OK, false = an error was found *
  1121. * *
  1122. * WARNINGS: *
  1123. * none. *
  1124. * *
  1125. * HISTORY: *
  1126. * 07/08/1995 BRR : Created. *
  1127. *=========================================================================*/
  1128. int MapClass::Validate(void)
  1129. {
  1130. CELL cell;
  1131. TemplateType ttype;
  1132. unsigned char ticon;
  1133. TemplateTypeClass const *tclass;
  1134. unsigned char map[13*8];
  1135. OverlayType overlay;
  1136. SmudgeType smudge;
  1137. ObjectClass * obj;
  1138. LandType land;
  1139. int i;
  1140. BlubCell = &Array[797];
  1141. if (BlubCell->Overlapper[1]) {
  1142. obj = BlubCell->Overlapper[1];
  1143. if (obj) {
  1144. if (obj->IsInLimbo)
  1145. obj = obj;
  1146. }
  1147. }
  1148. /*
  1149. ** Check every cell on the map, even those that aren't displayed,
  1150. ** in the hopes of detecting a memory trasher.
  1151. */
  1152. for (cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  1153. /*
  1154. ** Validate Template & Icon data
  1155. */
  1156. ttype = (*this)[cell].TType;
  1157. ticon = (*this)[cell].TIcon;
  1158. if (ttype >= TEMPLATE_COUNT && ttype != TEMPLATE_NONE)
  1159. return(false);
  1160. /*
  1161. ** To validate the icon value, we have to get a copy of the template's
  1162. ** "icon map"; this map will have 0xff's in spots where there is no
  1163. ** icon. If the icon value is out of range or points to an invalid spot,
  1164. ** return an error.
  1165. */
  1166. if (ttype != TEMPLATE_NONE) {
  1167. tclass = &TemplateTypeClass::As_Reference(ttype);
  1168. ticon = (*this)[cell].TIcon;
  1169. Mem_Copy(Get_Icon_Set_Map(tclass->Get_Image_Data()), map, tclass->Width * tclass->Height);
  1170. if (ticon < 0 || ticon >= (tclass->Width * tclass->Height) || map[ticon]==0xff) {
  1171. return (false);
  1172. }
  1173. }
  1174. /*
  1175. ** Validate Overlay
  1176. */
  1177. overlay = (*this)[cell].Overlay;
  1178. if (overlay < OVERLAY_NONE || overlay >= OVERLAY_COUNT) {
  1179. return(false);
  1180. }
  1181. /*
  1182. ** Validate Smudge
  1183. */
  1184. smudge = (*this)[cell].Smudge;
  1185. if (smudge < SMUDGE_NONE || smudge >= SMUDGE_COUNT) {
  1186. return(false);
  1187. }
  1188. /*
  1189. ** Validate LandType
  1190. */
  1191. land = (*this)[cell].Land_Type();
  1192. if (land < LAND_CLEAR || land >= LAND_COUNT) {
  1193. return(false);
  1194. }
  1195. /*
  1196. ** Validate Occupier
  1197. */
  1198. obj = (*this)[cell].Cell_Occupier();
  1199. if (obj) {
  1200. if (
  1201. ((unsigned int)obj & 0xff000000) ||
  1202. ((unsigned int)obj->Next & 0xff000000) ||
  1203. // ((unsigned int)obj->Trigger & 0xff000000) ||
  1204. obj->IsInLimbo ||
  1205. ((unsigned int)Coord_Cell(obj->Coord) >= MAP_CELL_TOTAL)) {
  1206. return (false);
  1207. }
  1208. }
  1209. /*
  1210. ** Validate Overlappers
  1211. */
  1212. for (i = 0; i < ARRAY_SIZE((*this)[cell].CellClass::Overlapper); i++) {
  1213. obj = (*this)[cell].Overlapper[i];
  1214. if (obj) {
  1215. if (
  1216. ((unsigned int)obj & 0xff000000) ||
  1217. ((unsigned int)obj->Next & 0xff000000) ||
  1218. // ((unsigned int)obj->Trigger & 0xff000000) ||
  1219. obj->IsInLimbo ||
  1220. ((unsigned int)Coord_Cell(obj->Coord) >= MAP_CELL_TOTAL)) {
  1221. return (false);
  1222. }
  1223. }
  1224. }
  1225. }
  1226. return (true);
  1227. }
  1228. /***********************************************************************************************
  1229. * MapClass::Close_Object -- Finds a clickable close object to the specified coordinate. *
  1230. * *
  1231. * This routine is used by the mouse input processing code to find a clickable object *
  1232. * close to coordinate specified. This is for targeting as well as selection determination. *
  1233. * *
  1234. * INPUT: coord -- The coordinate to scan for close object from. *
  1235. * *
  1236. * OUTPUT: Returns with a pointer to an object that is nearby the specified coordinate. *
  1237. * *
  1238. * WARNINGS: There could be a cloaked object at the location, but it won't be considered *
  1239. * if it is not owned by the player. *
  1240. * *
  1241. * HISTORY: *
  1242. * 08/20/1995 JLB : Created. *
  1243. *=============================================================================================*/
  1244. ObjectClass * MapClass::Close_Object(COORDINATE coord) const
  1245. {
  1246. ObjectClass * object = 0;
  1247. int distance = 0;
  1248. CELL cell = Coord_Cell(coord);
  1249. /*
  1250. ** Scan through current and adjacent cells, looking for the
  1251. ** closest object (within reason) to the specified coordinate.
  1252. */
  1253. static int _offsets[] = {0, -1, 1, -MAP_CELL_W, MAP_CELL_W, MAP_CELL_W-1, MAP_CELL_W+1, -(MAP_CELL_W-1), -(MAP_CELL_W+1)};
  1254. for (int index = 0; index < (sizeof(_offsets) / sizeof(_offsets[0])); index++) {
  1255. /*
  1256. ** Examine the cell for close object. Make sure that the cell actually is a
  1257. ** legal one.
  1258. */
  1259. CELL newcell = cell + _offsets[index];
  1260. if (In_Radar(newcell)) {
  1261. /*
  1262. ** Search through all objects that occupy this cell and then
  1263. ** find the closest object. Check against any previously found object
  1264. ** to ensure that it is actually closer.
  1265. */
  1266. ObjectClass * o = Array[newcell].Cell_Occupier();
  1267. while (o != NULL) {
  1268. /*
  1269. ** Special case check to ignore cloaked object if not owned by the player.
  1270. */
  1271. if (!o->Is_Techno() || ((TechnoClass *)o)->IsOwnedByPlayer || ((TechnoClass *)o)->Cloak != CLOAKED) {
  1272. int d=-1;
  1273. if (o->What_Am_I() == RTTI_BUILDING) {
  1274. d = Distance(coord, Cell_Coord(newcell));
  1275. if (d > 0x00B5) d = -1;
  1276. } else {
  1277. d = Distance(coord, o->Center_Coord());
  1278. }
  1279. if (d >= 0 && (!object || d < distance)) {
  1280. distance = d;
  1281. object = o;
  1282. }
  1283. }
  1284. o = o->Next;
  1285. }
  1286. }
  1287. }
  1288. /*
  1289. ** Only return the object if it is within 1/4 cell distance from the specified
  1290. ** coordinate.
  1291. */
  1292. if (object && distance > 0xB5) {
  1293. object = 0;
  1294. }
  1295. return(object);
  1296. }
  1297. /***********************************************************************************************
  1298. * MapClass::Zone_Reset -- Resets all zone numbers to match the map. *
  1299. * *
  1300. * This routine will rescan the map and fill in the zone values for each of the cells. *
  1301. * All cells that are contiguous are given the same zone number. *
  1302. * *
  1303. * INPUT: method -- The method to recalculate the zones upon. If 1 then recalc non *
  1304. * crushable zone. If 2 then recalc crushable zone. If 3, then *
  1305. * recalc both zones. *
  1306. * *
  1307. * OUTPUT: none *
  1308. * *
  1309. * WARNINGS: This is a time consuming routine. Call it as infrequently as possible. It must *
  1310. * be called whenever something that would affect contiguousness occurs. Example: *
  1311. * when a bridge is built or destroyed. *
  1312. * *
  1313. * HISTORY: *
  1314. * 09/22/1995 JLB : Created. *
  1315. *=============================================================================================*/
  1316. bool MapClass::Zone_Reset(int method)
  1317. {
  1318. /*
  1319. ** Zero out all zones to a null state.
  1320. */
  1321. for (int index = 0; index < MAP_CELL_TOTAL; index++) {
  1322. if (method & MZONEF_NORMAL) {
  1323. Array[index].Zones[MZONE_NORMAL] = 0;
  1324. }
  1325. if (method & MZONEF_CRUSHER) {
  1326. Array[index].Zones[MZONE_CRUSHER] = 0;
  1327. }
  1328. if (method & MZONEF_DESTROYER) {
  1329. Array[index].Zones[MZONE_DESTROYER] = 0;
  1330. }
  1331. if (method & MZONEF_WATER) {
  1332. Array[index].Zones[MZONE_WATER] = 0;
  1333. }
  1334. }
  1335. /*
  1336. ** Normal zone recalculation.
  1337. */
  1338. if (method & MZONEF_NORMAL) {
  1339. int zone = 1; // Starting zone number.
  1340. for (CELL cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  1341. if (Zone_Span(cell, zone, MZONE_NORMAL)) {
  1342. zone++;
  1343. }
  1344. }
  1345. }
  1346. /*
  1347. ** Crushable wall recalculation.
  1348. */
  1349. if (method & MZONEF_CRUSHER) {
  1350. int zone = 1; // Starting zone number.
  1351. for (CELL cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  1352. if (Zone_Span(cell, zone, MZONE_CRUSHER)) {
  1353. zone++;
  1354. }
  1355. }
  1356. }
  1357. /*
  1358. ** Wall destroyer zone recalculation.
  1359. */
  1360. if (method & MZONEF_DESTROYER) {
  1361. int zone = 1; // Starting zone number.
  1362. for (CELL cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  1363. if (Zone_Span(cell, zone, MZONE_DESTROYER)) {
  1364. zone++;
  1365. }
  1366. }
  1367. }
  1368. /*
  1369. ** Water based zone recalcuation.
  1370. */
  1371. if (method & MZONEF_WATER) {
  1372. int zone = 1; // Starting zone number.
  1373. for (CELL cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  1374. if (Zone_Span(cell, zone, MZONE_WATER)) {
  1375. zone++;
  1376. }
  1377. }
  1378. }
  1379. return(false);
  1380. }
  1381. /***********************************************************************************************
  1382. * MapClass::Zone_Span -- Flood fills the specified zone from the cell origin. *
  1383. * *
  1384. * This routine is used to fill a zone value into the map. The map is "flood filled" from *
  1385. * the cell specified. All adjacent (8 connected) and generally passable terrain cells are *
  1386. * given the zone number specified. This routine checks for legality before filling *
  1387. * occurs. The routine is safe to call even if the legality of the cell is unknown at the *
  1388. * time of the call. *
  1389. * *
  1390. * INPUT: cell -- The cell to begin filling from. *
  1391. * *
  1392. * zone -- The zone number to assign to all adjacent cells. *
  1393. * *
  1394. * check -- The zone type to check against. *
  1395. * *
  1396. * OUTPUT: Returns with the number of cells marked by this routine. *
  1397. * *
  1398. * WARNINGS: This routine is slow and recursive. Only use when necessary. *
  1399. * *
  1400. * HISTORY: *
  1401. * 09/25/1995 JLB : Created. *
  1402. * 10/05/1996 JLB : Examines crushable walls. *
  1403. *=============================================================================================*/
  1404. int MapClass::Zone_Span(CELL cell, int zone, MZoneType check)
  1405. {
  1406. int filled = 0;
  1407. int xbegin = Cell_X(cell);
  1408. int xend = xbegin;
  1409. int y = Cell_Y(cell);
  1410. /*
  1411. ** Perform some preliminary legality checks. If the cell specified
  1412. ** is illegal, then no further processing is necessary.
  1413. */
  1414. if (y < MapCellY || y >= MapCellY+MapCellHeight || xbegin < MapCellX || xbegin >= MapCellX+MapCellWidth) {
  1415. return(0);
  1416. }
  1417. /*
  1418. ** Find the full extent of the current span by first scanning leftward
  1419. ** until a boundary is reached.
  1420. */
  1421. for (; xbegin >= MapCellX; xbegin--) {
  1422. CellClass * cellptr = &(*this)[XY_Cell(xbegin, y)];
  1423. if (cellptr->Zones[check] != 0 || (!cellptr->Is_Clear_To_Move(check == MZONE_WATER ? SPEED_FLOAT : SPEED_TRACK, true, true, -1, check))) {
  1424. /*
  1425. ** Special short circuit code to bail from this entire routine if
  1426. ** it was called for a cell that is not a legal candidate for
  1427. ** zone marking. This eliminates redundant processing and allows this
  1428. ** routine to be called for illegal cells without causing an error.
  1429. */
  1430. if (xbegin == Cell_X(cell)) return(0);
  1431. /*
  1432. ** Otherwise break out of the left scan since a boundary was discovered.
  1433. */
  1434. xbegin++;
  1435. break;
  1436. }
  1437. }
  1438. xbegin = max(xbegin, MapCellX);
  1439. /*
  1440. ** Scan rightward until a boundary is reached. This will then define the
  1441. ** extent of the current span.
  1442. */
  1443. for (; xend < MapCellX+MapCellWidth; xend++) {
  1444. CellClass * cellptr = &(*this)[XY_Cell(xend, y)];
  1445. if (cellptr->Zones[check] != 0 || (!cellptr->Is_Clear_To_Move(check == MZONE_WATER ? SPEED_FLOAT : SPEED_TRACK, true, true, -1, check))) {
  1446. xend--;
  1447. break;
  1448. }
  1449. }
  1450. xend = min(xend, MapCellX+MapCellWidth-1);
  1451. /*
  1452. ** At this point we know the bounds of the current span. Fill in the zone values
  1453. ** for the entire span.
  1454. */
  1455. for (int x = xbegin; x <= xend; x++) {
  1456. (*this)[XY_Cell(x, y)].Zones[check] = zone;
  1457. filled++;
  1458. }
  1459. /*
  1460. ** Now scan the upper and lower shadow rows. If any of these rows contain
  1461. ** candidate cells, then recursively call the span process for them. Take
  1462. ** note that the adjacent span scanning starts one cell wider on each
  1463. ** end of the scan. This is necessary because diagonals are considered
  1464. ** adjacent.
  1465. */
  1466. for (x = xbegin-1; x <= xend; x++) {
  1467. filled += Zone_Span(XY_Cell(x, y-1), zone, check);
  1468. filled += Zone_Span(XY_Cell(x, y+1), zone, check);
  1469. }
  1470. return(filled);
  1471. }
  1472. /***********************************************************************************************
  1473. * MapClass::Nearby_Location -- Finds a generally clear location near a specified cell. *
  1474. * *
  1475. * This routine is used to find a location that probably will be ok to move to that is *
  1476. * located as close as possible to the specified cell. The computer uses this when it has *
  1477. * determined the ideal location for an object, but then needs to give a valid movement *
  1478. * destination to a unit. *
  1479. * *
  1480. * INPUT: cell -- The cell that scanning should radiate out from. *
  1481. * *
  1482. * zone -- The zone that must be matched to find a legal location (value of -1 means *
  1483. * any zone will do). *
  1484. * *
  1485. * *
  1486. * check -- The type of zone to check against. Only valid if a zone value is given. *
  1487. * *
  1488. * OUTPUT: Returns with the cell that is generally clear (legal to move to) that is close *
  1489. * to the specified cell. *
  1490. * *
  1491. * WARNINGS: none *
  1492. * *
  1493. * HISTORY: *
  1494. * 10/05/1995 JLB : Created. *
  1495. *=============================================================================================*/
  1496. CELL MapClass::Nearby_Location(CELL cell, SpeedType speed, int zone, MZoneType check) const
  1497. {
  1498. CELL topten[10];
  1499. int count = 0;
  1500. int xx = Cell_X(cell);
  1501. int yy = Cell_Y(cell);
  1502. /*
  1503. ** Determine the limits of the scanning in the four directions so that
  1504. ** it won't scan past the edge of the world.
  1505. */
  1506. int left = xx - MapCellX;
  1507. int right = (MapCellWidth - left) - 1;
  1508. int top = yy - MapCellY;
  1509. int bottom = (MapCellHeight - top) - 1;
  1510. /*
  1511. ** Radiate outward from the specified location, looking for the closest
  1512. ** location that is generally clear.
  1513. */
  1514. for (int radius = 0; radius < MAP_CELL_W/2; radius++) {
  1515. CELL newcell;
  1516. CellClass const * cellptr;
  1517. /*
  1518. ** Scan the top and bottom rows of the "box".
  1519. */
  1520. for (int x = -radius; x <= radius; x++) {
  1521. if (x >= -left && radius <= top) {
  1522. newcell = XY_Cell(xx+x, yy-radius);
  1523. cellptr = &Map[newcell];
  1524. if (Map.In_Radar(newcell) && cellptr->Is_Clear_To_Move(speed, false, false, zone, check)) {
  1525. topten[count++] = newcell;
  1526. }
  1527. }
  1528. if (count == ARRAY_SIZE(topten)) break;
  1529. if (x <= right && radius <= bottom) {
  1530. newcell = XY_Cell(xx+x, yy+radius);
  1531. cellptr = &Map[newcell];
  1532. if (Map.In_Radar(newcell) && cellptr->Is_Clear_To_Move(speed, false, false, zone, check)) {
  1533. topten[count++] = newcell;
  1534. }
  1535. }
  1536. if (count == ARRAY_SIZE(topten)) break;
  1537. }
  1538. if (count == ARRAY_SIZE(topten)) break;
  1539. /*
  1540. ** Scan the left and right columns of the "box".
  1541. */
  1542. for (int y = -(radius-1); y <= radius-1; y++) {
  1543. if (y >= -top && radius <= left) {
  1544. newcell = XY_Cell(xx-radius, yy+y);
  1545. cellptr = &Map[newcell];
  1546. if (Map.In_Radar(newcell) && cellptr->Is_Clear_To_Move(speed, false, false, zone, check)) {
  1547. topten[count++] = newcell;
  1548. }
  1549. }
  1550. if (count == ARRAY_SIZE(topten)) break;
  1551. if (y <= bottom && radius <= right) {
  1552. newcell = XY_Cell(xx+radius, yy+y);
  1553. cellptr = &Map[newcell];
  1554. if (Map.In_Radar(newcell) && cellptr->Is_Clear_To_Move(speed, false, false, zone, check)) {
  1555. topten[count++] = newcell;
  1556. }
  1557. }
  1558. if (count == ARRAY_SIZE(topten)) break;
  1559. }
  1560. if (count > 0) break;
  1561. }
  1562. if (count > 0) {
  1563. return(topten[Frame % count]);
  1564. }
  1565. return(0);
  1566. }
  1567. /***********************************************************************************************
  1568. * MapClass::Base_Region -- Finds the owner and base zone for specified cell. *
  1569. * *
  1570. * This routine is used to determine what base the specified cell is close to and what *
  1571. * zone of that base the cell lies in. This routine is particularly useful in letting the *
  1572. * computer know when the player targets a destination near a computer's base. *
  1573. * *
  1574. * INPUT: cell -- The cell that is to be checked. *
  1575. * *
  1576. * house -- Reference to the house type number. This value will be set if a base *
  1577. * was found nearby the specified cell. *
  1578. * *
  1579. * zone -- The zone that the cell is located in IF the cell is near a base. *
  1580. * *
  1581. * *
  1582. * OUTPUT: Was a base near the specified cell found? If not, then the 'house' and 'zone' *
  1583. * reference values are left in an undefined state and the return value will be *
  1584. * false. *
  1585. * *
  1586. * WARNINGS: none *
  1587. * *
  1588. * HISTORY: *
  1589. * 10/05/1995 JLB : Created. *
  1590. *=============================================================================================*/
  1591. bool MapClass::Base_Region(CELL cell, HousesType & house, ZoneType & zone) const
  1592. {
  1593. if ((unsigned)cell < MAP_CELL_TOTAL && In_Radar(cell)) {
  1594. for (house = HOUSE_FIRST; house < HOUSE_COUNT; house++) {
  1595. HouseClass * h = HouseClass::As_Pointer(house);
  1596. if (h && h->IsActive && !h->IsDefeated && h->Center) {
  1597. zone = h->Which_Zone(cell);
  1598. if (zone != ZONE_NONE) {
  1599. return(true);
  1600. }
  1601. }
  1602. }
  1603. }
  1604. return(false);
  1605. }
  1606. /***********************************************************************************************
  1607. * MapClass::Destroy_Bridge_At -- Destroyes the bridge at location specified. *
  1608. * *
  1609. * This routine will destroy the bridge at the location specified. *
  1610. * *
  1611. * INPUT: cell -- A cell that can uniquely identify the bridge. *
  1612. * *
  1613. * OUTPUT: bool; Was the bridge destroyed? *
  1614. * *
  1615. * WARNINGS: none *
  1616. * *
  1617. * HISTORY: *
  1618. * 07/29/1996 JLB : Created. *
  1619. *=============================================================================================*/
  1620. bool MapClass::Destroy_Bridge_At(CELL cell)
  1621. {
  1622. if (In_Radar(cell) && !Special.IsCaptureTheFlag) {
  1623. CellClass * cellptr = &(*this)[cell];
  1624. TemplateType ttype = cellptr->TType;
  1625. if (ttype == TEMPLATE_BRIDGE1 || ttype == TEMPLATE_BRIDGE2) {
  1626. int icon = cellptr->TIcon;
  1627. int w = TemplateTypeClass::As_Reference(ttype).Width;
  1628. int h = TemplateTypeClass::As_Reference(ttype).Height;
  1629. cell -= icon % w;
  1630. cell -= MAP_CELL_W * (icon / w);
  1631. if (ttype == TEMPLATE_BRIDGE1) {
  1632. new TemplateClass(TEMPLATE_BRIDGE1H, cell);
  1633. } else {
  1634. new TemplateClass(TEMPLATE_BRIDGE2H, cell);
  1635. }
  1636. new AnimClass(ANIM_NAPALM3, Cell_Coord(cell + w/2 + (h/2)*MAP_CELL_W));
  1637. }
  1638. if (ttype == TEMPLATE_BRIDGE1H || ttype == TEMPLATE_BRIDGE2H) {
  1639. int icon = cellptr->TIcon;
  1640. int bridge_w = TemplateTypeClass::As_Reference(ttype).Width;
  1641. int bridge_h = TemplateTypeClass::As_Reference(ttype).Height;
  1642. cell -= icon % bridge_w;
  1643. cell -= MAP_CELL_W * (icon / bridge_w);
  1644. if (ttype == TEMPLATE_BRIDGE1H) {
  1645. new TemplateClass(TEMPLATE_BRIDGE1D, cell);
  1646. } else {
  1647. new TemplateClass(TEMPLATE_BRIDGE2D, cell);
  1648. }
  1649. Scen.BridgeCount--;
  1650. Scen.IsBridgeChanged = true;
  1651. new AnimClass(ANIM_NAPALM3, Cell_Coord(cell + bridge_w/2 + (bridge_h/2)*MAP_CELL_W));
  1652. Map.Zone_Reset(MZONEF_ALL);
  1653. /*
  1654. ** Now, loop through all the bridge cells and find anyone standing
  1655. ** on a destroyed part (which is now river), and nuke them.
  1656. */
  1657. for (int y = 0; y < bridge_h; y++) {
  1658. for (int x = 0; x < bridge_w; x++) {
  1659. CellClass * bridge_cell = &(*this)[cell];
  1660. if (bridge_cell->TType == ttype) {
  1661. /*
  1662. ** Any unit that is firing on the bridge at this location, will stop
  1663. ** firing because the bridge has been destroyed.
  1664. */
  1665. Detach_This_From_All(As_Target(cell), true);
  1666. ObjectClass * obj = bridge_cell->Cell_Occupier();
  1667. while (obj != NULL) {
  1668. ObjectClass * next = obj->Next;
  1669. if (obj->Is_Techno()) {
  1670. int damage = obj->Strength;
  1671. obj->Take_Damage(damage, 0, WARHEAD_HE, NULL, true);
  1672. }
  1673. obj = next;
  1674. }
  1675. }
  1676. cell++;
  1677. }
  1678. cell += MAP_CELL_W - bridge_w;
  1679. }
  1680. Shake_The_Screen(3);
  1681. return(true);
  1682. } else {
  1683. /*
  1684. ** All this code is for the multi-part bridges.
  1685. */
  1686. if (ttype >= TEMPLATE_BRIDGE_1A && ttype <= TEMPLATE_BRIDGE_3E) {
  1687. int icon = cellptr->TIcon;
  1688. int w = TemplateTypeClass::As_Reference(ttype).Width;
  1689. int h = TemplateTypeClass::As_Reference(ttype).Height;
  1690. cell -= icon % w;
  1691. cell -= MAP_CELL_W * (icon / w);
  1692. switch (ttype) {
  1693. case TEMPLATE_BRIDGE_1A:
  1694. case TEMPLATE_BRIDGE_1B:
  1695. case TEMPLATE_BRIDGE_2A:
  1696. case TEMPLATE_BRIDGE_2B:
  1697. case TEMPLATE_BRIDGE_3A:
  1698. case TEMPLATE_BRIDGE_3B:
  1699. ttype++;
  1700. new TemplateClass(TemplateType(ttype), cell);
  1701. break;
  1702. }
  1703. /*
  1704. ** If we were a middle piece that just got blown up, update the
  1705. ** adjoining pieces to make sure they're shaped properly.
  1706. */
  1707. if (ttype == TEMPLATE_BRIDGE_3C) {
  1708. // check the template below us, at x-1, y+1
  1709. CELL cell2 = cell + (MAP_CELL_W - 1);
  1710. CellClass * celptr = &(*this)[cell2];
  1711. if (celptr->TType == TEMPLATE_BRIDGE_3C) {
  1712. // It was also destroyed. Update us and it.
  1713. new TemplateClass(TemplateType(TEMPLATE_BRIDGE_3D), cell);
  1714. new TemplateClass(TemplateType(TEMPLATE_BRIDGE_3E), cell2);
  1715. }
  1716. // Now check the template above us, at x+1, y-1.
  1717. cell2 = cell - (MAP_CELL_W - 1);
  1718. celptr = &(*this)[cell2];
  1719. if (celptr->TType == TEMPLATE_BRIDGE_3C) {
  1720. if (cellptr->TType == TEMPLATE_BRIDGE_3D) {
  1721. // if we're already one-sided, turn us to all water
  1722. new TemplateClass(TemplateType(TEMPLATE_BRIDGE_3F), cell);
  1723. } else {
  1724. new TemplateClass(TemplateType(TEMPLATE_BRIDGE_3E), cell);
  1725. }
  1726. new TemplateClass(TemplateType(TEMPLATE_BRIDGE_3D), cell2);
  1727. }
  1728. Map.Zone_Reset(MZONEF_ALL);
  1729. }
  1730. /*
  1731. ** If we're an end bridge piece, update the adjoining piece to
  1732. ** be the proper shape.
  1733. */
  1734. if (cellptr->TType == TEMPLATE_BRIDGE_1C) {
  1735. Scen.BridgeCount--;
  1736. Scen.IsBridgeChanged = true;
  1737. // Point to the template below us, x-1, y+2
  1738. CELL cell2 = cell + (MAP_CELL_W * 2) - 1;
  1739. switch ((*this)[cell2].TType) {
  1740. case TEMPLATE_BRIDGE_3A:
  1741. case TEMPLATE_BRIDGE_3B:
  1742. case TEMPLATE_BRIDGE_3C:
  1743. new TemplateClass(TemplateType(TEMPLATE_BRIDGE_3E), cell2);
  1744. break;
  1745. case TEMPLATE_BRIDGE_3D:
  1746. new TemplateClass(TemplateType(TEMPLATE_BRIDGE_3F), cell2);
  1747. break;
  1748. }
  1749. } else {
  1750. if (cellptr->TType == TEMPLATE_BRIDGE_2C) {
  1751. // Point to the template above us, x+2, y-1
  1752. CELL cell2 = cell - (MAP_CELL_W - 2);
  1753. switch ((*this)[cell2].TType) {
  1754. case TEMPLATE_BRIDGE_3A:
  1755. case TEMPLATE_BRIDGE_3B:
  1756. case TEMPLATE_BRIDGE_3C:
  1757. new TemplateClass(TemplateType(TEMPLATE_BRIDGE_3D), cell2);
  1758. break;
  1759. case TEMPLATE_BRIDGE_3E:
  1760. new TemplateClass(TemplateType(TEMPLATE_BRIDGE_3F), cell2);
  1761. break;
  1762. }
  1763. }
  1764. }
  1765. if (cellptr->TType == TEMPLATE_BRIDGE_1C ||
  1766. cellptr->TType == TEMPLATE_BRIDGE_2C ||
  1767. (cellptr->TType >= TEMPLATE_BRIDGE_3C && cellptr->TType <= TEMPLATE_BRIDGE_3E)) {
  1768. int x, y, tdata = 0;
  1769. for (y = 0; y < h; y++) {
  1770. for (x = 0; x < w; x++) {
  1771. CellClass * ptr = &(*this)[(CELL)(cell + x)];
  1772. if (ptr->TType == cellptr->TType || ptr->Land_Type() == LAND_RIVER || ptr->Land_Type() == LAND_WATER) {
  1773. Detach_This_From_All(As_Target((CELL)(cell+tdata)), true);
  1774. ObjectClass * obj = ptr->Cell_Occupier();
  1775. while (obj != NULL) {
  1776. ObjectClass * next = obj->Next;
  1777. if (obj->Is_Techno()) {
  1778. int damage = obj->Strength;
  1779. obj->Take_Damage(damage, 0, WARHEAD_HE, NULL, true);
  1780. }
  1781. obj = next;
  1782. }
  1783. }
  1784. tdata++;
  1785. }
  1786. cell += MAP_CELL_W;
  1787. }
  1788. Shake_The_Screen(3);
  1789. Map.Zone_Reset(MZONEF_ALL);
  1790. return(true);
  1791. }
  1792. Shake_The_Screen(3);
  1793. }
  1794. }
  1795. }
  1796. return(false);
  1797. }
  1798. /***********************************************************************************************
  1799. * MapClass::Detach -- Remove specified object from map references. *
  1800. * *
  1801. * This routine will take the object (represented by a target value) and remove all *
  1802. * references to it from the map. Typically, this is used to remove trigger reference. *
  1803. * *
  1804. * INPUT: target -- The target object to remove from the map. *
  1805. * *
  1806. * OUTPUT: none *
  1807. * *
  1808. * WARNINGS: none *
  1809. * *
  1810. * HISTORY: *
  1811. * 07/28/1996 JLB : Created. *
  1812. *=============================================================================================*/
  1813. void MapClass::Detach(TARGET target, bool)
  1814. {
  1815. /*
  1816. ** Remove this trigger from the map zone/line tracking list.
  1817. */
  1818. if (Is_Target_Trigger(target)) {
  1819. for (int index = 0; index < MapTriggers.Count(); index++) {
  1820. if (MapTriggers[index] == As_Trigger(target)) {
  1821. MapTriggers.Delete(index);
  1822. break;
  1823. }
  1824. }
  1825. /*
  1826. ** Loop through all cells; remove any reference to this trigger
  1827. */
  1828. for (CELL cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  1829. if ((*this)[cell].Trigger == As_Trigger(target)) {
  1830. (*this)[cell].Trigger = NULL;
  1831. }
  1832. }
  1833. }
  1834. }
  1835. /***********************************************************************************************
  1836. * MapClass::Intact_Bridge_Count -- Determine the number of intact bridges. *
  1837. * *
  1838. * This will examine the entire map and return the number of bridges that are intact. An *
  1839. * intact bridge is one that units can travel over. *
  1840. * *
  1841. * INPUT: none *
  1842. * *
  1843. * OUTPUT: Returns with the number of intact bridges on the map. *
  1844. * *
  1845. * WARNINGS: none *
  1846. * *
  1847. * HISTORY: *
  1848. * 07/28/1996 JLB : Created. *
  1849. *=============================================================================================*/
  1850. int MapClass::Intact_Bridge_Count(void) const
  1851. {
  1852. /*
  1853. ** Count all non-destroyed bridges on the map.
  1854. */
  1855. int count = 0;
  1856. CellClass const * cellptr = &(*this)[(CELL)0];
  1857. for (CELL cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  1858. switch (cellptr->TType) {
  1859. case TEMPLATE_BRIDGE1:
  1860. case TEMPLATE_BRIDGE1H:
  1861. case TEMPLATE_BRIDGE2:
  1862. case TEMPLATE_BRIDGE2H:
  1863. case TEMPLATE_BRIDGE_1A:
  1864. case TEMPLATE_BRIDGE_1B:
  1865. if (cellptr->TIcon == 6) {
  1866. count++;
  1867. }
  1868. break;
  1869. default:
  1870. break;
  1871. }
  1872. cellptr++;
  1873. }
  1874. return(count);
  1875. }
  1876. /***********************************************************************************************
  1877. * MapClass::Pick_Random_Location -- Picks a random location on the map. *
  1878. * *
  1879. * This routine will pick a random location on the map. It performs no legality checking *
  1880. * other than forcing the cell to be on the map proper. *
  1881. * *
  1882. * INPUT: none *
  1883. * *
  1884. * OUTPUT: Returns with a cell that is within the map. *
  1885. * *
  1886. * WARNINGS: none *
  1887. * *
  1888. * HISTORY: *
  1889. * 09/25/1996 JLB : Created. *
  1890. *=============================================================================================*/
  1891. CELL MapClass::Pick_Random_Location(void) const
  1892. {
  1893. int x = Map.MapCellX + Random_Pick(0, Map.MapCellWidth-1);
  1894. int y = Map.MapCellY + Random_Pick(0, Map.MapCellHeight-1);
  1895. return(XY_Cell(x, y));
  1896. }
  1897. /***********************************************************************************************
  1898. * MapClass::Shroud_The_Map -- cover the whole map in darkness (usually from blackout crate) *
  1899. * *
  1900. * INPUT: none *
  1901. * *
  1902. * OUTPUT: Returns with a cell that is within the map. *
  1903. * *
  1904. * WARNINGS: none *
  1905. * *
  1906. * HISTORY: *
  1907. * 10/19/1996 BWG : Created. *
  1908. *=============================================================================================*/
  1909. void MapClass::Shroud_The_Map(void)
  1910. {
  1911. for (CELL cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  1912. CellClass * cellptr = &Map[cell];
  1913. if (cellptr->IsMapped || cellptr->IsVisible) {
  1914. cellptr->Redraw_Objects();
  1915. /*
  1916. ** BG: remove "ring of darkness" around edge of map.
  1917. */
  1918. int x = Cell_X(cell);
  1919. int y = Cell_Y(cell);
  1920. if (x >= Map.MapCellX && x < (Map.MapCellX + Map.MapCellWidth) &&
  1921. y >= Map.MapCellY && y < (Map.MapCellY + Map.MapCellHeight)) {
  1922. cellptr->IsMapped = false;
  1923. cellptr->IsVisible = false;
  1924. }
  1925. }
  1926. }
  1927. for (int obj_index = 0; obj_index < DisplayClass::Layer[LAYER_GROUND].Count(); obj_index++) {
  1928. ObjectClass * layer_object = DisplayClass::Layer[LAYER_GROUND][obj_index];
  1929. if (layer_object && layer_object->Is_Techno() && ((TechnoClass *)layer_object)->House == PlayerPtr) {
  1930. layer_object->Look();
  1931. }
  1932. }
  1933. Flag_To_Redraw(true);
  1934. }