Search in sources :

Example 1 with EnumToolType

use of net.silentchaos512.gems.lib.EnumToolType in project SilentGems by SilentChaos512.

the class SoulSkill method selectSkillToLearn.

public static SoulSkill selectSkillToLearn(ToolSoul soul, ItemStack tool) {
    // if (soul.getLevel() == 5) {
    // return SUPER_SKILL;
    // }
    Map<SoulSkill, Double> candidates = new LinkedHashMap<>();
    // Create list of candidates
    for (SoulSkill skill : SKILL_LIST.values()) {
        if (skill.canLearn(soul, tool)) {
            boolean favorsElement = false;
            // Select a weight based on favored elements.
            double weight = skill.favoredElements.length < 1 ? 20 : 7;
            for (EnumSoulElement elem : skill.favoredElements) {
                if (elem == soul.getPrimaryElement()) {
                    weight = 20;
                    favorsElement = true;
                    break;
                } else if (elem == soul.getSecondaryElement()) {
                    weight = 15;
                    favorsElement = true;
                    break;
                }
            }
            // Favors certain tool types?
            if (skill.favoredType != EnumToolType.NONE) {
                EnumToolType toolType = tool.getItem() instanceof ITool ? ((ITool) tool.getItem()).getToolType() : tool.getItem() instanceof IArmor ? ((IArmor) tool.getItem()).getToolType() : EnumToolType.NONE;
                if (toolType == skill.favoredType) {
                    weight += 5;
                }
            }
            // If skill has a median level, apply that to the weight.
            if (skill.medianXpLevel > 0) {
                int diff = Math.abs(soul.level - skill.medianXpLevel);
                if (diff > 6) {
                    diff = 6;
                }
                weight -= 0.75 * diff;
            }
            // If a lower level of the skill is already known, reduce the weight.
            if (soul.skills.containsKey(skill)) {
                weight -= 2.5 * soul.skills.get(skill);
            }
            // Base weight diff, favors multiplier
            weight += skill.weightDiff;
            if (favorsElement) {
                weight *= skill.favorWeightMulti;
            }
            // Make sure weight is at least 1.
            if (weight < 1) {
                weight = 1;
            }
            candidates.put(skill, weight);
        }
    }
    // Seed based on soul elements, level, and tool UUID.
    Random rand = new Random(soul.getPrimaryElement().ordinal() + (soul.getSecondaryElement().ordinal() << 4) + (soul.getLevel() << 8) + (ToolHelper.getUUID(tool).getLeastSignificantBits() << 16));
    // Weighted random selection.
    SoulSkill selected = null;
    double bestValue = Double.MIN_VALUE;
    for (SoulSkill skill : candidates.keySet()) {
        double value = -Math.log(rand.nextFloat() / candidates.get(skill));
        SilentGems.logHelper.debug(skill.id, candidates.get(skill), value, bestValue);
        if (value > bestValue) {
            bestValue = value;
            selected = skill;
        }
    }
    return selected;
}
Also used : Random(java.util.Random) EnumToolType(net.silentchaos512.gems.lib.EnumToolType) EnumSoulElement(net.silentchaos512.gems.lib.soul.EnumSoulElement) ITool(net.silentchaos512.gems.api.ITool) IArmor(net.silentchaos512.gems.api.IArmor) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)1 Random (java.util.Random)1 IArmor (net.silentchaos512.gems.api.IArmor)1 ITool (net.silentchaos512.gems.api.ITool)1 EnumToolType (net.silentchaos512.gems.lib.EnumToolType)1 EnumSoulElement (net.silentchaos512.gems.lib.soul.EnumSoulElement)1