| 1 | #ifndef lint |
| 2 | static char *sccsid = "@(#)pl_1.c 2.3 83/11/02"; |
| 3 | #endif |
| 4 | |
| 5 | #include "player.h" |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/wait.h> |
| 8 | |
| 9 | int choke(), child(); |
| 10 | |
| 11 | static char hasdriver; |
| 12 | |
| 13 | /*ARGSUSED*/ |
| 14 | main(argc, argv) |
| 15 | int argc; |
| 16 | char **argv; |
| 17 | { |
| 18 | char nodrive = 0, randomize = 0, debug = 0; |
| 19 | extern char _sobuf[]; |
| 20 | |
| 21 | setbuf(stdout, _sobuf); |
| 22 | isplayer = 1; |
| 23 | |
| 24 | while (*++argv && **argv == '-') |
| 25 | switch (*++*argv) { |
| 26 | case 'd': |
| 27 | nodrive = 1; |
| 28 | break; |
| 29 | case 'D': |
| 30 | debug++; |
| 31 | break; |
| 32 | case 'x': |
| 33 | randomize = 1; |
| 34 | break; |
| 35 | default: |
| 36 | printf("Unknown flag '%s'\n",*argv); |
| 37 | break; |
| 38 | } |
| 39 | if (*argv) |
| 40 | game = atoi(*argv); |
| 41 | else |
| 42 | game = -1; |
| 43 | initialize(nodrive, randomize, debug); |
| 44 | Signal("Aye aye, Sir", (struct ship *)0); |
| 45 | play(); |
| 46 | } |
| 47 | |
| 48 | initialize(nodriver, randomize, debug) |
| 49 | char randomize, nodriver, debug; |
| 50 | { |
| 51 | register struct File *fp; |
| 52 | register struct ship *sp; |
| 53 | char captain[80]; |
| 54 | char message[60]; |
| 55 | int load; |
| 56 | register int n; |
| 57 | char *nameptr; |
| 58 | int nat[NNATION]; |
| 59 | |
| 60 | (void) srand(getpid()); |
| 61 | |
| 62 | if (game < 0) { |
| 63 | (void) puts("Choose a scenario:\n"); |
| 64 | (void) puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE"); |
| 65 | for (n = 0; n < NSCENE; n++) { |
| 66 | /* ( */ |
| 67 | printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels, |
| 68 | sync_exists(n) ? "YES" : "no", |
| 69 | scene[n].name); |
| 70 | } |
| 71 | reprint: |
| 72 | printf("\nScenario number? "); |
| 73 | (void) fflush(stdout); |
| 74 | (void) scanf("%d", &game); |
| 75 | while (getchar() != '\n') |
| 76 | ; |
| 77 | } |
| 78 | if (game < 0 || game >= NSCENE) { |
| 79 | (void) puts("Very funny."); |
| 80 | exit(1); |
| 81 | } |
| 82 | cc = &scene[game]; |
| 83 | ls = cc->ship + cc->vessels; |
| 84 | |
| 85 | for (n = 0; n < NNATION; n++) |
| 86 | nat[n] = 0; |
| 87 | foreachship(sp) { |
| 88 | sp->file = (struct File *) calloc(1, sizeof (struct File)); |
| 89 | if (sp->file == NULL) { |
| 90 | (void) puts("OUT OF MEMORY"); |
| 91 | exit(0); |
| 92 | } |
| 93 | sp->file->stern = nat[sp->nationality]++; |
| 94 | sp->file->dir = sp->shipdir; |
| 95 | sp->file->row = sp->shiprow; |
| 96 | sp->file->col = sp->shipcol; |
| 97 | } |
| 98 | windspeed = cc->windspeed; |
| 99 | winddir = cc->winddir; |
| 100 | |
| 101 | (void) signal(SIGHUP, choke); |
| 102 | (void) signal(SIGINT, choke); |
| 103 | (void) signal(SIGQUIT, choke); |
| 104 | |
| 105 | hasdriver = sync_exists(game); |
| 106 | if (sync_open() < 0) { |
| 107 | perror("sail: syncfile"); |
| 108 | exit(1); |
| 109 | } |
| 110 | |
| 111 | if (hasdriver) { |
| 112 | (void) puts("Synchronizing with the other players..."); |
| 113 | (void) fflush(stdout); |
| 114 | Sync(); |
| 115 | } |
| 116 | for (;;) { |
| 117 | foreachship(sp) |
| 118 | if (sp->file->captain[0] == 0 && !sp->file->struck |
| 119 | && sp->file->captured == 0) |
| 120 | break; |
| 121 | if (sp >= ls) { |
| 122 | (void) puts("All ships taken in that scenario."); |
| 123 | foreachship(sp) |
| 124 | free((char *)sp->file); |
| 125 | sync_close(0); |
| 126 | people = 0; |
| 127 | goto reprint; |
| 128 | } |
| 129 | if (randomize) { |
| 130 | player = sp - cc->ship; |
| 131 | } else { |
| 132 | printf("%s\n\n", cc->name); |
| 133 | foreachship(sp) |
| 134 | printf(" %2d: %-10s %-15s (%-2d pts) %s\n", |
| 135 | sp - SHIP(0), |
| 136 | countryname[sp->nationality], |
| 137 | sp->shipname, |
| 138 | sp->specs->pts, |
| 139 | saywhat(sp, 1)); |
| 140 | printf("\nWhich ship (0-%d)? ", cc->vessels-1); |
| 141 | (void) fflush(stdout); |
| 142 | if (scanf("%d", &player) != 1 || player < 0 |
| 143 | || player >= cc->vessels) { |
| 144 | while (getchar() != '\n') |
| 145 | ; |
| 146 | (void) puts("Say what?"); |
| 147 | player = -1; |
| 148 | } else |
| 149 | while (getchar() != '\n') |
| 150 | ; |
| 151 | } |
| 152 | if (player < 0) |
| 153 | continue; |
| 154 | Sync(); |
| 155 | fp = SHIP(player)->file; |
| 156 | if (fp->captain[0] || fp->struck || fp->captured != 0) |
| 157 | (void) puts("That ship is taken."); |
| 158 | else |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | ms = SHIP(player); |
| 163 | mf = ms->file; |
| 164 | mc = ms->specs; |
| 165 | |
| 166 | Write(W_BEGIN, ms, 0, 0, 0, 0, 0); |
| 167 | Sync(); |
| 168 | |
| 169 | (void) signal(SIGCHLD, child); |
| 170 | if (!hasdriver && !nodriver) { |
| 171 | char num[10]; |
| 172 | (void) sprintf(num, "%d", game); |
| 173 | switch (fork()) { |
| 174 | case 0: |
| 175 | execl(DRIVER1, DRIVERNAME, num, 0); |
| 176 | execl(DRIVER2, DRIVERNAME, num, 0); |
| 177 | execl(DRIVER3, DRIVERNAME, num, 0); |
| 178 | perror(DRIVERNAME); |
| 179 | exit(1); |
| 180 | break; |
| 181 | case -1: |
| 182 | perror("fork"); |
| 183 | leave(LEAVE_QUIT); |
| 184 | break; |
| 185 | default: |
| 186 | hasdriver++; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | printf("Your ship is the %s, a %d gun %s (%s crew).\n", |
| 191 | ms->shipname, mc->guns, classname[mc->class], |
| 192 | qualname[mc->qual]); |
| 193 | if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr) |
| 194 | (void) strncpy(captain, nameptr, sizeof captain); |
| 195 | else { |
| 196 | (void) printf("Your name, Captain? "); |
| 197 | (void) fflush(stdout); |
| 198 | (void) gets(captain); |
| 199 | if (!*captain) |
| 200 | (void) strcpy(captain, "no name"); |
| 201 | } |
| 202 | captain[sizeof captain - 1] = '\0'; |
| 203 | Write(W_CAPTAIN, ms, 1, (int)captain, 0, 0, 0); |
| 204 | for (n = 0; n < 2; n++) { |
| 205 | char buf[10]; |
| 206 | |
| 207 | printf("\nInitial broadside %s (grape, chain, round, double): ", |
| 208 | n ? "right" : "left"); |
| 209 | (void) fflush(stdout); |
| 210 | (void) scanf("%s", buf); |
| 211 | switch (*buf) { |
| 212 | case 'g': |
| 213 | load = L_GRAPE; |
| 214 | break; |
| 215 | case 'c': |
| 216 | load = L_CHAIN; |
| 217 | break; |
| 218 | case 'r': |
| 219 | load = L_ROUND; |
| 220 | break; |
| 221 | case 'd': |
| 222 | load = L_DOUBLE; |
| 223 | break; |
| 224 | default: |
| 225 | load = L_ROUND; |
| 226 | } |
| 227 | if (n) { |
| 228 | mf->loadR = load; |
| 229 | mf->readyR = R_LOADED|R_INITIAL; |
| 230 | } else { |
| 231 | mf->loadL = load; |
| 232 | mf->readyL = R_LOADED|R_INITIAL; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | initscreen(); |
| 237 | draw_board(); |
| 238 | (void) sprintf(message, "Captain %s assuming command", captain); |
| 239 | Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0); |
| 240 | newturn(); |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * If we get here before a ship is chosen, then ms == 0 and |
| 245 | * we don't want to update the score file, or do any Write's either. |
| 246 | * We can assume the sync file is already created and may need |
| 247 | * to be removed. |
| 248 | */ |
| 249 | leave(conditions) |
| 250 | int conditions; |
| 251 | { |
| 252 | FILE *fp; |
| 253 | int persons; |
| 254 | float net; |
| 255 | char message[60]; |
| 256 | register int n; |
| 257 | struct logs log[10], temp; |
| 258 | |
| 259 | (void) signal(SIGHUP, SIG_IGN); |
| 260 | (void) signal(SIGINT, SIG_IGN); |
| 261 | (void) signal(SIGQUIT, SIG_IGN); |
| 262 | (void) signal(SIGALRM, SIG_IGN); |
| 263 | (void) signal(SIGCHLD, SIG_IGN); |
| 264 | |
| 265 | if (done_curses) { |
| 266 | Signal("It looks like you've had it!", |
| 267 | (struct ship *)0); |
| 268 | switch (conditions) { |
| 269 | case LEAVE_QUIT: |
| 270 | break; |
| 271 | case LEAVE_CAPTURED: |
| 272 | Signal("Your ship was captured.", |
| 273 | (struct ship *)0); |
| 274 | break; |
| 275 | case LEAVE_HURRICAN: |
| 276 | Signal("Hurricane! All ships destroyed.", |
| 277 | (struct ship *)0); |
| 278 | break; |
| 279 | case LEAVE_DRIVER: |
| 280 | /* don't clear 'hasdriver' here */ |
| 281 | Signal("The driver died.", (struct ship *)0); |
| 282 | break; |
| 283 | default: |
| 284 | Signal("A funny thing happened (%d).", |
| 285 | (struct ship *)0, conditions); |
| 286 | } |
| 287 | } else { |
| 288 | switch (conditions) { |
| 289 | case LEAVE_QUIT: |
| 290 | break; |
| 291 | case LEAVE_DRIVER: |
| 292 | printf("The driver died.\n"); |
| 293 | break; |
| 294 | case LEAVE_FORK: |
| 295 | hasdriver = 0; |
| 296 | printf("Can't fork.\n"); |
| 297 | break; |
| 298 | default: |
| 299 | printf("A funny thing happened (%d).\n", |
| 300 | conditions); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (ms != 0) { |
| 305 | if (fp = fopen(LOGFILE, "r+")) { |
| 306 | net = (float)mf->points / mc->pts; |
| 307 | persons = getw(fp); |
| 308 | n = fread((char *)log, sizeof(struct logs), 10, fp); |
| 309 | for (; n < 10; n++) |
| 310 | log[n].l_name[0] |
| 311 | = log[n].l_uid |
| 312 | = log[n].l_shipnum |
| 313 | = log[n].l_gamenum |
| 314 | = log[n].l_netpoints = 0; |
| 315 | rewind(fp); |
| 316 | if (persons < 0) |
| 317 | (void) putw(1, fp); |
| 318 | else |
| 319 | (void) putw(persons + 1, fp); |
| 320 | for (n = 0; n < 10; n++) |
| 321 | if (net > (float) log[n].l_netpoints / scene[log[n].l_gamenum].ship[log[n].l_shipnum].specs->pts) { |
| 322 | (void) fwrite((char *)log, |
| 323 | sizeof (struct logs), n, fp); |
| 324 | (void) strcpy(temp.l_name, mf->captain); |
| 325 | temp.l_uid = getuid(); |
| 326 | temp.l_shipnum = player; |
| 327 | temp.l_gamenum = game; |
| 328 | temp.l_netpoints = mf->points; |
| 329 | (void) fwrite((char *)&temp, |
| 330 | sizeof temp, 1, fp); |
| 331 | (void) fwrite((char *)&log[n], |
| 332 | sizeof (struct logs), 9-n, fp); |
| 333 | break; |
| 334 | } |
| 335 | (void) fclose(fp); |
| 336 | } |
| 337 | makesignal(ms, "Captain %s relinquishing.", (struct ship *)0, |
| 338 | mf->captain); |
| 339 | Write(W_END, ms, 0, 0, 0, 0, 0); |
| 340 | Sync(); |
| 341 | } |
| 342 | sync_close(!hasdriver); |
| 343 | cleanupscreen(); |
| 344 | exit(0); |
| 345 | } |
| 346 | |
| 347 | choke() |
| 348 | { |
| 349 | leave(LEAVE_QUIT); |
| 350 | } |
| 351 | |
| 352 | child() |
| 353 | { |
| 354 | union wait status; |
| 355 | int pid; |
| 356 | |
| 357 | (void) signal(SIGCHLD, SIG_IGN); |
| 358 | do { |
| 359 | pid = wait3(&status, WNOHANG|WUNTRACED, (struct rusage *)0); |
| 360 | if (pid < 0 || pid > 0 && !WIFSTOPPED(status)) |
| 361 | leave(LEAVE_DRIVER); |
| 362 | } while (pid != 0); |
| 363 | (void) signal(SIGCHLD, child); |
| 364 | } |