restore_menu (trunk only)

Move the code to use a nethack menu for restoring a saved game.  It
was inline in tty_askname() but is now a separate routine, restore_menu()
in restore.c.  There was no port-specific code and only a small amount of
tty-specific code; it should be useable by anyone (but Qt doesn't have to
switch over if it doesn't want to).

     The original behaved strangely if there were exactly 26 saved chars;
the "start new game" menu entry ended up using "{" as selection character.
There wasn't any comparable problem at 52; it was limiting the menu to 51
games.  Now it will allow 52 (with "start a new game" bumped into "#" if
there are that many), and adds an explicit quit entry (unless there are
52 or more games so that # is already used by new-game, then quit remains
implicit rather than resort to some other none-of-the-above character).
This commit is contained in:
nethack.rankin
2006-12-12 04:52:39 +00:00
parent 176a59bb0a
commit 0db19f1c96
4 changed files with 113 additions and 61 deletions

View File

@@ -679,7 +679,6 @@ E char *FDECL(fname_encode, (const char *, CHAR_P, char *, char *, int));
E char *FDECL(fname_decode, (CHAR_P, char *, char *, int));
E const char *FDECL(fqname, (const char *, int, int));
E FILE *FDECL(fopen_datafile, (const char *,const char *,int));
E void FDECL(store_version, (int));
#ifdef MFLOPPY
E void NDECL(set_lock_and_bones);
#endif
@@ -1859,6 +1858,9 @@ E int FDECL(dorecover, (int));
E void FDECL(trickery, (char *));
E void FDECL(getlev, (int,int,XCHAR_P,BOOLEAN_P));
E void FDECL(get_plname_from_file, (int, char *));
#ifdef SELECTSAVED
E int FDECL(restore_menu, (winid));
#endif
E void NDECL(minit);
E boolean FDECL(lookup_id_mapping, (unsigned, unsigned *));
E void FDECL(mread, (int,genericptr_t,unsigned int));
@@ -2331,12 +2333,11 @@ E boolean FDECL(comp_times, (long));
#endif
E boolean FDECL(check_version, (struct version_info *,
const char *,BOOLEAN_P));
E boolean FDECL(uptodate, (int,const char *));
E void FDECL(store_version, (int));
E unsigned long FDECL(get_feature_notice_ver, (char *));
E unsigned long NDECL(get_current_feature_ver);
#ifdef RUNTIME_PORT_ID
E void FDECL(append_port_id, (char *));
#endif
E boolean FDECL(uptodate, (int,const char *));
E const char *FDECL(copyright_banner_line, (int));
/* ### video.c ### */

View File

@@ -1,4 +1,4 @@
/* SCCS Id: @(#)restore.c 3.5 2006/04/14 */
/* SCCS Id: @(#)restore.c 3.5 2006/12/11 */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */
@@ -1229,6 +1229,82 @@ boolean ghostly;
}
}
#ifdef SELECTSAVED
/* put up a menu listing each character from this player's saved games */
int
restore_menu(bannerwin) /* returns 1: use plname[], 0: new game, -1: quit */
winid bannerwin; /* if not WIN_ERR, clear window and show copyright in menu */
{
winid tmpwin;
anything any;
char **saved;
menu_item *chosen_game = (menu_item *)0;
int k, clet, ch = 0; /* ch: 0 => new game */
*plname = '\0';
saved = get_saved_games(); /* array of character names */
if (saved && *saved) {
tmpwin = create_nhwindow(NHW_MENU);
start_menu(tmpwin);
any = zeroany; /* no selection */
if (bannerwin != WIN_ERR) {
/* for tty; erase copyright notice and redo it in the menu */
clear_nhwindow(bannerwin);
/* COPYRIGHT_BANNER_A, COPYRIGHT_BANNER_B, COPYRIGHT_BANNER_C */
for (k = 1; k <= 3; ++k)
add_menu(tmpwin, NO_GLYPH, &any, 0, 0,
ATR_NONE, copyright_banner_line(k), MENU_UNSELECTED);
add_menu(tmpwin, NO_GLYPH, &any, 0, 0,
ATR_NONE, "", MENU_UNSELECTED);
}
add_menu(tmpwin, NO_GLYPH, &any, 0, 0,
ATR_NONE, "Select one of your saved games", MENU_UNSELECTED);
/* limited to 52 character entries, a-zA-Z, plus new game (as '#'
if necessary) and quit (omitted if new game ended up as '#') */
for (k = 0, clet = 'a'; saved[k] && k < 52; ++k) {
any.a_int = k + 1;
add_menu(tmpwin, NO_GLYPH, &any, clet, 0,
ATR_NONE, saved[k], MENU_UNSELECTED);
if (clet == 'z') clet = 'A';
else if (clet == 'Z') clet = '#'; /* use NOINVSYM for overflow */
else ++clet;
}
if (clet >= 'a' && clet < 'n') clet = 'n'; /* new game */
any.a_int = -1; /* not >= 0 */
add_menu(tmpwin, NO_GLYPH, &any, clet, 0,
ATR_NONE, "Start a new character", MENU_UNSELECTED);
/* quit entry is preselected, but omitted if there's no room */
if (clet != '#') {
if (clet >= 'a' && clet < 'q') clet = 'q'; /* quit */
else if (clet == 'z') clet = 'A';
else if (clet == 'Z') clet = '#';
else ++clet;
any.a_int = -2;
add_menu(tmpwin, NO_GLYPH, &any, clet, 0,
ATR_NONE, "Never mind (quit)", MENU_SELECTED);
}
/* no prompt on end_menu, as we've done our own at the top */
end_menu(tmpwin, (char *)0);
if (select_menu(tmpwin, PICK_ONE, &chosen_game) > 0) {
ch = chosen_game->item.a_int;
if (ch > 0) Strcpy(plname, saved[ch - 1]);
else if (ch < 0) ++ch; /* -1 -> 0 (new game), -2 -> -1 (quit) */
free((genericptr_t)chosen_game);
} else {
ch = -1; /* quit menu without making a selection => quit */
}
destroy_nhwindow(tmpwin);
if (bannerwin != WIN_ERR) {
/* for tty; clear the menu away and put subset of copyright back */
clear_nhwindow(bannerwin);
/* COPYRIGHT_BANNER_A, preceding "Who are you?" prompt */
if (ch == 0) putstr(bannerwin, 0, copyright_banner_line(1));
}
}
free_saved_games(saved);
return (ch > 0) ? 1 : ch;
}
#endif /* SELECTSAVED */
void
minit()

View File

@@ -1,4 +1,4 @@
/* SCCS Id: @(#)version.c 3.5 2003/11/22 */
/* SCCS Id: @(#)version.c 3.5 2006/12/11 */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */
@@ -183,4 +183,24 @@ get_current_feature_ver()
return FEATURE_NOTICE_VER(VERSION_MAJOR,VERSION_MINOR,PATCHLEVEL);
}
/*ARGUSED*/
const char *
copyright_banner_line(indx)
int indx;
{
#ifdef COPYRIGHT_BANNER_A
if (indx == 1) return COPYRIGHT_BANNER_A;
#endif
#ifdef COPYRIGHT_BANNER_B
if (indx == 2) return COPYRIGHT_BANNER_B;
#endif
#ifdef COPYRIGHT_BANNER_C
if (indx == 3) return COPYRIGHT_BANNER_C;
#endif
#ifdef COPYRIGHT_BANNER_D
if (indx == 4) return COPYRIGHT_BANNER_D;
#endif
return "";
}
/*version.c*/

View File

@@ -1,4 +1,4 @@
/* SCCS Id: @(#)wintty.c 3.5 2005/01/09 */
/* SCCS Id: @(#)wintty.c 3.5 2006/12/11 */
/* Copyright (c) David Cohrs, 1991 */
/* NetHack may be freely redistributed. See license for details. */
@@ -697,62 +697,17 @@ give_up: /* Quit */
void
tty_askname()
{
static char who_are_you[] = "Who are you? ";
static const char who_are_you[] = "Who are you? ";
register int c, ct, tryct = 0;
#ifdef SELECTSAVED
# if defined(WIN32CON) || defined(VMS)
int ch = -2;
char** saved = (char **)0;
if (iflags.wc2_selectsaved)
saved = get_saved_games();
if (saved && *saved) {
int k, clet = 'a';
winid tmpwin;
anything any;
menu_item *chosen_game = (menu_item *)0;
ch = -1;
tty_clear_nhwindow(BASE_WINDOW);
tmpwin = create_nhwindow(NHW_MENU);
start_menu(tmpwin);
any = zeroany; /* no selection */
add_menu(tmpwin, NO_GLYPH, &any, 0, 0,
ATR_NONE, COPYRIGHT_BANNER_A, MENU_UNSELECTED);
add_menu(tmpwin, NO_GLYPH, &any, 0, 0,
ATR_NONE, COPYRIGHT_BANNER_B, MENU_UNSELECTED);
add_menu(tmpwin, NO_GLYPH, &any, 0, 0,
ATR_NONE, COPYRIGHT_BANNER_C, MENU_UNSELECTED);
add_menu(tmpwin, NO_GLYPH, &any, 0, 0,
ATR_NONE, "", MENU_UNSELECTED);
add_menu(tmpwin, NO_GLYPH, &any, 0, 0,
ATR_NONE, "Select one of your saved games", MENU_UNSELECTED);
for (k = 0; saved[k]; ++k) {
if (clet == 'z' + 1) clet = 'A';
if (clet == 'Z') break;
any.a_int = k + 1;
add_menu(tmpwin, NO_GLYPH, &any, clet++, 0,
ATR_NONE, saved[k], MENU_UNSELECTED);
}
any.a_int = -2;
add_menu(tmpwin, NO_GLYPH, &any, clet, 0,
ATR_NONE, "Start a new character", MENU_UNSELECTED);
/* no prompt on end_menu, as we've done our own at the top */
end_menu(tmpwin, (char *)0);
if (select_menu(tmpwin, PICK_ONE, &chosen_game) > 0) {
ch = chosen_game->item.a_int;
if (ch > 0) {
ch--;
strcpy(plname,saved[ch]);
}
free((genericptr_t)chosen_game);
}
destroy_nhwindow(tmpwin);
}
free_saved_games(saved);
if (ch >= 0) return;
if (ch == -1) bail("Until next time then...");
# endif /* WIN32CON */
switch (restore_menu(BASE_WINDOW)) {
case -1: bail("Until next time then..."); /* quit */
/*NOTREACHED*/
case 0: break; /* no game chosen; start new game */
case 1: return; /* plname[] has been set */
}
#endif /* SELECTSAVED */
tty_putstr(BASE_WINDOW, 0, "");