xref: /csrg-svn/games/sail/assorted.c (revision 15317)
111587Sleres #ifndef lint
2*15317Sedward static	char *sccsid = "@(#)assorted.c	1.3 83/10/28";
311587Sleres #endif
414008Sedward 
511587Sleres #include "externs.h"
611587Sleres 
714008Sedward table(rig, shot, hittable, on, from, roll)
814008Sedward struct ship *on, *from;
914008Sedward 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];
1714008Sedward 	char *message;
1814008Sedward 	struct Tables *tp;
1911587Sleres 
2014008Sedward 	pc = on->file->pcrew;
2114008Sedward 	hull = on->specs->hull;
2214008Sedward 	crew[0] = on->specs->crew1;
2314008Sedward 	crew[1] = on->specs->crew2;
2414008Sedward 	crew[2] = on->specs->crew3;
2514008Sedward 	rigg[0] = on->specs->rig1;
2614008Sedward 	rigg[1] = on->specs->rig2;
2714008Sedward 	rigg[2] = on->specs->rig3;
2814008Sedward 	rigg[3] = on->specs->rig4;
2914008Sedward 	if (shot == L_GRAPE)
3011587Sleres 		Chit = chits = hittable;
3111587Sleres 	else {
3214008Sedward 		tp = &(rig ? RigTable : HullTable)[hittable][roll-1];
3314008Sedward 		Chit = chits = tp->C;
3414008Sedward 		Rhit = rhits = tp->R;
3514008Sedward 		Hhit = hhits = tp->H;
3614008Sedward 		Ghit = ghits = tp->G;
3714008Sedward 		if (on->file->FS)
3811587Sleres 			rhits *= 2;
3914008Sedward 		if (shot == L_CHAIN) {
4011587Sleres 			Ghit = ghits = 0;
4111587Sleres 			Hhit = hhits = 0;
4211587Sleres 		}
4311587Sleres 	}
4414008Sedward 	if (on->file->captured != 0) {
4514008Sedward 		pc -= (chits + 1) / 2;
4611587Sleres 		chits /= 2;
4711587Sleres 	}
4814008Sedward 	for (n = 0; n < 3; n++)
4914008Sedward 		if (chits > crew[n]) {
5011587Sleres 			chits -= crew[n];
5111587Sleres 			crew[n] = 0;
5214008Sedward 		} else {
5311587Sleres 			crew[n] -= chits;
5411587Sleres 			chits = 0;
5511587Sleres 		}
5614008Sedward 	for (n = 0; n < 3; n++)
5711587Sleres 		if (rhits > rigg[n]){
5811587Sleres 			rhits -= rigg[n];
5911587Sleres 			rigg[n] = 0;
6014008Sedward 		} else {
6111587Sleres 			rigg[n] -= rhits;
6211587Sleres 			rhits = 0;
6311587Sleres 		}
6414008Sedward 	if (rigg[3] != -1 && rhits > rigg[3]) {
6511587Sleres 		rhits -= rigg[3];
6611587Sleres 		rigg[3] = 0;
6714008Sedward 	} else if (rigg[3] != -1) {
6811587Sleres 		rigg[3] -= rhits;
6911587Sleres 	}
7011587Sleres 	if (rig && !rigg[2] && (!rigg[3] || rigg[3] == -1))
7114008Sedward 		makesignal(on, "dismasted!", (struct ship *)0);
7214008Sedward 	if (portside(from, on, 0)) {
7314008Sedward 		guns = on->specs->gunL;
7414008Sedward 		car = on->specs->carL;
7511587Sleres 	} else {
7614008Sedward 		guns = on->specs->gunR;
7714008Sedward 		car = on->specs->carR;
7811587Sleres 	}
7914008Sedward 	if (ghits > car) {
8011587Sleres 		ghits -= car;
8111587Sleres 		car = 0;
8214008Sedward 	} else {
8311587Sleres 		car -= ghits;
8411587Sleres 		ghits = 0;
8511587Sleres 	}
8611587Sleres 	if (ghits > guns){
8711587Sleres 		ghits -= guns;
8811587Sleres 		guns = 0;
8914008Sedward 	} else {
9011587Sleres 		guns -= ghits;
9111587Sleres 		ghits = 0;
9211587Sleres 	}
9311587Sleres 	hull -= ghits;
9414008Sedward 	if (Ghit)
9514008Sedward 		Write(portside(from, on, 0) ? W_GUNL : W_GUNR,
9614008Sedward 			on, 0, guns, car, 0, 0);
9711587Sleres 	hull -= hhits;
9811587Sleres 	hull = hull < 0 ? 0 : hull;
9914008Sedward 	if (on->file->captured != 0 && Chit)
10014008Sedward 		Write(W_PCREW, on, 0, pc, 0, 0, 0);
10111587Sleres 	if (Hhit)
10214008Sedward 		Write(W_HULL, on, 0, hull, 0, 0, 0);
10311587Sleres 	if (Chit)
10414008Sedward 		Write(W_CREW, on, 0, crew[0], crew[1], crew[2], 0);
10511587Sleres 	if (Rhit)
10614008Sedward 		Write(W_RIGG, on, 0, rigg[0], rigg[1], rigg[2], rigg[3]);
10714008Sedward 	switch (shot) {
10814008Sedward 	case L_ROUND:
10914008Sedward 		message = "firing round shot on %s (%c%c)";
11014008Sedward 		break;
11114008Sedward 	case L_GRAPE:
11214008Sedward 		message = "firing grape shot on %s (%c%c)";
11314008Sedward 		break;
11414008Sedward 	case L_CHAIN:
11514008Sedward 		message = "firing chain shot on %s (%c%c)";
11614008Sedward 		break;
11714008Sedward 	case L_DOUBLE:
11814008Sedward 		message = "firing double shot on %s (%c%c)";
11914008Sedward 		break;
12014008Sedward 	case L_EXPLODE:
12114008Sedward 		message = "exploding shot on %s (%c%c)";
12214008Sedward 	}
12314008Sedward 	makesignal(from, message, on);
12414008Sedward 	if (roll == 6 && rig) {
12514008Sedward 		switch(Rhit) {
12614008Sedward 		case 0:
12714008Sedward 			message = "fore topsail sheets parted";
12811587Sleres 			break;
12914008Sedward 		case 1:
13014008Sedward 			message = "mizzen shrouds parted";
13111587Sleres 			break;
13214008Sedward 		case 2:
13314008Sedward 			message = "main topsail yard shot away";
13411587Sleres 			break;
13514008Sedward 		case 4:
13614008Sedward 			message = "fore topmast and foremast shrouds shot away";
13711587Sleres 			break;
13814008Sedward 		case 5:
13914008Sedward 			message = "mizzen mast and yard shot through";
14014008Sedward 			break;
14114008Sedward 		case 6:
14214008Sedward 			message = "foremast and spritsail yard shattered";
14314008Sedward 			break;
14414008Sedward 		case 7:
14514008Sedward 			message = "main topmast and mizzen mast shattered";
14614008Sedward 			break;
14711587Sleres 		}
14814008Sedward 		makesignal(on, message, (struct ship *)0);
14914008Sedward 	} else if (roll == 6) {
15014008Sedward 		switch (Hhit) {
15114008Sedward 		case 0:
15214008Sedward 			message = "anchor cables severed";
15314008Sedward 			break;
15414008Sedward 		case 1:
15514008Sedward 			message = "two anchor stocks shot away";
15614008Sedward 			break;
15714008Sedward 		case 2:
15814008Sedward 			message = "quarterdeck bulwarks damaged";
15914008Sedward 			break;
16014008Sedward 		case 3:
16114008Sedward 			message = "three gun ports shot away";
16214008Sedward 			break;
16314008Sedward 		case 4:
16414008Sedward 			message = "four guns dismounted";
16514008Sedward 			break;
16614008Sedward 		case 5:
16714008Sedward 			message = "rudder cables shot through";
16814008Sedward 			Write(W_TA, on, 0, 0, 0, 0, 0);
16914008Sedward 			break;
17014008Sedward 		case 6:
17114008Sedward 			message = "shot holes below the water line";
17214008Sedward 			break;
17311587Sleres 		}
17414008Sedward 		makesignal(on, message, (struct ship *)0);
17511587Sleres 	}
176*15317Sedward 	/*
177*15317Sedward 	if (Chit > 1 && on->file->readyL&R_INITIAL && on->file->readyR&R_INITIAL) {
17814008Sedward 		on->specs->qual--;
17914008Sedward 		if (on->specs->qual <= 0) {
18014008Sedward 			makesignal(on, "crew mutinying!", (struct ship *)0);
18114008Sedward 			on->specs->qual = 5;
18214008Sedward 			Write(W_CAPTURED, on, 0, on-SHIP(0), 0, 0, 0);
18311587Sleres 		} else
18414008Sedward 			makesignal(on, "crew demoralized", (struct ship *)0);
18514008Sedward 		Write(W_QUAL, on, 0, on->specs->qual, 0, 0, 0);
18611587Sleres 	}
187*15317Sedward 	*/
18811587Sleres 	if (!hull)
18914008Sedward 		strike(on, from);
19011587Sleres }
19111587Sleres 
19214008Sedward cleanfoul(from, to, which)
19314008Sedward register struct ship *from, *to;
19414008Sedward int which;
19511587Sleres {
19614008Sedward 	register int n;
19714008Sedward 	register struct snag *s = to->file->fouls;
19811587Sleres 
19914008Sedward 	Write(W_FOUL, from, 0, which, 0, 0, 0);
20014008Sedward 	for (n = 0; n < NSHIP && (s->turnfoul || s->toship != from); n++, s++)
20114008Sedward 		;
20214008Sedward 	if (n < NSHIP)
20314008Sedward 		Write(W_FOUL, to, 0, n, 0, 0, 0);
20414008Sedward 	if (!snagged2(from, to)) {
20514008Sedward 		if (!fouled(from) && !grappled(from)) {
20614008Sedward 			unboard(from, from, 1);		/* defense */
20714008Sedward 			unboard(from, from, 0);		/* defense */
20811587Sleres 		} else
20914008Sedward 			unboard(from, to, 0);		/* defense */
21014008Sedward 		if (!fouled(to) && !grappled(to)) {	/* defense */
21114008Sedward 			unboard(to, to, 1);
21214008Sedward 			unboard(to, to, 0);
21311587Sleres 		} else
21414008Sedward 			unboard(to, from, 0);		/* offense */
21511587Sleres 	}
21611587Sleres }
21711587Sleres 
21814008Sedward cleangrapple(from, to, which)
21914008Sedward register struct ship *from, *to;
22014008Sedward int which;
22111587Sleres {
22214008Sedward 	register int n;
22314008Sedward 	register struct snag *s = to->file->grapples;
22411587Sleres 
22514008Sedward 	Write(W_GRAP, from, 0, which, 0, 0, 0);
22614008Sedward 	for (n = 0; n < NSHIP && (s->turnfoul || s->toship != from); n++, s++)
22714008Sedward 		;
22814008Sedward 	if (n < NSHIP)
22914008Sedward 		Write(W_FOUL, to, 0, n, 0, 0, 0);
23014008Sedward 	if (!snagged2(from, to)) {
23114008Sedward 		if (!fouled(from) && !grappled(from)) {
23214008Sedward 			unboard(from, from, 1);		/* defense */
23314008Sedward 			unboard(from, from, 0);		/* defense */
23411587Sleres 		} else
23514008Sedward 			unboard(from, to, 0);		/* defense */
23614008Sedward 		if (!fouled(to) && !grappled(to)) {	/* defense */
23714008Sedward 			unboard(to, to, 1);
23814008Sedward 			unboard(to, to, 0);
23911587Sleres 		} else
24014008Sedward 			unboard(to, from, 0);		/* offense */
24111587Sleres 	}
24211587Sleres }
24311587Sleres 
24414008Sedward strike(ship, from)
24514008Sedward register struct ship *ship, *from;
24611587Sleres {
24711587Sleres 	int points;
24811587Sleres 
24914008Sedward 	if (ship->file->struck)
25014008Sedward 		return;
25114008Sedward 	Write(W_STRUCK, ship, 0, 1, 0, 0, 0);
25214008Sedward 	points = ship->specs->pts + from->file->points;
25314008Sedward 	Write(W_POINTS, from, 0, points, 0, 0, 0);
25414008Sedward 	unboard(ship, ship, 0);		/* all offense */
25514008Sedward 	unboard(ship, ship, 1);		/* all defense */
25614008Sedward 	switch (die()) {
25714008Sedward 	case 3:
25814008Sedward 	case 4:		/* ship may sink */
25914008Sedward 		Write(W_SINK, ship, 0, 1, 0, 0, 0);
26014008Sedward 		break;
26114008Sedward 	case 5:
26214008Sedward 	case 6:		/* ship may explode */
26314008Sedward 		Write(W_EXPLODE, ship, 0, 1, 0, 0, 0);
26414008Sedward 		break;
26511587Sleres 	}
26614008Sedward 	Write(W_SIGNAL, ship, 1, (int) "striking her colours!", 0, 0, 0);
26711587Sleres }
268