-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.cpp
134 lines (124 loc) · 4.14 KB
/
level.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
//======================================================================================
//Source code for cLevel class
//
//(c) Patrick Dickinson, University of Lincoln, School of Computer Science, 2020
//======================================================================================
#include "level.hpp"
#include "bots.hpp"
#include <stdio.h>
//======================================================================================
//Globals
//======================================================================================
cLevel gLevel;
//======================================================================================
//Valid if in range and position is traversable
//======================================================================================
bool cLevel::isValid(int x, int y) const
{
if ((x < 0) || (x > 39)) return false;
if ((y < 0) || (y > 39)) return false;
int m = map[x][y];
if (m != 0) return false;
return true;
}
//======================================================================================
//Constructor
//======================================================================================
cLevel::cLevel()
{
for (int i = 0; i < GRIDHEIGHT; i++)
{
for (int j = 0; j < GRIDWIDTH; j++)
{
map[i][j] = 0;
}
}
}
//======================================================================================
//Load map from ascii text file
//======================================================================================
void cLevel::Load(const char* fname)
{
for (int i = 0; i < GRIDHEIGHT; i++)
{
for (int j = 0; j < GRIDWIDTH; j++)
{
map[i][j] = 0;
}
}
FILE* file = fopen(fname, "rt");
for (int j = 0; j < GRIDHEIGHT; j++)
{
char line[128];
fread(line, 1, 41, file);
for (int i = 0; i < GRIDWIDTH; i++)
{
if (line[i] == '#') map[i][j] = 1;
else map[i][j] = 0;
}
}
fclose(file);
}
//======================================================================================
//Draw grid
//======================================================================================
void cLevel::Draw(int select_function) // the function takes one argument now to enable the user to choose between functions
{
SDL_Rect position;
position.h = position.w = 16;
for (int i = 0; i < GRIDHEIGHT; i++)
{
for (int j = 0; j < GRIDWIDTH; j++)
{
position.x = i * 16;
position.y = j * 16;
if (select_function == 1) // Manhattan distance will be drawn
{
if (map[i][j] == 0)
{
if ((gAStar.completed) && (gAStar.inPath[i][j] == true))
SDL_RenderCopy(gMainRenderer, tileRouteTexture, NULL, &position);
else if ((gAStar.completed) && (gAStar.closed[i][j] == true))
SDL_RenderCopy(gMainRenderer, tileClosedTexture, NULL, &position);
else
SDL_RenderCopy(gMainRenderer, tileTexture, NULL, &position);
}
else
SDL_RenderCopy(gMainRenderer, tileBlockedTexture, NULL, &position);
}
else if (select_function == 2) // Euclidean distance will be drawn
{
if (map[i][j] == 0)
{
if ((gAStar2.completed) && (gAStar2.inPath[i][j] == true))
SDL_RenderCopy(gMainRenderer, tileRouteTexture, NULL, &position);
else if ((gAStar2.completed) && (gAStar2.closed[i][j] == true))
SDL_RenderCopy(gMainRenderer, tileClosedTexture, NULL, &position);
else SDL_RenderCopy(gMainRenderer, tileTexture, NULL, &position);
}
else SDL_RenderCopy(gMainRenderer, tileBlockedTexture, NULL, &position);
}
else if (select_function == 3) // diagonal distance will be drawn
{
if (map[i][j] == 0)
{
if ((gAStar3.completed) && (gAStar3.inPath[i][j] == true))
SDL_RenderCopy(gMainRenderer, tileRouteTexture, NULL, &position);
else if ((gAStar3.completed) && (gAStar3.closed[i][j] == true))
SDL_RenderCopy(gMainRenderer, tileClosedTexture, NULL, &position);
else SDL_RenderCopy(gMainRenderer, tileTexture, NULL, &position);
}
else SDL_RenderCopy(gMainRenderer, tileBlockedTexture, NULL, &position);
}
else
{
if (map[i][j] == 0)
{
SDL_RenderCopy(gMainRenderer, tileTexture, NULL, &position);
}
else
SDL_RenderCopy(gMainRenderer, tileBlockedTexture, NULL, &position);
}
}
}
}