@Nullable public static <T extends Object> T weightedRandom(Deque<WeightedPair<T>> weightList) { if (weightList == null || weightList.size() == 0) return null; Float maxWeight = weightList.getLast().getThreshold(); Float selectedWeight = random.nextFloat() * maxWeight; for (WeightedPair<T> weight : weightList) { if (selectedWeight < weight.getThreshold()) { return weight.getValue(); } } return weightList.getFirst().getValue(); }
public Deque<WeightedPair<String>> getRemainingMaterials(Wand wand) { Deque<WeightedPair<String>> remainingMaterials = new ArrayDeque<>(); for (WeightedPair<String> material : materialProbability) { String materialKey = material.getValue(); // Fixup |'s to :'s .... kinda hacky, but I didn't think this through unfortunately. :\ // TODO: escape the keys as strings with '', which is probably the right way to do it. materialKey = materialKey.replace("|", ":"); if (!wand.hasBrush(materialKey) && MaterialBrush.isValidMaterial(materialKey, false)) { remainingMaterials.add(material); } } return remainingMaterials; }
public Deque<WeightedPair<String>> getRemainingSpells(CasterProperties properties) { Deque<WeightedPair<String>> remainingSpells = new ArrayDeque<>(); for (WeightedPair<String> spell : spellProbability) { if (spell.getRawThreshold() >= 1 && !properties.hasSpell(spell.getValue())) { remainingSpells.add(spell); } } return remainingSpells; }
public static <T extends Object> Deque<WeightedPair<T>> merge(Deque<WeightedPair<T>> base, Deque<WeightedPair<T>> inherit) { if (inherit == null || inherit.size() == 0) { return base; } if (base == null) { base = new ArrayDeque<>(); } if (base.size() == 0) { base.addAll(inherit); } else { WeightedPair<T> lastPair = base.getLast(); float threshold = lastPair.getThreshold(); for (WeightedPair<T> inheritPair : inherit) { float weight = inheritPair.getThreshold(); threshold += weight; base.add(new WeightedPair<>(threshold, inheritPair.getRawThreshold(), inheritPair.getValue())); } } return base; }
if (controller.getSpellTemplate(spellValue.getValue()) != null) { spellProbability.add(spellValue); RandomUtils.populateStringProbabilityMap(brushes, template, "materials", levelIndex, nextLevelIndex, distance); for (WeightedPair<String> brushValue : brushes) { MaterialBrush brush = new MaterialBrush(brushValue.getValue()); if (brush.isValid(false)) { materialProbability.add(brushValue);