Search in sources :

Example 1 with IAgriMutation

use of com.infinityraider.agricraft.api.v1.genetics.IAgriMutation in project AgriCraft by AgriCraft.

the class JsonHelper method wrap.

@Nonnull
public static Optional<IAgriMutation> wrap(@Nullable AgriMutation mutation) {
    // Step I. Abort If Null Mutation.
    if (mutation == null) {
        return Optional.empty();
    }
    // Step II. Determine Chance.
    final double chance = mutation.getChance();
    // Step III. Determine ID.
    final String mutationId = mutation.getChild().getId().replace("_plant", "_mutation");
    // Step IV. Determine Child.
    final Optional<IAgriPlant> child = AgriApi.getPlantRegistry().get(mutation.getChild().getId());
    // Step V. Abort If Child Missing.
    if (!child.isPresent()) {
        return Optional.empty();
    }
    // Step VI. Determine Parents.
    final Optional<IAgriPlant> parentOne = AgriApi.getPlantRegistry().get(mutation.getParent1().getId());
    final Optional<IAgriPlant> parentTwo = AgriApi.getPlantRegistry().get(mutation.getParent2().getId());
    // Step VII. Abort If Missing Parent.
    if ((!parentOne.isPresent()) && (!parentTwo.isPresent())) {
        return Optional.empty();
    }
    // Step VIII. Create New Mutation
    return Optional.of(new Mutation(mutationId, chance, child.get(), parentOne.get(), parentTwo.get()));
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) AgriMutation(com.agricraft.agricore.plant.AgriMutation) IAgriMutation(com.infinityraider.agricraft.api.v1.mutation.IAgriMutation) Mutation(com.infinityraider.agricraft.farming.mutation.Mutation) Nonnull(javax.annotation.Nonnull)

Example 2 with IAgriMutation

use of com.infinityraider.agricraft.api.v1.genetics.IAgriMutation in project AgriCraft by AgriCraft.

the class StatCalculatorBase method calculateMutationStats.

@Override
public IAgriStat calculateMutationStats(IAgriMutation mutation, Collection<IAgriCrop> parents) {
    // Validate parameters.
    Objects.requireNonNull(mutation, "The mutation to calculate the stats for must not be null!");
    Objects.requireNonNull(parents, "The set of parents to calculate the mutation result stats from must not be null!");
    // Variables
    int invalidParents = 0;
    int validParents = 0;
    int growth = 0;
    int gain = 0;
    int strength = 0;
    // Sum values
    for (IAgriCrop parent : parents) {
        // Skip parent if null.
        if (parent == null) {
            continue;
        }
        // Fetch the seed associated with the parent.
        final AgriSeed parentSeed = parent.getSeed();
        // Skip if parent seed is null.
        if (parentSeed == null) {
            continue;
        }
        // If the parent is not mature, counts as invalid parent.
        if (!parent.isMature()) {
            invalidParents++;
            continue;
        }
        // If the parent plant does not match the child plant, invalid parent.
        if (!mutation.hasParent(parentSeed.getPlant())) {
            invalidParents++;
            continue;
        }
        // Otherwise everything is aok.
        validParents++;
        growth += parentSeed.getStat().getGrowth();
        gain += parentSeed.getStat().getGain();
        strength += parentSeed.getStat().getStrength();
    }
    // Determine the stat divisor.
    final int meanDivisor = calculateStatMeanDivisor(validParents, invalidParents);
    // Perform averages.
    growth = growth / meanDivisor;
    gain = gain / meanDivisor;
    strength = strength / meanDivisor;
    // Return the new plant stat.
    return new PlantStats(calculateStat(growth, validParents, AgriCraftConfig.cropStatDivisor), calculateStat(gain, validParents, AgriCraftConfig.cropStatDivisor), calculateStat(strength, validParents, AgriCraftConfig.cropStatDivisor));
}
Also used : IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) PlantStats(com.infinityraider.agricraft.farming.PlantStats)

Example 3 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 muation.
    return this.add(mutation);
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) ArrayList(java.util.ArrayList) IAgriMutation(com.infinityraider.agricraft.api.v1.mutation.IAgriMutation) IAgriMutation(com.infinityraider.agricraft.api.v1.mutation.IAgriMutation) Mutation(com.infinityraider.agricraft.farming.mutation.Mutation)

Example 4 with IAgriMutation

use of com.infinityraider.agricraft.api.v1.genetics.IAgriMutation in project AgriCraft by AgriCraft.

the class JsonHelper method wrap.

@Nonnull
public static Optional<IAgriMutation> wrap(@Nullable AgriMutation mutation) {
    // Step I. Abort If Null Mutation.
    if (mutation == null) {
        return Optional.empty();
    }
    // Step II. Determine Chance.
    final double chance = mutation.getChance();
    // Step III. Determine ID.
    final String mutationId = mutation.getChild().getId().replace("_plant", "_mutation");
    // Step IV. Determine Child.
    final Optional<IAgriPlant> child = AgriApi.getPlantRegistry().get(mutation.getChild().getId());
    // Step V. Abort If Child Missing.
    if (!child.isPresent()) {
        return Optional.empty();
    }
    // Step VI. Determine Parents.
    final Optional<IAgriPlant> parentOne = AgriApi.getPlantRegistry().get(mutation.getParent1().getId());
    final Optional<IAgriPlant> parentTwo = AgriApi.getPlantRegistry().get(mutation.getParent2().getId());
    // Step VII. Abort If Missing Parent.
    if ((!parentOne.isPresent()) && (!parentTwo.isPresent())) {
        return Optional.empty();
    }
    // Step VIII. Create New Mutation
    return Optional.of(new Mutation(mutationId, chance, child.get(), parentOne.get(), parentTwo.get()));
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) Mutation(com.infinityraider.agricraft.impl.v1.genetics.Mutation) AgriMutation(com.agricraft.agricore.plant.AgriMutation) IAgriMutation(com.infinityraider.agricraft.api.v1.genetics.IAgriMutation) Nonnull(javax.annotation.Nonnull)

Example 5 with IAgriMutation

use of com.infinityraider.agricraft.api.v1.genetics.IAgriMutation in project AgriCraft by AgriCraft.

the class CoreHandler method initMutations.

public 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 : AgriMutation(com.agricraft.agricore.plant.AgriMutation) IAgriMutation(com.infinityraider.agricraft.api.v1.mutation.IAgriMutation) Optional(java.util.Optional) IAgriMutation(com.infinityraider.agricraft.api.v1.mutation.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