#include #include #include #include #include #include "pline.h" #include "dice.h" void initrandom(void) { struct timeval tv; gettimeofday(&tv,(struct timezone *)0); srandom(tv.tv_sec^tv.tv_usec^getpid()^getuid()^gethostid()); } int rnd(int n) { return(random()%n); } int onein(int n) { return((n>1)?!rnd(n):1); } static int d(int n, int sides) { int total; total = 0; for (;n>0;n--) { total += 1 + (random() % sides); } return(total); } int roll(const char *str) { int total; int ndice; int nsides; const char *cp; int neg; cp = str; neg = 0; total = 0; while (1) { if (isdigit((unsigned char)*cp)) { ndice = 0; while (isdigit((unsigned char)*cp)) { ndice = (10 * ndice) + (*cp - '0'); cp ++; } } else if (*cp == 'd') { ndice = 1; } if (*cp == 'd') { cp ++; nsides = 0; while (isdigit((unsigned char)*cp)) { nsides = (10 * nsides) + (*cp - '0'); cp ++; } if (neg) { total -= d(ndice,nsides); } else { total += d(ndice,nsides); } } else { if (neg) { total -= ndice; } else { total += ndice; } } neg = 0; if (*cp == '+') { cp ++; } else if (*cp == '-') { neg = 1; cp ++; } else if (*cp == '\0') { break; } else { panic("Bad dice spec `%s' to roll()",str); } } return(total); }