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 | |
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 |
-rw-r--r-- | src/Enchantments.cpp | 14 | ||||
-rw-r--r-- | src/Enchantments.h | 6 | ||||
-rw-r--r-- | src/ItemGrid.cpp | 2 |
3 files changed, 16 insertions, 6 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(); } diff --git a/src/Enchantments.h b/src/Enchantments.h index e4390a5f2..31226b5c2 100644 --- a/src/Enchantments.h +++ b/src/Enchantments.h @@ -128,8 +128,10 @@ public: /** Gets random enchantment from Vector and returns it */ static cEnchantments GetRandomEnchantmentFromVector(cWeightedEnchantments & a_Enchantments); - /** Returns an enchantment from a Vector using cNoise. Mostly used for generators.*/ - static cEnchantments GenerateEnchantmentFromVector(cWeightedEnchantments & a_Enchantments, int a_Seed); + /** Selects one enchantment from a Vector using cNoise. Mostly used for generators. + Uses the enchantments' weights for the random distribution. + If a_Enchantments is empty, returns an empty enchantment. */ + static cEnchantments SelectEnchantmentFromVector(const cWeightedEnchantments & a_Enchantments, int a_Seed); /** Returns true if a_Other doesn't contain exactly the same enchantments and levels */ bool operator !=(const cEnchantments & a_Other) const; diff --git a/src/ItemGrid.cpp b/src/ItemGrid.cpp index d49ea9df1..06971a1ac 100644 --- a/src/ItemGrid.cpp +++ b/src/ItemGrid.cpp @@ -646,7 +646,7 @@ void cItemGrid::GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, s for (int j = 0; j <= NumEnchantments; j++) { - cEnchantments Enchantment = cEnchantments::GenerateEnchantmentFromVector(Enchantments, Noise.IntNoise2DInt(NumEnchantments, i)); + cEnchantments Enchantment = cEnchantments::SelectEnchantmentFromVector(Enchantments, Noise.IntNoise2DInt(NumEnchantments, i)); CurrentLoot.m_Enchantments.Add(Enchantment); cEnchantments::RemoveEnchantmentWeightFromVector(Enchantments, Enchantment); cEnchantments::CheckEnchantmentConflictsFromVector(Enchantments, Enchantment); |