2016-11-07 00:16:18 +00:00
|
|
|
/*************************************************************************/
|
2017-09-01 14:07:55 +00:00
|
|
|
/* a_star.h */
|
2016-11-07 00:16:18 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-11-07 00:16:18 +00:00
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-11-07 00:16:18 +00: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-04 23:50:27 +00:00
|
|
|
|
2016-09-13 21:17:18 +00:00
|
|
|
#ifndef ASTAR_H
|
|
|
|
#define ASTAR_H
|
|
|
|
|
|
|
|
#include "reference.h"
|
|
|
|
#include "self_list.h"
|
2016-11-07 00:16:18 +00:00
|
|
|
/**
|
2017-10-27 17:19:01 +00:00
|
|
|
A* pathfinding algorithm
|
|
|
|
|
2016-11-07 00:16:18 +00:00
|
|
|
@author Juan Linietsky <reduzio@gmail.com>
|
|
|
|
*/
|
2016-09-13 21:17:18 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
class AStar : public Reference {
|
2016-09-13 21:17:18 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
GDCLASS(AStar, Reference)
|
2016-09-13 21:17:18 +00:00
|
|
|
|
|
|
|
uint64_t pass;
|
|
|
|
|
|
|
|
struct Point {
|
|
|
|
|
|
|
|
SelfList<Point> list;
|
|
|
|
|
|
|
|
int id;
|
|
|
|
Vector3 pos;
|
2017-01-14 20:35:39 +00:00
|
|
|
real_t weight_scale;
|
2016-09-13 21:17:18 +00:00
|
|
|
uint64_t last_pass;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<Point *> neighbours;
|
2016-09-13 21:17:18 +00:00
|
|
|
|
2017-10-27 17:19:01 +00:00
|
|
|
// Used for pathfinding
|
2016-09-13 21:17:18 +00:00
|
|
|
Point *prev_point;
|
2017-01-14 20:35:39 +00:00
|
|
|
real_t distance;
|
2016-09-13 21:17:18 +00:00
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
Point() :
|
|
|
|
list(this) {}
|
2016-09-13 21:17:18 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<int, Point *> points;
|
2016-09-13 21:17:18 +00:00
|
|
|
|
|
|
|
struct Segment {
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
int32_t from;
|
|
|
|
int32_t to;
|
|
|
|
};
|
|
|
|
uint64_t key;
|
|
|
|
};
|
|
|
|
|
|
|
|
Point *from_point;
|
|
|
|
Point *to_point;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool operator<(const Segment &p_s) const { return key < p_s.key; }
|
|
|
|
Segment() { key = 0; }
|
|
|
|
Segment(int p_from, int p_to) {
|
2016-09-13 21:17:18 +00:00
|
|
|
if (p_from > p_to) {
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(p_from, p_to);
|
2016-09-13 21:17:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
from = p_from;
|
|
|
|
to = p_to;
|
2016-09-13 21:17:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Set<Segment> segments;
|
|
|
|
|
|
|
|
bool _solve(Point *begin_point, Point *end_point);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2017-03-25 09:44:41 +00: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 15:44:50 +00:00
|
|
|
public:
|
2016-09-13 21:17:18 +00:00
|
|
|
int get_available_point_id() const;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale = 1);
|
2017-09-10 13:37:49 +00:00
|
|
|
Vector3 get_point_position(int p_id) const;
|
2017-10-27 17:19:01 +00:00
|
|
|
void set_point_position(int p_id, const Vector3 &p_pos);
|
2017-01-14 20:35:39 +00:00
|
|
|
real_t get_point_weight_scale(int p_id) const;
|
2017-10-27 17:19:01 +00:00
|
|
|
void set_point_weight_scale(int p_id, real_t p_weight_scale);
|
2016-09-13 21:17:18 +00:00
|
|
|
void remove_point(int p_id);
|
2017-07-11 14:04:41 +00:00
|
|
|
bool has_point(int p_id) const;
|
2017-11-02 19:42:58 +00:00
|
|
|
PoolVector<int> get_point_connections(int p_id);
|
2017-09-07 14:11:48 +00:00
|
|
|
Array get_points();
|
2016-09-13 21:17:18 +00:00
|
|
|
|
2017-05-19 11:16:45 +00:00
|
|
|
void connect_points(int p_id, int p_with_id, bool bidirectional = true);
|
2017-03-05 15:44:50 +00:00
|
|
|
void disconnect_points(int p_id, int p_with_id);
|
|
|
|
bool are_points_connected(int p_id, int p_with_id) const;
|
2016-09-13 21:17:18 +00:00
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int get_closest_point(const Vector3 &p_point) const;
|
2017-09-10 13:37:49 +00:00
|
|
|
Vector3 get_closest_position_in_segment(const Vector3 &p_point) const;
|
2016-09-13 21:17:18 +00:00
|
|
|
|
2017-01-07 21:25:37 +00: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-13 21:17:18 +00:00
|
|
|
|
|
|
|
AStar();
|
|
|
|
~AStar();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ASTAR_H
|