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

@@ -9,6 +9,7 @@
static int FDECL(throw_obj, (struct obj *, int));
static boolean FDECL(ok_to_throw, (int *));
static int FDECL(throw_ok, (struct obj *));
static void NDECL(autoquiver);
static int FDECL(gem_accept, (struct monst *, struct obj *));
static void FDECL(tmiss, (struct obj *, struct monst *, BOOLEAN_P));
@@ -20,13 +21,6 @@ static boolean FDECL(toss_up, (struct obj *, BOOLEAN_P));
static void FDECL(sho_obj_return_to_u, (struct obj * obj));
static boolean FDECL(mhurtle_step, (genericptr_t, int, int));
static NEARDATA const char toss_objs[] = { ALLOW_COUNT, COIN_CLASS,
ALL_CLASSES, WEAPON_CLASS, 0 };
/* different default choices when wielding a sling (gold must be included) */
static NEARDATA const char t_bullets[] = {
ALLOW_COUNT, COIN_CLASS, ALL_CLASSES, GEM_CLASS, 0
};
/* g.thrownobj (decl.c) tracks an object until it lands */
int
@@ -275,6 +269,36 @@ int *shotlimit_p; /* (see dothrow()) */
return TRUE;
}
/* getobj callback for object to be thrown */
static int
throw_ok(obj)
struct obj *obj;
{
if (!obj)
return GETOBJ_EXCLUDE;
if (obj->quan == 1 && (obj == uwep || (obj == uswapwep && u.twoweap)))
return GETOBJ_DOWNPLAY;
/* Possible extension: return GETOBJ_SUGGEST for uwep if it will return when
* thrown. */
if (obj->oclass == COIN_CLASS)
return GETOBJ_SUGGEST;
if (!uslinging() && obj->oclass == WEAPON_CLASS)
return GETOBJ_SUGGEST;
/* Possible extension: exclude weapons that make no sense to throw, such as
* whips, bows, slings, rubber hoses. */
if (uslinging() && obj->oclass == GEM_CLASS)
return GETOBJ_SUGGEST;
if (throws_rocks(g.youmonst.data) && obj->otyp == BOULDER)
return GETOBJ_SUGGEST;
return GETOBJ_DOWNPLAY;
}
/* t command - throw */
int
dothrow()
@@ -296,7 +320,7 @@ dothrow()
if (!ok_to_throw(&shotlimit))
return 0;
obj = getobj(uslinging() ? t_bullets : toss_objs, "throw");
obj = getobj("throw", throw_ok, GETOBJ_PROMPT | GETOBJ_ALLOWCNT);
/* it is also possible to throw food */
/* (or jewels, or iron balls... ) */
@@ -408,7 +432,7 @@ dofire()
use direction of previous throw as getobj()'s choice here */
g.in_doagain = 0;
/* choose something from inventory, then usually quiver it */
obj = getobj(uslinging() ? t_bullets : toss_objs, "throw");
obj = getobj("throw", throw_ok, GETOBJ_PROMPT | GETOBJ_ALLOWCNT);
/* Q command doesn't allow gold in quiver */
if (obj && !obj->owornmask && obj->oclass != COIN_CLASS)
setuqwep(obj); /* demi-autoquiver */