MinecraftConsoles/Minecraft.World/WeighedRandom.cpp
ModMaker101 585455cef0
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
Replace MSVC __debugbreak with cross-compiler DEBUG_BREAK macro. (#1540)
2026-04-26 12:23:52 -05:00

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));
}