Refactor getobj() to use callbacks on candidate objects

This replaces the arcane system previously used by getobj where the
caller would pass in a "string" whose characters were object class
numbers, with the first up to four characters being special constants
that effectively acted as flags and had to be in a certain order.
Because there are many places where getobj must behave more granularly
than just object class filtering, this was supplemented by over a
hundred lines enumerating all these special cases and "ugly checks", as
well as other ugly code spread around in getobj callers that formatted
the "string".

Now, getobj callers pass in a callback which will return one of five
possible values for any given object in the player's inventory. The
logic of determining the eligibility of a given object is handled in the
caller, which greatly simplifies the code and makes it clearer to read.
Particularly since there's no real need to cram everything into one if
statement.

This is related to pull request #77 by FIQ; it's largely a
reimplementation of its callbacks system, without doing a bigger than
necessary refactor of getobj or adding the ability to select a
floor/trap/dungeon feature with getobj. Differences in implementation
are mostly minor:
- using enum constants for returns instead of magic numbers
- 5 possible return values for callbacks instead of 3, due to trying to
  make it behave exactly as it did previously. PR #77 would sometimes
  outright exclude objects because it lacked semantics for invalid
  objects that should be selectable anyway, or give slightly different
  messages.
- passing a bitmask of flags to getobj rather than booleans (easier to
  add more flags later - such as FIQ's "allow floor features" flag, if
  that becomes desirable)
- renaming some of getobj's variables to clearer versions
- naming all callbacks consistently with "_ok"
- generally more comments explaining things

The callbacks use the same logic from getobj_obj_exclude,
getobj_obj_exclude_too and getobj_obj_acceptable_unlisted (and in a few
cases, from special cases still within getobj). In a number of them, I
added comments suggesting possible further refinements to what is and
isn't eligible (e.g. should a bullwhip really be presented as a
candidate for readying a thrown weapon?)

This also removed ALLOW_COUNT and ALLOW_NONE, relics of the old system,
and moved ALLOW_ALL's definition into detect.c which is the only place
it's used now (unrelated to getobj). The ALLOW_ALL functionality still
exists as the GETOBJ_PROMPT flag, because its main use is to force
getobj to prompt for input even if nothing is valid.

I did not refactor ggetobj() as part of this change.
This commit is contained in:
copperwater
2021-01-07 11:05:59 -05:00
parent 39fbf083a2
commit 0b638592a4
20 changed files with 792 additions and 456 deletions

View File

@@ -478,6 +478,40 @@ enum bodypart_types {
#define MON_POLE_DIST 5 /* How far monsters can use pole-weapons */
#define PET_MISSILE_RANGE2 36 /* Square of distance within which pets shoot */
/* flags passed to getobj() to control how it responds to player input */
#define GETOBJ_NOFLAGS 0x0
#define GETOBJ_ALLOWCNT 0x1 /* is a count allowed with this command? */
#define GETOBJ_PROMPT 0x2 /* should it force a prompt for input? (prevents it
exiting early with "You don't have anything to
foo" if nothing in inventory is valid) */
/* values returned from getobj() callback functions */
enum getobj_callback_returns {
/* generally invalid - can't be used for this purpose. will give a "silly
* thing" message if the player tries to pick it, unless a more specific
* failure message is in getobj itself - e.g. "You cannot foo gold". */
GETOBJ_EXCLUDE = -2,
/* invalid because it is an inaccessible or unwanted piece of gear, but
* psuedo-valid for the purposes of allowing the player to select it and
* getobj to return it if there is a prompt instead of getting "silly
* thing", in order for the getobj caller to present a specific failure
* message. Other than that, the only thing this does differently from
* GETOBJ_EXCLUDE is that it inserts an "else" in "You don't have anything
* else to foo". */
GETOBJ_EXCLUDE_INACCESS = -1,
/* invalid for purposes of not showing a prompt if nothing is valid but
* psuedo-valid for selecting - identical to GETOBJ_EXCLUDE_INACCESS but
* without the "else" in "You don't have anything else to foo". */
GETOBJ_EXCLUDE_SELECTABLE = 0,
/* valid - invlet not presented in the summary or the ? menu as a
* recommendation, but is selectable if the player enters it anyway. Used
* for objects that are actually valid but unimportantly so, such as shirts
* for reading. */
GETOBJ_DOWNPLAY = 1,
/* valid - will be shown in summary and ? menu */
GETOBJ_SUGGEST = 2,
};
/*
* option setting restrictions
*/