a_star.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*************************************************************************/
  2. /* a_star.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. class AStar : public Reference {
  38. OBJ_TYPE(AStar, Reference)
  39. uint64_t pass;
  40. struct Point {
  41. SelfList<Point> list;
  42. int id;
  43. Vector3 pos;
  44. float weight_scale;
  45. uint64_t last_pass;
  46. Vector<Point *> neighbours;
  47. //used for pathfinding
  48. Point *prev_point;
  49. float distance;
  50. Point() :
  51. list(this) {}
  52. };
  53. Map<int, Point *> points;
  54. struct Segment {
  55. union {
  56. struct {
  57. int32_t from;
  58. int32_t to;
  59. };
  60. uint64_t key;
  61. };
  62. Point *from_point;
  63. Point *to_point;
  64. bool operator<(const Segment &p_s) const { return key < p_s.key; }
  65. Segment() { key = 0; }
  66. Segment(int p_from, int p_to) {
  67. if (p_from > p_to) {
  68. SWAP(p_from, p_to);
  69. }
  70. from = p_from;
  71. to = p_to;
  72. }
  73. };
  74. Set<Segment> segments;
  75. bool _solve(Point *begin_point, Point *end_point);
  76. protected:
  77. static void _bind_methods();
  78. virtual float _estimate_cost(int p_from_id, int p_to_id);
  79. virtual float _compute_cost(int p_from_id, int p_to_id);
  80. public:
  81. int get_available_point_id() const;
  82. void add_point(int p_id, const Vector3 &p_pos, float p_weight_scale = 1);
  83. Vector3 get_point_pos(int p_id) const;
  84. float get_point_weight_scale(int p_id) const;
  85. void remove_point(int p_id);
  86. bool has_point(int p_id) const;
  87. void connect_points(int p_id, int p_with_id, bool bidirectional = true);
  88. void disconnect_points(int p_id, int p_with_id);
  89. bool are_points_connected(int p_id, int p_with_id) const;
  90. void clear();
  91. int get_closest_point(const Vector3 &p_point) const;
  92. Vector3 get_closest_pos_in_segment(const Vector3 &p_point) const;
  93. DVector<Vector3> get_point_path(int p_from_id, int p_to_id);
  94. DVector<int> get_id_path(int p_from_id, int p_to_id);
  95. AStar();
  96. ~AStar();
  97. };
  98. #endif // ASTAR_H