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.63 by root, Mon Aug 6 10:54:11 2007 UTC vs.
Revision 1.87 by sf-marcmagus, Sun Oct 11 01:35:53 2009 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT 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 * Crossfire TRT 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
10 * the Free Software Foundation, either version 3 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * 20 *
21 * The authors can be reached via e-mail to <crossfire@schmorp.de> 21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 22 */
23 23
24/* 24/*
25 * General convenience functions for crossfire. 25 * General convenience functions for crossfire.
26 */ 26 */
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;
42 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}
221 219
222/* when doing transmutation of objects, we have to recheck the resistances, 220/* when doing transmutation of objects, we have to recheck the resistances,
223 * as some that did not apply previously, may apply now. 221 * as some that did not apply previously, may apply now.
224 */ 222 */
225
226void 223void
227transmute_materialname (object *op, const object *change) 224transmute_materialname (object *op, const object *change)
228{ 225{
229 materialtype_t *mt; 226 materialtype_t *mt;
230 int j; 227 int j;
231 228
232 if (op->materialname == NULL) 229 if (!op->materialname)
233 return; 230 return;
234 231
235 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 232 if (op->materialname != change->materialname)
236 return; 233 return;
237 234
238 if (!op->is_armor ()) 235 if (!op->is_armor ())
239 return; 236 return;
240 237
260void 257void
261set_materialname (object *op, int difficulty, materialtype_t *nmt) 258set_materialname (object *op, int difficulty, materialtype_t *nmt)
262{ 259{
263 materialtype_t *mt, *lmt; 260 materialtype_t *mt, *lmt;
264 261
265 if (op->materialname != NULL) 262 if (!op->materialname)
266 return; 263 return;
267 264
268 if (nmt == NULL) 265 if (nmt)
266 lmt = nmt;
267 else
269 { 268 {
270 lmt = NULL; 269 lmt = 0;
271 270
272 for (mt = materialt; mt && mt->next; mt = mt->next) 271 for (mt = materialt; mt; mt = mt->next)
273 if (op->materials & mt->material && rndm (1, 100) <= mt->chance && 272 if (op->materials & mt->material && rndm (1, 100) <= mt->chance &&
274 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 273 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
275 { 274 {
276 lmt = mt; 275 lmt = mt;
277 if (!(op->is_weapon () || op->is_armor ())) 276 if (!(op->is_weapon () || op->is_armor ()))
278 break; 277 break;
279 } 278 }
280 } 279 }
281 else
282 lmt = nmt;
283 280
284 if (lmt != NULL) 281 if (lmt)
285 { 282 {
286 if (op->stats.dam && op->is_weapon ()) 283 if (op->stats.dam && op->is_weapon ())
287 { 284 {
288 op->stats.dam += lmt->damage; 285 op->stats.dam += lmt->damage;
289 if (op->stats.dam < 1) 286 if (op->stats.dam < 1)
419 * a 'list' for the purposes here, is a string of items, seperated by commas, except 416 * a 'list' for the purposes here, is a string of items, seperated by commas, except
420 * for the last entry, which has an 'and' before it, and a full stop (period) after it. 417 * for the last entry, which has an 'and' before it, and a full stop (period) after it.
421 * This function will also strip all trailing non alphanumeric characters. 418 * This function will also strip all trailing non alphanumeric characters.
422 * It does not insert an oxford comma. 419 * It does not insert an oxford comma.
423 */ 420 */
424
425void 421void
426make_list_like (char *input) 422make_list_like (char *input)
427{ 423{
428 char *p, tmp[MAX_BUF]; 424 char *p, tmp[MAX_BUF];
429 int i; 425 int i;
452 strcpy (input, tmp); 448 strcpy (input, tmp);
453 449
454 return; 450 return;
455} 451}
456 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
457///////////////////////////////////////////////////////////////////////////// 474/////////////////////////////////////////////////////////////////////////////
458 475
459void 476void
460fork_abort (const char *msg) 477fork_abort (const char *msg)
461{ 478{
462 if (!fork ()) 479 if (!fork ())
463 { 480 {
481 signal (SIGINT , SIG_IGN);
482 signal (SIGTERM, SIG_IGN);
464 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
465 // try to put corefiles into a subdirectory, if existing, to allow 490 // try to put corefiles into a subdirectory, if existing, to allow
466 // an administrator to reduce the I/O load. 491 // an administrator to reduce the I/O load.
467 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);
468 abort (); 514 abort ();
469 } 515 }
470 516
471 LOG (llevError, "fork abort: %s\n", msg); 517 LOG (llevError, "fork abort: %s\n", msg);
472} 518}
473 519
474void *salloc_ (int n) throw (std::bad_alloc) 520void *salloc_ (int n) throw (std::bad_alloc)
475{ 521{
476#ifdef PREFER_MALLOC
477 void *ptr = malloc (n);
478#else
479 void *ptr = g_slice_alloc (n); 522 void *ptr = g_slice_alloc (n);
480#endif
481 523
482 if (!ptr) 524 if (!ptr)
483 throw std::bad_alloc (); 525 throw std::bad_alloc ();
484 526
527 slice_alloc += n;
485 return ptr; 528 return ptr;
486} 529}
487 530
488void *salloc_ (int n, void *src) throw (std::bad_alloc) 531void *salloc_ (int n, void *src) throw (std::bad_alloc)
489{ 532{
495 memset (ptr, 0, n); 538 memset (ptr, 0, n);
496 539
497 return ptr; 540 return ptr;
498} 541}
499 542
543/******************************************************************************/
544
545#if DEBUG_SALLOC
546
547#define MAGIC 0xa1b2c35543deadLL
548
549void *g_slice_alloc (unsigned long size)
550{
551 unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long));
552 *p++ = size ^ MAGIC;
553 //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D
554 return (void *)p;
555}
556
557void *g_slice_alloc0 (unsigned long size)
558{
559 return memset (g_slice_alloc (size), 0, size);
560}
561
562void g_slice_free1 (unsigned long size, void *ptr)
563{
564 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D
565 if (expect_true (ptr))
566 {
567 unsigned long *p = (unsigned long *)ptr;
568 unsigned long s = *--p ^ MAGIC;
569
570 if (size != (unsigned long)(*p ^ MAGIC))
571 {
572 LOG (logBacktrace | llevError, "slice free size (%lx) doesn't match alloc size (%lx)\n", size, s);
573 abort ();
574 }
575
576 *p = MAGIC;
577
578 (g_slice_free1)(s + sizeof (unsigned long), p);
579 }
580}
581
582#endif
583
584/******************************************************************************/
585
586int
500void assign (char *dst, const char *src, int maxlen) 587assign (char *dst, const char *src, int maxsize)
501{ 588{
502 if (!src) 589 if (!src)
503 src = ""; 590 src = "";
504 591
505 int len = strlen (src); 592 int len = strlen (src);
506 593
507 if (len >= maxlen - 1) 594 if (len >= maxsize)
508 { 595 {
509 if (maxlen <= 4) 596 if (maxsize <= 4)
510 { 597 {
511 memset (dst, '.', maxlen - 1); 598 memset (dst, '.', maxsize - 2);
512 dst [maxlen - 1] = 0; 599 dst [maxsize - 1] = 0;
513 } 600 }
514 else 601 else
515 { 602 {
516 memcpy (dst, src, maxlen - 4); 603 memcpy (dst, src, maxsize - 4);
517 memcpy (dst + maxlen - 4, "...", 4); 604 memcpy (dst + maxsize - 4, "...", 4);
518 } 605 }
606
607 len = maxsize;
519 } 608 }
520 else 609 else
521 memcpy (dst, src, len + 1); 610 memcpy (dst, src, ++len);
522}
523 611
524const std::string 612 return len;
613}
614
615const char *
525format (const char *format, ...) 616format (const char *format, ...)
526{ 617{
527 int len; 618 static dynbuf_text buf;
528 619
529 { 620 buf.clear ();
530 char buf[128];
531 621
532 va_list ap; 622 va_list ap;
533 va_start (ap, format); 623 va_start (ap, format);
534 len = vsnprintf (buf, sizeof (buf), format, ap); 624 buf.vprintf (format, ap);
535 va_end (ap); 625 va_end (ap);
536 626
537 assert (len >= 0); // shield againstz broken vsnprintf's
538
539 // was our static buffer short enough?
540 if (len < sizeof (buf))
541 return std::string (buf, len);
542 }
543
544 {
545 // longer, try harder
546 char *buf = salloc<char> (len + 1);
547
548 va_list ap;
549 va_start (ap, format);
550 vsnprintf (buf, len + 1, format, ap);
551 va_end (ap);
552
553 const std::string s (buf, len);
554 sfree<char> (buf, len + 1);
555
556 return buf; 627 return buf;
557 }
558} 628}
559 629
560tstamp now () 630tstamp now ()
561{ 631{
562 struct timeval tv; 632 struct timeval tv;
630 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, 700 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
631 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, 701 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
632 0x2d02ef8dL 702 0x2d02ef8dL
633}; 703};
634 704
635#if 0 705void thread::start (void *(*start_routine)(void *), void *arg)
636void xyzzy (object_ptr &a, object_ptr &o)
637{ 706{
638 asm volatile ("int3"); 707 pthread_attr_t attr;
639 a = o; 708
640 asm volatile ("int3"); 709 pthread_attr_init (&attr);
641} 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);
642#endif 715#endif
643 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