… | |
… | |
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 | int x, y, destroy; |
204 | object *op, *otmp; |
205 | object *op, *otmp; |
205 | |
206 | |
206 | if (m->unique) |
207 | for (x = 0; x < width; x++) |
207 | return; |
208 | for (y = 0; y < height; y++) |
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) |
209 | for (op = at (x, y).bot; op; op = otmp) |
212 | { |
210 | { |
213 | destroy = 0; |
211 | destroy = 0; |
214 | otmp = op->above; |
212 | otmp = op->above; |
|
|
213 | |
215 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) |
214 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) |
216 | break; |
215 | break; |
|
|
216 | |
217 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) || |
217 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) |
218 | QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || |
218 | || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) |
219 | QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || |
219 | || 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)) |
220 | || QUERY_FLAG (op, FLAG_UNIQUE) |
|
|
221 | || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) |
|
|
222 | || QUERY_FLAG (op, FLAG_UNPAID) |
|
|
223 | || op->is_alive ()) |
221 | continue; |
224 | continue; |
|
|
225 | |
222 | /* otherwise, we decay and destroy */ |
226 | /* otherwise, we decay and destroy */ |
223 | if (IS_WEAPON (op)) |
227 | if (op->is_weapon ()) |
224 | { |
228 | { |
225 | op->stats.dam--; |
229 | op->stats.dam--; |
226 | if (op->stats.dam < 0) |
230 | if (op->stats.dam < 0) |
227 | destroy = 1; |
231 | destroy = 1; |
228 | } |
232 | } |
229 | else if (IS_ARMOR (op)) |
233 | else if (op->is_armor ()) |
230 | { |
234 | { |
231 | op->stats.ac--; |
235 | op->stats.ac--; |
232 | if (op->stats.ac < 0) |
236 | if (op->stats.ac < 0) |
233 | destroy = 1; |
237 | destroy = 1; |
234 | } |
238 | } |
… | |
… | |
241 | else |
245 | else |
242 | { |
246 | { |
243 | if (op->material & M_PAPER || op->material & M_LEATHER || |
247 | if (op->material & M_PAPER || op->material & M_LEATHER || |
244 | op->material & M_WOOD || op->material & M_ORGANIC || op->material & M_CLOTH || op->material & M_LIQUID) |
248 | op->material & M_WOOD || op->material & M_ORGANIC || op->material & M_CLOTH || op->material & M_LIQUID) |
245 | destroy = 1; |
249 | destroy = 1; |
|
|
250 | |
246 | if (op->material & M_IRON && rndm (1, 5) == 1) |
251 | if (op->material & M_IRON && rndm (1, 5) == 1) |
247 | destroy = 1; |
252 | destroy = 1; |
|
|
253 | |
248 | if (op->material & M_GLASS && rndm (1, 2) == 1) |
254 | if (op->material & M_GLASS && rndm (1, 2) == 1) |
249 | destroy = 1; |
255 | destroy = 1; |
|
|
256 | |
250 | if ((op->material & M_STONE || op->material & M_ADAMANT) && rndm (1, 10) == 1) |
257 | if ((op->material & M_STONE || op->material & M_ADAMANT) && rndm (1, 10) == 1) |
251 | destroy = 1; |
258 | destroy = 1; |
|
|
259 | |
252 | if ((op->material & M_SOFT_METAL || op->material & M_BONE) && rndm (1, 3) == 1) |
260 | if ((op->material & M_SOFT_METAL || op->material & M_BONE) && rndm (1, 3) == 1) |
253 | destroy = 1; |
261 | destroy = 1; |
|
|
262 | |
254 | if (op->material & M_ICE && MAP_TEMP (m) > 32) |
263 | if (op->material & M_ICE && temp > 32) |
255 | destroy = 1; |
264 | destroy = 1; |
256 | } |
265 | } |
|
|
266 | |
257 | /* adjust overall chance below */ |
267 | /* adjust overall chance below */ |
258 | if (destroy && rndm (0, 1)) |
268 | if (destroy && rndm (0, 1)) |
259 | { |
269 | op->destroy (); |
260 | remove_ob (op); |
|
|
261 | free_object (op); |
|
|
262 | } |
|
|
263 | } |
270 | } |
264 | } |
271 | } |
265 | |
272 | |
266 | /* convert materialname to materialtype_t */ |
273 | /* convert materialname to materialtype_t */ |
267 | |
274 | |
… | |
… | |
296 | return; |
303 | return; |
297 | |
304 | |
298 | if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) |
305 | if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) |
299 | return; |
306 | return; |
300 | |
307 | |
301 | if (!IS_ARMOR (op)) |
308 | if (!op->is_armor ()) |
302 | return; |
309 | return; |
303 | |
310 | |
304 | mt = name_to_material (op->materialname); |
311 | mt = name_to_material (op->materialname); |
305 | if (!mt) |
312 | if (!mt) |
306 | { |
313 | { |
… | |
… | |
352 | { |
359 | { |
353 | if (op->material & mt->material && rndm (1, 100) <= mt->chance && |
360 | if (op->material & mt->material && rndm (1, 100) <= mt->chance && |
354 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
361 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
355 | { |
362 | { |
356 | lmt = mt; |
363 | lmt = mt; |
357 | if (!(IS_WEAPON (op) || IS_ARMOR (op))) |
364 | if (!(op->is_weapon () || op->is_armor ())) |
358 | break; |
365 | break; |
359 | } |
366 | } |
360 | } |
367 | } |
361 | #endif |
368 | #endif |
362 | } |
369 | } |
… | |
… | |
370 | #ifndef NEW_MATERIAL_CODE |
377 | #ifndef NEW_MATERIAL_CODE |
371 | op->materialname = lmt->name; |
378 | op->materialname = lmt->name; |
372 | return; |
379 | return; |
373 | #else |
380 | #else |
374 | |
381 | |
375 | if (op->stats.dam && IS_WEAPON (op)) |
382 | if (op->stats.dam && op->is_weapon ()) |
376 | { |
383 | { |
377 | op->stats.dam += lmt->damage; |
384 | op->stats.dam += lmt->damage; |
378 | if (op->stats.dam < 1) |
385 | if (op->stats.dam < 1) |
379 | op->stats.dam = 1; |
386 | op->stats.dam = 1; |
380 | } |
387 | } |
381 | if (op->stats.sp && op->type == BOW) |
388 | if (op->stats.sp && op->type == BOW) |
382 | op->stats.sp += lmt->sp; |
389 | op->stats.sp += lmt->sp; |
383 | if (op->stats.wc && IS_WEAPON (op)) |
390 | if (op->stats.wc && op->is_weapon ()) |
384 | op->stats.wc += lmt->wc; |
391 | op->stats.wc += lmt->wc; |
385 | if (IS_ARMOR (op)) |
392 | if (op->is_armor ()) |
386 | { |
393 | { |
387 | if (op->stats.ac) |
394 | if (op->stats.ac) |
388 | op->stats.ac += lmt->ac; |
395 | op->stats.ac += lmt->ac; |
389 | for (j = 0; j < NROFATTACKS; j++) |
396 | for (j = 0; j < NROFATTACKS; j++) |
390 | if (op->resist[j] != 0) |
397 | if (op->resist[j] != 0) |
… | |
… | |
396 | op->resist[j] = -100; |
403 | op->resist[j] = -100; |
397 | } |
404 | } |
398 | } |
405 | } |
399 | op->materialname = add_string (lmt->name); |
406 | op->materialname = add_string (lmt->name); |
400 | /* dont make it unstackable if it doesn't need to be */ |
407 | /* dont make it unstackable if it doesn't need to be */ |
401 | if (IS_WEAPON (op) || IS_ARMOR (op)) |
408 | if (op->is_weapon () || op->is_armor ()) |
402 | { |
409 | { |
403 | op->weight = (op->weight * lmt->weight) / 100; |
410 | op->weight = (op->weight * lmt->weight) / 100; |
404 | op->value = (op->value * lmt->value) / 100; |
411 | op->value = (op->value * lmt->value) / 100; |
405 | } |
412 | } |
406 | #endif |
413 | #endif |
… | |
… | |
541 | return; |
548 | return; |
542 | } |
549 | } |
543 | |
550 | |
544 | ///////////////////////////////////////////////////////////////////////////// |
551 | ///////////////////////////////////////////////////////////////////////////// |
545 | |
552 | |
546 | #if 0 |
|
|
547 | refcounted *refcounted::rc_first; |
|
|
548 | |
|
|
549 | refcounted::refcounted () |
|
|
550 | { |
|
|
551 | refcnt = 0; |
|
|
552 | rc_next = rc_first; |
|
|
553 | rc_first = this; |
|
|
554 | } |
|
|
555 | |
|
|
556 | refcounted::~refcounted () |
|
|
557 | { |
|
|
558 | assert (!rc_next); |
|
|
559 | assert (!refcnt); |
|
|
560 | } |
|
|
561 | #endif |
|
|
562 | |
|
|
563 | void *alloc (int s) throw (std::bad_alloc) |
553 | void *salloc_ (int n) throw (std::bad_alloc) |
564 | { |
554 | { |
565 | void *p = g_slice_alloc (s); |
555 | void *ptr = g_slice_alloc (n); |
566 | |
556 | |
567 | if (!p) |
557 | if (!ptr) |
568 | throw std::bad_alloc (); |
558 | throw std::bad_alloc (); |
569 | |
559 | |
570 | return p; |
560 | return ptr; |
|
|
561 | } |
|
|
562 | |
|
|
563 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
|
|
564 | { |
|
|
565 | void *ptr = salloc_ (n); |
|
|
566 | |
|
|
567 | if (src) |
|
|
568 | memcpy (ptr, src, n); |
|
|
569 | else |
|
|
570 | memset (ptr, 0, n); |
|
|
571 | |
|
|
572 | return ptr; |
571 | } |
573 | } |
572 | |
574 | |
573 | void assign (char *dst, const char *src, int maxlen) |
575 | void assign (char *dst, const char *src, int maxlen) |
574 | { |
576 | { |
575 | if (!src) |
577 | if (!src) |
… | |
… | |
592 | } |
594 | } |
593 | else |
595 | else |
594 | memcpy (dst, src, len + 1); |
596 | memcpy (dst, src, len + 1); |
595 | } |
597 | } |
596 | |
598 | |
|
|
599 | tstamp now () |
|
|
600 | { |
|
|
601 | struct timeval tv; |
597 | |
602 | |
|
|
603 | gettimeofday (&tv, 0); |
|
|
604 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
|
|
605 | } |
|
|
606 | |
|
|
607 | int |
|
|
608 | similar_direction (int a, int b) |
|
|
609 | { |
|
|
610 | if (!a || !b) |
|
|
611 | return 0; |
|
|
612 | |
|
|
613 | int diff = (b - a) & 7; |
|
|
614 | return diff <= 1 || diff >= 7; |
|
|
615 | } |
|
|
616 | |