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