Skip to content

Commit

Permalink
Don't discard changes that occur after hitting MAX_UPDATE
Browse files Browse the repository at this point in the history
  • Loading branch information
mastahg committed Feb 1, 2018
1 parent 5d41860 commit 25e5b35
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DetourTileCache/Include/DetourTileCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class dtTileCache
int m_nreqs;

static const int MAX_UPDATE = 64;
dtCompressedTileRef m_update[MAX_UPDATE];
dtCompressedTileRef m_update[MAX_UPDATE + DT_MAX_TOUCHED_TILES];//Allocate enough memory to always complete one obstacle
int m_nupdate;
};

Expand Down
69 changes: 46 additions & 23 deletions DetourTileCache/Source/DetourTileCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,23 +516,26 @@ dtStatus dtTileCache::queryTiles(const float* bmin, const float* bmax,
}

dtStatus dtTileCache::update(const float /*dt*/, dtNavMesh* navmesh,
bool* upToDate)
bool* upToDate)
{
if (m_nupdate == 0)
{
bool abortedEarly = false;
int lastSpot = 0;

// Process requests.
for (int i = 0; i < m_nreqs; ++i)
{
ObstacleRequest* req = &m_reqs[i];

unsigned int idx = decodeObstacleIdObstacle(req->ref);
if ((int)idx >= m_params.maxObstacles)
continue;
dtTileCacheObstacle* ob = &m_obstacles[idx];
unsigned int salt = decodeObstacleIdSalt(req->ref);
if (ob->salt != salt)
continue;

if (req->action == REQUEST_ADD)
{
// Find touched tiles.
Expand All @@ -546,12 +549,13 @@ dtStatus dtTileCache::update(const float /*dt*/, dtNavMesh* navmesh,
ob->npending = 0;
for (int j = 0; j < ob->ntouched; ++j)
{
if (m_nupdate < MAX_UPDATE)
{
if (!contains(m_update, m_nupdate, ob->touched[j]))
m_update[m_nupdate++] = ob->touched[j];
ob->pending[ob->npending++] = ob->touched[j];
}
if (m_nupdate == MAX_UPDATE && i != m_nreqs - 1)
abortedEarly = true;

if (!contains(m_update, m_nupdate, ob->touched[j]))
m_update[m_nupdate++] = ob->touched[j];
ob->pending[ob->npending++] = ob->touched[j];

}
}
else if (req->action == REQUEST_REMOVE)
Expand All @@ -562,19 +566,38 @@ dtStatus dtTileCache::update(const float /*dt*/, dtNavMesh* navmesh,
ob->npending = 0;
for (int j = 0; j < ob->ntouched; ++j)
{
if (m_nupdate < MAX_UPDATE)
{
if (!contains(m_update, m_nupdate, ob->touched[j]))
m_update[m_nupdate++] = ob->touched[j];
ob->pending[ob->npending++] = ob->touched[j];
}
if (m_nupdate == MAX_UPDATE && i != m_nreqs - 1)
abortedEarly = true;

if (!contains(m_update, m_nupdate, ob->touched[j]))
m_update[m_nupdate++] = ob->touched[j];
ob->pending[ob->npending++] = ob->touched[j];

}
}

if (abortedEarly)
{
lastSpot = ++i;
break;
}

}

if (abortedEarly)
{
//Move remaining obstacles to the front of the line
memmove(m_reqs, m_reqs + lastSpot,( m_nreqs - lastSpot) * sizeof(ObstacleRequest));
m_nreqs = m_nreqs - lastSpot;
}
else
{
m_nreqs = 0;
}
m_nreqs = 0;


}

dtStatus status = DT_SUCCESS;
// Process updates
if (m_nupdate)
Expand All @@ -584,7 +607,7 @@ dtStatus dtTileCache::update(const float /*dt*/, dtNavMesh* navmesh,
status = buildNavMeshTile(ref, navmesh);
m_nupdate--;
if (m_nupdate > 0)
memmove(m_update, m_update+1, m_nupdate*sizeof(dtCompressedTileRef));
memmove(m_update, m_update + 1, m_nupdate * sizeof(dtCompressedTileRef));

// Update obstacle states.
for (int i = 0; i < m_params.maxObstacles; ++i)
Expand All @@ -597,12 +620,12 @@ dtStatus dtTileCache::update(const float /*dt*/, dtNavMesh* navmesh,
{
if (ob->pending[j] == ref)
{
ob->pending[j] = ob->pending[(int)ob->npending-1];
ob->pending[j] = ob->pending[(int)ob->npending - 1];
ob->npending--;
break;
}
}

// If all pending tiles processed, change state.
if (ob->npending == 0)
{
Expand All @@ -614,7 +637,7 @@ dtStatus dtTileCache::update(const float /*dt*/, dtNavMesh* navmesh,
{
ob->state = DT_OBSTACLE_EMPTY;
// Update salt, salt should never be zero.
ob->salt = (ob->salt+1) & ((1<<16)-1);
ob->salt = (ob->salt + 1) & ((1 << 16) - 1);
if (ob->salt == 0)
ob->salt++;
// Return obstacle to free list.
Expand All @@ -625,7 +648,7 @@ dtStatus dtTileCache::update(const float /*dt*/, dtNavMesh* navmesh,
}
}
}

if (upToDate)
*upToDate = m_nupdate == 0 && m_nreqs == 0;

Expand Down

0 comments on commit 25e5b35

Please sign in to comment.