pl_3.c revision 1.13 1 /* $NetBSD: pl_3.c,v 1.13 2001/01/04 03:51:24 jwise Exp $ */
2
3 /*
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.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)pl_3.c 8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: pl_3.c,v 1.13 2001/01/04 03:51:24 jwise Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <signal.h>
46 #include <stdlib.h>
47 #include "player.h"
48
49 void acceptcombat(void);
50 void grapungrap(void);
51 void unfoulplayer(void);
52
53 void
54 acceptcombat(void)
55 {
56 int men = 0;
57 int target, temp;
58 int n, r;
59 int index, rakehim, sternrake;
60 int hhits = 0, ghits = 0, rhits = 0, chits = 0;
61 int crew[3];
62 int load;
63 int guns, car, ready, shootat, hit;
64 int roll;
65 struct ship *closest;
66
67 crew[0] = mc->crew1;
68 crew[1] = mc->crew2;
69 crew[2] = mc->crew3;
70 for (n = 0; n < 3; n++) {
71 if (mf->OBP[n].turnsent)
72 men += mf->OBP[n].mensent;
73 }
74 for (n = 0; n < 3; n++) {
75 if (mf->DBP[n].turnsent)
76 men += mf->DBP[n].mensent;
77 }
78 if (men) {
79 crew[0] = men/100 ? 0 : crew[0] != 0;
80 crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
81 crew[2] = men%10 ? 0 : crew[2] != 0;
82 }
83 for (r = 0; r < 2; r++) {
84 if (r) {
85 ready = mf->readyR;
86 load = mf->loadR;
87 guns = mc->gunR;
88 car = mc->carR;
89 } else {
90 ready = mf->readyL;
91 load = mf->loadL;
92 guns = mc->gunL;
93 car = mc->carL;
94 }
95 if ((!guns && !car) || load == L_EMPTY || (ready & R_LOADED) == 0)
96 goto cant;
97 if (mf->struck || !crew[2])
98 goto cant;
99 closest = closestenemy(ms, (r ? 'r' : 'l'), 1);
100 if (closest == 0)
101 goto cant;
102 if (closest->file->struck)
103 goto cant;
104 target = range(ms, closest);
105 if (target > rangeofshot[load] || (!guns && target >= 3))
106 goto cant;
107 Signal("$$ within range of %s broadside.",
108 closest, r ? "right" : "left");
109 if (load > L_CHAIN && target < 6) {
110 switch (sgetch("Aim for hull or rigging? ",
111 (struct ship *)0, 1)) {
112 case 'r':
113 shootat = RIGGING;
114 break;
115 case 'h':
116 shootat = HULL;
117 break;
118 default:
119 shootat = -1;
120 Msg("'Avast there! Hold your fire.'");
121 }
122 } else {
123 if (sgetch("Fire? ", (struct ship *)0, 1) == 'n') {
124 shootat = -1;
125 Msg("Belay that! Hold your fire.");
126 } else
127 shootat = RIGGING;
128 }
129 if (shootat == -1)
130 continue;
131 fired = 1;
132 rakehim = gunsbear(ms, closest) && !gunsbear(closest, ms);
133 temp = portside(closest, ms, 1) - closest->file->dir + 1;
134 if (temp < 1)
135 temp += 8;
136 else if (temp > 8)
137 temp -= 8;
138 sternrake = temp > 4 && temp < 6;
139 if (rakehim) {
140 if (!sternrake)
141 Msg("Raking the %s!", closest->shipname);
142 else
143 Msg("Stern Rake! %s splintering!",
144 closest->shipname);
145 }
146 index = guns;
147 if (target < 3)
148 index += car;
149 index = (index - 1)/3;
150 index = index > 8 ? 8 : index;
151 if (!rakehim)
152 hit = HDT[index][target-1];
153 else
154 hit = HDTrake[index][target-1];
155 if (rakehim && sternrake)
156 hit++;
157 hit += QUAL[index][mc->qual-1];
158 for (n = 0; n < 3 && mf->captured == 0; n++)
159 if (!crew[n]) {
160 if (index <= 5)
161 hit--;
162 else
163 hit -= 2;
164 }
165 if (ready & R_INITIAL) {
166 if (index <= 3)
167 hit++;
168 else
169 hit += 2;
170 }
171 if (mf->captured != 0) {
172 if (index <= 1)
173 hit--;
174 else
175 hit -= 2;
176 }
177 hit += AMMO[index][load - 1];
178 if (((temp = mc->class) >= 5 || temp == 1) && windspeed == 5)
179 hit--;
180 if (windspeed == 6 && temp == 4)
181 hit -= 2;
182 if (windspeed == 6 && temp <= 3)
183 hit--;
184 if (hit >= 0) {
185 roll = dieroll();
186 if (load == L_GRAPE)
187 chits = hit;
188 else {
189 const struct Tables *t;
190 if (hit > 10)
191 hit = 10;
192 t = &(shootat == RIGGING ? RigTable : HullTable)
193 [hit][roll-1];
194 chits = t->C;
195 rhits = t->R;
196 hhits = t->H;
197 ghits = t->G;
198 if (closest->file->FS)
199 rhits *= 2;
200 if (load == L_CHAIN) {
201 ghits = 0;
202 hhits = 0;
203 }
204 }
205 table(shootat, load, hit, closest, ms, roll);
206 }
207 Msg("Damage inflicted on the %s:", closest->shipname);
208 Msg("\t%d HULL, %d GUNS, %d CREW, %d RIGGING",
209 hhits, ghits, chits, rhits);
210 if (!r) {
211 mf->loadL = L_EMPTY;
212 mf->readyL = R_EMPTY;
213 } else {
214 mf->loadR = L_EMPTY;
215 mf->readyR = R_EMPTY;
216 }
217 continue;
218 cant:
219 Msg("Unable to fire %s broadside", r ? "right" : "left");
220 }
221 blockalarm();
222 draw_stat();
223 unblockalarm();
224 }
225
226 void
227 grapungrap(void)
228 {
229 struct ship *sp;
230 int i;
231
232 foreachship(sp) {
233 if (sp == ms || sp->file->dir == 0)
234 continue;
235 if (range(ms, sp) > 1 && !grappled2(ms, sp))
236 continue;
237 switch (sgetch("Attempt to grapple or ungrapple $$: ",
238 sp, 1)) {
239 case 'g':
240 if (dieroll() < 3
241 || ms->nationality == capship(sp)->nationality) {
242 Write(W_GRAP, ms, sp->file->index, 0, 0, 0);
243 Write(W_GRAP, sp, player, 0, 0, 0);
244 Msg("Attempt succeeds!");
245 makesignal(ms, "grappled with $$", sp);
246 } else
247 Msg("Attempt fails.");
248 break;
249 case 'u':
250 for (i = grappled2(ms, sp); --i >= 0;) {
251 if (ms->nationality
252 == capship(sp)->nationality
253 || dieroll() < 3) {
254 cleangrapple(ms, sp, 0);
255 Msg("Attempt succeeds!");
256 makesignal(ms, "ungrappling with $$",
257 sp);
258 } else
259 Msg("Attempt fails.");
260 }
261 break;
262 }
263 }
264 }
265
266 void
267 unfoulplayer(void)
268 {
269 struct ship *to;
270 int i;
271
272 foreachship(to) {
273 if (fouled2(ms, to) == 0)
274 continue;
275 if (sgetch("Attempt to unfoul with the $$? ", to, 1) != 'y')
276 continue;
277 for (i = fouled2(ms, to); --i >= 0;) {
278 if (dieroll() <= 2) {
279 cleanfoul(ms, to, 0);
280 Msg("Attempt succeeds!");
281 makesignal(ms, "Unfouling $$", to);
282 } else
283 Msg("Attempt fails.");
284 }
285 }
286 }
287