nav_region_2d.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**************************************************************************/
  2. /* nav_region_2d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "nav_region_2d.h"
  31. #include "nav_map_2d.h"
  32. #include "triangle2.h"
  33. #include "2d/nav_map_builder_2d.h"
  34. #include "2d/nav_mesh_queries_2d.h"
  35. #include "2d/nav_region_iteration_2d.h"
  36. using namespace nav_2d;
  37. void NavRegion2D::set_map(NavMap2D *p_map) {
  38. if (map == p_map) {
  39. return;
  40. }
  41. cancel_sync_request();
  42. if (map) {
  43. map->remove_region(this);
  44. }
  45. map = p_map;
  46. polygons_dirty = true;
  47. if (map) {
  48. map->add_region(this);
  49. request_sync();
  50. }
  51. }
  52. void NavRegion2D::set_enabled(bool p_enabled) {
  53. if (enabled == p_enabled) {
  54. return;
  55. }
  56. enabled = p_enabled;
  57. // TODO: This should not require a full rebuild as the region has not really changed.
  58. polygons_dirty = true;
  59. request_sync();
  60. }
  61. void NavRegion2D::set_use_edge_connections(bool p_enabled) {
  62. if (use_edge_connections != p_enabled) {
  63. use_edge_connections = p_enabled;
  64. polygons_dirty = true;
  65. }
  66. request_sync();
  67. }
  68. void NavRegion2D::set_transform(const Transform2D &p_transform) {
  69. if (transform == p_transform) {
  70. return;
  71. }
  72. transform = p_transform;
  73. polygons_dirty = true;
  74. request_sync();
  75. }
  76. void NavRegion2D::set_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) {
  77. #ifdef DEBUG_ENABLED
  78. if (map && p_navigation_polygon.is_valid() && !Math::is_equal_approx(double(map->get_cell_size()), double(p_navigation_polygon->get_cell_size()))) {
  79. ERR_PRINT_ONCE(vformat("Attempted to update a navigation region with a navigation mesh that uses a `cell_size` of %s while assigned to a navigation map set to a `cell_size` of %s. The cell size for navigation maps can be changed by using the NavigationServer map_set_cell_size() function. The cell size for default navigation maps can also be changed in the ProjectSettings.", double(p_navigation_polygon->get_cell_size()), double(map->get_cell_size())));
  80. }
  81. #endif // DEBUG_ENABLED
  82. RWLockWrite write_lock(navmesh_rwlock);
  83. pending_navmesh_vertices.clear();
  84. pending_navmesh_polygons.clear();
  85. if (p_navigation_polygon.is_valid()) {
  86. p_navigation_polygon->get_data(pending_navmesh_vertices, pending_navmesh_polygons);
  87. }
  88. polygons_dirty = true;
  89. request_sync();
  90. }
  91. ClosestPointQueryResult NavRegion2D::get_closest_point_info(const Vector2 &p_point) const {
  92. RWLockRead read_lock(region_rwlock);
  93. return NavMeshQueries2D::polygons_get_closest_point_info(get_polygons(), p_point);
  94. }
  95. Vector2 NavRegion2D::get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const {
  96. RWLockRead read_lock(region_rwlock);
  97. if (!get_enabled()) {
  98. return Vector2();
  99. }
  100. return NavMeshQueries2D::polygons_get_random_point(get_polygons(), p_navigation_layers, p_uniformly);
  101. }
  102. void NavRegion2D::set_navigation_layers(uint32_t p_navigation_layers) {
  103. if (navigation_layers == p_navigation_layers) {
  104. return;
  105. }
  106. navigation_layers = p_navigation_layers;
  107. region_dirty = true;
  108. request_sync();
  109. }
  110. void NavRegion2D::set_enter_cost(real_t p_enter_cost) {
  111. real_t new_enter_cost = MAX(p_enter_cost, 0.0);
  112. if (enter_cost == new_enter_cost) {
  113. return;
  114. }
  115. enter_cost = new_enter_cost;
  116. region_dirty = true;
  117. request_sync();
  118. }
  119. void NavRegion2D::set_travel_cost(real_t p_travel_cost) {
  120. real_t new_travel_cost = MAX(p_travel_cost, 0.0);
  121. if (travel_cost == new_travel_cost) {
  122. return;
  123. }
  124. travel_cost = new_travel_cost;
  125. region_dirty = true;
  126. request_sync();
  127. }
  128. void NavRegion2D::set_owner_id(ObjectID p_owner_id) {
  129. if (owner_id == p_owner_id) {
  130. return;
  131. }
  132. owner_id = p_owner_id;
  133. region_dirty = true;
  134. request_sync();
  135. }
  136. bool NavRegion2D::sync() {
  137. RWLockWrite write_lock(region_rwlock);
  138. bool something_changed = region_dirty || polygons_dirty;
  139. region_dirty = false;
  140. update_polygons();
  141. return something_changed;
  142. }
  143. void NavRegion2D::update_polygons() {
  144. if (!polygons_dirty) {
  145. return;
  146. }
  147. navmesh_polygons.clear();
  148. surface_area = 0.0;
  149. bounds = Rect2();
  150. polygons_dirty = false;
  151. if (map == nullptr) {
  152. return;
  153. }
  154. RWLockRead read_lock(navmesh_rwlock);
  155. if (pending_navmesh_vertices.is_empty() || pending_navmesh_polygons.is_empty()) {
  156. return;
  157. }
  158. int len = pending_navmesh_vertices.size();
  159. if (len == 0) {
  160. return;
  161. }
  162. const Vector2 *vertices_r = pending_navmesh_vertices.ptr();
  163. navmesh_polygons.resize(pending_navmesh_polygons.size());
  164. real_t _new_region_surface_area = 0.0;
  165. Rect2 _new_bounds;
  166. bool first_vertex = true;
  167. int navigation_mesh_polygon_index = 0;
  168. for (Polygon &polygon : navmesh_polygons) {
  169. polygon.surface_area = 0.0;
  170. Vector<int> navigation_mesh_polygon = pending_navmesh_polygons[navigation_mesh_polygon_index];
  171. navigation_mesh_polygon_index += 1;
  172. int navigation_mesh_polygon_size = navigation_mesh_polygon.size();
  173. if (navigation_mesh_polygon_size < 3) {
  174. continue;
  175. }
  176. const int *indices = navigation_mesh_polygon.ptr();
  177. bool valid(true);
  178. polygon.points.resize(navigation_mesh_polygon_size);
  179. polygon.edges.resize(navigation_mesh_polygon_size);
  180. real_t _new_polygon_surface_area = 0.0;
  181. for (int j(2); j < navigation_mesh_polygon_size; j++) {
  182. const Triangle2 triangle = Triangle2(
  183. transform.xform(vertices_r[indices[0]]),
  184. transform.xform(vertices_r[indices[j - 1]]),
  185. transform.xform(vertices_r[indices[j]]));
  186. _new_polygon_surface_area += triangle.get_area();
  187. }
  188. polygon.surface_area = _new_polygon_surface_area;
  189. _new_region_surface_area += _new_polygon_surface_area;
  190. for (int j(0); j < navigation_mesh_polygon_size; j++) {
  191. int idx = indices[j];
  192. if (idx < 0 || idx >= len) {
  193. valid = false;
  194. break;
  195. }
  196. Vector2 point_position = transform.xform(vertices_r[idx]);
  197. polygon.points[j].pos = point_position;
  198. polygon.points[j].key = NavMapBuilder2D::get_point_key(point_position, map->get_merge_rasterizer_cell_size());
  199. if (first_vertex) {
  200. first_vertex = false;
  201. _new_bounds.position = point_position;
  202. } else {
  203. _new_bounds.expand_to(point_position);
  204. }
  205. }
  206. if (!valid) {
  207. ERR_BREAK_MSG(!valid, "The navigation polygon set in this region is not valid!");
  208. }
  209. }
  210. surface_area = _new_region_surface_area;
  211. bounds = _new_bounds;
  212. }
  213. void NavRegion2D::get_iteration_update(NavRegionIteration2D &r_iteration) {
  214. r_iteration.navigation_layers = get_navigation_layers();
  215. r_iteration.enter_cost = get_enter_cost();
  216. r_iteration.travel_cost = get_travel_cost();
  217. r_iteration.owner_object_id = get_owner_id();
  218. r_iteration.owner_type = get_type();
  219. r_iteration.owner_rid = get_self();
  220. r_iteration.enabled = get_enabled();
  221. r_iteration.transform = get_transform();
  222. r_iteration.owner_use_edge_connections = get_use_edge_connections();
  223. r_iteration.bounds = get_bounds();
  224. r_iteration.surface_area = get_surface_area();
  225. r_iteration.navmesh_polygons.clear();
  226. r_iteration.navmesh_polygons.resize(navmesh_polygons.size());
  227. for (uint32_t i = 0; i < navmesh_polygons.size(); i++) {
  228. Polygon &navmesh_polygon = navmesh_polygons[i];
  229. navmesh_polygon.owner = &r_iteration;
  230. r_iteration.navmesh_polygons[i] = navmesh_polygon;
  231. }
  232. }
  233. void NavRegion2D::request_sync() {
  234. if (map && !sync_dirty_request_list_element.in_list()) {
  235. map->add_region_sync_dirty_request(&sync_dirty_request_list_element);
  236. }
  237. }
  238. void NavRegion2D::cancel_sync_request() {
  239. if (map && sync_dirty_request_list_element.in_list()) {
  240. map->remove_region_sync_dirty_request(&sync_dirty_request_list_element);
  241. }
  242. }
  243. NavRegion2D::NavRegion2D() :
  244. sync_dirty_request_list_element(this) {
  245. type = NavigationUtilities::PathSegmentType::PATH_SEGMENT_TYPE_REGION;
  246. }
  247. NavRegion2D::~NavRegion2D() {
  248. cancel_sync_request();
  249. }