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