Mercurial > MadButterfly
annotate examples/tank/enemy.c @ 1434:4be04f29fa70
Add functions to search for the text recursively inside coord_t tree. Once we find the first instance, we change the text of it. We need to think about how to manage the multiple segment texts, which is composed of several tspan.
author | wycc |
---|---|
date | Mon, 11 Apr 2011 12:52:09 +0800 |
parents | 45fcbd54e873 |
children |
rev | line source |
---|---|
1100 | 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 | 3 #include <stdlib.h> |
4 #include <string.h> | |
5 #include "tank.h" | |
6 | |
7 struct _enemy { | |
8 tank_rt_t *tank_rt; | |
9 tank_t *tank; | |
10 int memory[4]; | |
11 }; | |
12 typedef struct _enemy enemy_t; | |
13 | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
14 /*! \brief Pheromone Map trace the freq. the enemy tanks moving on an area. |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
15 * |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
16 * Every time a tank move to an area, it leaves 100 units of pheromone |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
17 * on the area. And 9 of 10 pheromone are removed from the area for |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
18 * every time slice (1/3 second). The max value of phereomone in an |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
19 * area is 991. |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
20 */ |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
21 static int pheromone_map[MAP_H][MAP_W]; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
22 |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
23 static void |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
24 init_pheromone_map(void) { |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
25 int i, j; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
26 |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
27 for(i = 0; i < MAP_H; i++) |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
28 for(j = 0; j < MAP_W; j++) |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
29 pheromone_map[i][j] = 0; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
30 } |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
31 |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
32 static void |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
33 remove_pheromone(void) { |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
34 int i, j; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
35 |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
36 for(i = 0; i < MAP_H; i++) |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
37 for(j = 0; j < MAP_W; j++) |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
38 pheromone_map[i][j] = pheromone_map[i][j] * 9 / 10; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
39 } |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
40 |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
41 static void |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
42 leave_pheromone(int x, int y) { |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
43 pheromone_map[y][x] += 100; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
44 } |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
45 |
1100 | 46 /* \brief Fire and move to the target tank if they are in a row or column. |
47 */ | |
48 static int | |
49 try_fire(tank_t *me, tank_t *target, tank_rt_t *tank_rt) { | |
50 int x, y; | |
51 int *tracer = NULL; | |
52 int target_value; | |
53 int target_dir; | |
1103
2b8b6deb35c8
Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1102
diff
changeset
|
54 tank_t **enemies; |
2b8b6deb35c8
Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1102
diff
changeset
|
55 int i; |
1100 | 56 |
57 if(me->map_x == target->map_x) { /* In a row */ | |
58 tracer = &y; | |
59 target_value = target->map_y; | |
60 if(me->map_y < target->map_y) | |
61 target_dir = TD_DOWN; | |
62 else | |
63 target_dir = TD_UP; | |
64 } | |
65 if(me->map_y == target->map_y) { /* In a column */ | |
66 tracer = &x; | |
67 target_value = target->map_x; | |
68 if(me->map_x < target->map_x) | |
69 target_dir = TD_RIGHT; | |
70 else | |
71 target_dir = TD_LEFT; | |
72 } | |
73 | |
74 if(tracer == NULL) | |
75 return 0; /* Not in a row or column */ | |
76 | |
77 /* Check obstacles between tanks */ | |
78 x = me->map_x; | |
79 y = me->map_y; | |
1103
2b8b6deb35c8
Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1102
diff
changeset
|
80 enemies = tank_rt->tank_enemies; |
1100 | 81 if(*tracer < target_value) { |
82 while(++*tracer < target_value) { | |
83 if(map[y][x] != MUD) | |
84 break; | |
1103
2b8b6deb35c8
Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1102
diff
changeset
|
85 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
|
86 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
|
87 break; |
2b8b6deb35c8
Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1102
diff
changeset
|
88 } |
2b8b6deb35c8
Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1102
diff
changeset
|
89 if(i != tank_rt->n_enemy) |
2b8b6deb35c8
Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1102
diff
changeset
|
90 break; |
1100 | 91 } |
92 } else { | |
93 while(--*tracer > target_value) { | |
94 if(map[y][x] != MUD) | |
95 break; | |
1103
2b8b6deb35c8
Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1102
diff
changeset
|
96 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
|
97 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
|
98 break; |
2b8b6deb35c8
Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1102
diff
changeset
|
99 } |
2b8b6deb35c8
Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1102
diff
changeset
|
100 if(i != tank_rt->n_enemy) |
2b8b6deb35c8
Fix bug of AI of tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1102
diff
changeset
|
101 break; |
1100 | 102 } |
103 } | |
104 | |
105 if(*tracer == target_value) { /* No any obstacle between tanks */ | |
106 /* Fire and move to target */ | |
107 if(me->direction == target_dir && me->bullet == NULL) | |
108 tank_fire_bullet(tank_rt, me); | |
109 tank_move(me, target_dir, tank_rt); | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
110 |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
111 leave_pheromone(me->map_x, me->map_y); |
1100 | 112 return 1; |
113 } | |
114 | |
115 return 0; /* Find one or more obstacles */ | |
116 } | |
117 | |
118 #define NOTHING 0 | |
119 #define SOMETHING 1 | |
120 | |
121 static void | |
122 move_tank(enemy_t *enemy, tank_rt_t *tank_rt) { | |
123 tank_t *me; | |
124 tank_t **tanks, *tank; | |
125 static const int shift_xy[4][2] = { | |
126 {1, 0}, {0, 1}, | |
127 {-1, 0}, {0, -1}}; | |
128 int status[4]; | |
129 int x, y; | |
130 int i, tank_i; | |
131 int dir, chk_dir; | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
132 int interest_areas[4]; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
133 int all_interest; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
134 int min_interest; |
1100 | 135 int possibles; |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
136 int interest_dir; |
1100 | 137 |
138 me = enemy->tank; | |
139 tanks = tank_rt->tanks; | |
140 | |
141 /* Collect status */ | |
142 for(i = 0; i < 4; i++) { | |
143 x = me->map_x + shift_xy[i][0]; | |
144 y = me->map_y + shift_xy[i][1]; | |
145 | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
146 interest_areas[i] = 0; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
147 |
1100 | 148 /* Check obstacles */ |
149 if(x == -1 || y == -1 || x >= MAP_W || y >= MAP_H) { | |
150 /* Out of range */ | |
151 status[i] = SOMETHING; | |
152 continue; | |
153 } | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
154 if(map[y][x] == MUD) { |
1100 | 155 status[i] = NOTHING; |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
156 interest_areas[i] = 992 - pheromone_map[y][x]; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
157 } else |
1100 | 158 status[i] = SOMETHING; |
159 | |
160 /* Check tanks */ | |
161 for(tank_i = 0; tank_i < tank_rt->n_tanks; tank_i++) { | |
162 tank = tanks[tank_i]; | |
163 if(tank->map_x == x && tank->map_y == y) { | |
164 status[i] = SOMETHING; | |
165 break; | |
166 } | |
167 } | |
168 } | |
169 | |
170 /* Try the same direction if status is not changed. */ | |
171 for(i = 0; i < 4; i++) { | |
172 if(status[i] != enemy->memory[i]) | |
173 break; | |
174 } | |
175 if(i == 4) { /* Status is not changed */ | |
176 tank_move(me, me->direction, tank_rt); | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
177 |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
178 leave_pheromone(me->map_x, me->map_y); |
1100 | 179 return; |
180 } | |
181 | |
182 memcpy(enemy->memory, status, sizeof(int) * 4); | |
183 | |
184 /* Check possible directions except backward. */ | |
185 switch(me->direction) { | |
186 case TD_UP: | |
187 dir = 3; | |
188 break; | |
189 case TD_RIGHT: | |
190 dir = 0; | |
191 break; | |
192 case TD_DOWN: | |
193 dir = 1; | |
194 break; | |
195 case TD_LEFT: | |
196 dir = 2; | |
197 break; | |
198 } | |
199 | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
200 /* Compute itnerest for nearby areas */ |
1100 | 201 possibles = 0; |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
202 all_interest = 0; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
203 min_interest = 992; |
1100 | 204 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
|
205 chk_dir = (dir + 3 + i) % 4; |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
206 if(status[chk_dir] == NOTHING) { |
1100 | 207 possibles++; |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
208 all_interest += interest_areas[chk_dir]; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
209 if(min_interest > interest_areas[chk_dir]) |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
210 min_interest = interest_areas[chk_dir]; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
211 } |
1100 | 212 } |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
213 all_interest -= (min_interest * possibles) + possibles; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
214 for(i = 0; i < 4; i++) |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
215 interest_areas[i] = interest_areas[i] - min_interest + 1; |
1100 | 216 |
217 if(possibles == 0) { /* Only can move backward */ | |
218 switch(me->direction) { | |
219 case TD_UP: | |
220 tank_move(me, TD_DOWN, tank_rt); | |
221 break; | |
222 case TD_RIGHT: | |
223 tank_move(me, TD_LEFT, tank_rt); | |
224 break; | |
225 case TD_DOWN: | |
226 tank_move(me, TD_UP, tank_rt); | |
227 break; | |
228 case TD_LEFT: | |
229 tank_move(me, TD_RIGHT, tank_rt); | |
230 break; | |
231 } | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
232 |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
233 leave_pheromone(me->map_x, me->map_y); |
1100 | 234 return; |
235 } | |
236 | |
1135
45fcbd54e873
Fix issue of divided by zero for tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1108
diff
changeset
|
237 if(all_interest == 0) /* all nearby places are occupied */ |
45fcbd54e873
Fix issue of divided by zero for tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1108
diff
changeset
|
238 return; |
45fcbd54e873
Fix issue of divided by zero for tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1108
diff
changeset
|
239 |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
240 interest_dir = (rand() % all_interest) + 1; |
1100 | 241 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
|
242 chk_dir = (dir + 3 + i) % 4; |
1100 | 243 if(status[chk_dir] == NOTHING) { |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
244 interest_dir -= interest_areas[chk_dir]; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
245 if(interest_dir <= 0) |
1100 | 246 break; |
247 } | |
248 } | |
249 switch(chk_dir) { | |
250 case 0: | |
251 tank_move(me, TD_RIGHT, tank_rt); | |
252 break; | |
253 case 1: | |
254 tank_move(me, TD_DOWN, tank_rt); | |
255 break; | |
256 case 2: | |
257 tank_move(me, TD_LEFT, tank_rt); | |
258 break; | |
259 case 3: | |
260 tank_move(me, TD_UP, tank_rt); | |
261 break; | |
262 } | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
263 |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
264 leave_pheromone(me->map_x, me->map_y); |
1100 | 265 } |
266 | |
267 static void | |
268 _drive_enemy_tank(enemy_t *enemy) { | |
269 tank_rt_t *tank_rt; | |
270 tank_t *me, *tank1, *tank2; | |
271 int r; | |
272 | |
273 tank_rt = enemy->tank_rt; | |
274 tank1 = tank_rt->tank1; | |
275 tank2 = tank_rt->tank2; | |
276 me = enemy->tank; | |
277 | |
278 r = try_fire(me, tank1, tank_rt); | |
279 if(r) | |
280 return; | |
281 r = try_fire(me, tank2, tank_rt); | |
282 if(r) | |
283 return; | |
284 | |
285 move_tank(enemy, tank_rt); | |
286 } | |
287 | |
288 static enemy_t *enemies = NULL; | |
289 static mb_timer_man_t *timer_man; | |
290 | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
291 static void enemy_tick(int hdl, const mb_timeval_t *tmo, |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
292 const mb_timeval_t *now, void *data); |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
293 |
1100 | 294 /*! \brief Drive every enemy tanks. |
295 */ | |
296 static void | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
297 enemy_tank_driver(tank_rt_t *tank_rt) { |
1102
dbea3e42bf93
Initial random number generator for tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1100
diff
changeset
|
298 enemy_t *enemy; |
1100 | 299 int n_enemy; |
300 mb_timeval_t timeout, addend; | |
301 int i; | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
302 |
1100 | 303 n_enemy = tank_rt->n_enemy; |
304 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
|
305 enemy = enemies + i; |
dbea3e42bf93
Initial random number generator for tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1100
diff
changeset
|
306 if(enemy->tank->progm == NULL) |
dbea3e42bf93
Initial random number generator for tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1100
diff
changeset
|
307 _drive_enemy_tank(enemy); |
1100 | 308 } |
309 | |
310 get_now(&timeout); | |
311 MB_TIMEVAL_SET(&addend, 0, 300000); | |
312 MB_TIMEVAL_ADD(&timeout, &addend); | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
313 mb_timer_man_timeout(timer_man, &timeout, enemy_tick, tank_rt); |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
314 } |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
315 |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
316 static void |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
317 enemy_tick(int hdl, const mb_timeval_t *tmo, |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
318 const mb_timeval_t *now, void *data) { |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
319 tank_rt_t *tank_rt = (tank_rt_t *)data; |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
320 |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
321 remove_pheromone(); |
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
322 enemy_tank_driver(tank_rt); |
1100 | 323 } |
324 | |
325 /*! \brief Start a timer for enemy tank driver. | |
326 */ | |
327 static void | |
328 start_enemy_tank_timer(tank_rt_t *tank_rt) { | |
329 mb_timeval_t timeout, addend; | |
330 | |
331 timer_man = mb_runtime_timer_man(tank_rt->mb_rt); | |
332 | |
333 get_now(&timeout); | |
334 MB_TIMEVAL_SET(&addend, 0, 300000); | |
335 MB_TIMEVAL_ADD(&timeout, &addend); | |
1108
236a47079c2b
Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents:
1103
diff
changeset
|
336 mb_timer_man_timeout(timer_man, &timeout, enemy_tick, tank_rt); |
1100 | 337 } |
338 | |
339 void | |
340 init_enemies(tank_rt_t *tank_rt) { | |
341 int n_enemy; | |
342 tank_t **tank_enemies; | |
343 tank_t *tank; | |
344 int i, j; | |
345 | |
346 n_enemy = tank_rt->n_enemy; | |
347 tank_enemies = tank_rt->tank_enemies; | |
348 | |
349 enemies = (enemy_t *)malloc(sizeof(enemy_t) * n_enemy); | |
350 | |
351 for(i = 0; i < n_enemy; i++) { | |
352 tank = tank_enemies[i]; | |
353 enemies[i].tank_rt = tank_rt; | |
354 enemies[i].tank = tank; | |
355 for(j = 0; j < 4; j++) | |
356 enemies[i].memory[j] = SOMETHING; | |
357 } | |
358 | |
1102
dbea3e42bf93
Initial random number generator for tank
Thinker K.F. Li <thinker@codemud.net>
parents:
1100
diff
changeset
|
359 srand(time(NULL)); |
1100 | 360 start_enemy_tank_timer(tank_rt); |
361 } |