forked from hoijui/KAIK
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ThreatMap.cpp
278 lines (218 loc) · 8.2 KB
/
ThreatMap.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
#include <cassert>
#include <sstream>
#include "IncExternAI.h"
#include "IncGlobalAI.h"
#define LUA_THREATMAP_DEBUG 0
CR_BIND(CThreatMap, (NULL))
CR_REG_METADATA(CThreatMap, (
CR_MEMBER(threatCellsRaw),
CR_MEMBER(threatCellsVis),
CR_MEMBER(ai),
CR_RESERVED(8),
CR_POSTLOAD(PostLoad)
))
CThreatMap::CThreatMap(AIClasses* aic): ai(aic) {
if (ai) {
PostLoad();
#if (LUA_THREATMAP_DEBUG == 1)
std::stringstream luaDataStream;
// this approach does not work because Spring's loadstring()
// function does _not_ run in the global environment, which
// means transferring the threatmap values requires an extra
// temporary table
//
// luaDataStream << "threatMapW = " << width << ";\n";
// luaDataStream << "threatMapH = " << height << ";\n";
//
// however, writing to a table declared in GG is fine
luaDataStream << "GG.AIThreatMap[\"threatMapSizeX\"] = " << width << ";\n";
luaDataStream << "GG.AIThreatMap[\"threatMapSizeZ\"] = " << height << ";\n";
luaDataStream << "GG.AIThreatMap[\"threatMapResX\"] = " << THREATRES << ";\n";
luaDataStream << "GG.AIThreatMap[\"threatMapResZ\"] = " << THREATRES << ";\n";
luaDataStream << "\n";
luaDataStream << "local threatMapSizeX = GG.AIThreatMap[\"threatMapSizeX\"];\n";
luaDataStream << "local threatMapSizeZ = GG.AIThreatMap[\"threatMapSizeZ\"];\n";
luaDataStream << "local threatMapArray = GG.AIThreatMap;\n";
luaDataStream << "\n";
luaDataStream << "for row = 0, (threatMapSizeZ - 1) do\n";
luaDataStream << "\tfor col = 0, (threatMapSizeX - 1) do\n";
luaDataStream << "\t\tthreatMapArray[row * threatMapSizeX + col] = 0.0;\n";
luaDataStream << "\tend\n";
luaDataStream << "end\n";
std::string luaDataStr = luaDataStream.str();
ai->cb->CallLuaRules("[AI::KAIK::ThreatMap::Init]", -1, NULL);
ai->cb->CallLuaRules(luaDataStr.c_str(), -1, NULL);
#endif
}
currMaxThreat = 0.0f; // maximum threat (normalizer)
currSumThreat = 0.0f; // threat summed over all cells
currAvgThreat = 0.0f; // average threat over all cells
}
CThreatMap::~CThreatMap() {
threatCellsRaw.clear();
threatCellsVis.clear();
if (threatMapTexID >= 0) {
ai->cb->DebugDrawerDelOverlayTexture(threatMapTexID);
}
}
void CThreatMap::PostLoad() {
width = ai->cb->GetMapWidth() / THREATRES;
height = ai->cb->GetMapHeight() / THREATRES;
area = width * height;
assert(threatCellsRaw.empty());
assert(threatCellsVis.empty());
threatCellsRaw.resize(area, THREATVAL_BASE);
threatCellsVis.resize(area, THREATVAL_BASE);
threatMapTexID = -1;
}
void CThreatMap::ToggleVisOverlay() {
if (threatMapTexID < 0) {
std::stringstream threatMapLabel;
threatMapLabel << "[KAIK][";
threatMapLabel << ai->cb->GetMyTeam();
threatMapLabel << "][ThreatMap]";
// /cheat
// /debugdrawai
// /team N
// /spectator
// "KAIK::ThreatMap::DBG"
threatMapTexID = ai->cb->DebugDrawerAddOverlayTexture(&threatCellsVis[0], width, height);
ai->cb->DebugDrawerSetOverlayTexturePos(threatMapTexID, 0.50f, 0.25f);
ai->cb->DebugDrawerSetOverlayTextureSize(threatMapTexID, 0.40f, 0.40f);
ai->cb->DebugDrawerSetOverlayTextureLabel(threatMapTexID, (threatMapLabel.str()).c_str());
} else {
ai->cb->DebugDrawerDelOverlayTexture(threatMapTexID);
threatMapTexID = -1;
}
}
void CThreatMap::EnemyCreated(int enemyUnitID) {
assert(!threatCellsRaw.empty());
assert(ai->ccb->GetUnitDef(enemyUnitID) != NULL);
EnemyUnit enemyUnit;
enemyUnit.id = enemyUnitID;
enemyUnit.pos = ai->ccb->GetUnitPos(enemyUnitID);
enemyUnit.threat = GetEnemyUnitThreat(enemyUnit);
enemyUnit.range = (ai->ut->GetMaxRange(ai->ccb->GetUnitDef(enemyUnitID)) + 100.0f) / (SQUARE_SIZE * THREATRES);
enemyUnits[enemyUnitID] = enemyUnit;
AddEnemyUnit(enemyUnit, 1.0f);
}
void CThreatMap::EnemyFinished(int enemyUnitID) {
// no-op
}
void CThreatMap::EnemyDamaged(int enemyUnitID, int) {
assert(!threatCellsRaw.empty());
std::map<int, EnemyUnit>::iterator it = enemyUnits.find(enemyUnitID);
if (it != enemyUnits.end()) {
EnemyUnit& enemyUnit = it->second;
DelEnemyUnit(enemyUnit);
enemyUnit.threat = GetEnemyUnitThreat(enemyUnit);
AddEnemyUnit(enemyUnit, 1.0f);
}
}
void CThreatMap::EnemyDestroyed(int enemyUnitID, int) {
assert(!threatCellsRaw.empty());
std::map<int, EnemyUnit>::const_iterator it = enemyUnits.find(enemyUnitID);
if (it != enemyUnits.end()) {
const EnemyUnit& enemyUnit = it->second;
DelEnemyUnit(enemyUnit);
enemyUnits.erase(enemyUnitID);
}
}
void CThreatMap::AddEnemyUnit(const EnemyUnit& e, const float scale) {
const int posx = e.pos.x / (SQUARE_SIZE * THREATRES);
const int posy = e.pos.z / (SQUARE_SIZE * THREATRES);
assert(!threatCellsRaw.empty());
if (!MAPPOS_IN_BOUNDS(e.pos)) {
std::stringstream msg;
msg << "[CThreatMap::AddEnemyUnit][frame=" << ai->cb->GetCurrentFrame() << "][scale=" << scale << "]\n";
msg << "\tposition <" << e.pos.x << ", " << e.pos.z << "> of unit " << e.id;
msg << " (health " << ai->ccb->GetUnitHealth(e.id) << ") is out-of-bounds\n";
ai->GetLogger()->Log(msg.str());
}
const float threat = e.threat * scale;
const int rangeSq = (e.range * e.range + 0.5);
const int beginX = std::max(int(posx - e.range ), 0);
const int endX = std::min(int(posx + e.range + 1), width);
const int beginY = std::max(int(posy - e.range ), 0);
const int endY = std::min(int(posy + e.range + 1), height);
for (int myx = beginX; myx < endX; myx++) {
const int dxSq = (posx - myx) * (posx - myx);
for (int myy = beginY; myy < endY; myy++) {
const int dySq = (posy - myy) * (posy - myy);
if (dxSq + dySq <= rangeSq) {
assert((myy * width + myx) < threatCellsRaw.size());
assert((myy * width + myx) < threatCellsVis.size());
// MicroPather cannot deal with negative costs
// (which may arise due to floating-point drift)
// nor with zero-cost nodes (see MP::SetMapData,
// threat is not used as an additive overlay)
threatCellsRaw[myy * width + myx] = std::max(threatCellsRaw[myy * width + myx] + threat, THREATVAL_BASE);
threatCellsVis[myy * width + myx] = std::max(threatCellsVis[myy * width + myx] + threat, THREATVAL_BASE);
currSumThreat += threat;
}
}
}
currAvgThreat = currSumThreat / area;
}
void CThreatMap::DelEnemyUnit(const EnemyUnit& e) {
AddEnemyUnit(e, -1.0f);
}
void CThreatMap::Update() {
currMaxThreat = 0.0f;
// account for moving units
for (std::map<int, EnemyUnit>::iterator it = enemyUnits.begin(); it != enemyUnits.end(); ++it) {
EnemyUnit& e = it->second;
DelEnemyUnit(e);
e.pos = ai->ccb->GetUnitPos(e.id);
e.threat = GetEnemyUnitThreat(e);
AddEnemyUnit(e, 1.0f);
currMaxThreat = std::max(currMaxThreat, e.threat);
}
// TODO: staggered updates
if (threatMapTexID >= 0) {
if (currMaxThreat > 0.0f) {
for (int i = 0; i < area; i++) {
threatCellsVis[i] = (threatCellsRaw[i] - THREATVAL_BASE) / currMaxThreat;
}
ai->cb->DebugDrawerUpdateOverlayTexture(threatMapTexID, &threatCellsVis[0], 0, 0, width, height);
}
}
#if (LUA_THREATMAP_DEBUG == 1)
{
std::string luaDataStr;
std::stringstream luaDataStream;
luaDataStream << "local threatMapArray = GG.AIThreatMap;\n";
// just copy the entire map
for (int i = 0; i < area; i++) {
luaDataStream << "threatMapArray[" << i << "]";
luaDataStream << " = ";
luaDataStream << (threatCellsRaw[i] - THREATVAL_BASE);
luaDataStream << " / ";
luaDataStream << currMaxThreat;
luaDataStream << ";\n";
}
luaDataStr = luaDataStream.str();
ai->cb->CallLuaRules("[AI::KAIK::ThreatMap::Update]", -1, NULL);
ai->cb->CallLuaRules(luaDataStr.c_str(), -1, NULL);
}
#endif
}
float CThreatMap::GetEnemyUnitThreat(const EnemyUnit& e) const {
const UnitDef* ud = ai->ccb->GetUnitDef(e.id);
// only unarmed units do not register on the threat-map
if (ud == NULL || ud->weapons.empty()) {
return 0.0f;
}
const float health = ai->ccb->GetUnitHealth(e.id);
if (health <= .0f) {
return .0f;
}
const float dps = std::min(ai->ut->GetDPS(ud), 2000.0f);
const float dpsMod = health / ai->ccb->GetUnitMaxHealth(e.id);
return (dps * dpsMod);
}
float CThreatMap::ThreatAtThisPoint(const float3& pos) const {
const int z = int(pos.z / (SQUARE_SIZE * THREATRES));
const int x = int(pos.x / (SQUARE_SIZE * THREATRES));
return (threatCellsRaw[z * width + x] - THREATVAL_BASE);
}