xref: /csrg-svn/games/trek/compkl.c (revision 34205)
121270Sdist /*
221270Sdist  * Copyright (c) 1980 Regents of the University of California.
3*34205Sbostic  * All rights reserved.
4*34205Sbostic  *
5*34205Sbostic  * Redistribution and use in source and binary forms are permitted
6*34205Sbostic  * provided that this notice is preserved and that due credit is given
7*34205Sbostic  * to the University of California at Berkeley. The name of the University
8*34205Sbostic  * may not be used to endorse or promote products derived from this
9*34205Sbostic  * software without specific prior written permission. This software
10*34205Sbostic  * is provided ``as is'' without express or implied warranty.
1121270Sdist  */
1221270Sdist 
1311662Smckusick #ifndef lint
14*34205Sbostic static char sccsid[] = "@(#)compkl.c	5.2 (Berkeley) 05/05/88";
15*34205Sbostic #endif /* not lint */
1611662Smckusick 
1711662Smckusick # include	"trek.h"
1811662Smckusick 
1911662Smckusick /*
2011662Smckusick **  compute klingon distances
2111662Smckusick **
2211662Smckusick **	The klingon list has the distances for all klingons recomputed
2311662Smckusick **	and sorted.  The parameter is a Boolean flag which is set if
2411662Smckusick **	we have just entered a new quadrant.
2511662Smckusick **
2611662Smckusick **	This routine is used every time the Enterprise or the Klingons
2711662Smckusick **	move.
2811662Smckusick */
2911662Smckusick 
3011662Smckusick compkldist(f)
3111662Smckusick int	f;		/* set if new quadrant */
3211662Smckusick {
3311662Smckusick 	register int		i, dx, dy;
3411662Smckusick 	double			d;
3511662Smckusick 	double			temp;
3611662Smckusick 
3711662Smckusick 	if (Etc.nkling == 0)
3811662Smckusick 		return;
3911662Smckusick 	for (i = 0; i < Etc.nkling; i++)
4011662Smckusick 	{
4111662Smckusick 		/* compute distance to the Klingon */
4211662Smckusick 		dx = Ship.sectx - Etc.klingon[i].x;
4311662Smckusick 		dy = Ship.secty - Etc.klingon[i].y;
4411662Smckusick 		d = dx * dx + dy * dy;
4511662Smckusick 		d = sqrt(d);
4611662Smckusick 
4711662Smckusick 		/* compute average of new and old distances to Klingon */
4811662Smckusick 		if (!f)
4911662Smckusick 		{
5011662Smckusick 			temp = Etc.klingon[i].dist;
5111662Smckusick 			Etc.klingon[i].avgdist = 0.5 * (temp + d);
5211662Smckusick 		}
5311662Smckusick 		else
5411662Smckusick 		{
5511662Smckusick 			/* new quadrant: average is current */
5611662Smckusick 			Etc.klingon[i].avgdist = d;
5711662Smckusick 		}
5811662Smckusick 		Etc.klingon[i].dist = d;
5911662Smckusick 	}
6011662Smckusick 
6111662Smckusick 	/* leave them sorted */
6211662Smckusick 	sortkl();
6311662Smckusick }
6411662Smckusick 
6511662Smckusick 
6611662Smckusick /*
6711662Smckusick **  sort klingons
6811662Smckusick **
6911662Smckusick **	bubble sort on ascending distance
7011662Smckusick */
7111662Smckusick 
7211662Smckusick sortkl()
7311662Smckusick {
7411662Smckusick 	struct kling		t;
7511662Smckusick 	register int		f, i, m;
7611662Smckusick 
7711662Smckusick 	m = Etc.nkling - 1;
7811662Smckusick 	f = 1;
7911662Smckusick 	while (f)
8011662Smckusick 	{
8111662Smckusick 		f = 0;
8211662Smckusick 		for (i = 0; i < m; i++)
8311662Smckusick 			if (Etc.klingon[i].dist > Etc.klingon[i+1].dist)
8411662Smckusick 			{
8511662Smckusick 				bmove(&Etc.klingon[i], &t, sizeof t);
8611662Smckusick 				bmove(&Etc.klingon[i+1], &Etc.klingon[i], sizeof t);
8711662Smckusick 				bmove(&t, &Etc.klingon[i+1], sizeof t);
8811662Smckusick 				f = 1;
8911662Smckusick 			}
9011662Smckusick 	}
9111662Smckusick }
92