Search in sources :

Example 6 with IAgriMutation

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);
    }
}
Also used : GuiComponent(com.infinityraider.agricraft.gui.component.GuiComponent) IAgriMutation(com.infinityraider.agricraft.api.v1.mutation.IAgriMutation) ItemStack(net.minecraft.item.ItemStack)

Example 7 with IAgriMutation

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));
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) IAgriCrossStrategy(com.infinityraider.agricraft.api.v1.mutation.IAgriCrossStrategy) AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) Random(java.util.Random) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) AgriCraftConfig(com.infinityraider.agricraft.reference.AgriCraftConfig) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) IAgriMutation(com.infinityraider.agricraft.api.v1.mutation.IAgriMutation) WorldHelper(com.infinityraider.infinitylib.utility.WorldHelper) Optional(java.util.Optional) IAgriStat(com.infinityraider.agricraft.api.v1.stat.IAgriStat) IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) IAgriStat(com.infinityraider.agricraft.api.v1.stat.IAgriStat) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) Objects(java.util.Objects) IAgriMutation(com.infinityraider.agricraft.api.v1.mutation.IAgriMutation) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed)

Example 8 with IAgriMutation

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);
    }
}
Also used : IAgriMutation(com.infinityraider.agricraft.api.v1.genetics.IAgriMutation) Optional(java.util.Optional) IAgriMutation(com.infinityraider.agricraft.api.v1.genetics.IAgriMutation)

Example 9 with IAgriMutation

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);
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) IAgriMutation(com.infinityraider.agricraft.api.v1.genetics.IAgriMutation) IAgriMutation(com.infinityraider.agricraft.api.v1.genetics.IAgriMutation)

Aggregations

IAgriMutation (com.infinityraider.agricraft.api.v1.mutation.IAgriMutation)5 IAgriPlant (com.infinityraider.agricraft.api.v1.plant.IAgriPlant)5 AgriMutation (com.agricraft.agricore.plant.AgriMutation)3 IAgriMutation (com.infinityraider.agricraft.api.v1.genetics.IAgriMutation)3 Optional (java.util.Optional)3 IAgriCrop (com.infinityraider.agricraft.api.v1.crop.IAgriCrop)2 AgriSeed (com.infinityraider.agricraft.api.v1.seed.AgriSeed)2 Mutation (com.infinityraider.agricraft.farming.mutation.Mutation)2 Nonnull (javax.annotation.Nonnull)2 AgriApi (com.infinityraider.agricraft.api.v1.AgriApi)1 IAgriCrossStrategy (com.infinityraider.agricraft.api.v1.mutation.IAgriCrossStrategy)1 IAgriStat (com.infinityraider.agricraft.api.v1.stat.IAgriStat)1 PlantStats (com.infinityraider.agricraft.farming.PlantStats)1 GuiComponent (com.infinityraider.agricraft.gui.component.GuiComponent)1 Mutation (com.infinityraider.agricraft.impl.v1.genetics.Mutation)1 AgriCraftConfig (com.infinityraider.agricraft.reference.AgriCraftConfig)1 WorldHelper (com.infinityraider.infinitylib.utility.WorldHelper)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Objects (java.util.Objects)1