Dice roll mod ... again.

Started by Adin, February 02, 2005, 08:49:07 AM

Previous topic - Next topic

Rasyr

Well, I have answered one of my own questions.  :D

I now have the functions built that will allow a person to make open-ended rolls in the Rolemaster (& HARP) style.

Just paste the following code into the rpg.php file
function oeDice($rolls, $sign, $modifier)
{
global $settings;

if ($rolls>50) {return 'Sorry, too many rolls!';}

$output = '<table border="01" cellspacing="0" cellpadding="0"><tr><td><img src="' . $settings['images_url'] . '/dice.gif" alt="" />&nbsp;&nbsp;Rolling the bones!<br />';
$output.='</td></tr><tr><td><table border="0" cellspacing="0" cellpadding="5" align="left" bgcolor="#87ceeb">';

for ($i=1; $i<=$rolls; $i++)
{

$total = 0;
$dv = rand(1, 100);
$total=$total+$dv;
$output.='<tr valign="top"><td align="left">Result #'.$i.' for 1d100';

if($modifier > 0){
$output.= $sign.$modifier.' = </td>';
}else{
$output.=' = </td>';
}

if ($total > 95) {$total= oeHigh($total);}
if ($total < 6) {$total= oeLow($total);}
if ($sign == '+'){$total=$total+$modifier;}
if ($sign == '-') {$total=$total-$modifier;}

$output .='<td align="right"><b> '.$total.'</b></td></tr>';
}
$output.='</table></td></tr></table>';
return $output;
}

function oeLow($orig)
{
$temp = rand(1,100);
if ($temp < 6)
{
$temp = $orig - $temp;
$temp = oeLow($temp);
} else {$temp = $orig-$temp;}
return $temp;
}

function oeHigh($orig)
{
$temp = rand(1,100);
if ($temp > 95)
{
$temp = $orig + $temp;
$temp = oeHigh($temp);
} else {$temp = $orig+$temp;}
return $temp;
}



And the following code into post.php right under the other dice roller modifications
//Rolemaster-style open-ended rolls
$_POST['message'] = preg_replace('~:oeDice (\d+),(\D*?)(\d*?):~ei', "oeDice('\$1', '\$2', '\$3')", $_POST['message']);


The syntax for use is as follows:
:oeDice [number of rolls to make],[+ or -][modifier]:
so it looks like :oeDice 5,+0:

Now, does anybody have any idea how I can hook it into the Personal Message files so it can be used there as well?

Rasyr

I just a problem with the code I posted in the above post.

When using the dice roller function I wrote, I have to use :oeDice 5,: in order to get it to work.

I have tried to change the line in post.php to

//Rolemaster-style open-ended rolls
$_POST['message'] = preg_replace('~:oeDice (\d+)(\D*?)(\D*?)(\d*?):~ei', "oeDice('\$1', '\$2', '\$3', '\4')", $_POST['message']);


and adding another variable to the function (a variable that I do nothing to or with), but that causes other issues (it likes to combine with the "sign" variable. Any help would be appreciated in fixing this.



ladyshanae

Any ideas when we might see a package of the dice modification? :)

Omniverse

I would most definately like to see this as a package.
Would be a great little addition :)

Nomina

Many thanks to everyone that has made this mod possible.  For the masses of us that are less-than-proficient at php-code, your aid is invaluable.  And, as is so often the case, we want more...or at least I do.

I'm curious about how to go about modifying the ST dice roll to have two features:


  • a variable difficulty to test against (whereas the current "roll" function automatically tests against 7).
  • the ability to return a "botch" message if the total successes is less than 0.

I've already made the necessary changes to the code to subtract rolls of 1 from the total number or successes, but doing so nearly exhausted by knowledge on the matter (and I probably didn't do it in a very "efficient manner.")  These two additions, or some hints on the direction to take in making them myself, would complete the ST dice funtion.

Many thanks in advance.

shanks

#65
I've not tested it, but I believe that this should work for your first query:

Find
function Post()

Add before:
function roll($rolls,$bm)
{
$successes=0;
$temp = 'Results = ';

if (rolls>40) {return 'Sorry, too many rolls!';}

if ($rolls>1)
{
for ($i=0; $i<($rolls-1); $i++)
{
$result=rand(1,10);
$temp.=$result.', ';
if ($result>=$bm) $successes=$successes+1;
}
}
$result=rand(1,10);
$temp.=$result;
if ($result>=$bm) $successes=$successes+1;

return 'Rolling '.$rolls.'d10<br><div style="background-color: #919191;">'.$temp.'<br>'.'<b>Successes at or over ',$bm,' = '.$successes.'</b></div>';
}


In post.php, find:
preparsecode($_POST['message']);

Add after:
$_POST['message'] = preg_replace('~:roll (\d+),(\d+):~ei', "roll('\$1','\$2')", $_POST['message']);

To use this roller to get successes for x amount of rolls against a success benchmark of y, enter:

:roll x,y:

ie, to roll 4 dice and count a 7 or above as successes enter:

:roll 4,7:

And with regards to your second query, I don't think that it is possible for the roller to return successes less than 0... If I'm wrong and you've encountered such an occurance then let me know and I'll include code for that eventuality as well.

TechnoDragon

#66
I have read this entire post...and though I may have skimmed this over...is it possible to hybrid the roller to do a combination of things:

1:  Pick the number of sides (I am looking specifically at 6-sided)

AND

2: Re-roll any that are over the max sides (i.e. if you choose six-sided and one of the rolls is six then it rerolls that.  When it returns the final it totals each reroll)

AND

3:  Count the total as successes.

Example;

In the Shadowrun RPG all rolls are done using six sided dice.

so in a given roll you may have to have x number of succesess against a given number.  so lets say you are trying to accomplish something and you need a success test again the number 11.

the character you are using has a modifier of +3

so you would roll say 7 six-sided dice.

the roller would roll the dice, determine if any of the rolls were six.  If they were it would reroll those, and then after rolling the 7 'dice' it would add each roll together and then add the modifier, then determine how many were successes against the target number.

The simple explanation of this is called the rule of six...any roll of 1 is a failure...any six is counted as a success unless the number is higher than six that is needed then you reroll all of the sixes and then add that to the first roll of six.  so forth untill no more sixes come up...then you add the modifier (if any) to the final totals to determine the number of successes.

rolls....
is this possible?

Possibly by adding another function similar to oeDice, or dice or roll...maybe srDice

Finally...could someone post the proper format for the stats part of this mod?
Don't tell me to get into shape...I have a shape...It is round!


Nomina

#67
First off, a huge thanks to "shanks" for getting this back out so quickly.  Building off his original code, I have the following working in RPG.php:


function roll($rolls, $diff)
{
$successes = 0;
$temp = 'Results = ';

if ($diff>10) {return 'Task impossible, difficulty too high!';}
if ($rolls>40) {return 'Sorry, too many rolls!';}
if ($rolls>1)
{
for ($i=0; $i<($rolls-1); $i++)
{
$result=rand(1,10);
$temp.=$result.', ';
if ($result>=$diff) $successes=$successes+1;
if ($result==1) $successes=$successes-1;
}
}

if ($successes<0) {
return 'Rolling '.$rolls.'d10 (diff '.$diff.')<br><div style="background-color: #919191;">'.$temp.'<br />'.'<b>Successes at difficulty '.$diff.' = '.$successes.'</b><br /><b>BOTCH!</b></div>';
} else

$result=rand(1,10);
$temp.=$result;
if ($result>=$diff) $successes=$successes+1;
if ($result==1) $successes=$successes-1;

return 'Rolling '.$rolls.'d10 (diff '.$diff.')<br><div style="background-color: #919191;">'.$temp.'<br />'.'<b>Successes at difficulty '.$diff.' = '.$successes.'</b></div>';
}


And the Post.php addition:


$_POST['message'] = preg_replace('~:roll (\d+),(\d+):~ei', "roll('\$1','\$2')", $_POST['message']);


You'll note I've changed his variable $bm to something I could better keep track of ($diff).  It was also necessary to change the commas (,) wrapping the $bm variable in the last line of the "roll-code" to periods (.) in order to get the code to work on my machine.

So far, so awesome.  The code successfully tests agains the desired difficulty, and subtracts rolls of "1" from the total successes, and on total successes less than 0, it will return an additional message, "BOTCH!"

My question now is how to go about setting a default difficulty (7) for rolls that don't include a specific difficulty.  For example:

:roll 6,5:
would roll 6 dice against a difficulty of 5, whereas

:roll 6:
would automatically defaut to a test against 7.

Again many thanks for all your help, shanks.


;D[edit]Corrected typo in code...oops...  Updated to include "botch" code...  Believe it or not, I'm learning.[/edit]

shanks

#68
Again, untested, but should default the success difficulty to 7 if no user value is entered.


function roll($rolls, $comma, $diff)
{
// $comma simply picks up the comma when the function is called. It actually has no use here.
       
        if ($diff=='') $diff = 7;
        $successes = 0;
$temp = 'Results = ';

if ($diff>10) {return 'Task impossible, difficulty too high!';}
if ($rolls>40) {return 'Sorry, too many rolls!';}
if ($rolls>1)
{
for ($i=0; $i<($rolls-1); $i++)
{
$result=rand(1,10);
$temp.=$result.', ';
if ($result>=$diff) $successes=$successes+1;
if ($result==1) $successes=$successes-1;
}
}

if ($successes<0) {
return 'Rolling '.$rolls.'d10 (diff '.$diff.')<br><div style="background-color: #919191;">'.$temp.'<br />'.'<b>Successes at difficulty '.$diff.' = '.$successes.'</b><br /><b>BOTCH!</b></div>';
} else

$result=rand(1,10);
$temp.=$result;
if ($result>=$diff) $successes=$successes+1;
if ($result==1) $successes=$successes-1;

return 'Rolling '.$rolls.'d10 (diff '.$diff.')<br><div style="background-color: #919191;">'.$temp.'<br />'.'<b>Successes at difficulty '.$diff.' = '.$successes.'</b></div>';
}


And the Post.php addition:


$_POST['message'] = preg_replace('~:roll (\d+)(\D*?)(\d*?):~ei', "roll('\$1','\$2','\$3')", $_POST['message']);


To roll 4 times against default difficulty of 7:

:roll 4:

To roll four times against difficulty of 5:

:roll 4,5:

(Or, if you fancy :roll 4v5: - It shouldn't actually matter what the single character is so long as it is only a single non-numeric character)

shanks

Quote from: technodragon73 on February 18, 2006, 04:31:35 PM
Finally...could someone post the proper format for the stats part of this mod?


Using this roller you can easily generate the base stats for your character.

To use:
:stats [number of rolls]d[number of sides][+/-][modifier],[drop lowest? y/n],[number of stats to roll]:

If there is none you may omit the modifier but you must always include the command to drop the lowest roll or not (y/n) and the amount of stats to roll.

Here are some examples...

1. This is the D&D method of rolling stats. 4d6 will be rolled 6 times and the lowest roll each time will be dropped.

:stats 4d6,y,6:

Stats rolled 6 times using 4d6 and dropping the lowest roll
6,2,3,4:  13
2,4,5,2:  11
5,1,6,2:  13
1,3,1,6:  10
4,1,4,1:  9
5,6,1,4:  15

2. This rolls the same as 1. but will also add 1 to the total roll.

:stats 4d6+1,y,6:

Stats rolled 6 times using 4d6+1 and dropping the lowest roll
3,1,2,6: 11 + 1 =  12
4,4,4,3: 12 + 1 =  13
6,6,1,1: 13 + 1 =  14
4,5,2,2: 11 + 1 =  12
6,2,3,1: 11 + 1 =  12
5,4,6,2: 15 + 1 =  16

3. If you don't want the lowest to be dropped then change the 'y' to a 'n'.

:stats 4d6,n,6:

Stats rolled 6 times using 4d6
5,3,3,3:  14
3,3,1,6:  13
4,3,5,2:  14
6,2,5,5:  18
2,5,6,5:  18
4,1,1,3:  9

URPG

Can you skip the last parameters for standard rolls?

It's a bit complicated to remember what command to use and all the parameters behind it...

Maybe a syntax like:

3d6 -> returns results from 3-18
3x1d6 -> returns 3 results between 1 and 6
4d6,d[1] -> rolls 4 d6 and drops the lowest
6d6,r -> rolls 6 dices and rerolls on a max (6)
1d20+5,s[15] -> rolls d20, adds 5 and tell you if you succeeded reaching 15

once a pharser like this is working you can do almost anything for every game with it...
would be a shame to have just another dice roller limited to D&D ...

uberjon

Quote from: Rasyr on December 29, 2005, 08:41:16 AM
rpg.php

u have an rpg (roll playing game) ?

very interested in this O.o

shanks

Quote from: uberjon on February 19, 2006, 08:22:11 AM
u have an rpg (roll playing game) ?

If you are asking specifically about rpg.php then that is a file I created to house dice rolling functions to prevent bloating core smf files.

uberjon

ok well then exactly what purpose does dice rolling feature serve?

shanks

For people who play pen and paper RPG games over message boards it enables a commonly visible dice roller that is slowly being tweaked, improved, and expanded to cover various dice rolling systems.

shanks

Quote from: URPG on February 19, 2006, 08:02:16 AM
It's a bit complicated to remember what command to use and all the parameters behind it...

Maybe a syntax like:

3d6 -> returns results from 3-18
3x1d6 -> returns 3 results between 1 and 6
4d6,d[1] -> rolls 4 d6 and drops the lowest
6d6,r -> rolls 6 dices and rerolls on a max (6)
1d20+5,s[15] -> rolls d20, adds 5 and tell you if you succeeded reaching 15

once a pharser like this is working you can do almost anything for every game with it...
would be a shame to have just another dice roller limited to D&D ...

I'm working on something even better  ;D

Nomina

#76
Shanks, thanks a ton for your help.  It was setting the seperator as a variable ($comma) that I was missing, along with the appropriate changes to the Post.php code.  The only change I had to make was to replace...

if ($diff=='') then $diff = 7;

with

if ($diff=='') {$diff=7;}

...in order to avoid a "parse error, unexpected T_VARIABLE."  Once that was done, it ran like butter.

Thanks again.  :D

shanks

Ah, yes. That pesky "then".

I've editted my post to correct that for other users.

Ta, Nomina.

shanks

#78
And here is something for URPG...

In index.php, find:

'requestmembers' => array('Subs-Auth.php', 'RequestMembers'),


Add after:
'rpg' => array('RPG.php', 'rpg'),

In most cases here you'll want to use the file in the default template folder for the next two.

In post_template.php, find:


global $context, $settings, $options, $txt, $modSettings;


Replace with:

global $context, $settings, $options, $txt, $modSettings, $scripturl;


In post_template.php, find:
// Finally the most important bit - the actual text box to write in!

Add before:

// Enter link to the roller!
echo '
<tr>
<td valign="top" align="right"></td>
<td><a href="'.$scripturl.'?action=rpg" onclick="return reqWin(this.href,350,320)">Dice roll code generator</a></td>
</tr>';


In post.php, find:

preparsecode($_POST['message']);


Add after:

include_once 'RPG.php';
$_POST['message'] = preg_replace('~:dice (\d+)d(\d+)(\D*?)(\d*?):~ei', "dice('\$1', '\$2', '\$3', '\$4')", $_POST['message']);
$_POST['message'] = preg_replace('~:stats (\d+)d(\d+)(\D*?)(\d*?),(\D+),(\d+):~ei', "statroll('\$1', '\$2', '\$3', '\$4', '\$5', '\$6')", $_POST['message']);
$_POST['message'] = preg_replace('~:rollgen (\d+)d(\d+),(\D*?)(\d+),(\d+),(\d+),(\d+),(\d+),(\d+):~ei', "rollgen('\$1','\$2','\$3','\$4','\$5','\$6','\$7','\$8','\$9')", $_POST['message']);


And then simply grab the attachment from below and upload into the Source folder in SMF.

This code will provide a link below and smilies in the "post reply" page that will pop a window up that will allow you to create code for a wide variety of rolls. Simply copy and paste this into your post.

I've done what testing I can, but let me know if something doesn't seem right. Because of the high and low reroll options there is potential for an infinite loop to be created, but I am fairly sure that I have prevented this. Don't blame me, however, if you slow your server down!  :o

For those occasions that you don't need all the generator offers, you can still use good ol'

:dice 2d6+1: etc

for those simple rolls!


TechnoDragon

#79
ok..i have been toying with the roll generator and i like the way it works...however, is there a way to make it reroll and then add if the reroll target is met?

that way if i set the target to say six...then it rerolls any sixes and add that total to the first roll...then if the second roll is higher than six...it would add that to the first two rolls and so on untill no sixes are rolled?

the purpose of this is that in the rpg i play in order to have succeses you use six-sided dice and if the die rolls a six, then you reroll that die and add the total of that roll to the first and so on like described above.


lol...lastly...is it possible to put the dice image in for the roll generator as well?  (I know...small picky stuff...but the dice image next to the rolls is kinda cool)

I modified the code slightly:
// Enter link to the roller!
echo '
<tr>
<td valign="top" align="right"></td>
<td><a href="'.$scripturl.'?action=rpg" onclick="return reqWin(this.href,350,320)"><img src="' . $settings['images_url'] . '/rollgen.gif" alt="Dice roll code generator" /></a></td>
</tr>';


it added a gif instead of the text link.

here is the image file i used:
Don't tell me to get into shape...I have a shape...It is round!


Advertisement: