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

View File

@@ -20,6 +20,7 @@ package tenor;
import java.util.List;
public class AquireCraftingItems extends QuantizedTaskNode implements ClaimProvider {
CraftingTask output;
public AquireCraftingItems() {
@@ -34,17 +35,17 @@ public class AquireCraftingItems extends QuantizedTaskNode implements ClaimProvi
// 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?
// how good would that be?allocatedPriority
return priority().value(actualQuantity);
}
@Override
public QuantityRelationship priority() {
return parents().get(0)::allocatedPriority; // gamer style
public IQuantityRelationship priority() {
return ((List<IQuantizedChildTaskRelationship>) (Object) parentTasks()).get(0)::allocatedPriority; // gamer style
}
@Override
public QuantityRelationship cost() {
public IQuantityRelationship cost() {
return null;
}
@@ -52,10 +53,10 @@ public class AquireCraftingItems extends QuantizedTaskNode implements ClaimProvi
public int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship) {
// our only parent is the crafting task
int minCompletion = Integer.MAX_VALUE;
for (IQuantizedChildTaskRelationship resource : (List<IQuantizedChildTaskRelationship>) (Object) children()) {
for (IQuantizedChildTaskRelationship resource : (List<IQuantizedChildTaskRelationship>) (Object) childTasks()) {
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);

View File

@@ -18,9 +18,10 @@
package tenor;
import java.util.HashMap;
import java.util.List;
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
@@ -34,11 +35,13 @@ public class AquireItemTask extends QuantizedTaskNode implements ClaimProvider,
}
public void reallocate() {
List<IQuantizedChildTaskRelationship> parents = (List<IQuantizedChildTaskRelationship>) (Object) parentTasks();
allocation.clear();
int amountToAllocate = getCurrentQuantityInInventory();
int[] newAmounts = ScarceParentPriorityAllocator.priorityAllocation(amountToAllocate, parents());
for (int i = 0; i < parents().size(); i++) {
allocation.put(parents().get(i), newAmounts[i]);
int[] newAmounts = ScarceParentPriorityAllocator.priorityAllocation(amountToAllocate, parents);
for (int i = 0; i < parents.size(); i++) {
allocation.put(parents.get(i), newAmounts[i]);
}
}
@@ -47,7 +50,7 @@ public class AquireItemTask extends QuantizedTaskNode implements ClaimProvider,
}
@Override
public QuantityRelationship priority() {
public IQuantityRelationship priority() {
return x -> {
double sum = 0;
for (Map.Entry<IQuantizedChildTaskRelationship, Integer> entry : allocation.entrySet()) {
@@ -62,8 +65,8 @@ public class AquireItemTask extends QuantizedTaskNode implements ClaimProvider,
}
@Override
public QuantityRelationship cost() {
return QuantizedDependentCostCalculator.super.cost(); // oppa
public IQuantityRelationship cost() {
return IQuantizedDependentCostCalculator.super.cost(); // oppa
}
@Override

View File

@@ -18,5 +18,6 @@
package tenor;
public interface ClaimProvider {
int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship);
}

View File

@@ -22,29 +22,29 @@ import net.minecraft.util.Tuple;
import java.util.List;
public class CraftingTask extends QuantizedTaskNode {
int outputQuantity;
List<Tuple<AquireItemTask, Integer>> recipe;
AquireCraftingItems inputs;
public CraftingTask() {
super(DependencyType.SERIAL);
}
@Override
public QuantityRelationship cost() {
public IQuantityRelationship cost() {
return x -> {
int actualQuantity = (int) Math.ceil(x * 1.0D / outputQuantity);
return inputs.cost().value(actualQuantity);
};
}
public QuantityRelationship priority() {
if (parents().size() != 1) {
public IQuantityRelationship priority() {
if (parentTasks().size() != 1) {
throw new IllegalStateException();
}
// TODO this is a short circuit
return ((QuantizedTask) (parents().get(0).parentTask())).priority();
return ((IQuantizedTask) (parentTasks().get(0).parentTask())).priority();
}
@Override

View File

@@ -17,6 +17,7 @@
package tenor;
public interface QuantityRelationship {
public interface IQuantityRelationship {
double value(int quantity);
}

View File

@@ -17,10 +17,7 @@
package tenor;
public interface IQuantizedChildTaskRelationship extends ITaskRelationshipBase {
default QuantizedTask child() {
return (QuantizedTask) childTask();
}
public interface IQuantizedChildTaskRelationship<T extends ITaskNodeBase> extends ITaskRelationshipBase<T, IQuantizedTask> {
double allocatedPriority(int quantity);
@@ -29,6 +26,7 @@ public interface IQuantizedChildTaskRelationship extends ITaskRelationshipBase {
}
default int quantityCompleted() {
return ((ClaimProvider) child()).quantityCompletedForParent(this);
// TODO: Resolve this cast, should QuantizedTask implement ClaimProvider?
return ((ClaimProvider) childTask()).quantityCompletedForParent(this);
}
}

View File

@@ -17,14 +17,14 @@
package tenor;
public interface QuantizedDependentCostCalculator extends ITaskNodeBase {
default QuantityRelationship cost() {
public interface IQuantizedDependentCostCalculator extends ITaskNodeBase {
default IQuantityRelationship cost() {
switch (type()) {
case SERIAL:
case PARALLEL_ALL:
return q -> {
double sum = 0;
for (TaskRelationship relationship : childTasks()) {
for (ITaskRelationshipBase relationship : childTasks()) {
sum += ((IQuantizedParentTaskRelationship) relationship).cost().value(q);
}
return sum;
@@ -32,7 +32,7 @@ public interface QuantizedDependentCostCalculator extends ITaskNodeBase {
case ANY_ONE_OF: // TODO this could be smarter about allocating
return q -> {
double min = -1;
for (TaskRelationship relationship : childTasks()) {
for (ITaskRelationshipBase relationship : childTasks()) {
double cost = ((IQuantizedParentTaskRelationship) relationship).cost().value(q);
if (min == -1 || cost < min) {
min = cost;

View File

@@ -17,10 +17,7 @@
package tenor;
public interface IQuantizedParentTaskRelationship extends ITaskRelationshipBase {
default QuantizedTaskNode parent() {
return (QuantizedTaskNode) parentTask();
}
public interface IQuantizedParentTaskRelationship<T extends ITask> extends ITaskRelationshipBase<QuantizedTaskNode, T> {
QuantityRelationship cost();
IQuantityRelationship cost();
}

View File

@@ -17,13 +17,7 @@
package tenor;
import java.util.List;
public interface QuantizedTask extends ITask {
default List<IQuantizedChildTaskRelationship> parents() {
return (List<IQuantizedChildTaskRelationship>) (Object) parentTasks();
}
public interface IQuantizedTask extends ITask {
/*default QuantityRelationship priority() {
return q -> {
@@ -34,7 +28,8 @@ public interface QuantizedTask extends ITask {
return sum;
};
}*/
QuantityRelationship priority();
QuantityRelationship cost();
IQuantityRelationship priority();
IQuantityRelationship cost();
}

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;
/**
* @author Brady
* @since 10/30/2018
*/
public interface IQuantizedTaskNode extends ITaskNodeBase, IQuantizedTask {
}

View File

@@ -17,10 +17,7 @@
package tenor;
public interface ISingularChildTaskRelationship extends ITaskRelationshipBase {
default SingularTask child() {
return (SingularTask) childTask();
}
public interface ISingularChildTaskRelationship<T extends ITaskNodeBase> extends ITaskRelationshipBase<T, ISingularTask> {
double allocatedPriority();
}

View File

@@ -17,10 +17,7 @@
package tenor;
public interface ISingularParentTaskRelationship extends ITaskRelationshipBase {
default SingularTask parent() {
return (SingularTask) parentTask();
}
public interface ISingularParentTaskRelationship<T extends ITask> extends ITaskRelationshipBase<SingularTaskNode, T> {
double cost();
}

View File

@@ -17,12 +17,7 @@
package tenor;
import java.util.List;
public interface SingularTask extends ITask {
default List<ISingularChildTaskRelationship> parents() {
return (List<ISingularChildTaskRelationship>) (Object) parentTasks();
}
public interface ISingularTask extends ITask {
double priorityAllocatedToChild(ISingularParentTaskRelationship relationship);

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;
/**
* @author Brady
* @since 10/30/2018
*/
public interface ISingularTaskNode extends ITaskNodeBase, ISingularTask {
}

View File

@@ -20,5 +20,6 @@ package tenor;
import java.util.List;
public interface ITask {
List<TaskRelationship> parentTasks();
List<ITaskRelationshipBase> parentTasks();
}

View File

@@ -20,7 +20,8 @@ package tenor;
import java.util.List;
public interface ITaskNodeBase extends ITask {
List<TaskRelationship> childTasks();
List<ITaskRelationshipBase> childTasks();
DependencyType type();
}

View File

@@ -17,8 +17,9 @@
package tenor;
public interface ITaskRelationshipBase {
TaskNode parentTask();
public interface ITaskRelationshipBase<P extends ITaskNodeBase, C extends ITask> {
Task childTask();
P parentTask();
C childTask();
}

View File

@@ -17,14 +17,14 @@
package tenor;
public class MineTask extends TaskLeaf implements QuantizedTask {
public class MineTask extends TaskLeaf implements IQuantizedTask {
@Override
public QuantityRelationship priority() {
public IQuantityRelationship priority() {
return null;
}
@Override
public QuantityRelationship cost() {
public IQuantityRelationship cost() {
return null;
}
}

View File

@@ -17,16 +17,11 @@
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) {
super(type);
}
public List<IQuantizedParentTaskRelationship> children() {
return (List<IQuantizedParentTaskRelationship>) (Object) childTasks();
}
public abstract double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity);
}

View File

@@ -17,14 +17,17 @@
package tenor;
public class QuantizedToQuantizedTaskRelationship extends TaskRelationship implements IQuantizedChildTaskRelationship, IQuantizedParentTaskRelationship {
public class QuantizedToQuantizedTaskRelationship
extends TaskRelationship<QuantizedTaskNode, IQuantizedTask>
implements IQuantizedChildTaskRelationship<QuantizedTaskNode>, IQuantizedParentTaskRelationship<IQuantizedTask> {
@Override
public double allocatedPriority(int quantity) {
return parent().priorityAllocatedTo(this, quantity);
return parentTask().priorityAllocatedTo(this, quantity);
}
@Override
public QuantityRelationship cost() {
return child().cost();
public IQuantityRelationship cost() {
return childTask().cost();
}
}

View File

@@ -17,11 +17,13 @@
package tenor;
public class QuantizedToSingularTaskRelationship extends TaskRelationship implements ISingularChildTaskRelationship, IQuantizedParentTaskRelationship {
public class QuantizedToSingularTaskRelationship
extends TaskRelationship<QuantizedTaskNode, ISingularTask>
implements ISingularChildTaskRelationship<QuantizedTaskNode>, IQuantizedParentTaskRelationship<ISingularTask> {
@Override
public QuantityRelationship cost() {
return x -> child().cost();
public IQuantityRelationship cost() {
return x -> childTask().cost();
}
@Override

View File

@@ -17,5 +17,9 @@
package tenor;
public class SingularTaskNode {
public abstract class SingularTaskNode extends TaskNode implements ISingularTaskNode {
public SingularTaskNode(DependencyType type) {
super(type);
}
}

View File

@@ -17,16 +17,19 @@
package tenor;
public class SingularToQuantizedTaskRelationship extends TaskRelationship implements IQuantizedChildTaskRelationship, ISingularParentTaskRelationship {
public class SingularToQuantizedTaskRelationship
extends TaskRelationship<SingularTaskNode, IQuantizedTask>
implements IQuantizedChildTaskRelationship<SingularTaskNode>, ISingularParentTaskRelationship<IQuantizedTask> {
int quantityRequired;
@Override
public double allocatedPriority(int quantity) {
return quantity >= quantityRequired ? parent().priorityAllocatedToChild(this) : 0;
return quantity >= quantityRequired ? parentTask().priorityAllocatedToChild(this) : 0;
}
@Override
public double cost() {
return child().cost().value(quantityRequired);
return childTask().cost().value(quantityRequired);
}
}

View File

@@ -17,14 +17,17 @@
package tenor;
public class SingularToSingularTaskRelationship extends TaskRelationship implements ISingularChildTaskRelationship, ISingularParentTaskRelationship {
public class SingularToSingularTaskRelationship
extends TaskRelationship<SingularTaskNode, ISingularTask>
implements ISingularChildTaskRelationship<SingularTaskNode>, ISingularParentTaskRelationship<ISingularTask> {
@Override
public double allocatedPriority() {
return parent().priorityAllocatedToChild(this);
return parentTask().priorityAllocatedToChild(this);
}
@Override
public double cost() {
return child().cost();
return childTask().cost();
}
}

View File

@@ -20,9 +20,10 @@ package tenor;
import java.util.List;
public abstract class Task implements ITask {
List<TaskRelationship> parentRelationships;
public List<TaskRelationship> parentTasks() {
List<ITaskRelationshipBase> parentRelationships;
public List<ITaskRelationshipBase> parentTasks() {
return parentRelationships;
}
}

View File

@@ -20,14 +20,15 @@ package tenor;
import java.util.List;
public abstract class TaskNode extends Task implements ITaskNodeBase {
List<TaskRelationship> childRelationships;
List<ITaskRelationshipBase> childRelationships;
DependencyType type;
public TaskNode(DependencyType type) {
this.type = type;
}
public List<TaskRelationship> childTasks() {
public List<ITaskRelationshipBase> childTasks() {
return childRelationships;
}

View File

@@ -17,18 +17,19 @@
package tenor;
public class TaskRelationship implements ITaskRelationshipBase {
TaskNode parent;
Task child;
public class TaskRelationship<P extends ITaskNodeBase, C extends ITask> implements ITaskRelationshipBase<P, C> {
P parent;
C child;
DependencyType type;
@Override
public TaskNode parentTask() {
public P parentTask() {
return parent;
}
@Override
public Task childTask() {
public C childTask() {
return child;
}
}