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