Here's some code I thought people might like.. I wrote it for my builders using dg scripts so they could use some random numbers to some of the commands, then found it worked pretty nicely for anywhere (loading mobs, etc) It will basically take any kind of format for dice (spaces/etc don't bother it although in the wrong format altogether, wierd things will happen) It also accepts single numeric argument for backward compat. Enjoy, lemme know if it don't work (It's not the neatest code, I know, but should be easy to fix up if you find a problem) -------- 8< snip ---------------- int parse_dice(char *line) { char num[80], sid[80], *tmp = NULL; int one = FALSE, two = FALSE; int mod = 1, number = 0, sides = 0, modifier = 0, temp = 0; if (!*line) { log("SYSERR: parse_dice called with no argument"); return (0); } skip_spaces(line); for (temp = 0, tmp = line; *tmp; temp++, tmp++) { if (!isdigit(*tmp)) { one = TRUE; break; } num[temp] = *tmp; } num[temp] = '\0'; number = atoi(num); for (; *tmp && !isdigit(*tmp); tmp++); if (one) { for (temp = 0; *tmp; temp++, tmp++) { if (isspace(*tmp)) continue; if (!isdigit(*tmp)) { two = TRUE; if (*tmp == '-') mod = -1; else mod = 1; break; } sid[temp] = *tmp; } sid[temp] = '\0'; sides = atoi(sid); } if (two) { tmp++; modifier = atoi(tmp) * mod; } if (one && two) { return (dice(number, sides) + modifier); } else { return (number + modifier); } } -------- >8 snip ----------- +------------------------------------------------------------+ | Ensure that you have read the CircleMUD Mailing List FAQ: | | http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html | +------------------------------------------------------------+
This archive was generated by hypermail 2b30 : 04/10/01 PDT