Implement attackRange() parameter

This commit is contained in:
fullwall 2013-12-16 08:31:23 +08:00
parent 64b2396e07
commit e772611ec1
2 changed files with 14 additions and 5 deletions

View File

@ -31,6 +31,7 @@ public class CitizensNavigator implements Navigator, Runnable {
private final NavigatorParameters defaultParams = new NavigatorParameters().baseSpeed(UNINITIALISED_SPEED)
.range(Setting.DEFAULT_PATHFINDING_RANGE.asFloat())
.defaultAttackStrategy(MCTargetStrategy.DEFAULT_ATTACK_STRATEGY)
.attackRange(Setting.NPC_ATTACK_DISTANCE.asDouble())
.stationaryTicks(Setting.DEFAULT_STATIONARY_TICKS.asInt()).stuckAction(TeleportStuckAction.INSTANCE)
.examiner(new MinecraftBlockExaminer()).useNewPathfinder(Setting.USE_NEW_PATHFINDER.asBoolean());
private PathStrategy executing;
@ -154,7 +155,7 @@ public class CitizensNavigator implements Navigator, Runnable {
cancelNavigation();
return;
}
localParams = defaultParams.clone();
switchParams();
updatePathfindingRange();
PathStrategy newStrategy = new MCTargetStrategy(npc, target, aggressive, localParams);
switchStrategyTo(newStrategy);
@ -173,7 +174,7 @@ public class CitizensNavigator implements Navigator, Runnable {
cancelNavigation();
return;
}
localParams = defaultParams.clone();
switchParams();
updatePathfindingRange();
PathStrategy newStrategy;
if (npc.isFlyable()) {
@ -231,10 +232,15 @@ public class CitizensNavigator implements Navigator, Runnable {
}
}
private void switchParams() {
localParams = defaultParams.clone();
}
private void switchStrategyTo(PathStrategy newStrategy) {
Messaging.debug(npc.getId(), "changing to new PathStrategy", newStrategy);
if (executing != null)
if (executing != null) {
Bukkit.getPluginManager().callEvent(new NavigationReplaceEvent(this));
}
executing = newStrategy;
stationaryTicks = 0;
if (npc.isSpawned()) {

View File

@ -2,7 +2,6 @@ package net.citizensnpcs.npc.ai;
import java.lang.reflect.Field;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.ai.AttackStrategy;
import net.citizensnpcs.api.ai.EntityTarget;
import net.citizensnpcs.api.ai.NavigatorParameters;
@ -48,7 +47,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
private boolean canAttack() {
return attackTicks == 0
&& (handle.boundingBox.e > target.boundingBox.b && handle.boundingBox.b < target.boundingBox.e)
&& distanceSquared() <= Setting.NPC_ATTACK_DISTANCE.asDouble() && hasLineOfSight();
&& closeEnough(distanceSquared()) && hasLineOfSight();
}
@Override
@ -56,6 +55,10 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
cancelReason = null;
}
private boolean closeEnough(double distance) {
return distance <= parameters.attackRange();
}
private double distanceSquared() {
return handle.getBukkitEntity().getLocation(HANDLE_LOCATION)
.distanceSquared(target.getBukkitEntity().getLocation(TARGET_LOCATION));