-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.cpp
361 lines (315 loc) · 11.4 KB
/
AI.cpp
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "AI.h"
#include "gameData.h"
#include "imageFactory.h"
AI::AI( const std::string& name) :
Drawable(name,
Vector2f(Gamedata::getInstance().getXmlInt(name+"/startLoc/x"),
Gamedata::getInstance().getXmlInt(name+"/startLoc/y")),
Vector2f(Gamedata::getInstance().getXmlInt(name+"/speedX"),
Gamedata::getInstance().getXmlInt(name+"/speedY"))
),
image( ImageFactory::getInstance().getImage(name+"/tracks/Donut_Plains_1/full", name) ),
track( IMG_Load(Gamedata::getInstance().getXmlStr("road/tracks/Donut_Plains_1/full").c_str())),
sky( IMG_Load(Gamedata::getInstance().getXmlStr("sky/file").c_str())),
grass( IMG_Load(Gamedata::getInstance().getXmlStr("road/tracks/Donut_Plains_1/grass").c_str())),
blocks( IMG_Load(Gamedata::getInstance().getXmlStr("road/tracks/Donut_Plains_1/boundry").c_str())),
worldWidth(Gamedata::getInstance().getXmlInt("background/width")),
worldHeight(Gamedata::getInstance().getXmlInt("background/height")),
fWorldX(Gamedata::getInstance().getXmlFloat("worldData/fworldx")),
fWorldY(Gamedata::getInstance().getXmlFloat("worldData/fworldy")),
fWorldA(Gamedata::getInstance().getXmlFloat("worldData/fworlda")),
fNear(Gamedata::getInstance().getXmlFloat("worldData/fnear")),
fFar(Gamedata::getInstance().getXmlFloat("worldData/ffar"))
{
}
AI::AI(const AI& s) :
Drawable(s),
image(s.image),
track(s.track),
sky(s.sky),
grass(s.grass),
blocks(s.blocks),
worldWidth( s.worldWidth ),
worldHeight( s.worldHeight )
{ }
AI& AI::operator=(const AI& s) {
Drawable::operator=(s);
image = (s.image);
track = s.track;
sky = s.sky;
grass = s.grass;
blocks = s.blocks;
worldWidth = ( s.worldWidth );
worldHeight = ( s.worldHeight );
return *this;
}
bool AI::checkVelocity(Uint32 ticks, int direction, float p_x, float p_y)
{
if(direction == 1)
{
//fNear -= 0.1f * ticks;
fWorldA -= 0.1f;
bool blocked = CheckNewValue(p_x, p_y);
if(!blocked)
{
fWorldA += 0.3f;
}
return blocked;
}
else if (direction == 2)
{
fWorldA += 0.1f;
bool blocked = CheckNewValue(p_x, p_y);
if(!blocked)
{
fWorldA -= 0.3f;
}
return blocked;
}
else if(direction ==3)
{
fWorldX += cosf(fWorldA) * 0.1f * ticks;
fWorldY += sinf(fWorldA) * 0.1f * ticks;
bool blocked = CheckNewValue(p_x, p_y);
if(!blocked)
{
fWorldX -= cosf(fWorldA) * 0.3f * ticks;
fWorldY -= sinf(fWorldA) * 0.3f * ticks;
}
return blocked;
}
else if (direction == 4)
{
fWorldX -= cosf(fWorldA) * 0.1f * ticks;
fWorldY -= sinf(fWorldA) * 0.1f * ticks;
bool blocked = CheckNewValue(p_x, p_y);
if(!blocked)
{
fWorldX += cosf(fWorldA) * 0.3f * ticks;
fWorldY += sinf(fWorldA) * 0.3f * ticks;
}
return blocked;
}
else if (direction == 5)
{
//fFoVHalf -= 0.2f * ticks;
fNear -= 0.1f * ticks;
}
else if (direction == 6)
{
//fFoVHalf += 0.2f * ticks;
fNear += 0.1f * ticks;
}
else if (direction == 7)
{
//fFoVHalf -= 0.2f * ticks;
fFar -= 0.1f * ticks;
}
else if (direction == 8)
{
//fFoVHalf += 0.2f * ticks;
fFar += 0.1f * ticks;
}
else if (direction == 9)
{
//fFoVHalf -= 0.2f * ticks;
fFoVHalf -= 0.1f;
}
else if (direction == 10)
{
//fFoVHalf += 0.2f * ticks;
fFoVHalf += 0.1f;
}
return true;
}
void AI::update(Uint32 ticks){}
void AI::update(Uint32 ticks, int direction, float factor)
{
if(direction == 1)
{
//fNear -= 0.1f * ticks;
fWorldA -= factor;
}
else if (direction == 2)
{
fWorldA += factor;
//fNear += 0.1f * ticks;
}
else if(direction ==3)
{
fWorldX += cosf(fWorldA) * factor* ticks;
fWorldY += sinf(fWorldA) * factor * ticks;
}
else if (direction == 4)
{
fWorldX -= cosf(fWorldA) * factor * ticks;
fWorldY -= sinf(fWorldA) * factor * ticks;
}
else if (direction == 5)
{
//fFoVHalf -= 0.2f * ticks;
fNear -= 0.1f * ticks;
}
else if (direction == 6)
{
//fFoVHalf += 0.2f * ticks;
fNear += 0.1f * ticks;
}
else if (direction == 7)
{
//fFoVHalf -= 0.2f * ticks;
fFar -= 0.1f * ticks;
}
else if (direction == 8)
{
//fFoVHalf += 0.2f * ticks;
fFar += 0.1f * ticks;
}
else if (direction == 9)
{
//fFoVHalf -= 0.2f * ticks;
fFoVHalf -= 0.1f;
}
else if (direction == 10)
{
//fFoVHalf += 0.2f * ticks;
fFoVHalf += 0.1f;
}
}
float AI::GrassVelocity(int x, int y)
{
float fFarX1 = fWorldX + cosf(fWorldA - fFoVHalf) * fFar;
float fFarY1 = fWorldY + sinf(fWorldA - fFoVHalf) * fFar;
float fNearX1 = fWorldX + cosf(fWorldA - fFoVHalf) * fNear;
float fNearY1 = fWorldY + sinf(fWorldA - fFoVHalf) * fNear;
float fFarX2 = fWorldX + cosf(fWorldA + fFoVHalf) * fFar;
float fFarY2 = fWorldY + sinf(fWorldA + fFoVHalf) * fFar;
float fNearX2 = fWorldX + cosf(fWorldA + fFoVHalf) * fNear;
float fNearY2 = fWorldY + sinf(fWorldA + fFoVHalf) * fNear;
float fSampleDepth = (float)y / ((float)worldHeight / 2.0f);
float fSampleWidth = (float)x / (float)worldWidth;
float fStartX = (fFarX1 - fNearX1) / (fSampleDepth) + fNearX1;
float fStartY = (fFarY1 - fNearY1) / (fSampleDepth) + fNearY1;
float fEndX = (fFarX2 - fNearX2) / (fSampleDepth) + fNearX2;
float fEndY = (fFarY2 - fNearY2) / (fSampleDepth) + fNearY2;
float fSampleX = (fEndX - fStartX) * fSampleWidth + fStartX;
float fSampleY = (fEndY - fStartY) * fSampleWidth + fStartY;
SDL_LockSurface(grass);
Uint32 *pixels = (Uint32*)grass->pixels;
Uint32 pixel = pixels[(int(fSampleY) * grass->pitch / 4) + int(fSampleX)];
SDL_Color color;
//SDL_LockSurface(surface2);
SDL_GetRGBA(pixel, grass->format, &color.r, &color.g, &color.b, &color.a);
SDL_UnlockSurface(grass);
//SDL_GetRGBA(pixel2, surface2->format, &color2.r, &color2.g, &color2.b, &color2.a);
if(int(color.a) == 255)
{
return 0.0f;
}
return 0.2f;
}
bool AI::CheckNewValue(int x, int y)
{
float fFarX1 = fWorldX + cosf(fWorldA - fFoVHalf) * fFar;
float fFarY1 = fWorldY + sinf(fWorldA - fFoVHalf) * fFar;
float fNearX1 = fWorldX + cosf(fWorldA - fFoVHalf) * fNear;
float fNearY1 = fWorldY + sinf(fWorldA - fFoVHalf) * fNear;
float fFarX2 = fWorldX + cosf(fWorldA + fFoVHalf) * fFar;
float fFarY2 = fWorldY + sinf(fWorldA + fFoVHalf) * fFar;
float fNearX2 = fWorldX + cosf(fWorldA + fFoVHalf) * fNear;
float fNearY2 = fWorldY + sinf(fWorldA + fFoVHalf) * fNear;
float fSampleDepth = (float)y / ((float)worldHeight / 2.0f);
float fSampleWidth = (float)x / (float)worldWidth;
float fStartX = (fFarX1 - fNearX1) / (fSampleDepth) + fNearX1;
float fStartY = (fFarY1 - fNearY1) / (fSampleDepth) + fNearY1;
float fEndX = (fFarX2 - fNearX2) / (fSampleDepth) + fNearX2;
float fEndY = (fFarY2 - fNearY2) / (fSampleDepth) + fNearY2;
float fSampleX = (fEndX - fStartX) * fSampleWidth + fStartX;
float fSampleY = (fEndY - fStartY) * fSampleWidth + fStartY;
SDL_LockSurface(blocks);
Uint32 *pixels = (Uint32*)blocks->pixels;
Uint32 pixel = pixels[(int(fSampleY) * blocks->pitch / 4) + int(fSampleX)];
SDL_Color color;
//SDL_LockSurface(surface2);
SDL_GetRGBA(pixel, blocks->format, &color.r, &color.g, &color.b, &color.a);
SDL_UnlockSurface(blocks);
//SDL_GetRGBA(pixel2, surface2->format, &color2.r, &color2.g, &color2.b, &color2.a);
if(int(color.a) == 255 || fSampleY < 0 || fSampleX < 0 || fSampleX > 1024 || fSampleY>1024)
{
return false;
}
return true;
}
void AI::draw(SDL_Renderer *renderer) const{
// Create Frustum corner points
float fFarX1 = fWorldX + cosf(fWorldA - fFoVHalf) * fFar;
float fFarY1 = fWorldY + sinf(fWorldA - fFoVHalf) * fFar;
float fNearX1 = fWorldX + cosf(fWorldA - fFoVHalf) * fNear;
float fNearY1 = fWorldY + sinf(fWorldA - fFoVHalf) * fNear;
float fFarX2 = fWorldX + cosf(fWorldA + fFoVHalf) * fFar;
float fFarY2 = fWorldY + sinf(fWorldA + fFoVHalf) * fFar;
float fNearX2 = fWorldX + cosf(fWorldA + fFoVHalf) * fNear;
float fNearY2 = fWorldY + sinf(fWorldA + fFoVHalf) * fNear;
// Starting with furthest away line and work towards the camera point
SDL_LockSurface(sky);
SDL_LockSurface(track);
Uint32 *pixels1 = (Uint32*)sky->pixels;
Uint32 *pixels2 = (Uint32*)track->pixels;
// [250*500];
//memcpy( cur_pixels, pixels, surface->pitch * surface->h );
for (int y = 0; y < worldHeight/2; y++)
{
//std::cout<<"drawing y "<<y<<std::endl;
// Take a sample point for depth linearly related to rows down screen
float fSampleDepth = (float)y / ((float)worldHeight / 2.0f);
//std::cout<<fSampleDepth<<std::endl;
if(fSampleDepth == 0)
fSampleDepth = 1;
// Use sample point in non-linear (1/x) way to enable perspective
// and grab start and end points for lines across the screen
float fStartX = (fFarX1 - fNearX1) / (fSampleDepth) + fNearX1;
float fStartY = (fFarY1 - fNearY1) / (fSampleDepth) + fNearY1;
float fEndX = (fFarX2 - fNearX2) / (fSampleDepth) + fNearX2;
float fEndY = (fFarY2 - fNearY2) / (fSampleDepth) + fNearY2;
// Linearly interpolate lines across the screen
for (int x = 0; x < worldWidth; x++)
{
//std::cout<<"drawing x "<<x<<std::endl;
float fSampleWidth = (float)x / (float)worldWidth;
float fSampleX = (fEndX - fStartX) * fSampleWidth + fStartX;
float fSampleY = (fEndY - fStartY) * fSampleWidth + fStartY;
// Wrap sample coordinates to give "infinite" periodicity on maps
//std::cout<<fSampleX<<" "<<fSampleY<<std::endl;
//fSampleX = fmod(fSampleX, 1024);
//fSampleY = fmod(fSampleY,1024);
SDL_Color color = {0,0,0,0};
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
//std::cout<<"draw color set "<<std::endl;
if(fSampleX < 0 || fSampleX > 1024 || fSampleY<0 || fSampleY > 1024)
{
SDL_Color w_color = {0,0,0,255};
SDL_SetRenderDrawColor(renderer, w_color.r, w_color.g, w_color.b, w_color.a);
SDL_RenderDrawPoint(renderer, x, y+worldHeight/2);
}
else
{
Uint32 pixel2 = pixels2[(int(fSampleY) * track->pitch / 4) + int(fSampleX)];
SDL_GetRGBA(pixel2, track->format, &color.r, &color.g, &color.b, &color.a);
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
SDL_RenderDrawPoint(renderer, x, y+worldHeight/2);
}
fSampleX = fabs(fmod(fSampleX, 1022)+1);
fSampleY = fabs(fmod(fSampleY,1022)+1);
Uint32 pixel1 = pixels1[(int(fSampleY) * sky->pitch / 4) + int(fSampleX)];
// Sample symbol and colour from map sprite, and draw the
// pixel to the screen
//SDL_Color color = getPixelColor(fSampleX,fSampleY);
SDL_GetRGBA(pixel1, sky->format, &color.r, &color.g, &color.b, &color.a);
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
//std::cout<<"draw color set "<<std::endl;
SDL_RenderDrawPoint(renderer, x, y);
}
}
SDL_UnlockSurface(track);
SDL_UnlockSurface(sky);
}