]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - robots/auto.c
sprinkle some more const
[bsdgames-darwin.git] / robots / auto.c
1 /* $NetBSD: auto.c,v 1.8 2008/04/28 20:22:54 martin Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Automatic move.
34 * intelligent ?
35 * Algo :
36 * IF scrapheaps don't exist THEN
37 * IF not in danger THEN
38 * stay at current position
39 * ELSE
40 * move away from the closest robot
41 * FI
42 * ELSE
43 * find closest heap
44 * find closest robot
45 * IF scrapheap is adjacent THEN
46 * move behind the scrapheap
47 * ELSE
48 * take the move that takes you away from the
49 * robots and closest to the heap
50 * FI
51 * FI
52 */
53 #include "robots.h"
54
55 #define ABS(a) (((a)>0)?(a):-(a))
56 #define MIN(a,b) (((a)>(b))?(b):(a))
57 #define MAX(a,b) (((a)<(b))?(b):(a))
58
59 #define CONSDEBUG(a)
60
61 static int distance(int, int, int, int);
62 static int xinc(int);
63 static int yinc(int);
64 static const char *find_moves(void);
65 static COORD *closest_robot(int *);
66 static COORD *closest_heap(int *);
67 static char move_towards(int, int);
68 static char move_away(COORD *);
69 static char move_between(COORD *, COORD *);
70 static int between(COORD *, COORD *);
71
72 /* distance():
73 * return "move" number distance of the two coordinates
74 */
75 static int
76 distance(x1, y1, x2, y2)
77 int x1, y1, x2, y2;
78 {
79 return MAX(ABS(ABS(x1) - ABS(x2)), ABS(ABS(y1) - ABS(y2)));
80 } /* end distance */
81
82 /* xinc():
83 * Return x coordinate moves
84 */
85 static int
86 xinc(dir)
87 int dir;
88 {
89 switch(dir) {
90 case 'b':
91 case 'h':
92 case 'y':
93 return -1;
94 case 'l':
95 case 'n':
96 case 'u':
97 return 1;
98 case 'j':
99 case 'k':
100 default:
101 return 0;
102 }
103 }
104
105 /* yinc():
106 * Return y coordinate moves
107 */
108 static int
109 yinc(dir)
110 int dir;
111 {
112 switch(dir) {
113 case 'k':
114 case 'u':
115 case 'y':
116 return -1;
117 case 'b':
118 case 'j':
119 case 'n':
120 return 1;
121 case 'h':
122 case 'l':
123 default:
124 return 0;
125 }
126 }
127
128 /* find_moves():
129 * Find possible moves
130 */
131 static const char *
132 find_moves()
133 {
134 int x, y;
135 COORD test;
136 const char *m;
137 char *a;
138 static const char moves[] = ".hjklyubn";
139 static char ans[sizeof moves];
140 a = ans;
141
142 for(m = moves; *m; m++) {
143 test.x = My_pos.x + xinc(*m);
144 test.y = My_pos.y + yinc(*m);
145 move(test.y, test.x);
146 switch(winch(stdscr)) {
147 case ' ':
148 case PLAYER:
149 for(x = test.x - 1; x <= test.x + 1; x++) {
150 for(y = test.y - 1; y <= test.y + 1; y++) {
151 move(y, x);
152 if(winch(stdscr) == ROBOT)
153 goto bad;
154 }
155 }
156 *a++ = *m;
157 }
158 bad:;
159 }
160 *a = 0;
161 if(ans[0])
162 return ans;
163 else
164 return "t";
165 }
166
167 /* closest_robot():
168 * return the robot closest to us
169 * and put in dist its distance
170 */
171 static COORD *
172 closest_robot(dist)
173 int *dist;
174 {
175 COORD *rob, *end, *minrob = NULL;
176 int tdist, mindist;
177
178 mindist = 1000000;
179 end = &Robots[MAXROBOTS];
180 for (rob = Robots; rob < end; rob++) {
181 tdist = distance(My_pos.x, My_pos.y, rob->x, rob->y);
182 if (tdist < mindist) {
183 minrob = rob;
184 mindist = tdist;
185 }
186 }
187 *dist = mindist;
188 return minrob;
189 } /* end closest_robot */
190
191 /* closest_heap():
192 * return the heap closest to us
193 * and put in dist its distance
194 */
195 static COORD *
196 closest_heap(dist)
197 int *dist;
198 {
199 COORD *hp, *end, *minhp = NULL;
200 int mindist, tdist;
201
202 mindist = 1000000;
203 end = &Scrap[MAXROBOTS];
204 for (hp = Scrap; hp < end; hp++) {
205 if (hp->x == 0 && hp->y == 0)
206 break;
207 tdist = distance(My_pos.x, My_pos.y, hp->x, hp->y);
208 if (tdist < mindist) {
209 minhp = hp;
210 mindist = tdist;
211 }
212 }
213 *dist = mindist;
214 return minhp;
215 } /* end closest_heap */
216
217 /* move_towards():
218 * move as close to the given direction as possible
219 */
220 static char
221 move_towards(dx, dy)
222 int dx, dy;
223 {
224 char ok_moves[10], best_move;
225 char *ptr;
226 int move_judge, cur_judge, mvx, mvy;
227
228 (void)strcpy(ok_moves, find_moves());
229 best_move = ok_moves[0];
230 if (best_move != 't') {
231 mvx = xinc(best_move);
232 mvy = yinc(best_move);
233 move_judge = ABS(mvx - dx) + ABS(mvy - dy);
234 for (ptr = &ok_moves[1]; *ptr != '\0'; ptr++) {
235 mvx = xinc(*ptr);
236 mvy = yinc(*ptr);
237 cur_judge = ABS(mvx - dx) + ABS(mvy - dy);
238 if (cur_judge < move_judge) {
239 move_judge = cur_judge;
240 best_move = *ptr;
241 }
242 }
243 }
244 return best_move;
245 } /* end move_towards */
246
247 /* move_away():
248 * move away form the robot given
249 */
250 static char
251 move_away(rob)
252 COORD *rob;
253 {
254 int dx, dy;
255
256 dx = sign(My_pos.x - rob->x);
257 dy = sign(My_pos.y - rob->y);
258 return move_towards(dx, dy);
259 } /* end move_away */
260
261
262 /* move_between():
263 * move the closest heap between us and the closest robot
264 */
265 static char
266 move_between(rob, hp)
267 COORD *rob;
268 COORD *hp;
269 {
270 int dx, dy;
271 float slope, cons;
272
273 /* equation of the line between us and the closest robot */
274 if (My_pos.x == rob->x) {
275 /*
276 * me and the robot are aligned in x
277 * change my x so I get closer to the heap
278 * and my y far from the robot
279 */
280 dx = - sign(My_pos.x - hp->x);
281 dy = sign(My_pos.y - rob->y);
282 CONSDEBUG(("aligned in x"));
283 }
284 else if (My_pos.y == rob->y) {
285 /*
286 * me and the robot are aligned in y
287 * change my y so I get closer to the heap
288 * and my x far from the robot
289 */
290 dx = sign(My_pos.x - rob->x);
291 dy = -sign(My_pos.y - hp->y);
292 CONSDEBUG(("aligned in y"));
293 }
294 else {
295 CONSDEBUG(("no aligned"));
296 slope = (My_pos.y - rob->y) / (My_pos.x - rob->x);
297 cons = slope * rob->y;
298 if (ABS(My_pos.x - rob->x) > ABS(My_pos.y - rob->y)) {
299 /*
300 * we are closest to the robot in x
301 * move away from the robot in x and
302 * close to the scrap in y
303 */
304 dx = sign(My_pos.x - rob->x);
305 dy = sign(((slope * ((float) hp->x)) + cons) -
306 ((float) hp->y));
307 }
308 else {
309 dx = sign(((slope * ((float) hp->x)) + cons) -
310 ((float) hp->y));
311 dy = sign(My_pos.y - rob->y);
312 }
313 }
314 CONSDEBUG(("me (%d,%d) robot(%d,%d) heap(%d,%d) delta(%d,%d)",
315 My_pos.x, My_pos.y, rob->x, rob->y, hp->x, hp->y, dx, dy));
316 return move_towards(dx, dy);
317 } /* end move_between */
318
319 /* between():
320 * Return true if the heap is between us and the robot
321 */
322 int
323 between(rob, hp)
324 COORD *rob;
325 COORD *hp;
326 {
327 /* I = @ */
328 if (hp->x > rob->x && My_pos.x < rob->x)
329 return 0;
330 /* @ = I */
331 if (hp->x < rob->x && My_pos.x > rob->x)
332 return 0;
333 /* @ */
334 /* = */
335 /* I */
336 if (hp->y < rob->y && My_pos.y > rob->y)
337 return 0;
338 /* I */
339 /* = */
340 /* @ */
341 if (hp->y > rob->y && My_pos.y < rob->y)
342 return 0;
343 return 1;
344 } /* end between */
345
346 /* automove():
347 * find and do the best move if flag
348 * else get the first move;
349 */
350 char
351 automove()
352 {
353 #if 0
354 return find_moves()[0];
355 #else
356 COORD *robot_close;
357 COORD *heap_close;
358 int robot_dist, robot_heap, heap_dist;
359
360 robot_close = closest_robot(&robot_dist);
361 if (robot_dist > 1)
362 return('.');
363 if (!Num_scrap)
364 /* no scrap heaps just run away */
365 return move_away(robot_close);
366
367 heap_close = closest_heap(&heap_dist);
368 robot_heap = distance(robot_close->x, robot_close->y,
369 heap_close->x, heap_close->y);
370 if (robot_heap <= heap_dist && !between(robot_close, heap_close)) {
371 /*
372 * robot is closest to us from the heap. Run away!
373 */
374 return move_away(robot_close);
375 }
376
377 return move_between(robot_close, heap_close);
378 #endif
379 } /* end automove */