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