Home | History | Annotate | Line # | Download | only in robots
auto.c revision 1.7
      1  1.7  christos /*	$NetBSD: auto.c,v 1.7 2004/08/27 09:06:25 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.7  christos  *				stay at current position
     46  1.7  christos  *		 	ELSE
     47  1.7  christos  *				move away from the closest robot
     48  1.1  christos  *			FI
     49  1.1  christos  *		ELSE
     50  1.7  christos  *			find closest heap
     51  1.7  christos  *			find closest robot
     52  1.7  christos  *			IF scrapheap is adjacent THEN
     53  1.7  christos  *				move behind the scrapheap
     54  1.1  christos  *			ELSE
     55  1.1  christos  *				take the move that takes you away from the
     56  1.1  christos  *				robots and closest to the heap
     57  1.1  christos  *			FI
     58  1.1  christos  *		FI
     59  1.1  christos  */
     60  1.1  christos #include "robots.h"
     61  1.1  christos 
     62  1.1  christos #define ABS(a) (((a)>0)?(a):-(a))
     63  1.1  christos #define MIN(a,b) (((a)>(b))?(b):(a))
     64  1.1  christos #define MAX(a,b) (((a)<(b))?(b):(a))
     65  1.1  christos 
     66  1.1  christos #define CONSDEBUG(a)
     67  1.1  christos 
     68  1.6       jsm static int distance(int, int, int, int);
     69  1.6       jsm static int xinc(int);
     70  1.6       jsm static int yinc(int);
     71  1.6       jsm static const char *find_moves(void);
     72  1.6       jsm static COORD *closest_robot(int *);
     73  1.6       jsm static COORD *closest_heap(int *);
     74  1.6       jsm static char move_towards(int, int);
     75  1.6       jsm static char move_away(COORD *);
     76  1.6       jsm static char move_between(COORD *, COORD *);
     77  1.6       jsm static int between(COORD *, COORD *);
     78  1.1  christos 
     79  1.1  christos /* distance():
     80  1.1  christos  * 	return "move" number distance of the two coordinates
     81  1.1  christos  */
     82  1.1  christos static int
     83  1.1  christos distance(x1, y1, x2, y2)
     84  1.1  christos 	int x1, y1, x2, y2;
     85  1.1  christos {
     86  1.1  christos 	return MAX(ABS(ABS(x1) - ABS(x2)), ABS(ABS(y1) - ABS(y2)));
     87  1.1  christos } /* end distance */
     88  1.1  christos 
     89  1.1  christos /* xinc():
     90  1.1  christos  *	Return x coordinate moves
     91  1.1  christos  */
     92  1.1  christos static int
     93  1.1  christos xinc(dir)
     94  1.1  christos         int dir;
     95  1.1  christos {
     96  1.1  christos         switch(dir) {
     97  1.1  christos         case 'b':
     98  1.1  christos         case 'h':
     99  1.1  christos         case 'y':
    100  1.1  christos                 return -1;
    101  1.1  christos         case 'l':
    102  1.1  christos         case 'n':
    103  1.1  christos         case 'u':
    104  1.1  christos                 return 1;
    105  1.1  christos         case 'j':
    106  1.1  christos         case 'k':
    107  1.1  christos         default:
    108  1.1  christos                 return 0;
    109  1.1  christos         }
    110  1.1  christos }
    111  1.1  christos 
    112  1.1  christos /* yinc():
    113  1.1  christos  *	Return y coordinate moves
    114  1.1  christos  */
    115  1.1  christos static int
    116  1.1  christos yinc(dir)
    117  1.1  christos         int dir;
    118  1.1  christos {
    119  1.1  christos         switch(dir) {
    120  1.1  christos         case 'k':
    121  1.1  christos         case 'u':
    122  1.1  christos         case 'y':
    123  1.1  christos                 return -1;
    124  1.1  christos         case 'b':
    125  1.1  christos         case 'j':
    126  1.1  christos         case 'n':
    127  1.1  christos                 return 1;
    128  1.1  christos         case 'h':
    129  1.1  christos         case 'l':
    130  1.1  christos         default:
    131  1.1  christos                 return 0;
    132  1.1  christos         }
    133  1.1  christos }
    134  1.1  christos 
    135  1.1  christos /* find_moves():
    136  1.1  christos  *	Find possible moves
    137  1.1  christos  */
    138  1.4       jsm static const char *
    139  1.1  christos find_moves()
    140  1.1  christos {
    141  1.1  christos         int x, y;
    142  1.1  christos         COORD test;
    143  1.4       jsm         const char *m;
    144  1.4       jsm         char *a;
    145  1.4       jsm         static const char moves[] = ".hjklyubn";
    146  1.1  christos         static char ans[sizeof moves];
    147  1.1  christos         a = ans;
    148  1.1  christos 
    149  1.1  christos         for(m = moves; *m; m++) {
    150  1.1  christos                 test.x = My_pos.x + xinc(*m);
    151  1.1  christos                 test.y = My_pos.y + yinc(*m);
    152  1.1  christos                 move(test.y, test.x);
    153  1.1  christos                 switch(winch(stdscr)) {
    154  1.1  christos                 case ' ':
    155  1.1  christos                 case PLAYER:
    156  1.1  christos                         for(x = test.x - 1; x <= test.x + 1; x++) {
    157  1.1  christos                                 for(y = test.y - 1; y <= test.y + 1; y++) {
    158  1.1  christos                                         move(y, x);
    159  1.1  christos                                         if(winch(stdscr) == ROBOT)
    160  1.1  christos 						goto bad;
    161  1.1  christos                                 }
    162  1.1  christos                         }
    163  1.1  christos                         *a++ = *m;
    164  1.1  christos                 }
    165  1.1  christos         bad:;
    166  1.1  christos         }
    167  1.1  christos         *a = 0;
    168  1.1  christos         if(ans[0])
    169  1.4       jsm                 return ans;
    170  1.1  christos         else
    171  1.4       jsm                 return "t";
    172  1.1  christos }
    173  1.1  christos 
    174  1.1  christos /* closest_robot():
    175  1.1  christos  *	return the robot closest to us
    176  1.1  christos  *	and put in dist its distance
    177  1.1  christos  */
    178  1.1  christos static COORD *
    179  1.1  christos closest_robot(dist)
    180  1.1  christos 	int *dist;
    181  1.1  christos {
    182  1.3  christos 	COORD *rob, *end, *minrob = NULL;
    183  1.1  christos 	int tdist, mindist;
    184  1.1  christos 
    185  1.1  christos 	mindist = 1000000;
    186  1.1  christos 	end = &Robots[MAXROBOTS];
    187  1.1  christos 	for (rob = Robots; rob < end; rob++) {
    188  1.1  christos 		tdist = distance(My_pos.x, My_pos.y, rob->x, rob->y);
    189  1.1  christos 		if (tdist < mindist) {
    190  1.1  christos 			minrob = rob;
    191  1.1  christos 			mindist = tdist;
    192  1.1  christos 		}
    193  1.1  christos 	}
    194  1.1  christos 	*dist = mindist;
    195  1.1  christos 	return minrob;
    196  1.1  christos } /* end closest_robot */
    197  1.1  christos 
    198  1.1  christos /* closest_heap():
    199  1.1  christos  *	return the heap closest to us
    200  1.1  christos  *	and put in dist its distance
    201  1.1  christos  */
    202  1.1  christos static COORD *
    203  1.1  christos closest_heap(dist)
    204  1.1  christos 	int *dist;
    205  1.1  christos {
    206  1.3  christos 	COORD *hp, *end, *minhp = NULL;
    207  1.1  christos 	int mindist, tdist;
    208  1.1  christos 
    209  1.1  christos 	mindist = 1000000;
    210  1.1  christos 	end = &Scrap[MAXROBOTS];
    211  1.1  christos 	for (hp = Scrap; hp < end; hp++) {
    212  1.1  christos 		if (hp->x == 0 && hp->y == 0)
    213  1.1  christos 			break;
    214  1.1  christos 		tdist = distance(My_pos.x, My_pos.y, hp->x, hp->y);
    215  1.1  christos 		if (tdist < mindist) {
    216  1.1  christos 			minhp = hp;
    217  1.1  christos 			mindist = tdist;
    218  1.1  christos 		}
    219  1.1  christos 	}
    220  1.1  christos 	*dist = mindist;
    221  1.1  christos 	return minhp;
    222  1.1  christos } /* end closest_heap */
    223  1.1  christos 
    224  1.1  christos /* move_towards():
    225  1.1  christos  *	move as close to the given direction as possible
    226  1.1  christos  */
    227  1.1  christos static char
    228  1.1  christos move_towards(dx, dy)
    229  1.1  christos 	int dx, dy;
    230  1.1  christos {
    231  1.1  christos 	char ok_moves[10], best_move;
    232  1.1  christos 	char *ptr;
    233  1.1  christos 	int move_judge, cur_judge, mvx, mvy;
    234  1.1  christos 
    235  1.1  christos 	(void)strcpy(ok_moves, find_moves());
    236  1.1  christos 	best_move = ok_moves[0];
    237  1.5  christos 	if (best_move != 't') {
    238  1.1  christos 		mvx = xinc(best_move);
    239  1.1  christos 		mvy = yinc(best_move);
    240  1.1  christos 		move_judge = ABS(mvx - dx) + ABS(mvy - dy);
    241  1.1  christos 		for (ptr = &ok_moves[1]; *ptr != '\0'; ptr++) {
    242  1.1  christos 			mvx = xinc(*ptr);
    243  1.1  christos 			mvy = yinc(*ptr);
    244  1.1  christos 			cur_judge = ABS(mvx - dx) + ABS(mvy - dy);
    245  1.1  christos 			if (cur_judge < move_judge) {
    246  1.1  christos 				move_judge = cur_judge;
    247  1.1  christos 				best_move = *ptr;
    248  1.1  christos 			}
    249  1.1  christos 		}
    250  1.1  christos 	}
    251  1.1  christos 	return best_move;
    252  1.1  christos } /* end move_towards */
    253  1.1  christos 
    254  1.1  christos /* move_away():
    255  1.1  christos  *	move away form the robot given
    256  1.1  christos  */
    257  1.1  christos static char
    258  1.1  christos move_away(rob)
    259  1.1  christos 	COORD *rob;
    260  1.1  christos {
    261  1.1  christos 	int dx, dy;
    262  1.1  christos 
    263  1.1  christos 	dx = sign(My_pos.x - rob->x);
    264  1.1  christos 	dy = sign(My_pos.y - rob->y);
    265  1.1  christos 	return  move_towards(dx, dy);
    266  1.1  christos } /* end move_away */
    267  1.1  christos 
    268  1.1  christos 
    269  1.1  christos /* move_between():
    270  1.1  christos  *	move the closest heap between us and the closest robot
    271  1.1  christos  */
    272  1.1  christos static char
    273  1.1  christos move_between(rob, hp)
    274  1.1  christos 	COORD *rob;
    275  1.1  christos 	COORD *hp;
    276  1.1  christos {
    277  1.2  christos 	int dx, dy;
    278  1.1  christos 	float slope, cons;
    279  1.1  christos 
    280  1.1  christos 	/* equation of the line between us and the closest robot */
    281  1.1  christos 	if (My_pos.x == rob->x) {
    282  1.1  christos 		/*
    283  1.1  christos 		 * me and the robot are aligned in x
    284  1.1  christos 		 * change my x so I get closer to the heap
    285  1.1  christos 		 * and my y far from the robot
    286  1.1  christos 		 */
    287  1.1  christos 		dx = - sign(My_pos.x - hp->x);
    288  1.1  christos 		dy = sign(My_pos.y - rob->y);
    289  1.1  christos 		CONSDEBUG(("aligned in x"));
    290  1.1  christos 	}
    291  1.1  christos 	else if (My_pos.y == rob->y) {
    292  1.1  christos 		/*
    293  1.1  christos 		 * me and the robot are aligned in y
    294  1.1  christos 		 * change my y so I get closer to the heap
    295  1.1  christos 		 * and my x far from the robot
    296  1.1  christos 		 */
    297  1.1  christos 		dx = sign(My_pos.x - rob->x);
    298  1.1  christos 		dy = -sign(My_pos.y - hp->y);
    299  1.1  christos 		CONSDEBUG(("aligned in y"));
    300  1.1  christos 	}
    301  1.1  christos 	else {
    302  1.1  christos 		CONSDEBUG(("no aligned"));
    303  1.1  christos 		slope = (My_pos.y - rob->y) / (My_pos.x - rob->x);
    304  1.1  christos 		cons = slope * rob->y;
    305  1.1  christos 		if (ABS(My_pos.x - rob->x) > ABS(My_pos.y - rob->y)) {
    306  1.1  christos 			/*
    307  1.1  christos 			 * we are closest to the robot in x
    308  1.1  christos 			 * move away from the robot in x and
    309  1.1  christos 			 * close to the scrap in y
    310  1.1  christos 			 */
    311  1.1  christos 			dx = sign(My_pos.x - rob->x);
    312  1.1  christos 			dy = sign(((slope * ((float) hp->x)) + cons) -
    313  1.1  christos 				  ((float) hp->y));
    314  1.1  christos 		}
    315  1.1  christos 		else {
    316  1.1  christos 			dx = sign(((slope * ((float) hp->x)) + cons) -
    317  1.1  christos 				  ((float) hp->y));
    318  1.1  christos 			dy = sign(My_pos.y - rob->y);
    319  1.1  christos 		}
    320  1.1  christos 	}
    321  1.1  christos 	CONSDEBUG(("me (%d,%d) robot(%d,%d) heap(%d,%d) delta(%d,%d)",
    322  1.1  christos 		My_pos.x, My_pos.y, rob->x, rob->y, hp->x, hp->y, dx, dy));
    323  1.1  christos 	return move_towards(dx, dy);
    324  1.1  christos } /* end move_between */
    325  1.1  christos 
    326  1.1  christos /* between():
    327  1.1  christos  * 	Return true if the heap is between us and the robot
    328  1.1  christos  */
    329  1.1  christos int
    330  1.1  christos between(rob, hp)
    331  1.1  christos 	COORD *rob;
    332  1.1  christos 	COORD *hp;
    333  1.1  christos {
    334  1.1  christos 	/* I = @ */
    335  1.1  christos 	if (hp->x > rob->x && My_pos.x < rob->x)
    336  1.1  christos 		return 0;
    337  1.1  christos 	/* @ = I */
    338  1.1  christos 	if (hp->x < rob->x && My_pos.x > rob->x)
    339  1.1  christos 		return 0;
    340  1.1  christos 	/* @ */
    341  1.1  christos 	/* = */
    342  1.1  christos 	/* I */
    343  1.1  christos 	if (hp->y < rob->y && My_pos.y > rob->y)
    344  1.1  christos 		return 0;
    345  1.1  christos 	/* I */
    346  1.1  christos 	/* = */
    347  1.1  christos 	/* @ */
    348  1.1  christos 	if (hp->y > rob->y && My_pos.y < rob->y)
    349  1.1  christos 		return 0;
    350  1.1  christos 	return 1;
    351  1.1  christos } /* end between */
    352  1.1  christos 
    353  1.1  christos /* automove():
    354  1.1  christos  * 	find and do the best move if flag
    355  1.1  christos  *	else get the first move;
    356  1.1  christos  */
    357  1.1  christos char
    358  1.1  christos automove()
    359  1.1  christos {
    360  1.1  christos #if 0
    361  1.1  christos 	return  find_moves()[0];
    362  1.1  christos #else
    363  1.1  christos 	COORD *robot_close;
    364  1.1  christos 	COORD *heap_close;
    365  1.1  christos 	int robot_dist, robot_heap, heap_dist;
    366  1.1  christos 
    367  1.1  christos 	robot_close = closest_robot(&robot_dist);
    368  1.1  christos 	if (robot_dist > 1)
    369  1.1  christos 		return('.');
    370  1.1  christos 	if (!Num_scrap)
    371  1.1  christos 		/* no scrap heaps just run away */
    372  1.1  christos 		return move_away(robot_close);
    373  1.1  christos 
    374  1.1  christos 	heap_close = closest_heap(&heap_dist);
    375  1.1  christos 	robot_heap = distance(robot_close->x, robot_close->y,
    376  1.1  christos 	    heap_close->x, heap_close->y);
    377  1.1  christos 	if (robot_heap <= heap_dist && !between(robot_close, heap_close)) {
    378  1.1  christos 		/*
    379  1.1  christos 		 * robot is closest to us from the heap. Run away!
    380  1.1  christos 		 */
    381  1.1  christos 		return  move_away(robot_close);
    382  1.1  christos 	}
    383  1.1  christos 
    384  1.1  christos 	return move_between(robot_close, heap_close);
    385  1.1  christos #endif
    386  1.2  christos } /* end automove */
    387