forked from konatakun/powerbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocatable.java
151 lines (131 loc) · 3.73 KB
/
Locatable.java
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
package org.powerbot.script;
import java.util.Comparator;
/**
* Locatable
* An entity located within the Runescape world map.
*/
public interface Locatable {
/**
* The current location of the entity.
*
* @return the entity's current location
*/
Tile tile();
/**
* Query
* A base for queries that make use of {@link Locatable} entities.
*
* @param <T> the type of query to return for chaining
*/
interface Query<T> {
/**
* Selects the entities which are located on the specified tile of the locatable into the query cache.
*
* @param t the locatable to check tile against
* @return {@code this} for the purpose of method chaining
*/
T at(Locatable t);
/**
* Selects the entities which are located within the specified radius of the local player into the query cache.
*
* @param radius the restriction radius
* @return {@code this} for the purpose of method chaining
*/
T within(double radius);
/**
* Selects the entities which are located within the specified radius of the locatable into the query cache.
*
* @param locatable the locatable to base the restriction radius around
* @param radius the restriction radius
* @return {@code this} for the purpose of method chaining
*/
T within(Locatable locatable, double radius);
/**
* Selects the entities which are located within the area into the query cache.
*
* @param area the restriction area
* @return {@code this} for the purpose of method chaining
*/
T within(Area area);
/**
* Sorts the query cache by ascending order by distance to the local player.
*
* @return {@code this} for the purpose of method chaining
*/
T nearest();
/**
* Sorts the query cache by ascending order by distance to the specified locatable.
*
* @param locatable the locatable to check distances from
* @return {@code this} for the purpose of method chaining
*/
T nearest(Locatable locatable);
}
/**
* Matcher
*/
class Matcher implements Filter<Locatable> {
private final Locatable target;
public Matcher(final Locatable target) {
this.target = target;
}
@Override
public boolean accept(final Locatable locatable) {
final Tile tile = locatable != null ? locatable.tile() : null;
final Tile target = this.target.tile();
return tile != null && target != null && target.equals(tile);
}
}
/**
* WithinRange
*/
class WithinRange implements Filter<Locatable> {
private final Locatable target;
private final double distance;
public WithinRange(final Locatable target, final double distance) {
this.target = target;
this.distance = distance;
}
@Override
public boolean accept(final Locatable l) {
final Tile tile = l != null ? l.tile() : null;
final Tile target = this.target.tile();
return tile != null && target != null && tile.distanceTo(target) <= distance;
}
}
/**
* WithinArea
*/
class WithinArea implements Filter<Locatable> {
private final Area area;
public WithinArea(final Area area) {
this.area = area;
}
@Override
public boolean accept(final Locatable l) {
final Tile tile = l != null ? l.tile() : null;
return tile != null && area.contains(tile);
}
}
/**
* NearestTo
*/
class NearestTo implements Comparator<Locatable> {
private final Locatable target;
public NearestTo(final Locatable target) {
this.target = target;
}
@Override
public int compare(final Locatable o1, final Locatable o2) {
final Tile target = this.target.tile();
final Tile t1 = o1.tile();
final Tile t2 = o2.tile();
if (target == null || t1 == null || t2 == null) {
return Integer.MAX_VALUE;
}
final double d1 = t1.distanceTo(target);
final double d2 = t2.distanceTo(target);
return Double.compare(d1, d2);
}
}
}