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.41 by root, Thu Jan 18 22:19:59 2007 UTC vs.
Revision 1.46 by root, Mon Feb 5 01:47:22 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 (time (0)); 42rand_gen rndm;
43 43
44void
44tausworthe_random_generator::tausworthe_random_generator (uint32_t seed) 45tausworthe_random_generator::seed (uint32_t seed)
45{ 46{
46 state [0] = max ( 2, seed * 69069U); 47 state [0] = max ( 2U, seed * 69069U);
47 state [1] = max ( 8, state [0] * 69069U); 48 state [1] = max ( 8U, state [0] * 69069U);
48 state [2] = max ( 16, state [1] * 69069U); 49 state [2] = max ( 16U, state [1] * 69069U);
49 state [3] = max (128, state [2] * 69069U); 50 state [3] = max (128U, state [2] * 69069U);
50 51
51 for (int i = 11; --i; ) 52 for (int i = 11; --i; )
52 operator ()(); 53 operator ()();
53} 54}
54 55
59 state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U); 60 state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U);
60 state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U); 61 state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U);
61 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); 62 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
62 63
63 return state [0] ^ state [1] ^ state [2] ^ state [3]; 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));
64} 78}
65 79
66/* 80/*
67 * The random functions here take luck into account when rolling random 81 * The random functions here take luck into account when rolling random
68 * 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
104 118
105/* 119/*
106 * 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
107 * for exp loss calculations for players changing religions. 121 * for exp loss calculations for players changing religions.
108 */ 122 */
109
110sint64 123sint64
111random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) 124random_roll64 (sint64 min, sint64 max, const object *op, int goodbad)
112{ 125{
113 sint64 omin, diff, luck, ran; 126 sint64 omin, diff, luck, ran;
114 int base; 127 int base;
121 { 134 {
122 LOG (llevError, "Calling random_roll with min=%" PRId64 " max=%" PRId64 "\n", min, max); 135 LOG (llevError, "Calling random_roll with min=%" PRId64 " max=%" PRId64 "\n", min, max);
123 return (min); /* avoids a float exception */ 136 return (min); /* avoids a float exception */
124 } 137 }
125 138
126 /* Don't know of a portable call to get 64 bit random values. 139 /*
127 * So make a call to get two 32 bit random numbers, and just to 140 * Make a call to get two 32 bit unsigned random numbers, and just to
128 * a little byteshifting. Do make sure the first one is only 141 * a little bitshifting.
129 * 32 bit, so we don't get skewed results
130 */ 142 */
131 ran = (RANDOM () & 0xffffffff) | ((sint64) RANDOM () << 32); 143 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31);
132 144
133 if (op->type != PLAYER) 145 if (op->type != PLAYER)
134 return ((ran % diff) + min); 146 return ((ran % diff) + min);
135 147
136 luck = op->stats.luck; 148 luck = op->stats.luck;
137 if (RANDOM () % base < MIN (10, abs (luck))) 149 if (rndm (base) < MIN (10, abs (luck)))
138 { 150 {
139 /* we have a winner */ 151 /* we have a winner */
140 ((luck > 0) ? (luck = 1) : (luck = -1)); 152 ((luck > 0) ? (luck = 1) : (luck = -1));
141 diff -= luck; 153 diff -= luck;
142 if (diff < 1) 154 if (diff < 1)
143 return (omin); /*check again */ 155 return (omin); /*check again */
144 ((goodbad) ? (min += luck) : (diff)); 156 ((goodbad) ? (min += luck) : (diff));
145 157
146 return (MAX (omin, MIN (max, (ran % diff) + min))); 158 return (MAX (omin, MIN (max, (ran % diff) + min)));
147 } 159 }
160
148 return ((ran % diff) + min); 161 return ((ran % diff) + min);
149} 162}
150 163
151/* 164/*
152 * Roll a number of dice (2d3, 4d6). Uses op to determine luck, 165 * Roll a number of dice (2d3, 4d6). Uses op to determine luck,
157 */ 170 */
158 171
159int 172int
160die_roll (int num, int size, const object *op, int goodbad) 173die_roll (int num, int size, const object *op, int goodbad)
161{ 174{
162 int min, diff, luck, total, i, gotlucky, base, ran; 175 int min, diff, luck, total, i, gotlucky, base;
163 176
164 diff = size; 177 diff = size;
165 min = 1; 178 min = 1;
166 luck = total = gotlucky = 0; 179 luck = total = gotlucky = 0;
167 ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ 180 ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */
174 if (op->type == PLAYER) 187 if (op->type == PLAYER)
175 luck = op->stats.luck; 188 luck = op->stats.luck;
176 189
177 for (i = 0; i < num; i++) 190 for (i = 0; i < num; i++)
178 { 191 {
179 if (RANDOM () % base < MIN (10, abs (luck)) && !gotlucky) 192 if (rndm (base) < MIN (10, abs (luck)) && !gotlucky)
180 { 193 {
181 /* we have a winner */ 194 /* we have a winner */
182 gotlucky++; 195 gotlucky++;
183 ((luck > 0) ? (luck = 1) : (luck = -1)); 196 ((luck > 0) ? (luck = 1) : (luck = -1));
184 diff -= luck; 197 diff -= luck;
185 if (diff < 1) 198 if (diff < 1)
186 return (num); /*check again */ 199 return (num); /*check again */
187 ((goodbad) ? (min += luck) : (diff)); 200 ((goodbad) ? (min += luck) : (diff));
188 ran = RANDOM ();
189 total += MAX (1, MIN (size, (ran % diff) + min)); 201 total += MAX (1, MIN (size, rndm (diff) + min));
190 } 202 }
191 else 203 else
192 {
193 total += RANDOM () % size + 1; 204 total += rndm (size) + 1;
194 }
195 } 205 }
206
196 return (total); 207 return total;
197} 208}
198 209
199/* decay and destroy perishable items in a map */ 210/* decay and destroy perishable items in a map */
200void 211void
201maptile::decay_objects () 212maptile::decay_objects ()
377 { 388 {
378 op->stats.dam += lmt->damage; 389 op->stats.dam += lmt->damage;
379 if (op->stats.dam < 1) 390 if (op->stats.dam < 1)
380 op->stats.dam = 1; 391 op->stats.dam = 1;
381 } 392 }
393
382 if (op->stats.sp && op->type == BOW) 394 if (op->stats.sp && op->type == BOW)
383 op->stats.sp += lmt->sp; 395 op->stats.sp += lmt->sp;
384 if (op->stats.wc && op->is_weapon ()) 396 if (op->stats.wc && op->is_weapon ())
385 op->stats.wc += lmt->wc; 397 op->stats.wc += lmt->wc;
386 if (op->is_armor ()) 398 if (op->is_armor ())
395 op->resist[j] = 100; 407 op->resist[j] = 100;
396 if (op->resist[j] < -100) 408 if (op->resist[j] < -100)
397 op->resist[j] = -100; 409 op->resist[j] = -100;
398 } 410 }
399 } 411 }
400 op->materialname = add_string (lmt->name); 412 op->materialname = lmt->name;
401 /* dont make it unstackable if it doesn't need to be */ 413 /* dont make it unstackable if it doesn't need to be */
402 if (op->is_weapon () || op->is_armor ()) 414 if (op->is_weapon () || op->is_armor ())
403 { 415 {
404 op->weight = (op->weight * lmt->weight) / 100; 416 op->weight = (op->weight * lmt->weight) / 100;
405 op->value = (op->value * lmt->value) / 100; 417 op->value = (op->value * lmt->value) / 100;
556 LOG (llevError, "fork abort: %s\n", msg); 568 LOG (llevError, "fork abort: %s\n", msg);
557} 569}
558 570
559void *salloc_ (int n) throw (std::bad_alloc) 571void *salloc_ (int n) throw (std::bad_alloc)
560{ 572{
573#ifdef PREFER_MALLOC
574 void *ptr = malloc (n);
575#else
561 void *ptr = g_slice_alloc (n); 576 void *ptr = g_slice_alloc (n);
577#endif
562 578
563 if (!ptr) 579 if (!ptr)
564 throw std::bad_alloc (); 580 throw std::bad_alloc ();
565 581
566 return ptr; 582 return ptr;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines