Refactor Probability API a bit

This commit is contained in:
nossr50
2022-12-18 16:02:43 -08:00
parent 7f66d27141
commit 59f711834b
39 changed files with 435 additions and 399 deletions

View File

@@ -7,7 +7,7 @@ import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.skills.repair.Repair;
import com.gmail.nossr50.skills.salvage.Salvage;
import com.gmail.nossr50.util.skills.SkillUtils;
import com.gmail.nossr50.util.random.ProbabilityUtil;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
@@ -67,7 +67,7 @@ public final class BlockUtils {
*/
public static boolean checkDoubleDrops(Player player, BlockState blockState, PrimarySkillType skillType, SubSkillType subSkillType) {
if (mcMMO.p.getGeneralConfig().getDoubleDropsEnabled(skillType, blockState.getType()) && Permissions.isSubSkillEnabled(player, subSkillType)) {
return SkillUtils.isSkillRNGSuccessful(subSkillType, player);
return ProbabilityUtil.isSkillRNGSuccessful(subSkillType, player);
}
return false;

View File

@@ -1,12 +1,9 @@
package com.gmail.nossr50.util.random;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.player.UserManager;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
import java.util.concurrent.ThreadLocalRandom;
public interface Probability {
/**
@@ -20,52 +17,38 @@ public interface Probability {
*/
double getValue();
static @NotNull Probability ofSubSkill(@Nullable Player player,
@NotNull SubSkillType subSkillType,
@NotNull SkillProbabilityType skillProbabilityType) {
switch (skillProbabilityType) {
case DYNAMIC_CONFIGURABLE:
double probabilityCeiling;
double xCeiling;
double xPos;
if (player != null) {
McMMOPlayer mmoPlayer = UserManager.getPlayer(player);
if(mmoPlayer != null)
xPos = mmoPlayer.getSkillLevel(subSkillType.getParentSkill());
else
xPos = 0;
} else {
xPos = 0;
}
//Probability ceiling is configurable in this type
probabilityCeiling = mcMMO.p.getAdvancedConfig().getMaximumProbability(subSkillType);
//The xCeiling is configurable in this type
xCeiling = mcMMO.p.getAdvancedConfig().getMaxBonusLevel(subSkillType);
return new ProbabilityImpl(xPos, xCeiling, probabilityCeiling);
case STATIC_CONFIGURABLE:
try {
return ofPercentageValue(getStaticRandomChance(subSkillType));
} catch (InvalidStaticChance invalidStaticChance) {
invalidStaticChance.printStackTrace();
}
default:
throw new RuntimeException("No case in switch statement for Skill Probability Type!");
}
static @NotNull Probability ofPercent(double percentageValue) {
return new ProbabilityImpl(percentageValue);
}
static @NotNull Probability ofPercentageValue(double percentageValue) {
return new ProbabilityImpl(percentageValue / 100);
/**
* Simulates a "roll of the dice"
* If the value passed is higher than the "random" value, than it is a successful roll
*
* @param probabilityValue probability value
* @return true for succeeding, false for failing
*/
static private boolean isSuccessfulRoll(double probabilityValue) {
return probabilityValue >= ThreadLocalRandom.current().nextDouble(100D);
}
static double getStaticRandomChance(@NotNull SubSkillType subSkillType) throws InvalidStaticChance {
return switch (subSkillType) {
case AXES_ARMOR_IMPACT -> mcMMO.p.getAdvancedConfig().getImpactChance();
case AXES_GREATER_IMPACT -> mcMMO.p.getAdvancedConfig().getGreaterImpactChance();
case TAMING_FAST_FOOD_SERVICE -> mcMMO.p.getAdvancedConfig().getFastFoodChance();
default -> throw new InvalidStaticChance();
};
/**
* Simulate an outcome on a probability and return true or false for the result of that outcome
*
* @return true if the probability succeeded, false if it failed
*/
default boolean evaluate() {
return isSuccessfulRoll(getValue());
}
/**
* Modify and then Simulate an outcome on a probability and return true or false for the result of that outcome
*
* @param probabilityMultiplier probability will be multiplied by this before success is checked
* @return true if the probability succeeded, false if it failed
*/
default boolean evaluate(double probabilityMultiplier) {
double probabilityValue = getValue() * probabilityMultiplier;
return isSuccessfulRoll(probabilityValue);
}
}

View File

@@ -20,7 +20,7 @@ public class ProbabilityImpl implements Probability {
probabilityValue = staticProbability;
}
public ProbabilityImpl(double xPos, double xCeiling, double probabilityCeiling) throws ValueOutOfBoundsException {
ProbabilityImpl(double xPos, double xCeiling, double probabilityCeiling) throws ValueOutOfBoundsException {
if(probabilityCeiling > 100) {
throw new ValueOutOfBoundsException("Probability Ceiling should never be above 100!");
} else if (probabilityCeiling < 0) {

View File

@@ -0,0 +1,226 @@
package com.gmail.nossr50.util.random;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.events.skills.secondaryabilities.SubSkillEvent;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.EventUtils;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.text.DecimalFormat;
public class ProbabilityUtil {
public static final @NotNull DecimalFormat percent = new DecimalFormat("##0.00%");
public static final double LUCKY_MODIFIER = 1.333D;
/**
* Return a chance of success in "percentage" format, show to the player in UI elements
*
* @param player target player
* @param subSkillType target subskill
* @param isLucky whether to apply luck modifiers
*
* @return "percentage" representation of success
*/
public static double chanceOfSuccessPercentage(@NotNull Player player,
@NotNull SubSkillType subSkillType,
boolean isLucky) {
Probability probability = getSubSkillProbability(subSkillType, player);
//Probability values are on a 0-1 scale and need to be "transformed" into a 1-100 scale
double percentageValue = probability.getValue(); //Doesn't need to be scaled
//Apply lucky modifier
if(isLucky) {
percentageValue *= LUCKY_MODIFIER;
}
return percentageValue;
}
public static double chanceOfSuccessPercentage(@NotNull Probability probability, boolean isLucky) {
//Probability values are on a 0-1 scale and need to be "transformed" into a 1-100 scale
double percentageValue = probability.getValue();
//Apply lucky modifier
if(isLucky) {
percentageValue *= LUCKY_MODIFIER;
}
return percentageValue;
}
static double getStaticRandomChance(@NotNull SubSkillType subSkillType) throws InvalidStaticChance {
return switch (subSkillType) {
case AXES_ARMOR_IMPACT -> mcMMO.p.getAdvancedConfig().getImpactChance();
case AXES_GREATER_IMPACT -> mcMMO.p.getAdvancedConfig().getGreaterImpactChance();
case TAMING_FAST_FOOD_SERVICE -> mcMMO.p.getAdvancedConfig().getFastFoodChance();
default -> throw new InvalidStaticChance();
};
}
static SkillProbabilityType getProbabilityType(@NotNull SubSkillType subSkillType) {
SkillProbabilityType skillProbabilityType = SkillProbabilityType.DYNAMIC_CONFIGURABLE;
if(subSkillType == SubSkillType.TAMING_FAST_FOOD_SERVICE
|| subSkillType == SubSkillType.AXES_ARMOR_IMPACT
|| subSkillType == SubSkillType.AXES_GREATER_IMPACT)
skillProbabilityType = SkillProbabilityType.STATIC_CONFIGURABLE;
return skillProbabilityType;
}
static @NotNull Probability ofSubSkill(@Nullable Player player,
@NotNull SubSkillType subSkillType) {
switch (getProbabilityType(subSkillType)) {
case DYNAMIC_CONFIGURABLE:
double probabilityCeiling;
double xCeiling;
double xPos;
if (player != null) {
McMMOPlayer mmoPlayer = UserManager.getPlayer(player);
if(mmoPlayer != null)
xPos = mmoPlayer.getSkillLevel(subSkillType.getParentSkill());
else
xPos = 0;
} else {
xPos = 0;
}
//Probability ceiling is configurable in this type
probabilityCeiling = mcMMO.p.getAdvancedConfig().getMaximumProbability(subSkillType);
//The xCeiling is configurable in this type
xCeiling = mcMMO.p.getAdvancedConfig().getMaxBonusLevel(subSkillType);
return new ProbabilityImpl(xPos, xCeiling, probabilityCeiling);
case STATIC_CONFIGURABLE:
try {
return Probability.ofPercent(getStaticRandomChance(subSkillType));
} catch (InvalidStaticChance invalidStaticChance) {
invalidStaticChance.printStackTrace();
}
default:
throw new RuntimeException("No case in switch statement for Skill Probability Type!");
}
}
/**
* This is one of several Skill RNG check methods
* This helper method is for specific {@link SubSkillType}, which help mcMMO understand where the RNG values used in our calculations come from this {@link SubSkillType}
* <p>
* 1) Determine where the RNG values come from for the passed {@link SubSkillType}
* NOTE: In the config file, there are values which are static and which are more dynamic, this is currently a bit hardcoded and will need to be updated manually
* <p>
* 2) Determine whether to use Lucky multiplier and influence the outcome
* <p>
* 3) Creates a {@link Probability} and pipes it to {@link ProbabilityUtil} which processes the result and returns it
* <p>
* This also calls a {@link SubSkillEvent} which can be cancelled, if it is cancelled this will return false
* The outcome of the probability can also be modified by this event that is called
*
* @param subSkillType target subskill
* @param player target player, can be null (null players are given odds equivalent to a player with no levels or luck)
* @return true if the Skill RNG succeeds, false if it fails
*/
public static boolean isSkillRNGSuccessful(@NotNull SubSkillType subSkillType, @NotNull Player player) {
//Process probability
Probability probability = getSubSkillProbability(subSkillType, player);
//Send out event
SubSkillEvent subSkillEvent = EventUtils.callSubSkillEvent(player, subSkillType);
if(subSkillEvent.isCancelled()) {
return false; //Event got cancelled so this doesn't succeed
}
//Result modifier
double resultModifier = subSkillEvent.getResultModifier();
//Mutate probability
if(resultModifier != 1.0D)
probability = Probability.ofPercent(probability.getValue() * resultModifier);
//Luck
boolean isLucky = Permissions.lucky(player, subSkillType.getParentSkill());
if(isLucky) {
return probability.evaluate(LUCKY_MODIFIER);
} else {
return probability.evaluate();
}
}
/**
* This is one of several Skill RNG check methods
* This helper method is specific to static value RNG, which can be influenced by a player's Luck
*
* @param primarySkillType the related primary skill
* @param player the target player, can be null (null players have the worst odds)
* @param probabilityPercentage the probability of this player succeeding in "percentage" format (0-100 inclusive)
* @return true if the RNG succeeds, false if it fails
*/
public static boolean isStaticSkillRNGSuccessful(@NotNull PrimarySkillType primarySkillType, @Nullable Player player, double probabilityPercentage) {
//Grab a probability converted from a "percentage" value
Probability probability = Probability.ofPercent(probabilityPercentage);
return isStaticSkillRNGSuccessful(primarySkillType, player, probability);
}
/**
* This is one of several Skill RNG check methods
* This helper method is specific to static value RNG, which can be influenced by a player's Luck
*
* @param primarySkillType the related primary skill
* @param player the target player, can be null (null players have the worst odds)
* @param probability the probability of this player succeeding
* @return true if the RNG succeeds, false if it fails
*/
public static boolean isStaticSkillRNGSuccessful(@NotNull PrimarySkillType primarySkillType, @Nullable Player player, @NotNull Probability probability) {
boolean isLucky = player != null && Permissions.lucky(player, primarySkillType);
if(isLucky) {
return probability.evaluate(LUCKY_MODIFIER);
} else {
return probability.evaluate();
}
}
/**
* Skills activate without RNG, this allows other plugins to prevent that activation
* @param subSkillType target subskill
* @param player target player
* @return true if the skill succeeds (wasn't cancelled by any other plugin)
*/
public static boolean isNonRNGSkillActivationSuccessful(@NotNull SubSkillType subSkillType, @NotNull Player player) {
return !EventUtils.callSubSkillEvent(player, subSkillType).isCancelled();
}
/**
* Grab the {@link Probability} for a specific {@link SubSkillType} for a specific {@link Player}
*
* @param subSkillType target subskill
* @param player target player
* @return the Probability of this skill succeeding
*/
public static @NotNull Probability getSubSkillProbability(@NotNull SubSkillType subSkillType, @Nullable Player player) {
return ProbabilityUtil.ofSubSkill(player, subSkillType);
}
public static @NotNull String[] getRNGDisplayValues(@NotNull Player player, @NotNull SubSkillType subSkill) {
double firstValue = chanceOfSuccessPercentage(player, subSkill, false);
double secondValue = chanceOfSuccessPercentage(player, subSkill, true);
return new String[]{percent.format(firstValue), percent.format(secondValue)};
}
public static @NotNull String[] getRNGDisplayValues(@NotNull Probability probability) {
double firstValue = chanceOfSuccessPercentage(probability, false);
double secondValue = chanceOfSuccessPercentage(probability, true);
return new String[]{percent.format(firstValue), percent.format(secondValue)};
}
}

View File

@@ -1,84 +0,0 @@
package com.gmail.nossr50.util.random;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.util.skills.SkillUtils;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.VisibleForTesting;
import java.text.DecimalFormat;
import java.util.concurrent.ThreadLocalRandom;
public class RandomChanceUtil {
public static final @NotNull DecimalFormat percent = new DecimalFormat("##0.00%");
public static final double LUCKY_MODIFIER = 1.333D;
/**
* Simulate an outcome on a probability and return true or false for the result of that outcome
*
* @param probability target probability
* @return true if the probability succeeded, false if it failed
*/
public static boolean processProbability(@NotNull Probability probability) {
return isSuccessfulRoll(probability.getValue());
}
/**
* Modify and then Simulate an outcome on a probability and return true or false for the result of that outcome
*
* @param probability target probability
* @param probabilityMultiplier probability will be multiplied by this before success is checked
* @return true if the probability succeeded, false if it failed
*/
public static boolean processProbability(@NotNull Probability probability, double probabilityMultiplier) {
double probabilityValue = probability.getValue() * probabilityMultiplier;
return isSuccessfulRoll(probabilityValue);
}
/**
* Simulates a "roll of the dice"
* If the value passed is higher than the "random" value, than it is a successful roll
*
* @param probabilityValue probability value
* @return true for succeeding, false for failing
*/
@VisibleForTesting
static boolean isSuccessfulRoll(double probabilityValue) {
return probabilityValue >= ThreadLocalRandom.current().nextDouble(100D);
}
/**
* Return a chance of success in "percentage" format, show to the player in UI elements
*
* @param player target player
* @param subSkillType target subskill
* @param isLucky whether to apply luck modifiers
*
* @return "percentage" representation of success
*/
public static double chanceOfSuccessPercentage(@NotNull Player player, @NotNull SubSkillType subSkillType, boolean isLucky) {
Probability probability = SkillUtils.getSubSkillProbability(subSkillType, player);
//Probability values are on a 0-1 scale and need to be "transformed" into a 1-100 scale
double percentageValue = probability.getValue(); //Doesn't need to be scaled
//Apply lucky modifier
if(isLucky) {
percentageValue *= LUCKY_MODIFIER;
}
return percentageValue;
}
public static double chanceOfSuccessPercentage(@NotNull Probability probability, boolean isLucky) {
//Probability values are on a 0-1 scale and need to be "transformed" into a 1-100 scale
double percentageValue = probability.getValue();
//Apply lucky modifier
if(isLucky) {
percentageValue *= LUCKY_MODIFIER;
}
return percentageValue;
}
}

View File

@@ -8,17 +8,13 @@ import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import com.gmail.nossr50.events.skills.secondaryabilities.SubSkillEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.metadata.ItemMetadataService;
import com.gmail.nossr50.util.EventUtils;
import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.NotificationManager;
import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.random.*;
import com.gmail.nossr50.util.text.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@@ -356,127 +352,4 @@ public final class SkillUtils {
return quantity;
}
/**
* This is one of several Skill RNG check methods
* This helper method is for specific {@link SubSkillType}, which help mcMMO understand where the RNG values used in our calculations come from from this {@link SubSkillType}
*
* 1) Determine where the RNG values come from for the passed {@link SubSkillType}
* NOTE: In the config file, there are values which are static and which are more dynamic, this is currently a bit hardcoded and will need to be updated manually
*
* 2) Determine whether or not to use Lucky multiplier and influence the outcome
*
* 3) Creates a {@link Probability} and pipes it to {@link RandomChanceUtil} which processes the result and returns it
*
* This also calls a {@link SubSkillEvent} which can be cancelled, if it is cancelled this will return false
* The outcome of the probability can also be modified by this event that is called
*
* @param subSkillType target subskill
* @param player target player, can be null (null players are given odds equivalent to a player with no levels or luck)
* @return true if the Skill RNG succeeds, false if it fails
*/
public static boolean isSkillRNGSuccessful(@NotNull SubSkillType subSkillType, @NotNull Player player) {
//Process probability
Probability probability = getSubSkillProbability(subSkillType, player);
//Send out event
SubSkillEvent subSkillEvent = EventUtils.callSubSkillEvent(player, subSkillType);
if(subSkillEvent.isCancelled()) {
return false; //Event got cancelled so this doesn't succeed
}
//Result modifier
double resultModifier = subSkillEvent.getResultModifier();
//Mutate probability
if(resultModifier != 1.0D)
probability = Probability.ofPercentageValue(probability.getValue() * resultModifier);
//Luck
boolean isLucky = Permissions.lucky(player, subSkillType.getParentSkill());
if(isLucky) {
return RandomChanceUtil.processProbability(probability, RandomChanceUtil.LUCKY_MODIFIER);
} else {
return RandomChanceUtil.processProbability(probability);
}
}
/**
* This is one of several Skill RNG check methods
* This helper method is specific to static value RNG, which can be influenced by a player's Luck
*
* @param primarySkillType the related primary skill
* @param player the target player, can be null (null players have the worst odds)
* @param probabilityPercentage the probability of this player succeeding in "percentage" format (0-100 inclusive)
* @return true if the RNG succeeds, false if it fails
*/
public static boolean isStaticSkillRNGSuccessful(@NotNull PrimarySkillType primarySkillType, @Nullable Player player, double probabilityPercentage) {
//Grab a probability converted from a "percentage" value
Probability probability = Probability.ofPercentageValue(probabilityPercentage);
return isStaticSkillRNGSuccessful(primarySkillType, player, probability);
}
/**
* This is one of several Skill RNG check methods
* This helper method is specific to static value RNG, which can be influenced by a player's Luck
*
* @param primarySkillType the related primary skill
* @param player the target player, can be null (null players have the worst odds)
* @param probability the probability of this player succeeding
* @return true if the RNG succeeds, false if it fails
*/
public static boolean isStaticSkillRNGSuccessful(@NotNull PrimarySkillType primarySkillType, @Nullable Player player, @NotNull Probability probability) {
boolean isLucky = player != null && Permissions.lucky(player, primarySkillType);
if(isLucky) {
return RandomChanceUtil.processProbability(probability, RandomChanceUtil.LUCKY_MODIFIER);
} else {
return RandomChanceUtil.processProbability(probability);
}
}
/**
* Skills activate without RNG, this allows other plugins to prevent that activation
* @param subSkillType target subskill
* @param player target player
* @return true if the skill succeeds (wasn't cancelled by any other plugin)
*/
public static boolean isNonRNGSkillActivationSuccessful(@NotNull SubSkillType subSkillType, @NotNull Player player) {
return !EventUtils.callSubSkillEvent(player, subSkillType).isCancelled();
}
/**
* Grab the {@link Probability} for a specific {@link SubSkillType} for a specific {@link Player}
*
* @param subSkillType target subskill
* @param player target player
* @return the Probability of this skill succeeding
* @throws InvalidStaticChance when a skill that does not have a hard coded static chance and it is asked for
* @throws RuntimeException
*/
public static @NotNull Probability getSubSkillProbability(@NotNull SubSkillType subSkillType, @Nullable Player player) {
SkillProbabilityType skillProbabilityType = SkillProbabilityType.DYNAMIC_CONFIGURABLE;
if(subSkillType == SubSkillType.TAMING_FAST_FOOD_SERVICE || subSkillType == SubSkillType.AXES_ARMOR_IMPACT || subSkillType == SubSkillType.AXES_GREATER_IMPACT)
skillProbabilityType = SkillProbabilityType.STATIC_CONFIGURABLE;
return Probability.ofSubSkill(player, subSkillType, skillProbabilityType);
}
public static @NotNull String[] getRNGDisplayValues(@NotNull Player player, @NotNull SubSkillType subSkill) {
double firstValue = RandomChanceUtil.chanceOfSuccessPercentage(player, subSkill, false);
double secondValue = RandomChanceUtil.chanceOfSuccessPercentage(player, subSkill, true);
return new String[]{RandomChanceUtil.percent.format(firstValue), RandomChanceUtil.percent.format(secondValue)};
}
public static @NotNull String[] getRNGDisplayValues(@NotNull Probability probability) {
double firstValue = RandomChanceUtil.chanceOfSuccessPercentage(probability, false);
double secondValue = RandomChanceUtil.chanceOfSuccessPercentage(probability, true);
return new String[]{RandomChanceUtil.percent.format(firstValue), RandomChanceUtil.percent.format(secondValue)};
}
}