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