Advertisement:

Author Topic: Dice roll mod ... again.  (Read 128970 times)

Offline Rasyr

  • Jr. Member
  • **
  • Posts: 170
Re: Dice roll mod ... again.
« Reply #60 on: December 29, 2005, 08:41:16 AM »
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
Code: [Select]
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
Code: [Select]
//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?

Offline Rasyr

  • Jr. Member
  • **
  • Posts: 170
Re: Dice roll mod ... again.
« Reply #61 on: December 30, 2005, 12:15:46 AM »
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

Code: [Select]
//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.



Offline ladyshanae

  • Semi-Newbie
  • *
  • Posts: 60
  • Structured Procrastinator
Re: Dice roll mod ... again.
« Reply #62 on: February 08, 2006, 05:53:46 PM »
Any ideas when we might see a package of the dice modification? :)

Offline Omniverse

  • Semi-Newbie
  • *
  • Posts: 85
  • Gender: Male
  • The only real hOle!!!
    • Perry
Re: Dice roll mod ... again.
« Reply #63 on: February 17, 2006, 02:20:37 PM »
I would most definately like to see this as a package.
Would be a great little addition :)

Offline Nomina

  • Semi-Newbie
  • *
  • Posts: 34
  • Gender: Male
Re: Dice roll mod ... again.
« Reply #64 on: February 17, 2006, 04:19:45 PM »
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.

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #65 on: February 18, 2006, 02:55:05 PM »
I've not tested it, but I believe that this should work for your first query:

Find
Code: [Select]
function Post()
Add before:
Code: [Select]
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:
Code: [Select]
preparsecode($_POST['message']);
Add after:
Code: [Select]
$_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.
« Last Edit: February 18, 2006, 02:57:29 PM by shanks »

Offline TechnoDragon

  • Jr. Member
  • **
  • Posts: 374
  • Gender: Male
  • With great power comes great resposibility...
    • Dragon Talk Forum
Re: Dice roll mod ... again.
« Reply #66 on: February 18, 2006, 04:31:35 PM »
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?
« Last Edit: February 18, 2006, 04:51:55 PM by technodragon73 »
Don't tell me to get into shape...I have a shape...It is round!


Offline Nomina

  • Semi-Newbie
  • *
  • Posts: 34
  • Gender: Male
Re: Dice roll mod ... again.
« Reply #67 on: February 18, 2006, 05:31:57 PM »
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:

Code: [Select]
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:

Code: [Select]
$_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]
« Last Edit: February 18, 2006, 06:23:25 PM by Nomina »

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #68 on: February 19, 2006, 07:29:54 AM »
Again, untested, but should default the success difficulty to 7 if no user value is entered.

Code: [Select]
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:

Code: [Select]
$_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)
« Last Edit: February 19, 2006, 03:18:35 PM by shanks »

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #69 on: February 19, 2006, 07:44:08 AM »
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

Offline URPG

  • Jr. Member
  • **
  • Posts: 360
Re: Dice roll mod ... again.
« Reply #70 on: February 19, 2006, 08:02:16 AM »
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 ...

Offline uberjon

  • Jr. Member
  • **
  • Posts: 348
  • Gender: Male
  • i tar and feather bad hosts on SMF's hosting board
Re: Dice roll mod ... again.
« Reply #71 on: February 19, 2006, 08:22:11 AM »
rpg.php

u have an rpg (roll playing game) ?

very interested in this O.o

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #72 on: February 19, 2006, 08:26:16 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.

Offline uberjon

  • Jr. Member
  • **
  • Posts: 348
  • Gender: Male
  • i tar and feather bad hosts on SMF's hosting board
Re: Dice roll mod ... again.
« Reply #73 on: February 19, 2006, 08:30:19 AM »
ok well then exactly what purpose does dice rolling feature serve?

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #74 on: February 19, 2006, 08:31:36 AM »
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.

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #75 on: February 19, 2006, 08:37:33 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

Offline Nomina

  • Semi-Newbie
  • *
  • Posts: 34
  • Gender: Male
Re: Dice roll mod ... again.
« Reply #76 on: February 19, 2006, 02:29:45 PM »
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...

Code: [Select]
if ($diff=='') then $diff = 7;
with

Code: [Select]
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
« Last Edit: February 19, 2006, 02:54:30 PM by Nomina »

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #77 on: February 19, 2006, 03:19:29 PM »
Ah, yes. That pesky "then".

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

Ta, Nomina.

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #78 on: February 19, 2006, 04:30:38 PM »
And here is something for URPG...

In index.php, find:
Code: [Select]
'requestmembers' => array('Subs-Auth.php', 'RequestMembers'),
Add after:
Code: [Select]
'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:

Code: [Select]
global $context, $settings, $options, $txt, $modSettings;

Replace with:
Code: [Select]
global $context, $settings, $options, $txt, $modSettings, $scripturl;

In post_template.php, find:
Code: [Select]
// Finally the most important bit - the actual text box to write in!
Add before:
Code: [Select]
// 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:
Code: [Select]
preparsecode($_POST['message']);

Add after:
Code: [Select]
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!

« Last Edit: February 20, 2006, 02:31:23 AM by shanks »

Offline TechnoDragon

  • Jr. Member
  • **
  • Posts: 374
  • Gender: Male
  • With great power comes great resposibility...
    • Dragon Talk Forum
Re: Dice roll mod ... again.
« Reply #79 on: February 19, 2006, 06:36:46 PM »
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:
Code: [Select]
// 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:
« Last Edit: February 19, 2006, 07:17:54 PM by technodragon73 »
Don't tell me to get into shape...I have a shape...It is round!