the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 72 lines 1.7 kB view raw
1#include "stdafx.h" 2#include "WeighedRandom.h" 3 4int WeighedRandom::getTotalWeight(vector<WeighedRandomItem *> *items) 5{ 6 int totalWeight = 0; 7 for( AUTO_VAR(it, items->begin()); it != items->end(); it++ ) 8 { 9 totalWeight += (*it)->randomWeight; 10 } 11 return totalWeight; 12} 13 14WeighedRandomItem *WeighedRandom::getRandomItem(Random *random, vector<WeighedRandomItem *> *items, int totalWeight) 15{ 16 if (totalWeight <= 0) 17 { 18 __debugbreak(); 19 } 20 21 int selection = random->nextInt(totalWeight); 22 23 for( AUTO_VAR(it, items->begin()); it != items->end(); it++ ) 24 { 25 selection -= (*it)->randomWeight; 26 if (selection < 0) 27 { 28 return *it; 29 } 30 } 31 return NULL; 32} 33 34WeighedRandomItem *WeighedRandom::getRandomItem(Random *random, vector<WeighedRandomItem *> *items) 35{ 36 return getRandomItem(random, items, getTotalWeight(items)); 37} 38 39int WeighedRandom::getTotalWeight(WeighedRandomItemArray items) 40{ 41 int totalWeight = 0; 42 for( unsigned int i = 0; i < items.length; i++ ) 43 { 44 totalWeight += items[i]->randomWeight; 45 } 46 return totalWeight; 47} 48 49WeighedRandomItem *WeighedRandom::getRandomItem(Random *random, WeighedRandomItemArray items, int totalWeight) 50{ 51 if (totalWeight <= 0) 52 { 53 __debugbreak(); 54 } 55 56 int selection = random->nextInt(totalWeight); 57 for( unsigned int i = 0; i < items.length; i++ ) 58 { 59 selection -= items[i]->randomWeight; 60 if (selection < 0) 61 { 62 return items[i]; 63 } 64 } 65 return NULL; 66} 67 68 69WeighedRandomItem *WeighedRandom::getRandomItem(Random *random, WeighedRandomItemArray items) 70{ 71 return getRandomItem(random, items, getTotalWeight(items)); 72}