1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char sccsid[] = "@(#)pl_main.c 8.1 (Berkeley) 05/31/93";
10 #endif /* not lint */
11
12 #include "player.h"
13 #include <sys/types.h>
14 #include <sys/wait.h>
15
16 void choke(), child();
17
18 /*ARGSUSED*/
pl_main()19 pl_main()
20 {
21
22 if (!SCREENTEST()) {
23 printf("Can't sail on this terminal.\n");
24 exit(1);
25 }
26 initialize();
27 Signal("Aye aye, Sir", (struct ship *)0);
28 play();
29 return 0; /* for lint, play() never returns */
30 }
31
initialize()32 initialize()
33 {
34 register struct File *fp;
35 register struct ship *sp;
36 char captain[80];
37 char message[60];
38 int load;
39 register int n;
40 char *nameptr;
41 int nat[NNATION];
42
43 if (game < 0) {
44 (void) puts("Choose a scenario:\n");
45 (void) puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
46 for (n = 0; n < NSCENE; n++) {
47 /* ( */
48 printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
49 sync_exists(n) ? "YES" : "no",
50 scene[n].name);
51 }
52 reprint:
53 printf("\nScenario number? ");
54 (void) fflush(stdout);
55 (void) scanf("%d", &game);
56 while (getchar() != '\n')
57 ;
58 }
59 if (game < 0 || game >= NSCENE) {
60 (void) puts("Very funny.");
61 exit(1);
62 }
63 cc = &scene[game];
64 ls = SHIP(cc->vessels);
65
66 for (n = 0; n < NNATION; n++)
67 nat[n] = 0;
68 foreachship(sp) {
69 if (sp->file == NULL &&
70 (sp->file = (struct File *)calloc(1, sizeof (struct File))) == NULL) {
71 (void) puts("OUT OF MEMORY");
72 exit(1);
73 }
74 sp->file->index = sp - SHIP(0);
75 sp->file->stern = nat[sp->nationality]++;
76 sp->file->dir = sp->shipdir;
77 sp->file->row = sp->shiprow;
78 sp->file->col = sp->shipcol;
79 }
80 windspeed = cc->windspeed;
81 winddir = cc->winddir;
82
83 (void) signal(SIGHUP, choke);
84 (void) signal(SIGINT, choke);
85
86 hasdriver = sync_exists(game);
87 if (sync_open() < 0) {
88 perror("sail: syncfile");
89 exit(1);
90 }
91
92 if (hasdriver) {
93 (void) puts("Synchronizing with the other players...");
94 (void) fflush(stdout);
95 if (Sync() < 0)
96 leave(LEAVE_SYNC);
97 }
98 for (;;) {
99 foreachship(sp)
100 if (sp->file->captain[0] == 0 && !sp->file->struck
101 && sp->file->captured == 0)
102 break;
103 if (sp >= ls) {
104 (void) puts("All ships taken in that scenario.");
105 foreachship(sp)
106 free((char *)sp->file);
107 sync_close(0);
108 people = 0;
109 goto reprint;
110 }
111 if (randomize) {
112 player = sp - SHIP(0);
113 } else {
114 printf("%s\n\n", cc->name);
115 foreachship(sp)
116 printf(" %2d: %-10s %-15s (%-2d pts) %s\n",
117 sp->file->index,
118 countryname[sp->nationality],
119 sp->shipname,
120 sp->specs->pts,
121 saywhat(sp, 1));
122 printf("\nWhich ship (0-%d)? ", cc->vessels-1);
123 (void) fflush(stdout);
124 if (scanf("%d", &player) != 1 || player < 0
125 || player >= cc->vessels) {
126 while (getchar() != '\n')
127 ;
128 (void) puts("Say what?");
129 player = -1;
130 } else
131 while (getchar() != '\n')
132 ;
133 }
134 if (player < 0)
135 continue;
136 if (Sync() < 0)
137 leave(LEAVE_SYNC);
138 fp = SHIP(player)->file;
139 if (fp->captain[0] || fp->struck || fp->captured != 0)
140 (void) puts("That ship is taken.");
141 else
142 break;
143 }
144
145 ms = SHIP(player);
146 mf = ms->file;
147 mc = ms->specs;
148
149 Write(W_BEGIN, ms, 0, 0, 0, 0, 0);
150 if (Sync() < 0)
151 leave(LEAVE_SYNC);
152
153 (void) signal(SIGCHLD, child);
154 if (!hasdriver)
155 switch (fork()) {
156 case 0:
157 longjmp(restart, MODE_DRIVER);
158 /*NOTREACHED*/
159 case -1:
160 perror("fork");
161 leave(LEAVE_FORK);
162 break;
163 default:
164 hasdriver++;
165 }
166
167 printf("Your ship is the %s, a %d gun %s (%s crew).\n",
168 ms->shipname, mc->guns, classname[mc->class],
169 qualname[mc->qual]);
170 if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
171 (void) strncpy(captain, nameptr, sizeof captain);
172 else {
173 (void) printf("Your name, Captain? ");
174 (void) fflush(stdout);
175 (void) gets(captain);
176 if (!*captain)
177 (void) strcpy(captain, "no name");
178 }
179 captain[sizeof captain - 1] = '\0';
180 Write(W_CAPTAIN, ms, 1, (int)captain, 0, 0, 0);
181 for (n = 0; n < 2; n++) {
182 char buf[10];
183
184 printf("\nInitial broadside %s (grape, chain, round, double): ",
185 n ? "right" : "left");
186 (void) fflush(stdout);
187 (void) scanf("%s", buf);
188 switch (*buf) {
189 case 'g':
190 load = L_GRAPE;
191 break;
192 case 'c':
193 load = L_CHAIN;
194 break;
195 case 'r':
196 load = L_ROUND;
197 break;
198 case 'd':
199 load = L_DOUBLE;
200 break;
201 default:
202 load = L_ROUND;
203 }
204 if (n) {
205 mf->loadR = load;
206 mf->readyR = R_LOADED|R_INITIAL;
207 } else {
208 mf->loadL = load;
209 mf->readyL = R_LOADED|R_INITIAL;
210 }
211 }
212
213 initscreen();
214 draw_board();
215 (void) sprintf(message, "Captain %s assuming command", captain);
216 Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0);
217 newturn();
218 }
219