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.78 by root, Fri May 16 17:09:38 2008 UTC vs.
Revision 1.84 by root, Tue Dec 30 07:24:16 2008 UTC

33#include <signal.h> 33#include <signal.h>
34 34
35#include <global.h> 35#include <global.h>
36#include <material.h> 36#include <material.h>
37 37
38#include <sys/time.h>
39#include <sys/resource.h>
40
38#include <glib.h> 41#include <glib.h>
39 42
40refcnt_base::refcnt_t refcnt_dummy; 43refcnt_base::refcnt_t refcnt_dummy;
41ssize_t slice_alloc; 44ssize_t slice_alloc;
42rand_gen rndm, rmg_rndm; 45rand_gen rndm, rmg_rndm;
48 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;
49 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;
50 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;
51 54
52 for (int i = 11; --i; ) 55 for (int i = 11; --i; )
53 operator ()(); 56 next ();
54} 57}
55 58
56uint32_t 59uint32_t
57tausworthe_random_generator::next () 60tausworthe_random_generator::next ()
58{ 61{
62 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);
63 66
64 return state [0] ^ state [1] ^ state [2] ^ state [3]; 67 return state [0] ^ state [1] ^ state [2] ^ state [3];
65} 68}
66 69
70template<class generator>
67uint32_t 71uint32_t
68tausworthe_random_generator::get_range (uint32_t num) 72random_number_generator<generator>::get_range (uint32_t num)
69{ 73{
70 return (next () * (uint64_t)num) >> 32U; 74 return (this->next () * (uint64_t)num) >> 32U;
71} 75}
72 76
73// return a number within (min .. max) 77// return a number within (min .. max)
78template<class generator>
74int 79int
75tausworthe_random_generator::get_range (int r_min, int r_max) 80random_number_generator<generator>::get_range (int r_min, int r_max)
76{ 81{
77 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));
78} 83}
84
85template struct random_number_generator<tausworthe_random_generator>;
86template struct random_number_generator<xorshift_random_generator>;
79 87
80/* 88/*
81 * The random functions here take luck into account when rolling random 89 * The random functions here take luck into account when rolling random
82 * 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
83 * difference becomes in the random numbers. IE, the effect is lessened 91 * difference becomes in the random numbers. IE, the effect is lessened
197} 205}
198 206
199/* convert materialname to materialtype_t */ 207/* convert materialname to materialtype_t */
200 208
201materialtype_t * 209materialtype_t *
202name_to_material (const shstr &name) 210name_to_material (const shstr_cmp name)
203{ 211{
204 for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) 212 for (materialtype_t *mt = materialt; mt; mt = mt->next)
205 if (name == mt->name) 213 if (name == mt->name)
206 return mt; 214 return mt;
207 215
208 return 0; 216 return 0;
209} 217}
215transmute_materialname (object *op, const object *change) 223transmute_materialname (object *op, const object *change)
216{ 224{
217 materialtype_t *mt; 225 materialtype_t *mt;
218 int j; 226 int j;
219 227
220 if (op->materialname == NULL) 228 if (!op->materialname)
221 return; 229 return;
222 230
223 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 231 if (change->materialname && strcmp (op->materialname, change->materialname))
224 return; 232 return;
225 233
226 if (!op->is_armor ()) 234 if (!op->is_armor ())
227 return; 235 return;
228 236
248void 256void
249set_materialname (object *op, int difficulty, materialtype_t *nmt) 257set_materialname (object *op, int difficulty, materialtype_t *nmt)
250{ 258{
251 materialtype_t *mt, *lmt; 259 materialtype_t *mt, *lmt;
252 260
253 if (op->materialname != NULL) 261 if (!op->materialname)
254 return; 262 return;
255 263
256 if (nmt == NULL) 264 if (nmt)
265 lmt = nmt;
266 else
257 { 267 {
258 lmt = NULL; 268 lmt = 0;
259 269
260 for (mt = materialt; mt && mt->next; mt = mt->next) 270 for (mt = materialt; mt; mt = mt->next)
261 if (op->materials & mt->material && rndm (1, 100) <= mt->chance && 271 if (op->materials & mt->material && rndm (1, 100) <= mt->chance &&
262 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 272 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
263 { 273 {
264 lmt = mt; 274 lmt = mt;
265 if (!(op->is_weapon () || op->is_armor ())) 275 if (!(op->is_weapon () || op->is_armor ()))
266 break; 276 break;
267 } 277 }
268 } 278 }
269 else
270 lmt = nmt;
271 279
272 if (lmt != NULL) 280 if (lmt)
273 { 281 {
274 if (op->stats.dam && op->is_weapon ()) 282 if (op->stats.dam && op->is_weapon ())
275 { 283 {
276 op->stats.dam += lmt->damage; 284 op->stats.dam += lmt->damage;
277 if (op->stats.dam < 1) 285 if (op->stats.dam < 1)
446void 454void
447fork_abort (const char *msg) 455fork_abort (const char *msg)
448{ 456{
449 if (!fork ()) 457 if (!fork ())
450 { 458 {
459 signal (SIGINT , SIG_IGN);
460 signal (SIGTERM, SIG_IGN);
451 signal (SIGABRT, SIG_DFL); 461 signal (SIGABRT, SIG_IGN);
462
463 signal (SIGSEGV, SIG_DFL);
464 signal (SIGBUS , SIG_DFL);
465 signal (SIGILL , SIG_DFL);
466 signal (SIGTRAP, SIG_DFL);
467
452 // try to put corefiles into a subdirectory, if existing, to allow 468 // try to put corefiles into a subdirectory, if existing, to allow
453 // an administrator to reduce the I/O load. 469 // an administrator to reduce the I/O load.
454 chdir ("cores"); 470 chdir ("cores");
471
472 // try to detach us from as many external dependencies as possible
473 // as coredumping can take time by closing all fd's.
474 {
475 struct rlimit lim;
476
477 if (getrlimit (RLIMIT_NOFILE, &lim))
478 lim.rlim_cur = 1024;
479
480 for (int i = 0; i < lim.rlim_cur; ++i)
481 close (i);
482 }
483
484 {
485 sigset_t empty;
486 sigemptyset (&empty);
487 sigprocmask (SIG_SETMASK, &empty, 0);
488 }
489
490 // try to coredump with SIGTRAP
491 kill (getpid (), SIGTRAP);
455 abort (); 492 abort ();
456 } 493 }
457 494
458 LOG (llevError, "fork abort: %s\n", msg); 495 LOG (llevError, "fork abort: %s\n", msg);
459} 496}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines