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