-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAAIUnitStatistics.h
267 lines (213 loc) · 9.23 KB
/
AAIUnitStatistics.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
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
// -------------------------------------------------------------------------
// AAI
//
// A skirmish AI for the Spring engine.
// Copyright Alexander Seizinger
//
// Released under GPL license: see LICENSE.html for more information.
// -------------------------------------------------------------------------
#ifndef AAI_UNIT_STATISTICS_H
#define AAI_UNIT_STATISTICS_H
#include <vector>
#include <list>
#include "aidef.h"
#include "AAITypes.h"
#include "AAIUnitTypes.h"
#include "LegacyCpp/UnitDef.h"
//! @brief This class stores the frequency the AI got attacked by a certain combat category (surface, air, floater, submerged) in a certain game phase
class AttackedByRatesPerGamePhase
{
public:
AttackedByRatesPerGamePhase()
{
m_attackedByRatesPerGamePhase.resize(GamePhase::numberOfGamePhases);
}
void AddAttack(const GamePhase& gamePhase, const AAITargetType& attackerTargetType)
{
m_attackedByRatesPerGamePhase[gamePhase.GetArrayIndex()].AddValueForTargetType(attackerTargetType, 1.0f);
}
void SetAttackedByRate(const GamePhase& gamePhase, const AAITargetType& attackerTargetType, float rate)
{
m_attackedByRatesPerGamePhase[gamePhase.GetArrayIndex()].SetValueForTargetType(attackerTargetType, rate);
}
float GetAttackedByRate(const GamePhase& gamePhase, const AAITargetType& attackerTargetType) const
{
return m_attackedByRatesPerGamePhase[gamePhase.GetArrayIndex()].GetValueOfTargetType(attackerTargetType);
}
void DecreaseByFactor(const GamePhase& updateUntilGamePhase, float factor)
{
for(int i = 0; i <= updateUntilGamePhase.GetArrayIndex(); ++i)
m_attackedByRatesPerGamePhase[i].MultiplyValues(factor);
}
float GetAttackedByRateUntilEarlyPhase(const AAITargetType& attackerTargetType) const
{
static_assert(GamePhase::numberOfGamePhases >= 2, "Number of game phases does not fit to implementation");
return (m_attackedByRatesPerGamePhase[0].GetValueOfTargetType(attackerTargetType) + m_attackedByRatesPerGamePhase[1].GetValueOfTargetType(attackerTargetType));
}
private:
//! Frequency of attacks in a certain game phase
std::vector< MobileTargetTypeValues > m_attackedByRatesPerGamePhase;
};
//! @brief This class stores the frequency the AI got attacked by a certain combat category (surface, air, floater, submerged) in a certain game phase
class AttackedByRatesPerGamePhaseAndMapType
{
public:
AttackedByRatesPerGamePhaseAndMapType()
{
m_attackedByRatesPerGamePhaseAndMapType.resize(AAIMapType::numberOfMapTypes);
}
void SetAttackedByRate(const AAIMapType& mapType, const GamePhase& gamePhase, const AAITargetType& attackerTargetType, float rate)
{
m_attackedByRatesPerGamePhaseAndMapType[mapType.GetArrayIndex()].SetAttackedByRate(gamePhase, attackerTargetType, rate);
}
float GetAttackedByRate(const AAIMapType& mapType, const GamePhase& gamePhase, const AAITargetType& attackerTargetType) const
{
return m_attackedByRatesPerGamePhaseAndMapType[mapType.GetArrayIndex()].GetAttackedByRate(gamePhase, attackerTargetType);
}
AttackedByRatesPerGamePhase& GetAttackedByRates(const AAIMapType& mapType)
{
return m_attackedByRatesPerGamePhaseAndMapType[mapType.GetArrayIndex()];
}
float GetAttackedByRateUntilEarlyPhase(const AAIMapType& mapType, const AAITargetType& attackerTargetType) const
{
return m_attackedByRatesPerGamePhaseAndMapType[mapType.GetArrayIndex()].GetAttackedByRateUntilEarlyPhase(attackerTargetType);
}
private:
//! Frequency of attacks in a certain game phase
std::vector< AttackedByRatesPerGamePhase > m_attackedByRatesPerGamePhaseAndMapType;
};
//! This class stores the statistical data (min, max, average) for each Unit Category (e.g. build cost)
class StatisticalData
{
public:
StatisticalData() : m_minValue(0.0f), m_maxValue(0.0f), m_avgValue(0.0f), m_valueRange(0.0f), m_dataPoints(0u) {};
//! Updates min and max value if necessary, adds given value to avg value
void AddValue(float value)
{
if((value < m_minValue) || (m_dataPoints == 0u))
m_minValue = value;
if(value > m_maxValue)
m_maxValue = value;
m_avgValue += value;
++m_dataPoints;
}
//! Calculates avg value (assumes update() has been called before)
void Finalize()
{
if(m_dataPoints > 0u)
m_avgValue /= static_cast<float>(m_dataPoints);
if(m_dataPoints > 1u)
{
m_valueRange = m_maxValue - m_minValue;
if(m_valueRange < 0.00001f)
m_valueRange = 0.0f;
}
}
// getter functions
float GetMinValue() const { return m_minValue; }
float GetMaxValue() const { return m_maxValue; }
float GetAvgValue() const { return m_avgValue; }
unsigned int GetSampleSize() const { return m_dataPoints; }
//! @brief Returns the normalized (interval [0:1]) deviation from max value (value must be between min and max)
float GetNormalizedDeviationFromMax(float value) const
{
if(m_valueRange != 0.0f) // range only exactly 0.0f if insufficient number of data points or difference too small
return (m_maxValue - value) / m_valueRange;
else
return 0.0f;
}
//! @brief Returns the deviation from max value normalized by [max value:0] -> [0:1]
float GetDeviationFromMax(float value) const
{
if(m_maxValue != 0.0f) // range only exactly 0.0f if insufficient number of data points or difference too small
return 1.0f - value / m_maxValue; //(m_maxValue - value) / m_maxValue;
else
return 0.0f;
}
//! @brief Returns the normalized (interval [0:1]) deviation from max value (value must be between min and max)
float GetNormalizedSquaredDeviationFromMax(float value) const
{
if(m_valueRange != 0.0f) // range only exactly 0.0f if insufficient number of data points or difference too small
{
const float x = 1.0f - (m_maxValue - value) / m_valueRange;
return (1.0f - x*x);
}
else
return 0.0f;
}
//! @brief Returns the normalized (interval [0:1]) deviation from min value (value must be between min and max)
float GetNormalizedDeviationFromMin(float value) const
{
if(m_valueRange != 0.0f) // range only exactly 0.0f if insufficient number of data points or difference too small
return (value - m_minValue) / m_valueRange;
else
return 0.0f;
}
//! @brief Returns the deviation from 0 normalized by [0:max value] -> [0:1]
float GetDeviationFromZero(float value) const
{
if(m_maxValue != 0.0f) // range only exactly 0.0f if insufficient number of data points or difference too small
return value / m_maxValue;
else
return 0.0f;
}
//! @brief Returns the normalized (interval [0:1]) deviation from min value (value must be between min and max)
float GetNormalizedSquaredDeviationFromMin(float value) const
{
if(m_valueRange != 0.0f) // range only exactly 0.0f if insufficient number of data points or difference too small
{
const float x = 1.0f - (value - m_minValue) / m_valueRange;
return (1.0f - x*x);
}
else
return 0.0f;
}
private:
float m_minValue;
float m_maxValue;
float m_avgValue;
float m_valueRange;
unsigned int m_dataPoints;
};
class SensorStatistics
{
public:
void Init(const std::vector<const springLegacyAI::UnitDef*>& unitDefs, const std::vector<UnitTypeProperties>& unitProperties, const std::vector< std::list<UnitDefId> >& unitsInCategory);
//! Min,max,avg range for static radars
StatisticalData m_radarRanges;
//! Min,max,avg range for static sonar detectors
StatisticalData m_sonarRanges;
//! Min,max,avg range for static seismic detectors
StatisticalData m_seismicRanges;
//! Min,max,avg range for static radars
StatisticalData m_radarCosts;
//! Min,max,avg range for static sonar detectors
StatisticalData m_sonarCosts;
//! Min,max,avg range for static seismic detectors
StatisticalData m_seismicCosts;
};
class AAIUnitStatistics
{
public:
AAIUnitStatistics();
~AAIUnitStatistics();
//! Calculates values for given input data
void Init(const std::vector<const springLegacyAI::UnitDef*>& unitDefs, const std::vector<UnitTypeProperties>& unitProperties, const std::vector< std::list<UnitDefId> >& unitsInCategory, const std::vector< std::list<UnitDefId> >& unitsInCombatCategory);
const StatisticalData& GetUnitCostStatistics(const AAIUnitCategory& category) const { return m_unitCostStatistics[category.GetArrayIndex()]; }
const StatisticalData& GetUnitBuildtimeStatistics(const AAIUnitCategory& category) const { return m_unitBuildtimeStatistics[category.GetArrayIndex()]; }
const StatisticalData& GetUnitPrimaryAbilityStatistics(const AAIUnitCategory& category) const { return m_unitPrimaryAbilityStatistics[category.GetArrayIndex()]; }
const StatisticalData& GetUnitSecondaryAbilityStatistics(const AAIUnitCategory& category) const { return m_unitSecondaryAbilityStatistics[category.GetArrayIndex()]; }
const SensorStatistics& GetSensorStatistics() const { return m_sensorStatistics; }
private:
//! Min,max,avg cost for every unit category
std::vector<StatisticalData> m_unitCostStatistics;
//! Min,max,avg buildtime for every unit category
std::vector<StatisticalData> m_unitBuildtimeStatistics;
//! Min,max,avg: sight range for scouts, radar/sonar/artillery range, build speed for construction units/factories
std::vector<StatisticalData> m_unitPrimaryAbilityStatistics;
//! Min,max,avg: speed for scouts, mobile constructors, mobile artillery
std::vector<StatisticalData> m_unitSecondaryAbilityStatistics;
//! Statistical data for radar, sonar, and seismic sensores
SensorStatistics m_sensorStatistics;
};
#endif