2016-11-07 01:16:18 +01:00
|
|
|
/**************************************************************************/
|
2017-09-01 16:07:55 +02:00
|
|
|
/* a_star.h */
|
2016-11-07 01:16:18 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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
|
|
|
|
2020-03-25 11:10:34 +01:00
|
|
|
#ifndef A_STAR_H
|
|
|
|
#define A_STAR_H
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2021-08-22 03:52:44 +02:00
|
|
|
#include "core/object/gdvirtual.gen.inc"
|
2021-06-04 18:03:15 +02:00
|
|
|
#include "core/object/ref_counted.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/templates/oa_hash_map.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
2016-11-07 01:16:18 +01:00
|
|
|
/**
|
2022-01-04 20:26:22 +01:00
|
|
|
A* pathfinding algorithm.
|
2016-11-07 01:16:18 +01:00
|
|
|
*/
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2022-03-20 15:20:32 +01:00
|
|
|
class AStar3D : public RefCounted {
|
|
|
|
GDCLASS(AStar3D, RefCounted);
|
2020-03-14 08:22:34 +01:00
|
|
|
friend class AStar2D;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
struct Point {
|
2020-05-12 17:01:17 +02:00
|
|
|
Point() {}
|
2019-08-25 21:30:52 +02:00
|
|
|
|
2022-06-16 10:56:12 +02:00
|
|
|
int64_t id = 0;
|
2016-09-13 23:17:18 +02:00
|
|
|
Vector3 pos;
|
2020-11-23 17:38:46 +01:00
|
|
|
real_t weight_scale = 0;
|
|
|
|
bool enabled = false;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2023-01-21 12:25:29 +01:00
|
|
|
OAHashMap<int64_t, Point *> neighbors = 4u;
|
2022-06-16 10:56:12 +02:00
|
|
|
OAHashMap<int64_t, Point *> unlinked_neighbours = 4u;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2019-08-16 00:22:52 +02:00
|
|
|
// Used for pathfinding.
|
2020-11-23 17:38:46 +01:00
|
|
|
Point *prev_point = nullptr;
|
|
|
|
real_t g_score = 0;
|
|
|
|
real_t f_score = 0;
|
|
|
|
uint64_t open_pass = 0;
|
|
|
|
uint64_t closed_pass = 0;
|
2024-02-07 05:20:13 +01:00
|
|
|
|
|
|
|
// Used for getting closest_point_of_last_pathing_call.
|
|
|
|
real_t abs_g_score = 0;
|
|
|
|
real_t abs_f_score = 0;
|
2016-09-13 23:17:18 +02:00
|
|
|
};
|
|
|
|
|
2019-05-16 20:09:47 +02:00
|
|
|
struct SortPoints {
|
2019-08-16 00:22:52 +02: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-16 20:09:47 +02:00
|
|
|
return true;
|
2019-08-16 00:22:52 +02:00
|
|
|
} else if (A->f_score < B->f_score) {
|
2019-05-16 20:09:47 +02:00
|
|
|
return false;
|
2019-08-16 00:22:52 +02: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-16 20:09:47 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-13 23:17:18 +02:00
|
|
|
struct Segment {
|
2022-06-16 10:56:12 +02:00
|
|
|
Pair<int64_t, int64_t> key;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2019-07-15 19:01:50 +02:00
|
|
|
enum {
|
|
|
|
NONE = 0,
|
|
|
|
FORWARD = 1,
|
|
|
|
BACKWARD = 2,
|
|
|
|
BIDIRECTIONAL = FORWARD | BACKWARD
|
|
|
|
};
|
2020-05-12 17:01:17 +02:00
|
|
|
unsigned char direction = NONE;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2022-05-19 17:00:06 +02:00
|
|
|
static uint32_t hash(const Segment &p_seg) {
|
2022-06-16 10:56:12 +02:00
|
|
|
return PairHash<int64_t, int64_t>().hash(p_seg.key);
|
2022-05-19 17:00:06 +02:00
|
|
|
}
|
|
|
|
bool operator==(const Segment &p_s) const { return key == p_s.key; }
|
2020-05-12 17:01:17 +02:00
|
|
|
|
|
|
|
Segment() {}
|
2022-06-16 10:56:12 +02:00
|
|
|
Segment(int64_t p_from, int64_t p_to) {
|
2019-07-15 19:01:50 +02:00
|
|
|
if (p_from < p_to) {
|
2022-06-16 10:56:12 +02:00
|
|
|
key.first = p_from;
|
|
|
|
key.second = p_to;
|
2019-07-15 19:01:50 +02:00
|
|
|
direction = FORWARD;
|
|
|
|
} else {
|
2022-06-16 10:56:12 +02:00
|
|
|
key.first = p_to;
|
|
|
|
key.second = p_from;
|
2019-07-15 19:01:50 +02:00
|
|
|
direction = BACKWARD;
|
2016-09-13 23:17:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-16 10:56:12 +02:00
|
|
|
int64_t last_free_id = 0;
|
2020-05-12 17:01:17 +02:00
|
|
|
uint64_t pass = 1;
|
2019-08-16 00:22:52 +02:00
|
|
|
|
2022-06-16 10:56:12 +02:00
|
|
|
OAHashMap<int64_t, Point *> points;
|
2022-05-19 17:00:06 +02:00
|
|
|
HashSet<Segment, Segment> segments;
|
2024-02-07 05:20:13 +01:00
|
|
|
Point *last_closest_point = nullptr;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
|
|
|
bool _solve(Point *begin_point, Point *end_point);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2024-05-19 16:08:36 +02:00
|
|
|
virtual real_t _estimate_cost(int64_t p_from_id, int64_t p_end_id);
|
2022-06-16 10:56:12 +02:00
|
|
|
virtual real_t _compute_cost(int64_t p_from_id, int64_t p_to_id);
|
2017-03-25 10:44:41 +01:00
|
|
|
|
2021-08-22 03:52:44 +02:00
|
|
|
GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
|
|
|
|
GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)
|
|
|
|
|
2024-02-07 05:20:13 +01:00
|
|
|
#ifndef DISABLE_DEPRECATED
|
|
|
|
Vector<int64_t> _get_id_path_bind_compat_88047(int64_t p_from_id, int64_t p_to_id);
|
|
|
|
Vector<Vector3> _get_point_path_bind_compat_88047(int64_t p_from_id, int64_t p_to_id);
|
|
|
|
static void _bind_compatibility_methods();
|
|
|
|
#endif
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
public:
|
2022-06-16 10:56:12 +02:00
|
|
|
int64_t get_available_point_id() const;
|
|
|
|
|
|
|
|
void add_point(int64_t p_id, const Vector3 &p_pos, real_t p_weight_scale = 1);
|
|
|
|
Vector3 get_point_position(int64_t p_id) const;
|
|
|
|
void set_point_position(int64_t p_id, const Vector3 &p_pos);
|
|
|
|
real_t get_point_weight_scale(int64_t p_id) const;
|
|
|
|
void set_point_weight_scale(int64_t p_id, real_t p_weight_scale);
|
|
|
|
void remove_point(int64_t p_id);
|
|
|
|
bool has_point(int64_t p_id) const;
|
|
|
|
Vector<int64_t> get_point_connections(int64_t p_id);
|
2022-08-05 03:41:48 +02:00
|
|
|
PackedInt64Array get_point_ids();
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2022-06-16 10:56:12 +02:00
|
|
|
void set_point_disabled(int64_t p_id, bool p_disabled = true);
|
|
|
|
bool is_point_disabled(int64_t p_id) const;
|
2019-03-29 09:10:57 +01:00
|
|
|
|
2022-06-16 10:56:12 +02:00
|
|
|
void connect_points(int64_t p_id, int64_t p_with_id, bool bidirectional = true);
|
|
|
|
void disconnect_points(int64_t p_id, int64_t p_with_id, bool bidirectional = true);
|
|
|
|
bool are_points_connected(int64_t p_id, int64_t p_with_id, bool bidirectional = true) const;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2022-06-16 10:56:12 +02:00
|
|
|
int64_t get_point_count() const;
|
|
|
|
int64_t get_point_capacity() const;
|
|
|
|
void reserve_space(int64_t p_num_nodes);
|
2016-09-13 23:17:18 +02:00
|
|
|
void clear();
|
|
|
|
|
2022-06-16 10:56:12 +02:00
|
|
|
int64_t get_closest_point(const Vector3 &p_point, bool p_include_disabled = false) const;
|
2017-09-10 15:37:49 +02:00
|
|
|
Vector3 get_closest_position_in_segment(const Vector3 &p_point) const;
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2024-02-07 05:20:13 +01:00
|
|
|
Vector<Vector3> get_point_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path = false);
|
|
|
|
Vector<int64_t> get_id_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path = false);
|
2016-09-13 23:17:18 +02:00
|
|
|
|
2022-03-20 15:20:32 +01:00
|
|
|
AStar3D() {}
|
|
|
|
~AStar3D();
|
2016-09-13 23:17:18 +02:00
|
|
|
};
|
|
|
|
|
2021-06-04 18:03:15 +02:00
|
|
|
class AStar2D : public RefCounted {
|
|
|
|
GDCLASS(AStar2D, RefCounted);
|
2022-03-20 15:20:32 +01:00
|
|
|
AStar3D astar;
|
2019-03-19 17:15:59 +01:00
|
|
|
|
2022-03-20 15:20:32 +01:00
|
|
|
bool _solve(AStar3D::Point *begin_point, AStar3D::Point *end_point);
|
2020-03-14 08:22:34 +01:00
|
|
|
|
2019-03-19 17:15:59 +01:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2024-05-19 16:08:36 +02:00
|
|
|
virtual real_t _estimate_cost(int64_t p_from_id, int64_t p_end_id);
|
2022-06-16 10:56:12 +02:00
|
|
|
virtual real_t _compute_cost(int64_t p_from_id, int64_t p_to_id);
|
2020-03-14 08:22:34 +01:00
|
|
|
|
2021-08-22 03:52:44 +02:00
|
|
|
GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
|
|
|
|
GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)
|
|
|
|
|
2024-02-07 05:20:13 +01:00
|
|
|
#ifndef DISABLE_DEPRECATED
|
|
|
|
Vector<int64_t> _get_id_path_bind_compat_88047(int64_t p_from_id, int64_t p_to_id);
|
|
|
|
Vector<Vector2> _get_point_path_bind_compat_88047(int64_t p_from_id, int64_t p_to_id);
|
|
|
|
static void _bind_compatibility_methods();
|
|
|
|
#endif
|
|
|
|
|
2019-03-19 17:15:59 +01:00
|
|
|
public:
|
2022-06-16 10:56:12 +02:00
|
|
|
int64_t get_available_point_id() const;
|
|
|
|
|
|
|
|
void add_point(int64_t p_id, const Vector2 &p_pos, real_t p_weight_scale = 1);
|
|
|
|
Vector2 get_point_position(int64_t p_id) const;
|
|
|
|
void set_point_position(int64_t p_id, const Vector2 &p_pos);
|
|
|
|
real_t get_point_weight_scale(int64_t p_id) const;
|
|
|
|
void set_point_weight_scale(int64_t p_id, real_t p_weight_scale);
|
|
|
|
void remove_point(int64_t p_id);
|
|
|
|
bool has_point(int64_t p_id) const;
|
|
|
|
Vector<int64_t> get_point_connections(int64_t p_id);
|
2022-08-05 03:41:48 +02:00
|
|
|
PackedInt64Array get_point_ids();
|
2019-03-19 17:15:59 +01:00
|
|
|
|
2022-06-16 10:56:12 +02:00
|
|
|
void set_point_disabled(int64_t p_id, bool p_disabled = true);
|
|
|
|
bool is_point_disabled(int64_t p_id) const;
|
2019-03-19 17:15:59 +01:00
|
|
|
|
2022-06-16 10:56:12 +02:00
|
|
|
void connect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true);
|
|
|
|
void disconnect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true);
|
|
|
|
bool are_points_connected(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true) const;
|
2019-03-19 17:15:59 +01:00
|
|
|
|
2022-06-16 10:56:12 +02:00
|
|
|
int64_t get_point_count() const;
|
|
|
|
int64_t get_point_capacity() const;
|
|
|
|
void reserve_space(int64_t p_num_nodes);
|
2019-03-19 17:15:59 +01:00
|
|
|
void clear();
|
|
|
|
|
2022-06-16 10:56:12 +02:00
|
|
|
int64_t get_closest_point(const Vector2 &p_point, bool p_include_disabled = false) const;
|
2019-03-19 17:15:59 +01:00
|
|
|
Vector2 get_closest_position_in_segment(const Vector2 &p_point) const;
|
|
|
|
|
2024-02-07 05:20:13 +01:00
|
|
|
Vector<Vector2> get_point_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path = false);
|
|
|
|
Vector<int64_t> get_id_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path = false);
|
2019-03-19 17:15:59 +01:00
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
AStar2D() {}
|
|
|
|
~AStar2D() {}
|
2019-03-19 17:15:59 +01:00
|
|
|
};
|
|
|
|
|
2020-03-25 11:10:34 +01:00
|
|
|
#endif // A_STAR_H
|