diff options
author | STRWarrior <niels.breuker@hotmail.nl> | 2014-10-15 14:44:07 +0200 |
---|---|---|
committer | STRWarrior <niels.breuker@hotmail.nl> | 2014-10-15 14:44:07 +0200 |
commit | ff5f57514e172e3344d6d50fce6bd8ea84e7f32b (patch) | |
tree | c3988d3d4b12ee37770ed026425bfa7b07f84dba | |
parent | Enchanted books generate in MineShafts chests (diff) | |
download | cuberite-ff5f57514e172e3344d6d50fce6bd8ea84e7f32b.tar cuberite-ff5f57514e172e3344d6d50fce6bd8ea84e7f32b.tar.gz cuberite-ff5f57514e172e3344d6d50fce6bd8ea84e7f32b.tar.bz2 cuberite-ff5f57514e172e3344d6d50fce6bd8ea84e7f32b.tar.lz cuberite-ff5f57514e172e3344d6d50fce6bd8ea84e7f32b.tar.xz cuberite-ff5f57514e172e3344d6d50fce6bd8ea84e7f32b.tar.zst cuberite-ff5f57514e172e3344d6d50fce6bd8ea84e7f32b.zip |
-rw-r--r-- | src/Enchantments.cpp | 32 | ||||
-rw-r--r-- | src/Enchantments.h | 5 | ||||
-rw-r--r-- | src/FastRandom.cpp | 11 | ||||
-rw-r--r-- | src/FastRandom.h | 2 | ||||
-rw-r--r-- | src/ItemGrid.cpp | 2 |
5 files changed, 36 insertions, 16 deletions
diff --git a/src/Enchantments.cpp b/src/Enchantments.cpp index e106ef398..3cddf37ae 100644 --- a/src/Enchantments.cpp +++ b/src/Enchantments.cpp @@ -6,6 +6,7 @@ #include "Enchantments.h" #include "WorldStorage/FastNBT.h" #include "FastRandom.h" +#include "Noise.h" @@ -994,20 +995,43 @@ void cEnchantments::CheckEnchantmentConflictsFromVector(cWeightedEnchantments & -cEnchantments cEnchantments::GetRandomEnchantmentFromVector(cWeightedEnchantments & a_Enchantments, int a_Seed) +cEnchantments cEnchantments::GetRandomEnchantmentFromVector(cWeightedEnchantments & a_Enchantments) { cFastRandom Random; - if (a_Seed != -1) + + int AllWeights = 0; + for (cWeightedEnchantments::iterator it = a_Enchantments.begin(); it != a_Enchantments.end(); ++it) + { + AllWeights += (*it).m_Weight; + } + int RandomNumber = Random.GenerateRandomInteger(0, AllWeights - 1); + for (cWeightedEnchantments::iterator it = a_Enchantments.begin(); it != a_Enchantments.end(); ++it) { - Random = cFastRandom(a_Seed); + RandomNumber -= (*it).m_Weight; + if (RandomNumber < 0) + { + return (*it).m_Enchantments; + } } + return cEnchantments(); +} + + + + + +cEnchantments cEnchantments::GenerateEnchantmentFromVector(cWeightedEnchantments & a_Enchantments, int a_Seed) +{ int AllWeights = 0; for (cWeightedEnchantments::iterator it = a_Enchantments.begin(); it != a_Enchantments.end(); ++it) { AllWeights += (*it).m_Weight; } - int RandomNumber = Random.GenerateRandomInteger(0, AllWeights - 1); + + cNoise Noise(a_Seed); + int RandomNumber = Noise.IntNoise1DInt(AllWeights) % AllWeights; + for (cWeightedEnchantments::iterator it = a_Enchantments.begin(); it != a_Enchantments.end(); ++it) { RandomNumber -= (*it).m_Weight; diff --git a/src/Enchantments.h b/src/Enchantments.h index 798d3bf61..e4390a5f2 100644 --- a/src/Enchantments.h +++ b/src/Enchantments.h @@ -126,7 +126,10 @@ public: static void CheckEnchantmentConflictsFromVector(cWeightedEnchantments & a_Enchantments, cEnchantments a_FirstEnchantment); /** Gets random enchantment from Vector and returns it */ - static cEnchantments GetRandomEnchantmentFromVector(cWeightedEnchantments & a_Enchantments, int a_Seed = -1); + 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); /** 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/FastRandom.cpp b/src/FastRandom.cpp index 052d976da..42bf5f3f9 100644 --- a/src/FastRandom.cpp +++ b/src/FastRandom.cpp @@ -90,17 +90,10 @@ int cFastRandom::m_SeedCounter = 0; -cFastRandom::cFastRandom(int a_Seed) : +cFastRandom::cFastRandom(void) : + m_Seed(m_SeedCounter++), m_Counter(0) { - if (a_Seed == -1) - { - m_Seed = m_SeedCounter++; - } - else - { - m_Seed = a_Seed; - } } diff --git a/src/FastRandom.h b/src/FastRandom.h index 5a8ac097a..cebebad96 100644 --- a/src/FastRandom.h +++ b/src/FastRandom.h @@ -30,7 +30,7 @@ salts, the values they get will be different. class cFastRandom { public: - cFastRandom(int a_Seed = -1); + cFastRandom(void); /// Returns a random int in the range [0 .. a_Range - 1]; a_Range must be less than 1M int NextInt(int a_Range); diff --git a/src/ItemGrid.cpp b/src/ItemGrid.cpp index 68e753e08..a5273c6da 100644 --- a/src/ItemGrid.cpp +++ b/src/ItemGrid.cpp @@ -646,7 +646,7 @@ void cItemGrid::GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, s for (int I = 0; I <= NumEnchantments; I++) { - cEnchantments Enchantment = cEnchantments::GetRandomEnchantmentFromVector(Enchantments, a_Seed); + cEnchantments Enchantment = cEnchantments::GenerateEnchantmentFromVector(Enchantments, Noise.IntNoise2DInt(NumEnchantments, i)); CurrentLoot.m_Enchantments.AddFromString(Enchantment.ToString()); cEnchantments::RemoveEnchantmentWeightFromVector(Enchantments, Enchantment); } |