Skip to content

Commit b6835d0

Browse files
committed
refactor(pathfinding): simplify and reduce branching in snapClosestGoalPosition
1 parent e41c407 commit b6835d0

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4837,37 +4837,41 @@ void Pathfinder::snapClosestGoalPosition(Object *obj, Coord3D *pos)
48374837

48384838
// Try adjusting by 1.
48394839
Int i,j;
4840-
for (i=cell.x-1; i<cell.x+2; i++) {
4841-
for (j=cell.y-1; j<cell.y+2; j++) {
4840+
for (i = cell.x - 1; i < cell.x + 2; i++) {
4841+
for (j = cell.y - 1; j < cell.y + 2; j++) {
48424842
if (checkDestination(obj, i, j, layer, iRadius, center)) {
4843-
adjustCoordToCell(i, j, center, *pos, layer);
4843+
adjustCoordToCell(i, j, center, *pos, layer);
48444844
return;
48454845
}
48464846
}
48474847
}
4848-
if (iRadius==0) {
4849-
// Try to find an unoccupied cell.
4850-
for (i=cell.x-1; i<cell.x+2; i++) {
4851-
for (j=cell.y-1; j<cell.y+2; j++) {
4852-
PathfindCell *newCell = getCell(layer,i, j);
4853-
if (newCell) {
4854-
if (newCell->getGoalUnit()==INVALID_ID || newCell->getGoalUnit()==obj->getID()) {
4855-
adjustCoordToCell(i, j, center, *pos, layer);
4856-
return;
4857-
}
4858-
}
4848+
4849+
if (iRadius > 0)
4850+
return;
4851+
4852+
// Try to find an unoccupied cell.
4853+
for (i = cell.x - 1; i < cell.x + 2; i++) {
4854+
for (j = cell.y - 1; j < cell.y + 2; j++) {
4855+
PathfindCell* newCell = getCell(layer, i, j);
4856+
if (!newCell)
4857+
continue;
4858+
4859+
if (newCell->getGoalUnit() == INVALID_ID || newCell->getGoalUnit() == obj->getID()) {
4860+
adjustCoordToCell(i, j, center, *pos, layer);
4861+
return;
48594862
}
48604863
}
4861-
// Try to find an unoccupied cell.
4862-
for (i=cell.x-1; i<cell.x+2; i++) {
4863-
for (j=cell.y-1; j<cell.y+2; j++) {
4864-
PathfindCell *newCell = getCell(layer,i, j);
4865-
if (newCell) {
4866-
if (newCell->getFlags()!=PathfindCell::UNIT_PRESENT_FIXED) {
4867-
adjustCoordToCell(i, j, center, *pos, layer);
4868-
return;
4869-
}
4870-
}
4864+
}
4865+
4866+
for (i = cell.x - 1; i < cell.x + 2; i++) {
4867+
for (j = cell.y - 1; j < cell.y + 2; j++) {
4868+
PathfindCell* newCell = getCell(layer, i, j);
4869+
if (!newCell)
4870+
continue;
4871+
4872+
if (newCell->getFlags()!=PathfindCell::UNIT_PRESENT_FIXED) {
4873+
adjustCoordToCell(i, j, center, *pos, layer);
4874+
return;
48714875
}
48724876
}
48734877
}

0 commit comments

Comments
 (0)