Compare commits
40 Commits
bot-system
...
tenor
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
060741bfdc | ||
|
|
2a21a1ca18 | ||
|
|
8ece179912 | ||
|
|
c79f40a5f1 | ||
|
|
3b7e1adf24 | ||
|
|
01bec5d8f9 | ||
|
|
82c64d4d06 | ||
|
|
c73751ef98 | ||
|
|
81c95a2d43 | ||
|
|
07970f803f | ||
|
|
5ae4f23886 | ||
|
|
7f1fb2a8fe | ||
|
|
c57f65f832 | ||
|
|
85a6ec022e | ||
|
|
806c2a4af1 | ||
|
|
c5ecb9bb9b | ||
|
|
e0d894d296 | ||
|
|
27c818f873 | ||
|
|
3a2620192b | ||
|
|
87a44e4093 | ||
|
|
d082d25253 | ||
|
|
1cf6768e27 | ||
|
|
890dbec852 | ||
|
|
24bb0c541c | ||
|
|
e7a09c34ea | ||
|
|
9642950b54 | ||
|
|
f76736a378 | ||
|
|
91c6baead1 | ||
|
|
e81b01a8f3 | ||
|
|
f2a45b9eeb | ||
|
|
5e2ccdac08 | ||
|
|
473f872d2f | ||
|
|
3d03f15749 | ||
|
|
15e91c7c7c | ||
|
|
5089c62ada | ||
|
|
7c69a188f6 | ||
|
|
ef6b36b2cc | ||
|
|
f2f806669c | ||
|
|
c887b27df9 | ||
|
|
e1bb8fd570 |
@@ -58,6 +58,12 @@ sourceSets {
|
||||
launch {
|
||||
compileClasspath += main.compileClasspath + main.runtimeClasspath + main.output
|
||||
}
|
||||
tenor {
|
||||
compileClasspath += comms.compileClasspath + comms.output
|
||||
}
|
||||
main {
|
||||
compileClasspath += tenor.compileClasspath + tenor.output
|
||||
}
|
||||
}
|
||||
|
||||
minecraft {
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
package cabaletta.comms;
|
||||
|
||||
import cabaletta.comms.downward.MessageChat;
|
||||
import cabaletta.comms.downward.MessageComputationRequest;
|
||||
import cabaletta.comms.upward.MessageComputationResponse;
|
||||
import cabaletta.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
|
||||
|
||||
@@ -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 cabaletta.comms.downward;
|
||||
|
||||
import cabaletta.comms.IMessageListener;
|
||||
import cabaletta.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);
|
||||
}
|
||||
}
|
||||
@@ -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 cabaletta.comms.upward;
|
||||
|
||||
import cabaletta.comms.IMessageListener;
|
||||
import cabaletta.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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,19 +20,28 @@ package baritone.behavior;
|
||||
import baritone.Baritone;
|
||||
import baritone.api.event.events.ChatEvent;
|
||||
import baritone.api.event.events.TickEvent;
|
||||
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 cabaletta.comms.BufferedConnection;
|
||||
import cabaletta.comms.IConnection;
|
||||
import cabaletta.comms.IMessageListener;
|
||||
import cabaletta.comms.downward.MessageChat;
|
||||
import cabaletta.comms.downward.MessageComputationRequest;
|
||||
import cabaletta.comms.iMessage;
|
||||
import cabaletta.comms.upward.MessageComputationResponse;
|
||||
import cabaletta.comms.upward.MessageStatus;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ControllerBehavior extends Behavior implements IMessageListener {
|
||||
|
||||
public ControllerBehavior(Baritone baritone) {
|
||||
super(baritone);
|
||||
}
|
||||
@@ -120,6 +129,33 @@ 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 (!Objects.equals(path.getGoal(), goal)) {
|
||||
throw new IllegalStateException(); // sanity check
|
||||
}
|
||||
try {
|
||||
BetterBlockPos dest = path.getDest();
|
||||
conn.sendMessage(new MessageComputationResponse(msg.computationID, path.length(), path.totalTicks(), path.getGoal().isInGoal(dest), dest.x, dest.y, dest.z));
|
||||
} 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 =)
|
||||
}
|
||||
}, () -> {
|
||||
try {
|
||||
conn.sendMessage(new MessageComputationResponse(msg.computationID, 0, 0, false, 0, 0, 0));
|
||||
} catch (IOException e) {
|
||||
// same deal
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unhandled(iMessage msg) {
|
||||
Helper.HELPER.logDebug("Unhandled message received by ControllerBehavior " + msg);
|
||||
|
||||
113
src/tenor/java/tenor/Bot.java
Normal file
113
src/tenor/java/tenor/Bot.java
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
import cabaletta.comms.BufferedConnection;
|
||||
import cabaletta.comms.IConnection;
|
||||
import cabaletta.comms.IMessageListener;
|
||||
import cabaletta.comms.iMessage;
|
||||
import cabaletta.comms.upward.MessageComputationResponse;
|
||||
import cabaletta.comms.upward.MessageStatus;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Bot implements IMessageListener {
|
||||
public final BotTaskRegistry taskRegistry = new BotTaskRegistry(this);
|
||||
|
||||
private final BufferedConnection connectionToBot;
|
||||
private volatile MessageStatus mostRecentUpdate;
|
||||
|
||||
public Bot(IConnection conn) {
|
||||
this.connectionToBot = BufferedConnection.makeBuffered(conn);
|
||||
// TODO event loop calling tick?
|
||||
}
|
||||
|
||||
public int getCurrentQuantityInInventory(String item) {
|
||||
// TODO get this information from the most recent update
|
||||
throw new UnsupportedOperationException("oppa");
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
// TODO i have no idea what tenor's threading model will be
|
||||
// probably single threaded idk
|
||||
ComputationRequestManager.INSTANCE.dispatchAll();
|
||||
receiveMessages();
|
||||
TaskLeaf<?> curr = decideWhatToDoNow();
|
||||
iMessage command = doTheTask(curr);
|
||||
try {
|
||||
connectionToBot.sendMessage(command);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public void receiveMessages() {
|
||||
try {
|
||||
connectionToBot.handleAllPendingMessages(this);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
// TODO i have no idea how to handle this
|
||||
// this destroys the task tree
|
||||
// wew lad?
|
||||
connectionToBot.close();
|
||||
}
|
||||
|
||||
public TaskLeaf<?> decideWhatToDoNow() {
|
||||
// TODO idk lol
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public iMessage doTheTask(TaskLeaf<?> task) {
|
||||
// TODO idk lol
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public void send(iMessage msg) {
|
||||
try {
|
||||
connectionToBot.sendMessage(msg);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public MessageStatus getMostRecentUpdate() {
|
||||
return mostRecentUpdate;
|
||||
}
|
||||
|
||||
public String getHostIdentifier() {
|
||||
// return ((SocketConnection) ((BufferedConnection) connectionToBot).getUnderlying()).getSocket().getHostname()
|
||||
return "localhost";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(MessageStatus msg) {
|
||||
mostRecentUpdate = msg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(MessageComputationResponse msg) {
|
||||
ComputationRequestManager.INSTANCE.onRecv(this, msg);
|
||||
}
|
||||
}
|
||||
115
src/tenor/java/tenor/BotTaskRegistry.java
Normal file
115
src/tenor/java/tenor/BotTaskRegistry.java
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class BotTaskRegistry {
|
||||
Bot target;
|
||||
|
||||
Map<Class<? extends ITask>, ITask<?>> singletonRegistry; // GetToCraftingTableTask
|
||||
|
||||
Map<Class<? extends ITask>, Map<String, ITask<?>>> itemBased; // MineTask, AquireItemTask
|
||||
|
||||
List<ITask<?>> allOthers; // AquireCraftingItems
|
||||
|
||||
List<ITask<?>> totalList; // everything from all lists
|
||||
|
||||
public BotTaskRegistry(Bot parent) {
|
||||
this.target = parent;
|
||||
this.singletonRegistry = new HashMap<>();
|
||||
this.itemBased = new HashMap<>();
|
||||
this.allOthers = new ArrayList<>();
|
||||
this.totalList = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void register(ITask<?> task) {
|
||||
if (task.bot() != target) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
if (totalList.contains(task)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
totalList.add(task);
|
||||
allOthers.add(task);
|
||||
}
|
||||
|
||||
public void registerSingleton(ITask<?> task) {
|
||||
if (!totalList.contains(task)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
if (!allOthers.contains(task)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
if (singletonRegistry.containsKey(task.getClass())) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
allOthers.remove(task);
|
||||
singletonRegistry.put(task.getClass(), task);
|
||||
}
|
||||
|
||||
public <T extends ITask<?>> T getSingleton(Class<T> klass) {
|
||||
if (singletonRegistry.containsKey(klass)) {
|
||||
return (T) singletonRegistry.get(klass);
|
||||
}
|
||||
try {
|
||||
T result = klass.getConstructor(Bot.class).newInstance(target);
|
||||
if (!singletonRegistry.containsKey(klass)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
return result;
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerItemBased(ITask<?> task, String key) {
|
||||
if (!totalList.contains(task)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
if (!allOthers.contains(task)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
Map<String, ITask<?>> specificMap = itemBased.computeIfAbsent(task.getClass(), x -> new HashMap<>());
|
||||
if (specificMap.containsKey(key)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
allOthers.remove(task);
|
||||
specificMap.put(key, task);
|
||||
}
|
||||
|
||||
public <T extends ITask<?>> T getItemBased(Class<T> klass, String key) {
|
||||
Map<String, ITask<?>> specificMap = itemBased.computeIfAbsent(klass, x -> new HashMap<>());
|
||||
if (specificMap.containsKey(key)) {
|
||||
return (T) specificMap.get(key);
|
||||
}
|
||||
try {
|
||||
T result = klass.getConstructor(Bot.class, String.class).newInstance(target, key);
|
||||
if (!specificMap.containsKey(key)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
return result;
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
94
src/tenor/java/tenor/ComputationRequest.java
Normal file
94
src/tenor/java/tenor/ComputationRequest.java
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
import cabaletta.comms.upward.MessageComputationResponse;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class ComputationRequest {
|
||||
public final Bot bot;
|
||||
public final String goal;
|
||||
private final Set<SingularTaskLeaf> parents; // TODO quantized UGH
|
||||
private MessageComputationResponse resp;
|
||||
|
||||
|
||||
ComputationRequest(Bot bot, String goal) {
|
||||
this.bot = bot;
|
||||
this.goal = goal;
|
||||
this.parents = new HashSet<>();
|
||||
this.resp = null;
|
||||
}
|
||||
|
||||
public MessageComputationResponse getResp() {
|
||||
return resp;
|
||||
}
|
||||
|
||||
public void receivedResponse(MessageComputationResponse mcrn) {
|
||||
resp = mcrn;
|
||||
// TODO notify parent tasks?
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
// :woke
|
||||
// make this a huge nested ternary when
|
||||
if (resp != null) {
|
||||
if (resp.pathLength == 0) {
|
||||
return Status.RECV_FAILURE;
|
||||
} else {
|
||||
return Status.RECV_SUCCESS;
|
||||
}
|
||||
} else {
|
||||
if (ComputationRequestManager.INSTANCE.inProgress(this)) {
|
||||
return Status.IN_PROGRESS;
|
||||
} else {
|
||||
return Status.QUEUED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addParentTask(SingularTaskLeaf parent) {
|
||||
parents.add(parent);
|
||||
}
|
||||
|
||||
// hmmmmm this has parent tasks and has a cost and a priority.... god forbid should i make this a SingularTaskLeaf itself?!?!??!?
|
||||
public Double cost() {
|
||||
switch (getStatus()) {
|
||||
case RECV_FAILURE:
|
||||
return 1000000000D;
|
||||
case RECV_SUCCESS:
|
||||
return resp.pathCost;
|
||||
case IN_PROGRESS:
|
||||
case QUEUED:
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public double priority() {
|
||||
return parents.stream().mapToDouble(SingularTaskLeaf::priority).sum();
|
||||
}
|
||||
|
||||
public enum Status {
|
||||
QUEUED,
|
||||
IN_PROGRESS,
|
||||
RECV_SUCCESS,
|
||||
RECV_FAILURE,
|
||||
}
|
||||
}
|
||||
74
src/tenor/java/tenor/ComputationRequestManager.java
Normal file
74
src/tenor/java/tenor/ComputationRequestManager.java
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
import cabaletta.comms.downward.MessageComputationRequest;
|
||||
import cabaletta.comms.upward.MessageComputationResponse;
|
||||
import cabaletta.comms.upward.MessageStatus;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public enum ComputationRequestManager {
|
||||
INSTANCE;
|
||||
public static final int MAX_SIMUL_COMPUTATIONS_PER_HOST = 2;
|
||||
|
||||
private Map<Long, ComputationRequest> inProgress = new HashMap<>();
|
||||
private Map<Bot, Map<String, ComputationRequest>> byGoal = new HashMap<>();
|
||||
public Function<Bot, String> botToHostMapping = Bot::toString; // TODO map bots to hosts by connection maybe?
|
||||
|
||||
|
||||
public ComputationRequest getByGoal(SingularTaskLeaf task, String goal) {
|
||||
Map<String, ComputationRequest> thisBot = byGoal.computeIfAbsent(task.bot, x -> new HashMap<>());
|
||||
ComputationRequest req = thisBot.computeIfAbsent(goal, g -> new ComputationRequest(task.bot, goal));
|
||||
req.addParentTask(task);
|
||||
return req;
|
||||
}
|
||||
|
||||
public void dispatchAll() {
|
||||
Map<String, List<ComputationRequest>> highestPriorityByHost = byGoal.values().stream().map(Map::values).flatMap(Collection::stream).filter(c -> c.getStatus() == ComputationRequest.Status.QUEUED).collect(Collectors.groupingBy(req -> botToHostMapping.apply(req.bot), Collectors.collectingAndThen(Collectors.toList(), l -> l.stream().sorted(Comparator.comparingDouble(ComputationRequest::priority).reversed()).collect(Collectors.toList()))));
|
||||
inProgress.values().stream().collect(Collectors.groupingBy(req -> botToHostMapping.apply(req.bot), Collectors.collectingAndThen(Collectors.counting(), count -> MAX_SIMUL_COMPUTATIONS_PER_HOST - count))).entrySet().stream().flatMap(entry -> highestPriorityByHost.get(entry.getKey()).subList(0, entry.getValue().intValue()).stream()).forEach(this::dispatch);
|
||||
}
|
||||
|
||||
private void dispatch(ComputationRequest req) {
|
||||
if (inProgress(req)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
long key = new SecureRandom().nextLong();
|
||||
inProgress.put(key, req);
|
||||
MessageStatus status = req.bot.getMostRecentUpdate();
|
||||
req.bot.send(new MessageComputationRequest(key, status.pathStartX, status.pathStartY, status.pathStartZ, req.goal));
|
||||
}
|
||||
|
||||
public void onRecv(Bot receivedFrom, MessageComputationResponse mcrn) {
|
||||
ComputationRequest req = inProgress.remove(mcrn.computationID);
|
||||
if (req == null) {
|
||||
throw new IllegalStateException("Received completed computation that we didn't ask for");
|
||||
}
|
||||
if (!Objects.equals(req.bot, receivedFrom)) {
|
||||
throw new IllegalStateException("Received completed computation from the wrong connection?????");
|
||||
}
|
||||
req.receivedResponse(mcrn);
|
||||
}
|
||||
|
||||
public boolean inProgress(ComputationRequest req) {
|
||||
return inProgress.values().contains(req);
|
||||
}
|
||||
}
|
||||
22
src/tenor/java/tenor/DependencyType.java
Normal file
22
src/tenor/java/tenor/DependencyType.java
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public enum DependencyType {
|
||||
SERIAL, PARALLEL_ALL, ANY_ONE_OF;
|
||||
}
|
||||
21
src/tenor/java/tenor/IChildTaskRelationship.java
Normal file
21
src/tenor/java/tenor/IChildTaskRelationship.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface IChildTaskRelationship {
|
||||
}
|
||||
23
src/tenor/java/tenor/IClaimProvider.java
Normal file
23
src/tenor/java/tenor/IClaimProvider.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface IClaimProvider {
|
||||
|
||||
int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship);
|
||||
}
|
||||
21
src/tenor/java/tenor/IParentTaskRelationship.java
Normal file
21
src/tenor/java/tenor/IParentTaskRelationship.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface IParentTaskRelationship {
|
||||
}
|
||||
23
src/tenor/java/tenor/IQuantityRelationship.java
Normal file
23
src/tenor/java/tenor/IQuantityRelationship.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface IQuantityRelationship {
|
||||
|
||||
double value(int quantity);
|
||||
}
|
||||
32
src/tenor/java/tenor/IQuantizedChildTaskRelationship.java
Normal file
32
src/tenor/java/tenor/IQuantizedChildTaskRelationship.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface IQuantizedChildTaskRelationship<T extends ITaskNodeBase> extends ITaskRelationshipBase<T, IQuantizedTask>, IChildTaskRelationship {
|
||||
|
||||
double allocatedPriority(int quantity);
|
||||
|
||||
default boolean childProvidesClaimTo(int quantity) {
|
||||
return quantityCompleted() >= quantity;
|
||||
}
|
||||
|
||||
default int quantityCompleted() {
|
||||
// TODO: Resolve this cast, should QuantizedTask implement ClaimProvider?
|
||||
return ((IClaimProvider) childTask()).quantityCompletedForParent(this);
|
||||
}
|
||||
}
|
||||
41
src/tenor/java/tenor/IQuantizedDependentCostCalculator.java
Normal file
41
src/tenor/java/tenor/IQuantizedDependentCostCalculator.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface IQuantizedDependentCostCalculator extends IQuantizedTaskNode {
|
||||
|
||||
@Override
|
||||
default IQuantityRelationship cost() {
|
||||
switch (type()) {
|
||||
case SERIAL:
|
||||
case PARALLEL_ALL:
|
||||
return q -> childTasks().stream()
|
||||
.map(IQuantizedParentTaskRelationship::cost)
|
||||
.mapToDouble(relationship -> relationship.value(q))
|
||||
.sum();
|
||||
case ANY_ONE_OF: // TODO this could be smarter about allocating
|
||||
return q -> childTasks().stream()
|
||||
.map(IQuantizedParentTaskRelationship::cost)
|
||||
.mapToDouble(relationship -> relationship.value(q))
|
||||
.min().orElse(-1);
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
23
src/tenor/java/tenor/IQuantizedParentTaskRelationship.java
Normal file
23
src/tenor/java/tenor/IQuantizedParentTaskRelationship.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface IQuantizedParentTaskRelationship<T extends ITask> extends ITaskRelationshipBase<IQuantizedTaskNode, T>, IParentTaskRelationship {
|
||||
|
||||
IQuantityRelationship cost();
|
||||
}
|
||||
43
src/tenor/java/tenor/IQuantizedTask.java
Normal file
43
src/tenor/java/tenor/IQuantizedTask.java
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface IQuantizedTask extends ITask<IQuantizedChildTaskRelationship> {
|
||||
|
||||
/*default QuantityRelationship priority() {
|
||||
return q -> {
|
||||
double sum = 0;
|
||||
for (IQuantizedChildTaskRelationship parent : parents()) {
|
||||
sum += parent.allocatedPriority(q);
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
}*/
|
||||
|
||||
IQuantityRelationship priority();
|
||||
|
||||
IQuantityRelationship cost();
|
||||
|
||||
default IQuantizedChildTaskRelationship createRelationshipToParent(ITaskNodeBase parent) {
|
||||
if (parent instanceof IQuantizedTask) {
|
||||
return new QuantizedToQuantizedTaskRelationship((IQuantizedTaskNode) parent, this);
|
||||
} else {
|
||||
throw new UnsupportedOperationException("SingularToQuantized must be constructed manually since it needs a quantity");
|
||||
}
|
||||
}
|
||||
}
|
||||
48
src/tenor/java/tenor/IQuantizedTaskNode.java
Normal file
48
src/tenor/java/tenor/IQuantizedTaskNode.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 10/30/2018
|
||||
*/
|
||||
public interface IQuantizedTaskNode extends ITaskNodeBase<IQuantizedChildTaskRelationship, IQuantizedParentTaskRelationship>, IQuantizedTask {
|
||||
// if the child task were able to provide this amount, how much priority would that be?
|
||||
double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity);
|
||||
|
||||
default int quantityExecutingInto(QuantizedToSingularTaskRelationship child) {
|
||||
if (type() != DependencyType.SERIAL) {
|
||||
throw new UnsupportedOperationException(this + " " + child);
|
||||
}
|
||||
// need to calculate from scratch
|
||||
int ind = childTasks().indexOf(child);
|
||||
if (ind <= 0) {
|
||||
throw new IllegalStateException(childTasks() + "");
|
||||
}
|
||||
int minQuantity = -1;
|
||||
for (int i = 0; i < childTasks().indexOf(child); i++) {
|
||||
IQuantizedChildTaskRelationship relationship = (IQuantizedChildTaskRelationship) childTasks().get(i);
|
||||
IClaimProvider claim = (IClaimProvider) relationship.childTask();
|
||||
int amt = claim.quantityCompletedForParent(relationship);
|
||||
if (minQuantity == -1 || amt < minQuantity) {
|
||||
minQuantity = amt;
|
||||
}
|
||||
}
|
||||
return minQuantity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface ISingleParentQuantizedPriorityAllocator extends IQuantizedTask {
|
||||
@Override
|
||||
default IQuantityRelationship priority() {
|
||||
if (parentTasks().size() != 1) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
return parentTasks().get(0)::allocatedPriority;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface ISingleParentSingularPriorityAllocator extends ISingularTask {
|
||||
@Override
|
||||
default double priority() {
|
||||
if (parentTasks().size() != 1) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
return parentTasks().get(0).allocatedPriority();
|
||||
}
|
||||
}
|
||||
23
src/tenor/java/tenor/ISingularChildTaskRelationship.java
Normal file
23
src/tenor/java/tenor/ISingularChildTaskRelationship.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface ISingularChildTaskRelationship<T extends ITaskNodeBase> extends ITaskRelationshipBase<T, ISingularTask>, IChildTaskRelationship {
|
||||
|
||||
double allocatedPriority();
|
||||
}
|
||||
23
src/tenor/java/tenor/ISingularParentTaskRelationship.java
Normal file
23
src/tenor/java/tenor/ISingularParentTaskRelationship.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface ISingularParentTaskRelationship<T extends ITask> extends ITaskRelationshipBase<ISingularTaskNode, T>, IParentTaskRelationship {
|
||||
|
||||
double cost();
|
||||
}
|
||||
32
src/tenor/java/tenor/ISingularTask.java
Normal file
32
src/tenor/java/tenor/ISingularTask.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface ISingularTask extends ITask<ISingularChildTaskRelationship> {
|
||||
double cost();
|
||||
|
||||
double priority();
|
||||
|
||||
default ISingularChildTaskRelationship<? extends ITaskNodeBase> createRelationshipToParent(ITaskNodeBase parent) {
|
||||
if (parent instanceof IQuantizedTask) {
|
||||
return new QuantizedToSingularTaskRelationship((IQuantizedTaskNode) parent, this);
|
||||
} else {
|
||||
return new SingularToSingularTaskRelationship((ISingularTaskNode) parent, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
26
src/tenor/java/tenor/ISingularTaskNode.java
Normal file
26
src/tenor/java/tenor/ISingularTaskNode.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 10/30/2018
|
||||
*/
|
||||
public interface ISingularTaskNode extends ITaskNodeBase<ISingularChildTaskRelationship, ISingularParentTaskRelationship>, ISingularTask {
|
||||
double priorityAllocatedToChild(ISingularParentTaskRelationship relationship);
|
||||
}
|
||||
39
src/tenor/java/tenor/ITask.java
Normal file
39
src/tenor/java/tenor/ITask.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ITask<T extends IChildTaskRelationship & ITaskRelationshipBase> {
|
||||
|
||||
List<T> parentTasks();
|
||||
|
||||
T createRelationshipToParent(ITaskNodeBase parent);
|
||||
|
||||
void addParent(T relationship);
|
||||
|
||||
default void addParent(ITaskNodeBase parent) {
|
||||
addParent(createRelationshipToParent(parent));
|
||||
}
|
||||
|
||||
Bot bot();
|
||||
|
||||
default BotTaskRegistry registry() {
|
||||
return bot().taskRegistry;
|
||||
}
|
||||
}
|
||||
29
src/tenor/java/tenor/ITaskNodeBase.java
Normal file
29
src/tenor/java/tenor/ITaskNodeBase.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ITaskNodeBase<T extends IChildTaskRelationship & ITaskRelationshipBase, S extends IParentTaskRelationship & ITaskRelationshipBase> extends ITask<T> {
|
||||
|
||||
List<S> childTasks();
|
||||
|
||||
DependencyType type();
|
||||
|
||||
void addChild(S relationship);
|
||||
}
|
||||
27
src/tenor/java/tenor/ITaskRelationshipBase.java
Normal file
27
src/tenor/java/tenor/ITaskRelationshipBase.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public interface ITaskRelationshipBase<P extends ITaskNodeBase, C extends ITask> {
|
||||
|
||||
P parentTask();
|
||||
|
||||
C childTask();
|
||||
|
||||
DependencyType type();
|
||||
}
|
||||
24
src/tenor/java/tenor/QuantizedTaskLeaf.java
Normal file
24
src/tenor/java/tenor/QuantizedTaskLeaf.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public abstract class QuantizedTaskLeaf extends TaskLeaf<IQuantizedChildTaskRelationship> implements IQuantizedTask {
|
||||
public QuantizedTaskLeaf(Bot bot) {
|
||||
super(bot);
|
||||
}
|
||||
}
|
||||
24
src/tenor/java/tenor/QuantizedTaskNode.java
Normal file
24
src/tenor/java/tenor/QuantizedTaskNode.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public abstract class QuantizedTaskNode extends TaskNode<IQuantizedChildTaskRelationship, IQuantizedParentTaskRelationship> implements IQuantizedTaskNode {
|
||||
public QuantizedTaskNode(Bot bot, DependencyType type) {
|
||||
super(bot, type);
|
||||
}
|
||||
}
|
||||
141
src/tenor/java/tenor/QuantizedTaskPriorityAllocationCache.java
Normal file
141
src/tenor/java/tenor/QuantizedTaskPriorityAllocationCache.java
Normal file
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class QuantizedTaskPriorityAllocationCache extends QuantizedTaskNode {
|
||||
|
||||
private final Map<Integer, Allocation> allocationByQuantity;
|
||||
|
||||
public QuantizedTaskPriorityAllocationCache(Bot bot, DependencyType type) {
|
||||
super(bot, type);
|
||||
this.allocationByQuantity = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity) {
|
||||
Allocation allocation = allocationByQuantity.get(quantity);
|
||||
if (allocation == null || allocation.total != effectiveAllocationSize(quantity)) {
|
||||
allocation = allocationStrategy(quantity);
|
||||
allocationByQuantity.put(quantity, allocation);
|
||||
}
|
||||
return allocation.allocatedTo(child);
|
||||
}
|
||||
|
||||
protected Allocation allocationStrategy(int quantity) { // overridable
|
||||
double amount = effectiveAllocationSize(quantity);
|
||||
Allocation alloc = new Allocation(amount);
|
||||
List<IQuantizedParentTaskRelationship> children = childTasks();
|
||||
if (children.size() < 2) {
|
||||
return alloc.distributeEqually(); // 0 or 1 cannot be anything but equal distribution
|
||||
}
|
||||
double[] costs = new double[children.size()];
|
||||
for (int i = 0; i < costs.length; i++) {
|
||||
costs[i] = children.get(i).cost().value(quantity);
|
||||
}
|
||||
applyAllocation(alloc, costs);
|
||||
return alloc;
|
||||
}
|
||||
|
||||
protected void applyAllocation(Allocation alloc, double[] childCosts) { // overridable
|
||||
switch (type) {
|
||||
case ANY_ONE_OF:
|
||||
// by default, give it all to the lowest cost
|
||||
// this is stable
|
||||
int lowestInd = 0;
|
||||
for (int i = 1; i < childCosts.length; i++) {
|
||||
if (childCosts[i] < childCosts[lowestInd]) {
|
||||
lowestInd = i;
|
||||
}
|
||||
}
|
||||
alloc.givePriority(lowestInd, alloc.unallocated());
|
||||
case SERIAL:
|
||||
// give it all to the first nonzero cost? maybe?
|
||||
for (int i = 1; i < childCosts.length; i++) {
|
||||
if (childCosts[i] > 0) {
|
||||
alloc.givePriority(i, alloc.unallocated());
|
||||
return;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new UnsupportedOperationException("Not implemented yet");
|
||||
}
|
||||
}
|
||||
|
||||
protected Allocation blend(Allocation previous, Allocation current) { // overridable
|
||||
if (previous.priorities.length != current.priorities.length) {
|
||||
return current;
|
||||
}
|
||||
Allocation blended = new Allocation(current.total);
|
||||
for (int i = 0; i < current.priorities.length; i++) {
|
||||
blended.givePriority(i, current.priorities[i] * 0.1d + previous.priorities[i] * 0.9d);
|
||||
}
|
||||
return blended;
|
||||
}
|
||||
|
||||
protected double effectiveAllocationSize(int quantity) { // overridable
|
||||
return priority().value(quantity);
|
||||
}
|
||||
|
||||
protected class Allocation {
|
||||
private final double[] priorities; // always sums up to 1 or less
|
||||
private final double total;
|
||||
|
||||
public Allocation(double total) {
|
||||
this.priorities = new double[childTasks().size()];
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public double unallocated() {
|
||||
return 1 - allocated();
|
||||
}
|
||||
|
||||
public double allocated() {
|
||||
double sum = 0;
|
||||
for (double d : priorities) {
|
||||
sum += d;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public Allocation distributeEqually() {
|
||||
int count = priorities.length;
|
||||
double toDistribute = unallocated();
|
||||
double priorityToEach = toDistribute / count;
|
||||
for (int i = 0; i < count; i++) {
|
||||
priorities[i] += priorityToEach;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void givePriority(int childTaskIndex, double amount) {
|
||||
if (amount > unallocated()) {
|
||||
throw new IllegalStateException("not enough space");
|
||||
}
|
||||
priorities[childTaskIndex] += amount;
|
||||
}
|
||||
|
||||
public double allocatedTo(IQuantizedParentTaskRelationship childRelationship) {
|
||||
return priorities[childTasks().indexOf(childRelationship)];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public class QuantizedToQuantizedTaskRelationship
|
||||
extends TaskRelationship<IQuantizedTaskNode, IQuantizedTask>
|
||||
implements IQuantizedChildTaskRelationship<IQuantizedTaskNode>, IQuantizedParentTaskRelationship<IQuantizedTask> {
|
||||
|
||||
public QuantizedToQuantizedTaskRelationship(IQuantizedTaskNode parent, IQuantizedTask child) {
|
||||
super(parent, child);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double allocatedPriority(int quantity) {
|
||||
return parentTask().priorityAllocatedTo(this, quantity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IQuantityRelationship cost() {
|
||||
return childTask().cost();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public class QuantizedToSingularTaskRelationship
|
||||
extends TaskRelationship<IQuantizedTaskNode, ISingularTask>
|
||||
implements ISingularChildTaskRelationship<IQuantizedTaskNode>, IQuantizedParentTaskRelationship<ISingularTask> {
|
||||
|
||||
public QuantizedToSingularTaskRelationship(IQuantizedTaskNode parent, ISingularTask child) {
|
||||
super(parent, child);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IQuantityRelationship cost() {
|
||||
return x -> childTask().cost();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double allocatedPriority() {
|
||||
return parentTask().priorityAllocatedTo(this, parentTask().quantityExecutingInto(this));
|
||||
}
|
||||
}
|
||||
80
src/tenor/java/tenor/ScarceParentPriorityAllocator.java
Normal file
80
src/tenor/java/tenor/ScarceParentPriorityAllocator.java
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ScarceParentPriorityAllocator {
|
||||
|
||||
public static PriorityAllocation priorityAllocation(int quantity, List<IQuantizedChildTaskRelationship> parents) {
|
||||
if (quantity == 0) {
|
||||
return new PriorityAllocation(new int[parents.size()], 0D);
|
||||
}
|
||||
double[][] priorities = new double[parents.size()][quantity];
|
||||
for (int i = 0; i < parents.size(); i++) {
|
||||
for (int j = 1; j < quantity; j++) {
|
||||
priorities[i][j] = parents.get(i).allocatedPriority(j);
|
||||
}
|
||||
}
|
||||
|
||||
int filled = 0;
|
||||
double totalPriority = 0;
|
||||
int[] taken = new int[parents.size()];
|
||||
while (true) {
|
||||
|
||||
double bestRatio = 0;
|
||||
int bestParent = -1;
|
||||
int bestQuantity = -1;
|
||||
for (int i = 0; i < priorities.length; i++) {
|
||||
for (int j = 1; j < quantity - filled; j++) {
|
||||
double ratio = priorities[i][j] / j;
|
||||
if (ratio > bestRatio) {
|
||||
bestRatio = ratio;
|
||||
bestParent = i;
|
||||
bestQuantity = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bestParent == -1 || bestRatio == 0) {
|
||||
return new PriorityAllocation(taken, totalPriority);
|
||||
}
|
||||
taken[bestParent] += bestQuantity;
|
||||
filled += bestQuantity;
|
||||
double priorityTaken = priorities[bestParent][bestQuantity];
|
||||
totalPriority += priorityTaken;
|
||||
|
||||
double[] repl = new double[priorities[bestParent].length - bestQuantity];
|
||||
for (int i = 0; i < repl.length; i++) {
|
||||
repl[i] = priorities[bestParent][i + bestQuantity] - priorityTaken;
|
||||
}
|
||||
priorities[bestParent] = repl;
|
||||
}
|
||||
}
|
||||
|
||||
public static class PriorityAllocation {
|
||||
|
||||
public final int[] parentAllocationQuantity;
|
||||
public final double totalPriority;
|
||||
|
||||
public PriorityAllocation(int[] parentAllocationQuantity, double totalPriority) {
|
||||
this.parentAllocationQuantity = parentAllocationQuantity;
|
||||
this.totalPriority = totalPriority;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
src/tenor/java/tenor/SingularTaskLeaf.java
Normal file
24
src/tenor/java/tenor/SingularTaskLeaf.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public abstract class SingularTaskLeaf extends TaskLeaf<ISingularChildTaskRelationship> implements ISingularTask {
|
||||
public SingularTaskLeaf(Bot bot) {
|
||||
super(bot);
|
||||
}
|
||||
}
|
||||
25
src/tenor/java/tenor/SingularTaskNode.java
Normal file
25
src/tenor/java/tenor/SingularTaskNode.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public abstract class SingularTaskNode extends TaskNode<ISingularChildTaskRelationship, ISingularParentTaskRelationship> implements ISingularTaskNode {
|
||||
|
||||
public SingularTaskNode(Bot bot, DependencyType type) {
|
||||
super(bot, type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public class SingularToQuantizedTaskRelationship
|
||||
extends TaskRelationship<ISingularTaskNode, IQuantizedTask>
|
||||
implements IQuantizedChildTaskRelationship<ISingularTaskNode>, ISingularParentTaskRelationship<IQuantizedTask> {
|
||||
|
||||
public int quantityRequired;
|
||||
|
||||
public SingularToQuantizedTaskRelationship(ISingularTaskNode parent, IQuantizedTask child, int quantityRequired) {
|
||||
super(parent, child);
|
||||
this.quantityRequired = quantityRequired;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double allocatedPriority(int quantity) {
|
||||
return quantity >= quantityRequired ? parentTask().priorityAllocatedToChild(this) : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double cost() {
|
||||
return childTask().cost().value(quantityRequired);
|
||||
}
|
||||
}
|
||||
37
src/tenor/java/tenor/SingularToSingularTaskRelationship.java
Normal file
37
src/tenor/java/tenor/SingularToSingularTaskRelationship.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public class SingularToSingularTaskRelationship
|
||||
extends TaskRelationship<ISingularTaskNode, ISingularTask>
|
||||
implements ISingularChildTaskRelationship<ISingularTaskNode>, ISingularParentTaskRelationship<ISingularTask> {
|
||||
|
||||
public SingularToSingularTaskRelationship(ISingularTaskNode parent, ISingularTask child) {
|
||||
super(parent, child);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double allocatedPriority() {
|
||||
return parentTask().priorityAllocatedToChild(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double cost() {
|
||||
return childTask().cost();
|
||||
}
|
||||
}
|
||||
52
src/tenor/java/tenor/Task.java
Normal file
52
src/tenor/java/tenor/Task.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Task<T extends IChildTaskRelationship & ITaskRelationshipBase> implements ITask<T> {
|
||||
|
||||
public final Bot bot;
|
||||
|
||||
private final List<T> parentRelationships = new ArrayList<>();
|
||||
|
||||
public Task(Bot bot) {
|
||||
this.bot = bot;
|
||||
registry().register(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bot bot() {
|
||||
return bot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> parentTasks() {
|
||||
return parentRelationships;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParent(T relationship) {
|
||||
if (relationship.childTask() != this) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
relationship.parentTask().addChild((IParentTaskRelationship) relationship);
|
||||
parentRelationships.add(relationship);
|
||||
}
|
||||
}
|
||||
24
src/tenor/java/tenor/TaskLeaf.java
Normal file
24
src/tenor/java/tenor/TaskLeaf.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public abstract class TaskLeaf<T extends IChildTaskRelationship & ITaskRelationshipBase> extends Task<T> {
|
||||
public TaskLeaf(Bot bot) {
|
||||
super(bot);
|
||||
}
|
||||
}
|
||||
56
src/tenor/java/tenor/TaskNode.java
Normal file
56
src/tenor/java/tenor/TaskNode.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class TaskNode<T extends IChildTaskRelationship & ITaskRelationshipBase, S extends IParentTaskRelationship & ITaskRelationshipBase> extends Task<T> implements ITaskNodeBase<T, S> {
|
||||
|
||||
private final List<S> childRelationships = new ArrayList<>();
|
||||
public final DependencyType type;
|
||||
|
||||
public TaskNode(Bot bot, DependencyType type) {
|
||||
super(bot);
|
||||
this.type = type;
|
||||
if (type == null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<S> childTasks() {
|
||||
return childRelationships;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DependencyType type() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addChild(S relationship) {
|
||||
if (relationship.parentTask() != this) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if (relationship.type() != type) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
childRelationships.add(relationship);
|
||||
}
|
||||
}
|
||||
49
src/tenor/java/tenor/TaskRelationship.java
Normal file
49
src/tenor/java/tenor/TaskRelationship.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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 tenor;
|
||||
|
||||
public class TaskRelationship<P extends ITaskNodeBase, C extends ITask> implements ITaskRelationshipBase<P, C> {
|
||||
|
||||
public final P parent;
|
||||
public final C child;
|
||||
public final DependencyType type;
|
||||
|
||||
public TaskRelationship(P parent, C child) {
|
||||
this.parent = parent;
|
||||
this.child = child;
|
||||
this.type = parent.type();
|
||||
if (parent.bot() != child.bot()) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public P parentTask() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public C childTask() {
|
||||
return child;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DependencyType type() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
86
src/tenor/java/tenor/game/AquireCraftingItems.java
Normal file
86
src/tenor/java/tenor/game/AquireCraftingItems.java
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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 tenor.game;
|
||||
|
||||
import tenor.*;
|
||||
|
||||
public class AquireCraftingItems extends QuantizedTaskNode implements IClaimProvider, ISingleParentQuantizedPriorityAllocator {
|
||||
|
||||
final CraftingTask parent;
|
||||
|
||||
public AquireCraftingItems(CraftingTask parent) {
|
||||
super(parent.bot, DependencyType.PARALLEL_ALL);
|
||||
this.parent = parent;
|
||||
addParent(parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity) {
|
||||
AquireItemTask resource = (AquireItemTask) child.childTask(); // all our dependents are aquire item tasks
|
||||
int amount = parent.inputSizeFor(resource);
|
||||
|
||||
// they could provide us with quantity
|
||||
int actualQuantity = (int) Math.ceil(quantity * 1.0D / amount);
|
||||
// so we could do the crafting recipe this many times
|
||||
// how good would that be?
|
||||
return priority().value(actualQuantity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IQuantityRelationship cost() {
|
||||
return x -> {
|
||||
// cost to get x copies of these items
|
||||
double sum = 0;
|
||||
for (IQuantizedParentTaskRelationship resource : childTasks()) {
|
||||
int amountPerCraft = parent.inputSizeFor((AquireItemTask) resource.childTask());
|
||||
int totalAmountNeeded = x * amountPerCraft;
|
||||
|
||||
int amountForUs = ((IQuantizedChildTaskRelationship) resource).quantityCompleted();
|
||||
totalAmountNeeded -= amountForUs;
|
||||
|
||||
if (totalAmountNeeded <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
sum += resource.cost().value(totalAmountNeeded);
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship) {
|
||||
if (relationship != parentTasks().get(0)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
// our only parent is the crafting task
|
||||
int minCompletion = Integer.MAX_VALUE;
|
||||
for (IQuantizedParentTaskRelationship resource : childTasks()) {
|
||||
int amountForUs = ((IQuantizedChildTaskRelationship) resource).quantityCompleted();
|
||||
|
||||
int amountPerCraft = parent.inputSizeFor((AquireItemTask) resource.childTask());
|
||||
|
||||
int actualQuantity = (int) Math.ceil(amountForUs * 1.0D / amountPerCraft);
|
||||
|
||||
if (actualQuantity < minCompletion || minCompletion == Integer.MAX_VALUE) {
|
||||
minCompletion = actualQuantity; // any missing ingredient and we aren't really done
|
||||
}
|
||||
}
|
||||
return minCompletion;
|
||||
}
|
||||
}
|
||||
81
src/tenor/java/tenor/game/AquireItemTask.java
Normal file
81
src/tenor/java/tenor/game/AquireItemTask.java
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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 tenor.game;
|
||||
|
||||
import tenor.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class AquireItemTask extends QuantizedTaskPriorityAllocationCache implements IClaimProvider, IQuantizedDependentCostCalculator {
|
||||
|
||||
HashMap<IQuantizedChildTaskRelationship, Integer> allocation; // allocation of what tasks have claim over what items in our inventory i guess
|
||||
String item;
|
||||
|
||||
public AquireItemTask(Bot bot, String item) {
|
||||
super(bot, DependencyType.ANY_ONE_OF);
|
||||
this.item = item;
|
||||
registry().registerItemBased(this, item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship) {
|
||||
return allocation.get(relationship);
|
||||
}
|
||||
|
||||
public void reallocate() {
|
||||
int amountToAllocate = getCurrentQuantityInInventory();
|
||||
if (amountToAllocate == cachedCurrentQuantity()) {
|
||||
// no change
|
||||
return;
|
||||
}
|
||||
List<IQuantizedChildTaskRelationship> parents = parentTasks();
|
||||
|
||||
allocation = null;
|
||||
HashMap<IQuantizedChildTaskRelationship, Integer> tmp = new HashMap<>();
|
||||
|
||||
int[] newAmounts = ScarceParentPriorityAllocator.priorityAllocation(amountToAllocate, parents).parentAllocationQuantity;
|
||||
for (int i = 0; i < parents.size(); i++) {
|
||||
tmp.put(parents.get(i), newAmounts[i]);
|
||||
}
|
||||
allocation = tmp;
|
||||
}
|
||||
|
||||
public int getCurrentQuantityInInventory() {
|
||||
return bot.getCurrentQuantityInInventory(item);
|
||||
}
|
||||
|
||||
public int cachedCurrentQuantity() {
|
||||
return allocation.values().stream().mapToInt(x -> x).sum();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IQuantityRelationship priority() { // TODO cache
|
||||
return x -> ScarceParentPriorityAllocator.priorityAllocation(x, parentTasks()).totalPriority;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double effectiveAllocationSize(int quantity) {
|
||||
// how much of our priority would go to this child if it could provide us with quantity of the item we need
|
||||
|
||||
// here's the thing honey, we *already have* some, so you're really asking what's the priority of getting quantity MORE
|
||||
int curr = cachedCurrentQuantity();
|
||||
|
||||
return priority().value(quantity + curr) - priority().value(curr);
|
||||
}
|
||||
}
|
||||
77
src/tenor/java/tenor/game/CraftingTask.java
Normal file
77
src/tenor/java/tenor/game/CraftingTask.java
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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 tenor.game;
|
||||
|
||||
import tenor.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CraftingTask extends QuantizedTaskNode implements ISingleParentQuantizedPriorityAllocator {
|
||||
|
||||
int outputQuantity;
|
||||
List<RecipeInput> recipe;
|
||||
|
||||
final AquireCraftingItems inputs;
|
||||
final GetToCraftingTableTask step2;
|
||||
final DoCraft step3;
|
||||
|
||||
final AquireItemTask parent;
|
||||
|
||||
public CraftingTask(AquireItemTask parent) {
|
||||
super(parent.bot, DependencyType.SERIAL);
|
||||
this.inputs = new AquireCraftingItems(this); // this adds the relationship
|
||||
this.step2 = registry().getSingleton(GetToCraftingTableTask.class);
|
||||
step2.addParent(this);
|
||||
this.step3 = new DoCraft(this);
|
||||
|
||||
this.parent = parent;
|
||||
addParent(parent);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public IQuantityRelationship cost() {
|
||||
return x -> {
|
||||
int actualQuantity = (int) Math.ceil(x * 1.0D / outputQuantity);
|
||||
return inputs.cost().value(actualQuantity) + step2.cost() + step3.cost();
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity) {
|
||||
// how much priority would we give this child if they could provide us this quantity?
|
||||
int amountWeWouldProvide = quantity * outputQuantity;
|
||||
double desirability = priority().value(amountWeWouldProvide);
|
||||
return desirability;
|
||||
}
|
||||
|
||||
public int inputSizeFor(AquireItemTask task) {
|
||||
for (RecipeInput item : recipe) {
|
||||
if (item.task.equals(task)) {
|
||||
return item.quantity;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
private class RecipeInput {
|
||||
|
||||
private AquireItemTask task;
|
||||
private int quantity;
|
||||
}
|
||||
}
|
||||
39
src/tenor/java/tenor/game/DoCraft.java
Normal file
39
src/tenor/java/tenor/game/DoCraft.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 tenor.game;
|
||||
|
||||
import tenor.ISingleParentSingularPriorityAllocator;
|
||||
import tenor.SingularTaskLeaf;
|
||||
|
||||
public class DoCraft extends SingularTaskLeaf implements ISingleParentSingularPriorityAllocator { // TODO this should be quantized!
|
||||
|
||||
public final CraftingTask parent;
|
||||
|
||||
public DoCraft(CraftingTask parent) {
|
||||
super(parent.bot);
|
||||
this.parent = parent;
|
||||
addParent(parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double cost() {
|
||||
// this is a dummy task to represent actually completing the crafting, given that we have all the ingredients and are at a crafting table
|
||||
// it's effectively free, so let's say it takes 1 second (20 ticks)
|
||||
return 20D;
|
||||
}
|
||||
}
|
||||
37
src/tenor/java/tenor/game/DoMine.java
Normal file
37
src/tenor/java/tenor/game/DoMine.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 tenor.game;
|
||||
|
||||
import tenor.IQuantityRelationship;
|
||||
import tenor.ISingleParentQuantizedPriorityAllocator;
|
||||
import tenor.QuantizedTaskLeaf;
|
||||
|
||||
public class DoMine extends QuantizedTaskLeaf implements ISingleParentQuantizedPriorityAllocator {
|
||||
final MineWithToolTask parent;
|
||||
|
||||
public DoMine(MineWithToolTask parent) {
|
||||
super(parent.bot);
|
||||
this.parent = parent;
|
||||
addParent(parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IQuantityRelationship cost() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
46
src/tenor/java/tenor/game/GetToCraftingTableTask.java
Normal file
46
src/tenor/java/tenor/game/GetToCraftingTableTask.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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 tenor.game;
|
||||
|
||||
import tenor.*;
|
||||
|
||||
public class GetToCraftingTableTask extends SingularTaskLeaf {
|
||||
|
||||
private final ComputationRequest craftingTable;
|
||||
|
||||
public GetToCraftingTableTask(Bot bot) {
|
||||
super(bot);
|
||||
registry().registerSingleton(this);
|
||||
this.craftingTable = ComputationRequestManager.INSTANCE.getByGoal(this, "GoalGetToBlock crafting_table"); // idk
|
||||
}
|
||||
|
||||
@Override
|
||||
public double cost() {
|
||||
Double d = craftingTable.cost();
|
||||
if (d == null) {
|
||||
// unknown, has not been calculated yet either way
|
||||
return 1000D; // estimate
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double priority() {
|
||||
return parentTasks().stream().mapToDouble(ISingularChildTaskRelationship::allocatedPriority).sum();
|
||||
}
|
||||
}
|
||||
57
src/tenor/java/tenor/game/MineTask.java
Normal file
57
src/tenor/java/tenor/game/MineTask.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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 tenor.game;
|
||||
|
||||
import tenor.DependencyType;
|
||||
import tenor.IQuantizedDependentCostCalculator;
|
||||
import tenor.ISingleParentQuantizedPriorityAllocator;
|
||||
import tenor.QuantizedTaskPriorityAllocationCache;
|
||||
|
||||
public class MineTask extends QuantizedTaskPriorityAllocationCache implements ISingleParentQuantizedPriorityAllocator, IQuantizedDependentCostCalculator {
|
||||
|
||||
// TODO shared claims of block locations in the world across all mine tasks across all bots
|
||||
|
||||
final AquireItemTask parent;
|
||||
final MineWithToolTask[] children;
|
||||
|
||||
|
||||
public MineTask(AquireItemTask parent) {
|
||||
super(parent.bot, DependencyType.ANY_ONE_OF);
|
||||
this.parent = parent;
|
||||
addParent(parent);
|
||||
String[] tools = getToolsCapableOfMining(parent.item);
|
||||
children = new MineWithToolTask[tools.length];
|
||||
for (int i = 0; i < children.length; i++) {
|
||||
children[i] = new MineWithToolTask(this, tools[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static String[] getToolsCapableOfMining(String block) {
|
||||
switch (block) {
|
||||
case "iron_ore":
|
||||
return new String[]{"stone_pickaxe"};
|
||||
case "stone":
|
||||
case "cobblestone":
|
||||
return new String[]{"wooden_pickaxe"};
|
||||
case "log":
|
||||
return new String[]{"hand"};
|
||||
default:
|
||||
throw new IllegalStateException(block);
|
||||
}
|
||||
}
|
||||
}
|
||||
44
src/tenor/java/tenor/game/MineWithToolTask.java
Normal file
44
src/tenor/java/tenor/game/MineWithToolTask.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 tenor.game;
|
||||
|
||||
import tenor.DependencyType;
|
||||
import tenor.IQuantizedDependentCostCalculator;
|
||||
import tenor.ISingleParentQuantizedPriorityAllocator;
|
||||
import tenor.QuantizedTaskPriorityAllocationCache;
|
||||
|
||||
public class MineWithToolTask extends QuantizedTaskPriorityAllocationCache implements ISingleParentQuantizedPriorityAllocator, IQuantizedDependentCostCalculator {
|
||||
|
||||
public final String tool;
|
||||
MineTask parent;
|
||||
|
||||
AquireItemTask aquireTool;
|
||||
// TODO locate task?
|
||||
DoMine doMine;
|
||||
|
||||
|
||||
public MineWithToolTask(MineTask parent, String tool) {
|
||||
super(parent.bot, DependencyType.SERIAL);
|
||||
this.tool = tool;
|
||||
this.parent = parent;
|
||||
addParent(parent);
|
||||
this.aquireTool = registry().getItemBased(AquireItemTask.class, tool);
|
||||
aquireTool.addParent(this); // we aren't constructing this, so need to add us as a parent manually
|
||||
this.doMine = new DoMine(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user