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