Skip to content

Commit 155280b

Browse files
Maullerxezon
authored andcommitted
refactor(pathfinder): Simplify and improve readability of Pathfinder::snapClosestGoalPosition (#1645)
1 parent 89858fb commit 155280b

File tree

2 files changed

+56
-48
lines changed

2 files changed

+56
-48
lines changed

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

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

45534553
// Try adjusting by 1.
45544554
Int i,j;
4555-
for (i=cell.x-1; i<cell.x+2; i++) {
4556-
for (j=cell.y-1; j<cell.y+2; j++) {
4555+
for (i = cell.x - 1; i < cell.x + 2; i++) {
4556+
for (j = cell.y - 1; j < cell.y + 2; j++) {
45574557
if (checkDestination(obj, i, j, layer, iRadius, center)) {
4558-
adjustCoordToCell(i, j, center, *pos, layer);
4558+
adjustCoordToCell(i, j, center, *pos, layer);
45594559
return;
45604560
}
45614561
}
45624562
}
4563-
if (iRadius==0) {
4564-
// Try to find an unoccupied cell.
4565-
for (i=cell.x-1; i<cell.x+2; i++) {
4566-
for (j=cell.y-1; j<cell.y+2; j++) {
4567-
PathfindCell *newCell = getCell(layer,i, j);
4568-
if (newCell) {
4569-
if (newCell->getGoalUnit()==INVALID_ID || newCell->getGoalUnit()==obj->getID()) {
4570-
adjustCoordToCell(i, j, center, *pos, layer);
4571-
return;
4572-
}
4573-
}
4563+
4564+
if (iRadius > 0)
4565+
return;
4566+
4567+
// Try to find an unoccupied cell.
4568+
for (i = cell.x - 1; i < cell.x + 2; i++) {
4569+
for (j = cell.y - 1; j < cell.y + 2; j++) {
4570+
PathfindCell* newCell = getCell(layer, i, j);
4571+
if (!newCell)
4572+
continue;
4573+
4574+
if (newCell->getGoalUnit() == INVALID_ID || newCell->getGoalUnit() == obj->getID()) {
4575+
adjustCoordToCell(i, j, center, *pos, layer);
4576+
return;
45744577
}
45754578
}
4576-
// Try to find an unoccupied cell.
4577-
for (i=cell.x-1; i<cell.x+2; i++) {
4578-
for (j=cell.y-1; j<cell.y+2; j++) {
4579-
PathfindCell *newCell = getCell(layer,i, j);
4580-
if (newCell) {
4581-
if (newCell->getFlags()!=PathfindCell::UNIT_PRESENT_FIXED) {
4582-
adjustCoordToCell(i, j, center, *pos, layer);
4583-
return;
4584-
}
4585-
}
4579+
}
4580+
4581+
for (i = cell.x - 1; i < cell.x + 2; i++) {
4582+
for (j = cell.y - 1; j < cell.y + 2; j++) {
4583+
PathfindCell* newCell = getCell(layer, i, j);
4584+
if (!newCell)
4585+
continue;
4586+
4587+
if (newCell->getFlags()!=PathfindCell::UNIT_PRESENT_FIXED) {
4588+
adjustCoordToCell(i, j, center, *pos, layer);
4589+
return;
45864590
}
45874591
}
45884592
}

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)