From e0d894d2964fe7c858eec6d03d505ff0c967c504 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 23 Nov 2018 12:09:35 -0800 Subject: [PATCH] computation request and response --- .../java/baritone/api/pathing/calc/IPath.java | 11 ++- src/comms/java/comms/IMessageListener.java | 10 +++ .../downward/MessageComputationRequest.java | 62 ++++++++++++++++ .../upward/MessageComputationResponse.java | 72 +++++++++++++++++++ .../baritone/behavior/ControllerBehavior.java | 40 +++++++++++ 5 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 src/comms/java/comms/downward/MessageComputationRequest.java create mode 100644 src/comms/java/comms/upward/MessageComputationResponse.java diff --git a/src/api/java/baritone/api/pathing/calc/IPath.java b/src/api/java/baritone/api/pathing/calc/IPath.java index 133de5efc..c3f7fc143 100644 --- a/src/api/java/baritone/api/pathing/calc/IPath.java +++ b/src/api/java/baritone/api/pathing/calc/IPath.java @@ -104,7 +104,7 @@ public interface IPath { * Returns the estimated number of ticks to complete the path from the given node index. * * @param pathPosition The index of the node we're calculating from - * @return The estimated number of ticks remaining frm the given position + * @return The estimated number of ticks remaining from the given position */ default double ticksRemainingFrom(int pathPosition) { double sum = 0; @@ -115,6 +115,15 @@ public interface IPath { return sum; } + /** + * Returns the estimated amount of time needed to complete this path from start to finish + * + * @return The estimated amount of time, in ticks + */ + default double totalTicks() { + return ticksRemainingFrom(0); + } + /** * Cuts off this path at the loaded chunk border, and returns the resulting path. Default * implementation just returns this path, without the intended functionality. diff --git a/src/comms/java/comms/IMessageListener.java b/src/comms/java/comms/IMessageListener.java index afed4cfee..94300f1f3 100644 --- a/src/comms/java/comms/IMessageListener.java +++ b/src/comms/java/comms/IMessageListener.java @@ -18,6 +18,8 @@ package comms; import comms.downward.MessageChat; +import comms.downward.MessageComputationRequest; +import comms.upward.MessageComputationResponse; import comms.upward.MessageStatus; public interface IMessageListener { @@ -29,6 +31,14 @@ public interface IMessageListener { unhandled(message); } + default void handle(MessageComputationRequest message) { + unhandled(message); + } + + default void handle(MessageComputationResponse message) { + unhandled(message); + } + default void unhandled(iMessage msg) { // can override this to throw UnsupportedOperationException, if you want to make sure you're handling everything // default is to silently ignore messages without handlers diff --git a/src/comms/java/comms/downward/MessageComputationRequest.java b/src/comms/java/comms/downward/MessageComputationRequest.java new file mode 100644 index 000000000..065c27a28 --- /dev/null +++ b/src/comms/java/comms/downward/MessageComputationRequest.java @@ -0,0 +1,62 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package comms.downward; + +import comms.IMessageListener; +import comms.iMessage; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class MessageComputationRequest implements iMessage { + public final long computationID; + public final int startX; + public final int startY; + public final int startZ; + public final String goal; // TODO find a better way to do this lol + + public MessageComputationRequest(DataInputStream in) throws IOException { + this.computationID = in.readLong(); + this.startX = in.readInt(); + this.startY = in.readInt(); + this.startZ = in.readInt(); + this.goal = in.readUTF(); + } + + public MessageComputationRequest(long computationID, int startX, int startY, int startZ, String goal) { + this.computationID = computationID; + this.startX = startX; + this.startY = startY; + this.startZ = startZ; + this.goal = goal; + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeLong(computationID); + out.writeInt(startX); + out.writeInt(startY); + out.writeUTF(goal); + } + + @Override + public void handle(IMessageListener listener) { + listener.handle(this); + } +} diff --git a/src/comms/java/comms/upward/MessageComputationResponse.java b/src/comms/java/comms/upward/MessageComputationResponse.java new file mode 100644 index 000000000..60118979c --- /dev/null +++ b/src/comms/java/comms/upward/MessageComputationResponse.java @@ -0,0 +1,72 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package comms.upward; + +import comms.IMessageListener; +import comms.iMessage; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class MessageComputationResponse implements iMessage { + public final long computationID; + public final int pathLength; + public final double pathCost; + public final boolean endsInGoal; + public final int endX; + public final int endY; + public final int endZ; + + public MessageComputationResponse(DataInputStream in) throws IOException { + this.computationID = in.readLong(); + this.pathLength = in.readInt(); + this.pathCost = in.readDouble(); + this.endsInGoal = in.readBoolean(); + this.endX = in.readInt(); + this.endY = in.readInt(); + this.endZ = in.readInt(); + } + + public MessageComputationResponse(long computationID, int pathLength, double pathCost, boolean endsInGoal, int endX, int endY, int endZ) { + this.computationID = computationID; + this.pathLength = pathLength; + this.pathCost = pathCost; + this.endsInGoal = endsInGoal; + this.endX = endX; + this.endY = endY; + this.endZ = endZ; + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeLong(computationID); + out.writeInt(pathLength); + out.writeDouble(pathCost); + out.writeBoolean(endsInGoal); + out.writeInt(endX); + out.writeInt(endY); + out.writeInt(endZ); + } + + @Override + public void handle(IMessageListener listener) { + listener.handle(this); + } +} + diff --git a/src/main/java/baritone/behavior/ControllerBehavior.java b/src/main/java/baritone/behavior/ControllerBehavior.java index 736d6ac63..405ccc4dd 100644 --- a/src/main/java/baritone/behavior/ControllerBehavior.java +++ b/src/main/java/baritone/behavior/ControllerBehavior.java @@ -20,20 +20,31 @@ package baritone.behavior; import baritone.Baritone; import baritone.api.event.events.ChatEvent; import baritone.api.event.events.TickEvent; +import baritone.api.pathing.calc.IPath; +import baritone.api.pathing.goals.Goal; +import baritone.api.pathing.goals.GoalYLevel; import baritone.api.process.IBaritoneProcess; +import baritone.api.utils.BetterBlockPos; +import baritone.pathing.movement.CalculationContext; import baritone.utils.Helper; +import baritone.utils.pathing.SegmentedCalculator; import comms.BufferedConnection; import comms.IConnection; import comms.IMessageListener; import comms.downward.MessageChat; +import comms.downward.MessageComputationRequest; import comms.iMessage; +import comms.upward.MessageComputationResponse; import comms.upward.MessageStatus; import net.minecraft.util.math.BlockPos; import java.io.IOException; import java.util.List; +import java.util.Objects; +import java.util.Optional; public class ControllerBehavior extends Behavior implements IMessageListener { + public ControllerBehavior(Baritone baritone) { super(baritone); } @@ -126,6 +137,35 @@ public class ControllerBehavior extends Behavior implements IMessageListener { baritone.getGameEventHandler().onSendChatMessage(event); } + @Override + public void handle(MessageComputationRequest msg) { + BetterBlockPos start = new BetterBlockPos(msg.startX, msg.startY, msg.startZ); + // TODO this may require scanning the world for blocks of a certain type, idk how to manage that + Goal goal = new GoalYLevel(Integer.parseInt(msg.goal)); // im already winston + SegmentedCalculator.calculateSegmentsThreaded(start, goal, new CalculationContext(baritone), path -> { + if (path.isPresent() && !Objects.equals(path.get().getGoal(), goal)) { + throw new IllegalStateException(); // sanity check + } + try { + conn.sendMessage(buildResponse(path, msg)); + } catch (IOException e) { + // nothing we can do about this, we just completed a computation but our tenor connection was closed in the meantime + // just discard the path we made for them =(( + e.printStackTrace(); // and complain =) + } + }); + } + + private static MessageComputationResponse buildResponse(Optional optPath, MessageComputationRequest req) { + if (optPath.isPresent()) { + IPath path = optPath.get(); + BetterBlockPos dest = path.getDest(); + return new MessageComputationResponse(req.computationID, path.length(), path.totalTicks(), path.getGoal().isInGoal(dest), dest.x, dest.y, dest.z); + } else { + return new MessageComputationResponse(req.computationID, 0, 0, false, 0, 0, 0); + } + } + @Override public void unhandled(iMessage msg) { Helper.HELPER.logDebug("Unhandled message received by ControllerBehavior " + msg);