summaryrefslogtreecommitdiff
path: root/ki.c
blob: 05c4591e1fe14fde6b12074b51f2a64f3bd1bc91 (plain)
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
/****************************************************************************\
*    																	*
*    Copyright by Casimo, 2013												*
*    																	*
*    This file is part of fxMaze.												*
*    																	*
*    fxMaze is free software: you can redistribute it and/or modify						*
*    it under the terms of the GNU General Public License as published by					*
*    the Free Software Foundation, either version 3 of the License, or					*
*    (at your option) any later version.											*
*    																	*
*    fxMaze is distributed in the hope that it will be useful,							*
*    but WITHOUT ANY WARRANTY; without even the implied warranty of				*
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the				*
*    GNU General Public License for more details.									*
*    																	*
*    You should have received a copy of the GNU General Public License					*
*    along with fxMaze.  If not, see <http://www.gnu.org/licenses/>.					*
*    																	*
\****************************************************************************/

#include "global.h"
#include "ki.h"
#include "calc.h"
#include "sprites.h"

#include "mathf.h"

// here are the ai's functions
// todo: change the function's names

// check if the player can be seen by the enemy
static int kshoot(float x, float y)
{
	float xadd, yadd;
	
	if(Abs(x - player.x) > Abs(y - player.y))
	{
		xadd = sgn(player.x - x);
		yadd = ((player.y - y) / Abs(x - player.x));
	}
	else
	{
		xadd = ((player.x - x) / Abs(y - player.y));
		yadd =sgn(player.y - y);
	}
	
	// this is raycasting again
	while(x < MAPSIZE && y < MAPSIZE && x >= 0 && y >= 0)
	{
		if((int)y == (int)player.y && (int)x == (int)player.x)
		{
			return TRUE;
		}
		if(solids[(int)x][(int)y] == SOLID)
		{
			break;
		}
		x += xadd;
		y += yadd;
	}
	return FALSE;
}

// move them in a random direction
static void kimove()
{
	int c;
	int dirx, diry;
	
	for(c = 0; c < spritecnt; c++)
	{
		int fail[4] = {0}, success = FALSE;

		// it _will_ move
		if(sprites[c].type == TYPE_ENEMY)
		{
			// try until there is no possible direction left
			while(!(fail[0] == TRUE && fail[1] == TRUE && fail[2] == TRUE && fail[3] == TRUE) && success == FALSE)
			{
				float newx, newy;
				
				if(kshoot(sprites[c].x, sprites[c].y) == TRUE)
				{
					// if the player is in sight (< 3 fields), go to him/her
					if(Abs((player.x) - sprites[c].x) > 3)
					{
						dirx = (player.x > sprites[c].x)?1:(player.x == sprites[c].x)?0:-1;
					}
					else
					{
						dirx = 0;
					}
					
					if(Abs((player.y) - sprites[c].y) > 3)
					{
						diry = (player.y > sprites[c].y)?1:(player.y == sprites[c].y)?0:-1;
					}
					else
					{
						diry = 0;
					}
					success = TRUE;
				}
				else
				{
					// there is no player, move randomly
					uint num;
					
					do
					{
						num = random() % 4;
					}while(fail[num] == TRUE);
					
					fail[num] = TRUE;
					
					switch(num)
					{
						case 0:
							dirx = 0;
							diry = -1;
							break;
						case 1:
							dirx = 1;
							diry = 0;
							break;
						case 2:
							dirx = 0;
							diry = 1;
							break;
						case 3:
							diry = 0;
							dirx = -1;
							break;
					}
				}
				
				newx = sprites[c].x + dirx;
				newy = sprites[c].y + diry;
				
				// move that stupid sprite
				if(newx < MAPSIZE && newx >= 0 && newy < MAPSIZE && newy  >= 0)
				{
					if(solids[(int)newx][(int)newy] == UNSOLID)
					{
						solids[(int)sprites[c].x][(int)sprites[c].y] = UNSOLID;
						sprites[c].x = newx;
						sprites[c].y = newy;
						solids[(int)sprites[c].x][(int)sprites[c].y] = SPRITE;
						success = TRUE;
					}
				}
			}
		}
	}
}

// this is a kind of high-level kshoot
// it subtracts the health
static void kishoot(void)
{
	int c;
	
	for(c = 0; c < spritecnt; c++)
	{
		if(sprites[c].type == TYPE_ENEMY)
		{
			if(kshoot(sprites[c].x, sprites[c].y) == TRUE)
			{
				player.health -= 5;
				output = 1;
				statustimer = 0;
				status = hitmsg;
				BacklightOn();
			}
		}
	}
}

// this is the timer that moves the ai
void timer(void)
{
	static int ki = 0;
	if(++ki == 10)
	{
		kimove();
		ki = 0;
		output = 1;
	}
	kishoot();
}

// set the new position in the map
void kisetpos(float *x, float *y)
{
	do
	{
		*x = crand() % MAPSIZE + 0.5;
		*y = crand() % MAPSIZE + 0.5;
	}while(solids[(int)*x][(int)*y] != UNSOLID);

	solids[(int)*x][(int)*y] = SPRITE;
}