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