From 580af2ab065605cfc42e49bd7666050980215c74 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Mon, 11 Mar 2019 18:24:31 -0700 Subject: [PATCH] processes tied in priority --- .../api/process/IBaritoneProcess.java | 14 +++++++++- .../baritone/api/process/PathingCommand.java | 5 ++++ .../movement/movements/MovementAscend.java | 4 +++ .../movement/movements/MovementPillar.java | 4 +++ .../baritone/process/CustomGoalProcess.java | 10 +++---- .../java/baritone/process/ExploreProcess.java | 2 +- .../java/baritone/process/FollowProcess.java | 2 +- .../baritone/process/GetToBlockProcess.java | 7 +++-- .../java/baritone/process/MineProcess.java | 2 +- .../baritone/utils/BaritoneProcessHelper.java | 9 +----- .../utils/ExampleBaritoneControl.java | 15 ++++++++++ .../baritone/utils/PathingControlManager.java | 28 +++++++++++++------ 12 files changed, 73 insertions(+), 29 deletions(-) diff --git a/src/api/java/baritone/api/process/IBaritoneProcess.java b/src/api/java/baritone/api/process/IBaritoneProcess.java index 01e833c0..0339179c 100644 --- a/src/api/java/baritone/api/process/IBaritoneProcess.java +++ b/src/api/java/baritone/api/process/IBaritoneProcess.java @@ -35,6 +35,16 @@ import baritone.api.event.events.PathEvent; */ public interface IBaritoneProcess { + /** + * Default priority. Most normal processes should have this value. + *

+ * Some examples of processes that should have different values might include some kind of automated mob avoidance + * that would be temporary and would forcefully take control. Same for something that pauses pathing for auto eat, etc. + *

+ * The value is -1 beacuse that's what Impact 4.5's beta auto walk returns and I want to tie with it. + */ + double DEFAULT_PRIORITY = -1; + /** * Would this process like to be in control? * @@ -82,7 +92,9 @@ public interface IBaritoneProcess { * * @return A double representing the priority */ - double priority(); + default double priority() { + return DEFAULT_PRIORITY; + } /** * Returns a user-friendly name for this process. Suitable for a HUD. diff --git a/src/api/java/baritone/api/process/PathingCommand.java b/src/api/java/baritone/api/process/PathingCommand.java index 2caef158..61445f8a 100644 --- a/src/api/java/baritone/api/process/PathingCommand.java +++ b/src/api/java/baritone/api/process/PathingCommand.java @@ -53,4 +53,9 @@ public class PathingCommand { this.goal = goal; this.commandType = commandType; } + + @Override + public String toString() { + return commandType + " " + goal; + } } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java index 3dc65990..d3ef9def 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java @@ -155,6 +155,10 @@ public class MovementAscend extends Movement { return state.setStatus(MovementStatus.SUCCESS); } + if (ctx.playerFeet().y < src.y) { + return state.setStatus(MovementStatus.UNREACHABLE); + } + IBlockState jumpingOnto = BlockStateInterface.get(ctx, positionToPlace); if (!MovementHelper.canWalkOn(ctx, positionToPlace, jumpingOnto)) { ticksWithoutPlacement++; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java index 1bbfa6f9..e899ce63 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java @@ -148,6 +148,10 @@ public class MovementPillar extends Movement { return state; } + if (ctx.playerFeet().y < src.y) { + return state.setStatus(MovementStatus.UNREACHABLE); + } + IBlockState fromDown = BlockStateInterface.get(ctx, src); if (MovementHelper.isWater(fromDown.getBlock()) && MovementHelper.isWater(ctx, dest)) { // stay centered while swimming up a water column diff --git a/src/main/java/baritone/process/CustomGoalProcess.java b/src/main/java/baritone/process/CustomGoalProcess.java index a6234227..2c8a3c33 100644 --- a/src/main/java/baritone/process/CustomGoalProcess.java +++ b/src/main/java/baritone/process/CustomGoalProcess.java @@ -24,8 +24,6 @@ import baritone.api.process.PathingCommand; import baritone.api.process.PathingCommandType; import baritone.utils.BaritoneProcessHelper; -import java.util.Objects; - /** * As set by ExampleBaritoneControl or something idk * @@ -46,7 +44,7 @@ public class CustomGoalProcess extends BaritoneProcessHelper implements ICustomG private State state; public CustomGoalProcess(Baritone baritone) { - super(baritone, 3); + super(baritone); } @Override @@ -79,20 +77,20 @@ public class CustomGoalProcess extends BaritoneProcessHelper implements ICustomG public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) { switch (this.state) { case GOAL_SET: - if (!baritone.getPathingBehavior().isPathing() && Objects.equals(baritone.getPathingBehavior().getGoal() + "", this.goal + "")) { - this.state = State.NONE; - } return new PathingCommand(this.goal, PathingCommandType.CANCEL_AND_SET_GOAL); case PATH_REQUESTED: + // return FORCE_REVALIDATE_GOAL_AND_PATH just once PathingCommand ret = new PathingCommand(this.goal, PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH); this.state = State.EXECUTING; return ret; case EXECUTING: if (calcFailed) { onLostControl(); + return new PathingCommand(this.goal, PathingCommandType.CANCEL_AND_SET_GOAL); } if (this.goal == null || this.goal.isInGoal(ctx.playerFeet())) { onLostControl(); // we're there xd + return new PathingCommand(this.goal, PathingCommandType.CANCEL_AND_SET_GOAL); } return new PathingCommand(this.goal, PathingCommandType.SET_GOAL_AND_PATH); default: diff --git a/src/main/java/baritone/process/ExploreProcess.java b/src/main/java/baritone/process/ExploreProcess.java index 3f8d0f82..3343d882 100644 --- a/src/main/java/baritone/process/ExploreProcess.java +++ b/src/main/java/baritone/process/ExploreProcess.java @@ -31,7 +31,7 @@ public class ExploreProcess extends BaritoneProcessHelper { private BlockPos explorationOrigin; public ExploreProcess(Baritone baritone) { - super(baritone, 0); + super(baritone); } @Override diff --git a/src/main/java/baritone/process/FollowProcess.java b/src/main/java/baritone/process/FollowProcess.java index 6e377dec..04e80e03 100644 --- a/src/main/java/baritone/process/FollowProcess.java +++ b/src/main/java/baritone/process/FollowProcess.java @@ -46,7 +46,7 @@ public final class FollowProcess extends BaritoneProcessHelper implements IFollo private List cache; public FollowProcess(Baritone baritone) { - super(baritone, 1); + super(baritone); } @Override diff --git a/src/main/java/baritone/process/GetToBlockProcess.java b/src/main/java/baritone/process/GetToBlockProcess.java index 7f63c6e2..4a2aa3e2 100644 --- a/src/main/java/baritone/process/GetToBlockProcess.java +++ b/src/main/java/baritone/process/GetToBlockProcess.java @@ -44,7 +44,7 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl private int tickCount = 0; public GetToBlockProcess(Baritone baritone) { - super(baritone, 2); + super(baritone); } @Override @@ -161,7 +161,10 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl @Override public String displayName() { - return "Get To Block " + gettingTo; + if (knownLocations.isEmpty()) { + return "Exploring randomly to find " + gettingTo + ", no known locations"; + } + return "Get To Block " + gettingTo + ", " + knownLocations.size() + " known locations"; } private synchronized void rescan(List known, CalculationContext context) { diff --git a/src/main/java/baritone/process/MineProcess.java b/src/main/java/baritone/process/MineProcess.java index 121ddb8f..495fb0ee 100644 --- a/src/main/java/baritone/process/MineProcess.java +++ b/src/main/java/baritone/process/MineProcess.java @@ -60,7 +60,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro private int tickCount; public MineProcess(Baritone baritone) { - super(baritone, 0); + super(baritone); } @Override diff --git a/src/main/java/baritone/utils/BaritoneProcessHelper.java b/src/main/java/baritone/utils/BaritoneProcessHelper.java index 1a66eaa5..61c2fd44 100644 --- a/src/main/java/baritone/utils/BaritoneProcessHelper.java +++ b/src/main/java/baritone/utils/BaritoneProcessHelper.java @@ -25,12 +25,10 @@ public abstract class BaritoneProcessHelper implements IBaritoneProcess, Helper protected final Baritone baritone; protected final IPlayerContext ctx; - private final double priority; - public BaritoneProcessHelper(Baritone baritone, double priority) { + public BaritoneProcessHelper(Baritone baritone) { this.baritone = baritone; this.ctx = baritone.getPlayerContext(); - this.priority = priority; baritone.getPathingControlManager().registerProcess(this); } @@ -38,9 +36,4 @@ public abstract class BaritoneProcessHelper implements IBaritoneProcess, Helper public boolean isTemporary() { return false; } - - @Override - public double priority() { - return priority; - } } diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 89aa0fb5..ca88a828 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -24,6 +24,7 @@ import baritone.api.cache.IWaypoint; import baritone.api.event.events.ChatEvent; import baritone.api.pathing.goals.*; import baritone.api.pathing.movement.ActionCosts; +import baritone.api.process.IBaritoneProcess; import baritone.api.utils.BetterBlockPos; import baritone.api.utils.SettingsUtil; import baritone.behavior.Behavior; @@ -225,6 +226,20 @@ public class ExampleBaritoneControl extends Behavior implements Helper { } return true; } + if (msg.equals("proc")) { + Optional proc = baritone.getPathingControlManager().mostRecentInControl(); + if (!proc.isPresent()) { + logDirect("No process is in control"); + return true; + } + IBaritoneProcess p = proc.get(); + logDirect("Class: " + p.getClass()); + logDirect("Priority: " + p.priority()); + logDirect("Temporary: " + p.isTemporary()); + logDirect("Display name: " + p.displayName()); + logDirect("Command: " + baritone.getPathingControlManager().mostRecentCommand()); + return true; + } if (msg.equals("version")) { String version = ExampleBaritoneControl.class.getPackage().getImplementationVersion(); if (version == null) { diff --git a/src/main/java/baritone/utils/PathingControlManager.java b/src/main/java/baritone/utils/PathingControlManager.java index 8d1616c4..a01a1036 100644 --- a/src/main/java/baritone/utils/PathingControlManager.java +++ b/src/main/java/baritone/utils/PathingControlManager.java @@ -29,11 +29,11 @@ import baritone.pathing.path.PathExecutor; import net.minecraft.util.math.BlockPos; import java.util.*; -import java.util.stream.Stream; public class PathingControlManager implements IPathingControlManager { private final Baritone baritone; private final HashSet processes; // unGh + private final List active; private IBaritoneProcess inControlLastTick; private IBaritoneProcess inControlThisTick; private PathingCommand command; @@ -41,13 +41,13 @@ public class PathingControlManager implements IPathingControlManager { public PathingControlManager(Baritone baritone) { this.baritone = baritone; this.processes = new HashSet<>(); + this.active = new ArrayList<>(); baritone.getGameEventHandler().registerEventListener(new AbstractGameEventListener() { // needs to be after all behavior ticks @Override public void onTick(TickEvent event) { - if (event.getType() == TickEvent.Type.OUT) { - return; + if (event.getType() == TickEvent.Type.IN) { + postTick(); } - postTick(); } }); } @@ -62,6 +62,7 @@ public class PathingControlManager implements IPathingControlManager { inControlLastTick = null; inControlThisTick = null; command = null; + active.clear(); for (IBaritoneProcess proc : processes) { proc.onLostControl(); if (proc.isActive() && !proc.isTemporary()) { // it's okay only for a temporary thing (like combat pause) to maintain control even if you say to cancel @@ -87,6 +88,7 @@ public class PathingControlManager implements IPathingControlManager { command = executeProcesses(); if (command == null) { p.cancelSegmentIfSafe(); + p.secretInternalSetGoal(null); return; } switch (command.commandType) { @@ -172,12 +174,20 @@ public class PathingControlManager implements IPathingControlManager { public PathingCommand executeProcesses() { - Stream inContention = processes.stream() - .filter(IBaritoneProcess::isActive) - .sorted(Comparator.comparingDouble(IBaritoneProcess::priority).reversed()); + for (IBaritoneProcess process : processes) { + if (process.isActive()) { + if (!active.contains(process)) { + // put a newly active process at the very front of the queue + active.add(0, process); + } + } else { + active.remove(process); + } + } + // ties are broken by which was added to the beginning of the list first + active.sort(Comparator.comparingDouble(IBaritoneProcess::priority).reversed()); - - Iterator iterator = inContention.iterator(); + Iterator iterator = active.iterator(); while (iterator.hasNext()) { IBaritoneProcess proc = iterator.next();