xref: /csrg-svn/games/sail/assorted.c (revision 14008)
111587Sleres #ifndef lint
2*14008Sedward static	char *sccsid = "@(#)assorted.c	1.2 83/07/20";
311587Sleres #endif
4*14008Sedward 
511587Sleres #include "externs.h"
611587Sleres 
7*14008Sedward table(rig, shot, hittable, on, from, roll)
8*14008Sedward struct ship *on, *from;
9*14008Sedward int rig, shot, hittable, roll;
1011587Sleres {
1111587Sleres 	register int hhits = 0, chits = 0, ghits = 0, rhits = 0;
1211587Sleres 	int Ghit = 0, Hhit = 0, Rhit = 0, Chit = 0;
1311587Sleres 	int guns, car, pc, hull;
1411587Sleres 	int crew[3];
1511587Sleres 	register int n;
1611587Sleres 	int rigg[4];
17*14008Sedward 	char *message;
18*14008Sedward 	struct Tables *tp;
1911587Sleres 
20*14008Sedward 	pc = on->file->pcrew;
21*14008Sedward 	hull = on->specs->hull;
22*14008Sedward 	crew[0] = on->specs->crew1;
23*14008Sedward 	crew[1] = on->specs->crew2;
24*14008Sedward 	crew[2] = on->specs->crew3;
25*14008Sedward 	rigg[0] = on->specs->rig1;
26*14008Sedward 	rigg[1] = on->specs->rig2;
27*14008Sedward 	rigg[2] = on->specs->rig3;
28*14008Sedward 	rigg[3] = on->specs->rig4;
29*14008Sedward 	if (shot == L_GRAPE)
3011587Sleres 		Chit = chits = hittable;
3111587Sleres 	else {
32*14008Sedward 		tp = &(rig ? RigTable : HullTable)[hittable][roll-1];
33*14008Sedward 		Chit = chits = tp->C;
34*14008Sedward 		Rhit = rhits = tp->R;
35*14008Sedward 		Hhit = hhits = tp->H;
36*14008Sedward 		Ghit = ghits = tp->G;
37*14008Sedward 		if (on->file->FS)
3811587Sleres 			rhits *= 2;
39*14008Sedward 		if (shot == L_CHAIN) {
4011587Sleres 			Ghit = ghits = 0;
4111587Sleres 			Hhit = hhits = 0;
4211587Sleres 		}
4311587Sleres 	}
44*14008Sedward 	if (on->file->captured != 0) {
45*14008Sedward 		pc -= (chits + 1) / 2;
4611587Sleres 		chits /= 2;
4711587Sleres 	}
48*14008Sedward 	for (n = 0; n < 3; n++)
49*14008Sedward 		if (chits > crew[n]) {
5011587Sleres 			chits -= crew[n];
5111587Sleres 			crew[n] = 0;
52*14008Sedward 		} else {
5311587Sleres 			crew[n] -= chits;
5411587Sleres 			chits = 0;
5511587Sleres 		}
56*14008Sedward 	for (n = 0; n < 3; n++)
5711587Sleres 		if (rhits > rigg[n]){
5811587Sleres 			rhits -= rigg[n];
5911587Sleres 			rigg[n] = 0;
60*14008Sedward 		} else {
6111587Sleres 			rigg[n] -= rhits;
6211587Sleres 			rhits = 0;
6311587Sleres 		}
64*14008Sedward 	if (rigg[3] != -1 && rhits > rigg[3]) {
6511587Sleres 		rhits -= rigg[3];
6611587Sleres 		rigg[3] = 0;
67*14008Sedward 	} else if (rigg[3] != -1) {
6811587Sleres 		rigg[3] -= rhits;
6911587Sleres 	}
7011587Sleres 	if (rig && !rigg[2] && (!rigg[3] || rigg[3] == -1))
71*14008Sedward 		makesignal(on, "dismasted!", (struct ship *)0);
72*14008Sedward 	if (portside(from, on, 0)) {
73*14008Sedward 		guns = on->specs->gunL;
74*14008Sedward 		car = on->specs->carL;
7511587Sleres 	} else {
76*14008Sedward 		guns = on->specs->gunR;
77*14008Sedward 		car = on->specs->carR;
7811587Sleres 	}
79*14008Sedward 	if (ghits > car) {
8011587Sleres 		ghits -= car;
8111587Sleres 		car = 0;
82*14008Sedward 	} else {
8311587Sleres 		car -= ghits;
8411587Sleres 		ghits = 0;
8511587Sleres 	}
8611587Sleres 	if (ghits > guns){
8711587Sleres 		ghits -= guns;
8811587Sleres 		guns = 0;
89*14008Sedward 	} else {
9011587Sleres 		guns -= ghits;
9111587Sleres 		ghits = 0;
9211587Sleres 	}
9311587Sleres 	hull -= ghits;
94*14008Sedward 	if (Ghit)
95*14008Sedward 		Write(portside(from, on, 0) ? W_GUNL : W_GUNR,
96*14008Sedward 			on, 0, guns, car, 0, 0);
9711587Sleres 	hull -= hhits;
9811587Sleres 	hull = hull < 0 ? 0 : hull;
99*14008Sedward 	if (on->file->captured != 0 && Chit)
100*14008Sedward 		Write(W_PCREW, on, 0, pc, 0, 0, 0);
10111587Sleres 	if (Hhit)
102*14008Sedward 		Write(W_HULL, on, 0, hull, 0, 0, 0);
10311587Sleres 	if (Chit)
104*14008Sedward 		Write(W_CREW, on, 0, crew[0], crew[1], crew[2], 0);
10511587Sleres 	if (Rhit)
106*14008Sedward 		Write(W_RIGG, on, 0, rigg[0], rigg[1], rigg[2], rigg[3]);
107*14008Sedward 	switch (shot) {
108*14008Sedward 	case L_ROUND:
109*14008Sedward 		message = "firing round shot on %s (%c%c)";
110*14008Sedward 		break;
111*14008Sedward 	case L_GRAPE:
112*14008Sedward 		message = "firing grape shot on %s (%c%c)";
113*14008Sedward 		break;
114*14008Sedward 	case L_CHAIN:
115*14008Sedward 		message = "firing chain shot on %s (%c%c)";
116*14008Sedward 		break;
117*14008Sedward 	case L_DOUBLE:
118*14008Sedward 		message = "firing double shot on %s (%c%c)";
119*14008Sedward 		break;
120*14008Sedward 	case L_EXPLODE:
121*14008Sedward 		message = "exploding shot on %s (%c%c)";
122*14008Sedward 	}
123*14008Sedward 	makesignal(from, message, on);
124*14008Sedward 	if (roll == 6 && rig) {
125*14008Sedward 		switch(Rhit) {
126*14008Sedward 		case 0:
127*14008Sedward 			message = "fore topsail sheets parted";
12811587Sleres 			break;
129*14008Sedward 		case 1:
130*14008Sedward 			message = "mizzen shrouds parted";
13111587Sleres 			break;
132*14008Sedward 		case 2:
133*14008Sedward 			message = "main topsail yard shot away";
13411587Sleres 			break;
135*14008Sedward 		case 4:
136*14008Sedward 			message = "fore topmast and foremast shrouds shot away";
13711587Sleres 			break;
138*14008Sedward 		case 5:
139*14008Sedward 			message = "mizzen mast and yard shot through";
140*14008Sedward 			break;
141*14008Sedward 		case 6:
142*14008Sedward 			message = "foremast and spritsail yard shattered";
143*14008Sedward 			break;
144*14008Sedward 		case 7:
145*14008Sedward 			message = "main topmast and mizzen mast shattered";
146*14008Sedward 			break;
14711587Sleres 		}
148*14008Sedward 		makesignal(on, message, (struct ship *)0);
149*14008Sedward 	} else if (roll == 6) {
150*14008Sedward 		switch (Hhit) {
151*14008Sedward 		case 0:
152*14008Sedward 			message = "anchor cables severed";
153*14008Sedward 			break;
154*14008Sedward 		case 1:
155*14008Sedward 			message = "two anchor stocks shot away";
156*14008Sedward 			break;
157*14008Sedward 		case 2:
158*14008Sedward 			message = "quarterdeck bulwarks damaged";
159*14008Sedward 			break;
160*14008Sedward 		case 3:
161*14008Sedward 			message = "three gun ports shot away";
162*14008Sedward 			break;
163*14008Sedward 		case 4:
164*14008Sedward 			message = "four guns dismounted";
165*14008Sedward 			break;
166*14008Sedward 		case 5:
167*14008Sedward 			message = "rudder cables shot through";
168*14008Sedward 			Write(W_TA, on, 0, 0, 0, 0, 0);
169*14008Sedward 			break;
170*14008Sedward 		case 6:
171*14008Sedward 			message = "shot holes below the water line";
172*14008Sedward 			break;
17311587Sleres 		}
174*14008Sedward 		makesignal(on, message, (struct ship *)0);
17511587Sleres 	}
176*14008Sedward 	if (Chit > 1 && on->file->readyL&R_INITIAL && on->file->readyR&R_INITIAL || Chit == 4) {
177*14008Sedward 		on->specs->qual--;
178*14008Sedward 		if (on->specs->qual <= 0) {
179*14008Sedward 			makesignal(on, "crew mutinying!", (struct ship *)0);
180*14008Sedward 			on->specs->qual = 5;
181*14008Sedward 			Write(W_CAPTURED, on, 0, on-SHIP(0), 0, 0, 0);
18211587Sleres 		} else
183*14008Sedward 			makesignal(on, "crew demoralized", (struct ship *)0);
184*14008Sedward 		Write(W_QUAL, on, 0, on->specs->qual, 0, 0, 0);
18511587Sleres 	}
18611587Sleres 	if (!hull)
187*14008Sedward 		strike(on, from);
18811587Sleres }
18911587Sleres 
190*14008Sedward cleanfoul(from, to, which)
191*14008Sedward register struct ship *from, *to;
192*14008Sedward int which;
19311587Sleres {
194*14008Sedward 	register int n;
195*14008Sedward 	register struct snag *s = to->file->fouls;
19611587Sleres 
197*14008Sedward 	Write(W_FOUL, from, 0, which, 0, 0, 0);
198*14008Sedward 	for (n = 0; n < NSHIP && (s->turnfoul || s->toship != from); n++, s++)
199*14008Sedward 		;
200*14008Sedward 	if (n < NSHIP)
201*14008Sedward 		Write(W_FOUL, to, 0, n, 0, 0, 0);
202*14008Sedward 	if (!snagged2(from, to)) {
203*14008Sedward 		if (!fouled(from) && !grappled(from)) {
204*14008Sedward 			unboard(from, from, 1);		/* defense */
205*14008Sedward 			unboard(from, from, 0);		/* defense */
20611587Sleres 		} else
207*14008Sedward 			unboard(from, to, 0);		/* defense */
208*14008Sedward 		if (!fouled(to) && !grappled(to)) {	/* defense */
209*14008Sedward 			unboard(to, to, 1);
210*14008Sedward 			unboard(to, to, 0);
21111587Sleres 		} else
212*14008Sedward 			unboard(to, from, 0);		/* offense */
21311587Sleres 	}
21411587Sleres }
21511587Sleres 
216*14008Sedward cleangrapple(from, to, which)
217*14008Sedward register struct ship *from, *to;
218*14008Sedward int which;
21911587Sleres {
220*14008Sedward 	register int n;
221*14008Sedward 	register struct snag *s = to->file->grapples;
22211587Sleres 
223*14008Sedward 	Write(W_GRAP, from, 0, which, 0, 0, 0);
224*14008Sedward 	for (n = 0; n < NSHIP && (s->turnfoul || s->toship != from); n++, s++)
225*14008Sedward 		;
226*14008Sedward 	if (n < NSHIP)
227*14008Sedward 		Write(W_FOUL, to, 0, n, 0, 0, 0);
228*14008Sedward 	if (!snagged2(from, to)) {
229*14008Sedward 		if (!fouled(from) && !grappled(from)) {
230*14008Sedward 			unboard(from, from, 1);		/* defense */
231*14008Sedward 			unboard(from, from, 0);		/* defense */
23211587Sleres 		} else
233*14008Sedward 			unboard(from, to, 0);		/* defense */
234*14008Sedward 		if (!fouled(to) && !grappled(to)) {	/* defense */
235*14008Sedward 			unboard(to, to, 1);
236*14008Sedward 			unboard(to, to, 0);
23711587Sleres 		} else
238*14008Sedward 			unboard(to, from, 0);		/* offense */
23911587Sleres 	}
24011587Sleres }
24111587Sleres 
242*14008Sedward strike(ship, from)
243*14008Sedward register struct ship *ship, *from;
24411587Sleres {
24511587Sleres 	int points;
24611587Sleres 
247*14008Sedward 	if (ship->file->struck)
248*14008Sedward 		return;
249*14008Sedward 	Write(W_STRUCK, ship, 0, 1, 0, 0, 0);
250*14008Sedward 	points = ship->specs->pts + from->file->points;
251*14008Sedward 	Write(W_POINTS, from, 0, points, 0, 0, 0);
252*14008Sedward 	unboard(ship, ship, 0);		/* all offense */
253*14008Sedward 	unboard(ship, ship, 1);		/* all defense */
254*14008Sedward 	switch (die()) {
255*14008Sedward 	case 3:
256*14008Sedward 	case 4:		/* ship may sink */
257*14008Sedward 		Write(W_SINK, ship, 0, 1, 0, 0, 0);
258*14008Sedward 		break;
259*14008Sedward 	case 5:
260*14008Sedward 	case 6:		/* ship may explode */
261*14008Sedward 		Write(W_EXPLODE, ship, 0, 1, 0, 0, 0);
262*14008Sedward 		break;
26311587Sleres 	}
264*14008Sedward 	Write(W_SIGNAL, ship, 1, (int) "striking her colours!", 0, 0, 0);
26511587Sleres }
266