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