xref: /netbsd/games/sail/pl_3.c (revision bf9ec67e)
1 /*	$NetBSD: pl_3.c,v 1.16 2001/02/05 01:10:10 christos 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.16 2001/02/05 01:10:10 christos Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <signal.h>
46 #include <stdlib.h>
47 #include "extern.h"
48 #include "player.h"
49 
50 void
51 acceptcombat(void)
52 {
53 	int men = 0;
54 	int target, temp;
55 	int n, r;
56 	int index, rakehim, sternrake;
57 	int hhits = 0, ghits = 0, rhits = 0, chits = 0;
58 	int crew[3];
59 	int load;
60 	int guns, car, ready, shootat, hit;
61 	int roll;
62 	struct ship *closest;
63 
64 	crew[0] = mc->crew1;
65 	crew[1] = mc->crew2;
66 	crew[2] = mc->crew3;
67 	for (n = 0; n < 3; n++) {
68 		if (mf->OBP[n].turnsent)
69 			men += mf->OBP[n].mensent;
70 	}
71 	for (n = 0; n < 3; n++) {
72 		if (mf->DBP[n].turnsent)
73 			men += mf->DBP[n].mensent;
74 	}
75 	if (men) {
76 		crew[0] = men/100 ? 0 : crew[0] != 0;
77 		crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
78 		crew[2] = men%10 ? 0 : crew[2] != 0;
79 	}
80 	for (r = 0; r < 2; r++) {
81 		if (r) {
82 			ready = mf->readyR;
83 			load = mf->loadR;
84 			guns = mc->gunR;
85 			car = mc->carR;
86 		} else {
87 			ready = mf->readyL;
88 			load = mf->loadL;
89 			guns = mc->gunL;
90 			car = mc->carL;
91 		}
92 		if ((!guns && !car) || load == L_EMPTY || (ready & R_LOADED) == 0)
93 			goto cant;
94 		if (mf->struck || !crew[2])
95 			goto cant;
96 		closest = closestenemy(ms, (r ? 'r' : 'l'), 1);
97 		if (closest == 0)
98 			goto cant;
99 		if (closest->file->struck)
100 			goto cant;
101 		target = range(ms, closest);
102 		if (target > rangeofshot[load] || (!guns && target >= 3))
103 			goto cant;
104 		Signal("$$ within range of %s broadside.",
105 			closest, r ? "right" : "left");
106 		if (load > L_CHAIN && target < 6) {
107 			switch (sgetch("Aim for hull or rigging? ",
108 				(struct ship *)0, 1)) {
109 			case 'r':
110 				shootat = RIGGING;
111 				break;
112 			case 'h':
113 				shootat = HULL;
114 				break;
115 			default:
116 				shootat = -1;
117 				Msg("'Avast there! Hold your fire.'");
118 			}
119 		} else {
120 			if (sgetch("Fire? ", (struct ship *)0, 1) == 'n') {
121 				shootat = -1;
122 				Msg("Belay that! Hold your fire.");
123 			} else
124 				shootat = RIGGING;
125 		}
126 		if (shootat == -1)
127 			continue;
128 		fired = 1;
129 		rakehim = gunsbear(ms, closest) && !gunsbear(closest, ms);
130 		temp = portside(closest, ms, 1) - closest->file->dir + 1;
131 		if (temp < 1)
132 			temp += 8;
133 		else if (temp > 8)
134 			temp -= 8;
135 		sternrake = temp > 4 && temp < 6;
136 		if (rakehim) {
137 			if (!sternrake)
138 				Msg("Raking the %s!", closest->shipname);
139 			else
140 				Msg("Stern Rake! %s splintering!",
141 				    closest->shipname);
142 		}
143 		index = guns;
144 		if (target < 3)
145 			index += car;
146 		index = (index - 1)/3;
147 		index = index > 8 ? 8 : index;
148 		if (!rakehim)
149 			hit = HDT[index][target-1];
150 		else
151 			hit = HDTrake[index][target-1];
152 		if (rakehim && sternrake)
153 			hit++;
154 		hit += QUAL[index][mc->qual-1];
155 		for (n = 0; n < 3 && mf->captured == 0; n++)
156 			if (!crew[n]) {
157 				if (index <= 5)
158 					hit--;
159 				else
160 					hit -= 2;
161 			}
162 		if (ready & R_INITIAL) {
163 			if (index <= 3)
164 				hit++;
165 			else
166 				hit += 2;
167 		}
168 		if (mf->captured != 0) {
169 			if (index <= 1)
170 				hit--;
171 			else
172 				hit -= 2;
173 		}
174 		hit += AMMO[index][load - 1];
175 		if (((temp = mc->class) >= 5 || temp == 1) && windspeed == 5)
176 			hit--;
177 		if (windspeed == 6 && temp == 4)
178 			hit -= 2;
179 		if (windspeed == 6 && temp <= 3)
180 			hit--;
181 		if (hit >= 0) {
182 			roll = dieroll();
183 			if (load == L_GRAPE)
184 				chits = hit;
185 			else {
186 				const struct Tables *t;
187 				if (hit > 10)
188 					hit = 10;
189 				t = &(shootat == RIGGING ? RigTable : HullTable)
190 					[hit][roll-1];
191 				chits = t->C;
192 				rhits = t->R;
193 				hhits = t->H;
194 				ghits = t->G;
195 				if (closest->file->FS)
196 					rhits *= 2;
197 				if (load == L_CHAIN) {
198 					ghits = 0;
199 					hhits = 0;
200 				}
201 			}
202 			table(ms, closest, shootat, load, hit, roll);
203 		}
204 		Msg("Damage inflicted on the %s:", closest->shipname);
205 		Msg("\t%d HULL, %d GUNS, %d CREW, %d RIGGING",
206 		    hhits, ghits, chits, rhits);
207 		if (!r) {
208 			mf->loadL = L_EMPTY;
209 			mf->readyL = R_EMPTY;
210 		} else {
211 			mf->loadR = L_EMPTY;
212 			mf->readyR = R_EMPTY;
213 		}
214 		continue;
215 	cant:
216 		Msg("Unable to fire %s broadside", r ? "right" : "left");
217 	}
218 	blockalarm();
219 	draw_stat();
220 	unblockalarm();
221 }
222 
223 void
224 grapungrap(void)
225 {
226 	struct ship *sp;
227 	int i;
228 
229 	foreachship(sp) {
230 		if (sp == ms || sp->file->dir == 0)
231 			continue;
232 		if (range(ms, sp) > 1 && !grappled2(ms, sp))
233 			continue;
234 		switch (sgetch("Attempt to grapple or ungrapple $$: ",
235 			sp, 1)) {
236 		case 'g':
237 			if (dieroll() < 3
238 			    || ms->nationality == capship(sp)->nationality) {
239 				Write(W_GRAP, ms, sp->file->index, 0, 0, 0);
240 				Write(W_GRAP, sp, player, 0, 0, 0);
241 				Msg("Attempt succeeds!");
242 				makesignal(ms, "grappled with $$", sp);
243 			} else
244 				Msg("Attempt fails.");
245 			break;
246 		case 'u':
247 			for (i = grappled2(ms, sp); --i >= 0;) {
248 				if (ms->nationality
249 					== capship(sp)->nationality
250 				    || dieroll() < 3) {
251 					cleangrapple(ms, sp, 0);
252 					Msg("Attempt succeeds!");
253 					makesignal(ms, "ungrappling with $$",
254 						sp);
255 				} else
256 					Msg("Attempt fails.");
257 			}
258 			break;
259 		}
260 	}
261 }
262 
263 void
264 unfoulplayer(void)
265 {
266 	struct ship *to;
267 	int i;
268 
269 	foreachship(to) {
270 		if (fouled2(ms, to) == 0)
271 			continue;
272 		if (sgetch("Attempt to unfoul with the $$? ", to, 1) != 'y')
273 			continue;
274 		for (i = fouled2(ms, to); --i >= 0;) {
275 			if (dieroll() <= 2) {
276 				cleanfoul(ms, to, 0);
277 				Msg("Attempt succeeds!");
278 				makesignal(ms, "Unfouling $$", to);
279 			} else
280 				Msg("Attempt fails.");
281 		}
282 	}
283 }
284