diff examples/tank/enemy.c @ 1103:2b8b6deb35c8

Fix bug of AI of tank
author Thinker K.F. Li <thinker@codemud.net>
date Sun, 05 Dec 2010 14:26:19 +0800
parents dbea3e42bf93
children 236a47079c2b
line wrap: on
line diff
--- a/examples/tank/enemy.c	Sun Dec 05 13:47:07 2010 +0800
+++ b/examples/tank/enemy.c	Sun Dec 05 14:26:19 2010 +0800
@@ -19,6 +19,8 @@
     int *tracer = NULL;
     int target_value;
     int target_dir;
+    tank_t **enemies;
+    int i;
     
     if(me->map_x == target->map_x) { /* In a row */
 	tracer = &y;
@@ -43,15 +45,28 @@
     /* Check obstacles between tanks */
     x = me->map_x;
     y = me->map_y;
+    enemies = tank_rt->tank_enemies;
     if(*tracer < target_value) {
 	while(++*tracer < target_value) {
 	    if(map[y][x] != MUD)
 		break;
+	    for(i = 0; i < tank_rt->n_enemy; i++) {
+		if(enemies[i]->map_x == x && enemies[i]->map_y == y)
+		    break;
+	    }
+	    if(i != tank_rt->n_enemy)
+		break;
 	}
     } else {
 	while(--*tracer > target_value) {
 	    if(map[y][x] != MUD)
 		break;
+	    for(i = 0; i < tank_rt->n_enemy; i++) {
+		if(enemies[i]->map_x == x && enemies[i]->map_y == y)
+		    break;
+	    }
+	    if(i != tank_rt->n_enemy)
+		break;
 	}
     }
     
@@ -142,7 +157,7 @@
 
     possibles = 0;
     for(i = 0; i < 3; i++) {
-	chk_dir = (dir - 1  + i) % 4;
+	chk_dir = (dir + 3  + i) % 4;
 	if(status[chk_dir] == NOTHING)
 	    possibles++;
     }
@@ -165,13 +180,13 @@
 	return;
     }
     
-    which_dir = rand() % possibles;
+    which_dir = (rand() % possibles) + 1;
     for(i = 0; i < 3; i++) {
-	chk_dir = (dir - 1  + i) % 4;
+	chk_dir = (dir + 3  + i) % 4;
 	if(status[chk_dir] == NOTHING) {
+	    which_dir--;
 	    if(which_dir == 0)
 		break;
-	    which_dir--;
 	}
     }
     switch(chk_dir) {