From 9cd1a571aeadfa4e09504b2d2f3ea43b1915f0c2 Mon Sep 17 00:00:00 2001 From: PatR Date: Thu, 25 Jan 2024 23:09:12 -0800 Subject: [PATCH] more weight tweaks Update some potential weight issues. Eggs won't hatch when in containers so they weren't affected but add some bulletproofing. Corpse revival from inside containers was already ok too, so effectively there's no change except for making container_weight() be global instead of local to mkobj.c. --- include/extern.h | 1 + src/mkobj.c | 18 +++++++----------- src/timeout.c | 16 +++++++++++----- src/zap.c | 1 + 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/include/extern.h b/include/extern.h index d0fb3b5de..f6a2d8982 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1639,6 +1639,7 @@ extern int add_to_minv(struct monst *, struct obj *) NONNULLARG12; extern struct obj *add_to_container(struct obj *, struct obj *) NONNULLARG12; extern void add_to_migration(struct obj *) NONNULLARG1; extern void add_to_buried(struct obj *) NONNULLARG1; +extern void container_weight(struct obj *) NONNULLARG1; extern void dealloc_obj(struct obj *) NONNULLARG1; extern void obj_ice_effects(coordxy, coordxy, boolean); extern long peek_at_iced_corpse_age(struct obj *) NONNULLARG1; diff --git a/src/mkobj.c b/src/mkobj.c index 14234d16e..8dd201c9d 100644 --- a/src/mkobj.c +++ b/src/mkobj.c @@ -13,7 +13,6 @@ static void mksobj_init(struct obj *, boolean); static int item_on_ice(struct obj *); static void shrinking_glob_gone(struct obj *); static void obj_timer_checks(struct obj *, coordxy, coordxy, int); -static void container_weight(struct obj *) NONNULLARG1; static struct obj *save_mtraits(struct obj *, struct monst *); static void objlist_sanity(struct obj *, int, const char *); static void shop_obj_sanity(struct obj *, const char *); @@ -2645,17 +2644,14 @@ add_to_buried(struct obj *obj) gl.level.buriedobjlist = obj; } -/* Recalculate the weight of this container and all of _its_ containers. */ -static void -container_weight(struct obj *container) +/* recalculate weight of object, which doesn't have to be a container + itself; if it is contained, recursively handle _its_ container(s) */ +void +container_weight(struct obj *object) { - container->owt = weight(container); - if (container->where == OBJ_CONTAINED) - container_weight(container->ocontainer); - /* - else if (container->where == OBJ_INVENT) - recalculate load delay here ??? - */ + object->owt = weight(object); + if (object->where == OBJ_CONTAINED) + container_weight(object->ocontainer); } /* diff --git a/src/timeout.c b/src/timeout.c index e6f3e0569..eb9de83e8 100644 --- a/src/timeout.c +++ b/src/timeout.c @@ -986,7 +986,9 @@ hatch_egg(anything *arg, long timeout) yours = (egg->spe || (!flags.female && carried(egg) && !rn2(2))); silent = (timeout != gm.moves); /* hatched while away */ - /* only can hatch when in INVENT, FLOOR, MINVENT */ + /* only can hatch when in INVENT, FLOOR, MINVENT; + get_obj_location() will fail for MIGRATING, also for CONTAINED + and BURIED when the flags for those aren't included in the call */ if (get_obj_location(egg, &x, &y, 0)) { hatchcount = rnd((int) egg->quan); cansee_hatchspot = cansee(x, y) && !silent; @@ -995,7 +997,7 @@ hatch_egg(anything *arg, long timeout) for (i = hatchcount; i > 0; i--) { if (!enexto(&cc, x, y, &mons[mnum]) || !(mon = makemon(&mons[mnum], cc.x, cc.y, - NO_MINVENT|MM_NOMSG))) + NO_MINVENT | MM_NOMSG))) break; /* tame if your own egg hatches while you're on the same dungeon level, or any dragon egg which hatches @@ -1115,10 +1117,14 @@ hatch_egg(anything *arg, long timeout) learn_egg_type(mnum); if (egg->quan > 0) { - /* still some eggs left */ - /* Instead of ordinary egg timeout use a short one */ + /* still some eggs left; we didn't split the stack, just + subtracted from quantity so weight needs to be updated; + for remainder of stack, add a new, short hatch timer */ attach_egg_hatch_timeout(egg, (long) rnd(12)); - egg->owt = weight(egg); + /* container_weight(arg) updates arg->owt, and if contained, + its enclosing container arg->ocontainer (recursively) + [egg won't be contained due to conditions imposed above] */ + container_weight(egg); } else if (carried(egg)) { useup(egg); } else { diff --git a/src/zap.c b/src/zap.c index 5184cfd2c..73c6f60b7 100644 --- a/src/zap.c +++ b/src/zap.c @@ -1069,6 +1069,7 @@ revive(struct obj *corpse, boolean by_hero) m_useup(corpse->ocarry, corpse); break; case OBJ_CONTAINED: + /* obj_extract_self() will update corpse->ocontainer->owt */ obj_extract_self(corpse); obfree(corpse, (struct obj *) 0); break;