Wed, 09 Apr 2014 18:11:51 +0200
tried to improve colors
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2014 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>
38 int get_settings(int argc, char **argv, Settings *settings) {
39 char *valid;
40 unsigned long int time, port;
41 uint8_t timeunit = 60;
42 size_t len;
44 for (int opt ; (opt = getopt(argc, argv, "a:bhp:rst:")) != -1 ;) {
45 switch (opt) {
46 case 'b':
47 settings->gameinfo.servercolor = BLACK;
48 break;
49 case 'r':
50 settings->gameinfo.servercolor = rand() & 1 ? WHITE : BLACK;
51 break;
52 case 's':
53 settings->singlemachine = 1;
54 break;
55 case 't':
56 case 'a':
57 len = strlen(optarg);
58 if (optarg[len-1] == 's') {
59 optarg[len-1] = '\0';
60 timeunit = 1;
61 }
63 if ((time = strtoul(optarg, &valid, 10))*timeunit > UINT16_MAX
64 || *valid != '\0') {
65 fprintf(stderr, "Specified time is invalid (%s)"
66 "- Maximum: 65535 seconds (1092 minutes)\n", optarg);
67 return 1;
68 } else {
69 settings->gameinfo.timecontrol = 1;
70 if (opt=='t') {
71 settings->gameinfo.time = timeunit * time;
72 } else {
73 settings->gameinfo.addtime = time;
74 }
75 }
76 break;
77 case 'p':
78 port = strtol(optarg, &valid, 10);
79 if (port < 1025 || port > 65535 || *valid != '\0') {
80 fprintf(stderr,
81 "Invalid port number (%s) - choose a number between "
82 "1025 and 65535\n",
83 optarg);
84 return 1;
85 } else {
86 settings->port = optarg;
87 }
88 break;
89 case 'h':
90 case '?':
91 settings->printhelp = 1;
92 break;
93 }
94 }
96 if (optind == argc - 1) {
97 settings->serverhost = argv[optind];
98 } else if (optind < argc - 1) {
99 fprintf(stderr, "Too many arguments\n");
100 return 1;
101 }
103 return 0;
104 }
106 Settings default_settings() {
107 Settings settings;
108 memset(&settings, 0, sizeof(Settings));
109 settings.gameinfo.servercolor = WHITE;
110 settings.port = "27015";
111 return settings;
112 }
114 void dump_gameinfo(GameInfo *gameinfo) {
115 int serverwhite = gameinfo->servercolor == WHITE;
116 attron(A_UNDERLINE);
117 printw("Game details\n");
118 attroff(A_UNDERLINE);
119 printw(" Server: %s\n Client: %s\n",
120 serverwhite?"White":"Black", serverwhite?"Black":"White"
121 );
122 if (gameinfo->timecontrol) {
123 if (gameinfo->time % 60) {
124 printw(" Time limit: %ds + %ds\n",
125 gameinfo->time, gameinfo->addtime);
126 } else {
127 printw(" Time limit: %dm + %ds\n",
128 gameinfo->time/60, gameinfo->addtime);
129 }
130 } else {
131 printw(" No time limit\n");
132 }
133 refresh();
134 }
136 void leavescr() {
137 endwin();
138 }
140 int main(int argc, char **argv) {
141 srand(time(NULL));
143 Settings settings = default_settings();
144 if (get_settings(argc, argv, &settings)) {
145 return 1;
146 }
148 if (settings.printhelp) {
149 printf(
150 "Usage: terminal-chess [OPTION]... [HOST]\n"
151 "Starts/joins a network chess game\n"
152 "\nGeneral options\n"
153 " -h This help page\n"
154 " -p TCP port to use (default: 27015)\n"
155 "\nServer options\n"
156 " -a <time> Specifies the time to add after each move\n"
157 " -b Server plays black pieces (default: white)\n"
158 " -r Distribute color randomly\n"
159 " -s Single machine mode\n"
160 " -t <time> Specifies time limit (default: no limit)\n"
161 "\nNotes\n"
162 "The time unit for -a is seconds and for -t minutes by default. To "
163 "specify\nseconds for the -t option, use the s suffix.\n"
164 "Example: -t 150s\n"
165 );
166 return EXIT_SUCCESS;
167 }
168 initscr();
169 halfdelay(1);
170 keypad(stdscr, TRUE);
171 if (has_colors()) {
172 start_color();
173 init_colorpairs();
174 bkgd(COLOR_PAIR(COL_APP));
175 } else {
176 fprintf(stderr, "Non-colored terminals are not supported yet.");
177 endwin();
178 return EXIT_FAILURE;
179 }
180 atexit(leavescr);
182 if (settings.singlemachine) {
183 game_start_singlemachine(&settings);
184 } else {
185 int exitcode = is_server(&settings) ?
186 server_run(&settings) : client_run(&settings);
188 if (exitcode != EXIT_SUCCESS) {
189 cbreak(); getch();
190 }
191 }
192 }