Skip to content

Instantly share code, notes, and snippets.

Created April 21, 2015 21:02
Show Gist options
  • Save anonymous/fcd266e17d903e1fd8e3 to your computer and use it in GitHub Desktop.
Save anonymous/fcd266e17d903e1fd8e3 to your computer and use it in GitHub Desktop.
A script for the roll20 api to pick a random encounter.
// A shorthand to create new events in the encounters table.
var event = function(freq, msg) {
return {message: msg, frequency: freq};
};
// The encounter table. Create a new section and populate it with events. The
// event text will be displayed in the GM chat window if it is selected.
// Events with a frequency of '3' will happen 3 times as often as events with
// a frequency of '1'.
var encounters = {
forest: [
event(1, "[[5d20]] dragons swoop down from the sky"),
event(2, "[[1d4]] goblins scouts are hidden in a bush"),
event(3, "a [[3d9]] large goblin hunty party is returning (injured) from a hunt"),
event(2, "A meteor crashes down next to the players"),
event(1, "everyone dies.")
],
desert: [
event(100, "A plain cactus blocks your path."),
event(1, "You find drinkable water."),
]
};
// A shorthand to whisper stuff to the GM>
var say = function(msg) {
sendChat("Table", "/w gm " + msg);
}
// Pick a random event from an array of events, respecting the weights
// of the events.
var resolve = function(table) {
var total = 0;
for (i in table) {
total += table[i].frequency;
};
var roll = randomInteger(total);
log("total is " + total + " roll " + roll);
for (i in table) {
roll -= table[i].frequency;
if (roll <= 0) {
return table[i].message;
}
}
return "something bad happened";
};
on("chat:message", function(msg) {
if (msg.type != "api" || msg.content.indexOf("!enc-") !== 0) {
return;
}
var loc = msg.content.replace("!enc-", "");
if (!(loc in encounters)) {
var choices = []
for (var c in encounters) {
choices.push("!enc-" + c);
}
say("Couldn't find " + loc + ". Try: " + choices);
return;
}
var result = resolve(encounters[loc]);
say("Rolling for " + loc.toUpperCase() + " random encounter: [[1d20]]. Found: " + result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment