-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCordsGen.java
More file actions
217 lines (188 loc) · 6.37 KB
/
CordsGen.java
File metadata and controls
217 lines (188 loc) · 6.37 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
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
import java.util.*;
public class CordsGen {
private final Queue<Integer> hints = new LinkedList<>();
private final Queue<Integer> goals = new LinkedList<>();
private final HashSet<Integer> visited = new HashSet<>();
private final Random random = new Random();
private final int edge;
private final TreasureHunt game;
public static boolean ab = false;
public CordsGen(TreasureHunt game) {
this.game = game;
this.edge = game.edgeLength;
}
public void addHint(int[] pos, int dist, int[] gran) {
remCord(pos);
hints.clear();
if (dist < 0)
return;
int glow = Math.max(1, gran[0]);
int ghigh = gran[1];
// pos.x - d -> pos.x + d
for (int x = Math.max(0, pos[0] - dist); x < Math.min(edge, pos[0] + dist); x++)
for (int y = Math.max(0, pos[1] - dist); y < Math.min(edge, pos[1] + dist); y++)
for (int z = Math.max(0, pos[2] - dist); z < Math.min(edge, pos[2] + dist); z++) {
int[] other = new int[]{x, y, z};
int iother = intify(other);
if (!visited.contains(iother) && mDist(pos, other) == dist)
hints.add(iother);
}
if (goals.isEmpty())
for (int x = Math.max(0, pos[0] - ghigh); x < Math.min(edge, pos[0] + ghigh); x++)
for (int y = Math.max(0, pos[1] - ghigh); y < Math.min(edge, pos[1] + ghigh); y++)
for (int z = Math.max(0, pos[2] - ghigh); z < Math.min(edge, pos[2] + ghigh); z++) {
int[] other = new int[]{x, y, z};
int iother = intify(other);
if (visited.contains(iother))
continue;
int dist2 = mDist(pos, other);
if (dist2 <= ghigh && dist2 >= glow)
goals.add(iother);
}
else
goals.removeIf(
other ->
mDist(pos, deintify(other)) < glow ||
mDist(pos, deintify(other)) > ghigh ||
visited.contains(other)
);
if (ghigh - glow <= 5)
sortGoals();
}
public int mDist(int[] u, int[] v) {
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += Math.abs(u[i] - v[i]);
}
return sum;
}
public double eDist(double[] u, int[] v) {
double sum = 0;
for (int i = 0; i < 3; i++) {
sum += Math.pow(u[i] - v[i], 2);
}
return Math.sqrt(sum);
}
public int numGoals() {
return goals.size();
}
public int numHints() {
return hints.size();
}
public void remCord(int[] pos) {
int cord = intify(pos);
visited.add(cord);
hints.remove(cord);
goals.remove(cord);
failsafe();
}
public void failsafe() {
if (hints.size() > 8000 || goals.size() > 8000 || visited.size() > 8000) {
System.err.println("Something went wrong:");
System.err.println("Hints: " + hints.size());
System.err.println("Goals: " + goals.size());
System.err.println("Visited: " + visited.size());
System.exit(1);
}
}
public int[] randCord() {
return new int[]{
random.nextInt(edge),
random.nextInt(edge),
random.nextInt(edge)
};
}
public int[] nextHint() {
return deintify(hints.poll());
}
public boolean hasHint() {
return !hints.isEmpty();
}
public int[] nextGoal() {
sortGoals();
return deintify(goals.poll());
}
public int intify(int[] pos) {
// x y z
// x * 10^2 + y * 10 + z
return pos[0] * 10_000 + pos[1] * 100 + pos[2];
}
public int[] deintify(Integer pos) {
if (pos == null)
return null;
int z = pos % 100;
int y = (pos / 100) % 100;
int x = (pos / 10_000) % 100;
return new int[]{x, y, z};
}
public void sortGoals() {
double x = 0;
double y = 0;
double z = 0;
for (int p : goals) {
int[] pos = deintify(p);
x += pos[0];
y += pos[1];
z += pos[2];
}
x /= goals.size();
y /= goals.size();
z /= goals.size();
double[] mean = new double[]{x, y, z};
TreeMap<Double, List<Integer>> dists = new TreeMap<>();
for (int p : goals) {
int[] pos = deintify(p);
double d = eDist(mean, pos);
if (!dists.containsKey(d))
dists.put(eDist(mean, pos), new ArrayList<>());
dists.get(eDist(mean, pos)).add(p);
}
Queue<Integer> goalsTMP = new LinkedList<>();
for (List<Integer> list : dists.values())
goalsTMP.addAll(list);
if (goalsTMP.size() != goals.size())
throw new RuntimeException("Something went wrong");
goals.clear();
goals.addAll(goalsTMP);
}
public void firstHint() {
int[] start;
start = randCord();
balloon(start);
}
public void balloon(int[] pos) {
int[] cur;
int dist = 0;
TreasureHunt.Collectible cl;
// Grow out from the start until we find a hint
while (
dist <= game.edgeLength * 3 &&
StudentAgent.remSteps() >= numGoals()
) {
addHint(pos, dist, new int[]{0, game.edgeLength * 3});
while (
hasHint() &&
StudentAgent.remSteps() >= numGoals()
) {
cur = nextHint();
StudentAgent.jump(game, cur);
cl = game.search();
if (cl == null)
remCord(cur);
else if (cl.isHint()) {
addHint(
cur,
(int) cl.getMessage()[0],
(int[]) cl.getMessage()[1]
);
return;
} else {
goals.clear();
goals.add(intify(cur));
return;
}
}
dist++;
}
}
}