1 /*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char sccsid[] = "@(#)klmove.c 8.1 (Berkeley) 05/31/93";
10 #endif /* not lint */
11
12 # include "trek.h"
13
14 /*
15 ** Move Klingons Around
16 **
17 ** This is a largely incomprehensible block of code that moves
18 ** Klingons around in a quadrant. It was written in a very
19 ** "program as you go" fashion, and is a prime candidate for
20 ** rewriting.
21 **
22 ** The flag `fl' is zero before an attack, one after an attack,
23 ** and two if you are leaving a quadrant. This serves to
24 ** change the probability and distance that it moves.
25 **
26 ** Basically, what it will try to do is to move a certain number
27 ** of steps either toward you or away from you. It will avoid
28 ** stars whenever possible. Nextx and nexty are the next
29 ** sector to move to on a per-Klingon basis; they are roughly
30 ** equivalent to Ship.sectx and Ship.secty for the starship. Lookx and
31 ** looky are the sector that you are going to look at to see
32 ** if you can move their. Dx and dy are the increment. Fudgex
33 ** and fudgey are the things you change around to change your
34 ** course around stars.
35 */
36
klmove(fl)37 klmove(fl)
38 int fl;
39 {
40 int n;
41 register struct kling *k;
42 double dx, dy;
43 int nextx, nexty;
44 register int lookx, looky;
45 int motion;
46 int fudgex, fudgey;
47 int qx, qy;
48 double bigger;
49 int i;
50
51 # ifdef xTRACE
52 if (Trace)
53 printf("klmove: fl = %d, Etc.nkling = %d\n", fl, Etc.nkling);
54 # endif
55 for (n = 0; n < Etc.nkling; k && n++)
56 {
57 k = &Etc.klingon[n];
58 i = 100;
59 if (fl)
60 i = 100.0 * k->power / Param.klingpwr;
61 if (ranf(i) >= Param.moveprob[2 * Move.newquad + fl])
62 continue;
63 /* compute distance to move */
64 motion = ranf(75) - 25;
65 motion *= k->avgdist * Param.movefac[2 * Move.newquad + fl];
66 /* compute direction */
67 dx = Ship.sectx - k->x + ranf(3) - 1;
68 dy = Ship.secty - k->y + ranf(3) - 1;
69 bigger = dx;
70 if (dy > bigger)
71 bigger = dy;
72 if (bigger == 0.0)
73 bigger = 1.0;
74 dx = dx / bigger + 0.5;
75 dy = dy / bigger + 0.5;
76 if (motion < 0)
77 {
78 motion = -motion;
79 dx = -dx;
80 dy = -dy;
81 }
82 fudgex = fudgey = 1;
83 /* try to move the klingon */
84 nextx = k->x;
85 nexty = k->y;
86 for (; motion > 0; motion--)
87 {
88 lookx = nextx + dx;
89 looky = nexty + dy;
90 if (lookx < 0 || lookx >= NSECTS || looky < 0 || looky >= NSECTS)
91 {
92 /* new quadrant */
93 qx = Ship.quadx;
94 qy = Ship.quady;
95 if (lookx < 0)
96 qx -= 1;
97 else
98 if (lookx >= NSECTS)
99 qx += 1;
100 if (looky < 0)
101 qy -= 1;
102 else
103 if (looky >= NSECTS)
104 qy += 1;
105 if (qx < 0 || qx >= NQUADS || qy < 0 || qy >= NQUADS ||
106 Quad[qx][qy].stars < 0 || Quad[qx][qy].klings > MAXKLQUAD - 1)
107 break;
108 if (!damaged(SRSCAN))
109 {
110 printf("Klingon at %d,%d escapes to quadrant %d,%d\n",
111 k->x, k->y, qx, qy);
112 motion = Quad[qx][qy].scanned;
113 if (motion >= 0 && motion < 1000)
114 Quad[qx][qy].scanned += 100;
115 motion = Quad[Ship.quadx][Ship.quady].scanned;
116 if (motion >= 0 && motion < 1000)
117 Quad[Ship.quadx][Ship.quady].scanned -= 100;
118 }
119 Sect[k->x][k->y] = EMPTY;
120 Quad[qx][qy].klings += 1;
121 Etc.nkling -= 1;
122 bmove(&Etc.klingon[Etc.nkling], k, sizeof *k);
123 Quad[Ship.quadx][Ship.quady].klings -= 1;
124 k = 0;
125 break;
126 }
127 if (Sect[lookx][looky] != EMPTY)
128 {
129 lookx = nextx + fudgex;
130 if (lookx < 0 || lookx >= NSECTS)
131 lookx = nextx + dx;
132 if (Sect[lookx][looky] != EMPTY)
133 {
134 fudgex = -fudgex;
135 looky = nexty + fudgey;
136 if (looky < 0 || looky >= NSECTS || Sect[lookx][looky] != EMPTY)
137 {
138 fudgey = -fudgey;
139 break;
140 }
141 }
142 }
143 nextx = lookx;
144 nexty = looky;
145 }
146 if (k && (k->x != nextx || k->y != nexty))
147 {
148 if (!damaged(SRSCAN))
149 printf("Klingon at %d,%d moves to %d,%d\n",
150 k->x, k->y, nextx, nexty);
151 Sect[k->x][k->y] = EMPTY;
152 Sect[k->x = nextx][k->y = nexty] = KLINGON;
153 }
154 }
155 compkldist(0);
156 }
157