Validate constraint: #polygons per tile (#449)

* Validate constraint: #polygons per tile

When adding a tile to a NavMesh, ensure that the number of polygons in that tile fit in the poly ID address space.

* Improve dtNavMeshParams field documentation
This commit is contained in:
Jan Haller 2020-11-19 10:13:30 +01:00 committed by GitHub
parent 6624e7aef5
commit 9dc88fcab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View File

@ -329,8 +329,8 @@ struct dtNavMeshParams
float orig[3]; ///< The world space origin of the navigation mesh's tile space. [(x, y, z)]
float tileWidth; ///< The width of each tile. (Along the x-axis.)
float tileHeight; ///< The height of each tile. (Along the z-axis.)
int maxTiles; ///< The maximum number of tiles the navigation mesh can contain.
int maxPolys; ///< The maximum number of polygons each tile can contain.
int maxTiles; ///< The maximum number of tiles the navigation mesh can contain. This and maxPolys are used to calculate how many bits are needed to identify tiles and polygons uniquely.
int maxPolys; ///< The maximum number of polygons each tile can contain. This and maxTiles are used to calculate how many bits are needed to identify tiles and polygons uniquely.
};
/// A navigation mesh based on tiles of convex polygons.

View File

@ -914,6 +914,13 @@ dtStatus dtNavMesh::addTile(unsigned char* data, int dataSize, int flags,
return DT_FAILURE | DT_WRONG_MAGIC;
if (header->version != DT_NAVMESH_VERSION)
return DT_FAILURE | DT_WRONG_VERSION;
#ifndef DT_POLYREF64
// Do not allow adding more polygons than specified in the NavMesh's maxPolys constraint.
// Otherwise, the poly ID cannot be represented with the given number of bits.
if (m_polyBits < dtIlog2(dtNextPow2((unsigned int)header->polyCount)))
return DT_FAILURE | DT_INVALID_PARAM;
#endif
// Make sure the location is free.
if (getTileAt(header->x, header->y, header->layer))