text
stringlengths
0
2.2M
const int maxTempVerts = (w+h)*2 * 2; // Twice around the layer.
dtFixedArray<unsigned char> tempVerts(alloc, maxTempVerts*4);
if (!tempVerts)
return DT_FAILURE | DT_OUT_OF_MEMORY;
dtFixedArray<unsigned short> tempPoly(alloc, maxTempVerts);
if (!tempPoly)
return DT_FAILURE | DT_OUT_OF_MEMORY;
dtTempContour temp(tempVerts, maxTempVerts, tempPoly, maxTempVerts);
// Find contours.
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
const int idx = x+y*w;
const unsigned char ri = layer.regs[idx];
if (ri == 0xff)
continue;
dtTileCacheContour& cont = lcset.conts[ri];
if (cont.nverts > 0)
continue;
cont.reg = ri;
cont.area = layer.areas[idx];
if (!walkContour(layer, x, y, temp))
{
// Too complex contour.
// Note: If you hit here ofte, try increasing 'maxTempVerts'.
return DT_FAILURE | DT_BUFFER_TOO_SMALL;
}
simplifyContour(temp, maxError);
// Store contour.
cont.nverts = temp.nverts;
if (cont.nverts > 0)
{
cont.verts = (unsigned char*)alloc->alloc(sizeof(unsigned char)*4*temp.nverts);
if (!cont.verts)
return DT_FAILURE | DT_OUT_OF_MEMORY;
for (int i = 0, j = temp.nverts-1; i < temp.nverts; j=i++)
{
unsigned char* dst = &cont.verts[j*4];
unsigned char* v = &temp.verts[j*4];
unsigned char* vn = &temp.verts[i*4];
unsigned char nei = vn[3]; // The neighbour reg is stored at segment vertex of a segment.
bool shouldRemove = false;
unsigned char lh = getCornerHeight(layer, (int)v[0], (int)v[1], (int)v[2],
walkableClimb, shouldRemove);
dst[0] = v[0];
dst[1] = lh;
dst[2] = v[2];
// Store portal direction and remove status to the fourth component.
dst[3] = 0x0f;
if (nei != 0xff && nei >= 0xf8)
dst[3] = nei - 0xf8;
if (shouldRemove)
dst[3] |= 0x80;
}
}
}
}
return DT_SUCCESS;
}
static const int VERTEX_BUCKET_COUNT2 = (1<<8);
inline int computeVertexHash2(int x, int y, int z)
{
const unsigned int h1 = 0x8da6b343; // Large multiplicative constants;
const unsigned int h2 = 0xd8163841; // here arbitrarily chosen primes
const unsigned int h3 = 0xcb1ab31f;
unsigned int n = h1 * x + h2 * y + h3 * z;
return (int)(n & (VERTEX_BUCKET_COUNT2-1));
}
static unsigned short addVertex(unsigned short x, unsigned short y, unsigned short z,
unsigned short* verts, unsigned short* firstVert, unsigned short* nextVert, int& nv)
{
int bucket = computeVertexHash2(x, 0, z);
unsigned short i = firstVert[bucket];
while (i != DT_TILECACHE_NULL_IDX)
{
const unsigned short* v = &verts[i*3];
if (v[0] == x && v[2] == z && (dtAbs(v[1] - y) <= 2))
return i;
i = nextVert[i]; // next