| 1 | /**************************************************************************/ |
| 2 | /* navigation_server_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_SERVER_2D_H |
| 32 | #define NAVIGATION_SERVER_2D_H |
| 33 | |
| 34 | #include "core/object/class_db.h" |
| 35 | #include "core/templates/rid.h" |
| 36 | |
| 37 | #include "scene/resources/navigation_polygon.h" |
| 38 | #include "servers/navigation/navigation_path_query_parameters_2d.h" |
| 39 | #include "servers/navigation/navigation_path_query_result_2d.h" |
| 40 | |
| 41 | // This server exposes the `NavigationServer3D` features in the 2D world. |
| 42 | class NavigationServer2D : public Object { |
| 43 | GDCLASS(NavigationServer2D, Object); |
| 44 | |
| 45 | static NavigationServer2D *singleton; |
| 46 | |
| 47 | void _emit_map_changed(RID p_map); |
| 48 | |
| 49 | protected: |
| 50 | static void _bind_methods(); |
| 51 | |
| 52 | public: |
| 53 | /// Thread safe, can be used across many threads. |
| 54 | static NavigationServer2D *get_singleton() { return singleton; } |
| 55 | |
| 56 | virtual TypedArray<RID> get_maps() const; |
| 57 | |
| 58 | /// Create a new map. |
| 59 | virtual RID map_create(); |
| 60 | |
| 61 | /// Set map active. |
| 62 | virtual void map_set_active(RID p_map, bool p_active); |
| 63 | |
| 64 | /// Returns true if the map is active. |
| 65 | virtual bool map_is_active(RID p_map) const; |
| 66 | |
| 67 | /// Set the map cell size used to weld the navigation mesh polygons. |
| 68 | virtual void map_set_cell_size(RID p_map, real_t p_cell_size); |
| 69 | |
| 70 | /// Returns the map cell size. |
| 71 | virtual real_t map_get_cell_size(RID p_map) const; |
| 72 | |
| 73 | virtual void map_set_use_edge_connections(RID p_map, bool p_enabled); |
| 74 | virtual bool map_get_use_edge_connections(RID p_map) const; |
| 75 | |
| 76 | /// Set the map edge connection margin used to weld the compatible region edges. |
| 77 | virtual void map_set_edge_connection_margin(RID p_map, real_t p_connection_margin); |
| 78 | |
| 79 | /// Returns the edge connection margin of this map. |
| 80 | virtual real_t map_get_edge_connection_margin(RID p_map) const; |
| 81 | |
| 82 | /// Set the map link connection radius used to attach links to the nav mesh. |
| 83 | virtual void map_set_link_connection_radius(RID p_map, real_t p_connection_radius); |
| 84 | |
| 85 | /// Returns the link connection radius of this map. |
| 86 | virtual real_t map_get_link_connection_radius(RID p_map) const; |
| 87 | |
| 88 | /// Returns the navigation path to reach the destination from the origin. |
| 89 | virtual Vector<Vector2> map_get_path(RID p_map, Vector2 p_origin, Vector2 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) const; |
| 90 | |
| 91 | virtual Vector2 map_get_closest_point(RID p_map, const Vector2 &p_point) const; |
| 92 | virtual RID map_get_closest_point_owner(RID p_map, const Vector2 &p_point) const; |
| 93 | |
| 94 | virtual TypedArray<RID> map_get_links(RID p_map) const; |
| 95 | virtual TypedArray<RID> map_get_regions(RID p_map) const; |
| 96 | virtual TypedArray<RID> map_get_agents(RID p_map) const; |
| 97 | virtual TypedArray<RID> map_get_obstacles(RID p_map) const; |
| 98 | |
| 99 | virtual void map_force_update(RID p_map); |
| 100 | |
| 101 | /// Creates a new region. |
| 102 | virtual RID region_create(); |
| 103 | |
| 104 | virtual void region_set_enabled(RID p_region, bool p_enabled); |
| 105 | virtual bool region_get_enabled(RID p_region) const; |
| 106 | |
| 107 | virtual void region_set_use_edge_connections(RID p_region, bool p_enabled); |
| 108 | virtual bool region_get_use_edge_connections(RID p_region) const; |
| 109 | |
| 110 | /// Set the enter_cost of a region |
| 111 | virtual void region_set_enter_cost(RID p_region, real_t p_enter_cost); |
| 112 | virtual real_t region_get_enter_cost(RID p_region) const; |
| 113 | |
| 114 | /// Set the travel_cost of a region |
| 115 | virtual void region_set_travel_cost(RID p_region, real_t p_travel_cost); |
| 116 | virtual real_t region_get_travel_cost(RID p_region) const; |
| 117 | |
| 118 | /// Set the node which manages this region. |
| 119 | virtual void region_set_owner_id(RID p_region, ObjectID p_owner_id); |
| 120 | virtual ObjectID region_get_owner_id(RID p_region) const; |
| 121 | |
| 122 | virtual bool region_owns_point(RID p_region, const Vector2 &p_point) const; |
| 123 | |
| 124 | /// Set the map of this region. |
| 125 | virtual void region_set_map(RID p_region, RID p_map); |
| 126 | virtual RID region_get_map(RID p_region) const; |
| 127 | |
| 128 | /// Set the region's layers |
| 129 | virtual void region_set_navigation_layers(RID p_region, uint32_t p_navigation_layers); |
| 130 | virtual uint32_t region_get_navigation_layers(RID p_region) const; |
| 131 | |
| 132 | /// Set the global transformation of this region. |
| 133 | virtual void region_set_transform(RID p_region, Transform2D p_transform); |
| 134 | |
| 135 | /// Set the navigation poly of this region. |
| 136 | virtual void region_set_navigation_polygon(RID p_region, Ref<NavigationPolygon> p_navigation_polygon); |
| 137 | |
| 138 | /// Get a list of a region's connection to other regions. |
| 139 | virtual int region_get_connections_count(RID p_region) const; |
| 140 | virtual Vector2 region_get_connection_pathway_start(RID p_region, int p_connection_id) const; |
| 141 | virtual Vector2 region_get_connection_pathway_end(RID p_region, int p_connection_id) const; |
| 142 | |
| 143 | /// Creates a new link between positions in the nav map. |
| 144 | virtual RID link_create(); |
| 145 | |
| 146 | /// Set the map of this link. |
| 147 | virtual void link_set_map(RID p_link, RID p_map); |
| 148 | virtual RID link_get_map(RID p_link) const; |
| 149 | |
| 150 | virtual void link_set_enabled(RID p_link, bool p_enabled); |
| 151 | virtual bool link_get_enabled(RID p_link) const; |
| 152 | |
| 153 | /// Set whether this link travels in both directions. |
| 154 | virtual void link_set_bidirectional(RID p_link, bool p_bidirectional); |
| 155 | virtual bool link_is_bidirectional(RID p_link) const; |
| 156 | |
| 157 | /// Set the link's layers. |
| 158 | virtual void link_set_navigation_layers(RID p_link, uint32_t p_navigation_layers); |
| 159 | virtual uint32_t link_get_navigation_layers(RID p_link) const; |
| 160 | |
| 161 | /// Set the start position of the link. |
| 162 | virtual void link_set_start_position(RID p_link, Vector2 p_position); |
| 163 | virtual Vector2 link_get_start_position(RID p_link) const; |
| 164 | |
| 165 | /// Set the end position of the link. |
| 166 | virtual void link_set_end_position(RID p_link, Vector2 p_position); |
| 167 | virtual Vector2 link_get_end_position(RID p_link) const; |
| 168 | |
| 169 | /// Set the enter cost of the link. |
| 170 | virtual void link_set_enter_cost(RID p_link, real_t p_enter_cost); |
| 171 | virtual real_t link_get_enter_cost(RID p_link) const; |
| 172 | |
| 173 | /// Set the travel cost of the link. |
| 174 | virtual void link_set_travel_cost(RID p_link, real_t p_travel_cost); |
| 175 | virtual real_t link_get_travel_cost(RID p_link) const; |
| 176 | |
| 177 | /// Set the node which manages this link. |
| 178 | virtual void link_set_owner_id(RID p_link, ObjectID p_owner_id); |
| 179 | virtual ObjectID link_get_owner_id(RID p_link) const; |
| 180 | |
| 181 | /// Creates the agent. |
| 182 | virtual RID agent_create(); |
| 183 | |
| 184 | /// Put the agent in the map. |
| 185 | virtual void agent_set_map(RID p_agent, RID p_map); |
| 186 | virtual RID agent_get_map(RID p_agent) const; |
| 187 | |
| 188 | virtual void agent_set_paused(RID p_agent, bool p_paused); |
| 189 | virtual bool agent_get_paused(RID p_agent) const; |
| 190 | |
| 191 | virtual void agent_set_avoidance_enabled(RID p_agent, bool p_enabled); |
| 192 | virtual bool agent_get_avoidance_enabled(RID p_agent) const; |
| 193 | |
| 194 | /// The maximum distance (center point to |
| 195 | /// center point) to other agents this agent |
| 196 | /// takes into account in the navigation. The |
| 197 | /// larger this number, the longer the running |
| 198 | /// time of the simulation. If the number is too |
| 199 | /// low, the simulation will not be safe. |
| 200 | /// Must be non-negative. |
| 201 | virtual void agent_set_neighbor_distance(RID p_agent, real_t p_distance); |
| 202 | |
| 203 | /// The maximum number of other agents this |
| 204 | /// agent takes into account in the navigation. |
| 205 | /// The larger this number, the longer the |
| 206 | /// running time of the simulation. If the |
| 207 | /// number is too low, the simulation will not |
| 208 | /// be safe. |
| 209 | virtual void agent_set_max_neighbors(RID p_agent, int p_count); |
| 210 | |
| 211 | /// The minimal amount of time for which this |
| 212 | /// agent's velocities that are computed by the |
| 213 | /// simulation are safe with respect to other |
| 214 | /// agents. The larger this number, the sooner |
| 215 | /// this agent will respond to the presence of |
| 216 | /// other agents, but the less freedom this |
| 217 | /// agent has in choosing its velocities. |
| 218 | /// Must be positive. |
| 219 | |
| 220 | virtual void agent_set_time_horizon_agents(RID p_agent, real_t p_time_horizon); |
| 221 | virtual void agent_set_time_horizon_obstacles(RID p_agent, real_t p_time_horizon); |
| 222 | |
| 223 | /// The radius of this agent. |
| 224 | /// Must be non-negative. |
| 225 | virtual void agent_set_radius(RID p_agent, real_t p_radius); |
| 226 | |
| 227 | /// The maximum speed of this agent. |
| 228 | /// Must be non-negative. |
| 229 | virtual void agent_set_max_speed(RID p_agent, real_t p_max_speed); |
| 230 | |
| 231 | /// forces and agent velocity change in the avoidance simulation, adds simulation instability if done recklessly |
| 232 | virtual void agent_set_velocity_forced(RID p_agent, Vector2 p_velocity); |
| 233 | |
| 234 | /// The wanted velocity for the agent as a "suggestion" to the avoidance simulation. |
| 235 | /// The simulation will try to fulfill this velocity wish if possible but may change the velocity depending on other agent's and obstacles'. |
| 236 | virtual void agent_set_velocity(RID p_agent, Vector2 p_velocity); |
| 237 | |
| 238 | /// Position of the agent in world space. |
| 239 | virtual void agent_set_position(RID p_agent, Vector2 p_position); |
| 240 | |
| 241 | /// Returns true if the map got changed the previous frame. |
| 242 | virtual bool agent_is_map_changed(RID p_agent) const; |
| 243 | |
| 244 | /// Callback called at the end of the RVO process |
| 245 | virtual void agent_set_avoidance_callback(RID p_agent, Callable p_callback); |
| 246 | |
| 247 | virtual void agent_set_avoidance_layers(RID p_agent, uint32_t p_layers); |
| 248 | virtual void agent_set_avoidance_mask(RID p_agent, uint32_t p_mask); |
| 249 | virtual void agent_set_avoidance_priority(RID p_agent, real_t p_priority); |
| 250 | |
| 251 | /// Creates the obstacle. |
| 252 | virtual RID obstacle_create(); |
| 253 | virtual void obstacle_set_avoidance_enabled(RID p_obstacle, bool p_enabled); |
| 254 | virtual bool obstacle_get_avoidance_enabled(RID p_obstacle) const; |
| 255 | virtual void obstacle_set_map(RID p_obstacle, RID p_map); |
| 256 | virtual RID obstacle_get_map(RID p_obstacle) const; |
| 257 | virtual void obstacle_set_paused(RID p_obstacle, bool p_paused); |
| 258 | virtual bool obstacle_get_paused(RID p_obstacle) const; |
| 259 | virtual void obstacle_set_radius(RID p_obstacle, real_t p_radius); |
| 260 | virtual void obstacle_set_velocity(RID p_obstacle, Vector2 p_velocity); |
| 261 | virtual void obstacle_set_position(RID p_obstacle, Vector2 p_position); |
| 262 | virtual void obstacle_set_vertices(RID p_obstacle, const Vector<Vector2> &p_vertices); |
| 263 | virtual void obstacle_set_avoidance_layers(RID p_obstacle, uint32_t p_layers); |
| 264 | |
| 265 | /// Returns a customized navigation path using a query parameters object |
| 266 | virtual void query_path(const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result) const; |
| 267 | |
| 268 | /// Destroy the `RID` |
| 269 | virtual void free(RID p_object); |
| 270 | |
| 271 | NavigationServer2D(); |
| 272 | virtual ~NavigationServer2D(); |
| 273 | |
| 274 | void set_debug_enabled(bool p_enabled); |
| 275 | bool get_debug_enabled() const; |
| 276 | |
| 277 | #ifdef DEBUG_ENABLED |
| 278 | void set_debug_navigation_enabled(bool p_enabled); |
| 279 | bool get_debug_navigation_enabled() const; |
| 280 | |
| 281 | void set_debug_avoidance_enabled(bool p_enabled); |
| 282 | bool get_debug_avoidance_enabled() const; |
| 283 | |
| 284 | void set_debug_navigation_edge_connection_color(const Color &p_color); |
| 285 | Color get_debug_navigation_edge_connection_color() const; |
| 286 | |
| 287 | void set_debug_navigation_geometry_face_color(const Color &p_color); |
| 288 | Color get_debug_navigation_geometry_face_color() const; |
| 289 | |
| 290 | void set_debug_navigation_geometry_face_disabled_color(const Color &p_color); |
| 291 | Color get_debug_navigation_geometry_face_disabled_color() const; |
| 292 | |
| 293 | void set_debug_navigation_geometry_edge_color(const Color &p_color); |
| 294 | Color get_debug_navigation_geometry_edge_color() const; |
| 295 | |
| 296 | void set_debug_navigation_geometry_edge_disabled_color(const Color &p_color); |
| 297 | Color get_debug_navigation_geometry_edge_disabled_color() const; |
| 298 | |
| 299 | void set_debug_navigation_link_connection_color(const Color &p_color); |
| 300 | Color get_debug_navigation_link_connection_color() const; |
| 301 | |
| 302 | void set_debug_navigation_link_connection_disabled_color(const Color &p_color); |
| 303 | Color get_debug_navigation_link_connection_disabled_color() const; |
| 304 | |
| 305 | void set_debug_navigation_enable_edge_connections(const bool p_value); |
| 306 | bool get_debug_navigation_enable_edge_connections() const; |
| 307 | |
| 308 | void set_debug_navigation_enable_geometry_face_random_color(const bool p_value); |
| 309 | bool get_debug_navigation_enable_geometry_face_random_color() const; |
| 310 | |
| 311 | void set_debug_navigation_enable_edge_lines(const bool p_value); |
| 312 | bool get_debug_navigation_enable_edge_lines() const; |
| 313 | |
| 314 | void set_debug_navigation_agent_path_color(const Color &p_color); |
| 315 | Color get_debug_navigation_agent_path_color() const; |
| 316 | |
| 317 | void set_debug_navigation_enable_agent_paths(const bool p_value); |
| 318 | bool get_debug_navigation_enable_agent_paths() const; |
| 319 | |
| 320 | void set_debug_navigation_agent_path_point_size(real_t p_point_size); |
| 321 | real_t get_debug_navigation_agent_path_point_size() const; |
| 322 | |
| 323 | void set_debug_navigation_avoidance_enable_agents_radius(const bool p_value); |
| 324 | bool get_debug_navigation_avoidance_enable_agents_radius() const; |
| 325 | |
| 326 | void set_debug_navigation_avoidance_enable_obstacles_radius(const bool p_value); |
| 327 | bool get_debug_navigation_avoidance_enable_obstacles_radius() const; |
| 328 | |
| 329 | void set_debug_navigation_avoidance_agents_radius_color(const Color &p_color); |
| 330 | Color get_debug_navigation_avoidance_agents_radius_color() const; |
| 331 | |
| 332 | void set_debug_navigation_avoidance_obstacles_radius_color(const Color &p_color); |
| 333 | Color get_debug_navigation_avoidance_obstacles_radius_color() const; |
| 334 | |
| 335 | void set_debug_navigation_avoidance_static_obstacle_pushin_face_color(const Color &p_color); |
| 336 | Color get_debug_navigation_avoidance_static_obstacle_pushin_face_color() const; |
| 337 | |
| 338 | void set_debug_navigation_avoidance_static_obstacle_pushout_face_color(const Color &p_color); |
| 339 | Color get_debug_navigation_avoidance_static_obstacle_pushout_face_color() const; |
| 340 | |
| 341 | void set_debug_navigation_avoidance_static_obstacle_pushin_edge_color(const Color &p_color); |
| 342 | Color get_debug_navigation_avoidance_static_obstacle_pushin_edge_color() const; |
| 343 | |
| 344 | void set_debug_navigation_avoidance_static_obstacle_pushout_edge_color(const Color &p_color); |
| 345 | Color get_debug_navigation_avoidance_static_obstacle_pushout_edge_color() const; |
| 346 | |
| 347 | void set_debug_navigation_avoidance_enable_obstacles_static(const bool p_value); |
| 348 | bool get_debug_navigation_avoidance_enable_obstacles_static() const; |
| 349 | #endif // DEBUG_ENABLED |
| 350 | |
| 351 | #ifdef DEBUG_ENABLED |
| 352 | private: |
| 353 | void _emit_navigation_debug_changed_signal(); |
| 354 | #endif // DEBUG_ENABLED |
| 355 | }; |
| 356 | |
| 357 | #endif // NAVIGATION_SERVER_2D_H |
| 358 | |