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