From 29946b551bfec60a41ca5a7e3b48fee5e09a9602 Mon Sep 17 00:00:00 2001 From: nhmall Date: Mon, 30 Sep 2019 10:54:03 -0400 Subject: [PATCH] autopickup exception priority in pull request 226 The pull request #226 commentary follows: One major limitation of the autopickup exception system is that you can't define an exception from an exception, despite both menucolors and msgtypes prioritizing rules based on the order they are defined in .nethackrc. This is because the "always pickup" and "never pickup" exceptions are tracked in different lists, and at runtime, when the player steps over an object, the game checks these lists seperately, with "never pickup" taking precedence. This means that if you want to pick up some but not all items matching a given expression, you may need to write a long and kludgy list of regexes to get the behavior you want. I've edited the autopickup exception code to remove this necessity: now the exceptions are stored in one list, and conflicts between them are resolved based on their relative position in that list. Whether an exception was inclusive or exclusive was already tracked individually; I don't know why they were stored separately in the first place. This edit makes the system both more convenient and more consistent with the semantics of menucolors and msgtypes. With these changes, the 33 autopickup exception rules in the wiki article linked above may be replaced with the following 7 much simpler rules for the exact same effect: AUTOPICKUP_EXCEPTION=">.* corpse.*" AUTOPICKUP_EXCEPTION="<.* newt corpse.*" AUTOPICKUP_EXCEPTION="<.* lichen corpse.*" AUTOPICKUP_EXCEPTION="<.* lizard corpse.*" AUTOPICKUP_EXCEPTION="<.* floating eye corpse.*" AUTOPICKUP_EXCEPTION="<.* wraith corpse.* AUTOPICKUP_EXCEPTION=">.*\>.*" closes #226 --- doc/fixes36.3 | 1 + include/decl.h | 1 + include/flag.h | 1 - src/cmd.c | 2 +- src/decl.c | 2 ++ src/options.c | 20 ++++++++++---------- src/pickup.c | 14 ++++++++------ 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/doc/fixes36.3 b/doc/fixes36.3 index fbac27988..4d853b695 100644 --- a/doc/fixes36.3 +++ b/doc/fixes36.3 @@ -314,6 +314,7 @@ NetHack Community Patches (or Variation) Included ------------------------------------------------- add a couple of engraving suggestions in pull request #79 chasonr's faster method to write characters to msdos VGA in pull request #220 +autopickup exception priority change in pull request #226 Code Cleanup and Reorganization diff --git a/include/decl.h b/include/decl.h index e030ce454..974af0f10 100644 --- a/include/decl.h +++ b/include/decl.h @@ -418,6 +418,7 @@ struct autopickup_exception { boolean grab; struct autopickup_exception *next; }; +E struct autopickup_exception *apelist; struct plinemsg_type { xchar msgtype; /* one of MSGTYP_foo */ diff --git a/include/flag.h b/include/flag.h index 090f6e1b0..8fe94339b 100644 --- a/include/flag.h +++ b/include/flag.h @@ -423,7 +423,6 @@ struct instance_flags { int wc2_statuslines; /* default = 2, curses can handle 3 */ int wc2_windowborders; /* display borders on NetHack windows */ int wc2_petattr; /* text attributes for pet */ - struct autopickup_exception *autopickup_exceptions; #ifdef WIN32 #define MAX_ALTKEYHANDLER 25 char altkeyhandler[MAX_ALTKEYHANDLER]; diff --git a/src/cmd.c b/src/cmd.c index a9b17bd86..e6c4049b1 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -2139,7 +2139,7 @@ int final; *ocl ? "'" : "", *ocl ? ocl : "all types", *ocl ? "'" : ""); if (flags.pickup_thrown && *ocl) /* *ocl: don't show if 'all types' */ Strcat(buf, " plus thrown"); - if (iflags.autopickup_exceptions) + if (apelist) Strcat(buf, ", with exceptions"); } else Strcpy(buf, "off"); diff --git a/src/decl.c b/src/decl.c index f81271b52..c6e6c6039 100644 --- a/src/decl.c +++ b/src/decl.c @@ -213,6 +213,8 @@ char preferred_pet; /* '\0', 'c', 'd', 'n' (none) */ NEARDATA struct monst *mydogs = (struct monst *) 0; /* monsters that are moving to another dungeon level */ NEARDATA struct monst *migrating_mons = (struct monst *) 0; +NEARDATA struct autopickup_exception *apelist = + (struct autopickup_exception *)0; NEARDATA struct mvitals mvitals[NUMMONS]; NEARDATA long domove_attempting = 0L; diff --git a/src/options.c b/src/options.c index 836e86d7a..bddc35102 100644 --- a/src/options.c +++ b/src/options.c @@ -4414,7 +4414,7 @@ int count_apes(VOID_ARGS) { int numapes = 0; - struct autopickup_exception *ape = iflags.autopickup_exceptions; + struct autopickup_exception *ape = apelist; while (ape) { numapes++; @@ -5327,7 +5327,7 @@ boolean setinitial, setfromfile; start_menu(tmpwin); if (numapes) { ape = (struct autopickup_exception *) - iflags.autopickup_exceptions; + apelist; any = zeroany; add_menu(tmpwin, NO_GLYPH, &any, 0, 0, iflags.menu_headings, "Always pickup '<'; never pickup '>'", @@ -5897,7 +5897,7 @@ dotogglepickup() if (flags.pickup) { oc_to_str(flags.pickup_types, ocl); Sprintf(buf, "ON, for %s objects%s", ocl[0] ? ocl : "all", - (iflags.autopickup_exceptions) + (apelist) ? ((count_apes() == 1) ? ", with one exception" : ", with some exceptions") @@ -5952,8 +5952,8 @@ const char *mapping; ape->pattern = dupstr(text); ape->grab = grab; - ape->next = (struct autopickup_exception *) iflags.autopickup_exceptions; - iflags.autopickup_exceptions = (struct autopickup_exception *) ape; + ape->next = (struct autopickup_exception *) apelist; + apelist = (struct autopickup_exception *) ape; return 1; } @@ -5963,14 +5963,14 @@ struct autopickup_exception *whichape; { struct autopickup_exception *ape, *freeape, *prev = 0; - for (ape = iflags.autopickup_exceptions; ape;) { + for (ape = apelist; ape;) { if (ape == whichape) { freeape = ape; ape = ape->next; if (prev) prev->next = ape; else - iflags.autopickup_exceptions = ape; + apelist = ape; regex_free(freeape->regex); free((genericptr_t) freeape->pattern); free((genericptr_t) freeape); @@ -5984,11 +5984,11 @@ struct autopickup_exception *whichape; void free_autopickup_exceptions() { - struct autopickup_exception *ape = iflags.autopickup_exceptions; - while ((ape = iflags.autopickup_exceptions) != 0) { + struct autopickup_exception *ape = apelist; + while ((ape = apelist) != 0) { regex_free(ape->regex); free((genericptr_t) ape->pattern); - iflags.autopickup_exceptions = ape->next; + apelist = ape->next; free((genericptr_t) ape); } } diff --git a/src/pickup.c b/src/pickup.c index c0ef2b0cf..ab8d5f069 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -718,11 +718,13 @@ struct obj *obj; /* * Does the text description of this match an exception? */ - struct autopickup_exception - *ape = iflags.autopickup_exceptions; + struct autopickup_exception *ape = apelist; + if (ape) { char *objdesc = makesingular(doname(obj)); - while (ape && !regex_match(objdesc, ape->regex)) ape = ape->next; + + while (ape && !regex_match(objdesc, ape->regex)) + ape = ape->next; } return ape; } @@ -732,6 +734,7 @@ autopick_testobj(otmp, calc_costly) struct obj *otmp; boolean calc_costly; { + struct autopickup_exception *ape; static boolean costly = FALSE; const char *otypes = flags.pickup_types; boolean pickit; @@ -748,9 +751,8 @@ boolean calc_costly; /* check for pickup_types */ pickit = (!*otypes || index(otypes, otmp->oclass)); - /* check for autopickup excpetions */ - struct autopickup_exception - *ape = check_autopickup_exceptions(otmp); + /* check for autopickup exceptions */ + ape = check_autopickup_exceptions(otmp); if (ape) pickit = ape->grab;