summaryrefslogtreecommitdiff
path: root/src/path_finder.cpp
blob: 8a61cc67c610a8cc1a7e3ceb18bf4e18e0114fbd (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
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
/*
This file is part of Minitrem.

Minitrem is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

Minitrem is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Minitrem.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.hpp"

namespace world {

static const v2f_t path_margin(5, 5);

// The path finder will try to walk in the eight directions listed below.
static const tile_index_t path_offsets[8] = {
	{+1, 0}, {+1, +1}, {0, +1}, {-1, +1},
	{-1, 0}, {-1, -1}, {0, -1}, {+1, -1}
};

path_finder_t::~path_finder_t()
{
	delete[] nodes;
}

void path_finder_t::setup_nodes(v2f_t src_, v2f_t dst_, cflags_t cflags_)
{
	rectf_t src_margin, dst_margin;
	tile_index_t end;

	src = src_;
	dst = dst_;
	cflags = cflags_;

	tile_center = v2f_t(0.5, 0.5);
	shortest_dist = INFINITY;

	src_margin[0] = src - path_margin;
	src_margin[1] = src + path_margin;
	dst_margin[0] = dst - path_margin;
	dst_margin[1] = dst + path_margin;
	bounds = src_margin | dst_margin;

	base = tile_index_at(bounds[0]);
	end = tile_index_at(bounds[1]);

	width = end[0] - base[0] + 1;
	height = end[1] - base[1] + 1;

	nodes = new path_node_t[width * height];
	for (size_t i = 0; i < width * height; i++) {
		nodes[i].accessible = true;
		nodes[i].dist = INFINITY;
	}
}

void path_finder_t::eliminate_nodes(rectf_t bounds)
{
	rect_t<coord_t, 2> index_bounds;
	tile_index_t index;

	bounds[0] -= tile_center;
	bounds[1] -= tile_center;

	index_bounds[0] = tile_index_t(bounds[0].ceil()) - base;
	index_bounds[1] = tile_index_t(bounds[1].floor()) - base;

	if (index_bounds[0][0] < 0)
		index_bounds[0][0] = 0;
	if (index_bounds[0][1] < 0)
		index_bounds[0][1] = 0;
	if (index_bounds[1][0] >= (coord_t)width)
		index_bounds[1][0] = width - 1;
	if (index_bounds[1][1] >= (coord_t)height)
		index_bounds[1][1] = height - 1;

	for (index[1] = index_bounds[0][1]; index[1] <= index_bounds[1][1]; index[1]++)
	for (index[0] = index_bounds[0][0]; index[0] <= index_bounds[1][0]; index[0]++) {
		path_node_t *node;

		node = nodes + index[1] * width + index[0];
		node->accessible = false;
	}
}

path_node_t *path_finder_t::node_at(tile_index_t index)
{
	return nodes + index[1] * width + index[0];
}

bool path_finder_t::is_accessible(tile_index_t index)
{
	if (index[0] < 0 || index[1] < 0 ||
	    index[0] >= (coord_t)width || index[1] >= (coord_t)height)
		return false;

	return node_at(index)->accessible;
}

// Walking diagonally requires an additional accessibility test. The tables
// below list when it's needed and which tile is to be tested. For example,
// when walking north-west, the north and the west neighbors have to be tested
// for accessibility.
bool path_finder_t::diagonal_test(tile_index_t index, size_t i)
{
	static const bool do_test[8] = {
		false, true, false, true, false, true, false, true
	};
	static const tile_index_t offsets[8][2] = {
		{},
		{{+1, 0}, {0, +1}},
		{},
		{{0, +1}, {-1, 0}},
		{},
		{{-1, 0}, {0, -1}},
		{},
		{{0, -1}, {+1, 0}}
	};

	if (!do_test[i])
		return true;

	for (size_t j = 0; j < 2; j++)
		if (!is_accessible(index + offsets[i][j]))
			return false;

	return true;
}

void path_finder_t::find_r(tile_index_t index, float dist, float limit)
{
	path_node_t *node;
	float dist_to_dst;

	node = nodes + index[1] * width + index[0];

	if (!node->accessible)
		return;
	if (node->dist <= dist)
		return;

	path.push_back(index);
	node->dist = dist;

	dist_to_dst = (v2f_t(base + index) + tile_center - dst).len();
	if (dist_to_dst < 1.0f && dist + dist_to_dst < shortest_dist) {
		shortest_path = path;
		shortest_dist = dist + dist_to_dst;
	}

	for (size_t i = 0; i < 8; i++) {
		tile_index_t offset, next;

		offset = path_offsets[i];
		next = index + offset;

		if (!is_accessible(next))
			continue;

		if (!diagonal_test(index, i))
			continue;

		if (dist + v2f_t(offset).len() > limit)
			continue;

		find_r(next, dist + v2f_t(offset).len(), limit);
	}

	path.pop_back();
}

bool path_finder_t::find(void)
{
	tile_index_t start;
	float dist_simple;

	dist_simple = (src - dst).len();

	if (dist_simple < 0.5f)
		return true;

	start = tile_index_at(src) - base;
	node_at(start)->accessible = false;

	for (size_t i = 0; i < 8; i++) {
		tile_index_t next;
		v2f_t offset;

		next = start + path_offsets[i];

		if (!is_accessible(next))
			continue;

		if (!diagonal_test(start, i))
			continue;

		offset = (src - v2f_t(base)) - v2f_t(next) - tile_center;
		find_r(next, v2f_t(offset).len(), dist_simple * 3);
	}

	return shortest_path.size() > 0;
}

void path_finder_t::export_path(std::list<v2f_t> *list)
{
	list->clear();

	for (tile_index_t &index : shortest_path)
		list->push_back(v2f_t(index + base) + tile_center);

	list->push_back(dst);
}

} // namespace world