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