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.76 by root, Mon Apr 21 23:35:24 2008 UTC vs.
Revision 1.85 by root, Wed Dec 31 17:35:37 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; 45rand_gen rndm, rmg_rndm;
43 46
44void 47void
45tausworthe_random_generator::seed (uint32_t seed) 48tausworthe_random_generator::seed (uint32_t seed)
46{ 49{
47 state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U; 50 state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U;
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
93 * not the recipient (ie, the poor slob getting hit). [garbled 20010916] 101 * not the recipient (ie, the poor slob getting hit). [garbled 20010916]
94 */ 102 */
95int 103int
96random_roll (int r_min, int r_max, const object *op, int goodbad) 104random_roll (int r_min, int r_max, const object *op, int goodbad)
97{ 105{
106 r_max = max (r_min, r_max);
107
98 int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ 108 int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */
99
100 if (r_max < r_min)
101 {
102 LOG (llevError | logBacktrace, "Calling random_roll with min=%d max=%d\n", r_min, r_max);
103 return r_min;
104 }
105 109
106 if (op->type == PLAYER) 110 if (op->type == PLAYER)
107 { 111 {
108 int luck = op->stats.luck; 112 int luck = op->stats.luck;
109 113
119/* 123/*
120 * This is a 64 bit version of random_roll above. This is needed 124 * This is a 64 bit version of random_roll above. This is needed
121 * for exp loss calculations for players changing religions. 125 * for exp loss calculations for players changing religions.
122 */ 126 */
123sint64 127sint64
124random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) 128random_roll64 (sint64 r_min, sint64 r_max, const object *op, int goodbad)
125{ 129{
126 sint64 omin = min; 130 sint64 omin = r_min;
127 sint64 diff = max - min + 1; 131 sint64 range = max (0, r_max - r_min + 1);
128 int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ 132 int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */
129
130 if (diff < 0)
131 {
132 LOG (llevError | logBacktrace, "Calling random_roll64 with min=%" PRId64 " max=%" PRId64 "\n", min, max);
133 return (min); /* avoids a float exception */
134 }
135 133
136 /* 134 /*
137 * Make a call to get two 32 bit unsigned random numbers, and just to 135 * Make a call to get two 32 bit unsigned random numbers, and just to
138 * a little bitshifting. 136 * a little bitshifting.
139 */ 137 */
140 sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); 138 sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31);
141 139
142 if (op->type != PLAYER) 140 if (op->type != PLAYER)
143 return ((ran % diff) + min); 141 return ((ran % range) + r_min);
144 142
145 int luck = op->stats.luck; 143 int luck = op->stats.luck;
146 144
147 if (rndm (base) < MIN (10, abs (luck))) 145 if (rndm (base) < min (10, abs (luck)))
148 { 146 {
149 /* we have a winner */ 147 /* we have a winner */
150 ((luck > 0) ? (luck = 1) : (luck = -1)); 148 ((luck > 0) ? (luck = 1) : (luck = -1));
151 diff -= luck; 149 range -= luck;
152 if (diff < 1) 150 if (range < 1)
153 return (omin); /*check again */ 151 return (omin); /*check again */
154 152
155 ((goodbad) ? (min += luck) : (diff)); 153 ((goodbad) ? (r_min += luck) : (range));
156 154
157 return (MAX (omin, MIN (max, (ran % diff) + min))); 155 return (max (omin, min (r_max, (ran % range) + r_min)));
158 } 156 }
159 157
160 return ran % diff + min; 158 return ran % range + r_min;
161} 159}
162 160
163/* 161/*
164 * Roll a number of dice (2d3, 4d6). Uses op to determine luck, 162 * Roll a number of dice (2d3, 4d6). Uses op to determine luck,
165 * If goodbad is non-zero, luck increases the roll, if zero, it decreases. 163 * If goodbad is non-zero, luck increases the roll, if zero, it decreases.
207} 205}
208 206
209/* convert materialname to materialtype_t */ 207/* convert materialname to materialtype_t */
210 208
211materialtype_t * 209materialtype_t *
212name_to_material (const shstr &name) 210name_to_material (const shstr_cmp name)
213{ 211{
214 for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) 212 for (materialtype_t *mt = materialt; mt; mt = mt->next)
215 if (name == mt->name) 213 if (name == mt->name)
216 return mt; 214 return mt;
217 215
218 return 0; 216 return 0;
219} 217}
225transmute_materialname (object *op, const object *change) 223transmute_materialname (object *op, const object *change)
226{ 224{
227 materialtype_t *mt; 225 materialtype_t *mt;
228 int j; 226 int j;
229 227
230 if (op->materialname == NULL) 228 if (!op->materialname)
231 return; 229 return;
232 230
233 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 231 if (op->materialname != change->materialname)
234 return; 232 return;
235 233
236 if (!op->is_armor ()) 234 if (!op->is_armor ())
237 return; 235 return;
238 236
258void 256void
259set_materialname (object *op, int difficulty, materialtype_t *nmt) 257set_materialname (object *op, int difficulty, materialtype_t *nmt)
260{ 258{
261 materialtype_t *mt, *lmt; 259 materialtype_t *mt, *lmt;
262 260
263 if (op->materialname != NULL) 261 if (!op->materialname)
264 return; 262 return;
265 263
266 if (nmt == NULL) 264 if (nmt)
265 lmt = nmt;
266 else
267 { 267 {
268 lmt = NULL; 268 lmt = 0;
269 269
270 for (mt = materialt; mt && mt->next; mt = mt->next) 270 for (mt = materialt; mt; mt = mt->next)
271 if (op->materials & mt->material && rndm (1, 100) <= mt->chance && 271 if (op->materials & mt->material && rndm (1, 100) <= mt->chance &&
272 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 272 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
273 { 273 {
274 lmt = mt; 274 lmt = mt;
275 if (!(op->is_weapon () || op->is_armor ())) 275 if (!(op->is_weapon () || op->is_armor ()))
276 break; 276 break;
277 } 277 }
278 } 278 }
279 else
280 lmt = nmt;
281 279
282 if (lmt != NULL) 280 if (lmt)
283 { 281 {
284 if (op->stats.dam && op->is_weapon ()) 282 if (op->stats.dam && op->is_weapon ())
285 { 283 {
286 op->stats.dam += lmt->damage; 284 op->stats.dam += lmt->damage;
287 if (op->stats.dam < 1) 285 if (op->stats.dam < 1)
456void 454void
457fork_abort (const char *msg) 455fork_abort (const char *msg)
458{ 456{
459 if (!fork ()) 457 if (!fork ())
460 { 458 {
459 signal (SIGINT , SIG_IGN);
460 signal (SIGTERM, SIG_IGN);
461 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
462 // try to put corefiles into a subdirectory, if existing, to allow 468 // try to put corefiles into a subdirectory, if existing, to allow
463 // an administrator to reduce the I/O load. 469 // an administrator to reduce the I/O load.
464 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);
465 abort (); 492 abort ();
466 } 493 }
467 494
468 LOG (llevError, "fork abort: %s\n", msg); 495 LOG (llevError, "fork abort: %s\n", msg);
469} 496}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines