annotate 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
rev   line source
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
1 #include <stdio.h>
1102
dbea3e42bf93 Initial random number generator for tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1100
diff changeset
2 #include <time.h>
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
3 #include <stdlib.h>
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
4 #include <string.h>
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
5 #include "tank.h"
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
6
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
7 struct _enemy {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
8 tank_rt_t *tank_rt;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
9 tank_t *tank;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
10 int memory[4];
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
11 };
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
12 typedef struct _enemy enemy_t;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
13
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
14 /* \brief Fire and move to the target tank if they are in a row or column.
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
15 */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
16 static int
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
17 try_fire(tank_t *me, tank_t *target, tank_rt_t *tank_rt) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
18 int x, y;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
19 int *tracer = NULL;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
20 int target_value;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
21 int target_dir;
1103
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
22 tank_t **enemies;
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
23 int i;
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
24
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
25 if(me->map_x == target->map_x) { /* In a row */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
26 tracer = &y;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
27 target_value = target->map_y;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
28 if(me->map_y < target->map_y)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
29 target_dir = TD_DOWN;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
30 else
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
31 target_dir = TD_UP;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
32 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
33 if(me->map_y == target->map_y) { /* In a column */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
34 tracer = &x;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
35 target_value = target->map_x;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
36 if(me->map_x < target->map_x)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
37 target_dir = TD_RIGHT;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
38 else
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
39 target_dir = TD_LEFT;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
40 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
41
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
42 if(tracer == NULL)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
43 return 0; /* Not in a row or column */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
44
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
45 /* Check obstacles between tanks */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
46 x = me->map_x;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
47 y = me->map_y;
1103
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
48 enemies = tank_rt->tank_enemies;
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
49 if(*tracer < target_value) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
50 while(++*tracer < target_value) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
51 if(map[y][x] != MUD)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
52 break;
1103
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
53 for(i = 0; i < tank_rt->n_enemy; i++) {
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
54 if(enemies[i]->map_x == x && enemies[i]->map_y == y)
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
55 break;
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
56 }
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
57 if(i != tank_rt->n_enemy)
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
58 break;
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
59 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
60 } else {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
61 while(--*tracer > target_value) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
62 if(map[y][x] != MUD)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
63 break;
1103
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
64 for(i = 0; i < tank_rt->n_enemy; i++) {
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
65 if(enemies[i]->map_x == x && enemies[i]->map_y == y)
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
66 break;
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
67 }
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
68 if(i != tank_rt->n_enemy)
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
69 break;
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
70 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
71 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
72
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
73 if(*tracer == target_value) { /* No any obstacle between tanks */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
74 /* Fire and move to target */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
75 if(me->direction == target_dir && me->bullet == NULL)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
76 tank_fire_bullet(tank_rt, me);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
77 tank_move(me, target_dir, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
78 return 1;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
79 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
80
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
81 return 0; /* Find one or more obstacles */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
82 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
83
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
84 #define NOTHING 0
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
85 #define SOMETHING 1
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
86
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
87 static void
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
88 move_tank(enemy_t *enemy, tank_rt_t *tank_rt) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
89 tank_t *me;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
90 tank_t **tanks, *tank;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
91 static const int shift_xy[4][2] = {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
92 {1, 0}, {0, 1},
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
93 {-1, 0}, {0, -1}};
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
94 int status[4];
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
95 int x, y;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
96 int i, tank_i;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
97 int dir, chk_dir;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
98 int possibles;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
99 int which_dir;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
101 me = enemy->tank;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
102 tanks = tank_rt->tanks;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
103
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
104 /* Collect status */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
105 for(i = 0; i < 4; i++) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
106 x = me->map_x + shift_xy[i][0];
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
107 y = me->map_y + shift_xy[i][1];
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
108
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
109 /* Check obstacles */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
110 if(x == -1 || y == -1 || x >= MAP_W || y >= MAP_H) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
111 /* Out of range */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
112 status[i] = SOMETHING;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
113 continue;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
114 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
115 if(map[y][x] == MUD)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
116 status[i] = NOTHING;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
117 else
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
118 status[i] = SOMETHING;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
119
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
120 /* Check tanks */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
121 for(tank_i = 0; tank_i < tank_rt->n_tanks; tank_i++) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
122 tank = tanks[tank_i];
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
123 if(tank->map_x == x && tank->map_y == y) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
124 status[i] = SOMETHING;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
125 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
126 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
127 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
128 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
129
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
130 /* Try the same direction if status is not changed. */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
131 for(i = 0; i < 4; i++) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
132 if(status[i] != enemy->memory[i])
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
133 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
134 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
135 if(i == 4) { /* Status is not changed */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
136 tank_move(me, me->direction, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
137 return;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
138 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
139
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
140 memcpy(enemy->memory, status, sizeof(int) * 4);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
141
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
142 /* Check possible directions except backward. */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
143 switch(me->direction) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
144 case TD_UP:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
145 dir = 3;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
146 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
147 case TD_RIGHT:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
148 dir = 0;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
149 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
150 case TD_DOWN:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
151 dir = 1;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
152 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
153 case TD_LEFT:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
154 dir = 2;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
155 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
156 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
157
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
158 possibles = 0;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
159 for(i = 0; i < 3; i++) {
1103
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
160 chk_dir = (dir + 3 + i) % 4;
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
161 if(status[chk_dir] == NOTHING)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
162 possibles++;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
163 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
164
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
165 if(possibles == 0) { /* Only can move backward */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
166 switch(me->direction) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
167 case TD_UP:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
168 tank_move(me, TD_DOWN, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
169 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
170 case TD_RIGHT:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
171 tank_move(me, TD_LEFT, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
172 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
173 case TD_DOWN:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
174 tank_move(me, TD_UP, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
175 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
176 case TD_LEFT:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
177 tank_move(me, TD_RIGHT, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
178 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
179 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
180 return;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
181 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
182
1103
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
183 which_dir = (rand() % possibles) + 1;
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
184 for(i = 0; i < 3; i++) {
1103
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
185 chk_dir = (dir + 3 + i) % 4;
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
186 if(status[chk_dir] == NOTHING) {
1103
2b8b6deb35c8 Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
187 which_dir--;
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
188 if(which_dir == 0)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
189 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
190 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
191 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
192 switch(chk_dir) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
193 case 0:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
194 tank_move(me, TD_RIGHT, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
195 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
196 case 1:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
197 tank_move(me, TD_DOWN, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
198 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
199 case 2:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
200 tank_move(me, TD_LEFT, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
201 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
202 case 3:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
203 tank_move(me, TD_UP, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
204 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
205 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
206 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
207
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
208 static void
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
209 _drive_enemy_tank(enemy_t *enemy) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
210 tank_rt_t *tank_rt;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
211 tank_t *me, *tank1, *tank2;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
212 int r;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
213
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
214 tank_rt = enemy->tank_rt;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
215 tank1 = tank_rt->tank1;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
216 tank2 = tank_rt->tank2;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
217 me = enemy->tank;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
218
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
219 r = try_fire(me, tank1, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
220 if(r)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
221 return;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
222 r = try_fire(me, tank2, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
223 if(r)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
224 return;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
225
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
226 move_tank(enemy, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
227 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
228
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
229 static enemy_t *enemies = NULL;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
230 static mb_timer_man_t *timer_man;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
231
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
232 /*! \brief Drive every enemy tanks.
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
233 */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
234 static void
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
235 enemy_tank_driver(int hdl, const mb_timeval_t *tmo,
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
236 const mb_timeval_t *now, void *data) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
237 tank_rt_t *tank_rt = (tank_rt_t *)data;
1102
dbea3e42bf93 Initial random number generator for tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1100
diff changeset
238 enemy_t *enemy;
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
239 int n_enemy;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
240 mb_timeval_t timeout, addend;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
241 int i;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
242
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
243 n_enemy = tank_rt->n_enemy;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
244 for(i = 0; i < n_enemy; i++) {
1102
dbea3e42bf93 Initial random number generator for tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1100
diff changeset
245 enemy = enemies + i;
dbea3e42bf93 Initial random number generator for tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1100
diff changeset
246 if(enemy->tank->progm == NULL)
dbea3e42bf93 Initial random number generator for tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1100
diff changeset
247 _drive_enemy_tank(enemy);
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
248 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
249
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
250 get_now(&timeout);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
251 MB_TIMEVAL_SET(&addend, 0, 300000);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
252 MB_TIMEVAL_ADD(&timeout, &addend);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
253 mb_timer_man_timeout(timer_man, &timeout, enemy_tank_driver, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
254 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
255
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
256 /*! \brief Start a timer for enemy tank driver.
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
257 */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
258 static void
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
259 start_enemy_tank_timer(tank_rt_t *tank_rt) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
260 mb_timeval_t timeout, addend;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
261
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
262 timer_man = mb_runtime_timer_man(tank_rt->mb_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
263
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
264 get_now(&timeout);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
265 MB_TIMEVAL_SET(&addend, 0, 300000);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
266 MB_TIMEVAL_ADD(&timeout, &addend);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
267 mb_timer_man_timeout(timer_man, &timeout, enemy_tank_driver, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
268 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
269
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
270 void
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
271 init_enemies(tank_rt_t *tank_rt) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
272 int n_enemy;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
273 tank_t **tank_enemies;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
274 tank_t *tank;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
275 int i, j;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
276
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
277 n_enemy = tank_rt->n_enemy;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
278 tank_enemies = tank_rt->tank_enemies;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
279
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
280 enemies = (enemy_t *)malloc(sizeof(enemy_t) * n_enemy);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
281
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
282 for(i = 0; i < n_enemy; i++) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
283 tank = tank_enemies[i];
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
284 enemies[i].tank_rt = tank_rt;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
285 enemies[i].tank = tank;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
286 for(j = 0; j < 4; j++)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
287 enemies[i].memory[j] = SOMETHING;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
288 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
289
1102
dbea3e42bf93 Initial random number generator for tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1100
diff changeset
290 srand(time(NULL));
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
291 start_enemy_tank_timer(tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
292 }