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