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

Need help with a script for a Mutants and Masterminds campaign

Unfortunately the only coding I know (very well) is for C++ / C# / XNA and its not translating well here... So... I am trying to write a script that does the following: Display in the chat window that a player wishes to use a hero point The script would check the last roll formula used in the chat window The script would then keep the static values, but would re-roll the D20, Then it would perform an if/then, if the dice roll is equal to or less than 10, add 10 to the die roll. Re-add the new die roll to the previous static values. So, if I had to pseudo-code it: cout > "(player name) uses a hero point to re-roll."/l; get previous roll static values; dieTotal = /roll 1d20; if (dieTotal <= 10) { dieTotal = dieTotal + 10; } newTotal = (dieTotal + staticValues); cout > "Player's hero point re-roll total was " + newTotal + "!"/l;
1401945756

Edited 1401945900
Lithl
Pro
Sheet Author
API Scripter
The following code is untested. What should happen: when a player uses the command "!hero", the system will spend a hero point on their character and perform a re-roll of the last roll made by that character, whether it was with the /roll command or with an [[inline]] roll. If the character used multiple inline rolls in a single chat message, the last one in the message will be used. The portion of the script recording the rolls doesn't explicitly make the assumption that the roll is in the form 1d20+mod; rather, it takes the total of the roll and subtracts the value of all dice involved in the roll. So, if the player did 1d8+1d6+1d4+10+1, the script would subtract the results of the d8/d6/d4 and end up with 11. This part is the most likely place where an error will occur, particularly if a player gets into some of the stranger dice roll syntax; the dice engine is complicated. (Again, untested code.) This script assumes hero points are stored in an attribute named "Hero Points". Change the heroPoints = findObjs(...) call if that's not the case. on('chat:message', function(msg) { if(!(msg.type == 'api' && msg.content.toLowerCase() == '!hero')) return; var character = findObjs({ type: 'character', name: msg.who })[0]; var player = findObjs({ type: 'player', displayname: msg.who.indexOf(' (GM)') > -1 ? msg.who.substring(0, msg.who.indexOf(' (GM)')) : msg.who })[0]; if (!character) { // If the user didn't post the message speaking as a character, pick the first character they control character = findObjs({ type: 'character', controlledby: player.id })[0]; if (!character) { sendChat('SYSTEM', 'Could not find character for ' + msg.who); return; } } var heroPoints = findObjs({ type: 'attribute', characterid: character.id, name: 'Hero Points' })[0]; if (!heroPoints) { // We must have the hero points attribute to function sendChat('SYSTEM', 'Could not find hero points for ' + character.get('name')); return; } if (parseInt(heroPoints.get('current')) < 1) { // Character must have a hero point to spend sendChat('SYSTEM', character.get('name') + ' does not have any hero points to spend'); return; } if (!state.henry_w) { // We need a previous roll to compare to state.henry_w = { Mods: {} }; sendChat('SYSTEM', character.get('name') + ' has not made any recorded rolls'); return; } if (!state.henry_w.Mods[character.id]) { // We need *this character* to have a previous roll sendChat('SYSTEM', character.get('name') + ' has not made any recorded rolls'); return; } // Now we're ready to spend that hero point! sendChat('character|'+character.id, '/me uses a hero point to re-roll.'); var lastRollMod = state.henry_w.Mods[character.id]; var dieTotal = randomInteger(20); dieTotal = dieTotal <= 10 ? dieTotal + 10 : dieTotal; heroPoints.set('current', parseInt(heroPoints.get('current')) - 1); sendChat('SYSTEM', character.get('name') + '\'s hero point re-roll was ' + (dieTotal + lastRollMod) + '!'); }); on('chat:message', function(msg) { if (!(msg.type == 'rollresult' || msg.inlinerolls)) return; var character = findObjs({ type: 'character', name: msg.who })[0]; var player = findObjs({ type: 'player', displayname: msg.who.indexOf(' (GM)') > -1 ? msg.who.substring(0, msg.who.indexOf(' (GM)')) : msg.who })[0]; if (!character) { // If the user didn't post the message speaking as a character, pick the first character they control character = findObjs({ type: 'character', controlledby: player.id })[0]; if (!character) { // Fail silently return; } } if (!state.henry_w) { state.henry_w = { Mods: {} }; } var mod, roll; if (msg.type == 'rollresult') { roll = JSON.parse(msg.content); mod = roll.total; _.each(roll.rolls, function(r) { _.each(r.results, function(d) { mod -= d.v; }); }); } else { roll = msg.inlinerolls[msg.inlinerolls.length - 1]; mod = roll.results.total; _.each(roll.results.rolls, function(r) { _.each(r.results, function(d) { mod -= d.v }); }); } state.henry_w.Mods[character.id] = mod; });
Tested it out, it seems to work great! Thank you very much! The dice thing you did was great because if a game uses a similar system to M&M it could be used in that, M&M doesn't use any dice other than the traditional D20 though. One question, is there a way to show if the re-rolled die (after the die's total has been decided) was a 20 before modifiers? In M&M a reroll of a natural 20, or a 10 (which is upgraded to a 20) is rolled then the character critically succeeds is why I ask.
Secondary note: I was able to modify this to also work with ranks of the Luck advantage for M&M so they can do !hero for hero point re-rolls and !luck for luck re-rolls. I can't thank you enough! These work splendidly! Thank you again.
If it is possible to get one more out of you? Just a base-line for me to build off of? Just a script that lets me click on a token and then add to a named attribute on the linked sheet? If I can get one that does that, I can modify it to control damage pips, and add hero points to tokens, etc etc