2016-11-07 08:16:18 +08:00
|
|
|
/*************************************************************************/
|
2017-09-01 22:07:55 +08:00
|
|
|
/* a_star.h */
|
2016-11-07 08:16:18 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2016-11-07 08:16:18 +08:00
|
|
|
/*************************************************************************/
|
2019-01-01 19:53:14 +08:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-11-07 08:16:18 +08: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 07:50:27 +08:00
|
|
|
|
2016-09-14 05:17:18 +08:00
|
|
|
#ifndef ASTAR_H
|
|
|
|
#define ASTAR_H
|
|
|
|
|
2019-08-16 06:22:52 +08:00
|
|
|
#include "core/oa_hash_map.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/reference.h"
|
|
|
|
|
2016-11-07 08:16:18 +08:00
|
|
|
/**
|
2017-10-28 01:19:01 +08:00
|
|
|
A* pathfinding algorithm
|
|
|
|
|
2016-11-07 08:16:18 +08:00
|
|
|
@author Juan Linietsky <reduzio@gmail.com>
|
|
|
|
*/
|
2016-09-14 05:17:18 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
class AStar : public Reference {
|
2016-09-14 05:17:18 +08:00
|
|
|
|
2019-03-20 02:35:57 +08:00
|
|
|
GDCLASS(AStar, Reference);
|
2016-09-14 05:17:18 +08:00
|
|
|
|
|
|
|
struct Point {
|
|
|
|
|
2019-08-26 03:30:52 +08:00
|
|
|
Point() :
|
|
|
|
neighbours(4u),
|
|
|
|
unlinked_neighbours(4u) {}
|
|
|
|
|
2016-09-14 05:17:18 +08:00
|
|
|
int id;
|
|
|
|
Vector3 pos;
|
2017-01-15 04:35:39 +08:00
|
|
|
real_t weight_scale;
|
2019-03-29 16:10:57 +08:00
|
|
|
bool enabled;
|
2016-09-14 05:17:18 +08:00
|
|
|
|
2019-08-16 06:22:52 +08:00
|
|
|
OAHashMap<int, Point *> neighbours;
|
|
|
|
OAHashMap<int, Point *> unlinked_neighbours;
|
2016-09-14 05:17:18 +08:00
|
|
|
|
2019-08-16 06:22:52 +08:00
|
|
|
// Used for pathfinding.
|
2016-09-14 05:17:18 +08:00
|
|
|
Point *prev_point;
|
2019-05-17 02:09:47 +08:00
|
|
|
real_t g_score;
|
|
|
|
real_t f_score;
|
|
|
|
uint64_t open_pass;
|
|
|
|
uint64_t closed_pass;
|
2016-09-14 05:17:18 +08:00
|
|
|
};
|
|
|
|
|
2019-05-17 02:09:47 +08:00
|
|
|
struct SortPoints {
|
2019-08-16 06:22:52 +08:00
|
|
|
_FORCE_INLINE_ bool operator()(const Point *A, const Point *B) const { // Returns true when the Point A is worse than Point B.
|
|
|
|
if (A->f_score > B->f_score) {
|
2019-05-17 02:09:47 +08:00
|
|
|
return true;
|
2019-08-16 06:22:52 +08:00
|
|
|
} else if (A->f_score < B->f_score) {
|
2019-05-17 02:09:47 +08:00
|
|
|
return false;
|
2019-08-16 06:22:52 +08:00
|
|
|
} else {
|
|
|
|
return A->g_score < B->g_score; // If the f_costs are the same then prioritize the points that are further away from the start.
|
|
|
|
}
|
2019-05-17 02:09:47 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-14 05:17:18 +08:00
|
|
|
struct Segment {
|
|
|
|
union {
|
|
|
|
struct {
|
2019-07-16 01:01:50 +08:00
|
|
|
int32_t u;
|
|
|
|
int32_t v;
|
2016-09-14 05:17:18 +08:00
|
|
|
};
|
|
|
|
uint64_t key;
|
|
|
|
};
|
|
|
|
|
2019-07-16 01:01:50 +08:00
|
|
|
enum {
|
|
|
|
NONE = 0,
|
|
|
|
FORWARD = 1,
|
|
|
|
BACKWARD = 2,
|
|
|
|
BIDIRECTIONAL = FORWARD | BACKWARD
|
|
|
|
};
|
|
|
|
unsigned char direction;
|
2016-09-14 05:17:18 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool operator<(const Segment &p_s) const { return key < p_s.key; }
|
2019-07-16 01:01:50 +08:00
|
|
|
Segment() {
|
|
|
|
key = 0;
|
|
|
|
direction = NONE;
|
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
Segment(int p_from, int p_to) {
|
2019-07-16 01:01:50 +08:00
|
|
|
if (p_from < p_to) {
|
|
|
|
u = p_from;
|
|
|
|
v = p_to;
|
|
|
|
direction = FORWARD;
|
|
|
|
} else {
|
|
|
|
u = p_to;
|
|
|
|
v = p_from;
|
|
|
|
direction = BACKWARD;
|
2016-09-14 05:17:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-16 06:22:52 +08:00
|
|
|
int last_free_id;
|
|
|
|
uint64_t pass;
|
|
|
|
|
|
|
|
OAHashMap<int, Point *> points;
|
2016-09-14 05:17:18 +08:00
|
|
|
Set<Segment> segments;
|
|
|
|
|
|
|
|
bool _solve(Point *begin_point, Point *end_point);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2017-03-25 17:44:41 +08:00
|
|
|
virtual float _estimate_cost(int p_from_id, int p_to_id);
|
|
|
|
virtual float _compute_cost(int p_from_id, int p_to_id);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
public:
|
2016-09-14 05:17:18 +08:00
|
|
|
int get_available_point_id() const;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale = 1);
|
2017-09-10 21:37:49 +08:00
|
|
|
Vector3 get_point_position(int p_id) const;
|
2017-10-28 01:19:01 +08:00
|
|
|
void set_point_position(int p_id, const Vector3 &p_pos);
|
2017-01-15 04:35:39 +08:00
|
|
|
real_t get_point_weight_scale(int p_id) const;
|
2017-10-28 01:19:01 +08:00
|
|
|
void set_point_weight_scale(int p_id, real_t p_weight_scale);
|
2016-09-14 05:17:18 +08:00
|
|
|
void remove_point(int p_id);
|
2017-07-11 22:04:41 +08:00
|
|
|
bool has_point(int p_id) const;
|
2017-11-03 03:42:58 +08:00
|
|
|
PoolVector<int> get_point_connections(int p_id);
|
2017-09-07 22:11:48 +08:00
|
|
|
Array get_points();
|
2016-09-14 05:17:18 +08:00
|
|
|
|
2019-03-29 16:10:57 +08:00
|
|
|
void set_point_disabled(int p_id, bool p_disabled = true);
|
|
|
|
bool is_point_disabled(int p_id) const;
|
|
|
|
|
2017-05-19 19:16:45 +08:00
|
|
|
void connect_points(int p_id, int p_with_id, bool bidirectional = true);
|
2019-07-13 11:22:12 +08:00
|
|
|
void disconnect_points(int p_id, int p_with_id, bool bidirectional = true);
|
|
|
|
bool are_points_connected(int p_id, int p_with_id, bool bidirectional = true) const;
|
2016-09-14 05:17:18 +08:00
|
|
|
|
2019-08-26 03:30:52 +08:00
|
|
|
int get_point_count() const;
|
|
|
|
int get_point_capacity() const;
|
|
|
|
void reserve_space(int p_num_nodes);
|
2016-09-14 05:17:18 +08:00
|
|
|
void clear();
|
|
|
|
|
2019-09-05 01:04:48 +08:00
|
|
|
int get_closest_point(const Vector3 &p_point, bool p_include_disabled = false) const;
|
2017-09-10 21:37:49 +08:00
|
|
|
Vector3 get_closest_position_in_segment(const Vector3 &p_point) const;
|
2016-09-14 05:17:18 +08:00
|
|
|
|
2017-01-08 05:25:37 +08:00
|
|
|
PoolVector<Vector3> get_point_path(int p_from_id, int p_to_id);
|
|
|
|
PoolVector<int> get_id_path(int p_from_id, int p_to_id);
|
2016-09-14 05:17:18 +08:00
|
|
|
|
|
|
|
AStar();
|
|
|
|
~AStar();
|
|
|
|
};
|
|
|
|
|
2019-03-20 00:15:59 +08:00
|
|
|
class AStar2D : public Reference {
|
|
|
|
GDCLASS(AStar2D, Reference);
|
|
|
|
AStar astar;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
|
|
|
int get_available_point_id() const;
|
|
|
|
|
|
|
|
void add_point(int p_id, const Vector2 &p_pos, real_t p_weight_scale = 1);
|
|
|
|
Vector2 get_point_position(int p_id) const;
|
|
|
|
void set_point_position(int p_id, const Vector2 &p_pos);
|
|
|
|
real_t get_point_weight_scale(int p_id) const;
|
|
|
|
void set_point_weight_scale(int p_id, real_t p_weight_scale);
|
|
|
|
void remove_point(int p_id);
|
|
|
|
bool has_point(int p_id) const;
|
|
|
|
PoolVector<int> get_point_connections(int p_id);
|
|
|
|
Array get_points();
|
|
|
|
|
|
|
|
void set_point_disabled(int p_id, bool p_disabled = true);
|
|
|
|
bool is_point_disabled(int p_id) const;
|
|
|
|
|
|
|
|
void connect_points(int p_id, int p_with_id, bool p_bidirectional = true);
|
|
|
|
void disconnect_points(int p_id, int p_with_id);
|
|
|
|
bool are_points_connected(int p_id, int p_with_id) const;
|
|
|
|
|
2019-08-26 03:30:52 +08:00
|
|
|
int get_point_count() const;
|
|
|
|
int get_point_capacity() const;
|
|
|
|
void reserve_space(int p_num_nodes);
|
2019-03-20 00:15:59 +08:00
|
|
|
void clear();
|
|
|
|
|
2019-09-05 01:04:48 +08:00
|
|
|
int get_closest_point(const Vector2 &p_point, bool p_include_disabled = false) const;
|
2019-03-20 00:15:59 +08:00
|
|
|
Vector2 get_closest_position_in_segment(const Vector2 &p_point) const;
|
|
|
|
|
|
|
|
PoolVector<Vector2> get_point_path(int p_from_id, int p_to_id);
|
|
|
|
PoolVector<int> get_id_path(int p_from_id, int p_to_id);
|
|
|
|
|
|
|
|
AStar2D();
|
|
|
|
~AStar2D();
|
|
|
|
};
|
|
|
|
|
2016-09-14 05:17:18 +08:00
|
|
|
#endif // ASTAR_H
|