Search in sources :

Example 6 with IAgriMutation

use of com.infinityraider.agricraft.api.v1.mutation.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.getWorld(), crop.getPos(), 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).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)

Aggregations

IAgriMutation (com.infinityraider.agricraft.api.v1.mutation.IAgriMutation)5 IAgriPlant (com.infinityraider.agricraft.api.v1.plant.IAgriPlant)3 AgriMutation (com.agricraft.agricore.plant.AgriMutation)2 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 Optional (java.util.Optional)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 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 Random (java.util.Random)1 Collectors (java.util.stream.Collectors)1 Nonnull (javax.annotation.Nonnull)1