]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/pl_main.c
Sprinkle some "bool". And don't use '\0' to mean 'false'.
[bsdgames-darwin.git] / sail / pl_main.c
1 /* $NetBSD: pl_main.c,v 1.23 2009/03/14 19:36:42 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.23 2009/03/14 19:36:42 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 "extern.h"
49 #include "player.h"
50 #include "restart.h"
51
52 static void initialize(void);
53
54 /*ARGSUSED*/
55 int
56 pl_main(void)
57 {
58 initialize();
59 Msg("Aye aye, Sir");
60 play();
61 return 0; /* for lint, play() never returns */
62 }
63
64 static void
65 initialize(void)
66 {
67 struct File *fp;
68 struct ship *sp;
69 char captain[80];
70 char message[60];
71 int load;
72 int n;
73 char *nameptr;
74 int nat[NNATION];
75
76 if (game < 0) {
77 puts("Choose a scenario:\n");
78 puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
79 for (n = 0; n < NSCENE; n++) {
80 /* ( */
81 printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
82 sync_exists(n) ? "YES" : "no",
83 scene[n].name);
84 }
85 reprint:
86 printf("\nScenario number? ");
87 fflush(stdout);
88 scanf("%d", &game);
89 while (getchar() != '\n' && !feof(stdin))
90 ;
91 }
92 if (game < 0 || game >= NSCENE) {
93 errx(1, "Very funny.");
94 }
95 cc = &scene[game];
96 ls = SHIP(cc->vessels);
97
98 for (n = 0; n < NNATION; n++)
99 nat[n] = 0;
100 foreachship(sp) {
101 if (sp->file == NULL &&
102 (sp->file = calloc(1, sizeof (struct File))) == NULL) {
103 err(1, "calloc");
104 }
105 sp->file->index = sp - SHIP(0);
106 sp->file->stern = nat[sp->nationality]++;
107 sp->file->dir = sp->shipdir;
108 sp->file->row = sp->shiprow;
109 sp->file->col = sp->shipcol;
110 }
111 windspeed = cc->windspeed;
112 winddir = cc->winddir;
113
114 signal(SIGHUP, choke);
115 signal(SIGINT, choke);
116
117 hasdriver = sync_exists(game);
118 if (sync_open() < 0) {
119 err(1, "syncfile");
120 }
121
122 if (hasdriver) {
123 puts("Synchronizing with the other players...");
124 fflush(stdout);
125 if (Sync() < 0)
126 leave(LEAVE_SYNC);
127 }
128 for (;;) {
129 foreachship(sp)
130 if (sp->file->captain[0] == 0 && !sp->file->struck
131 && sp->file->captured == 0)
132 break;
133 if (sp >= ls) {
134 puts("All ships taken in that scenario.");
135 foreachship(sp)
136 free(sp->file);
137 sync_close(0);
138 people = 0;
139 goto reprint;
140 }
141 if (randomize) {
142 player = sp - SHIP(0);
143 } else {
144 printf("%s\n\n", cc->name);
145 foreachship(sp)
146 printf(" %2d: %-10s %-15s (%-2d pts) %s\n",
147 sp->file->index,
148 countryname[sp->nationality],
149 sp->shipname,
150 sp->specs->pts,
151 saywhat(sp, 1));
152 printf("\nWhich ship (0-%d)? ", cc->vessels-1);
153 fflush(stdout);
154 if (scanf("%d", &player) != 1 || player < 0
155 || player >= cc->vessels) {
156 while (getchar() != '\n' && !feof(stdin))
157 ;
158 puts("Say what?");
159 player = -1;
160 } else
161 while (getchar() != '\n' && !feof(stdin))
162 ;
163 if (feof(stdin)) {
164 printf("\nExiting...\n");
165 leave(LEAVE_QUIT);
166 }
167 }
168 if (player < 0)
169 continue;
170 if (Sync() < 0)
171 leave(LEAVE_SYNC);
172 fp = SHIP(player)->file;
173 if (fp->captain[0] || fp->struck || fp->captured != 0)
174 puts("That ship is taken.");
175 else
176 break;
177 }
178
179 ms = SHIP(player);
180 mf = ms->file;
181 mc = ms->specs;
182
183 Write(W_BEGIN, ms, 0, 0, 0, 0);
184 if (Sync() < 0)
185 leave(LEAVE_SYNC);
186
187 signal(SIGCHLD, child);
188 if (!hasdriver)
189 switch (fork()) {
190 case 0:
191 longjmp(restart, MODE_DRIVER);
192 /*NOTREACHED*/
193 case -1:
194 perror("fork");
195 leave(LEAVE_FORK);
196 break;
197 default:
198 hasdriver++;
199 }
200
201 printf("Your ship is the %s, a %d gun %s (%s crew).\n",
202 ms->shipname, mc->guns, classname[mc->class],
203 qualname[mc->qual]);
204 if ((nameptr = getenv("SAILNAME")) && *nameptr)
205 strlcpy(captain, nameptr, sizeof captain);
206 else {
207 printf("Your name, Captain? ");
208 fflush(stdout);
209 if (fgets(captain, sizeof captain, stdin) == NULL)
210 strcpy(captain, "no name");
211 else if (*captain == '\0' || *captain == '\n')
212 strcpy(captain, "no name");
213 else
214 captain[strlen(captain) - 1] = '\0';
215 }
216 Writestr(W_CAPTAIN, ms, captain);
217 for (n = 0; n < 2; n++) {
218 char buf[10];
219
220 printf("\nInitial broadside %s (grape, chain, round, double): ",
221 n ? "right" : "left");
222 fflush(stdout);
223 fgets(buf, sizeof(buf), stdin);
224 switch (*buf) {
225 case 'g':
226 load = L_GRAPE;
227 break;
228 case 'c':
229 load = L_CHAIN;
230 break;
231 case 'r':
232 load = L_ROUND;
233 break;
234 case 'd':
235 load = L_DOUBLE;
236 break;
237 default:
238 load = L_ROUND;
239 }
240 if (n) {
241 mf->loadR = load;
242 mf->readyR = R_LOADED|R_INITIAL;
243 } else {
244 mf->loadL = load;
245 mf->readyL = R_LOADED|R_INITIAL;
246 }
247 }
248
249 printf("\n");
250 fflush(stdout);
251 initscreen();
252 draw_board();
253 snprintf(message, sizeof message, "Captain %s assuming command",
254 captain);
255 Writestr(W_SIGNAL, ms, message);
256 newturn(0);
257 }