fix(Core/Battlegrounds) Rewrite RandomBG and enabling bg/arenas weights (#2516)

This commit is contained in:
Shard
2019-12-28 09:40:13 +01:00
committed by Stoabrogga
parent 5198b2614e
commit 067d9e791f
19 changed files with 210 additions and 221 deletions

View File

@@ -13,9 +13,11 @@
#include <iterator>
#include <utility>
#include <list>
#include <vector>
//! Because circular includes are bad
extern uint32 urand(uint32 min, uint32 max);
extern uint32 urandweighted(size_t count, double const* chances);
namespace acore
{
@@ -97,6 +99,14 @@ namespace acore
std::advance(it, urand(0, container.size() - 1));
return *it;
}
/* Select a random element from a container where each element has a different chance to be selected. */
template <class C> typename C::value_type const& SelectRandomWeightedContainerElement(C const& container, std::vector<double> const& weights)
{
typename C::const_iterator it = container.begin();
std::advance(it, urandweighted(weights.size(), weights.data()));
return *it;
}
}
//! namespace Containers
}

View File

@@ -18,9 +18,11 @@
#include <array>
#include <cwchar>
#include <string>
#include <random>
typedef ACE_TSS<SFMTRand> SFMTRandTSS;
static SFMTRandTSS sfmtRand;
static SFMTEngine engine;
int32 irand(int32 min, int32 max)
{
@@ -55,6 +57,17 @@ double rand_chance()
return sfmtRand->Random() * 100.0;
}
uint32 urandweighted(size_t count, double const* chances)
{
std::discrete_distribution<uint32> dd(chances, chances + count);
return dd(SFMTEngine::Instance());
}
SFMTEngine& SFMTEngine::Instance()
{
return engine;
}
Tokenizer::Tokenizer(const std::string &src, const char sep, uint32 vectorReserve)
{
m_str = new char[src.length() + 1];

View File

@@ -82,6 +82,8 @@ double rand_norm();
/* Return a random double from 0.0 to 100.0 (exclusive). */
double rand_chance();
uint32 urandweighted(size_t count, double const* chances);
/* Return true if a random roll fits in the specified chance (range 0-100). */
inline bool roll_chance_f(float chance)
{
@@ -555,6 +557,21 @@ bool CompareValues(ComparisionType type, T val1, T val2)
}
}
/*
* SFMT wrapper satisfying UniformRandomNumberGenerator concept for use in <random> algorithms
*/
class SFMTEngine
{
public:
typedef uint32 result_type;
static constexpr result_type min() { return std::numeric_limits<result_type>::min(); }
static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
result_type operator()() const { return rand32(); }
static SFMTEngine& Instance();
};
class EventMap
{
typedef std::multimap<uint32, uint32> EventStore;