Skip to content

Commit

Permalink
Update MapBuilder.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamemechanicwow committed Sep 3, 2023
1 parent 8a70352 commit 28035e4
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions contrib/mmap/src/MapBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,33 +120,31 @@ bool IsModelArea(int area)
}

void filterLedgeSpans(const int walkableHeight, const int walkableClimbTransition, const int walkableClimbTerrain,
rcHeightfield& solid)
rcHeightfield& heightfield)
{
const int w = solid.width;
const int h = solid.height;
const int w = heightfield.width;
const int h = heightfield.height;
const int MAX_HEIGHT = 0xffff;
std::list<rcSpan*> nullSpans;
std::list<rcSpan*> steepSlopes;

for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
for (rcSpan* s = solid.spans[x + y*w]; s; s = s->next)
for (rcSpan* span = heightfield.spans[x + y*w]; span; span = span->next)
{
// Skip non walkable spans.
if (s->area == RC_NULL_AREA)
if (span->area == RC_NULL_AREA)
continue;

const int bot = (int)(s->smax);
const int top = s->next ? (int)(s->next->smin) : MAX_HEIGHT;
const int bot = (int)(span->smax);
const int top = span->next ? (int)(span->next->smin) : MAX_HEIGHT;

// Find neighbours minimum height.
int minh = MAX_HEIGHT;
int minNeighborHeight = MAX_HEIGHT;

// Min and max height of accessible neighbours.
int asmin = s->smax;
int asmax = s->smax;
int accessibleNeighborMinHeight = span->smax;
int accessibleNeighborMaxHeight = span->smax;
bool hasAllNbTerrain = true;
bool hasAllNbModel = true;

Expand All @@ -157,35 +155,36 @@ void filterLedgeSpans(const int walkableHeight, const int walkableClimbTransitio
// Skip neighbours which are out of bounds.
if (dx < 0 || dy < 0 || dx >= w || dy >= h)
{
//minh = rcMin(minh, -walkableClimbTerrain - bot);
//TODO: Figure out why this is commented out.
//minNeighborHeight = rcMin(minNeighborHeight , -walkableClimbTerrain - bot);
continue;
}

// From minus infinity to the first span.
rcSpan* ns = solid.spans[dx + dy*w];
rcSpan* neighborSpan = heightfield.spans[dx + dy*w];
int nbot = -walkableClimbTerrain;
int ntop = ns ? (int)ns->smin : MAX_HEIGHT;
int ntop = neighborSpan ? (int)neighborSpan->smin : MAX_HEIGHT;
// Skip neighbour if the gap between the spans is too small.
if (rcMin(top,ntop) - rcMax(bot,nbot) > walkableHeight)
minh = rcMin(minh, nbot - bot);
minNeighborHeight = rcMin(minNeighborHeight, nbot - bot);

// Rest of the spans.
for (ns = solid.spans[dx + dy*w]; ns; ns = ns->next)
for (neighborSpan = heightfield.spans[dx + dy*w]; neighborSpan; neighborSpan = neighborSpan->next)
{
if (ns->area == RC_NULL_AREA)
if (neighborSpan->area == RC_NULL_AREA)
continue;
nbot = (int)ns->smax;
ntop = ns->next ? (int)ns->next->smin : MAX_HEIGHT;
nbot = (int)neighborSpan->smax;
ntop = neighborSpan->next ? (int)neighborSpan->next->smin : MAX_HEIGHT;
// Skip neightbour if the gap between the spans is too small.
if (rcMin(top,ntop) - rcMax(bot,nbot) > walkableHeight)
{
minh = rcMin(minh, nbot - bot);
minNeighborHeight = rcMin(minNeighborHeight, nbot - bot);
// Find min/max accessible neighbour height.
if (rcAbs(nbot - bot) <= walkableClimbTerrain)
{
if (nbot < asmin) asmin = nbot;
if (nbot > asmax) asmax = nbot;
if (!IsModelArea(ns->area))
if (nbot < accessibleNeighborMinHeight) accessibleNeighborMinHeight = nbot;
if (nbot > accessibleNeighborMaxHeight) accessibleNeighborMaxHeight = nbot;
if (!IsModelArea(neighborSpan->area))
hasAllNbModel = false;
else
hasAllNbTerrain = false;
Expand All @@ -196,31 +195,27 @@ void filterLedgeSpans(const int walkableHeight, const int walkableClimbTransitio

// The current span is close to a ledge if the drop to any
// neighbour span is less than the walkableClimb.
bool modelToTerrainTransition = (IsModelArea(s->area) && !hasAllNbModel) || (!IsModelArea(s->area) && !hasAllNbTerrain);
bool modelToTerrainTransition = (IsModelArea(span->area) && !hasAllNbModel) || (!IsModelArea(span->area) && !hasAllNbTerrain);
int currentMaxClimb = walkableClimbTerrain;
// Model -> Terrain or Terrain -> Model
if (modelToTerrainTransition)
currentMaxClimb = walkableClimbTransition;
if (minh < -currentMaxClimb)
nullSpans.push_front(s);
if (minNeighborHeight < -currentMaxClimb)
span->area = RC_NULL_AREA;


// If the difference between all neighbours is too large,
// we are at steep slope, mark the span as it
else if ((asmax - asmin) > currentMaxClimb)
else if ((accessibleNeighborMaxHeight - accessibleNeighborMinHeight) > currentMaxClimb)
{
if (modelToTerrainTransition)
nullSpans.push_front(s);
span->area = RC_NULL_AREA;
else
steepSlopes.push_front(s);
span->area = AREA_STEEP_SLOPE;
}
}
}
}
for (std::list<rcSpan*>::iterator it = nullSpans.begin(); it != nullSpans.end(); ++it)
(*it)->area = RC_NULL_AREA;
for (std::list<rcSpan*>::iterator it = steepSlopes.begin(); it != steepSlopes.end(); ++it)
(*it)->area = AREA_STEEP_SLOPE;
}

void from_json(const json& j, rcConfig& config)
Expand Down Expand Up @@ -373,8 +368,7 @@ namespace MMAP

float bmin[3] = { 0, 0, 0 };
float bmax[3] = { 0, 0, 0 };
float lmin[3] = { 0, 0, 0 };
float lmax[3] = { 0, 0, 0 };

MeshData meshData;

// make sure we process maps which don't have tiles
Expand All @@ -389,6 +383,9 @@ namespace MMAP
// get the coord bounds of the model data
if (meshData.solidVerts.size() && meshData.liquidVerts.size())
{
float lmin[3] = { 0, 0, 0 };
float lmax[3] = { 0, 0, 0 };

rcCalcBounds(meshData.solidVerts.getCArray(), meshData.solidVerts.size() / 3, bmin, bmax);
rcCalcBounds(meshData.liquidVerts.getCArray(), meshData.liquidVerts.size() / 3, lmin, lmax);
rcVmin(bmin, lmin);
Expand All @@ -397,7 +394,7 @@ namespace MMAP
else if (meshData.solidVerts.size())
rcCalcBounds(meshData.solidVerts.getCArray(), meshData.solidVerts.size() / 3, bmin, bmax);
else
rcCalcBounds(meshData.liquidVerts.getCArray(), meshData.liquidVerts.size() / 3, lmin, lmax);
rcCalcBounds(meshData.liquidVerts.getCArray(), meshData.liquidVerts.size() / 3, bmin, bmax);

// convert coord bounds to grid bounds
maxX = 32 - bmin[0] / GRID_SIZE;
Expand Down Expand Up @@ -711,7 +708,7 @@ namespace MMAP
// - We are on a model (WMO...)
// - Also we want to remove under-terrain triangles
unsigned char* areas = new unsigned char[tTriCount];
memset(areas, 0, tTriCount * sizeof(unsigned char));
memset(areas, AREA_NONE, tTriCount * sizeof(unsigned char));
float norm[3];
const float playerClimbLimit = cosf(52.0f/180.0f*RC_PI);
const float maxClimbLimitTerrain = cosf(75.0f/180.0f*RC_PI);
Expand Down Expand Up @@ -756,7 +753,7 @@ namespace MMAP

if (m_terrainBuilder->IsUnderMap(&verts[0]) && m_terrainBuilder->IsUnderMap(&verts[3]) && m_terrainBuilder->IsUnderMap(&verts[6]))
{
areas[i] = 0;
areas[i] = AREA_NONE;
continue;
}
}
Expand Down Expand Up @@ -1275,7 +1272,7 @@ namespace MMAP
return;
}
unsigned char* m_triareas = new unsigned char[tTriCount];
memset(m_triareas, 0, tTriCount*sizeof(unsigned char));
memset(m_triareas, AREA_NONE, tTriCount*sizeof(unsigned char));
rcMarkWalkableTriangles(m_rcContext, config.walkableSlopeAngle, tVerts, tVertCount, tTris, tTriCount, m_triareas);
rcRasterizeTriangles(m_rcContext, tVerts, tVertCount, tTris, m_triareas, tTriCount, *tile.solid, config.walkableClimb);
rcFilterLowHangingWalkableObstacles(m_rcContext, config.walkableClimb, *tile.solid);
Expand Down

0 comments on commit 28035e4

Please sign in to comment.