Generify pt 1

This commit is contained in:
Brady
2018-10-30 15:51:19 -05:00
parent c887b27df9
commit f2f806669c
27 changed files with 146 additions and 93 deletions
+7 -6
View File
@@ -20,6 +20,7 @@ package tenor;
import java.util.List; import java.util.List;
public class AquireCraftingItems extends QuantizedTaskNode implements ClaimProvider { public class AquireCraftingItems extends QuantizedTaskNode implements ClaimProvider {
CraftingTask output; CraftingTask output;
public AquireCraftingItems() { public AquireCraftingItems() {
@@ -34,17 +35,17 @@ public class AquireCraftingItems extends QuantizedTaskNode implements ClaimProvi
// they could provide us with quantity // they could provide us with quantity
int actualQuantity = (int) Math.ceil(quantity * 1.0D / amount); int actualQuantity = (int) Math.ceil(quantity * 1.0D / amount);
// so we could do the crafting recipe this many times // so we could do the crafting recipe this many times
// how good would that be? // how good would that be?allocatedPriority
return priority().value(actualQuantity); return priority().value(actualQuantity);
} }
@Override @Override
public QuantityRelationship priority() { public IQuantityRelationship priority() {
return parents().get(0)::allocatedPriority; // gamer style return ((List<IQuantizedChildTaskRelationship>) (Object) parentTasks()).get(0)::allocatedPriority; // gamer style
} }
@Override @Override
public QuantityRelationship cost() { public IQuantityRelationship cost() {
return null; return null;
} }
@@ -52,10 +53,10 @@ public class AquireCraftingItems extends QuantizedTaskNode implements ClaimProvi
public int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship) { public int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship) {
// our only parent is the crafting task // our only parent is the crafting task
int minCompletion = Integer.MAX_VALUE; int minCompletion = Integer.MAX_VALUE;
for (IQuantizedChildTaskRelationship resource : (List<IQuantizedChildTaskRelationship>) (Object) children()) { for (IQuantizedChildTaskRelationship resource : (List<IQuantizedChildTaskRelationship>) (Object) childTasks()) {
int amountForUs = resource.quantityCompleted(); int amountForUs = resource.quantityCompleted();
int amountPerCraft = output.inputSizeFor((AquireItemTask) resource.child()); int amountPerCraft = output.inputSizeFor((AquireItemTask) resource.childTask());
int actualQuantity = (int) Math.ceil(amountForUs * 1.0D / amountPerCraft); int actualQuantity = (int) Math.ceil(amountForUs * 1.0D / amountPerCraft);
+10 -7
View File
@@ -18,9 +18,10 @@
package tenor; package tenor;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public class AquireItemTask extends QuantizedTaskNode implements ClaimProvider, QuantizedDependentCostCalculator { public class AquireItemTask extends QuantizedTaskNode implements ClaimProvider, 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
@@ -34,11 +35,13 @@ public class AquireItemTask extends QuantizedTaskNode implements ClaimProvider,
} }
public void reallocate() { public void reallocate() {
List<IQuantizedChildTaskRelationship> parents = (List<IQuantizedChildTaskRelationship>) (Object) parentTasks();
allocation.clear(); allocation.clear();
int amountToAllocate = getCurrentQuantityInInventory(); int amountToAllocate = getCurrentQuantityInInventory();
int[] newAmounts = ScarceParentPriorityAllocator.priorityAllocation(amountToAllocate, parents()); int[] newAmounts = ScarceParentPriorityAllocator.priorityAllocation(amountToAllocate, parents);
for (int i = 0; i < parents().size(); i++) { for (int i = 0; i < parents.size(); i++) {
allocation.put(parents().get(i), newAmounts[i]); allocation.put(parents.get(i), newAmounts[i]);
} }
} }
@@ -47,7 +50,7 @@ public class AquireItemTask extends QuantizedTaskNode implements ClaimProvider,
} }
@Override @Override
public QuantityRelationship priority() { public IQuantityRelationship priority() {
return x -> { return x -> {
double sum = 0; double sum = 0;
for (Map.Entry<IQuantizedChildTaskRelationship, Integer> entry : allocation.entrySet()) { for (Map.Entry<IQuantizedChildTaskRelationship, Integer> entry : allocation.entrySet()) {
@@ -62,8 +65,8 @@ public class AquireItemTask extends QuantizedTaskNode implements ClaimProvider,
} }
@Override @Override
public QuantityRelationship cost() { public IQuantityRelationship cost() {
return QuantizedDependentCostCalculator.super.cost(); // oppa return IQuantizedDependentCostCalculator.super.cost(); // oppa
} }
@Override @Override
+1
View File
@@ -18,5 +18,6 @@
package tenor; package tenor;
public interface ClaimProvider { public interface ClaimProvider {
int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship); int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship);
} }
+5 -5
View File
@@ -22,29 +22,29 @@ import net.minecraft.util.Tuple;
import java.util.List; import java.util.List;
public class CraftingTask extends QuantizedTaskNode { public class CraftingTask extends QuantizedTaskNode {
int outputQuantity; int outputQuantity;
List<Tuple<AquireItemTask, Integer>> recipe; List<Tuple<AquireItemTask, Integer>> recipe;
AquireCraftingItems inputs; AquireCraftingItems inputs;
public CraftingTask() { public CraftingTask() {
super(DependencyType.SERIAL); super(DependencyType.SERIAL);
} }
@Override @Override
public QuantityRelationship cost() { public IQuantityRelationship cost() {
return x -> { return x -> {
int actualQuantity = (int) Math.ceil(x * 1.0D / outputQuantity); int actualQuantity = (int) Math.ceil(x * 1.0D / outputQuantity);
return inputs.cost().value(actualQuantity); return inputs.cost().value(actualQuantity);
}; };
} }
public QuantityRelationship priority() { public IQuantityRelationship priority() {
if (parents().size() != 1) { if (parentTasks().size() != 1) {
throw new IllegalStateException(); throw new IllegalStateException();
} }
// TODO this is a short circuit // TODO this is a short circuit
return ((QuantizedTask) (parents().get(0).parentTask())).priority(); return ((IQuantizedTask) (parentTasks().get(0).parentTask())).priority();
} }
@Override @Override
@@ -17,6 +17,7 @@
package tenor; package tenor;
public interface QuantityRelationship { public interface IQuantityRelationship {
double value(int quantity); double value(int quantity);
} }
@@ -17,10 +17,7 @@
package tenor; package tenor;
public interface IQuantizedChildTaskRelationship extends ITaskRelationshipBase { public interface IQuantizedChildTaskRelationship<T extends ITaskNodeBase> extends ITaskRelationshipBase<T, IQuantizedTask> {
default QuantizedTask child() {
return (QuantizedTask) childTask();
}
double allocatedPriority(int quantity); double allocatedPriority(int quantity);
@@ -29,6 +26,7 @@ public interface IQuantizedChildTaskRelationship extends ITaskRelationshipBase {
} }
default int quantityCompleted() { default int quantityCompleted() {
return ((ClaimProvider) child()).quantityCompletedForParent(this); // TODO: Resolve this cast, should QuantizedTask implement ClaimProvider?
return ((ClaimProvider) childTask()).quantityCompletedForParent(this);
} }
} }
@@ -17,14 +17,14 @@
package tenor; package tenor;
public interface QuantizedDependentCostCalculator extends ITaskNodeBase { public interface IQuantizedDependentCostCalculator extends ITaskNodeBase {
default QuantityRelationship cost() { default IQuantityRelationship cost() {
switch (type()) { switch (type()) {
case SERIAL: case SERIAL:
case PARALLEL_ALL: case PARALLEL_ALL:
return q -> { return q -> {
double sum = 0; double sum = 0;
for (TaskRelationship relationship : childTasks()) { for (ITaskRelationshipBase relationship : childTasks()) {
sum += ((IQuantizedParentTaskRelationship) relationship).cost().value(q); sum += ((IQuantizedParentTaskRelationship) relationship).cost().value(q);
} }
return sum; return sum;
@@ -32,7 +32,7 @@ public interface QuantizedDependentCostCalculator extends ITaskNodeBase {
case ANY_ONE_OF: // TODO this could be smarter about allocating case ANY_ONE_OF: // TODO this could be smarter about allocating
return q -> { return q -> {
double min = -1; double min = -1;
for (TaskRelationship relationship : childTasks()) { for (ITaskRelationshipBase relationship : childTasks()) {
double cost = ((IQuantizedParentTaskRelationship) relationship).cost().value(q); double cost = ((IQuantizedParentTaskRelationship) relationship).cost().value(q);
if (min == -1 || cost < min) { if (min == -1 || cost < min) {
min = cost; min = cost;
@@ -17,10 +17,7 @@
package tenor; package tenor;
public interface IQuantizedParentTaskRelationship extends ITaskRelationshipBase { public interface IQuantizedParentTaskRelationship<T extends ITask> extends ITaskRelationshipBase<QuantizedTaskNode, T> {
default QuantizedTaskNode parent() {
return (QuantizedTaskNode) parentTask();
}
QuantityRelationship cost(); IQuantityRelationship cost();
} }
@@ -17,13 +17,7 @@
package tenor; package tenor;
import java.util.List; public interface IQuantizedTask extends ITask {
public interface QuantizedTask extends ITask {
default List<IQuantizedChildTaskRelationship> parents() {
return (List<IQuantizedChildTaskRelationship>) (Object) parentTasks();
}
/*default QuantityRelationship priority() { /*default QuantityRelationship priority() {
return q -> { return q -> {
@@ -34,7 +28,8 @@ public interface QuantizedTask extends ITask {
return sum; return sum;
}; };
}*/ }*/
QuantityRelationship priority();
QuantityRelationship cost(); IQuantityRelationship priority();
IQuantityRelationship cost();
} }
@@ -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 IQuantizedTaskNode extends ITaskNodeBase, IQuantizedTask {
}
@@ -17,10 +17,7 @@
package tenor; package tenor;
public interface ISingularChildTaskRelationship extends ITaskRelationshipBase { public interface ISingularChildTaskRelationship<T extends ITaskNodeBase> extends ITaskRelationshipBase<T, ISingularTask> {
default SingularTask child() {
return (SingularTask) childTask();
}
double allocatedPriority(); double allocatedPriority();
} }
@@ -17,10 +17,7 @@
package tenor; package tenor;
public interface ISingularParentTaskRelationship extends ITaskRelationshipBase { public interface ISingularParentTaskRelationship<T extends ITask> extends ITaskRelationshipBase<SingularTaskNode, T> {
default SingularTask parent() {
return (SingularTask) parentTask();
}
double cost(); double cost();
} }
@@ -17,12 +17,7 @@
package tenor; package tenor;
import java.util.List; public interface ISingularTask extends ITask {
public interface SingularTask extends ITask {
default List<ISingularChildTaskRelationship> parents() {
return (List<ISingularChildTaskRelationship>) (Object) parentTasks();
}
double priorityAllocatedToChild(ISingularParentTaskRelationship relationship); double priorityAllocatedToChild(ISingularParentTaskRelationship relationship);
@@ -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, ISingularTask {
}
+2 -1
View File
@@ -20,5 +20,6 @@ package tenor;
import java.util.List; import java.util.List;
public interface ITask { public interface ITask {
List<TaskRelationship> parentTasks();
List<ITaskRelationshipBase> parentTasks();
} }
+2 -1
View File
@@ -20,7 +20,8 @@ package tenor;
import java.util.List; import java.util.List;
public interface ITaskNodeBase extends ITask { public interface ITaskNodeBase extends ITask {
List<TaskRelationship> childTasks();
List<ITaskRelationshipBase> childTasks();
DependencyType type(); DependencyType type();
} }
@@ -17,8 +17,9 @@
package tenor; package tenor;
public interface ITaskRelationshipBase { public interface ITaskRelationshipBase<P extends ITaskNodeBase, C extends ITask> {
TaskNode parentTask();
Task childTask(); P parentTask();
C childTask();
} }
+3 -3
View File
@@ -17,14 +17,14 @@
package tenor; package tenor;
public class MineTask extends TaskLeaf implements QuantizedTask { public class MineTask extends TaskLeaf implements IQuantizedTask {
@Override @Override
public QuantityRelationship priority() { public IQuantityRelationship priority() {
return null; return null;
} }
@Override @Override
public QuantityRelationship cost() { public IQuantityRelationship cost() {
return null; return null;
} }
} }
+1 -6
View File
@@ -17,16 +17,11 @@
package tenor; package tenor;
import java.util.List; public abstract class QuantizedTaskNode extends TaskNode implements IQuantizedTask {
public abstract class QuantizedTaskNode extends TaskNode implements QuantizedTask {
public QuantizedTaskNode(DependencyType type) { public QuantizedTaskNode(DependencyType type) {
super(type); super(type);
} }
public List<IQuantizedParentTaskRelationship> children() {
return (List<IQuantizedParentTaskRelationship>) (Object) childTasks();
}
public abstract double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity); public abstract double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity);
} }
@@ -17,14 +17,17 @@
package tenor; package tenor;
public class QuantizedToQuantizedTaskRelationship extends TaskRelationship implements IQuantizedChildTaskRelationship, IQuantizedParentTaskRelationship { public class QuantizedToQuantizedTaskRelationship
extends TaskRelationship<QuantizedTaskNode, IQuantizedTask>
implements IQuantizedChildTaskRelationship<QuantizedTaskNode>, IQuantizedParentTaskRelationship<IQuantizedTask> {
@Override @Override
public double allocatedPriority(int quantity) { public double allocatedPriority(int quantity) {
return parent().priorityAllocatedTo(this, quantity); return parentTask().priorityAllocatedTo(this, quantity);
} }
@Override @Override
public QuantityRelationship cost() { public IQuantityRelationship cost() {
return child().cost(); return childTask().cost();
} }
} }
@@ -17,11 +17,13 @@
package tenor; package tenor;
public class QuantizedToSingularTaskRelationship extends TaskRelationship implements ISingularChildTaskRelationship, IQuantizedParentTaskRelationship { public class QuantizedToSingularTaskRelationship
extends TaskRelationship<QuantizedTaskNode, ISingularTask>
implements ISingularChildTaskRelationship<QuantizedTaskNode>, IQuantizedParentTaskRelationship<ISingularTask> {
@Override @Override
public QuantityRelationship cost() { public IQuantityRelationship cost() {
return x -> child().cost(); return x -> childTask().cost();
} }
@Override @Override
+5 -1
View File
@@ -17,5 +17,9 @@
package tenor; package tenor;
public class SingularTaskNode { public abstract class SingularTaskNode extends TaskNode implements ISingularTaskNode {
public SingularTaskNode(DependencyType type) {
super(type);
}
} }
@@ -17,16 +17,19 @@
package tenor; package tenor;
public class SingularToQuantizedTaskRelationship extends TaskRelationship implements IQuantizedChildTaskRelationship, ISingularParentTaskRelationship { public class SingularToQuantizedTaskRelationship
extends TaskRelationship<SingularTaskNode, IQuantizedTask>
implements IQuantizedChildTaskRelationship<SingularTaskNode>, ISingularParentTaskRelationship<IQuantizedTask> {
int quantityRequired; int quantityRequired;
@Override @Override
public double allocatedPriority(int quantity) { public double allocatedPriority(int quantity) {
return quantity >= quantityRequired ? parent().priorityAllocatedToChild(this) : 0; return quantity >= quantityRequired ? parentTask().priorityAllocatedToChild(this) : 0;
} }
@Override @Override
public double cost() { public double cost() {
return child().cost().value(quantityRequired); return childTask().cost().value(quantityRequired);
} }
} }
@@ -17,14 +17,17 @@
package tenor; package tenor;
public class SingularToSingularTaskRelationship extends TaskRelationship implements ISingularChildTaskRelationship, ISingularParentTaskRelationship { public class SingularToSingularTaskRelationship
extends TaskRelationship<SingularTaskNode, ISingularTask>
implements ISingularChildTaskRelationship<SingularTaskNode>, ISingularParentTaskRelationship<ISingularTask> {
@Override @Override
public double allocatedPriority() { public double allocatedPriority() {
return parent().priorityAllocatedToChild(this); return parentTask().priorityAllocatedToChild(this);
} }
@Override @Override
public double cost() { public double cost() {
return child().cost(); return childTask().cost();
} }
} }
+3 -2
View File
@@ -20,9 +20,10 @@ package tenor;
import java.util.List; import java.util.List;
public abstract class Task implements ITask { public abstract class Task implements ITask {
List<TaskRelationship> parentRelationships;
public List<TaskRelationship> parentTasks() { List<ITaskRelationshipBase> parentRelationships;
public List<ITaskRelationshipBase> parentTasks() {
return parentRelationships; return parentRelationships;
} }
} }
+3 -2
View File
@@ -20,14 +20,15 @@ package tenor;
import java.util.List; import java.util.List;
public abstract class TaskNode extends Task implements ITaskNodeBase { public abstract class TaskNode extends Task implements ITaskNodeBase {
List<TaskRelationship> childRelationships;
List<ITaskRelationshipBase> childRelationships;
DependencyType type; DependencyType type;
public TaskNode(DependencyType type) { public TaskNode(DependencyType type) {
this.type = type; this.type = type;
} }
public List<TaskRelationship> childTasks() { public List<ITaskRelationshipBase> childTasks() {
return childRelationships; return childRelationships;
} }
+6 -5
View File
@@ -17,18 +17,19 @@
package tenor; package tenor;
public class TaskRelationship implements ITaskRelationshipBase { public class TaskRelationship<P extends ITaskNodeBase, C extends ITask> implements ITaskRelationshipBase<P, C> {
TaskNode parent;
Task child; P parent;
C child;
DependencyType type; DependencyType type;
@Override @Override
public TaskNode parentTask() { public P parentTask() {
return parent; return parent;
} }
@Override @Override
public Task childTask() { public C childTask() {
return child; return child;
} }
} }