ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/utils.C
(Generate patch)

Comparing deliantra/server/common/utils.C (file contents):
Revision 1.39 by pippijn, Mon Jan 15 21:06:18 2007 UTC vs.
Revision 1.44 by root, Thu Jan 25 03:54:44 2007 UTC

37#include <funcpoint.h> 37#include <funcpoint.h>
38#include <material.h> 38#include <material.h>
39 39
40#include <glib.h> 40#include <glib.h>
41 41
42rand_gen rndm;
43
44void
45tausworthe_random_generator::seed (uint32_t seed)
46{
47 state [0] = max ( 2U, seed * 69069U);
48 state [1] = max ( 8U, state [0] * 69069U);
49 state [2] = max ( 16U, state [1] * 69069U);
50 state [3] = max (128U, state [2] * 69069U);
51
52 for (int i = 11; --i; )
53 operator ()();
54}
55
56uint32_t
57tausworthe_random_generator::next ()
58{
59 state [0] = ((state [0] & 0xFFFFFFFEU) << 18U) ^ (((state [0] << 6U) ^ state [0]) >> 13U);
60 state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U);
61 state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U);
62 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
63
64 return state [0] ^ state [1] ^ state [2] ^ state [3];
65}
66
67uint32_t
68tausworthe_random_generator::get_range (uint32_t r_max)
69{
70 return next () % r_max;
71}
72
73// return a number within (min .. max)
74int
75tausworthe_random_generator::get_range (int r_min, int r_max)
76{
77 return r_min + (*this) (max (r_max - r_min + 1, 1));
78}
79
42/* 80/*
43 * The random functions here take luck into account when rolling random 81 * The random functions here take luck into account when rolling random
44 * dice or numbers. This function has less of an impact the larger the 82 * dice or numbers. This function has less of an impact the larger the
45 * difference becomes in the random numbers. IE, the effect is lessened 83 * difference becomes in the random numbers. IE, the effect is lessened
46 * on a 1-1000 roll, vs a 1-6 roll. This can be used by crafty programmers, 84 * on a 1-1000 roll, vs a 1-6 roll. This can be used by crafty programmers,
52 * Roll a random number between min and max. Uses op to determine luck, 90 * Roll a random number between min and max. Uses op to determine luck,
53 * and if goodbad is non-zero, luck increases the roll, if zero, it decreases. 91 * and if goodbad is non-zero, luck increases the roll, if zero, it decreases.
54 * Generally, op should be the player/caster/hitter requesting the roll, 92 * Generally, op should be the player/caster/hitter requesting the roll,
55 * not the recipient (ie, the poor slob getting hit). [garbled 20010916] 93 * not the recipient (ie, the poor slob getting hit). [garbled 20010916]
56 */ 94 */
57
58int 95int
59random_roll (int min, int max, const object *op, int goodbad) 96random_roll (int r_min, int r_max, const object *op, int goodbad)
60{ 97{
61 int omin, diff, luck, base, ran; 98 int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */
62 99
63 omin = min; 100 if (r_max < 1 || r_max < r_min)
64 diff = max - min + 1;
65 ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */
66
67 if (max < 1 || diff < 1)
68 { 101 {
69 LOG (llevError, "Calling random_roll with min=%d max=%d\n", min, max); 102 LOG (llevError, "Calling random_roll with min=%d max=%d\n", r_min, r_max);
70 return (min); /* avoids a float exception */ 103 return r_min;
71 } 104 }
72 105
73 ran = RANDOM ();
74
75 if (op->type != PLAYER) 106 if (op->type == PLAYER)
76 return ((ran % diff) + min); 107 {
77
78 luck = op->stats.luck; 108 int luck = op->stats.luck;
79 if (RANDOM () % base < MIN (10, abs (luck))) 109
110 if (rndm (base) < min (10, abs (luck)))
111 {
112 //TODO: take luck into account
113 }
80 { 114 }
81 /* we have a winner */
82 ((luck > 0) ? (luck = 1) : (luck = -1));
83 diff -= luck;
84 if (diff < 1)
85 return (omin); /*check again */
86 ((goodbad) ? (min += luck) : (diff));
87 115
88 return (MAX (omin, MIN (max, (ran % diff) + min))); 116 return rndm (r_min, r_max);
89 }
90 return ((ran % diff) + min);
91} 117}
92 118
93/* 119/*
94 * This is a 64 bit version of random_roll above. This is needed 120 * This is a 64 bit version of random_roll above. This is needed
95 * for exp loss calculations for players changing religions. 121 * for exp loss calculations for players changing religions.
180 { 206 {
181 total += RANDOM () % size + 1; 207 total += RANDOM () % size + 1;
182 } 208 }
183 } 209 }
184 return (total); 210 return (total);
185}
186
187/*
188 * Another convenience function. Returns a number between min and max.
189 * It is suggested one use these functions rather than RANDOM()%, as it
190 * would appear that a number of off-by-one-errors exist due to improper
191 * use of %. This should also prevent SIGFPE.
192 */
193
194int
195rndm (int min, int max)
196{
197 int diff;
198
199 diff = max - min + 1;
200 if (max < 1 || diff < 1)
201 return (min);
202
203 return (RANDOM () % diff + min);
204} 211}
205 212
206/* decay and destroy perishable items in a map */ 213/* decay and destroy perishable items in a map */
207void 214void
208maptile::decay_objects () 215maptile::decay_objects ()
563 LOG (llevError, "fork abort: %s\n", msg); 570 LOG (llevError, "fork abort: %s\n", msg);
564} 571}
565 572
566void *salloc_ (int n) throw (std::bad_alloc) 573void *salloc_ (int n) throw (std::bad_alloc)
567{ 574{
575#ifdef PREFER_MALLOC
576 void *ptr = malloc (n);
577#else
568 void *ptr = g_slice_alloc (n); 578 void *ptr = g_slice_alloc (n);
579#endif
569 580
570 if (!ptr) 581 if (!ptr)
571 throw std::bad_alloc (); 582 throw std::bad_alloc ();
572 583
573 return ptr; 584 return ptr;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines