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