xref: /original-bsd/games/sail/pl_3.c (revision a4d3ae46)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)pl_3.c	5.2 (Berkeley) 03/09/88";
15 #endif /* not lint */
16 
17 #include "player.h"
18 
19 acceptcombat()
20 {
21 	int men = 0;
22 	int target, temp;
23 	int n, r;
24 	int index, rakehim, sternrake;
25 	int hhits = 0, ghits = 0, rhits = 0, chits = 0;
26 	int crew[3];
27 	int load;
28 	int guns, car, ready, shootat, hit;
29 	int roll;
30 	struct ship *closest;
31 
32 	crew[0] = mc->crew1;
33 	crew[1] = mc->crew2;
34 	crew[2] = mc->crew3;
35 	for (n = 0; n < 3; n++) {
36 		if (mf->OBP[n].turnsent)
37 			men += mf->OBP[n].mensent;
38 	}
39 	for (n = 0; n < 3; n++) {
40 		if (mf->DBP[n].turnsent)
41 			men += mf->DBP[n].mensent;
42 	}
43 	if (men) {
44 		crew[0] = men/100 ? 0 : crew[0] != 0;
45 		crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
46 		crew[2] = men%10 ? 0 : crew[2] != 0;
47 	}
48 	for (r = 0; r < 2; r++) {
49 		if (r) {
50 			ready = mf->readyR;
51 			load = mf->loadR;
52 			guns = mc->gunR;
53 			car = mc->carR;
54 		} else {
55 			ready = mf->readyL;
56 			load = mf->loadL;
57 			guns = mc->gunL;
58 			car = mc->carL;
59 		}
60 		if (!guns && !car || load == L_EMPTY || (ready & R_LOADED) == 0)
61 			goto cant;
62 		if (mf->struck || !crew[2])
63 			goto cant;
64 		closest = closestenemy(ms, (r ? 'r' : 'l'), 1);
65 		if (closest == 0)
66 			goto cant;
67 		if (closest->file->struck)
68 			goto cant;
69 		target = range(ms, closest);
70 		if (target > rangeofshot[load] || !guns && target >= 3)
71 			goto cant;
72 		Signal("%s (%c%c) within range of %s broadside.",
73 			closest, r ? "right" : "left");
74 		if (load > L_CHAIN && target < 6) {
75 			switch (sgetch("Aim for hull or rigging? ",
76 				(struct ship *)0, 1)) {
77 			case 'r':
78 				shootat = RIGGING;
79 				break;
80 			case 'h':
81 				shootat = HULL;
82 				break;
83 			default:
84 				shootat = -1;
85 				Signal("'Avast there! Hold your fire.'",
86 					(struct ship *)0);
87 			}
88 		} else {
89 			if (sgetch("Fire? ", (struct ship *)0, 1) == 'n') {
90 				shootat = -1;
91 				Signal("Belay that! Hold your fire.",
92 					(struct ship *)0);
93 			} else
94 				shootat = RIGGING;
95 		}
96 		if (shootat == -1)
97 			continue;
98 		fired = 1;
99 		rakehim = gunsbear(ms, closest) && !gunsbear(closest, ms);
100 		temp = portside(closest, ms, 1) - closest->file->dir + 1;
101 		if (temp < 1)
102 			temp += 8;
103 		else if (temp > 8)
104 			temp -= 8;
105 		sternrake = temp > 4 && temp < 6;
106 		if (rakehim)
107 			if (!sternrake)
108 				Signal("Raking the %s!", closest);
109 			else
110 				Signal("Stern Rake! %s splintering!", closest);
111 		index = guns;
112 		if (target < 3)
113 			index += car;
114 		index = (index - 1)/3;
115 		index = index > 8 ? 8 : index;
116 		if (!rakehim)
117 			hit = HDT[index][target-1];
118 		else
119 			hit = HDTrake[index][target-1];
120 		if (rakehim && sternrake)
121 			hit++;
122 		hit += QUAL[index][mc->qual-1];
123 		for (n = 0; n < 3 && mf->captured == 0; n++)
124 			if (!crew[n])
125 				if (index <= 5)
126 					hit--;
127 				else
128 					hit -= 2;
129 		if (ready & R_INITIAL)
130 			if (index <= 3)
131 				hit++;
132 			else
133 				hit += 2;
134 		if (mf->captured != 0)
135 			if (index <= 1)
136 				hit--;
137 			else
138 				hit -= 2;
139 		hit += AMMO[index][load - 1];
140 		if (((temp = mc->class) >= 5 || temp == 1) && windspeed == 5)
141 			hit--;
142 		if (windspeed == 6 && temp == 4)
143 			hit -= 2;
144 		if (windspeed == 6 && temp <= 3)
145 			hit--;
146 		if (hit >= 0) {
147 			roll = die();
148 			if (load == L_GRAPE)
149 				chits = hit;
150 			else {
151 				struct Tables *t;
152 				if (hit > 10)
153 					hit = 10;
154 				t = &(shootat == RIGGING ? RigTable : HullTable)
155 					[hit][roll-1];
156 				chits = t->C;
157 				rhits = t->R;
158 				hhits = t->H;
159 				ghits = t->G;
160 				if (closest->file->FS)
161 					rhits *= 2;
162 				if (load == L_CHAIN) {
163 					ghits = 0;
164 					hhits = 0;
165 				}
166 			}
167 			table(shootat, load, hit, closest, ms, roll);
168 		}
169 		Signal("Damage inflicted on the %s:",
170 			(struct ship *)0, closest->shipname);
171 		Signal("\t%d HULL, %d GUNS, %d CREW, %d RIGGING",
172 			(struct ship *)0, hhits, ghits, chits, rhits);
173 		if (!r) {
174 			mf->loadL = L_EMPTY;
175 			mf->readyL = R_EMPTY;
176 		} else {
177 			mf->loadR = L_EMPTY;
178 			mf->readyR = R_EMPTY;
179 		}
180 		continue;
181 	cant:
182 		Signal("Unable to fire %s broadside",
183 			(struct ship *)0, r ? "right" : "left");
184 	}
185 	blockalarm();
186 	draw_stat();
187 	unblockalarm();
188 }
189 
190 grapungrap()
191 {
192 	register struct ship *sp;
193 	register int i;
194 
195 	foreachship(sp) {
196 		if (sp == ms || sp->file->dir == 0)
197 			continue;
198 		if (range(ms, sp) > 1 && !grappled2(ms, sp))
199 			continue;
200 		switch (sgetch("Attempt to grapple or ungrapple %s (%c%c): ",
201 			sp, 1)) {
202 		case 'g':
203 			if (die() < 3
204 			    || ms->nationality == capship(sp)->nationality) {
205 				Write(W_GRAP, ms, 0, sp->file->index, 0, 0, 0);
206 				Write(W_GRAP, sp, 0, player, 0, 0, 0);
207 				Signal("Attempt succeeds!", (struct ship *)0);
208 				makesignal(ms, "grappled with %s (%c%c)", sp);
209 			} else
210 				Signal("Attempt fails.", (struct ship *)0);
211 			break;
212 		case 'u':
213 			for (i = grappled2(ms, sp); --i >= 0;) {
214 				if (ms->nationality
215 					== capship(sp)->nationality
216 				    || die() < 3) {
217 					cleangrapple(ms, sp, 0);
218 					Signal("Attempt succeeds!",
219 						(struct ship *)0);
220 					makesignal(ms,
221 						"ungrappling with %s (%c%c)",
222 						sp);
223 				} else
224 					Signal("Attempt fails.",
225 						(struct ship *)0);
226 			}
227 			break;
228 		}
229 	}
230 }
231 
232 unfoulplayer()
233 {
234 	register struct ship *to;
235 	register i;
236 
237 	foreachship(to) {
238 		if (fouled2(ms, to) == 0)
239 			continue;
240 		if (sgetch("Attempt to unfoul with the %s (%c%c)? ", to, 1) != 'y')
241 			continue;
242 		for (i = fouled2(ms, to); --i >= 0;) {
243 			if (die() <= 2) {
244 				cleanfoul(ms, to, 0);
245 				Signal("Attempt succeeds!", (struct ship *)0);
246 				makesignal(ms, "Unfouling %s (%c%c)", to);
247 			} else
248 				Signal("Attempt fails.", (struct ship *)0);
249 		}
250 	}
251 }
252