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