From 723994652fcd7fbaf44c8631a151938e95ddf657 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 11 Sep 2022 11:25:26 +0300 Subject: [PATCH] corrected impassible node calculation --- map.cpp | 2 +- unit.cpp | 31 +++++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/map.cpp b/map.cpp index 48d1b00..a5bcc6a 100755 --- a/map.cpp +++ b/map.cpp @@ -169,6 +169,6 @@ namespace battleship{ bool Map::isPointWithin(int id, Vector3 point, Vector3 cellSize){ Vector3 cellPos = getCellPos(id, cellSize); - return ((fabs(point.x - cellPos.x) < 0.5 * cellSize.x) && (point.y > 0) && (fabs(point.z - cellPos.z) < 0.5 * cellSize.z)); + return ((fabs(point.x - cellPos.x) < 0.5 * cellSize.x) && (fabs(point.z - cellPos.z) < 0.5 * cellSize.z)); } } diff --git a/unit.cpp b/unit.cpp index f7d1215..d9fafba 100755 --- a/unit.cpp +++ b/unit.cpp @@ -249,13 +249,12 @@ namespace battleship{ Map *map = Map::getSingleton(); int waterbodyId = -1; - if(type == UnitType::UNDERWATER || type == UnitType::SEA_LEVEL) - for(int i = 0; i < map->getNumWaterBodies(); i++){ - if(map->getWaterBody(i).isPointWithin(pos)){ - waterbodyId = i; - break; - } + for(int i = 0; i < map->getNumWaterBodies(); i++){ + if(map->getWaterBody(i).isPointWithin(pos)){ + waterbodyId = i; + break; } + } u32 impassibleNodeVal = Pathfinder::getSingleton()->getImpassibleNodeVal(); int size = cellsByDim[0] * cellsByDim[1] * cellsByDim[2]; @@ -293,12 +292,20 @@ namespace battleship{ MeshData::Vertex *verts = meshData.vertices; int numVerts = 3 * meshData.numTris; - if(type == UnitType::SEA_LEVEL){ - for(int k = 0; k < numVerts; k++) - if(map->isPointWithin(j, verts[k].pos, cellSize)){ - weight = impassibleNodeVal; - break; - } + for(int k = 0; k < numVerts; k++){ + bool pointWithin = map->isPointWithin(j, verts[k].pos, cellSize); + WaterBody waterbody; + + if(waterbodyId != -1) + waterbody = map->getWaterBody(waterbodyId); + + bool impassibleForSeaUnits = (type == UnitType::SEA_LEVEL && pointWithin && waterbodyId != -1 && verts[k].pos.y > waterbody.pos.y); + bool impassibleForLandUnits = (type == UnitType::LAND && pointWithin && (waterbodyId != -1 && verts[k].pos.y < waterbody.pos.y)); + + if(impassibleForSeaUnits || impassibleForLandUnits){ + weight = impassibleNodeVal; + break; + } } }