@@ -31,9 +31,9 @@ public class UnitFinder<U> extends AbstractCollection<U> {
31
31
private final DistanceProvider distanceProvider ;
32
32
private static final DistanceProvider EUCLIDEAN_DISTANCE =
33
33
(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
+ */
37
37
public static final DistanceProvider EUCLIDEAN_DISTANCE_SQUARED =
38
38
(ax , ay , bx , by ) -> (bx - ax ) * (bx - ax ) + (by - ay ) * (by - ay );
39
39
/** 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
119
119
}
120
120
121
121
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 );
127
127
}
128
128
129
129
private Stream <Entry <PositionAndId , U >> subMapOfArea (
130
130
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 ()));
136
136
}
137
137
138
138
/**
@@ -170,19 +170,18 @@ public Collection<U> inRadius(U unit, int radius) {
170
170
public Optional <U > closestTo (int x , int y ) {
171
171
PositionAndId query = new PositionAndId (-1 , x , y );
172
172
int squareHSize = Integer .MAX_VALUE ;
173
- Entry < PositionAndId , U > lowerBound = map .lowerEntry (query );
173
+ PositionAndId lowerBound = map .lowerKey (query );
174
174
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 ));
178
178
}
179
- Entry < PositionAndId , U > higherBound = map .higherEntry (query );
179
+ PositionAndId higherBound = map .higherKey (query );
180
180
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 ) {
186
185
return Optional .empty ();
187
186
}
188
187
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) {
204
203
if (lowerBound != null ) {
205
204
int lx = lowerBound .getKey ().x ;
206
205
int ly = lowerBound .getKey ().y ;
207
- squareHSize = max (abs ( lx - x ) , abs (ly - y ));
206
+ squareHSize = max (x - lx , abs (ly - y ));
208
207
}
209
208
Entry <PositionAndId , U > higherBound = map .higherEntry (query );
210
209
while (higherBound != null && !criteria .test (higherBound .getValue ())) {
@@ -213,9 +212,8 @@ public Optional<U> closestTo(int x, int y, Predicate<U> criteria) {
213
212
if (higherBound != null ) {
214
213
int hx = higherBound .getKey ().x ;
215
214
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 ) {
219
217
return Optional .empty ();
220
218
}
221
219
return subMapOfArea (
0 commit comments