This commit is contained in:
Leijurv
2018-10-30 15:05:17 -07:00
parent f2f806669c
commit ef6b36b2cc
19 changed files with 218 additions and 25 deletions

View File

@@ -17,5 +17,22 @@
package tenor;
public class ActuallyCraftTask {
public class ActuallyCraftTask extends TaskLeaf implements ISingularTask {
public final CraftingTask parent;
public ActuallyCraftTask(CraftingTask parent) {
this.parent = parent;
addParent(parent);
}
@Override
public double cost() {
return 420;
}
@Override
public double priority() {
return 0;
}
}

View File

@@ -21,16 +21,22 @@ import java.util.List;
public class AquireCraftingItems extends QuantizedTaskNode implements ClaimProvider {
CraftingTask output;
final CraftingTask parent;
final QuantizedToQuantizedTaskRelationship parentRelationship;
public AquireCraftingItems() {
public AquireCraftingItems(CraftingTask parent) {
super(DependencyType.PARALLEL_ALL);
this.parent = parent;
this.parentRelationship = createRelationshipToParent(parent);
addParent(parentRelationship);
}
@Override
public double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity) {
AquireItemTask resource = (AquireItemTask) child.childTask(); // all our dependents are aquire item tasks
int amount = output.inputSizeFor(resource);
int amount = parent.inputSizeFor(resource);
// they could provide us with quantity
int actualQuantity = (int) Math.ceil(quantity * 1.0D / amount);
@@ -41,22 +47,42 @@ public class AquireCraftingItems extends QuantizedTaskNode implements ClaimProvi
@Override
public IQuantityRelationship priority() {
return ((List<IQuantizedChildTaskRelationship>) (Object) parentTasks()).get(0)::allocatedPriority; // gamer style
return parentRelationship::allocatedPriority; // gamer style
}
@Override
public IQuantityRelationship cost() {
return null;
return x -> {
// cost to get x copies of these items
double sum = 0;
for (QuantizedToQuantizedTaskRelationship resource : (List<QuantizedToQuantizedTaskRelationship>) (Object) childTasks()) {
int amountPerCraft = parent.inputSizeFor((AquireItemTask) resource.childTask());
int totalAmountNeeded = x * amountPerCraft;
int amountForUs = resource.quantityCompleted();
totalAmountNeeded -= amountForUs;
if (totalAmountNeeded <= 0) {
continue;
}
sum += resource.cost().value(totalAmountNeeded);
}
return sum;
};
}
@Override
public int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship) {
if (relationship != parentRelationship) {
throw new IllegalStateException();
}
// our only parent is the crafting task
int minCompletion = Integer.MAX_VALUE;
for (IQuantizedChildTaskRelationship resource : (List<IQuantizedChildTaskRelationship>) (Object) childTasks()) {
int amountForUs = resource.quantityCompleted();
int amountPerCraft = output.inputSizeFor((AquireItemTask) resource.childTask());
int amountPerCraft = parent.inputSizeFor((AquireItemTask) resource.childTask());
int actualQuantity = (int) Math.ceil(amountForUs * 1.0D / amountPerCraft);

View File

@@ -25,10 +25,25 @@ public class CraftingTask extends QuantizedTaskNode {
int outputQuantity;
List<Tuple<AquireItemTask, Integer>> recipe;
AquireCraftingItems inputs;
public CraftingTask() {
final AquireCraftingItems inputs;
final GetToCraftingTableTask step2;
final ActuallyCraftTask step3;
final AquireItemTask parent;
final QuantizedToQuantizedTaskRelationship parentRelationship;
public CraftingTask(AquireItemTask parent) {
super(DependencyType.SERIAL);
this.inputs = new AquireCraftingItems(this); // this adds the relationship
this.step2 = GetToCraftingTableTask.INSTANCE;
step2.addParent(this);
this.step3 = new ActuallyCraftTask(this);
this.parent = parent;
this.parentRelationship = createRelationshipToParent(parent);
addParent(parentRelationship);
}
@Override
@@ -40,11 +55,7 @@ public class CraftingTask extends QuantizedTaskNode {
}
public IQuantityRelationship priority() {
if (parentTasks().size() != 1) {
throw new IllegalStateException();
}
// TODO this is a short circuit
return ((IQuantizedTask) (parentTasks().get(0).parentTask())).priority();
return parentRelationship::allocatedPriority;
}
@Override

View File

@@ -0,0 +1,35 @@
/*
* 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 GetToCraftingTableTask extends TaskLeaf implements ISingularTask {
public static final GetToCraftingTableTask INSTANCE = new GetToCraftingTableTask(); // ? idk
@Override
public double cost() {
return 69;
}
@Override
public double priority() {
List<ISingularChildTaskRelationship> parentTasks = (List) parentTasks(); // pog
return parentTasks.stream().mapToDouble(ISingularChildTaskRelationship::allocatedPriority).sum();
}
}

View File

@@ -32,4 +32,16 @@ public interface IQuantizedTask extends ITask {
IQuantityRelationship priority();
IQuantityRelationship cost();
default IQuantizedChildTaskRelationship<? extends ITaskNodeBase> createRelationshipToParent(ITaskNodeBase parent) {
if (parent instanceof IQuantizedTask) {
return new QuantizedToQuantizedTaskRelationship((QuantizedTaskNode) parent, this, parent.type());
} else {
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());
}
}

View File

@@ -18,8 +18,15 @@
package tenor;
public interface ISingularTask extends ITask {
double priorityAllocatedToChild(ISingularParentTaskRelationship relationship);
double cost();
double priority();
default ISingularChildTaskRelationship<? extends ITaskNodeBase> createRelationshipToParent(ITaskNodeBase parent) {
if (parent instanceof IQuantizedTask) {
return new QuantizedToSingularTaskRelationship((QuantizedTaskNode) parent, this, parent.type());
} else {
return new SingularToSingularTaskRelationship((SingularTaskNode) parent, this, parent.type());
}
}
}

View File

@@ -22,5 +22,5 @@ package tenor;
* @since 10/30/2018
*/
public interface ISingularTaskNode extends ITaskNodeBase, ISingularTask {
double priorityAllocatedToChild(ISingularParentTaskRelationship relationship);
}

View File

@@ -22,4 +22,12 @@ import java.util.List;
public interface ITask {
List<ITaskRelationshipBase> parentTasks();
ITaskRelationshipBase createRelationshipToParent(ITaskNodeBase parent);
void addParent(ITaskRelationshipBase relationship);
default void addParent(ITaskNodeBase parent) {
addParent(createRelationshipToParent(parent));
}
}

View File

@@ -24,4 +24,6 @@ public interface ITaskNodeBase extends ITask {
List<ITaskRelationshipBase> childTasks();
DependencyType type();
void addChild(ITaskRelationshipBase relationship);
}

View File

@@ -22,4 +22,6 @@ public interface ITaskRelationshipBase<P extends ITaskNodeBase, C extends ITask>
P parentTask();
C childTask();
DependencyType type();
}

View File

@@ -23,5 +23,27 @@ public abstract class QuantizedTaskNode extends TaskNode implements IQuantizedTa
super(type);
}
// if the child task were able to provide this amount, how much priority would that be?
public abstract double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity);
public 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++) {
QuantizedToQuantizedTaskRelationship relationship = (QuantizedToQuantizedTaskRelationship) childTasks().get(i);
ClaimProvider claim = (ClaimProvider) relationship.childTask();
int amt = claim.quantityCompletedForParent(relationship);
if (minQuantity == -1 || amt < minQuantity) {
minQuantity = amt;
}
}
return minQuantity;
}
}

View File

@@ -21,6 +21,10 @@ public class QuantizedToQuantizedTaskRelationship
extends TaskRelationship<QuantizedTaskNode, IQuantizedTask>
implements IQuantizedChildTaskRelationship<QuantizedTaskNode>, IQuantizedParentTaskRelationship<IQuantizedTask> {
public QuantizedToQuantizedTaskRelationship(QuantizedTaskNode parent, IQuantizedTask child, DependencyType type) {
super(parent, child, type);
}
@Override
public double allocatedPriority(int quantity) {
return parentTask().priorityAllocatedTo(this, quantity);

View File

@@ -21,6 +21,10 @@ public class QuantizedToSingularTaskRelationship
extends TaskRelationship<QuantizedTaskNode, ISingularTask>
implements ISingularChildTaskRelationship<QuantizedTaskNode>, IQuantizedParentTaskRelationship<ISingularTask> {
public QuantizedToSingularTaskRelationship(QuantizedTaskNode parent, ISingularTask child, DependencyType type) {
super(parent, child, type);
}
@Override
public IQuantityRelationship cost() {
return x -> childTask().cost();
@@ -28,6 +32,6 @@ public class QuantizedToSingularTaskRelationship
@Override
public double allocatedPriority() {
throw new UnsupportedOperationException("Cannot allocate priority from quantized parent to singular child");
return parentTask().priorityAllocatedTo(this, parentTask().quantityExecutingInto(this));
}
}

View File

@@ -21,7 +21,12 @@ public class SingularToQuantizedTaskRelationship
extends TaskRelationship<SingularTaskNode, IQuantizedTask>
implements IQuantizedChildTaskRelationship<SingularTaskNode>, ISingularParentTaskRelationship<IQuantizedTask> {
int quantityRequired;
public int quantityRequired;
public SingularToQuantizedTaskRelationship(SingularTaskNode parent, IQuantizedTask child, DependencyType type, int quantityRequired) {
super(parent, child, type);
this.quantityRequired = quantityRequired;
}
@Override
public double allocatedPriority(int quantity) {

View File

@@ -21,6 +21,10 @@ public class SingularToSingularTaskRelationship
extends TaskRelationship<SingularTaskNode, ISingularTask>
implements ISingularChildTaskRelationship<SingularTaskNode>, ISingularParentTaskRelationship<ISingularTask> {
public SingularToSingularTaskRelationship(SingularTaskNode parent, ISingularTask child, DependencyType type) {
super(parent, child, type);
}
@Override
public double allocatedPriority() {
return parentTask().priorityAllocatedToChild(this);

View File

@@ -23,7 +23,17 @@ public abstract class Task implements ITask {
List<ITaskRelationshipBase> parentRelationships;
@Override
public List<ITaskRelationshipBase> parentTasks() {
return parentRelationships;
}
@Override
public void addParent(ITaskRelationshipBase relationship) {
if (relationship.childTask() != this) {
throw new IllegalArgumentException();
}
relationship.parentTask().addChild(relationship);
parentRelationships.add(relationship);
}
}

View File

@@ -17,5 +17,5 @@
package tenor;
public class TaskLeaf extends Task {
public abstract class TaskLeaf extends Task {
}

View File

@@ -22,7 +22,7 @@ import java.util.List;
public abstract class TaskNode extends Task implements ITaskNodeBase {
List<ITaskRelationshipBase> childRelationships;
DependencyType type;
public final DependencyType type;
public TaskNode(DependencyType type) {
this.type = type;
@@ -35,4 +35,14 @@ public abstract class TaskNode extends Task implements ITaskNodeBase {
public DependencyType type() {
return type;
}
public void addChild(ITaskRelationshipBase relationship) {
if (relationship.parentTask() != this) {
throw new IllegalArgumentException();
}
if (relationship.type() != type) {
throw new IllegalArgumentException();
}
childRelationships.add(relationship);
}
}

View File

@@ -19,9 +19,18 @@ package tenor;
public class TaskRelationship<P extends ITaskNodeBase, C extends ITask> implements ITaskRelationshipBase<P, C> {
P parent;
C child;
DependencyType type;
public final P parent;
public final C child;
public final DependencyType type;
public TaskRelationship(P parent, C child, DependencyType type) {
this.parent = parent;
this.child = child;
this.type = type;
if (parent.type() != type) {
throw new IllegalArgumentException();
}
}
@Override
public P parentTask() {
@@ -32,4 +41,9 @@ public class TaskRelationship<P extends ITaskNodeBase, C extends ITask> implemen
public C childTask() {
return child;
}
@Override
public DependencyType type() {
return type;
}
}