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