-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPathFinder.cpp
468 lines (358 loc) · 11.8 KB
/
PathFinder.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <cassert>
#include "IncEngine.h"
#include "IncExternAI.h"
#include "IncGlobalAI.h"
CPathFinder::CPathFinder(AIClasses* aic) {
ai = aic;
// 8 = speed, 2 = precision
resScale = THREATRES;
squareSize = SQUARE_SIZE * resScale;
PathMapXSize = ai->cb->GetMapWidth() / resScale;
PathMapYSize = ai->cb->GetMapHeight() / resScale;
totalcells = PathMapXSize * PathMapYSize;
micropather = new MicroPather(this, ai, totalcells);
TestMoveArray = new bool[totalcells];
NumOfMoveTypes = 0;
HeightMap.resize(totalcells, 0.0f);
SlopeMap.resize(totalcells, 0.0f);
}
CPathFinder::~CPathFinder() {
delete[] TestMoveArray;
for (unsigned int i = 0; i != MoveArrays.size(); i++) {
delete[] MoveArrays[i];
}
delete micropather;
}
void CPathFinder::Init() {
AverageHeight = 0;
for (int x = 0; x < PathMapXSize; x++) {
for (int y = 0; y < PathMapYSize; y++) {
int index = y * PathMapXSize + x;
HeightMap[index] = *(ai->cb->GetHeightMap() + int(y * resScale * resScale * PathMapXSize + resScale * x));
if (HeightMap[index] > 0)
AverageHeight += HeightMap[index];
}
}
AverageHeight /= totalcells;
for (int i = 0; i < totalcells; i++) {
float maxslope = 0;
float tempslope;
if (i + 1 < totalcells && (i + 1) % PathMapXSize) {
tempslope = fabs(HeightMap[i] - HeightMap[i + 1]);
maxslope = std::max(tempslope, maxslope);
}
if (i - 1 >= 0 && i % PathMapXSize) {
tempslope = fabs(HeightMap[i] - HeightMap[i - 1]);
maxslope = std::max(tempslope, maxslope);
}
if (i + PathMapXSize < totalcells) {
tempslope = fabs(HeightMap[i] - HeightMap[i + PathMapXSize]);
maxslope = std::max(tempslope, maxslope);
}
if (i - PathMapXSize >= 0) {
tempslope = fabs(HeightMap[i] - HeightMap[i - PathMapXSize]);
maxslope = std::max(tempslope, maxslope);
}
SlopeMap[i] = maxslope * 6 / resScale;
if (SlopeMap[i] < 1)
SlopeMap[i] = 1;
}
// get all the different movetypes
std::vector<int> moveslopes;
std::vector<int> maxwaterdepths;
std::vector<int> minwaterdepths;
NumOfMoveTypes = ai->ut->moveDefs.size();
std::map<int, MoveData*>::const_iterator it;
for (it = ai->ut->moveDefs.begin(); it != ai->ut->moveDefs.end(); it++) {
const MoveData* md = it->second;
if (md->moveType == MoveData::Ship_Move) {
minwaterdepths.push_back(md->depth);
maxwaterdepths.push_back(10000);
} else {
minwaterdepths.push_back(-10000);
maxwaterdepths.push_back(md->depth);
}
moveslopes.push_back(md->maxSlope);
}
// add the last, tester movetype
minwaterdepths.push_back(-10000);
maxwaterdepths.push_back(20);
moveslopes.push_back(25);
NumOfMoveTypes++;
assert(moveslopes.size() == maxwaterdepths.size());
MoveArrays.resize(NumOfMoveTypes);
for (int m = 0; m < NumOfMoveTypes; m++) {
MoveArrays[m] = new bool[totalcells];
for (int i = 0; i < totalcells; i++) {
MoveArrays[m][i] = false;
if (SlopeMap[i] > moveslopes[m] || HeightMap[i] <= -maxwaterdepths[m] || HeightMap[i] >= -minwaterdepths[m]) {
MoveArrays[m][i] = false;
TestMoveArray[i] = true;
}
else {
MoveArrays[m][i] = true;
TestMoveArray[i] = true;
}
}
// make sure that the edges are no-go
for (int i = 0; i < PathMapXSize; i++) {
MoveArrays[m][i] = false;
}
for (int i = 0; i < PathMapYSize; i++) {
int k = i * PathMapXSize;
MoveArrays[m][k] = false;
}
for (int i = 0; i < PathMapYSize; i++) {
int k = i * PathMapXSize + PathMapXSize - 1;
MoveArrays[m][k] = false;
}
for (int i = 0; i < PathMapXSize; i++) {
int k = PathMapXSize * (PathMapYSize - 1) + i;
MoveArrays[m][k] = false;
}
}
}
void CPathFinder::CreateDefenseMatrix() {
int enemyStartUnitIDs[255] = {-1};
float3 enemyStartPositions[255] = {ZeroVector};
const int range = std::max(1.0f, sqrtf(float(PathMapXSize * PathMapYSize)) / THREATRES / 3);
const int rangeSq = range * range;
const int maskWidth = (2 * range + 1);
std::vector<float> costMask(maskWidth * maskWidth);
for (int x = 0; x < maskWidth; x++) {
for (int y = 0; y < maskWidth; y++) {
const int index = y * maskWidth + x;
const int distSq = (x - range) * (x - range) + (y - range) * (y - range);
if (distSq <= rangeSq) {
costMask[index] = ((distSq - rangeSq) * (distSq - rangeSq)) / (rangeSq * 2);
} else {
costMask[index] = 0.0f;
}
}
}
ai->dm->ChokeMapsByMovetype.resize(NumOfMoveTypes);
for (int m = 0; m < NumOfMoveTypes;m++) {
const int numEnemies = ai->ccb->GetEnemyUnits(enemyStartUnitIDs);
for (int i = 0; i < numEnemies; i++) {
enemyStartPositions[i] = ai->ccb->GetUnitPos(enemyStartUnitIDs[i]);
}
const float3& myPos = ai->cb->GetUnitPos(ai->uh->AllUnitsByCat[CAT_BUILDER].front());
const int reruns = 35;
ai->dm->ChokeMapsByMovetype[m].resize(totalcells);
micropather->SetMapData(MoveArrays[m], &ai->dm->ChokeMapsByMovetype[m][0], PathMapXSize, PathMapYSize);
for (int i = 0; i < totalcells; i++) {
ai->dm->ChokeMapsByMovetype[m][i] = 1;
}
// HACK:
// for each enemy start-unit, find a path to its position <N> times
// for each found path, deposit cost-crumbs at every second waypoint
// regions where many paths overlap indicate choke-points
if (numEnemies > 0 && m == PATHTOUSE) {
for (int r = 0; r < reruns; r++) {
for (int startPosIdx = 0; startPosIdx < numEnemies; startPosIdx++) {
void* startPos = Pos2Node(enemyStartPositions[startPosIdx]);
void* goalPos = Pos2Node(myPos);
if (micropather->Solve(startPos, goalPos, &path, &totalcost) != MicroPather::SOLVED) {
continue;
}
for (int i = 12; i < int(path.size() - 12); i++) {
if ((i % 2) == 0) { continue; }
int x, y;
Node2XY(path[i], &x, &y);
for (int myx = -range; myx <= range; myx++) {
const int actualx = x + myx;
if (actualx < 0 || actualx >= PathMapXSize) {
continue;
}
for (int myy = -range; myy <= range; myy++) {
const int actualy = y + myy;
const int cmIndex = actualy * PathMapXSize + actualx;
if (actualy < 0 || actualy >= PathMapYSize) {
continue;
}
ai->dm->ChokeMapsByMovetype[m][cmIndex] += costMask[(myy + range) * maskWidth + (myx + range)];
}
}
}
}
}
}
}
}
unsigned CPathFinder::Checksum() {
return micropather->Checksum();
}
void* CPathFinder::XY2Node(int x, int y) {
return (void*) (y * PathMapXSize + x);
}
void CPathFinder::Node2XY(void* node, int* x, int* y) {
size_t index = (size_t)node;
*y = index / PathMapXSize;
*x = index - (*y * PathMapXSize);
}
float3 CPathFinder::Node2Pos(void* node) {
const size_t index = (size_t)node;
float3 pos;
pos.z = (index / PathMapXSize) * squareSize;
pos.x = (index - ((index / PathMapXSize) * PathMapXSize)) * squareSize;
return pos;
}
void* CPathFinder::Pos2Node(float3 pos) {
return ((void*) (int(pos.z / SQUARE_SIZE / THREATRES) * PathMapXSize + int((pos.x / SQUARE_SIZE / THREATRES))));
}
/*
* radius is in full res.
* returns the path cost.
*/
float CPathFinder::MakePath(F3Vec& posPath, float3& startPos, float3& endPos, int radius) {
ai->math->TimerStart();
path.clear();
ai->math->F3MapBound(startPos);
ai->math->F3MapBound(endPos);
float pathCost = 0.0f;
const int ex = int(endPos.x / squareSize);
const int ey = int(endPos.z / squareSize);
const int sy = int(startPos.z / squareSize);
const int sx = int(startPos.x / squareSize);
radius /= squareSize;
if (micropather->FindBestPathToPointOnRadius(XY2Node(sx, sy), XY2Node(ex, ey), &path, &pathCost, radius) == MicroPather::SOLVED) {
posPath.reserve(path.size());
for (unsigned i = 0; i < path.size(); i++) {
float3 mypos = Node2Pos(path[i]);
mypos.y = ai->cb->GetElevation(mypos.x, mypos.z);
posPath.push_back(mypos);
}
}
return pathCost;
}
float CPathFinder::FindBestPath(F3Vec& posPath, float3& startPos, float maxRange, F3Vec& possibleTargets) {
float pathCost = 0.0f;
// <maxRange> must always be >= squareSize, otherwise
// <radius> will become 0 and the write to offsets[0]
// below is undefined
if (maxRange < float(squareSize))
return pathCost;
ai->math->TimerStart();
path.clear();
const unsigned int radius = maxRange / squareSize;
unsigned int offsetSize = 0;
std::vector<std::pair<int, int> > offsets;
std::vector<int> xend;
// make a list with the points that will count as end nodes
std::vector<void*> endNodes;
endNodes.reserve(possibleTargets.size() * radius * 10);
{
const unsigned int DoubleRadius = radius * 2;
const unsigned int SquareRadius = radius * radius;
xend.resize(DoubleRadius + 1);
offsets.resize(DoubleRadius * 5);
for (size_t a = 0; a < DoubleRadius + 1; a++) {
const float z = (int) (a - radius);
const float floatsqrradius = SquareRadius;
xend[a] = int(sqrt(floatsqrradius - z * z));
}
offsets[0].first = 0;
offsets[0].second = 0;
size_t index = 1;
size_t index2 = 1;
for (size_t a = 1; a < radius + 1; a++) {
int endPosIdx = xend[a];
int startPosIdx = xend[a - 1];
while (startPosIdx <= endPosIdx) {
assert(index < offsets.size());
offsets[index].first = startPosIdx;
offsets[index].second = a;
startPosIdx++;
index++;
}
startPosIdx--;
}
index2 = index;
for (size_t a = 0; a < index2 - 2; a++) {
assert(index < offsets.size());
assert(a < offsets.size());
offsets[index].first = offsets[a].first;
offsets[index].second = DoubleRadius - (offsets[a].second);
index++;
}
index2 = index;
for (size_t a = 0; a < index2; a++) {
assert(index < offsets.size());
assert(a < offsets.size());
offsets[index].first = -(offsets[a].first);
offsets[index].second = offsets[a].second;
index++;
}
for (size_t a = 0; a < index; a++) {
assert(a < offsets.size());
offsets[a].first = offsets[a].first; // ??
offsets[a].second = offsets[a].second - radius;
}
offsetSize = index;
}
for (unsigned int i = 0; i < possibleTargets.size(); i++) {
float3& f = possibleTargets[i];
int x, y;
// TODO: make the circle here
ai->math->F3MapBound(f);
Node2XY(Pos2Node(f), &x, &y);
for (unsigned int j = 0; j < offsetSize; j++) {
const int sx = x + offsets[j].first;
const int sy = y + offsets[j].second;
if (sx >= 0 && sx < PathMapXSize && sy >= 0 && sy < PathMapYSize) {
endNodes.push_back(XY2Node(sx, sy));
}
}
}
ai->math->F3MapBound(startPos);
if (micropather->FindBestPathToAnyGivenPoint(Pos2Node(startPos), endNodes, &path, &pathCost) == MicroPather::SOLVED) {
posPath.reserve(path.size());
for (unsigned i = 0; i < path.size(); i++) {
int x, y;
Node2XY(path[i], &x, &y);
float3 mypos = Node2Pos(path[i]);
mypos.y = ai->cb->GetElevation(mypos.x, mypos.z);
posPath.push_back(mypos);
}
}
return pathCost;
}
float CPathFinder::FindBestPathToRadius(std::vector<float3>& posPath, float3& startPos, float radiusAroundTarget, const float3& target) {
std::vector<float3> posTargets;
posTargets.push_back(target);
return (FindBestPath(posPath, startPos, radiusAroundTarget, posTargets));
}
bool CPathFinder::IsPositionReachable(const MoveData* md, const float3& pos) const {
if (md == 0) {
// aircraft or building
return true;
}
if (!MAPPOS_IN_BOUNDS(pos)) {
return false;
}
const float* hgtMap = ai->cb->GetHeightMap();
const float* slpMap = ai->cb->GetSlopeMap();
const int WH = ai->cb->GetMapWidth();
const int WS = WH >> 1;
const int xh = (pos.x / SQUARE_SIZE);
const int zh = (pos.z / SQUARE_SIZE);
const int xs = xh >> 1;
const int zs = zh >> 1;
bool heightOK = false;
bool slopeOK = false;
switch (md->moveType) {
case MoveData::Ship_Move: {
heightOK = (hgtMap[zh * WH + xh] < -md->depth);
slopeOK = true;
} break;
case MoveData::Ground_Move: {
heightOK = (hgtMap[zh * WH + xh] > -md->depth);
slopeOK = (slpMap[zs * WS + xs] < md->maxSlope);
} break;
case MoveData::Hover_Move: {
heightOK = true;
slopeOK = (slpMap[zs * WS + xs] < md->maxSlope);
} break;
}
return (heightOK && slopeOK);
}