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
+12
View File
@@ -548,6 +548,18 @@ enum hunger_state_types {
STARVED = 6 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 {
struct kinfo *next; /* chain of delayed killers */ struct kinfo *next; /* chain of delayed killers */
int id; /* uprop keys to ID a delayed killer */ int id; /* uprop keys to ID a delayed killer */
+1 -1
View File
@@ -361,7 +361,7 @@ touchfood(struct obj *otmp)
if (carried(otmp)) { if (carried(otmp)) {
freeinv(otmp); freeinv(otmp);
if (inv_cnt(FALSE) >= 52) { if (inv_cnt(FALSE) >= invlet_basic) {
sellobj_state(SELL_DONTSELL); sellobj_state(SELL_DONTSELL);
dropy(otmp); dropy(otmp);
sellobj_state(SELL_NORMAL); sellobj_state(SELL_NORMAL);
+1 -1
View File
@@ -3887,7 +3887,7 @@ wizkit_addinv(struct obj *obj)
if (Role_if(PM_CLERIC)) if (Role_if(PM_CLERIC))
obj->bknown = 1; /* ok to bypass set_bknown() */ obj->bknown = 1; /* ok to bypass set_bknown() */
/* same criteria as lift_object()'s check for available inventory slot */ /* 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)) { && !merge_choice(gi.invent, obj)) {
/* inventory overflow; can't just place & stack object since /* inventory overflow; can't just place & stack object since
hero isn't in position yet, so schedule for arrival later */ hero isn't in position yet, so schedule for arrival later */
+2 -1
View File
@@ -499,7 +499,8 @@ moverock(void)
pick up a boulder if you have a free pick up a boulder if you have a free
slot or into the overflow ('#') slot slot or into the overflow ('#') slot
unless already carrying at least one */ unless already carrying at least one */
&& (inv_cnt(FALSE) < 52 || !carrying(BOULDER))), && (inv_cnt(FALSE) < invlet_basic
|| !carrying(BOULDER))),
willpickup = (canpickup willpickup = (canpickup
&& (flags.pickup && !gc.context.nopick) && (flags.pickup && !gc.context.nopick)
&& autopick_testobj(otmp, TRUE)); && autopick_testobj(otmp, TRUE));
+13 -12
View File
@@ -401,8 +401,8 @@ invletter_value(char c)
return ('a' <= c && c <= 'z') ? (c - 'a' + 2) return ('a' <= c && c <= 'z') ? (c - 'a' + 2)
: ('A' <= c && c <= 'Z') ? (c - 'A' + 2 + 26) : ('A' <= c && c <= 'Z') ? (c - 'A' + 2 + 26)
: (c == '$') ? 1 : (c == '$') ? 1
: (c == '#') ? 1 + 52 + 1 : (c == '#') ? 1 + invlet_basic + 1
: 1 + 52 + 1 + 1; /* none of the above (shouldn't happen) */ : 1 + invlet_basic + 1 + 1; /* none of the above (shouldn't happen) */
} }
/* qsort comparison routine for sortloot() */ /* qsort comparison routine for sortloot() */
@@ -700,7 +700,7 @@ sortloot(
void void
assigninvlet(struct obj *otmp) assigninvlet(struct obj *otmp)
{ {
boolean inuse[52]; boolean inuse[invlet_basic];
register int i; register int i;
register struct obj *obj; register struct obj *obj;
@@ -710,7 +710,7 @@ assigninvlet(struct obj *otmp)
return; return;
} }
for (i = 0; i < 52; i++) for (i = 0; i < invlet_basic; i++)
inuse[i] = FALSE; inuse[i] = FALSE;
for (obj = gi.invent; obj; obj = obj->nobj) for (obj = gi.invent; obj; obj = obj->nobj)
if (obj != otmp) { if (obj != otmp) {
@@ -726,7 +726,7 @@ assigninvlet(struct obj *otmp)
&& (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z'))) && (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z')))
return; return;
for (i = gl.lastinvnr + 1; i != gl.lastinvnr; i++) { for (i = gl.lastinvnr + 1; i != gl.lastinvnr; i++) {
if (i == 52) { if (i == invlet_basic) {
i = -1; i = -1;
continue; continue;
} }
@@ -1242,8 +1242,9 @@ hold_another_object(
drop_arg = strcpy(buf, drop_arg); drop_arg = strcpy(buf, drop_arg);
obj = addinv_core0(obj, (struct obj *) 0, FALSE); obj = addinv_core0(obj, (struct obj *) 0, FALSE);
if (inv_cnt(FALSE) > 52 || ((obj->otyp != LOADSTONE || !obj->cursed) if (inv_cnt(FALSE) > invlet_basic
&& near_capacity() > prev_encumbr)) { || ((obj->otyp != LOADSTONE || !obj->cursed)
&& near_capacity() > prev_encumbr)) {
/* undo any merge which took place */ /* undo any merge which took place */
if (obj->quan > oquan) if (obj->quan > oquan)
obj = splitobj(obj, oquan); obj = splitobj(obj, oquan);
@@ -5108,7 +5109,7 @@ doprtool(void)
{ {
struct obj *otmp; struct obj *otmp;
int ct = 0; int ct = 0;
char lets[52 + 1]; char lets[invlet_basic + 1];
for (otmp = gi.invent; otmp; otmp = otmp->nobj) for (otmp = gi.invent; otmp; otmp = otmp->nobj)
if (tool_being_used(otmp)) { if (tool_being_used(otmp)) {
@@ -5464,8 +5465,8 @@ doorganize_core(struct obj *obj)
char let; char let;
#define GOLD_INDX 0 #define GOLD_INDX 0
#define GOLD_OFFSET 1 #define GOLD_OFFSET 1
#define OVRFLW_INDX (GOLD_OFFSET + 52) /* past gold and 2*26 letters */ #define OVRFLW_INDX (GOLD_OFFSET + invlet_basic) /* past gold & 2*26 letters */
char lets[1 + 52 + 1 + 1]; /* room for '$a-zA-Z#\0' */ char lets[1 + invlet_basic + 1 + 1]; /* room for '$a-zA-Z#\0' */
char qbuf[QBUFSZ]; char qbuf[QBUFSZ];
char *objname, *otmpname; char *objname, *otmpname;
const char *adj_type; const char *adj_type;
@@ -5498,7 +5499,7 @@ doorganize_core(struct obj *obj)
lets[OVRFLW_INDX] = ' '; lets[OVRFLW_INDX] = ' ';
lets[sizeof lets - 1] = '\0'; lets[sizeof lets - 1] = '\0';
/* for floating inv letters, truncate list after the first open slot */ /* 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'; lets[ix + (splitting ? 1 : 2)] = '\0';
/* blank out all the letters currently in use in the inventory /* 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:"; adj_type = "Splitting and merging:";
obj = otmp; obj = otmp;
extract_nobj(obj, &gi.invent); extract_nobj(obj, &gi.invent);
} else if (inv_cnt(FALSE) >= 52) { } else if (inv_cnt(FALSE) >= invlet_basic) {
(void) merged(&splitting, &obj); /* undo split */ (void) merged(&splitting, &obj); /* undo split */
/* "knapsack cannot accommodate any more items" */ /* "knapsack cannot accommodate any more items" */
Your("pack is too full."); Your("pack is too full.");
+3 -2
View File
@@ -1638,7 +1638,7 @@ lift_object(
availability of open inventory slot iff not already carrying one */ availability of open inventory slot iff not already carrying one */
if (obj->otyp == LOADSTONE if (obj->otyp == LOADSTONE
|| (obj->otyp == BOULDER && throws_rocks(gy.youmonst.data))) { || (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)) || merge_choice(gi.invent, obj))
return 1; /* lift regardless of current situation */ return 1; /* lift regardless of current situation */
/* if we reach here, we're out of slots and already have at least /* 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 } else if (obj->oclass != COIN_CLASS
/* [exception for gold coins will have to change /* [exception for gold coins will have to change
if silver/copper ones ever get implemented] */ 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), /* 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 we aren't limited by the 52 item limit for it, but caller and
"grandcaller" aren't prepared to skip stuff and then pickup "grandcaller" aren't prepared to skip stuff and then pickup
+2 -1
View File
@@ -151,7 +151,8 @@ money2u(struct monst* mon, long amount)
mongold = splitobj(mongold, amount); mongold = splitobj(mongold, amount);
obj_extract_self(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) {
You("have no room for the gold!"); You("have no room for the gold!");
dropy(mongold); dropy(mongold);
} else { } else {
+2 -1
View File
@@ -2683,7 +2683,8 @@ mhitm_ad_sgld(
if (mongold) { if (mongold) {
obj_extract_self(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); addinv(mongold);
Your("purse feels heavier."); Your("purse feels heavier.");
} else { } else {
+5 -3
View File
@@ -390,7 +390,8 @@ dowield(void)
return doswapweapon(); return doswapweapon();
} else if (wep == uquiver) { } else if (wep == uquiver) {
/* offer to split stack if multiple are quivered */ /* 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?", Sprintf(qbuf, "You have %ld %s readied. Wield one?",
uquiver->quan, simpleonames(uquiver)); uquiver->quan, simpleonames(uquiver));
switch (ynq(qbuf)) { switch (ynq(qbuf)) {
@@ -556,7 +557,8 @@ doquiver_core(const char *verb) /* "ready" or "fire" */
return weld_res ? ECMD_TIME : ECMD_OK; return weld_res ? ECMD_TIME : ECMD_OK;
} }
/* offer to split stack if wielding more than 1 */ /* 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?", Sprintf(qbuf, "You are wielding %ld %s. Ready %ld of them?",
uwep->quan, simpleonames(uwep), uwep->quan - 1L); uwep->quan, simpleonames(uwep), uwep->quan - 1L);
switch (ynq(qbuf)) { switch (ynq(qbuf)) {
@@ -590,7 +592,7 @@ doquiver_core(const char *verb) /* "ready" or "fire" */
untwoweapon(); untwoweapon();
was_uwep = TRUE; was_uwep = TRUE;
} else if (newquiver == uswapwep) { } else if (newquiver == uswapwep) {
if (uswapwep->quan > 1L && inv_cnt(FALSE) < 52 if (uswapwep->quan > 1L && inv_cnt(FALSE) < invlet_basic
&& splittable(uswapwep)) { && splittable(uswapwep)) {
Sprintf(qbuf, "%s %ld %s. Ready %ld of them?", Sprintf(qbuf, "%s %ld %s. Ready %ld of them?",
u.twoweap ? "You are dual wielding" u.twoweap ? "You are dual wielding"
+1 -1
View File
@@ -5636,7 +5636,7 @@ destroy_item(int osym, int dmgtyp)
int i, deferral_indx = 0; int i, deferral_indx = 0;
/* 1+52+1: try to handle a full inventory; it doesn't matter if /* 1+52+1: try to handle a full inventory; it doesn't matter if
inventory actually has more, even if everything should be deferred */ 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); (void) memset((genericptr_t) deferrals, 0, sizeof deferrals);
/* /*
+1 -1
View File
@@ -683,7 +683,7 @@ int
curses_character_dialog(const char **choices, const char *prompt) curses_character_dialog(const char **choices, const char *prompt)
{ {
int count, count2, ret, curletter; int count, count2, ret, curletter;
char used_letters[52]; char used_letters[invlet_basic]; /* a..zA..Z */
anything identifier; anything identifier;
menu_item *selected = NULL; menu_item *selected = NULL;
winid wid = curses_get_wid(NHW_MENU); winid wid = curses_get_wid(NHW_MENU);
+1 -2
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 ttyinvmode = InvNormal; /* enum is in wintype.h */
static int inuse_only_start = 0; /* next slot to use for in-use-only mode */ static int inuse_only_start = 0; /* next slot to use for in-use-only mode */
static boolean done_tty_perm_invent_init = FALSE; static boolean done_tty_perm_invent_init = FALSE;
enum { tty_slots = 1 + 52 + 1 }; /* 54 [0..53]: for !show_gold a..zA..Z with enum { tty_slots = invlet_basic + inv_special_slotcount }; /* 52 + 2 */
* 2 unused, or for 'show_gold' $a..zA..Z# */
static boolean slot_tracker[tty_slots]; static boolean slot_tracker[tty_slots];
static long last_glyph_reset_when; static long last_glyph_reset_when;
#ifndef NOINVSYM /* invent.c */ #ifndef NOINVSYM /* invent.c */