Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/navigation_2d/2d/godot_navigation_server_2d.h
10278 views
1
/**************************************************************************/
2
/* godot_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
#pragma once
32
33
#include "../nav_agent_2d.h"
34
#include "../nav_link_2d.h"
35
#include "../nav_map_2d.h"
36
#include "../nav_obstacle_2d.h"
37
#include "../nav_region_2d.h"
38
39
#include "core/templates/local_vector.h"
40
#include "core/templates/rid.h"
41
#include "core/templates/rid_owner.h"
42
#include "servers/navigation/navigation_path_query_parameters_2d.h"
43
#include "servers/navigation/navigation_path_query_result_2d.h"
44
#include "servers/navigation_server_2d.h"
45
46
/// The commands are functions executed during the `sync` phase.
47
48
#define MERGE_INTERNAL(A, B) A##B
49
#define MERGE(A, B) MERGE_INTERNAL(A, B)
50
51
#define COMMAND_1(F_NAME, T_0, D_0) \
52
virtual void F_NAME(T_0 D_0) override; \
53
void MERGE(_cmd_, F_NAME)(T_0 D_0)
54
55
#define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
56
virtual void F_NAME(T_0 D_0, T_1 D_1) override; \
57
void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)
58
59
class GodotNavigationServer2D;
60
#ifdef CLIPPER2_ENABLED
61
class NavMeshGenerator2D;
62
#endif // CLIPPER2_ENABLED
63
64
struct SetCommand2D {
65
virtual ~SetCommand2D() {}
66
virtual void exec(GodotNavigationServer2D *p_server) = 0;
67
};
68
69
// This server exposes the `NavigationServer3D` features in the 2D world.
70
class GodotNavigationServer2D : public NavigationServer2D {
71
GDCLASS(GodotNavigationServer2D, NavigationServer2D);
72
73
Mutex commands_mutex;
74
/// Mutex used to make any operation threadsafe.
75
Mutex operations_mutex;
76
77
LocalVector<SetCommand2D *> commands;
78
79
mutable RID_Owner<NavLink2D> link_owner;
80
mutable RID_Owner<NavMap2D> map_owner;
81
mutable RID_Owner<NavRegion2D> region_owner;
82
mutable RID_Owner<NavAgent2D> agent_owner;
83
mutable RID_Owner<NavObstacle2D> obstacle_owner;
84
85
bool active = true;
86
LocalVector<NavMap2D *> active_maps;
87
88
#ifdef CLIPPER2_ENABLED
89
NavMeshGenerator2D *navmesh_generator_2d = nullptr;
90
#endif // CLIPPER2_ENABLED
91
92
// Performance Monitor.
93
int pm_region_count = 0;
94
int pm_agent_count = 0;
95
int pm_link_count = 0;
96
int pm_polygon_count = 0;
97
int pm_edge_count = 0;
98
int pm_edge_merge_count = 0;
99
int pm_edge_connection_count = 0;
100
int pm_edge_free_count = 0;
101
int pm_obstacle_count = 0;
102
103
public:
104
GodotNavigationServer2D();
105
virtual ~GodotNavigationServer2D();
106
107
void add_command(SetCommand2D *p_command);
108
109
virtual TypedArray<RID> get_maps() const override;
110
111
virtual RID map_create() override;
112
COMMAND_2(map_set_active, RID, p_map, bool, p_active);
113
virtual bool map_is_active(RID p_map) const override;
114
115
COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size);
116
virtual real_t map_get_cell_size(RID p_map) const override;
117
118
COMMAND_2(map_set_merge_rasterizer_cell_scale, RID, p_map, float, p_value);
119
virtual float map_get_merge_rasterizer_cell_scale(RID p_map) const override;
120
121
COMMAND_2(map_set_use_edge_connections, RID, p_map, bool, p_enabled);
122
virtual bool map_get_use_edge_connections(RID p_map) const override;
123
124
COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin);
125
virtual real_t map_get_edge_connection_margin(RID p_map) const override;
126
127
COMMAND_2(map_set_link_connection_radius, RID, p_map, real_t, p_connection_radius);
128
virtual real_t map_get_link_connection_radius(RID p_map) const override;
129
130
virtual Vector<Vector2> map_get_path(RID p_map, Vector2 p_origin, Vector2 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) override;
131
132
virtual Vector2 map_get_closest_point(RID p_map, const Vector2 &p_point) const override;
133
134
virtual RID map_get_closest_point_owner(RID p_map, const Vector2 &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
virtual uint32_t map_get_iteration_id(RID p_map) const override;
143
144
COMMAND_2(map_set_use_async_iterations, RID, p_map, bool, p_enabled);
145
virtual bool map_get_use_async_iterations(RID p_map) const override;
146
147
virtual Vector2 map_get_random_point(RID p_map, uint32_t p_navigation_layers, bool p_uniformly) const override;
148
149
virtual RID region_create() override;
150
virtual uint32_t region_get_iteration_id(RID p_region) const override;
151
152
COMMAND_2(region_set_use_async_iterations, RID, p_region, bool, p_enabled);
153
virtual bool region_get_use_async_iterations(RID p_region) const override;
154
155
COMMAND_2(region_set_enabled, RID, p_region, bool, p_enabled);
156
virtual bool region_get_enabled(RID p_region) const override;
157
158
COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled);
159
virtual bool region_get_use_edge_connections(RID p_region) const override;
160
161
COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost);
162
virtual real_t region_get_enter_cost(RID p_region) const override;
163
COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost);
164
virtual real_t region_get_travel_cost(RID p_region) const override;
165
166
COMMAND_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id);
167
virtual ObjectID region_get_owner_id(RID p_region) const override;
168
169
virtual bool region_owns_point(RID p_region, const Vector2 &p_point) const override;
170
171
COMMAND_2(region_set_map, RID, p_region, RID, p_map);
172
virtual RID region_get_map(RID p_region) const override;
173
COMMAND_2(region_set_navigation_layers, RID, p_region, uint32_t, p_navigation_layers);
174
virtual uint32_t region_get_navigation_layers(RID p_region) const override;
175
COMMAND_2(region_set_transform, RID, p_region, Transform2D, p_transform);
176
virtual Transform2D region_get_transform(RID p_region) const override;
177
COMMAND_2(region_set_navigation_polygon, RID, p_region, Ref<NavigationPolygon>, p_navigation_polygon);
178
virtual int region_get_connections_count(RID p_region) const override;
179
virtual Vector2 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
180
virtual Vector2 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
181
virtual Vector2 region_get_closest_point(RID p_region, const Vector2 &p_point) const override;
182
virtual Vector2 region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const override;
183
virtual Rect2 region_get_bounds(RID p_region) const override;
184
185
virtual RID link_create() override;
186
virtual uint32_t link_get_iteration_id(RID p_link) const override;
187
188
/// Set the map of this link.
189
COMMAND_2(link_set_map, RID, p_link, RID, p_map);
190
virtual RID link_get_map(RID p_link) const override;
191
COMMAND_2(link_set_enabled, RID, p_link, bool, p_enabled);
192
virtual bool link_get_enabled(RID p_link) const override;
193
194
/// Set whether this link travels in both directions.
195
COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional);
196
virtual bool link_is_bidirectional(RID p_link) const override;
197
198
/// Set the link's layers.
199
COMMAND_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers);
200
virtual uint32_t link_get_navigation_layers(RID p_link) const override;
201
202
/// Set the start position of the link.
203
COMMAND_2(link_set_start_position, RID, p_link, Vector2, p_position);
204
virtual Vector2 link_get_start_position(RID p_link) const override;
205
206
/// Set the end position of the link.
207
COMMAND_2(link_set_end_position, RID, p_link, Vector2, p_position);
208
virtual Vector2 link_get_end_position(RID p_link) const override;
209
210
/// Set the enter cost of the link.
211
COMMAND_2(link_set_enter_cost, RID, p_link, real_t, p_enter_cost);
212
virtual real_t link_get_enter_cost(RID p_link) const override;
213
214
/// Set the travel cost of the link.
215
COMMAND_2(link_set_travel_cost, RID, p_link, real_t, p_travel_cost);
216
virtual real_t link_get_travel_cost(RID p_link) const override;
217
218
/// Set the node which manages this link.
219
COMMAND_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id);
220
virtual ObjectID link_get_owner_id(RID p_link) const override;
221
222
/// Creates the agent.
223
virtual RID agent_create() override;
224
225
/// Put the agent in the map.
226
COMMAND_2(agent_set_map, RID, p_agent, RID, p_map);
227
virtual RID agent_get_map(RID p_agent) const override;
228
229
COMMAND_2(agent_set_paused, RID, p_agent, bool, p_paused);
230
virtual bool agent_get_paused(RID p_agent) const override;
231
232
COMMAND_2(agent_set_avoidance_enabled, RID, p_agent, bool, p_enabled);
233
virtual bool agent_get_avoidance_enabled(RID p_agent) const override;
234
235
/// The maximum distance (center point to
236
/// center point) to other agents this agent
237
/// takes into account in the navigation. The
238
/// larger this number, the longer the running
239
/// time of the simulation. If the number is too
240
/// low, the simulation will not be safe.
241
/// Must be non-negative.
242
COMMAND_2(agent_set_neighbor_distance, RID, p_agent, real_t, p_distance);
243
virtual real_t agent_get_neighbor_distance(RID p_agent) const override;
244
245
/// The maximum number of other agents this
246
/// agent takes into account in the navigation.
247
/// The larger this number, the longer the
248
/// running time of the simulation. If the
249
/// number is too low, the simulation will not
250
/// be safe.
251
COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count);
252
virtual int agent_get_max_neighbors(RID p_agent) const override;
253
254
/// The minimal amount of time for which this
255
/// agent's velocities that are computed by the
256
/// simulation are safe with respect to other
257
/// agents. The larger this number, the sooner
258
/// this agent will respond to the presence of
259
/// other agents, but the less freedom this
260
/// agent has in choosing its velocities.
261
/// Must be positive.
262
COMMAND_2(agent_set_time_horizon_agents, RID, p_agent, real_t, p_time_horizon);
263
virtual real_t agent_get_time_horizon_agents(RID p_agent) const override;
264
COMMAND_2(agent_set_time_horizon_obstacles, RID, p_agent, real_t, p_time_horizon);
265
virtual real_t agent_get_time_horizon_obstacles(RID p_agent) const override;
266
267
/// The radius of this agent.
268
/// Must be non-negative.
269
COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius);
270
virtual real_t agent_get_radius(RID p_agent) const override;
271
272
/// The maximum speed of this agent.
273
/// Must be non-negative.
274
COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed);
275
virtual real_t agent_get_max_speed(RID p_agent) const override;
276
277
/// forces and agent velocity change in the avoidance simulation, adds simulation instability if done recklessly
278
COMMAND_2(agent_set_velocity_forced, RID, p_agent, Vector2, p_velocity);
279
280
/// The wanted velocity for the agent as a "suggestion" to the avoidance simulation.
281
/// The simulation will try to fulfill this velocity wish if possible but may change the velocity depending on other agent's and obstacles'.
282
COMMAND_2(agent_set_velocity, RID, p_agent, Vector2, p_velocity);
283
virtual Vector2 agent_get_velocity(RID p_agent) const override;
284
285
/// Position of the agent in world space.
286
COMMAND_2(agent_set_position, RID, p_agent, Vector2, p_position);
287
virtual Vector2 agent_get_position(RID p_agent) const override;
288
289
/// Returns true if the map got changed the previous frame.
290
virtual bool agent_is_map_changed(RID p_agent) const override;
291
292
/// Callback called at the end of the RVO process
293
COMMAND_2(agent_set_avoidance_callback, RID, p_agent, Callable, p_callback);
294
virtual bool agent_has_avoidance_callback(RID p_agent) const override;
295
296
COMMAND_2(agent_set_avoidance_layers, RID, p_agent, uint32_t, p_layers);
297
virtual uint32_t agent_get_avoidance_layers(RID p_agent) const override;
298
299
COMMAND_2(agent_set_avoidance_mask, RID, p_agent, uint32_t, p_mask);
300
virtual uint32_t agent_get_avoidance_mask(RID p_agent) const override;
301
302
COMMAND_2(agent_set_avoidance_priority, RID, p_agent, real_t, p_priority);
303
virtual real_t agent_get_avoidance_priority(RID p_agent) const override;
304
305
virtual RID obstacle_create() override;
306
COMMAND_2(obstacle_set_avoidance_enabled, RID, p_obstacle, bool, p_enabled);
307
virtual bool obstacle_get_avoidance_enabled(RID p_obstacle) const override;
308
COMMAND_2(obstacle_set_map, RID, p_obstacle, RID, p_map);
309
virtual RID obstacle_get_map(RID p_obstacle) const override;
310
COMMAND_2(obstacle_set_paused, RID, p_obstacle, bool, p_paused);
311
virtual bool obstacle_get_paused(RID p_obstacle) const override;
312
COMMAND_2(obstacle_set_radius, RID, p_obstacle, real_t, p_radius);
313
virtual real_t obstacle_get_radius(RID p_obstacle) const override;
314
COMMAND_2(obstacle_set_velocity, RID, p_obstacle, Vector2, p_velocity);
315
virtual Vector2 obstacle_get_velocity(RID p_obstacle) const override;
316
COMMAND_2(obstacle_set_position, RID, p_obstacle, Vector2, p_position);
317
virtual Vector2 obstacle_get_position(RID p_obstacle) const override;
318
COMMAND_2(obstacle_set_vertices, RID, p_obstacle, const Vector<Vector2> &, p_vertices);
319
virtual Vector<Vector2> obstacle_get_vertices(RID p_obstacle) const override;
320
COMMAND_2(obstacle_set_avoidance_layers, RID, p_obstacle, uint32_t, p_layers);
321
virtual uint32_t obstacle_get_avoidance_layers(RID p_obstacle) const override;
322
323
virtual void query_path(const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result, const Callable &p_callback = Callable()) override;
324
325
COMMAND_1(free, RID, p_object);
326
327
virtual void set_active(bool p_active) override;
328
329
void flush_queries();
330
331
virtual void process(double p_delta_time) override;
332
virtual void physics_process(double p_delta_time) override;
333
virtual void init() override;
334
virtual void sync() override;
335
virtual void finish() override;
336
337
virtual int get_process_info(ProcessInfo p_info) const override;
338
339
virtual void parse_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override;
340
virtual void bake_from_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
341
virtual void bake_from_source_geometry_data_async(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
342
virtual bool is_baking_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) const override;
343
344
virtual RID source_geometry_parser_create() override;
345
virtual void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) override;
346
347
virtual Vector<Vector2> simplify_path(const Vector<Vector2> &p_path, real_t p_epsilon) override;
348
349
private:
350
void internal_free_agent(RID p_object);
351
void internal_free_obstacle(RID p_object);
352
};
353
354
#undef COMMAND_1
355
#undef COMMAND_2
356
357