Character Vault
Any Concept / Any System
Compendium
Your System Come To Life
Roll20 for Android
Streamlined for your Tablet
Roll20 for iPad
Streamlined for your Tablet

Personal tools

Difference between revisions of "Script:Savage Worlds - Raise Count"

From Roll20 Wiki

Jump to: navigation, search
Line 1: Line 1:
The following script creates the API command <code>!rc</code>, which rolls dice and counts the number of raises for the Savage Worlds game system.
+
{{script overview
 +
|name=Raise Count
 +
|author={{user profile|235259|Brian}}
 +
|version=2.0
 +
|lastmodified=2015-01-08}}
  
==== Syntax ====
+
'''Raise Count''' creates the API command <code>!rc</code>, which rolls dice and counts the number of raises for the [[Savage Worlds]] game system.
<blockquote style="border:1px #0088ee solid;background:#eee;padding:0.5em">!rc ''roll''|''target''</blockquote>
+
<br clear="all">
  
{| class="wikitable"
+
=== Syntax ===
|-
+
{{syntaxbox top|Raise Count|formal=true}}
! Parameter
+
{{API command|rc}} {{API parameter|name=roll}} {{API parameter|name=target}}
! Values
+
{{Formal API command|
|-
+
{{token|S}} {{rarr}} {{API command|rc}} {{token|dice expression}} {{integer|-}}
| ''roll''
+
{{dice expression}}
| A dice roll, using Roll20's normal syntax. Do '''not''' include the <code>/r</code> command or inline brackets (<nowiki>[[ and ]]</nowiki>).
+
}}
|-
+
{{syntaxbox end}}
| ''target''
+
| The target number for the roll. The number of raises will be calculated against this target.
+
|}
+
  
==== Code ====
+
{{param description top}}
<pre data-language="javascript">
+
{{param description|name=roll|value=A dice roll, using Roll20's normal syntax. Do '''not''' include the <code>/r</code> command or inline brackets (<nowiki>[[ and ]]</nowiki>).}}
var raise_count = raise_count || {};
+
{{param description|name=target|value=The target number for the roll. The number of raises will be calculated against this target.}}
 +
{{param description bottom}}
  
// Raise increment; generalized in case there are cases where it isn't 4
+
=== Installation ===
raise_count.RAISE_SIZE = 4;
+
The format of the output can be customized by altering <code>config.outputFormat</code>, found near the top of the script. You may use the placeholders <code>{0}</code>, <code>{1}</code>, and <code>{2}</code> in the <code>outputFormat</code> string, which will become an inline roll, the value of ''target'', and the number of raises, respectively.
// Output formatting. %1$s will be replaced with an inline roll. %2$s will be replaced by the user's target input.
+
// %3$s will be replaced by the number of raises resulting the from roll. change this string if you want the results
+
// to show up differently in chat.
+
raise_count.OUTPUT_FORMAT = 'Roll: %1$s, Target: %2$s, Raises: %3$s';
+
  
on('chat:message', function(msg) {
+
You can change the size of a raise use to calculate the number of raises by altering the value of <code>config.raiseSize</code>.
    if(msg.type != 'api' || msg.content.indexOf('!rc ')) return;
+
   
+
    var roll = msg.content.substring(4, msg.content.indexOf('|'));
+
    var target = msg.content.substring(msg.content.indexOf('|')+1);
+
   
+
    var sendAs = 'system';
+
    var character = findObjs({_type: 'character', name: msg.who})[0];
+
    if(character) sendAs = 'character|'+character.id;
+
    else sendAs = 'player|'+msg.playerid;
+
   
+
    sendChat(sendAs, '[['+roll+']]', function(fmsg) {
+
        var expression = fmsg[0].inlinerolls['1'].expression;
+
        var total = fmsg[0].inlinerolls['1'].results.total;
+
        var raises = Math.floor((total - target) / raise_count.RAISE_SIZE);
+
       
+
        var rollOut = '<span title="Rolling '+expression+' = ';
+
        var fail = crit = false;
+
        for(var i in fmsg[0].inlinerolls['1'].results.rolls)
+
        {
+
            var r = fmsg[0].inlinerolls['1'].results.rolls[i];
+
            if(r['type'] != 'R') continue;
+
           
+
            rollOut += '(';
+
            var max = r['sides'];
+
           
+
            for(var k = 0; k < r['results'].length; k++)
+
            {
+
                var value = r['results'][k]['v'];
+
                crit = crit || (value == max);
+
                fail = fail || (value == 1);
+
                rollOut += '<span class=&quot;basicdiceroll'+(value==max?' critsuccess':(value==1?' critfail':''))+'&quot;>';
+
                rollOut += value+'</span>+';
+
            }
+
            rollOut = rollOut.substring(0,rollOut.length - 1)+')+';
+
        }
+
        rollOut = rollOut.substr(0, rollOut.length - 1);
+
        rollOut += '" class="a inlinerollresult showtip tipsy-n';
+
        rollOut += (crit&&fail?' importantroll':(crit?' fullcrit':(fail?' fullfail':'')))+'">'+total+'</span>';
+
       
+
        var message = '/direct '+raise_count.sprintf(raise_count.OUTPUT_FORMAT, rollOut, target, raises);
+
        sendChat(sendAs, message);
+
    });
+
});
+
 
+
/**
+
* Really really really super naive implementation of the sprintf function,
+
* which will only really work for this script. I should be ashamed for qriting it.
+
*/
+
raise_count.sprintf = function(format, arg1, arg2, arg3)
+
{
+
    var out = format.replace('%1$s', arg1);
+
    out = out.replace('%2$s', arg2);
+
    out = out.replace('%3$s', arg3);
+
    return out;
+
};
+
</pre>
+
 
+
==== Examples ====
+
'''!rc 2d12+d6|?{Target|7}'''
+
<blockquote>This will result in an output such as "Roll: [20], Target: 7, Raises: 3".</blockquote>
+
 
+
==== Note ====
+
On line 8, you will see the variable <code>raise_count.OUTPUT_FORMAT</code>. You can change this string to alter how the script outputs the result.
+
 
+
[[Category:User API Scripts|Raise Count]]
+
[[Category:API Commands|Raise Count]]
+

Revision as of 18:01, 14 January 2015

API ScriptAuthor: Brian
Version: 2.0
Last Modified: 2015-01-08
Code: Raise Count
Dependencies: None
Conflicts: None

Raise Count creates the API command !rc, which rolls dice and counts the number of raises for the Savage Worlds game system.

Syntax

!rc <roll> <target>
Formally:

S

→ !rc dice expression
integer

dice expression

roll


dice expression

dice expression

modifier


dice expression

→ {expression list

}group modifier


dice expression

dice expression
operator
dice expression


dice expression

function

(dice expression

)
roll

math expression

df
roll

math expression

dmath expression


math expression

integer

math expression

→ (math expression
operator
math expression

)
operator

→ +

operator

→ -

operator

→ *

operator

→ /

function

→ floor

function

→ ceil

function

→ round

function

→ abs

modifier

compare


modifier

→ fcompare


modifier

→ !compare


modifier

→ !!compare


modifier

→ !pcompare


modifier

→ rcompare


modifier

→ rocompare


modifier

keep-drop


modifier

→ s

modifier

→ sd

compare

→ ε

compare

integer

compare

→ >integer

compare

→ <integer

keep-drop

keep


keep-drop

keep

integer
keep-drop

drop


keep-drop

drop

integer
keep

→ k

keep

→ kh

keep

→ kl

drop

→ d

drop

→ dh

drop

→ dl

expression list

dice expression

, rest expression list


rest expression list

dice expression


rest expression list

expression list


group modifier

keep-drop


group modifier

compare


group modifier

→ fcompare


Parameter Values
roll A dice roll, using Roll20's normal syntax. Do not include the /r command or inline brackets ([[ and ]]).
target The target number for the roll. The number of raises will be calculated against this target.

Installation

The format of the output can be customized by altering config.outputFormat, found near the top of the script. You may use the placeholders {0}, {1}, and {2} in the outputFormat string, which will become an inline roll, the value of target, and the number of raises, respectively.

You can change the size of a raise use to calculate the number of raises by altering the value of config.raiseSize.