From e3aa6e48577f3247653e94a76b68a4626a630c65 Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Wed, 15 Oct 2014 14:08:45 +0200 Subject: Enchanted books generate in MineShafts chests --- src/Enchantments.cpp | 6 +++++- src/Enchantments.h | 2 +- src/FastRandom.cpp | 11 +++++++++-- src/FastRandom.h | 2 +- src/ItemGrid.cpp | 15 ++++++++++++++- 5 files changed, 30 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Enchantments.cpp b/src/Enchantments.cpp index 264878c22..e106ef398 100644 --- a/src/Enchantments.cpp +++ b/src/Enchantments.cpp @@ -994,9 +994,13 @@ void cEnchantments::CheckEnchantmentConflictsFromVector(cWeightedEnchantments & -cEnchantments cEnchantments::GetRandomEnchantmentFromVector(cWeightedEnchantments & a_Enchantments) +cEnchantments cEnchantments::GetRandomEnchantmentFromVector(cWeightedEnchantments & a_Enchantments, int a_Seed) { cFastRandom Random; + if (a_Seed != -1) + { + Random = cFastRandom(a_Seed); + } int AllWeights = 0; for (cWeightedEnchantments::iterator it = a_Enchantments.begin(); it != a_Enchantments.end(); ++it) diff --git a/src/Enchantments.h b/src/Enchantments.h index 824f6aa55..798d3bf61 100644 --- a/src/Enchantments.h +++ b/src/Enchantments.h @@ -126,7 +126,7 @@ public: static void CheckEnchantmentConflictsFromVector(cWeightedEnchantments & a_Enchantments, cEnchantments a_FirstEnchantment); /** Gets random enchantment from Vector and returns it */ - static cEnchantments GetRandomEnchantmentFromVector(cWeightedEnchantments & a_Enchantments); + static cEnchantments GetRandomEnchantmentFromVector(cWeightedEnchantments & a_Enchantments, int a_Seed = -1); /** 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 42bf5f3f9..052d976da 100644 --- a/src/FastRandom.cpp +++ b/src/FastRandom.cpp @@ -90,10 +90,17 @@ int cFastRandom::m_SeedCounter = 0; -cFastRandom::cFastRandom(void) : - m_Seed(m_SeedCounter++), +cFastRandom::cFastRandom(int a_Seed) : 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 cebebad96..5a8ac097a 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(void); + cFastRandom(int a_Seed = -1); /// 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 2344dc0a5..68e753e08 100644 --- a/src/ItemGrid.cpp +++ b/src/ItemGrid.cpp @@ -637,7 +637,20 @@ void cItemGrid::GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, s int Rnd = (Noise.IntNoise1DInt(i) / 7); int LootRnd = Rnd % TotalProbab; Rnd >>= 8; - cItem CurrentLoot = cItem(E_ITEM_BOOK, 1, 0); // TODO: enchantment + cItem CurrentLoot = cItem(E_ITEM_ENCHANTED_BOOK, 1, 0); + + // Choose the enchantments + cWeightedEnchantments Enchantments; + cEnchantments::AddItemEnchantmentWeights(Enchantments, E_ITEM_BOOK, 24 + Noise.IntNoise2DInt(a_Seed, TotalProbab) % 7); + int NumEnchantments = Noise.IntNoise3DInt(TotalProbab, Rnd, a_Seed) % 5; // The number of enchantments this book wil get. + + for (int I = 0; I <= NumEnchantments; I++) + { + cEnchantments Enchantment = cEnchantments::GetRandomEnchantmentFromVector(Enchantments, a_Seed); + CurrentLoot.m_Enchantments.AddFromString(Enchantment.ToString()); + cEnchantments::RemoveEnchantmentWeightFromVector(Enchantments, Enchantment); + } + for (size_t j = 0; j < a_CountLootProbabs; j++) { LootRnd -= a_LootProbabs[i].m_Weight; -- cgit v1.2.3 From ff5f57514e172e3344d6d50fce6bd8ea84e7f32b Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Wed, 15 Oct 2014 14:44:07 +0200 Subject: Returned cFastRandom and cEnchantment:GetRandomEnchantmentFromVector to before Added cEnchantment:GenerateEnchantmentFromVector that allows to get the same enchantments over and over. --- src/Enchantments.cpp | 32 ++++++++++++++++++++++++++++---- src/Enchantments.h | 5 ++++- src/FastRandom.cpp | 11 ++--------- src/FastRandom.h | 2 +- src/ItemGrid.cpp | 2 +- 5 files changed, 36 insertions(+), 16 deletions(-) (limited to 'src') 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); } -- cgit v1.2.3 From e4ee71868e307c5d51ca910a97a36623efdfaff0 Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Wed, 15 Oct 2014 15:25:48 +0200 Subject: Renamed the counter to 'j' --- src/ItemGrid.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/ItemGrid.cpp b/src/ItemGrid.cpp index a5273c6da..5621c00fd 100644 --- a/src/ItemGrid.cpp +++ b/src/ItemGrid.cpp @@ -644,7 +644,7 @@ void cItemGrid::GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, s cEnchantments::AddItemEnchantmentWeights(Enchantments, E_ITEM_BOOK, 24 + Noise.IntNoise2DInt(a_Seed, TotalProbab) % 7); int NumEnchantments = Noise.IntNoise3DInt(TotalProbab, Rnd, a_Seed) % 5; // The number of enchantments this book wil get. - for (int I = 0; I <= NumEnchantments; I++) + for (int j = 0; j <= NumEnchantments; j++) { cEnchantments Enchantment = cEnchantments::GenerateEnchantmentFromVector(Enchantments, Noise.IntNoise2DInt(NumEnchantments, i)); CurrentLoot.m_Enchantments.AddFromString(Enchantment.ToString()); -- cgit v1.2.3 From 57f8d2a55e2a20e255a64d46746eb3e58944ef6b Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Wed, 15 Oct 2014 15:31:06 +0200 Subject: Adding enchantments directly instead of first getting the name of it. --- src/ItemGrid.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/ItemGrid.cpp b/src/ItemGrid.cpp index 5621c00fd..cd77d52e1 100644 --- a/src/ItemGrid.cpp +++ b/src/ItemGrid.cpp @@ -647,7 +647,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)); - CurrentLoot.m_Enchantments.AddFromString(Enchantment.ToString()); + CurrentLoot.m_Enchantments.Add(Enchantment); cEnchantments::RemoveEnchantmentWeightFromVector(Enchantments, Enchantment); } -- cgit v1.2.3 From 0cdd2b64ed9c99c171c0689ada24147732fb3d4b Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Wed, 15 Oct 2014 15:42:45 +0200 Subject: IntNoise1DInt result gets devided by 7 first suggestion by xoft --- src/Enchantments.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/Enchantments.cpp b/src/Enchantments.cpp index 3cddf37ae..10d23a8fb 100644 --- a/src/Enchantments.cpp +++ b/src/Enchantments.cpp @@ -1030,7 +1030,7 @@ cEnchantments cEnchantments::GenerateEnchantmentFromVector(cWeightedEnchantments } cNoise Noise(a_Seed); - int RandomNumber = Noise.IntNoise1DInt(AllWeights) % AllWeights; + int RandomNumber = Noise.IntNoise1DInt(AllWeights) / 7 % AllWeights; for (cWeightedEnchantments::iterator it = a_Enchantments.begin(); it != a_Enchantments.end(); ++it) { -- cgit v1.2.3 From fa6f8fb38e8bb4a2d047ff9dee3ee57b10b5cf1b Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Wed, 15 Oct 2014 15:44:52 +0200 Subject: The code now prevents conflicting enchantments. --- src/ItemGrid.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/ItemGrid.cpp b/src/ItemGrid.cpp index cd77d52e1..6a3d08679 100644 --- a/src/ItemGrid.cpp +++ b/src/ItemGrid.cpp @@ -649,6 +649,7 @@ void cItemGrid::GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, s cEnchantments Enchantment = cEnchantments::GenerateEnchantmentFromVector(Enchantments, Noise.IntNoise2DInt(NumEnchantments, i)); CurrentLoot.m_Enchantments.Add(Enchantment); cEnchantments::RemoveEnchantmentWeightFromVector(Enchantments, Enchantment); + cEnchantments::CheckEnchantmentConflictsFromVector(Enchantments, Enchantment); } for (size_t j = 0; j < a_CountLootProbabs; j++) -- cgit v1.2.3 From 4038e1cb42e84f210826f22c6a55b13c440cf9fb Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Wed, 15 Oct 2014 18:11:28 +0200 Subject: Using C++11 loops --- src/Enchantments.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Enchantments.cpp b/src/Enchantments.cpp index 10d23a8fb..80a9810b6 100644 --- a/src/Enchantments.cpp +++ b/src/Enchantments.cpp @@ -1024,20 +1024,20 @@ cEnchantments cEnchantments::GetRandomEnchantmentFromVector(cWeightedEnchantment cEnchantments cEnchantments::GenerateEnchantmentFromVector(cWeightedEnchantments & a_Enchantments, int a_Seed) { int AllWeights = 0; - for (cWeightedEnchantments::iterator it = a_Enchantments.begin(); it != a_Enchantments.end(); ++it) + for (const auto Enchantment : a_Enchantments) { - AllWeights += (*it).m_Weight; + AllWeights += Enchantment.m_Weight; } cNoise Noise(a_Seed); int RandomNumber = Noise.IntNoise1DInt(AllWeights) / 7 % AllWeights; - for (cWeightedEnchantments::iterator it = a_Enchantments.begin(); it != a_Enchantments.end(); ++it) + for (const auto Enchantment : a_Enchantments) { - RandomNumber -= (*it).m_Weight; + RandomNumber -= Enchantment.m_Weight; if (RandomNumber < 0) { - return (*it).m_Enchantments; + return Enchantment.m_Enchantments; } } -- cgit v1.2.3