Citizens2/main/src/main/java/net/citizensnpcs/npc/ai/AStarNavigationStrategy.java

184 lines
7.0 KiB
Java
Raw Normal View History

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;
import org.bukkit.entity.Entity;
import org.bukkit.util.Vector;
import com.google.common.collect.Lists;
import net.citizensnpcs.Settings.Setting;
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.ChunkBlockSource;
2020-04-08 17:59:49 +02:00
import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer;
import net.citizensnpcs.api.astar.pathfinder.Path;
2020-04-08 17:59:49 +02:00
import net.citizensnpcs.api.astar.pathfinder.PathPoint;
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;
import net.citizensnpcs.util.Util;
public class AStarNavigationStrategy extends AbstractPathStrategy {
private final Location destination;
private int iterations;
private final NPC npc;
private final NavigatorParameters params;
private Path plan;
private boolean planned = false;
private AStarMachine<VectorNode, Path>.AStarState state;
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;
}
public void initialisePathfinder() {
params.examiner(new BlockExaminer() {
@Override
public float getCost(BlockSource source, PathPoint point) {
Vector pos = point.getVector();
Material in = source.getMaterialAt(pos);
Material above = source.getMaterialAt(pos.setY(pos.getY() + 1));
return params.avoidWater() && MinecraftBlockExaminer.isLiquid(in, above) ? 1F : 0F;
}
@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,
new VectorNode(goal, location, new ChunkBlockSource(location, params.range()), params.examiners()));
}
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();
}
state = null;
plan = null;
}
@Override
public boolean update() {
if (!planned) {
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
}
if (iterationsPerTick > 0 && maxIterations > 0) {
iterations += iterationsPerTick;
if (iterations > maxIterations) {
setCancelReason(CancelReason.STUCK);
}
2020-04-08 17:59:49 +02:00
}
} else {
setPlan(plan);
}
}
if (getCancelReason() != null || plan == null || plan.isComplete()) {
return true;
}
Location currLoc = npc.getEntity().getLocation(NPC_LOCATION);
Vector destVector = new Vector(vector.getX() + 0.5, vector.getY(), vector.getZ() + 0.5);
/* Proper door movement - gets stuck on corners at times
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
}*/
double dX = destVector.getX() - currLoc.getX();
double dZ = destVector.getZ() - currLoc.getZ();
double dY = destVector.getY() - currLoc.getY();
double xzDistance = dX * dX + dZ * dZ;
if ((dY * dY) < 1 && xzDistance <= params.distanceMargin()) {
plan.update(npc);
if (plan.isComplete()) {
return true;
}
vector = plan.getCurrentVector();
2019-10-17 11:38:57 +02:00
return false;
}
if (params.debug()) {
npc.getEntity().getWorld().playEffect(destVector.toLocation(npc.getEntity().getWorld()),
Effect.ENDER_SIGNAL, 0);
}
double distance = xzDistance + dY * dY;
if (distance > 0 && dY > NMS.getStepHeight(npc.getEntity()) && xzDistance <= 2.75) {
NMS.setShouldJump(npc.getEntity());
}
if (Util.isHorse(npc.getEntity().getType())) {
Entity passenger = npc.getEntity().getPassenger();
NMS.setDestination(passenger, destVector.getX(), destVector.getY(), destVector.getZ(), params.speed());
} else {
NMS.setDestination(npc.getEntity(), destVector.getX(), destVector.getY(), destVector.getZ(),
params.speed());
}
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);
}