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