From 21caf64b8606dab9d893fb8d6200a4183e59f3d8 Mon Sep 17 00:00:00 2001 From: "nethack.rankin" Date: Sat, 6 Jan 2007 04:53:56 +0000 Subject: [PATCH] !GOLDOBJ gold in inventory (trunk only) Simplify the insertion into and removal from of gold in inventory for the !GOLDOBJ configuration. If GOLDOBJ ever becomes unconditional this will be superfluous, but in the mean time it unclutters the container and drop code. Also, tweak a recent getobj() hack so that its purpose might be a bit clearer. --- include/extern.h | 6 +++++- src/do.c | 24 ++++----------------- src/invent.c | 54 +++++++++++++++++++++++++++++++++++++++++------- src/pickup.c | 39 ++++++++++++---------------------- 4 files changed, 68 insertions(+), 55 deletions(-) diff --git a/include/extern.h b/include/extern.h index 3c8a8ec40..f9749eeaa 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1,4 +1,4 @@ -/* SCCS Id: @(#)extern.h 3.5 2006/07/08 */ +/* SCCS Id: @(#)extern.h 3.5 2007/01/05 */ /* Copyright (c) Steve Creps, 1988. */ /* NetHack may be freely redistributed. See license for details. */ @@ -878,7 +878,11 @@ E boolean FDECL(obj_here, (struct obj *,int,int)); E boolean NDECL(wearing_armor); E boolean FDECL(is_worn, (struct obj *)); E struct obj *FDECL(g_at, (int,int)); +#ifndef GOLDOBJ E struct obj *FDECL(mkgoldobj, (long)); +E struct obj *NDECL(insert_gold_into_invent); +E void NDECL(remove_gold_from_invent); +#endif E struct obj *FDECL(getobj, (const char *,const char *)); E int FDECL(ggetobj, (const char *,int (*)(OBJ_P),int,BOOLEAN_P,unsigned *)); E void FDECL(fully_identify_obj, (struct obj *)); diff --git a/src/do.c b/src/do.c index ee75ca29f..83800be53 100644 --- a/src/do.c +++ b/src/do.c @@ -648,17 +648,8 @@ int retry; boolean drop_everything = FALSE; #ifndef GOLDOBJ - if (u.ugold) { - /* Hack: gold is not in the inventory, so make a gold object - and put it at the head of the inventory list. */ - u_gold = mkgoldobj(u.ugold); /* removes from u.ugold */ - u_gold->in_use = TRUE; - u.ugold = u_gold->quan; /* put the gold back */ - assigninvlet(u_gold); /* might end up as NOINVSYM */ - u_gold->nobj = invent; - invent = u_gold; - u_gold->where = OBJ_INVENT; - } + /* put gold where inventory traversal will see it */ + if (u.ugold) u_gold = insert_gold_into_invent(); #endif if (retry) { all_categories = (retry == -2); @@ -728,15 +719,8 @@ int retry; drop_done: #ifndef GOLDOBJ - if (u_gold && invent && invent->oclass == COIN_CLASS) { - /* didn't drop [all of] it */ - u_gold = invent; - invent = u_gold->nobj; - u_gold->in_use = FALSE; - u_gold->where = OBJ_FREE; - dealloc_obj(u_gold); - update_inventory(); - } + /* if we put gold into inventory above, take it back out now */ + if (u_gold) remove_gold_from_invent(); #endif return n_dropped; } diff --git a/src/invent.c b/src/invent.c index 5d32f5df9..9218b9495 100644 --- a/src/invent.c +++ b/src/invent.c @@ -694,7 +694,44 @@ register long q; context.botl = 1; return(otmp); } + +/* used for container apply/#loot and multi-item Drop */ +struct obj * +insert_gold_into_invent() +{ + struct obj *u_gold = 0; + + if (u.ugold) { + u_gold = mkgoldobj(u.ugold); + u_gold->in_use = 1; + u.ugold = u_gold->quan; /* put the gold back on status line */ + assigninvlet(u_gold); /* should yield '$' */ + u_gold->where = OBJ_INVENT; + u_gold->nobj = invent; + invent = u_gold; + } + return u_gold; +} + +/* undo insert_gold_into_invent; gold might have been used up though */ +void +remove_gold_from_invent() +{ + struct obj *u_gold = invent; /* we expect gold to be first */ + + if (u_gold && u_gold->otyp == GOLD_PIECE) { + invent = u_gold->nobj; + u_gold->where = OBJ_FREE; + dealloc_obj(u_gold); +#if 0 + } else if ((u_gold = carrying(GOLD_PIECE)) != 0) { + u.ugold += u_gold->quan; /* freeinv will subtract it back out */ + freeinv(u_gold); + dealloc_obj(u_gold); #endif + } +} +#endif /* !GOLDOBJ */ STATIC_OVL void compactify(buf) @@ -799,13 +836,6 @@ register const char *let,*word; #ifndef GOLDOBJ if(*let == COIN_CLASS) let++, usegold = TRUE, allowgold = (u.ugold ? TRUE : FALSE); - if (firstobj && firstobj->oclass == COIN_CLASS) { - /* gold has been inserted into inventory; skip it during - inventory letter collection */ - u_gold = firstobj; - firstobj = u_gold->nobj; - allowgold = usegold; - } #else if(*let == COIN_CLASS) let++, usegold = TRUE; #endif @@ -837,7 +867,15 @@ register const char *let,*word; if(allownone) *bp++ = '-'; #ifndef GOLDOBJ - if(allowgold) *bp++ = def_oc_syms[COIN_CLASS].sym; + if(allowgold) { + *bp++ = def_oc_syms[COIN_CLASS].sym; + if (firstobj && firstobj->otyp == GOLD_PIECE) { + /* gold has been inserted into inventory; skip it during + inventory letter collection */ + u_gold = firstobj; + firstobj = u_gold->nobj; + } + } #endif if(bp > buf && bp[-1] == '-') *bp++ = ' '; ap = altlets; diff --git a/src/pickup.c b/src/pickup.c index 1f7387a09..4de12e827 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -2258,19 +2258,9 @@ int held; } #ifndef GOLDOBJ - if ((loot_in ||stash_one) && u.ugold) { - /* - * Hack: gold is not in the inventory, so make a gold object - * and put it at the head of the inventory list. - */ - u_gold = mkgoldobj(u.ugold); /* removes from u.ugold */ - u_gold->in_use = TRUE; - u.ugold = u_gold->quan; /* put the gold back */ - assigninvlet(u_gold); /* might end up as NOINVSYM */ - u_gold->nobj = invent; - invent = u_gold; - u_gold->where = OBJ_INVENT; - } + /* if putting in, place gold where inventory traversal will see it */ + if ((loot_in || stash_one) && u.ugold) + u_gold = insert_gold_into_invent(); #endif if ((loot_in || stash_one) && (!invent || (invent == current_container && !invent->nobj))) { @@ -2308,14 +2298,8 @@ int held; if (!current_container) loot_out = FALSE; #ifndef GOLDOBJ - if (u_gold && invent && invent->oclass == COIN_CLASS) { - /* didn't stash [all of] it */ - u_gold = invent; - invent = u_gold->nobj; - u_gold->in_use = FALSE; - u_gold->where = OBJ_FREE; - dealloc_obj(u_gold); - } + /* if we put gold into inventory above, take it back out now */ + if (u_gold) remove_gold_from_invent(); #endif /* out after in */ @@ -2334,11 +2318,14 @@ int held; } containerdone: - /* Not completely correct; if we put something in without knowing - whatever was already inside, now we suddenly do. That can't be - helped unless we want to track things item by item and then deal - with containers whose contents are "partly known". */ - if (used && current_container) current_container->cknown = 1; + if (used) { + /* Not completely correct; if we put something in without knowing + whatever was already inside, now we suddenly do. That can't + be helped unless we want to track things item by item and then + deal with containers whose contents are "partly known". */ + if (current_container) current_container->cknown = 1; + update_inventory(); + } *objp = current_container; /* might have become null */ current_container = 0; /* avoid hanging on to stale pointer */