xref: /csrg-svn/games/sail/dr_2.c (revision 15296)
111590Sleres #ifndef lint
2*15296Sedward static	char *sccsid = "@(#)dr_2.c	1.6 83/10/27";
311590Sleres #endif
411590Sleres 
514010Sedward #include "driver.h"
611590Sleres 
714010Sedward #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
814010Sedward 
911590Sleres thinkofgrapples()
1011590Sleres {
1114010Sedward 	register struct ship *sp, *sq;
1214010Sedward 	char friendly;
1311590Sleres 
1414010Sedward 	foreachship(sp) {
1514010Sedward 		if (sp->file->captain[0] || sp->file->dir == 0)
1614010Sedward 			continue;
1714010Sedward 		foreachship(sq) {
1814010Sedward 			friendly = sp->nationality == capship(sq)->nationality;
1914010Sedward 			if (!friendly) {
2014010Sedward 				if (sp->file->struck || sp->file->captured != 0)
2114010Sedward 					continue;
2214010Sedward 				if (range(sp, sq) != 1)
2314010Sedward 					continue;
2414010Sedward 				if (grappled2(sp, sq))
2514010Sedward 					if (toughmelee(sp, sq, 0, 0))
2614010Sedward 						ungrap(sp, sq);
2714010Sedward 					else
2814010Sedward 						grap(sp, sq);
2914010Sedward 				else if (couldwin(sp, sq)) {
3014010Sedward 					grap(sp, sq);
3114010Sedward 					sp->file->loadwith = L_GRAPE;
3211590Sleres 				}
3314010Sedward 			} else
3414010Sedward 				ungrap(sp, sq);
3511590Sleres 		}
3611590Sleres 	}
3711590Sleres }
3811590Sleres 
3911590Sleres checkup()
4011590Sleres {
4114010Sedward 	register int k;
4214010Sedward 	register struct ship *sp, *sq;
4314010Sedward 	register char explode, sink;
4411590Sleres 
4515203Sedward 	/*
4611590Sleres 	readpos();
4715203Sedward 	*/
4814010Sedward 	foreachship(sp) {
4914010Sedward 		explode = sp->file->explode;
5014010Sedward 		sink = sp->file->sink;
5114010Sedward 		if (die() < 5)
5214010Sedward 			continue;
5314010Sedward 		if (explode != 1 && sink != 1)
5414010Sedward 			continue;
5514010Sedward 		Write(sink ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
5614010Sedward 		sp->file->dir = 0;	/* hopefully enough to kill ship */
5714010Sedward 		/* Write(n, 0, 10, 0); XXX */
5814010Sedward 		Write(W_CLASS, sp, 0, 0, 0, 0, 0);
5914010Sedward 		if (fouled(sp) || grappled(sp)) {
6014010Sedward 			for (k = 0; k < NSHIP; k++) {
6114010Sedward 				if (sp->file->fouls[k].turnfoul)
6214010Sedward 					cleanfoul(sp,
6314010Sedward 						sp->file->fouls[k].toship, k);
6411590Sleres 			}
6514010Sedward 			for (k = 0; k < NSHIP; k++) {
6614010Sedward 				if (sp->file->grapples[k].turnfoul)
6714010Sedward 					cleangrapple(sp,
6814010Sedward 						sp->file->grapples[k].toship,
6914010Sedward 						k);
7011590Sleres 			}
7114010Sedward 		}
7214010Sedward 		if (sink != 1) {
7314010Sedward 			makesignal(sp, "exploding!", (struct ship *)0);
7414010Sedward 			foreachship(sq) {
7514010Sedward 				if (sp != sq && sq->file->dir && range(sp, sq) < 4)
7614010Sedward 					table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
7711590Sleres 			}
7814010Sedward 		} else
7914010Sedward 			makesignal(sp, "sinking!", (struct ship *)0);
8011590Sleres 	}
8111590Sleres }
8211590Sleres 
8311590Sleres prizecheck()
8411590Sleres {
8514010Sedward 	register struct ship *sp;
8614010Sedward 	register int prisoners, points;
8711590Sleres 
8814010Sedward 	foreachship(sp) {
8914010Sedward 		if (sp->file->captured == 0)
9014010Sedward 			continue;
9114010Sedward 		if (sp->file->struck || sp->file->dir == 0)
9214010Sedward 			continue;
9314010Sedward 		prisoners = sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3;
9414010Sedward 		if (prisoners > sp->file->pcrew * 6) {
9514010Sedward 			Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
9614010Sedward 			Write(W_SIGNAL, sp, 1,
9714010Sedward 				(int)"prize crew overthrown", 0, 0, 0);
9814010Sedward 			points = sp->file->captured->file->points
9914010Sedward 				- 2 * sp->specs->pts;
10014010Sedward 			Write(W_POINTS, sp->file->captured, 0, points, 0, 0, 0);
10111590Sleres 		}
10211590Sleres 	}
10311590Sleres }
10411590Sleres 
10514010Sedward strend(str)
10611590Sleres char *str;
10711590Sleres {
10814010Sedward 	register char *p;
10911590Sleres 
11014010Sedward 	for (p = str; *p; p++)
11114010Sedward 		;
11214010Sedward 	return p == str ? 0 : p[-1];
11311590Sleres }
11411590Sleres 
11514010Sedward closeon(from, to, command, ta, ma, af)
11614010Sedward register struct ship *from, *to;
11714010Sedward char command[];
11814010Sedward int ma, ta, af;
11911590Sleres {
12011590Sleres 	int high;
12111590Sleres 	char temp[10];
12211590Sleres 
12311590Sleres 	temp[0] = command[0] = '\0';
12411590Sleres 	high = -30000;
12514010Sedward 	try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
12611590Sleres }
12711590Sleres 
12814010Sedward int dtab[] = {0,1,1,2,3,4,4,5};		/* diagonal distances in x==y */
12911590Sleres 
13014010Sedward score(movement, ship, to, onlytemp)
13114010Sedward char movement[];
13214010Sedward register struct ship *ship, *to;
13314010Sedward char onlytemp;
13411590Sleres {
13515169Sedward 	char drift;
13615169Sedward 	int row, col, dir, total, ran;
13714010Sedward 	register struct File *fp = ship->file;
13811590Sleres 
13914010Sedward 	if ((dir = fp->dir) == 0)
14014010Sedward 		return 0;
14114010Sedward 	row = fp->row;
14214010Sedward 	col = fp->col;
14314010Sedward 	drift = fp->drift;
14414010Sedward 	move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
14514010Sedward 	if (!*movement)
14614010Sedward 		(void) strcpy(movement, "d");
14714010Sedward 
14814010Sedward 	ran = range(ship, to);
14914010Sedward 	total = -50 * ran;
15014010Sedward 	if (ran < 4 && gunsbear(ship, to))
15114010Sedward 		total += 60;
15214010Sedward 	if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
15314010Sedward 		total = -30000;
15414010Sedward 
15514010Sedward 	if (!onlytemp) {
15614010Sedward 		fp->row = row;
15714010Sedward 		fp->col = col;
15814010Sedward 		fp->dir = dir;
15914010Sedward 	}
16014010Sedward 	return total;
16114010Sedward }
16214010Sedward 
16314010Sedward moveship(ship, movement)
16414010Sedward struct ship *ship;
16514010Sedward char *movement;
16614010Sedward {
16714010Sedward 	register struct File *fp = ship->file;
16815247Sedward 	char drift = fp->drift;
16915203Sedward 	int row = fp->row;
17015203Sedward 	int col = fp->col;
17115203Sedward 	int dir = fp->dir;
17214010Sedward 
17314010Sedward 	if (fp->dir == 0)
17414010Sedward 		return;
17514010Sedward 	move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
17614010Sedward 	if (drift > 2 || *movement == 0)
17714010Sedward 		(void) strcat(movement, "d");
17815203Sedward 	if (fp->drift != drift)
17914010Sedward 		Write(W_DRIFT, ship, 0, drift, 0, 0, 0);
18015203Sedward 	if (fp->row != row)
18114010Sedward 		Write(W_SHIPROW, ship, 0, fp->row, 0, 0, 0);
18215203Sedward 	if (fp->col != col)
18314010Sedward 		Write(W_SHIPCOL, ship, 0, fp->col, 0, 0, 0);
18415203Sedward 	if (fp->dir != dir)
18514010Sedward 		Write(W_SHIPDIR, ship, 0, fp->dir, 0, 0, 0);
18614010Sedward }
18714010Sedward 
18814010Sedward move(p, ship, dir, row, col, drift)
18914010Sedward register char *p;
19014010Sedward register struct ship *ship;
19115169Sedward register char *dir;
19215169Sedward register short *row, *col;
19315169Sedward register char *drift;
19414010Sedward {
19514010Sedward 	int dist;
19614010Sedward 	char moved = 0;
19714010Sedward 
19814010Sedward 	for (; *p; p++) {
19914010Sedward 		switch (*p) {
20014010Sedward 		case 'r':
20114010Sedward 			if (++*dir == 9)
20214010Sedward 				*dir = 1;
20314010Sedward 			break;
20414010Sedward 		case 'l':
20514010Sedward 			if (--*dir == 0)
20614010Sedward 				*dir = 8;
20714010Sedward 			break;
20814010Sedward 		case '1': case '2': case '3': case '4':
20914010Sedward 		case '5': case '6': case '7':
21014010Sedward 			moved++;
21114010Sedward 			if (*dir % 2 == 0)
21214010Sedward 				dist = dtab[*p - '0'];
21314010Sedward 			else
21414010Sedward 				dist = *p - '0';
21514010Sedward 			*row -= dr[*dir] * dist;
21614010Sedward 			*col -= dc[*dir] * dist;
21714010Sedward 			break;
21814010Sedward 		}
21914010Sedward 	}
22014010Sedward 	if (!windspeed)
22114010Sedward 		*drift = 1;
22214010Sedward 	if (!moved) {
22314010Sedward 		if (++*drift > 2) {
22414010Sedward 			if (ship->specs->class >= 3 && !snagged(ship)
225*15296Sedward 			    || (turn & 1) == 0) {
22614010Sedward 				*row -= dr[winddir];
22714010Sedward 				*col -= dc[winddir];
22811590Sleres 			}
22911590Sleres 		}
23014010Sedward 	} else
231*15296Sedward 		*drift = 0;
23211590Sleres }
23311590Sleres 
23414010Sedward try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
23514010Sedward register struct ship *f, *t;
23614010Sedward int ma, ta, af, *high, rakeme;
23714010Sedward char command[], temp[];
23811590Sleres {
23911590Sleres 	register int new, n;
24011590Sleres 	char st[4];
24114010Sedward #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
24211590Sleres 
24311590Sleres 	if ((n = strend(temp)) < '1' || n > '9')
24414010Sedward 		for (n = 1; vma - n >= 0; n++) {
24514010Sedward 			(void) sprintf(st, "%d", n);
24614010Sedward 			(void) strcat(temp, st);
24714010Sedward 			new = score(temp, f, t, rakeme);
24814010Sedward 			if (new > *high && (!rakeme || rakeyou)) {
24911590Sleres 				*high = new;
25014010Sedward 				(void) strcpy(command, temp);
25111590Sleres 			}
25214010Sedward 			try(command, temp, ma-n, ta, af, vma-n,
25314010Sedward 				dir, f, t, high, rakeme);
25411590Sleres 			rmend(temp);
25511590Sleres 		}
25614010Sedward 	if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
25714010Sedward 		(void) strcat(temp, "r");
25814010Sedward 		new = score(temp, f, t, rakeme);
25914010Sedward 		if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
26011590Sleres 			*high = new;
26114010Sedward 			(void) strcpy(command, temp);
26211590Sleres 		}
26314010Sedward 		try(command, temp, ma-1, ta-1, af, min(ma-1, maxmove(f, (dir == 8 ? 1 : dir+1), 0)), (dir == 8 ? 1 : dir+1),f,t,high,rakeme);
26411590Sleres 		rmend(temp);
26511590Sleres 	}
26614010Sedward 	if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
26714010Sedward 		(void) strcat(temp, "l");
26814010Sedward 		new = score(temp, f, t, rakeme);
26914010Sedward 		if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
27011590Sleres 			*high = new;
27114010Sedward 			(void) strcpy(command, temp);
27211590Sleres 		}
27314010Sedward 		try(command, temp, ma-1, ta-1, af, (min(ma-1,maxmove(f, (dir-1 ? dir-1 : 8), 0))), (dir-1 ? dir -1 : 8), f, t, high, rakeme);
27411590Sleres 		rmend(temp);
27511590Sleres 	}
27611590Sleres }
27715247Sedward 
27815247Sedward rmend(str)
27915247Sedward char *str;
28015247Sedward {
28115247Sedward 	register char *p;
28215247Sedward 
28315247Sedward 	for (p = str; *p; p++)
28415247Sedward 		;
28515247Sedward 	if (p != str)
28615247Sedward 		*--p = 0;
28715247Sedward }
288