xref: /dragonfly/games/sail/pl_3.c (revision 984263bc)
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 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)pl_3.c	8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD: src/games/sail/pl_3.c,v 1.6 1999/11/30 03:49:37 billf Exp $";
40 #endif /* not lint */
41 
42 #include "player.h"
43 
44 acceptcombat()
45 {
46 	int men = 0;
47 	int target, temp;
48 	int n, r;
49 	int index, rakehim, sternrake;
50 	int hhits = 0, ghits = 0, rhits = 0, chits = 0;
51 	int crew[3];
52 	int load;
53 	int guns, car, ready, shootat, hit;
54 	int roll;
55 	struct ship *closest;
56 
57 	crew[0] = mc->crew1;
58 	crew[1] = mc->crew2;
59 	crew[2] = mc->crew3;
60 	for (n = 0; n < 3; n++) {
61 		if (mf->OBP[n].turnsent)
62 			men += mf->OBP[n].mensent;
63 	}
64 	for (n = 0; n < 3; n++) {
65 		if (mf->DBP[n].turnsent)
66 			men += mf->DBP[n].mensent;
67 	}
68 	if (men) {
69 		crew[0] = men/100 ? 0 : crew[0] != 0;
70 		crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
71 		crew[2] = men%10 ? 0 : crew[2] != 0;
72 	}
73 	for (r = 0; r < 2; r++) {
74 		if (r) {
75 			ready = mf->readyR;
76 			load = mf->loadR;
77 			guns = mc->gunR;
78 			car = mc->carR;
79 		} else {
80 			ready = mf->readyL;
81 			load = mf->loadL;
82 			guns = mc->gunL;
83 			car = mc->carL;
84 		}
85 		if (!guns && !car || load == L_EMPTY || (ready & R_LOADED) == 0)
86 			goto cant;
87 		if (mf->struck || !crew[2])
88 			goto cant;
89 		closest = closestenemy(ms, (r ? 'r' : 'l'), 1);
90 		if (closest == 0)
91 			goto cant;
92 		if (closest->file->struck)
93 			goto cant;
94 		target = range(ms, closest);
95 		if (target > rangeofshot[load] || !guns && target >= 3)
96 			goto cant;
97 		Signal("%s (%c%c) within range of %s broadside.",
98 			closest, r ? "right" : "left");
99 		if (load > L_CHAIN && target < 6) {
100 			switch (sgetch("Aim for hull or rigging? ",
101 				(struct ship *)0, 1)) {
102 			case 'r':
103 				shootat = RIGGING;
104 				break;
105 			case 'h':
106 				shootat = HULL;
107 				break;
108 			default:
109 				shootat = -1;
110 				Signal("'Avast there! Hold your fire.'",
111 					(struct ship *)0);
112 			}
113 		} else {
114 			if (sgetch("Fire? ", (struct ship *)0, 1) == 'n') {
115 				shootat = -1;
116 				Signal("Belay that! Hold your fire.",
117 					(struct ship *)0);
118 			} else
119 				shootat = RIGGING;
120 		}
121 		if (shootat == -1)
122 			continue;
123 		fired = 1;
124 		rakehim = gunsbear(ms, closest) && !gunsbear(closest, ms);
125 		temp = portside(closest, ms, 1) - closest->file->dir + 1;
126 		if (temp < 1)
127 			temp += 8;
128 		else if (temp > 8)
129 			temp -= 8;
130 		sternrake = temp > 4 && temp < 6;
131 		if (rakehim) {
132 			if (!sternrake)
133 				Signal("Raking the %s!", closest);
134 			else
135 				Signal("Stern Rake! %s splintering!", closest);
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 				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 		Signal("Damage inflicted on the %s:",
199 			(struct ship *)0, closest->shipname);
200 		Signal("\t%d HULL, %d GUNS, %d CREW, %d RIGGING",
201 			(struct ship *)0, hhits, ghits, chits, rhits);
202 		if (!r) {
203 			mf->loadL = L_EMPTY;
204 			mf->readyL = R_EMPTY;
205 		} else {
206 			mf->loadR = L_EMPTY;
207 			mf->readyR = R_EMPTY;
208 		}
209 		continue;
210 	cant:
211 		Signal("Unable to fire %s broadside",
212 			(struct ship *)0, r ? "right" : "left");
213 	}
214 	blockalarm();
215 	draw_stat();
216 	unblockalarm();
217 }
218 
219 grapungrap()
220 {
221 	struct ship *sp;
222 	int i;
223 
224 	foreachship(sp) {
225 		if (sp == ms || sp->file->dir == 0)
226 			continue;
227 		if (range(ms, sp) > 1 && !grappled2(ms, sp))
228 			continue;
229 		switch (sgetch("Attempt to grapple or ungrapple %s (%c%c): ",
230 			sp, 1)) {
231 		case 'g':
232 			if (die() < 3
233 			    || ms->nationality == capship(sp)->nationality) {
234 				Write(W_GRAP, ms, 0, sp->file->index, 0, 0, 0);
235 				Write(W_GRAP, sp, 0, player, 0, 0, 0);
236 				Signal("Attempt succeeds!", (struct ship *)0);
237 				makesignal(ms, "grappled with %s (%c%c)", sp);
238 			} else
239 				Signal("Attempt fails.", (struct ship *)0);
240 			break;
241 		case 'u':
242 			for (i = grappled2(ms, sp); --i >= 0;) {
243 				if (ms->nationality
244 					== capship(sp)->nationality
245 				    || die() < 3) {
246 					cleangrapple(ms, sp, 0);
247 					Signal("Attempt succeeds!",
248 						(struct ship *)0);
249 					makesignal(ms,
250 						"ungrappling with %s (%c%c)",
251 						sp);
252 				} else
253 					Signal("Attempt fails.",
254 						(struct ship *)0);
255 			}
256 			break;
257 		}
258 	}
259 }
260 
261 unfoulplayer()
262 {
263 	struct ship *to;
264 	int i;
265 
266 	foreachship(to) {
267 		if (fouled2(ms, to) == 0)
268 			continue;
269 		if (sgetch("Attempt to unfoul with the %s (%c%c)? ", to, 1) != 'y')
270 			continue;
271 		for (i = fouled2(ms, to); --i >= 0;) {
272 			if (die() <= 2) {
273 				cleanfoul(ms, to, 0);
274 				Signal("Attempt succeeds!", (struct ship *)0);
275 				makesignal(ms, "Unfouling %s (%c%c)", to);
276 			} else
277 				Signal("Attempt fails.", (struct ship *)0);
278 		}
279 	}
280 }
281