Apply the new ground_level method.
[crawl.git] / crawl-ref / source / ng-input.cc
blob81d73a06295232ab59373425a8e18df6b4516614
1 #include "AppHdr.h"
3 #include "ng-input.h"
5 #include "cio.h"
6 #include "files.h"
7 #include "menu.h"
8 #include "options.h"
9 #include "stuff.h"
11 extern std::string init_file_error; // defined in main.cc
13 // Eventually, this should be something more grand. {dlb}
14 void opening_screen(void)
16 std::string msg =
17 "<yellow>Hello, welcome to " CRAWL " " + Version::Long() + "!</yellow>\n"
18 "<brown>(c) Copyright 1997-2002 Linley Henzell, "
19 "2002-2010 Crawl DevTeam\n"
20 "Read the instructions for legal details."
21 "</brown> " ;
23 const bool init_found = init_file_error.empty();
25 if (!init_found)
26 msg += "<lightred>(No init file ";
27 else
28 msg += "<lightgrey>(Read options from ";
30 if (init_found)
32 #ifdef DGAMELAUNCH
33 // For dgl installs, show only the last segment of the .crawlrc
34 // file name so that we don't leak details of the directory
35 // structure to (untrusted) users.
36 msg += get_base_filename(Options.filename);
37 #else
38 msg += Options.filename;
39 #endif
40 msg += ".)";
42 else
44 msg += init_file_error;
45 msg += ", using defaults.)";
48 msg += "\n";
50 formatted_string::parse_string(msg).display();
51 textcolor(LIGHTGREY);
54 static void _show_name_prompt(int where)
56 cgotoxy(1, where);
57 textcolor(CYAN);
59 cprintf("\nWhat is your name today? ");
61 textcolor(LIGHTGREY);
64 bool is_good_name(const std::string& name, bool blankOK, bool verbose)
66 // verification begins here {dlb}:
67 if (name.empty())
69 if (blankOK)
70 return (true);
72 if (verbose)
73 cprintf("\nThat's a silly name!\n");
74 return (false);
77 return (validate_player_name(name, verbose));
80 static bool _read_player_name(std::string &name)
82 const int name_x = wherex(), name_y = wherey();
83 char buf[kNameLen + 1];
84 // XXX: Prompt displays garbage otherwise, but don't really know why.
85 // Other places don't do this. --rob
86 buf[0] = '\0';
87 line_reader reader(buf, sizeof(buf));
89 while (true)
91 cgotoxy(name_x, name_y);
92 if (name_x <= 80)
93 cprintf("%-*s", 80 - name_x + 1, "");
95 cgotoxy(name_x, name_y);
96 int ret = reader.read_line(false);
97 if (!ret)
99 name = buf;
100 return (true);
103 if (key_is_escape(ret))
104 return (false);
106 // Go back and prompt the user.
110 // Reads a valid name from the player, writing it to ng.name.
111 void enter_player_name(newgame_def *ng)
113 int prompt_start = wherey();
117 // Prompt for a new name if current one unsatisfactory {dlb}:
118 _show_name_prompt(prompt_start);
120 // If the player wants out, we bail out.
121 if (!_read_player_name(ng->name))
122 end(0);
123 trim_string(ng->name);
125 while (!is_good_name(ng->name, false, true));
128 bool validate_player_name(const std::string &name, bool verbose)
130 #if defined(TARGET_OS_DOS) || defined(TARGET_OS_WINDOWS)
131 // Quick check for CON -- blows up real good under DOS/Windows.
132 if (stricmp(name.c_str(), "con") == 0
133 || stricmp(name.c_str(), "nul") == 0
134 || stricmp(name.c_str(), "prn") == 0
135 || strnicmp(name.c_str(), "LPT", 3) == 0)
137 if (verbose)
138 cprintf("\nSorry, that name gives your OS a headache.\n");
139 return (false);
141 #endif
143 if (strwidth(name) > kNameLen)
145 if (verbose)
146 cprintf("\nThat name is too long.\n");
147 return (false);
150 for (unsigned int i = 0; i < name.length(); i++)
152 char c = name[i];
153 // Note that this includes systems which may be using the
154 // packaging system. The packaging system is very simple
155 // and doesn't take the time to escape every character that
156 // might be a problem for some random shell or OS... so we
157 // play it very conservative here. -- bwr
158 // Accented 8-bit letters are probably harmless here. -- 1KB
159 if (!isalnum(c) && c != '-' && c != '.' && c != '_' && c != ' ')
161 if (verbose)
163 cprintf("\n"
164 "Alpha-numerics, spaces, dashes, periods "
165 "and underscores only, please."
166 "\n");
168 return (false);
172 return (true);