Skip to content

Commit 204ede8

Browse files
committed
refactor(pathfinding): simplify and reduce branching in snapClosestGoalPosition
1 parent d370d1b commit 204ede8

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4832,37 +4832,33 @@ void Pathfinder::snapClosestGoalPosition(Object *obj, Coord3D *pos)
48324832

48334833
// Try adjusting by 1.
48344834
Int i,j;
4835-
for (i=cell.x-1; i<cell.x+2; i++) {
4836-
for (j=cell.y-1; j<cell.y+2; j++) {
4835+
for (i = cell.x - 1; i < cell.x + 2; i++) {
4836+
for (j = cell.y - 1; j < cell.y + 2; j++) {
48374837
if (checkDestination(obj, i, j, layer, iRadius, center)) {
4838-
adjustCoordToCell(i, j, center, *pos, layer);
4838+
adjustCoordToCell(i, j, center, *pos, layer);
48394839
return;
48404840
}
48414841
}
48424842
}
4843-
if (iRadius==0) {
4844-
// Try to find an unoccupied cell.
4845-
for (i=cell.x-1; i<cell.x+2; i++) {
4846-
for (j=cell.y-1; j<cell.y+2; j++) {
4847-
PathfindCell *newCell = getCell(layer,i, j);
4848-
if (newCell) {
4849-
if (newCell->getGoalUnit()==INVALID_ID || newCell->getGoalUnit()==obj->getID()) {
4850-
adjustCoordToCell(i, j, center, *pos, layer);
4851-
return;
4852-
}
4853-
}
4843+
4844+
if (iRadius > 0)
4845+
return;
4846+
4847+
for (i = cell.x - 1; i < cell.x + 2; i++) {
4848+
for (j = cell.y - 1; j < cell.y + 2; j++) {
4849+
// Try to find an unoccupied cell.
4850+
PathfindCell *newCell = getCell(layer,i, j);
4851+
if (!newCell)
4852+
continue;
4853+
4854+
if (newCell->getGoalUnit() == INVALID_ID || newCell->getGoalUnit() == obj->getID()) {
4855+
adjustCoordToCell(i, j, center, *pos, layer);
4856+
return;
48544857
}
4855-
}
4856-
// Try to find an unoccupied cell.
4857-
for (i=cell.x-1; i<cell.x+2; i++) {
4858-
for (j=cell.y-1; j<cell.y+2; j++) {
4859-
PathfindCell *newCell = getCell(layer,i, j);
4860-
if (newCell) {
4861-
if (newCell->getFlags()!=PathfindCell::UNIT_PRESENT_FIXED) {
4862-
adjustCoordToCell(i, j, center, *pos, layer);
4863-
return;
4864-
}
4865-
}
4858+
4859+
if (newCell->getFlags()!=PathfindCell::UNIT_PRESENT_FIXED) {
4860+
adjustCoordToCell(i, j, center, *pos, layer);
4861+
return;
48664862
}
48674863
}
48684864
}

0 commit comments

Comments
 (0)