Commit | Line | Data |
---|---|---|
c77f0542 | 1 | #ifndef lint |
0c30e1ed | 2 | static char *sccsid = "@(#)pl_1.c 1.15 83/10/28"; |
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 | 9 | int choke(), child(); |
c77f0542 | 10 | |
b3a57661 EW |
11 | /*ARGSUSED*/ |
12 | main(argc, argv) | |
13 | int argc; | |
14 | char **argv; | |
c77f0542 | 15 | { |
b3a57661 | 16 | char nodrive = 0, randomize = 0, debug = 0; |
b3a57661 | 17 | extern char _sobuf[]; |
c77f0542 | 18 | |
b3a57661 | 19 | setbuf(stdout, _sobuf); |
9224751f | 20 | isplayer = 1; |
7fc06086 | 21 | |
b3a57661 EW |
22 | while (*++argv && **argv == '-') |
23 | switch (*++*argv) { | |
24 | case 'd': | |
25 | nodrive = 1; | |
26 | break; | |
27 | case 'D': | |
28 | debug++; | |
29 | break; | |
30 | case 'x': | |
31 | randomize = 1; | |
32 | break; | |
33 | default: | |
34 | printf("Unknown flag '%s'\n",*argv); | |
35 | break; | |
7fc06086 | 36 | } |
b3a57661 EW |
37 | if (*argv) |
38 | game = atoi(*argv); | |
39 | else | |
40 | game = -1; | |
41 | initialize(nodrive, randomize, debug); | |
42 | Signal("Aye aye, Sir", (struct ship *)0); | |
fe0cae24 | 43 | play(); |
c77f0542 CL |
44 | } |
45 | ||
bba7bfeb BJ |
46 | initialize(nodriver, randomize, debug) |
47 | char randomize, nodriver, debug; | |
c77f0542 | 48 | { |
b3a57661 EW |
49 | register struct File *fp; |
50 | register struct ship *sp; | |
6c17b19a | 51 | char captain[80]; |
7fc06086 | 52 | char message[60]; |
b3a57661 | 53 | int load; |
6c17b19a | 54 | int active; |
7fc06086 | 55 | register int n; |
7fc06086 | 56 | char *nameptr; |
b3a57661 | 57 | int nat[NNATION]; |
c77f0542 | 58 | |
b3a57661 | 59 | (void) srand(getpid()); |
c77f0542 | 60 | |
7fc06086 | 61 | if (game < 0) { |
b3a57661 EW |
62 | (void) puts("Choose a scenario:\n"); |
63 | (void) puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE"); | |
64 | for (n = 0; n < NSCENE; n++) { | |
6c17b19a EW |
65 | /* ( */ |
66 | printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels, | |
67 | sync_exists(n) ? "YES" : "no", | |
68 | scene[n].name); | |
7fc06086 | 69 | } |
c77f0542 | 70 | reprint: |
7fc06086 | 71 | printf("\nScenario number? "); |
b3a57661 EW |
72 | (void) fflush(stdout); |
73 | (void) scanf("%d", &game); | |
74 | while (getchar() != '\n') | |
75 | ; | |
7fc06086 | 76 | } |
b3a57661 EW |
77 | if (game < 0 || game >= NSCENE) { |
78 | (void) puts("Very funny."); | |
7fc06086 BJ |
79 | exit(1); |
80 | } | |
b3a57661 EW |
81 | cc = &scene[game]; |
82 | ls = cc->ship + cc->vessels; | |
83 | ||
6c17b19a EW |
84 | active = sync_exists(game); |
85 | if (sync_open() < 0) { | |
86 | perror("sail: syncfile"); | |
87 | exit(1); | |
c77f0542 | 88 | } |
b3a57661 EW |
89 | |
90 | for (n = 0; n < NNATION; n++) | |
91 | nat[n] = 0; | |
92 | foreachship(sp) { | |
93 | sp->file = (struct File *) calloc(1, sizeof (struct File)); | |
94 | if (sp->file == NULL) { | |
95 | (void) puts("OUT OF MEMORY"); | |
7fc06086 BJ |
96 | exit(0); |
97 | } | |
b3a57661 | 98 | sp->file->stern = nat[sp->nationality]++; |
0c30e1ed EW |
99 | sp->file->dir = sp->shipdir; |
100 | sp->file->row = sp->shiprow; | |
101 | sp->file->col = sp->shipcol; | |
c77f0542 | 102 | } |
0c30e1ed EW |
103 | windspeed = cc->windspeed; |
104 | winddir = cc->winddir; | |
6c17b19a EW |
105 | |
106 | if (active) { | |
b3a57661 EW |
107 | (void) puts("Synchronizing with the other players..."); |
108 | (void) fflush(stdout); | |
109 | Sync(); | |
6c17b19a EW |
110 | } |
111 | for (;;) { | |
112 | foreachship(sp) | |
113 | if (sp->file->captain[0] == 0 && !sp->file->struck | |
114 | && sp->file->captured == 0) | |
b3a57661 | 115 | break; |
b3a57661 EW |
116 | if (sp >= ls) { |
117 | (void) puts("All ships taken in that scenario."); | |
118 | foreachship(sp) | |
119 | free((char *)sp->file); | |
6c17b19a | 120 | sync_close(0); |
7fc06086 | 121 | people = 0; |
7fc06086 BJ |
122 | goto reprint; |
123 | } | |
6c17b19a EW |
124 | if (!randomize) { |
125 | player = sp - cc->ship; | |
7fc06086 | 126 | } else { |
6c17b19a EW |
127 | printf("%s\n\n", cc->name); |
128 | foreachship(sp) | |
129 | printf(" %2d: %-10s %-15s (%-2d pts) %s\n", | |
130 | sp - SHIP(0), | |
131 | countryname[sp->nationality], | |
132 | sp->shipname, | |
133 | sp->specs->pts, | |
134 | saywhat(sp, 1)); | |
135 | printf("\nWhich ship (0-%d)? ", cc->vessels-1); | |
136 | (void) fflush(stdout); | |
137 | if (scanf("%d", &player) != 1 || player < 0 | |
138 | || player >= cc->vessels) { | |
139 | while (getchar() != '\n') | |
140 | ; | |
141 | (void) puts("Say what?"); | |
142 | player = -1; | |
143 | } else | |
144 | while (getchar() != '\n') | |
145 | ; | |
7fc06086 | 146 | } |
6c17b19a EW |
147 | if (player < 0) |
148 | continue; | |
149 | Sync(); | |
150 | fp = SHIP(player)->file; | |
151 | if (fp->captain[0] || fp->struck || fp->captured != 0) | |
152 | (void) puts("That ship is taken."); | |
153 | else | |
154 | break; | |
c77f0542 | 155 | } |
7fc06086 | 156 | |
b3a57661 EW |
157 | ms = SHIP(player); |
158 | mf = ms->file; | |
159 | mc = ms->specs; | |
7fc06086 | 160 | |
b3a57661 EW |
161 | (void) signal(SIGHUP, choke); |
162 | (void) signal(SIGINT, choke); | |
163 | (void) signal(SIGQUIT, choke); | |
164 | (void) signal(SIGCHLD, child); | |
165 | ||
6c17b19a | 166 | Write(W_BEGIN, ms, 0, 0, 0, 0, 0); |
b3a57661 EW |
167 | Sync(); |
168 | printf("Your ship is the %s, a %d gun %s (%s crew).\n", | |
169 | ms->shipname, mc->guns, classname[mc->class], | |
170 | qualname[mc->qual]); | |
171 | if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr) | |
172 | (void) strncpy(captain, nameptr, sizeof captain); | |
7fc06086 | 173 | else { |
b3a57661 EW |
174 | (void) printf("Your name, Captain? "); |
175 | (void) fflush(stdout); | |
176 | (void) gets(captain); | |
177 | if (!*captain) | |
178 | (void) strcpy(captain, "no name"); | |
c77f0542 | 179 | } |
b3a57661 | 180 | captain[sizeof captain - 1] = '\0'; |
6c17b19a | 181 | Write(W_CAPTAIN, ms, 1, (int)captain, 0, 0, 0); |
b3a57661 | 182 | for (n = 0; n < 2; n++) { |
6c17b19a EW |
183 | char buf[10]; |
184 | ||
b3a57661 EW |
185 | printf("\nInitial broadside %s (grape, chain, round, double): ", |
186 | n ? "right" : "left"); | |
187 | (void) fflush(stdout); | |
6c17b19a EW |
188 | (void) scanf("%s", buf); |
189 | switch (*buf) { | |
7fc06086 | 190 | case 'g': |
b3a57661 | 191 | load = L_GRAPE; |
7fc06086 BJ |
192 | break; |
193 | case 'c': | |
b3a57661 | 194 | load = L_CHAIN; |
7fc06086 BJ |
195 | break; |
196 | case 'r': | |
b3a57661 | 197 | load = L_ROUND; |
7fc06086 BJ |
198 | break; |
199 | case 'd': | |
b3a57661 | 200 | load = L_DOUBLE; |
7fc06086 | 201 | break; |
b3a57661 EW |
202 | default: |
203 | load = L_ROUND; | |
7fc06086 | 204 | } |
b3a57661 EW |
205 | if (n) { |
206 | mf->loadR = load; | |
207 | mf->readyR = R_LOADED|R_INITIAL; | |
7fc06086 | 208 | } else { |
b3a57661 EW |
209 | mf->loadL = load; |
210 | mf->readyL = R_LOADED|R_INITIAL; | |
7fc06086 | 211 | } |
c77f0542 | 212 | } |
6c17b19a | 213 | if (!active && !nodriver) { |
7fc06086 | 214 | char num[10]; |
b3a57661 | 215 | (void) sprintf(num, "%d", game); |
7fc06086 | 216 | if (!fork()) { |
0c30e1ed EW |
217 | execl(DRIVER1, DRIVERNAME, num, 0); |
218 | execl(DRIVER2, DRIVERNAME, num, 0); | |
219 | execl(DRIVER3, DRIVERNAME, num, 0); | |
220 | perror(DRIVERNAME); | |
7fc06086 BJ |
221 | exit(1); |
222 | } | |
c77f0542 | 223 | } |
7fc06086 | 224 | |
b3a57661 | 225 | initscreen(); |
7fc06086 | 226 | |
fe0cae24 | 227 | draw_board(); |
b3a57661 EW |
228 | (void) sprintf(message, "Captain %s assuming command", captain); |
229 | Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0); | |
7fc06086 | 230 | |
b3a57661 | 231 | newturn(); |
c77f0542 CL |
232 | } |
233 | ||
b3a57661 EW |
234 | leave(conditions) |
235 | int conditions; | |
c77f0542 | 236 | { |
b3a57661 EW |
237 | FILE *fp; |
238 | int people; | |
239 | float net; | |
b3a57661 EW |
240 | char message[60]; |
241 | register int n; | |
242 | struct logs log[10], temp; | |
c77f0542 | 243 | |
b3a57661 EW |
244 | (void) signal(SIGHUP, SIG_IGN); |
245 | (void) signal(SIGINT, SIG_IGN); | |
246 | (void) signal(SIGQUIT, SIG_IGN); | |
247 | (void) signal(SIGALRM, SIG_IGN); | |
248 | (void) signal(SIGCHLD, SIG_IGN); | |
c77f0542 | 249 | |
b3a57661 | 250 | if (conditions != -1) { |
6c17b19a EW |
251 | (void) sprintf(message,"Captain %s relinquishing.", |
252 | mf->captain); | |
b3a57661 | 253 | Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0); |
6c17b19a | 254 | Write(W_END, ms, 0, 0, 0, 0, 0); |
c77f0542 | 255 | |
b3a57661 EW |
256 | if (fp = fopen(LOGFILE, "r+")) { |
257 | net = (float)mf->points / mc->pts; | |
258 | people = getw(fp); | |
259 | n = fread((char *)log, sizeof(struct logs), 10, fp); | |
260 | for (; n < 10; n++) | |
261 | log[n].l_name[0] | |
262 | = log[n].l_uid | |
263 | = log[n].l_shipnum | |
264 | = log[n].l_gamenum | |
265 | = log[n].l_netpoints = 0; | |
266 | rewind(fp); | |
267 | if (people < 0) | |
268 | (void) putw(1, fp); | |
269 | else | |
270 | (void) putw(people + 1, fp); | |
271 | for (n = 0; n < 10; n++) | |
272 | if (net > (float) log[n].l_netpoints / scene[log[n].l_gamenum].ship[log[n].l_shipnum].specs->pts) { | |
273 | (void) fwrite((char *)log, | |
274 | sizeof (struct logs), n, fp); | |
6c17b19a | 275 | (void) strcpy(temp.l_name, mf->captain); |
b3a57661 EW |
276 | temp.l_uid = getuid(); |
277 | temp.l_shipnum = player; | |
278 | temp.l_gamenum = game; | |
279 | temp.l_netpoints = mf->points; | |
280 | (void) fwrite((char *)&temp, | |
281 | sizeof temp, 1, fp); | |
282 | (void) fwrite((char *)&log[n], | |
283 | sizeof (struct logs), 9-n, fp); | |
284 | break; | |
285 | } | |
286 | (void) fclose(fp); | |
7fc06086 | 287 | } |
b3a57661 | 288 | if (done_curses) { |
b3a57661 EW |
289 | Signal("It looks like you've had it!", |
290 | (struct ship *)0); | |
291 | switch (conditions) { | |
292 | case LEAVE_QUIT: | |
293 | break; | |
294 | case LEAVE_CAPTURED: | |
295 | Signal("Your ship was captured.", | |
296 | (struct ship *)0); | |
297 | break; | |
298 | case LEAVE_HURRICAN: | |
299 | Signal("Hurricane! All ships destroyed.", | |
300 | (struct ship *)0); | |
301 | break; | |
302 | case LEAVE_DRIVER: | |
303 | Signal("The driver died.", (struct ship *)0); | |
304 | break; | |
305 | default: | |
306 | Signal("A funny thing happened (%d).", | |
307 | (struct ship *)0, conditions); | |
308 | } | |
309 | } else { | |
fe0cae24 EW |
310 | switch (conditions) { |
311 | case LEAVE_QUIT: | |
312 | break; | |
313 | case LEAVE_DRIVER: | |
b3a57661 | 314 | printf("The driver died.\n"); |
fe0cae24 EW |
315 | break; |
316 | default: | |
317 | printf("A funny thing happened (%d).\n", | |
318 | conditions); | |
319 | } | |
7fc06086 | 320 | } |
6c17b19a | 321 | Sync(); |
b3a57661 | 322 | } |
fe0cae24 | 323 | cleanupscreen(); |
b3a57661 | 324 | exit(0); |
7fc06086 | 325 | } |
c77f0542 | 326 | |
b3a57661 | 327 | choke() |
c77f0542 | 328 | { |
b3a57661 | 329 | leave(LEAVE_QUIT); |
c77f0542 CL |
330 | } |
331 | ||
b3a57661 | 332 | child() |
c77f0542 | 333 | { |
b3a57661 EW |
334 | union wait status; |
335 | int pid; | |
c77f0542 | 336 | |
b3a57661 EW |
337 | (void) signal(SIGCHLD, SIG_IGN); |
338 | do { | |
339 | pid = wait3(&status, WNOHANG|WUNTRACED, (struct rusage *)0); | |
340 | if (pid < 0 || pid > 0 && !WIFSTOPPED(status)) | |
341 | leave(LEAVE_DRIVER); | |
342 | } while (pid != 0); | |
343 | (void) signal(SIGCHLD, child); | |
c77f0542 | 344 | } |