new template
[unix-history] / usr / src / games / trek / compkl.c
CommitLineData
b6f0a7e4
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
f67257fc 7#ifndef lint
b6f0a7e4 8static char sccsid[] = "@(#)compkl.c 5.1 (Berkeley) %G%";
f67257fc
KM
9#endif not lint
10
11# include "trek.h"
12
13/*
14** compute klingon distances
15**
16** The klingon list has the distances for all klingons recomputed
17** and sorted. The parameter is a Boolean flag which is set if
18** we have just entered a new quadrant.
19**
20** This routine is used every time the Enterprise or the Klingons
21** move.
22*/
23
24compkldist(f)
25int f; /* set if new quadrant */
26{
27 register int i, dx, dy;
28 double d;
29 double temp;
30
31 if (Etc.nkling == 0)
32 return;
33 for (i = 0; i < Etc.nkling; i++)
34 {
35 /* compute distance to the Klingon */
36 dx = Ship.sectx - Etc.klingon[i].x;
37 dy = Ship.secty - Etc.klingon[i].y;
38 d = dx * dx + dy * dy;
39 d = sqrt(d);
40
41 /* compute average of new and old distances to Klingon */
42 if (!f)
43 {
44 temp = Etc.klingon[i].dist;
45 Etc.klingon[i].avgdist = 0.5 * (temp + d);
46 }
47 else
48 {
49 /* new quadrant: average is current */
50 Etc.klingon[i].avgdist = d;
51 }
52 Etc.klingon[i].dist = d;
53 }
54
55 /* leave them sorted */
56 sortkl();
57}
58
59
60/*
61** sort klingons
62**
63** bubble sort on ascending distance
64*/
65
66sortkl()
67{
68 struct kling t;
69 register int f, i, m;
70
71 m = Etc.nkling - 1;
72 f = 1;
73 while (f)
74 {
75 f = 0;
76 for (i = 0; i < m; i++)
77 if (Etc.klingon[i].dist > Etc.klingon[i+1].dist)
78 {
79 bmove(&Etc.klingon[i], &t, sizeof t);
80 bmove(&Etc.klingon[i+1], &Etc.klingon[i], sizeof t);
81 bmove(&t, &Etc.klingon[i+1], sizeof t);
82 f = 1;
83 }
84 }
f67257fc 85}