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));
}
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());
}
}
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);
}
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();
}
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;
}
Aggregations