sprintf doesn't necessarily return its first arg
[unix-history] / usr / src / games / sail / pl_main.c
CommitLineData
b5f0675e 1/*
1e008c14
DF
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
b5f0675e
EW
5 */
6
14665d6c 7#ifndef lint
1e008c14
DF
8static char sccsid[] = "@(#)pl_main.c 5.1 (Berkeley) %G%";
9#endif not lint
14665d6c
EW
10
11#include "player.h"
12#include <sys/types.h>
13#include <sys/wait.h>
14
15int choke(), child();
16
17/*ARGSUSED*/
18pl_main()
19{
20
21 if (!SCREENTEST()) {
22 printf("Can't sail on this terminal.\n");
23 exit(1);
24 }
25 initialize();
26 Signal("Aye aye, Sir", (struct ship *)0);
27 play();
f52d88b6 28 return 0; /* for lint, play() never returns */
14665d6c
EW
29}
30
31initialize()
32{
33 register struct File *fp;
34 register struct ship *sp;
35 char captain[80];
36 char message[60];
37 int load;
38 register int n;
39 char *nameptr;
40 int nat[NNATION];
41
42 if (game < 0) {
43 (void) puts("Choose a scenario:\n");
44 (void) puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
45 for (n = 0; n < NSCENE; n++) {
46 /* ( */
47 printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
48 sync_exists(n) ? "YES" : "no",
49 scene[n].name);
50 }
51reprint:
52 printf("\nScenario number? ");
53 (void) fflush(stdout);
54 (void) scanf("%d", &game);
55 while (getchar() != '\n')
56 ;
57 }
58 if (game < 0 || game >= NSCENE) {
59 (void) puts("Very funny.");
60 exit(1);
61 }
62 cc = &scene[game];
63 ls = SHIP(cc->vessels);
64
65 for (n = 0; n < NNATION; n++)
66 nat[n] = 0;
67 foreachship(sp) {
68 if (sp->file == NULL &&
69 (sp->file = (struct File *)calloc(1, sizeof (struct File))) == NULL) {
70 (void) puts("OUT OF MEMORY");
71 exit(1);
72 }
73 sp->file->index = sp - SHIP(0);
74 sp->file->stern = nat[sp->nationality]++;
75 sp->file->dir = sp->shipdir;
76 sp->file->row = sp->shiprow;
77 sp->file->col = sp->shipcol;
78 }
79 windspeed = cc->windspeed;
80 winddir = cc->winddir;
81
82 (void) signal(SIGHUP, choke);
83 (void) signal(SIGINT, choke);
84
85 hasdriver = sync_exists(game);
86 if (sync_open() < 0) {
87 perror("sail: syncfile");
88 exit(1);
89 }
90
91 if (hasdriver) {
92 (void) puts("Synchronizing with the other players...");
93 (void) fflush(stdout);
94 if (Sync() < 0)
95 leave(LEAVE_SYNC);
96 }
97 for (;;) {
98 foreachship(sp)
99 if (sp->file->captain[0] == 0 && !sp->file->struck
100 && sp->file->captured == 0)
101 break;
102 if (sp >= ls) {
103 (void) puts("All ships taken in that scenario.");
104 foreachship(sp)
105 free((char *)sp->file);
106 sync_close(0);
107 people = 0;
108 goto reprint;
109 }
110 if (randomize) {
111 player = sp - SHIP(0);
112 } else {
113 printf("%s\n\n", cc->name);
114 foreachship(sp)
115 printf(" %2d: %-10s %-15s (%-2d pts) %s\n",
116 sp->file->index,
117 countryname[sp->nationality],
118 sp->shipname,
119 sp->specs->pts,
120 saywhat(sp, 1));
121 printf("\nWhich ship (0-%d)? ", cc->vessels-1);
122 (void) fflush(stdout);
123 if (scanf("%d", &player) != 1 || player < 0
124 || player >= cc->vessels) {
125 while (getchar() != '\n')
126 ;
127 (void) puts("Say what?");
128 player = -1;
129 } else
130 while (getchar() != '\n')
131 ;
132 }
133 if (player < 0)
134 continue;
135 if (Sync() < 0)
136 leave(LEAVE_SYNC);
137 fp = SHIP(player)->file;
138 if (fp->captain[0] || fp->struck || fp->captured != 0)
139 (void) puts("That ship is taken.");
140 else
141 break;
142 }
143
144 ms = SHIP(player);
145 mf = ms->file;
146 mc = ms->specs;
147
148 Write(W_BEGIN, ms, 0, 0, 0, 0, 0);
149 if (Sync() < 0)
150 leave(LEAVE_SYNC);
151
152 (void) signal(SIGCHLD, child);
153 if (!hasdriver)
154 switch (fork()) {
155 case 0:
156 longjmp(restart, MODE_DRIVER);
157 /*NOTREACHED*/
158 case -1:
159 perror("fork");
160 leave(LEAVE_FORK);
161 break;
162 default:
163 hasdriver++;
164 }
165
166 printf("Your ship is the %s, a %d gun %s (%s crew).\n",
167 ms->shipname, mc->guns, classname[mc->class],
168 qualname[mc->qual]);
169 if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
170 (void) strncpy(captain, nameptr, sizeof captain);
171 else {
172 (void) printf("Your name, Captain? ");
173 (void) fflush(stdout);
174 (void) gets(captain);
175 if (!*captain)
176 (void) strcpy(captain, "no name");
177 }
178 captain[sizeof captain - 1] = '\0';
179 Write(W_CAPTAIN, ms, 1, (int)captain, 0, 0, 0);
180 for (n = 0; n < 2; n++) {
181 char buf[10];
182
183 printf("\nInitial broadside %s (grape, chain, round, double): ",
184 n ? "right" : "left");
185 (void) fflush(stdout);
186 (void) scanf("%s", buf);
187 switch (*buf) {
188 case 'g':
189 load = L_GRAPE;
190 break;
191 case 'c':
192 load = L_CHAIN;
193 break;
194 case 'r':
195 load = L_ROUND;
196 break;
197 case 'd':
198 load = L_DOUBLE;
199 break;
200 default:
201 load = L_ROUND;
202 }
203 if (n) {
204 mf->loadR = load;
205 mf->readyR = R_LOADED|R_INITIAL;
206 } else {
207 mf->loadL = load;
208 mf->readyL = R_LOADED|R_INITIAL;
209 }
210 }
211
212 initscreen();
213 draw_board();
214 (void) sprintf(message, "Captain %s assuming command", captain);
215 Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0);
216 newturn();
217}