| 1 |
// |
| 2 |
// Copyright (c) 2009-2010 Mikko Mononen [email protected] |
| 3 |
// |
| 4 |
// This software is provided 'as-is', without any express or implied |
| 5 |
// warranty. In no event will the authors be held liable for any damages |
| 6 |
// arising from the use of this software. |
| 7 |
// Permission is granted to anyone to use this software for any purpose, |
| 8 |
// including commercial applications, and to alter it and redistribute it |
| 9 |
// freely, subject to the following restrictions: |
| 10 |
// 1. The origin of this software must not be misrepresented; you must not |
| 11 |
// claim that you wrote the original software. If you use this software |
| 12 |
// in a product, an acknowledgment in the product documentation would be |
| 13 |
// appreciated but is not required. |
| 14 |
// 2. Altered source versions must be plainly marked as such, and must not be |
| 15 |
// misrepresented as being the original software. |
| 16 |
// 3. This notice may not be removed or altered from any source distribution. |
| 17 |
// |
| 18 |
|
| 19 |
#include <string.h> |
| 20 |
#include "DetourPathCorridor.h" |
| 21 |
#include "DetourNavMeshQuery.h" |
| 22 |
#include "DetourCommon.h" |
| 23 |
#include "DetourAssert.h" |
| 24 |
#include "DetourAlloc.h" |
| 25 |
|
| 26 |
|
| 27 |
int dtMergeCorridorStartMoved(dtPolyRef* path, const int npath, const int maxPath, |
| 28 |
const dtPolyRef* visited, const int nvisited) |
| 29 |
{ |
| 30 |
int furthestPath = -1; |
| 31 |
int furthestVisited = -1; |
| 32 |
|
| 33 |
// Find furthest common polygon. |
| 34 |
for (int i = npath-1; i >= 0; --i) |
| 35 |
{ |
| 36 |
bool found = false; |
| 37 |
for (int j = nvisited-1; j >= 0; --j) |
| 38 |
{ |
| 39 |
if (path[i] == visited[j]) |
| 40 |
{ |
| 41 |
furthestPath = i; |
| 42 |
furthestVisited = j; |
| 43 |
found = true; |
| 44 |
} |
| 45 |
} |
| 46 |
if (found) |
| 47 |
break; |
| 48 |
} |
| 49 |
|
| 50 |
// If no intersection found just return current path. |
| 51 |
if (furthestPath == -1 || furthestVisited == -1) |
| 52 |
return npath; |
| 53 |
|
| 54 |
// Concatenate paths. |
| 55 |
|
| 56 |
// Adjust beginning of the buffer to include the visited. |
| 57 |
const int req = nvisited - furthestVisited; |
| 58 |
const int orig = dtMin(furthestPath+1, npath); |
| 59 |
int size = dtMax(0, npath-orig); |
| 60 |
if (req+size > maxPath) |
| 61 |
size = maxPath-req; |
| 62 |
if (size) |
| 63 |
memmove(path+req, path+orig, size*sizeof(dtPolyRef)); |
| 64 |
|
| 65 |
// Store visited |
| 66 |
for (int i = 0; i < req; ++i) |
| 67 |
path[i] = visited[(nvisited-1)-i]; |
| 68 |
|
| 69 |
return req+size; |
| 70 |
} |
| 71 |
|
| 72 |
int dtMergeCorridorEndMoved(dtPolyRef* path, const int npath, const int maxPath, |
| 73 |
const dtPolyRef* visited, const int nvisited) |
| 74 |
{ |
| 75 |
int furthestPath = -1; |
| 76 |
int furthestVisited = -1; |
| 77 |
|
| 78 |
// Find furthest common polygon. |
| 79 |
for (int i = 0; i < npath; ++i) |
| 80 |
{ |
| 81 |
bool found = false; |
| 82 |
for (int j = nvisited-1; j >= 0; --j) |
| 83 |
{ |
| 84 |
if (path[i] == visited[j]) |
| 85 |
{ |
| 86 |
furthestPath = i; |
| 87 |
furthestVisited = j; |
| 88 |
found = true; |
| 89 |
} |
| 90 |
} |
| 91 |
if (found) |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
// If no intersection found just return current path. |
| 96 |
if (furthestPath == -1 || furthestVisited == -1) |
| 97 |
return npath; |
| 98 |
|
| 99 |
// Concatenate paths. |
| 100 |
const int ppos = furthestPath+1; |
| 101 |
const int vpos = furthestVisited+1; |
| 102 |
const int count = dtMin(nvisited-vpos, maxPath-ppos); |
| 103 |
dtAssert(ppos+count <= maxPath); |
| 104 |
if (count) |
| 105 |
memcpy(path+ppos, visited+vpos, sizeof(dtPolyRef)*count); |
| 106 |
|
| 107 |
return ppos+count; |
| 108 |
} |
| 109 |
|
| 110 |
int dtMergeCorridorStartShortcut(dtPolyRef* path, const int npath, const int maxPath, |
| 111 |
const dtPolyRef* visited, const int nvisited) |
| 112 |
{ |
| 113 |
int furthestPath = -1; |
| 114 |
int furthestVisited = -1; |
| 115 |
|
| 116 |
// Find furthest common polygon. |
| 117 |
for (int i = npath-1; i >= 0; --i) |
| 118 |
{ |
| 119 |
bool found = false; |
| 120 |
for (int j = nvisited-1; j >= 0; --j) |
| 121 |
{ |
| 122 |
if (path[i] == visited[j]) |
| 123 |
{ |
| 124 |
furthestPath = i; |
| 125 |
furthestVisited = j; |
| 126 |
found = true; |
| 127 |
} |
| 128 |
} |
| 129 |
if (found) |
| 130 |
break; |
| 131 |
} |
| 132 |
|
| 133 |
// If no intersection found just return current path. |
| 134 |
if (furthestPath == -1 || furthestVisited == -1) |
| 135 |
return npath; |
| 136 |
|
| 137 |
// Concatenate paths. |
| 138 |
|
| 139 |
// Adjust beginning of the buffer to include the visited. |
| 140 |
const int req = furthestVisited; |
| 141 |
if (req <= 0) |
| 142 |
return npath; |
| 143 |
|
| 144 |
const int orig = furthestPath; |
| 145 |
int size = dtMax(0, npath-orig); |
| 146 |
if (req+size > maxPath) |
| 147 |
size = maxPath-req; |
| 148 |
if (size) |
| 149 |
memmove(path+req, path+orig, size*sizeof(dtPolyRef)); |
| 150 |
|
| 151 |
// Store visited |
| 152 |
for (int i = 0; i < req; ++i) |
| 153 |
path[i] = visited[i]; |
| 154 |
|
| 155 |
return req+size; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
@class dtPathCorridor |
| 160 |
@par |
| 161 |
|
| 162 |
The corridor is loaded with a path, usually obtained from a #dtNavMeshQuery::findPath() query. The corridor |
| 163 |
is then used to plan local movement, with the corridor automatically updating as needed to deal with inaccurate |
| 164 |
agent locomotion. |
| 165 |
|
| 166 |
Example of a common use case: |
| 167 |
|
| 168 |
-# Construct the corridor object and call #init() to allocate its path buffer. |
| 169 |
-# Obtain a path from a #dtNavMeshQuery object. |
| 170 |
-# Use #reset() to set the agent's current position. (At the beginning of the path.) |
| 171 |
-# Use #setCorridor() to load the path and target. |
| 172 |
-# Use #findCorners() to plan movement. (This handles dynamic path straightening.) |
| 173 |
-# Use #movePosition() to feed agent movement back into the corridor. (The corridor will automatically adjust as needed.) |
| 174 |
-# If the target is moving, use #moveTargetPosition() to update the end of the corridor. |
| 175 |
(The corridor will automatically adjust as needed.) |
| 176 |
-# Repeat the previous 3 steps to continue to move the agent. |
| 177 |
|
| 178 |
The corridor position and target are always constrained to the navigation mesh. |
| 179 |
|
| 180 |
One of the difficulties in maintaining a path is that floating point errors, locomotion inaccuracies, and/or local |
| 181 |
steering can result in the agent crossing the boundary of the path corridor, temporarily invalidating the path. |
| 182 |
This class uses local mesh queries to detect and update the corridor as needed to handle these types of issues. |
| 183 |
|
| 184 |
The fact that local mesh queries are used to move the position and target locations results in two beahviors that |
| 185 |
need to be considered: |
| 186 |
|
| 187 |
Every time a move function is used there is a chance that the path will become non-optimial. Basically, the further |
| 188 |
the target is moved from its original location, and the further the position is moved outside the original corridor, |
| 189 |
the more likely the path will become non-optimal. This issue can be addressed by periodically running the |
| 190 |
#optimizePathTopology() and #optimizePathVisibility() methods. |
| 191 |
|
| 192 |
All local mesh queries have distance limitations. (Review the #dtNavMeshQuery methods for details.) So the most accurate |
| 193 |
use case is to move the position and target in small increments. If a large increment is used, then the corridor |
| 194 |
may not be able to accurately find the new location. Because of this limiation, if a position is moved in a large |
| 195 |
increment, then compare the desired and resulting polygon references. If the two do not match, then path replanning |
| 196 |
may be needed. E.g. If you move the target, check #getLastPoly() to see if it is the expected polygon. |
| 197 |
|
| 198 |
*/ |
| 199 |
|
| 200 |
dtPathCorridor::dtPathCorridor() : |
| 201 |
m_path(0), |
| 202 |
m_npath(0), |
| 203 |
m_maxPath(0) |
| 204 |
{ |
| 205 |
} |
| 206 |
|
| 207 |
dtPathCorridor::~dtPathCorridor() |
| 208 |
{ |
| 209 |
dtFree(m_path); |
| 210 |
} |
| 211 |
|
| 212 |
/// @par |
| 213 |
/// |
| 214 |
/// @warning Cannot be called more than once. |
| 215 |
bool dtPathCorridor::init(const int maxPath) |
| 216 |
{ |
| 217 |
dtAssert(!m_path); |
| 218 |
m_path = (dtPolyRef*)dtAlloc(sizeof(dtPolyRef)*maxPath, DT_ALLOC_PERM); |
| 219 |
if (!m_path) |
| 220 |
return false; |
| 221 |
m_npath = 0; |
| 222 |
m_maxPath = maxPath; |
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
/// @par |
| 227 |
/// |
| 228 |
/// Essentially, the corridor is set of one polygon in size with the target |
| 229 |
/// equal to the position. |
| 230 |
void dtPathCorridor::reset(dtPolyRef ref, const float* pos) |
| 231 |
{ |
| 232 |
dtAssert(m_path); |
| 233 |
dtVcopy(m_pos, pos); |
| 234 |
dtVcopy(m_target, pos); |
| 235 |
m_path[0] = ref; |
| 236 |
m_npath = 1; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
@par |
| 241 |
|
| 242 |
This is the function used to plan local movement within the corridor. One or more corners can be |
| 243 |
detected in order to plan movement. It performs essentially the same function as #dtNavMeshQuery::findStraightPath. |
| 244 |
|
| 245 |
Due to internal optimizations, the maximum number of corners returned will be (@p maxCorners - 1) |
| 246 |
For example: If the buffers are sized to hold 10 corners, the function will never return more than 9 corners. |
| 247 |
So if 10 corners are needed, the buffers should be sized for 11 corners. |
| 248 |
|
| 249 |
If the target is within range, it will be the last corner and have a polygon reference id of zero. |
| 250 |
*/ |
| 251 |
int dtPathCorridor::findCorners(float* cornerVerts, unsigned char* cornerFlags, |
| 252 |
dtPolyRef* cornerPolys, const int maxCorners, |
| 253 |
dtNavMeshQuery* navquery, const dtQueryFilter* /*filter*/) |
| 254 |
{ |
| 255 |
dtAssert(m_path); |
| 256 |
dtAssert(m_npath); |
| 257 |
|
| 258 |
static const float MIN_TARGET_DIST = 0.01f; |
| 259 |
|
| 260 |
int ncorners = 0; |
| 261 |
navquery->findStraightPath(m_pos, m_target, m_path, m_npath, |
| 262 |
cornerVerts, cornerFlags, cornerPolys, &ncorners, maxCorners); |
| 263 |
|
| 264 |
// Prune points in the beginning of the path which are too close. |
| 265 |
while (ncorners) |
| 266 |
{ |
| 267 |
if ((cornerFlags[0] & DT_STRAIGHTPATH_OFFMESH_CONNECTION) || |
| 268 |
dtVdist2DSqr(&cornerVerts[0], m_pos) > dtSqr(MIN_TARGET_DIST)) |
| 269 |
break; |
| 270 |
ncorners--; |
| 271 |
if (ncorners) |
| 272 |
{ |
| 273 |
memmove(cornerFlags, cornerFlags+1, sizeof(unsigned char)*ncorners); |
| 274 |
memmove(cornerPolys, cornerPolys+1, sizeof(dtPolyRef)*ncorners); |
| 275 |
memmove(cornerVerts, cornerVerts+3, sizeof(float)*3*ncorners); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
// Prune points after an off-mesh connection. |
| 280 |
for (int i = 0; i < ncorners; ++i) |
| 281 |
{ |
| 282 |
if (cornerFlags[i] & DT_STRAIGHTPATH_OFFMESH_CONNECTION) |
| 283 |
{ |
| 284 |
ncorners = i+1; |
| 285 |
break; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
return ncorners; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
@par |
| 294 |
|
| 295 |
Inaccurate locomotion or dynamic obstacle avoidance can force the argent position significantly outside the |
| 296 |
original corridor. Over time this can result in the formation of a non-optimal corridor. Non-optimal paths can |
| 297 |
also form near the corners of tiles. |
| 298 |
|
| 299 |
This function uses an efficient local visibility search to try to optimize the corridor |
| 300 |
between the current position and @p next. |
| 301 |
|
| 302 |
The corridor will change only if @p next is visible from the current position and moving directly toward the point |
| 303 |
is better than following the existing path. |
| 304 |
|
| 305 |
The more inaccurate the agent movement, the more beneficial this function becomes. Simply adjust the frequency |
| 306 |
of the call to match the needs to the agent. |
| 307 |
|
| 308 |
This function is not suitable for long distance searches. |
| 309 |
*/ |
| 310 |
void dtPathCorridor::optimizePathVisibility(const float* next, const float pathOptimizationRange, |
| 311 |
dtNavMeshQuery* navquery, const dtQueryFilter* filter) |
| 312 |
{ |
| 313 |
dtAssert(m_path); |
| 314 |
|
| 315 |
// Clamp the ray to max distance. |
| 316 |
float goal[3]; |
| 317 |
dtVcopy(goal, next); |
| 318 |
float dist = dtVdist2D(m_pos, goal); |
| 319 |
|
| 320 |
// If too close to the goal, do not try to optimize. |
| 321 |
if (dist < 0.01f) |
| 322 |
return; |
| 323 |
|
| 324 |
// Overshoot a little. This helps to optimize open fields in tiled meshes. |
| 325 |
dist = dtMin(dist+0.01f, pathOptimizationRange); |
| 326 |
|
| 327 |
// Adjust ray length. |
| 328 |
float delta[3]; |
| 329 |
dtVsub(delta, goal, m_pos); |
| 330 |
dtVmad(goal, m_pos, delta, pathOptimizationRange/dist); |
| 331 |
|
| 332 |
static const int MAX_RES = 32; |
| 333 |
dtPolyRef res[MAX_RES]; |
| 334 |
float t, norm[3]; |
| 335 |
int nres = 0; |
| 336 |
navquery->raycast(m_path[0], m_pos, goal, filter, &t, norm, res, &nres, MAX_RES); |
| 337 |
if (nres > 1 && t > 0.99f) |
| 338 |
{ |
| 339 |
m_npath = dtMergeCorridorStartShortcut(m_path, m_npath, m_maxPath, res, nres); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
@par |
| 345 |
|
| 346 |
Inaccurate locomotion or dynamic obstacle avoidance can force the agent position significantly outside the |
| 347 |
original corridor. Over time this can result in the formation of a non-optimal corridor. This function will use a |
| 348 |
local area path search to try to re-optimize the corridor. |
| 349 |
|
| 350 |
The more inaccurate the agent movement, the more beneficial this function becomes. Simply adjust the frequency of |
| 351 |
the call to match the needs to the agent. |
| 352 |
*/ |
| 353 |
bool dtPathCorridor::optimizePathTopology(dtNavMeshQuery* navquery, const dtQueryFilter* filter) |
| 354 |
{ |
| 355 |
dtAssert(navquery); |
| 356 |
dtAssert(filter); |
| 357 |
dtAssert(m_path); |
| 358 |
|
| 359 |
if (m_npath < 3) |
| 360 |
return false; |
| 361 |
|
| 362 |
static const int MAX_ITER = 32; |
| 363 |
static const int MAX_RES = 32; |
| 364 |
|
| 365 |
dtPolyRef res[MAX_RES]; |
| 366 |
int nres = 0; |
| 367 |
navquery->initSlicedFindPath(m_path[0], m_path[m_npath-1], m_pos, m_target, filter); |
| 368 |
navquery->updateSlicedFindPath(MAX_ITER, 0); |
| 369 |
dtStatus status = navquery->finalizeSlicedFindPathPartial(m_path, m_npath, res, &nres, MAX_RES); |
| 370 |
|
| 371 |
if (dtStatusSucceed(status) && nres > 0) |
| 372 |
{ |
| 373 |
m_npath = dtMergeCorridorStartShortcut(m_path, m_npath, m_maxPath, res, nres); |
| 374 |
return true; |
| 375 |
} |
| 376 |
|
| 377 |
return false; |
| 378 |
} |
| 379 |
|
| 380 |
bool dtPathCorridor::moveOverOffmeshConnection(dtPolyRef offMeshConRef, dtPolyRef* refs, |
| 381 |
float* startPos, float* endPos, |
| 382 |
dtNavMeshQuery* navquery) |
| 383 |
{ |
| 384 |
dtAssert(navquery); |
| 385 |
dtAssert(m_path); |
| 386 |
dtAssert(m_npath); |
| 387 |
|
| 388 |
// Advance the path up to and over the off-mesh connection. |
| 389 |
dtPolyRef prevRef = 0, polyRef = m_path[0]; |
| 390 |
int npos = 0; |
| 391 |
while (npos < m_npath && polyRef != offMeshConRef) |
| 392 |
{ |
| 393 |
prevRef = polyRef; |
| 394 |
polyRef = m_path[npos]; |
| 395 |
npos++; |
| 396 |
} |
| 397 |
if (npos == m_npath) |
| 398 |
{ |
| 399 |
// Could not find offMeshConRef |
| 400 |
return false; |
| 401 |
} |
| 402 |
|
| 403 |
// Prune path |
| 404 |
for (int i = npos; i < m_npath; ++i) |
| 405 |
m_path[i-npos] = m_path[i]; |
| 406 |
m_npath -= npos; |
| 407 |
|
| 408 |
refs[0] = prevRef; |
| 409 |
refs[1] = polyRef; |
| 410 |
|
| 411 |
const dtNavMesh* nav = navquery->getAttachedNavMesh(); |
| 412 |
dtAssert(nav); |
| 413 |
|
| 414 |
dtStatus status = nav->getOffMeshConnectionPolyEndPoints(refs[0], refs[1], startPos, endPos); |
| 415 |
if (dtStatusSucceed(status)) |
| 416 |
{ |
| 417 |
dtVcopy(m_pos, endPos); |
| 418 |
return true; |
| 419 |
} |
| 420 |
|
| 421 |
return false; |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
@par |
| 426 |
|
| 427 |
Behavior: |
| 428 |
|
| 429 |
- The movement is constrained to the surface of the navigation mesh. |
| 430 |
- The corridor is automatically adjusted (shorted or lengthened) in order to remain valid. |
| 431 |
- The new position will be located in the adjusted corridor's first polygon. |
| 432 |
|
| 433 |
The expected use case is that the desired position will be 'near' the current corridor. What is considered 'near' |
| 434 |
depends on local polygon density, query search half extents, etc. |
| 435 |
|
| 436 |
The resulting position will differ from the desired position if the desired position is not on the navigation mesh, |
| 437 |
or it can't be reached using a local search. |
| 438 |
*/ |
| 439 |
bool dtPathCorridor::movePosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter) |
| 440 |
{ |
| 441 |
dtAssert(m_path); |
| 442 |
dtAssert(m_npath); |
| 443 |
|
| 444 |
// Move along navmesh and update new position. |
| 445 |
float result[3]; |
| 446 |
static const int MAX_VISITED = 16; |
| 447 |
dtPolyRef visited[MAX_VISITED]; |
| 448 |
int nvisited = 0; |
| 449 |
dtStatus status = navquery->moveAlongSurface(m_path[0], m_pos, npos, filter, |
| 450 |
result, visited, &nvisited, MAX_VISITED); |
| 451 |
if (dtStatusSucceed(status)) { |
| 452 |
m_npath = dtMergeCorridorStartMoved(m_path, m_npath, m_maxPath, visited, nvisited); |
| 453 |
|
| 454 |
// Adjust the position to stay on top of the navmesh. |
| 455 |
float h = m_pos[1]; |
| 456 |
navquery->getPolyHeight(m_path[0], result, &h); |
| 457 |
result[1] = h; |
| 458 |
dtVcopy(m_pos, result); |
| 459 |
return true; |
| 460 |
} |
| 461 |
return false; |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
@par |
| 466 |
|
| 467 |
Behavior: |
| 468 |
|
| 469 |
- The movement is constrained to the surface of the navigation mesh. |
| 470 |
- The corridor is automatically adjusted (shorted or lengthened) in order to remain valid. |
| 471 |
- The new target will be located in the adjusted corridor's last polygon. |
| 472 |
|
| 473 |
The expected use case is that the desired target will be 'near' the current corridor. What is considered 'near' depends on local polygon density, query search half extents, etc. |
| 474 |
|
| 475 |
The resulting target will differ from the desired target if the desired target is not on the navigation mesh, or it can't be reached using a local search. |
| 476 |
*/ |
| 477 |
bool dtPathCorridor::moveTargetPosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter) |
| 478 |
{ |
| 479 |
dtAssert(m_path); |
| 480 |
dtAssert(m_npath); |
| 481 |
|
| 482 |
// Move along navmesh and update new position. |
| 483 |
float result[3]; |
| 484 |
static const int MAX_VISITED = 16; |
| 485 |
dtPolyRef visited[MAX_VISITED]; |
| 486 |
int nvisited = 0; |
| 487 |
dtStatus status = navquery->moveAlongSurface(m_path[m_npath-1], m_target, npos, filter, |
| 488 |
result, visited, &nvisited, MAX_VISITED); |
| 489 |
if (dtStatusSucceed(status)) |
| 490 |
{ |
| 491 |
m_npath = dtMergeCorridorEndMoved(m_path, m_npath, m_maxPath, visited, nvisited); |
| 492 |
// TODO: should we do that? |
| 493 |
// Adjust the position to stay on top of the navmesh. |
| 494 |
/* float h = m_target[1]; |
| 495 |
navquery->getPolyHeight(m_path[m_npath-1], result, &h); |
| 496 |
result[1] = h;*/ |
| 497 |
|
| 498 |
dtVcopy(m_target, result); |
| 499 |
|
| 500 |
return true; |
| 501 |
} |
| 502 |
return false; |
| 503 |
} |
| 504 |
|
| 505 |
/// @par |
| 506 |
/// |
| 507 |
/// The current corridor position is expected to be within the first polygon in the path. The target |
| 508 |
/// is expected to be in the last polygon. |
| 509 |
/// |
| 510 |
/// @warning The size of the path must not exceed the size of corridor's path buffer set during #init(). |
| 511 |
void dtPathCorridor::setCorridor(const float* target, const dtPolyRef* path, const int npath) |
| 512 |
{ |
| 513 |
dtAssert(m_path); |
| 514 |
dtAssert(npath > 0); |
| 515 |
dtAssert(npath <= m_maxPath); |
| 516 |
|
| 517 |
dtVcopy(m_target, target); |
| 518 |
memcpy(m_path, path, sizeof(dtPolyRef)*npath); |
| 519 |
m_npath = npath; |
| 520 |
} |
| 521 |
|
| 522 |
bool dtPathCorridor::fixPathStart(dtPolyRef safeRef, const float* safePos) |
| 523 |
{ |
| 524 |
dtAssert(m_path); |
| 525 |
|
| 526 |
dtVcopy(m_pos, safePos); |
| 527 |
if (m_npath < 3 && m_npath > 0) |
| 528 |
{ |
| 529 |
m_path[2] = m_path[m_npath-1]; |
| 530 |
m_path[0] = safeRef; |
| 531 |
m_path[1] = 0; |
| 532 |
m_npath = 3; |
| 533 |
} |
| 534 |
else |
| 535 |
{ |
| 536 |
m_path[0] = safeRef; |
| 537 |
m_path[1] = 0; |
| 538 |
} |
| 539 |
|
| 540 |
return true; |
| 541 |
} |
| 542 |
|
| 543 |
bool dtPathCorridor::trimInvalidPath(dtPolyRef safeRef, const float* safePos, |
| 544 |
dtNavMeshQuery* navquery, const dtQueryFilter* filter) |
| 545 |
{ |
| 546 |
dtAssert(navquery); |
| 547 |
dtAssert(filter); |
| 548 |
dtAssert(m_path); |
| 549 |
|
| 550 |
// Keep valid path as far as possible. |
| 551 |
int n = 0; |
| 552 |
while (n < m_npath && navquery->isValidPolyRef(m_path[n], filter)) { |
| 553 |
n++; |
| 554 |
} |
| 555 |
|
| 556 |
if (n == m_npath) |
| 557 |
{ |
| 558 |
// All valid, no need to fix. |
| 559 |
return true; |
| 560 |
} |
| 561 |
else if (n == 0) |
| 562 |
{ |
| 563 |
// The first polyref is bad, use current safe values. |
| 564 |
dtVcopy(m_pos, safePos); |
| 565 |
m_path[0] = safeRef; |
| 566 |
m_npath = 1; |
| 567 |
} |
| 568 |
else |
| 569 |
{ |
| 570 |
// The path is partially usable. |
| 571 |
m_npath = n; |
| 572 |
} |
| 573 |
|
| 574 |
// Clamp target pos to last poly |
| 575 |
float tgt[3]; |
| 576 |
dtVcopy(tgt, m_target); |
| 577 |
navquery->closestPointOnPolyBoundary(m_path[m_npath-1], tgt, m_target); |
| 578 |
|
| 579 |
return true; |
| 580 |
} |
| 581 |
|
| 582 |
/// @par |
| 583 |
/// |
| 584 |
/// The path can be invalidated if there are structural changes to the underlying navigation mesh, or the state of |
| 585 |
/// a polygon within the path changes resulting in it being filtered out. (E.g. An exclusion or inclusion flag changes.) |
| 586 |
bool dtPathCorridor::isValid(const int maxLookAhead, dtNavMeshQuery* navquery, const dtQueryFilter* filter) |
| 587 |
{ |
| 588 |
// Check that all polygons still pass query filter. |
| 589 |
const int n = dtMin(m_npath, maxLookAhead); |
| 590 |
for (int i = 0; i < n; ++i) |
| 591 |
{ |
| 592 |
if (!navquery->isValidPolyRef(m_path[i], filter)) |
| 593 |
return false; |
| 594 |
} |
| 595 |
|
| 596 |
return true; |
| 597 |
} |