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