forked from hoijui/KAIK
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDefenseMatrix.cpp
236 lines (194 loc) · 7.57 KB
/
DefenseMatrix.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
#include "IncCREG.h"
#include "IncExternAI.h"
#include "IncGlobalAI.h"
CR_BIND(CDefenseMatrix, (NULL))
CR_REG_METADATA(CDefenseMatrix, (
CR_MEMBER(ChokeMapsByMovetype),
CR_MEMBER(ChokePointArray),
CR_MEMBER(BuildMaskArray),
// CR_MEMBER(spotFinder),
CR_MEMBER(ai),
// These two only matter during one frame, at AI-Init mid-game -> do not care
// CR_MEMBER(defAddQueue),
// CR_MEMBER(defRemoveQueue),
CR_RESERVED(16),
CR_POSTLOAD(PostLoad)
))
CDefenseMatrix::CDefenseMatrix(AIClasses* ai)
: spotFinder(NULL)
, ai(ai)
{
}
void CDefenseMatrix::PostLoad() {
spotFinder = new CSpotFinder(ai, ai->pather->PathMapYSize, ai->pather->PathMapXSize);
spotFinder->SetBackingArray(&ChokePointArray.front(), ai->pather->PathMapYSize, ai->pather->PathMapXSize);
}
void CDefenseMatrix::Init() {
ChokePointArray.resize(ai->pather->totalcells);
// used to mask bad spots that workers can't build at
BuildMaskArray.resize(ai->pather->totalcells, 0);
ai->pather->CreateDefenseMatrix();
spotFinder = new CSpotFinder(ai, ai->pather->PathMapYSize, ai->pather->PathMapXSize);
spotFinder->SetBackingArray(&ChokePointArray.front(), ai->pather->PathMapYSize, ai->pather->PathMapXSize);
std::vector<DefPos>::const_iterator def;
for (def = defAddQueue.begin(); def != defAddQueue.end(); ++def) {
AddDefense(def->pos, def->def);
}
for (def = defRemoveQueue.begin(); def != defRemoveQueue.end(); ++def) {
RemoveDefense(def->pos, def->def);
}
defAddQueue.clear();
defRemoveQueue.clear();
}
void CDefenseMatrix::MaskBadBuildSpot(float3 pos) {
if (MAPPOS_IN_BOUNDS(pos)) {
const int f3multiplier = 8 * THREATRES;
const int x = (int) (pos.x / f3multiplier);
const int y = (int) (pos.z / f3multiplier);
BuildMaskArray[y * ai->pather->PathMapXSize + x] = 1;
}
}
float3 CDefenseMatrix::GetDefensePos(const UnitDef* def, float3 builderpos) {
ai->ut->UpdateChokePointArray();
int f3multiplier = 8 * THREATRES;
int Range = int(ai->ut->GetMaxRange(def) / f3multiplier);
int bestspotx = 0;
int bestspoty = 0;
float averagemapsize = sqrt(float(ai->pather->PathMapXSize * ai->pather->PathMapYSize)) * f3multiplier;
float bestscore_fast = 0.0f;
int bestspotx_fast = 0;
int bestspoty_fast = 0;
ai->math->TimerStart();
spotFinder->SetRadius(Range);
float* sumMap = spotFinder->GetSumMap();
// hack to find a good start
{
int x = (int) (builderpos.x / f3multiplier);
int y = (int) (builderpos.z / f3multiplier);
float fastSumMap = sumMap[y * ai->pather->PathMapXSize + x];
float3 spotpos = float3(x * f3multiplier, 0, y * f3multiplier);
float myscore = fastSumMap / (builderpos.distance2D(spotpos) + averagemapsize / 8) * ((ai->pather->HeightMap[y * ai->pather->PathMapXSize + x] + 200) / (ai->pather->AverageHeight + 10)) / (ai->thm->ThreatAtThisPoint(spotpos) + 0.01);
bestscore_fast = myscore;
bestspotx_fast = x;
bestspoty_fast = y;
}
int skipCount = 0;
int testCount = 0;
for (int x = 0; x < ai->pather->PathMapXSize / CACHEFACTOR; x++) {
for (int y = 0; y < ai->pather->PathMapYSize / CACHEFACTOR; y++) {
// KLOOTNOTE: SOMETIMES RETURNS UNINITIALIZED CRAP?
// (gdb) print cachePoint->y $2 = 219024104
// (gdb) print cachePoint->x $3 = -1215908928
CachePoint* cachePoint = spotFinder->GetBestCachePoint(x, y);
if (!cachePoint) {
return ZeroVector;
}
float bestScoreInThisBox = cachePoint->maxValueInBox;
// guess that this point is as good as posible
// make best posible build spot (nearest to builder)
float bestX = builderpos.x / f3multiplier;
float bestY = builderpos.z / f3multiplier;
if (bestX > x * CACHEFACTOR) {
if (bestX > (x * CACHEFACTOR + CACHEFACTOR)) {
bestX = x * CACHEFACTOR + CACHEFACTOR;
}
}
else {
bestX = x * CACHEFACTOR;
}
if (bestY > y * CACHEFACTOR) {
if (bestY > (y * CACHEFACTOR + CACHEFACTOR)) {
bestY = y * CACHEFACTOR + CACHEFACTOR;
}
}
else {
bestY = y * CACHEFACTOR;
}
float3 bestPosibleSpotpos = float3(bestX * f3multiplier, 0, bestY * f3multiplier);
// this must be guessed, set it to the best possible (slow)
float bestThreatAtThisPoint = 0.01 + ai->thm->GetAverageThreat() - 1;
float bestDistance = builderpos.distance2D(bestPosibleSpotpos);
float bestHeight = ai->pather->HeightMap[cachePoint->y * ai->pather->PathMapXSize + cachePoint->x] + 200;
float bestPosibleMyScore = bestScoreInThisBox / (bestDistance + averagemapsize / 4) * (bestHeight + 200) / bestThreatAtThisPoint;
// have a best posible score for all points inside the size of the cache box
// if this is better than the current known best, test if any point inside the box is better
if (bestPosibleMyScore > bestscore_fast) {
testCount++;
// must test all the points inside this box
for (int sx = x * CACHEFACTOR; sx < ai->pather->PathMapXSize && sx < (x * CACHEFACTOR + CACHEFACTOR); sx++) {
for (int sy = y * CACHEFACTOR; sy < ai->pather->PathMapYSize && sy < (y * CACHEFACTOR + CACHEFACTOR); sy++) {
float fastSumMap = sumMap[sy * ai->pather->PathMapXSize + sx];
float3 spotpos = float3(sx * f3multiplier, 0, sy * f3multiplier);
float myscore = fastSumMap / (builderpos.distance2D(spotpos) + averagemapsize / 4) * (ai->pather->HeightMap[sy * ai->pather->PathMapXSize + sx]+200) / (ai->thm->ThreatAtThisPoint(spotpos) + 0.01);
// THIS COULD BE REALLY SLOW!
if (myscore > bestscore_fast && BuildMaskArray[sy * ai->pather->PathMapXSize + sx] == 0 && ai->cb->CanBuildAt(def, spotpos)) {
bestscore_fast = myscore;
bestspotx_fast = sx;
bestspoty_fast = sy;
}
}
}
}
else {
// skip box
skipCount++;
}
}
}
bestspotx = bestspotx_fast;
bestspoty = bestspoty_fast;
return float3(bestspotx * f3multiplier, 0, bestspoty * f3multiplier);
}
void CDefenseMatrix::AddDefense(float3 pos, const UnitDef* def) {
if (!IsInitialized()) {
DefPos defPos = {pos, def};
defAddQueue.push_back(defPos);
return;
}
int f3multiplier = 8 * THREATRES;
int Range = int(ai->ut->GetMaxRange(def) / f3multiplier);
int squarerange = Range * Range;
int x, y;
ai->math->F32XY(pos, &x, &y, 8);
// TODO: test if this works
for (int myx = x - Range; myx <= x + Range; myx++) {
if (myx >= 0 && myx < ai->pather->PathMapXSize) {
for (int myy = y - Range; myy <= y + Range; myy++) {
int distance = int((x - myx) * (x - myx) + (y - myy) * (y - myy) - 0.5);
if (myy >= 0 && myy < ai->pather->PathMapYSize && (distance) <= squarerange) {
for (int i = 0; i < ai->pather->NumOfMoveTypes;i++) {
ChokeMapsByMovetype[i][myy * ai->pather->PathMapXSize + myx] /= 2;
}
}
}
}
}
spotFinder->InvalidateSumMap(x, y, Range + 1);
// ai->debug->MakeBWTGA(Chokepointmap, ai->thm->GetThreatMapWidth(), ai->thm->GetThreatMapHeight(), "DebugPathMatrix", 1);
}
void CDefenseMatrix::RemoveDefense(float3 pos, const UnitDef* def) {
if (!IsInitialized()) {
DefPos defPos = {pos, def};
defRemoveQueue.push_back(defPos);
return;
}
int f3multiplier = 8 * THREATRES;
int Range = int(ai->ut->GetMaxRange(def) / f3multiplier);
int squarerange = Range * Range;
int x, y;
ai->math->F32XY(pos, &x, &y, 8);
// TODO: test if this works
for (int myx = x - Range; myx <= x + Range; myx++) {
if (myx >= 0 && myx < ai->pather->PathMapXSize) {
for (int myy = y - Range; myy <= y + Range; myy++) {
int distance = int((x - myx) * (x - myx) + (y - myy) * (y - myy) - 0.5);
if (myy >= 0 && myy < ai->pather->PathMapYSize && (distance) <= squarerange) {
for (int i = 0; i < ai->pather->NumOfMoveTypes;i++) {
ChokeMapsByMovetype[i][myy * ai->pather->PathMapXSize + myx] *= 2;
}
}
}
}
}
spotFinder->InvalidateSumMap(x, y, Range);
}