### Re: [RC1] Dice MOD

Posted:

**Sat Jan 12, 2008 11:24 pm**Okay, I MAY be continuing my Warhammer Fantasy RPG, which ran from May 2003 until August 5th, 2006, and it was an excellent game with excellent players. I'm looking to upgrade to phpBB3, which is why I came here looking for a dice mod.

First concern is I've seen two statements that contradict each other throughout all seven of these pages, and to clarify I need to know... if I've made a post in which there are one or more dice rolls, and I go back to edit that post, do the previously rolled numbers remain, or are they generated new?

The second concern I have is that I need open-ended/meta/exploding dice available for my game; meta-dice are also used for over a dozen RPGs that could find their way to the internet, and it's the entire gammit of polyhedron's, not just d10s or d6s. For those who don't know, open-ended/meta/exploding dice are those in which if the maximum number for that dice is rolled (such as a 4 on 1d4 or an 8 on 1d8), that dice is rolled again and added to the previous roll as well as any remaining dice in the set.

A friend of mine, Niagol, who has/had a presence on these forums but doesn't visit that often, I think, came up with a solution for the exploding dice problem, and the following code is what he came up with... (NOTE that this is from around phpBB 2.0.16 release time, k?

This goes in includes/bbcode.php, just paste it over the old code.

First concern is I've seen two statements that contradict each other throughout all seven of these pages, and to clarify I need to know... if I've made a post in which there are one or more dice rolls, and I go back to edit that post, do the previously rolled numbers remain, or are they generated new?

The second concern I have is that I need open-ended/meta/exploding dice available for my game; meta-dice are also used for over a dozen RPGs that could find their way to the internet, and it's the entire gammit of polyhedron's, not just d10s or d6s. For those who don't know, open-ended/meta/exploding dice are those in which if the maximum number for that dice is rolled (such as a 4 on 1d4 or an 8 on 1d8), that dice is rolled again and added to the previous roll as well as any remaining dice in the set.

A friend of mine, Niagol, who has/had a presence on these forums but doesn't visit that often, I think, came up with a solution for the exploding dice problem, and the following code is what he came up with... (NOTE that this is from around phpBB 2.0.16 release time, k?

This goes in includes/bbcode.php, just paste it over the old code.

Code: Select all

```
function replace_dice_results($text, $uid)
{
global $lang;
// Take the desired Die string ($text) and generate values for each roll.
$Expressions = explode('=', $text);
if( (isset($Expressions[0])) && (isset($Expressions[1])) )
{
$Dice_Rolls = $Expressions[0];
$MT_Seed = intval($Expressions[1]);//(double)microtime()*1000000; doesn't work as it causes the rolls to be re-rolled every time the topic/post is re-loaded//
$Fixed = $Expressions[2];
}
else
{
return;
}
// Make sure we restore the MT gen to a random state after we are done...
$Future_Seed = mt_rand();
mt_srand( $MT_Seed );
$Original_Roll_String = (isset($Fixed)) ? $Dice_Rolls . ' <b><span style="color: #AA0000">' . $lang['Dice_Mod_Fixed'] . '</span></b>': $Dice_Rolls;
$Die_Rolls = explode(' ', trim($Dice_Rolls));
//Break off Original Roll string seperate from Roll results
$headerroll = '<b>' . $lang['Dice_Mod_Roll_String'] . '</b>: ' . $Original_Roll_String;
while( $Die = array_shift($Die_Rolls))
{
$footer = '';
$header = '';
$Die_Count = substr($Die,0,strpos($Die,'d'));
$Die_Type = substr($Die,strpos($Die,'d')+1);
if( strpos($Die_Type, '+') )
{
$Method = 1;
$Modifier = substr($Die_Type,strpos($Die_Type,'+')+1);
$Die_Type = substr($Die_Type,0,strpos($Die_Type,'+'));
}
else if( strpos($Die_Type, '-') )
{
$Method = 2;
$Modifier = substr($Die_Type,strpos($Die_Type,'-')+1);
$Die_Type = substr($Die_Type,0,strpos($Die_Type,'-'));
}
else if( strpos($Die_Type, '*') )
{
$Method = 3;
$Modifier = substr($Die_Type,strpos($Die_Type,'*')+1);
$Die_Type = substr($Die_Type,0,strpos($Die_Type,'*'));
}
else if( strpos($Die_Type, 'x') )
{
$Method = 3;
$Modifier = substr($Die_Type,strpos($Die_Type,'x')+1);
$Die_Type = substr($Die_Type,0,strpos($Die_Type,'x'));
}
else if( strpos($Die_Type, '/') )
{
$Method = 4;
$Modifier = substr($Die_Type,strpos($Die_Type,'/')+1);
$Die_Type = substr($Die_Type,0,strpos($Die_Type,'/'));
}
else if( strpos($Die_Type, 'e') )
{
$Method = 5; //Checking for the Exploding Dice
$Modifier = 0;
$Die_Type = substr($Die_Type,0,strpos($Die_Type,'e'));
}
else
{
$Method = 0;
$Modifier = 0;
}
$header = '<br /><b>' . $Die_Type . '-Sided Dice Results: </b>';
$footer = '';
$Die_Count = intval($Die_Count);
if( $Die_Count == 0 ) $Die_Count = 1;
$total = 0;
if( $Method )
{
$footer = $footer . "(";
}
// Loop Limit to prevent 500000d500000 sort of dice due to max. execution time limit
if($Die_Count <= 200 && $Die_Type <= 100)
{
for( $i = 0; $i < $Die_Count; $i++ )
{
if( $Die_Type == 100 )
{
$value1 = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * 10) * 10;
$value2 = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * 10);
$dieexp = ($value1 + $value2) + 1; //Add 1 to get 1 to 100 instead of 0 to 99
$exploding = 1;
if( $Method == 5 )
{
While( $dieexp == ($exploding * 100) )
{
$value1 = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * 10) * 10;
$value2 = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * 10);
$dieexp2 = ($value1 + $value2) + 1;
$dieexp = $dieexp + $dieexp2;
$exploding++;
}
}
$total = $total + $dieexp;
if( $exploding > 1 )
{
$footer = ($i != $Die_Count - 1) ? $footer . $dieexp . ' (100 + ' . ($dieexp - 100) . ' exploding<b></b>), ' : $footer . $dieexp . ' (100 + ' . ($dieexp - 100) . ' exploding<b></b>), ';
}
else
{
$footer = ($i != $Die_Count - 1) ? $footer . $dieexp . '<b></b>, ' : $footer . $dieexp . '<b></b>';
}
}
else
{
$value = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * $Die_Type) + 1;
$dieexp = $value;
$exploding = 1;
//Check for exploding method
if( $Method == 5 )
{
//Check to see if max roll was done and roll again if so
While( $dieexp == ($exploding * $Die_Type) )
{
$value = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * $Die_Type) + 1;
$dieexp2 = $value;
$dieexp = $dieexp + $dieexp2;
$exploding++;
}
}
$total = $total + $dieexp;
if( $exploding > 1 )
{
//Append dice roll + exploding count to display
$footer = ($i != $Die_Count - 1) ? $footer . $dieexp . ' (' . $Die_Type . ' + ' . ($dieexp - $Die_Type) . ' exploding<b></b>), ' : $footer . $dieexp . ' (' . $Die_Type . ' + ' . ($dieexp - $Die_Type) . ' exploding<b></b>), ';
}
else
{
$footer = ($i != $Die_Count - 1) ? $footer . $dieexp . '<b></b>, ' : $footer . $dieexp . '<b></b>';
}
}
}
} // Loop limit
else
{
$total = 0;
$Modifier = 0;
$footer = 'Too many dice and/or too many sides';
}
switch( $Method )
{
case 1:
$footer = $footer . '<b></b>) + ' . $Modifier . ' ';
$total = $total + $Modifier;
break;
case 2:
$footer = $footer . '<b></b>) - ' . $Modifier . ' ';
$total = $total - $Modifier;
break;
case 3:
$footer = $footer . '<b></b>) * ' . $Modifier . ' ';
$total = $total * $Modifier;
break;
case 4:
$footer = $footer . '<b></b>) / ' . $Modifier . ' ';
$total = $total / $Modifier;
break;
case 5:
$footer = $footer . '<b></b>) ';
break;
}
// I do b - /b on purpose... kills out some smilies that crop up.
if( ($Die_Count > 1) || ($Method != 0) )
{
$footer= $footer . ' (<b>Total =</b> ' . $total . '<b></b>)<BR>';
}
else
{
$footer = $footer . '<BR>';
}
$footer = $footer. "";
$results = $results . $header . $footer;
}
// Restore MT randomness
mt_srand($Future_Seed);
$results = $headerroll . $results; //appends dice roll information to original roll info
return $results;
}
// --------------------------------
// END DICE MOD CODE
// --------------------------------
```