Tue, 18 Sep 2018 15:02:08 +0200
adds unicode support
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2016 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
30 #include "terminal-chess.h"
31 #include "game.h"
32 #include "input.h"
33 #include "colors.h"
34 #include <string.h>
35 #include <time.h>
36 #include <getopt.h>
37 #include <locale.h>
39 int get_settings(int argc, char **argv, Settings *settings) {
40 char *valid;
41 unsigned long int time, port;
42 uint8_t timeunit = 60;
43 size_t len;
45 for (int opt ; (opt = getopt(argc, argv, "a:bc:hp:rsS:t:Uv")) != -1 ;) {
46 switch (opt) {
47 case 'c':
48 settings->continuepgn = optarg;
49 break;
50 case 'b':
51 settings->gameinfo.servercolor = BLACK;
52 break;
53 case 'r':
54 settings->gameinfo.servercolor = rand() & 1 ? WHITE : BLACK;
55 break;
56 case 's':
57 settings->singlemachine = 1;
58 break;
59 case 'S':
60 settings->analyzepgn = optarg;
61 break;
62 case 'U':
63 settings->unicode = 0;
64 break;
65 case 't':
66 case 'a':
67 len = strlen(optarg);
68 if (optarg[len-1] == 's') {
69 optarg[len-1] = '\0';
70 timeunit = 1;
71 }
73 if ((time = strtoul(optarg, &valid, 10))*timeunit > UINT16_MAX
74 || *valid != '\0') {
75 fprintf(stderr, "Specified time is invalid (%s)"
76 "- Maximum: 65535 seconds (1092 minutes)\n", optarg);
77 return 1;
78 } else {
79 settings->gameinfo.timecontrol = 1;
80 if (opt=='t') {
81 settings->gameinfo.time = timeunit * time;
82 } else {
83 settings->gameinfo.addtime = time;
84 }
85 }
86 break;
87 case 'p':
88 port = strtol(optarg, &valid, 10);
89 if (port < 1025 || port > 65535 || *valid != '\0') {
90 fprintf(stderr,
91 "Invalid port number (%s) - choose a number between "
92 "1025 and 65535\n",
93 optarg);
94 return 1;
95 } else {
96 settings->port = optarg;
97 }
98 break;
99 case 'v':
100 printf("terminal-chess : Version %s (Netcode Version %d)\n",
101 PROGRAM_VERSION, NETCODE_VERSION);
102 exit(0);
103 case 'h':
104 case '?':
105 printf(
106 "Usage: terminal-chess [OPTION]... [HOST]\n"
107 "Starts/joins a network chess game\n"
108 "\nGeneral options\n"
109 " -c <PGN file> Continue the specified game\n"
110 " -h This help page\n"
111 " -p TCP port to use (default: 27015)\n"
112 // TODO: implement and activate feature
113 //" -S <PGN file> Compute and print statistics for the specified game\n"
114 " -v Print version information and exists\n"
115 "\nServer options\n"
116 " -a <time> Specifies the time to add after each move\n"
117 " -b Server plays black pieces (default: white)\n"
118 " -r Distribute color randomly\n"
119 " -s Single machine mode\n"
120 " -t <time> Specifies time limit (default: no limit)\n"
121 " -U Disables unicode pieces\n"
122 "\nNotes\n"
123 "The time unit for -a is seconds and for -t minutes by default. To "
124 "specify\nseconds for the -t option, use the s suffix.\n"
125 "Example: -t 150s\n\n"
126 "Use '-' for PGN files to read PGN data from standard input\n"
127 );
128 exit(0);
129 }
130 }
132 if (optind == argc - 1) {
133 settings->serverhost = argv[optind];
134 } else if (optind < argc - 1) {
135 fprintf(stderr, "Too many arguments\n");
136 return 1;
137 }
140 if (settings->continuepgn) {
141 if (settings->serverhost) {
142 fprintf(stderr, "Can't continue a game when joining a server.\n");
143 return 1;
144 }
145 if (settings->analyzepgn) {
146 fprintf(stderr, "The options -c and -S are mutually exclusive\n");
147 return 1;
148 }
149 }
151 return 0;
152 }
154 static Settings default_settings() {
155 Settings settings;
156 memset(&settings, 0, sizeof(Settings));
157 settings.gameinfo.servercolor = WHITE;
158 settings.port = "27015";
159 settings.unicode = !!setlocale(LC_CTYPE, "C.UTF-8");
160 return settings;
161 }
163 void dump_gameinfo(GameInfo *gameinfo) {
164 int serverwhite = gameinfo->servercolor == WHITE;
165 attron(A_UNDERLINE);
166 printw("Game details\n");
167 attroff(A_UNDERLINE);
168 printw(" Server: %s\n Client: %s\n",
169 serverwhite?"White":"Black", serverwhite?"Black":"White"
170 );
171 if (gameinfo->timecontrol) {
172 if (gameinfo->time % 60) {
173 printw(" Time limit: %ds + %ds\n",
174 gameinfo->time, gameinfo->addtime);
175 } else {
176 printw(" Time limit: %dm + %ds\n",
177 gameinfo->time/60, gameinfo->addtime);
178 }
179 } else {
180 printw(" No time limit\n");
181 }
182 refresh();
183 }
185 void dump_moveinfo(GameState *gamestate) {
186 int i = 1;
187 for (MoveList *movelist = gamestate->movelist ;
188 movelist ; movelist = movelist->next) {
189 if (++i % 2 == 0) {
190 printw("%d. %s", i/2, movelist->move.string);
191 } else {
192 printw(" %s", movelist->move.string);
193 }
194 if (i % 20) {
195 addch(' ');
196 } else {
197 addch('\n');
198 }
199 }
200 refresh();
201 }
203 int main(int argc, char **argv) {
204 srand(time(NULL));
206 Settings settings = default_settings();
207 if (get_settings(argc, argv, &settings)) {
208 return 1;
209 }
211 initscr();
212 halfdelay(1);
213 keypad(stdscr, TRUE);
214 if (has_colors()) {
215 start_color();
216 init_colorpairs();
217 bkgd(COLOR_PAIR(COL_APP));
218 } else {
219 fprintf(stderr, "Non-colored terminals are not supported yet.");
220 endwin();
221 return EXIT_FAILURE;
222 }
223 atexit((void(*)())endwin);
225 int exitcode;
226 if (settings.singlemachine) {
227 game_start_singlemachine(&settings);
228 exitcode = EXIT_SUCCESS;
229 } else if (settings.analyzepgn) {
230 printw("Not implemented yet.\n");
231 exitcode = EXIT_SUCCESS;
232 } else {
233 exitcode = is_server(&settings) ?
234 server_run(&settings) : client_run(&settings);
235 }
237 mvaddstr(getmaxy(stdscr)-1, 0,
238 "Game has ended. Press any key to leave...");
239 clrtoeol();
240 refresh();
241 cbreak();
242 flushinp();
243 getch();
245 return exitcode;
246 }