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) |
… | |
… | |
222 | { |
214 | { |
223 | destroy = 0; |
215 | destroy = 0; |
224 | otmp = op->above; |
216 | otmp = op->above; |
225 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) |
217 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) |
226 | break; |
218 | break; |
227 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) || |
219 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) |
228 | QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || |
220 | || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) |
229 | QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || |
221 | || QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) |
230 | QUERY_FLAG (op, FLAG_UNIQUE) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) || QUERY_FLAG (op, FLAG_UNPAID) || IS_LIVE (op)) |
222 | || QUERY_FLAG (op, FLAG_UNIQUE) |
|
|
223 | || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) |
|
|
224 | || QUERY_FLAG (op, FLAG_UNPAID) |
|
|
225 | || op->is_alive ()) |
231 | continue; |
226 | continue; |
|
|
227 | |
232 | /* otherwise, we decay and destroy */ |
228 | /* otherwise, we decay and destroy */ |
233 | if (IS_WEAPON (op)) |
229 | if (op->is_weapon ()) |
234 | { |
230 | { |
235 | op->stats.dam--; |
231 | op->stats.dam--; |
236 | if (op->stats.dam < 0) |
232 | if (op->stats.dam < 0) |
237 | destroy = 1; |
233 | destroy = 1; |
238 | } |
234 | } |
239 | else if (IS_ARMOR (op)) |
235 | else if (op->is_armor ()) |
240 | { |
236 | { |
241 | op->stats.ac--; |
237 | op->stats.ac--; |
242 | if (op->stats.ac < 0) |
238 | if (op->stats.ac < 0) |
243 | destroy = 1; |
239 | destroy = 1; |
244 | } |
240 | } |
… | |
… | |
264 | if (op->material & M_ICE && MAP_TEMP (m) > 32) |
260 | if (op->material & M_ICE && MAP_TEMP (m) > 32) |
265 | destroy = 1; |
261 | destroy = 1; |
266 | } |
262 | } |
267 | /* adjust overall chance below */ |
263 | /* adjust overall chance below */ |
268 | if (destroy && rndm (0, 1)) |
264 | if (destroy && rndm (0, 1)) |
269 | { |
265 | op->destroy (); |
270 | remove_ob (op); |
|
|
271 | free_object (op); |
|
|
272 | } |
|
|
273 | } |
266 | } |
274 | } |
267 | } |
275 | |
268 | |
276 | /* convert materialname to materialtype_t */ |
269 | /* convert materialname to materialtype_t */ |
277 | |
270 | |
… | |
… | |
306 | return; |
299 | return; |
307 | |
300 | |
308 | if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) |
301 | if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) |
309 | return; |
302 | return; |
310 | |
303 | |
311 | if (!IS_ARMOR (op)) |
304 | if (!op->is_armor ()) |
312 | return; |
305 | return; |
313 | |
306 | |
314 | mt = name_to_material (op->materialname); |
307 | mt = name_to_material (op->materialname); |
315 | if (!mt) |
308 | if (!mt) |
316 | { |
309 | { |
… | |
… | |
362 | { |
355 | { |
363 | if (op->material & mt->material && rndm (1, 100) <= mt->chance && |
356 | if (op->material & mt->material && rndm (1, 100) <= mt->chance && |
364 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
357 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
365 | { |
358 | { |
366 | lmt = mt; |
359 | lmt = mt; |
367 | if (!(IS_WEAPON (op) || IS_ARMOR (op))) |
360 | if (!(op->is_weapon () || op->is_armor ())) |
368 | break; |
361 | break; |
369 | } |
362 | } |
370 | } |
363 | } |
371 | #endif |
364 | #endif |
372 | } |
365 | } |
… | |
… | |
380 | #ifndef NEW_MATERIAL_CODE |
373 | #ifndef NEW_MATERIAL_CODE |
381 | op->materialname = lmt->name; |
374 | op->materialname = lmt->name; |
382 | return; |
375 | return; |
383 | #else |
376 | #else |
384 | |
377 | |
385 | if (op->stats.dam && IS_WEAPON (op)) |
378 | if (op->stats.dam && op->is_weapon ()) |
386 | { |
379 | { |
387 | op->stats.dam += lmt->damage; |
380 | op->stats.dam += lmt->damage; |
388 | if (op->stats.dam < 1) |
381 | if (op->stats.dam < 1) |
389 | op->stats.dam = 1; |
382 | op->stats.dam = 1; |
390 | } |
383 | } |
391 | if (op->stats.sp && op->type == BOW) |
384 | if (op->stats.sp && op->type == BOW) |
392 | op->stats.sp += lmt->sp; |
385 | op->stats.sp += lmt->sp; |
393 | if (op->stats.wc && IS_WEAPON (op)) |
386 | if (op->stats.wc && op->is_weapon ()) |
394 | op->stats.wc += lmt->wc; |
387 | op->stats.wc += lmt->wc; |
395 | if (IS_ARMOR (op)) |
388 | if (op->is_armor ()) |
396 | { |
389 | { |
397 | if (op->stats.ac) |
390 | if (op->stats.ac) |
398 | op->stats.ac += lmt->ac; |
391 | op->stats.ac += lmt->ac; |
399 | for (j = 0; j < NROFATTACKS; j++) |
392 | for (j = 0; j < NROFATTACKS; j++) |
400 | if (op->resist[j] != 0) |
393 | if (op->resist[j] != 0) |
… | |
… | |
406 | op->resist[j] = -100; |
399 | op->resist[j] = -100; |
407 | } |
400 | } |
408 | } |
401 | } |
409 | op->materialname = add_string (lmt->name); |
402 | op->materialname = add_string (lmt->name); |
410 | /* dont make it unstackable if it doesn't need to be */ |
403 | /* dont make it unstackable if it doesn't need to be */ |
411 | if (IS_WEAPON (op) || IS_ARMOR (op)) |
404 | if (op->is_weapon () || op->is_armor ()) |
412 | { |
405 | { |
413 | op->weight = (op->weight * lmt->weight) / 100; |
406 | op->weight = (op->weight * lmt->weight) / 100; |
414 | op->value = (op->value * lmt->value) / 100; |
407 | op->value = (op->value * lmt->value) / 100; |
415 | } |
408 | } |
416 | #endif |
409 | #endif |
… | |
… | |
531 | |
524 | |
532 | strncpy (tmp, input, MAX_BUF - 5); |
525 | strncpy (tmp, input, MAX_BUF - 5); |
533 | /*trim all trailing commas, spaces etc. */ |
526 | /*trim all trailing commas, spaces etc. */ |
534 | for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) |
527 | for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) |
535 | tmp[i] = '\0'; |
528 | tmp[i] = '\0'; |
|
|
529 | |
536 | strcat (tmp, "."); |
530 | strcat (tmp, "."); |
537 | |
531 | |
538 | p = strrchr (tmp, ','); |
532 | p = strrchr (tmp, ','); |
539 | if (p) |
533 | if (p) |
540 | { |
534 | { |
… | |
… | |
544 | strcat (input, " and"); |
538 | strcat (input, " and"); |
545 | strcat (input, p); |
539 | strcat (input, p); |
546 | } |
540 | } |
547 | else |
541 | else |
548 | strcpy (input, tmp); |
542 | strcpy (input, tmp); |
|
|
543 | |
549 | return; |
544 | return; |
550 | } |
545 | } |
551 | |
546 | |
552 | void * |
547 | ///////////////////////////////////////////////////////////////////////////// |
553 | zero_initialised::operator new (size_t s, void *p) |
548 | |
|
|
549 | #if 0 |
|
|
550 | refcounted *refcounted::rc_first; |
|
|
551 | |
|
|
552 | refcounted::refcounted () |
554 | { |
553 | { |
555 | memset (p, 0, s); |
554 | refcnt = 0; |
|
|
555 | rc_next = rc_first; |
|
|
556 | rc_first = this; |
|
|
557 | } |
|
|
558 | |
|
|
559 | refcounted::~refcounted () |
|
|
560 | { |
|
|
561 | assert (!rc_next); |
|
|
562 | assert (!refcnt); |
|
|
563 | } |
|
|
564 | #endif |
|
|
565 | |
|
|
566 | void *salloc_ (int n) throw (std::bad_alloc) |
|
|
567 | { |
|
|
568 | void *ptr = g_slice_alloc (n); |
|
|
569 | |
|
|
570 | if (!ptr) |
|
|
571 | throw std::bad_alloc (); |
|
|
572 | |
556 | return p; |
573 | return ptr; |
557 | } |
574 | } |
558 | |
575 | |
559 | void * |
576 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
560 | zero_initialised::operator new (size_t s) |
|
|
561 | { |
577 | { |
562 | //return calloc (1, s); |
578 | void *ptr = salloc_ (n); |
563 | return g_slice_alloc0 (s); |
|
|
564 | } |
|
|
565 | |
579 | |
566 | void * |
580 | if (src) |
567 | zero_initialised::operator new[] (size_t s) |
581 | memcpy (ptr, src, n); |
568 | { |
582 | else |
569 | //return calloc (1, s); |
583 | memset (ptr, 0, n); |
570 | return g_slice_alloc0 (s); |
|
|
571 | } |
|
|
572 | |
584 | |
573 | void |
585 | return ptr; |
574 | zero_initialised::operator delete (void *p, size_t s) |
|
|
575 | { |
|
|
576 | //free (p); return; |
|
|
577 | g_slice_free1 (s, p); |
|
|
578 | } |
586 | } |
579 | |
587 | |
580 | void |
588 | void assign (char *dst, const char *src, int maxlen) |
581 | zero_initialised::operator delete[] (void *p, size_t s) |
|
|
582 | { |
589 | { |
583 | //free (p); return; |
590 | if (!src) |
584 | g_slice_free1 (s, p); |
591 | src = ""; |
|
|
592 | |
|
|
593 | int len = strlen (src); |
|
|
594 | |
|
|
595 | if (len >= maxlen - 1) |
|
|
596 | { |
|
|
597 | if (maxlen <= 4) |
|
|
598 | { |
|
|
599 | memset (dst, '.', maxlen - 1); |
|
|
600 | dst [maxlen - 1] = 0; |
|
|
601 | } |
|
|
602 | else |
|
|
603 | { |
|
|
604 | memcpy (dst, src, maxlen - 4); |
|
|
605 | memcpy (dst + maxlen - 4, "...", 4); |
|
|
606 | } |
|
|
607 | } |
|
|
608 | else |
|
|
609 | memcpy (dst, src, len + 1); |
585 | } |
610 | } |
|
|
611 | |
|
|
612 | tstamp now () |
|
|
613 | { |
|
|
614 | struct timeval tv; |
|
|
615 | |
|
|
616 | gettimeofday (&tv, 0); |
|
|
617 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
|
|
618 | } |
|
|
619 | |