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