From: PatR Date: Wed, 9 Jan 2019 09:13:01 +0000 (-0800) Subject: make zeroany, zeromonst, zeroobj 'const' X-Git-Tag: NetHack-3.6.2_Released~97^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dd13b10cf2bcab4f52299bd0171a374d7f58d2f0;p=nethack make zeroany, zeromonst, zeroobj 'const' They're never modified. Minor complication: &zeroobj is used as a special not-Null-but-not-an-object value in multiple places and needs to have 'const' removed with a cast in that situation. --- diff --git a/include/decl.h b/include/decl.h index c93091a73..11d478b68 100644 --- a/include/decl.h +++ b/include/decl.h @@ -1,4 +1,4 @@ -/* NetHack 3.6 decl.h $NHDT-Date: 1496531104 2017/06/03 23:05:04 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.82 $ */ +/* NetHack 3.6 decl.h $NHDT-Date: 1547025154 2019/01/09 09:12:34 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.147 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Michael Allison, 2007. */ /* NetHack may be freely redistributed. See license for details. */ @@ -247,9 +247,10 @@ E NEARDATA struct obj *migrating_objs; E NEARDATA struct obj *billobjs; E NEARDATA struct obj *current_wand, *thrownobj, *kickedobj; -E NEARDATA struct obj zeroobj; /* for init; &zeroobj used as special value */ +E NEARDATA const struct obj zeroobj; /* for init; also, &zeroobj is used + * as special value */ -E NEARDATA anything zeroany; /* init'd and defined in decl.c */ +E NEARDATA const anything zeroany; /* init'd and defined in decl.c */ #include "you.h" E NEARDATA struct you u; @@ -261,7 +262,7 @@ E NEARDATA struct u_realtime urealtime; #include "pm.h" #endif -E NEARDATA struct monst zeromonst; /* for init of new or temp monsters */ +E NEARDATA const struct monst zeromonst; /* for init of new or temp monsters */ E NEARDATA struct monst youmonst; /* monster details when hero is poly'd */ E NEARDATA struct monst *mydogs, *migrating_mons; diff --git a/src/decl.c b/src/decl.c index e9ab7fcb5..741703da5 100644 --- a/src/decl.c +++ b/src/decl.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 decl.c $NHDT-Date: 1446975463 2015/11/08 09:37:43 $ $NHDT-Branch: master $:$NHDT-Revision: 1.62 $ */ +/* NetHack 3.6 decl.c $NHDT-Date: 1547025164 2019/01/09 09:12:44 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.141 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Michael Allison, 2009. */ /* NetHack may be freely redistributed. See license for details. */ @@ -198,10 +198,10 @@ NEARDATA struct obj *migrating_objs = (struct obj *) 0; NEARDATA struct obj *billobjs = (struct obj *) 0; /* used to zero all elements of a struct obj and a struct monst */ -NEARDATA struct obj zeroobj = DUMMY; -NEARDATA struct monst zeromonst = DUMMY; +NEARDATA const struct obj zeroobj = DUMMY; +NEARDATA const struct monst zeromonst = DUMMY; /* used to zero out union any; initializer deliberately omitted */ -NEARDATA anything zeroany; +NEARDATA const anything zeroany; /* originally from dog.c */ NEARDATA char dogname[PL_PSIZ] = DUMMY; diff --git a/src/invent.c b/src/invent.c index 9e32a4363..0c1e45e23 100644 --- a/src/invent.c +++ b/src/invent.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 invent.c $NHDT-Date: 1546770988 2019/01/06 10:36:28 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.249 $ */ +/* NetHack 3.6 invent.c $NHDT-Date: 1547025166 2019/01/09 09:12:46 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.250 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Derek S. Ray, 2015. */ /* NetHack may be freely redistributed. See license for details. */ @@ -426,7 +426,7 @@ const genericptr vptr2; return val2 - val1; /* bigger is better */ } -tiebreak: + tiebreak: /* They're identical, as far as we're concerned. We want to force a deterministic order, and do so by producing a stable sort: maintain the original order of equal items. */ @@ -938,7 +938,7 @@ struct obj *obj; && obj->oartifact != ART_MJOLLNIR && (throwing_weapon(obj) || is_ammo(obj))) setuqwep(obj); -added: + added: addinv_core2(obj); carry_obj_effects(obj); /* carrying affects the obj */ update_inventory(); @@ -1702,9 +1702,9 @@ register const char *let, *word; You("mime %s something%s%s.", ing_suffix(bp), suf ? " " : "", suf ? suf : ""); } - return (allownone ? &zeroobj : (struct obj *) 0); + return (allownone ? (struct obj *) &zeroobj : (struct obj *) 0); } -redo_menu: + redo_menu: /* since gold is now kept in inventory, we need to do processing for select-from-invent before checking whether gold has been picked */ if (ilet == '?' || ilet == '*') { @@ -1735,7 +1735,7 @@ redo_menu: if (!ilet) continue; if (ilet == HANDS_SYM) - return &zeroobj; + return (struct obj *) &zeroobj; /* cast away 'const' */ if (ilet == '\033') { if (flags.verbose) pline1(Never_mind); @@ -2139,7 +2139,7 @@ int FDECL((*fn), (OBJ_P)), FDECL((*ckfn), (OBJ_P)); * For example, if a person specifies =/ then first all rings * will be asked about followed by all wands. -dgk */ -nextclass: + nextclass: ilet = 'a' - 1; if (*objchn && (*objchn)->oclass == COIN_CLASS) ilet--; /* extra iteration */ @@ -2249,7 +2249,7 @@ nextclass: pline("That was all."); else if (!dud && !cnt) pline("No applicable objects."); -ret: + ret: unsortloot(&sortedchn); bypass_objlist(*objchn, FALSE); return cnt; @@ -2670,7 +2670,7 @@ long *out_cnt; add_menu(win, NO_GLYPH, &any, HANDS_SYM, 0, ATR_NONE, xtra_choice, MENU_UNSELECTED); } -nextclass: + nextclass: classcount = 0; for (srtinv = sortedinvent; (otmp = srtinv->obj) != 0; ++srtinv) { if (lets && !index(lets, otmp->invlet)) @@ -4101,7 +4101,7 @@ doorganize() /* inventory organizer by Del Lamb */ compatible stacks get collected along the way, but splitting to same slot is not */ || (splitting && let == obj->invlet)) { - noadjust: + noadjust: if (splitting) (void) merged(&splitting, &obj); if (!ever_mind) diff --git a/src/muse.c b/src/muse.c index 94b0350ad..e98bbe553 100644 --- a/src/muse.c +++ b/src/muse.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 muse.c $NHDT-Date: 1539804880 2018/10/17 19:34:40 $ $NHDT-Branch: keni-makedefsm $:$NHDT-Revision: 1.85 $ */ +/* NetHack 3.6 muse.c $NHDT-Date: 1547025167 2019/01/09 09:12:47 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.92 $ */ /* Copyright (C) 1990 by Ken Arromdee */ /* NetHack may be freely redistributed. See license for details. */ @@ -477,7 +477,7 @@ struct monst *mtmp; } } } - toot: + toot: ; } @@ -585,7 +585,7 @@ struct monst *mtmp; m.has_defense = MUSE_SCR_CREATE_MONSTER; } } -botm: + botm: return (boolean) !!m.has_defense; #undef nomore } @@ -648,7 +648,7 @@ struct monst *mtmp; mzapmsg(mtmp, otmp, TRUE); otmp->spe--; how = WAN_TELEPORTATION; - mon_tele: + mon_tele: if (tele_restrict(mtmp)) { /* mysterious force... */ if (vismon && how) /* mentions 'teleport' */ makeknown(how); @@ -886,7 +886,7 @@ struct monst *mtmp; case MUSE_SSTAIRS: m_flee(mtmp); if (ledger_no(&u.uz) == 1) { - escape: + escape: /* Monsters without the Amulet escape the dungeon and * are gone for good when they leave up the up stairs. * A monster with the Amulet would leave it behind @@ -995,7 +995,7 @@ struct monst *mtmp; if (is_animal(pm) || attacktype(pm, AT_EXPL) || mindless(mtmp->data) || pm->mlet == S_GHOST || pm->mlet == S_KOP) return 0; -try_again: + try_again: switch (rn2(8 + (difficulty > 3) + (difficulty > 6) + (difficulty > 8))) { case 6: case 9: @@ -1791,7 +1791,7 @@ struct monst *mtmp; (coord *) 0); return 2; } else { - skipmsg: + skipmsg: if (vismon) { pline("%s looks uneasy.", Monnam(mtmp)); if (!objects[POT_GAIN_LEVEL].oc_name_known @@ -2412,7 +2412,7 @@ boolean by_you; } } if (t && t->ttyp == FIRE_TRAP) - return muse_unslime(mon, &zeroobj, t, by_you); + return muse_unslime(mon, (struct obj *) &zeroobj, t, by_you); } /* MUSE */ diff --git a/src/objnam.c b/src/objnam.c index d051fdc0b..0081b7267 100644 --- a/src/objnam.c +++ b/src/objnam.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 objnam.c $NHDT-Date: 1546687293 2019/01/05 11:21:33 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.232 $ */ +/* NetHack 3.6 objnam.c $NHDT-Date: 1547025168 2019/01/09 09:12:48 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.233 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Robert Patrick Rankin, 2011. */ /* NetHack may be freely redistributed. See license for details. */ @@ -705,7 +705,7 @@ unsigned cxn_flags; /* bitmask of CXN_xxx values */ if (has_oname(obj) && dknown) { Strcat(buf, " named "); - nameit: + nameit: Strcat(buf, ONAME(obj)); } @@ -1083,7 +1083,7 @@ unsigned doname_flags; goto charges; break; case WAND_CLASS: - charges: + charges: if (known) Sprintf(eos(bp), " (%d:%d)", (int) obj->recharged, obj->spe); break; @@ -1092,7 +1092,7 @@ unsigned doname_flags; Strcat(bp, " (lit)"); break; case RING_CLASS: - ring: + ring: if (obj->owornmask & W_RINGR) Strcat(bp, " (on right "); if (obj->owornmask & W_RINGL) @@ -2035,7 +2035,7 @@ register const char *verb; return strcpy(buf, verb); } -sing: + sing: Strcpy(buf, verb); len = (int) strlen(buf); bspot = buf + len - 1; @@ -2379,7 +2379,7 @@ const char *oldstr; /* Default: append an 's' */ Strcasecpy(spot + 1, "s"); -bottom: + bottom: if (excess) Strcat(str, excess); return str; @@ -2479,7 +2479,7 @@ const char *oldstr; || (p - 4 == bp && !strcmpi(p - 4, "lens"))) { goto bottom; } - mins: + mins: *(p - 1) = '\0'; /* drop s */ } else { /* input doesn't end in 's' */ @@ -2503,7 +2503,7 @@ const char *oldstr; /* here we cannot find the plural suffix */ } -bottom: + bottom: /* if we stripped off a suffix (" of bar" from "foo of bar"), put it back now [strcat() isn't actually 100% safe here...] */ if (excess) @@ -3376,7 +3376,7 @@ struct obj *no_wish; } } -retry: + retry: /* "grey stone" check must be before general "stone" */ for (i = 0; i < SIZE(o_ranges); i++) if (!strcmpi(bp, o_ranges[i].name)) { @@ -3425,7 +3425,7 @@ retry: actualn = bp; if (!dn) dn = actualn; /* ex. "skull cap" */ -srch: + srch: /* check real names of gems first */ if (!oclass && actualn) { for (i = bases[GEM_CLASS]; i <= LAST_GEM; i++) { @@ -3561,7 +3561,7 @@ srch: * trap objects like beartraps. * Disallow such topology tweaks for WIZKIT startup wishes. */ -wiztrap: + wiztrap: if (wizard && !program_state.wizkit_wishing) { struct rm *lev; int trap, x = u.ux, y = u.uy; @@ -3583,7 +3583,7 @@ wiztrap: (trap != MAGIC_PORTAL) ? "" : " to nowhere"); } else pline("Creation of %s failed.", an(tname)); - return &zeroobj; + return (struct obj *) &zeroobj; } /* furniture and terrain */ @@ -3596,20 +3596,20 @@ wiztrap: lev->blessedftn = 1; pline("A %sfountain.", lev->blessedftn ? "magic " : ""); newsym(x, y); - return &zeroobj; + return (struct obj *) &zeroobj; } if (!BSTRCMPI(bp, p - 6, "throne")) { lev->typ = THRONE; pline("A throne."); newsym(x, y); - return &zeroobj; + return (struct obj *) &zeroobj; } if (!BSTRCMPI(bp, p - 4, "sink")) { lev->typ = SINK; level.flags.nsinks++; pline("A sink."); newsym(x, y); - return &zeroobj; + return (struct obj *) &zeroobj; } /* ("water" matches "potion of water" rather than terrain) */ if (!BSTRCMPI(bp, p - 4, "pool") || !BSTRCMPI(bp, p - 4, "moat")) { @@ -3619,7 +3619,7 @@ wiztrap: /* Must manually make kelp! */ water_damage_chain(level.objects[x][y], TRUE); newsym(x, y); - return &zeroobj; + return (struct obj *) &zeroobj; } if (!BSTRCMPI(bp, p - 4, "lava")) { /* also matches "molten lava" */ lev->typ = LAVAPOOL; @@ -3628,7 +3628,7 @@ wiztrap: if (!(Levitation || Flying)) (void) lava_effects(); newsym(x, y); - return &zeroobj; + return (struct obj *) &zeroobj; } if (!BSTRCMPI(bp, p - 5, "altar")) { @@ -3648,7 +3648,7 @@ wiztrap: lev->altarmask = Align2amask(al); pline("%s altar.", An(align_str(al))); newsym(x, y); - return &zeroobj; + return (struct obj *) &zeroobj; } if (!BSTRCMPI(bp, p - 5, "grave") @@ -3657,7 +3657,7 @@ wiztrap: pline("%s.", IS_GRAVE(lev->typ) ? "A grave" : "Can't place a grave here"); newsym(x, y); - return &zeroobj; + return (struct obj *) &zeroobj; } if (!BSTRCMPI(bp, p - 4, "tree")) { @@ -3665,14 +3665,14 @@ wiztrap: pline("A tree."); newsym(x, y); block_point(x, y); - return &zeroobj; + return (struct obj *) &zeroobj; } if (!BSTRCMPI(bp, p - 4, "bars")) { lev->typ = IRONBARS; pline("Iron bars."); newsym(x, y); - return &zeroobj; + return (struct obj *) &zeroobj; } } @@ -3688,10 +3688,10 @@ wiztrap: if (!oclass) return ((struct obj *) 0); -any: + any: if (!oclass) - oclass = wrpsym[rn2((int) sizeof(wrpsym))]; -typfnd: + oclass = wrpsym[rn2((int) sizeof wrpsym)]; + typfnd: if (typ) oclass = objects[typ].oc_class; @@ -3981,7 +3981,7 @@ typfnd: || (otmp->oartifact && rn2(nartifact_exist()) > 1)) && !wizard) { artifact_exists(otmp, safe_oname(otmp), FALSE); obfree(otmp, (struct obj *) 0); - otmp = &zeroobj; + otmp = (struct obj *) &zeroobj; pline("For a moment, you feel %s in your %s, but it disappears!", something, makeplural(body_part(HAND))); } diff --git a/src/weapon.c b/src/weapon.c index 567aa9578..5ca8d7a6e 100644 --- a/src/weapon.c +++ b/src/weapon.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 weapon.c $NHDT-Date: 1545964580 2018/12/28 02:36:20 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.67 $ */ +/* NetHack 3.6 weapon.c $NHDT-Date: 1547025169 2019/01/09 09:12:49 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.68 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Robert Patrick Rankin, 2011. */ /* NetHack may be freely redistributed. See license for details. */ @@ -402,7 +402,7 @@ register struct monst *mtmp; char mlet = mtmp->data->mlet; - propellor = &zeroobj; + propellor = (struct obj *) &zeroobj; Oselect(EGG); /* cockatrice egg */ if (mlet == S_KOP) /* pies are first choice for Kops */ Oselect(CREAM_PIE); @@ -461,25 +461,25 @@ register struct monst *mtmp; } /* KMH -- This belongs here so darts will work */ - propellor = &zeroobj; + propellor = (struct obj *) &zeroobj; - prop = (objects[rwep[i]]).oc_skill; + prop = objects[rwep[i]].oc_skill; if (prop < 0) { switch (-prop) { case P_BOW: - propellor = (oselect(mtmp, YUMI)); + propellor = oselect(mtmp, YUMI); if (!propellor) - propellor = (oselect(mtmp, ELVEN_BOW)); + propellor = oselect(mtmp, ELVEN_BOW); if (!propellor) - propellor = (oselect(mtmp, BOW)); + propellor = oselect(mtmp, BOW); if (!propellor) - propellor = (oselect(mtmp, ORCISH_BOW)); + propellor = oselect(mtmp, ORCISH_BOW); break; case P_SLING: - propellor = (oselect(mtmp, SLING)); + propellor = oselect(mtmp, SLING); break; case P_CROSSBOW: - propellor = (oselect(mtmp, CROSSBOW)); + propellor = oselect(mtmp, CROSSBOW); } if ((otmp = MON_WEP(mtmp)) && mwelded(otmp) && otmp != propellor && mtmp->weapon_check == NO_WEAPON_WANTED) @@ -681,6 +681,7 @@ register struct monst *mon; } if (obj && obj != &zeroobj) { struct obj *mw_tmp = MON_WEP(mon); + if (mw_tmp && mw_tmp->otyp == obj->otyp) { /* already wielding it */ mon->weapon_check = NEED_WEAPON;