/
NavigatorParameters.java
343 lines (310 loc) · 9.44 KB
/
NavigatorParameters.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package net.citizensnpcs.api.ai;
import java.util.ArrayList;
import java.util.List;
import net.citizensnpcs.api.ai.event.CancelReason;
import net.citizensnpcs.api.ai.event.NavigatorCallback;
import net.citizensnpcs.api.astar.AStarMachine;
import net.citizensnpcs.api.astar.pathfinder.BlockExaminer;
import com.google.common.collect.Lists;
public class NavigatorParameters implements Cloneable {
private AttackStrategy attackStrategy;
private boolean avoidWater;
private float baseSpeed = 1F;
private List<NavigatorCallback> callbacks = Lists.newArrayListWithExpectedSize(3);
private AttackStrategy defaultStrategy;
private double distanceMargin = 2F;
private List<BlockExaminer> examiners = Lists.newArrayList();
private float range;
private float speedModifier = 1F;
private int stationaryTicks = -1;
private StuckAction stuckAction;
private boolean useNewPathfinder;
/**
* Adds a {@link NavigatorCallback} that will be removed
* <em>immediately</em> after being called.
*
* @param callback
* The callback
*/
public NavigatorParameters addSingleUseCallback(NavigatorCallback callback) {
callbacks.add(callback);
return this;
}
/**
* @return The {@link AttackStrategy} currently in use or the
* {@link #defaultAttackStrategy()} if not configured (may return
* null)
*/
public AttackStrategy attackStrategy() {
return attackStrategy == null ? defaultStrategy : attackStrategy;
}
/**
* Sets the {@link AttackStrategy} for use when attacking entity targets.
*
* @param strategy
* The strategy to use
*/
public void attackStrategy(AttackStrategy strategy) {
attackStrategy = strategy;
}
/**
* @return Whether to avoid water while pathfinding
*/
public boolean avoidWater() {
return avoidWater;
}
/**
* Sets whether to avoid water while pathfinding
*
* @param avoidWater
* Whether to avoid water
*/
public NavigatorParameters avoidWater(boolean avoidWater) {
this.avoidWater = avoidWater;
return this;
}
/**
* @return The base movement speed
*/
public float baseSpeed() {
return baseSpeed;
}
/**
* Sets the base movement speed of the {@link Navigator}. Note that this is
* mob-specific and may not always be sane. Using {@link #speedModifier()}
* is preferred.
*
* @see #speedModifier()
* @param speed
* The new movement speed
*/
public NavigatorParameters baseSpeed(float speed) {
this.baseSpeed = speed;
return this;
}
/**
* @return All callbacks currently registered
*/
public Iterable<NavigatorCallback> callbacks() {
return callbacks;
}
/**
* Clears all current {@link BlockExaminer}s.
*/
public NavigatorParameters clearExaminers() {
examiners.clear();
return this;
}
@Override
@SuppressWarnings("unchecked")
public NavigatorParameters clone() {
try {
NavigatorParameters clone = (NavigatorParameters) super.clone();
if (callbacks instanceof ArrayList) {
clone.callbacks = (List<NavigatorCallback>) ((ArrayList<NavigatorCallback>) callbacks).clone();
}
if (examiners instanceof ArrayList) {
clone.examiners = (List<BlockExaminer>) ((ArrayList<BlockExaminer>) examiners).clone();
}
return clone;
} catch (CloneNotSupportedException e) {
return null;
}
}
/**
* Returns the configured <em>default</em> attack strategy, which tries to
* perform the most Minecraft-like attack on the target.
*
* @return The default strategy
*/
public AttackStrategy defaultAttackStrategy() {
return this.defaultStrategy;
}
/**
* Sets the default {@link AttackStrategy}.
*
* @param defaultStrategy
* The new default strategy
* @see #defaultAttackStrategy()
*/
public NavigatorParameters defaultAttackStrategy(AttackStrategy defaultStrategy) {
this.defaultStrategy = defaultStrategy;
return this;
}
/**
* Returns the distance margin or leeway that the {@link Navigator} will be
* able to stop from the target destination. The margin will be measured
* against the block distance squared.
*
* For example: if the distance margin were 2, then the {@link Navigator}
* could stop moving towards the target when it is 2 blocks squared away
* from it.
*
* @return The distance margin
*/
public double distanceMargin() {
return distanceMargin;
}
/**
* Sets the distance margin.
*
* @see #distanceMargin()
* @param newMargin
* The new distance margin
*/
public NavigatorParameters distanceMargin(double newMargin) {
distanceMargin = newMargin;
return this;
}
/**
* Adds the given {@link BlockExaminer}.
*
* @param examiner
* The BlockExaminer to add
*/
public NavigatorParameters examiner(BlockExaminer examiner) {
examiners.add(examiner);
return this;
}
/**
* Gets a copy of all current {@link BlockExaminer}s.
*
* @return An array of all current examiners
*/
public BlockExaminer[] examiners() {
return examiners.toArray(new BlockExaminer[examiners.size()]);
}
/**
* Modifieds the given speed value based on the current parameters.
*
* @param toModify
* The speed value to modify
* @return The modified speed
*/
public float modifiedSpeed(float toModify) {
return toModify * speedModifier();
}
/**
* @return The pathfinding range of the navigator in blocks.
* @see #range(float)
*/
public float range() {
return range;
}
/**
* Sets the pathfinding range in blocks. The pathfinding range determines
* how far away the {@link Navigator} will attempt to pathfind before giving
* up to save computation.
*
* @param range
* The new range
*/
public NavigatorParameters range(float range) {
this.range = range;
return this;
}
/**
* @return The modified movement speed as given by {@link #baseSpeed()}
* multiplied by {@link #speedModifier()}
*/
public float speed() {
return modifiedSpeed(baseSpeed);
}
/**
* Sets the base movement speed of the {@link Navigator}. Note that this is
* mob-specific and may not always be sane. Using {@link #speedModifier()}
* is preferred.
*
* @see #speedModifier()
* @param speed
* The new movement speed
* @deprecated @see {@link #baseSpeed(float)}
*/
@Deprecated
public NavigatorParameters speed(float speed) {
this.baseSpeed = speed;
return this;
}
/**
* @return The movement speed modifier
* @see #speedModifier(float)
*/
public float speedModifier() {
return speedModifier;
}
/**
* Sets the movement speed modifier of the {@link Navigator}. This is a
* percentage modifier that alters the movement speed returned in
* {@link #speed()}.
*
* @param percent
* The new speed modifier
*/
public NavigatorParameters speedModifier(float percent) {
speedModifier = percent;
return this;
}
/**
* @return The number of stationary ticks
* @see #stationaryTicks(int)
*/
public int stationaryTicks() {
return stationaryTicks;
}
/**
* Sets the number of stationary ticks before navigation is cancelled with a
* {@link CancelReason} of STUCK.
*
* @param ticks
* The new number of stationary ticks
*/
public NavigatorParameters stationaryTicks(int ticks) {
stationaryTicks = ticks;
return this;
}
/**
* Gets the {@link StuckAction} of these parameters. This will be run when
* the navigation is stuck and must either be fixed up or cancelled.
*
* @return The current stuck action
*/
public StuckAction stuckAction() {
return stuckAction;
}
/**
* Sets the {@link StuckAction} of the parameters.
*
* @param action
* The new stuck action
* @see #stuckAction()
*/
public NavigatorParameters stuckAction(StuckAction action) {
stuckAction = action;
return this;
}
/**
* @see #useNewPathfinder(boolean)
* @return Whether to use the new pathfinder
*/
public boolean useNewPathfinder() {
return useNewPathfinder;
}
/**
* Sets whether or not to use an A* pathfinder defined in
* {@link AStarMachine} for pathfinding.
*
* If this is set to false, then the Minecraft pathfinder will be used,
* which may or may not be more consistent.
*
* Note that certain API features will not be possible if this is set to
* false - this includes {@link #examiner(BlockExaminer)} and
* {@link #distanceMargin(double)}.
*
* @param use
* Whether to use the A* pathfinder
*/
public NavigatorParameters useNewPathfinder(boolean use) {
useNewPathfinder = use;
return this;
}
}