… | |
… | |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * it under the terms of the GNU General Public License as published by |
9 | * the terms of the Affero GNU General Public License as published by the |
10 | * the Free Software Foundation, either version 3 of the License, or |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * (at your option) any later version. |
11 | * option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the Affero GNU General Public License |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | * and the GNU General Public License along with this program. If not, see |
|
|
20 | * <http://www.gnu.org/licenses/>. |
20 | * |
21 | * |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
23 | */ |
23 | |
24 | |
24 | /* |
25 | /* |
… | |
… | |
32 | #include <time.h> |
33 | #include <time.h> |
33 | #include <signal.h> |
34 | #include <signal.h> |
34 | |
35 | |
35 | #include <global.h> |
36 | #include <global.h> |
36 | #include <material.h> |
37 | #include <material.h> |
|
|
38 | #include <object.h> |
37 | |
39 | |
38 | #include <sys/time.h> |
40 | #include <sys/time.h> |
39 | #include <sys/resource.h> |
41 | #include <sys/resource.h> |
40 | |
42 | |
41 | #include <glib.h> |
43 | #include <glib.h> |
… | |
… | |
51 | state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; |
53 | state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; |
52 | state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; |
54 | state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; |
53 | state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U; |
55 | state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U; |
54 | |
56 | |
55 | for (int i = 11; --i; ) |
57 | for (int i = 11; --i; ) |
56 | operator ()(); |
58 | next (); |
57 | } |
59 | } |
58 | |
60 | |
59 | uint32_t |
61 | uint32_t |
60 | tausworthe_random_generator::next () |
62 | tausworthe_random_generator::next () |
61 | { |
63 | { |
… | |
… | |
65 | state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); |
67 | state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); |
66 | |
68 | |
67 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
69 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
68 | } |
70 | } |
69 | |
71 | |
|
|
72 | template<class generator> |
70 | uint32_t |
73 | uint32_t |
71 | tausworthe_random_generator::get_range (uint32_t num) |
74 | random_number_generator<generator>::get_range (uint32_t num) |
72 | { |
75 | { |
73 | return (next () * (uint64_t)num) >> 32U; |
76 | return (this->next () * (uint64_t)num) >> 32U; |
74 | } |
77 | } |
75 | |
78 | |
76 | // return a number within (min .. max) |
79 | // return a number within (min .. max) |
|
|
80 | template<class generator> |
77 | int |
81 | int |
78 | tausworthe_random_generator::get_range (int r_min, int r_max) |
82 | random_number_generator<generator>::get_range (int r_min, int r_max) |
79 | { |
83 | { |
80 | return r_min + get_range (max (r_max - r_min + 1, 0)); |
84 | return r_min + get_range (max (r_max - r_min + 1, 0)); |
81 | } |
85 | } |
|
|
86 | |
|
|
87 | template struct random_number_generator<tausworthe_random_generator>; |
|
|
88 | template struct random_number_generator<xorshift_random_generator>; |
82 | |
89 | |
83 | /* |
90 | /* |
84 | * The random functions here take luck into account when rolling random |
91 | * The random functions here take luck into account when rolling random |
85 | * dice or numbers. This function has less of an impact the larger the |
92 | * dice or numbers. This function has less of an impact the larger the |
86 | * difference becomes in the random numbers. IE, the effect is lessened |
93 | * difference becomes in the random numbers. IE, the effect is lessened |
… | |
… | |
200 | } |
207 | } |
201 | |
208 | |
202 | /* convert materialname to materialtype_t */ |
209 | /* convert materialname to materialtype_t */ |
203 | |
210 | |
204 | materialtype_t * |
211 | materialtype_t * |
205 | name_to_material (const shstr &name) |
212 | name_to_material (const shstr_cmp name) |
206 | { |
213 | { |
207 | for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) |
214 | for (materialtype_t *mt = materialt; mt; mt = mt->next) |
208 | if (name == mt->name) |
215 | if (name == mt->name) |
209 | return mt; |
216 | return mt; |
210 | |
217 | |
211 | return 0; |
218 | return 0; |
212 | } |
219 | } |
… | |
… | |
218 | transmute_materialname (object *op, const object *change) |
225 | transmute_materialname (object *op, const object *change) |
219 | { |
226 | { |
220 | materialtype_t *mt; |
227 | materialtype_t *mt; |
221 | int j; |
228 | int j; |
222 | |
229 | |
223 | if (op->materialname == NULL) |
230 | if (!op->materialname) |
224 | return; |
231 | return; |
225 | |
232 | |
226 | if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) |
233 | if (op->materialname != change->materialname) |
227 | return; |
234 | return; |
228 | |
235 | |
229 | if (!op->is_armor ()) |
236 | if (!op->is_armor ()) |
230 | return; |
237 | return; |
231 | |
238 | |
… | |
… | |
251 | void |
258 | void |
252 | set_materialname (object *op, int difficulty, materialtype_t *nmt) |
259 | set_materialname (object *op, int difficulty, materialtype_t *nmt) |
253 | { |
260 | { |
254 | materialtype_t *mt, *lmt; |
261 | materialtype_t *mt, *lmt; |
255 | |
262 | |
256 | if (op->materialname != NULL) |
263 | if (!op->materialname) |
257 | return; |
264 | return; |
258 | |
265 | |
259 | if (nmt == NULL) |
266 | if (nmt) |
|
|
267 | lmt = nmt; |
|
|
268 | else |
260 | { |
269 | { |
261 | lmt = NULL; |
270 | lmt = 0; |
262 | |
271 | |
263 | for (mt = materialt; mt && mt->next; mt = mt->next) |
272 | for (mt = materialt; mt; mt = mt->next) |
264 | if (op->materials & mt->material && rndm (1, 100) <= mt->chance && |
273 | if (op->materials & mt->material && rndm (1, 100) <= mt->chance && |
265 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
274 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
266 | { |
275 | { |
267 | lmt = mt; |
276 | lmt = mt; |
268 | if (!(op->is_weapon () || op->is_armor ())) |
277 | if (!(op->is_weapon () || op->is_armor ())) |
269 | break; |
278 | break; |
270 | } |
279 | } |
271 | } |
280 | } |
272 | else |
|
|
273 | lmt = nmt; |
|
|
274 | |
281 | |
275 | if (lmt != NULL) |
282 | if (lmt) |
276 | { |
283 | { |
277 | if (op->stats.dam && op->is_weapon ()) |
284 | if (op->stats.dam && op->is_weapon ()) |
278 | { |
285 | { |
279 | op->stats.dam += lmt->damage; |
286 | op->stats.dam += lmt->damage; |
280 | if (op->stats.dam < 1) |
287 | if (op->stats.dam < 1) |
… | |
… | |
442 | strcpy (input, tmp); |
449 | strcpy (input, tmp); |
443 | |
450 | |
444 | return; |
451 | return; |
445 | } |
452 | } |
446 | |
453 | |
|
|
454 | /******************************************************************************/ |
|
|
455 | |
|
|
456 | /* Checks a player-provided string which will become the msg property of |
|
|
457 | * an object for dangerous input. |
|
|
458 | */ |
|
|
459 | bool |
|
|
460 | msg_is_safe (const char *msg) |
|
|
461 | { |
|
|
462 | bool safe = true; |
|
|
463 | |
|
|
464 | /* Trying to cheat by getting data into the object */ |
|
|
465 | if (!strncmp (msg, "endmsg", sizeof ("endmsg") - 1) |
|
|
466 | || strstr (msg, "\nendmsg")) |
|
|
467 | safe = false; |
|
|
468 | |
|
|
469 | /* Trying to make the object talk, and potentially access arbitrary code */ |
|
|
470 | if (object::msg_has_dialogue (msg)) |
|
|
471 | safe = false; |
|
|
472 | |
|
|
473 | return safe; |
|
|
474 | } |
|
|
475 | |
447 | ///////////////////////////////////////////////////////////////////////////// |
476 | ///////////////////////////////////////////////////////////////////////////// |
448 | |
477 | |
449 | void |
478 | void |
450 | fork_abort (const char *msg) |
479 | fork_abort (const char *msg) |
451 | { |
480 | { |
452 | if (!fork ()) |
481 | if (!fork ()) |
453 | { |
482 | { |
454 | signal (SIGINT , SIG_IGN); |
483 | signal (SIGINT , SIG_IGN); |
455 | signal (SIGTERM, SIG_IGN); |
484 | signal (SIGTERM, SIG_IGN); |
456 | signal (SIGABRT, SIG_IGN); |
485 | signal (SIGABRT, SIG_IGN); |
|
|
486 | |
|
|
487 | signal (SIGSEGV, SIG_DFL); |
|
|
488 | signal (SIGBUS , SIG_DFL); |
|
|
489 | signal (SIGILL , SIG_DFL); |
|
|
490 | signal (SIGTRAP, SIG_DFL); |
457 | |
491 | |
458 | // try to put corefiles into a subdirectory, if existing, to allow |
492 | // try to put corefiles into a subdirectory, if existing, to allow |
459 | // an administrator to reduce the I/O load. |
493 | // an administrator to reduce the I/O load. |
460 | chdir ("cores"); |
494 | chdir ("cores"); |
461 | |
495 | |
… | |
… | |
469 | |
503 | |
470 | for (int i = 0; i < lim.rlim_cur; ++i) |
504 | for (int i = 0; i < lim.rlim_cur; ++i) |
471 | close (i); |
505 | close (i); |
472 | } |
506 | } |
473 | |
507 | |
|
|
508 | { |
|
|
509 | sigset_t empty; |
|
|
510 | sigemptyset (&empty); |
|
|
511 | sigprocmask (SIG_SETMASK, &empty, 0); |
|
|
512 | } |
|
|
513 | |
|
|
514 | // try to coredump with SIGTRAP |
|
|
515 | kill (getpid (), SIGTRAP); |
474 | abort (); |
516 | abort (); |
475 | } |
517 | } |
476 | |
518 | |
477 | LOG (llevError, "fork abort: %s\n", msg); |
519 | LOG (llevError, "fork abort: %s\n", msg); |
478 | } |
520 | } |
… | |
… | |
541 | |
583 | |
542 | #endif |
584 | #endif |
543 | |
585 | |
544 | /******************************************************************************/ |
586 | /******************************************************************************/ |
545 | |
587 | |
|
|
588 | int |
546 | void assign (char *dst, const char *src, int maxlen) |
589 | assign (char *dst, const char *src, int maxsize) |
547 | { |
590 | { |
548 | if (!src) |
591 | if (!src) |
549 | src = ""; |
592 | src = ""; |
550 | |
593 | |
551 | int len = strlen (src); |
594 | int len = strlen (src); |
552 | |
595 | |
553 | if (len >= maxlen - 1) |
596 | if (len >= maxsize) |
554 | { |
597 | { |
555 | if (maxlen <= 4) |
598 | if (maxsize <= 4) |
556 | { |
599 | { |
557 | memset (dst, '.', maxlen - 1); |
600 | memset (dst, '.', maxsize - 2); |
558 | dst [maxlen - 1] = 0; |
601 | dst [maxsize - 1] = 0; |
559 | } |
602 | } |
560 | else |
603 | else |
561 | { |
604 | { |
562 | memcpy (dst, src, maxlen - 4); |
605 | memcpy (dst, src, maxsize - 4); |
563 | memcpy (dst + maxlen - 4, "...", 4); |
606 | memcpy (dst + maxsize - 4, "...", 4); |
564 | } |
607 | } |
|
|
608 | |
|
|
609 | len = maxsize; |
565 | } |
610 | } |
566 | else |
611 | else |
567 | memcpy (dst, src, len + 1); |
612 | memcpy (dst, src, ++len); |
|
|
613 | |
|
|
614 | return len; |
568 | } |
615 | } |
569 | |
616 | |
570 | const char * |
617 | const char * |
571 | format (const char *format, ...) |
618 | format (const char *format, ...) |
572 | { |
619 | { |