From 566dde868359786f3c865e93c99365b67a5eb96c Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Tue, 20 Oct 2020 19:19:54 +0300 Subject: [PATCH] Match object description via single function making the code more readable. Instead of doing strcmp(OBJ_DESCR(objects[otyp]), "foo"), just call objdescr_is(obj, "foo") (via xNetHack) --- include/extern.h | 1 + src/apply.c | 2 +- src/eat.c | 2 +- src/mon.c | 2 +- src/mondata.c | 3 +-- src/muse.c | 6 ++---- src/o_init.c | 20 ++++++++++++++++++++ src/potion.c | 28 ++++++++++++---------------- src/spell.c | 2 +- src/steed.c | 6 ++---- 10 files changed, 42 insertions(+), 30 deletions(-) diff --git a/include/extern.h b/include/extern.h index 18f81ea48..51b62d1bb 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1776,6 +1776,7 @@ E void NDECL(nttty_exit); E void NDECL(init_objects); E void FDECL(obj_shuffle_range, (int, int *, int *)); E int NDECL(find_skates); +E boolean FDECL(objdescr_is, (struct obj *, const char *)); E void NDECL(oinit); E void FDECL(savenames, (NHFILE *)); E void FDECL(restnames, (NHFILE *)); diff --git a/src/apply.c b/src/apply.c index 32e33f97f..a43e3b730 100644 --- a/src/apply.c +++ b/src/apply.c @@ -3406,7 +3406,7 @@ struct obj *obj; boolean fillmsg = FALSE; int expltype = EXPL_MAGICAL; char confirm[QBUFSZ], buf[BUFSZ]; - boolean is_fragile = (!strcmp(OBJ_DESCR(objects[obj->otyp]), "balsa")); + boolean is_fragile = objdescr_is(obj, "balsa"); if (!paranoid_query(ParanoidBreakwand, safe_qbuf(confirm, diff --git a/src/eat.c b/src/eat.c index 17040fc9e..ffc36dd25 100644 --- a/src/eat.c +++ b/src/eat.c @@ -2134,7 +2134,7 @@ eatspecial() pline("Yuck%c", otmp->blessed ? '!' : '.'); else if (otmp->oclass == SCROLL_CLASS /* check description after checking for specific scrolls */ - && !strcmpi(OBJ_DESCR(objects[otmp->otyp]), "YUM YUM")) + && objdescr_is(otmp, "YUM YUM")) pline("Yum%c", otmp->blessed ? '!' : '.'); else pline("Needs salt..."); diff --git a/src/mon.c b/src/mon.c index c90ddcb28..85279ae2e 100644 --- a/src/mon.c +++ b/src/mon.c @@ -1114,7 +1114,7 @@ struct monst *mtmp; distant_name(otmp, doname)); /* give this one even if !verbose */ if (otmp->oclass == SCROLL_CLASS - && !strcmpi(OBJ_DESCR(objects[otmp->otyp]), "YUM YUM")) + && objdescr_is(otmp, "YUM YUM")) pline("Yum%c", otmp->blessed ? '!' : '.'); } else { if (flags.verbose) diff --git a/src/mondata.c b/src/mondata.c index d84ce949a..e3e5444d8 100644 --- a/src/mondata.c +++ b/src/mondata.c @@ -271,8 +271,7 @@ struct obj *obj; /* aatyp == AT_WEAP, AT_SPIT */ o = (mdef == &g.youmonst) ? g.invent : mdef->minvent; for (; o; o = o->nobj) if ((o->owornmask & W_ARMH) - && (s = OBJ_DESCR(objects[o->otyp])) != (char *) 0 - && !strcmp(s, "visored helmet")) + && objdescr_is(o, "visored helmet")) return FALSE; } diff --git a/src/muse.c b/src/muse.c index f14f979ff..31f88fbd6 100644 --- a/src/muse.c +++ b/src/muse.c @@ -57,11 +57,9 @@ struct obj *obj; if (obj->oclass == POTION_CLASS) { coord cc; static const char *empty = "The potion turns out to be empty."; - const char *potion_descr; struct monst *mtmp; - potion_descr = OBJ_DESCR(objects[obj->otyp]); - if (potion_descr && !strcmp(potion_descr, "milky")) { + if (objdescr_is(obj, "milky")) { if (!(g.mvitals[PM_GHOST].mvflags & G_GONE) && !rn2(POTION_OCCUPANT_CHANCE(g.mvitals[PM_GHOST].born))) { if (!enexto(&cc, mon->mx, mon->my, &mons[PM_GHOST])) @@ -87,7 +85,7 @@ struct obj *obj; return 2; } } - if (potion_descr && !strcmp(potion_descr, "smoky") + if (objdescr_is(obj, "smoky") && !(g.mvitals[PM_DJINNI].mvflags & G_GONE) && !rn2(POTION_OCCUPANT_CHANCE(g.mvitals[PM_DJINNI].born))) { if (!enexto(&cc, mon->mx, mon->my, &mons[PM_DJINNI])) diff --git a/src/o_init.c b/src/o_init.c index 1ea5085c4..0ef30c222 100644 --- a/src/o_init.c +++ b/src/o_init.c @@ -282,6 +282,26 @@ shuffle_all() return; } +/* Return TRUE if the provided string matches the unidentified description of + * the provided object. */ +boolean +objdescr_is(obj, descr) +struct obj *obj; +const char *descr; +{ + const char *objdescr; + + if (!obj) { + impossible("objdescr_is: null obj"); + return FALSE; + } + + objdescr = OBJ_DESCR(objects[obj->otyp]); + if (!objdescr) + return FALSE; /* no obj description, no match */ + return !strcmp(objdescr, descr); +} + /* find the object index for snow boots; used [once] by slippery ice code */ int find_skates() diff --git a/src/potion.c b/src/potion.c index 0785ebb1f..4b12acb32 100644 --- a/src/potion.c +++ b/src/potion.c @@ -486,7 +486,6 @@ int dodrink() { register struct obj *otmp; - const char *potion_descr; if (Strangled) { pline("If you can't breathe air, how can you drink liquid?"); @@ -538,21 +537,18 @@ dodrink() } otmp->in_use = TRUE; /* you've opened the stopper */ - potion_descr = OBJ_DESCR(objects[otmp->otyp]); - if (potion_descr) { - if (!strcmp(potion_descr, "milky") - && !(g.mvitals[PM_GHOST].mvflags & G_GONE) - && !rn2(POTION_OCCUPANT_CHANCE(g.mvitals[PM_GHOST].born))) { - ghost_from_bottle(); - useup(otmp); - return 1; - } else if (!strcmp(potion_descr, "smoky") - && !(g.mvitals[PM_DJINNI].mvflags & G_GONE) - && !rn2(POTION_OCCUPANT_CHANCE(g.mvitals[PM_DJINNI].born))) { - djinni_from_bottle(otmp); - useup(otmp); - return 1; - } + if (objdescr_is(otmp, "milky") + && !(g.mvitals[PM_GHOST].mvflags & G_GONE) + && !rn2(POTION_OCCUPANT_CHANCE(g.mvitals[PM_GHOST].born))) { + ghost_from_bottle(); + useup(otmp); + return 1; + } else if (objdescr_is(otmp, "smoky") + && !(g.mvitals[PM_DJINNI].mvflags & G_GONE) + && !rn2(POTION_OCCUPANT_CHANCE(g.mvitals[PM_DJINNI].born))) { + djinni_from_bottle(otmp); + useup(otmp); + return 1; } return dopotion(otmp); } diff --git a/src/spell.c b/src/spell.c index 312ca4475..160a8ed5d 100644 --- a/src/spell.c +++ b/src/spell.c @@ -446,7 +446,7 @@ register struct obj *spellbook; /* attempting to read dull book may make hero fall asleep */ if (!confused && !Sleep_resistance - && !strcmp(OBJ_DESCR(objects[booktype]), "dull")) { + && objdescr_is(spellbook, "dull")) { const char *eyes; int dullbook = rnd(25) - ACURR(A_WIS); diff --git a/src/steed.c b/src/steed.c index 84e18aa63..ae6d9a5da 100644 --- a/src/steed.c +++ b/src/steed.c @@ -114,12 +114,10 @@ struct obj *otmp; } if (Confusion || Fumbling || Glib) chance -= 20; - else if (uarmg && (s = OBJ_DESCR(objects[uarmg->otyp])) != (char *) 0 - && !strncmp(s, "riding ", 7)) + else if (uarmg && objdescr_is(uarmg, "riding gloves")) /* Bonus for wearing "riding" (but not fumbling) gloves */ chance += 10; - else if (uarmf && (s = OBJ_DESCR(objects[uarmf->otyp])) != (char *) 0 - && !strncmp(s, "riding ", 7)) + else if (uarmf && objdescr_is(uarmf, "riding boots")) /* ... or for "riding boots" */ chance += 10; if (otmp->cursed) -- 2.50.1