Add new setting and implement parameter

This commit is contained in:
fullwall 2020-11-22 21:29:55 +08:00
parent fb0591c187
commit 810441c17b
3 changed files with 56 additions and 2 deletions

View File

@ -89,6 +89,7 @@ public class Settings {
DEFAULT_RANDOM_TALKER("npc.default.random-talker", true),
DEFAULT_REALISTIC_LOOKING("npc.default.realistic-looking", false),
DEFAULT_STATIONARY_TICKS("npc.default.stationary-ticks", -1),
DEFAULT_STRAIGHT_LINE_TARGETING_DISTANCE("npc.pathfinding.straight-line-targeting-distance", 5),
DEFAULT_TALK_CLOSE("npc.default.talk-close.enabled", false),
DEFAULT_TALK_CLOSE_RANGE("npc.default.talk-close.range", 5),
DEFAULT_TEXT("npc.default.text.0", "Hi, I'm <npc>!") {

View File

@ -45,7 +45,8 @@ public class CitizensNavigator implements Navigator, Runnable {
.distanceMargin(Setting.DEFAULT_DISTANCE_MARGIN.asDouble())
.pathDistanceMargin(Setting.DEFAULT_PATH_DISTANCE_MARGIN.asDouble())
.stationaryTicks(Setting.DEFAULT_STATIONARY_TICKS.asInt()).stuckAction(TeleportStuckAction.INSTANCE)
.examiner(new MinecraftBlockExaminer()).useNewPathfinder(Setting.USE_NEW_PATHFINDER.asBoolean());
.examiner(new MinecraftBlockExaminer()).useNewPathfinder(Setting.USE_NEW_PATHFINDER.asBoolean())
.straightLineTargetingDistance(Setting.DEFAULT_STRAIGHT_LINE_TARGETING_DISTANCE.asFloat());
private PathStrategy executing;
private int lastX, lastY, lastZ;
private NavigatorParameters localParams = defaultParams;

View File

@ -26,7 +26,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
private final NPC npc;
private final NavigatorParameters parameters;
private final Entity target;
private final TargetNavigator targetNavigator;
private TargetNavigator targetNavigator;
private int updateCounter = -1;
public MCTargetStrategy(NPC npc, org.bukkit.entity.Entity target, boolean aggro, NavigatorParameters params) {
@ -115,6 +115,9 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
if (cancelReason != null) {
return true;
}
if (parameters.straightLineTargetingDistance() > 0 && !(targetNavigator instanceof StraightLineTargeter)) {
targetNavigator = new StraightLineTargeter(targetNavigator);
}
if (!aggro && distanceSquared() <= parameters.distanceMargin()) {
stop();
return false;
@ -198,6 +201,55 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
}
}
private class StraightLineTargeter implements TargetNavigator {
private PathStrategy active;
private final TargetNavigator fallback;
public StraightLineTargeter(TargetNavigator navigator) {
fallback = navigator;
}
@Override
public Iterable<Vector> getPath() {
if (active != null) {
return active.getPath();
}
return fallback.getPath();
}
@Override
public void setPath() {
Location location = parameters.entityTargetLocationMapper().apply(target);
if (location == null) {
throw new IllegalStateException("mapper should not return null");
}
if (parameters.straightLineTargetingDistance() > 0) {
double distance = npc.getStoredLocation().distance(location);
if (distance < parameters.straightLineTargetingDistance()) {
active = new StraightLineNavigationStrategy(npc, location, parameters);
return;
}
}
active = null;
}
@Override
public void stop() {
if (active != null) {
active.stop();
}
fallback.stop();
}
@Override
public void update() {
if (active != null) {
active.update();
}
fallback.update();
}
}
public static interface TargetNavigator {
Iterable<Vector> getPath();