]> granicus.if.org Git - nethack/commitdiff
Match object description via single function
authorPasi Kallinen <paxed@alt.org>
Tue, 20 Oct 2020 16:19:54 +0000 (19:19 +0300)
committerPasi Kallinen <paxed@alt.org>
Tue, 20 Oct 2020 16:19:57 +0000 (19:19 +0300)
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
src/apply.c
src/eat.c
src/mon.c
src/mondata.c
src/muse.c
src/o_init.c
src/potion.c
src/spell.c
src/steed.c

index 18f81ea4809b61b7a25c0b78ca42d0d4922d4997..51b62d1bbbbf69277026415d6d9c5a6ce849ecec 100644 (file)
@@ -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 *));
index 32e33f97f9446c2e0b6641599b11b4110cbab3b3..a43e3b73070475a09550df87bde40fbeaef6946e 100644 (file)
@@ -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,
index 17040fc9e28239a0537dd8b89d3aee821b245d6f..ffc36dd256eede3330ebd977092cac4c94661643 100644 (file)
--- 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...");
index c90ddcb2876e5a1ad3e39a0a7039f8da19d0706e..85279ae2e490729485490b2f577d8edb9f622abb 100644 (file)
--- 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)
index d84ce949ad0955b73d80d2f79607f5be80a136d4..e3e5444d8e9da2124fb457ff74b425b588f1eea9 100644 (file)
@@ -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;
     }
 
index f14f979ffc1e1c8e85e37c2b589b6dcfc2e13ee4..31f88fbd619e597004ea1e13a10b734aeb5804d4 100644 (file)
@@ -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]))
index 1ea5085c4e6148147483221e129264dbfe142762..0ef30c222f1cf7420cd0f70a289d731c5a31a3ee 100644 (file)
@@ -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()
index 0785ebb1f08aa6fa19b9c41e2f8496602e029f66..4b12acb3280e2b9e004163b92b7f54e466f8737a 100644 (file)
@@ -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);
 }
index 312ca447529f108a71cc17cd74eb99f245f09023..160a8ed5d6bcdd23b1f025360bb1502b608cf328 100644 (file)
@@ -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);
 
index 84e18aa6378c73188ea293dc7f286f4e81df2347..ae6d9a5dad225671bf3315a1de39f99b276cb33f 100644 (file)
@@ -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)