mirror of
https://github.com/smartcmd/MinecraftConsoles.git
synced 2026-05-21 21:53:51 +00:00
Initial commit
This commit is contained in:
parent
def8cb4153
commit
b691c43c44
19437 changed files with 4363922 additions and 0 deletions
72
Minecraft.World/WeighedRandom.cpp
Normal file
72
Minecraft.World/WeighedRandom.cpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include "stdafx.h"
|
||||
#include "WeighedRandom.h"
|
||||
|
||||
int WeighedRandom::getTotalWeight(vector<WeighedRandomItem *> *items)
|
||||
{
|
||||
int totalWeight = 0;
|
||||
for( AUTO_VAR(it, items->begin()); it != items->end(); it++ )
|
||||
{
|
||||
totalWeight += (*it)->randomWeight;
|
||||
}
|
||||
return totalWeight;
|
||||
}
|
||||
|
||||
WeighedRandomItem *WeighedRandom::getRandomItem(Random *random, vector<WeighedRandomItem *> *items, int totalWeight)
|
||||
{
|
||||
if (totalWeight <= 0)
|
||||
{
|
||||
__debugbreak();
|
||||
}
|
||||
|
||||
int selection = random->nextInt(totalWeight);
|
||||
|
||||
for( AUTO_VAR(it, items->begin()); it != items->end(); it++ )
|
||||
{
|
||||
selection -= (*it)->randomWeight;
|
||||
if (selection < 0)
|
||||
{
|
||||
return *it;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
__debugbreak();
|
||||
}
|
||||
|
||||
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 NULL;
|
||||
}
|
||||
|
||||
|
||||
WeighedRandomItem *WeighedRandom::getRandomItem(Random *random, WeighedRandomItemArray items)
|
||||
{
|
||||
return getRandomItem(random, items, getTotalWeight(items));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue