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