2016-11-07 01:16:18 +01:00
|
|
|
/*************************************************************************/
|
2017-01-16 08:04:19 +01:00
|
|
|
/* a_star.cpp */
|
2016-11-07 01:16:18 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-11-07 01:16:18 +01:00
|
|
|
/*************************************************************************/
|
2019-01-01 12:53:14 +01:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-11-07 01:16:18 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2016-09-13 23:17:18 +02:00
|
|
|
#include "a_star.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
|
|
|
#include "core/math/geometry.h"
|
|
|
|
#include "core/script_language.h"
|
2017-03-25 10:44:41 +01:00
|
|
|
#include "scene/scene_string_names.h"
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
int AStar::get_available_point_id() const {
|
|
|
|
|
|
|
|
if (points.empty()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return points.back()->key() + 1;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) {
|
2017-10-27 19:19:01 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(p_id < 0);
|
2017-05-21 21:55:21 +02:00
|
|
|
ERR_FAIL_COND(p_weight_scale < 1);
|
2017-10-27 19:19:01 +02:00
|
|
|
|
2016-09-13 23:17:18 +02:00
|
|
|
if (!points.has(p_id)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *pt = memnew(Point);
|
|
|
|
pt->id = p_id;
|
|
|
|
pt->pos = p_pos;
|
|
|
|
pt->weight_scale = p_weight_scale;
|
|
|
|
pt->prev_point = NULL;
|
|
|
|
pt->last_pass = 0;
|
|
|
|
points[p_id] = pt;
|
2016-09-13 23:17:18 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
points[p_id]->pos = p_pos;
|
|
|
|
points[p_id]->weight_scale = p_weight_scale;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-10 15:37:49 +02:00
|
|
|
Vector3 AStar::get_point_position(int p_id) const {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!points.has(p_id), Vector3());
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
return points[p_id]->pos;
|
|
|
|
}
|
2017-10-27 19:19:01 +02:00
|
|
|
|
|
|
|
void AStar::set_point_position(int p_id, const Vector3 &p_pos) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!points.has(p_id));
|
|
|
|
|
|
|
|
points[p_id]->pos = p_pos;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t AStar::get_point_weight_scale(int p_id) const {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!points.has(p_id), 0);
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
return points[p_id]->weight_scale;
|
|
|
|
}
|
2017-10-27 19:19:01 +02:00
|
|
|
|
|
|
|
void AStar::set_point_weight_scale(int p_id, real_t p_weight_scale) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!points.has(p_id));
|
|
|
|
ERR_FAIL_COND(p_weight_scale < 1);
|
|
|
|
|
|
|
|
points[p_id]->weight_scale = p_weight_scale;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AStar::remove_point(int p_id) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!points.has(p_id));
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *p = points[p_id];
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2018-06-28 07:36:38 +02:00
|
|
|
for (Set<Point *>::Element *E = p->neighbours.front(); E; E = E->next()) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2018-06-28 07:36:38 +02:00
|
|
|
Segment s(p_id, E->get()->id);
|
2016-09-13 23:17:18 +02:00
|
|
|
segments.erase(s);
|
2018-06-28 07:36:38 +02:00
|
|
|
E->get()->neighbours.erase(p);
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memdelete(p);
|
|
|
|
points.erase(p_id);
|
|
|
|
}
|
|
|
|
|
2017-05-19 13:16:45 +02:00
|
|
|
void AStar::connect_points(int p_id, int p_with_id, bool bidirectional) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!points.has(p_id));
|
|
|
|
ERR_FAIL_COND(!points.has(p_with_id));
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(p_id == p_with_id);
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *a = points[p_id];
|
|
|
|
Point *b = points[p_with_id];
|
2018-06-28 07:36:38 +02:00
|
|
|
a->neighbours.insert(b);
|
2017-05-19 13:16:45 +02:00
|
|
|
|
|
|
|
if (bidirectional)
|
2018-06-28 07:36:38 +02:00
|
|
|
b->neighbours.insert(a);
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Segment s(p_id, p_with_id);
|
|
|
|
if (s.from == p_id) {
|
|
|
|
s.from_point = a;
|
|
|
|
s.to_point = b;
|
2016-09-13 23:17:18 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
s.from_point = b;
|
|
|
|
s.to_point = a;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
segments.insert(s);
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
void AStar::disconnect_points(int p_id, int p_with_id) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Segment s(p_id, p_with_id);
|
2016-09-13 23:17:18 +02:00
|
|
|
ERR_FAIL_COND(!segments.has(s));
|
|
|
|
|
|
|
|
segments.erase(s);
|
|
|
|
|
|
|
|
Point *a = points[p_id];
|
|
|
|
Point *b = points[p_with_id];
|
|
|
|
a->neighbours.erase(b);
|
|
|
|
b->neighbours.erase(a);
|
|
|
|
}
|
2017-07-11 16:04:41 +02:00
|
|
|
|
|
|
|
bool AStar::has_point(int p_id) const {
|
|
|
|
|
|
|
|
return points.has(p_id);
|
|
|
|
}
|
|
|
|
|
2017-09-07 16:11:48 +02:00
|
|
|
Array AStar::get_points() {
|
2017-10-27 19:19:01 +02:00
|
|
|
|
2017-09-07 16:11:48 +02:00
|
|
|
Array point_list;
|
|
|
|
|
|
|
|
for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) {
|
|
|
|
point_list.push_back(E->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
return point_list;
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:42:58 +01:00
|
|
|
PoolVector<int> AStar::get_point_connections(int p_id) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(!points.has(p_id), PoolVector<int>());
|
|
|
|
|
|
|
|
PoolVector<int> point_list;
|
|
|
|
|
|
|
|
Point *p = points[p_id];
|
|
|
|
|
2018-06-28 07:36:38 +02:00
|
|
|
for (Set<Point *>::Element *E = p->neighbours.front(); E; E = E->next()) {
|
|
|
|
point_list.push_back(E->get()->id);
|
2017-11-02 20:42:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return point_list;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool AStar::are_points_connected(int p_id, int p_with_id) const {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Segment s(p_id, p_with_id);
|
2016-09-13 23:17:18 +02:00
|
|
|
return segments.has(s);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AStar::clear() {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
memdelete(E->get());
|
|
|
|
}
|
|
|
|
segments.clear();
|
|
|
|
points.clear();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int AStar::get_closest_point(const Vector3 &p_point) const {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int closest_id = -1;
|
|
|
|
real_t closest_dist = 1e20;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t d = p_point.distance_squared_to(E->get()->pos);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (closest_id < 0 || d < closest_dist) {
|
|
|
|
closest_dist = d;
|
|
|
|
closest_id = E->key();
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closest_id;
|
|
|
|
}
|
2017-10-27 19:19:01 +02:00
|
|
|
|
2017-09-10 15:37:49 +02:00
|
|
|
Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t closest_dist = 1e20;
|
2017-03-05 16:44:50 +01:00
|
|
|
bool found = false;
|
2016-09-13 23:17:18 +02:00
|
|
|
Vector3 closest_point;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (const Set<Segment>::Element *E = segments.front(); E; E = E->next()) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 segment[2] = {
|
2016-09-13 23:17:18 +02:00
|
|
|
E->get().from_point->pos,
|
|
|
|
E->get().to_point->pos,
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 p = Geometry::get_closest_point_to_segment(p_point, segment);
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t d = p_point.distance_squared_to(p);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!found || d < closest_dist) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
closest_point = p;
|
|
|
|
closest_dist = d;
|
|
|
|
found = true;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closest_point;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool AStar::_solve(Point *begin_point, Point *end_point) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
pass++;
|
|
|
|
|
|
|
|
SelfList<Point>::List open_list;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool found_route = false;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2018-06-28 07:36:38 +02:00
|
|
|
for (Set<Point *>::Element *E = begin_point->neighbours.front(); E; E = E->next()) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2018-06-28 07:36:38 +02:00
|
|
|
Point *n = E->get();
|
2017-03-05 16:44:50 +01:00
|
|
|
n->prev_point = begin_point;
|
2017-05-16 13:46:13 +02:00
|
|
|
n->distance = _compute_cost(begin_point->id, n->id) * n->weight_scale;
|
2017-03-05 16:44:50 +01:00
|
|
|
n->last_pass = pass;
|
2016-09-13 23:17:18 +02:00
|
|
|
open_list.add(&n->list);
|
|
|
|
}
|
|
|
|
|
2018-08-27 18:58:22 +02:00
|
|
|
while (true) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (open_list.first() == NULL) {
|
2017-10-27 19:19:01 +02:00
|
|
|
// No path found
|
2016-09-13 23:17:18 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-10-27 19:19:01 +02:00
|
|
|
// Check open list
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2019-01-30 19:22:08 +01:00
|
|
|
SelfList<Point> *least_cost_point = open_list.first();
|
|
|
|
real_t least_cost = Math_INF;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-10-27 19:19:01 +02:00
|
|
|
// TODO: Cache previous results
|
2017-03-05 16:44:50 +01:00
|
|
|
for (SelfList<Point> *E = open_list.first(); E; E = E->next()) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *p = E->self();
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t cost = p->distance;
|
2017-03-25 10:44:41 +01:00
|
|
|
cost += _estimate_cost(p->id, end_point->id);
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (cost < least_cost) {
|
|
|
|
least_cost_point = E;
|
|
|
|
least_cost = cost;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *p = least_cost_point->self();
|
2018-08-27 18:58:22 +02:00
|
|
|
if (p == end_point) {
|
|
|
|
found_route = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2018-06-28 07:36:38 +02:00
|
|
|
for (Set<Point *>::Element *E = p->neighbours.front(); E; E = E->next()) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2018-06-28 07:36:38 +02:00
|
|
|
Point *e = E->get();
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-05-16 13:46:13 +02:00
|
|
|
real_t distance = _compute_cost(p->id, e->id) * e->weight_scale + p->distance;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (e->last_pass == pass) {
|
2017-10-27 19:19:01 +02:00
|
|
|
// Already visited, is this cheaper?
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (e->distance > distance) {
|
|
|
|
e->prev_point = p;
|
|
|
|
e->distance = distance;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-27 19:19:01 +02:00
|
|
|
// Add to open neighbours
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
e->prev_point = p;
|
|
|
|
e->distance = distance;
|
2017-10-27 19:19:01 +02:00
|
|
|
e->last_pass = pass; // Mark as used
|
2016-09-13 23:17:18 +02:00
|
|
|
open_list.add(&e->list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
open_list.remove(least_cost_point);
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:19:01 +02:00
|
|
|
// Clear the openf list
|
2017-03-05 16:44:50 +01:00
|
|
|
while (open_list.first()) {
|
|
|
|
open_list.remove(open_list.first());
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return found_route;
|
|
|
|
}
|
|
|
|
|
2017-03-25 10:44:41 +01:00
|
|
|
float AStar::_estimate_cost(int p_from_id, int p_to_id) {
|
2017-10-27 19:19:01 +02:00
|
|
|
|
2017-03-25 10:44:41 +01:00
|
|
|
if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost))
|
|
|
|
return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
|
|
|
|
|
|
|
|
return points[p_from_id]->pos.distance_to(points[p_to_id]->pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
float AStar::_compute_cost(int p_from_id, int p_to_id) {
|
2017-10-27 19:19:01 +02:00
|
|
|
|
2017-03-25 10:44:41 +01:00
|
|
|
if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost))
|
|
|
|
return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
|
|
|
|
|
|
|
|
return points[p_from_id]->pos.distance_to(points[p_to_id]->pos);
|
|
|
|
}
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!points.has(p_from_id), PoolVector<Vector3>());
|
|
|
|
ERR_FAIL_COND_V(!points.has(p_to_id), PoolVector<Vector3>());
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
pass++;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *a = points[p_from_id];
|
|
|
|
Point *b = points[p_to_id];
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (a == b) {
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Vector3> ret;
|
2016-09-13 23:17:18 +02:00
|
|
|
ret.push_back(a->pos);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *begin_point = a;
|
|
|
|
Point *end_point = b;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool found_route = _solve(begin_point, end_point);
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
if (!found_route)
|
2017-01-07 22:25:37 +01:00
|
|
|
return PoolVector<Vector3>();
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-10-27 19:19:01 +02:00
|
|
|
// Midpoints
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *p = end_point;
|
2017-10-27 19:19:01 +02:00
|
|
|
int pc = 1; // Begin point
|
2017-03-05 16:44:50 +01:00
|
|
|
while (p != begin_point) {
|
2016-09-13 23:17:18 +02:00
|
|
|
pc++;
|
2017-03-05 16:44:50 +01:00
|
|
|
p = p->prev_point;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Vector3> path;
|
2016-09-13 23:17:18 +02:00
|
|
|
path.resize(pc);
|
|
|
|
|
|
|
|
{
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Vector3>::Write w = path.write();
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *p = end_point;
|
|
|
|
int idx = pc - 1;
|
|
|
|
while (p != begin_point) {
|
|
|
|
w[idx--] = p->pos;
|
|
|
|
p = p->prev_point;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:19:01 +02:00
|
|
|
w[0] = p->pos; // Assign first
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!points.has(p_from_id), PoolVector<int>());
|
|
|
|
ERR_FAIL_COND_V(!points.has(p_to_id), PoolVector<int>());
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
pass++;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *a = points[p_from_id];
|
|
|
|
Point *b = points[p_to_id];
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (a == b) {
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<int> ret;
|
2016-09-13 23:17:18 +02:00
|
|
|
ret.push_back(a->id);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *begin_point = a;
|
|
|
|
Point *end_point = b;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool found_route = _solve(begin_point, end_point);
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
if (!found_route)
|
2017-01-07 22:25:37 +01:00
|
|
|
return PoolVector<int>();
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-10-27 19:19:01 +02:00
|
|
|
// Midpoints
|
2017-03-05 16:44:50 +01:00
|
|
|
Point *p = end_point;
|
2017-10-27 19:19:01 +02:00
|
|
|
int pc = 1; // Begin point
|
2017-03-05 16:44:50 +01:00
|
|
|
while (p != begin_point) {
|
2016-09-13 23:17:18 +02:00
|
|
|
pc++;
|
2017-03-05 16:44:50 +01:00
|
|
|
p = p->prev_point;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<int> path;
|
2016-09-13 23:17:18 +02:00
|
|
|
path.resize(pc);
|
|
|
|
|
|
|
|
{
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<int>::Write w = path.write();
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p = end_point;
|
|
|
|
int idx = pc - 1;
|
|
|
|
while (p != begin_point) {
|
|
|
|
w[idx--] = p->id;
|
|
|
|
p = p->prev_point;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:19:01 +02:00
|
|
|
w[0] = p->id; // Assign first
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AStar::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id);
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar::add_point, DEFVAL(1.0));
|
|
|
|
ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar::get_point_position);
|
2017-10-27 19:19:01 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar::set_point_position);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar::get_point_weight_scale);
|
2017-10-27 19:19:01 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar::set_point_weight_scale);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar::remove_point);
|
2017-07-11 16:04:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar::has_point);
|
2017-09-07 16:11:48 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_points"), &AStar::get_points);
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-12-25 15:19:56 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar::get_point_connections);
|
2017-11-02 20:42:58 +01:00
|
|
|
|
2017-06-15 18:49:40 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar::connect_points, DEFVAL(true));
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id"), &AStar::disconnect_points);
|
|
|
|
ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id"), &AStar::are_points_connected);
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &AStar::clear);
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_closest_point", "to_position"), &AStar::get_closest_point);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar::get_closest_position_in_segment);
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar::get_id_path);
|
2017-03-25 10:44:41 +01:00
|
|
|
|
2017-12-10 15:10:54 +01:00
|
|
|
BIND_VMETHOD(MethodInfo(Variant::REAL, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
|
|
|
|
BIND_VMETHOD(MethodInfo(Variant::REAL, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AStar::AStar() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pass = 1;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AStar::~AStar() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pass = 1;
|
2017-10-23 00:37:02 +02:00
|
|
|
clear();
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|