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