Skip to content

Commit 24438fc

Browse files
author
Bytekeeper
committed
Small improvements for closestTo
1 parent 338f04c commit 24438fc

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

src/main/java/org/bk/ass/query/UnitFinder.java

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public class UnitFinder<U> extends AbstractCollection<U> {
3131
private final DistanceProvider distanceProvider;
3232
private static final DistanceProvider EUCLIDEAN_DISTANCE =
3333
(ax, ay, bx, by) -> (int) Math.sqrt((float) (bx - ax) * (bx - ax) + (by - ay) * (by - ay));
34-
/**
35-
* When using this, all radius queries need to be made with the squared radius
36-
*/
34+
/**
35+
* When using this, all radius queries need to be made with the squared radius
36+
*/
3737
public static final DistanceProvider EUCLIDEAN_DISTANCE_SQUARED =
3838
(ax, ay, bx, by) -> (bx - ax) * (bx - ax) + (by - ay) * (by - ay);
3939
/** Use this to query using the distance approximation used in OpenBW */
@@ -119,20 +119,20 @@ public Collection<U> inArea(int ax, int ay, int bx, int by, Predicate<U> criteri
119119
}
120120

121121
private Stream<Entry<PositionAndId, U>> subMapOfArea(int ax, int ay, int bx, int by) {
122-
return map
123-
.subMap(
124-
new PositionAndId(-1, ax, ay), true, new PositionAndId(Integer.MAX_VALUE, bx, by), true)
125-
.entrySet().stream()
126-
.filter(u -> u.getKey().y <= by && u.getKey().y >= ay);
122+
return map
123+
.subMap(
124+
new PositionAndId(-1, ax, ay), true, new PositionAndId(Integer.MAX_VALUE, bx, by), true)
125+
.entrySet().stream()
126+
.filter(u -> u.getKey().y <= by && u.getKey().y >= ay);
127127
}
128128

129129
private Stream<Entry<PositionAndId, U>> subMapOfArea(
130130
int ax, int ay, int bx, int by, Predicate<U> criteria) {
131-
return map
132-
.subMap(
133-
new PositionAndId(-1, ax, ay), true, new PositionAndId(Integer.MAX_VALUE, bx, by), true)
134-
.entrySet().stream()
135-
.filter(u -> u.getKey().y <= by && u.getKey().y >= ay && criteria.test(u.getValue()));
131+
return map
132+
.subMap(
133+
new PositionAndId(-1, ax, ay), true, new PositionAndId(Integer.MAX_VALUE, bx, by), true)
134+
.entrySet().stream()
135+
.filter(u -> u.getKey().y <= by && u.getKey().y >= ay && criteria.test(u.getValue()));
136136
}
137137

138138
/**
@@ -170,19 +170,18 @@ public Collection<U> inRadius(U unit, int radius) {
170170
public Optional<U> closestTo(int x, int y) {
171171
PositionAndId query = new PositionAndId(-1, x, y);
172172
int squareHSize = Integer.MAX_VALUE;
173-
Entry<PositionAndId, U> lowerBound = map.lowerEntry(query);
173+
PositionAndId lowerBound = map.lowerKey(query);
174174
if (lowerBound != null) {
175-
int lx = lowerBound.getKey().x;
176-
int ly = lowerBound.getKey().y;
177-
squareHSize = max(abs(lx - x), abs(ly - y));
175+
int lx = lowerBound.x;
176+
int ly = lowerBound.y;
177+
squareHSize = max(x - lx, abs(ly - y));
178178
}
179-
Entry<PositionAndId, U> higherBound = map.higherEntry(query);
179+
PositionAndId higherBound = map.higherKey(query);
180180
if (higherBound != null) {
181-
int hx = higherBound.getKey().x;
182-
int hy = higherBound.getKey().y;
183-
squareHSize = min(squareHSize, max(abs(hx - x), abs(hy - y)));
184-
}
185-
if (squareHSize == Integer.MAX_VALUE) {
181+
int hx = higherBound.x;
182+
int hy = higherBound.y;
183+
squareHSize = min(squareHSize, max(hx - x, abs(hy - y)));
184+
} else if (lowerBound == null) {
186185
return Optional.empty();
187186
}
188187
return subMapOfArea(x - squareHSize, y - squareHSize, x + squareHSize, y + squareHSize)
@@ -204,7 +203,7 @@ public Optional<U> closestTo(int x, int y, Predicate<U> criteria) {
204203
if (lowerBound != null) {
205204
int lx = lowerBound.getKey().x;
206205
int ly = lowerBound.getKey().y;
207-
squareHSize = max(abs(lx - x), abs(ly - y));
206+
squareHSize = max(x - lx, abs(ly - y));
208207
}
209208
Entry<PositionAndId, U> higherBound = map.higherEntry(query);
210209
while (higherBound != null && !criteria.test(higherBound.getValue())) {
@@ -213,9 +212,8 @@ public Optional<U> closestTo(int x, int y, Predicate<U> criteria) {
213212
if (higherBound != null) {
214213
int hx = higherBound.getKey().x;
215214
int hy = higherBound.getKey().y;
216-
squareHSize = min(squareHSize, max(abs(hx - x), abs(hy - y)));
217-
}
218-
if (squareHSize == Integer.MAX_VALUE) {
215+
squareHSize = min(squareHSize, max(hx - x, abs(hy - y)));
216+
} else if (lowerBound == null) {
219217
return Optional.empty();
220218
}
221219
return subMapOfArea(

0 commit comments

Comments
 (0)