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