-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevel.cpp
executable file
·282 lines (240 loc) · 8.71 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
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
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "utils.h"
#include "Level.h"
#include "Pacman.h"
#include "Ghost.h"
#include "Sprites.h"
#include "Ball.h"
// Singleton design pattern
Level* Level::instance()
{
static Level uniqueInstance;
return &uniqueInstance;
}
// utility function
void extractNodes(cv::Mat binary_image, std::vector <QPoint> & nodes, int expected_number = -1)
{
int original_size = int(nodes.size());
std::vector < std::vector <cv::Point> > contours;
cv::findContours(binary_image, contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
for(auto & c: contours)
{
if(cv::contourArea(c) == 1)
{
cv::Point topleft = cv::boundingRect(c).tl();
nodes.push_back(QPoint(topleft.x, topleft.y));
}
}
if(expected_number >= 0 && (nodes.size()-original_size) != expected_number)
printf("warning: expected %d nodes to extract, found %d\n", expected_number, (nodes.size()-original_size));
}
int extractAndMatchNode(cv::Mat binary_image, const std::vector <QPoint> & nodes)
{
std::vector <QPoint> node;
extractNodes(binary_image, node, 1);
for(int i=0; i<nodes.size(); i++)
if(nodes[i] == node[0])
return i;
return -1;
}
// create graph nodes and relations (neighbors) automatically
// by image-processing the given image
void Level::loadGraph(const std::string & imgPath, QGraphicsScene *scene)
{
// clear graph structures
_nodes.clear();
_neighbors.clear();
_nodeTypes.clear();
// load image
cv::Mat graph_img = qimread(imgPath.c_str());
if(!graph_img.data)
printf("warning: cannot read image at \"%s\"", imgPath.c_str());
cv::cvtColor(graph_img, graph_img, cv::COLOR_BGR2RGB);
// --> to gray for all nodes extraction
cv::Mat graph_img_gray;
cv::cvtColor(graph_img, graph_img_gray, cv::COLOR_RGB2GRAY);
// binarize image: node pixels have intensity > 0
cv::Mat graph_img_bin;
cv::threshold(graph_img_gray, graph_img_bin, 1, 255, cv::THRESH_BINARY);
// extract nodes by connected component analysis (select area = 1 points)
extractNodes(graph_img_bin, _nodes);
// initialize node types with default values
_nodeTypes.resize(_nodes.size());
for(auto & nt : _nodeTypes)
nt = CHECKPOINT;
// extract entity spawn nodes
// -- Pacman
cv::inRange(graph_img, cv::Scalar(COLOR_PACMAN), cv::Scalar(COLOR_PACMAN), graph_img_bin);
_pacmanSpawnNode = extractAndMatchNode(graph_img_bin, _nodes);
// -- Blinky
cv::inRange(graph_img, cv::Scalar(COLOR_BLINKY), cv::Scalar(COLOR_BLINKY), graph_img_bin);
_ghostSpawnNode[Ghost::BLINKY] = extractAndMatchNode(graph_img_bin, _nodes);
// -- Pinky
cv::inRange(graph_img, cv::Scalar(COLOR_PINKY), cv::Scalar(COLOR_PINKY), graph_img_bin);
_ghostSpawnNode[Ghost::PINKY] = extractAndMatchNode(graph_img_bin, _nodes);
// -- Inky
cv::inRange(graph_img, cv::Scalar(COLOR_INKY), cv::Scalar(COLOR_INKY), graph_img_bin);
_ghostSpawnNode[Ghost::INKY] = extractAndMatchNode(graph_img_bin, _nodes);
// -- Clyde
cv::inRange(graph_img, cv::Scalar(COLOR_CLYDE), cv::Scalar(COLOR_CLYDE), graph_img_bin);
_ghostSpawnNode[Ghost::CLYDE] = extractAndMatchNode(graph_img_bin, _nodes);
// extract entrance node
cv::inRange(graph_img, cv::Scalar(COLOR_ENTRANCE), cv::Scalar(COLOR_ENTRANCE), graph_img_bin);
_houseEntranceNode = extractAndMatchNode(graph_img_bin, _nodes);
// update node types
for(int i=0; i<4; i++)
_nodeTypes[_ghostSpawnNode[i]] = SPAWN;
_nodeTypes[_houseEntranceNode] = ENTRANCE;
// extract edges candidates
_neighbors.resize(_nodes.size());
for(int i = 0; i < _nodes.size(); i++)
{
// reset neighbors
for(int n = 0; n < 4; n++)
_neighbors[i][n] = -1;
for(int j = 0; j < _nodes.size(); j++)
{
// test RIGHT neighbor candidate
if(i != j &&
_nodes[i].y() == _nodes[j].y() &&
_nodes[i].x() < _nodes[j].x() &&
(_neighbors[i][RIGHT] < 0 || _nodes[j].x() < _nodes[_neighbors[i][RIGHT]].x()))
_neighbors[i][RIGHT] = j;
// test LEFT neighbor candidate
if(i != j &&
_nodes[i].y() == _nodes[j].y() &&
_nodes[i].x() > _nodes[j].x() &&
(_neighbors[i][LEFT] < 0 || _nodes[j].x() > _nodes[_neighbors[i][LEFT]].x()))
_neighbors[i][LEFT] = j;
// test UP neighbor candidate
if(i != j &&
_nodes[i].x() == _nodes[j].x() &&
_nodes[i].y() > _nodes[j].y() &&
(_neighbors[i][UP] < 0 || _nodes[j].y() > _nodes[_neighbors[i][UP]].y()))
_neighbors[i][UP] = j;
// test DOWN neighbor candidate
if(i != j &&
_nodes[i].x() == _nodes[j].x() &&
_nodes[i].y() < _nodes[j].y() &&
(_neighbors[i][DOWN] < 0 || _nodes[j].y() < _nodes[_neighbors[i][DOWN]].y()))
_neighbors[i][DOWN] = j;
}
}
// filter edges candidates by wall non-traversing
for(int i = 0; i < _neighbors.size(); i++)
for(int n = 0; n < 4; n++)
if(_neighbors[i][n] >= 0)
{
cv::LineIterator it(graph_img_gray, cv::Point(_nodes[i].x(), _nodes[i].y()), cv::Point(_nodes[_neighbors[i][n]].x(), _nodes[_neighbors[i][n]].y()), 4);
for(int k = 0; k < it.count; k++, ++it)
if(**it == COLOR_WALL)
{
_neighbors[i][n] = -1;
break;
}
}
// find teleporting nodes (horizontal direction only)
for(int i = 0; i < _nodes.size(); i++)
{
// no neighbor to the left
if(_neighbors[i][LEFT] < 0)
{
// search wall on the left
bool hasWall = false;
cv::LineIterator it(graph_img_gray, cv::Point(_nodes[i].x(), _nodes[i].y()), cv::Point(0, _nodes[i].y()), 4);
for(int k = 0; k < it.count && !hasWall; k++, ++it)
if(**it == COLOR_WALL)
hasWall = true;
// wall found -> search for node w/o wall on the right
// on the same row
if(!hasWall)
{
for(int j = 0; j < _nodes.size(); j++)
if( i != j && _neighbors[j][RIGHT] < 0 && _nodes[i].y() == _nodes[j].y())
{
cv::LineIterator it(graph_img_gray, cv::Point(_nodes[j].x(), _nodes[j].y()), cv::Point(scene->sceneRect().right(), _nodes[j].y()), 4);
for(int k = 0; k < it.count && !hasWall; k++, ++it)
if(**it == COLOR_WALL)
hasWall = true;
if(!hasWall)
{
_neighbors[i][LEFT] = j;
_neighbors[j][RIGHT] = i;
_nodeTypes[i] = TELEPORT;
_nodeTypes[j] = TELEPORT;
}
}
}
}
}
// visual debug: add graph edges
for(int i = 0; i < _neighbors.size(); i++)
for(int n = 0; n < 4; n++)
if(_neighbors[i][n] >= 0 && !(isTeleport(i) && isTeleport(_neighbors[i][n])))
_graph.push_back(scene->addLine(_nodes[i].x()+1, _nodes[i].y()+1, _nodes[_neighbors[i][n]].x()+1, _nodes[_neighbors[i][n]].y()+1, QPen(QBrush(QColor(COLOR_GRAPH)), 0.5)));
// visual debug: add graph nodes
for(int i=0; i<_nodes.size(); i++)
{
QColor color(255, 255, 255);
if(_nodeTypes[i] == ENTRANCE)
color = QColor(COLOR_ENTRANCE);
else if(_nodeTypes[i] == TELEPORT)
color = QColor(COLOR_TELEPORT);
else if(i == _pacmanSpawnNode)
color = QColor(COLOR_PACMAN);
else if(i == _ghostSpawnNode[Ghost::BLINKY])
color = QColor(COLOR_BLINKY);
else if(i == _ghostSpawnNode[Ghost::INKY])
color = QColor(COLOR_INKY);
else if(i == _ghostSpawnNode[Ghost::PINKY])
color = QColor(COLOR_PINKY);
else if(i == _ghostSpawnNode[Ghost::CLYDE])
color = QColor(COLOR_CLYDE);
_graph.push_back(scene->addRect(_nodes[i].x(), _nodes[i].y(), 2, 2, Qt::NoPen, QBrush(color)));
}
}
// create balls automatically
// by image-processing the given image
void Level::loadBalls(const std::string & imgPath, QGraphicsScene* scene)
{
// load image
cv::Mat balls_img = qimread(imgPath.c_str());
if(!balls_img.data)
printf("warning: cannot read image at \"%s\"", imgPath.c_str());
cv::cvtColor(balls_img, balls_img, cv::COLOR_BGR2RGB);
// extract small and big balls
cv::inRange(balls_img, cv::Scalar(COLOR_BALL), cv::Scalar(COLOR_BALL), balls_img);
std::vector < std::vector <cv::Point> > contours;
cv::findContours(balls_img, contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
for(auto & c: contours)
{
cv::Point topleft = cv::boundingRect(c).tl();
scene->addItem(new Ball(QPoint(topleft.x, topleft.y), cv::contourArea(c) > 1));
}
}
// load all items from the given map in the given scene
Pacman* Level::load(std::string map_name, QGraphicsScene* scene)
{
_graph.clear();
// load map and setup scene
scene->setBackgroundBrush(QBrush(Qt::black));
QPixmap map = loadTexture(":/graphics/maps/" + map_name + "-walls.png");
scene->addPixmap(map);
scene->setSceneRect(0, -3*8, map.width(), map.height()+5*8); // leave vertical space for scores
// load graph
loadGraph(":/graphics/maps/" + map_name + "-nodes.png", scene);
// load balls
loadBalls(":/graphics/maps/" + map_name + "-balls.png", scene);
// make graph not visible by default
toogleGraph();
// create pacman
Pacman* pacman = new Pacman();
scene->addItem(pacman);
// create ghosts
Ghost* blinky = new Ghost(Ghost::BLINKY, pacman);
scene->addItem(blinky);
return pacman;
}