Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

40k Dice Roll Success API, Dark Heresy 2nd Edition Style

1410889690

Edited 1411054101
So with Dark Heresy second edition they changed the RAW formula for successes. In a paraphrase you know get 1 success (or failure) for meeting or exceeding the roll. Then you get a number of additional success or failures based on subtracting the 10s digits of (the roll) and the (target number + Modifiers). The closest I can come, just using macros is this (ex. Melee attack): round(((@{selected|WeaponSkill} + ?{Modifier|0})-1d100)/10) This solves for the additional success or failures, but you will always need to increase the absolute value by 1 for the total, and note if it was a success or failure based on the sign of the results (Positive = Success, Negative = Failure). I am looking to undertake writing and API that will do the full calculation and provide a clean result back to the user. That said, I am very new to roll20 and while I have coded in several other languages I am rapidly learning Javascript now just for this purpose. I thought I would post here to see how others, if interested, might code this for roll20. Desired Call: !roll40k tokenName, attribute, modifer or !roll40k tokenName, attribute //Modifer will be acquired from user as part of the API Desired Output: I am looking to make the final output something like this: ' Jack 's target number is 45 and they rolled a 32 getting 2 success(es) !' or ' Jack 's target number is 45 and they rolled a 84 getting 5 failure(s) !' "[Token Name]'s target number is [baseTarget + totalMod] and they rolled a [rollResult] getting [numOfSuccess] [resultType]!' - rollResult = 1d100 - Parameters should include the token making the roll, and the attribute that would make up the baseTarget number. - The modifier could either be a passed parameter or be generated in the API, but should ask the user to enter it. Advanced: 100 is an automatic failures, while 1 is an automatic success so eventually this would account for those two and ensure that the right number of failures and successes are calculated even if one of those results in the normal calculation would prove otherwise. I have coded this for another tool (gasp) once before and here is my code: [h: attackType = "Melee, Ranged"] [h: actionVal = 0] //Prompt user to select if this is a Melee attack or Ranged attack //Prompt user to check if they are attacking a vehicle, used to determine hit results [h: status = input( "attack|"+ attackType +"|Attack Type|RADIO|ORIENT=H SELECT=1", "vehicle|0|Attacking a vehicle|CHECK")] [h: abort(status)] [h, if(attack == 0), CODE: { [attackChar = getProperty("Weapon Skill")] [attackDesc = "Weapon Skill"] [outputAttack = "Melee"] }; { [attackChar = getProperty("Ballistic Skill")] [attackDesc = "Ballistic Skill"] [outputAttack = "Ranged"] }] [h, if(vehicle == 1), CODE: { [locTypeList = "Hull, Side, Rear"] }; { [locTypeList = "Arm, Body, Head, Leg"] }] //Prompts user to input a modifier for the roll //Allows user to call a shot to a specific location on target [h: status = input( "junkVar||"+ outputAttack +" Attack|LABEL|TEXT=FALSE", "mod|0|Enter Modifier", "tgt|"+ attackChar +"|"+ attackDesc, "callShot|"+ locTypeList +"|Called Shot Location|LIST|VALUE=STRING")] [h: abort(status)] [h: roll = 1d100] [h: modifier = mod] [h: modTgt = tgt + mod] [h: output1 = "You rolled a <B>"+ Roll +"</B> with a modified target of <B>"+ modTgt +"</B>.<BR>"] //generate output results [h: output3 = ""] [h, switch(Roll), CODE: case 1: { [autoRoll = 1] }; case 100: { [autoRoll = 100] }; default: { [autoRoll = 0] }] [h,if(Roll <= modTgt),CODE: { [h, if(autoRoll == 100), CODE: { [output2 = "<FONT COLOR=RED>You automatically fail by <B>1 degree</B>.</FONT>"] }; { [variance = modTgt - Roll] [DegOfSuc = floor(variance/10)+1] [output2 = "<FONT COLOR=GREEN>You succeed by <B>"+ DegOfSuc +" degree(s)</B>.</FONT>"] [if(actionVal == 0): output3 = "Your <b>"+ outputAttack +"</b> attack strikes your target's <b>"+ hitLoc(Roll,vehicle) +"</b>."] }] }; { [h, if(autoRoll == 1), CODE: { [output2 = "<FONT COLOR=Green>You automatically succeed by <B>1 degree</B>.</FONT>"] }; { [variance = Roll - modTgt] [DegOfSuc = floor(variance/10)+1] [output2 = "<FONT COLOR=RED>You fail by <B>"+ DegOfSuc +" degree(s)</B>.</FONT>"] }] }] [h: output = "<p>"+ output1 + output2 + output3 +"</p>"] [h: macro.return = output]
So here is what I came up with, after exploring, learning, and borrowing some code. However I am getting and Unexpected token ; error that I can't seem to figure out. /** Rolls a d100 and calculates the success or fail results to the chat window. **/ var rollResult = function(token, attribute, modifier) { var roll = randomInteger(100); var modTarget = modifier + attribute; var output1 = "You rolled a " + roll + " with a modified target of " + modTarget ". "; var output2 = ""; var output3 = ""; var autoRoll; var degOfSuc; /** Check for an automatic result **/ switch (roll) { case 1: autoRoll = 1; break; case 100: autoRoll = 100; break; default: autoRoll = 0; } if(roll <= modTarget) { if(autoRoll === 100){ output2 = "You automatically fail by 1 degree. " } degOfSuc = (Math.floor(modTarget/10) - Math.Floor(roll/10)) + 1; output2 = "You succeed by " + degOfSuc + " degree(s). "; } else { if(autoRoll === 1) { output2 = "You automatically succeed by 1 degree. "; } degOfSuc = (Math.floor(roll/10) - Math.Floor(modTarget/10)) + 1; output2 = "You fail by " + degOfSuc + " degree(s). "; } var output = output1 + output2 + output3; sendChat(msg.who, "/w " + msg.who + output); } /** Interpret the chat commands. **/ on("chat:message", function(msg) { var cmdName; var msgTxt; cmdName = "!roll40k "; msgTxt = msg.content; if(msg.type == "api" && msgTxt.indexOf(cmdName) !== -1) { var paramList = msgTxt.slice(cmdName.length); if(paramList.indexOf(",") == -1) { sendChat(msg.who, "/w " + msg.who + " must specify three comma-separated parameters for !roll40k command."); } else { var paramArray = paramList.split(","); var curToken = trimString(paramArray[0]); var attribute = trimString(paramArray[1]); var modifier = trimString(paramArray[2]); rollResult(curToken, attribute, modifier); } } }); /** Trims a string **/ var trimString = function(src) { return src.replace(/^\s+|\s+$/g, ''); }
1410986927
The Aaron
Roll20 Production Team
API Scripter
You need a + after modTarget on line 7: var output1 = "You rolled a " + roll + " with a modified target of " + modTarget ". "; becomes: var output1 = "You rolled a " + roll + " with a modified target of " + modTarget + ". ";
I seem to have figured out the issues. Here is the working code. I use this Macro statement (for a melee attack), after selecting an icon to execute it: !roll40k @{selected|token_name},@{selected|WeaponSkill},?{Modifer|0} /** * This script rolls a d100 and computes and outputs the success results based * on Dark Heresy Second Edition RPG criteria. * * The following commands is used: * !roll40k [tokenName], [attributeValue], [ModifierValue] **/ //Rolls a d100 and calculates the success or fail results to the chat window. var rollResult = function(token, attribute, modifier) { var roll = randomInteger(100); var modTarget = parseInt(attribute) + parseInt(modifier); var output1 = token + " has a modified target of <B>" + modTarget + "</B> and rolled a <B>" + roll + "</B>. "; var output2 = ""; var output3 = ""; var autoRoll; var degOfSuc; //Check for an automatic result switch (roll) { case 1: autoRoll = 1; break; case 100: autoRoll = 100; break; default: autoRoll = 0; } //Form output message based on result if(roll <= modTarget) { if(autoRoll === 100){ output2 = token + " rolled a 100 and automatically <I>fails</I> by <B>1 degree</B>. " output2 = output2.fontcolor("red"); } degOfSuc = (Math.floor(modTarget/10) - Math.floor(roll/10)) + 1; output2 = token + " <I>succeeds</I> by <B>" + degOfSuc + " degree(s)</B>. "; output2 = output2.fontcolor("green"); } else { if(autoRoll === 1) { output2 = token + " rolled a 1 and automatically <I>succeeds</I> by <B>1 degree</B>. "; output2 = output2.fontcolor("green"); } degOfSuc = (Math.floor(roll/10) - Math.floor(modTarget/10)) + 1; output2 = token + " <I>fails</I> by <B>" + degOfSuc + " degree(s)</B>. "; output2 = output2.fontcolor("red"); } //Return output var output = output1 + output2 + output3; return output; } /** Interpret the chat commands. **/ on("chat:message", function(msg) { var cmdName; var msgTxt; cmdName = "!roll40k "; msgTxt = msg.content; if(msg.type == "api" && msgTxt.indexOf(cmdName) !== -1) { var paramList = msgTxt.slice(cmdName.length); if(paramList.indexOf(",") == -1) { sendChat(msg.who, "/w " + msg.who + " must specify three comma-separated parameters for !roll40k command."); } else { var paramArray = paramList.split(","); var curToken = trimString(paramArray[0]); var attribute = trimString(paramArray[1]); var modifier = trimString(paramArray[2]); var result = rollResult(curToken, attribute, modifier); sendChat(msg.who, result); } } }); /** Trims a string **/ var trimString = function(src) { return src.replace(/^\s+|\s+$/g, ''); }
Thanks Aaron! I did end up finding that... and that I had capitalized Floor in my Math.floor() function. Its working like a champ now.
So, I am guessing that we do not have a way to COLOR the output from an API script? The above code attempts to use the fontcolor() method but it does not seem to have an effect.
I will be adding a location hit to the code next.
1410989866
The Aaron
Roll20 Production Team
API Scripter
If you output HTML/CSS, it will be colored: output2 = token + ' rolled a 100 and automatically <span style="color: #ff3355;">fails</span> by <B>1 degree</B>. ' Be sure to use different types of quotes or escape them with \ where needed.
I did originally try a <FONT COLOR=RED></FONT>, but that did not work. I will give SPAN a shot.
1411054168

Edited 1411054310
Okay now to figure out Github.... here we go, the latest code: Updated output format to include color and line breaks. Code reworked to be consistant with the use of single quotes rather than double quotes for text. Code moved into Github Roll script for Dark Heresy 2nd Edition