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