a_star.h 3.7 KB

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