|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1980 Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted provided ! 6: * that: (1) source distributions retain this entire copyright notice and ! 7: * comment, and (2) distributions including binaries display the following ! 8: * acknowledgement: ``This product includes software developed by the ! 9: * University of California, Berkeley and its contributors'' in the ! 10: * documentation or other materials provided with the distribution and in ! 11: * all advertising materials mentioning features or use of this software. ! 12: * Neither the name of the University nor the names of its contributors may ! 13: * be used to endorse or promote products derived from this software without ! 14: * specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED ! 16: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF ! 17: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #ifndef lint ! 21: static char sccsid[] = "@(#)compkl.c 5.4 (Berkeley) 6/1/90"; ! 22: #endif /* not lint */ ! 23: ! 24: # include "trek.h" ! 25: ! 26: /* ! 27: ** compute klingon distances ! 28: ** ! 29: ** The klingon list has the distances for all klingons recomputed ! 30: ** and sorted. The parameter is a Boolean flag which is set if ! 31: ** we have just entered a new quadrant. ! 32: ** ! 33: ** This routine is used every time the Enterprise or the Klingons ! 34: ** move. ! 35: */ ! 36: ! 37: compkldist(f) ! 38: int f; /* set if new quadrant */ ! 39: { ! 40: register int i, dx, dy; ! 41: double d; ! 42: double temp; ! 43: ! 44: if (Etc.nkling == 0) ! 45: return; ! 46: for (i = 0; i < Etc.nkling; i++) ! 47: { ! 48: /* compute distance to the Klingon */ ! 49: dx = Ship.sectx - Etc.klingon[i].x; ! 50: dy = Ship.secty - Etc.klingon[i].y; ! 51: d = dx * dx + dy * dy; ! 52: d = sqrt(d); ! 53: ! 54: /* compute average of new and old distances to Klingon */ ! 55: if (!f) ! 56: { ! 57: temp = Etc.klingon[i].dist; ! 58: Etc.klingon[i].avgdist = 0.5 * (temp + d); ! 59: } ! 60: else ! 61: { ! 62: /* new quadrant: average is current */ ! 63: Etc.klingon[i].avgdist = d; ! 64: } ! 65: Etc.klingon[i].dist = d; ! 66: } ! 67: ! 68: /* leave them sorted */ ! 69: sortkl(); ! 70: } ! 71: ! 72: ! 73: /* ! 74: ** sort klingons ! 75: ** ! 76: ** bubble sort on ascending distance ! 77: */ ! 78: ! 79: sortkl() ! 80: { ! 81: struct kling t; ! 82: register int f, i, m; ! 83: ! 84: m = Etc.nkling - 1; ! 85: f = 1; ! 86: while (f) ! 87: { ! 88: f = 0; ! 89: for (i = 0; i < m; i++) ! 90: if (Etc.klingon[i].dist > Etc.klingon[i+1].dist) ! 91: { ! 92: bmove(&Etc.klingon[i], &t, sizeof t); ! 93: bmove(&Etc.klingon[i+1], &Etc.klingon[i], sizeof t); ! 94: bmove(&t, &Etc.klingon[i+1], sizeof t); ! 95: f = 1; ! 96: } ! 97: } ! 98: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.