computation request and response

This commit is contained in:
Leijurv
2018-11-23 12:09:35 -08:00
parent 27c818f873
commit e0d894d296
5 changed files with 194 additions and 1 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@@ -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<IPath> 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);