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.47 by root, Wed Feb 7 02:04:46 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 ()
254 if (op->stats.food < 0) 251 if (op->stats.food < 0)
255 destroy = 1; 252 destroy = 1;
256 } 253 }
257 else 254 else
258 { 255 {
259 int mat = op->material; 256 int mat = op->materials;
260 257
261 if (mat & M_PAPER 258 if (mat & M_PAPER
262 || mat & M_LEATHER 259 || mat & M_LEATHER
263 || mat & M_WOOD 260 || mat & M_WOOD
264 || mat & M_ORGANIC 261 || mat & M_ORGANIC
279} 276}
280 277
281/* convert materialname to materialtype_t */ 278/* convert materialname to materialtype_t */
282 279
283materialtype_t * 280materialtype_t *
284name_to_material (const char *name) 281name_to_material (const shstr &name)
285{ 282{
286 materialtype_t *mt, *nmt; 283 for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next)
287 284 if (name == mt->name)
288 mt = NULL;
289 for (nmt = materialt; nmt != NULL && nmt->next != NULL; nmt = nmt->next)
290 {
291 if (strcmp (name, nmt->name) == 0)
292 {
293 mt = nmt;
294 break;
295 }
296 }
297 return mt; 285 return mt;
286
287 return materialt;
298} 288}
299 289
300/* when doing transmutation of objects, we have to recheck the resistances, 290/* when doing transmutation of objects, we have to recheck the resistances,
301 * as some that did not apply previously, may apply now. 291 * as some that did not apply previously, may apply now.
302 */ 292 */
351 341
352 if (nmt == NULL) 342 if (nmt == NULL)
353 { 343 {
354 lmt = NULL; 344 lmt = NULL;
355#ifndef NEW_MATERIAL_CODE 345#ifndef NEW_MATERIAL_CODE
356 for (mt = materialt; mt != NULL && mt->next != NULL; mt = mt->next) 346 for (mt = materialt; mt && mt->next; mt = mt->next)
357 { 347 {
358 if (op->material & mt->material) 348 if (op->materials & mt->material)
359 { 349 {
360 lmt = mt; 350 lmt = mt;
361 break; 351 break;
362 } 352 }
363 } 353 }
364 354
365#else 355#else
366 for (mt = materialt; mt != NULL && mt->next != NULL; mt = mt->next) 356 for (mt = materialt; mt && mt->next; mt = mt->next)
367 { 357 {
368 if (op->material & mt->material && rndm (1, 100) <= mt->chance && 358 if (op->materials & mt->material && rndm (1, 100) <= mt->chance &&
369 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 359 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
370 { 360 {
371 lmt = mt; 361 lmt = mt;
372 if (!(op->is_weapon () || op->is_armor ())) 362 if (!(op->is_weapon () || op->is_armor ()))
373 break; 363 break;
391 { 381 {
392 op->stats.dam += lmt->damage; 382 op->stats.dam += lmt->damage;
393 if (op->stats.dam < 1) 383 if (op->stats.dam < 1)
394 op->stats.dam = 1; 384 op->stats.dam = 1;
395 } 385 }
386
396 if (op->stats.sp && op->type == BOW) 387 if (op->stats.sp && op->type == BOW)
397 op->stats.sp += lmt->sp; 388 op->stats.sp += lmt->sp;
398 if (op->stats.wc && op->is_weapon ()) 389 if (op->stats.wc && op->is_weapon ())
399 op->stats.wc += lmt->wc; 390 op->stats.wc += lmt->wc;
400 if (op->is_armor ()) 391 if (op->is_armor ())
409 op->resist[j] = 100; 400 op->resist[j] = 100;
410 if (op->resist[j] < -100) 401 if (op->resist[j] < -100)
411 op->resist[j] = -100; 402 op->resist[j] = -100;
412 } 403 }
413 } 404 }
414 op->materialname = add_string (lmt->name); 405 op->materialname = lmt->name;
415 /* dont make it unstackable if it doesn't need to be */ 406 /* dont make it unstackable if it doesn't need to be */
416 if (op->is_weapon () || op->is_armor ()) 407 if (op->is_weapon () || op->is_armor ())
417 { 408 {
418 op->weight = (op->weight * lmt->weight) / 100; 409 op->weight = (op->weight * lmt->weight) / 100;
419 op->value = (op->value * lmt->value) / 100; 410 op->value = (op->value * lmt->value) / 100;
570 LOG (llevError, "fork abort: %s\n", msg); 561 LOG (llevError, "fork abort: %s\n", msg);
571} 562}
572 563
573void *salloc_ (int n) throw (std::bad_alloc) 564void *salloc_ (int n) throw (std::bad_alloc)
574{ 565{
566#ifdef PREFER_MALLOC
567 void *ptr = malloc (n);
568#else
575 void *ptr = g_slice_alloc (n); 569 void *ptr = g_slice_alloc (n);
570#endif
576 571
577 if (!ptr) 572 if (!ptr)
578 throw std::bad_alloc (); 573 throw std::bad_alloc ();
579 574
580 return ptr; 575 return ptr;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines