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.42 by root, Fri Jan 19 15:15:49 2007 UTC vs.
Revision 1.46 by root, Mon Feb 5 01:47:22 2007 UTC

42rand_gen rndm; 42rand_gen rndm;
43 43
44void 44void
45tausworthe_random_generator::seed (uint32_t seed) 45tausworthe_random_generator::seed (uint32_t seed)
46{ 46{
47 state [0] = max ( 2, seed * 69069U); 47 state [0] = max ( 2U, seed * 69069U);
48 state [1] = max ( 8, state [0] * 69069U); 48 state [1] = max ( 8U, state [0] * 69069U);
49 state [2] = max ( 16, state [1] * 69069U); 49 state [2] = max ( 16U, state [1] * 69069U);
50 state [3] = max (128, state [2] * 69069U); 50 state [3] = max (128U, state [2] * 69069U);
51 51
52 for (int i = 11; --i; ) 52 for (int i = 11; --i; )
53 operator ()(); 53 operator ()();
54} 54}
55 55
118 118
119/* 119/*
120 * 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
121 * for exp loss calculations for players changing religions. 121 * for exp loss calculations for players changing religions.
122 */ 122 */
123
124sint64 123sint64
125random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) 124random_roll64 (sint64 min, sint64 max, const object *op, int goodbad)
126{ 125{
127 sint64 omin, diff, luck, ran; 126 sint64 omin, diff, luck, ran;
128 int base; 127 int base;
135 { 134 {
136 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);
137 return (min); /* avoids a float exception */ 136 return (min); /* avoids a float exception */
138 } 137 }
139 138
140 /* Don't know of a portable call to get 64 bit random values. 139 /*
141 * 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
142 * a little byteshifting. Do make sure the first one is only 141 * a little bitshifting.
143 * 32 bit, so we don't get skewed results
144 */ 142 */
145 ran = (RANDOM () & 0xffffffff) | ((sint64) RANDOM () << 32); 143 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31);
146 144
147 if (op->type != PLAYER) 145 if (op->type != PLAYER)
148 return ((ran % diff) + min); 146 return ((ran % diff) + min);
149 147
150 luck = op->stats.luck; 148 luck = op->stats.luck;
151 if (RANDOM () % base < MIN (10, abs (luck))) 149 if (rndm (base) < MIN (10, abs (luck)))
152 { 150 {
153 /* we have a winner */ 151 /* we have a winner */
154 ((luck > 0) ? (luck = 1) : (luck = -1)); 152 ((luck > 0) ? (luck = 1) : (luck = -1));
155 diff -= luck; 153 diff -= luck;
156 if (diff < 1) 154 if (diff < 1)
157 return (omin); /*check again */ 155 return (omin); /*check again */
158 ((goodbad) ? (min += luck) : (diff)); 156 ((goodbad) ? (min += luck) : (diff));
159 157
160 return (MAX (omin, MIN (max, (ran % diff) + min))); 158 return (MAX (omin, MIN (max, (ran % diff) + min)));
161 } 159 }
160
162 return ((ran % diff) + min); 161 return ((ran % diff) + min);
163} 162}
164 163
165/* 164/*
166 * 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,
171 */ 170 */
172 171
173int 172int
174die_roll (int num, int size, const object *op, int goodbad) 173die_roll (int num, int size, const object *op, int goodbad)
175{ 174{
176 int min, diff, luck, total, i, gotlucky, base, ran; 175 int min, diff, luck, total, i, gotlucky, base;
177 176
178 diff = size; 177 diff = size;
179 min = 1; 178 min = 1;
180 luck = total = gotlucky = 0; 179 luck = total = gotlucky = 0;
181 ((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 */
188 if (op->type == PLAYER) 187 if (op->type == PLAYER)
189 luck = op->stats.luck; 188 luck = op->stats.luck;
190 189
191 for (i = 0; i < num; i++) 190 for (i = 0; i < num; i++)
192 { 191 {
193 if (RANDOM () % base < MIN (10, abs (luck)) && !gotlucky) 192 if (rndm (base) < MIN (10, abs (luck)) && !gotlucky)
194 { 193 {
195 /* we have a winner */ 194 /* we have a winner */
196 gotlucky++; 195 gotlucky++;
197 ((luck > 0) ? (luck = 1) : (luck = -1)); 196 ((luck > 0) ? (luck = 1) : (luck = -1));
198 diff -= luck; 197 diff -= luck;
199 if (diff < 1) 198 if (diff < 1)
200 return (num); /*check again */ 199 return (num); /*check again */
201 ((goodbad) ? (min += luck) : (diff)); 200 ((goodbad) ? (min += luck) : (diff));
202 ran = RANDOM ();
203 total += MAX (1, MIN (size, (ran % diff) + min)); 201 total += MAX (1, MIN (size, rndm (diff) + min));
204 } 202 }
205 else 203 else
206 {
207 total += RANDOM () % size + 1; 204 total += rndm (size) + 1;
208 }
209 } 205 }
206
210 return (total); 207 return total;
211} 208}
212 209
213/* decay and destroy perishable items in a map */ 210/* decay and destroy perishable items in a map */
214void 211void
215maptile::decay_objects () 212maptile::decay_objects ()
391 { 388 {
392 op->stats.dam += lmt->damage; 389 op->stats.dam += lmt->damage;
393 if (op->stats.dam < 1) 390 if (op->stats.dam < 1)
394 op->stats.dam = 1; 391 op->stats.dam = 1;
395 } 392 }
393
396 if (op->stats.sp && op->type == BOW) 394 if (op->stats.sp && op->type == BOW)
397 op->stats.sp += lmt->sp; 395 op->stats.sp += lmt->sp;
398 if (op->stats.wc && op->is_weapon ()) 396 if (op->stats.wc && op->is_weapon ())
399 op->stats.wc += lmt->wc; 397 op->stats.wc += lmt->wc;
400 if (op->is_armor ()) 398 if (op->is_armor ())
409 op->resist[j] = 100; 407 op->resist[j] = 100;
410 if (op->resist[j] < -100) 408 if (op->resist[j] < -100)
411 op->resist[j] = -100; 409 op->resist[j] = -100;
412 } 410 }
413 } 411 }
414 op->materialname = add_string (lmt->name); 412 op->materialname = lmt->name;
415 /* dont make it unstackable if it doesn't need to be */ 413 /* dont make it unstackable if it doesn't need to be */
416 if (op->is_weapon () || op->is_armor ()) 414 if (op->is_weapon () || op->is_armor ())
417 { 415 {
418 op->weight = (op->weight * lmt->weight) / 100; 416 op->weight = (op->weight * lmt->weight) / 100;
419 op->value = (op->value * lmt->value) / 100; 417 op->value = (op->value * lmt->value) / 100;
570 LOG (llevError, "fork abort: %s\n", msg); 568 LOG (llevError, "fork abort: %s\n", msg);
571} 569}
572 570
573void *salloc_ (int n) throw (std::bad_alloc) 571void *salloc_ (int n) throw (std::bad_alloc)
574{ 572{
573#ifdef PREFER_MALLOC
574 void *ptr = malloc (n);
575#else
575 void *ptr = g_slice_alloc (n); 576 void *ptr = g_slice_alloc (n);
577#endif
576 578
577 if (!ptr) 579 if (!ptr)
578 throw std::bad_alloc (); 580 throw std::bad_alloc ();
579 581
580 return ptr; 582 return ptr;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines