Annotation of 42BSD/games/trek/compkl.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.