aboutsummaryrefslogtreecommitdiffstats
path: root/src/astar.cpp
blob: a8fa97016a647f86f7c9e3e81fe43186ad67da8a (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
#include "astar.hpp"

#include <cmath>

AStar::AStar() : graph(NULL), graph_width(0), graph_height(0), path_length(10) {
}

AStar::AStar(int *curr_graph, const unsigned int& width, const unsigned int& height) : graph(curr_graph), graph_width(width), graph_height(height), path_length(1) {
	for(unsigned int i = 0; i < graph_width * graph_height; ++i)
		if(graph[i] == 3) {
			start_node.x = i % graph_width;
			start_node.y = i / graph_width;
			start_node.g_score = 0;
			calc_heuristic(start_node);
			calc_f(start_node);
		} else if(graph[i] == 2) {
			end_node.x = i % graph_width;
			end_node.y = i / graph_width;
		}
	open_set.push(start_node);
	start_algorithm();
}

bool AStar::start_algorithm(int *curr_graph, const unsigned int& width, const unsigned int& height) {
	graph_width = width;
	graph_height = height;
	graph = curr_graph;

	closed_set.clear();
	open_set.clear();

	for(unsigned int i = 0; i < graph_width * graph_height; ++i)
		if(graph[i] == 3) {
			start_node.x = i % graph_width;
			start_node.y = i / graph_width;
			start_node.g_score = 0;
			calc_heuristic(start_node);
			start_node.x_prev = -1;
			start_node.y_prev = -1;
			calc_f(start_node);
		} else if(graph[i] == 2) {
			end_node.x = i % graph_width;
			end_node.y = i / graph_width;
		}
	open_set.push(start_node);
	return start_algorithm();
}

bool AStar::start_algorithm() {
	while(open_set.get_first() != end_node) {
		Node current = open_set.pop();
		closed_set.push_back(current);
		for(unsigned int i = 0; i < NEIGHBOUR_NUM; ++i) {
			Node n = get_neighbour(current, i);

			if(graph[n.y * graph_width + n.x] != 1 && n.x != -1 && n.y != -1) {
				graph[n.x + n.y * graph_width] = 9;
				graph[start_node.x + start_node.y * graph_width] = 3;
				graph[end_node.x + end_node.y * graph_width] = 2;
				int cost = current.g_score + path_length;
				if(open_set.check_item(n))
					if(cost < open_set[open_set.get_index(n)].g_score)
						open_set.remove_item(n);
				if(check_item_vec(n))
					if(cost < closed_set[get_index_vec(n)].g_score)
						remove_from_vec(n);
				if(!open_set.check_item(n) && !check_item_vec(n)) {
					n.g_score = cost;
					calc_heuristic(n);
					calc_f(n);
					n.x_prev = current.x;
					n.y_prev = current.y;
					open_set.push(n);
				}
			}
		}
	}
	recreate_path(open_set.get_first());
	return true;
}

void AStar::recreate_path(Node n) {
	while(n.x != -1 && n.y != -1) {
		graph[n.x + n.y * graph_width] = 8;
		n = find_node(n.x_prev, n.y_prev);
	}
}

Node AStar::get_neighbour(Node& n_in, const int& neighbour_num) {
	Node n;

	if(neighbour_num == 0 && n_in.y > 0) {
		n.x = n_in.x;
		n.y = n_in.y - 1;
		path_length = 10;
	} else if(neighbour_num == 1 && n_in.x < graph_width - 1) {
		n.x = n_in.x + 1;
		n.y = n_in.y;
		path_length = 10;
	} else if(neighbour_num == 2 && n_in.y < graph_height - 1) {
		n.x = n_in.x;
		n.y = n_in.y + 1;
		path_length = 10;
	} else if(neighbour_num == 3 && n_in.x > 0) {
		n.x = n_in.x - 1;
		n.y = n_in.y;
		path_length = 10;
	} else if(neighbour_num == 4 && n_in.y > 0 && n_in.x < graph_height - 1) {
		n.x = n_in.x + 1;
		n.y = n_in.y - 1;
		path_length = 14;
	} else if(neighbour_num == 5 && n_in.y < graph_height - 1 && n_in.x < graph_width - 1) {
		n.x = n_in.x + 1;
		n.y = n_in.y + 1;
		path_length = 14;
	} else if(neighbour_num == 6 && n_in.y < graph_height - 1 && n_in.x > 0) {
		n.x = n_in.x - 1;
		n.y = n_in.y + 1;
		path_length = 14;
	} else if(neighbour_num == 7 && n_in.y > 0 && n_in.x > 0) {
		n.x = n_in.x - 1;
		n.y = n_in.y - 1;
		path_length = 14;
	}

	return n;
}

void AStar::calc_heuristic(Node& n) {
	n.h_score = pow(10 * (end_node.x - n.x), 2) + pow(10 * (end_node.y - n.y), 2);
}

void AStar::calc_f(Node& n) {
	n.f_score = n.g_score + n.h_score;
}

bool AStar::check_item_vec(const Node& n) {
	for(unsigned int i = 0; i < closed_set.size(); ++i)
		if(closed_set[i] == n)
			return true;
	return false;
}

int AStar::get_index_vec(const Node& n) {
	for(unsigned int i = 0; i < closed_set.size(); ++i)
		if(closed_set[i] == n)
			return i;
	return -1;
}

void AStar::remove_from_vec(const Node& n) {
	std::vector<Node> tmp_set;
	for(unsigned int i = 0; i < closed_set.size(); ++i)
		if(closed_set[i] != n)
			tmp_set.push_back(closed_set[i]);
	closed_set = tmp_set;
}

Node AStar::find_node(const unsigned int& x, const unsigned int& y) {
	Node n;

	for(unsigned int i = 0; i < closed_set.size(); ++i)
		if(closed_set[i].x == x && closed_set[i].y == y)
			n = closed_set[i];
	return n;
}