src/main.c

Tue, 18 Sep 2018 15:02:08 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 18 Sep 2018 15:02:08 +0200
changeset 69
c8f2c280cff7
parent 55
54ea19938d57
permissions
-rw-r--r--

adds unicode support

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

mercurial