1 | |
|
|
2 | /* |
|
|
3 | * static char *rcsid_utils_c = |
|
|
4 | * "$Id: utils.C,v 1.9 2006/09/10 16:00:23 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 | */ |
… | |
… | |
103 | diff = max - min + 1; |
97 | diff = max - min + 1; |
104 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
98 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
105 | |
99 | |
106 | if (max < 1 || diff < 1) |
100 | if (max < 1 || diff < 1) |
107 | { |
101 | { |
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); |
102 | LOG (llevError, "Calling random_roll with min=%" PRId64 " max=%" PRId64 "\n", min, max); |
112 | #endif |
|
|
113 | return (min); /* avoids a float exception */ |
103 | return (min); /* avoids a float exception */ |
114 | } |
104 | } |
115 | |
105 | |
116 | /* Don't know of a portable call to get 64 bit random values. |
106 | /* 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 |
107 | * So make a call to get two 32 bit random numbers, and just to |
… | |
… | |
206 | } |
196 | } |
207 | |
197 | |
208 | /* decay and destroy persihable items in a map */ |
198 | /* decay and destroy persihable items in a map */ |
209 | |
199 | |
210 | void |
200 | void |
211 | decay_objects (mapstruct *m) |
201 | decay_objects (maptile *m) |
212 | { |
202 | { |
213 | int x, y, destroy; |
203 | int x, y, destroy; |
214 | object *op, *otmp; |
204 | object *op, *otmp; |
215 | |
205 | |
216 | if (m->unique) |
206 | if (m->unique) |
… | |
… | |
265 | destroy = 1; |
255 | destroy = 1; |
266 | } |
256 | } |
267 | /* adjust overall chance below */ |
257 | /* adjust overall chance below */ |
268 | if (destroy && rndm (0, 1)) |
258 | if (destroy && rndm (0, 1)) |
269 | { |
259 | { |
270 | remove_ob (op); |
260 | op->remove (); |
271 | free_object (op); |
261 | op->destroy (0); |
272 | } |
262 | } |
273 | } |
263 | } |
274 | } |
264 | } |
275 | |
265 | |
276 | /* convert materialname to materialtype_t */ |
266 | /* convert materialname to materialtype_t */ |
… | |
… | |
531 | |
521 | |
532 | strncpy (tmp, input, MAX_BUF - 5); |
522 | strncpy (tmp, input, MAX_BUF - 5); |
533 | /*trim all trailing commas, spaces etc. */ |
523 | /*trim all trailing commas, spaces etc. */ |
534 | for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) |
524 | for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) |
535 | tmp[i] = '\0'; |
525 | tmp[i] = '\0'; |
|
|
526 | |
536 | strcat (tmp, "."); |
527 | strcat (tmp, "."); |
537 | |
528 | |
538 | p = strrchr (tmp, ','); |
529 | p = strrchr (tmp, ','); |
539 | if (p) |
530 | if (p) |
540 | { |
531 | { |
… | |
… | |
544 | strcat (input, " and"); |
535 | strcat (input, " and"); |
545 | strcat (input, p); |
536 | strcat (input, p); |
546 | } |
537 | } |
547 | else |
538 | else |
548 | strcpy (input, tmp); |
539 | strcpy (input, tmp); |
|
|
540 | |
549 | return; |
541 | return; |
550 | } |
542 | } |
551 | |
543 | |
552 | void * |
544 | ///////////////////////////////////////////////////////////////////////////// |
553 | zero_initialised::operator |
|
|
554 | new (size_t s) |
|
|
555 | { |
|
|
556 | //return calloc (1, s); |
|
|
557 | return g_slice_alloc0 (s); |
|
|
558 | } |
|
|
559 | |
545 | |
560 | void * |
546 | #if 0 |
561 | zero_initialised::operator |
547 | refcounted *refcounted::rc_first; |
562 | new[] (size_t s) |
|
|
563 | { |
|
|
564 | //return calloc (1, s); |
|
|
565 | return g_slice_alloc0 (s); |
|
|
566 | } |
|
|
567 | |
548 | |
568 | void |
549 | refcounted::refcounted () |
569 | zero_initialised::operator |
|
|
570 | delete (void *p, size_t s) |
|
|
571 | { |
550 | { |
572 | //free (p); return; |
551 | refcnt = 0; |
573 | g_slice_free1 (s, p); |
552 | rc_next = rc_first; |
|
|
553 | rc_first = this; |
574 | } |
554 | } |
575 | |
555 | |
576 | void |
556 | refcounted::~refcounted () |
577 | zero_initialised::operator |
|
|
578 | delete[] (void *p, size_t s) |
|
|
579 | { |
557 | { |
580 | //free (p); return; |
558 | assert (!rc_next); |
581 | g_slice_free1 (s, p); |
559 | assert (!refcnt); |
582 | } |
560 | } |
|
|
561 | #endif |
|
|
562 | |
|
|
563 | void *alloc (int s) throw (std::bad_alloc) |
|
|
564 | { |
|
|
565 | void *p = g_slice_alloc (s); |
|
|
566 | |
|
|
567 | if (!p) |
|
|
568 | throw std::bad_alloc (); |
|
|
569 | |
|
|
570 | return p; |
|
|
571 | } |
|
|
572 | |
|
|
573 | void assign (char *dst, const char *src, int maxlen) |
|
|
574 | { |
|
|
575 | if (!src) |
|
|
576 | src = ""; |
|
|
577 | |
|
|
578 | int len = strlen (src); |
|
|
579 | |
|
|
580 | if (len >= maxlen - 1) |
|
|
581 | { |
|
|
582 | if (maxlen <= 4) |
|
|
583 | { |
|
|
584 | memset (dst, '.', maxlen - 1); |
|
|
585 | dst [maxlen - 1] = 0; |
|
|
586 | } |
|
|
587 | else |
|
|
588 | { |
|
|
589 | memcpy (dst, src, maxlen - 4); |
|
|
590 | memcpy (dst + maxlen - 4, "...", 4); |
|
|
591 | } |
|
|
592 | } |
|
|
593 | else |
|
|
594 | memcpy (dst, src, len + 1); |
|
|
595 | } |
|
|
596 | |
|
|
597 | |