Add FlyingAStarNavigationStrategy

This commit is contained in:
fullwall 2013-11-04 21:33:56 +08:00
parent 7235b6afee
commit 4bbac37918
3 changed files with 99 additions and 7 deletions

View File

@ -0,0 +1,87 @@
package net.citizensnpcs.npc.ai;
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;
import net.citizensnpcs.api.astar.pathfinder.ChunkBlockSource;
import net.citizensnpcs.api.astar.pathfinder.Path;
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.minecraft.server.v1_6_R3.MathHelper;
import org.bukkit.Location;
import org.bukkit.util.Vector;
public class FlyingAStarNavigationStrategy extends AbstractPathStrategy {
private final NPC npc;
private final NavigatorParameters parameters;
private Path plan;
private final Location target;
private Vector vector;
public FlyingAStarNavigationStrategy(final NPC npc, Location dest, NavigatorParameters params) {
super(TargetType.LOCATION);
this.target = dest;
this.parameters = params;
this.npc = npc;
Location location = npc.getBukkitEntity().getEyeLocation();
plan = ASTAR.runFully(new VectorGoal(dest, (float) params.distanceMargin()), new VectorNode(location,
new ChunkBlockSource(location, params.range()), params.examiners()), 50000);
if (plan == null || plan.isComplete()) {
setCancelReason(CancelReason.STUCK);
} else {
vector = plan.getCurrentVector();
}
}
@Override
public Location getTargetAsLocation() {
return target;
}
@Override
public void stop() {
plan = null;
}
@Override
public boolean update() {
if (getCancelReason() != null || plan == null || plan.isComplete()) {
return true;
}
Location current = npc.getBukkitEntity().getLocation(NPC_LOCATION);
if (current.toVector().distanceSquared(vector) <= parameters.distanceMargin()) {
plan.update(npc);
if (plan.isComplete()) {
return true;
}
vector = plan.getCurrentVector();
}
double d0 = vector.getX() + 0.5D - current.getX();
double d1 = vector.getY() + 0.1D - current.getY();
double d2 = vector.getZ() + 0.5D - current.getZ();
Vector velocity = npc.getBukkitEntity().getVelocity();
double motX = velocity.getX(), motY = velocity.getY(), motZ = velocity.getZ();
motX += (Math.signum(d0) * 0.5D - motX) * 0.1;
motY += (Math.signum(d1) * 0.7D - motY) * 0.1;
motZ += (Math.signum(d2) * 0.5D - motZ) * 0.1;
float targetYaw = (float) (Math.atan2(motZ, motX) * 180.0D / Math.PI) - 90.0F;
float normalisedTargetYaw = MathHelper.g(targetYaw - current.getYaw());
velocity.setX(motX).setY(motY).setZ(motZ);
npc.getBukkitEntity().setVelocity(velocity);
NMS.setVerticalMovement(npc.getBukkitEntity(), 0.5);
NMS.setHeadYaw(NMS.getHandle(npc.getBukkitEntity()), current.getYaw() + normalisedTargetYaw);
return false;
}
private static final AStarMachine<VectorNode, Path> ASTAR = AStarMachine.createWithDefaultStorage();
private static final Location NPC_LOCATION = new Location(null, 0, 0, 0);
}

View File

@ -49,13 +49,6 @@ public class PigController extends MobEntityController {
super.a(entitylightning);
}
@Override
public void bi() {
super.bi();
if (npc != null)
npc.update();
}
@Override
public boolean bH() {
if (npc == null)
@ -69,6 +62,13 @@ public class PigController extends MobEntityController {
return false; // shouldLeash
}
@Override
public void bi() {
super.bi();
if (npc != null)
npc.update();
}
@Override
public void collide(net.minecraft.server.v1_6_R3.Entity entity) {
// this method is called by both the entities involved - cancelling

View File

@ -324,6 +324,11 @@ public class NMS {
entity.Y = height;
}
public static void setVerticalMovement(LivingEntity bukkitEntity, double d) {
EntityLiving handle = NMS.getHandle(bukkitEntity);
handle.bf = (float) d;
}
public static boolean shouldJump(net.minecraft.server.v1_6_R3.Entity entity) {
if (JUMP_FIELD == null || !(entity instanceof EntityLiving))
return false;