2003-08-07 13:36:50 +04:00
|
|
|
/* $NetBSD: pl_main.c,v 1.16 2003/08/07 09:37:44 agc Exp $ */
|
1995-04-22 14:36:43 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1995-04-22 14:36:43 +04:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:36:50 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-04-22 14:36:43 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)pl_main.c 8.1 (Berkeley) 5/31/93";
|
|
|
|
#else
|
2003-08-07 13:36:50 +04:00
|
|
|
__RCSID("$NetBSD: pl_main.c,v 1.16 2003/08/07 09:37:44 agc Exp $");
|
1995-04-22 14:36:43 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2001-01-04 06:51:23 +03:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
2001-01-04 06:21:16 +03:00
|
|
|
#include <stdio.h>
|
1997-10-13 23:42:53 +04:00
|
|
|
#include <stdlib.h>
|
2001-01-04 06:51:23 +03:00
|
|
|
#include <string.h>
|
1997-10-13 23:42:53 +04:00
|
|
|
#include <unistd.h>
|
2001-01-04 07:41:41 +03:00
|
|
|
#include "extern.h"
|
2001-01-04 06:21:16 +03:00
|
|
|
#include "player.h"
|
2001-01-04 06:51:23 +03:00
|
|
|
#include "restart.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-01-04 05:43:32 +03:00
|
|
|
static void initialize(void);
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*ARGSUSED*/
|
1997-10-13 23:42:53 +04:00
|
|
|
int
|
2001-01-02 01:23:29 +03:00
|
|
|
pl_main(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
initialize();
|
1997-10-13 23:42:53 +04:00
|
|
|
Msg("Aye aye, Sir");
|
1993-03-21 12:45:37 +03:00
|
|
|
play();
|
|
|
|
return 0; /* for lint, play() never returns */
|
|
|
|
}
|
|
|
|
|
2001-01-04 05:43:32 +03:00
|
|
|
static void
|
2001-01-02 01:23:29 +03:00
|
|
|
initialize(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-01-04 04:13:34 +03:00
|
|
|
struct File *fp;
|
|
|
|
struct ship *sp;
|
1993-03-21 12:45:37 +03:00
|
|
|
char captain[80];
|
|
|
|
char message[60];
|
|
|
|
int load;
|
2001-01-04 04:13:34 +03:00
|
|
|
int n;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *nameptr;
|
|
|
|
int nat[NNATION];
|
|
|
|
|
|
|
|
if (game < 0) {
|
2001-01-02 01:23:29 +03:00
|
|
|
puts("Choose a scenario:\n");
|
|
|
|
puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
|
1993-03-21 12:45:37 +03:00
|
|
|
for (n = 0; n < NSCENE; n++) {
|
|
|
|
/* ( */
|
|
|
|
printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
|
|
|
|
sync_exists(n) ? "YES" : "no",
|
|
|
|
scene[n].name);
|
|
|
|
}
|
|
|
|
reprint:
|
|
|
|
printf("\nScenario number? ");
|
2001-01-02 01:23:29 +03:00
|
|
|
fflush(stdout);
|
|
|
|
scanf("%d", &game);
|
1993-03-21 12:45:37 +03:00
|
|
|
while (getchar() != '\n')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if (game < 0 || game >= NSCENE) {
|
2001-01-02 01:23:29 +03:00
|
|
|
puts("Very funny.");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
cc = &scene[game];
|
|
|
|
ls = SHIP(cc->vessels);
|
|
|
|
|
|
|
|
for (n = 0; n < NNATION; n++)
|
|
|
|
nat[n] = 0;
|
|
|
|
foreachship(sp) {
|
|
|
|
if (sp->file == NULL &&
|
|
|
|
(sp->file = (struct File *)calloc(1, sizeof (struct File))) == NULL) {
|
2001-01-02 01:23:29 +03:00
|
|
|
puts("OUT OF MEMORY");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
sp->file->index = sp - SHIP(0);
|
|
|
|
sp->file->stern = nat[sp->nationality]++;
|
|
|
|
sp->file->dir = sp->shipdir;
|
|
|
|
sp->file->row = sp->shiprow;
|
|
|
|
sp->file->col = sp->shipcol;
|
|
|
|
}
|
|
|
|
windspeed = cc->windspeed;
|
|
|
|
winddir = cc->winddir;
|
|
|
|
|
2001-01-02 01:23:29 +03:00
|
|
|
signal(SIGHUP, choke);
|
|
|
|
signal(SIGINT, choke);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
hasdriver = sync_exists(game);
|
|
|
|
if (sync_open() < 0) {
|
|
|
|
perror("sail: syncfile");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasdriver) {
|
2001-01-02 01:23:29 +03:00
|
|
|
puts("Synchronizing with the other players...");
|
|
|
|
fflush(stdout);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (Sync() < 0)
|
|
|
|
leave(LEAVE_SYNC);
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
foreachship(sp)
|
|
|
|
if (sp->file->captain[0] == 0 && !sp->file->struck
|
|
|
|
&& sp->file->captured == 0)
|
|
|
|
break;
|
|
|
|
if (sp >= ls) {
|
2001-01-02 01:23:29 +03:00
|
|
|
puts("All ships taken in that scenario.");
|
1993-03-21 12:45:37 +03:00
|
|
|
foreachship(sp)
|
|
|
|
free((char *)sp->file);
|
|
|
|
sync_close(0);
|
|
|
|
people = 0;
|
|
|
|
goto reprint;
|
|
|
|
}
|
|
|
|
if (randomize) {
|
|
|
|
player = sp - SHIP(0);
|
|
|
|
} else {
|
|
|
|
printf("%s\n\n", cc->name);
|
|
|
|
foreachship(sp)
|
|
|
|
printf(" %2d: %-10s %-15s (%-2d pts) %s\n",
|
|
|
|
sp->file->index,
|
|
|
|
countryname[sp->nationality],
|
|
|
|
sp->shipname,
|
|
|
|
sp->specs->pts,
|
|
|
|
saywhat(sp, 1));
|
|
|
|
printf("\nWhich ship (0-%d)? ", cc->vessels-1);
|
2001-01-02 01:23:29 +03:00
|
|
|
fflush(stdout);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (scanf("%d", &player) != 1 || player < 0
|
|
|
|
|| player >= cc->vessels) {
|
|
|
|
while (getchar() != '\n')
|
|
|
|
;
|
2001-01-02 01:23:29 +03:00
|
|
|
puts("Say what?");
|
1993-03-21 12:45:37 +03:00
|
|
|
player = -1;
|
|
|
|
} else
|
|
|
|
while (getchar() != '\n')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if (player < 0)
|
|
|
|
continue;
|
|
|
|
if (Sync() < 0)
|
|
|
|
leave(LEAVE_SYNC);
|
|
|
|
fp = SHIP(player)->file;
|
|
|
|
if (fp->captain[0] || fp->struck || fp->captured != 0)
|
2001-01-02 01:23:29 +03:00
|
|
|
puts("That ship is taken.");
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ms = SHIP(player);
|
|
|
|
mf = ms->file;
|
|
|
|
mc = ms->specs;
|
|
|
|
|
1999-02-10 03:45:45 +03:00
|
|
|
Write(W_BEGIN, ms, 0, 0, 0, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (Sync() < 0)
|
|
|
|
leave(LEAVE_SYNC);
|
|
|
|
|
2001-01-02 01:23:29 +03:00
|
|
|
signal(SIGCHLD, child);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!hasdriver)
|
|
|
|
switch (fork()) {
|
|
|
|
case 0:
|
|
|
|
longjmp(restart, MODE_DRIVER);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
case -1:
|
|
|
|
perror("fork");
|
|
|
|
leave(LEAVE_FORK);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
hasdriver++;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Your ship is the %s, a %d gun %s (%s crew).\n",
|
|
|
|
ms->shipname, mc->guns, classname[mc->class],
|
|
|
|
qualname[mc->qual]);
|
|
|
|
if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
|
2001-01-02 01:23:29 +03:00
|
|
|
strncpy(captain, nameptr, sizeof captain);
|
1993-03-21 12:45:37 +03:00
|
|
|
else {
|
2001-01-02 01:23:29 +03:00
|
|
|
printf("Your name, Captain? ");
|
|
|
|
fflush(stdout);
|
|
|
|
fgets(captain, sizeof captain, stdin);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!*captain)
|
2001-01-02 01:23:29 +03:00
|
|
|
strcpy(captain, "no name");
|
1994-01-13 22:22:29 +03:00
|
|
|
else
|
|
|
|
captain[strlen(captain) - 1] = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
captain[sizeof captain - 1] = '\0';
|
1999-02-10 03:45:45 +03:00
|
|
|
Writestr(W_CAPTAIN, ms, captain);
|
1993-03-21 12:45:37 +03:00
|
|
|
for (n = 0; n < 2; n++) {
|
|
|
|
char buf[10];
|
|
|
|
|
|
|
|
printf("\nInitial broadside %s (grape, chain, round, double): ",
|
|
|
|
n ? "right" : "left");
|
2001-01-02 01:23:29 +03:00
|
|
|
fflush(stdout);
|
|
|
|
scanf("%s", buf);
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (*buf) {
|
|
|
|
case 'g':
|
|
|
|
load = L_GRAPE;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
load = L_CHAIN;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
load = L_ROUND;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
load = L_DOUBLE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
load = L_ROUND;
|
|
|
|
}
|
|
|
|
if (n) {
|
|
|
|
mf->loadR = load;
|
|
|
|
mf->readyR = R_LOADED|R_INITIAL;
|
|
|
|
} else {
|
|
|
|
mf->loadL = load;
|
|
|
|
mf->readyL = R_LOADED|R_INITIAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initscreen();
|
|
|
|
draw_board();
|
2001-01-02 01:23:29 +03:00
|
|
|
snprintf(message, sizeof message, "Captain %s assuming command",
|
1999-02-10 03:45:45 +03:00
|
|
|
captain);
|
|
|
|
Writestr(W_SIGNAL, ms, message);
|
1997-10-13 23:42:53 +04:00
|
|
|
newturn(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|