123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- /* $NetBSD: pl_main.c,v 1.16 2003/08/07 09:37:44 agc Exp $ */
- /*
- * Copyright (c) 1983, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- #include <sys/cdefs.h>
- #ifndef lint
- #if 0
- static char sccsid[] = "@(#)pl_main.c 8.1 (Berkeley) 5/31/93";
- #else
- __RCSID("$NetBSD: pl_main.c,v 1.16 2003/08/07 09:37:44 agc Exp $");
- #endif
- #endif /* not lint */
- #include <setjmp.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include "extern.h"
- #include "player.h"
- #include "restart.h"
- static void initialize(void);
- /*ARGSUSED*/
- int
- pl_main(void)
- {
- initialize();
- Msg("Aye aye, Sir");
- play();
- return 0; /* for lint, play() never returns */
- }
- static void
- initialize(void)
- {
- struct File *fp;
- struct ship *sp;
- char captain[80];
- char message[60];
- int load;
- int n;
- char *nameptr;
- int nat[NNATION];
- if (game < 0) {
- puts("Choose a scenario:\n");
- puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
- for (n = 0; n < NSCENE; n++) {
- /* ( */
- printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
- sync_exists(n) ? "YES" : "no",
- scene[n].name);
- }
- reprint:
- printf("\nScenario number? ");
- fflush(stdout);
- scanf("%d", &game);
- while (getchar() != '\n')
- ;
- }
- if (game < 0 || game >= NSCENE) {
- puts("Very funny.");
- exit(1);
- }
- cc = &scene[game];
- ls = SHIP(cc->vessels);
- for (n = 0; n < NNATION; n++)
- nat[n] = 0;
- foreachship(sp) {
- if (sp->file == NULL &&
- (sp->file = (struct File *)calloc(1, sizeof (struct File))) == NULL) {
- puts("OUT OF MEMORY");
- exit(1);
- }
- sp->file->index = sp - SHIP(0);
- sp->file->stern = nat[sp->nationality]++;
- sp->file->dir = sp->shipdir;
- sp->file->row = sp->shiprow;
- sp->file->col = sp->shipcol;
- }
- windspeed = cc->windspeed;
- winddir = cc->winddir;
- signal(SIGHUP, choke);
- signal(SIGINT, choke);
- hasdriver = sync_exists(game);
- if (sync_open() < 0) {
- perror("sail: syncfile");
- exit(1);
- }
- if (hasdriver) {
- puts("Synchronizing with the other players...");
- fflush(stdout);
- if (Sync() < 0)
- leave(LEAVE_SYNC);
- }
- for (;;) {
- foreachship(sp)
- if (sp->file->captain[0] == 0 && !sp->file->struck
- && sp->file->captured == 0)
- break;
- if (sp >= ls) {
- puts("All ships taken in that scenario.");
- foreachship(sp)
- free((char *)sp->file);
- sync_close(0);
- people = 0;
- goto reprint;
- }
- if (randomize) {
- player = sp - SHIP(0);
- } else {
- printf("%s\n\n", cc->name);
- foreachship(sp)
- printf(" %2d: %-10s %-15s (%-2d pts) %s\n",
- sp->file->index,
- countryname[sp->nationality],
- sp->shipname,
- sp->specs->pts,
- saywhat(sp, 1));
- printf("\nWhich ship (0-%d)? ", cc->vessels-1);
- fflush(stdout);
- if (scanf("%d", &player) != 1 || player < 0
- || player >= cc->vessels) {
- while (getchar() != '\n')
- ;
- puts("Say what?");
- player = -1;
- } else
- while (getchar() != '\n')
- ;
- }
- if (player < 0)
- continue;
- if (Sync() < 0)
- leave(LEAVE_SYNC);
- fp = SHIP(player)->file;
- if (fp->captain[0] || fp->struck || fp->captured != 0)
- puts("That ship is taken.");
- else
- break;
- }
- ms = SHIP(player);
- mf = ms->file;
- mc = ms->specs;
- Write(W_BEGIN, ms, 0, 0, 0, 0);
- if (Sync() < 0)
- leave(LEAVE_SYNC);
- signal(SIGCHLD, child);
- if (!hasdriver)
- switch (fork()) {
- case 0:
- longjmp(restart, MODE_DRIVER);
- /*NOTREACHED*/
- case -1:
- perror("fork");
- leave(LEAVE_FORK);
- break;
- default:
- hasdriver++;
- }
- printf("Your ship is the %s, a %d gun %s (%s crew).\n",
- ms->shipname, mc->guns, classname[mc->class],
- qualname[mc->qual]);
- if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
- strncpy(captain, nameptr, sizeof captain);
- else {
- printf("Your name, Captain? ");
- fflush(stdout);
- fgets(captain, sizeof captain, stdin);
- if (!*captain)
- strcpy(captain, "no name");
- else
- captain[strlen(captain) - 1] = '\0';
- }
- captain[sizeof captain - 1] = '\0';
- Writestr(W_CAPTAIN, ms, captain);
- for (n = 0; n < 2; n++) {
- char buf[10];
- printf("\nInitial broadside %s (grape, chain, round, double): ",
- n ? "right" : "left");
- fflush(stdout);
- scanf("%s", buf);
- switch (*buf) {
- case 'g':
- load = L_GRAPE;
- break;
- case 'c':
- load = L_CHAIN;
- break;
- case 'r':
- load = L_ROUND;
- break;
- case 'd':
- load = L_DOUBLE;
- break;
- default:
- load = L_ROUND;
- }
- if (n) {
- mf->loadR = load;
- mf->readyR = R_LOADED|R_INITIAL;
- } else {
- mf->loadL = load;
- mf->readyL = R_LOADED|R_INITIAL;
- }
- }
- initscreen();
- draw_board();
- snprintf(message, sizeof message, "Captain %s assuming command",
- captain);
- Writestr(W_SIGNAL, ms, message);
- newturn(0);
- }
|