a_star.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*************************************************************************/
  2. /* a_star.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef ASTAR_H
  31. #define ASTAR_H
  32. #include "reference.h"
  33. #include "self_list.h"
  34. /**
  35. A* pathfinding algorithm
  36. @author Juan Linietsky <reduzio@gmail.com>
  37. */
  38. class AStar : public Reference {
  39. GDCLASS(AStar, Reference)
  40. uint64_t pass;
  41. struct Point {
  42. SelfList<Point> list;
  43. int id;
  44. Vector3 pos;
  45. real_t weight_scale;
  46. uint64_t last_pass;
  47. Vector<Point *> neighbours;
  48. // Used for pathfinding
  49. Point *prev_point;
  50. real_t distance;
  51. Point()
  52. : list(this) {}
  53. };
  54. Map<int, Point *> points;
  55. struct Segment {
  56. union {
  57. struct {
  58. int32_t from;
  59. int32_t to;
  60. };
  61. uint64_t key;
  62. };
  63. Point *from_point;
  64. Point *to_point;
  65. bool operator<(const Segment &p_s) const { return key < p_s.key; }
  66. Segment() { key = 0; }
  67. Segment(int p_from, int p_to) {
  68. if (p_from > p_to) {
  69. SWAP(p_from, p_to);
  70. }
  71. from = p_from;
  72. to = p_to;
  73. }
  74. };
  75. Set<Segment> segments;
  76. bool _solve(Point *begin_point, Point *end_point);
  77. protected:
  78. static void _bind_methods();
  79. virtual float _estimate_cost(int p_from_id, int p_to_id);
  80. virtual float _compute_cost(int p_from_id, int p_to_id);
  81. public:
  82. int get_available_point_id() const;
  83. void add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale = 1);
  84. Vector3 get_point_position(int p_id) const;
  85. void set_point_position(int p_id, const Vector3 &p_pos);
  86. real_t get_point_weight_scale(int p_id) const;
  87. void set_point_weight_scale(int p_id, real_t p_weight_scale);
  88. void remove_point(int p_id);
  89. bool has_point(int p_id) const;
  90. PoolVector<int> get_point_connections(int p_id);
  91. Array get_points();
  92. void connect_points(int p_id, int p_with_id, bool bidirectional = true);
  93. void disconnect_points(int p_id, int p_with_id);
  94. bool are_points_connected(int p_id, int p_with_id) const;
  95. void clear();
  96. int get_closest_point(const Vector3 &p_point) const;
  97. Vector3 get_closest_position_in_segment(const Vector3 &p_point) const;
  98. PoolVector<Vector3> get_point_path(int p_from_id, int p_to_id);
  99. PoolVector<int> get_id_path(int p_from_id, int p_to_id);
  100. AStar();
  101. ~AStar();
  102. };
  103. #endif // ASTAR_H