Skip to content

Instantly share code, notes, and snippets.

@Geritz
Last active July 1, 2021 03:43
Show Gist options
  • Save Geritz/f2474a55d19a9d61f038cb1dcf54ed14 to your computer and use it in GitHub Desktop.
Save Geritz/f2474a55d19a9d61f038cb1dcf54ed14 to your computer and use it in GitHub Desktop.
A mob damage calculator for DND 5th edition on Roll20.
/*
AutoMob5e.js by Spencer Moran. MIT License
This program automatically calculates mob combat attacks
and damage based on a few input values, complete with a
handy query macro to speed things along. You can modify the
generated macro if you want to save time with some default
values.
Intended for use with DND 5e.
Commands:
1. !automob <ac> <attack bonus> <creature count> <avg damage> <multiattack>
Example: !automob 14 5 8 4 2
2. !automob_getmacro
3. !autoMob_help
*/
on('chat:message', function(msg){
"use strict";
if('api' !== msg.type) {
return;
}
var args = msg.content.split(/\s+/);
if (args[0] == '!automob')
{
let ac = parseInt(args[1]);
let attackBonus = parseInt(args[2]);
let creaturesNeeded = automob_getReqCreatures(ac-attackBonus);
let numCreatures = parseInt(args[3]);
let numHits = Math.floor(parseInt(args[3])/creaturesNeeded);
let avgDam = parseInt(args[4]);
let atkNum = parseInt(args[5]);
let atkDam = parseInt(args[4]) * parseInt(args[5]);
let totalDam = atkDam * numHits;
sendChat("AutoMob5e",'&{template:default} {{name=Mob Attack}} {{Target AC='+ac+'}}{{Attack bonus='+attackBonus+'}}{{Creatures needed for 1 hit='+creaturesNeeded+'}}{{Number of creatures='+numCreatures+'}}{{Number of creatures that hit='+numHits+'}}{{Avg damage per hit='+avgDam+'}}{{Number of attacks per creature='+atkNum+'}}{{Attack damage per creature hit='+atkDam+'}}{{Total damage from all hits='+totalDam+'}}');
} else if (args[0] == '!automob_getmacro'){
automob_getMacro(msg.playerid);
} else if (args[0] == '!automob_help'){
sendChat("AutoMob5e", '&{template:default} {{name=AutoMob Commands}} {{ !automob (ac) (atkBonus) (creatures) (avgDamage) (atkCount) }} {{ !automob_getmacro }}')
} else{
return;
}
});
function automob_getReqCreatures(num){
if(num <= 5){
return 1;
}
else if (num <= 12){
return 2;
}
else if (num <= 14){
return 3;
}
else if (num <= 16){
return 4;
}
else if (num <= 18){
return 5;
}
else if (num == 19){
return 10;
}else{
return 20;
}
}
function automob_getMacro(id){
createObj('macro', {
_playerid: id,
name: "Query-Mob-Attack",
action: "!automob ?{Target AC|} ?{Attack Bonus|} ?{Number of Attackers|} ?{Average Damage of One Hit|} ?{Number of attacks|}",
visibleto: 'all',
istokenaction: false
});
sendChat("AutoMob", "Macro added. Look for 'Query-Mob-Attack' in the macro tab");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment