1 | |
|
|
2 | /* |
|
|
3 | * static char *rcsid_utils_c = |
|
|
4 | * "$Id: utils.C,v 1.12 2006/09/12 20:55:40 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) |
217 | return; |
209 | return; |
218 | |
210 | |
219 | for (x = 0; x < MAP_WIDTH (m); x++) |
211 | for (x = 0; x < MAP_WIDTH (m); x++) |
220 | for (y = 0; y < MAP_HEIGHT (m); y++) |
212 | for (y = 0; y < MAP_HEIGHT (m); y++) |
221 | for (op = get_map_ob (m, x, y); op; op = otmp) |
213 | for (op = GET_MAP_OB (m, x, y); op; op = otmp) |
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 |
… | |
… | |
549 | strcpy (input, tmp); |
542 | strcpy (input, tmp); |
550 | |
543 | |
551 | return; |
544 | return; |
552 | } |
545 | } |
553 | |
546 | |
|
|
547 | ///////////////////////////////////////////////////////////////////////////// |
|
|
548 | |
554 | void *alloc (int s) throw (std::bad_alloc) |
549 | void *salloc_ (int n) throw (std::bad_alloc) |
555 | { |
550 | { |
|
|
551 | void *ptr = g_slice_alloc (n); |
|
|
552 | |
|
|
553 | if (!ptr) |
|
|
554 | throw std::bad_alloc (); |
|
|
555 | |
|
|
556 | return ptr; |
|
|
557 | } |
|
|
558 | |
|
|
559 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
|
|
560 | { |
|
|
561 | void *ptr = salloc_ (n); |
|
|
562 | |
556 | if (s) |
563 | if (src) |
557 | return g_slice_alloc (s); |
564 | memcpy (ptr, src, n); |
558 | else |
565 | else |
|
|
566 | memset (ptr, 0, n); |
|
|
567 | |
559 | return 0; |
568 | return ptr; |
560 | } |
|
|
561 | |
|
|
562 | void dealloc (void *p, int s) throw () |
|
|
563 | { |
|
|
564 | if (s) |
|
|
565 | g_slice_free1 (s, p); |
|
|
566 | } |
569 | } |
567 | |
570 | |
568 | void assign (char *dst, const char *src, int maxlen) |
571 | void assign (char *dst, const char *src, int maxlen) |
569 | { |
572 | { |
570 | if (!src) |
573 | if (!src) |
… | |
… | |
587 | } |
590 | } |
588 | else |
591 | else |
589 | memcpy (dst, src, len + 1); |
592 | memcpy (dst, src, len + 1); |
590 | } |
593 | } |
591 | |
594 | |
|
|
595 | tstamp now () |
|
|
596 | { |
|
|
597 | struct timeval tv; |
|
|
598 | |
|
|
599 | gettimeofday (&tv, 0); |
|
|
600 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
|
|
601 | } |
|
|
602 | |