-
Notifications
You must be signed in to change notification settings - Fork 1
/
AAIBrain.h
200 lines (135 loc) · 8.31 KB
/
AAIBrain.h
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
// -------------------------------------------------------------------------
// AAI
//
// A skirmish AI for the Spring engine.
// Copyright Alexander Seizinger
//
// Released under GPL license: see LICENSE.html for more information.
// -------------------------------------------------------------------------
#ifndef AAI_BRAIN_H
#define AAI_BRAIN_H
class AAI;
class AAIExecute;
class AIIMap;
class AAISector;
#include "aidef.h"
#include "AAIMapRelatedTypes.h"
#include "AAIUnitStatistics.h"
#include "AAIBuildTable.h"
class AAIBrain
{
public:
AAIBrain(AAI *ai, int maxSectorDistanceToBase);
~AAIBrain(void);
void InitAttackedByRates(const AttackedByRatesPerGamePhase& attackedByRates);
//! @brief Returns the current estimation how much the AAI instance is under pressure by the enemies, values ranging from 0 (min) to 1 (max).
float GetPressureByEnemy() const { return m_estimatedPressureByEnemies; }
float GetAverageAvailableMetal() const { return m_metalAvailable.GetAverageValue(); }
float GetAverageMetalSurplus() const { return m_metalSurplus.GetAverageValue(); }
float GetAveragEnergySurplus() const { return m_energySurplus.GetAverageValue(); }
float GetBaseFlatLandRatio() const { return m_baseFlatLandRatio; }
float GetBaseWaterRatio() const { return m_baseWaterRatio; }
//! @brief Returns the center of the base in map coordinates
const MapPos& GetCenterOfBase() const { return m_centerOfBase; }
//! @brief Adds/removes the given sector to the base
void AssignSectorToBase(AAISector *sector, bool addToBase);
//! @brief Updates the (smoothened) energy/metal income
void UpdateResources(springLegacyAI::IAICallback* cb);
//! @brief Overwrites the smoothed energy income/surplus with the current values (called after power plant is finished)
void PowerPlantFinished(UnitDefId powerPlant);
//! @brief Updates the maximum number of spotted combat units for each category (old max values decrease over time)
void UpdateMaxCombatUnitsSpotted(const MobileTargetTypeValues& spottedCombatUnits);
void UpdateAttackedByValues();
//! @brief Update counters after AAI has been attacked by a certain unit
void AttackedBy(const AAITargetType& attackerTargetType);
//! @brief Returns the frequencies of attacks by different combat unit categories in different phases of the game
const AttackedByRatesPerGamePhase& GetAttackedByRates() const { return s_attackedByRates; }
// recalculates def capabilities of all units
void UpdateDefenceCapabilities();
//! @brief Adds the combat power of the given unit type to the global defence capabilities
void AddDefenceCapabilities(UnitDefId unitDefId);
//! @brief Expands base for the first time at startup (chooses sector based on map type and start sector)
void ExpandBaseAtStartup();
//! @brief Tries to add a new sectors to base, returns true if successful (may fail because base already reached maximum size or no suitable sectors found)
bool ExpandBase(const AAIMapType& sectorType, bool preferSafeSector = true);
// returns how much ressources can be spent for unit construction atm
float Affordable();
//! @brief Returns whether commander is allowed for construction in the given sector
bool IsCommanderAllowedForConstructionInSector(const AAISector *sector) const;
void DefendCommander(int attacker);
void BuildUnits();
//! @brief Determines the currently estimated pressure by the enemy based on which sectors are believed to be occupied by enemy units
void UpdatePressureByEnemy(const SectorMap& sectors);
//! @brief Returns the frequency of attacks by units of specified combat category
//! The value is determined according to to current game phase, data from this game and learned data.
float GetAttacksBy(const AAITargetType& targetType, const GamePhase& gamePhase) const;
//! @brief Returns the recent attacks by the given target type
float GetRecentAttacksBy(const AAITargetType& targetType) const { return m_recentlyAttackedByRates.GetValueOfTargetType(targetType); }
//! @brief Return the current power suplus (excess energy + small percentage of stored energy - offset)
float GetAveragePowerSurplus() const;
//! @brief Returns urgency to build power plant
float GetEnergyUrgency() const;
//! @brief Returns urgency to build metal extractor
float GetMetalUrgency() const;
//! @brief Returns urgency to build energy storage
float GetEnergyStorageUrgency() const;
//! @brief Returns urgency to build metal storage
float GetMetalStorageUrgency() const;
//! @brief Returns whether construction of unit of given type shall be assisted (taking current resources into account)
bool SufficientResourcesToAssistsConstructionOf(UnitDefId defId) const;
//! @brief Determines the construction priority of the given factory
float DetermineConstructionUrgencyOfFactory(UnitDefId factoryDefId, const TargetTypeValues& combatPowerVsTargetType) const;
//! @brief Determines the combat efficiciency vs different target types for the selection of the next combat unit
TargetTypeValues DetermineCombatPowerVsTargetType() const;
//! @brief Determines the selection criteria for a scout
ScoutSelectionCriteria DetermineScoutSelectionCriteria() const;
//! @brief Determines the selection criteria for a power plant
PowerPlantSelectionCriteria DeterminePowerPlantSelectionCriteria() const;
//! @brief Determine the selection criteria for a storage
StorageSelectionCriteria DetermineStorageSelectionCriteria() const;
//! @brief Determine the selection criteria for a metal extractor
ExtractorSelectionCriteria DetermineExtractorSelectionCriteria() const;
//! @brief Determine the selection criteria for static defences for the given sector
void DetermineStaticDefenceSelectionCriteria(StaticDefenceSelectionCriteria& selectionCriteria, const AAISector* sector) const;
//! A list of sectors with ceratain distance (in number of sectors) to base; 0 = sectors the ai uses to build its base, 1 = direct neighbours etc.
std::vector< std::list<AAISector*> > m_sectorsInDistToBase;
//! Holding max number of units of a category spotted at the same time (float as maximum values will slowly decay over time)
MobileTargetTypeValues m_maxSpottedCombatUnitsOfTargetType;
private:
//! @brief Recalculates the center of the base (needs to be called after sectors have been added or removed)
void UpdateCenterOfBase();
// returns true if sufficient ressources to build unit are availbale
bool RessourcesForConstr(int unit, int workertime = 175);
//! @brief Returns the movement type of the next combat unit that shall be ordered
AAIMovementType DetermineMovementTypeForCombatUnitConstruction(const GamePhase& gamePhase) const;
//! @brief Determines criteria for combat unit selection based on current economical and combat/pressure situation
UnitSelectionCriteria DetermineCombatUnitSelectionCriteria() const;
//! The combat power of all mobile units against the different target types
MobileTargetTypeValues m_totalMobileCombatPower;
//! Ratio of cells with flat land of all base sectors (ranging from 0 (none) to 1(all))
float m_baseFlatLandRatio;
//! Ratio of cells with water of all base sectors (ranging from 0 (none) to 1(all))
float m_baseWaterRatio;
//! Center of base (mean value of centers of all base sectors) in build map coordinates
MapPos m_centerOfBase;
//! Average stored metal over the last AAIConfig::INCOME_SAMPLE_POINTS frames
SmoothedData m_metalAvailable;
//! Average stored energy over the last AAIConfig::INCOME_SAMPLE_POINTS frames
SmoothedData m_energyAvailable;
//! Average metal income over the last AAIConfig::INCOME_SAMPLE_POINTS frames
SmoothedData m_metalIncome;
//! Average energy income over the last AAIConfig::INCOME_SAMPLE_POINTS frames
SmoothedData m_energyIncome;
//! Average metal surplus over the last AAIConfig::INCOME_SAMPLE_POINTS frames
SmoothedData m_metalSurplus;
//! Average energy surplus over the last AAIConfig::INCOME_SAMPLE_POINTS frames
SmoothedData m_energySurplus;
//! Counter by what enemy unit category own units/buidlings have been killed (counter is decreasing over time)
MobileTargetTypeValues m_recentlyAttackedByRates;
//! Frequency of attacks by different combat categories throughout the gane
static AttackedByRatesPerGamePhase s_attackedByRates;
//! Estimation how much the AAI instance is under pressure in the current game situation, values ranging from 0 (min) to 1 (max).
float m_estimatedPressureByEnemies;
AAI *ai;
};
#endif