Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save damccull/f58e8594027e557e533bef57528d7507 to your computer and use it in GitHub Desktop.
Save damccull/f58e8594027e557e533bef57528d7507 to your computer and use it in GitHub Desktop.
FoundryVTT script to roll 4d6, rerolling all 1s and 2s infinitely, then discarding the lowest die
//Create a new roll for 4d6 and roll it
let roll = new Roll('4d6').roll();
//Convert to json
let rjson = roll.toJSON();
//Manually set properties
rjson.formula = "4d6r<=2dl1";
rjson.dice[0].formula = rjson.formula;
//Create a new empty array
let newrolls = Array();
//Reroll 1s and 2s
rjson.dice[0].rolls.forEach(r => {
//Loop all individual dice results and modify them if necessary
if (r.roll < 3) {
//Set die to rerolled
r.rerolled = true;
//infinitely roll dice until it's not a 1 or 2
let rnewval = 0;
while (rnewval < 3) {
console.log("rerolling");
rnewval = new Roll('1d6').roll().total;
}
//Add the new die to the newrolls array
newrolls.push({
"roll": rnewval
});
}
});
//Combine existing rolls and new rolls
rjson.dice[0].rolls = rjson.dice[0].rolls.concat(newrolls);
//Find lowest active die and drop it
let lowestValueIndex = 0;
rjson.dice[0].rolls.forEach((r, index, arr) => {
if(!is_active(r) && lowestValueIndex == index) {
lowestValueIndex++;
}
else if (is_active(r) && r.roll < arr[lowestValueIndex].roll) {
lowestValueIndex = index;
}
});
//drop the lowest die
rjson.dice[0].rolls[lowestValueIndex].discarded = true;
//Total up new rolls for active dice only
let newtotal = 0;
rjson.dice[0].rolls.forEach(r => {
if (is_active(r)) {
newtotal += r.roll;
}
});
//Set new totals
rjson.result = newtotal.toString();
rjson.total = newtotal;
//Recreate roll object from json
roll = Roll.fromData(rjson);
//Send roll to chat
roll.toMessage();
function is_active(r) {
if(!r.hasOwnProperty('rerolled') && !r.hasOwnProperty('discarded')) {
return true;
}
return false;
}
//Create a new roll for 4d6 and roll it
let roll = new Roll('4d6').roll();
//Convert to json
let rjson = roll.toJSON();
//Manually set properties
rjson.formula = "4d6r<=2dl1";
rjson.terms[0].modifers = ["r<=2","dl1"];
//Create a new empty array
let newrolls = Array();
//Reroll 1s and 2s
rjson.terms[0].results.forEach(r => {
//Loop all individual dice results and modify them if necessary
if (r.result < 3) {
//Set die to inactive and rerolled
r.active = false;
r.rerolled = true;
//infinitely roll dice until it's not a 1 or 2
let rnewval = 0;
while (rnewval < 3) {
console.log("rerolling");
rnewval = new Roll('1d6').roll().total;
}
//Add the new die to the newrolls array
newrolls.push({
"result": rnewval,
"active": true
});
}
});
//Combine existing rolls and new rolls
rjson.terms[0].results = rjson.terms[0].results.concat(newrolls);
//Find lowest active die and drop it
let lowestValueIndex = 0;
rjson.terms[0].results.forEach((r, index, arr) => {
if(!r.active && lowestValueIndex == index) {
lowestValueIndex++;
}
else if (r.active && r.result < arr[lowestValueIndex].result) {
lowestValueIndex = index;
}
});
//drop the lowest die
rjson.terms[0].results[lowestValueIndex].active = false;
rjson.terms[0].results[lowestValueIndex].discarded = true;
//Total up new rolls for active dice only
let newtotal = 0;
rjson.terms[0].results.forEach(r => {
if (r.active) {
newtotal += r.result;
}
});
//Set new totals
rjson.results = [newtotal];
rjson.total = newtotal;
//Recreate roll object from json
roll = Roll.fromData(rjson);
//Send roll to chat
roll.toMessage();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment