Search in sources :

Example 26 with IAgriPlant

use of com.infinityraider.agricraft.api.v1.plant.IAgriPlant 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 27 with IAgriPlant

use of com.infinityraider.agricraft.api.v1.plant.IAgriPlant in project AgriCraft by AgriCraft.

the class CoreHandler method initPlants.

private static void initPlants() {
    // Announce Progress
    AgriCore.getLogger("agricraft").info("Registering Plants!");
    // See if plants are valid...
    final int raw = AgriCore.getPlants().getAllElements().size();
    AgriCore.getPlants().validate();
    final int count = AgriCore.getPlants().getAllElements().size();
    // Transfer
    AgriCore.getPlants().validate();
    AgriCore.getPlants().getAllElements().stream().filter(AgriPlant::isEnabled).map(JsonPlant::new).forEach(AgriApi.getPlantRegistry()::add);
    // Display Plants
    AgriCore.getLogger("agricraft").info("Registered Plants ({0}/{1}):", count, raw);
    for (IAgriPlant plant : AgriApi.getPlantRegistry().all()) {
        AgriCore.getLogger("agricraft").info(" - {0}", plant.getId());
    }
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant)

Example 28 with IAgriPlant

use of com.infinityraider.agricraft.api.v1.plant.IAgriPlant 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)

Example 29 with IAgriPlant

use of com.infinityraider.agricraft.api.v1.plant.IAgriPlant in project AgriCraft by AgriCraft.

the class Mutation method toString.

@Override
public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append(this.id).append(": ");
    for (IAgriPlant p : this.parents) {
        sb.append(p.getId()).append(" + ");
    }
    sb.replace(sb.length() - 3, sb.length(), " = ");
    sb.append(this.child.getId());
    return sb.toString();
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant)

Example 30 with IAgriPlant

use of com.infinityraider.agricraft.api.v1.plant.IAgriPlant in project AgriCraft by AgriCraft.

the class AgriClocheRecipe method getTime.

@Override
public int getTime(ItemStack seed, ItemStack soilStack) {
    IAgriPlant plant = this.getPlant(seed);
    IAgriSoil soil = this.getSoil(soilStack);
    Optional<IAgriStatsMap> statsOptional = this.getStats(seed);
    if (plant.isPlant() && soil.isSoil() && statsOptional.isPresent()) {
        IAgriStatsMap stats = statsOptional.get();
        IAgriGrowthRequirement req = plant.getGrowthRequirement(plant.getInitialGrowthStage());
        int strength = stats.getStrength();
        if (!req.getSoilHumidityResponse(soil.getHumidity(), strength).isFertile()) {
            return Integer.MAX_VALUE;
        }
        if (!req.getSoilAcidityResponse(soil.getAcidity(), strength).isFertile()) {
            return Integer.MAX_VALUE;
        }
        if (!req.getSoilNutrientsResponse(soil.getNutrients(), strength).isFertile()) {
            return Integer.MAX_VALUE;
        }
        double growthFactor = 1 - stats.getGrowth() * this.getGrowthStatFactor();
        double soilFactor = 2 - soil.getGrowthModifier();
        return Math.max((int) (this.getGrowthTicks() * growthFactor * soilFactor), 1);
    }
    return Integer.MAX_VALUE;
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) IAgriGrowthRequirement(com.infinityraider.agricraft.api.v1.requirement.IAgriGrowthRequirement) IAgriStatsMap(com.infinityraider.agricraft.api.v1.stat.IAgriStatsMap) IAgriSoil(com.infinityraider.agricraft.api.v1.requirement.IAgriSoil)

Aggregations

IAgriPlant (com.infinityraider.agricraft.api.v1.plant.IAgriPlant)32 ItemStack (net.minecraft.item.ItemStack)8 AgriSeed (com.infinityraider.agricraft.api.v1.seed.AgriSeed)7 Nonnull (javax.annotation.Nonnull)6 IAgriStat (com.infinityraider.agricraft.api.v1.stat.IAgriStat)5 PlantStats (com.infinityraider.agricraft.farming.PlantStats)5 IAgriCrop (com.infinityraider.agricraft.api.v1.crop.IAgriCrop)4 AgriApi (com.infinityraider.agricraft.api.v1.AgriApi)3 IAgriMutation (com.infinityraider.agricraft.api.v1.mutation.IAgriMutation)3 IAgriGrowthRequirement (com.infinityraider.agricraft.api.v1.requirement.IAgriGrowthRequirement)3 AgriMutation (com.agricraft.agricore.plant.AgriMutation)2 ImmutableList (com.google.common.collect.ImmutableList)2 IAgriJournalItem (com.infinityraider.agricraft.api.v1.content.items.IAgriJournalItem)2 IAgriGrowthStage (com.infinityraider.agricraft.api.v1.crop.IAgriGrowthStage)2 IAgriMutation (com.infinityraider.agricraft.api.v1.genetics.IAgriMutation)2 AgriPlantIngredient (com.infinityraider.agricraft.api.v1.plant.AgriPlantIngredient)2 IAgriWeed (com.infinityraider.agricraft.api.v1.plant.IAgriWeed)2 IAgriSoil (com.infinityraider.agricraft.api.v1.requirement.IAgriSoil)2 IAgriStatsMap (com.infinityraider.agricraft.api.v1.stat.IAgriStatsMap)2 BlockCropPlant (com.infinityraider.agricraft.content.core.BlockCropPlant)2