Home | History | Annotate | Line # | Download | only in sail
pl_main.c revision 1.11
      1 /*	$NetBSD: pl_main.c,v 1.11 2001/01/04 02:43:33 jwise Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     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  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)pl_main.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: pl_main.c,v 1.11 2001/01/04 02:43:33 jwise Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include "player.h"
     46 #include <sys/types.h>
     47 #include <sys/wait.h>
     48 #include <stdlib.h>
     49 #include <unistd.h>
     50 
     51 int	pl_main(void);
     52 static void	initialize(void);
     53 
     54 /*ARGSUSED*/
     55 int
     56 pl_main(void)
     57 {
     58 
     59 	initialize();
     60 	Msg("Aye aye, Sir");
     61 	play();
     62 	return 0;			/* for lint,  play() never returns */
     63 }
     64 
     65 static void
     66 initialize(void)
     67 {
     68 	struct File *fp;
     69 	struct ship *sp;
     70 	char captain[80];
     71 	char message[60];
     72 	int load;
     73 	int n;
     74 	char *nameptr;
     75 	int nat[NNATION];
     76 
     77 	if (game < 0) {
     78 		puts("Choose a scenario:\n");
     79 		puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
     80 		for (n = 0; n < NSCENE; n++) {
     81 			/* ( */
     82 			printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
     83 				sync_exists(n) ? "YES" : "no",
     84 				scene[n].name);
     85 		}
     86 reprint:
     87 		printf("\nScenario number? ");
     88 		fflush(stdout);
     89 		scanf("%d", &game);
     90 		while (getchar() != '\n')
     91 			;
     92 	}
     93 	if (game < 0 || game >= NSCENE) {
     94 		puts("Very funny.");
     95 		exit(1);
     96 	}
     97 	cc = &scene[game];
     98 	ls = SHIP(cc->vessels);
     99 
    100 	for (n = 0; n < NNATION; n++)
    101 		nat[n] = 0;
    102 	foreachship(sp) {
    103 		if (sp->file == NULL &&
    104 		    (sp->file = (struct File *)calloc(1, sizeof (struct File))) == NULL) {
    105 			puts("OUT OF MEMORY");
    106 			exit(1);
    107 		}
    108 		sp->file->index = sp - SHIP(0);
    109 		sp->file->stern = nat[sp->nationality]++;
    110 		sp->file->dir = sp->shipdir;
    111 		sp->file->row = sp->shiprow;
    112 		sp->file->col = sp->shipcol;
    113 	}
    114 	windspeed = cc->windspeed;
    115 	winddir = cc->winddir;
    116 
    117 	signal(SIGHUP, choke);
    118 	signal(SIGINT, choke);
    119 
    120 	hasdriver = sync_exists(game);
    121 	if (sync_open() < 0) {
    122 		perror("sail: syncfile");
    123 		exit(1);
    124 	}
    125 
    126 	if (hasdriver) {
    127 		puts("Synchronizing with the other players...");
    128 		fflush(stdout);
    129 		if (Sync() < 0)
    130 			leave(LEAVE_SYNC);
    131 	}
    132 	for (;;) {
    133 		foreachship(sp)
    134 			if (sp->file->captain[0] == 0 && !sp->file->struck
    135 			    && sp->file->captured == 0)
    136 				break;
    137 		if (sp >= ls) {
    138 			puts("All ships taken in that scenario.");
    139 			foreachship(sp)
    140 				free((char *)sp->file);
    141 			sync_close(0);
    142 			people = 0;
    143 			goto reprint;
    144 		}
    145 		if (randomize) {
    146 			player = sp - SHIP(0);
    147 		} else {
    148 			printf("%s\n\n", cc->name);
    149 			foreachship(sp)
    150 				printf("  %2d:  %-10s %-15s  (%-2d pts)   %s\n",
    151 					sp->file->index,
    152 					countryname[sp->nationality],
    153 					sp->shipname,
    154 					sp->specs->pts,
    155 					saywhat(sp, 1));
    156 			printf("\nWhich ship (0-%d)? ", cc->vessels-1);
    157 			fflush(stdout);
    158 			if (scanf("%d", &player) != 1 || player < 0
    159 			    || player >= cc->vessels) {
    160 				while (getchar() != '\n')
    161 					;
    162 				puts("Say what?");
    163 				player = -1;
    164 			} else
    165 				while (getchar() != '\n')
    166 					;
    167 		}
    168 		if (player < 0)
    169 			continue;
    170 		if (Sync() < 0)
    171 			leave(LEAVE_SYNC);
    172 		fp = SHIP(player)->file;
    173 		if (fp->captain[0] || fp->struck || fp->captured != 0)
    174 			puts("That ship is taken.");
    175 		else
    176 			break;
    177 	}
    178 
    179 	ms = SHIP(player);
    180 	mf = ms->file;
    181 	mc = ms->specs;
    182 
    183 	Write(W_BEGIN, ms, 0, 0, 0, 0);
    184 	if (Sync() < 0)
    185 		leave(LEAVE_SYNC);
    186 
    187 	signal(SIGCHLD, child);
    188 	if (!hasdriver)
    189 		switch (fork()) {
    190 		case 0:
    191 			longjmp(restart, MODE_DRIVER);
    192 			/*NOTREACHED*/
    193 		case -1:
    194 			perror("fork");
    195 			leave(LEAVE_FORK);
    196 			break;
    197 		default:
    198 			hasdriver++;
    199 		}
    200 
    201 	printf("Your ship is the %s, a %d gun %s (%s crew).\n",
    202 		ms->shipname, mc->guns, classname[mc->class],
    203 		qualname[mc->qual]);
    204 	if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
    205 		strncpy(captain, nameptr, sizeof captain);
    206 	else {
    207 		printf("Your name, Captain? ");
    208 		fflush(stdout);
    209 		fgets(captain, sizeof captain, stdin);
    210 		if (!*captain)
    211 			strcpy(captain, "no name");
    212 		else
    213 		    captain[strlen(captain) - 1] = '\0';
    214 	}
    215 	captain[sizeof captain - 1] = '\0';
    216 	Writestr(W_CAPTAIN, ms, captain);
    217 	for (n = 0; n < 2; n++) {
    218 		char buf[10];
    219 
    220 		printf("\nInitial broadside %s (grape, chain, round, double): ",
    221 			n ? "right" : "left");
    222 		fflush(stdout);
    223 		scanf("%s", buf);
    224 		switch (*buf) {
    225 		case 'g':
    226 			load = L_GRAPE;
    227 			break;
    228 		case 'c':
    229 			load = L_CHAIN;
    230 			break;
    231 		case 'r':
    232 			load = L_ROUND;
    233 			break;
    234 		case 'd':
    235 			load = L_DOUBLE;
    236 			break;
    237 		default:
    238 			load = L_ROUND;
    239 		}
    240 		if (n) {
    241 			mf->loadR = load;
    242 			mf->readyR = R_LOADED|R_INITIAL;
    243 		} else {
    244 			mf->loadL = load;
    245 			mf->readyL = R_LOADED|R_INITIAL;
    246 		}
    247 	}
    248 
    249 	initscreen();
    250 	draw_board();
    251 	snprintf(message, sizeof message, "Captain %s assuming command",
    252 			captain);
    253 	Writestr(W_SIGNAL, ms, message);
    254 	newturn(0);
    255 }
    256