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.82 by root, Thu Sep 11 12:43:17 2008 UTC vs.
Revision 1.86 by root, Mon Jan 12 03:40:21 2009 UTC

51 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; 51 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U;
52 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; 52 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U;
53 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U; 53 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U;
54 54
55 for (int i = 11; --i; ) 55 for (int i = 11; --i; )
56 operator ()(); 56 next ();
57} 57}
58 58
59uint32_t 59uint32_t
60tausworthe_random_generator::next () 60tausworthe_random_generator::next ()
61{ 61{
65 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); 65 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
66 66
67 return state [0] ^ state [1] ^ state [2] ^ state [3]; 67 return state [0] ^ state [1] ^ state [2] ^ state [3];
68} 68}
69 69
70template<class generator>
70uint32_t 71uint32_t
71tausworthe_random_generator::get_range (uint32_t num) 72random_number_generator<generator>::get_range (uint32_t num)
72{ 73{
73 return (next () * (uint64_t)num) >> 32U; 74 return (this->next () * (uint64_t)num) >> 32U;
74} 75}
75 76
76// return a number within (min .. max) 77// return a number within (min .. max)
78template<class generator>
77int 79int
78tausworthe_random_generator::get_range (int r_min, int r_max) 80random_number_generator<generator>::get_range (int r_min, int r_max)
79{ 81{
80 return r_min + get_range (max (r_max - r_min + 1, 0)); 82 return r_min + get_range (max (r_max - r_min + 1, 0));
81} 83}
84
85template struct random_number_generator<tausworthe_random_generator>;
86template struct random_number_generator<xorshift_random_generator>;
82 87
83/* 88/*
84 * The random functions here take luck into account when rolling random 89 * The random functions here take luck into account when rolling random
85 * dice or numbers. This function has less of an impact the larger the 90 * dice or numbers. This function has less of an impact the larger the
86 * difference becomes in the random numbers. IE, the effect is lessened 91 * difference becomes in the random numbers. IE, the effect is lessened
200} 205}
201 206
202/* convert materialname to materialtype_t */ 207/* convert materialname to materialtype_t */
203 208
204materialtype_t * 209materialtype_t *
205name_to_material (const shstr &name) 210name_to_material (const shstr_cmp name)
206{ 211{
207 for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) 212 for (materialtype_t *mt = materialt; mt; mt = mt->next)
208 if (name == mt->name) 213 if (name == mt->name)
209 return mt; 214 return mt;
210 215
211 return 0; 216 return 0;
212} 217}
218transmute_materialname (object *op, const object *change) 223transmute_materialname (object *op, const object *change)
219{ 224{
220 materialtype_t *mt; 225 materialtype_t *mt;
221 int j; 226 int j;
222 227
223 if (op->materialname == NULL) 228 if (!op->materialname)
224 return; 229 return;
225 230
226 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 231 if (op->materialname != change->materialname)
227 return; 232 return;
228 233
229 if (!op->is_armor ()) 234 if (!op->is_armor ())
230 return; 235 return;
231 236
251void 256void
252set_materialname (object *op, int difficulty, materialtype_t *nmt) 257set_materialname (object *op, int difficulty, materialtype_t *nmt)
253{ 258{
254 materialtype_t *mt, *lmt; 259 materialtype_t *mt, *lmt;
255 260
256 if (op->materialname != NULL) 261 if (!op->materialname)
257 return; 262 return;
258 263
259 if (nmt == NULL) 264 if (nmt)
265 lmt = nmt;
266 else
260 { 267 {
261 lmt = NULL; 268 lmt = 0;
262 269
263 for (mt = materialt; mt && mt->next; mt = mt->next) 270 for (mt = materialt; mt; mt = mt->next)
264 if (op->materials & mt->material && rndm (1, 100) <= mt->chance && 271 if (op->materials & mt->material && rndm (1, 100) <= mt->chance &&
265 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 272 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
266 { 273 {
267 lmt = mt; 274 lmt = mt;
268 if (!(op->is_weapon () || op->is_armor ())) 275 if (!(op->is_weapon () || op->is_armor ()))
269 break; 276 break;
270 } 277 }
271 } 278 }
272 else
273 lmt = nmt;
274 279
275 if (lmt != NULL) 280 if (lmt)
276 { 281 {
277 if (op->stats.dam && op->is_weapon ()) 282 if (op->stats.dam && op->is_weapon ())
278 { 283 {
279 op->stats.dam += lmt->damage; 284 op->stats.dam += lmt->damage;
280 if (op->stats.dam < 1) 285 if (op->stats.dam < 1)
554 559
555#endif 560#endif
556 561
557/******************************************************************************/ 562/******************************************************************************/
558 563
564int
559void assign (char *dst, const char *src, int maxlen) 565assign (char *dst, const char *src, int maxsize)
560{ 566{
561 if (!src) 567 if (!src)
562 src = ""; 568 src = "";
563 569
564 int len = strlen (src); 570 int len = strlen (src);
565 571
566 if (len >= maxlen - 1) 572 if (len >= maxsize)
567 { 573 {
568 if (maxlen <= 4) 574 if (maxsize <= 4)
569 { 575 {
570 memset (dst, '.', maxlen - 1); 576 memset (dst, '.', maxsize - 2);
571 dst [maxlen - 1] = 0; 577 dst [maxsize - 1] = 0;
572 } 578 }
573 else 579 else
574 { 580 {
575 memcpy (dst, src, maxlen - 4); 581 memcpy (dst, src, maxsize - 4);
576 memcpy (dst + maxlen - 4, "...", 4); 582 memcpy (dst + maxsize - 4, "...", 4);
577 } 583 }
584
585 len = maxsize;
578 } 586 }
579 else 587 else
580 memcpy (dst, src, len + 1); 588 memcpy (dst, src, ++len);
589
590 return len;
581} 591}
582 592
583const char * 593const char *
584format (const char *format, ...) 594format (const char *format, ...)
585{ 595{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines