forked from Error323/E323AI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCThreatMap.cpp
222 lines (182 loc) · 5.74 KB
/
CThreatMap.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
#include "CThreatMap.h"
#include <math.h>
#include "CAI.h"
#include "CUnitTable.h"
#include "CIntel.h"
#include "CUnit.h"
#include "CGroup.h"
#include "MathUtil.h"
CThreatMap::CThreatMap(AIClasses *ai) {
this->ai = ai;
X = ai->cb->GetMapWidth() / (HEIGHT2SLOPE*I_MAP_RES);
Z = ai->cb->GetMapHeight() / (HEIGHT2SLOPE*I_MAP_RES);
REAL = HEIGHT2SLOPE * HEIGHT2REAL * I_MAP_RES;
units = &ai->unitIDs[0]; // save about 4x32KB of memory
maps[TMT_AIR] = new float[X*Z];
maps[TMT_SURFACE] = new float[X*Z];
#if !defined(BUILDING_AI_FOR_SPRING_0_81_2)
handles[TMT_AIR] = ai->cb->DebugDrawerAddOverlayTexture(maps[TMT_AIR], X, Z);
ai->cb->DebugDrawerSetOverlayTexturePos(handles[TMT_AIR], -0.95f, -0.5f);
ai->cb->DebugDrawerSetOverlayTextureSize(handles[TMT_AIR], 0.4f, 0.4f);
ai->cb->DebugDrawerSetOverlayTextureLabel(handles[TMT_AIR], "Air ThreatMap");
handles[TMT_SURFACE] = ai->cb->DebugDrawerAddOverlayTexture(maps[TMT_SURFACE], X, Z);
ai->cb->DebugDrawerSetOverlayTexturePos(handles[TMT_SURFACE], 0.55f, -0.5f);
ai->cb->DebugDrawerSetOverlayTextureSize(handles[TMT_SURFACE], 0.4f, 0.4f);
ai->cb->DebugDrawerSetOverlayTextureLabel(handles[TMT_SURFACE], "Surface ThreatMap");
#endif
drawMap = TMT_NONE;
reset();
}
CThreatMap::~CThreatMap() {
std::map<ThreatMapType,float*>::iterator i;
for (i = maps.begin(); i != maps.end(); i++)
delete[] i->second;
}
void CThreatMap::reset() {
float *map;
std::map<ThreatMapType,float*>::iterator i;
for (i = maps.begin(); i != maps.end(); i++) {
maxPower[i->first] = 0.0f;
map = i->second;
for (int i = 0; i < X*Z; i++)
map[i] = 1.0f;
}
}
float *CThreatMap::getMap(ThreatMapType type) {
std::map<ThreatMapType,float*>::iterator i = maps.find(type);
if (i == maps.end())
return NULL;
return i->second;
}
float CThreatMap::getThreat(float3 ¢er, float radius, ThreatMapType type) {
int i = center.z / REAL;
int j = center.x / REAL;
float *map = maps[type];
if (radius < EPS)
return map[ID(j,i)];
int R = ceil(radius / REAL);
float power = 0.0f;
for (int z = -R; z <= R; z++) {
int zz = z + i;
if (zz > Z-1 || zz < 0)
continue;
for (int x = -R; x <= R; x++) {
int xx = x+j;
if (xx < X-1 && xx >= 0)
power += map[ID(xx,zz)];
}
}
//return power/(2.0f*R*M_PI);
return power / (R*R*M_PI);
}
float CThreatMap::getThreat(float3 ¢er, float radius, CGroup *group) {
unsigned int cats = group->cats;
ThreatMapType type;
if (cats&LAND)
type = TMT_SURFACE;
else
type = TMT_AIR;
return getThreat(center, radius, type);
}
void CThreatMap::update(int frame) {
std::list<ThreatMapType> activeTypes;
std::list<ThreatMapType>::iterator tmi;
reset();
int numUnits = ai->cbc->GetEnemyUnits(units, MAX_UNITS_AI);
/* Add enemy threats */
for (int i = 0; i < numUnits; i++) {
const int uid = units[i];
const UnitDef *ud = ai->cbc->GetUnitDef(uid);
if (ud == NULL)
continue;
const UnitType *ut = UT(ud->id);
if (!(ut->cats&ATTACKER) || ai->cbc->IsUnitParalyzed(uid) || ai->cbc->UnitBeingBuilt(uid))
continue;
if ((ut->cats&AIR) && !(ut->cats&ASSAULT))
continue;
// FIXME: think smth cleverer
if (ud->maxWeaponRange > MAX_WEAPON_RANGE_FOR_TM)
continue;
activeTypes.clear();
if (ut->cats&ANTIAIR)
activeTypes.push_back(TMT_AIR);
// NOTE: assuming DEFENSE unit can shoot ground units
if (((ut->cats&AIR) && (ut->cats&ASSAULT)) || (ut->cats&LAND && ((ut->cats&DEFENSE) || !(ut->cats&ANTIAIR))))
activeTypes.push_back(TMT_SURFACE);
/*
if (ut->cats&TORPEDO)
activeTypes.push_back(TMT_UNDERWATER);
*/
const float3 upos = ai->cbc->GetUnitPos(uid);
const float uRealX = upos.x/REAL;
const float uRealZ = upos.z/REAL;
const float range = (ud->maxWeaponRange + 100.0f)/REAL;
float powerT = ai->cbc->GetUnitPower(uid);
const float power = ut->cats&COMMANDER ? powerT/20.0f : powerT;
float3 pos(0.0f, 0.0f, 0.0f);
const int R = (int) ceil(range);
for (int z = -R; z <= R; z++) {
for (int x = -R; x <= R; x++) {
pos.x = x;
pos.z = z;
if (pos.Length2D() <= range) {
pos.x += uRealX;
pos.z += uRealZ;
const unsigned int mx = (unsigned int) round(pos.x);
const unsigned int mz = (unsigned int) round(pos.z);
if (mx < X && mz < Z) {
for (tmi = activeTypes.begin(); tmi != activeTypes.end(); tmi++) {
maps[*tmi][ID(mx,mz)] += power;
}
}
}
}
}
for (tmi = activeTypes.begin(); tmi != activeTypes.end(); tmi++) {
maxPower[*tmi] = std::max<float>(power, maxPower[*tmi]);
}
}
#if !defined(BUILDING_AI_FOR_SPRING_0_81_2)
if (ai->cb->IsDebugDrawerEnabled()) {
std::map<ThreatMapType,int>::iterator i;
for (i = handles.begin(); i != handles.end(); i++) {
float power = maxPower[i->first];
// normalize the data
for (int j = 0, N = X*Z; j < N; j++)
maps[i->first][j] /= power;
// update texturemap
ai->cb->DebugDrawerUpdateOverlayTexture(i->second, maps[i->first], 0, 0, X, Z);
}
}
#endif
if (drawMap != TMT_NONE)
visualizeMap(drawMap);
}
float CThreatMap::gauss(float x, float sigma, float mu) {
float a = 1.0f / (sigma * sqrt(2*M_PI));
float b = exp( -( pow(x-mu, 2) / (2*(pow(sigma,2))) ) );
return a * b;
}
void CThreatMap::visualizeMap(ThreatMapType type) {
float *map = maps[type];
float total = maxPower[type];
for (int z = 0; z < Z; z++) {
for (int x = 0; x < X; x++) {
if (map[ID(x,z)] > 1.0f + EPSILON) {
float3 p0(x*REAL, ai->cb->GetElevation(x*REAL,z*REAL), z*REAL);
float3 p1(p0);
p1.y += (map[ID(x,z)]/total) * 300.0f;
ai->cb->CreateLineFigure(p0, p1, 4, 10.0, MULTIPLEXER, 5);
}
}
}
ai->cb->SetFigureColor(5, 1.0f, 0.0f, 0.0f, 1.0f);
}
bool CThreatMap::switchDebugMode() {
int i = drawMap;
i++;
drawMap = (ThreatMapType)i;
if (drawMap >= TMT_LAST)
drawMap = TMT_NONE;
return drawMap != TMT_NONE;
}