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
+3
View File
@@ -467,6 +467,9 @@ struct instance_globals_i {
/* sp_lev.c */ /* sp_lev.c */
boolean in_mk_themerooms; boolean in_mk_themerooms;
/* new */
struct obj invalid_obj;
boolean havestate; boolean havestate;
unsigned long magic; /* validate that structure layout is preserved */ unsigned long magic; /* validate that structure layout is preserved */
}; };
+1 -1
View File
@@ -2564,7 +2564,7 @@ use_grease(struct obj *obj)
consume_obj_charge(obj, TRUE); consume_obj_charge(obj, TRUE);
oldglib = (int) (Glib & TIMEOUT); oldglib = (int) (Glib & TIMEOUT);
if (otmp != &cg.zeroobj) { if (otmp != &gi.invalid_obj) {
You("cover %s with a thick layer of grease.", yname(otmp)); You("cover %s with a thick layer of grease.", yname(otmp));
otmp->greased = 1; otmp->greased = 1;
if (obj->cursed && !nohands(gy.youmonst.data)) { if (obj->cursed && !nohands(gy.youmonst.data)) {
+2
View File
@@ -440,6 +440,8 @@ const struct instance_globals_i g_init_i = {
UNDEFINED_PTR, /* id_map */ UNDEFINED_PTR, /* id_map */
/* sp_lev.c */ /* sp_lev.c */
FALSE, /* in_mk_themerooms */ FALSE, /* in_mk_themerooms */
DUMMY, /* invalid_obj */
TRUE, /* havestate*/ TRUE, /* havestate*/
IVMAGIC /* i_magic to validate that structure layout has been preserved */ IVMAGIC /* i_magic to validate that structure layout has been preserved */
}; };
+2 -2
View File
@@ -2720,7 +2720,7 @@ doeat(void)
} }
/* from floorfood(), &zeroobj means iron bars at current spot */ /* 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 /* hero in metallivore form is eating [diggable] iron bars
at current location so skip the other assorted checks; at current location so skip the other assorted checks;
operates as if digging rather than via the eat occupation */ operates as if digging rather than via the eat occupation */
@@ -3507,7 +3507,7 @@ floorfood(
c = yn_function(qbuf, ynqchars, 'n', TRUE); c = yn_function(qbuf, ynqchars, 'n', TRUE);
} }
if (c == 'y') if (c == 'y')
return (struct obj *) &cg.zeroobj; /* csst away 'const' */ return (struct obj *) &gi.invalid_obj; /* csst away 'const' */
else if (c == 'q') else if (c == 'q')
return (struct obj *) 0; return (struct obj *) 0;
++getobj_else; ++getobj_else;
+4 -4
View File
@@ -532,7 +532,7 @@ doengrave(void)
if (!otmp) /* otmp == cg.zeroobj if fingers */ if (!otmp) /* otmp == cg.zeroobj if fingers */
return ECMD_CANCEL; return ECMD_CANCEL;
if (otmp == &cg.zeroobj) { if (otmp == &gi.invalid_obj) {
Strcat(strcpy(fbuf, "your "), body_part(FINGERTIP)); Strcat(strcpy(fbuf, "your "), body_part(FINGERTIP));
writer = fbuf; writer = fbuf;
} else { } else {
@@ -563,7 +563,7 @@ doengrave(void)
return ECMD_OK; return ECMD_OK;
} }
if (IS_GRAVE(levl[u.ux][u.uy].typ)) { 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.", You("would only make a small smudge on the %s.",
surface(u.ux, u.uy)); surface(u.ux, u.uy));
return ECMD_OK; return ECMD_OK;
@@ -1024,7 +1024,7 @@ doengrave(void)
} }
/* Tell adventurer what is going on */ /* 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)); You("%s the %s with %s.", everb, eloc, doname(otmp));
else else
You("%s the %s with your %s.", everb, eloc, body_part(FINGERTIP)); 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. /* Stylus might have been taken out of inventory and destroyed somehow.
* Not safe to dereference stylus until after this. */ * 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; stylus = (struct obj *) 0;
} else { } else {
for (stylus = gi.invent; stylus; stylus = stylus->nobj) { for (stylus = gi.invent; stylus; stylus = stylus->nobj) {
+2 -2
View File
@@ -3880,7 +3880,7 @@ fopen_wizkit_file(void)
static void static void
wizkit_addinv(struct obj *obj) wizkit_addinv(struct obj *obj)
{ {
if (!obj || obj == &cg.zeroobj) if (!obj || obj == &gi.invalid_obj)
return; return;
/* subset of starting inventory pre-ID */ /* subset of starting inventory pre-ID */
@@ -3912,7 +3912,7 @@ proc_wizkit_line(char *buf)
otmp = readobjnam(buf, (struct obj *) 0); otmp = readobjnam(buf, (struct obj *) 0);
if (otmp) { if (otmp) {
if (otmp != &cg.zeroobj) if (otmp != &gi.invalid_obj)
wizkit_addinv(otmp); wizkit_addinv(otmp);
} else { } else {
/* .60 limits output line width to 79 chars */ /* .60 limits output line width to 79 chars */
+2 -2
View File
@@ -391,7 +391,7 @@ void
dipfountain(struct obj *obj) dipfountain(struct obj *obj)
{ {
int er = ER_NOTHING; int er = ER_NOTHING;
boolean is_hands = (obj == &cg.zeroobj); boolean is_hands = (obj == &gi.invalid_obj);
if (Levitation) { if (Levitation) {
floating_above("fountain"); floating_above("fountain");
@@ -713,7 +713,7 @@ dipsink(struct obj *obj)
if (Glib && (!obj || obj == uarmg)) if (Glib && (!obj || obj == uarmg))
Your("%s are still slippery.", fingers_or_gloves(TRUE)); Your("%s are still slippery.", fingers_or_gloves(TRUE));
return; return;
} else if (obj == &cg.zeroobj || obj == uarmg) { } else if (obj == &gi.invalid_obj || obj == uarmg) {
(void) wash_hands(); (void) wash_hands();
return; return;
} else if (obj->oclass != POTION_CLASS) { } else if (obj->oclass != POTION_CLASS) {
+5 -5
View File
@@ -1527,13 +1527,13 @@ any_obj_ok(struct obj *obj)
* getobj returns: * getobj returns:
* struct obj *xxx: object to do something with. * struct obj *xxx: object to do something with.
* (struct obj *) 0 error return: no object. * (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 * The obj_ok callback should not have side effects (apart from
* abnormal-behavior things like impossible calls); it can be called multiple * abnormal-behavior things like impossible calls); it can be called multiple
* times on the same object during the execution of this function. * times on the same object during the execution of this function.
* Callbacks' argument is either a valid object pointer or a null pointer, * Callbacks' argument is either a valid object pointer or a null pointer,
* which represents the validity of doing that action on HANDS_SYM. getobj * 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. * case.
*/ */
struct obj * struct obj *
@@ -1579,7 +1579,7 @@ getobj(
/* check whether the hands/self choice is suitable */ /* check whether the hands/self choice is suitable */
v = (*obj_ok)((struct obj *) 0); v = (*obj_ok)((struct obj *) 0);
if (v == GETOBJ_SUGGEST || v == GETOBJ_DOWNPLAY) if (v == GETOBJ_SUGGEST || v == GETOBJ_DOWNPLAY)
otmp = (struct obj *) &cg.zeroobj; otmp = &gi.invalid_obj;
} else { } else {
/* there could be more than one match if key is '#'; /* there could be more than one match if key is '#';
take first one which passes the obj_ok callback */ take first one which passes the obj_ok callback */
@@ -1742,7 +1742,7 @@ getobj(
if (ilet == HANDS_SYM) { /* '-' */ if (ilet == HANDS_SYM) { /* '-' */
if (!allownone) if (!allownone)
mime_action(word); mime_action(word);
return (allownone ? (struct obj *) &cg.zeroobj : (struct obj *) 0); return (allownone ? &gi.invalid_obj : (struct obj *) 0);
} }
redo_menu: redo_menu:
/* since gold is now kept in inventory, we need to do processing for /* since gold is now kept in inventory, we need to do processing for
@@ -1778,7 +1778,7 @@ getobj(
continue; continue;
} }
if (ilet == HANDS_SYM) if (ilet == HANDS_SYM)
return (struct obj *) &cg.zeroobj; /* cast away 'const' */ return &gi.invalid_obj;
if (ilet == '\033') { if (ilet == '\033') {
if (flags.verbose) if (flags.verbose)
pline1(Never_mind); pline1(Never_mind);
+1 -1
View File
@@ -2886,7 +2886,7 @@ munslime(struct monst *mon, boolean by_you)
} }
} }
if (t && t->ttyp == FIRE_TRAP) 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 */ } /* MUSE */
+4 -4
View File
@@ -3266,7 +3266,7 @@ wizterrainwish(struct _readobjnam_data *d)
(trap != MAGIC_PORTAL) ? "" : " to nowhere"); (trap != MAGIC_PORTAL) ? "" : " to nowhere");
} else } else
pline("Creation of %s failed.", an(tname)); 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 /* furniture and terrain (use at your own risk; can clobber stairs
@@ -3540,7 +3540,7 @@ wizterrainwish(struct _readobjnam_data *d)
} }
if (madeterrain || badterrain) { if (madeterrain || badterrain) {
/* cast 'const' away; caller won't modify this */ /* cast 'const' away; caller won't modify this */
return (struct obj *) &cg.zeroobj; return &gi.invalid_obj;
} }
return (struct obj *) 0; return (struct obj *) 0;
@@ -4495,7 +4495,7 @@ readobjnam_postparse3(struct _readobjnam_data *d)
* Return something wished for. Specifying a null pointer for * Return something wished for. Specifying a null pointer for
* the user request string results in a random object. Otherwise, * the user request string results in a random object. Otherwise,
* if asking explicitly for "nothing" (or "nil") return no_wish; * 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. * return null.
*/ */
struct obj * struct obj *
@@ -4960,7 +4960,7 @@ readobjnam(char *bp, struct obj *no_wish)
|| (d.otmp->oartifact && rn2(nartifact_exist()) > 1)) && !wizard) { || (d.otmp->oartifact && rn2(nartifact_exist()) > 1)) && !wizard) {
artifact_exists(d.otmp, safe_oname(d.otmp), FALSE, ONAME_NO_FLAGS); artifact_exists(d.otmp, safe_oname(d.otmp), FALSE, ONAME_NO_FLAGS);
obfree(d.otmp, (struct obj *) 0); 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!", pline("For a moment, you feel %s in your %s, but it disappears!",
something, makeplural(body_part(HAND))); something, makeplural(body_part(HAND)));
return d.otmp; return d.otmp;
+2 -2
View File
@@ -2246,7 +2246,7 @@ dodip(void)
if (inaccessible_equipment(obj, "dip", FALSE)) if (inaccessible_equipment(obj, "dip", FALSE))
return ECMD_OK; return ECMD_OK;
is_hands = (obj == &cg.zeroobj); is_hands = (obj == &gi.invalid_obj);
shortestname = (is_hands || is_plural(obj) || pair_of(obj)) ? "them" shortestname = (is_hands || is_plural(obj) || pair_of(obj)) ? "them"
: "it"; : "it";
drink_ok_extra = 0; 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!"); pline("That is a potion bottle, not a Klein bottle!");
return ECMD_OK; return ECMD_OK;
} }
if (obj == &cg.zeroobj) { if (obj == &gi.invalid_obj) {
You("can't fit your %s into the mouth of the bottle!", You("can't fit your %s into the mouth of the bottle!",
body_part(HAND)); body_part(HAND));
return ECMD_OK; return ECMD_OK;
+4 -4
View File
@@ -509,7 +509,7 @@ select_rwep(struct monst *mtmp)
char mlet = mtmp->data->mlet; char mlet = mtmp->data->mlet;
gp.propellor = (struct obj *) &cg.zeroobj; gp.propellor = &gi.invalid_obj;
Oselect(EGG); /* cockatrice egg */ Oselect(EGG); /* cockatrice egg */
if (mlet == S_KOP) /* pies are first choice for Kops */ if (mlet == S_KOP) /* pies are first choice for Kops */
Oselect(CREAM_PIE); Oselect(CREAM_PIE);
@@ -568,7 +568,7 @@ select_rwep(struct monst *mtmp)
} }
/* KMH -- This belongs here so darts will work */ /* 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; prop = objects[rwep[i]].oc_skill;
if (prop < 0) { if (prop < 0) {
@@ -593,7 +593,7 @@ select_rwep(struct monst *mtmp)
gp.propellor = 0; gp.propellor = 0;
} }
/* propellor = obj, propellor to use /* 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 * propellor = 0, needed one and didn't have one
*/ */
if (gp.propellor != 0) { if (gp.propellor != 0) {
@@ -788,7 +788,7 @@ mon_wield_item(struct monst *mon)
mon_nam(mon)); mon_nam(mon));
return 0; return 0;
} }
if (obj && obj != &cg.zeroobj) { if (obj && obj != &gi.invalid_obj) {
struct obj *mw_tmp = MON_WEP(mon); struct obj *mw_tmp = MON_WEP(mon);
if (mw_tmp && mw_tmp->otyp == obj->otyp) { if (mw_tmp && mw_tmp->otyp == obj->otyp) {
+2 -2
View File
@@ -384,7 +384,7 @@ dowield(void)
} }
/* Handle no object, or object in other slot */ /* Handle no object, or object in other slot */
if (wep == &cg.zeroobj) { if (wep == &gi.invalid_obj) {
wep = (struct obj *) 0; wep = (struct obj *) 0;
} else if (wep == uswapwep) { } else if (wep == uswapwep) {
return doswapweapon(); return doswapweapon();
@@ -516,7 +516,7 @@ doquiver_core(const char *verb) /* "ready" or "fire" */
if (!newquiver) { if (!newquiver) {
/* Cancelled */ /* Cancelled */
return ECMD_CANCEL; return ECMD_CANCEL;
} else if (newquiver == &cg.zeroobj) { /* no object */ } else if (newquiver == &gi.invalid_obj) { /* no object */
/* Explicitly nothing */ /* Explicitly nothing */
if (uquiver) { if (uquiver) {
You("now have no ammunition readied."); You("now have no ammunition readied.");
+1 -1
View File
@@ -5999,7 +5999,7 @@ makewish(void)
to retain wishless conduct */ to retain wishless conduct */
livelog_printf(LL_WISH, "declined to make a wish"); livelog_printf(LL_WISH, "declined to make a wish");
return; return;
} else if (otmp == &cg.zeroobj) { } else if (otmp == &gi.invalid_obj) {
/* wizard mode terrain wish: skip livelogging, etc */ /* wizard mode terrain wish: skip livelogging, etc */
return; return;
} }