/
WanderGoal.java
160 lines (141 loc) · 5.17 KB
/
WanderGoal.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
package net.citizensnpcs.api.ai.goals;
import java.util.Random;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import ch.ethz.globis.phtree.PhTreeSolid;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.Goal;
import net.citizensnpcs.api.ai.event.NavigationCompleteEvent;
import net.citizensnpcs.api.ai.tree.Behavior;
import net.citizensnpcs.api.ai.tree.BehaviorGoalAdapter;
import net.citizensnpcs.api.ai.tree.BehaviorStatus;
import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer;
import net.citizensnpcs.api.npc.NPC;
/**
* A sample {@link Goal}/{@link Behavior} that will wander within a certain radius or {@link QuadTree}.
*/
public class WanderGoal extends BehaviorGoalAdapter implements Listener {
private int delay;
private int delayedTicks;
private final Function<NPC, Location> fallback;
private boolean forceFinish;
private final NPC npc;
private boolean paused;
private final Random random = new Random();
private final Supplier<PhTreeSolid<Boolean>> tree;
private int xrange;
private int yrange;
private WanderGoal(NPC npc, int xrange, int yrange, Supplier<PhTreeSolid<Boolean>> tree,
Function<NPC, Location> fallback) {
this.npc = npc;
this.xrange = xrange;
this.yrange = yrange;
this.tree = tree;
this.fallback = fallback;
}
private Location findRandomPosition() {
Location base = npc.getEntity().getLocation();
Location found = null;
for (int i = 0; i < 10; i++) {
int x = base.getBlockX() + random.nextInt(2 * xrange) - xrange;
int y = base.getBlockY() + random.nextInt(2 * yrange) - yrange;
int z = base.getBlockZ() + random.nextInt(2 * xrange) - xrange;
Block block = base.getWorld().getBlockAt(x, y, z);
if (MinecraftBlockExaminer.canStandOn(block)) {
if ((block.getRelative(BlockFace.UP).isLiquid() || block.getRelative(0, 2, 0).isLiquid())
&& npc.getNavigator().getDefaultParameters().avoidWater()) {
continue;
}
long[] pt = { x, y, z };
if (tree != null && tree.get() != null && !tree.get().queryIntersect(pt, pt).hasNext()) {
continue;
}
found = block.getLocation().add(0, 1, 0);
break;
}
}
if (found == null && fallback != null) {
return fallback.apply(npc);
}
return found;
}
@EventHandler
public void onFinish(NavigationCompleteEvent event) {
forceFinish = true;
}
public void pause() {
this.paused = true;
}
@Override
public void reset() {
delayedTicks = delay;
forceFinish = false;
HandlerList.unregisterAll(this);
}
@Override
public BehaviorStatus run() {
if (!npc.getNavigator().isNavigating() || forceFinish)
return BehaviorStatus.SUCCESS;
return BehaviorStatus.RUNNING;
}
public void setDelay(int delay) {
this.delay = delay;
this.delayedTicks = delay;
}
public void setXYRange(int xrange, int yrange) {
this.xrange = xrange;
this.yrange = yrange;
}
@Override
public boolean shouldExecute() {
if (!npc.isSpawned() || npc.getNavigator().isNavigating() || paused)
return false;
if (delayedTicks-- > 0) {
return false;
}
Location dest = findRandomPosition();
if (dest == null)
return false;
npc.getNavigator().setTarget(dest);
CitizensAPI.registerEvents(this);
return true;
}
public void unpause() {
this.paused = false;
}
public static WanderGoal createWithNPC(NPC npc) {
return createWithNPCAndRange(npc, 10, 2);
}
public static WanderGoal createWithNPCAndRange(NPC npc, int xrange, int yrange) {
return createWithNPCAndRangeAndTree(npc, xrange, yrange, null);
}
public static WanderGoal createWithNPCAndRangeAndTree(NPC npc, int xrange, int yrange,
Supplier<PhTreeSolid<Boolean>> tree) {
return createWithNPCAndRangeAndTreeAndFallback(npc, xrange, yrange, tree, null);
}
/**
* The full builder method.
*
* @param npc
* the NPC to wander
* @param xrange
* x/z range, in blocks
* @param yrange
* y range, in blocks
* @param tree
* an optional {@link PhTreeSolid} supplier to allow only wandering within a certain {@link PhTreeSolid}
* @param fallback
* an optional fallback location
* @return the built goal
*/
public static WanderGoal createWithNPCAndRangeAndTreeAndFallback(NPC npc, int xrange, int yrange,
Supplier<PhTreeSolid<Boolean>> tree, Function<NPC, Location> fallback) {
return new WanderGoal(npc, xrange, yrange, tree, fallback);
}
}