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