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