forked from yanntm/YoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistUtil.cpp
More file actions
67 lines (57 loc) · 1.88 KB
/
DistUtil.cpp
File metadata and controls
67 lines (57 loc) · 1.88 KB
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
#include "DistUtil.h"
using namespace sc2;
namespace sc2util {
void sortByDistanceTo(Units & units, const Point2D & start) {
std::sort(units.begin(), units.end(), [start](const Unit * a, const Unit * b) { return DistanceSquared2D(start, a->pos) < DistanceSquared2D(start, b->pos); });
}
std::vector<int> sortByDistanceTo(std::valarray<float> matrix, int ti, size_t sz) {
std::vector<int> byDist;
for (int i = 0; i < sz; i++) {
byDist.push_back(i);
}
std::sort(byDist.begin(), byDist.end(), [matrix, ti, sz](int a, int b) { return matrix[ti*sz + a] < matrix[ti*sz + b]; });
return byDist;
}
std::valarray<float> computeDistanceMatrix(const std::vector<Point3D> expansions, QueryInterface * query) {
std::vector <sc2::QueryInterface::PathingQuery> queries;
auto sz = expansions.size();
// create the set of queries
for (int i = 0; i < sz; i++) {
for (int j = i + 1; j < sz; j++) {
queries.push_back({ 0, expansions[i], expansions[j] });
}
}
// send the query and wait for answer
std::vector<float> distances = query->PathingDistance(queries);
for (auto & f : distances) {
if (f == 0) {
f = std::numeric_limits<float>::max();
}
}
std::valarray<float> matrix(sz *sz); // no more, no less, than a matrix
auto dit = distances.begin();
for (int i = 0; i < sz; i++) {
// set diagonal to 0
matrix[i*sz + i] = 0;
for (int j = i + 1; j < sz; j++, dit++) {
matrix[i*sz + j] = *dit;
matrix[j*sz + i] = *dit;
}
}
return matrix;
}
const Point2D cog(const std::vector<Point2D>& points) {
if (points.size() == 0) {
return Point2D(0, 0);
// return (Observation()->GetGameInfo().playable_min + Observation()->GetGameInfo().playable_max) / 2;
}
Point2D targetc = points.front();
float div = 1;
for (auto it = ++points.begin(); it != points.end(); ++it) {
targetc += *it;
div++;
}
targetc /= div;
return targetc;
}
}