use of com.infinityraider.agricraft.api.v1.genetics.IAgriMutation in project AgriCraft by AgriCraft.
the class JournalPageSeed method addSeeds.
private void addSeeds(List<GuiComponent> components) {
List<IAgriMutation> completedMutations = getCompletedMutations();
List<IAgriMutation> uncompletedMutations = getUncompleteMutations();
int y = 1;
int x = 132;
for (IAgriMutation mutation : completedMutations) {
// Increment Row.
y = y + MUTATION_ROW_HEIGHT;
// Child Component
final ItemStack resultStack = mutation.getChild().getSeed();
final GuiComponent child = BasicComponents.getStackComponent(resultStack, x + 69, y);
child.setMouseClickAction((c, p) -> journal.switchPage(mutation.getChild()));
components.add(child);
// Parent 1 Component
final ItemStack parent1Stack = mutation.getParents().get(0).getSeed();
final GuiComponent parent1 = BasicComponents.getStackComponent(parent1Stack, x, y);
parent1.setMouseClickAction((c, p) -> journal.switchPage(mutation.getParents().get(0)));
components.add(parent1);
// Parent 2 Component
final ItemStack parent2Stack = mutation.getParents().get(1).getSeed();
final GuiComponent parent2 = BasicComponents.getStackComponent(parent2Stack, x + 35, y);
parent2.setMouseClickAction((c, p) -> journal.switchPage(mutation.getParents().get(1)));
components.add(parent2);
}
for (IAgriMutation mutation : uncompletedMutations) {
// Increment Row.
y = y + MUTATION_ROW_HEIGHT;
// Parent 1 Component
final ItemStack parent1Stack = mutation.getParents().get(0).getSeed();
final GuiComponent parent1 = BasicComponents.getStackComponent(parent1Stack, x, y);
parent1.setMouseClickAction((c, p) -> journal.switchPage(mutation.getParents().get(0)));
components.add(parent1);
// Parent 2 Component
final ItemStack parent2Stack = mutation.getParents().get(1).getSeed();
final GuiComponent parent2 = BasicComponents.getStackComponent(parent2Stack, x + 35, y);
parent2.setMouseClickAction((c, p) -> journal.switchPage(mutation.getParents().get(1)));
components.add(parent2);
}
}
use of com.infinityraider.agricraft.api.v1.genetics.IAgriMutation in project AgriCraft by AgriCraft.
the class MutateStrategy method executeStrategy.
@Override
public Optional<AgriSeed> executeStrategy(IAgriCrop crop, Random rand) {
// Validate the parameters.
Objects.requireNonNull(crop, "You cannot execute a mutation on a null crop!");
Objects.requireNonNull(rand, "The random passed to a mutation strategy should not be null!");
// Fetch all neighboring crop instances.
final List<IAgriCrop> neighbors = WorldHelper.getTileNeighbors(crop.getCropWorld(), crop.getCropPos(), IAgriCrop.class);
// Determine all possible parents.
final List<IAgriPlant> parents = neighbors.stream().filter(IAgriCrop::isMature).map(IAgriCrop::getSeed).filter(Objects::nonNull).map(AgriSeed::getPlant).collect(Collectors.toList());
// If we have less than two parents, might as well as abort.
if (parents.size() < 2) {
return Optional.empty();
}
// Determine the list of possible cross-over mutations.
final List<IAgriMutation> mutations = AgriApi.getMutationRegistry().stream().filter(m -> m.areParentsIn(parents)).filter(m -> crop.isFertile(m.getChild())).collect(Collectors.toList());
// If we didn't find any valid mutations, might as well as abort.
if (mutations.isEmpty()) {
return Optional.empty();
}
// Choose a random index in the list.
final int index = rand.nextInt(mutations.size());
// Fetch the chosen mutation from the list.
final IAgriMutation mutation = mutations.get(index);
// Determine if we should actually go through with this.
if (mutation.getChance() <= rand.nextDouble()) {
return Optional.empty();
}
// Calculate the stat associated with the new plant.
Optional<IAgriStat> stat = AgriApi.getStatCalculatorRegistry().valueOf(mutation.getChild()).map(c -> c.calculateMutationStats(mutation, neighbors));
// Return the mutation result.
return stat.map(s -> new AgriSeed(mutation.getChild(), s));
}
use of com.infinityraider.agricraft.api.v1.genetics.IAgriMutation in project AgriCraft by AgriCraft.
the class CoreHandler method initMutations.
private static void initMutations() {
// Announce Progress
AgriCore.getLogger("agricraft").info("Registering Mutations!");
// See if mutations are valid...
final int raw = AgriCore.getMutations().getAll().size();
AgriCore.getMutations().validate();
final int count = AgriCore.getMutations().getAll().size();
// Transfer
AgriCore.getMutations().getAll().stream().filter(AgriMutation::isEnabled).map(JsonHelper::wrap).filter(Optional::isPresent).map(Optional::get).forEach(AgriApi.getMutationRegistry()::add);
// Display Mutations
AgriCore.getLogger("agricraft").info("Registered Mutations ({0}/{1}):", count, raw);
for (IAgriMutation mutation : AgriApi.getMutationRegistry().all()) {
AgriCore.getLogger("agricraft").info(" - {0}", mutation);
}
}
use of com.infinityraider.agricraft.api.v1.genetics.IAgriMutation in project AgriCraft by AgriCraft.
the class AgriMutationRegistry method add.
@Override
public boolean add(@Nonnull String id, double chance, @Nonnull String childId, @Nonnull List<String> parentIds) {
// Step I. Validate Parameters.
Objects.requireNonNull(id, "The id of a mutation may not be null!");
Objects.requireNonNull(childId, "The id of the child plant for a mutation may not be null!");
Objects.requireNonNull(parentIds, "The supplied list of parents for a mutation may not be null!");
// Step II. Validate Parents.
parentIds.forEach(parentId -> Objects.requireNonNull(parentId, "The id of a parent for a mutation may not be null!"));
// Step III. Map Child.
final IAgriPlant childPlant = AgriApi.getPlantRegistry().get(id).orElse(null);
// Step IV. Abort If Child Missing.
if (childPlant == null) {
// We tried, so don't throw error, just return false.
return false;
}
// Step V. Allocate Parent Plant List.
final List<IAgriPlant> parentPlants = new ArrayList<>(parentIds.size());
// Step VI. Map Parents, Aborting If Missing.
for (String parentId : parentIds) {
final IAgriPlant parentPlant = AgriApi.getPlantRegistry().get(parentId).orElse(null);
if (parentPlant != null) {
parentPlants.add(parentPlant);
} else {
return false;
}
}
// Step VII. Create the new mutation.
final IAgriMutation mutation = new Mutation(id, chance, childPlant, parentPlants);
// Step VIII. Register the new mutation.
return this.add(mutation);
}
Aggregations