Skip to content

Instantly share code, notes, and snippets.

@sturtus
Last active January 2, 2016 21:28
Show Gist options
  • Save sturtus/8363191 to your computer and use it in GitHub Desktop.
Save sturtus/8363191 to your computer and use it in GitHub Desktop.
Roll20 DCC Wizard Spell script
/*
Wizard spell script for Dungeon Crawl Classics in Roll20. Calculates the target for the spell check, checks for spell loss.
Requires my Roll20 Library script. https://gist.github.com/sturtus/8352577
To use, the character must have the attributes defined in the config portion of the script.
- ActionDie: the die to roll for the spell check, as a d20 or d16 or whatever.
- CasterLevel: level of the spellcaster
- List of attributes to possibly affect the roll, expressed as bonuses such as +1 or -1 or 0. In my campaign we use 3 letter abbreviations for these like STR, AGI, PER, LCK, and INT. Change the array to match your campaign.
To call the script do this:
!wizardSpell Spell Name|Level of spell|list of attributes or bonuses separated by commas
for example:
!wizardSpell Charm Person|1|INT,+1
*/
function wizardSpell(characterObj, attributeObjArray, spellName, spellLevel, spellModArray) {
//finally assign the variables for output.
var characterName = characterObj.get("name");
var actionDieValue = attributeObjArray[0].get("current");
var spellTarget = 10+(2*Number(spellLevel));
// get the action die max value and die roll, as expressed as 1d20 or d5 or whatever in the current value of the attribute.
var d = actionDieValue.indexOf("d")+1;
var actionDieMax = parseInt(actionDieValue.slice(d));
var actionDieResult = randomInteger(actionDieMax);
var spellRoll = Number(actionDieResult);
//get the values in spellModArray, return current numbers if attributes and numbers if numbers
var spellMods = spellModArray;
for (var i = 2; i < attributeObjArray.length; i++) {
for (var j = 0; j < spellModArray.length; j++) {
if (attributeObjArray[i].get("name") == spellModArray[j]) {
spellMods[j] = attributeObjArray[i].get("current");
};
};
};
//clean up any + symbols
spellMods = removePlus(spellMods);
//build results and send to chat
var spellChatString = spellName + ": [[" + actionDieResult;
if (spellModArray[0] != "None") {
for (var i = 0; i < spellMods.length; i++) {
if (spellMods[i].indexOf("+") !== -1) {
var p = spellMods[i].indexOf("+")+1;
spellMods[i] = parseInt(spellMods[i].slice(p));
}
spellChatString = spellChatString.concat(" +", spellMods[i]);
spellRoll = spellRoll+Number(spellMods[i]);
};
};
spellChatString = spellChatString.concat(" ]]");
sendChat(characterName,spellChatString);
// spell fails if spellRoll is < (10 + (2*spellLevel))
// 1 = Lost, failure, and worse!
// 2-11 = Lost. Failure.
// 12+ = If spellRoll < spellTarget && spellRoll is >= 12,
// Failure, but spell is not lost.
if (spellRoll < spellTarget) {
if (spellRoll == 1) {
sendChat("", "/desc Lost, failure, and worse!");
};
if ((spellRoll >= 2) && (spellRoll <= 11)) {
sendChat("", "/desc Lost. Failure.");
};
if (spellRoll > 12) {
sendChat("", "/desc Failure, but spell is not lost.");
};
};
};
on("chat:message", function(msg) {
if (msg.type == "api" && msg.content.indexOf("!wizardSpell ") !== -1) {
//parse the input into two variables, oAttrib and newValue
var selected = msg.selected;
var attributeArray = ["ActionDie", "CasterLevel", "INT"];
var param = msg.content.split("!wizardSpell ")[1];
var spellName = param.split("|")[0];
var spellLevel = param.split("|")[1];
var spellMod = param.split("|")[2];
var spellModArray = spellMod.split(",");
if(!selected) {
sendChat("", "/desc Select token and try again.");
return; //quit if nothing selected
};
//loop through selected tokens
_.each(selected, function(obj) {
var characterObj = getCharacterObj(obj);
if (characterObj == false) return;
var attributeObjArray = getAttributeObjects(characterObj, attributeArray);
if (attributeObjArray == false) return;
wizardSpell(characterObj, attributeObjArray, spellName, spellLevel, spellModArray);
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment