-
Notifications
You must be signed in to change notification settings - Fork 1
/
ai.c
342 lines (242 loc) · 9.82 KB
/
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* @file ai.c
* @brief - Implementacije funkcija za vestacku inteligenciju.
*/
#include "global.h"
void updateDecisions(Tank *tank,float baseRandom, float chaseRandom,int chaseMeX, int chaseMeY, int aiSpeed, int aiParam1, int aiParam2, bool dontShoot){
int x = tank->x, y = tank->y,i,dx,dy,di,dj, currX, currY, empty, popX, popY,dir, fromX, fromY;
float newRand = (float)rand()/(float)RAND_MAX;
int mapMove[MAP_SIZE][MAP_SIZE], mapMoveLast[MAP_SIZE][MAP_SIZE];
char mapAI[MAP_SIZE][MAP_SIZE];
queue matrixQueue;
//what map to use ??
for(di=0; di<MAP_SIZE; di++) for (dj =0; dj<MAP_SIZE; dj++) mapAI[di][dj] = map[di][dj];
//update player_1 to mapAI
for (dx = 0; dx < 3; dx++)
for (dy = 0; dy < 3; dy++) mapAI[player1.x + dx][player1.y + dy] = 'T';
//update tanks to mapAI
for (i = 0; i < MAX_SPRITES; i++) if (tanks[i].alive)
{
for (dx = 0; dx < 3; dx++)
for (dy = 0; dy < 3; dy++) mapAI[tanks[i].x + dx][tanks[i].y + dy] = 'T';
}
//set empty for this tank
for (dx = 0; dx < 3; dx++)
for (dy = 0; dy < 3; dy++) mapAI[tank->x + dx][tank->y + dy] = EMPTY;
matrixQueue.front = matrixQueue.end = 0;
memset(mapMove,0,sizeof(mapMove));
memset(mapMoveLast,0,sizeof(mapMove));
// move(50,50);
// printw("%d %d ",chaseMeX,chaseMeY);
// move(51,50);
// printw("%d %d ",player1.x, player1.y);
// refresh();
// move(52,50); printw("inna ai "); refresh();
//go towards base
if (newRand < baseRandom + chaseRandom){
if (newRand < baseRandom){
//go toward base
fromX = base_x;
fromY = base_y;
}
else{
//go toward ME
fromX = chaseMeX;
fromY = chaseMeY;
}
if (dontShoot){
mapMove [ fromX ][ fromY ] = 1;
queuePush(&matrixQueue, fromX*MAP_SIZE + fromY);
}
//generate mapShoot and init for mapMove (set ones from the spot that can shoot)
else for (dx = -1; dx < 2; dx++)
for (dy = -1; dy < 2; dy++){
if ( (dx+dy)%2 == 0) continue;
currX = fromX;
currY = fromY;
empty=1;
while(empty && currX < MAP_SIZE -2 && currX >= 0 && currY < MAP_SIZE -2 && currY >= 0){
for(di=0;di<3;di++) for(dj=0;dj<3;dj++){
if (mapAI[ currX + di ][ currY + dj ] != EMPTY && mapAI[ currX + di ][ currY + dj ] != GRASS) empty = 0;
}
if (empty && !dontShoot){
mapMove[currX][currY] = 1;
queuePush(&matrixQueue, currX*MAP_SIZE + currY);
}
empty =1;
for(di=0;di<3;di++) for(dj=0;dj<3;dj++){
if (mapAI[ currX + di ][ currY + dj ] == STEEL ) empty = 0;
}
currX += dx;
currY += dy;
}
}
//now find path to player
while(! queueEmpty( &matrixQueue )){
//this is some retarded data representation in a queue (two ints in a int)
popX = queuePop(&matrixQueue);
popY = popX % MAP_SIZE;
popX /= MAP_SIZE;
// move(53,50);printw("pop: %d %d ",popX,popY);refresh();
if (popX ==0 && popY ==0){
popX =0 ;
}
for (dx = -1; dx < 2; dx++)
for (dy = -1; dy < 2; dy++){
if ( (dx+dy)%2 == 0) continue;
currX = popX + dx;
currY = popY + dy;
//off the map
if (currX >=MAP_SIZE -2 || currY >= MAP_SIZE -2 || currX < 0 || currY < 0) continue;
empty =1;
for(di=0;di<3;di++) for(dj=0;dj<3;dj++){
if (mapAI[ currX + di ][ currY + dj ] != EMPTY && mapAI[ currX + di ][ currY + dj ] != GRASS) empty = 0;
}
if (empty && ( mapMove[ currX ][ currY ]==0 || mapMove[ currX ][ currY ] > mapMove[ popX ][ popY ] + 1) ){
mapMove[ currX ][ currY ] = mapMove[ popX ][ popY ] + 1;
queuePush(&matrixQueue, currX*MAP_SIZE + currY);
switch (dx){
case 0:
switch(dy){
case -1:
mapMoveLast[ currX ][ currY ] = RIGHT;
break;
case 1:
mapMoveLast[ currX ][ currY ] = LEFT;
break;
}
break;
case -1:
mapMoveLast[ currX ][ currY ] = DOWN;
break;
case 1:
mapMoveLast[ currX ][ currY ] = UP;
break;
}
}
}
}
currX = tank->x;
currY = tank->y;
//you can go to the chasing object
if (mapMove[ currX ][ currY ]){
i=aiSpeed;
while(i--){
if (mapMove[ currX ][ currY ] == 1 ){
if (currX == fromX){
if (currY > fromY){
dir = LEFT;
}
else{
dir = RIGHT;
}
}
else{
if (currX > fromX){
dir = UP;
}
else{
dir = DOWN;
}
}
//now check if the last decision was exactly this direction
if ( i== aiSpeed-1 ){
if (tank->dir != dir){
tank->AIDecisions[ i-- ] = dir;
}
}
else{
if (tank->AIDecisions[ i+1 ] != dir){
tank->AIDecisions[ i-- ] = dir;
}
}
//now lets shoot !!!
while(i >=0 ) tank->AIDecisions[ i-- ] = 4;
return ;
}
else {
tank->AIDecisions[ i ] = mapMoveLast[ currX ][ currY ];
switch(mapMoveLast[currX][currY]){
case DOWN:
currX++;
break;
case UP:
currX--;
break;
case RIGHT:
currY++;
break;
case LEFT:
currY--;
break;
}
}
}
return ;
}
}
//just do it random
for( i=0; i<aiSpeed; i++ ){
dir = rand()%(int)(5 + aiParam2*4.);
if (dir > 3 && dir < (int)(4 + aiParam2*4.) ) dir = 4;
if (tank == &player1 && dir == 4 && tank->dir != UP) dir = UP;
for (di =0 ;di<aiParam1; di++){
if (i + di >= aiSpeed) break;
tank->AIDecisions[ i+di ] = dir;
}
i+=di-1;
}
}
int get_decision_easy(Tank *tank){
if (--(tank->AIState) < 0){
updateDecisions(tank, BASE_EASY,MYTANK_EASY, player1.x+1, player1.y +1, AI_SPEED, AI_PARAM1, AI_PARAM2,0);
tank->AIState = AI_SPEED-1;
}
return tank->AIDecisions[ tank->AIState ];
}
int get_decision_medium(Tank *tank){
if (--(tank->AIState) < 0){
updateDecisions(tank, BASE_MEDIUM,MYTANK_MEDIUM, player1.x+1, player1.y +1, AI_SPEED, AI_PARAM1, AI_PARAM2, 0);
tank->AIState = AI_SPEED-1;
}
return tank->AIDecisions[ tank->AIState ];
}
int get_decision_hard(Tank *tank){
if (--(tank->AIState) < 0){
updateDecisions(tank, BASE_HARD,MYTANK_HARD, player1.x+1, player1.y +1, AI_SPEED, AI_PARAM1, AI_PARAM2, 0);
tank->AIState = AI_SPEED-1;
}
return tank->AIDecisions[ tank->AIState ];
}
int get_decision_demo(){
int chaseX,chaseY,di,dj,i,chasePowerUp;
chaseX=-1;
di = RAND_MAX;
//if there is a powerUP
if (power_up.type !=NORMAL){
chaseX = power_up.x;
chaseY = power_up.y;
chasePowerUp = true;
di =(player1.x - power_up.x)*(player1.x - power_up.x) + (player1.y - power_up.y)*(player1.y - power_up.y);
}
//find nearest
for(i=0; i<MAX_SPRITES; i++){
if (!tanks[i].alive) continue;
dj = (player1.x - tanks[i].x)*(player1.x - tanks[i].x) + (player1.y - tanks[i].y)*(player1.y - tanks[i].y);
if ((float)di/dj > AI_PARAM3) {
di = dj;
chasePowerUp = false;
chaseX = tanks[i].x;
chaseY = tanks[i].y;
}
}
if (chaseX != -1 ){
if (--(player1.AIState) < 0){
updateDecisions(&player1, 0, 1 , chaseX, chaseY,1 , 1 ,1, chasePowerUp );
player1.AIState = 0;
}
return player1.AIDecisions[ player1.AIState ];
}
//dont do anything
return 5;
}