Fix bee breeding overflowing with mutation.

This commit is contained in:
GTNH-Colen committed 2024-06-27 14:30:45 +01:00
1 parent 9570cb2982
commit da4f06a618
4 files changed
+41 -5

No files matched your search

@@ -67,6 +67,9 @@ import forestry.core.utils.Log;
import forestry.core.utils.StringUtil;
import forestry.core.utils.vect.Vect;
import static forestry.core.utils.MathUtil.safeAdd;
import static forestry.core.utils.MathUtil.safeMultiply;
public class Bee extends IndividualLiving implements IBee {
private int generation;
@@ -699,9 +702,9 @@ public class Bee extends IndividualLiving implements IBee {
// boost chance for researched mutations
if (breedingTracker.isResearched(beeMutation)) {
float mutationBoost = chance * (Config.researchMutationBoostMultiplier - 1.0f);
float mutationBoost = safeMultiply(chance, Config.researchMutationBoostMultiplier - 1.0f);
mutationBoost = Math.min(Config.maxResearchMutationBoostPercent, mutationBoost);
chance += mutationBoost;
chance = safeAdd(chance, mutationBoost);
}
if (chance > world.rand.nextFloat() * 100) {
@@ -21,6 +21,8 @@ import forestry.api.apiculture.IBeeRoot;
import forestry.api.genetics.IAllele;
import forestry.core.genetics.mutations.Mutation;
import static forestry.core.utils.MathUtil.safeMultiply;
public class BeeMutation extends Mutation implements IBeeMutationCustom {
public BeeMutation(IAlleleBeeSpecies bee0, IAlleleBeeSpecies bee1, IAllele[] result, int chance) {
@@ -49,8 +51,8 @@ public class BeeMutation extends Mutation implements IBeeMutationCustom {
IBeeModifier beeHousingModifier = BeeManager.beeRoot.createBeeHousingModifier(housing);
IBeeModifier beeModeModifier = BeeManager.beeRoot.getBeekeepingMode(world).getBeeModifier();
processedChance *= beeHousingModifier.getMutationModifier(genome0, genome1, processedChance);
processedChance *= beeModeModifier.getMutationModifier(genome0, genome1, processedChance);
processedChance = safeMultiply(processedChance, beeHousingModifier.getMutationModifier(genome0, genome1, processedChance));
processedChance = safeMultiply(processedChance, beeModeModifier.getMutationModifier(genome0, genome1, processedChance));
return processedChance;
}
@@ -25,6 +25,8 @@ import forestry.api.genetics.IGenome;
import forestry.api.genetics.IMutationCondition;
import forestry.api.genetics.IMutationCustom;
import static forestry.core.utils.MathUtil.safeMultiply;
public abstract class Mutation implements IMutationCustom {
private final int chance;
@@ -126,7 +128,7 @@ public abstract class Mutation implements IMutationCustom {
IGenome genome1, IClimateProvider climate) {
float mutationChance = chance;
for (IMutationCondition mutationCondition : mutationConditions) {
mutationChance *= mutationCondition.getChance(world, x, y, z, allele0, allele1, genome0, genome1, climate);
mutationChance = safeMultiply(mutationChance, mutationCondition.getChance(world, x, y, z, allele0, allele1, genome0, genome1, climate));
if (mutationChance == 0) {
return 0;
}
@@ -0,0 +1,29 @@
package forestry.core.utils;
public class MathUtil {
public static float safeMultiply(float a, float b) {
if (a > 0 && b > 0 && a > Float.MAX_VALUE / b) {
return Float.MAX_VALUE;
} else if (a > 0 && b < 0 && a > Float.MIN_VALUE / b) {
return Float.MIN_VALUE;
} else if (a < 0 && b > 0 && a < Float.MIN_VALUE / b) {
return Float.MIN_VALUE;
} else if (a < 0 && b < 0 && a < Float.MAX_VALUE / b) {
return Float.MAX_VALUE;
} else {
return a * b;
}
}
public static float safeAdd(float a, float b) {
if (a > 0 && b > 0 && a > Float.MAX_VALUE - b) {
return Float.MAX_VALUE;
} else if (a < 0 && b < 0 && a < Float.MIN_VALUE - b) {
return Float.MIN_VALUE;
} else {
return a + b;
}
}
}