1*50b862e2Sdholland /* $NetBSD: compkl.c,v 1.8 2009/05/24 21:44:56 dholland Exp $ */
2887dd216Scgd
361f28255Scgd /*
4887dd216Scgd * Copyright (c) 1980, 1993
5887dd216Scgd * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32ef383c95Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34887dd216Scgd #if 0
35887dd216Scgd static char sccsid[] = "@(#)compkl.c 8.1 (Berkeley) 5/31/93";
36887dd216Scgd #else
37*50b862e2Sdholland __RCSID("$NetBSD: compkl.c,v 1.8 2009/05/24 21:44:56 dholland Exp $");
38887dd216Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
41ef383c95Schristos #include <math.h>
4261f28255Scgd #include "trek.h"
4361f28255Scgd
4461f28255Scgd /*
4561f28255Scgd ** compute klingon distances
4661f28255Scgd **
4761f28255Scgd ** The klingon list has the distances for all klingons recomputed
4861f28255Scgd ** and sorted. The parameter is a Boolean flag which is set if
4961f28255Scgd ** we have just entered a new quadrant.
5061f28255Scgd **
5161f28255Scgd ** This routine is used every time the Enterprise or the Klingons
5261f28255Scgd ** move.
53bf0917b6Sdholland **
54bf0917b6Sdholland ** f -- set if new quadrant
5561f28255Scgd */
5661f28255Scgd
57cb5fd834Sjsm static void sortkl(void);
58ef383c95Schristos
59ef383c95Schristos void
compkldist(int f)60bf0917b6Sdholland compkldist(int f)
6161f28255Scgd {
62ef383c95Schristos int i, dx, dy;
6361f28255Scgd double d;
6461f28255Scgd double temp;
6561f28255Scgd
6661f28255Scgd if (Etc.nkling == 0)
6761f28255Scgd return;
68*50b862e2Sdholland for (i = 0; i < Etc.nkling; i++) {
6961f28255Scgd /* compute distance to the Klingon */
7061f28255Scgd dx = Ship.sectx - Etc.klingon[i].x;
7161f28255Scgd dy = Ship.secty - Etc.klingon[i].y;
7261f28255Scgd d = dx * dx + dy * dy;
7361f28255Scgd d = sqrt(d);
7461f28255Scgd
7561f28255Scgd /* compute average of new and old distances to Klingon */
76*50b862e2Sdholland if (!f) {
7761f28255Scgd temp = Etc.klingon[i].dist;
7861f28255Scgd Etc.klingon[i].avgdist = 0.5 * (temp + d);
79*50b862e2Sdholland } else {
8061f28255Scgd /* new quadrant: average is current */
8161f28255Scgd Etc.klingon[i].avgdist = d;
8261f28255Scgd }
8361f28255Scgd Etc.klingon[i].dist = d;
8461f28255Scgd }
8561f28255Scgd
8661f28255Scgd /* leave them sorted */
8761f28255Scgd sortkl();
8861f28255Scgd }
8961f28255Scgd
9061f28255Scgd
9161f28255Scgd /*
9261f28255Scgd ** sort klingons
9361f28255Scgd **
9461f28255Scgd ** bubble sort on ascending distance
9561f28255Scgd */
9661f28255Scgd
97ef383c95Schristos static void
sortkl(void)98bf0917b6Sdholland sortkl(void)
9961f28255Scgd {
10061f28255Scgd struct kling t;
101ef383c95Schristos int f, i, m;
10261f28255Scgd
10361f28255Scgd m = Etc.nkling - 1;
10461f28255Scgd f = 1;
105*50b862e2Sdholland while (f) {
10661f28255Scgd f = 0;
107*50b862e2Sdholland for (i = 0; i < m; i++) {
108*50b862e2Sdholland if (Etc.klingon[i].dist > Etc.klingon[i+1].dist) {
109ef383c95Schristos t = Etc.klingon[i];
110ef383c95Schristos Etc.klingon[i] = Etc.klingon[i+1];
111ef383c95Schristos Etc.klingon[i+1] = t;
11261f28255Scgd f = 1;
11361f28255Scgd }
11461f28255Scgd }
11561f28255Scgd }
116*50b862e2Sdholland }
117