… | |
… | |
37 | #include <funcpoint.h> |
37 | #include <funcpoint.h> |
38 | #include <material.h> |
38 | #include <material.h> |
39 | |
39 | |
40 | #include <glib.h> |
40 | #include <glib.h> |
41 | |
41 | |
42 | rand_gen rndm (time (0)); |
42 | rand_gen rndm; |
43 | |
43 | |
|
|
44 | void |
44 | tausworthe_random_generator::tausworthe_random_generator (uint32_t seed) |
45 | tausworthe_random_generator::seed (uint32_t seed) |
45 | { |
46 | { |
46 | state [0] = max ( 2, seed * 69069U); |
47 | state [0] = max ( 2U, seed * 69069U); |
47 | state [1] = max ( 8, state [0] * 69069U); |
48 | state [1] = max ( 8U, state [0] * 69069U); |
48 | state [2] = max ( 16, state [1] * 69069U); |
49 | state [2] = max ( 16U, state [1] * 69069U); |
49 | state [3] = max (128, state [2] * 69069U); |
50 | state [3] = max (128U, state [2] * 69069U); |
50 | |
51 | |
51 | for (int i = 11; --i; ) |
52 | for (int i = 11; --i; ) |
52 | operator ()(); |
53 | operator ()(); |
53 | } |
54 | } |
54 | |
55 | |
… | |
… | |
59 | state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U); |
60 | state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U); |
60 | state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U); |
61 | state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U); |
61 | state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); |
62 | state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); |
62 | |
63 | |
63 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
64 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
|
|
65 | } |
|
|
66 | |
|
|
67 | uint32_t |
|
|
68 | tausworthe_random_generator::get_range (uint32_t r_max) |
|
|
69 | { |
|
|
70 | return next () % r_max; |
|
|
71 | } |
|
|
72 | |
|
|
73 | // return a number within (min .. max) |
|
|
74 | int |
|
|
75 | tausworthe_random_generator::get_range (int r_min, int r_max) |
|
|
76 | { |
|
|
77 | return r_min + (*this) (max (r_max - r_min + 1, 1)); |
64 | } |
78 | } |
65 | |
79 | |
66 | /* |
80 | /* |
67 | * The random functions here take luck into account when rolling random |
81 | * The random functions here take luck into account when rolling random |
68 | * dice or numbers. This function has less of an impact the larger the |
82 | * dice or numbers. This function has less of an impact the larger the |
… | |
… | |
104 | |
118 | |
105 | /* |
119 | /* |
106 | * This is a 64 bit version of random_roll above. This is needed |
120 | * This is a 64 bit version of random_roll above. This is needed |
107 | * for exp loss calculations for players changing religions. |
121 | * for exp loss calculations for players changing religions. |
108 | */ |
122 | */ |
109 | |
|
|
110 | sint64 |
123 | sint64 |
111 | random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) |
124 | random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) |
112 | { |
125 | { |
113 | sint64 omin, diff, luck, ran; |
126 | sint64 omin, diff, luck, ran; |
114 | int base; |
127 | int base; |
… | |
… | |
121 | { |
134 | { |
122 | LOG (llevError, "Calling random_roll with min=%" PRId64 " max=%" PRId64 "\n", min, max); |
135 | LOG (llevError, "Calling random_roll with min=%" PRId64 " max=%" PRId64 "\n", min, max); |
123 | return (min); /* avoids a float exception */ |
136 | return (min); /* avoids a float exception */ |
124 | } |
137 | } |
125 | |
138 | |
126 | /* Don't know of a portable call to get 64 bit random values. |
139 | /* |
127 | * So make a call to get two 32 bit random numbers, and just to |
140 | * Make a call to get two 32 bit unsigned random numbers, and just to |
128 | * a little byteshifting. Do make sure the first one is only |
141 | * a little bitshifting. |
129 | * 32 bit, so we don't get skewed results |
|
|
130 | */ |
142 | */ |
131 | ran = (RANDOM () & 0xffffffff) | ((sint64) RANDOM () << 32); |
143 | ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); |
132 | |
144 | |
133 | if (op->type != PLAYER) |
145 | if (op->type != PLAYER) |
134 | return ((ran % diff) + min); |
146 | return ((ran % diff) + min); |
135 | |
147 | |
136 | luck = op->stats.luck; |
148 | luck = op->stats.luck; |
137 | if (RANDOM () % base < MIN (10, abs (luck))) |
149 | if (rndm (base) < MIN (10, abs (luck))) |
138 | { |
150 | { |
139 | /* we have a winner */ |
151 | /* we have a winner */ |
140 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
152 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
141 | diff -= luck; |
153 | diff -= luck; |
142 | if (diff < 1) |
154 | if (diff < 1) |
143 | return (omin); /*check again */ |
155 | return (omin); /*check again */ |
144 | ((goodbad) ? (min += luck) : (diff)); |
156 | ((goodbad) ? (min += luck) : (diff)); |
145 | |
157 | |
146 | return (MAX (omin, MIN (max, (ran % diff) + min))); |
158 | return (MAX (omin, MIN (max, (ran % diff) + min))); |
147 | } |
159 | } |
|
|
160 | |
148 | return ((ran % diff) + min); |
161 | return ((ran % diff) + min); |
149 | } |
162 | } |
150 | |
163 | |
151 | /* |
164 | /* |
152 | * Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
165 | * Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
… | |
… | |
157 | */ |
170 | */ |
158 | |
171 | |
159 | int |
172 | int |
160 | die_roll (int num, int size, const object *op, int goodbad) |
173 | die_roll (int num, int size, const object *op, int goodbad) |
161 | { |
174 | { |
162 | int min, diff, luck, total, i, gotlucky, base, ran; |
175 | int min, diff, luck, total, i, gotlucky, base; |
163 | |
176 | |
164 | diff = size; |
177 | diff = size; |
165 | min = 1; |
178 | min = 1; |
166 | luck = total = gotlucky = 0; |
179 | luck = total = gotlucky = 0; |
167 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
180 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
… | |
… | |
174 | if (op->type == PLAYER) |
187 | if (op->type == PLAYER) |
175 | luck = op->stats.luck; |
188 | luck = op->stats.luck; |
176 | |
189 | |
177 | for (i = 0; i < num; i++) |
190 | for (i = 0; i < num; i++) |
178 | { |
191 | { |
179 | if (RANDOM () % base < MIN (10, abs (luck)) && !gotlucky) |
192 | if (rndm (base) < MIN (10, abs (luck)) && !gotlucky) |
180 | { |
193 | { |
181 | /* we have a winner */ |
194 | /* we have a winner */ |
182 | gotlucky++; |
195 | gotlucky++; |
183 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
196 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
184 | diff -= luck; |
197 | diff -= luck; |
185 | if (diff < 1) |
198 | if (diff < 1) |
186 | return (num); /*check again */ |
199 | return (num); /*check again */ |
187 | ((goodbad) ? (min += luck) : (diff)); |
200 | ((goodbad) ? (min += luck) : (diff)); |
188 | ran = RANDOM (); |
|
|
189 | total += MAX (1, MIN (size, (ran % diff) + min)); |
201 | total += MAX (1, MIN (size, rndm (diff) + min)); |
190 | } |
202 | } |
191 | else |
203 | else |
192 | { |
|
|
193 | total += RANDOM () % size + 1; |
204 | total += rndm (size) + 1; |
194 | } |
|
|
195 | } |
205 | } |
|
|
206 | |
196 | return (total); |
207 | return total; |
197 | } |
208 | } |
198 | |
209 | |
199 | /* decay and destroy perishable items in a map */ |
210 | /* decay and destroy perishable items in a map */ |
200 | void |
211 | void |
201 | maptile::decay_objects () |
212 | maptile::decay_objects () |
… | |
… | |
240 | if (op->stats.food < 0) |
251 | if (op->stats.food < 0) |
241 | destroy = 1; |
252 | destroy = 1; |
242 | } |
253 | } |
243 | else |
254 | else |
244 | { |
255 | { |
245 | int mat = op->material; |
256 | int mat = op->materials; |
246 | |
257 | |
247 | if (mat & M_PAPER |
258 | if (mat & M_PAPER |
248 | || mat & M_LEATHER |
259 | || mat & M_LEATHER |
249 | || mat & M_WOOD |
260 | || mat & M_WOOD |
250 | || mat & M_ORGANIC |
261 | || mat & M_ORGANIC |
… | |
… | |
265 | } |
276 | } |
266 | |
277 | |
267 | /* convert materialname to materialtype_t */ |
278 | /* convert materialname to materialtype_t */ |
268 | |
279 | |
269 | materialtype_t * |
280 | materialtype_t * |
270 | name_to_material (const char *name) |
281 | name_to_material (const shstr &name) |
271 | { |
282 | { |
272 | materialtype_t *mt, *nmt; |
283 | for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) |
273 | |
284 | if (name == mt->name) |
274 | mt = NULL; |
|
|
275 | for (nmt = materialt; nmt != NULL && nmt->next != NULL; nmt = nmt->next) |
|
|
276 | { |
|
|
277 | if (strcmp (name, nmt->name) == 0) |
|
|
278 | { |
|
|
279 | mt = nmt; |
|
|
280 | break; |
|
|
281 | } |
|
|
282 | } |
|
|
283 | return mt; |
285 | return mt; |
|
|
286 | |
|
|
287 | return materialt; |
284 | } |
288 | } |
285 | |
289 | |
286 | /* when doing transmutation of objects, we have to recheck the resistances, |
290 | /* when doing transmutation of objects, we have to recheck the resistances, |
287 | * as some that did not apply previously, may apply now. |
291 | * as some that did not apply previously, may apply now. |
288 | */ |
292 | */ |
… | |
… | |
337 | |
341 | |
338 | if (nmt == NULL) |
342 | if (nmt == NULL) |
339 | { |
343 | { |
340 | lmt = NULL; |
344 | lmt = NULL; |
341 | #ifndef NEW_MATERIAL_CODE |
345 | #ifndef NEW_MATERIAL_CODE |
342 | for (mt = materialt; mt != NULL && mt->next != NULL; mt = mt->next) |
346 | for (mt = materialt; mt && mt->next; mt = mt->next) |
343 | { |
347 | { |
344 | if (op->material & mt->material) |
348 | if (op->materials & mt->material) |
345 | { |
349 | { |
346 | lmt = mt; |
350 | lmt = mt; |
347 | break; |
351 | break; |
348 | } |
352 | } |
349 | } |
353 | } |
350 | |
354 | |
351 | #else |
355 | #else |
352 | for (mt = materialt; mt != NULL && mt->next != NULL; mt = mt->next) |
356 | for (mt = materialt; mt && mt->next; mt = mt->next) |
353 | { |
357 | { |
354 | if (op->material & mt->material && rndm (1, 100) <= mt->chance && |
358 | if (op->materials & mt->material && rndm (1, 100) <= mt->chance && |
355 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
359 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
356 | { |
360 | { |
357 | lmt = mt; |
361 | lmt = mt; |
358 | if (!(op->is_weapon () || op->is_armor ())) |
362 | if (!(op->is_weapon () || op->is_armor ())) |
359 | break; |
363 | break; |
… | |
… | |
377 | { |
381 | { |
378 | op->stats.dam += lmt->damage; |
382 | op->stats.dam += lmt->damage; |
379 | if (op->stats.dam < 1) |
383 | if (op->stats.dam < 1) |
380 | op->stats.dam = 1; |
384 | op->stats.dam = 1; |
381 | } |
385 | } |
|
|
386 | |
382 | if (op->stats.sp && op->type == BOW) |
387 | if (op->stats.sp && op->type == BOW) |
383 | op->stats.sp += lmt->sp; |
388 | op->stats.sp += lmt->sp; |
384 | if (op->stats.wc && op->is_weapon ()) |
389 | if (op->stats.wc && op->is_weapon ()) |
385 | op->stats.wc += lmt->wc; |
390 | op->stats.wc += lmt->wc; |
386 | if (op->is_armor ()) |
391 | if (op->is_armor ()) |
… | |
… | |
395 | op->resist[j] = 100; |
400 | op->resist[j] = 100; |
396 | if (op->resist[j] < -100) |
401 | if (op->resist[j] < -100) |
397 | op->resist[j] = -100; |
402 | op->resist[j] = -100; |
398 | } |
403 | } |
399 | } |
404 | } |
400 | op->materialname = add_string (lmt->name); |
405 | op->materialname = lmt->name; |
401 | /* dont make it unstackable if it doesn't need to be */ |
406 | /* dont make it unstackable if it doesn't need to be */ |
402 | if (op->is_weapon () || op->is_armor ()) |
407 | if (op->is_weapon () || op->is_armor ()) |
403 | { |
408 | { |
404 | op->weight = (op->weight * lmt->weight) / 100; |
409 | op->weight = (op->weight * lmt->weight) / 100; |
405 | op->value = (op->value * lmt->value) / 100; |
410 | op->value = (op->value * lmt->value) / 100; |
… | |
… | |
556 | LOG (llevError, "fork abort: %s\n", msg); |
561 | LOG (llevError, "fork abort: %s\n", msg); |
557 | } |
562 | } |
558 | |
563 | |
559 | void *salloc_ (int n) throw (std::bad_alloc) |
564 | void *salloc_ (int n) throw (std::bad_alloc) |
560 | { |
565 | { |
|
|
566 | #ifdef PREFER_MALLOC |
|
|
567 | void *ptr = malloc (n); |
|
|
568 | #else |
561 | void *ptr = g_slice_alloc (n); |
569 | void *ptr = g_slice_alloc (n); |
|
|
570 | #endif |
562 | |
571 | |
563 | if (!ptr) |
572 | if (!ptr) |
564 | throw std::bad_alloc (); |
573 | throw std::bad_alloc (); |
565 | |
574 | |
566 | return ptr; |
575 | return ptr; |