text
stringlengths
0
2.2M
return true;
}
static bool canMerge(unsigned char oldRegId, unsigned char newRegId, const dtLayerMonotoneRegion* regs, const int nregs)
{
int count = 0;
for (int i = 0; i < nregs; ++i)
{
const dtLayerMonotoneRegion& reg = regs[i];
if (reg.regId != oldRegId) continue;
const int nnei = (int)reg.nneis;
for (int j = 0; j < nnei; ++j)
{
if (regs[reg.neis[j]].regId == newRegId)
count++;
}
}
return count == 1;
}
dtStatus dtBuildTileCacheRegions(dtTileCacheAlloc* alloc,
dtTileCacheLayer& layer,
const int walkableClimb)
{
dtAssert(alloc);
const int w = (int)layer.header->width;
const int h = (int)layer.header->height;
memset(layer.regs,0xff,sizeof(unsigned char)*w*h);
const int nsweeps = w;
dtFixedArray<dtLayerSweepSpan> sweeps(alloc, nsweeps);
if (!sweeps)
return DT_FAILURE | DT_OUT_OF_MEMORY;
memset(sweeps,0,sizeof(dtLayerSweepSpan)*nsweeps);
// Partition walkable area into monotone regions.
unsigned char prevCount[256];
unsigned char regId = 0;
for (int y = 0; y < h; ++y)
{
if (regId > 0)
memset(prevCount,0,sizeof(unsigned char)*regId);
unsigned char sweepId = 0;
for (int x = 0; x < w; ++x)
{
const int idx = x + y*w;
if (layer.areas[idx] == DT_TILECACHE_NULL_AREA) continue;
unsigned char sid = 0xff;
// -x
const int xidx = (x-1)+y*w;
if (x > 0 && isConnected(layer, idx, xidx, walkableClimb))
{
if (layer.regs[xidx] != 0xff)
sid = layer.regs[xidx];
}
if (sid == 0xff)
{
sid = sweepId++;
sweeps[sid].nei = 0xff;
sweeps[sid].ns = 0;
}
// -y
const int yidx = x+(y-1)*w;
if (y > 0 && isConnected(layer, idx, yidx, walkableClimb))
{
const unsigned char nr = layer.regs[yidx];
if (nr != 0xff)
{
// Set neighbour when first valid neighbour is encoutered.
if (sweeps[sid].ns == 0)
sweeps[sid].nei = nr;
if (sweeps[sid].nei == nr)
{
// Update existing neighbour
sweeps[sid].ns++;
prevCount[nr]++;
}
else
{
// This is hit if there is nore than one neighbour.
// Invalidate the neighbour.
sweeps[sid].nei = 0xff;
}
}
}
layer.regs[idx] = sid;
}
// Create unique ID.