1 | |
|
|
2 | /* |
|
|
3 | * static char *rcsid_utils_c = |
|
|
4 | * "$Id: utils.C,v 1.11 2006/09/12 19:20:06 root Exp $"; |
|
|
5 | */ |
|
|
6 | |
|
|
7 | /* |
1 | /* |
8 | CrossFire, A Multiplayer game for X-windows |
2 | CrossFire, A Multiplayer game for X-windows |
9 | |
3 | |
10 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
4 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
11 | Copyright (C) 1992 Frank Tore Johansen |
5 | Copyright (C) 1992 Frank Tore Johansen |
… | |
… | |
22 | |
16 | |
23 | 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 |
24 | along with this program; if not, write to the Free Software |
18 | along with this program; if not, write to the Free Software |
25 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
26 | |
20 | |
27 | 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> |
28 | */ |
22 | */ |
29 | |
23 | |
30 | /* |
24 | /* |
31 | * General convenience functions for crossfire. |
25 | * General convenience functions for crossfire. |
32 | */ |
26 | */ |
33 | |
27 | |
34 | #include <global.h> |
28 | #include <global.h> |
35 | #include <funcpoint.h> |
29 | #include <funcpoint.h> |
36 | #include <material.h> |
30 | #include <material.h> |
37 | |
31 | |
|
|
32 | #include <sys/time.h> |
|
|
33 | #include <time.h> |
38 | #include <glib.h> |
34 | #include <glib.h> |
39 | |
35 | |
40 | /* |
36 | /* |
41 | * The random functions here take luck into account when rolling random |
37 | * The random functions here take luck into account when rolling random |
42 | * 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 |
… | |
… | |
103 | diff = max - min + 1; |
99 | diff = max - min + 1; |
104 | ((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 */ |
105 | |
101 | |
106 | if (max < 1 || diff < 1) |
102 | if (max < 1 || diff < 1) |
107 | { |
103 | { |
108 | #ifndef WIN32 |
|
|
109 | LOG (llevError, "Calling random_roll with min=%lld max=%lld\n", min, max); |
|
|
110 | #else |
|
|
111 | 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); |
112 | #endif |
|
|
113 | return (min); /* avoids a float exception */ |
105 | return (min); /* avoids a float exception */ |
114 | } |
106 | } |
115 | |
107 | |
116 | /* 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. |
117 | * 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 |
… | |
… | |
206 | } |
198 | } |
207 | |
199 | |
208 | /* decay and destroy persihable items in a map */ |
200 | /* decay and destroy persihable items in a map */ |
209 | |
201 | |
210 | void |
202 | void |
211 | decay_objects (mapstruct *m) |
203 | decay_objects (maptile *m) |
212 | { |
204 | { |
213 | int x, y, destroy; |
205 | int x, y, destroy; |
214 | object *op, *otmp; |
206 | object *op, *otmp; |
215 | |
207 | |
216 | if (m->unique) |
208 | if (m->unique) |
… | |
… | |
264 | if (op->material & M_ICE && MAP_TEMP (m) > 32) |
256 | if (op->material & M_ICE && MAP_TEMP (m) > 32) |
265 | destroy = 1; |
257 | destroy = 1; |
266 | } |
258 | } |
267 | /* adjust overall chance below */ |
259 | /* adjust overall chance below */ |
268 | if (destroy && rndm (0, 1)) |
260 | if (destroy && rndm (0, 1)) |
269 | { |
261 | op->destroy (); |
270 | remove_ob (op); |
|
|
271 | free_object (op); |
|
|
272 | } |
|
|
273 | } |
262 | } |
274 | } |
263 | } |
275 | |
264 | |
276 | /* convert materialname to materialtype_t */ |
265 | /* convert materialname to materialtype_t */ |
277 | |
266 | |
… | |
… | |
549 | strcpy (input, tmp); |
538 | strcpy (input, tmp); |
550 | |
539 | |
551 | return; |
540 | return; |
552 | } |
541 | } |
553 | |
542 | |
554 | void * |
543 | ///////////////////////////////////////////////////////////////////////////// |
555 | zero_initialised::operator new (size_t s, void *p) |
544 | |
|
|
545 | #if 0 |
|
|
546 | refcounted *refcounted::rc_first; |
|
|
547 | |
|
|
548 | refcounted::refcounted () |
556 | { |
549 | { |
557 | memset (p, 0, s); |
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 | |
|
|
562 | void *salloc (int size) throw (std::bad_alloc) |
|
|
563 | { |
|
|
564 | void *ptr = g_slice_alloc (size); |
|
|
565 | |
|
|
566 | if (!ptr) |
|
|
567 | throw std::bad_alloc (); |
|
|
568 | |
558 | return p; |
569 | return ptr; |
559 | } |
570 | } |
560 | |
571 | |
561 | void * |
572 | void *salloc (int size, void *src) throw (std::bad_alloc) |
562 | zero_initialised::operator new (size_t s) |
|
|
563 | { |
573 | { |
564 | //return calloc (1, s); |
574 | void *ptr = salloc (size); |
565 | return g_slice_alloc0 (s); |
|
|
566 | } |
|
|
567 | |
575 | |
568 | void * |
576 | if (src) |
569 | zero_initialised::operator new[] (size_t s) |
577 | memcpy (ptr, src, size); |
570 | { |
578 | else |
571 | //return calloc (1, s); |
579 | memset (ptr, 0, size); |
572 | return g_slice_alloc0 (s); |
|
|
573 | } |
|
|
574 | |
580 | |
575 | void |
581 | return ptr; |
576 | zero_initialised::operator delete (void *p, size_t s) |
|
|
577 | { |
|
|
578 | //free (p); return; |
|
|
579 | g_slice_free1 (s, p); |
|
|
580 | } |
|
|
581 | |
|
|
582 | void |
|
|
583 | zero_initialised::operator delete[] (void *p, size_t s) |
|
|
584 | { |
|
|
585 | //free (p); return; |
|
|
586 | g_slice_free1 (s, p); |
|
|
587 | } |
582 | } |
588 | |
583 | |
589 | void assign (char *dst, const char *src, int maxlen) |
584 | void assign (char *dst, const char *src, int maxlen) |
590 | { |
585 | { |
591 | if (!src) |
586 | if (!src) |
… | |
… | |
608 | } |
603 | } |
609 | else |
604 | else |
610 | memcpy (dst, src, len + 1); |
605 | memcpy (dst, src, len + 1); |
611 | } |
606 | } |
612 | |
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 | |