klmove.c revision 1.2 1 /*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)klmove.c 5.4 (Berkeley) 6/1/90";*/
36 static char rcsid[] = "$Id: klmove.c,v 1.2 1993/08/01 18:50:23 mycroft Exp $";
37 #endif /* not lint */
38
39 # include "trek.h"
40
41 /*
42 ** Move Klingons Around
43 **
44 ** This is a largely incomprehensible block of code that moves
45 ** Klingons around in a quadrant. It was written in a very
46 ** "program as you go" fashion, and is a prime candidate for
47 ** rewriting.
48 **
49 ** The flag `fl' is zero before an attack, one after an attack,
50 ** and two if you are leaving a quadrant. This serves to
51 ** change the probability and distance that it moves.
52 **
53 ** Basically, what it will try to do is to move a certain number
54 ** of steps either toward you or away from you. It will avoid
55 ** stars whenever possible. Nextx and nexty are the next
56 ** sector to move to on a per-Klingon basis; they are roughly
57 ** equivalent to Ship.sectx and Ship.secty for the starship. Lookx and
58 ** looky are the sector that you are going to look at to see
59 ** if you can move their. Dx and dy are the increment. Fudgex
60 ** and fudgey are the things you change around to change your
61 ** course around stars.
62 */
63
64 klmove(fl)
65 int fl;
66 {
67 int n;
68 register struct kling *k;
69 double dx, dy;
70 int nextx, nexty;
71 register int lookx, looky;
72 int motion;
73 int fudgex, fudgey;
74 int qx, qy;
75 double bigger;
76 int i;
77
78 # ifdef xTRACE
79 if (Trace)
80 printf("klmove: fl = %d, Etc.nkling = %d\n", fl, Etc.nkling);
81 # endif
82 for (n = 0; n < Etc.nkling; k && n++)
83 {
84 k = &Etc.klingon[n];
85 i = 100;
86 if (fl)
87 i = 100.0 * k->power / Param.klingpwr;
88 if (ranf(i) >= Param.moveprob[2 * Move.newquad + fl])
89 continue;
90 /* compute distance to move */
91 motion = ranf(75) - 25;
92 motion *= k->avgdist * Param.movefac[2 * Move.newquad + fl];
93 /* compute direction */
94 dx = Ship.sectx - k->x + ranf(3) - 1;
95 dy = Ship.secty - k->y + ranf(3) - 1;
96 bigger = dx;
97 if (dy > bigger)
98 bigger = dy;
99 if (bigger == 0.0)
100 bigger = 1.0;
101 dx = dx / bigger + 0.5;
102 dy = dy / bigger + 0.5;
103 if (motion < 0)
104 {
105 motion = -motion;
106 dx = -dx;
107 dy = -dy;
108 }
109 fudgex = fudgey = 1;
110 /* try to move the klingon */
111 nextx = k->x;
112 nexty = k->y;
113 for (; motion > 0; motion--)
114 {
115 lookx = nextx + dx;
116 looky = nexty + dy;
117 if (lookx < 0 || lookx >= NSECTS || looky < 0 || looky >= NSECTS)
118 {
119 /* new quadrant */
120 qx = Ship.quadx;
121 qy = Ship.quady;
122 if (lookx < 0)
123 qx -= 1;
124 else
125 if (lookx >= NSECTS)
126 qx += 1;
127 if (looky < 0)
128 qy -= 1;
129 else
130 if (looky >= NSECTS)
131 qy += 1;
132 if (qx < 0 || qx >= NQUADS || qy < 0 || qy >= NQUADS ||
133 Quad[qx][qy].stars < 0 || Quad[qx][qy].klings > MAXKLQUAD - 1)
134 break;
135 if (!damaged(SRSCAN))
136 {
137 printf("Klingon at %d,%d escapes to quadrant %d,%d\n",
138 k->x, k->y, qx, qy);
139 motion = Quad[qx][qy].scanned;
140 if (motion >= 0 && motion < 1000)
141 Quad[qx][qy].scanned += 100;
142 motion = Quad[Ship.quadx][Ship.quady].scanned;
143 if (motion >= 0 && motion < 1000)
144 Quad[Ship.quadx][Ship.quady].scanned -= 100;
145 }
146 Sect[k->x][k->y] = EMPTY;
147 Quad[qx][qy].klings += 1;
148 Etc.nkling -= 1;
149 bmove(&Etc.klingon[Etc.nkling], k, sizeof *k);
150 Quad[Ship.quadx][Ship.quady].klings -= 1;
151 k = 0;
152 break;
153 }
154 if (Sect[lookx][looky] != EMPTY)
155 {
156 lookx = nextx + fudgex;
157 if (lookx < 0 || lookx >= NSECTS)
158 lookx = nextx + dx;
159 if (Sect[lookx][looky] != EMPTY)
160 {
161 fudgex = -fudgex;
162 looky = nexty + fudgey;
163 if (looky < 0 || looky >= NSECTS || Sect[lookx][looky] != EMPTY)
164 {
165 fudgey = -fudgey;
166 break;
167 }
168 }
169 }
170 nextx = lookx;
171 nexty = looky;
172 }
173 if (k && (k->x != nextx || k->y != nexty))
174 {
175 if (!damaged(SRSCAN))
176 printf("Klingon at %d,%d moves to %d,%d\n",
177 k->x, k->y, nextx, nexty);
178 Sect[k->x][k->y] = EMPTY;
179 Sect[k->x = nextx][k->y = nexty] = KLINGON;
180 }
181 }
182 compkldist(0);
183 }
184