--- deliantra/server/common/utils.C 2007/04/21 22:57:15 1.49 +++ deliantra/server/common/utils.C 2007/04/28 21:34:37 1.55 @@ -65,16 +65,16 @@ } uint32_t -tausworthe_random_generator::get_range (uint32_t r_max) +tausworthe_random_generator::get_range (uint32_t num) { - return (next () * (uint64_t)r_max) >> 32U; + return (next () * (uint64_t)num) >> 32U; } // return a number within (min .. max) int tausworthe_random_generator::get_range (int r_min, int r_max) { - return r_min + get_range (max (r_max - r_min + 1, 1)); + return r_min + get_range (max (r_max - r_min + 1, 0)); } /* @@ -97,9 +97,9 @@ { int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ - if (r_max < 1 || r_max < r_min) + if (r_max < r_min) { - LOG (llevError, "Calling random_roll with min=%d max=%d\n", r_min, r_max); + LOG (llevError | logBacktrace, "Calling random_roll with min=%d max=%d\n", r_min, r_max); return r_min; } @@ -123,16 +123,13 @@ sint64 random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) { - sint64 omin, diff, luck, ran; - int base; + sint64 omin = min; + sint64 diff = max - min + 1; + int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ - omin = min; - diff = max - min + 1; - ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ - - if (max < 1 || diff < 1) + if (diff < 0) { - LOG (llevError, "Calling random_roll with min=%" PRId64 " max=%" PRId64 "\n", min, max); + LOG (llevError | logBacktrace, "Calling random_roll64 with min=%" PRId64 " max=%" PRId64 "\n", min, max); return (min); /* avoids a float exception */ } @@ -140,12 +137,13 @@ * Make a call to get two 32 bit unsigned random numbers, and just to * a little bitshifting. */ - ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); + sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); if (op->type != PLAYER) return ((ran % diff) + min); - luck = op->stats.luck; + int luck = op->stats.luck; + if (rndm (base) < MIN (10, abs (luck))) { /* we have a winner */ @@ -153,12 +151,13 @@ diff -= luck; if (diff < 1) return (omin); /*check again */ + ((goodbad) ? (min += luck) : (diff)); return (MAX (omin, MIN (max, (ran % diff) + min))); } - return ((ran % diff) + min); + return ran % diff + min; } /* @@ -168,20 +167,20 @@ * not the recipient (ie, the poor slob getting hit). * The args are num D size (ie 4d6) [garbled 20010916] */ - int die_roll (int num, int size, const object *op, int goodbad) { - int min, diff, luck, total, i, gotlucky, base; + int min, luck, total, i, gotlucky; - diff = size; + int diff = size; min = 1; luck = total = gotlucky = 0; - ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ + int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ + if (size < 2 || diff < 1) { LOG (llevError, "Calling die_roll with num=%d size=%d\n", num, size); - return (num); /* avoids a float exception */ + return num; /* avoids a float exception */ } if (op->type == PLAYER) @@ -555,6 +554,9 @@ if (!fork ()) { signal (SIGABRT, SIG_DFL); + // try to put corefiles into a subdirectory, if existing, to allow + // an administrator to reduce the I/O load. + chdir ("cores"); abort (); } @@ -611,6 +613,42 @@ memcpy (dst, src, len + 1); } +const std::string +format (const char *format, ...) +{ + int len; + + { + char buf[128]; + + va_list ap; + va_start (ap, format); + len = vsnprintf (buf, sizeof (buf), format, ap); + va_end (ap); + + assert (len >= 0); // shield againstz broken vsnprintf's + + // was our static buffer short enough? + if (len < sizeof (buf)) + return std::string (buf, len); + } + + { + // longer, try harder + char *buf = salloc (len + 1); + + va_list ap; + va_start (ap, format); + vsnprintf (buf, len + 1, format, ap); + va_end (ap); + + const std::string s (buf, len); + sfree (buf, len + 1); + + return buf; + } +} + tstamp now () { struct timeval tv;