1 | |
|
|
2 | /* |
|
|
3 | * static char *rcsid_utils_c = |
|
|
4 | * "$Id: utils.C,v 1.10 2006/09/11 01:16:20 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 | |
… | |
… | |
531 | |
520 | |
532 | strncpy (tmp, input, MAX_BUF - 5); |
521 | strncpy (tmp, input, MAX_BUF - 5); |
533 | /*trim all trailing commas, spaces etc. */ |
522 | /*trim all trailing commas, spaces etc. */ |
534 | for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) |
523 | for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) |
535 | tmp[i] = '\0'; |
524 | tmp[i] = '\0'; |
|
|
525 | |
536 | strcat (tmp, "."); |
526 | strcat (tmp, "."); |
537 | |
527 | |
538 | p = strrchr (tmp, ','); |
528 | p = strrchr (tmp, ','); |
539 | if (p) |
529 | if (p) |
540 | { |
530 | { |
… | |
… | |
544 | strcat (input, " and"); |
534 | strcat (input, " and"); |
545 | strcat (input, p); |
535 | strcat (input, p); |
546 | } |
536 | } |
547 | else |
537 | else |
548 | strcpy (input, tmp); |
538 | strcpy (input, tmp); |
|
|
539 | |
549 | return; |
540 | return; |
550 | } |
541 | } |
551 | |
542 | |
552 | void * |
543 | ///////////////////////////////////////////////////////////////////////////// |
553 | zero_initialised::operator new (size_t s, void *p) |
544 | |
|
|
545 | #if 0 |
|
|
546 | refcounted *refcounted::rc_first; |
|
|
547 | |
|
|
548 | refcounted::refcounted () |
554 | { |
549 | { |
555 | 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 n) throw (std::bad_alloc) |
|
|
563 | { |
|
|
564 | void *ptr = g_slice_alloc (n); |
|
|
565 | |
|
|
566 | if (!ptr) |
|
|
567 | throw std::bad_alloc (); |
|
|
568 | |
556 | return p; |
569 | return ptr; |
557 | } |
570 | } |
558 | |
571 | |
559 | void * |
572 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
560 | zero_initialised::operator new (size_t s) |
|
|
561 | { |
573 | { |
562 | //return calloc (1, s); |
574 | void *ptr = salloc_ (n); |
563 | return g_slice_alloc0 (s); |
|
|
564 | } |
|
|
565 | |
575 | |
566 | void * |
576 | if (src) |
567 | zero_initialised::operator new[] (size_t s) |
577 | memcpy (ptr, src, n); |
568 | { |
578 | else |
569 | //return calloc (1, s); |
579 | memset (ptr, 0, n); |
570 | return g_slice_alloc0 (s); |
|
|
571 | } |
|
|
572 | |
580 | |
573 | void |
581 | return ptr; |
574 | zero_initialised::operator delete (void *p, size_t s) |
|
|
575 | { |
|
|
576 | //free (p); return; |
|
|
577 | g_slice_free1 (s, p); |
|
|
578 | } |
582 | } |
579 | |
583 | |
580 | void |
584 | void assign (char *dst, const char *src, int maxlen) |
581 | zero_initialised::operator delete[] (void *p, size_t s) |
|
|
582 | { |
585 | { |
583 | //free (p); return; |
586 | if (!src) |
584 | g_slice_free1 (s, p); |
587 | src = ""; |
|
|
588 | |
|
|
589 | int len = strlen (src); |
|
|
590 | |
|
|
591 | if (len >= maxlen - 1) |
|
|
592 | { |
|
|
593 | if (maxlen <= 4) |
|
|
594 | { |
|
|
595 | memset (dst, '.', maxlen - 1); |
|
|
596 | dst [maxlen - 1] = 0; |
|
|
597 | } |
|
|
598 | else |
|
|
599 | { |
|
|
600 | memcpy (dst, src, maxlen - 4); |
|
|
601 | memcpy (dst + maxlen - 4, "...", 4); |
|
|
602 | } |
|
|
603 | } |
|
|
604 | else |
|
|
605 | memcpy (dst, src, len + 1); |
585 | } |
606 | } |
|
|
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 | |