]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/pl_main.c
1eb08f345f0481cbef332d374b8a2f1bb0c24b5b
[bsdgames-darwin.git] / sail / pl_main.c
1 /* $NetBSD: pl_main.c,v 1.9 2001/01/01 22:23:29 jwise 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)pl_main.c 8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: pl_main.c,v 1.9 2001/01/01 22:23:29 jwise Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include "player.h"
46 #include <sys/types.h>
47 #include <sys/wait.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50
51 /*ARGSUSED*/
52 int
53 pl_main(void)
54 {
55
56 initialize();
57 Msg("Aye aye, Sir");
58 play();
59 return 0; /* for lint, play() never returns */
60 }
61
62 void
63 initialize(void)
64 {
65 register struct File *fp;
66 register struct ship *sp;
67 char captain[80];
68 char message[60];
69 int load;
70 register int n;
71 char *nameptr;
72 int nat[NNATION];
73
74 if (game < 0) {
75 puts("Choose a scenario:\n");
76 puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
77 for (n = 0; n < NSCENE; n++) {
78 /* ( */
79 printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
80 sync_exists(n) ? "YES" : "no",
81 scene[n].name);
82 }
83 reprint:
84 printf("\nScenario number? ");
85 fflush(stdout);
86 scanf("%d", &game);
87 while (getchar() != '\n')
88 ;
89 }
90 if (game < 0 || game >= NSCENE) {
91 puts("Very funny.");
92 exit(1);
93 }
94 cc = &scene[game];
95 ls = SHIP(cc->vessels);
96
97 for (n = 0; n < NNATION; n++)
98 nat[n] = 0;
99 foreachship(sp) {
100 if (sp->file == NULL &&
101 (sp->file = (struct File *)calloc(1, sizeof (struct File))) == NULL) {
102 puts("OUT OF MEMORY");
103 exit(1);
104 }
105 sp->file->index = sp - SHIP(0);
106 sp->file->stern = nat[sp->nationality]++;
107 sp->file->dir = sp->shipdir;
108 sp->file->row = sp->shiprow;
109 sp->file->col = sp->shipcol;
110 }
111 windspeed = cc->windspeed;
112 winddir = cc->winddir;
113
114 signal(SIGHUP, choke);
115 signal(SIGINT, choke);
116
117 hasdriver = sync_exists(game);
118 if (sync_open() < 0) {
119 perror("sail: syncfile");
120 exit(1);
121 }
122
123 if (hasdriver) {
124 puts("Synchronizing with the other players...");
125 fflush(stdout);
126 if (Sync() < 0)
127 leave(LEAVE_SYNC);
128 }
129 for (;;) {
130 foreachship(sp)
131 if (sp->file->captain[0] == 0 && !sp->file->struck
132 && sp->file->captured == 0)
133 break;
134 if (sp >= ls) {
135 puts("All ships taken in that scenario.");
136 foreachship(sp)
137 free((char *)sp->file);
138 sync_close(0);
139 people = 0;
140 goto reprint;
141 }
142 if (randomize) {
143 player = sp - SHIP(0);
144 } else {
145 printf("%s\n\n", cc->name);
146 foreachship(sp)
147 printf(" %2d: %-10s %-15s (%-2d pts) %s\n",
148 sp->file->index,
149 countryname[sp->nationality],
150 sp->shipname,
151 sp->specs->pts,
152 saywhat(sp, 1));
153 printf("\nWhich ship (0-%d)? ", cc->vessels-1);
154 fflush(stdout);
155 if (scanf("%d", &player) != 1 || player < 0
156 || player >= cc->vessels) {
157 while (getchar() != '\n')
158 ;
159 puts("Say what?");
160 player = -1;
161 } else
162 while (getchar() != '\n')
163 ;
164 }
165 if (player < 0)
166 continue;
167 if (Sync() < 0)
168 leave(LEAVE_SYNC);
169 fp = SHIP(player)->file;
170 if (fp->captain[0] || fp->struck || fp->captured != 0)
171 puts("That ship is taken.");
172 else
173 break;
174 }
175
176 ms = SHIP(player);
177 mf = ms->file;
178 mc = ms->specs;
179
180 Write(W_BEGIN, ms, 0, 0, 0, 0);
181 if (Sync() < 0)
182 leave(LEAVE_SYNC);
183
184 signal(SIGCHLD, child);
185 if (!hasdriver)
186 switch (fork()) {
187 case 0:
188 longjmp(restart, MODE_DRIVER);
189 /*NOTREACHED*/
190 case -1:
191 perror("fork");
192 leave(LEAVE_FORK);
193 break;
194 default:
195 hasdriver++;
196 }
197
198 printf("Your ship is the %s, a %d gun %s (%s crew).\n",
199 ms->shipname, mc->guns, classname[mc->class],
200 qualname[mc->qual]);
201 if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
202 strncpy(captain, nameptr, sizeof captain);
203 else {
204 printf("Your name, Captain? ");
205 fflush(stdout);
206 fgets(captain, sizeof captain, stdin);
207 if (!*captain)
208 strcpy(captain, "no name");
209 else
210 captain[strlen(captain) - 1] = '\0';
211 }
212 captain[sizeof captain - 1] = '\0';
213 Writestr(W_CAPTAIN, ms, captain);
214 for (n = 0; n < 2; n++) {
215 char buf[10];
216
217 printf("\nInitial broadside %s (grape, chain, round, double): ",
218 n ? "right" : "left");
219 fflush(stdout);
220 scanf("%s", buf);
221 switch (*buf) {
222 case 'g':
223 load = L_GRAPE;
224 break;
225 case 'c':
226 load = L_CHAIN;
227 break;
228 case 'r':
229 load = L_ROUND;
230 break;
231 case 'd':
232 load = L_DOUBLE;
233 break;
234 default:
235 load = L_ROUND;
236 }
237 if (n) {
238 mf->loadR = load;
239 mf->readyR = R_LOADED|R_INITIAL;
240 } else {
241 mf->loadL = load;
242 mf->readyL = R_LOADED|R_INITIAL;
243 }
244 }
245
246 initscreen();
247 draw_board();
248 snprintf(message, sizeof message, "Captain %s assuming command",
249 captain);
250 Writestr(W_SIGNAL, ms, message);
251 newturn(0);
252 }