File: GTerrainMap.cpp

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (736 lines) | stat: -rw-r--r-- 26,146 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#include "GTerrainMap.h"
#include "RAI.h"
#include <set>
#include "LegacyCpp/MoveData.h"
#include "System/Util.h"
#include "CUtils/Util.h"
#include <stdexcept>
//#include <time.h>
using std::deque;
using std::set;


template<typename T>
static inline void file_read(T* value, FILE* file)
{
	const size_t readCount = fread(value, sizeof(T), 1, file);
	if (readCount != 1) {
		throw std::runtime_error("failed reading from file");
	}
}

template<typename T>
static inline void file_write(const T* value, FILE* file)
{
	const size_t writeCount = fwrite(value, sizeof(T), 1, file);
	if (writeCount != 1) {
		throw std::runtime_error("failed writing to file");
	}
}

GlobalTerrainMap::GlobalTerrainMap(IAICallback* cb, cLogFile* l)
{
	const int mapFileVersion = 3;

	*l<<"\n Loading the Terrain-Map ...";

	// Reading the WaterDamage entry from the map file
	
	waterIsHarmful = false;
	waterIsAVoid = false;

	string relMapFileName = "cache/";
	relMapFileName += cRAI::MakeFileSystemCompatible(cb->GetMapName());
	relMapFileName += "-" + IntToString(cb->GetMapHash(), "%x");
	relMapFileName += ".res";

	string mapFileName_r;
	FILE* mapFile_r = NULL;
	// NOTE: cRAI::LocateFile() returns TRUE for _absent_ file under WIN32
	if (cRAI::LocateFile(cb, relMapFileName, mapFileName_r, false)) {
		mapFile_r = fopen(mapFileName_r.c_str(), "rb");
	}
	
	if (mapFile_r == NULL) {
		*l<<"\n  Could not find cache file for reading: " << relMapFileName;
	}

	bool mapFileLoaded = false;
	if( mapFile_r )
	{
		int version;

		try
		{
			file_read(&version, mapFile_r);

			if( version == mapFileVersion )
			{
				file_read(&waterIsHarmful, mapFile_r);
				file_read(&waterIsAVoid,   mapFile_r);
				mapFileLoaded = true;
			}
			else
			{
				*l<<"\n  The cached map-file is using a different version format, reloading...";
			}
		}
		catch (const std::exception& ex)
		{
			*l<<"\nERROR: failed reading the terrain map: " << ex.what();
		}

		fclose(mapFile_r);
	}
	
	if( !mapFileLoaded )
	{
//		double mapArchiveTimer = clock();
		string mapArchiveFileName = "maps/";
		mapArchiveFileName += cRAI::MakeFileSystemCompatible(cb->GetMapName());
		mapArchiveFileName += ".smd";

		int mapArchiveFileSize = cb->GetFileSize(mapArchiveFileName.c_str());
		if( mapArchiveFileSize > 0 )
		{
			*l<<"\n  Searching the Map-Archive File: '" << mapArchiveFileName << "'  File Size: "<<mapArchiveFileSize;
			char *archiveFile = new char[mapArchiveFileSize];
			cb->ReadFile(mapArchiveFileName.c_str(),archiveFile,mapArchiveFileSize);
			int waterDamage = GetFileValue(mapArchiveFileSize,archiveFile,"WaterDamage");
			waterIsAVoid = GetFileValue(mapArchiveFileSize,archiveFile,"VoidWater") > 0;
			*l<<"\n   Void Water: "<<waterIsAVoid;
			if( waterIsAVoid )
				*l<<" (This map has no water)";
			*l<<"\n   Water Damage: "<<waterDamage;
			if( waterDamage > 0 )
			{
				waterIsHarmful = true;
				*l<<" (This map's water is harmful to land units";
				if( waterDamage > 10000 )
				{
					waterIsAVoid = true; // UNTESTED
					*l<<" as well as hovercraft";
				}
				*l<<")";
			}
			delete [] archiveFile;
		}
		else
		{
			*l<<"\n  Could not find Map-Archive file for reading additional map info: " << mapArchiveFileName;
		}

//		*l<<"\n  Map-Archive Timer: "<<(clock()-mapArchiveTimer)/CLOCKS_PER_SEC<<" seconds";

		string mapFileName_w;
		FILE* mapFile_w = NULL;
		if (cRAI::LocateFile(cb, relMapFileName, mapFileName_w, true)) {
			mapFile_w = fopen(mapFileName_w.c_str(), "wb");
		}

		if (mapFile_w) {
			try
			{
				file_write(&mapFileVersion, mapFile_w);
				file_write(&waterIsHarmful, mapFile_w);
				file_write(&waterIsAVoid,   mapFile_w);
			}
			catch (const std::exception& ex)
			{
				*l<<"\nERROR: failed writing the terrain map: " << ex.what();
			}
			fclose(mapFile_w);
		} else {
			*l<<"\nERROR: Could not write to file: "<<relMapFileName;
		}
	}

	convertStoP = 64; // = 2^x, should not be less than 16
	if( (cb->GetMapWidth()/64)*(cb->GetMapHeight()/64) < 8*8 )
		convertStoP /= 2; // Smaller Sectors, more detailed analysis
//	else if( (cb->GetMapWidth()/16)*(cb->GetMapHeight()/16) > 20*20 )
//		convertStoP *= 2; // Larger Sectors, less detailed analysis
	sectorXSize = (8*cb->GetMapWidth())/convertStoP;
	sectorZSize = (8*cb->GetMapHeight())/convertStoP;
	
	sectorAirType = new TerrainMapAreaSector[sectorXSize*sectorZSize];
	
	*l<<"\n  Sector-Map Block Size: "<<convertStoP;
	*l<<"\n  Sector-Map Size: "<<sectorXSize*sectorZSize<<" (x"<<sectorXSize<<",z"<<sectorZSize<<")";

	// MoveType Detection and TerrainMapMobileType Initialization
	const UnitDef **udList = new const UnitDef*[cb->GetNumUnitDefs()];
	cb->GetUnitDefList(udList);
	int udSize=cb->GetNumUnitDefs();
	typedef pair<int,TerrainMapMobileType*> itmPair; // used to access mobileType
	typedef pair<int,TerrainMapImmobileType*> itiPair; // used to access immobileType

	for( int iud=0; iud<udSize; iud++ )
		if( udList[iud] != 0 )
		{
			if( udList[iud]->canfly )
				udMobileType.insert(itmPair(udList[iud]->id,0));
			else if( udList[iud]->movedata == 0 )
			{
				TerrainMapImmobileType* IT = 0;
				for(list<TerrainMapImmobileType>::iterator iIT = immobileType.begin(); iIT != immobileType.end(); ++iIT )
					if( iIT->maxElevation == -udList[iud]->minWaterDepth && iIT->canHover == udList[iud]->canhover && iIT->canFloat == udList[iud]->floater )
						if( iIT->minElevation == -udList[iud]->maxWaterDepth ||
							((iIT->canHover || iIT->canFloat) && iIT->minElevation <= 0 && -udList[iud]->maxWaterDepth <= 0) )
						{
							IT = &*iIT;
							break;
						}
				if( IT == 0 )
				{
					TerrainMapImmobileType IT2;
					immobileType.push_back(IT2);
					IT = &immobileType.back();
					IT->maxElevation = -udList[iud]->minWaterDepth;
					IT->minElevation = -udList[iud]->maxWaterDepth;
					IT->canHover = udList[iud]->canhover;
					IT->canFloat = udList[iud]->floater;
				}
				IT->udSize++;
				udImmobileType.insert(itiPair(udList[iud]->id,IT));
			}
			else
			{
				MoveData* md = udList[iud]->movedata;
				TerrainMapMobileType* MT = 0;
				for( list<TerrainMapMobileType>::iterator iMT=mobileType.begin(); iMT!=mobileType.end(); ++iMT )
					if( iMT->maxElevation == -udList[iud]->minWaterDepth && iMT->maxSlope == md->maxSlope &&
						iMT->canHover == udList[iud]->canhover && iMT->canFloat == udList[iud]->floater )
						if( iMT->minElevation == -md->depth ||
							((iMT->canHover || iMT->canFloat) && iMT->minElevation <= 0 && -udList[iud]->maxWaterDepth <= 0) )
						{
							MT = &*iMT;
							break;
						}
				if( MT == 0 )
				{
					TerrainMapMobileType MT2;
					mobileType.push_back(MT2);
					MT = &mobileType.back();
					MT->maxSlope = md->maxSlope;
					MT->maxElevation = -udList[iud]->minWaterDepth;
					MT->minElevation = -md->depth;
					MT->canHover = udList[iud]->canhover;
					MT->canFloat = udList[iud]->floater;
					MT->sector = new TerrainMapAreaSector[sectorXSize*sectorZSize];
					MT->MD = md;
//					MT->PF = new GlobalPathfinder();
//					MT->PF->Init(cb,l,MT->maxElevation,-md->depth,md->maxSlope,MT->canHover,MT->canFloat,waterIsHarmful);
//					vector<float3> path;
//					float3 start = float3(20.0,0,20.0);
//					float3 end = float3(80.0,0,80.0);
//					*l<<" distance="; *l<<MT->PF->FindPath(path,start,end);
				}
				MT->udSize++;
				udMobileType.insert(itmPair(udList[iud]->id,MT));
				if( MT->MD->crushStrength < udList[iud]->movedata->crushStrength )
					MT->MD = udList[iud]->movedata; // figured it would be easier on the pathfinder
			}
		}
	delete [] udList;

	if( waterIsAVoid )
	{
		if( mobileType.empty() ) // Work-Around(Mod FF): buildings use canFloat instead of canFly to represent their ability to fly in space
			waterIsAVoid = false;
		else
			waterIsHarmful = true; // If there is no water then this will prevent water units from being used
	}

	// Special types
	landSectorType = 0;
	waterSectorType = 0;
	for(list<TerrainMapImmobileType>::iterator iMT = immobileType.begin(); iMT != immobileType.end(); ++iMT )
		if( !iMT->canFloat && !iMT->canHover )
		{
			if( iMT->minElevation == 0 && (landSectorType == 0 || iMT->maxElevation > landSectorType->maxElevation ) )
				landSectorType = &*iMT;
			if( iMT->maxElevation == 0 && (waterSectorType == 0 || iMT->minElevation < waterSectorType->minElevation ) )
				waterSectorType = &*iMT;
		}
	if( landSectorType == 0 )
	{
		immobileType.push_back(TerrainMapImmobileType());
		landSectorType = &immobileType.back();
		immobileType.back().maxElevation = 1e7;
		immobileType.back().minElevation = 0;
		immobileType.back().canFloat = false;
		immobileType.back().canHover = false;
	}
	if( waterSectorType == 0 )
	{
		immobileType.push_back(TerrainMapImmobileType());
		waterSectorType = &immobileType.back();
		immobileType.back().maxElevation = 0;
		immobileType.back().minElevation = -1e7;
		immobileType.back().canFloat = false;
		immobileType.back().canHover = false;
	}

	*l<<"\n  Determining Usable Terrain for all units ...";
	// Setting sector & determining sectors for immobileType
	sector = new TerrainMapSector[sectorXSize*sectorZSize];
	const float *standardSlopeMap = cb->GetSlopeMap();
	const float *standardHeightMap = cb->GetHeightMap();
	const int convertStoSM = convertStoP/16; // * for conversion, / for reverse conversion
	const int convertStoHM = convertStoP/8; // * for conversion, / for reverse conversion
	const int slopeMapXSize = sectorXSize*convertStoSM;
	const int heightMapXSize = sectorXSize*convertStoHM;
	
	typedef pair<int,TerrainMapSector*> itdPair;
	
	minElevation=0;
	percentLand=0.0;

	for(int z=0; z < sectorZSize; z++)
		for(int x=0; x < sectorXSize; x++)
		{
			int i=(z*sectorXSize)+x;

			sector[i].position.x = x*convertStoP+convertStoP/2; // Center position of the Block
			sector[i].position.z = z*convertStoP+convertStoP/2; //
			sector[i].position.y = cb->GetElevation(sector[i].position.x,sector[i].position.z);
			
			sectorAirType[i].S = &sector[i];
			
			for(list<TerrainMapMobileType>::iterator iMT = mobileType.begin(); iMT != mobileType.end(); ++iMT )
				iMT->sector[i].S = &sector[i];

			int iMap = ((z*convertStoSM)*slopeMapXSize)+x*convertStoSM;
			for(int zS=0; zS<convertStoSM; zS++)
				for(int xS=0,iS=iMap+zS*slopeMapXSize+xS; xS<convertStoSM; xS++,iS=iMap+zS*slopeMapXSize+xS )
					if( sector[i].maxSlope < standardSlopeMap[iS] )
						sector[i].maxSlope = standardSlopeMap[iS];

			iMap = ((z*convertStoHM)*heightMapXSize)+x*convertStoHM;
			sector[i].minElevation = standardHeightMap[iMap];
			sector[i].maxElevation = standardHeightMap[iMap];
			
			for(int zH=0; zH<convertStoHM; zH++)
			{
				for(int xH=0,iH=iMap+zH*heightMapXSize+xH; xH<convertStoHM; xH++,iH=iMap+zH*heightMapXSize+xH )
				{
					if( standardHeightMap[iH] >= 0 )
					{
						sector[i].percentLand++;
						percentLand++;
					}
					
					if( sector[i].minElevation > standardHeightMap[iH] )
					{
						sector[i].minElevation = standardHeightMap[iH];
						if( minElevation > standardHeightMap[i] )
							minElevation = standardHeightMap[i];
					}
					else if( sector[i].maxElevation < standardHeightMap[iH] )
						sector[i].maxElevation = standardHeightMap[iH];
				}
			}

			sector[i].percentLand *= 100.0 / (convertStoHM*convertStoHM);
			
			if( sector[i].percentLand > 50.0 )
				sector[i].isWater = false;
			else
				sector[i].isWater = true;

			for(list<TerrainMapImmobileType>::iterator iMT = immobileType.begin(); iMT != immobileType.end(); ++iMT )
				if( (iMT->canHover && iMT->maxElevation >= sector[i].maxElevation && !waterIsAVoid ) ||
					(iMT->canFloat && iMT->maxElevation >= sector[i].maxElevation && !waterIsHarmful ) ||
					(iMT->minElevation <= sector[i].minElevation && iMT->maxElevation >= sector[i].maxElevation && (!waterIsHarmful || sector[i].minElevation >=0 ) ) )
					iMT->sector.insert(itdPair(i,&sector[i]));
		}
	
	percentLand *= 100.0/(sectorXSize*convertStoHM*sectorZSize*convertStoHM);

	for(list<TerrainMapImmobileType>::iterator iMT = immobileType.begin(); iMT != immobileType.end(); ++iMT )
		if( (100.0*iMT->sector.size())/float(sectorXSize*sectorZSize) >= 20.0 || (double)convertStoP*convertStoP*iMT->sector.size() >= 1.8e7 )
			iMT->typeUsable = true;
		else
			iMT->typeUsable = false;

	*l<<"\n   Map Land Percent: " << percentLand<<"%";
	if (percentLand < 85.0f) {
		*l<<"\n   Water is a void: " << waterIsAVoid;
		*l<<"\n   Water is harmful: " << waterIsHarmful;
	}
	*l<<"\n   Minimum Elevation: " << minElevation;

	for(list<TerrainMapImmobileType>::iterator iMT = immobileType.begin(); iMT != immobileType.end(); ++iMT )
	{
		*l<<"\n   Immobile-Type: Min/Max Elevation=(";
		if( iMT->canHover )
			*l<<"hover";
		else if( iMT->canFloat || iMT->minElevation < -10000 )
			*l<<"any";
		else
			*l<<iMT->minElevation;
		*l<<" / ";
		if( iMT->maxElevation < 10000 )
			*l<<iMT->maxElevation;
		else
			*l<<"any";
		*l<<")   \tIs buildable across "<<(100.0*iMT->sector.size())/(sectorXSize*sectorZSize)<<"% of the map. (used by "<<iMT->udSize<<" unit-defs)"; //<<" "<<iMT->sector.size()<<" sector(s)";
	}
/*
	*l<<"\n  Displaying the Standard Land/Water Area(s) ...";
	for( int i=0; i<areaIUSize; i++ )
	{
		*l<<"\n   ";
		*l<<"("<<i+1<<"), occupying "<<areaIU[i]->percentOfMap<<"% of the map ("<<areaIU[i]->percentMetal<<"% Metal) ";
		if( areaIU[i]->isMetalMap ) *l<<"(MetalMap)";
		*l<<", has been detected with "<<areaIU[i]->sector.size()<<" sectors.";
	}
*/
	const size_t MAMinimalSectors = 8;          // Minimal # of sector for a valid MapArea
	const float MAMinimalSectorPercent = 0.5;   // Minimal % of map for a valid MapArea
	for( list<TerrainMapMobileType>::iterator iMT=mobileType.begin(); iMT!=mobileType.end(); ++iMT )
	{
		*l<<"\n   Mobile-Type: Min/Max Elevation=(";
		if( iMT->canFloat )
			*l<<"any";
		else if( iMT->canHover )
			*l<<"hover";
		else
			*l<<iMT->minElevation;
		*l<<" / ";
		if( iMT->maxElevation < 10000 )
			*l<<iMT->maxElevation;
		else
			*l<<"any";
		*l<<")\tMax Slope=("<<iMT->maxSlope;
		*l<<")   \tMove-Data used:'"<<iMT->MD->name<<"'";

		deque<int> sectorSearch;
		set<int> sectorsRemaining;
		for(int iS=0; iS<sectorZSize*sectorXSize; iS++)
			if( (iMT->canHover && iMT->maxElevation >= sector[iS].maxElevation && !waterIsAVoid && (sector[iS].maxElevation <= 0 || iMT->maxSlope >= sector[iS].maxSlope) ) ||
				(iMT->canFloat && iMT->maxElevation >= sector[iS].maxElevation && !waterIsHarmful && (sector[iS].maxElevation <= 0 || iMT->maxSlope >= sector[iS].maxSlope) ) ||
				(iMT->maxSlope >= sector[iS].maxSlope && iMT->minElevation <= sector[iS].minElevation && iMT->maxElevation >= sector[iS].maxElevation && (!waterIsHarmful || sector[iS].minElevation >=0 ) ) )
				sectorsRemaining.insert(iS);
		int i,iX,iZ,aIndex = 0; // Temp Var.
		while( int(sectorsRemaining.size())>0 || int(sectorSearch.size())>0 )
		{
			if( int(sectorSearch.size()) > 0 )
			{
				i = sectorSearch.front();
				iMT->area[aIndex]->sector.insert(iasPair(i,&iMT->sector[i]));
				iX = i%sectorXSize;
				iZ = i/sectorXSize;
				if( sectorsRemaining.find(i-1)!=sectorsRemaining.end() && iX > 0 ) // Search left
				{
					sectorSearch.push_back(i-1);
					sectorsRemaining.erase(i-1);
				}
				if( sectorsRemaining.find(i+1)!=sectorsRemaining.end() && iX < sectorXSize-1 ) // Search right
				{
					sectorSearch.push_back(i+1);
					sectorsRemaining.erase(i+1);
				}
				if( sectorsRemaining.find(i-sectorXSize)!=sectorsRemaining.end() && iZ > 0 ) // Search up
				{
					sectorSearch.push_back(i-sectorXSize);
					sectorsRemaining.erase(i-sectorXSize);
				}
				if( sectorsRemaining.find(i+sectorXSize)!=sectorsRemaining.end() && iZ < sectorZSize-1 ) // Search down
				{
					sectorSearch.push_back(i+sectorXSize);
					sectorsRemaining.erase(i+sectorXSize);
				}
				sectorSearch.pop_front();
			}
			else
			{
				if( iMT->areaSize > 0 &&
					( iMT->areaSize == 50 ||
					  iMT->area[iMT->areaSize-1]->sector.size() <= MAMinimalSectors ||
					  100.*float(iMT->area[iMT->areaSize-1]->sector.size())/float(sectorXSize*sectorZSize) <= MAMinimalSectorPercent ) )
				{
					// Too mand areas detected.  Find, erase & ignore the smallest one that was found so far
					if( iMT->areaSize == 50 )
						*l<<"\nWARNING: The MapArea limit has been reached (possible error).";
					aIndex=0;
					for( int iA=1; iA<iMT->areaSize; iA++ )
						if( iMT->area[iA]->sector.size() < iMT->area[aIndex]->sector.size() )
							aIndex = iA;
					delete iMT->area[aIndex];
					iMT->areaSize--;
				}
				else
					aIndex=iMT->areaSize;

				i=*sectorsRemaining.begin();
				sectorSearch.push_back(i);
				sectorsRemaining.erase(i);
				iMT->area[aIndex] = new TerrainMapArea(aIndex,&*iMT);
				iMT->areaSize++;
			}
		}
		if( iMT->areaSize > 0 &&
			( iMT->area[iMT->areaSize-1]->sector.size() <= MAMinimalSectors ||
			  100.0*float(iMT->area[iMT->areaSize-1]->sector.size())/float(sectorXSize*sectorZSize) <= MAMinimalSectorPercent ) )
		{
			iMT->areaSize--;
			delete iMT->area[iMT->areaSize];
		}

		// Calculations
		float percentOfMap = 0.0;
		for( int iA=0; iA<iMT->areaSize; iA++ )
		{
			for( map<int,TerrainMapAreaSector*>::iterator iS=iMT->area[iA]->sector.begin(); iS!=iMT->area[iA]->sector.end(); ++iS )
				iS->second->area = iMT->area[iA];
			iMT->area[iA]->percentOfMap = (100.0*iMT->area[iA]->sector.size())/(sectorXSize*sectorZSize);
			if( iMT->area[iA]->percentOfMap >= 20.0 ) // A map area occupying 20% of the map
			{
				iMT->area[iA]->areaUsable = true;
				iMT->typeUsable = true;
			}
			else
				iMT->area[iA]->areaUsable = false;
			if( iMT->areaLargest == 0 || iMT->areaLargest->percentOfMap < iMT->area[iA]->percentOfMap )
				iMT->areaLargest = iMT->area[iA];

			percentOfMap += iMT->area[iA]->percentOfMap;
		}
		*l<<"  \tHas "<<iMT->areaSize<<" Map-Area(s) occupying "<<percentOfMap<<"% of the map. (used by "<<iMT->udSize<<" unit-defs)";
	}
/*
	// Debugging
	l = new cLogFile("TerrainMapDebug.log",false);
	for( int iS=0; iS<sectorXSize*sectorZSize; iS++ )
	{
		if( iS % sectorXSize == 0 ) *l<<"\n";
		if( sector[iS].maxElevation < 0.0 ) *l<<"~";
		else if( sector[iS].maxSlope > 0.5 ) *l<<"^";
		else if( sector[iS].maxSlope > 0.25 ) *l<<"#";
		else *l<<"x";
	}
	for( list<TerrainMapMobileType>::iterator iM=mobileType.begin(); iM!=mobileType.end(); iM++ )
	{
		*l<<"\n\n "<<iM->MD->name<<" h="<<iM->canHover<<" f="<<iM->canFloat<<" mb="<<iM->areaSize;
		for( int iS=0; iS<sectorXSize*sectorZSize; iS++ )
		{
			if( iS % sectorXSize == 0 ) *l<<"\n";
			if( iM->sector[iS].area != 0 ) *l<<"*";
			else if( sector[iS].maxElevation < 0.0 ) *l<<"~";
			else if( sector[iS].maxSlope > 0.5 ) *l<<"^";
			else *l<<"x";
		}
	}
	*l<<"\n";
*/
}

GlobalTerrainMap::~GlobalTerrainMap()
{
	delete [] sector;
	delete [] sectorAirType;
}

bool GlobalTerrainMap::CanMoveToPos(TerrainMapArea *area, const float3& destination)
{
	int iS = GetSectorIndex(destination);
	if( !IsSectorValid(iS) )
		return false;
	if( area == 0 ) // either a flying unit or a unit was somehow created at an impossible position
		return true;
	if( area == GetSectorList(area)[iS].area )
		return true;
	return false;
}

TerrainMapAreaSector* GlobalTerrainMap::GetSectorList(TerrainMapArea* sourceArea)
{
	if( sourceArea == 0 || sourceArea->mobileType == 0 ) // It flies or it's immobile
		return sectorAirType;
	return sourceArea->mobileType->sector;
}

TerrainMapAreaSector* GlobalTerrainMap::GetClosestSector(TerrainMapArea* sourceArea, const int& destinationSIndex)
{
	map<int,TerrainMapAreaSector*>::iterator iAS = sourceArea->sectorClosest.find(destinationSIndex);
	if( iAS != sourceArea->sectorClosest.end() ) // It's already been determined
		return iAS->second;
//*l<<"\n GCAS";
	TerrainMapAreaSector* TMSectors = GetSectorList(sourceArea);
	if( sourceArea == TMSectors[destinationSIndex].area )
	{
		sourceArea->sectorClosest.insert(iasPair(destinationSIndex,&TMSectors[destinationSIndex]));
//*l<<"1(#)";
		return &TMSectors[destinationSIndex];
	}

	float3 *destination = &TMSectors[destinationSIndex].S->position;
	TerrainMapAreaSector* SClosest = 0;
	float DisClosest = 0.0f;
	for( map<int,TerrainMapAreaSector*>::iterator iS=sourceArea->sector.begin(); iS!=sourceArea->sector.end(); ++iS )
		if( SClosest == 0 || iS->second->S->position.distance(*destination) < DisClosest )
		{
			SClosest = iS->second;
			DisClosest = iS->second->S->position.distance(*destination);
		}
	sourceArea->sectorClosest.insert(iasPair(destinationSIndex,SClosest));
//*l<<"2(#)";
	return SClosest;
}

TerrainMapSector* GlobalTerrainMap::GetClosestSector(TerrainMapImmobileType* sourceIT, const int& destinationSIndex)
{
	map<int,TerrainMapSector*>::iterator iS = sourceIT->sectorClosest.find(destinationSIndex);
	if( iS != sourceIT->sectorClosest.end() ) // It's already been determined
		return iS->second;
//*l<<"\n GCS";
	if( sourceIT->sector.find(destinationSIndex) != sourceIT->sector.end() )
	{
		sourceIT->sectorClosest.insert(isdPair(destinationSIndex,&sector[destinationSIndex]));
//*l<<"1(#)";
		return &sector[destinationSIndex];
	}

	const float3 *destination = &sector[destinationSIndex].position;
	TerrainMapSector* SClosest = 0;
	float DisClosest = 0.0f;
	for( map<int,TerrainMapSector*>::iterator iS=sourceIT->sector.begin(); iS!=sourceIT->sector.end(); ++iS )
		if( SClosest == 0 || iS->second->position.distance(*destination) < DisClosest )
		{
			SClosest = iS->second;
			DisClosest = iS->second->position.distance(*destination);
		}
	sourceIT->sectorClosest.insert(isdPair(destinationSIndex,SClosest));
//*l<<"2(#)";
	return SClosest;
}

TerrainMapAreaSector* GlobalTerrainMap::GetAlternativeSector(TerrainMapArea* sourceArea, const int& sourceSIndex, TerrainMapMobileType* destinationMT)
{
	TerrainMapAreaSector* TMSectors = GetSectorList(sourceArea);
	map<TerrainMapMobileType*,TerrainMapAreaSector*>::iterator iMS = TMSectors[sourceSIndex].sectorAlternativeM.find(destinationMT);
	if( iMS != TMSectors[sourceSIndex].sectorAlternativeM.end() ) // It's already been determined
		return iMS->second;
//*l<<"\nGSAM";
	if( destinationMT == 0 ) // flying unit movetype
	{
//		*l<<"(2#)";
		return &TMSectors[sourceSIndex];
	}

	if( sourceArea != 0 && sourceArea != TMSectors[sourceSIndex].area )
	{
//		*l<<"(3#)";
		return GetAlternativeSector(sourceArea, GetSectorIndex(GetClosestSector(sourceArea,sourceSIndex)->S->position), destinationMT);
	}

	const float3* position = &TMSectors[sourceSIndex].S->position;
	TerrainMapAreaSector* bestAS = 0;
	TerrainMapArea* largestArea = 0;
	float bestDistance = -1.0;
	float bestMidDistance = -1.0;
	TerrainMapArea **TMAreas = destinationMT->area;
	const int *TMAreaSize = &destinationMT->areaSize;
	for( int iA=0; iA<*TMAreaSize; iA++ )
		if( largestArea == 0 || largestArea->percentOfMap < TMAreas[iA]->percentOfMap )
			largestArea = TMAreas[iA];
	for( int iA=0; iA<*TMAreaSize; iA++ )
		if( TMAreas[iA]->areaUsable || !largestArea->areaUsable )
		{
			TerrainMapAreaSector* CAS = GetClosestSector(TMAreas[iA],sourceSIndex);
			float midDistance; // how much of a gap exists between the two areas (source & destination)
			if( sourceArea == 0 || sourceArea == TMSectors[GetSectorIndex(CAS->S->position)].area )
				midDistance = 0.0;
			else
				midDistance = CAS->S->position.distance2D(GetClosestSector(sourceArea,GetSectorIndex(CAS->S->position))->S->position);
			if( bestMidDistance < 0 || midDistance < bestMidDistance )
			{
				bestMidDistance = midDistance;
				bestAS = 0;
				bestDistance = -1.0;
			}
			if( midDistance == bestMidDistance )
			{
				float distance = position->distance2D(CAS->S->position);
				if( bestAS == 0 || distance*TMAreas[iA]->percentOfMap < bestDistance*bestAS->area->percentOfMap )
				{
					bestAS = CAS;
					bestDistance = distance;
				}
			}
		}
	TMSectors[sourceSIndex].sectorAlternativeM.insert(msPair(destinationMT,bestAS));
//	*l<<"(!#)";
	return bestAS;
}

TerrainMapSector* GlobalTerrainMap::GetAlternativeSector(TerrainMapArea* destinationArea, const int& sourceSIndex, TerrainMapImmobileType* destinationIT)
{
	TerrainMapAreaSector* TMSectors = GetSectorList(destinationArea);
	map<TerrainMapImmobileType*,TerrainMapSector*>::iterator iMS = TMSectors[sourceSIndex].sectorAlternativeI.find(destinationIT);
	if( iMS != TMSectors[sourceSIndex].sectorAlternativeI.end() ) // It's already been determined
		return iMS->second;

//*l<<"\nGSAI";
	TerrainMapSector* closestS = 0;
	if( destinationArea != 0 && destinationArea != TMSectors[sourceSIndex].area )
	{
//		*l<<"(3#)";
		closestS = GetAlternativeSector(destinationArea, GetSectorIndex(GetClosestSector(destinationArea,sourceSIndex)->S->position), destinationIT);
		TMSectors[sourceSIndex].sectorAlternativeI.insert(isPair(destinationIT,closestS));
		return closestS;
	}

	const float3 *position = &sector[sourceSIndex].position;
	float closestDistance = -1.0;
	for( map<int,TerrainMapAreaSector*>::iterator iS=destinationArea->sector.begin(); iS!=destinationArea->sector.end(); ++iS )
		if( closestS == 0 || iS->second->S->position.distance(*position) < closestDistance )
		{
			closestS = iS->second->S;
			closestDistance = iS->second->S->position.distance(*position);
		}

	TMSectors[sourceSIndex].sectorAlternativeI.insert(isPair(destinationIT,closestS));
//	*l<<"(!#)";
	return closestS;
}

int GlobalTerrainMap::GetSectorIndex(const float3& position)
{
	return sectorXSize*(int(position.z)/convertStoP) + int(position.x)/convertStoP;
}

bool GlobalTerrainMap::IsSectorValid(const int& sIndex)
{
	if( sIndex < 0 || sIndex >= sectorXSize*sectorZSize )
		return false;
	return true;
}

int GlobalTerrainMap::GetFileValue(int &fileSize, char *&file, string entry)
{
	for(size_t i=0; i<entry.size(); i++) {
		if( !islower(entry[i]) ) {
			entry[i] = tolower(entry[i]);
		}
	}
	size_t entryIndex = 0;
	string entryValue = "";
	for(int i=0; i<fileSize; i++)
	{
		if( entryIndex >= entry.size() )
		{	// Entry Found: Reading the value
			if( file[i] >= '0' && file[i] <= '9' )
				entryValue += file[i];
			else if( file[i] == ';' )
				return atoi(entryValue.c_str());
		}
		else if( entry[entryIndex] == file[i] || (!islower(file[i]) && entry[entryIndex] == tolower(file[i]) ) ) // the current letter matches
			entryIndex++;
		else
			entryIndex = 0;
	}
	return 0;
}