2020-01-10 11:22:34 +00:00
/**************************************************************************/
2023-10-13 15:20:31 +00:00
/* godot_navigation_server_3d.cpp */
2020-01-10 11:22:34 +00:00
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
2020-02-11 13:01:43 +00:00
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
2020-01-10 11:22:34 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2023-10-13 15:20:31 +00:00
# include "godot_navigation_server_3d.h"
2020-01-10 11:22:34 +00:00
2024-02-26 12:34:53 +00:00
# include "core/os/mutex.h"
# include "scene/main/node.h"
2020-01-10 11:22:34 +00:00
# ifndef _3D_DISABLED
2023-07-07 13:59:10 +00:00
# include "nav_mesh_generator_3d.h"
# endif // _3D_DISABLED
2020-01-10 11:22:34 +00:00
2022-10-05 23:24:45 +00:00
using namespace NavigationUtilities ;
2020-01-10 11:22:34 +00:00
/// Creates a struct for each function and a function that once called creates
2020-03-11 17:59:18 +00:00
/// an instance of that struct with the submitted parameters.
2020-01-10 11:22:34 +00:00
/// Then, that struct is stored in an array; the `sync` function consume that array.
2023-10-13 15:20:31 +00:00
# define COMMAND_1(F_NAME, T_0, D_0) \
struct MERGE ( F_NAME , _command ) : public SetCommand { \
T_0 d_0 ; \
MERGE ( F_NAME , _command ) \
( T_0 p_d_0 ) : \
d_0 ( p_d_0 ) { } \
virtual void exec ( GodotNavigationServer3D * server ) override { \
server - > MERGE ( _cmd_ , F_NAME ) ( d_0 ) ; \
} \
} ; \
void GodotNavigationServer3D : : F_NAME ( T_0 D_0 ) { \
auto cmd = memnew ( MERGE ( F_NAME , _command ) ( \
D_0 ) ) ; \
add_command ( cmd ) ; \
} \
void GodotNavigationServer3D : : MERGE ( _cmd_ , F_NAME ) ( T_0 D_0 )
# define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
struct MERGE ( F_NAME , _command ) : public SetCommand { \
T_0 d_0 ; \
T_1 d_1 ; \
MERGE ( F_NAME , _command ) \
( \
T_0 p_d_0 , \
T_1 p_d_1 ) : \
d_0 ( p_d_0 ) , \
d_1 ( p_d_1 ) { } \
virtual void exec ( GodotNavigationServer3D * server ) override { \
server - > MERGE ( _cmd_ , F_NAME ) ( d_0 , d_1 ) ; \
} \
} ; \
void GodotNavigationServer3D : : F_NAME ( T_0 D_0 , T_1 D_1 ) { \
auto cmd = memnew ( MERGE ( F_NAME , _command ) ( \
D_0 , \
D_1 ) ) ; \
add_command ( cmd ) ; \
} \
void GodotNavigationServer3D : : MERGE ( _cmd_ , F_NAME ) ( T_0 D_0 , T_1 D_1 )
GodotNavigationServer3D : : GodotNavigationServer3D ( ) { }
GodotNavigationServer3D : : ~ GodotNavigationServer3D ( ) {
2020-02-26 08:42:51 +00:00
flush_queries ( ) ;
2020-02-15 05:45:17 +00:00
}
2020-01-10 11:22:34 +00:00
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : add_command ( SetCommand * command ) {
2022-12-21 22:21:43 +00:00
MutexLock lock ( commands_mutex ) ;
commands . push_back ( command ) ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
TypedArray < RID > GodotNavigationServer3D : : get_maps ( ) const {
2022-08-05 18:35:08 +00:00
TypedArray < RID > all_map_rids ;
2022-06-22 13:33:40 +00:00
List < RID > maps_owned ;
map_owner . get_owned_list ( & maps_owned ) ;
if ( maps_owned . size ( ) ) {
for ( const RID & E : maps_owned ) {
all_map_rids . push_back ( E ) ;
}
}
return all_map_rids ;
}
2023-10-13 15:20:31 +00:00
RID GodotNavigationServer3D : : map_create ( ) {
2022-12-21 22:21:43 +00:00
MutexLock lock ( operations_mutex ) ;
2021-06-29 13:58:28 +00:00
RID rid = map_owner . make_rid ( ) ;
2022-12-21 22:21:43 +00:00
NavMap * map = map_owner . get_or_null ( rid ) ;
map - > set_self ( rid ) ;
2020-01-10 11:22:34 +00:00
return rid ;
}
COMMAND_2 ( map_set_active , RID , p_map , bool , p_active ) {
2021-09-29 17:08:41 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( map ) ;
2020-01-10 11:22:34 +00:00
if ( p_active ) {
if ( ! map_is_active ( p_map ) ) {
active_maps . push_back ( map ) ;
2023-11-01 02:02:59 +00:00
active_maps_iteration_id . push_back ( map - > get_iteration_id ( ) ) ;
2020-01-10 11:22:34 +00:00
}
} else {
2021-03-15 11:45:28 +00:00
int map_index = active_maps . find ( map ) ;
ERR_FAIL_COND ( map_index < 0 ) ;
2021-07-03 22:17:03 +00:00
active_maps . remove_at ( map_index ) ;
2023-11-01 02:02:59 +00:00
active_maps_iteration_id . remove_at ( map_index ) ;
2020-01-10 11:22:34 +00:00
}
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : map_is_active ( RID p_map ) const {
2021-09-29 17:08:41 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , false ) ;
2020-01-10 11:22:34 +00:00
2024-05-06 14:43:04 +00:00
return active_maps . has ( map ) ;
2020-01-10 11:22:34 +00:00
}
COMMAND_2 ( map_set_up , RID , p_map , Vector3 , p_up ) {
2021-09-29 17:08:41 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( map ) ;
2020-01-10 11:22:34 +00:00
map - > set_up ( p_up ) ;
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : map_get_up ( RID p_map ) const {
2021-09-29 17:08:41 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , Vector3 ( ) ) ;
2020-01-10 11:22:34 +00:00
return map - > get_up ( ) ;
}
COMMAND_2 ( map_set_cell_size , RID , p_map , real_t , p_cell_size ) {
2021-09-29 17:08:41 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( map ) ;
2020-01-10 11:22:34 +00:00
map - > set_cell_size ( p_cell_size ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : map_get_cell_size ( RID p_map ) const {
2021-09-29 17:08:41 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , 0 ) ;
2020-01-10 11:22:34 +00:00
return map - > get_cell_size ( ) ;
}
2023-06-13 11:36:05 +00:00
COMMAND_2 ( map_set_cell_height , RID , p_map , real_t , p_cell_height ) {
NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( map ) ;
2023-06-13 11:36:05 +00:00
map - > set_cell_height ( p_cell_height ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : map_get_cell_height ( RID p_map ) const {
2023-06-13 11:36:05 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , 0 ) ;
2023-06-13 11:36:05 +00:00
return map - > get_cell_height ( ) ;
}
2024-02-04 20:31:07 +00:00
COMMAND_2 ( map_set_merge_rasterizer_cell_scale , RID , p_map , float , p_value ) {
NavMap * map = map_owner . get_or_null ( p_map ) ;
ERR_FAIL_NULL ( map ) ;
map - > set_merge_rasterizer_cell_scale ( p_value ) ;
}
2023-10-13 15:20:31 +00:00
float GodotNavigationServer3D : : map_get_merge_rasterizer_cell_scale ( RID p_map ) const {
2024-02-04 20:31:07 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
ERR_FAIL_NULL_V ( map , false ) ;
return map - > get_merge_rasterizer_cell_scale ( ) ;
}
2023-03-31 23:49:43 +00:00
COMMAND_2 ( map_set_use_edge_connections , RID , p_map , bool , p_enabled ) {
NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( map ) ;
2023-03-31 23:49:43 +00:00
map - > set_use_edge_connections ( p_enabled ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : map_get_use_edge_connections ( RID p_map ) const {
2023-03-31 23:49:43 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , false ) ;
2023-03-31 23:49:43 +00:00
return map - > get_use_edge_connections ( ) ;
}
2020-01-10 11:22:34 +00:00
COMMAND_2 ( map_set_edge_connection_margin , RID , p_map , real_t , p_connection_margin ) {
2021-09-29 17:08:41 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( map ) ;
2020-01-10 11:22:34 +00:00
map - > set_edge_connection_margin ( p_connection_margin ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : map_get_edge_connection_margin ( RID p_map ) const {
2021-09-29 17:08:41 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , 0 ) ;
2020-01-10 11:22:34 +00:00
return map - > get_edge_connection_margin ( ) ;
}
2022-01-30 23:39:52 +00:00
COMMAND_2 ( map_set_link_connection_radius , RID , p_map , real_t , p_connection_radius ) {
NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( map ) ;
2022-01-30 23:39:52 +00:00
map - > set_link_connection_radius ( p_connection_radius ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : map_get_link_connection_radius ( RID p_map ) const {
2022-01-30 23:39:52 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , 0 ) ;
2022-01-30 23:39:52 +00:00
return map - > get_link_connection_radius ( ) ;
}
2023-10-13 15:20:31 +00:00
Vector < Vector3 > GodotNavigationServer3D : : map_get_path ( RID p_map , Vector3 p_origin , Vector3 p_destination , bool p_optimize , uint32_t p_navigation_layers ) const {
2021-09-29 17:08:41 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , Vector < Vector3 > ( ) ) ;
2020-01-10 11:22:34 +00:00
2022-10-05 23:24:45 +00:00
return map - > get_path ( p_origin , p_destination , p_optimize , p_navigation_layers , nullptr , nullptr , nullptr ) ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : map_get_closest_point_to_segment ( RID p_map , const Vector3 & p_from , const Vector3 & p_to , const bool p_use_collision ) const {
2021-09-29 17:08:41 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , Vector3 ( ) ) ;
2020-02-18 16:08:34 +00:00
return map - > get_closest_point_to_segment ( p_from , p_to , p_use_collision ) ;
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : map_get_closest_point ( RID p_map , const Vector3 & p_point ) const {
2021-09-29 17:08:41 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , Vector3 ( ) ) ;
2020-02-18 16:08:34 +00:00
return map - > get_closest_point ( p_point ) ;
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : map_get_closest_point_normal ( RID p_map , const Vector3 & p_point ) const {
2021-09-29 17:08:41 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , Vector3 ( ) ) ;
2020-02-18 16:08:34 +00:00
return map - > get_closest_point_normal ( p_point ) ;
}
2023-10-13 15:20:31 +00:00
RID GodotNavigationServer3D : : map_get_closest_point_owner ( RID p_map , const Vector3 & p_point ) const {
2021-09-29 17:08:41 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , RID ( ) ) ;
2020-02-18 16:08:34 +00:00
return map - > get_closest_point_owner ( p_point ) ;
}
2023-10-13 15:20:31 +00:00
TypedArray < RID > GodotNavigationServer3D : : map_get_links ( RID p_map ) const {
2022-01-30 23:39:52 +00:00
TypedArray < RID > link_rids ;
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , link_rids ) ;
2022-01-30 23:39:52 +00:00
2023-05-06 18:16:55 +00:00
const LocalVector < NavLink * > & links = map - > get_links ( ) ;
2022-01-30 23:39:52 +00:00
link_rids . resize ( links . size ( ) ) ;
for ( uint32_t i = 0 ; i < links . size ( ) ; i + + ) {
link_rids [ i ] = links [ i ] - > get_self ( ) ;
}
return link_rids ;
}
2023-10-13 15:20:31 +00:00
TypedArray < RID > GodotNavigationServer3D : : map_get_regions ( RID p_map ) const {
2022-08-05 18:35:08 +00:00
TypedArray < RID > regions_rids ;
2022-05-12 00:52:48 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , regions_rids ) ;
2022-12-21 22:21:43 +00:00
2023-05-06 18:16:55 +00:00
const LocalVector < NavRegion * > & regions = map - > get_regions ( ) ;
2022-07-28 17:24:14 +00:00
regions_rids . resize ( regions . size ( ) ) ;
2022-12-21 22:21:43 +00:00
2022-07-28 17:24:14 +00:00
for ( uint32_t i = 0 ; i < regions . size ( ) ; i + + ) {
regions_rids [ i ] = regions [ i ] - > get_self ( ) ;
2022-05-12 00:52:48 +00:00
}
return regions_rids ;
}
2023-10-13 15:20:31 +00:00
TypedArray < RID > GodotNavigationServer3D : : map_get_agents ( RID p_map ) const {
2022-08-05 18:35:08 +00:00
TypedArray < RID > agents_rids ;
2022-05-12 00:52:48 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , agents_rids ) ;
2022-12-21 22:21:43 +00:00
2023-05-06 18:16:55 +00:00
const LocalVector < NavAgent * > & agents = map - > get_agents ( ) ;
2022-07-28 17:24:14 +00:00
agents_rids . resize ( agents . size ( ) ) ;
2022-12-21 22:21:43 +00:00
2022-07-28 17:24:14 +00:00
for ( uint32_t i = 0 ; i < agents . size ( ) ; i + + ) {
agents_rids [ i ] = agents [ i ] - > get_self ( ) ;
2022-05-12 00:52:48 +00:00
}
return agents_rids ;
}
2023-10-13 15:20:31 +00:00
TypedArray < RID > GodotNavigationServer3D : : map_get_obstacles ( RID p_map ) const {
2023-01-10 06:14:16 +00:00
TypedArray < RID > obstacles_rids ;
const NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , obstacles_rids ) ;
2023-01-10 06:14:16 +00:00
const LocalVector < NavObstacle * > obstacles = map - > get_obstacles ( ) ;
obstacles_rids . resize ( obstacles . size ( ) ) ;
for ( uint32_t i = 0 ; i < obstacles . size ( ) ; i + + ) {
obstacles_rids [ i ] = obstacles [ i ] - > get_self ( ) ;
}
return obstacles_rids ;
}
2023-10-13 15:20:31 +00:00
RID GodotNavigationServer3D : : region_get_map ( RID p_region ) const {
2022-05-12 00:52:48 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( region , RID ( ) ) ;
2022-12-21 22:21:43 +00:00
2022-05-17 18:08:39 +00:00
if ( region - > get_map ( ) ) {
return region - > get_map ( ) - > get_self ( ) ;
}
return RID ( ) ;
2022-05-12 00:52:48 +00:00
}
2023-10-13 15:20:31 +00:00
RID GodotNavigationServer3D : : agent_get_map ( RID p_agent ) const {
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( agent , RID ( ) ) ;
2022-12-21 22:21:43 +00:00
2022-05-17 18:08:39 +00:00
if ( agent - > get_map ( ) ) {
return agent - > get_map ( ) - > get_self ( ) ;
}
return RID ( ) ;
2022-05-12 00:52:48 +00:00
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : map_get_random_point ( RID p_map , uint32_t p_navigation_layers , bool p_uniformly ) const {
2023-10-23 16:16:05 +00:00
const NavMap * map = map_owner . get_or_null ( p_map ) ;
ERR_FAIL_NULL_V ( map , Vector3 ( ) ) ;
return map - > get_random_point ( p_navigation_layers , p_uniformly ) ;
}
2023-10-13 15:20:31 +00:00
RID GodotNavigationServer3D : : region_create ( ) {
2022-12-21 22:21:43 +00:00
MutexLock lock ( operations_mutex ) ;
2021-06-29 13:58:28 +00:00
RID rid = region_owner . make_rid ( ) ;
2021-09-29 17:08:41 +00:00
NavRegion * reg = region_owner . get_or_null ( rid ) ;
2020-01-10 11:22:34 +00:00
reg - > set_self ( rid ) ;
return rid ;
}
2023-07-06 21:01:19 +00:00
COMMAND_2 ( region_set_enabled , RID , p_region , bool , p_enabled ) {
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( region ) ;
2023-07-06 21:01:19 +00:00
region - > set_enabled ( p_enabled ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : region_get_enabled ( RID p_region ) const {
2023-07-06 21:01:19 +00:00
const NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( region , false ) ;
2023-07-06 21:01:19 +00:00
return region - > get_enabled ( ) ;
}
2023-03-31 23:49:43 +00:00
COMMAND_2 ( region_set_use_edge_connections , RID , p_region , bool , p_enabled ) {
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( region ) ;
2023-03-31 23:49:43 +00:00
region - > set_use_edge_connections ( p_enabled ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : region_get_use_edge_connections ( RID p_region ) const {
2023-03-31 23:49:43 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( region , false ) ;
2023-03-31 23:49:43 +00:00
return region - > get_use_edge_connections ( ) ;
}
2020-01-10 11:22:34 +00:00
COMMAND_2 ( region_set_map , RID , p_region , RID , p_map ) {
2021-09-29 17:08:41 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( region ) ;
2020-01-10 11:22:34 +00:00
2023-06-25 03:28:21 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
2020-01-10 11:22:34 +00:00
2023-06-25 03:28:21 +00:00
region - > set_map ( map ) ;
2020-01-10 11:22:34 +00:00
}
2020-10-17 05:08:21 +00:00
COMMAND_2 ( region_set_transform , RID , p_region , Transform3D , p_transform ) {
2021-09-29 17:08:41 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( region ) ;
2020-01-10 11:22:34 +00:00
region - > set_transform ( p_transform ) ;
}
2023-10-13 15:20:31 +00:00
Transform3D GodotNavigationServer3D : : region_get_transform ( RID p_region ) const {
2023-12-19 18:51:49 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
ERR_FAIL_NULL_V ( region , Transform3D ( ) ) ;
return region - > get_transform ( ) ;
}
2022-06-06 03:24:11 +00:00
COMMAND_2 ( region_set_enter_cost , RID , p_region , real_t , p_enter_cost ) {
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( region ) ;
2022-06-06 03:24:11 +00:00
ERR_FAIL_COND ( p_enter_cost < 0.0 ) ;
region - > set_enter_cost ( p_enter_cost ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : region_get_enter_cost ( RID p_region ) const {
2022-06-06 03:24:11 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( region , 0 ) ;
2022-06-06 03:24:11 +00:00
return region - > get_enter_cost ( ) ;
}
COMMAND_2 ( region_set_travel_cost , RID , p_region , real_t , p_travel_cost ) {
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( region ) ;
2022-06-06 03:24:11 +00:00
ERR_FAIL_COND ( p_travel_cost < 0.0 ) ;
region - > set_travel_cost ( p_travel_cost ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : region_get_travel_cost ( RID p_region ) const {
2022-06-06 03:24:11 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( region , 0 ) ;
2022-06-06 03:24:11 +00:00
return region - > get_travel_cost ( ) ;
}
2022-10-01 05:49:39 +00:00
COMMAND_2 ( region_set_owner_id , RID , p_region , ObjectID , p_owner_id ) {
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( region ) ;
2022-10-01 05:49:39 +00:00
region - > set_owner_id ( p_owner_id ) ;
}
2023-10-13 15:20:31 +00:00
ObjectID GodotNavigationServer3D : : region_get_owner_id ( RID p_region ) const {
2022-10-01 05:49:39 +00:00
const NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( region , ObjectID ( ) ) ;
2022-10-01 05:49:39 +00:00
return region - > get_owner_id ( ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : region_owns_point ( RID p_region , const Vector3 & p_point ) const {
2022-06-23 17:16:47 +00:00
const NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( region , false ) ;
2022-12-21 22:21:43 +00:00
2022-06-23 17:16:47 +00:00
if ( region - > get_map ( ) ) {
RID closest_point_owner = map_get_closest_point_owner ( region - > get_map ( ) - > get_self ( ) , p_point ) ;
return closest_point_owner = = region - > get_self ( ) ;
}
return false ;
}
2022-06-14 21:34:43 +00:00
COMMAND_2 ( region_set_navigation_layers , RID , p_region , uint32_t , p_navigation_layers ) {
2021-09-29 17:08:41 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( region ) ;
2021-03-08 19:56:33 +00:00
2022-06-14 21:34:43 +00:00
region - > set_navigation_layers ( p_navigation_layers ) ;
2021-03-08 19:56:33 +00:00
}
2023-10-13 15:20:31 +00:00
uint32_t GodotNavigationServer3D : : region_get_navigation_layers ( RID p_region ) const {
2021-09-29 17:08:41 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( region , 0 ) ;
2021-03-08 19:56:33 +00:00
2022-06-14 21:34:43 +00:00
return region - > get_navigation_layers ( ) ;
2021-03-08 19:56:33 +00:00
}
2022-12-11 17:02:35 +00:00
COMMAND_2 ( region_set_navigation_mesh , RID , p_region , Ref < NavigationMesh > , p_navigation_mesh ) {
2021-09-29 17:08:41 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( region ) ;
2020-01-10 11:22:34 +00:00
2024-06-20 13:26:57 +00:00
region - > set_navigation_mesh ( p_navigation_mesh ) ;
2020-01-10 11:22:34 +00:00
}
2023-07-06 22:31:33 +00:00
# ifndef DISABLE_DEPRECATED
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : region_bake_navigation_mesh ( Ref < NavigationMesh > p_navigation_mesh , Node * p_root_node ) {
2022-12-11 17:02:35 +00:00
ERR_FAIL_COND ( p_navigation_mesh . is_null ( ) ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( p_root_node ) ;
2020-01-10 11:22:34 +00:00
2023-07-06 22:31:33 +00:00
WARN_PRINT_ONCE ( " NavigationServer3D::region_bake_navigation_mesh() is deprecated due to core threading changes. To upgrade existing code, first create a NavigationMeshSourceGeometryData3D resource. Use this resource with method parse_source_geometry_data() to parse the SceneTree for nodes that should contribute to the navigation mesh baking. The SceneTree parsing needs to happen on the main thread. After the parsing is finished use the resource with method bake_from_source_geometry_data() to bake a navigation mesh.. " ) ;
2020-01-10 11:22:34 +00:00
# ifndef _3D_DISABLED
2023-07-07 13:59:10 +00:00
p_navigation_mesh - > clear ( ) ;
2023-05-23 17:55:31 +00:00
Ref < NavigationMeshSourceGeometryData3D > source_geometry_data ;
source_geometry_data . instantiate ( ) ;
2023-07-07 13:59:10 +00:00
parse_source_geometry_data ( p_navigation_mesh , source_geometry_data , p_root_node ) ;
bake_from_source_geometry_data ( p_navigation_mesh , source_geometry_data ) ;
2020-01-10 11:22:34 +00:00
# endif
}
2023-07-06 22:31:33 +00:00
# endif // DISABLE_DEPRECATED
2020-01-10 11:22:34 +00:00
2023-10-13 15:20:31 +00:00
int GodotNavigationServer3D : : region_get_connections_count ( RID p_region ) const {
2021-09-29 17:08:41 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( region , 0 ) ;
2024-06-23 02:00:34 +00:00
NavMap * map = region - > get_map ( ) ;
if ( map ) {
return map - > get_region_connections_count ( region ) ;
}
return 0 ;
2021-03-15 11:45:28 +00:00
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : region_get_connection_pathway_start ( RID p_region , int p_connection_id ) const {
2021-09-29 17:08:41 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( region , Vector3 ( ) ) ;
2024-06-23 02:00:34 +00:00
NavMap * map = region - > get_map ( ) ;
if ( map ) {
return map - > get_region_connection_pathway_start ( region , p_connection_id ) ;
}
return Vector3 ( ) ;
2021-03-15 11:45:28 +00:00
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : region_get_connection_pathway_end ( RID p_region , int p_connection_id ) const {
2021-09-29 17:08:41 +00:00
NavRegion * region = region_owner . get_or_null ( p_region ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( region , Vector3 ( ) ) ;
2024-06-23 02:00:34 +00:00
NavMap * map = region - > get_map ( ) ;
if ( map ) {
return map - > get_region_connection_pathway_end ( region , p_connection_id ) ;
}
return Vector3 ( ) ;
2021-03-15 11:45:28 +00:00
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : region_get_random_point ( RID p_region , uint32_t p_navigation_layers , bool p_uniformly ) const {
2023-10-23 16:16:05 +00:00
const NavRegion * region = region_owner . get_or_null ( p_region ) ;
ERR_FAIL_NULL_V ( region , Vector3 ( ) ) ;
return region - > get_random_point ( p_navigation_layers , p_uniformly ) ;
}
2023-10-13 15:20:31 +00:00
RID GodotNavigationServer3D : : link_create ( ) {
2022-12-21 22:21:43 +00:00
MutexLock lock ( operations_mutex ) ;
2022-01-30 23:39:52 +00:00
RID rid = link_owner . make_rid ( ) ;
NavLink * link = link_owner . get_or_null ( rid ) ;
link - > set_self ( rid ) ;
return rid ;
}
COMMAND_2 ( link_set_map , RID , p_link , RID , p_map ) {
NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( link ) ;
2022-01-30 23:39:52 +00:00
2023-06-25 03:28:21 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
2022-01-30 23:39:52 +00:00
2023-06-25 03:28:21 +00:00
link - > set_map ( map ) ;
2022-01-30 23:39:52 +00:00
}
2023-10-13 15:20:31 +00:00
RID GodotNavigationServer3D : : link_get_map ( const RID p_link ) const {
2022-01-30 23:39:52 +00:00
const NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( link , RID ( ) ) ;
2022-01-30 23:39:52 +00:00
if ( link - > get_map ( ) ) {
return link - > get_map ( ) - > get_self ( ) ;
}
return RID ( ) ;
}
2023-07-06 21:01:19 +00:00
COMMAND_2 ( link_set_enabled , RID , p_link , bool , p_enabled ) {
NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( link ) ;
2023-07-06 21:01:19 +00:00
link - > set_enabled ( p_enabled ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : link_get_enabled ( RID p_link ) const {
2023-07-06 21:01:19 +00:00
const NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( link , false ) ;
2023-07-06 21:01:19 +00:00
return link - > get_enabled ( ) ;
}
2022-01-30 23:39:52 +00:00
COMMAND_2 ( link_set_bidirectional , RID , p_link , bool , p_bidirectional ) {
NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( link ) ;
2022-01-30 23:39:52 +00:00
link - > set_bidirectional ( p_bidirectional ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : link_is_bidirectional ( RID p_link ) const {
2022-01-30 23:39:52 +00:00
const NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( link , false ) ;
2022-01-30 23:39:52 +00:00
return link - > is_bidirectional ( ) ;
}
COMMAND_2 ( link_set_navigation_layers , RID , p_link , uint32_t , p_navigation_layers ) {
NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( link ) ;
2022-01-30 23:39:52 +00:00
link - > set_navigation_layers ( p_navigation_layers ) ;
}
2023-10-13 15:20:31 +00:00
uint32_t GodotNavigationServer3D : : link_get_navigation_layers ( const RID p_link ) const {
2022-01-30 23:39:52 +00:00
const NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( link , 0 ) ;
2022-01-30 23:39:52 +00:00
return link - > get_navigation_layers ( ) ;
}
2022-12-06 22:32:11 +00:00
COMMAND_2 ( link_set_start_position , RID , p_link , Vector3 , p_position ) {
2022-01-30 23:39:52 +00:00
NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( link ) ;
2022-01-30 23:39:52 +00:00
2022-12-06 22:32:11 +00:00
link - > set_start_position ( p_position ) ;
2022-01-30 23:39:52 +00:00
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : link_get_start_position ( RID p_link ) const {
2022-01-30 23:39:52 +00:00
const NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( link , Vector3 ( ) ) ;
2022-01-30 23:39:52 +00:00
2022-12-06 22:32:11 +00:00
return link - > get_start_position ( ) ;
2022-01-30 23:39:52 +00:00
}
2022-12-06 22:32:11 +00:00
COMMAND_2 ( link_set_end_position , RID , p_link , Vector3 , p_position ) {
2022-01-30 23:39:52 +00:00
NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( link ) ;
2022-01-30 23:39:52 +00:00
2022-12-06 22:32:11 +00:00
link - > set_end_position ( p_position ) ;
2022-01-30 23:39:52 +00:00
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : link_get_end_position ( RID p_link ) const {
2022-01-30 23:39:52 +00:00
const NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( link , Vector3 ( ) ) ;
2022-01-30 23:39:52 +00:00
2022-12-06 22:32:11 +00:00
return link - > get_end_position ( ) ;
2022-01-30 23:39:52 +00:00
}
COMMAND_2 ( link_set_enter_cost , RID , p_link , real_t , p_enter_cost ) {
NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( link ) ;
2022-01-30 23:39:52 +00:00
link - > set_enter_cost ( p_enter_cost ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : link_get_enter_cost ( const RID p_link ) const {
2022-01-30 23:39:52 +00:00
const NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( link , 0 ) ;
2022-01-30 23:39:52 +00:00
return link - > get_enter_cost ( ) ;
}
COMMAND_2 ( link_set_travel_cost , RID , p_link , real_t , p_travel_cost ) {
NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( link ) ;
2022-01-30 23:39:52 +00:00
link - > set_travel_cost ( p_travel_cost ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : link_get_travel_cost ( const RID p_link ) const {
2022-01-30 23:39:52 +00:00
const NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( link , 0 ) ;
2022-01-30 23:39:52 +00:00
return link - > get_travel_cost ( ) ;
}
2022-10-01 05:49:39 +00:00
COMMAND_2 ( link_set_owner_id , RID , p_link , ObjectID , p_owner_id ) {
NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( link ) ;
2022-10-01 05:49:39 +00:00
link - > set_owner_id ( p_owner_id ) ;
}
2023-10-13 15:20:31 +00:00
ObjectID GodotNavigationServer3D : : link_get_owner_id ( RID p_link ) const {
2022-10-01 05:49:39 +00:00
const NavLink * link = link_owner . get_or_null ( p_link ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( link , ObjectID ( ) ) ;
2022-10-01 05:49:39 +00:00
return link - > get_owner_id ( ) ;
}
2023-10-13 15:20:31 +00:00
RID GodotNavigationServer3D : : agent_create ( ) {
2022-12-21 22:21:43 +00:00
MutexLock lock ( operations_mutex ) ;
2021-06-29 13:58:28 +00:00
RID rid = agent_owner . make_rid ( ) ;
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( rid ) ;
2020-01-10 11:22:34 +00:00
agent - > set_self ( rid ) ;
return rid ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_avoidance_enabled , RID , p_agent , bool , p_enabled ) {
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2023-01-10 06:14:16 +00:00
agent - > set_avoidance_enabled ( p_enabled ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : agent_get_avoidance_enabled ( RID p_agent ) const {
2023-01-10 06:14:16 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( agent , false ) ;
2023-01-10 06:14:16 +00:00
return agent - > is_avoidance_enabled ( ) ;
}
COMMAND_2 ( agent_set_use_3d_avoidance , RID , p_agent , bool , p_enabled ) {
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2023-01-10 06:14:16 +00:00
agent - > set_use_3d_avoidance ( p_enabled ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : agent_get_use_3d_avoidance ( RID p_agent ) const {
2023-01-10 06:14:16 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( agent , false ) ;
2023-01-10 06:14:16 +00:00
return agent - > get_use_3d_avoidance ( ) ;
}
2020-01-10 11:22:34 +00:00
COMMAND_2 ( agent_set_map , RID , p_agent , RID , p_map ) {
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2020-01-10 11:22:34 +00:00
2023-06-25 03:28:21 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
2020-01-10 11:22:34 +00:00
2023-06-25 03:28:21 +00:00
agent - > set_map ( map ) ;
2020-01-10 11:22:34 +00:00
}
2023-06-15 13:35:53 +00:00
COMMAND_2 ( agent_set_paused , RID , p_agent , bool , p_paused ) {
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2023-06-15 13:35:53 +00:00
agent - > set_paused ( p_paused ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : agent_get_paused ( RID p_agent ) const {
2023-06-15 13:35:53 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( agent , false ) ;
2023-06-15 13:35:53 +00:00
return agent - > get_paused ( ) ;
}
2022-08-13 10:26:13 +00:00
COMMAND_2 ( agent_set_neighbor_distance , RID , p_agent , real_t , p_distance ) {
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2020-01-10 11:22:34 +00:00
2023-01-10 06:14:16 +00:00
agent - > set_neighbor_distance ( p_distance ) ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : agent_get_neighbor_distance ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , 0 ) ;
return agent - > get_neighbor_distance ( ) ;
}
2020-01-10 11:22:34 +00:00
COMMAND_2 ( agent_set_max_neighbors , RID , p_agent , int , p_count ) {
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2020-01-10 11:22:34 +00:00
2023-01-10 06:14:16 +00:00
agent - > set_max_neighbors ( p_count ) ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
int GodotNavigationServer3D : : agent_get_max_neighbors ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , 0 ) ;
return agent - > get_max_neighbors ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_time_horizon_agents , RID , p_agent , real_t , p_time_horizon ) {
2023-12-19 18:51:49 +00:00
ERR_FAIL_COND_MSG ( p_time_horizon < 0.0 , " Time horizon must be positive. " ) ;
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2020-01-10 11:22:34 +00:00
2023-01-10 06:14:16 +00:00
agent - > set_time_horizon_agents ( p_time_horizon ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : agent_get_time_horizon_agents ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , 0 ) ;
return agent - > get_time_horizon_agents ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_time_horizon_obstacles , RID , p_agent , real_t , p_time_horizon ) {
2023-12-19 18:51:49 +00:00
ERR_FAIL_COND_MSG ( p_time_horizon < 0.0 , " Time horizon must be positive. " ) ;
2023-01-10 06:14:16 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2023-01-10 06:14:16 +00:00
agent - > set_time_horizon_obstacles ( p_time_horizon ) ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : agent_get_time_horizon_obstacles ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , 0 ) ;
return agent - > get_time_horizon_obstacles ( ) ;
}
2020-01-10 11:22:34 +00:00
COMMAND_2 ( agent_set_radius , RID , p_agent , real_t , p_radius ) {
2023-01-10 06:14:16 +00:00
ERR_FAIL_COND_MSG ( p_radius < 0.0 , " Radius must be positive. " ) ;
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2020-01-10 11:22:34 +00:00
2023-01-10 06:14:16 +00:00
agent - > set_radius ( p_radius ) ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : agent_get_radius ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , 0 ) ;
return agent - > get_radius ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_height , RID , p_agent , real_t , p_height ) {
ERR_FAIL_COND_MSG ( p_height < 0.0 , " Height must be positive. " ) ;
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2020-01-10 11:22:34 +00:00
2023-01-10 06:14:16 +00:00
agent - > set_height ( p_height ) ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : agent_get_height ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , 0 ) ;
return agent - > get_height ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_max_speed , RID , p_agent , real_t , p_max_speed ) {
ERR_FAIL_COND_MSG ( p_max_speed < 0.0 , " Max speed must be positive. " ) ;
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2020-01-10 11:22:34 +00:00
2023-01-10 06:14:16 +00:00
agent - > set_max_speed ( p_max_speed ) ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : agent_get_max_speed ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , 0 ) ;
return agent - > get_max_speed ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_velocity , RID , p_agent , Vector3 , p_velocity ) {
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2020-01-10 11:22:34 +00:00
2023-01-10 06:14:16 +00:00
agent - > set_velocity ( p_velocity ) ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : agent_get_velocity ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , Vector3 ( ) ) ;
return agent - > get_velocity ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_velocity_forced , RID , p_agent , Vector3 , p_velocity ) {
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2020-01-10 11:22:34 +00:00
2023-01-10 06:14:16 +00:00
agent - > set_velocity_forced ( p_velocity ) ;
2020-01-10 11:22:34 +00:00
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_position , RID , p_agent , Vector3 , p_position ) {
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2020-01-10 11:22:34 +00:00
2023-01-10 06:14:16 +00:00
agent - > set_position ( p_position ) ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : agent_get_position ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , Vector3 ( ) ) ;
return agent - > get_position ( ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : agent_is_map_changed ( RID p_agent ) const {
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( agent , false ) ;
2020-01-10 11:22:34 +00:00
return agent - > is_map_changed ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_avoidance_callback , RID , p_agent , Callable , p_callback ) {
2023-02-01 21:11:10 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2020-01-10 11:22:34 +00:00
2023-01-10 06:14:16 +00:00
agent - > set_avoidance_callback ( p_callback ) ;
2020-01-10 11:22:34 +00:00
if ( agent - > get_map ( ) ) {
2023-01-28 04:15:10 +00:00
if ( p_callback . is_valid ( ) ) {
2020-01-10 11:22:34 +00:00
agent - > get_map ( ) - > set_agent_as_controlled ( agent ) ;
2023-01-28 04:15:10 +00:00
} else {
agent - > get_map ( ) - > remove_agent_as_controlled ( agent ) ;
2020-01-10 11:22:34 +00:00
}
}
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : agent_has_avoidance_callback ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , false ) ;
return agent - > has_avoidance_callback ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_avoidance_layers , RID , p_agent , uint32_t , p_layers ) {
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2023-01-10 06:14:16 +00:00
agent - > set_avoidance_layers ( p_layers ) ;
}
2023-10-13 15:20:31 +00:00
uint32_t GodotNavigationServer3D : : agent_get_avoidance_layers ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , 0 ) ;
return agent - > get_avoidance_layers ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_avoidance_mask , RID , p_agent , uint32_t , p_mask ) {
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2023-01-10 06:14:16 +00:00
agent - > set_avoidance_mask ( p_mask ) ;
}
2023-10-13 15:20:31 +00:00
uint32_t GodotNavigationServer3D : : agent_get_avoidance_mask ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , 0 ) ;
return agent - > get_avoidance_mask ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( agent_set_avoidance_priority , RID , p_agent , real_t , p_priority ) {
ERR_FAIL_COND_MSG ( p_priority < 0.0 , " Avoidance priority must be between 0.0 and 1.0 inclusive. " ) ;
ERR_FAIL_COND_MSG ( p_priority > 1.0 , " Avoidance priority must be between 0.0 and 1.0 inclusive. " ) ;
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( agent ) ;
2023-01-10 06:14:16 +00:00
agent - > set_avoidance_priority ( p_priority ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : agent_get_avoidance_priority ( RID p_agent ) const {
2023-12-19 18:51:49 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_agent ) ;
ERR_FAIL_NULL_V ( agent , 0 ) ;
return agent - > get_avoidance_priority ( ) ;
}
2023-10-13 15:20:31 +00:00
RID GodotNavigationServer3D : : obstacle_create ( ) {
2023-07-26 10:56:27 +00:00
MutexLock lock ( operations_mutex ) ;
2023-01-10 06:14:16 +00:00
RID rid = obstacle_owner . make_rid ( ) ;
NavObstacle * obstacle = obstacle_owner . get_or_null ( rid ) ;
obstacle - > set_self ( rid ) ;
2023-06-10 13:16:04 +00:00
RID agent_rid = agent_owner . make_rid ( ) ;
NavAgent * agent = agent_owner . get_or_null ( agent_rid ) ;
agent - > set_self ( agent_rid ) ;
obstacle - > set_agent ( agent ) ;
2023-01-10 06:14:16 +00:00
return rid ;
}
2023-06-10 13:16:04 +00:00
COMMAND_2 ( obstacle_set_avoidance_enabled , RID , p_obstacle , bool , p_enabled ) {
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( obstacle ) ;
2023-06-10 13:16:04 +00:00
obstacle - > set_avoidance_enabled ( p_enabled ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : obstacle_get_avoidance_enabled ( RID p_obstacle ) const {
2023-06-10 13:16:04 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( obstacle , false ) ;
2023-06-10 13:16:04 +00:00
return obstacle - > is_avoidance_enabled ( ) ;
}
COMMAND_2 ( obstacle_set_use_3d_avoidance , RID , p_obstacle , bool , p_enabled ) {
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( obstacle ) ;
2023-06-10 13:16:04 +00:00
obstacle - > set_use_3d_avoidance ( p_enabled ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : obstacle_get_use_3d_avoidance ( RID p_obstacle ) const {
2023-06-10 13:16:04 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( obstacle , false ) ;
2023-06-10 13:16:04 +00:00
return obstacle - > get_use_3d_avoidance ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( obstacle_set_map , RID , p_obstacle , RID , p_map ) {
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( obstacle ) ;
2023-01-10 06:14:16 +00:00
2023-06-25 03:28:21 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-01-10 06:14:16 +00:00
2023-06-25 03:28:21 +00:00
obstacle - > set_map ( map ) ;
2023-01-10 06:14:16 +00:00
}
2023-10-13 15:20:31 +00:00
RID GodotNavigationServer3D : : obstacle_get_map ( RID p_obstacle ) const {
2023-01-10 06:14:16 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( obstacle , RID ( ) ) ;
2023-01-10 06:14:16 +00:00
if ( obstacle - > get_map ( ) ) {
return obstacle - > get_map ( ) - > get_self ( ) ;
}
return RID ( ) ;
}
2023-06-15 13:35:53 +00:00
COMMAND_2 ( obstacle_set_paused , RID , p_obstacle , bool , p_paused ) {
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( obstacle ) ;
2023-06-15 13:35:53 +00:00
obstacle - > set_paused ( p_paused ) ;
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : obstacle_get_paused ( RID p_obstacle ) const {
2023-06-15 13:35:53 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( obstacle , false ) ;
2023-06-15 13:35:53 +00:00
return obstacle - > get_paused ( ) ;
}
2023-06-10 13:16:04 +00:00
COMMAND_2 ( obstacle_set_radius , RID , p_obstacle , real_t , p_radius ) {
ERR_FAIL_COND_MSG ( p_radius < 0.0 , " Radius must be positive. " ) ;
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( obstacle ) ;
2023-06-10 13:16:04 +00:00
obstacle - > set_radius ( p_radius ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : obstacle_get_radius ( RID p_obstacle ) const {
2023-12-19 18:51:49 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
ERR_FAIL_NULL_V ( obstacle , 0 ) ;
return obstacle - > get_radius ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( obstacle_set_height , RID , p_obstacle , real_t , p_height ) {
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( obstacle ) ;
2023-01-10 06:14:16 +00:00
obstacle - > set_height ( p_height ) ;
}
2023-10-13 15:20:31 +00:00
real_t GodotNavigationServer3D : : obstacle_get_height ( RID p_obstacle ) const {
2023-12-19 18:51:49 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
ERR_FAIL_NULL_V ( obstacle , 0 ) ;
return obstacle - > get_height ( ) ;
}
2023-06-10 13:16:04 +00:00
COMMAND_2 ( obstacle_set_velocity , RID , p_obstacle , Vector3 , p_velocity ) {
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( obstacle ) ;
2023-06-10 13:16:04 +00:00
obstacle - > set_velocity ( p_velocity ) ;
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : obstacle_get_velocity ( RID p_obstacle ) const {
2023-12-19 18:51:49 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
ERR_FAIL_NULL_V ( obstacle , Vector3 ( ) ) ;
return obstacle - > get_velocity ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( obstacle_set_position , RID , p_obstacle , Vector3 , p_position ) {
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( obstacle ) ;
2023-01-10 06:14:16 +00:00
obstacle - > set_position ( p_position ) ;
}
2023-10-13 15:20:31 +00:00
Vector3 GodotNavigationServer3D : : obstacle_get_position ( RID p_obstacle ) const {
2023-12-19 18:51:49 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
ERR_FAIL_NULL_V ( obstacle , Vector3 ( ) ) ;
return obstacle - > get_position ( ) ;
}
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : obstacle_set_vertices ( RID p_obstacle , const Vector < Vector3 > & p_vertices ) {
2023-01-10 06:14:16 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( obstacle ) ;
2023-01-10 06:14:16 +00:00
obstacle - > set_vertices ( p_vertices ) ;
}
2023-10-13 15:20:31 +00:00
Vector < Vector3 > GodotNavigationServer3D : : obstacle_get_vertices ( RID p_obstacle ) const {
2023-12-19 18:51:49 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
ERR_FAIL_NULL_V ( obstacle , Vector < Vector3 > ( ) ) ;
return obstacle - > get_vertices ( ) ;
}
2023-01-10 06:14:16 +00:00
COMMAND_2 ( obstacle_set_avoidance_layers , RID , p_obstacle , uint32_t , p_layers ) {
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( obstacle ) ;
2023-01-10 06:14:16 +00:00
obstacle - > set_avoidance_layers ( p_layers ) ;
}
2023-10-13 15:20:31 +00:00
uint32_t GodotNavigationServer3D : : obstacle_get_avoidance_layers ( RID p_obstacle ) const {
2023-12-19 18:51:49 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_obstacle ) ;
ERR_FAIL_NULL_V ( obstacle , 0 ) ;
return obstacle - > get_avoidance_layers ( ) ;
}
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : 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 ) {
2023-05-23 17:55:31 +00:00
# ifndef _3D_DISABLED
2023-07-07 13:59:10 +00:00
ERR_FAIL_COND_MSG ( ! Thread : : is_main_thread ( ) , " The SceneTree can only be parsed on the main thread. Call this function from the main thread or use call_deferred(). " ) ;
ERR_FAIL_COND_MSG ( ! p_navigation_mesh . is_valid ( ) , " Invalid navigation mesh. " ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_MSG ( p_root_node , " No parsing root node specified. " ) ;
2023-07-07 13:59:10 +00:00
ERR_FAIL_COND_MSG ( ! p_root_node - > is_inside_tree ( ) , " The root node needs to be inside the SceneTree. " ) ;
ERR_FAIL_NULL ( NavMeshGenerator3D : : get_singleton ( ) ) ;
NavMeshGenerator3D : : get_singleton ( ) - > parse_source_geometry_data ( p_navigation_mesh , p_source_geometry_data , p_root_node , p_callback ) ;
# endif // _3D_DISABLED
2023-05-23 17:55:31 +00:00
}
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : bake_from_source_geometry_data ( const Ref < NavigationMesh > & p_navigation_mesh , const Ref < NavigationMeshSourceGeometryData3D > & p_source_geometry_data , const Callable & p_callback ) {
2023-05-23 17:55:31 +00:00
# ifndef _3D_DISABLED
2023-07-07 13:59:10 +00:00
ERR_FAIL_COND_MSG ( ! p_navigation_mesh . is_valid ( ) , " Invalid navigation mesh. " ) ;
ERR_FAIL_COND_MSG ( ! p_source_geometry_data . is_valid ( ) , " Invalid NavigationMeshSourceGeometryData3D. " ) ;
ERR_FAIL_NULL ( NavMeshGenerator3D : : get_singleton ( ) ) ;
NavMeshGenerator3D : : get_singleton ( ) - > bake_from_source_geometry_data ( p_navigation_mesh , p_source_geometry_data , p_callback ) ;
# endif // _3D_DISABLED
2023-05-23 17:55:31 +00:00
}
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : bake_from_source_geometry_data_async ( const Ref < NavigationMesh > & p_navigation_mesh , const Ref < NavigationMeshSourceGeometryData3D > & p_source_geometry_data , const Callable & p_callback ) {
2023-07-07 13:59:10 +00:00
# ifndef _3D_DISABLED
ERR_FAIL_COND_MSG ( ! p_navigation_mesh . is_valid ( ) , " Invalid navigation mesh. " ) ;
ERR_FAIL_COND_MSG ( ! p_source_geometry_data . is_valid ( ) , " Invalid NavigationMeshSourceGeometryData3D. " ) ;
ERR_FAIL_NULL ( NavMeshGenerator3D : : get_singleton ( ) ) ;
NavMeshGenerator3D : : get_singleton ( ) - > bake_from_source_geometry_data_async ( p_navigation_mesh , p_source_geometry_data , p_callback ) ;
# endif // _3D_DISABLED
}
2023-10-13 15:20:31 +00:00
bool GodotNavigationServer3D : : is_baking_navigation_mesh ( Ref < NavigationMesh > p_navigation_mesh ) const {
2024-02-22 19:21:43 +00:00
# ifdef _3D_DISABLED
return false ;
# else
2024-01-29 21:28:01 +00:00
return NavMeshGenerator3D : : get_singleton ( ) - > is_baking ( p_navigation_mesh ) ;
2024-02-22 19:21:43 +00:00
# endif // _3D_DISABLED
2024-01-29 21:28:01 +00:00
}
2020-01-10 11:22:34 +00:00
COMMAND_1 ( free , RID , p_object ) {
if ( map_owner . owns ( p_object ) ) {
2021-09-29 17:08:41 +00:00
NavMap * map = map_owner . get_or_null ( p_object ) ;
2020-01-10 11:22:34 +00:00
// Removes any assigned region
2022-12-29 00:24:45 +00:00
for ( NavRegion * region : map - > get_regions ( ) ) {
map - > remove_region ( region ) ;
region - > set_map ( nullptr ) ;
2020-01-10 11:22:34 +00:00
}
2022-01-30 23:39:52 +00:00
// Removes any assigned links
2022-12-29 00:24:45 +00:00
for ( NavLink * link : map - > get_links ( ) ) {
map - > remove_link ( link ) ;
link - > set_map ( nullptr ) ;
2022-01-30 23:39:52 +00:00
}
2020-01-10 11:22:34 +00:00
// Remove any assigned agent
2023-02-01 21:11:10 +00:00
for ( NavAgent * agent : map - > get_agents ( ) ) {
2022-12-29 00:24:45 +00:00
map - > remove_agent ( agent ) ;
agent - > set_map ( nullptr ) ;
2020-01-10 11:22:34 +00:00
}
2023-01-10 06:14:16 +00:00
// Remove any assigned obstacles
for ( NavObstacle * obstacle : map - > get_obstacles ( ) ) {
map - > remove_obstacle ( obstacle ) ;
obstacle - > set_map ( nullptr ) ;
}
2021-03-15 11:45:28 +00:00
int map_index = active_maps . find ( map ) ;
2023-06-19 21:41:24 +00:00
if ( map_index > = 0 ) {
active_maps . remove_at ( map_index ) ;
2023-11-01 02:02:59 +00:00
active_maps_iteration_id . remove_at ( map_index ) ;
2023-06-19 21:41:24 +00:00
}
2020-01-10 11:22:34 +00:00
map_owner . free ( p_object ) ;
} else if ( region_owner . owns ( p_object ) ) {
2021-09-29 17:08:41 +00:00
NavRegion * region = region_owner . get_or_null ( p_object ) ;
2020-01-10 11:22:34 +00:00
// Removes this region from the map if assigned
2020-04-01 23:20:12 +00:00
if ( region - > get_map ( ) ! = nullptr ) {
2020-01-10 11:22:34 +00:00
region - > get_map ( ) - > remove_region ( region ) ;
2020-04-01 23:20:12 +00:00
region - > set_map ( nullptr ) ;
2020-01-10 11:22:34 +00:00
}
region_owner . free ( p_object ) ;
2022-01-30 23:39:52 +00:00
} else if ( link_owner . owns ( p_object ) ) {
NavLink * link = link_owner . get_or_null ( p_object ) ;
// Removes this link from the map if assigned
if ( link - > get_map ( ) ! = nullptr ) {
link - > get_map ( ) - > remove_link ( link ) ;
link - > set_map ( nullptr ) ;
}
link_owner . free ( p_object ) ;
2020-01-10 11:22:34 +00:00
} else if ( agent_owner . owns ( p_object ) ) {
2023-06-10 13:16:04 +00:00
internal_free_agent ( p_object ) ;
} else if ( obstacle_owner . owns ( p_object ) ) {
internal_free_obstacle ( p_object ) ;
2024-04-18 20:47:28 +00:00
# ifndef _3D_DISABLED
} else if ( navmesh_generator_3d & & navmesh_generator_3d - > owns ( p_object ) ) {
navmesh_generator_3d - > free ( p_object ) ;
# endif // _3D_DISABLED
2023-06-10 13:16:04 +00:00
} else {
ERR_PRINT ( " Attempted to free a NavigationServer RID that did not exist (or was already freed). " ) ;
}
}
2020-01-10 11:22:34 +00:00
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : internal_free_agent ( RID p_object ) {
2023-06-10 13:16:04 +00:00
NavAgent * agent = agent_owner . get_or_null ( p_object ) ;
if ( agent ) {
2020-04-01 23:20:12 +00:00
if ( agent - > get_map ( ) ! = nullptr ) {
2020-01-10 11:22:34 +00:00
agent - > get_map ( ) - > remove_agent ( agent ) ;
2020-04-01 23:20:12 +00:00
agent - > set_map ( nullptr ) ;
2020-01-10 11:22:34 +00:00
}
agent_owner . free ( p_object ) ;
2023-06-10 13:16:04 +00:00
}
}
2020-01-10 11:22:34 +00:00
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : internal_free_obstacle ( RID p_object ) {
2023-06-10 13:16:04 +00:00
NavObstacle * obstacle = obstacle_owner . get_or_null ( p_object ) ;
if ( obstacle ) {
2023-06-25 03:28:21 +00:00
NavAgent * obstacle_agent = obstacle - > get_agent ( ) ;
if ( obstacle_agent ) {
RID _agent_rid = obstacle_agent - > get_self ( ) ;
internal_free_agent ( _agent_rid ) ;
obstacle - > set_agent ( nullptr ) ;
}
2023-01-10 06:14:16 +00:00
if ( obstacle - > get_map ( ) ! = nullptr ) {
obstacle - > get_map ( ) - > remove_obstacle ( obstacle ) ;
obstacle - > set_map ( nullptr ) ;
}
obstacle_owner . free ( p_object ) ;
2020-01-10 11:22:34 +00:00
}
}
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : set_active ( bool p_active ) {
2022-12-21 22:21:43 +00:00
MutexLock lock ( operations_mutex ) ;
active = p_active ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : flush_queries ( ) {
2020-02-26 08:42:51 +00:00
// In c++ we can't be sure that this is performed in the main thread
// even with mutable functions.
2020-02-26 10:28:13 +00:00
MutexLock lock ( commands_mutex ) ;
MutexLock lock2 ( operations_mutex ) ;
2022-12-21 22:21:43 +00:00
2022-12-29 00:24:45 +00:00
for ( SetCommand * command : commands ) {
command - > exec ( this ) ;
memdelete ( command ) ;
2020-01-10 11:22:34 +00:00
}
commands . clear ( ) ;
2020-02-26 08:42:51 +00:00
}
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : map_force_update ( RID p_map ) {
2022-06-22 08:16:38 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL ( map ) ;
2022-06-22 08:16:38 +00:00
flush_queries ( ) ;
map - > sync ( ) ;
}
2023-10-13 15:20:31 +00:00
uint32_t GodotNavigationServer3D : : map_get_iteration_id ( RID p_map ) const {
2023-11-01 02:02:59 +00:00
NavMap * map = map_owner . get_or_null ( p_map ) ;
ERR_FAIL_NULL_V ( map , 0 ) ;
return map - > get_iteration_id ( ) ;
}
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : sync ( ) {
2023-08-17 16:32:30 +00:00
# ifndef _3D_DISABLED
if ( navmesh_generator_3d ) {
navmesh_generator_3d - > sync ( ) ;
}
# endif // _3D_DISABLED
}
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : process ( real_t p_delta_time ) {
2020-02-26 08:42:51 +00:00
flush_queries ( ) ;
2020-01-10 11:22:34 +00:00
2020-02-26 08:42:51 +00:00
if ( ! active ) {
return ;
}
2022-12-30 04:19:15 +00:00
int _new_pm_region_count = 0 ;
int _new_pm_agent_count = 0 ;
int _new_pm_link_count = 0 ;
int _new_pm_polygon_count = 0 ;
int _new_pm_edge_count = 0 ;
int _new_pm_edge_merge_count = 0 ;
int _new_pm_edge_connection_count = 0 ;
int _new_pm_edge_free_count = 0 ;
2024-06-25 02:50:43 +00:00
int _new_pm_obstacle_count = 0 ;
2022-12-30 04:19:15 +00:00
2020-02-26 08:42:51 +00:00
// In c++ we can't be sure that this is performed in the main thread
// even with mutable functions.
2020-02-26 10:28:13 +00:00
MutexLock lock ( operations_mutex ) ;
2021-03-15 11:45:28 +00:00
for ( uint32_t i ( 0 ) ; i < active_maps . size ( ) ; i + + ) {
2020-01-10 11:22:34 +00:00
active_maps [ i ] - > sync ( ) ;
active_maps [ i ] - > step ( p_delta_time ) ;
active_maps [ i ] - > dispatch_callbacks ( ) ;
2021-03-15 11:45:28 +00:00
2022-12-30 04:19:15 +00:00
_new_pm_region_count + = active_maps [ i ] - > get_pm_region_count ( ) ;
_new_pm_agent_count + = active_maps [ i ] - > get_pm_agent_count ( ) ;
_new_pm_link_count + = active_maps [ i ] - > get_pm_link_count ( ) ;
_new_pm_polygon_count + = active_maps [ i ] - > get_pm_polygon_count ( ) ;
_new_pm_edge_count + = active_maps [ i ] - > get_pm_edge_count ( ) ;
_new_pm_edge_merge_count + = active_maps [ i ] - > get_pm_edge_merge_count ( ) ;
_new_pm_edge_connection_count + = active_maps [ i ] - > get_pm_edge_connection_count ( ) ;
_new_pm_edge_free_count + = active_maps [ i ] - > get_pm_edge_free_count ( ) ;
2024-06-25 02:50:43 +00:00
_new_pm_obstacle_count + = active_maps [ i ] - > get_pm_obstacle_count ( ) ;
2022-12-30 04:19:15 +00:00
2021-03-15 11:45:28 +00:00
// Emit a signal if a map changed.
2023-11-01 02:02:59 +00:00
const uint32_t new_map_iteration_id = active_maps [ i ] - > get_iteration_id ( ) ;
if ( new_map_iteration_id ! = active_maps_iteration_id [ i ] ) {
2021-07-17 21:22:52 +00:00
emit_signal ( SNAME ( " map_changed " ) , active_maps [ i ] - > get_self ( ) ) ;
2023-11-01 02:02:59 +00:00
active_maps_iteration_id [ i ] = new_map_iteration_id ;
2021-03-15 11:45:28 +00:00
}
2020-01-10 11:22:34 +00:00
}
2022-12-30 04:19:15 +00:00
pm_region_count = _new_pm_region_count ;
pm_agent_count = _new_pm_agent_count ;
pm_link_count = _new_pm_link_count ;
pm_polygon_count = _new_pm_polygon_count ;
pm_edge_count = _new_pm_edge_count ;
pm_edge_merge_count = _new_pm_edge_merge_count ;
pm_edge_connection_count = _new_pm_edge_connection_count ;
pm_edge_free_count = _new_pm_edge_free_count ;
2024-06-25 02:50:43 +00:00
pm_obstacle_count = _new_pm_obstacle_count ;
2020-01-10 11:22:34 +00:00
}
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : init ( ) {
2023-07-07 13:59:10 +00:00
# ifndef _3D_DISABLED
navmesh_generator_3d = memnew ( NavMeshGenerator3D ) ;
# endif // _3D_DISABLED
}
2023-10-13 15:20:31 +00:00
void GodotNavigationServer3D : : finish ( ) {
2023-07-07 13:59:10 +00:00
flush_queries ( ) ;
# ifndef _3D_DISABLED
if ( navmesh_generator_3d ) {
navmesh_generator_3d - > finish ( ) ;
memdelete ( navmesh_generator_3d ) ;
navmesh_generator_3d = nullptr ;
}
# endif // _3D_DISABLED
}
2023-10-13 15:20:31 +00:00
PathQueryResult GodotNavigationServer3D : : _query_path ( const PathQueryParameters & p_parameters ) const {
2022-10-05 23:24:45 +00:00
PathQueryResult r_query_result ;
2022-06-26 10:43:01 +00:00
const NavMap * map = map_owner . get_or_null ( p_parameters . map ) ;
2023-09-09 15:40:07 +00:00
ERR_FAIL_NULL_V ( map , r_query_result ) ;
2022-06-26 10:43:01 +00:00
// run the pathfinding
2022-10-05 23:24:45 +00:00
if ( p_parameters . pathfinding_algorithm = = PathfindingAlgorithm : : PATHFINDING_ALGORITHM_ASTAR ) {
2022-06-26 10:43:01 +00:00
// while postprocessing is still part of map.get_path() need to check and route it here for the correct "optimize" post-processing
2022-10-05 23:24:45 +00:00
if ( p_parameters . path_postprocessing = = PathPostProcessing : : PATH_POSTPROCESSING_CORRIDORFUNNEL ) {
r_query_result . path = map - > get_path (
p_parameters . start_position ,
p_parameters . target_position ,
true ,
p_parameters . navigation_layers ,
p_parameters . metadata_flags . has_flag ( PathMetadataFlags : : PATH_INCLUDE_TYPES ) ? & r_query_result . path_types : nullptr ,
p_parameters . metadata_flags . has_flag ( PathMetadataFlags : : PATH_INCLUDE_RIDS ) ? & r_query_result . path_rids : nullptr ,
p_parameters . metadata_flags . has_flag ( PathMetadataFlags : : PATH_INCLUDE_OWNERS ) ? & r_query_result . path_owner_ids : nullptr ) ;
} else if ( p_parameters . path_postprocessing = = PathPostProcessing : : PATH_POSTPROCESSING_EDGECENTERED ) {
r_query_result . path = map - > get_path (
p_parameters . start_position ,
p_parameters . target_position ,
false ,
p_parameters . navigation_layers ,
p_parameters . metadata_flags . has_flag ( PathMetadataFlags : : PATH_INCLUDE_TYPES ) ? & r_query_result . path_types : nullptr ,
p_parameters . metadata_flags . has_flag ( PathMetadataFlags : : PATH_INCLUDE_RIDS ) ? & r_query_result . path_rids : nullptr ,
p_parameters . metadata_flags . has_flag ( PathMetadataFlags : : PATH_INCLUDE_OWNERS ) ? & r_query_result . path_owner_ids : nullptr ) ;
2022-06-26 10:43:01 +00:00
}
} else {
return r_query_result ;
}
// add path postprocessing
2024-04-09 02:27:54 +00:00
if ( r_query_result . path . size ( ) > 2 & & p_parameters . simplify_path ) {
const LocalVector < uint32_t > & simplified_path_indices = get_simplified_path_indices ( r_query_result . path , p_parameters . simplify_epsilon ) ;
uint32_t indices_count = simplified_path_indices . size ( ) ;
{
Vector3 * w = r_query_result . path . ptrw ( ) ;
const Vector3 * r = r_query_result . path . ptr ( ) ;
for ( uint32_t i = 0 ; i < indices_count ; i + + ) {
w [ i ] = r [ simplified_path_indices [ i ] ] ;
}
r_query_result . path . resize ( indices_count ) ;
}
if ( p_parameters . metadata_flags . has_flag ( PathMetadataFlags : : PATH_INCLUDE_TYPES ) ) {
int32_t * w = r_query_result . path_types . ptrw ( ) ;
const int32_t * r = r_query_result . path_types . ptr ( ) ;
for ( uint32_t i = 0 ; i < indices_count ; i + + ) {
w [ i ] = r [ simplified_path_indices [ i ] ] ;
}
r_query_result . path_types . resize ( indices_count ) ;
}
if ( p_parameters . metadata_flags . has_flag ( PathMetadataFlags : : PATH_INCLUDE_RIDS ) ) {
TypedArray < RID > simplified_path_rids ;
simplified_path_rids . resize ( indices_count ) ;
for ( uint32_t i = 0 ; i < indices_count ; i + + ) {
simplified_path_rids [ i ] = r_query_result . path_rids [ i ] ;
}
r_query_result . path_rids = simplified_path_rids ;
}
if ( p_parameters . metadata_flags . has_flag ( PathMetadataFlags : : PATH_INCLUDE_OWNERS ) ) {
int64_t * w = r_query_result . path_owner_ids . ptrw ( ) ;
const int64_t * r = r_query_result . path_owner_ids . ptr ( ) ;
for ( uint32_t i = 0 ; i < indices_count ; i + + ) {
w [ i ] = r [ simplified_path_indices [ i ] ] ;
}
r_query_result . path_owner_ids . resize ( indices_count ) ;
}
}
2022-06-26 10:43:01 +00:00
// add path stats
return r_query_result ;
}
2024-04-18 20:47:28 +00:00
RID GodotNavigationServer3D : : source_geometry_parser_create ( ) {
# ifndef _3D_DISABLED
if ( navmesh_generator_3d ) {
return navmesh_generator_3d - > source_geometry_parser_create ( ) ;
}
# endif // _3D_DISABLED
return RID ( ) ;
}
void GodotNavigationServer3D : : source_geometry_parser_set_callback ( RID p_parser , const Callable & p_callback ) {
# ifndef _3D_DISABLED
if ( navmesh_generator_3d ) {
navmesh_generator_3d - > source_geometry_parser_set_callback ( p_parser , p_callback ) ;
}
# endif // _3D_DISABLED
}
2024-04-09 02:27:54 +00:00
Vector < Vector3 > GodotNavigationServer3D : : simplify_path ( const Vector < Vector3 > & p_path , real_t p_epsilon ) {
if ( p_path . size ( ) < = 2 ) {
return p_path ;
}
p_epsilon = MAX ( 0.0 , p_epsilon ) ;
LocalVector < uint32_t > simplified_path_indices = get_simplified_path_indices ( p_path , p_epsilon ) ;
uint32_t indices_count = simplified_path_indices . size ( ) ;
Vector < Vector3 > simplified_path ;
simplified_path . resize ( indices_count ) ;
Vector3 * w = simplified_path . ptrw ( ) ;
const Vector3 * r = p_path . ptr ( ) ;
for ( uint32_t i = 0 ; i < indices_count ; i + + ) {
w [ i ] = r [ simplified_path_indices [ i ] ] ;
}
return simplified_path ;
}
LocalVector < uint32_t > GodotNavigationServer3D : : get_simplified_path_indices ( const Vector < Vector3 > & p_path , real_t p_epsilon ) {
p_epsilon = MAX ( 0.0 , p_epsilon ) ;
real_t squared_epsilon = p_epsilon * p_epsilon ;
LocalVector < bool > valid_points ;
valid_points . resize ( p_path . size ( ) ) ;
for ( uint32_t i = 0 ; i < valid_points . size ( ) ; i + + ) {
valid_points [ i ] = false ;
}
simplify_path_segment ( 0 , p_path . size ( ) - 1 , p_path , squared_epsilon , valid_points ) ;
int valid_point_index = 0 ;
for ( bool valid : valid_points ) {
if ( valid ) {
valid_point_index + = 1 ;
}
}
LocalVector < uint32_t > simplified_path_indices ;
simplified_path_indices . resize ( valid_point_index ) ;
valid_point_index = 0 ;
for ( uint32_t i = 0 ; i < valid_points . size ( ) ; i + + ) {
if ( valid_points [ i ] ) {
simplified_path_indices [ valid_point_index ] = i ;
valid_point_index + = 1 ;
}
}
return simplified_path_indices ;
}
void GodotNavigationServer3D : : simplify_path_segment ( int p_start_inx , int p_end_inx , const Vector < Vector3 > & p_points , real_t p_epsilon , LocalVector < bool > & r_valid_points ) {
r_valid_points [ p_start_inx ] = true ;
r_valid_points [ p_end_inx ] = true ;
const Vector3 & start_point = p_points [ p_start_inx ] ;
const Vector3 & end_point = p_points [ p_end_inx ] ;
Vector3 path_segment [ 2 ] = { start_point , end_point } ;
real_t point_max_distance = 0.0 ;
int point_max_index = 0 ;
for ( int i = p_start_inx ; i < p_end_inx ; i + + ) {
const Vector3 & checked_point = p_points [ i ] ;
const Vector3 closest_point = Geometry3D : : get_closest_point_to_segment ( checked_point , path_segment ) ;
real_t distance_squared = closest_point . distance_squared_to ( checked_point ) ;
if ( distance_squared > point_max_distance ) {
point_max_index = i ;
point_max_distance = distance_squared ;
}
}
if ( point_max_distance > p_epsilon ) {
simplify_path_segment ( p_start_inx , point_max_index , p_points , p_epsilon , r_valid_points ) ;
simplify_path_segment ( point_max_index , p_end_inx , p_points , p_epsilon , r_valid_points ) ;
}
}
2023-10-13 15:20:31 +00:00
int GodotNavigationServer3D : : get_process_info ( ProcessInfo p_info ) const {
2022-12-30 04:19:15 +00:00
switch ( p_info ) {
case INFO_ACTIVE_MAPS : {
return active_maps . size ( ) ;
} break ;
case INFO_REGION_COUNT : {
return pm_region_count ;
} break ;
case INFO_AGENT_COUNT : {
return pm_agent_count ;
} break ;
case INFO_LINK_COUNT : {
return pm_link_count ;
} break ;
case INFO_POLYGON_COUNT : {
return pm_polygon_count ;
} break ;
case INFO_EDGE_COUNT : {
return pm_edge_count ;
} break ;
case INFO_EDGE_MERGE_COUNT : {
return pm_edge_merge_count ;
} break ;
case INFO_EDGE_CONNECTION_COUNT : {
return pm_edge_connection_count ;
} break ;
case INFO_EDGE_FREE_COUNT : {
return pm_edge_free_count ;
} break ;
2024-06-25 02:50:43 +00:00
case INFO_OBSTACLE_COUNT : {
return pm_obstacle_count ;
} break ;
2022-12-30 04:19:15 +00:00
}
return 0 ;
}
2020-01-10 11:22:34 +00:00
# undef COMMAND_1
# undef COMMAND_2