Debian Codegraph @

servers/navigation_2d/navigation_server_2d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* navigation_server_2d.cpp */
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 #include "navigation_server_2d.h"
32 #include "navigation_server_2d.compat.inc"
33
34 #include "core/config/project_settings.h"
35 #include "scene/main/node.h"
36 #include "servers/navigation_2d/navigation_server_2d_dummy.h"
37
38 NavigationServer2D *NavigationServer2D::singleton = nullptr;
39
40 RWLock NavigationServer2D::geometry_parser_rwlock;
41 RID_Owner<NavMeshGeometryParser2D> NavigationServer2D::geometry_parser_owner;
42 LocalVector<NavMeshGeometryParser2D *> NavigationServer2D::generator_parsers;
43
44 void NavigationServer2D::_bind_methods() {
45 ClassDB::bind_method(D_METHOD("get_maps"), &NavigationServer2D::get_maps);
46
47 ClassDB::bind_method(D_METHOD("map_create"), &NavigationServer2D::map_create);
48 ClassDB::bind_method(D_METHOD("map_set_active", "map", "active"), &NavigationServer2D::map_set_active);
49 ClassDB::bind_method(D_METHOD("map_is_active", "map"), &NavigationServer2D::map_is_active);
50 ClassDB::bind_method(D_METHOD("map_set_cell_size", "map", "cell_size"), &NavigationServer2D::map_set_cell_size);
51 ClassDB::bind_method(D_METHOD("map_get_cell_size", "map"), &NavigationServer2D::map_get_cell_size);
52 ClassDB::bind_method(D_METHOD("map_set_merge_rasterizer_cell_scale", "map", "scale"), &NavigationServer2D::map_set_merge_rasterizer_cell_scale);
53 ClassDB::bind_method(D_METHOD("map_get_merge_rasterizer_cell_scale", "map"), &NavigationServer2D::map_get_merge_rasterizer_cell_scale);
54 ClassDB::bind_method(D_METHOD("map_set_use_edge_connections", "map", "enabled"), &NavigationServer2D::map_set_use_edge_connections);
55 ClassDB::bind_method(D_METHOD("map_get_use_edge_connections", "map"), &NavigationServer2D::map_get_use_edge_connections);
56 ClassDB::bind_method(D_METHOD("map_set_edge_connection_margin", "map", "margin"), &NavigationServer2D::map_set_edge_connection_margin);
57 ClassDB::bind_method(D_METHOD("map_get_edge_connection_margin", "map"), &NavigationServer2D::map_get_edge_connection_margin);
58 ClassDB::bind_method(D_METHOD("map_set_link_connection_radius", "map", "radius"), &NavigationServer2D::map_set_link_connection_radius);
59 ClassDB::bind_method(D_METHOD("map_get_link_connection_radius", "map"), &NavigationServer2D::map_get_link_connection_radius);
60 ClassDB::bind_method(D_METHOD("map_get_path", "map", "origin", "destination", "optimize", "navigation_layers"), &NavigationServer2D::map_get_path, DEFVAL(1));
61 ClassDB::bind_method(D_METHOD("map_get_closest_point", "map", "to_point"), &NavigationServer2D::map_get_closest_point);
62 ClassDB::bind_method(D_METHOD("map_get_closest_point_owner", "map", "to_point"), &NavigationServer2D::map_get_closest_point_owner);
63
64 ClassDB::bind_method(D_METHOD("map_get_links", "map"), &NavigationServer2D::map_get_links);
65 ClassDB::bind_method(D_METHOD("map_get_regions", "map"), &NavigationServer2D::map_get_regions);
66 ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &NavigationServer2D::map_get_agents);
67 ClassDB::bind_method(D_METHOD("map_get_obstacles", "map"), &NavigationServer2D::map_get_obstacles);
68
69 ClassDB::bind_method(D_METHOD("map_force_update", "map"), &NavigationServer2D::map_force_update);
70 ClassDB::bind_method(D_METHOD("map_get_iteration_id", "map"), &NavigationServer2D::map_get_iteration_id);
71 ClassDB::bind_method(D_METHOD("map_set_use_async_iterations", "map", "enabled"), &NavigationServer2D::map_set_use_async_iterations);
72 ClassDB::bind_method(D_METHOD("map_get_use_async_iterations", "map"), &NavigationServer2D::map_get_use_async_iterations);
73
74 ClassDB::bind_method(D_METHOD("map_get_random_point", "map", "navigation_layers", "uniformly"), &NavigationServer2D::map_get_random_point);
75
76 ClassDB::bind_method(D_METHOD("query_path", "parameters", "result", "callback"), &NavigationServer2D::query_path, DEFVAL(Callable()));
77
78 ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer2D::region_create);
79 ClassDB::bind_method(D_METHOD("region_get_iteration_id", "region"), &NavigationServer2D::region_get_iteration_id);
80 ClassDB::bind_method(D_METHOD("region_set_use_async_iterations", "region", "enabled"), &NavigationServer2D::region_set_use_async_iterations);
81 ClassDB::bind_method(D_METHOD("region_get_use_async_iterations", "region"), &NavigationServer2D::region_get_use_async_iterations);
82 ClassDB::bind_method(D_METHOD("region_set_enabled", "region", "enabled"), &NavigationServer2D::region_set_enabled);
83 ClassDB::bind_method(D_METHOD("region_get_enabled", "region"), &NavigationServer2D::region_get_enabled);
84 ClassDB::bind_method(D_METHOD("region_set_use_edge_connections", "region", "enabled"), &NavigationServer2D::region_set_use_edge_connections);
85 ClassDB::bind_method(D_METHOD("region_get_use_edge_connections", "region"), &NavigationServer2D::region_get_use_edge_connections);
86 ClassDB::bind_method(D_METHOD("region_set_enter_cost", "region", "enter_cost"), &NavigationServer2D::region_set_enter_cost);
87 ClassDB::bind_method(D_METHOD("region_get_enter_cost", "region"), &NavigationServer2D::region_get_enter_cost);
88 ClassDB::bind_method(D_METHOD("region_set_travel_cost", "region", "travel_cost"), &NavigationServer2D::region_set_travel_cost);
89 ClassDB::bind_method(D_METHOD("region_get_travel_cost", "region"), &NavigationServer2D::region_get_travel_cost);
90 ClassDB::bind_method(D_METHOD("region_set_owner_id", "region", "owner_id"), &NavigationServer2D::region_set_owner_id);
91 ClassDB::bind_method(D_METHOD("region_get_owner_id", "region"), &NavigationServer2D::region_get_owner_id);
92 ClassDB::bind_method(D_METHOD("region_owns_point", "region", "point"), &NavigationServer2D::region_owns_point);
93 ClassDB::bind_method(D_METHOD("region_set_map", "region", "map"), &NavigationServer2D::region_set_map);
94 ClassDB::bind_method(D_METHOD("region_get_map", "region"), &NavigationServer2D::region_get_map);
95 ClassDB::bind_method(D_METHOD("region_set_navigation_layers", "region", "navigation_layers"), &NavigationServer2D::region_set_navigation_layers);
96 ClassDB::bind_method(D_METHOD("region_get_navigation_layers", "region"), &NavigationServer2D::region_get_navigation_layers);
97 ClassDB::bind_method(D_METHOD("region_set_transform", "region", "transform"), &NavigationServer2D::region_set_transform);
98 ClassDB::bind_method(D_METHOD("region_get_transform", "region"), &NavigationServer2D::region_get_transform);
99 ClassDB::bind_method(D_METHOD("region_set_navigation_polygon", "region", "navigation_polygon"), &NavigationServer2D::region_set_navigation_polygon);
100 ClassDB::bind_method(D_METHOD("region_get_connections_count", "region"), &NavigationServer2D::region_get_connections_count);
101 ClassDB::bind_method(D_METHOD("region_get_connection_pathway_start", "region", "connection"), &NavigationServer2D::region_get_connection_pathway_start);
102 ClassDB::bind_method(D_METHOD("region_get_connection_pathway_end", "region", "connection"), &NavigationServer2D::region_get_connection_pathway_end);
103 ClassDB::bind_method(D_METHOD("region_get_closest_point", "region", "to_point"), &NavigationServer2D::region_get_closest_point);
104 ClassDB::bind_method(D_METHOD("region_get_random_point", "region", "navigation_layers", "uniformly"), &NavigationServer2D::region_get_random_point);
105 ClassDB::bind_method(D_METHOD("region_get_bounds", "region"), &NavigationServer2D::region_get_bounds);
106
107 ClassDB::bind_method(D_METHOD("link_create"), &NavigationServer2D::link_create);
108 ClassDB::bind_method(D_METHOD("link_get_iteration_id", "link"), &NavigationServer2D::link_get_iteration_id);
109 ClassDB::bind_method(D_METHOD("link_set_map", "link", "map"), &NavigationServer2D::link_set_map);
110 ClassDB::bind_method(D_METHOD("link_get_map", "link"), &NavigationServer2D::link_get_map);
111 ClassDB::bind_method(D_METHOD("link_set_enabled", "link", "enabled"), &NavigationServer2D::link_set_enabled);
112 ClassDB::bind_method(D_METHOD("link_get_enabled", "link"), &NavigationServer2D::link_get_enabled);
113 ClassDB::bind_method(D_METHOD("link_set_bidirectional", "link", "bidirectional"), &NavigationServer2D::link_set_bidirectional);
114 ClassDB::bind_method(D_METHOD("link_is_bidirectional", "link"), &NavigationServer2D::link_is_bidirectional);
115 ClassDB::bind_method(D_METHOD("link_set_navigation_layers", "link", "navigation_layers"), &NavigationServer2D::link_set_navigation_layers);
116 ClassDB::bind_method(D_METHOD("link_get_navigation_layers", "link"), &NavigationServer2D::link_get_navigation_layers);
117 ClassDB::bind_method(D_METHOD("link_set_start_position", "link", "position"), &NavigationServer2D::link_set_start_position);
118 ClassDB::bind_method(D_METHOD("link_get_start_position", "link"), &NavigationServer2D::link_get_start_position);
119 ClassDB::bind_method(D_METHOD("link_set_end_position", "link", "position"), &NavigationServer2D::link_set_end_position);
120 ClassDB::bind_method(D_METHOD("link_get_end_position", "link"), &NavigationServer2D::link_get_end_position);
121 ClassDB::bind_method(D_METHOD("link_set_enter_cost", "link", "enter_cost"), &NavigationServer2D::link_set_enter_cost);
122 ClassDB::bind_method(D_METHOD("link_get_enter_cost", "link"), &NavigationServer2D::link_get_enter_cost);
123 ClassDB::bind_method(D_METHOD("link_set_travel_cost", "link", "travel_cost"), &NavigationServer2D::link_set_travel_cost);
124 ClassDB::bind_method(D_METHOD("link_get_travel_cost", "link"), &NavigationServer2D::link_get_travel_cost);
125 ClassDB::bind_method(D_METHOD("link_set_owner_id", "link", "owner_id"), &NavigationServer2D::link_set_owner_id);
126 ClassDB::bind_method(D_METHOD("link_get_owner_id", "link"), &NavigationServer2D::link_get_owner_id);
127
128 ClassDB::bind_method(D_METHOD("agent_create"), &NavigationServer2D::agent_create);
129 ClassDB::bind_method(D_METHOD("agent_set_avoidance_enabled", "agent", "enabled"), &NavigationServer2D::agent_set_avoidance_enabled);
130 ClassDB::bind_method(D_METHOD("agent_get_avoidance_enabled", "agent"), &NavigationServer2D::agent_get_avoidance_enabled);
131 ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &NavigationServer2D::agent_set_map);
132 ClassDB::bind_method(D_METHOD("agent_get_map", "agent"), &NavigationServer2D::agent_get_map);
133 ClassDB::bind_method(D_METHOD("agent_set_paused", "agent", "paused"), &NavigationServer2D::agent_set_paused);
134 ClassDB::bind_method(D_METHOD("agent_get_paused", "agent"), &NavigationServer2D::agent_get_paused);
135 ClassDB::bind_method(D_METHOD("agent_set_neighbor_distance", "agent", "distance"), &NavigationServer2D::agent_set_neighbor_distance);
136 ClassDB::bind_method(D_METHOD("agent_get_neighbor_distance", "agent"), &NavigationServer2D::agent_get_neighbor_distance);
137 ClassDB::bind_method(D_METHOD("agent_set_max_neighbors", "agent", "count"), &NavigationServer2D::agent_set_max_neighbors);
138 ClassDB::bind_method(D_METHOD("agent_get_max_neighbors", "agent"), &NavigationServer2D::agent_get_max_neighbors);
139 ClassDB::bind_method(D_METHOD("agent_set_time_horizon_agents", "agent", "time_horizon"), &NavigationServer2D::agent_set_time_horizon_agents);
140 ClassDB::bind_method(D_METHOD("agent_get_time_horizon_agents", "agent"), &NavigationServer2D::agent_get_time_horizon_agents);
141 ClassDB::bind_method(D_METHOD("agent_set_time_horizon_obstacles", "agent", "time_horizon"), &NavigationServer2D::agent_set_time_horizon_obstacles);
142 ClassDB::bind_method(D_METHOD("agent_get_time_horizon_obstacles", "agent"), &NavigationServer2D::agent_get_time_horizon_obstacles);
143 ClassDB::bind_method(D_METHOD("agent_set_radius", "agent", "radius"), &NavigationServer2D::agent_set_radius);
144 ClassDB::bind_method(D_METHOD("agent_get_radius", "agent"), &NavigationServer2D::agent_get_radius);
145 ClassDB::bind_method(D_METHOD("agent_set_max_speed", "agent", "max_speed"), &NavigationServer2D::agent_set_max_speed);
146 ClassDB::bind_method(D_METHOD("agent_get_max_speed", "agent"), &NavigationServer2D::agent_get_max_speed);
147 ClassDB::bind_method(D_METHOD("agent_set_velocity_forced", "agent", "velocity"), &NavigationServer2D::agent_set_velocity_forced);
148 ClassDB::bind_method(D_METHOD("agent_set_velocity", "agent", "velocity"), &NavigationServer2D::agent_set_velocity);
149 ClassDB::bind_method(D_METHOD("agent_get_velocity", "agent"), &NavigationServer2D::agent_get_velocity);
150 ClassDB::bind_method(D_METHOD("agent_set_position", "agent", "position"), &NavigationServer2D::agent_set_position);
151 ClassDB::bind_method(D_METHOD("agent_get_position", "agent"), &NavigationServer2D::agent_get_position);
152 ClassDB::bind_method(D_METHOD("agent_is_map_changed", "agent"), &NavigationServer2D::agent_is_map_changed);
153 ClassDB::bind_method(D_METHOD("agent_set_avoidance_callback", "agent", "callback"), &NavigationServer2D::agent_set_avoidance_callback);
154 ClassDB::bind_method(D_METHOD("agent_has_avoidance_callback", "agent"), &NavigationServer2D::agent_has_avoidance_callback);
155 ClassDB::bind_method(D_METHOD("agent_set_avoidance_layers", "agent", "layers"), &NavigationServer2D::agent_set_avoidance_layers);
156 ClassDB::bind_method(D_METHOD("agent_get_avoidance_layers", "agent"), &NavigationServer2D::agent_get_avoidance_layers);
157 ClassDB::bind_method(D_METHOD("agent_set_avoidance_mask", "agent", "mask"), &NavigationServer2D::agent_set_avoidance_mask);
158 ClassDB::bind_method(D_METHOD("agent_get_avoidance_mask", "agent"), &NavigationServer2D::agent_get_avoidance_mask);
159 ClassDB::bind_method(D_METHOD("agent_set_avoidance_priority", "agent", "priority"), &NavigationServer2D::agent_set_avoidance_priority);
160 ClassDB::bind_method(D_METHOD("agent_get_avoidance_priority", "agent"), &NavigationServer2D::agent_get_avoidance_priority);
161
162 ClassDB::bind_method(D_METHOD("obstacle_create"), &NavigationServer2D::obstacle_create);
163 ClassDB::bind_method(D_METHOD("obstacle_set_avoidance_enabled", "obstacle", "enabled"), &NavigationServer2D::obstacle_set_avoidance_enabled);
164 ClassDB::bind_method(D_METHOD("obstacle_get_avoidance_enabled", "obstacle"), &NavigationServer2D::obstacle_get_avoidance_enabled);
165 ClassDB::bind_method(D_METHOD("obstacle_set_map", "obstacle", "map"), &NavigationServer2D::obstacle_set_map);
166 ClassDB::bind_method(D_METHOD("obstacle_get_map", "obstacle"), &NavigationServer2D::obstacle_get_map);
167 ClassDB::bind_method(D_METHOD("obstacle_set_paused", "obstacle", "paused"), &NavigationServer2D::obstacle_set_paused);
168 ClassDB::bind_method(D_METHOD("obstacle_get_paused", "obstacle"), &NavigationServer2D::obstacle_get_paused);
169 ClassDB::bind_method(D_METHOD("obstacle_set_radius", "obstacle", "radius"), &NavigationServer2D::obstacle_set_radius);
170 ClassDB::bind_method(D_METHOD("obstacle_get_radius", "obstacle"), &NavigationServer2D::obstacle_get_radius);
171 ClassDB::bind_method(D_METHOD("obstacle_set_velocity", "obstacle", "velocity"), &NavigationServer2D::obstacle_set_velocity);
172 ClassDB::bind_method(D_METHOD("obstacle_get_velocity", "obstacle"), &NavigationServer2D::obstacle_get_velocity);
173 ClassDB::bind_method(D_METHOD("obstacle_set_position", "obstacle", "position"), &NavigationServer2D::obstacle_set_position);
174 ClassDB::bind_method(D_METHOD("obstacle_get_position", "obstacle"), &NavigationServer2D::obstacle_get_position);
175 ClassDB::bind_method(D_METHOD("obstacle_set_vertices", "obstacle", "vertices"), &NavigationServer2D::obstacle_set_vertices);
176 ClassDB::bind_method(D_METHOD("obstacle_get_vertices", "obstacle"), &NavigationServer2D::obstacle_get_vertices);
177 ClassDB::bind_method(D_METHOD("obstacle_set_avoidance_layers", "obstacle", "layers"), &NavigationServer2D::obstacle_set_avoidance_layers);
178 ClassDB::bind_method(D_METHOD("obstacle_get_avoidance_layers", "obstacle"), &NavigationServer2D::obstacle_get_avoidance_layers);
179
180 ClassDB::bind_method(D_METHOD("parse_source_geometry_data", "navigation_polygon", "source_geometry_data", "root_node", "callback"), &NavigationServer2D::parse_source_geometry_data, DEFVAL(Callable()));
181 ClassDB::bind_method(D_METHOD("bake_from_source_geometry_data", "navigation_polygon", "source_geometry_data", "callback"), &NavigationServer2D::bake_from_source_geometry_data, DEFVAL(Callable()));
182 ClassDB::bind_method(D_METHOD("bake_from_source_geometry_data_async", "navigation_polygon", "source_geometry_data", "callback"), &NavigationServer2D::bake_from_source_geometry_data_async, DEFVAL(Callable()));
183 ClassDB::bind_method(D_METHOD("is_baking_navigation_polygon", "navigation_polygon"), &NavigationServer2D::is_baking_navigation_polygon);
184
185 ClassDB::bind_method(D_METHOD("source_geometry_parser_create"), &NavigationServer2D::source_geometry_parser_create);
186 ClassDB::bind_method(D_METHOD("source_geometry_parser_set_callback", "parser", "callback"), &NavigationServer2D::source_geometry_parser_set_callback);
187
188 ClassDB::bind_method(D_METHOD("simplify_path", "path", "epsilon"), &NavigationServer2D::simplify_path);
189
190 ClassDB::bind_method(D_METHOD("free_rid", "rid"), &NavigationServer2D::free_rid);
191
192 ClassDB::bind_method(D_METHOD("set_active", "active"), &NavigationServer2D::set_active);
193
194 ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationServer2D::set_debug_enabled);
195 ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationServer2D::get_debug_enabled);
196
197 ADD_SIGNAL(MethodInfo("map_changed", PropertyInfo(Variant::RID, "map")));
198
199 ADD_SIGNAL(MethodInfo("navigation_debug_changed"));
200 ADD_SIGNAL(MethodInfo("avoidance_debug_changed"));
201
202 ClassDB::bind_method(D_METHOD("get_process_info", "process_info"), &NavigationServer2D::get_process_info);
203
204 BIND_ENUM_CONSTANT(INFO_ACTIVE_MAPS);
205 BIND_ENUM_CONSTANT(INFO_REGION_COUNT);
206 BIND_ENUM_CONSTANT(INFO_AGENT_COUNT);
207 BIND_ENUM_CONSTANT(INFO_LINK_COUNT);
208 BIND_ENUM_CONSTANT(INFO_POLYGON_COUNT);
209 BIND_ENUM_CONSTANT(INFO_EDGE_COUNT);
210 BIND_ENUM_CONSTANT(INFO_EDGE_MERGE_COUNT);
211 BIND_ENUM_CONSTANT(INFO_EDGE_CONNECTION_COUNT);
212 BIND_ENUM_CONSTANT(INFO_EDGE_FREE_COUNT);
213 BIND_ENUM_CONSTANT(INFO_OBSTACLE_COUNT);
214 }
215
216 NavigationServer2D *NavigationServer2D::get_singleton() {
217 return singleton;
218 }
219
220 NavigationServer2D::NavigationServer2D() {
221 ERR_FAIL_COND(singleton != nullptr);
222 singleton = this;
223
224 GLOBAL_DEF_BASIC(PropertyInfo(Variant::FLOAT, "navigation/2d/default_cell_size", PROPERTY_HINT_RANGE, NavigationDefaults2D::NAV_MESH_CELL_SIZE_HINT), NavigationDefaults2D::NAV_MESH_CELL_SIZE);
225 GLOBAL_DEF("navigation/2d/use_edge_connections", true);
226 GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "navigation/2d/merge_rasterizer_cell_scale", PROPERTY_HINT_RANGE, "0.001,1,0.001,or_greater"), 1.0);
227 GLOBAL_DEF_BASIC(PropertyInfo(Variant::FLOAT, "navigation/2d/default_edge_connection_margin", PROPERTY_HINT_RANGE, "0.01,10,0.001,or_greater"), NavigationDefaults2D::EDGE_CONNECTION_MARGIN);
228 GLOBAL_DEF_BASIC(PropertyInfo(Variant::FLOAT, "navigation/2d/default_link_connection_radius", PROPERTY_HINT_RANGE, "0.01,10,0.001,or_greater"), NavigationDefaults2D::LINK_CONNECTION_RADIUS);
229
230 #ifdef DEBUG_ENABLED
231 debug_navigation_edge_connection_color = GLOBAL_DEF("debug/shapes/navigation/2d/edge_connection_color", Color(1.0, 0.0, 1.0, 1.0));
232 debug_navigation_geometry_edge_color = GLOBAL_DEF("debug/shapes/navigation/2d/geometry_edge_color", Color(0.5, 1.0, 1.0, 1.0));
233 debug_navigation_geometry_face_color = GLOBAL_DEF("debug/shapes/navigation/2d/geometry_face_color", Color(0.5, 1.0, 1.0, 0.4));
234 debug_navigation_geometry_edge_disabled_color = GLOBAL_DEF("debug/shapes/navigation/2d/geometry_edge_disabled_color", Color(0.5, 0.5, 0.5, 1.0));
235 debug_navigation_geometry_face_disabled_color = GLOBAL_DEF("debug/shapes/navigation/2d/geometry_face_disabled_color", Color(0.5, 0.5, 0.5, 0.4));
236 debug_navigation_link_connection_color = GLOBAL_DEF("debug/shapes/navigation/2d/link_connection_color", Color(1.0, 0.5, 1.0, 1.0));
237 debug_navigation_link_connection_disabled_color = GLOBAL_DEF("debug/shapes/navigation/2d/link_connection_disabled_color", Color(0.5, 0.5, 0.5, 1.0));
238 debug_navigation_agent_path_color = GLOBAL_DEF("debug/shapes/navigation/2d/agent_path_color", Color(1.0, 0.0, 0.0, 1.0));
239
240 debug_navigation_enable_edge_connections = GLOBAL_DEF("debug/shapes/navigation/2d/enable_edge_connections", true);
241 debug_navigation_enable_edge_lines = GLOBAL_DEF("debug/shapes/navigation/2d/enable_edge_lines", true);
242 debug_navigation_enable_geometry_face_random_color = GLOBAL_DEF("debug/shapes/navigation/2d/enable_geometry_face_random_color", true);
243 debug_navigation_enable_link_connections = GLOBAL_DEF("debug/shapes/navigation/2d/enable_link_connections", true);
244
245 debug_navigation_enable_agent_paths = GLOBAL_DEF("debug/shapes/navigation/2d/enable_agent_paths", true);
246 debug_navigation_agent_path_point_size = GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "debug/shapes/navigation/2d/agent_path_point_size", PROPERTY_HINT_RANGE, "0.01,10,0.001,or_greater"), 4.0);
247
248 debug_navigation_avoidance_agents_radius_color = GLOBAL_DEF("debug/shapes/avoidance/2d/agents_radius_color", Color(1.0, 1.0, 0.0, 0.25));
249 debug_navigation_avoidance_obstacles_radius_color = GLOBAL_DEF("debug/shapes/avoidance/2d/obstacles_radius_color", Color(1.0, 0.5, 0.0, 0.25));
250 debug_navigation_avoidance_static_obstacle_pushin_face_color = GLOBAL_DEF("debug/shapes/avoidance/2d/obstacles_static_face_pushin_color", Color(1.0, 0.0, 0.0, 0.0));
251 debug_navigation_avoidance_static_obstacle_pushin_edge_color = GLOBAL_DEF("debug/shapes/avoidance/2d/obstacles_static_edge_pushin_color", Color(1.0, 0.0, 0.0, 1.0));
252 debug_navigation_avoidance_static_obstacle_pushout_face_color = GLOBAL_DEF("debug/shapes/avoidance/2d/obstacles_static_face_pushout_color", Color(1.0, 1.0, 0.0, 0.5));
253 debug_navigation_avoidance_static_obstacle_pushout_edge_color = GLOBAL_DEF("debug/shapes/avoidance/2d/obstacles_static_edge_pushout_color", Color(1.0, 1.0, 0.0, 1.0));
254 debug_navigation_avoidance_enable_agents_radius = GLOBAL_DEF("debug/shapes/avoidance/2d/enable_agents_radius", true);
255 debug_navigation_avoidance_enable_obstacles_radius = GLOBAL_DEF("debug/shapes/avoidance/2d/enable_obstacles_radius", true);
256 debug_navigation_avoidance_enable_obstacles_static = GLOBAL_DEF("debug/shapes/avoidance/2d/enable_obstacles_static", true);
257
258 if (Engine::get_singleton()->is_editor_hint()) {
259 // Enable NavigationServer2D when in Editor or navigation mesh edge connections are invisible.
260 // On runtime tests SceneTree has "Visible Navigation" set and main iteration takes care of this.
261 set_debug_enabled(true);
262 set_debug_navigation_enabled(true);
263 set_debug_avoidance_enabled(true);
264 }
265 #endif // DEBUG_ENABLED
266 }
267
268 NavigationServer2D::~NavigationServer2D() {
269 singleton = nullptr;
270
271 RWLockWrite write_lock(geometry_parser_rwlock);
272 for (NavMeshGeometryParser2D *parser : generator_parsers) {
273 geometry_parser_owner.free(parser->self);
274 }
275 generator_parsers.clear();
276 }
277
278 RID NavigationServer2D::source_geometry_parser_create() {
279 RWLockWrite write_lock(geometry_parser_rwlock);
280
281 RID rid = geometry_parser_owner.make_rid();
282
283 NavMeshGeometryParser2D *parser = geometry_parser_owner.get_or_null(rid);
284 parser->self = rid;
285
286 generator_parsers.push_back(parser);
287
288 return rid;
289 }
290
291 void NavigationServer2D::free_rid(RID p_rid) {
292 if (!geometry_parser_owner.owns(p_rid)) {
293 return;
294 }
295 RWLockWrite write_lock(geometry_parser_rwlock);
296
297 NavMeshGeometryParser2D *parser = geometry_parser_owner.get_or_null(p_rid);
298 ERR_FAIL_NULL(parser);
299
300 generator_parsers.erase(parser);
301 geometry_parser_owner.free(parser->self);
302 }
303
304 void NavigationServer2D::source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) {
305 RWLockWrite write_lock(geometry_parser_rwlock);
306
307 NavMeshGeometryParser2D *parser = geometry_parser_owner.get_or_null(p_parser);
308 ERR_FAIL_NULL(parser);
309
310 parser->callback = p_callback;
311 }
312
313 void NavigationServer2D::set_debug_enabled(bool p_enabled) {
314 #ifdef DEBUG_ENABLED
315 if (debug_enabled != p_enabled) {
316 debug_dirty = true;
317 }
318
319 debug_enabled = p_enabled;
320
321 if (debug_dirty) {
322 navigation_debug_dirty = true;
323 callable_mp(this, &NavigationServer2D::_emit_navigation_debug_changed_signal).call_deferred();
324
325 avoidance_debug_dirty = true;
326 callable_mp(this, &NavigationServer2D::_emit_avoidance_debug_changed_signal).call_deferred();
327 }
328 #endif // DEBUG_ENABLED
329 }
330
331 bool NavigationServer2D::get_debug_enabled() const {
332 return debug_enabled;
333 }
334
335 #ifdef DEBUG_ENABLED
336 void NavigationServer2D::_emit_navigation_debug_changed_signal() {
337 if (navigation_debug_dirty) {
338 navigation_debug_dirty = false;
339 emit_signal(SNAME("navigation_debug_changed"));
340 }
341 }
342
343 void NavigationServer2D::_emit_avoidance_debug_changed_signal() {
344 if (avoidance_debug_dirty) {
345 avoidance_debug_dirty = false;
346 emit_signal(SNAME("avoidance_debug_changed"));
347 }
348 }
349 #endif // DEBUG_ENABLED
350
351 #ifdef DEBUG_ENABLED
352 void NavigationServer2D::set_debug_navigation_enabled(bool p_enabled) {
353 debug_navigation_enabled = p_enabled;
354 navigation_debug_dirty = true;
355 callable_mp(this, &NavigationServer2D::_emit_navigation_debug_changed_signal).call_deferred();
356 }
357
358 bool NavigationServer2D::get_debug_navigation_enabled() const {
359 return debug_navigation_enabled;
360 }
361
362 void NavigationServer2D::set_debug_avoidance_enabled(bool p_enabled) {
363 debug_avoidance_enabled = p_enabled;
364 avoidance_debug_dirty = true;
365 callable_mp(this, &NavigationServer2D::_emit_avoidance_debug_changed_signal).call_deferred();
366 }
367
368 bool NavigationServer2D::get_debug_avoidance_enabled() const {
369 return debug_avoidance_enabled;
370 }
371
372 void NavigationServer2D::set_debug_navigation_edge_connection_color(const Color &p_color) {
373 debug_navigation_edge_connection_color = p_color;
374 }
375
376 Color NavigationServer2D::get_debug_navigation_edge_connection_color() const {
377 return debug_navigation_edge_connection_color;
378 }
379
380 void NavigationServer2D::set_debug_navigation_geometry_face_color(const Color &p_color) {
381 debug_navigation_geometry_face_color = p_color;
382 }
383
384 Color NavigationServer2D::get_debug_navigation_geometry_face_color() const {
385 return debug_navigation_geometry_face_color;
386 }
387
388 void NavigationServer2D::set_debug_navigation_geometry_face_disabled_color(const Color &p_color) {
389 debug_navigation_geometry_face_disabled_color = p_color;
390 }
391
392 Color NavigationServer2D::get_debug_navigation_geometry_face_disabled_color() const {
393 return debug_navigation_geometry_face_disabled_color;
394 }
395
396 void NavigationServer2D::set_debug_navigation_link_connection_color(const Color &p_color) {
397 debug_navigation_link_connection_color = p_color;
398 }
399
400 Color NavigationServer2D::get_debug_navigation_link_connection_color() const {
401 return debug_navigation_link_connection_color;
402 }
403
404 void NavigationServer2D::set_debug_navigation_link_connection_disabled_color(const Color &p_color) {
405 debug_navigation_link_connection_disabled_color = p_color;
406 }
407
408 Color NavigationServer2D::get_debug_navigation_link_connection_disabled_color() const {
409 return debug_navigation_link_connection_disabled_color;
410 }
411
412 void NavigationServer2D::set_debug_navigation_geometry_edge_color(const Color &p_color) {
413 debug_navigation_geometry_edge_color = p_color;
414 }
415
416 Color NavigationServer2D::get_debug_navigation_geometry_edge_color() const {
417 return debug_navigation_geometry_edge_color;
418 }
419
420 void NavigationServer2D::set_debug_navigation_geometry_edge_disabled_color(const Color &p_color) {
421 debug_navigation_geometry_edge_disabled_color = p_color;
422 }
423
424 Color NavigationServer2D::get_debug_navigation_geometry_edge_disabled_color() const {
425 return debug_navigation_geometry_edge_disabled_color;
426 }
427
428 void NavigationServer2D::set_debug_navigation_enable_edge_connections(const bool p_value) {
429 debug_navigation_enable_edge_connections = p_value;
430 }
431
432 bool NavigationServer2D::get_debug_navigation_enable_edge_connections() const {
433 return debug_navigation_enable_edge_connections;
434 }
435
436 void NavigationServer2D::set_debug_navigation_enable_geometry_face_random_color(const bool p_value) {
437 debug_navigation_enable_geometry_face_random_color = p_value;
438 }
439
440 bool NavigationServer2D::get_debug_navigation_enable_geometry_face_random_color() const {
441 return debug_navigation_enable_geometry_face_random_color;
442 }
443
444 void NavigationServer2D::set_debug_navigation_enable_edge_lines(const bool p_value) {
445 debug_navigation_enable_edge_lines = p_value;
446 }
447
448 bool NavigationServer2D::get_debug_navigation_enable_edge_lines() const {
449 return debug_navigation_enable_edge_lines;
450 }
451
452 void NavigationServer2D::set_debug_navigation_agent_path_color(const Color &p_color) {
453 debug_navigation_agent_path_color = p_color;
454 }
455
456 Color NavigationServer2D::get_debug_navigation_agent_path_color() const {
457 return debug_navigation_agent_path_color;
458 }
459
460 void NavigationServer2D::set_debug_navigation_enable_agent_paths(const bool p_value) {
461 debug_navigation_enable_agent_paths = p_value;
462 }
463
464 bool NavigationServer2D::get_debug_navigation_enable_agent_paths() const {
465 return debug_navigation_enable_agent_paths;
466 }
467
468 void NavigationServer2D::set_debug_navigation_agent_path_point_size(real_t p_point_size) {
469 debug_navigation_agent_path_point_size = p_point_size;
470 }
471
472 real_t NavigationServer2D::get_debug_navigation_agent_path_point_size() const {
473 return debug_navigation_agent_path_point_size;
474 }
475
476 void NavigationServer2D::set_debug_navigation_avoidance_enable_agents_radius(const bool p_value) {
477 debug_navigation_avoidance_enable_agents_radius = p_value;
478 }
479
480 bool NavigationServer2D::get_debug_navigation_avoidance_enable_agents_radius() const {
481 return debug_navigation_avoidance_enable_agents_radius;
482 }
483
484 void NavigationServer2D::set_debug_navigation_avoidance_enable_obstacles_radius(const bool p_value) {
485 debug_navigation_avoidance_enable_obstacles_radius = p_value;
486 }
487
488 bool NavigationServer2D::get_debug_navigation_avoidance_enable_obstacles_radius() const {
489 return debug_navigation_avoidance_enable_obstacles_radius;
490 }
491
492 void NavigationServer2D::set_debug_navigation_avoidance_agents_radius_color(const Color &p_color) {
493 debug_navigation_avoidance_agents_radius_color = p_color;
494 }
495
496 Color NavigationServer2D::get_debug_navigation_avoidance_agents_radius_color() const {
497 return debug_navigation_avoidance_agents_radius_color;
498 }
499
500 void NavigationServer2D::set_debug_navigation_avoidance_obstacles_radius_color(const Color &p_color) {
501 debug_navigation_avoidance_obstacles_radius_color = p_color;
502 }
503
504 Color NavigationServer2D::get_debug_navigation_avoidance_obstacles_radius_color() const {
505 return debug_navigation_avoidance_obstacles_radius_color;
506 }
507
508 void NavigationServer2D::set_debug_navigation_avoidance_static_obstacle_pushin_face_color(const Color &p_color) {
509 debug_navigation_avoidance_static_obstacle_pushin_face_color = p_color;
510 }
511
512 Color NavigationServer2D::get_debug_navigation_avoidance_static_obstacle_pushin_face_color() const {
513 return debug_navigation_avoidance_static_obstacle_pushin_face_color;
514 }
515
516 void NavigationServer2D::set_debug_navigation_avoidance_static_obstacle_pushout_face_color(const Color &p_color) {
517 debug_navigation_avoidance_static_obstacle_pushout_face_color = p_color;
518 }
519
520 Color NavigationServer2D::get_debug_navigation_avoidance_static_obstacle_pushout_face_color() const {
521 return debug_navigation_avoidance_static_obstacle_pushout_face_color;
522 }
523
524 void NavigationServer2D::set_debug_navigation_avoidance_static_obstacle_pushin_edge_color(const Color &p_color) {
525 debug_navigation_avoidance_static_obstacle_pushin_edge_color = p_color;
526 }
527
528 Color NavigationServer2D::get_debug_navigation_avoidance_static_obstacle_pushin_edge_color() const {
529 return debug_navigation_avoidance_static_obstacle_pushin_edge_color;
530 }
531
532 void NavigationServer2D::set_debug_navigation_avoidance_static_obstacle_pushout_edge_color(const Color &p_color) {
533 debug_navigation_avoidance_static_obstacle_pushout_edge_color = p_color;
534 }
535
536 Color NavigationServer2D::get_debug_navigation_avoidance_static_obstacle_pushout_edge_color() const {
537 return debug_navigation_avoidance_static_obstacle_pushout_edge_color;
538 }
539
540 void NavigationServer2D::set_debug_navigation_avoidance_enable_obstacles_static(const bool p_value) {
541 debug_navigation_avoidance_enable_obstacles_static = p_value;
542 }
543
544 bool NavigationServer2D::get_debug_navigation_avoidance_enable_obstacles_static() const {
545 return debug_navigation_avoidance_enable_obstacles_static;
546 }
547 #endif // DEBUG_ENABLED
548
549 ///////////////////////////////////////////////////////
550
551 static NavigationServer2D *navigation_server_2d = nullptr;
552
553 void NavigationServer2DManager::initialize_server() {
554 ERR_FAIL_COND(navigation_server_2d != nullptr);
555
556 // Init 2D Navigation Server.
557 navigation_server_2d = NavigationServer2DManager::get_singleton()->new_server(
558 GLOBAL_GET(NavigationServer2DManager::setting_property_name));
559 if (!navigation_server_2d) {
560 // Navigation server not found, use the default.
561 navigation_server_2d = NavigationServer2DManager::get_singleton()->new_default_server();
562 }
563
564 // Fall back to dummy if no default server has been registered.
565 if (!navigation_server_2d) {
566 WARN_VERBOSE("Failed to initialize NavigationServer2D. Fall back to dummy server.");
567 navigation_server_2d = memnew(NavigationServer2DDummy);
568 }
569
570 // Should be impossible, but make sure it's not null.
571 ERR_FAIL_NULL_MSG(navigation_server_2d, "Failed to initialize NavigationServer2D.");
572 navigation_server_2d->init();
573 }
574
575 void NavigationServer2DManager::finalize_server() {
576 ERR_FAIL_NULL(navigation_server_2d);
577 navigation_server_2d->finish();
578 memdelete(navigation_server_2d);
579 navigation_server_2d = nullptr;
580 }
581
582 const String NavigationServer2DManager::setting_property_name(PNAME("navigation/2d/navigation_engine"));
583
584 void NavigationServer2DManager::on_servers_changed() {
585 String navigation_servers_enum_str("DEFAULT");
586 for (int i = get_servers_count() - 1; 0 <= i; --i) {
587 navigation_servers_enum_str += "," + get_server_name(i);
588 }
589 ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, navigation_servers_enum_str));
590 ProjectSettings::get_singleton()->set_restart_if_changed(setting_property_name, true);
591 ProjectSettings::get_singleton()->set_as_basic(setting_property_name, true);
592 }
593
594 void NavigationServer2DManager::_bind_methods() {
595 ClassDB::bind_method(D_METHOD("register_server", "name", "create_callback"), &NavigationServer2DManager::register_server);
596 ClassDB::bind_method(D_METHOD("set_default_server", "name", "priority"), &NavigationServer2DManager::set_default_server);
597 }
598
599 NavigationServer2DManager *NavigationServer2DManager::get_singleton() {
600 return singleton;
601 }
602
603 void NavigationServer2DManager::register_server(const String &p_name, const Callable &p_create_callback) {
604 ERR_FAIL_COND(find_server_id(p_name) != -1);
605 navigation_servers.push_back(ClassInfo(p_name, p_create_callback));
606 on_servers_changed();
607 }
608
609 void NavigationServer2DManager::set_default_server(const String &p_name, int p_priority) {
610 const int id = find_server_id(p_name);
611 ERR_FAIL_COND(id == -1); // Not found
612 if (default_server_priority < p_priority) {
613 default_server_id = id;
614 default_server_priority = p_priority;
615 }
616 }
617
618 int NavigationServer2DManager::find_server_id(const String &p_name) {
619 for (int i = navigation_servers.size() - 1; 0 <= i; --i) {
620 if (p_name == navigation_servers[i].name) {
621 return i;
622 }
623 }
624 return -1;
625 }
626
627 int NavigationServer2DManager::get_servers_count() {
628 return navigation_servers.size();
629 }
630
631 String NavigationServer2DManager::get_server_name(int p_id) {
632 ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
633 return navigation_servers[p_id].name;
634 }
635
636 NavigationServer2D *NavigationServer2DManager::new_default_server() {
637 if (default_server_id == -1) {
638 return nullptr;
639 }
640 Variant ret;
641 Callable::CallError ce;
642 navigation_servers[default_server_id].create_callback.callp(nullptr, 0, ret, ce);
643 ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
644 return Object::cast_to<NavigationServer2D>(ret.get_validated_object());
645 }
646
647 NavigationServer2D *NavigationServer2DManager::new_server(const String &p_name) {
648 int id = find_server_id(p_name);
649 if (id == -1) {
650 return nullptr;
651 } else {
652 Variant ret;
653 Callable::CallError ce;
654 navigation_servers[id].create_callback.callp(nullptr, 0, ret, ce);
655 ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
656 return Object::cast_to<NavigationServer2D>(ret.get_validated_object());
657 }
658 }
659
660 NavigationServer2D *NavigationServer2DManager::create_dummy_server_callback() {
661 return memnew(NavigationServer2DDummy);
662 }
663
664 NavigationServer2DManager::NavigationServer2DManager() {
665 }
666
667 NavigationServer2DManager::~NavigationServer2DManager() {
668 }
669
670 void NavigationServer2DManager::initialize_server_manager() {
671 ERR_FAIL_COND(singleton != nullptr);
672 singleton = memnew(NavigationServer2DManager);
673 }
674
675 void NavigationServer2DManager::finalize_server_manager() {
676 ERR_FAIL_NULL(singleton);
677 memdelete(singleton);
678 }