… | |
… | |
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. |
… | |
… | |
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 | |
462 | // try to put corefiles into a subdirectory, if existing, to allow |
458 | // try to put corefiles into a subdirectory, if existing, to allow |
463 | // an administrator to reduce the I/O load. |
459 | // an administrator to reduce the I/O load. |
464 | chdir ("cores"); |
460 | chdir ("cores"); |
|
|
461 | |
|
|
462 | // try to detach us from as many external dependencies as possible |
|
|
463 | // as coredumping can take time by closing all fd's. |
|
|
464 | { |
|
|
465 | struct rlimit lim; |
|
|
466 | |
|
|
467 | if (getrlimit (RLIMIT_NOFILE, &lim)) |
|
|
468 | lim.rlim_cur = 1024; |
|
|
469 | |
|
|
470 | for (int i = 0; i < lim.rlim_cur; ++i) |
|
|
471 | close (i); |
|
|
472 | } |
|
|
473 | |
465 | abort (); |
474 | abort (); |
466 | } |
475 | } |
467 | |
476 | |
468 | LOG (llevError, "fork abort: %s\n", msg); |
477 | LOG (llevError, "fork abort: %s\n", msg); |
469 | } |
478 | } |