use of com.elmakers.mine.bukkit.utility.WeightedPair in project MagicPlugin by elBukkit.
the class WandLevel method randomizeWand.
public boolean randomizeWand(Mage mage, Wand wand, boolean additive, boolean hasUpgrade, boolean addSpells) {
// Add random spells to the wand
Mage activeMage = wand.getActiveMage();
if (mage == null) {
mage = activeMage;
}
wand.setActiveMage(mage);
boolean addedSpells = false;
Deque<WeightedPair<String>> remainingSpells = getRemainingSpells(wand);
if (addSpells) {
if (remainingSpells.size() > 0) {
Integer spellCount = RandomUtils.weightedRandom(spellCountProbability);
for (int i = 0; spellCount != null && i < spellCount; i++) {
String spellKey = RandomUtils.weightedRandom(remainingSpells);
boolean added = wand.addSpell(spellKey);
mage.sendDebugMessage("Trying to add spell: " + spellKey + " ? " + added);
if (added) {
addedSpells = true;
}
}
}
}
// Look through all spells for the max mana casting cost
// Also look for any material-using spells
boolean needsMaterials = false;
int maxManaCost = 0;
Set<String> spells = wand.getSpells();
for (String spellName : spells) {
SpellTemplate spell = wand.getController().getSpellTemplate(spellName);
if (spell != null) {
needsMaterials = needsMaterials || spell.usesBrush();
Collection<CastingCost> costs = spell.getCosts();
if (costs != null) {
for (CastingCost cost : costs) {
maxManaCost = Math.max(maxManaCost, cost.getMana());
}
}
}
}
// Add random materials
boolean addedMaterials = false;
Deque<WeightedPair<String>> remainingMaterials = getRemainingMaterials(wand);
if (needsMaterials && remainingMaterials.size() > 0) {
int currentMaterialCount = wand.getBrushes().size();
Integer materialCount = RandomUtils.weightedRandom(materialCountProbability);
// Make sure the wand has at least one material.
if (materialCount == null) {
materialCount = 0;
}
if (currentMaterialCount == 0) {
materialCount = Math.max(1, materialCount);
}
int retries = 100;
for (int i = 0; i < materialCount; i++) {
String materialKey = RandomUtils.weightedRandom(remainingMaterials);
materialKey = materialKey.replace("|", ":");
if (!wand.addBrush(materialKey)) {
// Try again up to a certain number if we picked one the wand already had.
if (retries-- > 0)
i--;
} else {
addedMaterials = true;
}
}
}
// Let them upgrade if they aren't getting any new spells or brushes
if (hasUpgrade && addSpells && !(addedMaterials && needsMaterials) && !addedSpells && ((getSpellCount() > 0 && spellProbability.size() > 0) || (getMaterialCount() > 0 && materialProbability.size() > 0))) {
if (mage != null && mage.getDebugLevel() > 0) {
mage.sendDebugMessage("Has upgrade: " + hasUpgrade);
mage.sendDebugMessage("Added spells: " + addedSpells + ", should: " + addSpells);
mage.sendDebugMessage("Spells per enchant: " + getSpellCount());
mage.sendDebugMessage("Spells in list: " + spellProbability.size());
mage.sendDebugMessage("Added brushes: " + addedMaterials + ", needed: " + needsMaterials);
}
wand.setActiveMage(activeMage);
return false;
}
// Add random wand properties
boolean addedProperties = false;
Integer propertyCount = propertyCountProbability.size() == 0 ? Integer.valueOf(0) : RandomUtils.weightedRandom(propertyCountProbability);
ConfigurationSection wandProperties = new MemoryConfiguration();
List<String> propertyKeys = new ArrayList<>(propertiesProbability.keySet());
List<String> propertiesAvailable = new ArrayList<>();
for (String propertyKey : propertyKeys) {
double currentValue = wand.getDouble(propertyKey);
double maxValue = path.getMaxProperty(propertyKey);
if (currentValue < maxValue) {
propertiesAvailable.add(propertyKey);
}
}
// Make sure we give them *something* if something is available
if (propertiesAvailable.size() > 0 && !addedMaterials && !addedSpells && propertyCount == 0) {
propertyCount = 1;
}
while (propertyCount != null && propertyCount-- > 0 && propertiesAvailable.size() > 0) {
int randomPropertyIndex = (int) (Math.random() * propertiesAvailable.size());
String randomProperty = propertiesAvailable.get(randomPropertyIndex);
Deque<WeightedPair<Float>> probabilities = propertiesProbability.get(randomProperty);
double current = wand.getDouble(randomProperty);
double maxValue = path.getMaxProperty(randomProperty);
if (probabilities.size() > 0 && current < maxValue) {
addedProperties = true;
current = Math.min(maxValue, current + RandomUtils.weightedRandom(probabilities));
wandProperties.set(randomProperty, current);
}
}
if (wand.isCostFree()) {
// Cost-Free wands don't need mana.
wandProperties.set("mana_regeneration", 0);
wandProperties.set("mana_max", 0);
wandProperties.set("mana", 0);
} else {
int manaRegeneration = wand.getManaRegeneration();
if (manaRegenerationProbability.size() > 0 && manaRegeneration < path.getMaxManaRegeneration()) {
addedProperties = true;
manaRegeneration = Math.min(path.getMaxManaRegeneration(), manaRegeneration + RandomUtils.weightedRandom(manaRegenerationProbability));
wandProperties.set("mana_regeneration", manaRegeneration);
}
int manaMax = wand.getManaMax();
if (manaMaxProbability.size() > 0 && manaMax < path.getMaxMaxMana()) {
manaMax = Math.min(path.getMaxMaxMana(), manaMax + RandomUtils.weightedRandom(manaMaxProbability));
if (path.getMatchSpellMana()) {
// Make sure the wand has at least enough mana to cast the highest costing spell it has.
manaMax = Math.max(maxManaCost, manaMax);
}
wandProperties.set("mana_max", manaMax);
addedProperties = true;
}
// Refill the wand's mana, why not
wandProperties.set("mana", manaMax);
}
// Add or set uses to the wand
if (additive) {
// Only add uses to a wand if it already has some.
int wandUses = wand.getRemainingUses();
if (wandUses > 0 && wandUses < path.getMaxUses() && addUseProbability.size() > 0) {
wandProperties.set("uses", Math.min(path.getMaxUses(), wandUses + RandomUtils.weightedRandom(addUseProbability)));
addedProperties = true;
}
} else if (useProbability.size() > 0) {
wandProperties.set("uses", Math.min(path.getMaxUses(), RandomUtils.weightedRandom(useProbability)));
}
// Set properties.
wand.upgrade(wandProperties);
wand.setActiveMage(activeMage);
return addedMaterials || addedSpells || addedProperties;
}
use of com.elmakers.mine.bukkit.utility.WeightedPair in project MagicPlugin by elBukkit.
the class WandUpgradePath method canProgress.
@Override
public boolean canProgress(CasterProperties properties) {
if (levelMap == null)
return false;
WandLevel maxLevel = levelMap.get(levels[levels.length - 1]);
Deque<WeightedPair<String>> remainingSpells = maxLevel.getRemainingSpells(properties);
Mage mage = properties.getMage();
if (mage != null && mage.getDebugLevel() > 0) {
mage.sendDebugMessage("Spells remaining: " + remainingSpells.size());
}
return (remainingSpells.size() > 0);
}
Aggregations