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.69 by root, Tue Jan 22 15:53:01 2008 UTC vs.
Revision 1.87 by sf-marcmagus, Sun Oct 11 01:35:53 2009 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
31#include <sys/time.h> 31#include <sys/time.h>
32#include <time.h> 32#include <time.h>
33#include <signal.h> 33#include <signal.h>
34 34
35#include <global.h> 35#include <global.h>
36#include <funcpoint.h>
37#include <material.h> 36#include <material.h>
37#include <object.h>
38
39#include <sys/time.h>
40#include <sys/resource.h>
38 41
39#include <glib.h> 42#include <glib.h>
40 43
41refcnt_base::refcnt_t refcnt_dummy; 44refcnt_base::refcnt_t refcnt_dummy;
42size_t slice_alloc; 45ssize_t slice_alloc;
43rand_gen rndm; 46rand_gen rndm, rmg_rndm;
44 47
45void 48void
46tausworthe_random_generator::seed (uint32_t seed) 49tausworthe_random_generator::seed (uint32_t seed)
47{ 50{
48 state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U; 51 state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U;
49 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; 52 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U;
50 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; 53 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U;
51 state [3] = state [2] * 69069U; if (state [0] < 128) state [0] += 128U; 54 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U;
52 55
53 for (int i = 11; --i; ) 56 for (int i = 11; --i; )
54 operator ()(); 57 next ();
55} 58}
56 59
57uint32_t 60uint32_t
58tausworthe_random_generator::next () 61tausworthe_random_generator::next ()
59{ 62{
63 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); 66 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
64 67
65 return state [0] ^ state [1] ^ state [2] ^ state [3]; 68 return state [0] ^ state [1] ^ state [2] ^ state [3];
66} 69}
67 70
71template<class generator>
68uint32_t 72uint32_t
69tausworthe_random_generator::get_range (uint32_t num) 73random_number_generator<generator>::get_range (uint32_t num)
70{ 74{
71 return (next () * (uint64_t)num) >> 32U; 75 return (this->next () * (uint64_t)num) >> 32U;
72} 76}
73 77
74// return a number within (min .. max) 78// return a number within (min .. max)
79template<class generator>
75int 80int
76tausworthe_random_generator::get_range (int r_min, int r_max) 81random_number_generator<generator>::get_range (int r_min, int r_max)
77{ 82{
78 return r_min + get_range (max (r_max - r_min + 1, 0)); 83 return r_min + get_range (max (r_max - r_min + 1, 0));
79} 84}
85
86template struct random_number_generator<tausworthe_random_generator>;
87template struct random_number_generator<xorshift_random_generator>;
80 88
81/* 89/*
82 * The random functions here take luck into account when rolling random 90 * The random functions here take luck into account when rolling random
83 * dice or numbers. This function has less of an impact the larger the 91 * dice or numbers. This function has less of an impact the larger the
84 * difference becomes in the random numbers. IE, the effect is lessened 92 * difference becomes in the random numbers. IE, the effect is lessened
94 * not the recipient (ie, the poor slob getting hit). [garbled 20010916] 102 * not the recipient (ie, the poor slob getting hit). [garbled 20010916]
95 */ 103 */
96int 104int
97random_roll (int r_min, int r_max, const object *op, int goodbad) 105random_roll (int r_min, int r_max, const object *op, int goodbad)
98{ 106{
107 r_max = max (r_min, r_max);
108
99 int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ 109 int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */
100
101 if (r_max < r_min)
102 {
103 LOG (llevError | logBacktrace, "Calling random_roll with min=%d max=%d\n", r_min, r_max);
104 return r_min;
105 }
106 110
107 if (op->type == PLAYER) 111 if (op->type == PLAYER)
108 { 112 {
109 int luck = op->stats.luck; 113 int luck = op->stats.luck;
110 114
120/* 124/*
121 * This is a 64 bit version of random_roll above. This is needed 125 * This is a 64 bit version of random_roll above. This is needed
122 * for exp loss calculations for players changing religions. 126 * for exp loss calculations for players changing religions.
123 */ 127 */
124sint64 128sint64
125random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) 129random_roll64 (sint64 r_min, sint64 r_max, const object *op, int goodbad)
126{ 130{
127 sint64 omin = min; 131 sint64 omin = r_min;
128 sint64 diff = max - min + 1; 132 sint64 range = max (0, r_max - r_min + 1);
129 int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ 133 int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */
130
131 if (diff < 0)
132 {
133 LOG (llevError | logBacktrace, "Calling random_roll64 with min=%" PRId64 " max=%" PRId64 "\n", min, max);
134 return (min); /* avoids a float exception */
135 }
136 134
137 /* 135 /*
138 * Make a call to get two 32 bit unsigned random numbers, and just to 136 * Make a call to get two 32 bit unsigned random numbers, and just to
139 * a little bitshifting. 137 * a little bitshifting.
140 */ 138 */
141 sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); 139 sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31);
142 140
143 if (op->type != PLAYER) 141 if (op->type != PLAYER)
144 return ((ran % diff) + min); 142 return ((ran % range) + r_min);
145 143
146 int luck = op->stats.luck; 144 int luck = op->stats.luck;
147 145
148 if (rndm (base) < MIN (10, abs (luck))) 146 if (rndm (base) < min (10, abs (luck)))
149 { 147 {
150 /* we have a winner */ 148 /* we have a winner */
151 ((luck > 0) ? (luck = 1) : (luck = -1)); 149 ((luck > 0) ? (luck = 1) : (luck = -1));
152 diff -= luck; 150 range -= luck;
153 if (diff < 1) 151 if (range < 1)
154 return (omin); /*check again */ 152 return (omin); /*check again */
155 153
156 ((goodbad) ? (min += luck) : (diff)); 154 ((goodbad) ? (r_min += luck) : (range));
157 155
158 return (MAX (omin, MIN (max, (ran % diff) + min))); 156 return (max (omin, min (r_max, (ran % range) + r_min)));
159 } 157 }
160 158
161 return ran % diff + min; 159 return ran % range + r_min;
162} 160}
163 161
164/* 162/*
165 * Roll a number of dice (2d3, 4d6). Uses op to determine luck, 163 * Roll a number of dice (2d3, 4d6). Uses op to determine luck,
166 * If goodbad is non-zero, luck increases the roll, if zero, it decreases. 164 * If goodbad is non-zero, luck increases the roll, if zero, it decreases.
208} 206}
209 207
210/* convert materialname to materialtype_t */ 208/* convert materialname to materialtype_t */
211 209
212materialtype_t * 210materialtype_t *
213name_to_material (const shstr &name) 211name_to_material (const shstr_cmp name)
214{ 212{
215 for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) 213 for (materialtype_t *mt = materialt; mt; mt = mt->next)
216 if (name == mt->name) 214 if (name == mt->name)
217 return mt; 215 return mt;
218 216
219 return 0; 217 return 0;
220} 218}
226transmute_materialname (object *op, const object *change) 224transmute_materialname (object *op, const object *change)
227{ 225{
228 materialtype_t *mt; 226 materialtype_t *mt;
229 int j; 227 int j;
230 228
231 if (op->materialname == NULL) 229 if (!op->materialname)
232 return; 230 return;
233 231
234 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 232 if (op->materialname != change->materialname)
235 return; 233 return;
236 234
237 if (!op->is_armor ()) 235 if (!op->is_armor ())
238 return; 236 return;
239 237
259void 257void
260set_materialname (object *op, int difficulty, materialtype_t *nmt) 258set_materialname (object *op, int difficulty, materialtype_t *nmt)
261{ 259{
262 materialtype_t *mt, *lmt; 260 materialtype_t *mt, *lmt;
263 261
264 if (op->materialname != NULL) 262 if (!op->materialname)
265 return; 263 return;
266 264
267 if (nmt == NULL) 265 if (nmt)
266 lmt = nmt;
267 else
268 { 268 {
269 lmt = NULL; 269 lmt = 0;
270 270
271 for (mt = materialt; mt && mt->next; mt = mt->next) 271 for (mt = materialt; mt; mt = mt->next)
272 if (op->materials & mt->material && rndm (1, 100) <= mt->chance && 272 if (op->materials & mt->material && rndm (1, 100) <= mt->chance &&
273 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 273 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
274 { 274 {
275 lmt = mt; 275 lmt = mt;
276 if (!(op->is_weapon () || op->is_armor ())) 276 if (!(op->is_weapon () || op->is_armor ()))
277 break; 277 break;
278 } 278 }
279 } 279 }
280 else
281 lmt = nmt;
282 280
283 if (lmt != NULL) 281 if (lmt)
284 { 282 {
285 if (op->stats.dam && op->is_weapon ()) 283 if (op->stats.dam && op->is_weapon ())
286 { 284 {
287 op->stats.dam += lmt->damage; 285 op->stats.dam += lmt->damage;
288 if (op->stats.dam < 1) 286 if (op->stats.dam < 1)
450 strcpy (input, tmp); 448 strcpy (input, tmp);
451 449
452 return; 450 return;
453} 451}
454 452
453/******************************************************************************/
454
455/* Checks a player-provided string which will become the msg property of
456 * an object for dangerous input.
457 */
458bool
459msg_is_safe (const char *msg)
460{
461 bool safe = true;
462
463 /* Trying to cheat by getting data into the object */
464 if (!strncmp (msg, "endmsg", strlen ("endmsg")) || strstr (msg, "\nendmsg"))
465 safe = false;
466
467 /* Trying to make the object talk, and potentially access arbitrary code */
468 if (object::msg_has_dialogue (msg))
469 safe = false;
470
471 return safe;
472}
473
455///////////////////////////////////////////////////////////////////////////// 474/////////////////////////////////////////////////////////////////////////////
456 475
457void 476void
458fork_abort (const char *msg) 477fork_abort (const char *msg)
459{ 478{
460 if (!fork ()) 479 if (!fork ())
461 { 480 {
481 signal (SIGINT , SIG_IGN);
482 signal (SIGTERM, SIG_IGN);
462 signal (SIGABRT, SIG_DFL); 483 signal (SIGABRT, SIG_IGN);
484
485 signal (SIGSEGV, SIG_DFL);
486 signal (SIGBUS , SIG_DFL);
487 signal (SIGILL , SIG_DFL);
488 signal (SIGTRAP, SIG_DFL);
489
463 // try to put corefiles into a subdirectory, if existing, to allow 490 // try to put corefiles into a subdirectory, if existing, to allow
464 // an administrator to reduce the I/O load. 491 // an administrator to reduce the I/O load.
465 chdir ("cores"); 492 chdir ("cores");
493
494 // try to detach us from as many external dependencies as possible
495 // as coredumping can take time by closing all fd's.
496 {
497 struct rlimit lim;
498
499 if (getrlimit (RLIMIT_NOFILE, &lim))
500 lim.rlim_cur = 1024;
501
502 for (int i = 0; i < lim.rlim_cur; ++i)
503 close (i);
504 }
505
506 {
507 sigset_t empty;
508 sigemptyset (&empty);
509 sigprocmask (SIG_SETMASK, &empty, 0);
510 }
511
512 // try to coredump with SIGTRAP
513 kill (getpid (), SIGTRAP);
466 abort (); 514 abort ();
467 } 515 }
468 516
469 LOG (llevError, "fork abort: %s\n", msg); 517 LOG (llevError, "fork abort: %s\n", msg);
470} 518}
471 519
472void *salloc_ (int n) throw (std::bad_alloc) 520void *salloc_ (int n) throw (std::bad_alloc)
473{ 521{
474#ifdef PREFER_MALLOC
475 void *ptr = malloc (n);
476#else
477 slice_alloc += n;
478 void *ptr = g_slice_alloc (n); 522 void *ptr = g_slice_alloc (n);
479#endif
480 523
481 if (!ptr) 524 if (!ptr)
482 throw std::bad_alloc (); 525 throw std::bad_alloc ();
483 526
527 slice_alloc += n;
484 return ptr; 528 return ptr;
485} 529}
486 530
487void *salloc_ (int n, void *src) throw (std::bad_alloc) 531void *salloc_ (int n, void *src) throw (std::bad_alloc)
488{ 532{
496 return ptr; 540 return ptr;
497} 541}
498 542
499/******************************************************************************/ 543/******************************************************************************/
500 544
501#ifdef DEBUG_SALLOC 545#if DEBUG_SALLOC
502 546
503#define MAGIC 0xa1b2c35543deadL 547#define MAGIC 0xa1b2c35543deadLL
504 548
505void *g_slice_alloc (unsigned long size) 549void *g_slice_alloc (unsigned long size)
506{ 550{
507 unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long)); 551 unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long));
508 *p++ = size ^ MAGIC; 552 *p++ = size ^ MAGIC;
515 return memset (g_slice_alloc (size), 0, size); 559 return memset (g_slice_alloc (size), 0, size);
516} 560}
517 561
518void g_slice_free1 (unsigned long size, void *ptr) 562void g_slice_free1 (unsigned long size, void *ptr)
519{ 563{
564 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D
520 if (expect_true (ptr)) 565 if (expect_true (ptr))
521 { 566 {
522 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D
523 unsigned long *p = (unsigned long *)ptr; 567 unsigned long *p = (unsigned long *)ptr;
524 unsigned long s = *--p ^ MAGIC; 568 unsigned long s = *--p ^ MAGIC;
525 569
526 if ((*p ^ MAGIC) != size) 570 if (size != (unsigned long)(*p ^ MAGIC))
571 {
527 LOG (logBacktrace | llevError, "slice free size (%lx) doesn't match alloc size (%lx)\n", size, s); 572 LOG (logBacktrace | llevError, "slice free size (%lx) doesn't match alloc size (%lx)\n", size, s);
573 abort ();
574 }
528 575
529 *p = MAGIC; 576 *p = MAGIC;
530 577
531 (g_slice_free1)(s + sizeof (unsigned long), p); 578 (g_slice_free1)(s + sizeof (unsigned long), p);
532 } 579 }
534 581
535#endif 582#endif
536 583
537/******************************************************************************/ 584/******************************************************************************/
538 585
586int
539void assign (char *dst, const char *src, int maxlen) 587assign (char *dst, const char *src, int maxsize)
540{ 588{
541 if (!src) 589 if (!src)
542 src = ""; 590 src = "";
543 591
544 int len = strlen (src); 592 int len = strlen (src);
545 593
546 if (len >= maxlen - 1) 594 if (len >= maxsize)
547 { 595 {
548 if (maxlen <= 4) 596 if (maxsize <= 4)
549 { 597 {
550 memset (dst, '.', maxlen - 1); 598 memset (dst, '.', maxsize - 2);
551 dst [maxlen - 1] = 0; 599 dst [maxsize - 1] = 0;
552 } 600 }
553 else 601 else
554 { 602 {
555 memcpy (dst, src, maxlen - 4); 603 memcpy (dst, src, maxsize - 4);
556 memcpy (dst + maxlen - 4, "...", 4); 604 memcpy (dst + maxsize - 4, "...", 4);
557 } 605 }
606
607 len = maxsize;
558 } 608 }
559 else 609 else
560 memcpy (dst, src, len + 1); 610 memcpy (dst, src, ++len);
611
612 return len;
561} 613}
562 614
563const char * 615const char *
564format (const char *format, ...) 616format (const char *format, ...)
565{ 617{
648 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, 700 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
649 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, 701 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
650 0x2d02ef8dL 702 0x2d02ef8dL
651}; 703};
652 704
705void thread::start (void *(*start_routine)(void *), void *arg)
706{
707 pthread_attr_t attr;
708
709 pthread_attr_init (&attr);
710 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
711 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096
712 ? sizeof (long) * 4096 : PTHREAD_STACK_MIN);
713#ifdef PTHREAD_SCOPE_PROCESS
714 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
715#endif
716
717 sigset_t fullsigset, oldsigset;
718 sigfillset (&fullsigset);
719
720 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
721
722 if (pthread_create (&id, &attr, start_routine, arg))
723 cleanup ("unable to create a new thread");
724
725 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
726}
727

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines