Home | History | Annotate | Line # | Download | only in sail
pl_main.c revision 1.6
      1 /*	$NetBSD: pl_main.c,v 1.6 1997/10/13 19:45:48 christos 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.6 1997/10/13 19:45:48 christos 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 /*ARGSUSED*/
     52 int
     53 pl_main()
     54 {
     55 
     56 	if (!SCREENTEST()) {
     57 		printf("Can't sail on this terminal.\n");
     58 		exit(1);
     59 	}
     60 	initialize();
     61 	Msg("Aye aye, Sir");
     62 	play();
     63 	return 0;			/* for lint,  play() never returns */
     64 }
     65 
     66 void
     67 initialize()
     68 {
     69 	register struct File *fp;
     70 	register struct ship *sp;
     71 	char captain[80];
     72 	char message[60];
     73 	int load;
     74 	register int n;
     75 	char *nameptr;
     76 	int nat[NNATION];
     77 
     78 	if (game < 0) {
     79 		(void) puts("Choose a scenario:\n");
     80 		(void) puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
     81 		for (n = 0; n < NSCENE; n++) {
     82 			/* ( */
     83 			printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
     84 				sync_exists(n) ? "YES" : "no",
     85 				scene[n].name);
     86 		}
     87 reprint:
     88 		printf("\nScenario number? ");
     89 		(void) fflush(stdout);
     90 		(void) scanf("%d", &game);
     91 		while (getchar() != '\n')
     92 			;
     93 	}
     94 	if (game < 0 || game >= NSCENE) {
     95 		(void) puts("Very funny.");
     96 		exit(1);
     97 	}
     98 	cc = &scene[game];
     99 	ls = SHIP(cc->vessels);
    100 
    101 	for (n = 0; n < NNATION; n++)
    102 		nat[n] = 0;
    103 	foreachship(sp) {
    104 		if (sp->file == NULL &&
    105 		    (sp->file = (struct File *)calloc(1, sizeof (struct File))) == NULL) {
    106 			(void) puts("OUT OF MEMORY");
    107 			exit(1);
    108 		}
    109 		sp->file->index = sp - SHIP(0);
    110 		sp->file->stern = nat[sp->nationality]++;
    111 		sp->file->dir = sp->shipdir;
    112 		sp->file->row = sp->shiprow;
    113 		sp->file->col = sp->shipcol;
    114 	}
    115 	windspeed = cc->windspeed;
    116 	winddir = cc->winddir;
    117 
    118 	(void) signal(SIGHUP, choke);
    119 	(void) signal(SIGINT, choke);
    120 
    121 	hasdriver = sync_exists(game);
    122 	if (sync_open() < 0) {
    123 		perror("sail: syncfile");
    124 		exit(1);
    125 	}
    126 
    127 	if (hasdriver) {
    128 		(void) puts("Synchronizing with the other players...");
    129 		(void) fflush(stdout);
    130 		if (Sync() < 0)
    131 			leave(LEAVE_SYNC);
    132 	}
    133 	for (;;) {
    134 		foreachship(sp)
    135 			if (sp->file->captain[0] == 0 && !sp->file->struck
    136 			    && sp->file->captured == 0)
    137 				break;
    138 		if (sp >= ls) {
    139 			(void) puts("All ships taken in that scenario.");
    140 			foreachship(sp)
    141 				free((char *)sp->file);
    142 			sync_close(0);
    143 			people = 0;
    144 			goto reprint;
    145 		}
    146 		if (randomize) {
    147 			player = sp - SHIP(0);
    148 		} else {
    149 			printf("%s\n\n", cc->name);
    150 			foreachship(sp)
    151 				printf("  %2d:  %-10s %-15s  (%-2d pts)   %s\n",
    152 					sp->file->index,
    153 					countryname[sp->nationality],
    154 					sp->shipname,
    155 					sp->specs->pts,
    156 					saywhat(sp, 1));
    157 			printf("\nWhich ship (0-%d)? ", cc->vessels-1);
    158 			(void) fflush(stdout);
    159 			if (scanf("%d", &player) != 1 || player < 0
    160 			    || player >= cc->vessels) {
    161 				while (getchar() != '\n')
    162 					;
    163 				(void) puts("Say what?");
    164 				player = -1;
    165 			} else
    166 				while (getchar() != '\n')
    167 					;
    168 		}
    169 		if (player < 0)
    170 			continue;
    171 		if (Sync() < 0)
    172 			leave(LEAVE_SYNC);
    173 		fp = SHIP(player)->file;
    174 		if (fp->captain[0] || fp->struck || fp->captured != 0)
    175 			(void) puts("That ship is taken.");
    176 		else
    177 			break;
    178 	}
    179 
    180 	ms = SHIP(player);
    181 	mf = ms->file;
    182 	mc = ms->specs;
    183 
    184 	Write(W_BEGIN, ms, 0, 0, 0, 0, 0);
    185 	if (Sync() < 0)
    186 		leave(LEAVE_SYNC);
    187 
    188 	(void) signal(SIGCHLD, child);
    189 	if (!hasdriver)
    190 		switch (fork()) {
    191 		case 0:
    192 			longjmp(restart, MODE_DRIVER);
    193 			/*NOTREACHED*/
    194 		case -1:
    195 			perror("fork");
    196 			leave(LEAVE_FORK);
    197 			break;
    198 		default:
    199 			hasdriver++;
    200 		}
    201 
    202 	printf("Your ship is the %s, a %d gun %s (%s crew).\n",
    203 		ms->shipname, mc->guns, classname[mc->class],
    204 		qualname[mc->qual]);
    205 	if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
    206 		(void) strncpy(captain, nameptr, sizeof captain);
    207 	else {
    208 		(void) printf("Your name, Captain? ");
    209 		(void) fflush(stdout);
    210 		(void) fgets(captain, sizeof captain, stdin);
    211 		if (!*captain)
    212 			(void) strcpy(captain, "no name");
    213 		else
    214 		    captain[strlen(captain) - 1] = '\0';
    215 	}
    216 	captain[sizeof captain - 1] = '\0';
    217 	Write(W_CAPTAIN, ms, 1, (long)captain, 0, 0, 0);
    218 	for (n = 0; n < 2; n++) {
    219 		char buf[10];
    220 
    221 		printf("\nInitial broadside %s (grape, chain, round, double): ",
    222 			n ? "right" : "left");
    223 		(void) fflush(stdout);
    224 		(void) scanf("%s", buf);
    225 		switch (*buf) {
    226 		case 'g':
    227 			load = L_GRAPE;
    228 			break;
    229 		case 'c':
    230 			load = L_CHAIN;
    231 			break;
    232 		case 'r':
    233 			load = L_ROUND;
    234 			break;
    235 		case 'd':
    236 			load = L_DOUBLE;
    237 			break;
    238 		default:
    239 			load = L_ROUND;
    240 		}
    241 		if (n) {
    242 			mf->loadR = load;
    243 			mf->readyR = R_LOADED|R_INITIAL;
    244 		} else {
    245 			mf->loadL = load;
    246 			mf->readyL = R_LOADED|R_INITIAL;
    247 		}
    248 	}
    249 
    250 	initscreen();
    251 	draw_board();
    252 	(void) sprintf(message, "Captain %s assuming command", captain);
    253 	Write(W_SIGNAL, ms, 1, (long)message, 0, 0, 0);
    254 	newturn(0);
    255 }
    256