got rid of unnecessary variables
[unix-history] / usr / src / games / sail / pl_1.c
CommitLineData
c77f0542 1#ifndef lint
6c17b19a 2static char *sccsid = "@(#)pl_1.c 1.10 83/10/10";
c77f0542 3#endif
7fc06086 4
c77f0542
CL
5#include "player.h"
6#include <sys/types.h>
6c17b19a 7#include <sys/wait.h>
c77f0542 8
b3a57661 9int choke(), child();
c77f0542 10
b3a57661
EW
11/*ARGSUSED*/
12main(argc, argv)
13int argc;
14char **argv;
c77f0542 15{
b3a57661 16 register struct ship *sp;
b3a57661 17 int ta;
6c17b19a
EW
18 char aheadfirst;
19 int ma;
b3a57661
EW
20 char nodrive = 0, randomize = 0, debug = 0;
21 char *badstring();
22 extern char _sobuf[];
c77f0542 23
b3a57661 24 setbuf(stdout, _sobuf);
7fc06086 25
b3a57661
EW
26 while (*++argv && **argv == '-')
27 switch (*++*argv) {
28 case 'd':
29 nodrive = 1;
30 break;
31 case 'D':
32 debug++;
33 break;
34 case 'x':
35 randomize = 1;
36 break;
37 default:
38 printf("Unknown flag '%s'\n",*argv);
39 break;
7fc06086 40 }
b3a57661
EW
41 if (*argv)
42 game = atoi(*argv);
43 else
44 game = -1;
45 initialize(nodrive, randomize, debug);
46 Signal("Aye aye, Sir", (struct ship *)0);
47 for (;;) {
48 prompt();
49 switch (sgetch(0)) {
50 case 'm':
51 if (mc->crew3 && !snagged(ms)
52 && windspeed != 0) {
6c17b19a 53 ta = maxturns(ms, &aheadfirst);
b3a57661 54 ma = maxmove(ms, mf->dir, 0);
b3a57661
EW
55 acceptmove(ma, ta, aheadfirst);
56 } else
57 Signal("Unable to move", (struct ship *)0);
58 break;
59 case 's':
60 acceptsignal();
61 break;
62 case 'g':
63 grapungrap();
64 break;
65 case 'u':
66 unfoulplayer();
67 break;
68 case 'v':
69 Signal("%s", (struct ship *)0, version);
70 break;
71 case 'b':
72 doboarding();
73 break;
74 case 'f':
75 acceptcombat();
76 break;
77 case 'l':
78 loadplayer();
79 break;
80 case 'c':
81 changesail();
82 break;
83 case 'r':
84 repair();
85 break;
86 case 'B':
87 Signal("'Hands to stations!'", (struct ship *)0);
88 unboard(ms, ms, 1); /* cancel DBP's */
89 unboard(ms, ms, 0); /* cancel offense */
90 break;
91 case '\f':
92 centerview();
93 board();
7fc06086 94 screen();
b3a57661
EW
95 break;
96 case 'L':
97 mf->loadL = L_EMPTY;
98 mf->loadR = L_EMPTY;
99 mf->readyL = R_EMPTY;
100 mf->readyR = R_EMPTY;
101 Signal("Broadsides unloaded", (struct ship *)0);
102 break;
103 case 'q':
104 Signal("Type 'Q' to quit", (struct ship *)0);
105 break;
106 case 'Q':
107 leave(LEAVE_QUIT);
108 break;
109 case 'I':
110 foreachship(sp)
111 eyeball(sp);
112 break;
113 case 'i':
114 eyeball(closestenemy(ms, 0, 1));
115 break;
116 case 'C':
117 centerview();
118 draw_view();
119 break;
120 case 'U':
121 upview();
122 draw_view();
123 break;
124 case 'D':
125 case 'N':
126 downview();
127 draw_view();
128 break;
129 case 'H':
130 leftview();
131 draw_view();
132 break;
133 case 'J':
134 rightview();
135 draw_view();
136 break;
137 case 'F':
138 lookout();
139 break;
140 case 'S':
141 dont_adjust = !dont_adjust;
142 break;
7fc06086 143 }
b3a57661
EW
144 signalflags();
145 lost();
c77f0542 146 }
c77f0542
CL
147}
148
bba7bfeb
BJ
149initialize(nodriver, randomize, debug)
150char randomize, nodriver, debug;
c77f0542 151{
b3a57661
EW
152 register struct File *fp;
153 register struct ship *sp;
6c17b19a 154 char captain[80];
7fc06086 155 char message[60];
b3a57661 156 int load;
6c17b19a 157 int active;
7fc06086 158 register int n;
7fc06086 159 char *nameptr;
b3a57661 160 int nat[NNATION];
c77f0542 161
b3a57661 162 (void) srand(getpid());
c77f0542 163
7fc06086 164 if (game < 0) {
b3a57661
EW
165 (void) puts("Choose a scenario:\n");
166 (void) puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
167 for (n = 0; n < NSCENE; n++) {
6c17b19a
EW
168 /* ( */
169 printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
170 sync_exists(n) ? "YES" : "no",
171 scene[n].name);
7fc06086 172 }
c77f0542 173reprint:
7fc06086 174 printf("\nScenario number? ");
b3a57661
EW
175 (void) fflush(stdout);
176 (void) scanf("%d", &game);
177 while (getchar() != '\n')
178 ;
7fc06086 179 }
b3a57661
EW
180 if (game < 0 || game >= NSCENE) {
181 (void) puts("Very funny.");
7fc06086
BJ
182 exit(1);
183 }
b3a57661
EW
184 cc = &scene[game];
185 ls = cc->ship + cc->vessels;
186
6c17b19a
EW
187 active = sync_exists(game);
188 if (sync_open() < 0) {
189 perror("sail: syncfile");
190 exit(1);
c77f0542 191 }
b3a57661
EW
192
193 for (n = 0; n < NNATION; n++)
194 nat[n] = 0;
195 foreachship(sp) {
196 sp->file = (struct File *) calloc(1, sizeof (struct File));
197 if (sp->file == NULL) {
198 (void) puts("OUT OF MEMORY");
7fc06086
BJ
199 exit(0);
200 }
b3a57661 201 sp->file->stern = nat[sp->nationality]++;
c77f0542 202 }
6c17b19a
EW
203
204 if (active) {
b3a57661
EW
205 (void) puts("Synchronizing with the other players...");
206 (void) fflush(stdout);
207 Sync();
6c17b19a
EW
208 }
209 for (;;) {
210 foreachship(sp)
211 if (sp->file->captain[0] == 0 && !sp->file->struck
212 && sp->file->captured == 0)
b3a57661 213 break;
b3a57661
EW
214 if (sp >= ls) {
215 (void) puts("All ships taken in that scenario.");
216 foreachship(sp)
217 free((char *)sp->file);
6c17b19a 218 sync_close(0);
7fc06086 219 people = 0;
7fc06086
BJ
220 goto reprint;
221 }
6c17b19a
EW
222 if (!randomize) {
223 player = sp - cc->ship;
7fc06086 224 } else {
6c17b19a
EW
225 printf("%s\n\n", cc->name);
226 foreachship(sp)
227 printf(" %2d: %-10s %-15s (%-2d pts) %s\n",
228 sp - SHIP(0),
229 countryname[sp->nationality],
230 sp->shipname,
231 sp->specs->pts,
232 saywhat(sp, 1));
233 printf("\nWhich ship (0-%d)? ", cc->vessels-1);
234 (void) fflush(stdout);
235 if (scanf("%d", &player) != 1 || player < 0
236 || player >= cc->vessels) {
237 while (getchar() != '\n')
238 ;
239 (void) puts("Say what?");
240 player = -1;
241 } else
242 while (getchar() != '\n')
243 ;
7fc06086 244 }
6c17b19a
EW
245 if (player < 0)
246 continue;
247 Sync();
248 fp = SHIP(player)->file;
249 if (fp->captain[0] || fp->struck || fp->captured != 0)
250 (void) puts("That ship is taken.");
251 else
252 break;
c77f0542 253 }
7fc06086 254
b3a57661
EW
255 ms = SHIP(player);
256 mf = ms->file;
257 mc = ms->specs;
7fc06086 258
b3a57661
EW
259 (void) signal(SIGHUP, choke);
260 (void) signal(SIGINT, choke);
261 (void) signal(SIGQUIT, choke);
262 (void) signal(SIGCHLD, child);
263
6c17b19a 264 Write(W_BEGIN, ms, 0, 0, 0, 0, 0);
b3a57661
EW
265 Sync();
266 printf("Your ship is the %s, a %d gun %s (%s crew).\n",
267 ms->shipname, mc->guns, classname[mc->class],
268 qualname[mc->qual]);
269 if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
270 (void) strncpy(captain, nameptr, sizeof captain);
7fc06086 271 else {
b3a57661
EW
272 (void) printf("Your name, Captain? ");
273 (void) fflush(stdout);
274 (void) gets(captain);
275 if (!*captain)
276 (void) strcpy(captain, "no name");
c77f0542 277 }
b3a57661 278 captain[sizeof captain - 1] = '\0';
6c17b19a 279 Write(W_CAPTAIN, ms, 1, (int)captain, 0, 0, 0);
b3a57661 280 for (n = 0; n < 2; n++) {
6c17b19a
EW
281 char buf[10];
282
b3a57661
EW
283 printf("\nInitial broadside %s (grape, chain, round, double): ",
284 n ? "right" : "left");
285 (void) fflush(stdout);
6c17b19a
EW
286 (void) scanf("%s", buf);
287 switch (*buf) {
7fc06086 288 case 'g':
b3a57661 289 load = L_GRAPE;
7fc06086
BJ
290 break;
291 case 'c':
b3a57661 292 load = L_CHAIN;
7fc06086
BJ
293 break;
294 case 'r':
b3a57661 295 load = L_ROUND;
7fc06086
BJ
296 break;
297 case 'd':
b3a57661 298 load = L_DOUBLE;
7fc06086 299 break;
b3a57661
EW
300 default:
301 load = L_ROUND;
7fc06086 302 }
b3a57661
EW
303 if (n) {
304 mf->loadR = load;
305 mf->readyR = R_LOADED|R_INITIAL;
7fc06086 306 } else {
b3a57661
EW
307 mf->loadL = load;
308 mf->readyL = R_LOADED|R_INITIAL;
7fc06086 309 }
c77f0542 310 }
6c17b19a 311 if (!active && !nodriver) {
7fc06086 312 char num[10];
b3a57661 313 (void) sprintf(num, "%d", game);
7fc06086 314 if (!fork()) {
bba7bfeb
BJ
315 if (debug)
316 execl(DEBUGDRIVER, DRIVERNAME, num, 0);
317 else
318 execl(DRIVER, DRIVERNAME, num, 0);
7fc06086 319 perror(DRIVER);
7fc06086
BJ
320 exit(1);
321 }
c77f0542 322 }
7fc06086 323
6c17b19a
EW
324 foreachship(sp) {
325 sp->file->dir = sp->shipdir;
326 sp->file->row = sp->shiprow;
327 sp->file->col = sp->shipcol;
328 }
329 windspeed = cc->windspeed;
330 winddir = cc->winddir;
331
b3a57661 332 initscreen();
7fc06086
BJ
333
334 board();
b3a57661
EW
335 (void) sprintf(message, "Captain %s assuming command", captain);
336 Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0);
7fc06086 337
b3a57661 338 newturn();
c77f0542
CL
339}
340
b3a57661
EW
341leave(conditions)
342int conditions;
c77f0542 343{
b3a57661
EW
344 FILE *fp;
345 int people;
346 float net;
b3a57661
EW
347 char message[60];
348 register int n;
349 struct logs log[10], temp;
c77f0542 350
b3a57661
EW
351 (void) signal(SIGHUP, SIG_IGN);
352 (void) signal(SIGINT, SIG_IGN);
353 (void) signal(SIGQUIT, SIG_IGN);
354 (void) signal(SIGALRM, SIG_IGN);
355 (void) signal(SIGCHLD, SIG_IGN);
c77f0542 356
b3a57661 357 if (conditions != -1) {
6c17b19a
EW
358 (void) sprintf(message,"Captain %s relinquishing.",
359 mf->captain);
b3a57661 360 Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0);
6c17b19a 361 Write(W_END, ms, 0, 0, 0, 0, 0);
c77f0542 362
b3a57661
EW
363 if (fp = fopen(LOGFILE, "r+")) {
364 net = (float)mf->points / mc->pts;
365 people = getw(fp);
366 n = fread((char *)log, sizeof(struct logs), 10, fp);
367 for (; n < 10; n++)
368 log[n].l_name[0]
369 = log[n].l_uid
370 = log[n].l_shipnum
371 = log[n].l_gamenum
372 = log[n].l_netpoints = 0;
373 rewind(fp);
374 if (people < 0)
375 (void) putw(1, fp);
376 else
377 (void) putw(people + 1, fp);
378 for (n = 0; n < 10; n++)
379 if (net > (float) log[n].l_netpoints / scene[log[n].l_gamenum].ship[log[n].l_shipnum].specs->pts) {
380 (void) fwrite((char *)log,
381 sizeof (struct logs), n, fp);
6c17b19a 382 (void) strcpy(temp.l_name, mf->captain);
b3a57661
EW
383 temp.l_uid = getuid();
384 temp.l_shipnum = player;
385 temp.l_gamenum = game;
386 temp.l_netpoints = mf->points;
387 (void) fwrite((char *)&temp,
388 sizeof temp, 1, fp);
389 (void) fwrite((char *)&log[n],
390 sizeof (struct logs), 9-n, fp);
391 break;
392 }
393 (void) fclose(fp);
7fc06086 394 }
b3a57661
EW
395 if (done_curses) {
396 screen();
397 Signal("It looks like you've had it!",
398 (struct ship *)0);
399 switch (conditions) {
400 case LEAVE_QUIT:
401 break;
402 case LEAVE_CAPTURED:
403 Signal("Your ship was captured.",
404 (struct ship *)0);
405 break;
406 case LEAVE_HURRICAN:
407 Signal("Hurricane! All ships destroyed.",
408 (struct ship *)0);
409 break;
410 case LEAVE_DRIVER:
411 Signal("The driver died.", (struct ship *)0);
412 break;
413 default:
414 Signal("A funny thing happened (%d).",
415 (struct ship *)0, conditions);
416 }
417 } else {
418 if (conditions == LEAVE_DRIVER)
419 printf("The driver died.\n");
420 else
421 printf("leave: unknown code %d\n", conditions);
7fc06086 422 }
6c17b19a 423 Sync();
b3a57661
EW
424 }
425 if (done_curses) {
426 lastline();
427 nocrmode();
428 echo();
429 endwin();
c77f0542 430 }
b3a57661 431 exit(0);
7fc06086 432}
c77f0542 433
b3a57661 434choke()
c77f0542 435{
b3a57661 436 leave(LEAVE_QUIT);
c77f0542
CL
437}
438
b3a57661 439child()
c77f0542 440{
b3a57661
EW
441 union wait status;
442 int pid;
c77f0542 443
b3a57661
EW
444 (void) signal(SIGCHLD, SIG_IGN);
445 do {
446 pid = wait3(&status, WNOHANG|WUNTRACED, (struct rusage *)0);
447 if (pid < 0 || pid > 0 && !WIFSTOPPED(status))
448 leave(LEAVE_DRIVER);
449 } while (pid != 0);
450 (void) signal(SIGCHLD, child);
c77f0542 451}