15 #include "../clib/logfacility.h" 16 #include "../clib/random.h" 17 #include "../clib/rawtypes.h" 18 #include "../clib/stlutil.h" 24 Dice::Dice() : die_count( 0 ), die_type( 0 ), plus_damage( 0 ) {}
29 for (
unsigned i = 0; i <
die_count; i++ )
39 return static_cast<unsigned short>( total );
42 bool Dice::load(
const char* dicestr, std::string* errormsg )
46 const char* str = dicestr;
50 if ( strchr( str,
'D' ) || strchr( str,
'd' ) )
52 if ( toupper( *str ) ==
'D' )
59 if ( !isdigit( *str ) )
61 *errormsg =
"Error reading die string '";
64 *errormsg +=
"Expected digit at beginning";
71 *errormsg =
"Error reading die string '";
74 *errormsg +=
"Got a die count of 0? strtoul failed?";
78 if ( toupper( *endptr ) !=
'D' )
80 *errormsg =
"Error reading die string '";
83 *errormsg +=
"Didn't expect '";
85 *errormsg +=
"' before the 'D'";
91 if ( !isdigit( *str ) )
93 *errormsg =
"Error reading die string '";
96 *errormsg +=
"expected a number after the 'D', got '";
101 die_type = strtoul( str, &endptr, 0 );
104 *errormsg =
"Error reading die string '";
105 *errormsg += dicestr;
107 *errormsg +=
"Die type of '";
109 *errormsg +=
"' doesn't make sense!";
114 while ( *str && isspace( *str ) )
124 if ( sign !=
'+' && sign !=
'-' )
126 *errormsg =
"Error reading die string '";
127 *errormsg += dicestr;
129 *errormsg +=
"Expected '+' or '-' after xDy, got '";
142 if ( !isdigit( *str ) )
144 *errormsg =
"Error reading die string '";
145 *errormsg += dicestr;
147 *errormsg +=
"Expected digit at beginning, got '";
158 catch ( std::exception& ex )
160 ERROR_PRINT <<
"Uh, looks like I found damage of " << dicestr <<
" confusing.\n" 161 <<
"Valid formats look like: 3D6+2, 2D8, D10, 4, 2d20-4\n" 162 <<
"No spaces please!\n";
163 POLLOG.Format(
"Dice String {} hurt me!: {}\n" ) << dicestr << ex.what();
164 *errormsg =
"An exception occured trying to decipher dice '";
165 *errormsg += dicestr;
185 return static_cast<u16>( total );
195 return static_cast<u16>( total );
unsigned short roll(void) const
unsigned short min_value(void) const
#define OSTRINGSTREAM_STR(x)
void die_string(std::string &str) const
unsigned short max_value(void) const
bool load(const char *dice, std::string *errormsg)