Skip to content

Commit 4c7b28b

Browse files
committed
refactor(pathfinding): simplify and reduce branching in snapClosestGoalPosition
1 parent 041d30a commit 4c7b28b

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
@@ -4829,37 +4829,33 @@ void Pathfinder::snapClosestGoalPosition(Object *obj, Coord3D *pos)
48294829

48304830
// Try adjusting by 1.
48314831
Int i,j;
4832-
for (i=cell.x-1; i<cell.x+2; i++) {
4833-
for (j=cell.y-1; j<cell.y+2; j++) {
4832+
for (i = cell.x - 1; i < cell.x + 2; i++) {
4833+
for (j = cell.y - 1; j < cell.y + 2; j++) {
48344834
if (checkDestination(obj, i, j, layer, iRadius, center)) {
4835-
adjustCoordToCell(i, j, center, *pos, layer);
4835+
adjustCoordToCell(i, j, center, *pos, layer);
48364836
return;
48374837
}
48384838
}
48394839
}
4840-
if (iRadius==0) {
4841-
// Try to find an unoccupied cell.
4842-
for (i=cell.x-1; i<cell.x+2; i++) {
4843-
for (j=cell.y-1; j<cell.y+2; j++) {
4844-
PathfindCell *newCell = getCell(layer,i, j);
4845-
if (newCell) {
4846-
if (newCell->getGoalUnit()==INVALID_ID || newCell->getGoalUnit()==obj->getID()) {
4847-
adjustCoordToCell(i, j, center, *pos, layer);
4848-
return;
4849-
}
4850-
}
4840+
4841+
if (iRadius > 0)
4842+
return;
4843+
4844+
for (i = cell.x - 1; i < cell.x + 2; i++) {
4845+
for (j = cell.y - 1; j < cell.y + 2; j++) {
4846+
// Try to find an unoccupied cell.
4847+
PathfindCell *newCell = getCell(layer,i, j);
4848+
if (!newCell)
4849+
continue;
4850+
4851+
if (newCell->getGoalUnit() == INVALID_ID || newCell->getGoalUnit() == obj->getID()) {
4852+
adjustCoordToCell(i, j, center, *pos, layer);
4853+
return;
48514854
}
4852-
}
4853-
// Try to find an unoccupied cell.
4854-
for (i=cell.x-1; i<cell.x+2; i++) {
4855-
for (j=cell.y-1; j<cell.y+2; j++) {
4856-
PathfindCell *newCell = getCell(layer,i, j);
4857-
if (newCell) {
4858-
if (newCell->getFlags()!=PathfindCell::UNIT_PRESENT_FIXED) {
4859-
adjustCoordToCell(i, j, center, *pos, layer);
4860-
return;
4861-
}
4862-
}
4855+
4856+
if (newCell->getFlags()!=PathfindCell::UNIT_PRESENT_FIXED) {
4857+
adjustCoordToCell(i, j, center, *pos, layer);
4858+
return;
48634859
}
48644860
}
48654861
}

0 commit comments

Comments
 (0)