-
Notifications
You must be signed in to change notification settings - Fork 1
/
AAISector.cpp
692 lines (559 loc) · 23.5 KB
/
AAISector.cpp
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
// ------------------------------------------------------------------------
// AAI
//
// A skirmish AI for the Spring engine.
// Copyright Alexander Seizinger
//
// Released under GPL license: see LICENSE.html for more information.
// ------------------------------------------------------------------------
#include "AAISector.h"
#include "AAI.h"
#include "AAIBuildTable.h"
#include "AAIBrain.h"
#include "AAIConfig.h"
#include "AAIMap.h"
#include "AAIThreatMap.h"
#include "LegacyCpp/IGlobalAICallback.h"
#include "LegacyCpp/UnitDef.h"
AAISector::AAISector() :
m_sectorIndex(0, 0),
m_distanceToBase(-1),
m_lostUnits(),
m_ownBuildingsOfCategory(AAIUnitCategory::numberOfUnitCategories, 0),
m_enemyCombatUnits(0.0f),
m_enemyBuildings(0),
m_alliedBuildings(0),
m_enemyUnitsDetectedBySensor(0),
m_skippedAsScoutDestination(0),
m_failedAttemptsToConstructStaticDefence(0)
{
}
AAISector::~AAISector(void)
{
m_ownBuildingsOfCategory.clear();
}
void AAISector::Init(AAI *ai, int x, int y)
{
this->ai = ai;
// set coordinates of the corners
m_sectorIndex.x = x;
m_sectorIndex.y = y;
// determine map border distance
const int xEdgeDist = std::min(x, AAIMap::xSectors - 1 - x);
const int yEdgeDist = std::min(y, AAIMap::ySectors - 1 - y);
m_minSectorDistanceToMapEdge = std::min(xEdgeDist, yEdgeDist);
const float3 center = GetCenter();
m_continentId = AAIMap::GetContinentID(center);
importance_this_game = 1.0f + (rand()%5)/20.0f;
}
void AAISector::LoadDataFromFile(FILE* file)
{
if(file != nullptr)
{
fscanf(file, "%f %f %f", &m_flatTilesRatio, &m_waterTilesRatio, &importance_learned);
if(importance_learned < 1.0f)
importance_learned += (rand()%5)/20.0f;
m_attacksByTargetTypeInPreviousGames.LoadFromFile(file);
}
else // no learning data available -> init with default data
{
importance_learned = 1.0f + (rand()%5)/20.0f;
m_flatTilesRatio = DetermineFlatRatio();
m_waterTilesRatio = DetermineWaterRatio();
}
importance_this_game = importance_learned;
}
void AAISector::SaveDataToFile(FILE* file)
{
fprintf(file, "%f %f %f ", m_flatTilesRatio, m_waterTilesRatio, importance_this_game);
m_attacksByTargetTypeInPreviousGames.SaveToFile(file);
}
void AAISector::UpdateLearnedData()
{
importance_this_game = 0.93f * (importance_this_game + 3.0f * importance_learned) / 4.0f;
if(importance_this_game < 1.0f)
importance_this_game = 1.0f;
m_attacksByTargetTypeInPreviousGames.MultiplyValues(3.0f);
m_attacksByTargetTypeInPreviousGames.AddMobileTargetValues(m_attacksByTargetTypeInCurrentGame);
m_attacksByTargetTypeInPreviousGames.MultiplyValues(0.225f); // 0.225f = 0.9f / 4.0f ->decrease by 0.9 and account for 3.0f in line above
}
bool AAISector::AddToBase(bool addToBase)
{
if(addToBase)
{
// check if already occupied (may happen if two coms start in same sector)
if(AAIMap::s_teamSectorMap.IsSectorOccupied(m_sectorIndex))
{
ai->Log("\nTeam %i could not add sector %i,%i to base, already occupied by ally team %i!\n\n",ai->GetAICallback()->GetMyAllyTeam(), m_sectorIndex.x, m_sectorIndex.y, AAIMap::s_teamSectorMap.GetTeam(m_sectorIndex));
return false;
}
m_distanceToBase = 0;
importance_this_game = std::min(importance_this_game + 1.0f, AAIConstants::maxSectorImportance);
AAIMap::s_teamSectorMap.SetSectorAsOccupiedByTeam(m_sectorIndex, ai->GetMyTeamId());
return true;
}
else // remove from base
{
m_distanceToBase = 1;
AAIMap::s_teamSectorMap.SetSectorAsUnoccupied(m_sectorIndex);
return true;
}
}
void AAISector::ResetLocalCombatPower()
{
m_alliedBuildings = 0;
m_friendlyStaticCombatPower.Reset();
m_friendlyMobileCombatPower.Reset();
}
void AAISector::ResetScoutedEnemiesData()
{
m_enemyBuildings = 0;
m_enemyCombatUnits.Fill(0.0f);
m_enemyStaticCombatPower.Reset();
m_enemyMobileCombatPower.Reset();
};
void AAISector::AddScoutedEnemyUnit(UnitDefId enemyDefId, int framesSinceLastUpdate)
{
const AAIUnitCategory& categoryOfEnemyUnit = ai->s_buildTree.GetUnitCategory(enemyDefId);
// add building to sector (and update stat_combat_power if it's a stat defence)
if(categoryOfEnemyUnit.IsBuilding())
{
++m_enemyBuildings;
if(categoryOfEnemyUnit.IsStaticDefence())
{
m_enemyStaticCombatPower.AddCombatPower( ai->s_buildTree.GetCombatPower(enemyDefId) );
m_enemyCombatUnits.AddValue(ETargetType::STATIC, 1.0f);
}
}
// add unit to sector and update mobile_combat_power
else if(categoryOfEnemyUnit.IsCombatUnit())
{
// units that have been scouted long time ago matter less (1 min ~ 70%, 2 min ~ 48%, 5 min ~ 16%)
const float lastSeen = exp(- static_cast<float>(framesSinceLastUpdate) / 5000.0f );
const AAITargetType& targetType = ai->s_buildTree.GetTargetType(enemyDefId);
m_enemyCombatUnits.AddValue(targetType, lastSeen);
m_enemyMobileCombatPower.AddCombatPower( ai->s_buildTree.GetCombatPower(enemyDefId), lastSeen );
}
}
void AAISector::DecreaseLostUnits()
{
m_lostUnits.MultiplyValues(AAIConstants::lostUnitsMemoryFadeRate);
}
void AAISector::AddMetalSpot(AAIMetalSpot *spot)
{
metalSpots.push_back(spot);
}
void AAISector::AddExtractor(UnitId unitId, UnitDefId unitDefId, float3 position)
{
ai->Map()->ConvertPositionToFinalBuildsite(position, ai->s_buildTree.GetFootprint(unitDefId));
for(auto spot : metalSpots)
{
// only check occupied spots
if(spot->occupied && spot->DoesSpotBelongToPosition(position))
{
spot->extractorUnitId = unitId;
spot->extractorDefId = unitDefId;
}
}
}
void AAISector::FreeMetalSpot(float3 position, UnitDefId extractorDefId)
{
ai->Map()->ConvertPositionToFinalBuildsite(position, ai->s_buildTree.GetFootprint(extractorDefId));
// get metalspot according to position
for(auto spot : metalSpots)
{
// only check occupied spots
if(spot->occupied && spot->DoesSpotBelongToPosition(position) )
{
spot->SetUnoccupied();
return;
}
}
}
float3 AAISector::GetCenter() const
{
return float3( static_cast<float>(m_sectorIndex.x * AAIMap::xSectorSize + AAIMap::xSectorSize/2), 0.0f, static_cast<float>(m_sectorIndex.y * AAIMap::ySectorSize + AAIMap::ySectorSize/2));
}
float AAISector::GetImportanceForStaticDefenceVs(AAITargetType& targetType, const GamePhase& gamePhase, float previousGames, float currentGame)
{
if( AreFurtherStaticDefencesAllowed() )
{
if(m_failedAttemptsToConstructStaticDefence < 2) // do not try to build defences if last two attempts failed
{
const float baseProximity = (m_distanceToBase <= 1) ? 1.0f : 0.0f;
std::vector<float> importanceVsTargetType(AAITargetType::numberOfMobileTargetTypes, 0.0f);
importanceVsTargetType[AAITargetType::airIndex] = baseProximity +
(0.1f + GetLocalAttacksBy(ETargetType::AIR, previousGames, currentGame) + ai->Brain()->GetAttacksBy(ETargetType::AIR, gamePhase))
/ (1.0f + GetFriendlyStaticDefencePower(ETargetType::AIR));
if(m_waterTilesRatio < 0.7f)
{
importanceVsTargetType[AAITargetType::surfaceIndex] = baseProximity +
(0.1f + GetLocalAttacksBy(ETargetType::SURFACE, previousGames, currentGame) + ai->Brain()->GetAttacksBy(ETargetType::SURFACE, gamePhase))
/ (1.0f + GetFriendlyStaticDefencePower(ETargetType::SURFACE));
}
if(m_waterTilesRatio > 0.3f)
{
importanceVsTargetType[AAITargetType::floaterIndex] = baseProximity +
(0.1f + GetLocalAttacksBy(ETargetType::FLOATER, previousGames, currentGame) + ai->Brain()->GetAttacksBy(ETargetType::FLOATER, gamePhase))
/ (1.0f + GetFriendlyStaticDefencePower(ETargetType::FLOATER));
importanceVsTargetType[AAITargetType::submergedIndex] = baseProximity +
(0.1f + GetLocalAttacksBy(ETargetType::SUBMERGED, previousGames, currentGame) + ai->Brain()->GetAttacksBy(ETargetType::SUBMERGED, gamePhase))
/ (1.0f + GetFriendlyStaticDefencePower(ETargetType::SUBMERGED));
}
float highestImportance(0.0f);
for(int targetTypeId = 0; targetTypeId < importanceVsTargetType.size(); ++targetTypeId)
{
if( importanceVsTargetType[targetTypeId] > highestImportance )
{
highestImportance = importanceVsTargetType[targetTypeId];
targetType = AAITargetType(static_cast<ETargetType>(targetTypeId));
}
}
// modify importance based on location of sector (higher importance for sectors "facing" the enemy)
if(highestImportance > 0.0f)
{
const MapPos& enemyBaseCenter = ai->Map()->GetCenterOfEnemyBase();
const MapPos& baseCenter = ai->Brain()->GetCenterOfBase();
const MapPos sectorCenter(m_sectorIndex.x * AAIMap::xSectorSizeMap + AAIMap::xSectorSizeMap/2, m_sectorIndex.y * AAIMap::ySectorSizeMap + AAIMap::ySectorSizeMap/2);
const int distEnemyBase = (enemyBaseCenter.x - sectorCenter.x)*(enemyBaseCenter.x - sectorCenter.x)
+ (enemyBaseCenter.y - sectorCenter.y)*(enemyBaseCenter.y - sectorCenter.y);
const int distOwnToEnemyBase = (enemyBaseCenter.x - baseCenter.x)*(enemyBaseCenter.x - baseCenter.x)
+ (enemyBaseCenter.y - baseCenter.y)*(enemyBaseCenter.y - baseCenter.y);
if(distEnemyBase < distOwnToEnemyBase)
highestImportance *= 2.0f;
highestImportance *= static_cast<float>(2 + this->GetEdgeDistance()) * (2.0f / static_cast<float>(m_distanceToBase+1));
}
return highestImportance;
}
m_failedAttemptsToConstructStaticDefence = 0;
}
return 0.0f;
}
float AAISector::GetAttackRating(const AAISector* currentSector, bool landSectorSelectable, bool waterSectorSelectable, const MobileTargetTypeValues& targetTypeOfUnits) const
{
float rating(0.0f);
if( (m_distanceToBase > 0) && (GetNumberOfEnemyBuildings() > 0) )
{
const bool landCheckPassed = landSectorSelectable && (m_waterTilesRatio < 0.35f);
const bool waterCheckPassed = waterSectorSelectable && (m_waterTilesRatio > 0.65f);
if(landCheckPassed || waterCheckPassed)
{
const float dx = static_cast<float>(m_sectorIndex.x - currentSector->m_sectorIndex.x);
const float dy = static_cast<float>(m_sectorIndex.y - currentSector->m_sectorIndex.y);
const float dist = fastmath::apxsqrt(dx*dx + dy*dy );
const float enemyBuildings = static_cast<float>(GetNumberOfEnemyBuildings());
// prefer sectors with many buildings, few lost units and low defence power/short distance to current sector
rating = GetTotalLostUnits() * enemyBuildings / ( (1.0f + GetEnemyCombatPowerVsUnits(targetTypeOfUnits)) * (1.0f + dist) );
}
}
return rating;
}
float AAISector::GetAttackRating(const std::vector<float>& globalCombatPower, const std::vector< std::vector<float> >& continentCombatPower, const MobileTargetTypeValues& assaultGroupsOfType, float maxLostUnits) const
{
float rating(0.0f);
if( (m_distanceToBase > 0) && (GetNumberOfEnemyBuildings() > 0))
{
const float myAttackPower = globalCombatPower[AAITargetType::staticIndex] + continentCombatPower[m_continentId][AAITargetType::staticIndex];
const float enemyDefencePower = assaultGroupsOfType.GetValueOfTargetType(ETargetType::SURFACE) * GetEnemyCombatPower(ETargetType::SURFACE)
+ assaultGroupsOfType.GetValueOfTargetType(ETargetType::FLOATER) * GetEnemyCombatPower(ETargetType::FLOATER)
+ assaultGroupsOfType.GetValueOfTargetType(ETargetType::SUBMERGED) * GetEnemyCombatPower(ETargetType::SUBMERGED);
const float lostUnitsFactor = (maxLostUnits > 1.0f) ? (2.0f - (GetTotalLostUnits() / maxLostUnits) ) : 1.0f;
const float enemyBuildings = static_cast<float>(GetNumberOfEnemyBuildings());
// prefer sectors with many buildings, few lost units and low defence power/short distance to own base
rating = lostUnitsFactor * (2.0f + enemyBuildings) * myAttackPower / ( (1.5f + enemyDefencePower) * static_cast<float>(1 + 2 * m_distanceToBase) );
}
return rating;
}
float AAISector::GetRatingAsNextScoutDestination(const AAIMovementType& scoutMoveType, const AAITargetType& scoutTargetType, const float3& currentPositionOfScout)
{
if( (m_distanceToBase == 0)
|| (scoutMoveType.IsIncludedIn(m_suitableMovementTypes) == false)
|| (GetNumberOfAlliedBuildings() > 0) )
return 0.0f;
else
{
++m_skippedAsScoutDestination;
const float3 center = GetCenter();
const float dx = currentPositionOfScout.x - center.x;
const float dy = currentPositionOfScout.z - center.z;
// factor between 0.1 (max dist from one corner of the map tpo the other) and 1.0
const float distanceToCurrentLocationFactor = 0.1f + 0.9f * (1.0f - (dx*dx+dy*dy) / AAIMap::s_maxSquaredMapDist);
// factor between 1 and 0.4 (depending on number of recently lost units)
//const float lostUnits = // scoutMoveType.IsAir() ? m_lostAirUnits : m_lostUnits;
const float lostScoutsFactor = 0.4f + 0.6f / (0.5f * m_lostUnits.GetValueOfTargetType(scoutTargetType) + 1.0f);
const float metalSpotsFactor = 2.0f + static_cast<float>(metalSpots.size());
//! @todo Take learned starting locations into account in early phase
return metalSpotsFactor * distanceToCurrentLocationFactor * lostScoutsFactor * static_cast<float>(m_skippedAsScoutDestination);
}
}
float AAISector::GetRatingForRallyPoint(const AAIMovementType& moveType, int continentId) const
{
if( (continentId != AAIMap::ignoreContinentID) && (continentId != m_continentId) )
return 0.0f;
const float edgeDistance = static_cast<float>( GetEdgeDistance() );
const float totalAttacks = GetTotalLostUnits() + GetTotalAttacksInThisGame();
float rating(0.0f);
if(moveType.IsAir())
{
rating = (GetFlatTilesRatio() + GetWaterTilesRatio()) / (1.0f + std::min(totalAttacks, 3.0f));
}
else
{
rating = std::min(totalAttacks, 6.0f)
+ std::min(2.0f * edgeDistance, 6.0f)
+ 3.0f * GetNumberOfBuildings(EUnitCategory::METAL_EXTRACTOR);
if( moveType.IsGround() )
{
rating += 3.0f * GetFlatTilesRatio();
}
else if( moveType.IsAmphibious() || moveType.IsHover())
{
rating += 3.0f * (GetFlatTilesRatio() + GetWaterTilesRatio());
}
else
{
rating += 3.0f * GetWaterTilesRatio();
}
}
return rating;
}
float AAISector::GetRatingAsStartSector() const
{
if(AAIMap::s_teamSectorMap.IsSectorOccupied(m_sectorIndex))
return 0.0f;
else
return ( static_cast<float>(2 * GetNumberOfMetalSpots() + 1) ) * m_flatTilesRatio * m_flatTilesRatio;
}
float AAISector::GetRatingForPowerPlant(float weightPreviousGames, float weightCurrentGame) const
{
if(m_ownBuildingsOfCategory[AAIUnitCategory(EUnitCategory::STATIC_CONSTRUCTOR).GetArrayIndex()] > 1)
return 0.0f;
else
{
const float attacks = 0.1f + weightPreviousGames * m_attacksByTargetTypeInPreviousGames.CalculateSum() + weightCurrentGame * m_attacksByTargetTypeInCurrentGame.CalculateSum();
return 1.0f / (attacks * static_cast<float>(GetEdgeDistance()+1) );
}
}
bool AAISector::IsSectorSuitableForBaseExpansion() const
{
const bool consideredToBeSafe = (m_lostUnits.CalculateSum() < 1.0f) || m_friendlyMobileCombatPower.CalculateSum() > 2.0f;
return (IsOccupiedByEnemies() == false)
&& (GetNumberOfAlliedBuildings() < 3)
&& (AAIMap::s_teamSectorMap.IsSectorOccupied(m_sectorIndex) == false)
&& consideredToBeSafe;
}
bool AAISector::ShallBeConsideredForExtractorConstruction() const
{
const bool consideredToBeSafe = (m_distanceToBase == 0) || (m_lostUnits.CalculateSum() < 1.0f) || m_friendlyMobileCombatPower.CalculateSum() > 2.0f;
return (AAIMap::s_teamSectorMap.IsOccupiedByOtherTeam(m_sectorIndex, ai->GetMyTeamId()) == false)
&& (IsOccupiedByEnemies() == false)
&& (GetNumberOfAlliedBuildings() <= 0)
&& consideredToBeSafe;
}
bool AAISector::AreFreeMetalSpotsAvailable() const
{
for(const auto spot : metalSpots)
{
if(spot->occupied == false)
return true;
}
return false;
}
BuildSite AAISector::DetermineRandomBuildsite(UnitDefId buildingDefId, int tries) const
{
int xStart, xEnd, yStart, yEnd;
DetermineBuildsiteRectangle(&xStart, &xEnd, &yStart, &yEnd);
return ai->Map()->DetermineRandomBuildsite(buildingDefId, xStart, xEnd, yStart, yEnd, tries);
}
BuildSite AAISector::DetermineElevatedBuildsite(UnitDefId buildingDefId, float range) const
{
int xStart, xEnd, yStart, yEnd;
DetermineBuildsiteRectangle(&xStart, &xEnd, &yStart, &yEnd);
return ai->Map()->DetermineElevatedBuildsite(buildingDefId, xStart, xEnd, yStart, yEnd, range);
}
float3 AAISector::DetermineAttackPosition() const
{
if(GetNumberOfEnemyBuildings() == 0)
return GetCenter();
else
{
const int xStart = m_sectorIndex.x * AAIMap::xSectorSizeMap;
const int xEnd = (m_sectorIndex.x + 1) * AAIMap::xSectorSizeMap;
const int yStart = m_sectorIndex.y * AAIMap::ySectorSizeMap;
const int yEnd = (m_sectorIndex.y + 1) * AAIMap::ySectorSizeMap;
return ai->Map()->DeterminePositionOfEnemyBuildingInSector(xStart, xEnd, yStart, yEnd);
}
}
void AAISector::DetermineBuildsiteRectangle(int *xStart, int *xEnd, int *yStart, int *yEnd) const
{
*xStart = m_sectorIndex.x * AAIMap::xSectorSizeMap;
*xEnd = *xStart + AAIMap::xSectorSizeMap;
if(*xStart == 0)
*xStart = 8;
*yStart = m_sectorIndex.y * AAIMap::ySectorSizeMap;
*yEnd = *yStart + AAIMap::ySectorSizeMap;
if(*yStart == 0)
*yStart = 8;
// reserve buildspace for def. buildings
/*if(x > 0 && ai->Getmap()->m_sector[x-1][y].distance_to_base > 0 )
*xStart += ai->Getmap()->xSectorSizeMap/8;
if(x < ai->Getmap()->xSectors-1 && ai->Getmap()->m_sector[x+1][y].distance_to_base > 0)
*xEnd -= ai->Getmap()->xSectorSizeMap/8;
if(y > 0 && ai->Getmap()->m_sector[x][y-1].distance_to_base > 0)
*yStart += ai->Getmap()->ySectorSizeMap/8;
if(y < ai->Getmap()->ySectors-1 && ai->Getmap()->m_sector[x][y+1].distance_to_base > 0)
*yEnd -= ai->Getmap()->ySectorSizeMap/8;*/
}
bool AAISector::IsSupportNeededToDefenceVs(const AAITargetType& targetType) const
{
const float enemyCombatPower = GetEnemyCombatPower(targetType);
const float friendlyCombatPower = GetFriendlyCombatPower(targetType);
if(enemyCombatPower < 0.02f)
{
if(friendlyCombatPower < AAIConstants::localDefencePowerToRequestSupportThreshold)
return true;
}
else
{
if(friendlyCombatPower < enemyCombatPower)
return true;
}
return false;
}
float AAISector::GetLocalAttacksBy(const AAITargetType& targetType, float previousGames, float currentGame) const
{
const float totalAttacks = (previousGames * m_attacksByTargetTypeInPreviousGames.GetValueOfTargetType(targetType) + currentGame * m_attacksByTargetTypeInCurrentGame.GetValueOfTargetType(targetType) );
return totalAttacks / (previousGames + currentGame);
}
float AAISector::GetEnemyCombatPowerVsUnits(const MobileTargetTypeValues& unitsOfTargetType) const
{
float defencePower(0.0f);
for(const auto& targetType : AAITargetType::m_mobileTargetTypes)
{
const float totalDefPower = m_enemyStaticCombatPower.GetValueOfTargetType(targetType) + m_enemyMobileCombatPower.GetValueOfTargetType(targetType);
defencePower += unitsOfTargetType.GetValueOfTargetType(targetType) * totalDefPower;
}
return defencePower;
}
float AAISector::DetermineWaterRatio() const
{
int waterCells(0);
for(int yPos = m_sectorIndex.y * AAIMap::ySectorSizeMap; yPos < (m_sectorIndex.y+1) * AAIMap::ySectorSizeMap; ++yPos)
{
for(int xPos = m_sectorIndex.x * AAIMap::xSectorSizeMap; xPos < (m_sectorIndex.x+1) * AAIMap::xSectorSizeMap; ++xPos)
{
if(AAIMap::s_buildmap[xPos + yPos * AAIMap::xMapSize].IsTileTypeSet(EBuildMapTileType::WATER))
++waterCells;
}
}
const int totalCells = AAIMap::xSectorSizeMap * AAIMap::ySectorSizeMap;
return waterCells / static_cast<float>(totalCells);
}
float AAISector::DetermineFlatRatio() const
{
// get number of cliffy & flat cells
const int cliffyCells = ai->Map()->GetCliffyCells(m_sectorIndex.x * AAIMap::xSectorSizeMap, m_sectorIndex.y * AAIMap::ySectorSizeMap, AAIMap::xSectorSizeMap, AAIMap::ySectorSizeMap);
const int totalCells = ai->Map()->xSectorSizeMap * ai->Map()->ySectorSizeMap;
const int flatCells = totalCells - cliffyCells;
return static_cast<float>(flatCells) / static_cast<float>(totalCells);
}
void AAISector::UpdateThreatValues(UnitDefId destroyedDefId, UnitDefId attackerDefId)
{
const AAIUnitCategory& destroyedCategory = ai->s_buildTree.GetUnitCategory(destroyedDefId);
const AAIUnitCategory& attackerCategory = ai->s_buildTree.GetUnitCategory(attackerDefId);
// if lost unit is a building, increase attacked_by
if(destroyedCategory.IsBuilding())
{
if(attackerCategory.IsCombatUnit())
{
const float increment = (m_distanceToBase == 0) ? 0.5f : 1.0f;
m_attacksByTargetTypeInCurrentGame.AddValueForTargetType(ai->s_buildTree.GetTargetType(attackerDefId) , increment);
}
}
else // unit was lost
{
const AAITargetType& targetType = ai->s_buildTree.GetTargetType(destroyedDefId);
m_lostUnits.AddValueForTargetType(targetType, 1.0f);
}
}
bool AAISector::PosInSector(const float3& pos) const
{
if( (pos.x < static_cast<float>( m_sectorIndex.x * AAIMap::xSectorSize))
|| (pos.x > static_cast<float>( (m_sectorIndex.x+1) * AAIMap::xSectorSize))
|| (pos.z < static_cast<float>( m_sectorIndex.y * AAIMap::ySectorSize))
|| (pos.z > static_cast<float>( (m_sectorIndex.y+1) * AAIMap::ySectorSize)) )
return false;
else
return true;
}
bool AAISector::ConnectedToOcean() const
{
if(m_waterTilesRatio < 0.2f)
return false;
const int xStart = m_sectorIndex.x * AAIMap::xSectorSizeMap;
const int xEnd = (m_sectorIndex.x+1) * AAIMap::xSectorSizeMap;
const int yStart = m_sectorIndex.y * AAIMap::ySectorSizeMap;
const int yEnd = (m_sectorIndex.y+1) * AAIMap::ySectorSizeMap;
return ai->Map()->IsConnectedToOcean(xStart, xEnd, yStart, yEnd);
}
float3 AAISector::DetermineUnitMovePos(AAIMovementType moveType, int continentId) const
{
BuildMapTileType forbiddenMapTileTypes(EBuildMapTileType::OCCUPIED);
forbiddenMapTileTypes.SetTileType(EBuildMapTileType::BLOCKED_SPACE);
if(moveType.IsMobileSea())
forbiddenMapTileTypes.SetTileType(EBuildMapTileType::LAND);
else if(moveType.IsAmphibious() || moveType.IsHover())
forbiddenMapTileTypes.SetTileType(EBuildMapTileType::CLIFF);
else if(moveType.IsGround())
{
forbiddenMapTileTypes.SetTileType(EBuildMapTileType::WATER);
forbiddenMapTileTypes.SetTileType(EBuildMapTileType::CLIFF);
}
const float xPosStart = static_cast<float>(m_sectorIndex.x * AAIMap::xSectorSize);
const float yPosStart = static_cast<float>(m_sectorIndex.y * AAIMap::ySectorSize);
// try to get random spot
for(int i = 0; i < 6; ++i)
{
float3 position;
position.x = xPosStart + static_cast<float>(AAIMap::xSectorSize) * (0.1f + 0.08f * (float)(rand()%11) );
position.z = yPosStart + static_cast<float>(AAIMap::ySectorSize) * (0.1f + 0.08f * (float)(rand()%11) );
if(IsValidMovePos(position, forbiddenMapTileTypes, continentId))
{
position.y = ai->GetAICallback()->GetElevation(position.x, position.z);
return position;
}
}
// search systematically
for(int i = 0; i < AAIMap::xSectorSizeMap; i += 4)
{
for(int j = 0; j < AAIMap::ySectorSizeMap; j += 4)
{
float3 position;
position.x = xPosStart + static_cast<float>(i * SQUARE_SIZE);
position.z = yPosStart + static_cast<float>(j * SQUARE_SIZE);
if(IsValidMovePos(position, forbiddenMapTileTypes, continentId))
{
position.y = ai->GetAICallback()->GetElevation(position.x, position.z);
return position;
}
}
}
return ZeroVector;
}
bool AAISector::IsValidMovePos(const float3& pos, BuildMapTileType forbiddenMapTileTypes, int continentId) const
{
const int x = (int) (pos.x / SQUARE_SIZE);
const int y = (int) (pos.z / SQUARE_SIZE);
if(AAIMap::s_buildmap[x + y * AAIMap::xMapSize].IsTileTypeNotSet(forbiddenMapTileTypes))
{
if( (continentId == AAIMap::ignoreContinentID) || (AAIMap::GetContinentID(pos) == continentId) )
return true;
}
return false;
}
bool AAISector::AreFurtherStaticDefencesAllowed() const
{
return (GetNumberOfBuildings(EUnitCategory::STATIC_DEFENCE) < cfg->MAX_DEFENCES)
&& (GetNumberOfAlliedBuildings() < 3)
&& (AAIMap::s_teamSectorMap.IsOccupiedByOtherTeam(m_sectorIndex, ai->GetMyTeamId()) == false);
}