… | |
… | |
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 | |
40 | refcnt_base::refcnt_t refcnt_dummy; |
43 | refcnt_base::refcnt_t refcnt_dummy; |
41 | ssize_t slice_alloc; |
44 | ssize_t slice_alloc; |
42 | rand_gen rndm; |
45 | rand_gen rndm, rmg_rndm; |
43 | |
46 | |
44 | void |
47 | void |
45 | tausworthe_random_generator::seed (uint32_t seed) |
48 | tausworthe_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; |
… | |
… | |
93 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
96 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
94 | */ |
97 | */ |
95 | int |
98 | int |
96 | random_roll (int r_min, int r_max, const object *op, int goodbad) |
99 | random_roll (int r_min, int r_max, const object *op, int goodbad) |
97 | { |
100 | { |
|
|
101 | r_max = max (r_min, r_max); |
|
|
102 | |
98 | int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ |
103 | 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 | |
104 | |
106 | if (op->type == PLAYER) |
105 | if (op->type == PLAYER) |
107 | { |
106 | { |
108 | int luck = op->stats.luck; |
107 | int luck = op->stats.luck; |
109 | |
108 | |
… | |
… | |
119 | /* |
118 | /* |
120 | * This is a 64 bit version of random_roll above. This is needed |
119 | * This is a 64 bit version of random_roll above. This is needed |
121 | * for exp loss calculations for players changing religions. |
120 | * for exp loss calculations for players changing religions. |
122 | */ |
121 | */ |
123 | sint64 |
122 | sint64 |
124 | random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) |
123 | random_roll64 (sint64 r_min, sint64 r_max, const object *op, int goodbad) |
125 | { |
124 | { |
126 | sint64 omin = min; |
125 | sint64 omin = r_min; |
127 | sint64 diff = max - min + 1; |
126 | sint64 range = max (0, r_max - r_min + 1); |
128 | int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
127 | 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 | |
128 | |
136 | /* |
129 | /* |
137 | * Make a call to get two 32 bit unsigned random numbers, and just to |
130 | * Make a call to get two 32 bit unsigned random numbers, and just to |
138 | * a little bitshifting. |
131 | * a little bitshifting. |
139 | */ |
132 | */ |
140 | sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); |
133 | sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); |
141 | |
134 | |
142 | if (op->type != PLAYER) |
135 | if (op->type != PLAYER) |
143 | return ((ran % diff) + min); |
136 | return ((ran % range) + r_min); |
144 | |
137 | |
145 | int luck = op->stats.luck; |
138 | int luck = op->stats.luck; |
146 | |
139 | |
147 | if (rndm (base) < MIN (10, abs (luck))) |
140 | if (rndm (base) < min (10, abs (luck))) |
148 | { |
141 | { |
149 | /* we have a winner */ |
142 | /* we have a winner */ |
150 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
143 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
151 | diff -= luck; |
144 | range -= luck; |
152 | if (diff < 1) |
145 | if (range < 1) |
153 | return (omin); /*check again */ |
146 | return (omin); /*check again */ |
154 | |
147 | |
155 | ((goodbad) ? (min += luck) : (diff)); |
148 | ((goodbad) ? (r_min += luck) : (range)); |
156 | |
149 | |
157 | return (MAX (omin, MIN (max, (ran % diff) + min))); |
150 | return (max (omin, min (r_max, (ran % range) + r_min))); |
158 | } |
151 | } |
159 | |
152 | |
160 | return ran % diff + min; |
153 | return ran % range + r_min; |
161 | } |
154 | } |
162 | |
155 | |
163 | /* |
156 | /* |
164 | * Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
157 | * 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. |
158 | * If goodbad is non-zero, luck increases the roll, if zero, it decreases. |
… | |
… | |
207 | } |
200 | } |
208 | |
201 | |
209 | /* convert materialname to materialtype_t */ |
202 | /* convert materialname to materialtype_t */ |
210 | |
203 | |
211 | materialtype_t * |
204 | materialtype_t * |
212 | name_to_material (const shstr &name) |
205 | name_to_material (const shstr_cmp name) |
213 | { |
206 | { |
214 | for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) |
207 | for (materialtype_t *mt = materialt; mt; mt = mt->next) |
215 | if (name == mt->name) |
208 | if (name == mt->name) |
216 | return mt; |
209 | return mt; |
217 | |
210 | |
218 | return 0; |
211 | return 0; |
219 | } |
212 | } |
… | |
… | |
225 | transmute_materialname (object *op, const object *change) |
218 | transmute_materialname (object *op, const object *change) |
226 | { |
219 | { |
227 | materialtype_t *mt; |
220 | materialtype_t *mt; |
228 | int j; |
221 | int j; |
229 | |
222 | |
230 | if (op->materialname == NULL) |
223 | if (!op->materialname) |
231 | return; |
224 | return; |
232 | |
225 | |
233 | if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) |
226 | if (change->materialname && strcmp (op->materialname, change->materialname)) |
234 | return; |
227 | return; |
235 | |
228 | |
236 | if (!op->is_armor ()) |
229 | if (!op->is_armor ()) |
237 | return; |
230 | return; |
238 | |
231 | |
… | |
… | |
258 | void |
251 | void |
259 | set_materialname (object *op, int difficulty, materialtype_t *nmt) |
252 | set_materialname (object *op, int difficulty, materialtype_t *nmt) |
260 | { |
253 | { |
261 | materialtype_t *mt, *lmt; |
254 | materialtype_t *mt, *lmt; |
262 | |
255 | |
263 | if (op->materialname != NULL) |
256 | if (!op->materialname) |
264 | return; |
257 | return; |
265 | |
258 | |
266 | if (nmt == NULL) |
259 | if (nmt) |
|
|
260 | lmt = nmt; |
|
|
261 | else |
267 | { |
262 | { |
268 | lmt = NULL; |
263 | lmt = 0; |
269 | |
264 | |
270 | for (mt = materialt; mt && mt->next; mt = mt->next) |
265 | for (mt = materialt; mt; mt = mt->next) |
271 | if (op->materials & mt->material && rndm (1, 100) <= mt->chance && |
266 | if (op->materials & mt->material && rndm (1, 100) <= mt->chance && |
272 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
267 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
273 | { |
268 | { |
274 | lmt = mt; |
269 | lmt = mt; |
275 | if (!(op->is_weapon () || op->is_armor ())) |
270 | if (!(op->is_weapon () || op->is_armor ())) |
276 | break; |
271 | break; |
277 | } |
272 | } |
278 | } |
273 | } |
279 | else |
|
|
280 | lmt = nmt; |
|
|
281 | |
274 | |
282 | if (lmt != NULL) |
275 | if (lmt) |
283 | { |
276 | { |
284 | if (op->stats.dam && op->is_weapon ()) |
277 | if (op->stats.dam && op->is_weapon ()) |
285 | { |
278 | { |
286 | op->stats.dam += lmt->damage; |
279 | op->stats.dam += lmt->damage; |
287 | if (op->stats.dam < 1) |
280 | if (op->stats.dam < 1) |
… | |
… | |
456 | void |
449 | void |
457 | fork_abort (const char *msg) |
450 | fork_abort (const char *msg) |
458 | { |
451 | { |
459 | if (!fork ()) |
452 | if (!fork ()) |
460 | { |
453 | { |
|
|
454 | signal (SIGINT , SIG_IGN); |
|
|
455 | signal (SIGTERM, SIG_IGN); |
461 | signal (SIGABRT, SIG_DFL); |
456 | signal (SIGABRT, SIG_IGN); |
|
|
457 | |
|
|
458 | signal (SIGSEGV, SIG_DFL); |
|
|
459 | signal (SIGBUS , SIG_DFL); |
|
|
460 | signal (SIGILL , SIG_DFL); |
|
|
461 | signal (SIGTRAP, SIG_DFL); |
|
|
462 | |
462 | // try to put corefiles into a subdirectory, if existing, to allow |
463 | // try to put corefiles into a subdirectory, if existing, to allow |
463 | // an administrator to reduce the I/O load. |
464 | // an administrator to reduce the I/O load. |
464 | chdir ("cores"); |
465 | chdir ("cores"); |
|
|
466 | |
|
|
467 | // try to detach us from as many external dependencies as possible |
|
|
468 | // as coredumping can take time by closing all fd's. |
|
|
469 | { |
|
|
470 | struct rlimit lim; |
|
|
471 | |
|
|
472 | if (getrlimit (RLIMIT_NOFILE, &lim)) |
|
|
473 | lim.rlim_cur = 1024; |
|
|
474 | |
|
|
475 | for (int i = 0; i < lim.rlim_cur; ++i) |
|
|
476 | close (i); |
|
|
477 | } |
|
|
478 | |
|
|
479 | { |
|
|
480 | sigset_t empty; |
|
|
481 | sigemptyset (&empty); |
|
|
482 | sigprocmask (SIG_SETMASK, &empty, 0); |
|
|
483 | } |
|
|
484 | |
|
|
485 | // try to coredump with SIGTRAP |
|
|
486 | kill (getpid (), SIGTRAP); |
465 | abort (); |
487 | abort (); |
466 | } |
488 | } |
467 | |
489 | |
468 | LOG (llevError, "fork abort: %s\n", msg); |
490 | LOG (llevError, "fork abort: %s\n", msg); |
469 | } |
491 | } |