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