1 | /* |
1 | /* |
2 | CrossFire, A Multiplayer game for X-windows |
2 | CrossFire, A Multiplayer game for X-windows |
3 | |
3 | |
|
|
4 | Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 | Copyright (C) 1992 Frank Tore Johansen |
6 | Copyright (C) 1992 Frank Tore Johansen |
6 | |
7 | |
7 | This program is free software; you can redistribute it and/or modify |
8 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
9 | it under the terms of the GNU General Public License as published by |
… | |
… | |
23 | |
24 | |
24 | /* |
25 | /* |
25 | * General convenience functions for crossfire. |
26 | * General convenience functions for crossfire. |
26 | */ |
27 | */ |
27 | |
28 | |
|
|
29 | #include <cstdlib> |
|
|
30 | #include <sys/types.h> |
|
|
31 | #include <unistd.h> |
|
|
32 | #include <sys/time.h> |
|
|
33 | #include <time.h> |
|
|
34 | #include <signal.h> |
|
|
35 | |
28 | #include <global.h> |
36 | #include <global.h> |
29 | #include <funcpoint.h> |
37 | #include <funcpoint.h> |
30 | #include <material.h> |
38 | #include <material.h> |
31 | |
39 | |
32 | #include <sys/time.h> |
|
|
33 | #include <time.h> |
|
|
34 | #include <glib.h> |
40 | #include <glib.h> |
35 | |
41 | |
36 | /* |
42 | /* |
37 | * The random functions here take luck into account when rolling random |
43 | * The random functions here take luck into account when rolling random |
38 | * dice or numbers. This function has less of an impact the larger the |
44 | * dice or numbers. This function has less of an impact the larger the |
… | |
… | |
195 | return (min); |
201 | return (min); |
196 | |
202 | |
197 | return (RANDOM () % diff + min); |
203 | return (RANDOM () % diff + min); |
198 | } |
204 | } |
199 | |
205 | |
200 | /* decay and destroy persihable items in a map */ |
206 | /* decay and destroy perishable items in a map */ |
201 | |
|
|
202 | void |
207 | void |
203 | decay_objects (maptile *m) |
208 | maptile::decay_objects () |
204 | { |
209 | { |
205 | int x, y, destroy; |
210 | if (!spaces) |
206 | object *op, *otmp; |
|
|
207 | |
|
|
208 | if (m->unique) |
|
|
209 | return; |
211 | return; |
210 | |
212 | |
211 | for (x = 0; x < MAP_WIDTH (m); x++) |
213 | for (mapspace *ms = spaces + size (); ms-- > spaces; ) |
212 | for (y = 0; y < MAP_HEIGHT (m); y++) |
214 | for (object *above, *op = ms->bot; op; op = above) |
213 | for (op = GET_MAP_OB (m, x, y); op; op = otmp) |
|
|
214 | { |
215 | { |
|
|
216 | above = op->above; |
|
|
217 | |
215 | destroy = 0; |
218 | bool destroy = 0; |
216 | otmp = op->above; |
219 | |
|
|
220 | // do not decay anything above unique floor tiles (yet :) |
217 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) |
221 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) |
218 | break; |
222 | break; |
|
|
223 | |
219 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) |
224 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) |
220 | || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) |
225 | || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) |
221 | || QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) |
226 | || QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) |
222 | || QUERY_FLAG (op, FLAG_UNIQUE) |
227 | || QUERY_FLAG (op, FLAG_UNIQUE) |
223 | || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) |
228 | || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) |
224 | || QUERY_FLAG (op, FLAG_UNPAID) |
229 | || QUERY_FLAG (op, FLAG_UNPAID) |
225 | || op->is_alive ()) |
230 | || op->is_alive ()) |
226 | continue; |
231 | ; // do not decay |
227 | |
|
|
228 | /* otherwise, we decay and destroy */ |
|
|
229 | if (op->is_weapon ()) |
232 | else if (op->is_weapon ()) |
230 | { |
233 | { |
231 | op->stats.dam--; |
234 | op->stats.dam--; |
232 | if (op->stats.dam < 0) |
235 | if (op->stats.dam < 0) |
233 | destroy = 1; |
236 | destroy = 1; |
234 | } |
237 | } |
235 | else if (op->is_armor ()) |
238 | else if (op->is_armor ()) |
236 | { |
239 | { |
237 | op->stats.ac--; |
240 | op->stats.ac--; |
238 | if (op->stats.ac < 0) |
241 | if (op->stats.ac < 0) |
239 | destroy = 1; |
242 | destroy = 1; |
240 | } |
243 | } |
241 | else if (op->type == FOOD) |
244 | else if (op->type == FOOD) |
242 | { |
245 | { |
243 | op->stats.food -= rndm (5, 20); |
246 | op->stats.food -= rndm (5, 20); |
244 | if (op->stats.food < 0) |
247 | if (op->stats.food < 0) |
245 | destroy = 1; |
248 | destroy = 1; |
246 | } |
249 | } |
247 | else |
250 | else |
248 | { |
251 | { |
249 | if (op->material & M_PAPER || op->material & M_LEATHER || |
252 | int mat = op->material; |
250 | op->material & M_WOOD || op->material & M_ORGANIC || op->material & M_CLOTH || op->material & M_LIQUID) |
253 | |
|
|
254 | if (mat & M_PAPER |
|
|
255 | || mat & M_LEATHER |
|
|
256 | || mat & M_WOOD |
|
|
257 | || mat & M_ORGANIC |
|
|
258 | || mat & M_CLOTH |
|
|
259 | || mat & M_LIQUID |
|
|
260 | || (mat & M_IRON && rndm (1, 5) == 1) |
|
|
261 | || (mat & M_GLASS && rndm (1, 2) == 1) |
|
|
262 | || ((mat & M_STONE || mat & M_ADAMANT) && rndm (1, 10) == 1) |
|
|
263 | || ((mat & M_SOFT_METAL || mat & M_BONE) && rndm (1, 3) == 1) |
|
|
264 | || (mat & M_ICE && temp > 32)) |
251 | destroy = 1; |
265 | destroy = 1; |
252 | if (op->material & M_IRON && rndm (1, 5) == 1) |
|
|
253 | destroy = 1; |
|
|
254 | if (op->material & M_GLASS && rndm (1, 2) == 1) |
|
|
255 | destroy = 1; |
|
|
256 | if ((op->material & M_STONE || op->material & M_ADAMANT) && rndm (1, 10) == 1) |
|
|
257 | destroy = 1; |
|
|
258 | if ((op->material & M_SOFT_METAL || op->material & M_BONE) && rndm (1, 3) == 1) |
|
|
259 | destroy = 1; |
|
|
260 | if (op->material & M_ICE && MAP_TEMP (m) > 32) |
|
|
261 | destroy = 1; |
|
|
262 | } |
266 | } |
|
|
267 | |
263 | /* adjust overall chance below */ |
268 | /* adjust overall chance below */ |
264 | if (destroy && rndm (0, 1)) |
269 | if (destroy && rndm (0, 1)) |
265 | op->destroy (); |
270 | op->destroy (); |
266 | } |
271 | } |
267 | } |
272 | } |
268 | |
273 | |
269 | /* convert materialname to materialtype_t */ |
274 | /* convert materialname to materialtype_t */ |
270 | |
275 | |
271 | materialtype_t * |
276 | materialtype_t * |
… | |
… | |
544 | return; |
549 | return; |
545 | } |
550 | } |
546 | |
551 | |
547 | ///////////////////////////////////////////////////////////////////////////// |
552 | ///////////////////////////////////////////////////////////////////////////// |
548 | |
553 | |
549 | #if 0 |
554 | void |
550 | refcounted *refcounted::rc_first; |
555 | fork_abort (const char *msg) |
551 | |
|
|
552 | refcounted::refcounted () |
|
|
553 | { |
556 | { |
554 | refcnt = 0; |
557 | if (!fork ()) |
555 | rc_next = rc_first; |
558 | { |
556 | rc_first = this; |
559 | signal (SIGABRT, SIG_DFL); |
557 | } |
560 | abort (); |
558 | #endif |
561 | } |
559 | |
562 | |
560 | refcounted::~refcounted () |
563 | LOG (llevError, "fork abort: %s\n", msg); |
561 | { |
|
|
562 | #if 0 |
|
|
563 | assert (!rc_next); |
|
|
564 | assert (!refcnt); |
|
|
565 | #endif |
|
|
566 | } |
564 | } |
567 | |
565 | |
568 | void *salloc_ (int n) throw (std::bad_alloc) |
566 | void *salloc_ (int n) throw (std::bad_alloc) |
569 | { |
567 | { |
570 | void *ptr = g_slice_alloc (n); |
568 | void *ptr = g_slice_alloc (n); |
… | |
… | |
617 | |
615 | |
618 | gettimeofday (&tv, 0); |
616 | gettimeofday (&tv, 0); |
619 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
617 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
620 | } |
618 | } |
621 | |
619 | |
|
|
620 | int |
|
|
621 | similar_direction (int a, int b) |
|
|
622 | { |
|
|
623 | if (!a || !b) |
|
|
624 | return 0; |
|
|
625 | |
|
|
626 | int diff = (b - a) & 7; |
|
|
627 | return diff <= 1 || diff >= 7; |
|
|
628 | } |
|
|
629 | |