Skip to content

Instantly share code, notes, and snippets.

@AcrylicShrimp
Last active February 27, 2020 07:59
Show Gist options
  • Save AcrylicShrimp/82bce2228769a53b95ae41c985efe76e to your computer and use it in GitHub Desktop.
Save AcrylicShrimp/82bce2228769a53b95ae41c985efe76e to your computer and use it in GitHub Desktop.
Implementing gotcha system.
const itemArray = [
['banana-1000', 0.65],
['choco-960', 0.65],
['back-0', 13.7 / 16],
['back-1', 13.7 / 16],
['back-2', 13.7 / 16],
['char-0', 13.7 / 16],
['char-1', 13.7 / 16],
['char-2', 13.7 / 16],
['char-3', 13.7 / 16],
['char-4', 13.7 / 16],
['char-5', 13.7 / 16],
['char-6', 13.7 / 16],
['char-7', 13.7 / 16],
['char-8', 13.7 / 16],
['char-9', 13.7 / 16],
['char-10', 13.7 / 16],
['char-11', 13.7 / 16],
['char-12', 13.7 / 16],
['jam-500', 2],
['jam-300', 6],
['jam-100', 22],
['jam-50', 25],
['', 30]
];
const itemProbArray = itemArray.map(value => value[1]);
itemProbArray.reduce((acc, cur, i, src) => (src[i] = acc + cur));
// ...
const mt = require('mersenne-twister');
// Sample item here;
const pivot =
new mt().random() *
itemProbArray[itemProbArray.length - 1];
let item = '';
for (let index = 0; index < itemProbArray.length; ++index)
if (pivot <= itemProbArray[index]) {
item = itemArray[index][0];
break;
}
print(item);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment