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