processes tied in priority

This commit is contained in:
Leijurv
2019-03-11 18:24:31 -07:00
parent d144d30890
commit 580af2ab06
12 changed files with 73 additions and 29 deletions

View File

@@ -35,6 +35,16 @@ import baritone.api.event.events.PathEvent;
*/ */
public interface IBaritoneProcess { public interface IBaritoneProcess {
/**
* Default priority. Most normal processes should have this value.
* <p>
* 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.
* <p>
* 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? * Would this process like to be in control?
* *
@@ -82,7 +92,9 @@ public interface IBaritoneProcess {
* *
* @return A double representing the priority * @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. * Returns a user-friendly name for this process. Suitable for a HUD.

View File

@@ -53,4 +53,9 @@ public class PathingCommand {
this.goal = goal; this.goal = goal;
this.commandType = commandType; this.commandType = commandType;
} }
@Override
public String toString() {
return commandType + " " + goal;
}
} }

View File

@@ -155,6 +155,10 @@ public class MovementAscend extends Movement {
return state.setStatus(MovementStatus.SUCCESS); return state.setStatus(MovementStatus.SUCCESS);
} }
if (ctx.playerFeet().y < src.y) {
return state.setStatus(MovementStatus.UNREACHABLE);
}
IBlockState jumpingOnto = BlockStateInterface.get(ctx, positionToPlace); IBlockState jumpingOnto = BlockStateInterface.get(ctx, positionToPlace);
if (!MovementHelper.canWalkOn(ctx, positionToPlace, jumpingOnto)) { if (!MovementHelper.canWalkOn(ctx, positionToPlace, jumpingOnto)) {
ticksWithoutPlacement++; ticksWithoutPlacement++;

View File

@@ -148,6 +148,10 @@ public class MovementPillar extends Movement {
return state; return state;
} }
if (ctx.playerFeet().y < src.y) {
return state.setStatus(MovementStatus.UNREACHABLE);
}
IBlockState fromDown = BlockStateInterface.get(ctx, src); IBlockState fromDown = BlockStateInterface.get(ctx, src);
if (MovementHelper.isWater(fromDown.getBlock()) && MovementHelper.isWater(ctx, dest)) { if (MovementHelper.isWater(fromDown.getBlock()) && MovementHelper.isWater(ctx, dest)) {
// stay centered while swimming up a water column // stay centered while swimming up a water column

View File

@@ -24,8 +24,6 @@ import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType; import baritone.api.process.PathingCommandType;
import baritone.utils.BaritoneProcessHelper; import baritone.utils.BaritoneProcessHelper;
import java.util.Objects;
/** /**
* As set by ExampleBaritoneControl or something idk * As set by ExampleBaritoneControl or something idk
* *
@@ -46,7 +44,7 @@ public class CustomGoalProcess extends BaritoneProcessHelper implements ICustomG
private State state; private State state;
public CustomGoalProcess(Baritone baritone) { public CustomGoalProcess(Baritone baritone) {
super(baritone, 3); super(baritone);
} }
@Override @Override
@@ -79,20 +77,20 @@ public class CustomGoalProcess extends BaritoneProcessHelper implements ICustomG
public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) { public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
switch (this.state) { switch (this.state) {
case GOAL_SET: 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); return new PathingCommand(this.goal, PathingCommandType.CANCEL_AND_SET_GOAL);
case PATH_REQUESTED: case PATH_REQUESTED:
// return FORCE_REVALIDATE_GOAL_AND_PATH just once
PathingCommand ret = new PathingCommand(this.goal, PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH); PathingCommand ret = new PathingCommand(this.goal, PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH);
this.state = State.EXECUTING; this.state = State.EXECUTING;
return ret; return ret;
case EXECUTING: case EXECUTING:
if (calcFailed) { if (calcFailed) {
onLostControl(); onLostControl();
return new PathingCommand(this.goal, PathingCommandType.CANCEL_AND_SET_GOAL);
} }
if (this.goal == null || this.goal.isInGoal(ctx.playerFeet())) { if (this.goal == null || this.goal.isInGoal(ctx.playerFeet())) {
onLostControl(); // we're there xd 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); return new PathingCommand(this.goal, PathingCommandType.SET_GOAL_AND_PATH);
default: default:

View File

@@ -31,7 +31,7 @@ public class ExploreProcess extends BaritoneProcessHelper {
private BlockPos explorationOrigin; private BlockPos explorationOrigin;
public ExploreProcess(Baritone baritone) { public ExploreProcess(Baritone baritone) {
super(baritone, 0); super(baritone);
} }
@Override @Override

View File

@@ -46,7 +46,7 @@ public final class FollowProcess extends BaritoneProcessHelper implements IFollo
private List<Entity> cache; private List<Entity> cache;
public FollowProcess(Baritone baritone) { public FollowProcess(Baritone baritone) {
super(baritone, 1); super(baritone);
} }
@Override @Override

View File

@@ -44,7 +44,7 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl
private int tickCount = 0; private int tickCount = 0;
public GetToBlockProcess(Baritone baritone) { public GetToBlockProcess(Baritone baritone) {
super(baritone, 2); super(baritone);
} }
@Override @Override
@@ -161,7 +161,10 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl
@Override @Override
public String displayName() { 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<BlockPos> known, CalculationContext context) { private synchronized void rescan(List<BlockPos> known, CalculationContext context) {

View File

@@ -60,7 +60,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
private int tickCount; private int tickCount;
public MineProcess(Baritone baritone) { public MineProcess(Baritone baritone) {
super(baritone, 0); super(baritone);
} }
@Override @Override

View File

@@ -25,12 +25,10 @@ public abstract class BaritoneProcessHelper implements IBaritoneProcess, Helper
protected final Baritone baritone; protected final Baritone baritone;
protected final IPlayerContext ctx; protected final IPlayerContext ctx;
private final double priority;
public BaritoneProcessHelper(Baritone baritone, double priority) { public BaritoneProcessHelper(Baritone baritone) {
this.baritone = baritone; this.baritone = baritone;
this.ctx = baritone.getPlayerContext(); this.ctx = baritone.getPlayerContext();
this.priority = priority;
baritone.getPathingControlManager().registerProcess(this); baritone.getPathingControlManager().registerProcess(this);
} }
@@ -38,9 +36,4 @@ public abstract class BaritoneProcessHelper implements IBaritoneProcess, Helper
public boolean isTemporary() { public boolean isTemporary() {
return false; return false;
} }
@Override
public double priority() {
return priority;
}
} }

View File

@@ -24,6 +24,7 @@ import baritone.api.cache.IWaypoint;
import baritone.api.event.events.ChatEvent; import baritone.api.event.events.ChatEvent;
import baritone.api.pathing.goals.*; import baritone.api.pathing.goals.*;
import baritone.api.pathing.movement.ActionCosts; import baritone.api.pathing.movement.ActionCosts;
import baritone.api.process.IBaritoneProcess;
import baritone.api.utils.BetterBlockPos; import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.SettingsUtil; import baritone.api.utils.SettingsUtil;
import baritone.behavior.Behavior; import baritone.behavior.Behavior;
@@ -225,6 +226,20 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
} }
return true; return true;
} }
if (msg.equals("proc")) {
Optional<IBaritoneProcess> 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")) { if (msg.equals("version")) {
String version = ExampleBaritoneControl.class.getPackage().getImplementationVersion(); String version = ExampleBaritoneControl.class.getPackage().getImplementationVersion();
if (version == null) { if (version == null) {

View File

@@ -29,11 +29,11 @@ import baritone.pathing.path.PathExecutor;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.*; import java.util.*;
import java.util.stream.Stream;
public class PathingControlManager implements IPathingControlManager { public class PathingControlManager implements IPathingControlManager {
private final Baritone baritone; private final Baritone baritone;
private final HashSet<IBaritoneProcess> processes; // unGh private final HashSet<IBaritoneProcess> processes; // unGh
private final List<IBaritoneProcess> active;
private IBaritoneProcess inControlLastTick; private IBaritoneProcess inControlLastTick;
private IBaritoneProcess inControlThisTick; private IBaritoneProcess inControlThisTick;
private PathingCommand command; private PathingCommand command;
@@ -41,13 +41,13 @@ public class PathingControlManager implements IPathingControlManager {
public PathingControlManager(Baritone baritone) { public PathingControlManager(Baritone baritone) {
this.baritone = baritone; this.baritone = baritone;
this.processes = new HashSet<>(); this.processes = new HashSet<>();
this.active = new ArrayList<>();
baritone.getGameEventHandler().registerEventListener(new AbstractGameEventListener() { // needs to be after all behavior ticks baritone.getGameEventHandler().registerEventListener(new AbstractGameEventListener() { // needs to be after all behavior ticks
@Override @Override
public void onTick(TickEvent event) { public void onTick(TickEvent event) {
if (event.getType() == TickEvent.Type.OUT) { if (event.getType() == TickEvent.Type.IN) {
return; postTick();
} }
postTick();
} }
}); });
} }
@@ -62,6 +62,7 @@ public class PathingControlManager implements IPathingControlManager {
inControlLastTick = null; inControlLastTick = null;
inControlThisTick = null; inControlThisTick = null;
command = null; command = null;
active.clear();
for (IBaritoneProcess proc : processes) { for (IBaritoneProcess proc : processes) {
proc.onLostControl(); 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 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(); command = executeProcesses();
if (command == null) { if (command == null) {
p.cancelSegmentIfSafe(); p.cancelSegmentIfSafe();
p.secretInternalSetGoal(null);
return; return;
} }
switch (command.commandType) { switch (command.commandType) {
@@ -172,12 +174,20 @@ public class PathingControlManager implements IPathingControlManager {
public PathingCommand executeProcesses() { public PathingCommand executeProcesses() {
Stream<IBaritoneProcess> inContention = processes.stream() for (IBaritoneProcess process : processes) {
.filter(IBaritoneProcess::isActive) if (process.isActive()) {
.sorted(Comparator.comparingDouble(IBaritoneProcess::priority).reversed()); 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<IBaritoneProcess> iterator = active.iterator();
Iterator<IBaritoneProcess> iterator = inContention.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
IBaritoneProcess proc = iterator.next(); IBaritoneProcess proc = iterator.next();