/
drunkenbot.c
135 lines (107 loc) · 4.15 KB
/
drunkenbot.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
/*
drunkenbot.c
This file is a part of Test Stuff 2.x source code.
Comes under the terms of GNU General Public License v.2.0.
*/
#include "draw.h"
#include "level.h"
#include "utils.h"
#include "player.h"
#include "drunkenbot.h"
int mv_tick = STEP;
/* Checks if direction is OK for moving (no collision, stc.) */
int dbDirSucceeded(t_Direction dir)
{
switch(dir)
{
case DIR_UP:
return (isCollision( currentLevel.DrunkenBots[0].posX,
currentLevel.DrunkenBots[0].posY-STEP) |
isOutOfBounds(currentLevel.DrunkenBots[0].posX,
currentLevel.DrunkenBots[0].posY-STEP)) ?
0 : 1;
break;
case DIR_RIGHT:
return (isCollision( currentLevel.DrunkenBots[0].posX+STEP,
currentLevel.DrunkenBots[0].posY) |
isOutOfBounds(currentLevel.DrunkenBots[0].posX+STEP,
currentLevel.DrunkenBots[0].posY)) ?
0 : 1;
break;
case DIR_DOWN:
return (isCollision( currentLevel.DrunkenBots[0].posX,
currentLevel.DrunkenBots[0].posY+STEP) |
isOutOfBounds(currentLevel.DrunkenBots[0].posX,
currentLevel.DrunkenBots[0].posY+STEP)) ?
0 : 1;
break;
case DIR_LEFT:
return (isCollision( currentLevel.DrunkenBots[0].posX-STEP,
currentLevel.DrunkenBots[0].posY) |
isOutOfBounds(currentLevel.DrunkenBots[0].posX-STEP,
currentLevel.DrunkenBots[0].posY)) ?
0 : 1;
break;
default: return 1;
}
return 1;
}
#define PATHFIND_TIMEOUT 0x400
/* Randomizes DrunkenBot's direction */
void dbRandomize()
{
int timeout = PATHFIND_TIMEOUT;
do
{
currentLevel.DrunkenBots[0].direction = rand()%4+1;
if(currentLevel.DrunkenBots[0].direction == 3) currentLevel.DrunkenBots[0].direction = DIR_DOWN;
else if(currentLevel.DrunkenBots[0].direction == 4) currentLevel.DrunkenBots[0].direction = DIR_LEFT;
timeout -- ;
if(!timeout)
{
fprintf(stderr, "DrunkenBot: path find timeout, self-destructing.\n");
currentLevel.DrunkenBots[0].isEnabled = 0;
break;
}
}
while(!dbDirSucceeded(currentLevel.DrunkenBots[0].direction));
}
void drunkenbotLoop()
{
if(!currentLevel.DrunkenBots[0].isEnabled) return;
switch(currentLevel.DrunkenBots[0].direction)
{
case DIR_UP: currentLevel.DrunkenBots[0].posY--; break;
case DIR_RIGHT: currentLevel.DrunkenBots[0].posX++; break;
case DIR_DOWN: currentLevel.DrunkenBots[0].posY++; break;
case DIR_LEFT: currentLevel.DrunkenBots[0].posX--; break;
}
mv_tick ++ ;
if(mv_tick >= STEP)
{
mv_tick = 0;
dbRandomize();
}
/* Slayin` the Player */
static SDL_Rect db_rect;
db_rect.x = currentLevel.DrunkenBots[0].posX;
db_rect.y = currentLevel.DrunkenBots[0].posY;
db_rect.w = STEP;
db_rect.h = STEP;
static SDL_Rect plr_rect;
plr_rect.x = player.posX;
plr_rect.y = player.posY;
plr_rect.w = STEP;
plr_rect.h = STEP;
if(isRectsCrosses(plr_rect, db_rect))
{
fprintf(stderr, "Player contacted with DrunkenBot\n");
playerSlay();
}
}
void drunkenbotDraw()
{
if(!currentLevel.DrunkenBots[0].isEnabled) return;
drawImage(&images[IMG_DRUNKENBOT], currentLevel.DrunkenBots[0].posX,
currentLevel.DrunkenBots[0].posY, NULL);
}