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