… | |
… | |
27 | |
27 | |
28 | #include <global.h> |
28 | #include <global.h> |
29 | #include <funcpoint.h> |
29 | #include <funcpoint.h> |
30 | #include <material.h> |
30 | #include <material.h> |
31 | |
31 | |
|
|
32 | #include <sys/time.h> |
|
|
33 | #include <time.h> |
32 | #include <glib.h> |
34 | #include <glib.h> |
33 | |
35 | |
34 | /* |
36 | /* |
35 | * The random functions here take luck into account when rolling random |
37 | * The random functions here take luck into account when rolling random |
36 | * 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 |
… | |
… | |
193 | return (min); |
195 | return (min); |
194 | |
196 | |
195 | return (RANDOM () % diff + min); |
197 | return (RANDOM () % diff + min); |
196 | } |
198 | } |
197 | |
199 | |
198 | /* decay and destroy persihable items in a map */ |
200 | /* decay and destroy perishable items in a map */ |
199 | |
|
|
200 | void |
201 | void |
201 | decay_objects (maptile *m) |
202 | maptile::decay_objects () |
202 | { |
203 | { |
203 | int x, y, destroy; |
204 | for (int x = 0; x < width; x++) |
204 | object *op, *otmp; |
205 | for (int y = 0; y < height; y++) |
205 | |
206 | for (object *above = 0, *op = at (x, y).bot; op; op = above) |
206 | if (m->unique) |
|
|
207 | return; |
|
|
208 | |
|
|
209 | for (x = 0; x < MAP_WIDTH (m); x++) |
|
|
210 | for (y = 0; y < MAP_HEIGHT (m); y++) |
|
|
211 | for (op = get_map_ob (m, x, y); op; op = otmp) |
|
|
212 | { |
207 | { |
213 | destroy = 0; |
208 | bool destroy = 0; |
|
|
209 | |
214 | otmp = op->above; |
210 | above = op->above; |
|
|
211 | |
|
|
212 | // do not decay anything above unique floor tiles (yet :) |
215 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) |
213 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) |
216 | break; |
214 | break; |
|
|
215 | |
217 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) || |
216 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) |
218 | QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || |
217 | || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) |
219 | QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || |
218 | || QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) |
220 | QUERY_FLAG (op, FLAG_UNIQUE) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) || QUERY_FLAG (op, FLAG_UNPAID) || IS_LIVE (op)) |
219 | || QUERY_FLAG (op, FLAG_UNIQUE) |
221 | continue; |
220 | || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) |
222 | /* otherwise, we decay and destroy */ |
221 | || QUERY_FLAG (op, FLAG_UNPAID) |
223 | if (IS_WEAPON (op)) |
222 | || op->is_alive ()) |
|
|
223 | ; // do not decay |
|
|
224 | else if (op->is_weapon ()) |
224 | { |
225 | { |
225 | op->stats.dam--; |
226 | op->stats.dam--; |
226 | if (op->stats.dam < 0) |
227 | if (op->stats.dam < 0) |
227 | destroy = 1; |
228 | destroy = 1; |
228 | } |
229 | } |
229 | else if (IS_ARMOR (op)) |
230 | else if (op->is_armor ()) |
230 | { |
231 | { |
231 | op->stats.ac--; |
232 | op->stats.ac--; |
232 | if (op->stats.ac < 0) |
233 | if (op->stats.ac < 0) |
233 | destroy = 1; |
234 | destroy = 1; |
234 | } |
235 | } |
… | |
… | |
238 | if (op->stats.food < 0) |
239 | if (op->stats.food < 0) |
239 | destroy = 1; |
240 | destroy = 1; |
240 | } |
241 | } |
241 | else |
242 | else |
242 | { |
243 | { |
243 | if (op->material & M_PAPER || op->material & M_LEATHER || |
244 | int mat = op->material; |
244 | op->material & M_WOOD || op->material & M_ORGANIC || op->material & M_CLOTH || op->material & M_LIQUID) |
245 | |
245 | destroy = 1; |
246 | if (mat & M_PAPER |
|
|
247 | || mat & M_LEATHER |
|
|
248 | || mat & M_WOOD |
|
|
249 | || mat & M_ORGANIC |
|
|
250 | || mat & M_CLOTH |
|
|
251 | || mat & M_LIQUID |
246 | if (op->material & M_IRON && rndm (1, 5) == 1) |
252 | || (mat & M_IRON && rndm (1, 5) == 1) |
247 | destroy = 1; |
|
|
248 | if (op->material & M_GLASS && rndm (1, 2) == 1) |
253 | || (mat & M_GLASS && rndm (1, 2) == 1) |
249 | destroy = 1; |
|
|
250 | if ((op->material & M_STONE || op->material & M_ADAMANT) && rndm (1, 10) == 1) |
254 | || ((mat & M_STONE || mat & M_ADAMANT) && rndm (1, 10) == 1) |
251 | destroy = 1; |
|
|
252 | if ((op->material & M_SOFT_METAL || op->material & M_BONE) && rndm (1, 3) == 1) |
255 | || ((mat & M_SOFT_METAL || mat & M_BONE) && rndm (1, 3) == 1) |
253 | destroy = 1; |
256 | || (mat & M_ICE && temp > 32)) |
254 | if (op->material & M_ICE && MAP_TEMP (m) > 32) |
|
|
255 | destroy = 1; |
257 | destroy = 1; |
256 | } |
258 | } |
|
|
259 | |
257 | /* adjust overall chance below */ |
260 | /* adjust overall chance below */ |
258 | if (destroy && rndm (0, 1)) |
261 | if (destroy && rndm (0, 1)) |
259 | op->destroy (); |
262 | op->destroy (); |
260 | } |
263 | } |
261 | } |
264 | } |
… | |
… | |
293 | return; |
296 | return; |
294 | |
297 | |
295 | if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) |
298 | if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) |
296 | return; |
299 | return; |
297 | |
300 | |
298 | if (!IS_ARMOR (op)) |
301 | if (!op->is_armor ()) |
299 | return; |
302 | return; |
300 | |
303 | |
301 | mt = name_to_material (op->materialname); |
304 | mt = name_to_material (op->materialname); |
302 | if (!mt) |
305 | if (!mt) |
303 | { |
306 | { |
… | |
… | |
349 | { |
352 | { |
350 | if (op->material & mt->material && rndm (1, 100) <= mt->chance && |
353 | if (op->material & mt->material && rndm (1, 100) <= mt->chance && |
351 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
354 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
352 | { |
355 | { |
353 | lmt = mt; |
356 | lmt = mt; |
354 | if (!(IS_WEAPON (op) || IS_ARMOR (op))) |
357 | if (!(op->is_weapon () || op->is_armor ())) |
355 | break; |
358 | break; |
356 | } |
359 | } |
357 | } |
360 | } |
358 | #endif |
361 | #endif |
359 | } |
362 | } |
… | |
… | |
367 | #ifndef NEW_MATERIAL_CODE |
370 | #ifndef NEW_MATERIAL_CODE |
368 | op->materialname = lmt->name; |
371 | op->materialname = lmt->name; |
369 | return; |
372 | return; |
370 | #else |
373 | #else |
371 | |
374 | |
372 | if (op->stats.dam && IS_WEAPON (op)) |
375 | if (op->stats.dam && op->is_weapon ()) |
373 | { |
376 | { |
374 | op->stats.dam += lmt->damage; |
377 | op->stats.dam += lmt->damage; |
375 | if (op->stats.dam < 1) |
378 | if (op->stats.dam < 1) |
376 | op->stats.dam = 1; |
379 | op->stats.dam = 1; |
377 | } |
380 | } |
378 | if (op->stats.sp && op->type == BOW) |
381 | if (op->stats.sp && op->type == BOW) |
379 | op->stats.sp += lmt->sp; |
382 | op->stats.sp += lmt->sp; |
380 | if (op->stats.wc && IS_WEAPON (op)) |
383 | if (op->stats.wc && op->is_weapon ()) |
381 | op->stats.wc += lmt->wc; |
384 | op->stats.wc += lmt->wc; |
382 | if (IS_ARMOR (op)) |
385 | if (op->is_armor ()) |
383 | { |
386 | { |
384 | if (op->stats.ac) |
387 | if (op->stats.ac) |
385 | op->stats.ac += lmt->ac; |
388 | op->stats.ac += lmt->ac; |
386 | for (j = 0; j < NROFATTACKS; j++) |
389 | for (j = 0; j < NROFATTACKS; j++) |
387 | if (op->resist[j] != 0) |
390 | if (op->resist[j] != 0) |
… | |
… | |
393 | op->resist[j] = -100; |
396 | op->resist[j] = -100; |
394 | } |
397 | } |
395 | } |
398 | } |
396 | op->materialname = add_string (lmt->name); |
399 | op->materialname = add_string (lmt->name); |
397 | /* dont make it unstackable if it doesn't need to be */ |
400 | /* dont make it unstackable if it doesn't need to be */ |
398 | if (IS_WEAPON (op) || IS_ARMOR (op)) |
401 | if (op->is_weapon () || op->is_armor ()) |
399 | { |
402 | { |
400 | op->weight = (op->weight * lmt->weight) / 100; |
403 | op->weight = (op->weight * lmt->weight) / 100; |
401 | op->value = (op->value * lmt->value) / 100; |
404 | op->value = (op->value * lmt->value) / 100; |
402 | } |
405 | } |
403 | #endif |
406 | #endif |
… | |
… | |
538 | return; |
541 | return; |
539 | } |
542 | } |
540 | |
543 | |
541 | ///////////////////////////////////////////////////////////////////////////// |
544 | ///////////////////////////////////////////////////////////////////////////// |
542 | |
545 | |
543 | #if 0 |
|
|
544 | refcounted *refcounted::rc_first; |
|
|
545 | |
|
|
546 | refcounted::refcounted () |
|
|
547 | { |
|
|
548 | refcnt = 0; |
|
|
549 | rc_next = rc_first; |
|
|
550 | rc_first = this; |
|
|
551 | } |
|
|
552 | |
|
|
553 | refcounted::~refcounted () |
|
|
554 | { |
|
|
555 | assert (!rc_next); |
|
|
556 | assert (!refcnt); |
|
|
557 | } |
|
|
558 | #endif |
|
|
559 | |
|
|
560 | void *alloc (int s) throw (std::bad_alloc) |
546 | void *salloc_ (int n) throw (std::bad_alloc) |
561 | { |
547 | { |
562 | void *p = g_slice_alloc (s); |
548 | void *ptr = g_slice_alloc (n); |
563 | |
549 | |
564 | if (!p) |
550 | if (!ptr) |
565 | throw std::bad_alloc (); |
551 | throw std::bad_alloc (); |
566 | |
552 | |
567 | return p; |
553 | return ptr; |
|
|
554 | } |
|
|
555 | |
|
|
556 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
|
|
557 | { |
|
|
558 | void *ptr = salloc_ (n); |
|
|
559 | |
|
|
560 | if (src) |
|
|
561 | memcpy (ptr, src, n); |
|
|
562 | else |
|
|
563 | memset (ptr, 0, n); |
|
|
564 | |
|
|
565 | return ptr; |
568 | } |
566 | } |
569 | |
567 | |
570 | void assign (char *dst, const char *src, int maxlen) |
568 | void assign (char *dst, const char *src, int maxlen) |
571 | { |
569 | { |
572 | if (!src) |
570 | if (!src) |
… | |
… | |
589 | } |
587 | } |
590 | else |
588 | else |
591 | memcpy (dst, src, len + 1); |
589 | memcpy (dst, src, len + 1); |
592 | } |
590 | } |
593 | |
591 | |
|
|
592 | tstamp now () |
|
|
593 | { |
|
|
594 | struct timeval tv; |
594 | |
595 | |
|
|
596 | gettimeofday (&tv, 0); |
|
|
597 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
|
|
598 | } |
|
|
599 | |
|
|
600 | int |
|
|
601 | similar_direction (int a, int b) |
|
|
602 | { |
|
|
603 | if (!a || !b) |
|
|
604 | return 0; |
|
|
605 | |
|
|
606 | int diff = (b - a) & 7; |
|
|
607 | return diff <= 1 || diff >= 7; |
|
|
608 | } |
|
|
609 | |