Skip to content

Commit 6c6e05b

Browse files
Maullerxezon
authored andcommitted
refactor(pathfinder): Simplify and improve readability of Pathfinder::internal_findHierarchicalPath (#1619)
1 parent ab2c835 commit 6c6e05b

File tree

2 files changed

+119
-124
lines changed

2 files changed

+119
-124
lines changed

Generals/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5536,13 +5536,10 @@ Int Pathfinder::examineNeighboringCells(PathfindCell *parentCell, PathfindCell *
55365536
if ((newCell->getLayer()==LAYER_GROUND) && !m_zoneManager.isPassable(newCellCoord.x, newCellCoord.y)) {
55375537
notZonePassable = true;
55385538
}
5539-
if (isHuman) {
5540-
// check if new cell is in logical map. (computer can move off logical map)
5541-
if (newCellCoord.x < m_logicalExtent.lo.x) continue;
5542-
if (newCellCoord.y < m_logicalExtent.lo.y) continue;
5543-
if (newCellCoord.x > m_logicalExtent.hi.x) continue;
5544-
if (newCellCoord.y > m_logicalExtent.hi.y) continue;
5545-
}
5539+
5540+
// check if new cell is in logical map. (computer can move off logical map)
5541+
if (isHuman && checkCellOutsideExtents(newCellCoord))
5542+
continue;
55465543

55475544
// check if this neighbor cell is already on the open (waiting to be tried)
55485545
// or closed (already tried) lists
@@ -6825,9 +6822,10 @@ Path *Pathfinder::internal_findHierarchicalPath( Bool isHuman, const LocomotorSu
68256822

68266823
// determine goal cell
68276824
PathfindCell *goalCell = getCell( destinationLayer, cell.x, cell.y );
6828-
if (goalCell == NULL) {
6825+
if (!goalCell) {
68296826
return NULL;
68306827
}
6828+
68316829
if (!goalCell->allocateInfo(cell)) {
68326830
return NULL;
68336831
}
@@ -6836,9 +6834,10 @@ Path *Pathfinder::internal_findHierarchicalPath( Bool isHuman, const LocomotorSu
68366834
ICoord2D startCellNdx;
68376835
PathfindLayerEnum layer = TheTerrainLogic->getLayerForDestination(from);
68386836
PathfindCell *parentCell = getClippedCell( layer,&clipFrom );
6839-
if (parentCell == NULL) {
6837+
if (!parentCell) {
68406838
return NULL;
68416839
}
6840+
68426841
if (parentCell!=goalCell) {
68436842
worldToCell(&clipFrom, &startCellNdx);
68446843
if (!parentCell->allocateInfo(startCellNdx)) {
@@ -6879,11 +6878,10 @@ Path *Pathfinder::internal_findHierarchicalPath( Bool isHuman, const LocomotorSu
68796878
goalBlockNdx.y = -1;
68806879
}
68816880

6882-
if (parentCell->getLayer()==LAYER_GROUND) {
6883-
// initialize "open" list to contain start cell
6884-
m_openList = parentCell;
6885-
} else {
6886-
m_openList = parentCell;
6881+
// initialize "open" list to contain start cell
6882+
m_openList = parentCell;
6883+
6884+
if (parentCell->getLayer()!=LAYER_GROUND) {
68876885
PathfindLayerEnum layer = parentCell->getLayer();
68886886
// We're starting on a bridge, so link to land at the bridge end points.
68896887
ICoord2D ndx;
@@ -7015,12 +7013,16 @@ Path *Pathfinder::internal_findHierarchicalPath( Bool isHuman, const LocomotorSu
70157013
break;
70167014
}
70177015
PathfindCell *cell = getCell(LAYER_GROUND, toNdx.x, toNdx.y);
7018-
if (cell==NULL) continue;
7019-
if (cell->hasInfo() && (cell->getClosed() || cell->getOpen())) {
7016+
if (!cell)
70207017
continue;
7021-
}
7018+
7019+
if (cell->hasInfo() && (cell->getClosed() || cell->getOpen()))
7020+
continue;
7021+
70227022
PathfindCell *startCell = getCell(LAYER_GROUND, ndx.x, ndx.y);
7023-
if (startCell==NULL) continue;
7023+
if (!startCell)
7024+
continue;
7025+
70247026
if (startCell != parentCell) {
70257027
if(!startCell->allocateInfo(ndx)) {
70267028
// TheSuperHackers @info We need to forcefully cleanup dangling pathfinding cells if this failure condition is hit in retail
@@ -7156,20 +7158,19 @@ Path *Pathfinder::internal_findHierarchicalPath( Bool isHuman, const LocomotorSu
71567158
ICoord2D delta;
71577159
delta.x = -1; // left side moves -1.
71587160
delta.y = 0;
7161+
71597162
PathfindCell *cell = getCell(LAYER_GROUND, scanCell.x, scanCell.y);
7160-
if (cell==NULL) continue;
7161-
if (cell->hasInfo() && (cell->getClosed() || cell->getOpen())) {
7162-
if (parentZone == m_zoneManager.getBlockZone(locomotorSurface,
7163-
crusher, scanCell.x, scanCell.y, m_map)) {
7163+
if (!cell)
7164+
continue;
7165+
7166+
if ( cell->hasInfo() && (cell->getClosed() || cell->getOpen()) ) {
7167+
if (parentZone == m_zoneManager.getBlockZone(locomotorSurface, crusher, scanCell.x, scanCell.y, m_map))
71647168
break;
7165-
}
7166-
}
7167-
if (isHuman) {
7168-
if (scanCell.x < m_logicalExtent.lo.x || scanCell.x > m_logicalExtent.hi.x ||
7169-
scanCell.y < m_logicalExtent.lo.y || scanCell.y > m_logicalExtent.hi.y) {
7170-
continue;
7171-
}
71727169
}
7170+
7171+
if (isHuman && checkCellOutsideExtents(scanCell))
7172+
continue;
7173+
71737174
processHierarchicalCell(scanCell, delta, parentCell,
71747175
goalCell, parentZone, examinedZones, numExZones, crusher, cellCount);
71757176
}
@@ -7189,20 +7190,19 @@ Path *Pathfinder::internal_findHierarchicalPath( Bool isHuman, const LocomotorSu
71897190
ICoord2D delta;
71907191
delta.x = 1; // right side moves +1.
71917192
delta.y = 0;
7193+
71927194
PathfindCell *cell = getCell(LAYER_GROUND, scanCell.x, scanCell.y);
7193-
if (cell==NULL) continue;
7194-
if (cell->hasInfo() && (cell->getClosed() || cell->getOpen())) {
7195-
if (parentZone == m_zoneManager.getBlockZone(locomotorSurface,
7196-
crusher, scanCell.x, scanCell.y, m_map)) {
7195+
if (!cell)
7196+
continue;
7197+
7198+
if ( cell->hasInfo() && (cell->getClosed() || cell->getOpen()) ) {
7199+
if (parentZone == m_zoneManager.getBlockZone(locomotorSurface, crusher, scanCell.x, scanCell.y, m_map))
71977200
break;
7198-
}
7199-
}
7200-
if (isHuman) {
7201-
if (scanCell.x < m_logicalExtent.lo.x || scanCell.x > m_logicalExtent.hi.x ||
7202-
scanCell.y < m_logicalExtent.lo.y || scanCell.y > m_logicalExtent.hi.y) {
7203-
continue;
7204-
}
72057201
}
7202+
7203+
if (isHuman && checkCellOutsideExtents(scanCell))
7204+
continue;
7205+
72067206
processHierarchicalCell(scanCell, delta, parentCell,
72077207
goalCell, parentZone, examinedZones, numExZones, crusher, cellCount);
72087208
}
@@ -7221,20 +7221,19 @@ Path *Pathfinder::internal_findHierarchicalPath( Bool isHuman, const LocomotorSu
72217221
ICoord2D delta;
72227222
delta.x = 0;
72237223
delta.y = -1; // Top side moves -1.
7224+
72247225
PathfindCell *cell = getCell(LAYER_GROUND, scanCell.x, scanCell.y);
7225-
if (cell==NULL) continue;
7226-
if (cell->hasInfo() && (cell->getClosed() || cell->getOpen())) {
7227-
if (parentZone == m_zoneManager.getBlockZone(locomotorSurface,
7228-
crusher, scanCell.x, scanCell.y, m_map)) {
7226+
if (!cell)
7227+
continue;
7228+
7229+
if ( cell->hasInfo() && (cell->getClosed() || cell->getOpen()) ) {
7230+
if (parentZone == m_zoneManager.getBlockZone(locomotorSurface, crusher, scanCell.x, scanCell.y, m_map))
72297231
break;
7230-
}
7231-
}
7232-
if (isHuman) {
7233-
if (scanCell.x < m_logicalExtent.lo.x || scanCell.x > m_logicalExtent.hi.x ||
7234-
scanCell.y < m_logicalExtent.lo.y || scanCell.y > m_logicalExtent.hi.y) {
7235-
continue;
7236-
}
72377232
}
7233+
7234+
if (isHuman && checkCellOutsideExtents(scanCell))
7235+
continue;
7236+
72387237
processHierarchicalCell(scanCell, delta, parentCell,
72397238
goalCell, parentZone, examinedZones, numExZones, crusher, cellCount);
72407239
}
@@ -7254,20 +7253,19 @@ Path *Pathfinder::internal_findHierarchicalPath( Bool isHuman, const LocomotorSu
72547253
ICoord2D delta;
72557254
delta.x = 0;
72567255
delta.y = 1; // Top side moves +1.
7256+
72577257
PathfindCell *cell = getCell(LAYER_GROUND, scanCell.x, scanCell.y);
7258-
if (cell==NULL) continue;
7259-
if (cell->hasInfo() && (cell->getClosed() || cell->getOpen())) {
7260-
if (parentZone == m_zoneManager.getBlockZone(locomotorSurface,
7261-
crusher, scanCell.x, scanCell.y, m_map)) {
7258+
if (!cell)
7259+
continue;
7260+
7261+
if ( cell->hasInfo() && (cell->getClosed() || cell->getOpen()) ) {
7262+
if (parentZone == m_zoneManager.getBlockZone(locomotorSurface, crusher, scanCell.x, scanCell.y, m_map))
72627263
break;
7263-
}
7264-
}
7265-
if (isHuman) {
7266-
if (scanCell.x < m_logicalExtent.lo.x || scanCell.x > m_logicalExtent.hi.x ||
7267-
scanCell.y < m_logicalExtent.lo.y || scanCell.y > m_logicalExtent.hi.y) {
7268-
continue;
7269-
}
72707264
}
7265+
7266+
if (isHuman && checkCellOutsideExtents(scanCell))
7267+
continue;
7268+
72717269
processHierarchicalCell(scanCell, delta, parentCell,
72727270
goalCell, parentZone, examinedZones, numExZones, crusher, cellCount);
72737271
}

0 commit comments

Comments
 (0)