-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstall_ai.c
executable file
·210 lines (186 loc) · 4.84 KB
/
stall_ai.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* there is everything ai-related */
#include <SDL.h>
#include "stall_types.h"
#include "stall_ini.h"
extern global_t global;
extern wave_t waves[WAVE_NUM];
extern enemy_ship_t enemy_ship[MAX_ENEMIES];
extern player_ship_t *player_ship;
/* -------------------- AI API --------------------- */
/* distance. used as return values in ai_look_at_player() */
static int dist_x, dist_y;
static int goal_x, goal_y;
/* return destination between ai and player */
static void ai_look_at_player(int i)
{
dist_x = player_ship->crd.x1 - enemy_ship[i].crd.x1;
dist_y = player_ship->crd.y1 - enemy_ship[i].crd.y1;
}
/* move to the goal coords. check if goal reached */
static void ai_move(int i)
{
switch(enemy_ship[i].move)
{
case MOVE_RIGHT:
{
enemy_ship[i].crd.x1 += enemy_ship[i].speed;
enemy_ship[i].crd.x2 += enemy_ship[i].speed;
break;
}
case MOVE_LEFT:
{
enemy_ship[i].crd.x1 -= enemy_ship[i].speed;
enemy_ship[i].crd.x2 -= enemy_ship[i].speed;
break;
}
case MOVE_DOWN:
{
enemy_ship[i].crd.y1 += enemy_ship[i].speed;
enemy_ship[i].crd.y2 += enemy_ship[i].speed;
break;
}
case MOVE_KAMIKAZE: //move on both x and y axis at same time
{
if (enemy_ship[i].crd.x1 > player_ship->crd.x1)
{
enemy_ship[i].crd.x1 -= enemy_ship[i].speed;
enemy_ship[i].crd.x2 -= enemy_ship[i].speed;
}
else
{
enemy_ship[i].crd.x1 += enemy_ship[i].speed;
enemy_ship[i].crd.x2 += enemy_ship[i].speed;
}
enemy_ship[i].crd.y1 += enemy_ship[i].speed;
enemy_ship[i].crd.y2 += enemy_ship[i].speed;
}
default:
break;
}
// if (enemy_ship[i].x1 != enemy_ship[i].goal_x)
/*
if (enemy_ship[i].y1 != enemy_ship[i].goal_y)
{
enemy_ship[i].y1 += enemy_ship[i].speed;
enemy_ship[i].y2 += enemy_ship[i].speed;
}
*/
#if 0
if (i==0) debug("x1 %d, y1 %d , goal_x %d, goal_y %d \n",
enemy_ship[i].crd.x1, enemy_ship[i].crd.y1, enemy_ship[i].goal_x, enemy_ship[i].goal_y);
#endif
/* if goal reached - remove current action */
if (enemy_ship[i].crd.x1 < enemy_ship[i].goal_x + 3 &&
enemy_ship[i].crd.x1 > enemy_ship[i].goal_x - 3 &&
enemy_ship[i].crd.y1 < enemy_ship[i].goal_y + 3 &&
enemy_ship[i].crd.y1 > enemy_ship[i].goal_y - 3 )
{
enemy_ship[i].in_action = 0;
//if (i==0) debug ("stop action \n");
}
}
static void ai_move_direction(int i)
{
switch(enemy_ship[i].move)
{
case MOVE_DONT: // now same as MOVE_LEFT
{
enemy_ship[i].move = MOVE_RIGHT;
goal_x = rand() % (GAME_WIDTH - waves[global.wave].enemy_width - enemy_ship[i].crd.x1);
enemy_ship[i].goal_x = enemy_ship[i].crd.x1 + goal_x;
enemy_ship[i].goal_y = enemy_ship[i].crd.y1;
break;
}
case MOVE_RIGHT:
{
enemy_ship[i].move = MOVE_LEFT;
goal_x = rand() % enemy_ship[i].crd.x1;
enemy_ship[i].goal_x = goal_x;
enemy_ship[i].goal_y = enemy_ship[i].crd.y1;
break;
}
case MOVE_LEFT:
{
enemy_ship[i].move = MOVE_RIGHT;
goal_x = rand() % (GAME_WIDTH - waves[global.wave].enemy_width - enemy_ship[i].crd.x1);
enemy_ship[i].goal_x = enemy_ship[i].crd.x1 + goal_x;
enemy_ship[i].goal_y = enemy_ship[i].crd.y1;
break;
}
case MOVE_DOWN: /* MOVE_RIGHT or MOVE_LEFT will be set next time */
{
if (rand() % 2)
{
enemy_ship[i].move = MOVE_LEFT; //TODO - same code as above
goal_x = rand() % enemy_ship[i].crd.x1;
enemy_ship[i].goal_x = goal_x;
enemy_ship[i].goal_y = enemy_ship[i].crd.y1;
}
else
{
enemy_ship[i].move = MOVE_RIGHT; //TODO - same code as above
goal_x = rand() % (GAME_WIDTH - waves[global.wave].enemy_width - enemy_ship[i].crd.x1);
enemy_ship[i].goal_x = enemy_ship[i].crd.x1 + goal_x;
enemy_ship[i].goal_y = enemy_ship[i].crd.y1;
}
break;
}
default:
break;
}
//debug("ai_mov_direction. i %d ,goal_x %d, move %d \n",i, goal_x, enemy_ship[i].move);
}
static void ai_new_move(int i)
{
switch (enemy_ship[i].mental) //XXX - add 2 more mentalities here
{
case MENTAL_AGGRESSIVE:
{
goal_x = player_ship->crd.x1;
goal_y = player_ship->crd.y1;
enemy_ship[i].goal_x = player_ship->crd.x1;
enemy_ship[i].goal_y = player_ship->crd.y1;
enemy_ship[i].move = MOVE_KAMIKAZE;
break;
}
case MENTAL_MIDDLE:
{
ai_move_direction(i);
break;
}
default:
{
ai_move_direction(i);
break;
}
}
/* now ai has action to do */
enemy_ship[i].in_action = 1;
//debug("ai_new_move. i %d, goal_x %d \n", i, enemy_ship[i].goal_x);
}
/* this is the most important function.
ai has to make a decision what to do next */
static void ai_decision(int i)
{
if (enemy_ship[i].in_action) /* already have action to do */
{
ai_move(i);
return;
}
if (!enemy_ship[i].in_action)
{
ai_look_at_player(i); //XXX - not used
ai_new_move(i);
ai_move(i);
}
}
/* main ai cycle. every enemy must choose decision */
void ai_cycle()
{
int i;
for (i = 0; i < MAX_ENEMIES; i++)
{
if (enemy_ship[i].alive)
ai_decision(i);
}
}