1*38821839Sdholland /* $NetBSD: pl_main.c,v 1.27 2010/08/06 09:14:40 dholland Exp $ */
2982615d7Scgd
361f28255Scgd /*
4982615d7Scgd * Copyright (c) 1983, 1993
5982615d7Scgd * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32bf72b656Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34982615d7Scgd #if 0
35982615d7Scgd static char sccsid[] = "@(#)pl_main.c 8.1 (Berkeley) 5/31/93";
36982615d7Scgd #else
37*38821839Sdholland __RCSID("$NetBSD: pl_main.c,v 1.27 2010/08/06 09:14:40 dholland Exp $");
38982615d7Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
41f521abdfSdholland #include <err.h>
4261a39e0bSjwise #include <signal.h>
43aac077fcSjwise #include <stdio.h>
44bf72b656Schristos #include <stdlib.h>
4561a39e0bSjwise #include <string.h>
46bf72b656Schristos #include <unistd.h>
4787d1d157Sdholland #include "display.h"
489baf035bSjwise #include "extern.h"
49aac077fcSjwise #include "player.h"
50*38821839Sdholland
51*38821839Sdholland char myname[MAXNAMESIZE];
5261f28255Scgd
5343894be6Sjwise static void initialize(void);
5443894be6Sjwise
55*38821839Sdholland void
pl_main_init(void)56*38821839Sdholland pl_main_init(void)
5761f28255Scgd {
5861f28255Scgd int nat[NNATION];
59*38821839Sdholland int n;
60*38821839Sdholland struct ship *sp;
6161f28255Scgd
6261f28255Scgd if (game < 0 || game >= NSCENE) {
63f521abdfSdholland errx(1, "Very funny.");
6461f28255Scgd }
6561f28255Scgd cc = &scene[game];
6661f28255Scgd ls = SHIP(cc->vessels);
6761f28255Scgd
6861f28255Scgd for (n = 0; n < NNATION; n++)
6961f28255Scgd nat[n] = 0;
7061f28255Scgd foreachship(sp) {
7161f28255Scgd if (sp->file == NULL &&
723b678797Sdholland (sp->file = calloc(1, sizeof (struct File))) == NULL) {
73f521abdfSdholland err(1, "calloc");
7461f28255Scgd }
7561f28255Scgd sp->file->index = sp - SHIP(0);
7661f28255Scgd sp->file->stern = nat[sp->nationality]++;
7761f28255Scgd sp->file->dir = sp->shipdir;
7861f28255Scgd sp->file->row = sp->shiprow;
7961f28255Scgd sp->file->col = sp->shipcol;
8061f28255Scgd }
8161f28255Scgd windspeed = cc->windspeed;
8261f28255Scgd winddir = cc->winddir;
8361f28255Scgd
84fdb512d4Sjwise signal(SIGHUP, choke);
85fdb512d4Sjwise signal(SIGINT, choke);
86*38821839Sdholland signal(SIGCHLD, child);
8761f28255Scgd }
8861f28255Scgd
89*38821839Sdholland void
pl_main_uninit(void)90*38821839Sdholland pl_main_uninit(void)
91*38821839Sdholland {
92*38821839Sdholland struct ship *sp;
93*38821839Sdholland
94*38821839Sdholland foreachship(sp) {
959a834809Sdholland free(sp->file);
96*38821839Sdholland sp->file = NULL;
9761f28255Scgd }
9861f28255Scgd }
9961f28255Scgd
100*38821839Sdholland static void
initialize(void)101*38821839Sdholland initialize(void)
102*38821839Sdholland {
103*38821839Sdholland char captain[MAXNAMESIZE];
104*38821839Sdholland char message[60];
105*38821839Sdholland
106*38821839Sdholland if (game < 0 || game >= NSCENE) {
107*38821839Sdholland errx(1, "Very funny.");
108*38821839Sdholland }
10961f28255Scgd
110e544d2b8Sdholland send_begin(ms);
11161f28255Scgd if (Sync() < 0)
11261f28255Scgd leave(LEAVE_SYNC);
11361f28255Scgd
114*38821839Sdholland #if 0
11561f28255Scgd printf("Your ship is the %s, a %d gun %s (%s crew).\n",
11661f28255Scgd ms->shipname, mc->guns, classname[mc->class],
11761f28255Scgd qualname[mc->qual]);
118*38821839Sdholland #endif
119*38821839Sdholland
120*38821839Sdholland strlcpy(captain, myname, sizeof(captain));
121e544d2b8Sdholland send_captain(ms, captain);
12261f28255Scgd
12387d1d157Sdholland display_redraw();
124fdb512d4Sjwise snprintf(message, sizeof message, "Captain %s assuming command",
12524f809b0Shubertf captain);
126e544d2b8Sdholland send_signal(ms, message);
127bf72b656Schristos newturn(0);
12861f28255Scgd }
129*38821839Sdholland
130*38821839Sdholland void
pl_main(void)131*38821839Sdholland pl_main(void)
132*38821839Sdholland {
133*38821839Sdholland initialize();
134*38821839Sdholland Msg("Aye aye, Sir");
135*38821839Sdholland play();
136*38821839Sdholland }
137