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:
73
src/wield.c
73
src/wield.c
@@ -54,6 +54,8 @@
|
||||
|
||||
static boolean FDECL(cant_wield_corpse, (struct obj *));
|
||||
static int FDECL(ready_weapon, (struct obj *));
|
||||
static int FDECL(ready_ok, (struct obj *));
|
||||
static int FDECL(wield_ok, (struct obj *));
|
||||
|
||||
/* used by will_weld() */
|
||||
/* probably should be renamed */
|
||||
@@ -267,18 +269,51 @@ register struct obj *obj;
|
||||
return;
|
||||
}
|
||||
|
||||
/*** Commands to change particular slot(s) ***/
|
||||
/* getobj callback for object to ready for throwing/shooting */
|
||||
static int
|
||||
ready_ok(obj)
|
||||
struct obj *obj;
|
||||
{
|
||||
if (!obj)
|
||||
return GETOBJ_SUGGEST;
|
||||
|
||||
static NEARDATA const char wield_objs[] = {
|
||||
ALLOW_COUNT, ALL_CLASSES, ALLOW_NONE, WEAPON_CLASS, TOOL_CLASS, 0
|
||||
};
|
||||
static NEARDATA const char ready_objs[] = {
|
||||
ALLOW_COUNT, COIN_CLASS, ALL_CLASSES, ALLOW_NONE, WEAPON_CLASS, 0
|
||||
};
|
||||
static NEARDATA const char w_bullets[] = { /* (different from dothrow.c) */
|
||||
ALLOW_COUNT, COIN_CLASS, ALL_CLASSES, ALLOW_NONE,
|
||||
GEM_CLASS, WEAPON_CLASS, 0
|
||||
};
|
||||
/* exclude when wielded... */
|
||||
if ((obj == uwep || (obj == uswapwep && u.twoweap))
|
||||
&& obj->quan == 1) /* ...unless more than one */
|
||||
return GETOBJ_EXCLUDE_INACCESS;
|
||||
|
||||
if (obj->oclass == WEAPON_CLASS || obj->oclass == COIN_CLASS)
|
||||
return GETOBJ_SUGGEST;
|
||||
/* Possible extension: exclude weapons that make no sense to throw, such as
|
||||
* whips, bows, slings, rubber hoses. */
|
||||
|
||||
/* Include gems/stones as likely candidates if either primary
|
||||
or secondary weapon is a sling. */
|
||||
if (obj->oclass == GEM_CLASS
|
||||
&& (uslinging()
|
||||
|| (uswapwep && objects[uswapwep->otyp].oc_skill == P_SLING)))
|
||||
return GETOBJ_SUGGEST;
|
||||
|
||||
return GETOBJ_DOWNPLAY;
|
||||
}
|
||||
|
||||
/* getobj callback for object to wield */
|
||||
static int
|
||||
wield_ok(obj)
|
||||
struct obj *obj;
|
||||
{
|
||||
if (!obj)
|
||||
return GETOBJ_SUGGEST;
|
||||
|
||||
if (obj->oclass == COIN_CLASS)
|
||||
return GETOBJ_EXCLUDE;
|
||||
|
||||
if (obj->oclass == WEAPON_CLASS
|
||||
|| (obj->oclass == TOOL_CLASS && is_weptool(obj)))
|
||||
return GETOBJ_SUGGEST;
|
||||
|
||||
return GETOBJ_DOWNPLAY;
|
||||
}
|
||||
|
||||
int
|
||||
dowield()
|
||||
@@ -297,7 +332,7 @@ dowield()
|
||||
|
||||
/* Prompt for a new weapon */
|
||||
clear_splitobjs();
|
||||
if (!(wep = getobj(wield_objs, "wield"))) {
|
||||
if (!(wep = getobj("wield", wield_ok, GETOBJ_PROMPT | GETOBJ_ALLOWCNT))) {
|
||||
/* Cancelled */
|
||||
return 0;
|
||||
} else if (wep == uwep) {
|
||||
@@ -438,7 +473,6 @@ dowieldquiver()
|
||||
{
|
||||
char qbuf[QBUFSZ];
|
||||
struct obj *newquiver;
|
||||
const char *quivee_types;
|
||||
int res;
|
||||
boolean finish_splitting = FALSE,
|
||||
was_uwep = FALSE, was_twoweap = u.twoweap;
|
||||
@@ -446,18 +480,11 @@ dowieldquiver()
|
||||
/* Since the quiver isn't in your hands, don't check cantwield(), */
|
||||
/* will_weld(), touch_petrifies(), etc. */
|
||||
g.multi = 0;
|
||||
/* forget last splitobj() before calling getobj() with ALLOW_COUNT */
|
||||
/* forget last splitobj() before calling getobj() with GETOBJ_ALLOWCNT */
|
||||
clear_splitobjs();
|
||||
|
||||
/* Prompt for a new quiver: "What do you want to ready?"
|
||||
(Include gems/stones as likely candidates if either primary
|
||||
or secondary weapon is a sling.) */
|
||||
quivee_types = (uslinging()
|
||||
|| (uswapwep
|
||||
&& objects[uswapwep->otyp].oc_skill == P_SLING))
|
||||
? w_bullets
|
||||
: ready_objs;
|
||||
newquiver = getobj(quivee_types, "ready");
|
||||
/* Prompt for a new quiver: "What do you want to ready?" */
|
||||
newquiver = getobj("ready", ready_ok, GETOBJ_PROMPT | GETOBJ_ALLOWCNT);
|
||||
|
||||
if (!newquiver) {
|
||||
/* Cancelled */
|
||||
|
||||
Reference in New Issue
Block a user