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