bunch of stuff

This commit is contained in:
Leijurv
2018-10-31 20:04:32 -07:00
parent 24bb0c541c
commit 890dbec852
23 changed files with 222 additions and 35 deletions
+26
View 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;
public class Bot {
public final BotTaskRegistry taskRegistry = new BotTaskRegistry(this);
public int getCurrentQuantityInInventory(String item) {
throw new UnsupportedOperationException("oppa");
}
}
+115
View 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);
}
}
}
+1 -5
View File
@@ -35,13 +35,9 @@ public interface IQuantizedTask extends ITask<IQuantizedChildTaskRelationship> {
default IQuantizedChildTaskRelationship createRelationshipToParent(ITaskNodeBase parent) { default IQuantizedChildTaskRelationship createRelationshipToParent(ITaskNodeBase parent) {
if (parent instanceof IQuantizedTask) { if (parent instanceof IQuantizedTask) {
return new QuantizedToQuantizedTaskRelationship((QuantizedTaskNode) parent, this, parent.type()); return new QuantizedToQuantizedTaskRelationship((IQuantizedTaskNode) parent, this);
} else { } else {
throw new UnsupportedOperationException("SingularToQuantized must be constructed manually since it needs a quantity"); throw new UnsupportedOperationException("SingularToQuantized must be constructed manually since it needs a quantity");
} }
} }
default QuantizedToQuantizedTaskRelationship createRelationshipToParent(QuantizedTaskNode parent) {
return new QuantizedToQuantizedTaskRelationship(parent, this, parent.type());
}
} }
+2 -2
View File
@@ -24,9 +24,9 @@ public interface ISingularTask extends ITask<ISingularChildTaskRelationship> {
default ISingularChildTaskRelationship<? extends ITaskNodeBase> createRelationshipToParent(ITaskNodeBase parent) { default ISingularChildTaskRelationship<? extends ITaskNodeBase> createRelationshipToParent(ITaskNodeBase parent) {
if (parent instanceof IQuantizedTask) { if (parent instanceof IQuantizedTask) {
return new QuantizedToSingularTaskRelationship((QuantizedTaskNode) parent, this, parent.type()); return new QuantizedToSingularTaskRelationship((IQuantizedTaskNode) parent, this);
} else { } else {
return new SingularToSingularTaskRelationship((SingularTaskNode) parent, this, parent.type()); return new SingularToSingularTaskRelationship((ISingularTaskNode) parent, this);
} }
} }
} }
+6
View File
@@ -30,4 +30,10 @@ public interface ITask<T extends IChildTaskRelationship & ITaskRelationshipBase>
default void addParent(ITaskNodeBase parent) { default void addParent(ITaskNodeBase parent) {
addParent(createRelationshipToParent(parent)); addParent(createRelationshipToParent(parent));
} }
Bot bot();
default BotTaskRegistry registry() {
return bot().taskRegistry;
}
} }
@@ -18,4 +18,7 @@
package tenor; package tenor;
public abstract class QuantizedTaskLeaf extends TaskLeaf<IQuantizedChildTaskRelationship> implements IQuantizedTask { public abstract class QuantizedTaskLeaf extends TaskLeaf<IQuantizedChildTaskRelationship> implements IQuantizedTask {
public QuantizedTaskLeaf(Bot bot) {
super(bot);
}
} }
+2 -3
View File
@@ -18,8 +18,7 @@
package tenor; package tenor;
public abstract class QuantizedTaskNode extends TaskNode<IQuantizedChildTaskRelationship, IQuantizedParentTaskRelationship> implements IQuantizedTaskNode { public abstract class QuantizedTaskNode extends TaskNode<IQuantizedChildTaskRelationship, IQuantizedParentTaskRelationship> implements IQuantizedTaskNode {
public QuantizedTaskNode(Bot bot, DependencyType type) {
public QuantizedTaskNode(DependencyType type) { super(bot, type);
super(type);
} }
} }
@@ -21,8 +21,8 @@ public class QuantizedToQuantizedTaskRelationship
extends TaskRelationship<IQuantizedTaskNode, IQuantizedTask> extends TaskRelationship<IQuantizedTaskNode, IQuantizedTask>
implements IQuantizedChildTaskRelationship<IQuantizedTaskNode>, IQuantizedParentTaskRelationship<IQuantizedTask> { implements IQuantizedChildTaskRelationship<IQuantizedTaskNode>, IQuantizedParentTaskRelationship<IQuantizedTask> {
public QuantizedToQuantizedTaskRelationship(IQuantizedTaskNode parent, IQuantizedTask child, DependencyType type) { public QuantizedToQuantizedTaskRelationship(IQuantizedTaskNode parent, IQuantizedTask child) {
super(parent, child, type); super(parent, child);
} }
@Override @Override
@@ -21,8 +21,8 @@ public class QuantizedToSingularTaskRelationship
extends TaskRelationship<IQuantizedTaskNode, ISingularTask> extends TaskRelationship<IQuantizedTaskNode, ISingularTask>
implements ISingularChildTaskRelationship<IQuantizedTaskNode>, IQuantizedParentTaskRelationship<ISingularTask> { implements ISingularChildTaskRelationship<IQuantizedTaskNode>, IQuantizedParentTaskRelationship<ISingularTask> {
public QuantizedToSingularTaskRelationship(IQuantizedTaskNode parent, ISingularTask child, DependencyType type) { public QuantizedToSingularTaskRelationship(IQuantizedTaskNode parent, ISingularTask child) {
super(parent, child, type); super(parent, child);
} }
@Override @Override
@@ -18,4 +18,7 @@
package tenor; package tenor;
public abstract class SingularTaskLeaf extends TaskLeaf<ISingularChildTaskRelationship> implements ISingularTask { public abstract class SingularTaskLeaf extends TaskLeaf<ISingularChildTaskRelationship> implements ISingularTask {
public SingularTaskLeaf(Bot bot) {
super(bot);
}
} }
+2 -2
View File
@@ -19,7 +19,7 @@ package tenor;
public abstract class SingularTaskNode extends TaskNode<ISingularChildTaskRelationship, ISingularParentTaskRelationship> implements ISingularTaskNode { public abstract class SingularTaskNode extends TaskNode<ISingularChildTaskRelationship, ISingularParentTaskRelationship> implements ISingularTaskNode {
public SingularTaskNode(DependencyType type) { public SingularTaskNode(Bot bot, DependencyType type) {
super(type); super(bot, type);
} }
} }
@@ -23,8 +23,8 @@ public class SingularToQuantizedTaskRelationship
public int quantityRequired; public int quantityRequired;
public SingularToQuantizedTaskRelationship(ISingularTaskNode parent, IQuantizedTask child, DependencyType type, int quantityRequired) { public SingularToQuantizedTaskRelationship(ISingularTaskNode parent, IQuantizedTask child, int quantityRequired) {
super(parent, child, type); super(parent, child);
this.quantityRequired = quantityRequired; this.quantityRequired = quantityRequired;
} }
@@ -21,8 +21,8 @@ public class SingularToSingularTaskRelationship
extends TaskRelationship<ISingularTaskNode, ISingularTask> extends TaskRelationship<ISingularTaskNode, ISingularTask>
implements ISingularChildTaskRelationship<ISingularTaskNode>, ISingularParentTaskRelationship<ISingularTask> { implements ISingularChildTaskRelationship<ISingularTaskNode>, ISingularParentTaskRelationship<ISingularTask> {
public SingularToSingularTaskRelationship(ISingularTaskNode parent, ISingularTask child, DependencyType type) { public SingularToSingularTaskRelationship(ISingularTaskNode parent, ISingularTask child) {
super(parent, child, type); super(parent, child);
} }
@Override @Override
+14 -1
View File
@@ -17,11 +17,24 @@
package tenor; package tenor;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public abstract class Task<T extends IChildTaskRelationship & ITaskRelationshipBase> implements ITask<T> { public abstract class Task<T extends IChildTaskRelationship & ITaskRelationshipBase> implements ITask<T> {
List<T> parentRelationships; 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 @Override
public List<T> parentTasks() { public List<T> parentTasks() {
+3
View File
@@ -18,4 +18,7 @@
package tenor; package tenor;
public abstract class TaskLeaf<T extends IChildTaskRelationship & ITaskRelationshipBase> extends Task<T> { public abstract class TaskLeaf<T extends IChildTaskRelationship & ITaskRelationshipBase> extends Task<T> {
public TaskLeaf(Bot bot) {
super(bot);
}
} }
+4 -2
View File
@@ -17,14 +17,16 @@
package tenor; package tenor;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public abstract class TaskNode<T extends IChildTaskRelationship & ITaskRelationshipBase, S extends IParentTaskRelationship & ITaskRelationshipBase> extends Task<T> implements ITaskNodeBase<T, S> { public abstract class TaskNode<T extends IChildTaskRelationship & ITaskRelationshipBase, S extends IParentTaskRelationship & ITaskRelationshipBase> extends Task<T> implements ITaskNodeBase<T, S> {
List<S> childRelationships; private final List<S> childRelationships = new ArrayList<>();
public final DependencyType type; public final DependencyType type;
public TaskNode(DependencyType type) { public TaskNode(Bot bot, DependencyType type) {
super(bot);
this.type = type; this.type = type;
} }
+3 -3
View File
@@ -23,11 +23,11 @@ public class TaskRelationship<P extends ITaskNodeBase, C extends ITask> implemen
public final C child; public final C child;
public final DependencyType type; public final DependencyType type;
public TaskRelationship(P parent, C child, DependencyType type) { public TaskRelationship(P parent, C child) {
this.parent = parent; this.parent = parent;
this.child = child; this.child = child;
this.type = type; this.type = parent.type();
if (parent.type() != type) { if (parent.bot() != child.bot()) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} }
@@ -25,6 +25,7 @@ public class ActuallyCraftTask extends SingularTaskLeaf implements ISingleParent
public final CraftingTask parent; public final CraftingTask parent;
public ActuallyCraftTask(CraftingTask parent) { public ActuallyCraftTask(CraftingTask parent) {
super(parent.bot);
this.parent = parent; this.parent = parent;
addParent(parent); addParent(parent);
} }
@@ -24,7 +24,7 @@ public class AquireCraftingItems extends QuantizedTaskNode implements IClaimProv
final CraftingTask parent; final CraftingTask parent;
public AquireCraftingItems(CraftingTask parent) { public AquireCraftingItems(CraftingTask parent) {
super(DependencyType.PARALLEL_ALL); super(parent.bot, DependencyType.PARALLEL_ALL);
this.parent = parent; this.parent = parent;
addParent(parent); addParent(parent);
} }
+17 -5
View File
@@ -26,9 +26,12 @@ import java.util.Map;
public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider, IQuantizedDependentCostCalculator { public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider, IQuantizedDependentCostCalculator {
HashMap<IQuantizedChildTaskRelationship, Integer> allocation; // allocation of what tasks have claim over what items in our inventory i guess HashMap<IQuantizedChildTaskRelationship, Integer> allocation; // allocation of what tasks have claim over what items in our inventory i guess
String item;
public AquireItemTask() { public AquireItemTask(Bot bot, String item) {
super(DependencyType.ANY_ONE_OF); super(bot, DependencyType.ANY_ONE_OF);
this.item = item;
registry().registerItemBased(this, item);
} }
@Override @Override
@@ -37,11 +40,16 @@ public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider,
} }
public void reallocate() { public void reallocate() {
int amountToAllocate = getCurrentQuantityInInventory();
if (amountToAllocate == cachedCurrentQuantity()) {
// no change
return;
}
List<IQuantizedChildTaskRelationship> parents = parentTasks(); List<IQuantizedChildTaskRelationship> parents = parentTasks();
allocation = null; allocation = null;
HashMap<IQuantizedChildTaskRelationship, Integer> tmp = new HashMap<>(); HashMap<IQuantizedChildTaskRelationship, Integer> tmp = new HashMap<>();
int amountToAllocate = getCurrentQuantityInInventory();
int[] newAmounts = ScarceParentPriorityAllocator.priorityAllocation(amountToAllocate, parents).parentAllocationQuantity; int[] newAmounts = ScarceParentPriorityAllocator.priorityAllocation(amountToAllocate, parents).parentAllocationQuantity;
for (int i = 0; i < parents.size(); i++) { for (int i = 0; i < parents.size(); i++) {
tmp.put(parents.get(i), newAmounts[i]); tmp.put(parents.get(i), newAmounts[i]);
@@ -50,7 +58,11 @@ public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider,
} }
public int getCurrentQuantityInInventory() { public int getCurrentQuantityInInventory() {
throw new UnsupportedOperationException("oppa"); return bot.getCurrentQuantityInInventory(item);
}
public int cachedCurrentQuantity() {
return allocation.entrySet().stream().mapToInt(Map.Entry::getValue).sum();
} }
@Override @Override
@@ -63,7 +75,7 @@ public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider,
// how much of our priority would go to this child if it could provide us with quantity of the item we need // 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 // here's the thing honey, we *already have* some, so you're really asking what's the priority of getting quantity MORE
int curr = allocation.entrySet().stream().mapToInt(Map.Entry::getValue).sum(); int curr = cachedCurrentQuantity();
return priority().value(quantity + curr) - priority().value(curr); return priority().value(quantity + curr) - priority().value(curr);
} }
+2 -2
View File
@@ -34,9 +34,9 @@ public class CraftingTask extends QuantizedTaskNode implements ISingleParentQuan
final AquireItemTask parent; final AquireItemTask parent;
public CraftingTask(AquireItemTask parent) { public CraftingTask(AquireItemTask parent) {
super(DependencyType.SERIAL); super(parent.bot, DependencyType.SERIAL);
this.inputs = new AquireCraftingItems(this); // this adds the relationship this.inputs = new AquireCraftingItems(this); // this adds the relationship
this.step2 = GetToCraftingTableTask.INSTANCE; this.step2 = registry().getSingleton(GetToCraftingTableTask.class);
step2.addParent(this); step2.addParent(this);
this.step3 = new ActuallyCraftTask(this); this.step3 = new ActuallyCraftTask(this);
@@ -17,11 +17,16 @@
package tenor.game; package tenor.game;
import tenor.Bot;
import tenor.ISingularChildTaskRelationship; import tenor.ISingularChildTaskRelationship;
import tenor.SingularTaskLeaf; import tenor.SingularTaskLeaf;
public class GetToCraftingTableTask extends SingularTaskLeaf { public class GetToCraftingTableTask extends SingularTaskLeaf {
public static final GetToCraftingTableTask INSTANCE = new GetToCraftingTableTask(); // ? idk
public GetToCraftingTableTask(Bot bot) {
super(bot);
registry().registerSingleton(this);
}
@Override @Override
public double cost() { public double cost() {
+3
View File
@@ -23,9 +23,12 @@ import tenor.QuantizedTaskLeaf;
public class MineTask extends QuantizedTaskLeaf implements ISingleParentQuantizedPriorityAllocator { public class MineTask extends QuantizedTaskLeaf implements ISingleParentQuantizedPriorityAllocator {
// TODO shared claims of block locations in the world across all mine tasks across all bots
final AquireItemTask parent; final AquireItemTask parent;
public MineTask(AquireItemTask parent) { public MineTask(AquireItemTask parent) {
super(parent.bot);
this.parent = parent; this.parent = parent;
addParent(parent); addParent(parent);
} }