diff options
author | Mattes D <github@xoft.cz> | 2014-12-21 19:48:29 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2014-12-21 19:48:29 +0100 |
commit | 9e9459a36735b768f647a4cb5013c2928464a6eb (patch) | |
tree | 1ce4f6138e5258f1c265411fc31cf0750403f7ae /src/Enchantments.cpp | |
parent | Fixed crash on restart (diff) | |
download | cuberite-9e9459a36735b768f647a4cb5013c2928464a6eb.tar cuberite-9e9459a36735b768f647a4cb5013c2928464a6eb.tar.gz cuberite-9e9459a36735b768f647a4cb5013c2928464a6eb.tar.bz2 cuberite-9e9459a36735b768f647a4cb5013c2928464a6eb.tar.lz cuberite-9e9459a36735b768f647a4cb5013c2928464a6eb.tar.xz cuberite-9e9459a36735b768f647a4cb5013c2928464a6eb.tar.zst cuberite-9e9459a36735b768f647a4cb5013c2928464a6eb.zip |
Diffstat (limited to 'src/Enchantments.cpp')
-rw-r--r-- | src/Enchantments.cpp | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/Enchantments.cpp b/src/Enchantments.cpp index 36c451b81..e72ec668a 100644 --- a/src/Enchantments.cpp +++ b/src/Enchantments.cpp @@ -1021,26 +1021,34 @@ cEnchantments cEnchantments::GetRandomEnchantmentFromVector(cWeightedEnchantment -cEnchantments cEnchantments::GenerateEnchantmentFromVector(cWeightedEnchantments & a_Enchantments, int a_Seed) +cEnchantments cEnchantments::SelectEnchantmentFromVector(const cWeightedEnchantments & a_Enchantments, int a_Seed) { + // Sum up all the enchantments' weights: int AllWeights = 0; for (const auto Enchantment : a_Enchantments) { AllWeights += Enchantment.m_Weight; } + // If there's no weight for any of the enchantments, return an empty enchantment + if (AllWeights <= 0) + { + return cEnchantments(); + } + + // Pick a random enchantment: cNoise Noise(a_Seed); int RandomNumber = Noise.IntNoise1DInt(AllWeights) / 7 % AllWeights; - for (const auto Enchantment : a_Enchantments) { RandomNumber -= Enchantment.m_Weight; - if (RandomNumber < 0) + if (RandomNumber <= 0) { return Enchantment.m_Enchantments; } } + // No enchantment picked, return an empty one (we probably shouldn't ever get here): return cEnchantments(); } |