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