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