Path: blob/master/modules/navigation_2d/2d/godot_navigation_server_2d.h
10278 views
/**************************************************************************/1/* godot_navigation_server_2d.h */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#pragma once3132#include "../nav_agent_2d.h"33#include "../nav_link_2d.h"34#include "../nav_map_2d.h"35#include "../nav_obstacle_2d.h"36#include "../nav_region_2d.h"3738#include "core/templates/local_vector.h"39#include "core/templates/rid.h"40#include "core/templates/rid_owner.h"41#include "servers/navigation/navigation_path_query_parameters_2d.h"42#include "servers/navigation/navigation_path_query_result_2d.h"43#include "servers/navigation_server_2d.h"4445/// The commands are functions executed during the `sync` phase.4647#define MERGE_INTERNAL(A, B) A##B48#define MERGE(A, B) MERGE_INTERNAL(A, B)4950#define COMMAND_1(F_NAME, T_0, D_0) \51virtual void F_NAME(T_0 D_0) override; \52void MERGE(_cmd_, F_NAME)(T_0 D_0)5354#define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \55virtual void F_NAME(T_0 D_0, T_1 D_1) override; \56void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)5758class GodotNavigationServer2D;59#ifdef CLIPPER2_ENABLED60class NavMeshGenerator2D;61#endif // CLIPPER2_ENABLED6263struct SetCommand2D {64virtual ~SetCommand2D() {}65virtual void exec(GodotNavigationServer2D *p_server) = 0;66};6768// This server exposes the `NavigationServer3D` features in the 2D world.69class GodotNavigationServer2D : public NavigationServer2D {70GDCLASS(GodotNavigationServer2D, NavigationServer2D);7172Mutex commands_mutex;73/// Mutex used to make any operation threadsafe.74Mutex operations_mutex;7576LocalVector<SetCommand2D *> commands;7778mutable RID_Owner<NavLink2D> link_owner;79mutable RID_Owner<NavMap2D> map_owner;80mutable RID_Owner<NavRegion2D> region_owner;81mutable RID_Owner<NavAgent2D> agent_owner;82mutable RID_Owner<NavObstacle2D> obstacle_owner;8384bool active = true;85LocalVector<NavMap2D *> active_maps;8687#ifdef CLIPPER2_ENABLED88NavMeshGenerator2D *navmesh_generator_2d = nullptr;89#endif // CLIPPER2_ENABLED9091// Performance Monitor.92int pm_region_count = 0;93int pm_agent_count = 0;94int pm_link_count = 0;95int pm_polygon_count = 0;96int pm_edge_count = 0;97int pm_edge_merge_count = 0;98int pm_edge_connection_count = 0;99int pm_edge_free_count = 0;100int pm_obstacle_count = 0;101102public:103GodotNavigationServer2D();104virtual ~GodotNavigationServer2D();105106void add_command(SetCommand2D *p_command);107108virtual TypedArray<RID> get_maps() const override;109110virtual RID map_create() override;111COMMAND_2(map_set_active, RID, p_map, bool, p_active);112virtual bool map_is_active(RID p_map) const override;113114COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size);115virtual real_t map_get_cell_size(RID p_map) const override;116117COMMAND_2(map_set_merge_rasterizer_cell_scale, RID, p_map, float, p_value);118virtual float map_get_merge_rasterizer_cell_scale(RID p_map) const override;119120COMMAND_2(map_set_use_edge_connections, RID, p_map, bool, p_enabled);121virtual bool map_get_use_edge_connections(RID p_map) const override;122123COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin);124virtual real_t map_get_edge_connection_margin(RID p_map) const override;125126COMMAND_2(map_set_link_connection_radius, RID, p_map, real_t, p_connection_radius);127virtual real_t map_get_link_connection_radius(RID p_map) const override;128129virtual Vector<Vector2> map_get_path(RID p_map, Vector2 p_origin, Vector2 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) override;130131virtual Vector2 map_get_closest_point(RID p_map, const Vector2 &p_point) const override;132133virtual RID map_get_closest_point_owner(RID p_map, const Vector2 &p_point) const override;134135virtual TypedArray<RID> map_get_links(RID p_map) const override;136virtual TypedArray<RID> map_get_regions(RID p_map) const override;137virtual TypedArray<RID> map_get_agents(RID p_map) const override;138virtual TypedArray<RID> map_get_obstacles(RID p_map) const override;139140virtual void map_force_update(RID p_map) override;141virtual uint32_t map_get_iteration_id(RID p_map) const override;142143COMMAND_2(map_set_use_async_iterations, RID, p_map, bool, p_enabled);144virtual bool map_get_use_async_iterations(RID p_map) const override;145146virtual Vector2 map_get_random_point(RID p_map, uint32_t p_navigation_layers, bool p_uniformly) const override;147148virtual RID region_create() override;149virtual uint32_t region_get_iteration_id(RID p_region) const override;150151COMMAND_2(region_set_use_async_iterations, RID, p_region, bool, p_enabled);152virtual bool region_get_use_async_iterations(RID p_region) const override;153154COMMAND_2(region_set_enabled, RID, p_region, bool, p_enabled);155virtual bool region_get_enabled(RID p_region) const override;156157COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled);158virtual bool region_get_use_edge_connections(RID p_region) const override;159160COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost);161virtual real_t region_get_enter_cost(RID p_region) const override;162COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost);163virtual real_t region_get_travel_cost(RID p_region) const override;164165COMMAND_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id);166virtual ObjectID region_get_owner_id(RID p_region) const override;167168virtual bool region_owns_point(RID p_region, const Vector2 &p_point) const override;169170COMMAND_2(region_set_map, RID, p_region, RID, p_map);171virtual RID region_get_map(RID p_region) const override;172COMMAND_2(region_set_navigation_layers, RID, p_region, uint32_t, p_navigation_layers);173virtual uint32_t region_get_navigation_layers(RID p_region) const override;174COMMAND_2(region_set_transform, RID, p_region, Transform2D, p_transform);175virtual Transform2D region_get_transform(RID p_region) const override;176COMMAND_2(region_set_navigation_polygon, RID, p_region, Ref<NavigationPolygon>, p_navigation_polygon);177virtual int region_get_connections_count(RID p_region) const override;178virtual Vector2 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;179virtual Vector2 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;180virtual Vector2 region_get_closest_point(RID p_region, const Vector2 &p_point) const override;181virtual Vector2 region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const override;182virtual Rect2 region_get_bounds(RID p_region) const override;183184virtual RID link_create() override;185virtual uint32_t link_get_iteration_id(RID p_link) const override;186187/// Set the map of this link.188COMMAND_2(link_set_map, RID, p_link, RID, p_map);189virtual RID link_get_map(RID p_link) const override;190COMMAND_2(link_set_enabled, RID, p_link, bool, p_enabled);191virtual bool link_get_enabled(RID p_link) const override;192193/// Set whether this link travels in both directions.194COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional);195virtual bool link_is_bidirectional(RID p_link) const override;196197/// Set the link's layers.198COMMAND_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers);199virtual uint32_t link_get_navigation_layers(RID p_link) const override;200201/// Set the start position of the link.202COMMAND_2(link_set_start_position, RID, p_link, Vector2, p_position);203virtual Vector2 link_get_start_position(RID p_link) const override;204205/// Set the end position of the link.206COMMAND_2(link_set_end_position, RID, p_link, Vector2, p_position);207virtual Vector2 link_get_end_position(RID p_link) const override;208209/// Set the enter cost of the link.210COMMAND_2(link_set_enter_cost, RID, p_link, real_t, p_enter_cost);211virtual real_t link_get_enter_cost(RID p_link) const override;212213/// Set the travel cost of the link.214COMMAND_2(link_set_travel_cost, RID, p_link, real_t, p_travel_cost);215virtual real_t link_get_travel_cost(RID p_link) const override;216217/// Set the node which manages this link.218COMMAND_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id);219virtual ObjectID link_get_owner_id(RID p_link) const override;220221/// Creates the agent.222virtual RID agent_create() override;223224/// Put the agent in the map.225COMMAND_2(agent_set_map, RID, p_agent, RID, p_map);226virtual RID agent_get_map(RID p_agent) const override;227228COMMAND_2(agent_set_paused, RID, p_agent, bool, p_paused);229virtual bool agent_get_paused(RID p_agent) const override;230231COMMAND_2(agent_set_avoidance_enabled, RID, p_agent, bool, p_enabled);232virtual bool agent_get_avoidance_enabled(RID p_agent) const override;233234/// The maximum distance (center point to235/// center point) to other agents this agent236/// takes into account in the navigation. The237/// larger this number, the longer the running238/// time of the simulation. If the number is too239/// low, the simulation will not be safe.240/// Must be non-negative.241COMMAND_2(agent_set_neighbor_distance, RID, p_agent, real_t, p_distance);242virtual real_t agent_get_neighbor_distance(RID p_agent) const override;243244/// The maximum number of other agents this245/// agent takes into account in the navigation.246/// The larger this number, the longer the247/// running time of the simulation. If the248/// number is too low, the simulation will not249/// be safe.250COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count);251virtual int agent_get_max_neighbors(RID p_agent) const override;252253/// The minimal amount of time for which this254/// agent's velocities that are computed by the255/// simulation are safe with respect to other256/// agents. The larger this number, the sooner257/// this agent will respond to the presence of258/// other agents, but the less freedom this259/// agent has in choosing its velocities.260/// Must be positive.261COMMAND_2(agent_set_time_horizon_agents, RID, p_agent, real_t, p_time_horizon);262virtual real_t agent_get_time_horizon_agents(RID p_agent) const override;263COMMAND_2(agent_set_time_horizon_obstacles, RID, p_agent, real_t, p_time_horizon);264virtual real_t agent_get_time_horizon_obstacles(RID p_agent) const override;265266/// The radius of this agent.267/// Must be non-negative.268COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius);269virtual real_t agent_get_radius(RID p_agent) const override;270271/// The maximum speed of this agent.272/// Must be non-negative.273COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed);274virtual real_t agent_get_max_speed(RID p_agent) const override;275276/// forces and agent velocity change in the avoidance simulation, adds simulation instability if done recklessly277COMMAND_2(agent_set_velocity_forced, RID, p_agent, Vector2, p_velocity);278279/// The wanted velocity for the agent as a "suggestion" to the avoidance simulation.280/// The simulation will try to fulfill this velocity wish if possible but may change the velocity depending on other agent's and obstacles'.281COMMAND_2(agent_set_velocity, RID, p_agent, Vector2, p_velocity);282virtual Vector2 agent_get_velocity(RID p_agent) const override;283284/// Position of the agent in world space.285COMMAND_2(agent_set_position, RID, p_agent, Vector2, p_position);286virtual Vector2 agent_get_position(RID p_agent) const override;287288/// Returns true if the map got changed the previous frame.289virtual bool agent_is_map_changed(RID p_agent) const override;290291/// Callback called at the end of the RVO process292COMMAND_2(agent_set_avoidance_callback, RID, p_agent, Callable, p_callback);293virtual bool agent_has_avoidance_callback(RID p_agent) const override;294295COMMAND_2(agent_set_avoidance_layers, RID, p_agent, uint32_t, p_layers);296virtual uint32_t agent_get_avoidance_layers(RID p_agent) const override;297298COMMAND_2(agent_set_avoidance_mask, RID, p_agent, uint32_t, p_mask);299virtual uint32_t agent_get_avoidance_mask(RID p_agent) const override;300301COMMAND_2(agent_set_avoidance_priority, RID, p_agent, real_t, p_priority);302virtual real_t agent_get_avoidance_priority(RID p_agent) const override;303304virtual RID obstacle_create() override;305COMMAND_2(obstacle_set_avoidance_enabled, RID, p_obstacle, bool, p_enabled);306virtual bool obstacle_get_avoidance_enabled(RID p_obstacle) const override;307COMMAND_2(obstacle_set_map, RID, p_obstacle, RID, p_map);308virtual RID obstacle_get_map(RID p_obstacle) const override;309COMMAND_2(obstacle_set_paused, RID, p_obstacle, bool, p_paused);310virtual bool obstacle_get_paused(RID p_obstacle) const override;311COMMAND_2(obstacle_set_radius, RID, p_obstacle, real_t, p_radius);312virtual real_t obstacle_get_radius(RID p_obstacle) const override;313COMMAND_2(obstacle_set_velocity, RID, p_obstacle, Vector2, p_velocity);314virtual Vector2 obstacle_get_velocity(RID p_obstacle) const override;315COMMAND_2(obstacle_set_position, RID, p_obstacle, Vector2, p_position);316virtual Vector2 obstacle_get_position(RID p_obstacle) const override;317COMMAND_2(obstacle_set_vertices, RID, p_obstacle, const Vector<Vector2> &, p_vertices);318virtual Vector<Vector2> obstacle_get_vertices(RID p_obstacle) const override;319COMMAND_2(obstacle_set_avoidance_layers, RID, p_obstacle, uint32_t, p_layers);320virtual uint32_t obstacle_get_avoidance_layers(RID p_obstacle) const override;321322virtual void query_path(const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result, const Callable &p_callback = Callable()) override;323324COMMAND_1(free, RID, p_object);325326virtual void set_active(bool p_active) override;327328void flush_queries();329330virtual void process(double p_delta_time) override;331virtual void physics_process(double p_delta_time) override;332virtual void init() override;333virtual void sync() override;334virtual void finish() override;335336virtual int get_process_info(ProcessInfo p_info) const override;337338virtual void parse_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override;339virtual void bake_from_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;340virtual void bake_from_source_geometry_data_async(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;341virtual bool is_baking_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) const override;342343virtual RID source_geometry_parser_create() override;344virtual void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) override;345346virtual Vector<Vector2> simplify_path(const Vector<Vector2> &p_path, real_t p_epsilon) override;347348private:349void internal_free_agent(RID p_object);350void internal_free_obstacle(RID p_object);351};352353#undef COMMAND_1354#undef COMMAND_2355356357