use gi.invalid_obj instead of cg.zeroobj

cg.zeroobj was originally added (under its previous unprefixed name)
for providing a one-line way to zero out the fields of a struct obj.

    struct obj tempobj;
    tempobj = cg.zeroobj;

    initfn(struct obj *otmp)
    {
        if (otmp)
            *otmp = cg.zeroobj;
    }

More recently, the address of cg.zeroobj began to be used as a return
flag to indicate some things, but the 'const struct obj zeroobj' wasn't
an ideal fit for the purpose and required a number of casts, including
casting away const.

Provide a better fitting variable (gi.invalid_obj) and eliminate a
number of casts.
This commit is contained in:
nhmall
2023-11-10 11:07:49 -05:00
parent 17dd51d922
commit 314a2a9489
14 changed files with 35 additions and 30 deletions

View File

@@ -2564,7 +2564,7 @@ use_grease(struct obj *obj)
consume_obj_charge(obj, TRUE);
oldglib = (int) (Glib & TIMEOUT);
if (otmp != &cg.zeroobj) {
if (otmp != &gi.invalid_obj) {
You("cover %s with a thick layer of grease.", yname(otmp));
otmp->greased = 1;
if (obj->cursed && !nohands(gy.youmonst.data)) {

View File

@@ -440,6 +440,8 @@ const struct instance_globals_i g_init_i = {
UNDEFINED_PTR, /* id_map */
/* sp_lev.c */
FALSE, /* in_mk_themerooms */
DUMMY, /* invalid_obj */
TRUE, /* havestate*/
IVMAGIC /* i_magic to validate that structure layout has been preserved */
};

View File

@@ -2720,7 +2720,7 @@ doeat(void)
}
/* from floorfood(), &zeroobj means iron bars at current spot */
if (otmp == &cg.zeroobj) {
if (otmp == &gi.invalid_obj) {
/* hero in metallivore form is eating [diggable] iron bars
at current location so skip the other assorted checks;
operates as if digging rather than via the eat occupation */
@@ -3507,7 +3507,7 @@ floorfood(
c = yn_function(qbuf, ynqchars, 'n', TRUE);
}
if (c == 'y')
return (struct obj *) &cg.zeroobj; /* csst away 'const' */
return (struct obj *) &gi.invalid_obj; /* csst away 'const' */
else if (c == 'q')
return (struct obj *) 0;
++getobj_else;

View File

@@ -532,7 +532,7 @@ doengrave(void)
if (!otmp) /* otmp == cg.zeroobj if fingers */
return ECMD_CANCEL;
if (otmp == &cg.zeroobj) {
if (otmp == &gi.invalid_obj) {
Strcat(strcpy(fbuf, "your "), body_part(FINGERTIP));
writer = fbuf;
} else {
@@ -563,7 +563,7 @@ doengrave(void)
return ECMD_OK;
}
if (IS_GRAVE(levl[u.ux][u.uy].typ)) {
if (otmp == &cg.zeroobj) { /* using only finger */
if (otmp == &gi.invalid_obj) { /* using only finger */
You("would only make a small smudge on the %s.",
surface(u.ux, u.uy));
return ECMD_OK;
@@ -1024,7 +1024,7 @@ doengrave(void)
}
/* Tell adventurer what is going on */
if (otmp != &cg.zeroobj)
if (otmp != &gi.invalid_obj)
You("%s the %s with %s.", everb, eloc, doname(otmp));
else
You("%s the %s with your %s.", everb, eloc, body_part(FINGERTIP));
@@ -1131,7 +1131,7 @@ engrave(void)
}
/* Stylus might have been taken out of inventory and destroyed somehow.
* Not safe to dereference stylus until after this. */
if (gc.context.engraving.stylus == &cg.zeroobj) { /* bare finger */
if (gc.context.engraving.stylus == &gi.invalid_obj) { /* bare finger */
stylus = (struct obj *) 0;
} else {
for (stylus = gi.invent; stylus; stylus = stylus->nobj) {

View File

@@ -3880,7 +3880,7 @@ fopen_wizkit_file(void)
static void
wizkit_addinv(struct obj *obj)
{
if (!obj || obj == &cg.zeroobj)
if (!obj || obj == &gi.invalid_obj)
return;
/* subset of starting inventory pre-ID */
@@ -3912,7 +3912,7 @@ proc_wizkit_line(char *buf)
otmp = readobjnam(buf, (struct obj *) 0);
if (otmp) {
if (otmp != &cg.zeroobj)
if (otmp != &gi.invalid_obj)
wizkit_addinv(otmp);
} else {
/* .60 limits output line width to 79 chars */

View File

@@ -391,7 +391,7 @@ void
dipfountain(struct obj *obj)
{
int er = ER_NOTHING;
boolean is_hands = (obj == &cg.zeroobj);
boolean is_hands = (obj == &gi.invalid_obj);
if (Levitation) {
floating_above("fountain");
@@ -713,7 +713,7 @@ dipsink(struct obj *obj)
if (Glib && (!obj || obj == uarmg))
Your("%s are still slippery.", fingers_or_gloves(TRUE));
return;
} else if (obj == &cg.zeroobj || obj == uarmg) {
} else if (obj == &gi.invalid_obj || obj == uarmg) {
(void) wash_hands();
return;
} else if (obj->oclass != POTION_CLASS) {

View File

@@ -1527,13 +1527,13 @@ any_obj_ok(struct obj *obj)
* getobj returns:
* struct obj *xxx: object to do something with.
* (struct obj *) 0 error return: no object.
* &cg.zeroobj explicitly no object (as in w-).
* &gi.invalid_obj explicitly no object (as in w-).
* The obj_ok callback should not have side effects (apart from
* abnormal-behavior things like impossible calls); it can be called multiple
* times on the same object during the execution of this function.
* Callbacks' argument is either a valid object pointer or a null pointer,
* which represents the validity of doing that action on HANDS_SYM. getobj
* won't call it with &cg.zeroobj, so its behavior can be undefined in that
* won't call it with &gi.invalid_obj, so its behavior can be undefined in that
* case.
*/
struct obj *
@@ -1579,7 +1579,7 @@ getobj(
/* check whether the hands/self choice is suitable */
v = (*obj_ok)((struct obj *) 0);
if (v == GETOBJ_SUGGEST || v == GETOBJ_DOWNPLAY)
otmp = (struct obj *) &cg.zeroobj;
otmp = &gi.invalid_obj;
} else {
/* there could be more than one match if key is '#';
take first one which passes the obj_ok callback */
@@ -1742,7 +1742,7 @@ getobj(
if (ilet == HANDS_SYM) { /* '-' */
if (!allownone)
mime_action(word);
return (allownone ? (struct obj *) &cg.zeroobj : (struct obj *) 0);
return (allownone ? &gi.invalid_obj : (struct obj *) 0);
}
redo_menu:
/* since gold is now kept in inventory, we need to do processing for
@@ -1778,7 +1778,7 @@ getobj(
continue;
}
if (ilet == HANDS_SYM)
return (struct obj *) &cg.zeroobj; /* cast away 'const' */
return &gi.invalid_obj;
if (ilet == '\033') {
if (flags.verbose)
pline1(Never_mind);

View File

@@ -2886,7 +2886,7 @@ munslime(struct monst *mon, boolean by_you)
}
}
if (t && t->ttyp == FIRE_TRAP)
return muse_unslime(mon, (struct obj *) &cg.zeroobj, t, by_you);
return muse_unslime(mon, &gi.invalid_obj, t, by_you);
} /* MUSE */

View File

@@ -3266,7 +3266,7 @@ wizterrainwish(struct _readobjnam_data *d)
(trap != MAGIC_PORTAL) ? "" : " to nowhere");
} else
pline("Creation of %s failed.", an(tname));
return (struct obj *) &cg.zeroobj;
return &gi.invalid_obj;
}
/* furniture and terrain (use at your own risk; can clobber stairs
@@ -3540,7 +3540,7 @@ wizterrainwish(struct _readobjnam_data *d)
}
if (madeterrain || badterrain) {
/* cast 'const' away; caller won't modify this */
return (struct obj *) &cg.zeroobj;
return &gi.invalid_obj;
}
return (struct obj *) 0;
@@ -4495,7 +4495,7 @@ readobjnam_postparse3(struct _readobjnam_data *d)
* Return something wished for. Specifying a null pointer for
* the user request string results in a random object. Otherwise,
* if asking explicitly for "nothing" (or "nil") return no_wish;
* if not an object return &cg.zeroobj; if an error (no matching object),
* if not an object return &gi.invalid_obj; if an error (no matching object),
* return null.
*/
struct obj *
@@ -4960,7 +4960,7 @@ readobjnam(char *bp, struct obj *no_wish)
|| (d.otmp->oartifact && rn2(nartifact_exist()) > 1)) && !wizard) {
artifact_exists(d.otmp, safe_oname(d.otmp), FALSE, ONAME_NO_FLAGS);
obfree(d.otmp, (struct obj *) 0);
d.otmp = (struct obj *) &cg.zeroobj;
d.otmp = (struct obj *) &gi.invalid_obj;
pline("For a moment, you feel %s in your %s, but it disappears!",
something, makeplural(body_part(HAND)));
return d.otmp;

View File

@@ -2246,7 +2246,7 @@ dodip(void)
if (inaccessible_equipment(obj, "dip", FALSE))
return ECMD_OK;
is_hands = (obj == &cg.zeroobj);
is_hands = (obj == &gi.invalid_obj);
shortestname = (is_hands || is_plural(obj) || pair_of(obj)) ? "them"
: "it";
drink_ok_extra = 0;
@@ -2381,7 +2381,7 @@ potion_dip(struct obj *obj, struct obj *potion)
pline("That is a potion bottle, not a Klein bottle!");
return ECMD_OK;
}
if (obj == &cg.zeroobj) {
if (obj == &gi.invalid_obj) {
You("can't fit your %s into the mouth of the bottle!",
body_part(HAND));
return ECMD_OK;

View File

@@ -509,7 +509,7 @@ select_rwep(struct monst *mtmp)
char mlet = mtmp->data->mlet;
gp.propellor = (struct obj *) &cg.zeroobj;
gp.propellor = &gi.invalid_obj;
Oselect(EGG); /* cockatrice egg */
if (mlet == S_KOP) /* pies are first choice for Kops */
Oselect(CREAM_PIE);
@@ -568,7 +568,7 @@ select_rwep(struct monst *mtmp)
}
/* KMH -- This belongs here so darts will work */
gp.propellor = (struct obj *) &cg.zeroobj;
gp.propellor = &gi.invalid_obj;
prop = objects[rwep[i]].oc_skill;
if (prop < 0) {
@@ -593,7 +593,7 @@ select_rwep(struct monst *mtmp)
gp.propellor = 0;
}
/* propellor = obj, propellor to use
* propellor = &cg.zeroobj, doesn't need a propellor
* propellor = &gi.invalid_obj, doesn't need a propellor
* propellor = 0, needed one and didn't have one
*/
if (gp.propellor != 0) {
@@ -788,7 +788,7 @@ mon_wield_item(struct monst *mon)
mon_nam(mon));
return 0;
}
if (obj && obj != &cg.zeroobj) {
if (obj && obj != &gi.invalid_obj) {
struct obj *mw_tmp = MON_WEP(mon);
if (mw_tmp && mw_tmp->otyp == obj->otyp) {

View File

@@ -384,7 +384,7 @@ dowield(void)
}
/* Handle no object, or object in other slot */
if (wep == &cg.zeroobj) {
if (wep == &gi.invalid_obj) {
wep = (struct obj *) 0;
} else if (wep == uswapwep) {
return doswapweapon();
@@ -516,7 +516,7 @@ doquiver_core(const char *verb) /* "ready" or "fire" */
if (!newquiver) {
/* Cancelled */
return ECMD_CANCEL;
} else if (newquiver == &cg.zeroobj) { /* no object */
} else if (newquiver == &gi.invalid_obj) { /* no object */
/* Explicitly nothing */
if (uquiver) {
You("now have no ammunition readied.");

View File

@@ -5999,7 +5999,7 @@ makewish(void)
to retain wishless conduct */
livelog_printf(LL_WISH, "declined to make a wish");
return;
} else if (otmp == &cg.zeroobj) {
} else if (otmp == &gi.invalid_obj) {
/* wizard mode terrain wish: skip livelogging, etc */
return;
}