xref: /csrg-svn/games/trek/compkl.c (revision 60857)
121270Sdist /*
2*60857Sbostic  * Copyright (c) 1980, 1993
3*60857Sbostic  *	The Regents of the University of California.  All rights reserved.
434205Sbostic  *
542605Sbostic  * %sccs.include.redist.c%
621270Sdist  */
721270Sdist 
811662Smckusick #ifndef lint
9*60857Sbostic static char sccsid[] = "@(#)compkl.c	8.1 (Berkeley) 05/31/93";
1034205Sbostic #endif /* not lint */
1111662Smckusick 
1211662Smckusick # include	"trek.h"
1311662Smckusick 
1411662Smckusick /*
1511662Smckusick **  compute klingon distances
1611662Smckusick **
1711662Smckusick **	The klingon list has the distances for all klingons recomputed
1811662Smckusick **	and sorted.  The parameter is a Boolean flag which is set if
1911662Smckusick **	we have just entered a new quadrant.
2011662Smckusick **
2111662Smckusick **	This routine is used every time the Enterprise or the Klingons
2211662Smckusick **	move.
2311662Smckusick */
2411662Smckusick 
compkldist(f)2511662Smckusick compkldist(f)
2611662Smckusick int	f;		/* set if new quadrant */
2711662Smckusick {
2811662Smckusick 	register int		i, dx, dy;
2911662Smckusick 	double			d;
3011662Smckusick 	double			temp;
3111662Smckusick 
3211662Smckusick 	if (Etc.nkling == 0)
3311662Smckusick 		return;
3411662Smckusick 	for (i = 0; i < Etc.nkling; i++)
3511662Smckusick 	{
3611662Smckusick 		/* compute distance to the Klingon */
3711662Smckusick 		dx = Ship.sectx - Etc.klingon[i].x;
3811662Smckusick 		dy = Ship.secty - Etc.klingon[i].y;
3911662Smckusick 		d = dx * dx + dy * dy;
4011662Smckusick 		d = sqrt(d);
4111662Smckusick 
4211662Smckusick 		/* compute average of new and old distances to Klingon */
4311662Smckusick 		if (!f)
4411662Smckusick 		{
4511662Smckusick 			temp = Etc.klingon[i].dist;
4611662Smckusick 			Etc.klingon[i].avgdist = 0.5 * (temp + d);
4711662Smckusick 		}
4811662Smckusick 		else
4911662Smckusick 		{
5011662Smckusick 			/* new quadrant: average is current */
5111662Smckusick 			Etc.klingon[i].avgdist = d;
5211662Smckusick 		}
5311662Smckusick 		Etc.klingon[i].dist = d;
5411662Smckusick 	}
5511662Smckusick 
5611662Smckusick 	/* leave them sorted */
5711662Smckusick 	sortkl();
5811662Smckusick }
5911662Smckusick 
6011662Smckusick 
6111662Smckusick /*
6211662Smckusick **  sort klingons
6311662Smckusick **
6411662Smckusick **	bubble sort on ascending distance
6511662Smckusick */
6611662Smckusick 
sortkl()6711662Smckusick sortkl()
6811662Smckusick {
6911662Smckusick 	struct kling		t;
7011662Smckusick 	register int		f, i, m;
7111662Smckusick 
7211662Smckusick 	m = Etc.nkling - 1;
7311662Smckusick 	f = 1;
7411662Smckusick 	while (f)
7511662Smckusick 	{
7611662Smckusick 		f = 0;
7711662Smckusick 		for (i = 0; i < m; i++)
7811662Smckusick 			if (Etc.klingon[i].dist > Etc.klingon[i+1].dist)
7911662Smckusick 			{
8011662Smckusick 				bmove(&Etc.klingon[i], &t, sizeof t);
8111662Smckusick 				bmove(&Etc.klingon[i+1], &Etc.klingon[i], sizeof t);
8211662Smckusick 				bmove(&t, &Etc.klingon[i+1], sizeof t);
8311662Smckusick 				f = 1;
8411662Smckusick 			}
8511662Smckusick 	}
8611662Smckusick }
87