nav_link_3d.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**************************************************************************/
  2. /* nav_link_3d.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_link_3d.h"
  31. #include "nav_map_3d.h"
  32. void NavLink3D::set_map(NavMap3D *p_map) {
  33. if (map == p_map) {
  34. return;
  35. }
  36. cancel_sync_request();
  37. if (map) {
  38. map->remove_link(this);
  39. }
  40. map = p_map;
  41. iteration_dirty = true;
  42. if (map) {
  43. map->add_link(this);
  44. request_sync();
  45. }
  46. }
  47. void NavLink3D::set_enabled(bool p_enabled) {
  48. if (enabled == p_enabled) {
  49. return;
  50. }
  51. enabled = p_enabled;
  52. iteration_dirty = true;
  53. request_sync();
  54. }
  55. void NavLink3D::set_bidirectional(bool p_bidirectional) {
  56. if (bidirectional == p_bidirectional) {
  57. return;
  58. }
  59. bidirectional = p_bidirectional;
  60. iteration_dirty = true;
  61. request_sync();
  62. }
  63. void NavLink3D::set_start_position(const Vector3 p_position) {
  64. if (start_position == p_position) {
  65. return;
  66. }
  67. start_position = p_position;
  68. iteration_dirty = true;
  69. request_sync();
  70. }
  71. void NavLink3D::set_end_position(const Vector3 p_position) {
  72. if (end_position == p_position) {
  73. return;
  74. }
  75. end_position = p_position;
  76. iteration_dirty = true;
  77. request_sync();
  78. }
  79. void NavLink3D::set_navigation_layers(uint32_t p_navigation_layers) {
  80. if (navigation_layers == p_navigation_layers) {
  81. return;
  82. }
  83. navigation_layers = p_navigation_layers;
  84. iteration_dirty = true;
  85. request_sync();
  86. }
  87. void NavLink3D::set_enter_cost(real_t p_enter_cost) {
  88. real_t new_enter_cost = MAX(p_enter_cost, 0.0);
  89. if (enter_cost == new_enter_cost) {
  90. return;
  91. }
  92. enter_cost = new_enter_cost;
  93. iteration_dirty = true;
  94. request_sync();
  95. }
  96. void NavLink3D::set_travel_cost(real_t p_travel_cost) {
  97. real_t new_travel_cost = MAX(p_travel_cost, 0.0);
  98. if (travel_cost == new_travel_cost) {
  99. return;
  100. }
  101. travel_cost = new_travel_cost;
  102. iteration_dirty = true;
  103. request_sync();
  104. }
  105. void NavLink3D::set_owner_id(ObjectID p_owner_id) {
  106. if (owner_id == p_owner_id) {
  107. return;
  108. }
  109. owner_id = p_owner_id;
  110. iteration_dirty = true;
  111. request_sync();
  112. }
  113. bool NavLink3D::sync() {
  114. bool requires_map_update = false;
  115. if (!map) {
  116. return requires_map_update;
  117. }
  118. if (iteration_dirty && !iteration_building && !iteration_ready) {
  119. _build_iteration();
  120. iteration_ready = false;
  121. requires_map_update = true;
  122. }
  123. return requires_map_update;
  124. }
  125. void NavLink3D::_build_iteration() {
  126. if (!iteration_dirty || iteration_building || iteration_ready) {
  127. return;
  128. }
  129. iteration_dirty = false;
  130. iteration_building = true;
  131. iteration_ready = false;
  132. Ref<NavLinkIteration3D> new_iteration;
  133. new_iteration.instantiate();
  134. new_iteration->navigation_layers = get_navigation_layers();
  135. new_iteration->enter_cost = get_enter_cost();
  136. new_iteration->travel_cost = get_travel_cost();
  137. new_iteration->owner_object_id = get_owner_id();
  138. new_iteration->owner_type = get_type();
  139. new_iteration->owner_rid = get_self();
  140. new_iteration->enabled = get_enabled();
  141. new_iteration->start_position = get_start_position();
  142. new_iteration->end_position = get_end_position();
  143. new_iteration->bidirectional = is_bidirectional();
  144. RWLockWrite write_lock(iteration_rwlock);
  145. ERR_FAIL_COND(iteration.is_null());
  146. iteration = Ref<NavLinkIteration3D>();
  147. DEV_ASSERT(iteration.is_null());
  148. iteration = new_iteration;
  149. iteration_id = iteration_id % UINT32_MAX + 1;
  150. iteration_building = false;
  151. iteration_ready = true;
  152. }
  153. void NavLink3D::request_sync() {
  154. if (map && !sync_dirty_request_list_element.in_list()) {
  155. map->add_link_sync_dirty_request(&sync_dirty_request_list_element);
  156. }
  157. }
  158. void NavLink3D::cancel_sync_request() {
  159. if (map && sync_dirty_request_list_element.in_list()) {
  160. map->remove_link_sync_dirty_request(&sync_dirty_request_list_element);
  161. }
  162. }
  163. Ref<NavLinkIteration3D> NavLink3D::get_iteration() {
  164. RWLockRead read_lock(iteration_rwlock);
  165. return iteration;
  166. }
  167. NavLink3D::NavLink3D() :
  168. sync_dirty_request_list_element(this) {
  169. type = NavigationUtilities::PathSegmentType::PATH_SEGMENT_TYPE_LINK;
  170. iteration.instantiate();
  171. }
  172. NavLink3D::~NavLink3D() {
  173. cancel_sync_request();
  174. iteration = Ref<NavLinkIteration3D>();
  175. }