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