xref: /openbsd/games/sail/pl_5.c (revision 404b540a)
1 /*	$OpenBSD: pl_5.c,v 1.5 2006/03/27 00:10:15 tedu Exp $	*/
2 /*	$NetBSD: pl_5.c,v 1.4 1995/04/24 12:25:21 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 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)pl_5.c	8.1 (Berkeley) 5/31/93";
36 #else
37 static char rcsid[] = "$OpenBSD: pl_5.c,v 1.5 2006/03/27 00:10:15 tedu Exp $";
38 #endif
39 #endif /* not lint */
40 
41 #include "player.h"
42 
43 #define turnfirst(x) (*x == 'r' || *x == 'l')
44 
45 void
46 acceptmove()
47 {
48 	int ta;
49 	int ma;
50 	char af;
51 	int moved = 0;
52 	int vma, dir;
53 	char prompt[60];
54 	char buf[60], last = '\0';
55 	char *p;
56 
57 	if (!mc->crew3 || snagged(ms) || !windspeed) {
58 		Msg("Unable to move");
59 		return;
60 	}
61 
62 	ta = maxturns(ms, &af);
63 	ma = maxmove(ms, mf->dir, 0);
64 	(void) snprintf(prompt, sizeof prompt,
65 		"move (%d,%c%d): ", ma, af ? '\'' : ' ', ta);
66 	sgetstr(prompt, buf, sizeof buf);
67 	dir = mf->dir;
68 	vma = ma;
69 	for (p = buf; *p; p++)
70 		switch (*p) {
71 		case 'l':
72 			dir -= 2;
73 		case 'r':
74 			if (++dir == 0)
75 				dir = 8;
76 			else if (dir == 9)
77 				dir = 1;
78 			if (last == 't') {
79 				Msg("Ship can't turn that fast.");
80 				*p-- = '\0';
81 			}
82 			last = 't';
83 			ma--;
84 			ta--;
85 			vma = min(ma, maxmove(ms, dir, 0));
86 			if ((ta < 0 && moved) || (vma < 0 && moved))
87 				*p-- = '\0';
88 			break;
89 		case 'b':
90 			ma--;
91 			vma--;
92 			last = 'b';
93 			if ((ta < 0 && moved) || (vma < 0 && moved))
94 				*p-- = '\0';
95 			break;
96 		case '0':
97 		case 'd':
98 			*p-- = '\0';
99 			break;
100 		case '\n':
101 			*p-- = '\0';
102 			break;
103 		case '1': case '2': case '3': case '4':
104 		case '5': case '6': case '7':
105 			if (last == '0') {
106 				Msg("Can't move that fast.");
107 				*p-- = '\0';
108 			}
109 			last = '0';
110 			moved = 1;
111 			ma -= *p - '0';
112 			vma -= *p - '0';
113 			if ((ta < 0 && moved) || (vma < 0 && moved))
114 				*p-- = '\0';
115 			break;
116 		default:
117 			if (!isspace(*p)) {
118 				Msg("Input error.");
119 				*p-- = '\0';
120 			}
121 		}
122 	if ((ta < 0 && moved) || (vma < 0 && moved)
123 	    || (af && turnfirst(buf) && moved)) {
124 		Msg("Movement error.");
125 		if (ta < 0 && moved) {
126 			if (mf->FS == 1) {
127 				Write(W_FS, ms, 0, 0, 0, 0);
128 				Msg("No hands to set full sails.");
129 			}
130 		} else if (ma >= 0)
131 			buf[1] = '\0';
132 	}
133 	if (af && !moved) {
134 		if (mf->FS == 1) {
135 			Write(W_FS, ms, 0, 0, 0, 0);
136 			Msg("No hands to set full sails.");
137 		}
138 	}
139 	if (*buf)
140 		(void) strlcpy(movebuf, buf, sizeof movebuf);
141 	else
142 		(void) strlcpy(movebuf, "d", sizeof movebuf);
143 	Writestr(W_MOVE, ms, movebuf);
144 	Msg("Helm: %s.", movebuf);
145 }
146 
147 void
148 acceptboard()
149 {
150 	struct ship *sp;
151 	int n;
152 	int crew[3];
153 	int men = 0;
154 	char c;
155 
156 	crew[0] = mc->crew1;
157 	crew[1] = mc->crew2;
158 	crew[2] = mc->crew3;
159 	for (n = 0; n < NBP; n++) {
160 		if (mf->OBP[n].turnsent)
161 			    men += mf->OBP[n].mensent;
162 	}
163 	for (n = 0; n < NBP; n++) {
164 		if (mf->DBP[n].turnsent)
165 			    men += mf->DBP[n].mensent;
166 	}
167 	if (men) {
168 		crew[0] = men/100 ? 0 : crew[0] != 0;
169 		crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
170 		crew[2] = men%10 ? 0 : crew[2] != 0;
171 	} else {
172 		crew[0] = crew[0] != 0;
173 		crew[1] = crew[1] != 0;
174 		crew[2] = crew[2] != 0;
175 	}
176 	foreachship(sp) {
177 		if (sp == ms || sp->file->dir == 0 || range(ms, sp) > 1)
178 			continue;
179 		if (ms->nationality == capship(sp)->nationality)
180 			continue;
181 		if (meleeing(ms, sp) && crew[2]) {
182 			c = sgetch("How many more to board the $$? ",
183 				sp, 1);
184 			parties(crew, sp, 0, c);
185 		} else if ((fouled2(ms, sp) || grappled2(ms, sp)) && crew[2]) {
186 			c = sgetch("Crew sections to board the $$ (3 max) ?", sp, 1);
187 			parties(crew, sp, 0, c);
188 		}
189 	}
190 	if (crew[2]) {
191 		c = sgetch("How many sections to repel boarders? ",
192 			(struct ship *)0, 1);
193 		parties(crew, ms, 1, c);
194 	}
195 	blockalarm();
196 	draw_slot();
197 	unblockalarm();
198 }
199 
200 void
201 parties(crew, to, isdefense, buf)
202 	struct ship *to;
203 	int crew[3];
204 	char isdefense;
205 	char buf;
206 {
207 	int k, j, men;
208 	struct BP *ptr;
209 	int temp[3];
210 
211 	for (k = 0; k < 3; k++)
212 		temp[k] = crew[k];
213 	if (isdigit(buf)) {
214 		ptr = isdefense ? to->file->DBP : to->file->OBP;
215 		for (j = 0; j < NBP && ptr[j].turnsent; j++)
216 			;
217 		if (j < NBP && buf > '0') {
218 			men = 0;
219 			for (k = 0; k < 3 && buf > '0'; k++) {
220 				men += crew[k]
221 					* (k == 0 ? 100 : (k == 1 ? 10 : 1));
222 				crew[k] = 0;
223 				if (men)
224 					buf--;
225 			}
226 			if (buf > '0')
227 				Msg("Sending all crew sections.");
228 			Write(isdefense ? W_DBP : W_OBP, ms,
229 				j, turn, to->file->index, men);
230 			if (isdefense) {
231 				(void) wmove(slot_w, 2, 0);
232 				for (k=0; k < NBP; k++)
233 					if (temp[k] && !crew[k])
234 						(void) waddch(slot_w, k + '1');
235 					else
236 						(void) wmove(slot_w, 2, 1 + k);
237 				(void) mvwaddstr(slot_w, 3, 0, "DBP");
238 				makemsg(ms, "repelling boarders");
239 			} else {
240 				(void) wmove(slot_w, 0, 0);
241 				for (k=0; k < NBP; k++)
242 					if (temp[k] && !crew[k])
243 						(void) waddch(slot_w, k + '1');
244 					else
245 						(void) wmove(slot_w, 0, 1 + k);
246 				(void) mvwaddstr(slot_w, 1, 0, "OBP");
247 				makesignal(ms, "boarding the $$", to);
248 			}
249 			blockalarm();
250 			(void) wrefresh(slot_w);
251 			unblockalarm();
252 		} else
253 			Msg("Sending no crew sections.");
254 	}
255 }
256