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