xref: /netbsd/games/sail/pl_5.c (revision bf9ec67e)
1 /*	$NetBSD: pl_5.c,v 1.15 2001/02/05 01:10:11 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)pl_5.c	8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: pl_5.c,v 1.15 2001/02/05 01:10:11 christos Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <ctype.h>
46 #include <curses.h>
47 #include <signal.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include "extern.h"
51 #include "player.h"
52 #include "display.h"
53 
54 #define turnfirst(x) (*x == 'r' || *x == 'l')
55 
56 static void	parties(struct ship *, int *, int, int);
57 
58 void
59 acceptmove(void)
60 {
61 	int ta;
62 	int ma;
63 	char af;
64 	int moved = 0;
65 	int vma, dir;
66 	char prompt[60];
67 	char buf[60], last = '\0';
68 	char *p;
69 
70 	if (!mc->crew3 || snagged(ms) || !windspeed) {
71 		Msg("Unable to move");
72 		return;
73 	}
74 
75 	ta = maxturns(ms, &af);
76 	ma = maxmove(ms, mf->dir, 0);
77 	sprintf(prompt, "move (%d,%c%d): ", ma, af ? '\'' : ' ', ta);
78 	sgetstr(prompt, buf, sizeof buf);
79 	dir = mf->dir;
80 	vma = ma;
81 	for (p = buf; *p; p++)
82 		switch (*p) {
83 		case 'l':
84 			dir -= 2;
85 		case 'r':
86 			if (++dir == 0)
87 				dir = 8;
88 			else if (dir == 9)
89 				dir = 1;
90 			if (last == 't') {
91 				Msg("Ship can't turn that fast.");
92 				*p-- = '\0';
93 			}
94 			last = 't';
95 			ma--;
96 			ta--;
97 			vma = min(ma, maxmove(ms, dir, 0));
98 			if ((ta < 0 && moved) || (vma < 0 && moved))
99 				*p-- = '\0';
100 			break;
101 		case 'b':
102 			ma--;
103 			vma--;
104 			last = 'b';
105 			if ((ta < 0 && moved) || (vma < 0 && moved))
106 				*p-- = '\0';
107 			break;
108 		case '0':
109 		case 'd':
110 			*p-- = '\0';
111 			break;
112 		case '\n':
113 			*p-- = '\0';
114 			break;
115 		case '1': case '2': case '3': case '4':
116 		case '5': case '6': case '7':
117 			if (last == '0') {
118 				Msg("Can't move that fast.");
119 				*p-- = '\0';
120 			}
121 			last = '0';
122 			moved = 1;
123 			ma -= *p - '0';
124 			vma -= *p - '0';
125 			if ((ta < 0 && moved) || (vma < 0 && moved))
126 				*p-- = '\0';
127 			break;
128 		default:
129 			if (!isspace(*p)) {
130 				Msg("Input error.");
131 				*p-- = '\0';
132 			}
133 		}
134 	if ((ta < 0 && moved) || (vma < 0 && moved)
135 	    || (af && turnfirst(buf) && moved)) {
136 		Msg("Movement error.");
137 		if (ta < 0 && moved) {
138 			if (mf->FS == 1) {
139 				Write(W_FS, ms, 0, 0, 0, 0);
140 				Msg("No hands to set full sails.");
141 			}
142 		} else if (ma >= 0)
143 			buf[1] = '\0';
144 	}
145 	if (af && !moved) {
146 		if (mf->FS == 1) {
147 			Write(W_FS, ms, 0, 0, 0, 0);
148 			Msg("No hands to set full sails.");
149 		}
150 	}
151 	if (*buf)
152 		strcpy(movebuf, buf);
153 	else
154 		strcpy(movebuf, "d");
155 	Writestr(W_MOVE, ms, movebuf);
156 	Msg("Helm: %s.", movebuf);
157 }
158 
159 void
160 acceptboard(void)
161 {
162 	struct ship *sp;
163 	int n;
164 	int crew[3];
165 	int men = 0;
166 	char c;
167 
168 	crew[0] = mc->crew1;
169 	crew[1] = mc->crew2;
170 	crew[2] = mc->crew3;
171 	for (n = 0; n < NBP; n++) {
172 		if (mf->OBP[n].turnsent)
173 			    men += mf->OBP[n].mensent;
174 	}
175 	for (n = 0; n < NBP; n++) {
176 		if (mf->DBP[n].turnsent)
177 			    men += mf->DBP[n].mensent;
178 	}
179 	if (men) {
180 		crew[0] = men/100 ? 0 : crew[0] != 0;
181 		crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
182 		crew[2] = men%10 ? 0 : crew[2] != 0;
183 	} else {
184 		crew[0] = crew[0] != 0;
185 		crew[1] = crew[1] != 0;
186 		crew[2] = crew[2] != 0;
187 	}
188 	foreachship(sp) {
189 		if (sp == ms || sp->file->dir == 0 || range(ms, sp) > 1)
190 			continue;
191 		if (ms->nationality == capship(sp)->nationality)
192 			continue;
193 		if (meleeing(ms, sp) && crew[2]) {
194 			c = sgetch("How many more to board the $$? ",
195 				sp, 1);
196 			parties(sp, crew, 0, c);
197 		} else if ((fouled2(ms, sp) || grappled2(ms, sp)) && crew[2]) {
198 			c = sgetch("Crew sections to board the $$ (3 max) ?", sp, 1);
199 			parties(sp, crew, 0, c);
200 		}
201 	}
202 	if (crew[2]) {
203 		c = sgetch("How many sections to repel boarders? ",
204 			(struct ship *)0, 1);
205 		parties(ms, crew, 1, c);
206 	}
207 	blockalarm();
208 	draw_slot();
209 	unblockalarm();
210 }
211 
212 static void
213 parties(struct ship *to, int *crew, int isdefense, int buf)
214 {
215 	int k, j, men;
216 	struct BP *ptr;
217 	int temp[3];
218 
219 	for (k = 0; k < 3; k++)
220 		temp[k] = crew[k];
221 	if (isdigit(buf)) {
222 		ptr = isdefense ? to->file->DBP : to->file->OBP;
223 		for (j = 0; j < NBP && ptr[j].turnsent; j++)
224 			;
225 		if (!ptr[j].turnsent && buf > '0') {
226 			men = 0;
227 			for (k = 0; k < 3 && buf > '0'; k++) {
228 				men += crew[k]
229 					* (k == 0 ? 100 : (k == 1 ? 10 : 1));
230 				crew[k] = 0;
231 				if (men)
232 					buf--;
233 			}
234 			if (buf > '0')
235 				Msg("Sending all crew sections.");
236 			Write(isdefense ? W_DBP : W_OBP, ms,
237 				j, turn, to->file->index, men);
238 			if (isdefense) {
239 				wmove(slot_w, 2, 0);
240 				for (k=0; k < NBP; k++)
241 					if (temp[k] && !crew[k])
242 						waddch(slot_w, k + '1');
243 					else
244 						wmove(slot_w, 2, 1 + k);
245 				mvwaddstr(slot_w, 3, 0, "DBP");
246 				makemsg(ms, "repelling boarders");
247 			} else {
248 				wmove(slot_w, 0, 0);
249 				for (k=0; k < NBP; k++)
250 					if (temp[k] && !crew[k])
251 						waddch(slot_w, k + '1');
252 					else
253 						wmove(slot_w, 0, 1 + k);
254 				mvwaddstr(slot_w, 1, 0, "OBP");
255 				makesignal(ms, "boarding the $$", to);
256 			}
257 			blockalarm();
258 			wrefresh(slot_w);
259 			unblockalarm();
260 		} else
261 			Msg("Sending no crew sections.");
262 	}
263 }
264