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