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