Skip to content

Commit

Permalink
Implement attackRange() parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Dec 16, 2013
1 parent a1d8675 commit e891d17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/main/java/net/citizensnpcs/npc/ai/CitizensNavigator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -154,7 +155,7 @@ public void setTarget(Entity target, boolean aggressive) {
cancelNavigation();
return;
}
localParams = defaultParams.clone();
switchParams();
updatePathfindingRange();
PathStrategy newStrategy = new MCTargetStrategy(npc, target, aggressive, localParams);
switchStrategyTo(newStrategy);
Expand All @@ -173,7 +174,7 @@ public void setTarget(Location target) {
cancelNavigation();
return;
}
localParams = defaultParams.clone();
switchParams();
updatePathfindingRange();
PathStrategy newStrategy;
if (npc.isFlyable()) {
Expand Down Expand Up @@ -231,10 +232,15 @@ private void stopNavigating(CancelReason reason) {
}
}

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()) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/net/citizensnpcs/npc/ai/MCTargetStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand Down Expand Up @@ -48,14 +47,18 @@ public MCTargetStrategy(NPC npc, org.bukkit.entity.Entity target, boolean aggro,
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
public void clearCancelReason() {
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));
Expand Down

0 comments on commit e891d17

Please sign in to comment.