mirror of
https://github.com/smartcmd/MinecraftConsoles.git
synced 2026-04-30 10:55:04 +00:00
Some checks failed
Nightly Server Release / build (Windows64) (push) Has been cancelled
Nightly Release / build (Windows64) (push) Has been cancelled
Nightly Server Release / release (push) Has been cancelled
Nightly Server Release / Build and Push Docker Image (push) Has been cancelled
Nightly Server Release / cleanup (push) Has been cancelled
Nightly Release / release (push) Has been cancelled
Nightly Release / cleanup (push) Has been cancelled
72 lines
1.5 KiB
C++
72 lines
1.5 KiB
C++
#include "stdafx.h"
|
|
#include "WeighedRandom.h"
|
|
|
|
int WeighedRandom::getTotalWeight(vector<WeighedRandomItem *> *items)
|
|
{
|
|
int totalWeight = 0;
|
|
for( const auto& item : *items)
|
|
{
|
|
totalWeight += item->randomWeight;
|
|
}
|
|
return totalWeight;
|
|
}
|
|
|
|
WeighedRandomItem *WeighedRandom::getRandomItem(Random *random, vector<WeighedRandomItem *> *items, int totalWeight)
|
|
{
|
|
if (totalWeight <= 0)
|
|
{
|
|
DEBUG_BREAK();
|
|
}
|
|
|
|
int selection = random->nextInt(totalWeight);
|
|
|
|
for(const auto& item : *items)
|
|
{
|
|
selection -= item->randomWeight;
|
|
if (selection < 0)
|
|
{
|
|
return item;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
WeighedRandomItem *WeighedRandom::getRandomItem(Random *random, vector<WeighedRandomItem *> *items)
|
|
{
|
|
return getRandomItem(random, items, getTotalWeight(items));
|
|
}
|
|
|
|
int WeighedRandom::getTotalWeight(WeighedRandomItemArray items)
|
|
{
|
|
int totalWeight = 0;
|
|
for( unsigned int i = 0; i < items.length; i++ )
|
|
{
|
|
totalWeight += items[i]->randomWeight;
|
|
}
|
|
return totalWeight;
|
|
}
|
|
|
|
WeighedRandomItem *WeighedRandom::getRandomItem(Random *random, WeighedRandomItemArray items, int totalWeight)
|
|
{
|
|
if (totalWeight <= 0)
|
|
{
|
|
DEBUG_BREAK();
|
|
}
|
|
|
|
int selection = random->nextInt(totalWeight);
|
|
for( unsigned int i = 0; i < items.length; i++ )
|
|
{
|
|
selection -= items[i]->randomWeight;
|
|
if (selection < 0)
|
|
{
|
|
return items[i];
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
|
|
WeighedRandomItem *WeighedRandom::getRandomItem(Random *random, WeighedRandomItemArray items)
|
|
{
|
|
return getRandomItem(random, items, getTotalWeight(items));
|
|
} |