Unify getting a count into single function

This commit is contained in:
Pasi Kallinen
2016-01-06 13:50:16 +02:00
parent 1bc05714b5
commit 4c016853d4
4 changed files with 80 additions and 67 deletions

View File

@@ -92,6 +92,8 @@ fix death reason when eating tainted glob of <monster> (not corpse)
use appropriate place name for drum of earthquake shakes
fix unmapped branch stairs on sokoban level
redraw map when hilite_pile is toggled to display the highlighting
make commands that accept a count prefix for item selection
show "Count:" like command repeating does
Platform- and/or Interface-Specific Fixes

View File

@@ -204,6 +204,7 @@ E int FDECL(isok, (int, int));
E int FDECL(get_adjacent_loc,
(const char *, const char *, XCHAR_P, XCHAR_P, coord *));
E const char *FDECL(click_to_cmd, (int, int, int));
E char FDECL(get_count, (char *, char, long, long *));
#ifdef HANGUPHANDLING
E void FDECL(hangup, (int));
E void NDECL(end_of_input);

View File

@@ -3864,6 +3864,57 @@ int x, y, mod;
return cmd;
}
char
get_count(allowchars, inkey, max, count)
char *allowchars;
char inkey;
long max;
long *count;
{
char qbuf[QBUFSZ];
int key;
long cnt = 0;
boolean backspaced = FALSE;
char *ret;
for (;;) {
if (inkey) {
key = inkey;
inkey = '\0';
} else
key = readchar();
if (digit(key)) {
cnt = 10L * cnt + (long)(key - '0');
} else if (key == '\b') {
cnt = cnt / 10;
backspaced = TRUE;
} else if (key == '\033') {
return '\033';
} else if (allowchars) {
if (ret = index(allowchars, key)) {
*count = cnt;
return *ret;
}
} else {
*count = cnt;
return key;
}
if (max && (cnt > max))
cnt = max;
if (cnt > 9 || backspaced) {
clear_nhwindow(WIN_MESSAGE);
if (backspaced && !cnt)
Sprintf(qbuf, "Count: ");
else {
Sprintf(qbuf, "Count: %d", cnt);
backspaced = FALSE;
}
pline1(qbuf);
mark_synch();
}
}
}
STATIC_OVL char *
parse()
{
@@ -3883,35 +3934,11 @@ parse()
#ifdef ALTMETA
alt_esc = iflags.altmeta; /* readchar() hack */
#endif
if (!Cmd.num_pad || (foo = readchar()) == 'n')
for (;;) {
foo = readchar();
if ((foo >= '0' && foo <= '9') || (foo == '\b')) {
if (foo >= '0' && foo <= '9')
multi = 10 * multi + foo - '0';
else {
multi = multi / 10;
backspaced = TRUE;
}
if (multi < 0 || multi >= LARGEST_INT)
multi = LARGEST_INT;
if (multi > 9 || backspaced) {
clear_nhwindow(WIN_MESSAGE);
if (backspaced && !multi)
Sprintf(in_line, "Count: ");
else {
Sprintf(in_line, "Count: %d", multi);
backspaced = FALSE;
}
pline1(in_line);
mark_synch();
}
last_multi = multi;
if (!multi && foo == '0')
prezero = TRUE;
} else
break; /* not a digit */
}
if (!Cmd.num_pad || (foo = readchar()) == 'n') {
long tmpmulti = multi;
foo = get_count(NULL, '\0', LARGEST_INT, &tmpmulti);
last_multi = multi = tmpmulti;
}
#ifdef ALTMETA
alt_esc = FALSE; /* readchar() reset */
#endif

View File

@@ -970,8 +970,8 @@ register const char *let, *word;
boolean allownone = FALSE;
boolean useboulder = FALSE;
xchar foox = 0;
long cnt, prevcnt;
boolean prezero;
long cnt;
boolean cntgiven = FALSE;
long dummymask;
if (*let == ALLOW_COUNT)
@@ -1163,9 +1163,7 @@ register const char *let, *word;
}
for (;;) {
cnt = 0;
if (allowcnt == 2)
allowcnt = 1; /* abort previous count */
prezero = FALSE;
cntgiven = FALSE;
if (!buf[0]) {
Sprintf(qbuf, "What do you want to %s? [*]", word);
} else {
@@ -1175,28 +1173,17 @@ register const char *let, *word;
ilet = readchar();
else
ilet = yn_function(qbuf, (char *) 0, '\0');
if (digit(ilet) && !allowcnt) {
pline("No count allowed with this command.");
continue;
}
if (ilet == '0')
prezero = TRUE;
while (digit(ilet)) {
if (ilet != '?' && ilet != '*')
savech(ilet);
/* accumulate unless cnt has overflowed */
if (allowcnt < 3) {
prevcnt = cnt;
cnt = 10L * cnt + (long) (ilet - '0');
/* signal presence of cnt */
allowcnt = (cnt >= prevcnt) ? 2 : 3;
if (digit(ilet)) {
long tmpcnt = 0;
if (!allowcnt) {
pline("No count allowed with this command.");
continue;
}
ilet = get_count(NULL, ilet, LARGEST_INT, &tmpcnt);
if (tmpcnt) {
cnt = tmpcnt;
cntgiven = TRUE;
}
ilet = readchar();
}
if (allowcnt == 3) {
/* overflow detected; force cnt to be invalid */
cnt = -1L;
allowcnt = 2;
}
if (index(quitchars, ilet)) {
if (flags.verbose)
@@ -1237,9 +1224,7 @@ register const char *let, *word;
continue;
if (allowcnt && ctmp >= 0) {
cnt = ctmp;
if (!cnt)
prezero = TRUE;
allowcnt = 2;
cntgiven = TRUE;
}
if (ilet == '\033') {
if (flags.verbose)
@@ -1267,23 +1252,21 @@ register const char *let, *word;
* to your money supply. The LRS is the tax bureau
* from Larn.
*/
if (allowcnt == 2 && cnt <= 0) {
if (cnt < 0 || !prezero)
if (cntgiven && cnt <= 0) {
if (cnt < 0)
pline_The(
"LRS would be very interested to know you have that much.");
return (struct obj *) 0;
}
}
if (allowcnt == 2 && !strcmp(word, "throw")) {
if (cntgiven && !strcmp(word, "throw")) {
/* permit counts for throwing gold, but don't accept
* counts for other things since the throw code will
* split off a single item anyway */
if (ilet != def_oc_syms[COIN_CLASS].sym
&& !(otmp && otmp->oclass == COIN_CLASS))
allowcnt = 1;
if (cnt == 0 && prezero)
if (cnt == 0)
return (struct obj *) 0;
if (cnt > 1) {
if (cnt > 1 && (ilet != def_oc_syms[COIN_CLASS].sym
&& !(otmp && otmp->oclass == COIN_CLASS))) {
You("can only throw one item at a time.");
continue;
}
@@ -1311,7 +1294,7 @@ register const char *let, *word;
silly_thing(word, otmp);
return (struct obj *) 0;
}
if (allowcnt == 2) { /* cnt given */
if (cntgiven) {
if (cnt == 0)
return (struct obj *) 0;
if (cnt != otmp->quan) {