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