xref: /csrg-svn/games/sail/dr_2.c (revision 60846)
118695Sedward /*
2*60846Sbostic  * Copyright (c) 1983, 1993
3*60846Sbostic  *	The Regents of the University of California.  All rights reserved.
433695Sbostic  *
542602Sbostic  * %sccs.include.redist.c%
618695Sedward  */
718695Sedward 
811590Sleres #ifndef lint
9*60846Sbostic static char sccsid[] = "@(#)dr_2.c	8.1 (Berkeley) 05/31/93";
1033695Sbostic #endif /* not lint */
1111590Sleres 
1214010Sedward #include "driver.h"
1311590Sleres 
1414010Sedward #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
1514010Sedward 
thinkofgrapples()1611590Sleres thinkofgrapples()
1711590Sleres {
1814010Sedward 	register struct ship *sp, *sq;
1914010Sedward 	char friendly;
2011590Sleres 
2114010Sedward 	foreachship(sp) {
2214010Sedward 		if (sp->file->captain[0] || sp->file->dir == 0)
2314010Sedward 			continue;
2414010Sedward 		foreachship(sq) {
2514010Sedward 			friendly = sp->nationality == capship(sq)->nationality;
2614010Sedward 			if (!friendly) {
2714010Sedward 				if (sp->file->struck || sp->file->captured != 0)
2814010Sedward 					continue;
2914010Sedward 				if (range(sp, sq) != 1)
3014010Sedward 					continue;
3114010Sedward 				if (grappled2(sp, sq))
3214010Sedward 					if (toughmelee(sp, sq, 0, 0))
3314010Sedward 						ungrap(sp, sq);
3414010Sedward 					else
3514010Sedward 						grap(sp, sq);
3614010Sedward 				else if (couldwin(sp, sq)) {
3714010Sedward 					grap(sp, sq);
3814010Sedward 					sp->file->loadwith = L_GRAPE;
3911590Sleres 				}
4014010Sedward 			} else
4114010Sedward 				ungrap(sp, sq);
4211590Sleres 		}
4311590Sleres 	}
4411590Sleres }
4511590Sleres 
checkup()4611590Sleres checkup()
4711590Sleres {
4814010Sedward 	register struct ship *sp, *sq;
4914010Sedward 	register char explode, sink;
5011590Sleres 
5114010Sedward 	foreachship(sp) {
5215711Sedward 		if (sp->file->dir == 0)
5315711Sedward 			continue;
5414010Sedward 		explode = sp->file->explode;
5514010Sedward 		sink = sp->file->sink;
5615711Sedward 		if (explode != 1 && sink != 1)
5715711Sedward 			continue;
5814010Sedward 		if (die() < 5)
5914010Sedward 			continue;
6015711Sedward 		Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
6115722Sedward 		Write(W_DIR, sp, 0, 0, 0, 0, 0);
6215722Sedward 		if (snagged(sp))
6315722Sedward 			foreachship(sq)
6415722Sedward 				cleansnag(sp, sq, 1);
6514010Sedward 		if (sink != 1) {
6614010Sedward 			makesignal(sp, "exploding!", (struct ship *)0);
6714010Sedward 			foreachship(sq) {
6814010Sedward 				if (sp != sq && sq->file->dir && range(sp, sq) < 4)
6914010Sedward 					table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
7011590Sleres 			}
7114010Sedward 		} else
7214010Sedward 			makesignal(sp, "sinking!", (struct ship *)0);
7311590Sleres 	}
7411590Sleres }
7511590Sleres 
prizecheck()7611590Sleres prizecheck()
7711590Sleres {
7814010Sedward 	register struct ship *sp;
7911590Sleres 
8014010Sedward 	foreachship(sp) {
8114010Sedward 		if (sp->file->captured == 0)
8214010Sedward 			continue;
8314010Sedward 		if (sp->file->struck || sp->file->dir == 0)
8414010Sedward 			continue;
8515878Sedward 		if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
8614010Sedward 			Write(W_SIGNAL, sp, 1,
8714010Sedward 				(int)"prize crew overthrown", 0, 0, 0);
8815878Sedward 			Write(W_POINTS, sp->file->captured, 0, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
8915878Sedward 			Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
9011590Sleres 		}
9111590Sleres 	}
9211590Sleres }
9311590Sleres 
strend(str)9414010Sedward strend(str)
9511590Sleres char *str;
9611590Sleres {
9714010Sedward 	register char *p;
9811590Sleres 
9914010Sedward 	for (p = str; *p; p++)
10014010Sedward 		;
10114010Sedward 	return p == str ? 0 : p[-1];
10211590Sleres }
10311590Sleres 
closeon(from,to,command,ta,ma,af)10414010Sedward closeon(from, to, command, ta, ma, af)
10514010Sedward register struct ship *from, *to;
10614010Sedward char command[];
10714010Sedward int ma, ta, af;
10811590Sleres {
10911590Sleres 	int high;
11011590Sleres 	char temp[10];
11111590Sleres 
11211590Sleres 	temp[0] = command[0] = '\0';
11311590Sleres 	high = -30000;
11414010Sedward 	try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
11511590Sleres }
11611590Sleres 
11714010Sedward int dtab[] = {0,1,1,2,3,4,4,5};		/* diagonal distances in x==y */
11811590Sleres 
score(movement,ship,to,onlytemp)11914010Sedward score(movement, ship, to, onlytemp)
12014010Sedward char movement[];
12114010Sedward register struct ship *ship, *to;
12214010Sedward char onlytemp;
12311590Sleres {
12415169Sedward 	char drift;
12515169Sedward 	int row, col, dir, total, ran;
12614010Sedward 	register struct File *fp = ship->file;
12711590Sleres 
12814010Sedward 	if ((dir = fp->dir) == 0)
12914010Sedward 		return 0;
13014010Sedward 	row = fp->row;
13114010Sedward 	col = fp->col;
13214010Sedward 	drift = fp->drift;
13314010Sedward 	move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
13414010Sedward 	if (!*movement)
13514010Sedward 		(void) strcpy(movement, "d");
13614010Sedward 
13714010Sedward 	ran = range(ship, to);
13814010Sedward 	total = -50 * ran;
13914010Sedward 	if (ran < 4 && gunsbear(ship, to))
14014010Sedward 		total += 60;
14114010Sedward 	if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
14214010Sedward 		total = -30000;
14314010Sedward 
14414010Sedward 	if (!onlytemp) {
14514010Sedward 		fp->row = row;
14614010Sedward 		fp->col = col;
14714010Sedward 		fp->dir = dir;
14814010Sedward 	}
14914010Sedward 	return total;
15014010Sedward }
15114010Sedward 
move(p,ship,dir,row,col,drift)15214010Sedward move(p, ship, dir, row, col, drift)
15314010Sedward register char *p;
15414010Sedward register struct ship *ship;
15515169Sedward register char *dir;
15615169Sedward register short *row, *col;
15715169Sedward register char *drift;
15814010Sedward {
15914010Sedward 	int dist;
16014010Sedward 	char moved = 0;
16114010Sedward 
16214010Sedward 	for (; *p; p++) {
16314010Sedward 		switch (*p) {
16414010Sedward 		case 'r':
16514010Sedward 			if (++*dir == 9)
16614010Sedward 				*dir = 1;
16714010Sedward 			break;
16814010Sedward 		case 'l':
16914010Sedward 			if (--*dir == 0)
17014010Sedward 				*dir = 8;
17114010Sedward 			break;
17214010Sedward 		case '1': case '2': case '3': case '4':
17314010Sedward 		case '5': case '6': case '7':
17414010Sedward 			moved++;
17514010Sedward 			if (*dir % 2 == 0)
17614010Sedward 				dist = dtab[*p - '0'];
17714010Sedward 			else
17814010Sedward 				dist = *p - '0';
17914010Sedward 			*row -= dr[*dir] * dist;
18014010Sedward 			*col -= dc[*dir] * dist;
18114010Sedward 			break;
18214010Sedward 		}
18314010Sedward 	}
18414010Sedward 	if (!moved) {
18516426Sedward 		if (windspeed != 0 && ++*drift > 2) {
18614010Sedward 			if (ship->specs->class >= 3 && !snagged(ship)
18715296Sedward 			    || (turn & 1) == 0) {
18814010Sedward 				*row -= dr[winddir];
18914010Sedward 				*col -= dc[winddir];
19011590Sleres 			}
19111590Sleres 		}
19214010Sedward 	} else
19315296Sedward 		*drift = 0;
19411590Sleres }
19511590Sleres 
try(command,temp,ma,ta,af,vma,dir,f,t,high,rakeme)19614010Sedward try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
19714010Sedward register struct ship *f, *t;
19814010Sedward int ma, ta, af, *high, rakeme;
19914010Sedward char command[], temp[];
20011590Sleres {
20111590Sleres 	register int new, n;
20211590Sleres 	char st[4];
20314010Sedward #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
20411590Sleres 
20511590Sleres 	if ((n = strend(temp)) < '1' || n > '9')
20614010Sedward 		for (n = 1; vma - n >= 0; n++) {
20714010Sedward 			(void) sprintf(st, "%d", n);
20814010Sedward 			(void) strcat(temp, st);
20914010Sedward 			new = score(temp, f, t, rakeme);
21014010Sedward 			if (new > *high && (!rakeme || rakeyou)) {
21111590Sleres 				*high = new;
21214010Sedward 				(void) strcpy(command, temp);
21311590Sleres 			}
21414010Sedward 			try(command, temp, ma-n, ta, af, vma-n,
21514010Sedward 				dir, f, t, high, rakeme);
21611590Sleres 			rmend(temp);
21711590Sleres 		}
21814010Sedward 	if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
21914010Sedward 		(void) strcat(temp, "r");
22014010Sedward 		new = score(temp, f, t, rakeme);
22114010Sedward 		if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
22211590Sleres 			*high = new;
22314010Sedward 			(void) strcpy(command, temp);
22411590Sleres 		}
22514010Sedward 		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);
22611590Sleres 		rmend(temp);
22711590Sleres 	}
22814010Sedward 	if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
22914010Sedward 		(void) strcat(temp, "l");
23014010Sedward 		new = score(temp, f, t, rakeme);
23114010Sedward 		if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
23211590Sleres 			*high = new;
23314010Sedward 			(void) strcpy(command, temp);
23411590Sleres 		}
23514010Sedward 		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);
23611590Sleres 		rmend(temp);
23711590Sleres 	}
23811590Sleres }
23915247Sedward 
rmend(str)24015247Sedward rmend(str)
24115247Sedward char *str;
24215247Sedward {
24315247Sedward 	register char *p;
24415247Sedward 
24515247Sedward 	for (p = str; *p; p++)
24615247Sedward 		;
24715247Sedward 	if (p != str)
24815247Sedward 		*--p = 0;
24915247Sedward }
250