-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyStrategy.cs
6159 lines (5729 loc) · 284 KB
/
MyStrategy.cs
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
using Aicup2020.Model;
using System.Collections.Generic;
namespace Aicup2020
{
public class MyStrategy
{
Dictionary<int, EntityMemory> entityMemories = new Dictionary<int, EntityMemory>();
#region Служебные переменные
EntityType[] entityTypesArray = { EntityType.BuilderUnit, EntityType.RangedUnit, EntityType.MeleeUnit,
EntityType.Turret, EntityType.House, EntityType.BuilderBase, EntityType.MeleeBase, EntityType.RangedBase, EntityType.Wall,
EntityType.Resource };
bool needPrepare = true;
#endregion
#region клетки где можно построить юнита вокруг здания
int largeBuildingSize = 5;
int[] buildingPositionDX = { -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0 };
int[] buildingPositionDY = { 0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0, -1, -1, -1, -1, -1 };
int[] buildingPositionIter = { 0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8, -8, 9, -9, 10 };
// позиции расположены вот так
// 5 6 7 8 9
// 4 10
// 3 11
// 2 12
// 1 13
// 0 X 14
// 19 18 17 16 15
#endregion
Dictionary<EntityType, Group> basicEntityIdGroups = new Dictionary<EntityType, Group>();
Group groupHouseBuilders = new Group();
Group groupRepairBuilders = new Group();
Group groupRetreatBuilders = new Group();
Group groupMyBuildersAttackEnemyBuilders = new Group();
List<int> needRepairBuildingIdList = new List<int>();
List<int> needRepairUnitsIdList = new List<int>();
enum DebugOptions
{
canDrawGetAction, drawBuildBarrierMap, drawBuildAndRepairOrder, drawBuildAndRepairPath, drawRetreat,
drawOnceVisibleMap, drawRangedBasePotencPlace,
drawPotencAttack, drawOptAttack,
canDrawDebugUpdate, allOptionsCount
}
bool[] debugOptions = new bool[(int)DebugOptions.allOptionsCount];
PlayerView _playerView;
DebugInterface _debugInterface;
public IDictionary<EntityType, EntityProperties> properties;
System.Random random = new System.Random();
int[][] cellWithIdOnlyBuilding;
int[][] cellWithIdAny;
int[][] nextPositionMyUnitsMap;
int[][] onceVisibleMap;
bool[][] currentVisibleMap;
int[][] resourceMemoryMap;
int[][] resourcePotentialField;
const int RPFmyBuildingWeight = -10;
const int RPFdangerCellWeight = -6;
const int RPFenemyEntityWeight = -2;
const int RPFdeniedBuilderWeight = 1;
const int RPFwarningCellWeight = 2;
class BuildMapCell
{
public bool s2canBuildNow;
public bool s2canBuildAfter;
public bool s2noBaseOrWarriorBarrier;
public bool s2noBuilderBarrier;
public bool s2noEnemiesBarrier;
public bool s2noUnvisivleBarrier;
public int s2howManyResBarrier;
public bool s3canBuildNow;
public bool s3canBuildAfter;
public bool s3noBaseOrWarriorBarrier;
public bool s3noBuilderBarrier;
public bool s3noEnemiesBarrier;
public bool s3noUnvisivleBarrier;
public int s3howManyResBarrier;
public bool s5canBuildNow;
public bool s5canBuildAfter;
public bool s5noBaseOrWarriorBarrier;
public bool s5noBuilderBarrier;
public bool s5noEnemiesBarrier;
public bool s5noUnvisivleBarrier;
public int s5howManyResBarrier;
public BuildMapCell()
{
Reset();
}
public void Check()
{
if (s2noBaseOrWarriorBarrier
&& s2noEnemiesBarrier
&& s2howManyResBarrier == 0
&& s2noUnvisivleBarrier)
{
s2canBuildAfter = true;
if (s2noBuilderBarrier)
{
s2canBuildNow = true;
}
}
if (s3noBaseOrWarriorBarrier
&& s3noEnemiesBarrier
&& s3howManyResBarrier == 0
&& s3noUnvisivleBarrier)
{
s3canBuildAfter = true;
if (s3noBuilderBarrier)
{
s3canBuildNow = true;
}
}
if (s5noBaseOrWarriorBarrier
&& s5noEnemiesBarrier
&& s5howManyResBarrier == 0
&& s5noUnvisivleBarrier)
{
s5canBuildAfter = true;
if (s5noBuilderBarrier)
{
s5canBuildNow = true;
}
}
}
public void Reset()
{
s2canBuildNow = s3canBuildNow = s5canBuildNow = false;
s2canBuildAfter = s3canBuildAfter = s5canBuildAfter = false;
s2noBaseOrWarriorBarrier = s3noBaseOrWarriorBarrier = s5noBaseOrWarriorBarrier = true;
s2noBuilderBarrier = s3noBuilderBarrier = s5noBuilderBarrier = true;
s2noEnemiesBarrier = s3noEnemiesBarrier = s5noEnemiesBarrier = true;
s2howManyResBarrier = s3howManyResBarrier = s5howManyResBarrier = 0;
s2noUnvisivleBarrier = s3noUnvisivleBarrier = s5noUnvisivleBarrier = true;
}
public int HowManyResBarrier(int size)
{
switch (size)
{
case 2: return s2howManyResBarrier;
case 3: return s3howManyResBarrier;
case 5: return s5howManyResBarrier;
default:
break;
}
return 0;
}
public bool CanBuildNow(int size)
{
switch (size)
{
case 2: return s2canBuildNow;
case 3: return s3canBuildNow;
case 5: return s5canBuildNow;
default:
break;
}
return false;
}
public bool CanBuildAfter(int size)
{
switch (size)
{
case 2: return s2canBuildAfter;
case 3: return s3canBuildAfter;
case 5: return s5canBuildAfter;
default:
break;
}
return false;
}
}
BuildBarrierMap buildBarrierMap;
class BuildBarrierMap
{
BuildMapCell[,] _buildBarrierMap;
int _mapSize;
public BuildBarrierMap(int size)
{
_mapSize = size;
_buildBarrierMap = new BuildMapCell[_mapSize, _mapSize];
for (int x = 0; x < _mapSize; x++)
{
for (int y = 0; y < _mapSize; y++)
{
_buildBarrierMap[x, y] = new BuildMapCell();
}
}
}
public void Reset()
{
for (int x = 0; x < _mapSize; x++)
{
for (int y = 0; y < _mapSize; y++)
{
_buildBarrierMap[x, y].Reset();
}
}
}
public enum BlockVariant {Enemy, Builder, MyBuilding, Unvisible };
public void BlockCell(int x, int y, BlockVariant variant)
{
for (int dx = -4; dx <= 0; dx++)
{
for (int dy = -4; dy <= 0; dy++)
{
bool s2 = dx > -2 && dy > -2;
bool s3 = dx > -3 && dy > -3;
bool s5 = true;
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < _mapSize && ny >= 0 && ny < _mapSize)
{
switch (variant)
{
case BlockVariant.Enemy:
if (s2) _buildBarrierMap[nx, ny].s2noEnemiesBarrier = false;
if (s3) _buildBarrierMap[nx, ny].s3noEnemiesBarrier = false;
if (s5) _buildBarrierMap[nx, ny].s5noEnemiesBarrier = false;
break;
case BlockVariant.Builder:
if (s2) _buildBarrierMap[nx, ny].s2noBuilderBarrier = false;
if (s3) _buildBarrierMap[nx, ny].s3noBuilderBarrier = false;
if (s5) _buildBarrierMap[nx, ny].s5noBuilderBarrier = false;
break;
case BlockVariant.MyBuilding:
if (s2) _buildBarrierMap[nx, ny].s2noBaseOrWarriorBarrier = false;
if (s3) _buildBarrierMap[nx, ny].s3noBaseOrWarriorBarrier = false;
if (s5) _buildBarrierMap[nx, ny].s5noBaseOrWarriorBarrier = false;
break;
case BlockVariant.Unvisible:
if (s2) _buildBarrierMap[nx, ny].s2noUnvisivleBarrier = false;
if (s3) _buildBarrierMap[nx, ny].s3noUnvisivleBarrier = false;
if (s5) _buildBarrierMap[nx, ny].s5noUnvisivleBarrier = false;
break;
default:
throw new System.Exception("unknown variant");
}
}
}
}
}
public void AddResource(int x, int y)
{
for (int dx = -4; dx <= 0; dx++)
{
for (int dy = -4; dy <= 0; dy++)
{
bool s2 = dx > -2 && dy > -2;
bool s3 = dx > -3 && dy > -3;
bool s5 = true;
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < _mapSize && ny >= 0 && ny < _mapSize)
{
if (s2) _buildBarrierMap[nx, ny].s2howManyResBarrier++;
if (s3) _buildBarrierMap[nx, ny].s3howManyResBarrier++;
if (s5) _buildBarrierMap[nx, ny].s5howManyResBarrier++;
}
}
}
}
public BuildMapCell this[int x, int y]
{
get
{
if (x >= 0 && x < _mapSize && y >= 0 && y < _mapSize)
return _buildBarrierMap[x, y];
else
return null;
}
set
{
if (x >= 0 && x < _mapSize && y >= 0 && y < _mapSize)
_buildBarrierMap[x, y] = value;
}
}
}
//List<Vec2Int> preSetHousePositions;
//bool preSetHousePlacingComplete = false;
struct EnemyDangerCell
{
public byte rangersWarning; // могут подойти в следующий ход
public byte rangersAim; // могут атаковать сейчас
public byte turretsAim;
public byte meleesWarning;
public byte meleesAim;
public byte buildersWarning;
public byte buildersAim;
//public EnemyDangerCell(EnemyDangerCell cell)
//{
// rangersWarning = cell.rangersAim; // могут подойти в следующий ход
// rangersAim = cell.rangersAim; // могут атаковать сейчас
// turretsAim = cell.;
// meleesWarning = 0;
// meleesAim = 0;
// builderWarning = 0;
// builderAim = 0;
//}
}
EnemyDangerCell[][] enemyDangerCells;
class PotencAttackCell
{
public int dist5low;
public int dist6;
public int dist7;
public int dist8;
public bool drawn;
public PotencAttackCell()
{
Reset();
}
public void Reset()
{
dist5low = dist6 = dist7 = dist8 = 0;
drawn = false;
}
public bool TryDraw()
{
if (drawn == false)
{
drawn = true;
return true;
}
return false;
}
}
class PotencAttackMap
{
PotencAttackCell[,] _potencAttackMap;
int _mapSize;
public PotencAttackMap(int mapS)
{
_mapSize = mapS;
_potencAttackMap = new PotencAttackCell[_mapSize, _mapSize];
for (int x = 0; x < _mapSize; x++)
{
for (int y = 0; y < _mapSize; y++)
{
_potencAttackMap[x, y] = new PotencAttackCell();
}
}
}
public PotencAttackCell this[int x, int y]
{
get
{
if (x >= 0 && x < _mapSize && y >= 0 && y < _mapSize)
return _potencAttackMap[x, y];
else
return null;
}
}
public void Reset()
{
for (int x = 0; x < _mapSize; x++)
{
for (int y = 0; y < _mapSize; y++)
{
_potencAttackMap[x, y].Reset();
}
}
}
public void AddCell(int x, int y, int d5, int d6, int d7, int d8)
{
if (x >= 0 && x < _mapSize && y >= 0 && y < _mapSize)
{
_potencAttackMap[x, y].dist5low += d5;
_potencAttackMap[x, y].dist6 += d6;
_potencAttackMap[x, y].dist7 += d7;
_potencAttackMap[x, y].dist8 += d8;
}
}
public void AddCell(int x, int y, int dist, bool increment)
{
if (x >= 0 && x < _mapSize && y >= 0 && y < _mapSize)
{
if (increment)
{
if (dist <= 5) _potencAttackMap[x, y].dist5low++;
else if (dist == 6) _potencAttackMap[x, y].dist6++;
else if (dist == 7) _potencAttackMap[x, y].dist7++;
else if (dist == 8) _potencAttackMap[x, y].dist8++;
}
else
{
if (dist <= 5) _potencAttackMap[x, y].dist5low--;
else if (dist == 6) _potencAttackMap[x, y].dist6--;
else if (dist == 7) _potencAttackMap[x, y].dist7--;
else if (dist == 8) _potencAttackMap[x, y].dist8--;
}
}
}
public bool TryDraw(int x, int y)
{
if (x >= 0 && x < _mapSize && y >= 0 && y < _mapSize)
{
return _potencAttackMap[x, y].TryDraw();
}
return false;
}
}
PotencAttackMap potencAttackMap;
float buildBuildingDistCoef = 0.3f; // коэффициент как далеко мы ищем строителей для помощи 0,3 = 30% от расчетного времени строительства (меньше ищем)
float repairBuildingDistCoef = 0.5f; // аналогично но для ремонта
int buildTurretThenResourcesOver = 720;
Vec2Int rangedBasePotencPlace1;
Vec2Int rangedBasePotencPlace2;
//int builderCountForStartBuilding = 3; // количество ближайших свободных строителей которое ищется при начале строительства
//float startBuildingFindDistanceFromHealth = 0.4f; // дистанция поиска строителей как процент здоровья
#region Статистические переменные
Dictionary<Model.EntityType, int> currentMyEntityCount = new Dictionary<Model.EntityType, int>();
Dictionary<Model.EntityType, int> previousEntityCount = new Dictionary<Model.EntityType, int>();
Dictionary<Model.EntityType, float> buildEntityPriority = new Dictionary<Model.EntityType, float>();
Dictionary<int, Entity> enemiesById = new Dictionary<int, Entity>();
int howMuchResourcesIHaveNextTurn = 0;
int nextTurnResourcesSelectCount = 5;
int nextTurnResourcesBonus = 1;
int howMuchResourcesCollectLastTurn = 0;
const int howManyTurnsHistory = 30;
int[] howMuchResourcesCollectLastNTurns = new int[howManyTurnsHistory];
int[] howMuchResourcesCollectCPALastNTurns = new int[howManyTurnsHistory];
int[] howMuchLiveBuildersLast10Turns = new int[howManyTurnsHistory];
int howMuchResourcesCollectAll = 0;
bool iHaveActiveRangedBase = false;
bool iHaveActiveBuilderBase = false;
bool opponentHasResourcesForRangersBase = false;
int myResources;
int myScore;
int myId;
int mapSize;
int populationMax = 0;
int populationUsing = 0;
bool fogOfWar;
#endregion
#region Желания, Планы, Намерения и т.д.
enum DesireType
{
WantCreateBuilders, WantCreateRangers,
WantCreateHouses, WantCreateRangerBase, WantCreateTurret,
WantRepairBuildings,
WantCollectResources, WantRetreatBuilders,
WantTurretAttacks, WantAllWarriorsAttack
};
List<DesireType> desires = new List<DesireType>();
List<DesireType> prevDesires = new List<DesireType>();
enum PlanType
{
PlanCreateBuilders, PlanCreateRangers,
PlanCreateHouses, PlanCreateRangerBase, PlanCreateTurret,
PlanRepairNewBuildings, PlanRepairOldBuildings,
PlanExtractResources, PlanRetreatBuilders,
PlanTurretAttacks, PlanAllWarriorsAttack
}
List<PlanType> plans = new List<PlanType>();
List<PlanType> prevPlans = new List<PlanType>();
enum IntentionType
{
IntentionCreateBuilder, IntentionStopCreatingBuilder,
IntentionCreateRanger, IntentionStopCreatingRanger,
IntentionCreateHouse, IntentionCreateRangedBase, IntentionCreateTurret,
IntentionRepairNewBuilding, IntentionRepairOldBuilding,
IntentionExtractResources, IntentionFindResources, IntentionRetreatBuilders, IntentionMyBuiAttackEnemyBui,
IntentionTurretAttacks,
IntentionAllWarriorsAttack
}
class Intention
{
public IntentionType intentionType;
public int targetId;
public Group targetGroup;
public Vec2Int targetPosition;
public EntityType targetEntityType;
public Intention(IntentionType type, Vec2Int pos, EntityType _entityType)
{
intentionType = type;
targetId = -1;
targetGroup = new Group();
targetPosition = pos;
targetEntityType = _entityType;
}
public Intention(IntentionType type, int _targetId)
{
intentionType = type;
targetId = _targetId;
targetGroup = new Group();
targetPosition = new Vec2Int();
targetEntityType = EntityType.Resource;
}
public Intention(IntentionType type, Group _targetGroup)
{
intentionType = type;
targetId = -1;
targetGroup = _targetGroup;
targetPosition = new Vec2Int();
targetEntityType = EntityType.Resource;
}
}
List<Intention> intentions = new List<Intention>();
List<Intention> prevIntentions = new List<Intention>();
Dictionary<int, EntityAction> actions = new Dictionary<int, EntityAction>();
#endregion
struct DebugLine
{
public float _x1;
public float _x2;
public float _y1;
public float _y2;
public Color _color1;
public Color _color2;
public DebugLine(float x1, float y1, float x2, float y2, Color color1, Color color2)
{
_x1 = x1;
_y1 = y1;
_x2 = x2;
_y2 = y2;
_color1 = color1;
_color2 = color2;
}
}
List<DebugLine> debugLines = new List<DebugLine>();
public Action GetAction(PlayerView playerView, DebugInterface debugInterface)
{
_playerView = playerView;
_debugInterface = debugInterface;
#region first initialization arrays and lists (once)
if (needPrepare == true)
{
//once prepare variables and struct
myId = _playerView.MyId;
mapSize = _playerView.MapSize;
properties = _playerView.EntityProperties;
needPrepare = false;
fogOfWar = _playerView.FogOfWar;
Prepare();
}
#endregion
#region calc statistics and informations
foreach (var p in _playerView.Players)
{
if (p.Id == myId)
{
howMuchResourcesCollectLastTurn = p.Resource - myResources; // не учитывает стоимость произведенных сущностей
myResources = p.Resource;
myScore = p.Score;
break;
}
}
CountNumberOfEntitiesAndMap();
CheckAliveAndDieEntities();
CheckDeadResourceOnCurrentVisibleMap();
GenerateResourcePotentialField();
GenerateBuildBarrierMap();
GeneratePotencAttackMap();
SelectRangedBasePotencPlace();
iHaveActiveRangedBase = false;
foreach (var id in basicEntityIdGroups[EntityType.RangedBase].members)
{
if (entityMemories[id].myEntity.Active == true)
{
iHaveActiveRangedBase = true;
break;
}
}
iHaveActiveBuilderBase = false;
foreach (var id in basicEntityIdGroups[EntityType.BuilderBase].members)
{
if (entityMemories[id].myEntity.Active == true)
{
iHaveActiveBuilderBase = true;
break;
}
}
#endregion
debugLines.Clear();
GenerateDesires(); // Желания - Что я хочу сделать?
ConvertDesiresToPlans(); // Планы - Какие из желаний я могу сейчас сделать?
PrepareBeforeCreateIntentions(); //Подготовительные меропрития перед созданиями намерений
ConvertPlansToIntentions(); // Намерения - Как и кем я буду выполнять планы?
CorrectCrossIntentions();// Проверяем взаимоискулючающие и противоречащие намерения. Оставляем только нужные.
ConvertIntentionsToOrders(); // определяем конкретные приказы для каждой сущности
OptimizeOrders(); // корректируем приказы, чтобы не было столкновений
ConvertOrdersToActions(); // Приказы - Кто будет выполнять намерения? //приказы превращаются в конкретные action для entities
SaveEntitiesMemory();
if (debugOptions[(int)DebugOptions.canDrawGetAction])
{
if (debugOptions[(int)DebugOptions.drawPotencAttack] == true)
DrawPotencMap(3);
if (debugOptions[(int)DebugOptions.drawOnceVisibleMap] == true)
{
for (int x = 0; x < mapSize; x++)
{
for (int y = 0; y < mapSize; y++)
{
ColoredVertex position = new ColoredVertex(new Vec2Float(x + 0.5f, y + 0.3f), new Vec2Float(0, 0), colorRed);
debugInterface.Send(new DebugCommand.Add(new DebugData.PlacedText(position, onceVisibleMap[x][y].ToString(), 0, 16)));
//if (onceVisibleMap[x][y] == 0)
//{
// ColoredVertex position = new ColoredVertex(new Vec2Float(x + 0.5f, y+0.3f), new Vec2Float(0, 0), colorRed);
// debugInterface.Send(new DebugCommand.Add(new DebugData.PlacedText(position, "x", 0, 16)));
//}
}
}
}
if (debugOptions[(int)DebugOptions.drawRangedBasePotencPlace] == true)
{
int x1 = rangedBasePotencPlace1.X - 1;
int y1 = rangedBasePotencPlace1.Y - 1;
int size = 7;
DrawLineOnce(x1, y1, x1 + size, y1, colorWhite,colorWhite);
DrawLineOnce(x1 + size, y1, x1 + size, y1 + size, colorWhite, colorWhite);
DrawLineOnce(x1 + size, y1 + size, x1, y1 + size, colorWhite, colorWhite);
DrawLineOnce(x1, y1 + size, x1, y1, colorWhite, colorWhite);
x1 = rangedBasePotencPlace2.X - 1;
y1 = rangedBasePotencPlace2.Y - 1;
size = 7;
DrawLineOnce(x1, y1, x1 + size, y1, colorGreen, colorGreen);
DrawLineOnce(x1 + size, y1, x1 + size, y1 + size, colorGreen, colorGreen);
DrawLineOnce(x1 + size, y1 + size, x1, y1 + size, colorGreen, colorGreen);
DrawLineOnce(x1, y1 + size, x1, y1, colorGreen, colorGreen);
//for (int x = 0; x < mapSize; x++)
//{
// for (int y = 0; y < mapSize; y++)
// {
// //int res = buildBarrierMap[x, y].HowManyResBarrier(5);
// //if (res > 0)
// //{
// // ColoredVertex position = new ColoredVertex(new Vec2Float(x + 0.5f, y + 0.3f), new Vec2Float(0, 0), colorRed);
// // debugInterface.Send(new DebugCommand.Add(new DebugData.PlacedText(position, res.ToString(), 0.5f, 16)));
// //}
// if (buildBarrierMap[x, y].s5noUnvisivleBarrier == false)
// {
// ColoredVertex position = new ColoredVertex(new Vec2Float(x + 0.5f, y + 0.3f), new Vec2Float(0, 0), colorRed);
// debugInterface.Send(new DebugCommand.Add(new DebugData.PlacedText(position, "x", 0.5f, 16)));
// }
// }
//}
}
_debugInterface.Send(new DebugCommand.Flush());
}
return new Action(actions);
}
void Prepare()
{
#region draw debug settings
debugOptions[(int)DebugOptions.canDrawGetAction] = true;
debugOptions[(int)DebugOptions.drawRetreat] = true;
debugOptions[(int)DebugOptions.drawBuildBarrierMap] = false;
debugOptions[(int)DebugOptions.drawOnceVisibleMap] = false;
debugOptions[(int)DebugOptions.drawBuildAndRepairOrder] = true;
debugOptions[(int)DebugOptions.drawBuildAndRepairPath] = false;
debugOptions[(int)DebugOptions.drawPotencAttack] = false;
debugOptions[(int)DebugOptions.drawRangedBasePotencPlace] = true;
debugOptions[(int)DebugOptions.drawOptAttack] = true;
debugOptions[(int)DebugOptions.canDrawDebugUpdate] = false;
if (_debugInterface == null)
{
debugOptions[(int)DebugOptions.canDrawGetAction] = false;
debugOptions[(int)DebugOptions.canDrawDebugUpdate] = false; // отображение отладочной информации на стадии debugUpdate
}
else
{
_debugInterface.Send(new DebugCommand.Clear());
_debugInterface.Send(new DebugCommand.SetAutoFlush(false));
}
#endregion
#region init arrays
cellWithIdAny = new int[mapSize][];
cellWithIdOnlyBuilding = new int[mapSize][];
onceVisibleMap = new int[mapSize][];
currentVisibleMap = new bool[mapSize][];
resourceMemoryMap = new int[mapSize][];
enemyDangerCells = new EnemyDangerCell[mapSize][];
resourcePotentialField = new int[mapSize][];
nextPositionMyUnitsMap = new int[mapSize][];
for (var i = 0; i < mapSize; i++)
{
cellWithIdAny[i] = new int[mapSize];
cellWithIdOnlyBuilding[i] = new int[mapSize];
onceVisibleMap[i] = new int[mapSize];
currentVisibleMap[i] = new bool[mapSize];
resourceMemoryMap[i] = new int[mapSize];
enemyDangerCells[i] = new EnemyDangerCell[mapSize];
resourcePotentialField[i] = new int[mapSize];
nextPositionMyUnitsMap[i] = new int[mapSize];
// auto zeroing all when created
}
potencAttackMap = new PotencAttackMap(mapSize);
buildBarrierMap = new BuildBarrierMap(mapSize);
#endregion
if (!fogOfWar)
{
for (int x = 0; x < mapSize; x++)
{
for (int y = 0; y < mapSize; y++)
{
onceVisibleMap[x][y] = 20;
currentVisibleMap[x][y] = true;
}
}
}
//preSetHousePositions = new List<Vec2Int>();
//preSetHousePositions.Add(new Vec2Int(2, 2));
//preSetHousePositions.Add(new Vec2Int(5, 2));
//preSetHousePositions.Add(new Vec2Int(8, 2));
//preSetHousePositions.Add(new Vec2Int(2, 5));
//preSetHousePositions.Add(new Vec2Int(2, 8));
//prepare current and previous entity counter
foreach (var ent in entityTypesArray)
{
currentMyEntityCount.Add(ent, 0);
previousEntityCount.Add(ent, 0);
buildEntityPriority.Add(ent, 0f);
basicEntityIdGroups.Add(ent, new Group());
}
}
void CheckAliveAndDieEntities()
{
needRepairBuildingIdList.Clear();
needRepairUnitsIdList.Clear();
int currentTick = _playerView.CurrentTick;
//zeroing visible map all only with for of war
if (fogOfWar)
{
for (int x = 0; x < mapSize; x++)
currentVisibleMap[x] = new bool[mapSize];
}
// zero enemies dictionary
enemiesById.Clear();
// zero enemy danger cells
for (var x = 0; x < mapSize; x++)
{
for (var y = 0; y < mapSize; y++)
{
enemyDangerCells[x][y] = new EnemyDangerCell();
}
}
//uncheck memory
foreach (var m in entityMemories)
{
m.Value.checkedNow = false;
}
//update and add live entity
foreach (var e in _playerView.Entities)
{
//use only my entities
if (e.PlayerId == myId)
{
if (entityMemories.ContainsKey(e.Id))
{
//update
entityMemories[e.Id].Update(e);
//once and current visible map update
AddEntityViewToOnceVisibleMap(e.EntityType, e.Position.X, e.Position.Y);
AddEntityViewToCurrentVisibleMap(e.EntityType, e.Position.X, e.Position.Y);
}
else
{
//add my entity
var em = new EntityMemory(e);
em.SetGroup(basicEntityIdGroups[e.EntityType]);
entityMemories.Add(e.Id, em);
//once and current visible map update
AddEntityViewToOnceVisibleMap(e.EntityType, e.Position.X, e.Position.Y);
AddEntityViewToCurrentVisibleMap(e.EntityType, e.Position.X, e.Position.Y);
howMuchResourcesCollectLastTurn += properties[e.EntityType].InitialCost + currentMyEntityCount[e.EntityType] - 1;
//check my builder units
//if (properties[em.myEntity.EntityType].CanMove == false)
//{
// for (int i = 0; i < intentions.Count; i++)
// {
// if (intentions[i].intentionType == IntentionType.IntentionCreateHouse)
// {
// if (intentions[i].targetPosition.X == em.myEntity.Position.X && intentions[i].targetPosition.Y == em.myEntity.Position.Y)
// {
// intentions[i].targetId = em.myEntity.Id;
// break;
// }
// }
// }
//}
}
// добаввляем раненых в списки лечения
if (e.Health < properties[e.EntityType].MaxHealth)
{
if (properties[e.EntityType].CanMove)
needRepairUnitsIdList.Add(e.Id);
else
needRepairBuildingIdList.Add(e.Id);
}
}
else if (e.PlayerId == null)// it s resource
{
resourceMemoryMap[e.Position.X][e.Position.Y] = currentTick+1;
}
else // it's enemy
{
enemiesById.Add(e.Id, e);
AddEnemyDangerCells(e.Position.X, e.Position.Y, e.EntityType);
}
}
//remove died entity
foreach (var m in entityMemories)
{
if (m.Value.checkedNow == false)
{
m.Value.Die();
entityMemories.Remove(m.Key);
}
}
//statistics
if (_playerView.CurrentTick == 0)
{
howMuchResourcesCollectLastTurn = 0;
}
howMuchResourcesCollectAll += howMuchResourcesCollectLastTurn;
for (int i = howMuchResourcesCollectCPALastNTurns.Length - 1; i > 0; i--)
{
howMuchResourcesCollectLastNTurns[i] = howMuchResourcesCollectLastNTurns[i - 1];
howMuchResourcesCollectCPALastNTurns[i] = howMuchResourcesCollectCPALastNTurns[i - 1];
howMuchLiveBuildersLast10Turns[i] = howMuchLiveBuildersLast10Turns[i - 1];
}
howMuchResourcesCollectLastNTurns[0] = howMuchResourcesCollectLastTurn;
int co = currentMyEntityCount[EntityType.BuilderUnit];
howMuchLiveBuildersLast10Turns[0] = co;
howMuchResourcesCollectCPALastNTurns[0] = (co == 0) ? 0 : howMuchResourcesCollectLastTurn * 100 / co;
int sum = 0;
for (int i = 0; i < nextTurnResourcesSelectCount; i++)
{
sum += howMuchResourcesCollectLastNTurns[0];
}
howMuchResourcesIHaveNextTurn = myResources + sum / nextTurnResourcesSelectCount + nextTurnResourcesBonus;
}
void CheckDeadResourceOnCurrentVisibleMap()
{
int currentTick = _playerView.CurrentTick;
for (int x = 0; x < mapSize; x++)
{
for (int y = 0; y < mapSize; y++)
{
if (currentVisibleMap[x][y] == true)
{
if (resourceMemoryMap[x][y] > 0 && resourceMemoryMap[x][y] <= currentTick)
resourceMemoryMap[x][y] = 0;
}
}
}
}
void GenerateResourcePotentialField()
{
//zeroing
for (int i = 0; i < mapSize; i++)
{
resourcePotentialField[i] = new int[mapSize];
}
//стартовое значение, которое будем уменьшать
int startWeight = mapSize * mapSize;
//int firstCellWeight = startWeight - 1; // соседние клетки с ресурсом. Считаем, что строители на них всегда добывают ресурс и не строим путь через него
//int minIndex = startIndex - maxDistance; //минимальное значение, дальше которого не будем искать
//добавляем стартовые клетки поиска
List<XYWeight> findCells = new List<XYWeight>();
foreach (var en in _playerView.Entities)
{
if (en.EntityType == EntityType.Resource) // it is resource
{
findCells.Add(new XYWeight(en.Position.X, en.Position.Y, startWeight));
resourcePotentialField[en.Position.X][en.Position.Y] = startWeight;
}
}
while (findCells.Count > 0)
{
int bx = findCells[0].x;
int by = findCells[0].y;
int w = findCells[0].weight;
for (int jj = 0; jj < 4; jj++)
{
int nx = bx;
int ny = by;
if (jj == 0) nx--;
if (jj == 1) ny--;
if (jj == 2) nx++;
if (jj == 3) ny++;
if (nx >= 0 && nx < mapSize && ny >= 0 && ny < mapSize)
{
if (resourcePotentialField[nx][ny] == 0)
{
bool canContinueField = true;
// проверка опасной зоны
var dCell = enemyDangerCells[nx][ny];
if (dCell.meleesAim + dCell.rangersAim + dCell.turretsAim > 0)
{
canContinueField = false;
resourcePotentialField[nx][ny] = RPFdangerCellWeight;
}
else if (dCell.meleesWarning + dCell.rangersWarning > 0)
{
canContinueField = false;
resourcePotentialField[nx][ny] = RPFwarningCellWeight;
//findCells.Add(new XYWeight(nx, ny, RPFwarningCellWeight));
}
// проверка занятой клетки
if (canContinueField == true)
{
int id = cellWithIdAny[nx][ny];
if (id >= 0)// occupied cell
{
if (entityMemories.ContainsKey(id))
{
if (entityMemories[id].myEntity.EntityType == EntityType.BuilderUnit)
{
if (w == startWeight)//check my builder на соседней клетке с ресурсомs
{
canContinueField = false;
resourcePotentialField[nx][ny] = RPFdeniedBuilderWeight;
}