avoid another magic number

Some of the hardcoded +1 scattered about are likely
invlet_gold or invlet_overflow, but I didn't hunt those down.
This commit is contained in:
nhmall
2023-11-30 11:15:32 -05:00
parent 9847fcb5b1
commit d7fef5f194
12 changed files with 44 additions and 26 deletions

View File

@@ -548,6 +548,18 @@ enum hunger_state_types {
STARVED = 6
};
/* inventory counts (slots in tty parlance)
* a...zA..Z invlet_basic (52)
* $a...zA..Z# 2 special additions
*/
enum inventory_counts {
invlet_basic = 52,
invlet_gold = 1,
invlet_overflow = 1,
invlet_max = invlet_basic + invlet_gold + invlet_overflow,
/* 2023/11/30 invlet_max is not yet used anywhere */
};
struct kinfo {
struct kinfo *next; /* chain of delayed killers */
int id; /* uprop keys to ID a delayed killer */

View File

@@ -361,7 +361,7 @@ touchfood(struct obj *otmp)
if (carried(otmp)) {
freeinv(otmp);
if (inv_cnt(FALSE) >= 52) {
if (inv_cnt(FALSE) >= invlet_basic) {
sellobj_state(SELL_DONTSELL);
dropy(otmp);
sellobj_state(SELL_NORMAL);

View File

@@ -3887,7 +3887,7 @@ wizkit_addinv(struct obj *obj)
if (Role_if(PM_CLERIC))
obj->bknown = 1; /* ok to bypass set_bknown() */
/* same criteria as lift_object()'s check for available inventory slot */
if (obj->oclass != COIN_CLASS && inv_cnt(FALSE) >= 52
if (obj->oclass != COIN_CLASS && inv_cnt(FALSE) >= invlet_basic
&& !merge_choice(gi.invent, obj)) {
/* inventory overflow; can't just place & stack object since
hero isn't in position yet, so schedule for arrival later */

View File

@@ -499,7 +499,8 @@ moverock(void)
pick up a boulder if you have a free
slot or into the overflow ('#') slot
unless already carrying at least one */
&& (inv_cnt(FALSE) < 52 || !carrying(BOULDER))),
&& (inv_cnt(FALSE) < invlet_basic
|| !carrying(BOULDER))),
willpickup = (canpickup
&& (flags.pickup && !gc.context.nopick)
&& autopick_testobj(otmp, TRUE));

View File

@@ -401,8 +401,8 @@ invletter_value(char c)
return ('a' <= c && c <= 'z') ? (c - 'a' + 2)
: ('A' <= c && c <= 'Z') ? (c - 'A' + 2 + 26)
: (c == '$') ? 1
: (c == '#') ? 1 + 52 + 1
: 1 + 52 + 1 + 1; /* none of the above (shouldn't happen) */
: (c == '#') ? 1 + invlet_basic + 1
: 1 + invlet_basic + 1 + 1; /* none of the above (shouldn't happen) */
}
/* qsort comparison routine for sortloot() */
@@ -700,7 +700,7 @@ sortloot(
void
assigninvlet(struct obj *otmp)
{
boolean inuse[52];
boolean inuse[invlet_basic];
register int i;
register struct obj *obj;
@@ -710,7 +710,7 @@ assigninvlet(struct obj *otmp)
return;
}
for (i = 0; i < 52; i++)
for (i = 0; i < invlet_basic; i++)
inuse[i] = FALSE;
for (obj = gi.invent; obj; obj = obj->nobj)
if (obj != otmp) {
@@ -726,7 +726,7 @@ assigninvlet(struct obj *otmp)
&& (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z')))
return;
for (i = gl.lastinvnr + 1; i != gl.lastinvnr; i++) {
if (i == 52) {
if (i == invlet_basic) {
i = -1;
continue;
}
@@ -1242,8 +1242,9 @@ hold_another_object(
drop_arg = strcpy(buf, drop_arg);
obj = addinv_core0(obj, (struct obj *) 0, FALSE);
if (inv_cnt(FALSE) > 52 || ((obj->otyp != LOADSTONE || !obj->cursed)
&& near_capacity() > prev_encumbr)) {
if (inv_cnt(FALSE) > invlet_basic
|| ((obj->otyp != LOADSTONE || !obj->cursed)
&& near_capacity() > prev_encumbr)) {
/* undo any merge which took place */
if (obj->quan > oquan)
obj = splitobj(obj, oquan);
@@ -5108,7 +5109,7 @@ doprtool(void)
{
struct obj *otmp;
int ct = 0;
char lets[52 + 1];
char lets[invlet_basic + 1];
for (otmp = gi.invent; otmp; otmp = otmp->nobj)
if (tool_being_used(otmp)) {
@@ -5464,8 +5465,8 @@ doorganize_core(struct obj *obj)
char let;
#define GOLD_INDX 0
#define GOLD_OFFSET 1
#define OVRFLW_INDX (GOLD_OFFSET + 52) /* past gold and 2*26 letters */
char lets[1 + 52 + 1 + 1]; /* room for '$a-zA-Z#\0' */
#define OVRFLW_INDX (GOLD_OFFSET + invlet_basic) /* past gold & 2*26 letters */
char lets[1 + invlet_basic + 1 + 1]; /* room for '$a-zA-Z#\0' */
char qbuf[QBUFSZ];
char *objname, *otmpname;
const char *adj_type;
@@ -5498,7 +5499,7 @@ doorganize_core(struct obj *obj)
lets[OVRFLW_INDX] = ' ';
lets[sizeof lets - 1] = '\0';
/* for floating inv letters, truncate list after the first open slot */
if (!flags.invlet_constant && (ix = inv_cnt(FALSE)) < 52)
if (!flags.invlet_constant && (ix = inv_cnt(FALSE)) < invlet_basic)
lets[ix + (splitting ? 1 : 2)] = '\0';
/* blank out all the letters currently in use in the inventory
@@ -5624,7 +5625,7 @@ doorganize_core(struct obj *obj)
adj_type = "Splitting and merging:";
obj = otmp;
extract_nobj(obj, &gi.invent);
} else if (inv_cnt(FALSE) >= 52) {
} else if (inv_cnt(FALSE) >= invlet_basic) {
(void) merged(&splitting, &obj); /* undo split */
/* "knapsack cannot accommodate any more items" */
Your("pack is too full.");

View File

@@ -1638,7 +1638,7 @@ lift_object(
availability of open inventory slot iff not already carrying one */
if (obj->otyp == LOADSTONE
|| (obj->otyp == BOULDER && throws_rocks(gy.youmonst.data))) {
if (inv_cnt(FALSE) < 52 || !carrying(obj->otyp)
if (inv_cnt(FALSE) < invlet_basic || !carrying(obj->otyp)
|| merge_choice(gi.invent, obj))
return 1; /* lift regardless of current situation */
/* if we reach here, we're out of slots and already have at least
@@ -1655,7 +1655,8 @@ lift_object(
} else if (obj->oclass != COIN_CLASS
/* [exception for gold coins will have to change
if silver/copper ones ever get implemented] */
&& inv_cnt(FALSE) >= 52 && !merge_choice(gi.invent, obj)) {
&& inv_cnt(FALSE) >= invlet_basic
&& !merge_choice(gi.invent, obj)) {
/* if there is some gold here (and we haven't already skipped it),
we aren't limited by the 52 item limit for it, but caller and
"grandcaller" aren't prepared to skip stuff and then pickup

View File

@@ -151,7 +151,8 @@ money2u(struct monst* mon, long amount)
mongold = splitobj(mongold, amount);
obj_extract_self(mongold);
if (!merge_choice(gi.invent, mongold) && inv_cnt(FALSE) >= 52) {
if (!merge_choice(gi.invent, mongold)
&& inv_cnt(FALSE) >= invlet_basic) {
You("have no room for the gold!");
dropy(mongold);
} else {

View File

@@ -2683,7 +2683,8 @@ mhitm_ad_sgld(
if (mongold) {
obj_extract_self(mongold);
if (merge_choice(gi.invent, mongold) || inv_cnt(FALSE) < 52) {
if (merge_choice(gi.invent, mongold)
|| inv_cnt(FALSE) < invlet_basic) {
addinv(mongold);
Your("purse feels heavier.");
} else {

View File

@@ -390,7 +390,8 @@ dowield(void)
return doswapweapon();
} else if (wep == uquiver) {
/* offer to split stack if multiple are quivered */
if (uquiver->quan > 1L && inv_cnt(FALSE) < 52 && splittable(uquiver)) {
if (uquiver->quan > 1L && inv_cnt(FALSE) < invlet_basic
&& splittable(uquiver)) {
Sprintf(qbuf, "You have %ld %s readied. Wield one?",
uquiver->quan, simpleonames(uquiver));
switch (ynq(qbuf)) {
@@ -556,7 +557,8 @@ doquiver_core(const char *verb) /* "ready" or "fire" */
return weld_res ? ECMD_TIME : ECMD_OK;
}
/* offer to split stack if wielding more than 1 */
if (uwep->quan > 1L && inv_cnt(FALSE) < 52 && splittable(uwep)) {
if (uwep->quan > 1L && inv_cnt(FALSE) < invlet_basic
&& splittable(uwep)) {
Sprintf(qbuf, "You are wielding %ld %s. Ready %ld of them?",
uwep->quan, simpleonames(uwep), uwep->quan - 1L);
switch (ynq(qbuf)) {
@@ -590,7 +592,7 @@ doquiver_core(const char *verb) /* "ready" or "fire" */
untwoweapon();
was_uwep = TRUE;
} else if (newquiver == uswapwep) {
if (uswapwep->quan > 1L && inv_cnt(FALSE) < 52
if (uswapwep->quan > 1L && inv_cnt(FALSE) < invlet_basic
&& splittable(uswapwep)) {
Sprintf(qbuf, "%s %ld %s. Ready %ld of them?",
u.twoweap ? "You are dual wielding"

View File

@@ -5636,7 +5636,7 @@ destroy_item(int osym, int dmgtyp)
int i, deferral_indx = 0;
/* 1+52+1: try to handle a full inventory; it doesn't matter if
inventory actually has more, even if everything should be deferred */
unsigned short deferrals[1 + 52 + 1]; /* +1: gold, overflow */
unsigned short deferrals[invlet_gold + invlet_basic + invlet_overflow];
(void) memset((genericptr_t) deferrals, 0, sizeof deferrals);
/*

View File

@@ -683,7 +683,7 @@ int
curses_character_dialog(const char **choices, const char *prompt)
{
int count, count2, ret, curletter;
char used_letters[52];
char used_letters[invlet_basic]; /* a..zA..Z */
anything identifier;
menu_item *selected = NULL;
winid wid = curses_get_wid(NHW_MENU);

View File

@@ -261,8 +261,7 @@ static int bordercol[border_elements] = { 0, 0, 0 }; /* left, middle, right */
static int ttyinvmode = InvNormal; /* enum is in wintype.h */
static int inuse_only_start = 0; /* next slot to use for in-use-only mode */
static boolean done_tty_perm_invent_init = FALSE;
enum { tty_slots = 1 + 52 + 1 }; /* 54 [0..53]: for !show_gold a..zA..Z with
* 2 unused, or for 'show_gold' $a..zA..Z# */
enum { tty_slots = invlet_basic + inv_special_slotcount }; /* 52 + 2 */
static boolean slot_tracker[tty_slots];
static long last_glyph_reset_when;
#ifndef NOINVSYM /* invent.c */