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