File: RAI.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 (1198 lines) | stat: -rw-r--r-- 34,358 bytes parent folder | download | duplicates (2)
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
#include "RAI.h"
#include "LegacyCpp/IGlobalAICallback.h"
#include "LegacyCpp/UnitDef.h"
#include "LegacyCpp/CommandQueue.h"
#include "LegacyCpp/MoveData.h"
#include "CUtils/Util.h"
#include "System/Util.h"
#include "System/SafeCStrings.h"
#include <stdio.h>
//#include <direct.h>	// mkdir function (windows)
//#include <sys/stat.h>	// mkdir function (linux)
#include <time.h>		// time(NULL)

static GlobalResourceMap* GRMap = NULL;
static GlobalTerrainMap* GTMap  = NULL;
static cLogFile* gl = NULL;
static int RAIs=0;

namespace std
{
	void _xlen(){}
}

UnitInfo::UnitInfo(sRAIUnitDef* rUnitDef)
{
	udr=rUnitDef;
	ud=rUnitDef->ud;

	AIDisabled=false;
	humanCommand=false;
	unitBeingBuilt=true;
	inCombat=false;
	lastUnitIdleFrame=0;
	lastUnitDamagedFrame=0;
	enemyID=-1;
	area=0;
	E=0;
	enemyEff=0;
	udrBL=0;
	RS=0;
	BuildQ=0;
	CloakUI=0;
	OnOffUI=0;
	SWeaponUI=0;
	group=0;
	UE = 0;
}

EnemyInfo::EnemyInfo()
{
	inLOS=false;
	inRadar=false;
	baseThreatFrame=-1;
	baseThreatID=-1;
	ud=0;
	udr=0;
	RS=0;
	posLocked=false;
}

cRAI::cRAI()
{
	DebugEnemyEnterLOS=0;
	DebugEnemyLeaveLOS=0;
	DebugEnemyEnterRadar=0;
	DebugEnemyLeaveRadar=0;
	DebugEnemyDestroyedLOS=0;
	DebugEnemyDestroyedRadar=0;
	DebugEnemyEnterLOSError=0;
	DebugEnemyLeaveLOSError=0;
	DebugEnemyEnterRadarError=0;
	DebugEnemyLeaveRadarError=0;
	eventSize = 0;
	SWM=0;
	cb=0;
	frame=0;
	memset(eventList, 0, EVENT_LIST_SIZE);
	TM=0;
	RM=0;
	UM=0;
	UDH=0;
	CM=0;
	B=0;
	l=0;
}

cRAI::~cRAI()
{
	*l<<"\n\nShutting Down ...";
	if( RAIDEBUGGING )
	{
		*l<<"\n cRAI Debug:";
		*l<<"\n  clearing Units size="<<int(Units.size())<<": ";
		*l<<"\n  clearing Enemies size="<<Enemies.size()<<": ";
	}

	while( int(Units.size()) > 0 )
		UnitDestroyed(Units.begin()->first,-1);
	while( int(Enemies.size()) > 0 )
		EnemyDestroyed(Enemies.begin()->first,-1);

	if( RAIDEBUGGING )
	{
		int ERadar,ELOS;
		*l<<"\n Enemys (Enter LOS - Errors)                        = "<<DebugEnemyEnterLOS<<" = "<<DebugEnemyEnterLOS+DebugEnemyEnterLOSError<<"-"<<DebugEnemyEnterLOSError;
		*l<<"\n Enemys (Leave LOS + Destroyed in LOS - Errors)     = "<<DebugEnemyLeaveLOS+DebugEnemyDestroyedLOS<<" = "<<DebugEnemyLeaveLOS+DebugEnemyLeaveLOSError<<"+"<<DebugEnemyDestroyedLOS<<"-"<<DebugEnemyLeaveLOSError;
		*l<<"\n Enemies Remaining in LOS                           = "<<(ELOS=DebugEnemyEnterLOS-DebugEnemyLeaveLOS-DebugEnemyDestroyedLOS);
		*l<<"\n Enemys (Enter Radar - Errors)                      = "<<DebugEnemyEnterRadar<<" = "<<DebugEnemyEnterRadar+DebugEnemyEnterRadarError<<"-"<<DebugEnemyEnterRadarError;
		*l<<"\n Enemys (Leave Radar + Destroyed in Radar - Errors) = "<<DebugEnemyLeaveRadar+DebugEnemyDestroyedRadar<<" = "<<DebugEnemyLeaveRadar+DebugEnemyLeaveRadarError<<"+"<<DebugEnemyDestroyedRadar<<"-"<<DebugEnemyLeaveRadarError;
		*l<<"\n Enemies Remaining in Radar                         = "<<(ERadar=DebugEnemyEnterRadar-DebugEnemyLeaveRadar-DebugEnemyDestroyedRadar);
		*l<<"\n Enemies Remaining                                  = "<<int(Enemies.size());
		*l<<"\n Units Remaining                                    = "<<UM->GroupSize;
		*l<<"\n Groups Remaining                                   = "<<UM->GroupSize;
		if( UM->GroupSize != 0 || ELOS != 0 || ERadar != 0 )
			*l<<"\n  (ERROR)";
	}

	delete UM;
	UM = NULL;
	delete B;
	B = NULL;
	delete SWM;
	SWM = NULL;
	delete CM;
	CM = NULL;
	delete UDH;
	UDH = NULL;

	RAIs--;
	if( RAIs == 0 )
	{
		*gl<<"\n Global RAI Shutting Down";
//		double closingTimer = clock();
		GlobalResourceMap* tmpRM = GRMap;
		GRMap = NULL;
		RM    = NULL;
		delete tmpRM;
		tmpRM = NULL;
//		*gl<<"\n Resource-Map Closing Time: "<<(clock()-closingTimer)/(double)CLOCKS_PER_SEC<<" seconds";
		GlobalTerrainMap* tmpTM = GTMap;
		GTMap = NULL;
		TM    = NULL;
		delete tmpTM;
		tmpTM = NULL;
		*gl<<"\n Global RAI Shutdown Complete.";
		delete gl;
		gl = NULL;
	}

	*l<<"\nShutdown Complete.";
	delete l;
	l = NULL;
}

void cRAI::InitAI(IGlobalAICallback* callback, int team)
{
	cb = callback->GetAICallback();
	if( GRMap == 0 )
		ClearLogFiles();
	l = new cLogFile(cb, GetLogFileSubPath(cb->GetMyTeam()), false);

/*	string test = (char*)cb->GetMyTeam(); // Crashes Spring?  Spring-Version(v0.76b1)
	*l<<"cb->GetMyTeam()="<<test<<"\n";
*/
	if( GRMap == 0 )
	{
		ClearLogFiles();
		gl = new cLogFile(cb, "log/RAIGlobal_LastGame.log", false);
		*gl<<"Loading Global RAI...";
		*gl<<"\n Mod = " << cb->GetModHumanName() << "(" << IntToString(cb->GetModHash(), "%x") << ")";
		*gl<<"\n Map = " << cb->GetMapName()      << "(" << IntToString(cb->GetMapHash(), "%x") << ")";
		int seed = time(NULL);
		srand(seed);
		RAIs=0;
		double loadingTimer = clock();
		GTMap = new GlobalTerrainMap(cb,gl);
		*gl<<"\n  Terrain-Map Loading Time: "<<(clock()-loadingTimer)/(double)CLOCKS_PER_SEC<<" seconds";
		loadingTimer = clock();
		GRMap = new GlobalResourceMap(cb,gl,GTMap);
		*gl<<"\n  Resource-Map Loading Time: "<<(clock()-loadingTimer)/(double)CLOCKS_PER_SEC<<" seconds\n";
/*
		loadingTimer = clock();
		CMetalMap* KMM;
		KMM = new CMetalMap(cb);
		KMM->Init();
		*l<<"  KAI Metal-Map Loading Time: "<<(clock()-loadingTimer)/(double)CLOCKS_PER_SEC<<" seconds";
		*l<<"\n   KAI Metal-Sites Found: "<<KMM->NumSpotsFound;
		delete KMM;
*/
		*gl<<"\nGlobal RAI Loading Complete.\n\n";
	}
	RM = GRMap;
	TM = GTMap;
	RAIs++;

	*l<<"Loading ...";
	*l<<"\n Team = "<<cb->GetMyTeam();
	*l<<"\n Ally Team = "<<cb->GetMyAllyTeam();

	*l<<"\nLoading cRAIUnitDefHandler ...";
	UDH = new cRAIUnitDefHandler(cb,RM,TM,l);

	UM = new cUnitManager(cb,this);
	B = new cBuilder(cb,this);
	UpdateEventAdd(3,cb->GetCurrentFrame()-1);
	SWM = new cSWeaponManager(cb,this);
	CM = new cCombatManager(cb,this);
	*l<<"\nLoading Complete.\n";
/*
	if( cb->GetUnitDef("arm_retaliator") != 0 ) // XTA Cheat Nuke Test
	{
		IAICheats* cheat=callback->GetCheatInterface();
		float3 pos =*cb->GetStartPos();
		float3 pos2 =*cb->GetStartPos();
		float3 pos3 =*cb->GetStartPos();
		if( pos.x < 8*cb->GetMapWidth() - pos.x )
		{
			pos.x = 1;
			pos2.x = 200;
			pos3.x = 1;
		}
		else
		{
			pos.x = 8*cb->GetMapWidth();
			pos2.x = pos.x - 200;
			pos3.x = pos.x;
		}
		if( pos.z < 8*cb->GetMapHeight() - pos.z )
		{
			pos.z = 1;
			pos2.z = 1;
			pos3.z = 200;
		}
		else
		{
			pos.z = 8*cb->GetMapHeight();
			pos2.z = pos.z;
			pos3.z = pos.z - 200;
		}

		pos = cb->ClosestBuildSite(cb->GetUnitDef("arm_retaliator"),pos,1500,0);
		cheat->CreateUnit("arm_retaliator",pos);
		cheat->CreateUnit("arm_moho_metal_maker",cb->ClosestBuildSite(cb->GetUnitDef("arm_moho_metal_maker"),pos,1500,3));
		cheat->CreateUnit("arm_fusion_reactor",cb->ClosestBuildSite(cb->GetUnitDef("arm_fusion_reactor"),pos,1500,3));
		cheat->CreateUnit("arm_energy_storage",cb->ClosestBuildSite(cb->GetUnitDef("arm_energy_storage"),pos,1500,3));
		cheat->CreateUnit("arm_annihilator",cb->ClosestBuildSite(cb->GetUnitDef("arm_annihilator"),pos,1500,10));
		cheat->CreateUnit("arm_annihilator",cb->ClosestBuildSite(cb->GetUnitDef("arm_annihilator"),pos,1500,10));
		cheat->CreateUnit("arm_annihilator",cb->ClosestBuildSite(cb->GetUnitDef("arm_annihilator"),pos,1500,10));
	}
*/
}

void cRAI::UnitCreated(int unit, int builder)
{
	const UnitDef* ud=cb->GetUnitDef(unit);
	if( RAIDEBUGGING ) { *l<<"\nUnitCreated("<<unit; *l<<")["+ud->humanName+"]"; }
	Units.insert(iuPair(unit,UnitInfo(&UDH->UDR.find(ud->id)->second)));
	UnitInfo* U = &Units.find(unit)->second;
	float3 position = cb->GetUnitPos(unit);
	U->area = GetCurrentMapArea(U->udr,position);

	if( ud->speed == 0 )
	{
		for(map<int,UnitInfo*>::iterator i=UImmobile.begin(); i!=UImmobile.end(); ++i )
		{
			if( U->udr->WeaponGuardRange > 0 && i->second->udr->WeaponGuardRange == 0 && position.distance2D(cb->GetUnitPos(i->first)) < U->udr->WeaponGuardRange )
			{
				U->UDefending.insert(cRAI::iupPair(i->first,i->second));
				i->second->UDefences.insert(cRAI::iupPair(unit,U));
			}
			else if( U->udr->WeaponGuardRange == 0 && i->second->udr->WeaponGuardRange > 0 && position.distance2D(cb->GetUnitPos(i->first)) < i->second->udr->WeaponGuardRange )
			{
				U->UDefences.insert(cRAI::iupPair(i->first,i->second));
				i->second->UDefending.insert(cRAI::iupPair(unit,U));
			}
		}
		UImmobile.insert(iupPair(unit,U));
	}
	else
		UMobile.insert(iupPair(unit,U));

	B->UnitCreated(unit,U);
	B->BP->UResourceCreated(unit,U);
	if( RAIDEBUGGING ) *l<<"#";
}

void cRAI::UnitFinished(int unit)
{
	if( RAIDEBUGGING ) *l<<"\nUnitFinished("<<unit<<")";
	if( Units.find(unit) == Units.end() ) // Occurs if a player canceled a build order with more than one quaried and something still finished
		UnitCreated(unit, -1);

	UnitInfo* U = &Units.find(unit)->second;
	U->unitBeingBuilt = false;
	if( U->AIDisabled )
	{
		if( RAIDEBUGGING ) *l<<"#";
		return;
	}

	B->UnitFinished(unit,U);
	if( U->AIDisabled )
	{
		if( RAIDEBUGGING ) *l<<"#";
		return;
	}
	B->PM->UnitFinished(unit,U);
	SWM->UnitFinished(unit,U->udr);
	UM->UnitFinished(unit,U);
	if( U->ud->highTrajectoryType == 2 && rand()%4 == 0 )
	{	// Nothing too meanful, just a degree of randomness
		Command c;
		c.id = CMD_TRAJECTORY;
		c.params.push_back(1);
		cb->GiveOrder(unit,&c);
	}
	if( U->ud->speed == 0 )
		UnitIdle(unit);

	if( RAIDEBUGGING ) *l<<"#";
}

void cRAI::UnitDestroyed(int unit,int attacker)
{
	if( RAIDEBUGGING ) *l<<"\nUnitDestroyed("<<unit<<","<<attacker<<")";
	if( Units.find(unit) == Units.end() ) // Occurs if a player canceled a build order with more than one quaried and something was still being worked on
	{
		if( RAIDEBUGGING ) *l<<"#";
		return;
	}
	UnitInfo *U = &Units.find(unit)->second;
	if( U->UE != 0 )
		UpdateEventRemove(U->UE);
	if( !U->AIDisabled )
	{
		B->UnitDestroyed(unit,U);
		if( !U->unitBeingBuilt )
		{
			B->PM->UnitDestroyed(unit,U);
			SWM->UnitDestroyed(unit);
			UM->UnitDestroyed(unit,U);
		}
	}
	B->BP->UResourceDestroyed(unit,U);
	if( U->ud->speed == 0 )
	{
		for(map<int,UnitInfo*>::iterator i=U->UDefending.begin(); i!=U->UDefending.end(); ++i )
			i->second->UDefences.erase(unit);
		for(map<int,UnitInfo*>::iterator i=U->UDefences.begin(); i!=U->UDefences.end(); ++i )
			i->second->UDefending.erase(unit);
		UImmobile.erase(unit);
	}
	else
		UMobile.erase(unit);
	Units.erase(unit);
	if( RAIDEBUGGING ) *l<<"#";
}

void cRAI::EnemyEnterLOS(int enemy)
{
	if( RAIDEBUGGING ) *l<<"\nEnemyEnterLOS("<<enemy<<")";
	if( cb->GetUnitHealth(enemy) <= 0 ) // ! Work Around:  Spring-Version(v0.72b1-0.76b1)
	{
		DebugEnemyEnterLOSError++;
		*l<<"\nWARNING: EnemyEnterLOS("<<enemy<<"): enemy is either dead or not in LOS";
		return;
	}
	DebugEnemyEnterLOS++;
	if( Enemies.find(enemy) == Enemies.end() )
		Enemies.insert(iePair(enemy,EnemyInfo()));
	EnemyInfo *E = &Enemies.find(enemy)->second;
	E->inLOS=true;
	E->ud=cb->GetUnitDef(enemy);
	E->udr=&UDH->UDR.find(E->ud->id)->second;
	if( E->ud->speed == 0 )
	{
		E->position = cb->GetUnitPos(enemy);
		E->posLocked = true;
	}
	else if( E->posLocked )
	{	// just in case the id is being reused
		E->posLocked = false;
	}

	UM->EnemyEnterLOS(enemy,E);
	B->BP->EResourceEnterLOS(enemy,E);
	if( RAIDEBUGGING ) *l<<"#";
}

void cRAI::EnemyLeaveLOS(int enemy)
{
	if( RAIDEBUGGING ) *l<<"\nEnemyLeaveLOS("<<enemy<<")";
	if( Enemies.find(enemy) == Enemies.end() ) // ! Work Around:  Spring-Version(v0.72b1-0.76b1)
	{
		DebugEnemyLeaveLOSError++;
		*l<<"\nWARNING: EnemyLeaveLOS("<<enemy<<"): unknown unit id";
		return;
	}
	EnemyInfo *E = &Enemies.find(enemy)->second;
	if( !E->inLOS ) // Work Around:  Spring-Version(v0.76b1)
	{
		DebugEnemyLeaveLOSError++;
		*l<<"\nWARNING: EnemyLeaveLOS("<<enemy<<"): not in LOS";
		return;
	}

	DebugEnemyLeaveLOS++;
	E->inLOS=false;
	if( !E->inRadar )
	{
		if( !E->posLocked )
			E->position = cb->GetUnitPos(enemy);
		if( !TM->IsSectorValid(TM->GetSectorIndex(E->position)) )
			EnemyRemove(enemy,E);
	}
	if( RAIDEBUGGING ) *l<<"#";
}

void cRAI::EnemyEnterRadar(int enemy)
{
	if( RAIDEBUGGING ) *l<<"\nEnemyEnterRadar("<<enemy<<")";
	if( cb->GetUnitPos(enemy).x <= 0 && cb->GetUnitPos(enemy).y <= 0 && cb->GetUnitPos(enemy).z <= 0 ) // ! Work Around:  Spring-Version(v0.72b1-0.76b1)
	{
		DebugEnemyEnterRadarError++;
		*l<<"\nWARNING: EnemyEnterRadar("<<enemy<<"): enemy position is invalid";
		return;
	}
	DebugEnemyEnterRadar++;
	if( Enemies.find(enemy) == Enemies.end() )
		Enemies.insert(iePair(enemy,EnemyInfo()));
	EnemyInfo *E = &Enemies.find(enemy)->second;
	E->inRadar=true;
	UM->EnemyEnterRadar(enemy,E);
	if( RAIDEBUGGING ) *l<<"#";
}

void cRAI::EnemyLeaveRadar(int enemy)
{
	if( RAIDEBUGGING ) *l<<"\nEnemyLeaveRadar("<<enemy<<")";
	if( Enemies.find(enemy) == Enemies.end() ) // ! Work Around:  Spring-Version(v0.72b1-0.76b1)
	{
		DebugEnemyLeaveRadarError++;
		*l<<"\nWARNING: EnemyLeaveRadar("<<enemy<<"): unknown unit id";
		return;
	}
	EnemyInfo *E = &Enemies.find(enemy)->second;
	if( !E->inRadar ) // Work Around:  Spring-Version(v0.76b1)
	{
		DebugEnemyLeaveRadarError++;
		*l<<"\nWARNING: EnemyLeaveRadar("<<enemy<<"): not in radar";
		return;
	}

	DebugEnemyLeaveRadar++;
	E->inRadar=false;
	if( !E->inLOS )
	{
		if( !E->posLocked )
			E->position = cb->GetUnitPos(enemy);
		if( !TM->IsSectorValid(TM->GetSectorIndex(E->position)) )
			EnemyRemove(enemy,E);
	}
	if( RAIDEBUGGING ) *l<<"#";
}

void cRAI::EnemyDestroyed(int enemy,int attacker)
{
	if( RAIDEBUGGING ) *l<<"\nEnemyDestroyed("<<enemy<<","<<attacker<<")";
	if( Enemies.find(enemy) == Enemies.end() ) // ! Work Around:  Spring-Version(v0.72b1-0.76b1)
	{
		*l<<"\nWARNING: EnemyDestroyed("<<enemy<<","<<attacker<<"): unknown unit id";
		return;
	}

	EnemyInfo *E = &Enemies.find(enemy)->second;
	if( E->inLOS )
		DebugEnemyDestroyedLOS++;
	if( E->inRadar )
		DebugEnemyDestroyedRadar++;
	EnemyRemove(enemy,E);
	if( RAIDEBUGGING ) *l<<"#";
}

void cRAI::EnemyRemove(int enemy, EnemyInfo *E)
{
	if( E->RS != 0 && E->RS->unitID == enemy )
	{
		E->RS->unitID=-1;
		E->RS->unitUD=0;
		E->RS->enemy = false;
	}
	if( E->baseThreatID != -1 )
		EThreat.erase(enemy);
	while( int(E->attackGroups.size()) > 0 )
		UM->GroupRemoveEnemy(enemy,E,*E->attackGroups.begin());
	Enemies.erase(enemy);
}

void cRAI::EnemyDamaged(int damaged,int attacker,float damage,float3 dir)
{

}

void cRAI::UnitIdle(int unit)
{
	if( RAIDEBUGGING ) *l<<"\nUI("<<unit<<")";
	if( Units.find(unit) == Units.end() ) // ! Work Around:  Spring-Version(v0.72b1-0.76b1)
	{
		*l<<"\nWARNING: UnitIdle("<<unit<<"): unknown unit id";
		return;
	}
	UnitInfo *U = &Units.find(unit)->second;
	if( U->AIDisabled || cb->UnitBeingBuilt(unit) || cb->IsUnitParalyzed(unit) || int(cb->GetCurrentUnitCommands(unit)->size()) > 0 )
	{
		if( RAIDEBUGGING ) *l<<"#";
		return;
	}
	U->humanCommand=false;
	if( cb->GetCurrentFrame() <= U->lastUnitIdleFrame+15 ) // !! Occurs if enemy attack order fails/...possibly some other reason
	{
		UpdateEventAdd(1,cb->GetCurrentFrame()+15,unit,U);
		if( RAIDEBUGGING ) *l<<"#";
		return;
	}
	U->lastUnitIdleFrame=cb->GetCurrentFrame();
	if( U->UE != 0 && U->UE->type == 1 )
		UpdateEventRemove(U->UE);
	if( U->inCombat )
	{
		CM->UnitIdle(unit,U);
		if( RAIDEBUGGING ) *l<<"#";
		return;
	}
	UM->UnitIdle(unit,U);
	if( RAIDEBUGGING ) *l<<"#";
}


void cRAI::UnitDamaged(int unit,int attacker,float damage,float3 dir)
{
	if( RAIDEBUGGING ) *l<<"\nUnitDamaged("<<unit<<","<<attacker<<","<<damage<<",x"<<dir.x<<" z"<<dir.z<<" y"<<dir.y<<")";
	if( cb->UnitBeingBuilt(unit) || cb->IsUnitParalyzed(unit) || cb->GetUnitHealth(unit) <= 0.0 )
	{
		if( RAIDEBUGGING ) *l<<"#";
		return;
	}

	UnitInfo *U = &Units.find(unit)->second;
	if( cb->GetCurrentFrame() <= U->lastUnitDamagedFrame+15 )
	{
		if( RAIDEBUGGING ) *l<<"#";
		return;
	}

	U->lastUnitDamagedFrame=cb->GetCurrentFrame();

	EnemyInfo *E=0;
	if( Enemies.find(attacker) != Enemies.end() )
		E = &Enemies.find(attacker)->second;
	else
		attacker = -1;

	if( U->ud->speed==0 )
	{
		if( E != 0 )
		{
			E->baseThreatFrame=cb->GetCurrentFrame();
			E->baseThreatID=unit;
			if( EThreat.find(attacker) == EThreat.end() )
			{
				EThreat.insert(iepPair(attacker,E));
				for( int i=0; i<UM->GroupSize; i++ )
					if( int(UM->Group[i]->Enemies.size()) == 0 && !UM->Group[i]->Units.begin()->second->ud->canLoopbackAttack )
						CM->GetClosestEnemy(cb->GetUnitPos(UM->Group[i]->Units.begin()->first),UM->Group[i]->Units.begin()->second);
			}
		}
		ValidateUnitList(&U->UGuards);
		for( map<int,UnitInfo*>::iterator i = U->UGuards.begin(); i != U->UGuards.end(); ++i )
		{
			if( int(i->second->URepair.size()) == 0 && !IsHumanControled(i->first,i->second) )
			{
				Command c;
				c.id = CMD_REPAIR;
				c.params.push_back(unit);
				cb->GiveOrder(i->first, &c);
			}
			if( i->second->URepair.find(unit) == i->second->URepair.end() )
				i->second->URepair.insert(iupPair(unit,U));
		}
		if( U->ud->canReclaim && attacker > -1 && U->udr->IsNano() && cb->GetUnitPos(unit).distance2D(cb->GetUnitPos(attacker)) < U->ud->buildDistance && !IsHumanControled(unit,U) )
		{
			if( cb->GetCurrentUnitCommands(unit)->size() == 0 || (cb->GetCurrentUnitCommands(unit)->front().id != CMD_REPAIR && cb->GetCurrentUnitCommands(unit)->front().id != CMD_RECLAIM) )
			{
				Command c;
				c.id = CMD_RECLAIM;
				c.params.push_back(attacker);
				cb->GiveOrder(unit, &c);
			}
		}
	}
	if( U->AIDisabled || U->udrBL->task<=0 || U->ud->speed==0 )
	{
		if( RAIDEBUGGING ) *l<<"#";
		return;
	}

	if( E != 0 && U->group != 0 && U->group->Enemies.find(attacker) == U->group->Enemies.end() )
	{
		if( E->baseThreatID != -1 || UM->ActiveAttackOrders() )
			UM->GroupAddEnemy(attacker,E,U->group);
	}

	if( !IsHumanControled(unit,U) )
		CM->UnitDamaged(unit,U,attacker,E,dir);

	if( RAIDEBUGGING ) *l<<"#";
}

void cRAI::UnitMoveFailed(int unit)
{
	if( RAIDEBUGGING ) *l<<"\nUnitMoveFailed("<<unit<<")";
	if( UMobile.find(unit) == UMobile.end() ) // ! Work Around:  Spring-Version(v0.72b1-0.76b1)
	{
		*l<<"\nWARNING: UnitMoveFailed("<<unit<<"): unknown unit id";
		return;
	}

	UnitInfo *U = UMobile.find(unit)->second;
	if( U->AIDisabled || cb->IsUnitParalyzed(unit) ||
		B->UBuilderMoveFailed(unit,U) || UM->UnitMoveFailed(unit,U) ||
		int(cb->GetCurrentUnitCommands(unit)->size()) > 0 )
	{
		if( RAIDEBUGGING ) *l<<"#";
		return;
	}

	Command c;
	c.id=CMD_WAIT;
	//c.timeOut=cb->GetCurrentFrame()+120;
	cb->GiveOrder(unit,&c);
	UpdateEventAdd(1,cb->GetCurrentFrame()+90,unit,U);
	if( RAIDEBUGGING ) *l<<"#";
}

int cRAI::HandleEvent(int msg,const void* data)
{
	if( RAIDEBUGGING ) *l<<"\nHandleEvent("<<msg<<","<<"~"<<")";
	switch (msg)
	{
	case AI_EVENT_UNITGIVEN:
	case AI_EVENT_UNITCAPTURED:
		{
			const IGlobalAI::ChangeTeamEvent* cte = (const IGlobalAI::ChangeTeamEvent*) data;

			const int myAllyTeamId = cb->GetMyAllyTeam();
			const bool oldEnemy = !cb->IsAllied(myAllyTeamId, cb->GetTeamAllyTeam(cte->oldteam));
			const bool newEnemy = !cb->IsAllied(myAllyTeamId, cb->GetTeamAllyTeam(cte->newteam));

			if ( oldEnemy && !newEnemy ) {
			{
				if( Enemies.find(cte->unit) != Enemies.end() )
					EnemyDestroyed(cte->unit,-1);
				}
			}
			else if( !oldEnemy && newEnemy )
			{
				// unit changed from an ally to an enemy team
				// we lost a friend! :(
				EnemyCreated(cte->unit);
				if (!cb->UnitBeingBuilt(cte->unit)) {
					EnemyFinished(cte->unit);
				}
			}

			if( cte->oldteam == cb->GetMyTeam() )
			{
				UnitDestroyed(cte->unit,-1);
			}
			else if( cte->newteam == cb->GetMyTeam() )
			{
				if( cb->GetUnitHealth(cte->unit) <= 0 ) // ! Work Around:  Spring-Version(v0.74b1-0.75b2)
				{
					*l<<"\nERROR: HandleEvent(AI_EVENT_(UNITGIVEN|UNITCAPTURED)): given unit is dead or does not exist";
					return 0;
				}
				UnitCreated(cte->unit, -1);
				Units.find(cte->unit)->second.AIDisabled=false;
				if( !cb->UnitBeingBuilt(cte->unit) )
				{
					UnitFinished(cte->unit);
					UnitIdle(cte->unit);
				}
			}
		}
		break;
	case AI_EVENT_PLAYER_COMMAND:
		{
			const IGlobalAI::PlayerCommandEvent* pce = (const IGlobalAI::PlayerCommandEvent*) data;
			bool ImportantCommand=false;
			if( pce->command.id < 0 )
				ImportantCommand = true;
			switch( pce->command.id )
			{
			case CMD_MOVE:
			case CMD_PATROL:
			case CMD_FIGHT:
			case CMD_ATTACK:
			case CMD_AREA_ATTACK:
			case CMD_GUARD:
			case CMD_REPAIR:
			case CMD_LOAD_UNITS:
			case CMD_UNLOAD_UNITS:
			case CMD_UNLOAD_UNIT:
			case CMD_RECLAIM:
			case CMD_DGUN:
			case CMD_RESTORE:
			case CMD_RESURRECT:
			case CMD_CAPTURE:
				ImportantCommand = true;
			}

			for( int i=0; i<int(pce->units.size()); i++ )
			{
				if( Units.find(pce->units.at(i)) == Units.end() ) // ! Work Around:  Spring-Version(v0.75b2)
				{
					*l<<"\nERROR: HandleEvent(AI_EVENT_PLAYER_COMMAND): unknown unit id="<<pce->units.at(i);
//					pce->units.erase(pce->units.begin()+i);
//					i--;
				}
				else if( ImportantCommand )
					Units.find(pce->units.at(i))->second.humanCommand = true;
			}
			if( ImportantCommand )
			{
				B->HandleEvent(pce);
			}
			else if( pce->command.id == CMD_SELFD )
			{
				for( vector<int>::const_iterator i=pce->units.begin(); i!=pce->units.end(); ++i )
					UnitDestroyed(*i,-1);
			}
		}
		break;
	}
	if( RAIDEBUGGING ) *l<<"#";
	return 0;
}

void cRAI::Update()
{
	frame=cb->GetCurrentFrame();
	if(frame%FUPDATE_MINIMAL)
		return;

	if( RAIDEBUGGING ) *l<<"\nUpdate("<<frame<<")";

	if(!(frame%FUPDATE_POWER))
	{	// Old Code, ensures a unit won't just go permanently idle, hopefully unnecessary in the future
		ValidateAllUnits();
		for(map<int,UnitInfo>::iterator iU=Units.begin(); iU!=Units.end(); ++iU)
			if( !cb->UnitBeingBuilt(iU->first) && !iU->second.AIDisabled && iU->second.udrBL->task > 1 &&
				frame > iU->second.lastUnitIdleFrame+FUPDATE_UNITS && iU->second.UE == 0 && cb->GetCurrentUnitCommands(iU->first)->size() == 0 )
			{
//				*l<<"\nWARNING: Unit was Idle  Name="<<iU->second.ud->humanName<<"("<<iU->first<<")";
				UnitIdle(iU->first);
			}
	}

	while( eventSize > 0 && eventList[0]->frame <= frame )
	{
		switch( eventList[0]->type )
		{
		case 1: // Checks for idleness
//			*l<<"\n(u1)";
			if( ValidateUnit(eventList[0]->unitID) ) // if false, the event will be removed elsewhere
			{
				if( !IsHumanControled(eventList[0]->unitID,eventList[0]->unitI) )
				{
//					*l<<" Stopping "<<eventList[0]->unitI->ud->humanName<<"("<<eventList[0]->unitID<<")";
					eventList[0]->unitI->lastUnitIdleFrame = -1;
					if( eventList[0]->unitI->ud->speed == 0 )
					{
						int unit = eventList[0]->unitID;
						UpdateEventRemove(eventList[0]);
						UnitIdle(unit);
					}
					else
					{	// doesn't seem to work for factories  Spring-Version(v0.76b1)
						Command c;
						c.id=CMD_STOP;
						cb->GiveOrder(eventList[0]->unitID, &c);
						UpdateEventRemove(eventList[0]);
					}
				}
				else
				{
					eventList[0]->frame = frame;
					UpdateEventReorderFirst();
				}
			}
			break;
		case 2: // Few Unit Monitoring
//			*l<<"\n(u2)";
			if( ValidateUnit(eventList[0]->unitID) ) // if false, the event will be removed elsewhere
			{
				if( !eventList[0]->unitI->inCombat &&
					!IsHumanControled(eventList[0]->unitID,eventList[0]->unitI) &&
					eventList[0]->unitI->BuildQ != 0 &&
					cb->GetCurrentUnitCommands(eventList[0]->unitID)->front().id < 0 )
				{
					if( eventList[0]->lastPosition == 0 )
						eventList[0]->lastPosition = new float3;
					float3 position = cb->GetUnitPos(eventList[0]->unitID);
					if( eventList[0]->unitI->BuildQ->creationID.size() > 0 )
					{
						eventList[0]->lastPosition->x = -1.0;
						float3 conPosition = cb->GetUnitPos(eventList[0]->unitI->BuildQ->creationID.front());
						if( abs(int(position.x-conPosition.x)) + 4.0 < 8.0*eventList[0]->unitI->ud->xsize/2.0 + 8.0*eventList[0]->unitI->BuildQ->creationUD->ud->xsize/2.0 &&
							abs(int(position.z-conPosition.z)) + 4.0 < 8.0*eventList[0]->unitI->ud->zsize/2.0 + 8.0*eventList[0]->unitI->BuildQ->creationUD->ud->zsize/2.0 )
						{	// most likely, the commander built something on top of himself
							Command c;
							c.id = CMD_RECLAIM;
							c.params.push_back(eventList[0]->unitI->BuildQ->creationID.front());
							cb->GiveOrder(eventList[0]->unitID,&c);
							if( B->BP->NeedResourceSite(eventList[0]->unitI->BuildQ->creationUD->ud) )
							{
								c.params.clear();
								c.id = CMD_MOVE;
								c.options = SHIFT_KEY;
								float3 newPos;
								if( position.x < conPosition.x )
									newPos.x = (position.x - ((position.x-conPosition.x)/position.distance2D(conPosition))*(8.0*eventList[0]->unitI->ud->xsize/2.0 +8.0*eventList[0]->unitI->BuildQ->creationUD->ud->xsize/2.0) );
								else
									newPos.x = (position.x + ((position.x-conPosition.x)/position.distance2D(conPosition))*(8.0*eventList[0]->unitI->ud->xsize/2.0 +8.0*eventList[0]->unitI->BuildQ->creationUD->ud->xsize/2.0) );
								if( position.z < conPosition.z )
									newPos.z = (position.z - ((position.z-conPosition.z)/position.distance2D(conPosition))*(8.0*eventList[0]->unitI->ud->zsize/2.0 +8.0*eventList[0]->unitI->BuildQ->creationUD->ud->zsize/2.0) );
								else
									newPos.z = (position.z + ((position.z-conPosition.z)/position.distance2D(conPosition))*(8.0*eventList[0]->unitI->ud->zsize/2.0 +8.0*eventList[0]->unitI->BuildQ->creationUD->ud->zsize/2.0) );
								CorrectPosition(newPos);
								c.params.push_back(newPos.x);
								c.params.push_back(newPos.y);
								c.params.push_back(newPos.z);
								cb->GiveOrder(eventList[0]->unitID,&c);
							}
						}
					}
					else if( position == *eventList[0]->lastPosition )
					{	// most likely, the commander is stuck at the starting point
//						if( eventList[0]->unitI->area == 0 && TM->udMobileType.find(eventList[0]->unitI->ud->id)->second != 0 )
//						{} // trapped forever
						eventList[0]->unitI->lastUnitIdleFrame = -1;
						Command c;
						c.id = CMD_MOVE;
						float f = (40.0+(rand()%401)/10.0);
						float3 newPos = position;
						if( rand()%2 == 0 )
							newPos.x += f;
						else
							newPos.x -= f;
						f = (40.0+(rand()%401)/10.0);
						if( rand()%2 == 0 )
							newPos.z += f;
						else
							newPos.z -= f;

						CorrectPosition(newPos);
						c.params.push_back(newPos.x);
						c.params.push_back(newPos.y);
						c.params.push_back(newPos.z);
						cb->GiveOrder(eventList[0]->unitID,&c);
						*eventList[0]->lastPosition = position;
					}
					else
						*eventList[0]->lastPosition = position;
				}
				if( Units.size() >= 10 )
				{
					if( eventList[0]->lastPosition != 0 )
						delete eventList[0]->lastPosition;
					UpdateEventRemove(eventList[0]);
				}
				else
					UpdateEventReorderFirst();
			}
			break;
		case 3: // Initiatization
//			*l<<"\n(u3)";
			if( frame >= 210 || ( cb->GetMetalIncome()>0 && cb->GetMetalIncome()<0.9*cb->GetMetalStorage() ) || ( cb->GetEnergyIncome()>0 && cb->GetEnergyIncome()<0.9*cb->GetEnergyStorage() ) )
			{
				*l<<"\nInitiated=true  Frame="<<frame<<" Metal-Income="<<cb->GetMetalIncome()<<" Energy-Income="<<cb->GetEnergyIncome()<<"\n";
				UpdateEventRemove(eventList[0]);
				B->UpdateUDRCost();
				for(map<int,UnitInfo>::iterator i=Units.begin(); i!=Units.end(); ++i )
					if( !i->second.AIDisabled  )
					{
						if( Units.size() < 10 && i->second.ud->movedata != 0 )
							UpdateEventAdd(2,frame+FUPDATE_MINIMAL,i->first,&i->second);
						if( cb->GetCurrentUnitCommands(i->first)->size() == 0 )
							UnitIdle(i->first);
					}
				B->bInitiated=true;
			}
			else
				UpdateEventReorderFirst();
			break;
		default:
			*l<<"(ERROR)";
			UpdateEventRemove(eventList[0]);
			break;
		}
	}

	if(!(frame%FUPDATE_POWER))
	{
		B->PM->Update();
		SWM->Update();
		if(!(frame%FUPDATE_BUILDLIST))
			B->UpdateUDRCost();
	}
	if( RAIDEBUGGING ) *l<<"#";
}

void cRAI::UpdateEventAdd(const int &eventType, const int &eventFrame, int uID, UnitInfo* uI)
{
	if( eventSize == 10000 )
	{
		*l<<"\nERROR: Event Maximum Reached.";
		return;
	}

	UpdateEvent* e = new UpdateEvent;
	if( uI != 0 )
	{
		if( uI->UE != 0 ) // The unit already has an event
		{
			if( eventType >= uI->UE->type ) // more or equally important
				UpdateEventRemove(uI->UE);	// remove the old
			else
			{
				delete e;
				return;
			}
		}
		uI->UE = e;
	}

	e->type = eventType;
	e->frame = eventFrame;
	e->unitID = uID;
	e->unitI = uI;
	e->lastPosition = 0;

	for(e->index = eventSize; e->index>0; e->index-- )
		if( e->frame < eventList[e->index-1]->frame )
		{
			eventList[e->index] = eventList[e->index-1];
			eventList[e->index]->index = e->index;
		}
		else
			break;

	eventList[e->index] = e;
	eventSize++;
}

void cRAI::UpdateEventReorderFirst()
{
	UpdateEvent* e = eventList[0];
	e->frame += FUPDATE_MINIMAL;
	while( e->index < eventSize-1 && eventList[e->index+1]->frame < e->frame )
	{
		eventList[e->index] = eventList[e->index+1];
		eventList[e->index]->index = e->index;
		e->index++;
	}
	eventList[e->index] = e;
}

void cRAI::UpdateEventRemove(UpdateEvent* e)
{
	if( e->unitI != 0 )
		e->unitI->UE = 0;
	eventSize--;
	while( e->index < eventSize )
	{
		eventList[e->index] = eventList[e->index+1];
		eventList[e->index]->index = e->index;
		e->index++;
	}
	delete e;
}

void cRAI::CorrectPosition(float3& position)
{
	if( position.x < 1 )
		position.x = 1;
	else if( position.x > 8*cb->GetMapWidth()-2 )
		position.x = 8*cb->GetMapWidth()-2;
	if( position.z < 1 )
		position.z = 1;
	else if( position.z > 8*cb->GetMapHeight()-2 )
		position.z = 8*cb->GetMapHeight()-2;
	position.y = cb->GetElevation(position.x,position.z);
}

TerrainMapArea* cRAI::GetCurrentMapArea(sRAIUnitDef* udr, float3& position)
{
	if( udr->mobileType == 0 ) // flying units & buildings
		return 0;

	// other mobile units & their factories
	int iS = TM->GetSectorIndex(position);
	if( !TM->IsSectorValid(iS) )
	{
		CorrectPosition(position);
		iS = TM->GetSectorIndex(position);
	}
	return udr->mobileType->sector[iS].area;
}

float3 cRAI::GetRandomPosition(TerrainMapArea* area)
{
	float3 Pos;
	if( area == 0 )
	{
		Pos.x=1.0 + rand()%7 + 8.0*(rand()%cb->GetMapWidth());
		Pos.z=1.0 + rand()%7 + 8.0*(rand()%cb->GetMapHeight());
		CorrectPosition(Pos);
		return Pos;
	}

	vector<int> Temp;
	for( map<int,TerrainMapAreaSector*>::iterator iS=area->sector.begin(); iS!=area->sector.end(); ++iS )
		Temp.push_back(iS->first);
	int iS=Temp.at(rand()%int(Temp.size()));
	Pos.x=TM->sector[iS].position.x - TM->convertStoP/2-1.0 + rand()%(TM->convertStoP-1);
	Pos.z=TM->sector[iS].position.z - TM->convertStoP/2-1.0 + rand()%(TM->convertStoP-1);
	CorrectPosition(Pos);
	return Pos;
}

bool cRAI::IsHumanControled(const int& unit,UnitInfo* U)
{
	if( int(cb->GetCurrentUnitCommands(unit)->size()) == 0 )
		return false;
	if( U->humanCommand )
		return true;
	return false;
}

bool cRAI::ValidateUnit(const int& unitID)
{
	if( cb->GetUnitDef(unitID) == 0 ) // ! Work Around:  Spring-Version(v0.74b1-0.75b2)
	{
		*l<<"\nERROR: ValidateUnit(): iU->first="<<unitID;
		UnitDestroyed(unitID,-1);
		return false;
	}
	return true;
}

bool cRAI::ValidateUnitList(map<int,UnitInfo*>* UL)
{
	int ULsize = UL->size();
	for(map<int,UnitInfo*>::iterator iU=UL->begin(); iU!=UL->end(); ++iU)
	{
		if( !ValidateUnit(iU->first) )
		{
			// The iterator has becomes invalid at this point
			if( ULsize == 1 ) // if true then the list is now empty, and may have been deleted (UL is invalid)
				return false;
			else
				return ValidateUnitList(UL);
		}
	}
	return true;
}

void cRAI::ValidateAllUnits()
{
	for(map<int,UnitInfo>::iterator iU=Units.begin(); iU!=Units.end(); ++iU)
	{
		if( !ValidateUnit(iU->first) )
		{
			// The iterator has becomes invalid at this point
			ValidateAllUnits();
			return;
		}
	}
}

void cRAI::DebugDrawLine(float3 StartPos, float distance, int direction, float xposoffset, float zposoffset, float yposoffset, int lifetime, int arrow, float width, int group)
{
	StartPos.x+=xposoffset;
	StartPos.z+=zposoffset;
	StartPos.y+=yposoffset;
	float3 EndPos=StartPos;
	switch( direction )
	{
	case 0:
			EndPos.x+=distance;
		break;
	case 1:
			EndPos.z+=distance;
		break;
	case 2:
			EndPos.x-=distance;
		break;
	case 3:
			EndPos.z-=distance;
		break;
	}
	cb->CreateLineFigure(StartPos, EndPos, width, arrow, lifetime, group);
}

void cRAI::DebugDrawShape(float3 centerPos, float lineLength, float width, int arrow, float yPosOffset, int lifeTime, int sides, int group)
{
	DebugDrawLine(centerPos, lineLength, 0, -lineLength/2,  lineLength/2, yPosOffset, lifeTime, arrow, width, group);
	DebugDrawLine(centerPos, lineLength, 1, -lineLength/2, -lineLength/2, yPosOffset, lifeTime, arrow, width, group);
	DebugDrawLine(centerPos, lineLength, 2,  lineLength/2, -lineLength/2, yPosOffset, lifeTime, arrow, width, group);
	DebugDrawLine(centerPos, lineLength, 3,  lineLength/2,  lineLength/2, yPosOffset, lifeTime, arrow, width, group);
}

bool cRAI::LocateFile(IAICallback* cb, const string& relFileName, string& absFileName, bool forWriting) 
{
	int action = forWriting ? AIVAL_LOCATE_FILE_W : AIVAL_LOCATE_FILE_R;

	char absFN[2048];
	STRCPY_T(absFN, sizeof(absFN), relFileName.c_str());
	const bool located = cb->GetValue(action, absFN);

	if (located) {
		absFileName = absFN;
	} else {
		absFileName = "";
	}

	return located;
}

static bool IsFSGoodChar(const char c) {

	if ((c >= '0') && (c <= '9')) {
		return true;
	} else if ((c >= 'a') && (c <= 'z')) {
		return true;
	} else if ((c >= 'A') && (c <= 'Z')) {
		return true;
	} else if ((c == '.') || (c == '_') || (c == '-')) {
		return true;
	}

	return false;
}
std::string cRAI::MakeFileSystemCompatible(const std::string& str) {

	std::string cleaned = str;

	for (std::string::size_type i=0; i < cleaned.size(); i++) {
		if (!IsFSGoodChar(cleaned[i])) {
			cleaned[i] = '_';
		}
	}

	return cleaned;
}

void cRAI::RemoveLogFile(string relFileName) const {

	string absFileName;
	if (cRAI::LocateFile(cb, relFileName, absFileName, true)) {
		remove(absFileName.c_str());
	}
}

std::string cRAI::GetLogFileSubPath(int teamId) const {

	static const size_t logFileSubPath_sizeMax = 64;
	char logFileSubPath[logFileSubPath_sizeMax];
	SNPRINTF(logFileSubPath, logFileSubPath_sizeMax, "log/RAI%i_LastGame.log", teamId);
	return std::string(logFileSubPath);
}

void cRAI::ClearLogFiles()
{
	for( int t=0; t < 255; t++ )
	{
		RemoveLogFile(GetLogFileSubPath(t));
	}

	RemoveLogFile("log/RAIGlobal_LastGame.log");
	RemoveLogFile("log/TerrainMapDebug.log");
//	RemoveLogFile("log/PathfinderDebug.log");
//	RemoveLogFile("log/PathFinderAPNDebug.log");
//	RemoveLogFile("log/PathFinderNPNDebug.log");
//	RemoveLogFile("log/Prerequisite.log");
//	RemoveLogFile("log/Debug.log");
}