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