Terrain.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. //---------------------------------------------------------------------------
  2. //
  3. // Terrain.cpp -- File contains calss definitions for the Terrain
  4. //
  5. // MechCommander 2
  6. //
  7. //---------------------------------------------------------------------------//
  8. // Copyright (C) Microsoft Corporation. All rights reserved. //
  9. //===========================================================================//
  10. //---------------------------------------------------------------------------
  11. // Include Files
  12. #ifndef TERRAIN_H
  13. #include "terrain.h"
  14. #endif
  15. #ifndef VERTEX_H
  16. #include "vertex.h"
  17. #endif
  18. #ifndef CAMERA_H
  19. #include "camera.h"
  20. #endif
  21. #ifndef TERRTXM_H
  22. #include "terrtxm.h"
  23. #endif
  24. #ifndef DBASEGUI_H
  25. #include "dbasegui.h"
  26. #endif
  27. #ifndef CIDENT_H
  28. #include "cident.h"
  29. #endif
  30. #ifndef PATHS_H
  31. #include "paths.h"
  32. #endif
  33. #ifndef USERINPUT_H
  34. #include "userinput.h"
  35. #endif
  36. #ifndef TIMING_H
  37. #include "timing.h"
  38. #endif
  39. #ifndef PACKET_H
  40. #include "Packet.h"
  41. #endif
  42. #ifndef INIFILE_H
  43. #include "FitIniFile.h"
  44. #endif
  45. #ifndef TGAINFO_H
  46. #include "tgainfo.h"
  47. #endif
  48. //---------------------------------------------------------------------------
  49. // Static Globals
  50. float worldUnitsPerMeter = 5.01f;
  51. float metersPerWorldUnit = 0.2f;
  52. long terrainLineChanged = 0;
  53. MapDataPtr Terrain::mapData = NULL;
  54. TerrainTexturesPtr Terrain::terrainTextures = NULL;
  55. TerrainColorMapPtr Terrain::terrainTextures2 = NULL;
  56. const long Terrain::verticesBlockSide = 20; //Changes for new terrain?
  57. long Terrain::blocksMapSide = 0; //Calced during load.
  58. long Terrain::visibleVerticesPerSide = 0; //Passed in.
  59. const float Terrain::worldUnitsPerVertex = 128.0;
  60. const float Terrain::worldUnitsPerCell = Terrain::worldUnitsPerVertex / MAPCELL_DIM;
  61. const float Terrain::halfWorldUnitsPerCell = Terrain::worldUnitsPerCell / 2.0f;
  62. const float Terrain::metersPerCell = Terrain::worldUnitsPerCell * metersPerWorldUnit;
  63. const float Terrain::worldUnitsBlockSide = Terrain::worldUnitsPerVertex * Terrain::verticesBlockSide;
  64. const float Terrain::oneOverWorldUnitsPerVertex = 1.0f / Terrain::worldUnitsPerVertex;
  65. const float Terrain::oneOverWorldUnitsPerCell = 1.0f / Terrain::worldUnitsPerCell;
  66. const float Terrain::oneOverMetersPerCell = 1.0f / Terrain::metersPerCell;
  67. const float Terrain::oneOverVerticesBlockSide = 1.0f / Terrain::verticesBlockSide;
  68. float Terrain::worldUnitsMapSide = 0.0; //Calced during load.
  69. float Terrain::oneOverWorldUnitsMapSide = 0.0f;
  70. long Terrain::halfVerticesMapSide = 0;
  71. long Terrain::realVerticesMapSide = 0;
  72. Stuff::Vector3D Terrain::mapTopLeft3d; //Calced during load.
  73. UserHeapPtr Terrain::terrainHeap = NULL; //Setup at load time.
  74. char * Terrain::terrainName = NULL;
  75. char * Terrain::colorMapName = NULL;
  76. long Terrain::numObjBlocks = 0;
  77. ObjBlockInfo *Terrain::objBlockInfo = NULL;
  78. bool *Terrain::objVertexActive = NULL;
  79. float *Terrain::tileRowToWorldCoord = NULL;
  80. float *Terrain::tileColToWorldCoord = NULL;
  81. float *Terrain::cellToWorldCoord = NULL;
  82. float *Terrain::cellColToWorldCoord = NULL;
  83. float *Terrain::cellRowToWorldCoord = NULL;
  84. float Terrain::waterElevation = 0.0f;
  85. float Terrain::frameAngle = 0.0f;
  86. float Terrain::frameCos = 1.0f;
  87. float Terrain::frameCosAlpha = 1.0f;
  88. DWORD Terrain::alphaMiddle = 0xaf000000;
  89. DWORD Terrain::alphaEdge = 0x3f000000;
  90. DWORD Terrain::alphaDeep = 0xff000000;
  91. float Terrain::waterFreq = 4.0f;
  92. float Terrain::waterAmplitude = 10.0f;
  93. long Terrain::userMin = 0;
  94. long Terrain::userMax = 0;
  95. unsigned long Terrain::baseTerrain = 0;
  96. unsigned char Terrain::fractalThreshold = 1;
  97. unsigned char Terrain::fractalNoise = 0;
  98. bool Terrain::recalcShadows = false;
  99. bool Terrain::recalcLight = false;
  100. Clouds *Terrain::cloudLayer = NULL;
  101. bool drawTerrainGrid = false; //Override locally in editor so game don't come with these please! Love -fs
  102. bool drawLOSGrid = false;
  103. bool drawTerrainTiles = true;
  104. bool drawTerrainOverlays = true;
  105. bool drawTerrainMines = true;
  106. bool renderObjects = true;
  107. bool renderTrees = true;
  108. TerrainPtr land = NULL;
  109. long *usedBlockList; //Used to determine what objects to deal with.
  110. long *moverBlockList;
  111. unsigned long blockMemSize = 0; //Misc Flags.
  112. bool useOldProject = FALSE;
  113. bool projectAll = FALSE;
  114. bool useClouds = false;
  115. bool useFog = true;
  116. bool useVertexLighting = true;
  117. bool useFaceLighting = false;
  118. extern bool useRealLOS;
  119. unsigned char godMode = 0; //Can I simply see everything, enemy and friendly?
  120. extern long DrawDebugCells;
  121. #define MAX_TERRAIN_HEAP_SIZE 1024000
  122. long visualRangeTable[256];
  123. extern bool justResaveAllMaps;
  124. //---------------------------------------------------------------------------
  125. // These are used to determine what terrain objects to process.
  126. // They date back to GenCon 1996!!
  127. void addBlockToList (long blockNum)
  128. {
  129. long totalBlocks = Terrain::blocksMapSide * Terrain::blocksMapSide;
  130. for (long i=0;i<totalBlocks;i++)
  131. {
  132. if (usedBlockList[i] == blockNum)
  133. {
  134. return;
  135. }
  136. else if (usedBlockList[i] == -1)
  137. {
  138. usedBlockList[i] = blockNum;
  139. return;
  140. }
  141. }
  142. }
  143. //---------------------------------------------------------------------------
  144. void addMoverToList (long blockNum)
  145. {
  146. long totalBlocks = Terrain::blocksMapSide * Terrain::blocksMapSide;
  147. for (long i=0;i<totalBlocks;i++)
  148. {
  149. if (moverBlockList[i] == blockNum)
  150. {
  151. return;
  152. }
  153. else if (moverBlockList[i] == -1)
  154. {
  155. moverBlockList[i] = blockNum;
  156. return;
  157. }
  158. }
  159. }
  160. //---------------------------------------------------------------------------
  161. void clearList (void)
  162. {
  163. long totalBlocks = Terrain::blocksMapSide * Terrain::blocksMapSide;
  164. blockMemSize = totalBlocks * sizeof(long);
  165. if (usedBlockList)
  166. memset(usedBlockList,-1,blockMemSize);
  167. }
  168. //---------------------------------------------------------------------------
  169. void clearMoverList (void)
  170. {
  171. long totalBlocks = Terrain::blocksMapSide * Terrain::blocksMapSide;
  172. blockMemSize = totalBlocks * sizeof(long);
  173. if (moverBlockList)
  174. memset(moverBlockList,-1,blockMemSize);
  175. }
  176. //---------------------------------------------------------------------------
  177. //---------------------------------------------------------------------------
  178. // class Terrain
  179. void Terrain::init (void)
  180. {
  181. vertexList = NULL;
  182. numberVertices = 0;
  183. quadList = NULL;
  184. numberQuads = 0;
  185. }
  186. //---------------------------------------------------------------------------
  187. void Terrain::initMapCellArrays (void)
  188. {
  189. if (!tileRowToWorldCoord)
  190. {
  191. tileRowToWorldCoord = (float *)terrainHeap->Malloc(sizeof(float) * realVerticesMapSide);
  192. gosASSERT(tileRowToWorldCoord != NULL);
  193. }
  194. if (!tileColToWorldCoord)
  195. {
  196. tileColToWorldCoord = (float *)terrainHeap->Malloc(sizeof(float) * realVerticesMapSide);
  197. gosASSERT(tileColToWorldCoord != NULL);
  198. }
  199. if (!cellToWorldCoord)
  200. {
  201. cellToWorldCoord = (float *)terrainHeap->Malloc(sizeof(float) * MAPCELL_DIM);
  202. gosASSERT(cellToWorldCoord != NULL);
  203. }
  204. if (!cellColToWorldCoord)
  205. {
  206. cellColToWorldCoord = (float *)terrainHeap->Malloc(sizeof(float) * realVerticesMapSide * MAPCELL_DIM);
  207. gosASSERT(cellColToWorldCoord != NULL);
  208. }
  209. if (!cellRowToWorldCoord)
  210. {
  211. cellRowToWorldCoord = (float *)terrainHeap->Malloc(sizeof(float) * realVerticesMapSide * MAPCELL_DIM);
  212. gosASSERT(cellRowToWorldCoord != NULL);
  213. }
  214. long i=0;
  215. long height = realVerticesMapSide, width = height;
  216. for (i = 0; i < height; i++)
  217. tileRowToWorldCoord[i] = (worldUnitsMapSide / 2.0) - (i * worldUnitsPerVertex);
  218. for (i = 0; i < width; i++)
  219. tileColToWorldCoord[i] = (i * worldUnitsPerVertex) - (worldUnitsMapSide / 2.0);
  220. for (i = 0; i < MAPCELL_DIM; i++)
  221. cellToWorldCoord[i] = (worldUnitsPerVertex / (float)MAPCELL_DIM) * i;
  222. long maxCell = height * MAPCELL_DIM;
  223. for (i = 0; i < maxCell; i++)
  224. cellRowToWorldCoord[i] = (worldUnitsMapSide / 2.0) - (i * worldUnitsPerCell);
  225. maxCell = width * MAPCELL_DIM;
  226. for (i = 0; i < maxCell; i++)
  227. cellColToWorldCoord[i] = (i * worldUnitsPerCell) - (worldUnitsMapSide / 2.0);
  228. }
  229. //---------------------------------------------------------------------------
  230. long Terrain::init (PacketFile* pakFile, int whichPacket, unsigned long visibleVertices, volatile float& percent,
  231. float percentRange )
  232. {
  233. clearList();
  234. clearMoverList();
  235. long result = pakFile->seekPacket( whichPacket );
  236. if (result != NO_ERR)
  237. STOP(("Unable to seek Packet %d in file %s",whichPacket,pakFile->getFilename()));
  238. int tmp = pakFile->getPacketSize();
  239. realVerticesMapSide = sqrt( float(tmp/ sizeof(PostcompVertex)));
  240. if (!justResaveAllMaps &&
  241. (realVerticesMapSide != 120) &&
  242. (realVerticesMapSide != 100) &&
  243. (realVerticesMapSide != 80) &&
  244. (realVerticesMapSide != 60))
  245. {
  246. PAUSE(("This map size NO longer supported %d. Must be 120, 100, 80 or 60 now! Can Continue, for NOW!!",realVerticesMapSide));
  247. // return -1;
  248. }
  249. init( realVerticesMapSide, pakFile, visibleVertices, percent, percentRange );
  250. return(NO_ERR);
  251. }
  252. //---------------------------------------------------------------------------
  253. void Terrain::getColorMapName (FitIniFile *file)
  254. {
  255. if (file)
  256. {
  257. if (file->seekBlock("ColorMap") == NO_ERR)
  258. {
  259. char mapName[1024];
  260. if (file->readIdString("ColorMapName",mapName,1023) == NO_ERR)
  261. {
  262. colorMapName = new char[strlen(mapName)+1];
  263. strcpy(colorMapName,mapName);
  264. return;
  265. }
  266. }
  267. }
  268. colorMapName = NULL;
  269. }
  270. //---------------------------------------------------------------------------
  271. void Terrain::setColorMapName (char *mapName)
  272. {
  273. if (colorMapName)
  274. {
  275. delete [] colorMapName;
  276. colorMapName = NULL;
  277. }
  278. if (mapName)
  279. {
  280. colorMapName = new char [strlen(mapName)+1];
  281. strcpy(colorMapName,mapName);
  282. }
  283. }
  284. //---------------------------------------------------------------------------
  285. void Terrain::saveColorMapName (FitIniFile *file)
  286. {
  287. if (file && colorMapName)
  288. {
  289. file->writeBlock("ColorMap");
  290. file->writeIdString("ColorMapName",colorMapName);
  291. }
  292. }
  293. //---------------------------------------------------------------------------
  294. long Terrain::init( unsigned long verticesPerMapSide, PacketFile* pakFile, unsigned long visibleVertices,
  295. volatile float& percent,
  296. float percentRange)
  297. {
  298. //Did we pass in the hi-res colormap?
  299. // If so, convert back to old verticesPerMapSide!
  300. if (verticesPerMapSide > 300)
  301. verticesPerMapSide /= 12.8;
  302. realVerticesMapSide = verticesPerMapSide;
  303. halfVerticesMapSide = realVerticesMapSide >> 1;
  304. blocksMapSide = realVerticesMapSide / verticesBlockSide;
  305. worldUnitsMapSide = realVerticesMapSide * worldUnitsPerVertex;
  306. if (worldUnitsMapSide > Stuff::SMALL)
  307. oneOverWorldUnitsMapSide = 1.0f / worldUnitsMapSide;
  308. else
  309. oneOverWorldUnitsMapSide = 0.0f;
  310. Terrain::numObjBlocks = blocksMapSide * blocksMapSide;
  311. visibleVerticesPerSide = visibleVertices;
  312. terrainHeapSize = MAX_TERRAIN_HEAP_SIZE;
  313. //-----------------------------------------------------------------
  314. // Startup to Terrain Heap
  315. if( !terrainHeap )
  316. {
  317. terrainHeap = new UserHeap;
  318. gosASSERT(terrainHeap != NULL);
  319. terrainHeap->init(terrainHeapSize,"TERRAIN");
  320. }
  321. percent += percentRange/5.f;
  322. //-----------------------------------------------------------------
  323. // Startup the Terrain Texture Maps
  324. if ( !terrainTextures )
  325. {
  326. char baseName[256];
  327. if (pakFile)
  328. {
  329. _splitpath(pakFile->getFilename(),NULL,NULL,baseName,NULL);
  330. }
  331. else
  332. {
  333. strcpy(baseName,"newmap");
  334. }
  335. terrainTextures = new TerrainTextures;
  336. terrainTextures->init("textures",baseName);
  337. }
  338. percent += percentRange/5.f;
  339. if ( !pakFile && !realVerticesMapSide )
  340. return NO_ERR;
  341. //-----------------------------------------------------------------
  342. // Startup the Terrain Color Map
  343. if ( !terrainTextures2 && pakFile)
  344. {
  345. char name[1024];
  346. _splitpath(pakFile->getFilename(),NULL,NULL,name,NULL);
  347. terrainName = new char[strlen(name)+1];
  348. strcpy(terrainName,name);
  349. if (colorMapName)
  350. strcpy(name,colorMapName);
  351. FullPathFileName tgaColorMapName;
  352. tgaColorMapName.init(texturePath,name,".tga");
  353. FullPathFileName tgaColorMapBurninName;
  354. tgaColorMapBurninName.init(texturePath,name,".burnin.tga");
  355. FullPathFileName tgaColorMapJPGName;
  356. tgaColorMapJPGName.init(texturePath,name,".burnin.jpg");
  357. if (fileExists(tgaColorMapName) || fileExists(tgaColorMapBurninName) || fileExists(tgaColorMapJPGName))
  358. {
  359. terrainTextures2 = new TerrainColorMap; //Otherwise, this will stay NULL and we know not to use them
  360. }
  361. }
  362. percent += percentRange/5.f;
  363. mapTopLeft3d.x = -worldUnitsMapSide / 2.0f;
  364. mapTopLeft3d.y = worldUnitsMapSide / 2.0f;
  365. percent += percentRange/5.f;
  366. //----------------------------------------------------------------------
  367. // Setup number of blocks
  368. long numberBlocks = blocksMapSide * blocksMapSide;
  369. numObjBlocks = numberBlocks;
  370. objBlockInfo = (ObjBlockInfo *)terrainHeap->Malloc(sizeof(ObjBlockInfo)*numObjBlocks);
  371. gosASSERT(objBlockInfo != NULL);
  372. memset(objBlockInfo,0,sizeof(ObjBlockInfo)*numObjBlocks);
  373. objVertexActive = (bool *)terrainHeap->Malloc(sizeof(bool) * realVerticesMapSide * realVerticesMapSide);
  374. gosASSERT(objVertexActive != NULL);
  375. memset(objVertexActive,0,sizeof(bool)*numObjBlocks);
  376. moverBlockList = (long *)terrainHeap->Malloc(sizeof(long) * numberBlocks);
  377. gosASSERT(moverBlockList != NULL);
  378. usedBlockList = (long *)terrainHeap->Malloc(sizeof(long) * numberBlocks);
  379. gosASSERT(usedBlockList != NULL);
  380. clearList();
  381. clearMoverList();
  382. //----------------------------------------------------------------------
  383. // Calculate size of each mapblock
  384. long blockSize = verticesBlockSide * verticesBlockSide;
  385. blockSize *= sizeof(PostcompVertex);
  386. //----------------------------------------------------------------------
  387. // Create the MapBlock Manager and allocate its RAM
  388. if ( !mapData )
  389. {
  390. mapData = new MapData;
  391. if ( pakFile )
  392. mapData->newInit( pakFile, realVerticesMapSide*realVerticesMapSide);
  393. else
  394. mapData->newInit( realVerticesMapSide*realVerticesMapSide );
  395. mapTopLeft3d.z = mapData->getTopLeftElevation();
  396. }
  397. percent += percentRange/5.f;
  398. //----------------------------------------------------------------------
  399. // Create the VertexList
  400. numberVertices = 0;
  401. vertexList = (VertexPtr)terrainHeap->Malloc(sizeof(Vertex) * visibleVertices * visibleVertices);
  402. gosASSERT(vertexList != NULL);
  403. memset(vertexList,0,sizeof(Vertex) * visibleVertices * visibleVertices);
  404. //----------------------------------------------------------------------
  405. // Create the QuadList
  406. numberQuads = 0;
  407. quadList = (TerrainQuadPtr)terrainHeap->Malloc(sizeof(TerrainQuad) * visibleVertices * visibleVertices);
  408. gosASSERT(quadList != NULL);
  409. memset(quadList,0,sizeof(TerrainQuad) * visibleVertices * visibleVertices);
  410. //-------------------------------------------------------------------
  411. initMapCellArrays();
  412. //-----------------------------------------------------------------
  413. // Startup the Terrain Color Map
  414. if ( terrainTextures2 && !(terrainTextures2->colorMapStarted))
  415. {
  416. if (colorMapName)
  417. terrainTextures2->init(colorMapName);
  418. else
  419. terrainTextures2->init(terrainName);
  420. }
  421. return NO_ERR;
  422. }
  423. void Terrain::resetVisibleVertices (long maxVisibleVertices)
  424. {
  425. terrainHeap->Free(vertexList);
  426. vertexList = NULL;
  427. terrainHeap->Free(quadList);
  428. quadList = NULL;
  429. visibleVerticesPerSide = maxVisibleVertices;
  430. //----------------------------------------------------------------------
  431. // Create the VertexList
  432. numberVertices = 0;
  433. vertexList = (VertexPtr)terrainHeap->Malloc(sizeof(Vertex) * visibleVerticesPerSide * visibleVerticesPerSide);
  434. gosASSERT(vertexList != NULL);
  435. memset(vertexList,0,sizeof(Vertex) * visibleVerticesPerSide * visibleVerticesPerSide);
  436. //----------------------------------------------------------------------
  437. // Create the QuadList
  438. numberQuads = 0;
  439. quadList = (TerrainQuadPtr)terrainHeap->Malloc(sizeof(TerrainQuad) * visibleVerticesPerSide * visibleVerticesPerSide);
  440. gosASSERT(quadList != NULL);
  441. memset(quadList,0,sizeof(TerrainQuad) * visibleVerticesPerSide * visibleVerticesPerSide);
  442. }
  443. //---------------------------------------------------------------------------
  444. bool Terrain::IsValidTerrainPosition (Stuff::Vector3D pos)
  445. {
  446. float metersCheck = (Terrain::worldUnitsMapSide / 2.0f);
  447. if ((pos.x > -metersCheck) &&
  448. (pos.x < metersCheck) &&
  449. (pos.y > -metersCheck) &&
  450. (pos.y < metersCheck))
  451. {
  452. return true;
  453. }
  454. return false;
  455. }
  456. //---------------------------------------------------------------------------
  457. bool Terrain::IsEditorSelectTerrainPosition (Stuff::Vector3D pos)
  458. {
  459. float metersCheck = (Terrain::worldUnitsMapSide / 2.0f) - Terrain::worldUnitsPerVertex;
  460. if ((pos.x > -metersCheck) &&
  461. (pos.x < metersCheck) &&
  462. (pos.y > -metersCheck) &&
  463. (pos.y < metersCheck))
  464. {
  465. return true;
  466. }
  467. return false;
  468. }
  469. //---------------------------------------------------------------------------
  470. bool Terrain::IsGameSelectTerrainPosition (Stuff::Vector3D pos)
  471. {
  472. float metersCheck = (Terrain::worldUnitsMapSide / 2.0f) - (Terrain::worldUnitsPerVertex * 2.0f);
  473. if ((pos.x > -metersCheck) &&
  474. (pos.x < metersCheck) &&
  475. (pos.y > -metersCheck) &&
  476. (pos.y < metersCheck))
  477. {
  478. return true;
  479. }
  480. return false;
  481. }
  482. //---------------------------------------------------------------------------
  483. void Terrain::purgeTransitions (void)
  484. {
  485. terrainTextures->purgeTransitions();
  486. mapData->calcTransitions();
  487. }
  488. //---------------------------------------------------------------------------
  489. void Terrain::destroy (void)
  490. {
  491. if (terrainTextures)
  492. {
  493. terrainTextures->destroy();
  494. delete terrainTextures;
  495. terrainTextures = NULL;
  496. }
  497. if (terrainTextures2)
  498. {
  499. terrainTextures2->destroy();
  500. delete terrainTextures2;
  501. terrainTextures2 = NULL;
  502. }
  503. delete mapData;
  504. mapData = NULL;
  505. if (terrainName)
  506. {
  507. delete [] terrainName;
  508. terrainName = NULL;
  509. }
  510. if (colorMapName)
  511. {
  512. delete [] colorMapName;
  513. colorMapName = NULL;
  514. }
  515. if (tileRowToWorldCoord)
  516. {
  517. terrainHeap->Free(tileRowToWorldCoord);
  518. tileRowToWorldCoord = NULL;
  519. }
  520. if (tileColToWorldCoord)
  521. {
  522. terrainHeap->Free(tileColToWorldCoord);
  523. tileColToWorldCoord = NULL;
  524. }
  525. if (cellToWorldCoord)
  526. {
  527. terrainHeap->Free(cellToWorldCoord);
  528. cellToWorldCoord = NULL;
  529. }
  530. if (cellColToWorldCoord)
  531. {
  532. terrainHeap->Free(cellColToWorldCoord);
  533. cellColToWorldCoord = NULL;
  534. }
  535. if (cellRowToWorldCoord)
  536. {
  537. terrainHeap->Free(cellRowToWorldCoord);
  538. cellRowToWorldCoord = NULL;
  539. }
  540. if (moverBlockList)
  541. {
  542. terrainHeap->Free(moverBlockList);
  543. moverBlockList = NULL;
  544. }
  545. if (usedBlockList)
  546. {
  547. terrainHeap->Free(usedBlockList);
  548. usedBlockList = NULL;
  549. }
  550. if (vertexList)
  551. {
  552. terrainHeap->Free(vertexList);
  553. vertexList = NULL;
  554. }
  555. if (quadList)
  556. {
  557. terrainHeap->Free(quadList);
  558. quadList = NULL;
  559. }
  560. if (objBlockInfo)
  561. {
  562. terrainHeap->Free(objBlockInfo);
  563. objBlockInfo = NULL;
  564. }
  565. if (objVertexActive)
  566. {
  567. terrainHeap->Free(objVertexActive);
  568. objVertexActive = NULL;
  569. }
  570. if (terrainHeap)
  571. {
  572. terrainHeap->destroy();
  573. delete terrainHeap;
  574. terrainHeap = NULL;
  575. }
  576. numberVertices =
  577. numberQuads =
  578. halfVerticesMapSide =
  579. realVerticesMapSide =
  580. visibleVerticesPerSide =
  581. blocksMapSide = 0;
  582. worldUnitsMapSide = 0.0f;
  583. mapTopLeft3d.Zero();
  584. numObjBlocks = 0;
  585. recalcShadows =
  586. recalcLight = false;
  587. //Reset these. This will fix the mine problem.
  588. TerrainQuad::rainLightLevel = 1.0f;
  589. TerrainQuad::lighteningLevel = 0;
  590. TerrainQuad::mineTextureHandle = 0xffffffff;
  591. TerrainQuad::blownTextureHandle = 0xffffffff;
  592. }
  593. extern float textureOffset;
  594. //---------------------------------------------------------------------------
  595. long Terrain::update (void)
  596. {
  597. //-----------------------------------------------------------------
  598. // Startup the Terrain Color Map
  599. if ( terrainTextures2 && !(terrainTextures2->colorMapStarted))
  600. {
  601. if (colorMapName)
  602. terrainTextures2->init(colorMapName);
  603. else
  604. terrainTextures2->init(terrainName);
  605. }
  606. //----------------------------------------------------------------
  607. // Nothing is ever visible. We recalc every frame. True LOS!
  608. // Terrain::VisibleBits->resetAll(0);
  609. if (godMode)
  610. {
  611. // Terrain::VisibleBits->resetAll(0xff);
  612. }
  613. if (turn > terrainLineChanged+10)
  614. {
  615. if (userInput->getKeyDown(KEY_UP) && userInput->ctrl() && userInput->alt() && !userInput->shift())
  616. {
  617. textureOffset += 0.1f;;
  618. terrainLineChanged = turn;
  619. }
  620. if (userInput->getKeyDown(KEY_DOWN) && userInput->ctrl() && userInput->alt() && !userInput->shift())
  621. {
  622. textureOffset -= 0.1f;;
  623. terrainLineChanged = turn;
  624. }
  625. }
  626. //---------------------------------------------------------------------
  627. Terrain::mapData->update();
  628. Terrain::mapData->makeLists(vertexList,numberVertices,quadList,numberQuads);
  629. return TRUE;
  630. }
  631. //---------------------------------------------------------------------------
  632. void Terrain::setOverlayTile (long block, long vertex, long offset)
  633. {
  634. mapData->setOverlayTile(block,vertex,offset);
  635. }
  636. //---------------------------------------------------------------------------
  637. void Terrain::setOverlay( long tileR, long tileC, Overlays type, unsigned long offset )
  638. {
  639. mapData->setOverlay( tileR, tileC, type, offset );
  640. }
  641. //---------------------------------------------------------------------------
  642. void Terrain::setTerrain( long tileR, long tileC, int terrainType )
  643. {
  644. mapData->setTerrain( tileR, tileC, terrainType );
  645. }
  646. //---------------------------------------------------------------------------
  647. int Terrain::getTerrain( long tileR, long tileC )
  648. {
  649. return mapData->getTerrain( tileR, tileC );
  650. }
  651. //---------------------------------------------------------------------------
  652. void Terrain::calcWater (float waterDepth, float waterShallowDepth, float waterAlphaDepth)
  653. {
  654. mapData->calcWater(waterDepth, waterShallowDepth, waterAlphaDepth);
  655. }
  656. //---------------------------------------------------------------------------
  657. long Terrain::getOverlayTile (long block, long vertex)
  658. {
  659. return (mapData->getOverlayTile(block,vertex));
  660. }
  661. //---------------------------------------------------------------------------
  662. void Terrain::getOverlay( long tileR, long tileC, enum Overlays& type, unsigned long& Offset )
  663. {
  664. mapData->getOverlay( tileR, tileC, type, Offset );
  665. }
  666. //---------------------------------------------------------------------------
  667. void Terrain::setVertexHeight( int VertexIndex, float Val )
  668. {
  669. if ( VertexIndex > -1 && VertexIndex < realVerticesMapSide * realVerticesMapSide )
  670. mapData->setVertexHeight( VertexIndex, Val );
  671. }
  672. //---------------------------------------------------------------------------
  673. float Terrain::getVertexHeight( int VertexIndex )
  674. {
  675. if ( VertexIndex > -1 && VertexIndex < realVerticesMapSide * realVerticesMapSide )
  676. return mapData->getVertexHeight(VertexIndex);
  677. return -1.f;
  678. }
  679. //---------------------------------------------------------------------------
  680. void Terrain::render (void)
  681. {
  682. //-----------------------------------
  683. // render the cloud layer
  684. if (Terrain::cloudLayer)
  685. Terrain::cloudLayer->render();
  686. //-----------------------------------
  687. // Draw resulting terrain quads
  688. TerrainQuadPtr currentQuad = quadList;
  689. DWORD fogColor = eye->fogColor;
  690. for (long i=0;i<numberQuads;i++)
  691. {
  692. if (drawTerrainTiles)
  693. currentQuad->draw();
  694. if (drawTerrainTiles)
  695. currentQuad->drawMine();
  696. //--------------------------
  697. // Used to debug stuff
  698. if (drawTerrainGrid)
  699. {
  700. if (useFog)
  701. gos_SetRenderState( gos_State_Fog, 0);
  702. currentQuad->drawLine();
  703. if (useFog)
  704. gos_SetRenderState( gos_State_Fog, (int)&fogColor);
  705. }
  706. else if (DrawDebugCells)
  707. {
  708. if (useFog)
  709. gos_SetRenderState( gos_State_Fog, 0);
  710. currentQuad->drawDebugCellLine();
  711. if (useFog)
  712. gos_SetRenderState( gos_State_Fog, (int)&fogColor);
  713. }
  714. else if (drawLOSGrid)
  715. {
  716. if (useFog)
  717. gos_SetRenderState( gos_State_Fog, 0);
  718. currentQuad->drawLOSLine();
  719. if (useFog)
  720. gos_SetRenderState( gos_State_Fog, (int)&fogColor);
  721. }
  722. currentQuad++;
  723. }
  724. }
  725. //---------------------------------------------------------------------------
  726. void Terrain::renderWater (void)
  727. {
  728. //-----------------------------------
  729. // Draw resulting terrain quads
  730. TerrainQuadPtr currentQuad = quadList;
  731. for (long i=0;i<numberQuads;i++)
  732. {
  733. if (drawTerrainTiles)
  734. currentQuad->drawWater();
  735. currentQuad++;
  736. }
  737. }
  738. float cosineEyeHalfFOV = 0.0f;
  739. #define MAX_CAMERA_RADIUS (250.0f)
  740. #define CLIP_THRESHOLD_DISTANCE (768.0f)
  741. //a full triangle.
  742. #define VERTEX_EXTENT_RADIUS (384.0f)
  743. float leastZ = 1.0f,leastW = 1.0f;
  744. float mostZ = -1.0f, mostW = -1.0;
  745. float leastWY = 0.0f, mostWY = 0.0f;
  746. extern bool InEditor;
  747. //---------------------------------------------------------------------------
  748. void Terrain::geometry (void)
  749. {
  750. //---------------------------------------------------------------------
  751. leastZ = 1.0f;leastW = 1.0f;
  752. mostZ = -1.0f; mostW = -1.0;
  753. leastWY = 0.0f; mostWY = 0.0f;
  754. //-----------------------------------
  755. // Transform entire list of vertices
  756. VertexPtr currentVertex = vertexList;
  757. Stuff::Vector3D cameraPos;
  758. cameraPos.x = -eye->getCameraOrigin().x;
  759. cameraPos.y = eye->getCameraOrigin().z;
  760. cameraPos.z = eye->getCameraOrigin().y;
  761. float vClipConstant = eye->verticalSphereClipConstant;
  762. float hClipConstant = eye->horizontalSphereClipConstant;
  763. long i=0;
  764. for (i=0;i<numberVertices;i++)
  765. {
  766. //----------------------------------------------------------------------------------------
  767. // Figure out if we are in front of camera or not. Should be faster then actual project!
  768. // Should weed out VAST overwhelming majority of vertices!
  769. bool onScreen = false;
  770. //-----------------------------------------------------------------
  771. // Find angle between lookVector of Camera and vector from camPos
  772. // to Target. If angle is less then halfFOV, object is visible.
  773. if (eye->usePerspective)
  774. {
  775. //-------------------------------------------------------------------
  776. //NEW METHOD from the WAY BACK Days
  777. onScreen = true;
  778. Stuff::Vector3D vPosition;
  779. vPosition.x = currentVertex->vx;
  780. vPosition.y = currentVertex->vy;
  781. vPosition.z = currentVertex->pVertex->elevation;
  782. Stuff::Vector3D objectCenter;
  783. objectCenter.Subtract(vPosition,cameraPos);
  784. Camera::cameraFrame.trans_to_frame(objectCenter);
  785. float distanceToEye = objectCenter.GetApproximateLength();
  786. Stuff::Vector3D clipVector = objectCenter;
  787. clipVector.z = 0.0f;
  788. float distanceToClip = clipVector.GetApproximateLength();
  789. float clip_distance = fabs(1.0f / objectCenter.y);
  790. if (distanceToClip > CLIP_THRESHOLD_DISTANCE)
  791. {
  792. //Is vertex on Screen OR close enough to screen that its triangle MAY be visible?
  793. // WE have removed the atans here by simply taking the tan of the angle we want above.
  794. float object_angle = fabs(objectCenter.z) * clip_distance;
  795. float extent_angle = VERTEX_EXTENT_RADIUS / distanceToEye;
  796. if (object_angle > (vClipConstant + extent_angle))
  797. {
  798. //In theory, we would return here. Object is NOT on screen.
  799. onScreen = false;
  800. }
  801. else
  802. {
  803. object_angle = fabs(objectCenter.x) * clip_distance;
  804. if (object_angle > (hClipConstant + extent_angle))
  805. {
  806. //In theory, we would return here. Object is NOT on screen.
  807. onScreen = false;
  808. }
  809. }
  810. }
  811. if (onScreen)
  812. {
  813. if (distanceToEye > Camera::MaxClipDistance)
  814. {
  815. currentVertex->hazeFactor = 1.0f;
  816. }
  817. else if (distanceToEye > Camera::MinHazeDistance)
  818. {
  819. currentVertex->hazeFactor = (distanceToEye - Camera::MinHazeDistance) * Camera::DistanceFactor;
  820. }
  821. else
  822. {
  823. currentVertex->hazeFactor = 0.0f;
  824. }
  825. //---------------------------------------
  826. // Vertex is at edge of world or beyond.
  827. Stuff::Vector3D vPos(currentVertex->vx,currentVertex->vy,currentVertex->pVertex->elevation);
  828. bool isVisible = Terrain::IsGameSelectTerrainPosition(vPos) || drawTerrainGrid;
  829. if (!isVisible)
  830. {
  831. currentVertex->hazeFactor = 1.0f;
  832. onScreen = true;
  833. }
  834. }
  835. else
  836. {
  837. currentVertex->hazeFactor = 1.0f;
  838. }
  839. }
  840. else
  841. {
  842. currentVertex->hazeFactor = 0.0f;
  843. onScreen = true;
  844. }
  845. bool inView = false;
  846. Stuff::Vector4D screenPos(-10000.0f,-10000.0f,-10000.0f,-10000.0f);
  847. if (onScreen)
  848. {
  849. Stuff::Vector3D vertex3D(currentVertex->vx,currentVertex->vy,currentVertex->pVertex->elevation);
  850. inView = eye->projectZ(vertex3D,screenPos);
  851. currentVertex->px = screenPos.x;
  852. currentVertex->py = screenPos.y;
  853. currentVertex->pz = screenPos.z;
  854. currentVertex->pw = screenPos.w;
  855. //----------------------------------------------------------------------------------
  856. //We must transform these but should NOT draw any face where all three are fogged.
  857. // if (currentVertex->hazeFactor == 1.0f)
  858. // onScreen = false;
  859. }
  860. else
  861. {
  862. currentVertex->px = currentVertex->py = 10000.0f;
  863. currentVertex->pz = -0.5f;
  864. currentVertex->pw = 0.5f;
  865. currentVertex->hazeFactor = 0.0f;
  866. }
  867. //------------------------------------------------------------
  868. // Fix clip. Vertices can all be off screen and triangle
  869. // still needs to be drawn!
  870. if (eye->usePerspective && Environment.Renderer != 3)
  871. {
  872. currentVertex->clipInfo = onScreen;
  873. }
  874. else
  875. currentVertex->clipInfo = inView;
  876. if (currentVertex->clipInfo) //ONLY set TRUE ones. Otherwise we just reset the FLAG each vertex!
  877. {
  878. setObjBlockActive(currentVertex->getBlockNumber(), true);
  879. setObjVertexActive(currentVertex->vertexNum,true);
  880. if (inView)
  881. {
  882. if (screenPos.z < leastZ)
  883. {
  884. leastZ = screenPos.z;
  885. }
  886. if (screenPos.z > mostZ)
  887. {
  888. mostZ = screenPos.z;
  889. }
  890. if (screenPos.w < leastW)
  891. {
  892. leastW = screenPos.w;
  893. leastWY = screenPos.y;
  894. }
  895. if (screenPos.w > mostW)
  896. {
  897. mostW = screenPos.w;
  898. mostWY = screenPos.y;
  899. }
  900. }
  901. }
  902. currentVertex++;
  903. }
  904. //-----------------------------------
  905. // setup terrain quad textures
  906. // Also sets up mine data.
  907. TerrainQuadPtr currentQuad = quadList;
  908. for (i=0;i<numberQuads;i++)
  909. {
  910. currentQuad->setupTextures();
  911. currentQuad++;
  912. }
  913. float ywRange = 0.0f, yzRange = 0.0f;
  914. if (fabs(mostWY - leastWY) > Stuff::SMALL)
  915. {
  916. ywRange = (mostW - leastW) / (mostWY - leastWY);
  917. yzRange = (mostZ - leastZ) / (mostWY - leastWY);
  918. }
  919. eye->setInverseProject(mostZ,leastW,yzRange,ywRange);
  920. //-----------------------------------
  921. // update the cloud layer
  922. if (Terrain::cloudLayer)
  923. Terrain::cloudLayer->update();
  924. }
  925. //---------------------------------------------------------------------------
  926. float Terrain::getTerrainElevation (Stuff::Vector3D &position)
  927. {
  928. float result = mapData->terrainElevation(position);
  929. return(result);
  930. }
  931. //---------------------------------------------------------------------------
  932. float Terrain::getTerrainElevation( long tileR, long tileC )
  933. {
  934. return mapData->terrainElevation( tileR, tileC );
  935. }
  936. //---------------------------------------------------------------------------
  937. unsigned long Terrain::getTexture( long tileR, long tileC )
  938. {
  939. return mapData->getTexture( tileR, tileC );
  940. }
  941. //---------------------------------------------------------------------------
  942. float Terrain::getTerrainAngle (Stuff::Vector3D &position, Stuff::Vector3D* normal)
  943. {
  944. float result = mapData->terrainAngle(position, normal);
  945. return(result);
  946. }
  947. //---------------------------------------------------------------------------
  948. float Terrain::getTerrainLight (Stuff::Vector3D &position)
  949. {
  950. float result = mapData->terrainLight(position);
  951. return(result);
  952. }
  953. //---------------------------------------------------------------------------
  954. Stuff::Vector3D Terrain::getTerrainNormal (Stuff::Vector3D &position)
  955. {
  956. Stuff::Vector3D result = Terrain::mapData->terrainNormal(position);
  957. return(result);
  958. }
  959. //---------------------------------------------------------------------------
  960. // Uses a simple value to mark radius. It never changes now!!
  961. // First value in range table!!
  962. void Terrain::markSeen (Stuff::Vector3D &looker, byte who, float specialUnitExpand)
  963. {
  964. return;
  965. /* Not needed anymore. Real LOS now.
  966. //-----------------------------------------------------------
  967. // This function marks vertices has being seen by a given side.
  968. Stuff::Vector3D position = looker;
  969. position.x -= mapTopLeft3d.x;
  970. position.y = mapTopLeft3d.y - looker.y;
  971. Stuff::Vector2DOf<float> upperLeft;
  972. upperLeft.x = floor(position.x * oneOverWorldUnitsPerVertex);
  973. upperLeft.y = floor(position.y * oneOverWorldUnitsPerVertex);
  974. Stuff::Vector2DOf<long> meshOffset;
  975. meshOffset.x = float2long(upperLeft.x);
  976. meshOffset.y = float2long(upperLeft.y);
  977. unsigned long xCenter = meshOffset.x;
  978. unsigned long yCenter = meshOffset.y;
  979. //Figure out altitude above minimum terrain altitude and look up in table.
  980. float baseElevation = MapData::waterDepth;
  981. if (MapData::waterDepth < Terrain::userMin)
  982. baseElevation = Terrain::userMin;
  983. float altitude = position.z - baseElevation;
  984. float altitudeIntegerRange = (Terrain::userMax - baseElevation) * 0.00390625f;
  985. long altLevel = 0;
  986. if (altitudeIntegerRange > Stuff::SMALL)
  987. altLevel = altitude / altitudeIntegerRange;
  988. if (altLevel < 0)
  989. altLevel = 0;
  990. if (altLevel > 255)
  991. altLevel = 255;
  992. float radius = visualRangeTable[altLevel];
  993. radius += (radius * specialUnitExpand);
  994. if (radius <= 0.0f)
  995. return;
  996. //-----------------------------------------------------
  997. // Who is the shift value to create the mask
  998. BYTE wer = (1 << who);
  999. VisibleBits->setCircle(xCenter,yCenter,float2long(radius),wer);
  1000. */
  1001. }
  1002. //---------------------------------------------------------------------------
  1003. // Uses dist passed in as radius.
  1004. void Terrain::markRadiusSeen (Stuff::Vector3D &looker, float dist, byte who)
  1005. {
  1006. return;
  1007. //Not needed. Real LOS now!
  1008. /*
  1009. if (dist <= 0.0f)
  1010. return;
  1011. //-----------------------------------------------------------
  1012. // This function marks vertices has being seen by
  1013. // a given side.
  1014. dist *= worldUnitsPerMeter;
  1015. dist *= Terrain::oneOverWorldUnitsPerVertex;
  1016. Stuff::Vector3D position = looker;
  1017. position.x -= mapTopLeft3d.x;
  1018. position.y = mapTopLeft3d.y - looker.y;
  1019. Stuff::Vector2DOf<float> upperLeft;
  1020. upperLeft.x = floor(position.x * oneOverWorldUnitsPerVertex);
  1021. upperLeft.y = floor(position.y * oneOverWorldUnitsPerVertex);
  1022. Stuff::Vector2DOf<long> meshOffset;
  1023. meshOffset.x = floor(upperLeft.x);
  1024. meshOffset.y = floor(upperLeft.y);
  1025. unsigned long xCenter = meshOffset.x;
  1026. unsigned long yCenter = meshOffset.y;
  1027. //-----------------------------------------------------
  1028. // Who is the shift value to create the mask
  1029. BYTE wer = (1 << who);
  1030. VisibleBits->setCircle(xCenter,yCenter,dist,wer);
  1031. */
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. void Terrain::setObjBlockActive (long blockNum, bool active)
  1035. {
  1036. if ((blockNum >= 0) && (blockNum < numObjBlocks))
  1037. objBlockInfo[blockNum].active = active;
  1038. }
  1039. //---------------------------------------------------------------------------
  1040. void Terrain::clearObjBlocksActive (void)
  1041. {
  1042. for (long i = 0; i < numObjBlocks; i++)
  1043. setObjBlockActive(i, false);
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. void Terrain::setObjVertexActive (long vertexNum, bool active)
  1047. {
  1048. if ( (vertexNum >= 0) && (vertexNum < (realVerticesMapSide * realVerticesMapSide)) )
  1049. objVertexActive[vertexNum] = active;
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. void Terrain::clearObjVerticesActive (void)
  1053. {
  1054. memset(objVertexActive,0,sizeof(bool) * realVerticesMapSide * realVerticesMapSide);
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. long Terrain::save( PacketFile* fileName, int whichPacket, bool quickSave )
  1058. {
  1059. if (!quickSave)
  1060. {
  1061. recalcShadows = true;
  1062. mapData->calcLight();
  1063. }
  1064. else
  1065. {
  1066. recalcShadows = false;
  1067. }
  1068. return mapData->save( fileName, whichPacket );
  1069. }
  1070. //-----------------------------------------------------
  1071. bool Terrain::save( FitIniFile* fitFile )
  1072. {
  1073. // write out the water info
  1074. #ifdef _DEBUG
  1075. long result =
  1076. #endif
  1077. fitFile->writeBlock( "Water" );
  1078. gosASSERT( result > 0 );
  1079. fitFile->writeIdFloat( "Elevation", mapData->waterDepth );
  1080. fitFile->writeIdFloat( "Frequency", waterFreq );
  1081. fitFile->writeIdFloat( "Ampliture", waterAmplitude );
  1082. fitFile->writeIdULong( "AlphaShallow", alphaEdge );
  1083. fitFile->writeIdULong( "AlphaMiddle", alphaMiddle );
  1084. fitFile->writeIdULong( "AlphaDeep", alphaDeep );
  1085. fitFile->writeIdFloat( "AlphaDepth", mapData->alphaDepth );
  1086. fitFile->writeIdFloat( "ShallowDepth", mapData->shallowDepth );
  1087. fitFile->writeBlock( "Terrain" );
  1088. fitFile->writeIdLong( "UserMin", userMin );
  1089. fitFile->writeIdLong( "UserMax", userMax );
  1090. fitFile->writeIdFloat( "TerrainMinX", tileColToWorldCoord[0] );
  1091. fitFile->writeIdFloat( "TerrainMinY", tileRowToWorldCoord[0] );
  1092. fitFile->writeIdUChar( "Noise", fractalNoise);
  1093. fitFile->writeIdUChar( "Threshold", fractalThreshold);
  1094. if (terrainTextures2)
  1095. {
  1096. terrainTextures2->saveTilingFactors(fitFile);
  1097. }
  1098. return true;
  1099. }
  1100. bool Terrain::load( FitIniFile* fitFile )
  1101. {
  1102. // write out the water info
  1103. long result = fitFile->seekBlock( "Water" );
  1104. gosASSERT( result == NO_ERR );
  1105. result = fitFile->readIdFloat( "Elevation", mapData->waterDepth );
  1106. gosASSERT( result == NO_ERR );
  1107. waterElevation = mapData->waterDepth;
  1108. result = fitFile->readIdFloat( "Frequency", waterFreq );
  1109. gosASSERT( result == NO_ERR );
  1110. result = fitFile->readIdFloat( "Ampliture", waterAmplitude );
  1111. gosASSERT( result == NO_ERR );
  1112. result = fitFile->readIdULong( "AlphaShallow", alphaEdge );
  1113. gosASSERT( result == NO_ERR );
  1114. result = fitFile->readIdULong( "AlphaMiddle", alphaMiddle );
  1115. gosASSERT( result == NO_ERR );
  1116. result = fitFile->readIdULong( "AlphaDeep", alphaDeep );
  1117. gosASSERT( result == NO_ERR );
  1118. result = fitFile->readIdFloat( "AlphaDepth", mapData->alphaDepth );
  1119. gosASSERT( result == NO_ERR );
  1120. result = fitFile->readIdFloat( "ShallowDepth", mapData->shallowDepth );
  1121. gosASSERT( result == NO_ERR );
  1122. fitFile->seekBlock( "Terrain" );
  1123. fitFile->readIdLong( "UserMin", userMin );
  1124. fitFile->readIdLong( "UserMax", userMax );
  1125. fitFile->readIdUChar( "Noise", fractalNoise);
  1126. fitFile->readIdUChar( "Threshold", fractalThreshold);
  1127. return true;
  1128. }
  1129. //---------------------------------------------------------------------------
  1130. void Terrain::unselectAll()
  1131. {
  1132. mapData->unselectAll();
  1133. }
  1134. //---------------------------------------------------------------------------
  1135. void Terrain::selectVerticesInRect( const Stuff::Vector4D& topLeft, const Stuff::Vector4D& bottomRight, bool bToggle )
  1136. {
  1137. Stuff::Vector3D worldPos;
  1138. Stuff::Vector4D screenPos;
  1139. int xMin, xMax;
  1140. int yMin, yMax;
  1141. if ( topLeft.x < bottomRight.x )
  1142. {
  1143. xMin = topLeft.x;
  1144. xMax = bottomRight.x;
  1145. }
  1146. else
  1147. {
  1148. xMin = bottomRight.x;
  1149. xMax = topLeft.x;
  1150. }
  1151. if ( topLeft.y < bottomRight.y )
  1152. {
  1153. yMin = topLeft.y;
  1154. yMax = bottomRight.y;
  1155. }
  1156. else
  1157. {
  1158. yMin = bottomRight.y;
  1159. yMax = topLeft.y;
  1160. }
  1161. for ( int i = 0; i < realVerticesMapSide; ++i )
  1162. {
  1163. for ( int j = 0; j < realVerticesMapSide; ++j )
  1164. {
  1165. worldPos.y = tileRowToWorldCoord[j];
  1166. worldPos.x = tileColToWorldCoord[i];
  1167. worldPos.z = mapData->terrainElevation( j, i );
  1168. eye->projectZ( worldPos, screenPos );
  1169. if ( screenPos.x >= xMin && screenPos.x <= xMax &&
  1170. screenPos.y >= yMin && screenPos.y <= yMax )
  1171. {
  1172. mapData->selectVertex( j, i, true, bToggle );
  1173. }
  1174. }
  1175. }
  1176. }
  1177. //---------------------------------------------------------------------------
  1178. bool Terrain::hasSelection()
  1179. {
  1180. return mapData->selection();
  1181. }
  1182. //---------------------------------------------------------------------------
  1183. bool Terrain::isVertexSelected( long tileR, long tileC )
  1184. {
  1185. return mapData->isVertexSelected( tileR, tileC );
  1186. }
  1187. //---------------------------------------------------------------------------
  1188. bool Terrain::selectVertex( long tileR, long tileC, bool bSelect )
  1189. {
  1190. //We never use the return value so just send back false.
  1191. if ( (tileR <= -1) || (tileR >= realVerticesMapSide) )
  1192. return false;
  1193. if ( (tileC <= -1) || (tileC >= realVerticesMapSide) )
  1194. return false;
  1195. mapData->selectVertex( tileR, tileC, bSelect, 0 );
  1196. return true;
  1197. }
  1198. //---------------------------------------------------------------------------
  1199. float Terrain::getHighestVertex( long& tileR, long& tileC )
  1200. {
  1201. float highest = -9999999.; // an absurdly small number
  1202. for ( int i = 0; i < realVerticesMapSide * realVerticesMapSide; ++i )
  1203. {
  1204. float tmp = getVertexHeight( i );
  1205. if ( tmp > highest )
  1206. {
  1207. highest = tmp;
  1208. tileR = i/realVerticesMapSide;
  1209. tileC = i % realVerticesMapSide;
  1210. }
  1211. }
  1212. return highest;
  1213. }
  1214. //---------------------------------------------------------------------------
  1215. float Terrain::getLowestVertex( long& tileR, long& tileC )
  1216. {
  1217. float lowest = 9999999.; // an absurdly big number
  1218. for ( int i = 0; i < realVerticesMapSide * realVerticesMapSide; ++i )
  1219. {
  1220. float tmp = getVertexHeight( i );
  1221. if ( tmp < lowest )
  1222. {
  1223. lowest = tmp;
  1224. tileR = i/realVerticesMapSide;
  1225. tileC = i % realVerticesMapSide;
  1226. }
  1227. }
  1228. return lowest;
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. void Terrain::setUserSettings( long min, long max, int terrainType )
  1232. {
  1233. userMin = min;
  1234. userMax = max;
  1235. baseTerrain = terrainType;
  1236. }
  1237. //---------------------------------------------------------------------------
  1238. void Terrain::getUserSettings( long& min, long& max, int& terrainType )
  1239. {
  1240. min = userMin;
  1241. max = userMax;
  1242. terrainType = baseTerrain;
  1243. }
  1244. //---------------------------------------------------------------------------
  1245. void Terrain::recalcWater()
  1246. {
  1247. mapData->recalcWater();
  1248. }
  1249. //---------------------------------------------------------------------------
  1250. void Terrain::reCalcLight(bool doShadows)
  1251. {
  1252. recalcLight = true;
  1253. recalcShadows = doShadows;
  1254. //Do a new burnin for the colormap
  1255. if (terrainTextures2)
  1256. {
  1257. if (colorMapName)
  1258. terrainTextures2->recalcLight(colorMapName);
  1259. else
  1260. terrainTextures2->recalcLight(terrainName);
  1261. }
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. void Terrain::clearShadows()
  1265. {
  1266. mapData->clearShadows();
  1267. }
  1268. //---------------------------------------------------------------------------
  1269. long Terrain::getWater (Stuff::Vector3D& worldPos) {
  1270. //-------------------------------------------------
  1271. // Get elevation at this point and compare to deep
  1272. // water altitude for this map.
  1273. float elevation = getTerrainElevation(worldPos);
  1274. if (elevation < (waterElevation - MapData::shallowDepth))
  1275. return(2);
  1276. if (elevation < waterElevation)
  1277. return(1);
  1278. return(0);
  1279. }
  1280. //---------------------------------------------------------------------------