Skip to content

Instantly share code, notes, and snippets.

@victusfate
Last active April 20, 2018 14:33
Show Gist options
  • Save victusfate/e0eb00ddcfd6b83d3fe8a0785e10fb34 to your computer and use it in GitHub Desktop.
Save victusfate/e0eb00ddcfd6b83d3fe8a0785e10fb34 to your computer and use it in GitHub Desktop.
AD&D 1e magic item stats reroller (sample)
function btw(start,val,end) {
return val >= start && val <= end;
}
function randomInteger(val) {
return Math.floor(Math.random()*val)+1;
}
function SwordExtraordinaryPower() {
return "Awesome";
}
function SwordPrimaryPowerTableResult()
{
let d100 = randomInteger(100);
let powerResult = "";
let nRerolls = 0;
switch(true)
{
case(btw(1,d100,10)):
powerResult = "Detect Evil";
break;
case(btw(11,d100,15)):
powerResult = "Detect Gems";
break;
case(btw(16,d100,25)):
powerResult = "Detect Magic";
break;
case(btw(26,d100,35)):
powerResult = "Detect Metal";
break;
case(btw(36,d100,50)):
powerResult = "Detect Shifting Walls and Rooms";
break;
case(btw(51,d100,65)):
powerResult = "Detect Sloping Passages";
break;
case(btw(66,d100,75)):
powerResult = "Find Secret Doors";
break;
case(btw(76,d100,85)):
powerResult = "Find Traps";
break;
case(btw(86,d100,95)):
powerResult = "See Invisible";
break;
case(btw(96,d100,99)):
powerResult = SwordExtraordinaryPower();
break;
case(btw(100,d100,100)):
powerResult = "Multi";
nRerolls = 2;
break;
default:
break;
}
return { powerResult: powerResult, nRerolls: nRerolls }
}
function SwordPrimaryPower(aResults = [],nRerolls = 0)
{
let powerResult = "";
let powerResultArray = [];
let bDone = false;
while (!bDone) {
const oResult = SwordPrimaryPowerTableResult();
if (!aResults.includes(oResult.powerResult)) {
bDone = true;
if (oResult.nRerolls > 0) {
nRerolls = oResult.nRerolls;
}
// handle rerolls
if (nRerolls > 0) {
if (oResult.powerResult !== 'Multi') {
nRerolls--;
}
aResults.push(oResult.powerResult);
return SwordPrimaryPower(aResults,nRerolls)
}
else {
aResults.push(oResult.powerResult);
if (aResults[0] == 'Multi') {
aResults.shift();
}
powerResult = aResults.join(', ');
}
}
}
return powerResult;
}
for (let i = 0;i < 1000;i++) {
let sPowers = SwordPrimaryPower();
console.log('completed roll',sPowers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment