BSD 4_3_Reno release
[unix-history] / usr / src / games / trek / klmove.c
CommitLineData
9758240b
KM
1/*
2 * Copyright (c) 1980 Regents of the University of California.
e9fb6bea
KB
3 * All rights reserved.
4 *
1c15e888
C
5 * Redistribution and use in source and binary forms are permitted provided
6 * that: (1) source distributions retain this entire copyright notice and
7 * comment, and (2) distributions including binaries display the following
8 * acknowledgement: ``This product includes software developed by the
9 * University of California, Berkeley and its contributors'' in the
10 * documentation or other materials provided with the distribution and in
11 * all advertising materials mentioning features or use of this software.
12 * Neither the name of the University nor the names of its contributors may
13 * be used to endorse or promote products derived from this software without
14 * specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
9758240b
KM
18 */
19
b8768ee6 20#ifndef lint
1c15e888 21static char sccsid[] = "@(#)klmove.c 5.4 (Berkeley) 6/1/90";
e9fb6bea 22#endif /* not lint */
b8768ee6
KM
23
24# include "trek.h"
25
26/*
27** Move Klingons Around
28**
29** This is a largely incomprehensible block of code that moves
30** Klingons around in a quadrant. It was written in a very
31** "program as you go" fashion, and is a prime candidate for
32** rewriting.
33**
34** The flag `fl' is zero before an attack, one after an attack,
35** and two if you are leaving a quadrant. This serves to
36** change the probability and distance that it moves.
37**
38** Basically, what it will try to do is to move a certain number
39** of steps either toward you or away from you. It will avoid
40** stars whenever possible. Nextx and nexty are the next
41** sector to move to on a per-Klingon basis; they are roughly
42** equivalent to Ship.sectx and Ship.secty for the starship. Lookx and
43** looky are the sector that you are going to look at to see
44** if you can move their. Dx and dy are the increment. Fudgex
45** and fudgey are the things you change around to change your
46** course around stars.
47*/
48
49klmove(fl)
50int fl;
51{
52 int n;
53 register struct kling *k;
54 double dx, dy;
55 int nextx, nexty;
56 register int lookx, looky;
57 int motion;
58 int fudgex, fudgey;
59 int qx, qy;
60 double bigger;
61 int i;
62
63# ifdef xTRACE
64 if (Trace)
65 printf("klmove: fl = %d, Etc.nkling = %d\n", fl, Etc.nkling);
66# endif
67 for (n = 0; n < Etc.nkling; k && n++)
68 {
69 k = &Etc.klingon[n];
70 i = 100;
71 if (fl)
72 i = 100.0 * k->power / Param.klingpwr;
73 if (ranf(i) >= Param.moveprob[2 * Move.newquad + fl])
74 continue;
75 /* compute distance to move */
76 motion = ranf(75) - 25;
06d69904 77 motion *= k->avgdist * Param.movefac[2 * Move.newquad + fl];
b8768ee6
KM
78 /* compute direction */
79 dx = Ship.sectx - k->x + ranf(3) - 1;
80 dy = Ship.secty - k->y + ranf(3) - 1;
81 bigger = dx;
82 if (dy > bigger)
83 bigger = dy;
84 if (bigger == 0.0)
85 bigger = 1.0;
86 dx = dx / bigger + 0.5;
87 dy = dy / bigger + 0.5;
88 if (motion < 0)
89 {
90 motion = -motion;
91 dx = -dx;
92 dy = -dy;
93 }
94 fudgex = fudgey = 1;
95 /* try to move the klingon */
96 nextx = k->x;
97 nexty = k->y;
98 for (; motion > 0; motion--)
99 {
100 lookx = nextx + dx;
101 looky = nexty + dy;
102 if (lookx < 0 || lookx >= NSECTS || looky < 0 || looky >= NSECTS)
103 {
104 /* new quadrant */
105 qx = Ship.quadx;
106 qy = Ship.quady;
107 if (lookx < 0)
06d69904 108 qx -= 1;
b8768ee6
KM
109 else
110 if (lookx >= NSECTS)
06d69904 111 qx += 1;
b8768ee6 112 if (looky < 0)
06d69904 113 qy -= 1;
b8768ee6
KM
114 else
115 if (looky >= NSECTS)
06d69904 116 qy += 1;
b8768ee6
KM
117 if (qx < 0 || qx >= NQUADS || qy < 0 || qy >= NQUADS ||
118 Quad[qx][qy].stars < 0 || Quad[qx][qy].klings > MAXKLQUAD - 1)
119 break;
120 if (!damaged(SRSCAN))
121 {
122 printf("Klingon at %d,%d escapes to quadrant %d,%d\n",
123 k->x, k->y, qx, qy);
124 motion = Quad[qx][qy].scanned;
125 if (motion >= 0 && motion < 1000)
06d69904 126 Quad[qx][qy].scanned += 100;
b8768ee6
KM
127 motion = Quad[Ship.quadx][Ship.quady].scanned;
128 if (motion >= 0 && motion < 1000)
06d69904 129 Quad[Ship.quadx][Ship.quady].scanned -= 100;
b8768ee6
KM
130 }
131 Sect[k->x][k->y] = EMPTY;
06d69904
KL
132 Quad[qx][qy].klings += 1;
133 Etc.nkling -= 1;
b8768ee6 134 bmove(&Etc.klingon[Etc.nkling], k, sizeof *k);
06d69904 135 Quad[Ship.quadx][Ship.quady].klings -= 1;
b8768ee6
KM
136 k = 0;
137 break;
138 }
139 if (Sect[lookx][looky] != EMPTY)
140 {
141 lookx = nextx + fudgex;
142 if (lookx < 0 || lookx >= NSECTS)
143 lookx = nextx + dx;
144 if (Sect[lookx][looky] != EMPTY)
145 {
146 fudgex = -fudgex;
147 looky = nexty + fudgey;
148 if (looky < 0 || looky >= NSECTS || Sect[lookx][looky] != EMPTY)
149 {
150 fudgey = -fudgey;
151 break;
152 }
153 }
154 }
155 nextx = lookx;
156 nexty = looky;
157 }
158 if (k && (k->x != nextx || k->y != nexty))
159 {
160 if (!damaged(SRSCAN))
161 printf("Klingon at %d,%d moves to %d,%d\n",
162 k->x, k->y, nextx, nexty);
163 Sect[k->x][k->y] = EMPTY;
164 Sect[k->x = nextx][k->y = nexty] = KLINGON;
165 }
166 }
167 compkldist(0);
168}