Path: blob/master/servers/navigation_2d/navigation_server_2d.cpp
11322 views
/**************************************************************************/1/* navigation_server_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "navigation_server_2d.h"31#include "navigation_server_2d.compat.inc"3233#include "core/config/project_settings.h"34#include "scene/main/node.h"35#include "servers/navigation_2d/navigation_server_2d_dummy.h"3637NavigationServer2D *NavigationServer2D::singleton = nullptr;3839RWLock NavigationServer2D::geometry_parser_rwlock;40RID_Owner<NavMeshGeometryParser2D> NavigationServer2D::geometry_parser_owner;41LocalVector<NavMeshGeometryParser2D *> NavigationServer2D::generator_parsers;4243void NavigationServer2D::_bind_methods() {44ClassDB::bind_method(D_METHOD("get_maps"), &NavigationServer2D::get_maps);4546ClassDB::bind_method(D_METHOD("map_create"), &NavigationServer2D::map_create);47ClassDB::bind_method(D_METHOD("map_set_active", "map", "active"), &NavigationServer2D::map_set_active);48ClassDB::bind_method(D_METHOD("map_is_active", "map"), &NavigationServer2D::map_is_active);49ClassDB::bind_method(D_METHOD("map_set_cell_size", "map", "cell_size"), &NavigationServer2D::map_set_cell_size);50ClassDB::bind_method(D_METHOD("map_get_cell_size", "map"), &NavigationServer2D::map_get_cell_size);51ClassDB::bind_method(D_METHOD("map_set_merge_rasterizer_cell_scale", "map", "scale"), &NavigationServer2D::map_set_merge_rasterizer_cell_scale);52ClassDB::bind_method(D_METHOD("map_get_merge_rasterizer_cell_scale", "map"), &NavigationServer2D::map_get_merge_rasterizer_cell_scale);53ClassDB::bind_method(D_METHOD("map_set_use_edge_connections", "map", "enabled"), &NavigationServer2D::map_set_use_edge_connections);54ClassDB::bind_method(D_METHOD("map_get_use_edge_connections", "map"), &NavigationServer2D::map_get_use_edge_connections);55ClassDB::bind_method(D_METHOD("map_set_edge_connection_margin", "map", "margin"), &NavigationServer2D::map_set_edge_connection_margin);56ClassDB::bind_method(D_METHOD("map_get_edge_connection_margin", "map"), &NavigationServer2D::map_get_edge_connection_margin);57ClassDB::bind_method(D_METHOD("map_set_link_connection_radius", "map", "radius"), &NavigationServer2D::map_set_link_connection_radius);58ClassDB::bind_method(D_METHOD("map_get_link_connection_radius", "map"), &NavigationServer2D::map_get_link_connection_radius);59ClassDB::bind_method(D_METHOD("map_get_path", "map", "origin", "destination", "optimize", "navigation_layers"), &NavigationServer2D::map_get_path, DEFVAL(1));60ClassDB::bind_method(D_METHOD("map_get_closest_point", "map", "to_point"), &NavigationServer2D::map_get_closest_point);61ClassDB::bind_method(D_METHOD("map_get_closest_point_owner", "map", "to_point"), &NavigationServer2D::map_get_closest_point_owner);6263ClassDB::bind_method(D_METHOD("map_get_links", "map"), &NavigationServer2D::map_get_links);64ClassDB::bind_method(D_METHOD("map_get_regions", "map"), &NavigationServer2D::map_get_regions);65ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &NavigationServer2D::map_get_agents);66ClassDB::bind_method(D_METHOD("map_get_obstacles", "map"), &NavigationServer2D::map_get_obstacles);6768ClassDB::bind_method(D_METHOD("map_force_update", "map"), &NavigationServer2D::map_force_update);69ClassDB::bind_method(D_METHOD("map_get_iteration_id", "map"), &NavigationServer2D::map_get_iteration_id);70ClassDB::bind_method(D_METHOD("map_set_use_async_iterations", "map", "enabled"), &NavigationServer2D::map_set_use_async_iterations);71ClassDB::bind_method(D_METHOD("map_get_use_async_iterations", "map"), &NavigationServer2D::map_get_use_async_iterations);7273ClassDB::bind_method(D_METHOD("map_get_random_point", "map", "navigation_layers", "uniformly"), &NavigationServer2D::map_get_random_point);7475ClassDB::bind_method(D_METHOD("query_path", "parameters", "result", "callback"), &NavigationServer2D::query_path, DEFVAL(Callable()));7677ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer2D::region_create);78ClassDB::bind_method(D_METHOD("region_get_iteration_id", "region"), &NavigationServer2D::region_get_iteration_id);79ClassDB::bind_method(D_METHOD("region_set_use_async_iterations", "region", "enabled"), &NavigationServer2D::region_set_use_async_iterations);80ClassDB::bind_method(D_METHOD("region_get_use_async_iterations", "region"), &NavigationServer2D::region_get_use_async_iterations);81ClassDB::bind_method(D_METHOD("region_set_enabled", "region", "enabled"), &NavigationServer2D::region_set_enabled);82ClassDB::bind_method(D_METHOD("region_get_enabled", "region"), &NavigationServer2D::region_get_enabled);83ClassDB::bind_method(D_METHOD("region_set_use_edge_connections", "region", "enabled"), &NavigationServer2D::region_set_use_edge_connections);84ClassDB::bind_method(D_METHOD("region_get_use_edge_connections", "region"), &NavigationServer2D::region_get_use_edge_connections);85ClassDB::bind_method(D_METHOD("region_set_enter_cost", "region", "enter_cost"), &NavigationServer2D::region_set_enter_cost);86ClassDB::bind_method(D_METHOD("region_get_enter_cost", "region"), &NavigationServer2D::region_get_enter_cost);87ClassDB::bind_method(D_METHOD("region_set_travel_cost", "region", "travel_cost"), &NavigationServer2D::region_set_travel_cost);88ClassDB::bind_method(D_METHOD("region_get_travel_cost", "region"), &NavigationServer2D::region_get_travel_cost);89ClassDB::bind_method(D_METHOD("region_set_owner_id", "region", "owner_id"), &NavigationServer2D::region_set_owner_id);90ClassDB::bind_method(D_METHOD("region_get_owner_id", "region"), &NavigationServer2D::region_get_owner_id);91ClassDB::bind_method(D_METHOD("region_owns_point", "region", "point"), &NavigationServer2D::region_owns_point);92ClassDB::bind_method(D_METHOD("region_set_map", "region", "map"), &NavigationServer2D::region_set_map);93ClassDB::bind_method(D_METHOD("region_get_map", "region"), &NavigationServer2D::region_get_map);94ClassDB::bind_method(D_METHOD("region_set_navigation_layers", "region", "navigation_layers"), &NavigationServer2D::region_set_navigation_layers);95ClassDB::bind_method(D_METHOD("region_get_navigation_layers", "region"), &NavigationServer2D::region_get_navigation_layers);96ClassDB::bind_method(D_METHOD("region_set_transform", "region", "transform"), &NavigationServer2D::region_set_transform);97ClassDB::bind_method(D_METHOD("region_get_transform", "region"), &NavigationServer2D::region_get_transform);98ClassDB::bind_method(D_METHOD("region_set_navigation_polygon", "region", "navigation_polygon"), &NavigationServer2D::region_set_navigation_polygon);99ClassDB::bind_method(D_METHOD("region_get_connections_count", "region"), &NavigationServer2D::region_get_connections_count);100ClassDB::bind_method(D_METHOD("region_get_connection_pathway_start", "region", "connection"), &NavigationServer2D::region_get_connection_pathway_start);101ClassDB::bind_method(D_METHOD("region_get_connection_pathway_end", "region", "connection"), &NavigationServer2D::region_get_connection_pathway_end);102ClassDB::bind_method(D_METHOD("region_get_closest_point", "region", "to_point"), &NavigationServer2D::region_get_closest_point);103ClassDB::bind_method(D_METHOD("region_get_random_point", "region", "navigation_layers", "uniformly"), &NavigationServer2D::region_get_random_point);104ClassDB::bind_method(D_METHOD("region_get_bounds", "region"), &NavigationServer2D::region_get_bounds);105106ClassDB::bind_method(D_METHOD("link_create"), &NavigationServer2D::link_create);107ClassDB::bind_method(D_METHOD("link_get_iteration_id", "link"), &NavigationServer2D::link_get_iteration_id);108ClassDB::bind_method(D_METHOD("link_set_map", "link", "map"), &NavigationServer2D::link_set_map);109ClassDB::bind_method(D_METHOD("link_get_map", "link"), &NavigationServer2D::link_get_map);110ClassDB::bind_method(D_METHOD("link_set_enabled", "link", "enabled"), &NavigationServer2D::link_set_enabled);111ClassDB::bind_method(D_METHOD("link_get_enabled", "link"), &NavigationServer2D::link_get_enabled);112ClassDB::bind_method(D_METHOD("link_set_bidirectional", "link", "bidirectional"), &NavigationServer2D::link_set_bidirectional);113ClassDB::bind_method(D_METHOD("link_is_bidirectional", "link"), &NavigationServer2D::link_is_bidirectional);114ClassDB::bind_method(D_METHOD("link_set_navigation_layers", "link", "navigation_layers"), &NavigationServer2D::link_set_navigation_layers);115ClassDB::bind_method(D_METHOD("link_get_navigation_layers", "link"), &NavigationServer2D::link_get_navigation_layers);116ClassDB::bind_method(D_METHOD("link_set_start_position", "link", "position"), &NavigationServer2D::link_set_start_position);117ClassDB::bind_method(D_METHOD("link_get_start_position", "link"), &NavigationServer2D::link_get_start_position);118ClassDB::bind_method(D_METHOD("link_set_end_position", "link", "position"), &NavigationServer2D::link_set_end_position);119ClassDB::bind_method(D_METHOD("link_get_end_position", "link"), &NavigationServer2D::link_get_end_position);120ClassDB::bind_method(D_METHOD("link_set_enter_cost", "link", "enter_cost"), &NavigationServer2D::link_set_enter_cost);121ClassDB::bind_method(D_METHOD("link_get_enter_cost", "link"), &NavigationServer2D::link_get_enter_cost);122ClassDB::bind_method(D_METHOD("link_set_travel_cost", "link", "travel_cost"), &NavigationServer2D::link_set_travel_cost);123ClassDB::bind_method(D_METHOD("link_get_travel_cost", "link"), &NavigationServer2D::link_get_travel_cost);124ClassDB::bind_method(D_METHOD("link_set_owner_id", "link", "owner_id"), &NavigationServer2D::link_set_owner_id);125ClassDB::bind_method(D_METHOD("link_get_owner_id", "link"), &NavigationServer2D::link_get_owner_id);126127ClassDB::bind_method(D_METHOD("agent_create"), &NavigationServer2D::agent_create);128ClassDB::bind_method(D_METHOD("agent_set_avoidance_enabled", "agent", "enabled"), &NavigationServer2D::agent_set_avoidance_enabled);129ClassDB::bind_method(D_METHOD("agent_get_avoidance_enabled", "agent"), &NavigationServer2D::agent_get_avoidance_enabled);130ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &NavigationServer2D::agent_set_map);131ClassDB::bind_method(D_METHOD("agent_get_map", "agent"), &NavigationServer2D::agent_get_map);132ClassDB::bind_method(D_METHOD("agent_set_paused", "agent", "paused"), &NavigationServer2D::agent_set_paused);133ClassDB::bind_method(D_METHOD("agent_get_paused", "agent"), &NavigationServer2D::agent_get_paused);134ClassDB::bind_method(D_METHOD("agent_set_neighbor_distance", "agent", "distance"), &NavigationServer2D::agent_set_neighbor_distance);135ClassDB::bind_method(D_METHOD("agent_get_neighbor_distance", "agent"), &NavigationServer2D::agent_get_neighbor_distance);136ClassDB::bind_method(D_METHOD("agent_set_max_neighbors", "agent", "count"), &NavigationServer2D::agent_set_max_neighbors);137ClassDB::bind_method(D_METHOD("agent_get_max_neighbors", "agent"), &NavigationServer2D::agent_get_max_neighbors);138ClassDB::bind_method(D_METHOD("agent_set_time_horizon_agents", "agent", "time_horizon"), &NavigationServer2D::agent_set_time_horizon_agents);139ClassDB::bind_method(D_METHOD("agent_get_time_horizon_agents", "agent"), &NavigationServer2D::agent_get_time_horizon_agents);140ClassDB::bind_method(D_METHOD("agent_set_time_horizon_obstacles", "agent", "time_horizon"), &NavigationServer2D::agent_set_time_horizon_obstacles);141ClassDB::bind_method(D_METHOD("agent_get_time_horizon_obstacles", "agent"), &NavigationServer2D::agent_get_time_horizon_obstacles);142ClassDB::bind_method(D_METHOD("agent_set_radius", "agent", "radius"), &NavigationServer2D::agent_set_radius);143ClassDB::bind_method(D_METHOD("agent_get_radius", "agent"), &NavigationServer2D::agent_get_radius);144ClassDB::bind_method(D_METHOD("agent_set_max_speed", "agent", "max_speed"), &NavigationServer2D::agent_set_max_speed);145ClassDB::bind_method(D_METHOD("agent_get_max_speed", "agent"), &NavigationServer2D::agent_get_max_speed);146ClassDB::bind_method(D_METHOD("agent_set_velocity_forced", "agent", "velocity"), &NavigationServer2D::agent_set_velocity_forced);147ClassDB::bind_method(D_METHOD("agent_set_velocity", "agent", "velocity"), &NavigationServer2D::agent_set_velocity);148ClassDB::bind_method(D_METHOD("agent_get_velocity", "agent"), &NavigationServer2D::agent_get_velocity);149ClassDB::bind_method(D_METHOD("agent_set_position", "agent", "position"), &NavigationServer2D::agent_set_position);150ClassDB::bind_method(D_METHOD("agent_get_position", "agent"), &NavigationServer2D::agent_get_position);151ClassDB::bind_method(D_METHOD("agent_is_map_changed", "agent"), &NavigationServer2D::agent_is_map_changed);152ClassDB::bind_method(D_METHOD("agent_set_avoidance_callback", "agent", "callback"), &NavigationServer2D::agent_set_avoidance_callback);153ClassDB::bind_method(D_METHOD("agent_has_avoidance_callback", "agent"), &NavigationServer2D::agent_has_avoidance_callback);154ClassDB::bind_method(D_METHOD("agent_set_avoidance_layers", "agent", "layers"), &NavigationServer2D::agent_set_avoidance_layers);155ClassDB::bind_method(D_METHOD("agent_get_avoidance_layers", "agent"), &NavigationServer2D::agent_get_avoidance_layers);156ClassDB::bind_method(D_METHOD("agent_set_avoidance_mask", "agent", "mask"), &NavigationServer2D::agent_set_avoidance_mask);157ClassDB::bind_method(D_METHOD("agent_get_avoidance_mask", "agent"), &NavigationServer2D::agent_get_avoidance_mask);158ClassDB::bind_method(D_METHOD("agent_set_avoidance_priority", "agent", "priority"), &NavigationServer2D::agent_set_avoidance_priority);159ClassDB::bind_method(D_METHOD("agent_get_avoidance_priority", "agent"), &NavigationServer2D::agent_get_avoidance_priority);160161ClassDB::bind_method(D_METHOD("obstacle_create"), &NavigationServer2D::obstacle_create);162ClassDB::bind_method(D_METHOD("obstacle_set_avoidance_enabled", "obstacle", "enabled"), &NavigationServer2D::obstacle_set_avoidance_enabled);163ClassDB::bind_method(D_METHOD("obstacle_get_avoidance_enabled", "obstacle"), &NavigationServer2D::obstacle_get_avoidance_enabled);164ClassDB::bind_method(D_METHOD("obstacle_set_map", "obstacle", "map"), &NavigationServer2D::obstacle_set_map);165ClassDB::bind_method(D_METHOD("obstacle_get_map", "obstacle"), &NavigationServer2D::obstacle_get_map);166ClassDB::bind_method(D_METHOD("obstacle_set_paused", "obstacle", "paused"), &NavigationServer2D::obstacle_set_paused);167ClassDB::bind_method(D_METHOD("obstacle_get_paused", "obstacle"), &NavigationServer2D::obstacle_get_paused);168ClassDB::bind_method(D_METHOD("obstacle_set_radius", "obstacle", "radius"), &NavigationServer2D::obstacle_set_radius);169ClassDB::bind_method(D_METHOD("obstacle_get_radius", "obstacle"), &NavigationServer2D::obstacle_get_radius);170ClassDB::bind_method(D_METHOD("obstacle_set_velocity", "obstacle", "velocity"), &NavigationServer2D::obstacle_set_velocity);171ClassDB::bind_method(D_METHOD("obstacle_get_velocity", "obstacle"), &NavigationServer2D::obstacle_get_velocity);172ClassDB::bind_method(D_METHOD("obstacle_set_position", "obstacle", "position"), &NavigationServer2D::obstacle_set_position);173ClassDB::bind_method(D_METHOD("obstacle_get_position", "obstacle"), &NavigationServer2D::obstacle_get_position);174ClassDB::bind_method(D_METHOD("obstacle_set_vertices", "obstacle", "vertices"), &NavigationServer2D::obstacle_set_vertices);175ClassDB::bind_method(D_METHOD("obstacle_get_vertices", "obstacle"), &NavigationServer2D::obstacle_get_vertices);176ClassDB::bind_method(D_METHOD("obstacle_set_avoidance_layers", "obstacle", "layers"), &NavigationServer2D::obstacle_set_avoidance_layers);177ClassDB::bind_method(D_METHOD("obstacle_get_avoidance_layers", "obstacle"), &NavigationServer2D::obstacle_get_avoidance_layers);178179ClassDB::bind_method(D_METHOD("parse_source_geometry_data", "navigation_polygon", "source_geometry_data", "root_node", "callback"), &NavigationServer2D::parse_source_geometry_data, DEFVAL(Callable()));180ClassDB::bind_method(D_METHOD("bake_from_source_geometry_data", "navigation_polygon", "source_geometry_data", "callback"), &NavigationServer2D::bake_from_source_geometry_data, DEFVAL(Callable()));181ClassDB::bind_method(D_METHOD("bake_from_source_geometry_data_async", "navigation_polygon", "source_geometry_data", "callback"), &NavigationServer2D::bake_from_source_geometry_data_async, DEFVAL(Callable()));182ClassDB::bind_method(D_METHOD("is_baking_navigation_polygon", "navigation_polygon"), &NavigationServer2D::is_baking_navigation_polygon);183184ClassDB::bind_method(D_METHOD("source_geometry_parser_create"), &NavigationServer2D::source_geometry_parser_create);185ClassDB::bind_method(D_METHOD("source_geometry_parser_set_callback", "parser", "callback"), &NavigationServer2D::source_geometry_parser_set_callback);186187ClassDB::bind_method(D_METHOD("simplify_path", "path", "epsilon"), &NavigationServer2D::simplify_path);188189ClassDB::bind_method(D_METHOD("free_rid", "rid"), &NavigationServer2D::free_rid);190191ClassDB::bind_method(D_METHOD("set_active", "active"), &NavigationServer2D::set_active);192193ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationServer2D::set_debug_enabled);194ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationServer2D::get_debug_enabled);195196ADD_SIGNAL(MethodInfo("map_changed", PropertyInfo(Variant::RID, "map")));197198ADD_SIGNAL(MethodInfo("navigation_debug_changed"));199ADD_SIGNAL(MethodInfo("avoidance_debug_changed"));200201ClassDB::bind_method(D_METHOD("get_process_info", "process_info"), &NavigationServer2D::get_process_info);202203BIND_ENUM_CONSTANT(INFO_ACTIVE_MAPS);204BIND_ENUM_CONSTANT(INFO_REGION_COUNT);205BIND_ENUM_CONSTANT(INFO_AGENT_COUNT);206BIND_ENUM_CONSTANT(INFO_LINK_COUNT);207BIND_ENUM_CONSTANT(INFO_POLYGON_COUNT);208BIND_ENUM_CONSTANT(INFO_EDGE_COUNT);209BIND_ENUM_CONSTANT(INFO_EDGE_MERGE_COUNT);210BIND_ENUM_CONSTANT(INFO_EDGE_CONNECTION_COUNT);211BIND_ENUM_CONSTANT(INFO_EDGE_FREE_COUNT);212BIND_ENUM_CONSTANT(INFO_OBSTACLE_COUNT);213}214215NavigationServer2D *NavigationServer2D::get_singleton() {216return singleton;217}218219NavigationServer2D::NavigationServer2D() {220ERR_FAIL_COND(singleton != nullptr);221singleton = this;222223GLOBAL_DEF_BASIC(PropertyInfo(Variant::FLOAT, "navigation/2d/default_cell_size", PROPERTY_HINT_RANGE, NavigationDefaults2D::NAV_MESH_CELL_SIZE_HINT), NavigationDefaults2D::NAV_MESH_CELL_SIZE);224GLOBAL_DEF("navigation/2d/use_edge_connections", true);225GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "navigation/2d/merge_rasterizer_cell_scale", PROPERTY_HINT_RANGE, "0.001,1,0.001,or_greater"), 1.0);226GLOBAL_DEF_BASIC(PropertyInfo(Variant::FLOAT, "navigation/2d/default_edge_connection_margin", PROPERTY_HINT_RANGE, "0.01,10,0.001,or_greater"), NavigationDefaults2D::EDGE_CONNECTION_MARGIN);227GLOBAL_DEF_BASIC(PropertyInfo(Variant::FLOAT, "navigation/2d/default_link_connection_radius", PROPERTY_HINT_RANGE, "0.01,10,0.001,or_greater"), NavigationDefaults2D::LINK_CONNECTION_RADIUS);228229#ifdef DEBUG_ENABLED230debug_navigation_edge_connection_color = GLOBAL_DEF("debug/shapes/navigation/2d/edge_connection_color", Color(1.0, 0.0, 1.0, 1.0));231debug_navigation_geometry_edge_color = GLOBAL_DEF("debug/shapes/navigation/2d/geometry_edge_color", Color(0.5, 1.0, 1.0, 1.0));232debug_navigation_geometry_face_color = GLOBAL_DEF("debug/shapes/navigation/2d/geometry_face_color", Color(0.5, 1.0, 1.0, 0.4));233debug_navigation_geometry_edge_disabled_color = GLOBAL_DEF("debug/shapes/navigation/2d/geometry_edge_disabled_color", Color(0.5, 0.5, 0.5, 1.0));234debug_navigation_geometry_face_disabled_color = GLOBAL_DEF("debug/shapes/navigation/2d/geometry_face_disabled_color", Color(0.5, 0.5, 0.5, 0.4));235debug_navigation_link_connection_color = GLOBAL_DEF("debug/shapes/navigation/2d/link_connection_color", Color(1.0, 0.5, 1.0, 1.0));236debug_navigation_link_connection_disabled_color = GLOBAL_DEF("debug/shapes/navigation/2d/link_connection_disabled_color", Color(0.5, 0.5, 0.5, 1.0));237debug_navigation_agent_path_color = GLOBAL_DEF("debug/shapes/navigation/2d/agent_path_color", Color(1.0, 0.0, 0.0, 1.0));238239debug_navigation_enable_edge_connections = GLOBAL_DEF("debug/shapes/navigation/2d/enable_edge_connections", true);240debug_navigation_enable_edge_lines = GLOBAL_DEF("debug/shapes/navigation/2d/enable_edge_lines", true);241debug_navigation_enable_geometry_face_random_color = GLOBAL_DEF("debug/shapes/navigation/2d/enable_geometry_face_random_color", true);242debug_navigation_enable_link_connections = GLOBAL_DEF("debug/shapes/navigation/2d/enable_link_connections", true);243244debug_navigation_enable_agent_paths = GLOBAL_DEF("debug/shapes/navigation/2d/enable_agent_paths", true);245debug_navigation_agent_path_point_size = GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "debug/shapes/navigation/2d/agent_path_point_size", PROPERTY_HINT_RANGE, "0.01,10,0.001,or_greater"), 4.0);246247debug_navigation_avoidance_agents_radius_color = GLOBAL_DEF("debug/shapes/avoidance/2d/agents_radius_color", Color(1.0, 1.0, 0.0, 0.25));248debug_navigation_avoidance_obstacles_radius_color = GLOBAL_DEF("debug/shapes/avoidance/2d/obstacles_radius_color", Color(1.0, 0.5, 0.0, 0.25));249debug_navigation_avoidance_static_obstacle_pushin_face_color = GLOBAL_DEF("debug/shapes/avoidance/2d/obstacles_static_face_pushin_color", Color(1.0, 0.0, 0.0, 0.0));250debug_navigation_avoidance_static_obstacle_pushin_edge_color = GLOBAL_DEF("debug/shapes/avoidance/2d/obstacles_static_edge_pushin_color", Color(1.0, 0.0, 0.0, 1.0));251debug_navigation_avoidance_static_obstacle_pushout_face_color = GLOBAL_DEF("debug/shapes/avoidance/2d/obstacles_static_face_pushout_color", Color(1.0, 1.0, 0.0, 0.5));252debug_navigation_avoidance_static_obstacle_pushout_edge_color = GLOBAL_DEF("debug/shapes/avoidance/2d/obstacles_static_edge_pushout_color", Color(1.0, 1.0, 0.0, 1.0));253debug_navigation_avoidance_enable_agents_radius = GLOBAL_DEF("debug/shapes/avoidance/2d/enable_agents_radius", true);254debug_navigation_avoidance_enable_obstacles_radius = GLOBAL_DEF("debug/shapes/avoidance/2d/enable_obstacles_radius", true);255debug_navigation_avoidance_enable_obstacles_static = GLOBAL_DEF("debug/shapes/avoidance/2d/enable_obstacles_static", true);256257if (Engine::get_singleton()->is_editor_hint()) {258// enable NavigationServer3D when in Editor or else navigation mesh edge connections are invisible259// on runtime tests SceneTree has "Visible Navigation" set and main iteration takes care of this.260set_debug_enabled(true);261set_debug_navigation_enabled(true);262set_debug_avoidance_enabled(true);263}264#endif // DEBUG_ENABLED265}266267NavigationServer2D::~NavigationServer2D() {268singleton = nullptr;269270RWLockWrite write_lock(geometry_parser_rwlock);271for (NavMeshGeometryParser2D *parser : generator_parsers) {272geometry_parser_owner.free(parser->self);273}274generator_parsers.clear();275}276277RID NavigationServer2D::source_geometry_parser_create() {278RWLockWrite write_lock(geometry_parser_rwlock);279280RID rid = geometry_parser_owner.make_rid();281282NavMeshGeometryParser2D *parser = geometry_parser_owner.get_or_null(rid);283parser->self = rid;284285generator_parsers.push_back(parser);286287return rid;288}289290void NavigationServer2D::free_rid(RID p_rid) {291if (!geometry_parser_owner.owns(p_rid)) {292return;293}294RWLockWrite write_lock(geometry_parser_rwlock);295296NavMeshGeometryParser2D *parser = geometry_parser_owner.get_or_null(p_rid);297ERR_FAIL_NULL(parser);298299generator_parsers.erase(parser);300geometry_parser_owner.free(parser->self);301}302303void NavigationServer2D::source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) {304RWLockWrite write_lock(geometry_parser_rwlock);305306NavMeshGeometryParser2D *parser = geometry_parser_owner.get_or_null(p_parser);307ERR_FAIL_NULL(parser);308309parser->callback = p_callback;310}311312void NavigationServer2D::set_debug_enabled(bool p_enabled) {313#ifdef DEBUG_ENABLED314if (debug_enabled != p_enabled) {315debug_dirty = true;316}317318debug_enabled = p_enabled;319320if (debug_dirty) {321navigation_debug_dirty = true;322callable_mp(this, &NavigationServer2D::_emit_navigation_debug_changed_signal).call_deferred();323324avoidance_debug_dirty = true;325callable_mp(this, &NavigationServer2D::_emit_avoidance_debug_changed_signal).call_deferred();326}327#endif // DEBUG_ENABLED328}329330bool NavigationServer2D::get_debug_enabled() const {331return debug_enabled;332}333334#ifdef DEBUG_ENABLED335void NavigationServer2D::_emit_navigation_debug_changed_signal() {336if (navigation_debug_dirty) {337navigation_debug_dirty = false;338emit_signal(SNAME("navigation_debug_changed"));339}340}341342void NavigationServer2D::_emit_avoidance_debug_changed_signal() {343if (avoidance_debug_dirty) {344avoidance_debug_dirty = false;345emit_signal(SNAME("avoidance_debug_changed"));346}347}348#endif // DEBUG_ENABLED349350#ifdef DEBUG_ENABLED351void NavigationServer2D::set_debug_navigation_enabled(bool p_enabled) {352debug_navigation_enabled = p_enabled;353navigation_debug_dirty = true;354callable_mp(this, &NavigationServer2D::_emit_navigation_debug_changed_signal).call_deferred();355}356357bool NavigationServer2D::get_debug_navigation_enabled() const {358return debug_navigation_enabled;359}360361void NavigationServer2D::set_debug_avoidance_enabled(bool p_enabled) {362debug_avoidance_enabled = p_enabled;363avoidance_debug_dirty = true;364callable_mp(this, &NavigationServer2D::_emit_avoidance_debug_changed_signal).call_deferred();365}366367bool NavigationServer2D::get_debug_avoidance_enabled() const {368return debug_avoidance_enabled;369}370371void NavigationServer2D::set_debug_navigation_edge_connection_color(const Color &p_color) {372debug_navigation_edge_connection_color = p_color;373}374375Color NavigationServer2D::get_debug_navigation_edge_connection_color() const {376return debug_navigation_edge_connection_color;377}378379void NavigationServer2D::set_debug_navigation_geometry_face_color(const Color &p_color) {380debug_navigation_geometry_face_color = p_color;381}382383Color NavigationServer2D::get_debug_navigation_geometry_face_color() const {384return debug_navigation_geometry_face_color;385}386387void NavigationServer2D::set_debug_navigation_geometry_face_disabled_color(const Color &p_color) {388debug_navigation_geometry_face_disabled_color = p_color;389}390391Color NavigationServer2D::get_debug_navigation_geometry_face_disabled_color() const {392return debug_navigation_geometry_face_disabled_color;393}394395void NavigationServer2D::set_debug_navigation_link_connection_color(const Color &p_color) {396debug_navigation_link_connection_color = p_color;397}398399Color NavigationServer2D::get_debug_navigation_link_connection_color() const {400return debug_navigation_link_connection_color;401}402403void NavigationServer2D::set_debug_navigation_link_connection_disabled_color(const Color &p_color) {404debug_navigation_link_connection_disabled_color = p_color;405}406407Color NavigationServer2D::get_debug_navigation_link_connection_disabled_color() const {408return debug_navigation_link_connection_disabled_color;409}410411void NavigationServer2D::set_debug_navigation_geometry_edge_color(const Color &p_color) {412debug_navigation_geometry_edge_color = p_color;413}414415Color NavigationServer2D::get_debug_navigation_geometry_edge_color() const {416return debug_navigation_geometry_edge_color;417}418419void NavigationServer2D::set_debug_navigation_geometry_edge_disabled_color(const Color &p_color) {420debug_navigation_geometry_edge_disabled_color = p_color;421}422423Color NavigationServer2D::get_debug_navigation_geometry_edge_disabled_color() const {424return debug_navigation_geometry_edge_disabled_color;425}426427void NavigationServer2D::set_debug_navigation_enable_edge_connections(const bool p_value) {428debug_navigation_enable_edge_connections = p_value;429}430431bool NavigationServer2D::get_debug_navigation_enable_edge_connections() const {432return debug_navigation_enable_edge_connections;433}434435void NavigationServer2D::set_debug_navigation_enable_geometry_face_random_color(const bool p_value) {436debug_navigation_enable_geometry_face_random_color = p_value;437}438439bool NavigationServer2D::get_debug_navigation_enable_geometry_face_random_color() const {440return debug_navigation_enable_geometry_face_random_color;441}442443void NavigationServer2D::set_debug_navigation_enable_edge_lines(const bool p_value) {444debug_navigation_enable_edge_lines = p_value;445}446447bool NavigationServer2D::get_debug_navigation_enable_edge_lines() const {448return debug_navigation_enable_edge_lines;449}450451void NavigationServer2D::set_debug_navigation_agent_path_color(const Color &p_color) {452debug_navigation_agent_path_color = p_color;453}454455Color NavigationServer2D::get_debug_navigation_agent_path_color() const {456return debug_navigation_agent_path_color;457}458459void NavigationServer2D::set_debug_navigation_enable_agent_paths(const bool p_value) {460debug_navigation_enable_agent_paths = p_value;461}462463bool NavigationServer2D::get_debug_navigation_enable_agent_paths() const {464return debug_navigation_enable_agent_paths;465}466467void NavigationServer2D::set_debug_navigation_agent_path_point_size(real_t p_point_size) {468debug_navigation_agent_path_point_size = p_point_size;469}470471real_t NavigationServer2D::get_debug_navigation_agent_path_point_size() const {472return debug_navigation_agent_path_point_size;473}474475void NavigationServer2D::set_debug_navigation_avoidance_enable_agents_radius(const bool p_value) {476debug_navigation_avoidance_enable_agents_radius = p_value;477}478479bool NavigationServer2D::get_debug_navigation_avoidance_enable_agents_radius() const {480return debug_navigation_avoidance_enable_agents_radius;481}482483void NavigationServer2D::set_debug_navigation_avoidance_enable_obstacles_radius(const bool p_value) {484debug_navigation_avoidance_enable_obstacles_radius = p_value;485}486487bool NavigationServer2D::get_debug_navigation_avoidance_enable_obstacles_radius() const {488return debug_navigation_avoidance_enable_obstacles_radius;489}490491void NavigationServer2D::set_debug_navigation_avoidance_agents_radius_color(const Color &p_color) {492debug_navigation_avoidance_agents_radius_color = p_color;493}494495Color NavigationServer2D::get_debug_navigation_avoidance_agents_radius_color() const {496return debug_navigation_avoidance_agents_radius_color;497}498499void NavigationServer2D::set_debug_navigation_avoidance_obstacles_radius_color(const Color &p_color) {500debug_navigation_avoidance_obstacles_radius_color = p_color;501}502503Color NavigationServer2D::get_debug_navigation_avoidance_obstacles_radius_color() const {504return debug_navigation_avoidance_obstacles_radius_color;505}506507void NavigationServer2D::set_debug_navigation_avoidance_static_obstacle_pushin_face_color(const Color &p_color) {508debug_navigation_avoidance_static_obstacle_pushin_face_color = p_color;509}510511Color NavigationServer2D::get_debug_navigation_avoidance_static_obstacle_pushin_face_color() const {512return debug_navigation_avoidance_static_obstacle_pushin_face_color;513}514515void NavigationServer2D::set_debug_navigation_avoidance_static_obstacle_pushout_face_color(const Color &p_color) {516debug_navigation_avoidance_static_obstacle_pushout_face_color = p_color;517}518519Color NavigationServer2D::get_debug_navigation_avoidance_static_obstacle_pushout_face_color() const {520return debug_navigation_avoidance_static_obstacle_pushout_face_color;521}522523void NavigationServer2D::set_debug_navigation_avoidance_static_obstacle_pushin_edge_color(const Color &p_color) {524debug_navigation_avoidance_static_obstacle_pushin_edge_color = p_color;525}526527Color NavigationServer2D::get_debug_navigation_avoidance_static_obstacle_pushin_edge_color() const {528return debug_navigation_avoidance_static_obstacle_pushin_edge_color;529}530531void NavigationServer2D::set_debug_navigation_avoidance_static_obstacle_pushout_edge_color(const Color &p_color) {532debug_navigation_avoidance_static_obstacle_pushout_edge_color = p_color;533}534535Color NavigationServer2D::get_debug_navigation_avoidance_static_obstacle_pushout_edge_color() const {536return debug_navigation_avoidance_static_obstacle_pushout_edge_color;537}538539void NavigationServer2D::set_debug_navigation_avoidance_enable_obstacles_static(const bool p_value) {540debug_navigation_avoidance_enable_obstacles_static = p_value;541}542543bool NavigationServer2D::get_debug_navigation_avoidance_enable_obstacles_static() const {544return debug_navigation_avoidance_enable_obstacles_static;545}546#endif // DEBUG_ENABLED547548///////////////////////////////////////////////////////549550static NavigationServer2D *navigation_server_2d = nullptr;551552NavigationServer2DCallback NavigationServer2DManager::create_callback = nullptr;553554void NavigationServer2DManager::set_default_server(NavigationServer2DCallback p_callback) {555create_callback = p_callback;556}557558NavigationServer2D *NavigationServer2DManager::new_default_server() {559if (create_callback == nullptr) {560return nullptr;561}562563return create_callback();564}565566void NavigationServer2DManager::initialize_server() {567ERR_FAIL_COND(navigation_server_2d != nullptr);568569// Init 2D Navigation Server570navigation_server_2d = NavigationServer2DManager::new_default_server();571if (!navigation_server_2d) {572WARN_VERBOSE("Failed to initialize NavigationServer2D. Fall back to dummy server.");573navigation_server_2d = memnew(NavigationServer2DDummy);574}575576ERR_FAIL_NULL_MSG(navigation_server_2d, "Failed to initialize NavigationServer2D.");577navigation_server_2d->init();578}579580void NavigationServer2DManager::finalize_server() {581ERR_FAIL_NULL(navigation_server_2d);582navigation_server_2d->finish();583memdelete(navigation_server_2d);584navigation_server_2d = nullptr;585}586587588