fd179d43381b72d603492ac388cbdf8b91a3dae6
[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  * @(#)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.3 2006/09/03 17:33:13 pavalos Exp $
36  */
37
38 #include <string.h>
39 #include "player.h"
40
41 #define turnfirst(x) (*x == 'r' || *x == 'l')
42
43 static void     parties(int [3], struct ship *, char, char);
44
45 void
46 acceptmove(void)
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                 Signal("Unable to move", (struct ship *)0);
59                 return;
60         }
61
62         ta = maxturns(ms, &af);
63         ma = maxmove(ms, mf->dir, 0);
64         sprintf(prompt, "move (%d,%c%d): ", ma, af ? '\'' : ' ', ta);
65         sgetstr(prompt, buf, sizeof buf);
66         dir = mf->dir;
67         vma = ma;
68         for (p = buf; *p; p++)
69                 switch (*p) {
70                 case 'l':
71                         dir -= 2;
72                 case 'r':
73                         if (++dir == 0)
74                                 dir = 8;
75                         else if (dir == 9)
76                                 dir = 1;
77                         if (last == 't') {
78                                 Signal("Ship can't turn that fast.",
79                                         (struct ship *)0);
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                                 Signal("Can't move that fast.",
107                                         (struct ship *)0);
108                                 *p-- = '\0';
109                         }
110                         last = '0';
111                         moved = 1;
112                         ma -= *p - '0';
113                         vma -= *p - '0';
114                         if ((ta < 0 && moved) || (vma < 0 && moved))
115                                 *p-- = '\0';
116                         break;
117                 default:
118                         if (!isspace(*p)) {
119                                 Signal("Input error.", (struct ship *)0);
120                                 *p-- = '\0';
121                         }
122                 }
123         if ((ta < 0 && moved) || (vma < 0 && moved)
124             || (af && turnfirst(buf) && moved)) {
125                 Signal("Movement error.", (struct ship *)0);
126                 if (ta < 0 && moved) {
127                         if (mf->FS == 1) {
128                                 Write(W_FS, ms, 0, 0, 0, 0);
129                                 Signal("No hands to set full sails.",
130                                         (struct ship *)0);
131                         }
132                 } else if (ma >= 0)
133                         buf[1] = '\0';
134         }
135         if (af && !moved) {
136                 if (mf->FS == 1) {
137                         Write(W_FS, ms, 0, 0, 0, 0);
138                         Signal("No hands to set full sails.",
139                                 (struct ship *)0);
140                 }
141         }
142         if (*buf)
143                 strcpy(movebuf, buf);
144         else
145                 strcpy(movebuf, "d");
146         Writestr(W_MOVE, ms, movebuf);
147         Signal("Helm: %s.", (struct ship *)0, movebuf);
148 }
149
150 void
151 acceptboard(void)
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 static void
204 parties(int crew[3], struct ship *to, char isdefense, char buf)
205 {
206         int k, j, men;
207         struct BP *ptr;
208         int temp[3];
209
210         for (k = 0; k < 3; k++)
211                 temp[k] = crew[k];
212         if (isdigit(buf)) {
213                 ptr = isdefense ? to->file->DBP : to->file->OBP;
214                 for (j = 0; j < NBP && ptr[j].turnsent; j++)
215                         ;
216                 if (!ptr[j].turnsent && buf > '0') {
217                         men = 0;
218                         for (k = 0; k < 3 && buf > '0'; k++) {
219                                 men += crew[k]
220                                         * (k == 0 ? 100 : (k == 1 ? 10 : 1));
221                                 crew[k] = 0;
222                                 if (men)
223                                         buf--;
224                         }
225                         if (buf > '0')
226                                 Signal("Sending all crew sections.",
227                                         (struct ship *)0);
228                         Write(isdefense ? W_DBP : W_OBP, ms,
229                                 j, turn, to->file->index, men);
230                         if (isdefense) {
231                                 wmove(slot_w, 2, 0);
232                                 for (k=0; k < NBP; k++)
233                                         if (temp[k] && !crew[k])
234                                                 waddch(slot_w, k + '1');
235                                         else
236                                                 wmove(slot_w, 2, 1 + k);
237                                 mvwaddstr(slot_w, 3, 0, "DBP");
238                                 makesignal(ms, "repelling boarders",
239                                         (struct ship *)0);
240                         } else {
241                                 wmove(slot_w, 0, 0);
242                                 for (k=0; k < NBP; k++)
243                                         if (temp[k] && !crew[k])
244                                                 waddch(slot_w, k + '1');
245                                         else
246                                                 wmove(slot_w, 0, 1 + k);
247                                 mvwaddstr(slot_w, 1, 0, "OBP");
248                                 makesignal(ms, "boarding the %s (%c%c)", to);
249                         }
250                         blockalarm();
251                         wrefresh(slot_w);
252                         unblockalarm();
253                 } else
254                         Signal("Sending no crew sections.", (struct ship *)0);
255         }
256 }