… | |
… | |
16 | |
16 | |
17 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | |
20 | |
21 | The authors can be reached via e-mail at crossfire-devel@real-time.com |
21 | The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | */ |
22 | */ |
23 | |
23 | |
24 | /* |
24 | /* |
25 | * General convenience functions for crossfire. |
25 | * General convenience functions for crossfire. |
26 | */ |
26 | */ |
27 | |
27 | |
28 | #include <global.h> |
28 | #include <global.h> |
29 | #include <funcpoint.h> |
29 | #include <funcpoint.h> |
30 | #include <material.h> |
30 | #include <material.h> |
31 | |
31 | |
|
|
32 | #include <sys/time.h> |
|
|
33 | #include <time.h> |
32 | #include <glib.h> |
34 | #include <glib.h> |
33 | |
35 | |
34 | /* |
36 | /* |
35 | * The random functions here take luck into account when rolling random |
37 | * The random functions here take luck into account when rolling random |
36 | * dice or numbers. This function has less of an impact the larger the |
38 | * dice or numbers. This function has less of an impact the larger the |
… | |
… | |
97 | diff = max - min + 1; |
99 | diff = max - min + 1; |
98 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
100 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
99 | |
101 | |
100 | if (max < 1 || diff < 1) |
102 | if (max < 1 || diff < 1) |
101 | { |
103 | { |
102 | #ifndef WIN32 |
|
|
103 | LOG (llevError, "Calling random_roll with min=%lld max=%lld\n", min, max); |
|
|
104 | #else |
|
|
105 | LOG (llevError, "Calling random_roll with min=%I64d max=%I64d\n", min, max); |
104 | LOG (llevError, "Calling random_roll with min=%" PRId64 " max=%" PRId64 "\n", min, max); |
106 | #endif |
|
|
107 | return (min); /* avoids a float exception */ |
105 | return (min); /* avoids a float exception */ |
108 | } |
106 | } |
109 | |
107 | |
110 | /* Don't know of a portable call to get 64 bit random values. |
108 | /* Don't know of a portable call to get 64 bit random values. |
111 | * So make a call to get two 32 bit random numbers, and just to |
109 | * So make a call to get two 32 bit random numbers, and just to |
… | |
… | |
200 | } |
198 | } |
201 | |
199 | |
202 | /* decay and destroy persihable items in a map */ |
200 | /* decay and destroy persihable items in a map */ |
203 | |
201 | |
204 | void |
202 | void |
205 | decay_objects (mapstruct *m) |
203 | decay_objects (maptile *m) |
206 | { |
204 | { |
207 | int x, y, destroy; |
205 | int x, y, destroy; |
208 | object *op, *otmp; |
206 | object *op, *otmp; |
209 | |
207 | |
210 | if (m->unique) |
208 | if (m->unique) |
… | |
… | |
258 | if (op->material & M_ICE && MAP_TEMP (m) > 32) |
256 | if (op->material & M_ICE && MAP_TEMP (m) > 32) |
259 | destroy = 1; |
257 | destroy = 1; |
260 | } |
258 | } |
261 | /* adjust overall chance below */ |
259 | /* adjust overall chance below */ |
262 | if (destroy && rndm (0, 1)) |
260 | if (destroy && rndm (0, 1)) |
263 | { |
261 | op->destroy (); |
264 | remove_ob (op); |
|
|
265 | free_object (op); |
|
|
266 | } |
|
|
267 | } |
262 | } |
268 | } |
263 | } |
269 | |
264 | |
270 | /* convert materialname to materialtype_t */ |
265 | /* convert materialname to materialtype_t */ |
271 | |
266 | |
… | |
… | |
543 | strcpy (input, tmp); |
538 | strcpy (input, tmp); |
544 | |
539 | |
545 | return; |
540 | return; |
546 | } |
541 | } |
547 | |
542 | |
|
|
543 | ///////////////////////////////////////////////////////////////////////////// |
|
|
544 | |
|
|
545 | #if 0 |
|
|
546 | refcounted *refcounted::rc_first; |
|
|
547 | |
|
|
548 | refcounted::refcounted () |
|
|
549 | { |
|
|
550 | refcnt = 0; |
|
|
551 | rc_next = rc_first; |
|
|
552 | rc_first = this; |
|
|
553 | } |
|
|
554 | |
|
|
555 | refcounted::~refcounted () |
|
|
556 | { |
|
|
557 | assert (!rc_next); |
|
|
558 | assert (!refcnt); |
|
|
559 | } |
|
|
560 | #endif |
|
|
561 | |
548 | void *alloc (int s) throw (std::bad_alloc) |
562 | void *salloc_ (int n) throw (std::bad_alloc) |
549 | { |
563 | { |
550 | void *p = g_slice_alloc (s); |
564 | void *ptr = g_slice_alloc (n); |
551 | |
565 | |
552 | if (!p) |
566 | if (!ptr) |
553 | throw std::bad_alloc (); |
567 | throw std::bad_alloc (); |
554 | |
568 | |
555 | return p; |
569 | return ptr; |
|
|
570 | } |
|
|
571 | |
|
|
572 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
|
|
573 | { |
|
|
574 | void *ptr = salloc_ (n); |
|
|
575 | |
|
|
576 | if (src) |
|
|
577 | memcpy (ptr, src, n); |
|
|
578 | else |
|
|
579 | memset (ptr, 0, n); |
|
|
580 | |
|
|
581 | return ptr; |
556 | } |
582 | } |
557 | |
583 | |
558 | void assign (char *dst, const char *src, int maxlen) |
584 | void assign (char *dst, const char *src, int maxlen) |
559 | { |
585 | { |
560 | if (!src) |
586 | if (!src) |
… | |
… | |
577 | } |
603 | } |
578 | else |
604 | else |
579 | memcpy (dst, src, len + 1); |
605 | memcpy (dst, src, len + 1); |
580 | } |
606 | } |
581 | |
607 | |
|
|
608 | tstamp now () |
|
|
609 | { |
|
|
610 | struct timeval tv; |
|
|
611 | |
|
|
612 | gettimeofday (&tv, 0); |
|
|
613 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
|
|
614 | } |
|
|
615 | |