xref: /original-bsd/games/sail/assorted.c (revision 2ce9ec30)
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[] = "@(#)assorted.c	5.2 (Berkeley) 03/09/88";
15 #endif /* not lint */
16 
17 #include "externs.h"
18 
19 table(rig, shot, hittable, on, from, roll)
20 struct ship *on, *from;
21 int rig, shot, hittable, roll;
22 {
23 	register int hhits = 0, chits = 0, ghits = 0, rhits = 0;
24 	int Ghit = 0, Hhit = 0, Rhit = 0, Chit = 0;
25 	int guns, car, pc, hull;
26 	int crew[3];
27 	register int n;
28 	int rigg[4];
29 	char *message;
30 	struct Tables *tp;
31 
32 	pc = on->file->pcrew;
33 	hull = on->specs->hull;
34 	crew[0] = on->specs->crew1;
35 	crew[1] = on->specs->crew2;
36 	crew[2] = on->specs->crew3;
37 	rigg[0] = on->specs->rig1;
38 	rigg[1] = on->specs->rig2;
39 	rigg[2] = on->specs->rig3;
40 	rigg[3] = on->specs->rig4;
41 	if (shot == L_GRAPE)
42 		Chit = chits = hittable;
43 	else {
44 		tp = &(rig ? RigTable : HullTable)[hittable][roll-1];
45 		Chit = chits = tp->C;
46 		Rhit = rhits = tp->R;
47 		Hhit = hhits = tp->H;
48 		Ghit = ghits = tp->G;
49 		if (on->file->FS)
50 			rhits *= 2;
51 		if (shot == L_CHAIN) {
52 			Ghit = ghits = 0;
53 			Hhit = hhits = 0;
54 		}
55 	}
56 	if (on->file->captured != 0) {
57 		pc -= (chits + 1) / 2;
58 		chits /= 2;
59 	}
60 	for (n = 0; n < 3; n++)
61 		if (chits > crew[n]) {
62 			chits -= crew[n];
63 			crew[n] = 0;
64 		} else {
65 			crew[n] -= chits;
66 			chits = 0;
67 		}
68 	for (n = 0; n < 3; n++)
69 		if (rhits > rigg[n]){
70 			rhits -= rigg[n];
71 			rigg[n] = 0;
72 		} else {
73 			rigg[n] -= rhits;
74 			rhits = 0;
75 		}
76 	if (rigg[3] != -1 && rhits > rigg[3]) {
77 		rhits -= rigg[3];
78 		rigg[3] = 0;
79 	} else if (rigg[3] != -1) {
80 		rigg[3] -= rhits;
81 	}
82 	if (rig && !rigg[2] && (!rigg[3] || rigg[3] == -1))
83 		makesignal(on, "dismasted!", (struct ship *)0);
84 	if (portside(from, on, 0)) {
85 		guns = on->specs->gunR;
86 		car = on->specs->carR;
87 	} else {
88 		guns = on->specs->gunL;
89 		car = on->specs->carL;
90 	}
91 	if (ghits > car) {
92 		ghits -= car;
93 		car = 0;
94 	} else {
95 		car -= ghits;
96 		ghits = 0;
97 	}
98 	if (ghits > guns){
99 		ghits -= guns;
100 		guns = 0;
101 	} else {
102 		guns -= ghits;
103 		ghits = 0;
104 	}
105 	hull -= ghits;
106 	if (Ghit)
107 		Write(portside(from, on, 0) ? W_GUNR : W_GUNL,
108 			on, 0, guns, car, 0, 0);
109 	hull -= hhits;
110 	hull = hull < 0 ? 0 : hull;
111 	if (on->file->captured != 0 && Chit)
112 		Write(W_PCREW, on, 0, pc, 0, 0, 0);
113 	if (Hhit)
114 		Write(W_HULL, on, 0, hull, 0, 0, 0);
115 	if (Chit)
116 		Write(W_CREW, on, 0, crew[0], crew[1], crew[2], 0);
117 	if (Rhit)
118 		Write(W_RIGG, on, 0, rigg[0], rigg[1], rigg[2], rigg[3]);
119 	switch (shot) {
120 	case L_ROUND:
121 		message = "firing round shot on %s (%c%c)";
122 		break;
123 	case L_GRAPE:
124 		message = "firing grape shot on %s (%c%c)";
125 		break;
126 	case L_CHAIN:
127 		message = "firing chain shot on %s (%c%c)";
128 		break;
129 	case L_DOUBLE:
130 		message = "firing double shot on %s (%c%c)";
131 		break;
132 	case L_EXPLODE:
133 		message = "exploding shot on %s (%c%c)";
134 	}
135 	makesignal(from, message, on);
136 	if (roll == 6 && rig) {
137 		switch(Rhit) {
138 		case 0:
139 			message = "fore topsail sheets parted";
140 			break;
141 		case 1:
142 			message = "mizzen shrouds parted";
143 			break;
144 		case 2:
145 			message = "main topsail yard shot away";
146 			break;
147 		case 4:
148 			message = "fore topmast and foremast shrouds shot away";
149 			break;
150 		case 5:
151 			message = "mizzen mast and yard shot through";
152 			break;
153 		case 6:
154 			message = "foremast and spritsail yard shattered";
155 			break;
156 		case 7:
157 			message = "main topmast and mizzen mast shattered";
158 			break;
159 		}
160 		makesignal(on, message, (struct ship *)0);
161 	} else if (roll == 6) {
162 		switch (Hhit) {
163 		case 0:
164 			message = "anchor cables severed";
165 			break;
166 		case 1:
167 			message = "two anchor stocks shot away";
168 			break;
169 		case 2:
170 			message = "quarterdeck bulwarks damaged";
171 			break;
172 		case 3:
173 			message = "three gun ports shot away";
174 			break;
175 		case 4:
176 			message = "four guns dismounted";
177 			break;
178 		case 5:
179 			message = "rudder cables shot through";
180 			Write(W_TA, on, 0, 0, 0, 0, 0);
181 			break;
182 		case 6:
183 			message = "shot holes below the water line";
184 			break;
185 		}
186 		makesignal(on, message, (struct ship *)0);
187 	}
188 	/*
189 	if (Chit > 1 && on->file->readyL&R_INITIAL && on->file->readyR&R_INITIAL) {
190 		on->specs->qual--;
191 		if (on->specs->qual <= 0) {
192 			makesignal(on, "crew mutinying!", (struct ship *)0);
193 			on->specs->qual = 5;
194 			Write(W_CAPTURED, on, 0, on->file->index, 0, 0, 0);
195 		} else
196 			makesignal(on, "crew demoralized", (struct ship *)0);
197 		Write(W_QUAL, on, 0, on->specs->qual, 0, 0, 0);
198 	}
199 	*/
200 	if (!hull)
201 		strike(on, from);
202 }
203 
204 Cleansnag(from, to, all, flag)
205 register struct ship *from, *to;
206 char all, flag;
207 {
208 	if (flag & 1) {
209 		Write(W_UNGRAP, from, 0, to->file->index, all, 0, 0);
210 		Write(W_UNGRAP, to, 0, from->file->index, all, 0, 0);
211 	}
212 	if (flag & 2) {
213 		Write(W_UNFOUL, from, 0, to->file->index, all, 0, 0);
214 		Write(W_UNFOUL, to, 0, from->file->index, all, 0, 0);
215 	}
216 	if (!snagged2(from, to)) {
217 		if (!snagged(from)) {
218 			unboard(from, from, 1);		/* defense */
219 			unboard(from, from, 0);		/* defense */
220 		} else
221 			unboard(from, to, 0);		/* offense */
222 		if (!snagged(to)) {
223 			unboard(to, to, 1);		/* defense */
224 			unboard(to, to, 0);		/* defense */
225 		} else
226 			unboard(to, from, 0);		/* offense */
227 	}
228 }
229 
230 strike(ship, from)
231 register struct ship *ship, *from;
232 {
233 	int points;
234 
235 	if (ship->file->struck)
236 		return;
237 	Write(W_STRUCK, ship, 0, 1, 0, 0, 0);
238 	points = ship->specs->pts + from->file->points;
239 	Write(W_POINTS, from, 0, points, 0, 0, 0);
240 	unboard(ship, ship, 0);		/* all offense */
241 	unboard(ship, ship, 1);		/* all defense */
242 	switch (die()) {
243 	case 3:
244 	case 4:		/* ship may sink */
245 		Write(W_SINK, ship, 0, 1, 0, 0, 0);
246 		break;
247 	case 5:
248 	case 6:		/* ship may explode */
249 		Write(W_EXPLODE, ship, 0, 1, 0, 0, 0);
250 		break;
251 	}
252 	Write(W_SIGNAL, ship, 1, (int) "striking her colours!", 0, 0, 0);
253 }
254