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