annotate examples/tank/enemy.c @ 1100:fb79175e6cc3

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