1/**************************************************************************/
2/* godot_navigation_server.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 GODOT_NAVIGATION_SERVER_H
32#define GODOT_NAVIGATION_SERVER_H
33
34#include "nav_agent.h"
35#include "nav_link.h"
36#include "nav_map.h"
37#include "nav_obstacle.h"
38#include "nav_region.h"
39
40#include "core/templates/local_vector.h"
41#include "core/templates/rid.h"
42#include "core/templates/rid_owner.h"
43#include "servers/navigation_server_3d.h"
44
45/// The commands are functions executed during the `sync` phase.
46
47#define MERGE_INTERNAL(A, B) A##B
48#define MERGE(A, B) MERGE_INTERNAL(A, B)
49
50#define COMMAND_1(F_NAME, T_0, D_0) \
51 virtual void F_NAME(T_0 D_0) override; \
52 void MERGE(_cmd_, F_NAME)(T_0 D_0)
53
54#define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
55 virtual void F_NAME(T_0 D_0, T_1 D_1) override; \
56 void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)
57
58class GodotNavigationServer;
59#ifndef _3D_DISABLED
60class NavMeshGenerator3D;
61#endif // _3D_DISABLED
62
63struct SetCommand {
64 virtual ~SetCommand() {}
65 virtual void exec(GodotNavigationServer *server) = 0;
66};
67
68class GodotNavigationServer : public NavigationServer3D {
69 Mutex commands_mutex;
70 /// Mutex used to make any operation threadsafe.
71 Mutex operations_mutex;
72
73 LocalVector<SetCommand *> commands;
74
75 mutable RID_Owner<NavLink> link_owner;
76 mutable RID_Owner<NavMap> map_owner;
77 mutable RID_Owner<NavRegion> region_owner;
78 mutable RID_Owner<NavAgent> agent_owner;
79 mutable RID_Owner<NavObstacle> obstacle_owner;
80
81 bool active = true;
82 LocalVector<NavMap *> active_maps;
83 LocalVector<uint32_t> active_maps_update_id;
84
85#ifndef _3D_DISABLED
86 NavMeshGenerator3D *navmesh_generator_3d = nullptr;
87#endif // _3D_DISABLED
88
89 // Performance Monitor
90 int pm_region_count = 0;
91 int pm_agent_count = 0;
92 int pm_link_count = 0;
93 int pm_polygon_count = 0;
94 int pm_edge_count = 0;
95 int pm_edge_merge_count = 0;
96 int pm_edge_connection_count = 0;
97 int pm_edge_free_count = 0;
98
99public:
100 GodotNavigationServer();
101 virtual ~GodotNavigationServer();
102
103 void add_command(SetCommand *command);
104
105 virtual TypedArray<RID> get_maps() const override;
106
107 virtual RID map_create() override;
108 COMMAND_2(map_set_active, RID, p_map, bool, p_active);
109 virtual bool map_is_active(RID p_map) const override;
110
111 COMMAND_2(map_set_up, RID, p_map, Vector3, p_up);
112 virtual Vector3 map_get_up(RID p_map) const override;
113
114 COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size);
115 virtual real_t map_get_cell_size(RID p_map) const override;
116
117 COMMAND_2(map_set_cell_height, RID, p_map, real_t, p_cell_height);
118 virtual real_t map_get_cell_height(RID p_map) const override;
119
120 COMMAND_2(map_set_use_edge_connections, RID, p_map, bool, p_enabled);
121 virtual bool map_get_use_edge_connections(RID p_map) const override;
122
123 COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin);
124 virtual real_t map_get_edge_connection_margin(RID p_map) const override;
125
126 COMMAND_2(map_set_link_connection_radius, RID, p_map, real_t, p_connection_radius);
127 virtual real_t map_get_link_connection_radius(RID p_map) const override;
128
129 virtual Vector<Vector3> map_get_path(RID p_map, Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) const override;
130
131 virtual Vector3 map_get_closest_point_to_segment(RID p_map, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision = false) const override;
132 virtual Vector3 map_get_closest_point(RID p_map, const Vector3 &p_point) const override;
133 virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const override;
134 virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const override;
135
136 virtual TypedArray<RID> map_get_links(RID p_map) const override;
137 virtual TypedArray<RID> map_get_regions(RID p_map) const override;
138 virtual TypedArray<RID> map_get_agents(RID p_map) const override;
139 virtual TypedArray<RID> map_get_obstacles(RID p_map) const override;
140
141 virtual void map_force_update(RID p_map) override;
142
143 virtual RID region_create() override;
144
145 COMMAND_2(region_set_enabled, RID, p_region, bool, p_enabled);
146 virtual bool region_get_enabled(RID p_region) const override;
147
148 COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled);
149 virtual bool region_get_use_edge_connections(RID p_region) const override;
150
151 COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost);
152 virtual real_t region_get_enter_cost(RID p_region) const override;
153 COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost);
154 virtual real_t region_get_travel_cost(RID p_region) const override;
155
156 COMMAND_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id);
157 virtual ObjectID region_get_owner_id(RID p_region) const override;
158
159 virtual bool region_owns_point(RID p_region, const Vector3 &p_point) const override;
160
161 COMMAND_2(region_set_map, RID, p_region, RID, p_map);
162 virtual RID region_get_map(RID p_region) const override;
163 COMMAND_2(region_set_navigation_layers, RID, p_region, uint32_t, p_navigation_layers);
164 virtual uint32_t region_get_navigation_layers(RID p_region) const override;
165 COMMAND_2(region_set_transform, RID, p_region, Transform3D, p_transform);
166 COMMAND_2(region_set_navigation_mesh, RID, p_region, Ref<NavigationMesh>, p_navigation_mesh);
167#ifndef DISABLE_DEPRECATED
168 virtual void region_bake_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh, Node *p_root_node) override;
169#endif // DISABLE_DEPRECATED
170 virtual int region_get_connections_count(RID p_region) const override;
171 virtual Vector3 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
172 virtual Vector3 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
173
174 virtual RID link_create() override;
175 COMMAND_2(link_set_map, RID, p_link, RID, p_map);
176 virtual RID link_get_map(RID p_link) const override;
177 COMMAND_2(link_set_enabled, RID, p_link, bool, p_enabled);
178 virtual bool link_get_enabled(RID p_link) const override;
179 COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional);
180 virtual bool link_is_bidirectional(RID p_link) const override;
181 COMMAND_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers);
182 virtual uint32_t link_get_navigation_layers(RID p_link) const override;
183 COMMAND_2(link_set_start_position, RID, p_link, Vector3, p_position);
184 virtual Vector3 link_get_start_position(RID p_link) const override;
185 COMMAND_2(link_set_end_position, RID, p_link, Vector3, p_position);
186 virtual Vector3 link_get_end_position(RID p_link) const override;
187 COMMAND_2(link_set_enter_cost, RID, p_link, real_t, p_enter_cost);
188 virtual real_t link_get_enter_cost(RID p_link) const override;
189 COMMAND_2(link_set_travel_cost, RID, p_link, real_t, p_travel_cost);
190 virtual real_t link_get_travel_cost(RID p_link) const override;
191 COMMAND_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id);
192 virtual ObjectID link_get_owner_id(RID p_link) const override;
193
194 virtual RID agent_create() override;
195 COMMAND_2(agent_set_avoidance_enabled, RID, p_agent, bool, p_enabled);
196 virtual bool agent_get_avoidance_enabled(RID p_agent) const override;
197 COMMAND_2(agent_set_use_3d_avoidance, RID, p_agent, bool, p_enabled);
198 virtual bool agent_get_use_3d_avoidance(RID p_agent) const override;
199 COMMAND_2(agent_set_map, RID, p_agent, RID, p_map);
200 virtual RID agent_get_map(RID p_agent) const override;
201 COMMAND_2(agent_set_paused, RID, p_agent, bool, p_paused);
202 virtual bool agent_get_paused(RID p_agent) const override;
203 COMMAND_2(agent_set_neighbor_distance, RID, p_agent, real_t, p_distance);
204 COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count);
205 COMMAND_2(agent_set_time_horizon_agents, RID, p_agent, real_t, p_time_horizon);
206 COMMAND_2(agent_set_time_horizon_obstacles, RID, p_agent, real_t, p_time_horizon);
207 COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius);
208 COMMAND_2(agent_set_height, RID, p_agent, real_t, p_height);
209 COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed);
210 COMMAND_2(agent_set_velocity, RID, p_agent, Vector3, p_velocity);
211 COMMAND_2(agent_set_velocity_forced, RID, p_agent, Vector3, p_velocity);
212 COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position);
213 virtual bool agent_is_map_changed(RID p_agent) const override;
214 COMMAND_2(agent_set_avoidance_callback, RID, p_agent, Callable, p_callback);
215 COMMAND_2(agent_set_avoidance_layers, RID, p_agent, uint32_t, p_layers);
216 COMMAND_2(agent_set_avoidance_mask, RID, p_agent, uint32_t, p_mask);
217 COMMAND_2(agent_set_avoidance_priority, RID, p_agent, real_t, p_priority);
218
219 virtual RID obstacle_create() override;
220 COMMAND_2(obstacle_set_avoidance_enabled, RID, p_obstacle, bool, p_enabled);
221 virtual bool obstacle_get_avoidance_enabled(RID p_obstacle) const override;
222 COMMAND_2(obstacle_set_use_3d_avoidance, RID, p_obstacle, bool, p_enabled);
223 virtual bool obstacle_get_use_3d_avoidance(RID p_obstacle) const override;
224 COMMAND_2(obstacle_set_map, RID, p_obstacle, RID, p_map);
225 virtual RID obstacle_get_map(RID p_obstacle) const override;
226 COMMAND_2(obstacle_set_paused, RID, p_obstacle, bool, p_paused);
227 virtual bool obstacle_get_paused(RID p_obstacle) const override;
228 COMMAND_2(obstacle_set_radius, RID, p_obstacle, real_t, p_radius);
229 COMMAND_2(obstacle_set_velocity, RID, p_obstacle, Vector3, p_velocity);
230 COMMAND_2(obstacle_set_position, RID, p_obstacle, Vector3, p_position);
231 COMMAND_2(obstacle_set_height, RID, p_obstacle, real_t, p_height);
232 virtual void obstacle_set_vertices(RID p_obstacle, const Vector<Vector3> &p_vertices) override;
233 COMMAND_2(obstacle_set_avoidance_layers, RID, p_obstacle, uint32_t, p_layers);
234
235 virtual void parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override;
236 virtual void bake_from_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
237 virtual void bake_from_source_geometry_data_async(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
238
239 COMMAND_1(free, RID, p_object);
240
241 virtual void set_active(bool p_active) override;
242
243 void flush_queries();
244 virtual void process(real_t p_delta_time) override;
245 virtual void init() override;
246 virtual void finish() override;
247
248 virtual NavigationUtilities::PathQueryResult _query_path(const NavigationUtilities::PathQueryParameters &p_parameters) const override;
249
250 int get_process_info(ProcessInfo p_info) const override;
251
252private:
253 void internal_free_agent(RID p_object);
254 void internal_free_obstacle(RID p_object);
255};
256
257#undef COMMAND_1
258#undef COMMAND_2
259
260#endif // GODOT_NAVIGATION_SERVER_H
261