1/**************************************************************************/
2/* navigation_agent_2d.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
31#ifndef NAVIGATION_AGENT_2D_H
32#define NAVIGATION_AGENT_2D_H
33
34#include "scene/main/node.h"
35#include "servers/navigation/navigation_path_query_parameters_2d.h"
36#include "servers/navigation/navigation_path_query_result_2d.h"
37
38class Node2D;
39
40class NavigationAgent2D : public Node {
41 GDCLASS(NavigationAgent2D, Node);
42
43 Node2D *agent_parent = nullptr;
44
45 RID agent;
46 RID map_override;
47
48 bool avoidance_enabled = false;
49 uint32_t avoidance_layers = 1;
50 uint32_t avoidance_mask = 1;
51 real_t avoidance_priority = 1.0;
52 uint32_t navigation_layers = 1;
53 NavigationPathQueryParameters2D::PathfindingAlgorithm pathfinding_algorithm = NavigationPathQueryParameters2D::PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
54 NavigationPathQueryParameters2D::PathPostProcessing path_postprocessing = NavigationPathQueryParameters2D::PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
55 BitField<NavigationPathQueryParameters2D::PathMetadataFlags> path_metadata_flags = NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_ALL;
56
57 real_t path_desired_distance = 20.0;
58 real_t target_desired_distance = 10.0;
59 real_t radius = 10.0;
60 real_t neighbor_distance = 500.0;
61 int max_neighbors = 10;
62 real_t time_horizon_agents = 1.0;
63 real_t time_horizon_obstacles = 0.0;
64 real_t max_speed = 100.0;
65 real_t path_max_distance = 100.0;
66
67 Vector2 target_position;
68
69 Ref<NavigationPathQueryParameters2D> navigation_query;
70 Ref<NavigationPathQueryResult2D> navigation_result;
71 int navigation_path_index = 0;
72
73 // the velocity result of the avoidance simulation step
74 Vector2 safe_velocity;
75
76 /// The submitted target velocity, sets the "wanted" rvo agent velocity on the next update
77 // this velocity is not guaranteed, the simulation will try to fulfill it if possible
78 // if other agents or obstacles interfere it will be changed accordingly
79 Vector2 velocity;
80 bool velocity_submitted = false;
81
82 /// The submitted forced velocity, overrides the rvo agent velocity on the next update
83 // should only be used very intentionally and not every frame as it interferes with the simulation stability
84 Vector2 velocity_forced;
85 bool velocity_forced_submitted = false;
86
87 bool target_position_submitted = false;
88
89 bool target_reached = false;
90 bool navigation_finished = true;
91 // No initialized on purpose
92 uint32_t update_frame_id = 0;
93
94 // Debug properties for exposed bindings
95 bool debug_enabled = false;
96 float debug_path_custom_point_size = 4.0;
97 float debug_path_custom_line_width = -1.0;
98 bool debug_use_custom = false;
99 Color debug_path_custom_color = Color(1.0, 1.0, 1.0, 1.0);
100
101#ifdef DEBUG_ENABLED
102 // Debug properties internal only
103 bool debug_path_dirty = true;
104 RID debug_path_instance;
105#endif // DEBUG_ENABLED
106
107protected:
108 static void _bind_methods();
109 void _notification(int p_what);
110
111#ifndef DISABLE_DEPRECATED
112 bool _set(const StringName &p_name, const Variant &p_value);
113 bool _get(const StringName &p_name, Variant &r_ret) const;
114#endif // DISABLE_DEPRECATED
115
116public:
117 NavigationAgent2D();
118 virtual ~NavigationAgent2D();
119
120 RID get_rid() const { return agent; }
121
122 void set_avoidance_enabled(bool p_enabled);
123 bool get_avoidance_enabled() const;
124
125 void set_agent_parent(Node *p_agent_parent);
126
127 void set_navigation_layers(uint32_t p_navigation_layers);
128 uint32_t get_navigation_layers() const;
129
130 void set_navigation_layer_value(int p_layer_number, bool p_value);
131 bool get_navigation_layer_value(int p_layer_number) const;
132
133 void set_pathfinding_algorithm(const NavigationPathQueryParameters2D::PathfindingAlgorithm p_pathfinding_algorithm);
134 NavigationPathQueryParameters2D::PathfindingAlgorithm get_pathfinding_algorithm() const {
135 return pathfinding_algorithm;
136 }
137
138 void set_path_postprocessing(const NavigationPathQueryParameters2D::PathPostProcessing p_path_postprocessing);
139 NavigationPathQueryParameters2D::PathPostProcessing get_path_postprocessing() const {
140 return path_postprocessing;
141 }
142
143 void set_path_metadata_flags(BitField<NavigationPathQueryParameters2D::PathMetadataFlags> p_flags);
144 BitField<NavigationPathQueryParameters2D::PathMetadataFlags> get_path_metadata_flags() const {
145 return path_metadata_flags;
146 }
147
148 void set_navigation_map(RID p_navigation_map);
149 RID get_navigation_map() const;
150
151 void set_path_desired_distance(real_t p_dd);
152 real_t get_path_desired_distance() const { return path_desired_distance; }
153
154 void set_target_desired_distance(real_t p_dd);
155 real_t get_target_desired_distance() const { return target_desired_distance; }
156
157 void set_radius(real_t p_radius);
158 real_t get_radius() const { return radius; }
159
160 void set_neighbor_distance(real_t p_distance);
161 real_t get_neighbor_distance() const { return neighbor_distance; }
162
163 void set_max_neighbors(int p_count);
164 int get_max_neighbors() const { return max_neighbors; }
165
166 void set_time_horizon_agents(real_t p_time_horizon);
167 real_t get_time_horizon_agents() const { return time_horizon_agents; }
168
169 void set_time_horizon_obstacles(real_t p_time_horizon);
170 real_t get_time_horizon_obstacles() const { return time_horizon_obstacles; }
171
172 void set_max_speed(real_t p_max_speed);
173 real_t get_max_speed() const { return max_speed; }
174
175 void set_path_max_distance(real_t p_pmd);
176 real_t get_path_max_distance();
177
178 void set_target_position(Vector2 p_position);
179 Vector2 get_target_position() const;
180
181 Vector2 get_next_path_position();
182
183 Ref<NavigationPathQueryResult2D> get_current_navigation_result() const { return navigation_result; }
184
185 const Vector<Vector2> &get_current_navigation_path() const { return navigation_result->get_path(); }
186
187 int get_current_navigation_path_index() const { return navigation_path_index; }
188
189 real_t distance_to_target() const;
190 bool is_target_reached() const;
191 bool is_target_reachable();
192 bool is_navigation_finished();
193 Vector2 get_final_position();
194
195 void set_velocity(const Vector2 p_velocity);
196 Vector2 get_velocity() { return velocity; }
197
198 void set_velocity_forced(const Vector2 p_velocity);
199
200 void _avoidance_done(Vector3 p_new_velocity);
201
202 PackedStringArray get_configuration_warnings() const override;
203
204 void set_avoidance_layers(uint32_t p_layers);
205 uint32_t get_avoidance_layers() const;
206
207 void set_avoidance_mask(uint32_t p_mask);
208 uint32_t get_avoidance_mask() const;
209
210 void set_avoidance_layer_value(int p_layer_number, bool p_value);
211 bool get_avoidance_layer_value(int p_layer_number) const;
212
213 void set_avoidance_mask_value(int p_mask_number, bool p_value);
214 bool get_avoidance_mask_value(int p_mask_number) const;
215
216 void set_avoidance_priority(real_t p_priority);
217 real_t get_avoidance_priority() const;
218
219 void set_debug_enabled(bool p_enabled);
220 bool get_debug_enabled() const;
221
222 void set_debug_use_custom(bool p_enabled);
223 bool get_debug_use_custom() const;
224
225 void set_debug_path_custom_color(Color p_color);
226 Color get_debug_path_custom_color() const;
227
228 void set_debug_path_custom_point_size(float p_point_size);
229 float get_debug_path_custom_point_size() const;
230
231 void set_debug_path_custom_line_width(float p_line_width);
232 float get_debug_path_custom_line_width() const;
233
234private:
235 void update_navigation();
236 void _request_repath();
237 void _check_distance_to_target();
238
239#ifdef DEBUG_ENABLED
240 void _navigation_debug_changed();
241 void _update_debug_path();
242#endif // DEBUG_ENABLED
243};
244
245#endif // NAVIGATION_AGENT_2D_H
246