-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
158 lines (132 loc) · 3.74 KB
/
main.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
#include "Agent.h"
#include "SFML/Graphics/RenderTarget.hpp"
#include <iostream>
#include <set>
auto cmp = [](Agent* a, Agent* b) {return a->getF() < b->getF(); };
const static float sweep = 10;
const static sf::Vector2f dirs[] = {
{-sweep, 0}, {sweep, 0},
{0, sweep}, {0, -sweep},
{-sweep, sweep}, {sweep, sweep},
{-sweep, -sweep}, {sweep, -sweep},
};
void renderAgents(const std::vector<Agent*>& v, sf::RenderWindow* win) {
for (Agent* a : v)
a->render(win);
}
bool inClosed(const Agent* agent, const std::vector<Agent*>& v) {
for (Agent* a : v)
if (*a == *agent)
return true;
return false;
}
Agent* inOpen(const Agent* agent, const std::set<Agent*, decltype(cmp)>& s) {
for (Agent* a : s)
if (*a == *agent)
return a;
return nullptr;
}
void reset(int& count, std::vector<Agent*>& states, std::vector<Agent*>& closed, std::set<Agent*, decltype(cmp)>& open) {
count = 0;
closed.clear();
states.clear();
open.clear();
}
bool inRange(float x, float y, const sf::Image& img) {
return ((x >= 0 && x < img.getSize().x) && (y >= 0 && y < img.getSize().y));
}
bool notAccessible(float x, float y, const sf::Image& img) {
return (img.getPixel(x, y) == sf::Color::Black);
}
bool AStar(std::vector<Agent*>& states, std::vector<Agent*>& closed, std::set<Agent*, decltype(cmp)>& open, int sweep, sf::RenderWindow* win, sf::Image& img, std::vector<Agent*>& path) {
if (open.size() > 0) {
Agent* curr = *open.begin();
open.erase(open.begin());
curr->setColor(sf::Color::Red);
closed.push_back(curr);
if (*curr == *states[1]) {
Agent* hold = curr;
path.push_back(states[0]);
while (hold) {
hold->setColor(sf::Color::Green);
path.push_back(hold);
hold = hold->getParent();
}
path.push_back(states[1]);
return true;
}
for (int i = 0; i < 8; i++) {
Agent* n = new Agent(*curr);
n->setPosition(curr->getPosition() + dirs[i]);
float x = n->getPosition().x, y = n->getPosition().y;
if (inClosed(n, closed) || !inRange(x, y, img) || notAccessible(x, y, img)) {
delete n;
continue;
}
Agent* iOpen;
if (!(iOpen = inOpen(n, open)) || n->getF() < curr->getF()) {
n->setG(curr->getG());
n->setH(states[1]->getPosition());
n->setParent(curr);
if (iOpen)
open.erase(iOpen);
open.insert(n);
}
}
}
return false;
}
int main() {
sf::Image img;
if (!img.loadFromFile("map.png"))
return -1;
sf::Texture tex;
tex.loadFromImage(img);
tex.setSmooth(true);
sf::Sprite sp;
sp.setTexture(tex, true);
sf::RenderWindow window(sf::VideoMode(sp.getTexture()->getSize().x, sp.getTexture()->getSize().y), "AStar");
std::vector<Agent*> agents;
std::vector<Agent*> path;
bool flag = true;
int count = 0;
std::vector<Agent*> closed;
std::set<Agent*, decltype(cmp)> open(cmp);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::MouseButtonPressed && count < 2) {
float mX = event.touch.x;
float mY = event.touch.y;
if (!notAccessible(mX, mY, img)) {
if (count == 0) {
agents.push_back(new Agent(sf::Color::Blue, sf::Vector2f(mX, mY), sweep));
open.insert(agents[0]);
}
else
agents.push_back(new Agent(sf::Color::Cyan, sf::Vector2f(mX, mY), sweep));
count++;
}
}
}
if (count == 2) {
if (path.size() > 0)
path.clear();
if (AStar(agents, closed, open, 8, &window, img, path))
reset(count, agents, closed, open);
}
if (!open.size() && count == 2)
reset(count, agents, closed, open);
window.clear();
window.draw(sp);
renderAgents(agents, &window);
if(closed.size() > 0)
renderAgents(closed, &window);
else if(path.size() > 0)
renderAgents(path, &window);
window.display();
}
return 0;
}