2017-12-30 08:36:36 +01:00
|
|
|
package net.citizensnpcs.npc.ai;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bukkit.Effect;
|
|
|
|
import org.bukkit.Location;
|
2020-04-08 17:59:49 +02:00
|
|
|
import org.bukkit.Material;
|
2021-06-25 11:01:18 +02:00
|
|
|
import org.bukkit.block.Block;
|
2020-07-27 09:56:42 +02:00
|
|
|
import org.bukkit.entity.LivingEntity;
|
2017-12-30 08:36:36 +01:00
|
|
|
import org.bukkit.util.Vector;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
2018-03-15 09:46:57 +01:00
|
|
|
import net.citizensnpcs.Settings.Setting;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.api.ai.AbstractPathStrategy;
|
|
|
|
import net.citizensnpcs.api.ai.NavigatorParameters;
|
|
|
|
import net.citizensnpcs.api.ai.TargetType;
|
|
|
|
import net.citizensnpcs.api.ai.event.CancelReason;
|
|
|
|
import net.citizensnpcs.api.astar.AStarMachine;
|
2020-04-08 17:59:49 +02:00
|
|
|
import net.citizensnpcs.api.astar.pathfinder.BlockExaminer;
|
|
|
|
import net.citizensnpcs.api.astar.pathfinder.BlockSource;
|
|
|
|
import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.api.astar.pathfinder.Path;
|
2020-04-08 17:59:49 +02:00
|
|
|
import net.citizensnpcs.api.astar.pathfinder.PathPoint;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.api.astar.pathfinder.VectorGoal;
|
|
|
|
import net.citizensnpcs.api.astar.pathfinder.VectorNode;
|
|
|
|
import net.citizensnpcs.api.npc.NPC;
|
|
|
|
import net.citizensnpcs.util.NMS;
|
2020-07-27 09:56:42 +02:00
|
|
|
import net.citizensnpcs.util.Util;
|
2017-12-30 08:36:36 +01:00
|
|
|
|
|
|
|
public class AStarNavigationStrategy extends AbstractPathStrategy {
|
|
|
|
private final Location destination;
|
2020-05-06 12:08:12 +02:00
|
|
|
private int iterations;
|
2017-12-30 08:36:36 +01:00
|
|
|
private final NPC npc;
|
|
|
|
private final NavigatorParameters params;
|
|
|
|
private Path plan;
|
|
|
|
private boolean planned = false;
|
2020-05-06 12:08:12 +02:00
|
|
|
private AStarMachine<VectorNode, Path>.AStarState state;
|
2017-12-30 08:36:36 +01:00
|
|
|
private Vector vector;
|
|
|
|
|
|
|
|
public AStarNavigationStrategy(NPC npc, Iterable<Vector> path, NavigatorParameters params) {
|
|
|
|
super(TargetType.LOCATION);
|
|
|
|
List<Vector> list = Lists.newArrayList(path);
|
|
|
|
this.params = params;
|
|
|
|
this.destination = list.get(list.size() - 1).toLocation(npc.getStoredLocation().getWorld());
|
|
|
|
this.npc = npc;
|
|
|
|
setPlan(new Path(list));
|
|
|
|
}
|
|
|
|
|
|
|
|
public AStarNavigationStrategy(NPC npc, Location dest, NavigatorParameters params) {
|
|
|
|
super(TargetType.LOCATION);
|
|
|
|
this.params = params;
|
|
|
|
this.destination = dest;
|
|
|
|
this.npc = npc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterable<Vector> getPath() {
|
|
|
|
return plan == null ? null : plan.getPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Location getTargetAsLocation() {
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
2020-05-06 12:08:12 +02:00
|
|
|
public void initialisePathfinder() {
|
|
|
|
params.examiner(new BlockExaminer() {
|
|
|
|
@Override
|
|
|
|
public float getCost(BlockSource source, PathPoint point) {
|
|
|
|
Vector pos = point.getVector();
|
|
|
|
Material above = source.getMaterialAt(pos.setY(pos.getY() + 1));
|
2021-06-25 11:01:18 +02:00
|
|
|
return params.avoidWater() && (MinecraftBlockExaminer.isLiquid(above)
|
|
|
|
|| MinecraftBlockExaminer.isLiquidOrInLiquid(pos.toLocation(source.getWorld()).getBlock())) ? 1F
|
|
|
|
: 0F;
|
2020-05-06 12:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public PassableState isPassable(BlockSource source, PathPoint point) {
|
|
|
|
return PassableState.IGNORE;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Location location = npc.getEntity().getLocation();
|
|
|
|
VectorGoal goal = new VectorGoal(destination, (float) params.pathDistanceMargin());
|
|
|
|
state = ASTAR.getStateFor(goal,
|
2020-06-30 14:39:40 +02:00
|
|
|
new VectorNode(goal, location, new NMSChunkBlockSource(location, params.range()), params.examiners()));
|
2020-05-06 12:08:12 +02:00
|
|
|
}
|
|
|
|
|
2017-12-30 08:36:36 +01:00
|
|
|
public void setPlan(Path path) {
|
|
|
|
this.plan = path;
|
|
|
|
this.planned = true;
|
|
|
|
if (plan == null || plan.isComplete()) {
|
|
|
|
setCancelReason(CancelReason.STUCK);
|
|
|
|
} else {
|
|
|
|
vector = plan.getCurrentVector();
|
|
|
|
if (params.debug()) {
|
|
|
|
plan.debug();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void stop() {
|
|
|
|
if (plan != null && params.debug()) {
|
|
|
|
plan.debugEnd();
|
|
|
|
}
|
2020-05-06 12:08:12 +02:00
|
|
|
state = null;
|
2017-12-30 08:36:36 +01:00
|
|
|
plan = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean update() {
|
|
|
|
if (!planned) {
|
2020-05-06 12:08:12 +02:00
|
|
|
if (state == null) {
|
|
|
|
initialisePathfinder();
|
|
|
|
}
|
|
|
|
int maxIterations = Setting.MAXIMUM_ASTAR_ITERATIONS.asInt();
|
|
|
|
int iterationsPerTick = Setting.ASTAR_ITERATIONS_PER_TICK.asInt();
|
|
|
|
Path plan = ASTAR.run(state, iterationsPerTick);
|
|
|
|
if (plan == null) {
|
|
|
|
if (state.isEmpty()) {
|
|
|
|
setCancelReason(CancelReason.STUCK);
|
2020-04-08 17:59:49 +02:00
|
|
|
}
|
2020-05-06 12:08:12 +02:00
|
|
|
if (iterationsPerTick > 0 && maxIterations > 0) {
|
|
|
|
iterations += iterationsPerTick;
|
|
|
|
if (iterations > maxIterations) {
|
|
|
|
setCancelReason(CancelReason.STUCK);
|
|
|
|
}
|
2020-04-08 17:59:49 +02:00
|
|
|
}
|
2020-05-06 12:08:12 +02:00
|
|
|
} else {
|
|
|
|
setPlan(plan);
|
|
|
|
}
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
|
|
|
if (getCancelReason() != null || plan == null || plan.isComplete()) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-11-21 15:12:17 +01:00
|
|
|
Location loc = npc.getEntity().getLocation(NPC_LOCATION);
|
2020-05-06 12:08:12 +02:00
|
|
|
/* Proper door movement - gets stuck on corners at times
|
2021-07-19 17:02:41 +02:00
|
|
|
|
2020-05-06 12:08:12 +02:00
|
|
|
Block block = currLoc.getWorld().getBlockAt(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ());
|
2019-05-23 11:32:38 +02:00
|
|
|
if (MinecraftBlockExaminer.isDoor(block.getType())) {
|
2019-04-25 10:57:36 +02:00
|
|
|
Door door = (Door) block.getState().getData();
|
|
|
|
if (door.isOpen()) {
|
|
|
|
BlockFace targetFace = door.getFacing().getOppositeFace();
|
|
|
|
destVector.setX(vector.getX() + targetFace.getModX());
|
|
|
|
destVector.setZ(vector.getZ() + targetFace.getModZ());
|
|
|
|
}
|
2019-05-23 11:32:38 +02:00
|
|
|
}*/
|
2021-11-21 15:12:17 +01:00
|
|
|
Location dest = Util.getCenterLocation(
|
|
|
|
new Location(loc.getWorld(), vector.getX(), vector.getY(), vector.getZ()).getBlock());
|
|
|
|
double dX = dest.getX() - loc.getX();
|
|
|
|
double dZ = dest.getZ() - loc.getZ();
|
|
|
|
double dY = dest.getY() - loc.getY();
|
2019-10-16 12:04:55 +02:00
|
|
|
double xzDistance = dX * dX + dZ * dZ;
|
|
|
|
if ((dY * dY) < 1 && xzDistance <= params.distanceMargin()) {
|
2017-12-30 08:36:36 +01:00
|
|
|
plan.update(npc);
|
|
|
|
if (plan.isComplete()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
vector = plan.getCurrentVector();
|
2019-10-17 11:38:57 +02:00
|
|
|
return false;
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
|
|
|
if (params.debug()) {
|
2021-11-21 15:12:17 +01:00
|
|
|
npc.getEntity().getWorld().playEffect(dest, Effect.ENDER_SIGNAL, 0);
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
2019-10-16 12:04:55 +02:00
|
|
|
double distance = xzDistance + dY * dY;
|
2020-05-12 10:48:21 +02:00
|
|
|
|
2021-07-19 17:02:41 +02:00
|
|
|
if (npc.getEntity() instanceof LivingEntity && !npc.getEntity().getType().name().contains("ARMOR_STAND")) {
|
2021-11-21 15:12:17 +01:00
|
|
|
NMS.setDestination(npc.getEntity(), dest.getX(), dest.getY(), dest.getZ(), params.speed());
|
2020-07-27 09:56:42 +02:00
|
|
|
} else {
|
2021-11-21 15:12:17 +01:00
|
|
|
Vector dir = dest.toVector().subtract(npc.getEntity().getLocation().toVector()).normalize().multiply(0.2);
|
2021-06-25 11:01:18 +02:00
|
|
|
Block in = npc.getEntity().getLocation().getBlock();
|
|
|
|
if (distance > 0 && dY >= 1 && xzDistance <= 2.75
|
|
|
|
|| (dY >= 0.2 && MinecraftBlockExaminer.isLiquidOrInLiquid(in))) {
|
2020-07-27 09:56:42 +02:00
|
|
|
dir.add(new Vector(0, 0.75, 0));
|
|
|
|
}
|
|
|
|
npc.getEntity().setVelocity(dir);
|
2021-11-21 15:12:17 +01:00
|
|
|
Util.faceLocation(npc.getEntity(), dest);
|
2020-07-27 09:56:42 +02:00
|
|
|
}
|
2017-12-30 08:36:36 +01:00
|
|
|
params.run();
|
|
|
|
plan.run(npc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final AStarMachine<VectorNode, Path> ASTAR = AStarMachine.createWithDefaultStorage();
|
|
|
|
private static final Location NPC_LOCATION = new Location(null, 0, 0, 0);
|
|
|
|
}
|