annotate examples/tank/enemy.c @ 1482:3a671e79429a

Hide/show selection for states
author Thinker K.F. Li <thinker@codemud.net>
date Fri, 29 Apr 2011 14:59:40 +0800
parents 45fcbd54e873
children
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
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
46 /* \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
47 */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
48 static int
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
49 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
50 int x, y;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
51 int *tracer = NULL;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
52 int target_value;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
56
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
57 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
58 tracer = &y;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
59 target_value = target->map_y;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
60 if(me->map_y < target->map_y)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
61 target_dir = TD_DOWN;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
62 else
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
63 target_dir = TD_UP;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
64 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
65 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
66 tracer = &x;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
67 target_value = target->map_x;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
68 if(me->map_x < target->map_x)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
69 target_dir = TD_RIGHT;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
70 else
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
71 target_dir = TD_LEFT;
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
74 if(tracer == NULL)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
75 return 0; /* Not in a row or column */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
76
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
77 /* Check obstacles between tanks */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
78 x = me->map_x;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
81 if(*tracer < target_value) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
82 while(++*tracer < target_value) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
83 if(map[y][x] != MUD)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
91 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
92 } else {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
93 while(--*tracer > target_value) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
94 if(map[y][x] != MUD)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
102 }
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
105 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
106 /* Fire and move to target */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
107 if(me->direction == target_dir && me->bullet == NULL)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
108 tank_fire_bullet(tank_rt, me);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
112 return 1;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
113 }
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 return 0; /* Find one or more obstacles */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
116 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
117
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
118 #define NOTHING 0
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
119 #define SOMETHING 1
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
120
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
121 static void
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
122 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
123 tank_t *me;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
124 tank_t **tanks, *tank;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
125 static const int shift_xy[4][2] = {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
126 {1, 0}, {0, 1},
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
127 {-1, 0}, {0, -1}};
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
128 int status[4];
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
129 int x, y;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
130 int i, tank_i;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
137
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
138 me = enemy->tank;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
139 tanks = tank_rt->tanks;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
140
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
141 /* Collect status */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
142 for(i = 0; i < 4; i++) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
143 x = me->map_x + shift_xy[i][0];
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
144 y = me->map_y + shift_xy[i][1];
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
148 /* Check obstacles */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
149 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
150 /* Out of range */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
151 status[i] = SOMETHING;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
152 continue;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
158 status[i] = SOMETHING;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
159
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
160 /* Check tanks */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
161 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
162 tank = tanks[tank_i];
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
163 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
164 status[i] = SOMETHING;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
165 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
166 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
167 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
168 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
169
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
170 /* 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
171 for(i = 0; i < 4; i++) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
172 if(status[i] != enemy->memory[i])
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
173 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
174 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
175 if(i == 4) { /* Status is not changed */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
179 return;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
180 }
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 memcpy(enemy->memory, status, sizeof(int) * 4);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
183
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
184 /* Check possible directions except backward. */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
185 switch(me->direction) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
186 case TD_UP:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
187 dir = 3;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
188 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
189 case TD_RIGHT:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
190 dir = 0;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
191 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
192 case TD_DOWN:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
193 dir = 1;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
194 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
195 case TD_LEFT:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
196 dir = 2;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
197 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
198 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
216
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
217 if(possibles == 0) { /* Only can move backward */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
218 switch(me->direction) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
219 case TD_UP:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
220 tank_move(me, TD_DOWN, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
221 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
222 case TD_RIGHT:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
223 tank_move(me, TD_LEFT, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
224 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
225 case TD_DOWN:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
226 tank_move(me, TD_UP, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
227 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
228 case TD_LEFT:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
229 tank_move(me, TD_RIGHT, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
230 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
234 return;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
235 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
246 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
247 }
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 switch(chk_dir) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
250 case 0:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
251 tank_move(me, TD_RIGHT, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
252 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
253 case 1:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
254 tank_move(me, TD_DOWN, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
255 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
256 case 2:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
257 tank_move(me, TD_LEFT, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
258 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
259 case 3:
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
260 tank_move(me, TD_UP, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
261 break;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
265 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
266
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
267 static void
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
268 _drive_enemy_tank(enemy_t *enemy) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
269 tank_rt_t *tank_rt;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
270 tank_t *me, *tank1, *tank2;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
271 int r;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
272
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
273 tank_rt = enemy->tank_rt;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
274 tank1 = tank_rt->tank1;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
275 tank2 = tank_rt->tank2;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
276 me = enemy->tank;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
277
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
278 r = try_fire(me, tank1, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
279 if(r)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
280 return;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
281 r = try_fire(me, tank2, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
282 if(r)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
283 return;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
284
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
285 move_tank(enemy, tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
286 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
287
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
288 static enemy_t *enemies = NULL;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
289 static mb_timer_man_t *timer_man;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
294 /*! \brief Drive every enemy tanks.
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
295 */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
299 int n_enemy;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
300 mb_timeval_t timeout, addend;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
301 int i;
1108
236a47079c2b Add pheromone for tank AI
Thinker K.F. Li <thinker@codemud.net>
parents: 1103
diff changeset
302
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
303 n_enemy = tank_rt->n_enemy;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
308 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
309
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
310 get_now(&timeout);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
311 MB_TIMEVAL_SET(&addend, 0, 300000);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
323 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
324
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
325 /*! \brief Start a timer for enemy tank driver.
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
326 */
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
327 static void
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
328 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
329 mb_timeval_t timeout, addend;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
330
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
331 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
332
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
333 get_now(&timeout);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
334 MB_TIMEVAL_SET(&addend, 0, 300000);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
337 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
338
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
339 void
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
340 init_enemies(tank_rt_t *tank_rt) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
341 int n_enemy;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
342 tank_t **tank_enemies;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
343 tank_t *tank;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
344 int i, j;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
345
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
346 n_enemy = tank_rt->n_enemy;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
347 tank_enemies = tank_rt->tank_enemies;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
348
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
349 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
350
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
351 for(i = 0; i < n_enemy; i++) {
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
352 tank = tank_enemies[i];
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
353 enemies[i].tank_rt = tank_rt;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
354 enemies[i].tank = tank;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
355 for(j = 0; j < 4; j++)
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
356 enemies[i].memory[j] = SOMETHING;
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
357 }
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
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
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
360 start_enemy_tank_timer(tank_rt);
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
361 }