Skip to content

Commit 89858fb

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

File tree

2 files changed

+106
-77
lines changed

2 files changed

+106
-77
lines changed

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

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4339,49 +4339,63 @@ Bool Pathfinder::checkDestination(const Object *obj, Int cellX, Int cellY, Pathf
43394339
for (i=cellX-iRadius; i<cellX+numCellsAbove; i++) {
43404340
for (j=cellY-iRadius; j<cellY+numCellsAbove; j++) {
43414341
PathfindCell *cell = getCell(layer, i, j);
4342-
if (cell) {
4343-
if (checkForAircraft) {
4344-
if (!cell->isAircraftGoal()) continue;
4345-
if (cell->getGoalAircraft()==objID) continue;
4346-
return false;
4347-
}
4348-
if (cell->getType()==PathfindCell::CELL_OBSTACLE) {
4349-
if (cell->isObstaclePresent( ignoreId ))
4350-
continue;
4351-
return false;
4342+
if (!cell) {
4343+
return false; // off the map, so can't place here.
4344+
}
4345+
4346+
if (checkForAircraft) {
4347+
if (!cell->isAircraftGoal()) {
4348+
continue;
43524349
}
4353-
if (cell->getFlags() == PathfindCell::NO_UNITS) {
4354-
continue; // Nobody is here, so it's ok.
4350+
if (cell->getGoalAircraft() == objID) {
4351+
continue;
43554352
}
4356-
ObjectID goalUnitID = cell->getGoalUnit();
4357-
if (goalUnitID==objID) {
4358-
continue; // we got it.
4359-
} else if (ignoreId==goalUnitID) {
4360-
continue; // we are ignoring it.
4361-
} else if (goalUnitID!=INVALID_ID) {
4362-
if (obj==NULL) {
4363-
return false;
4364-
}
4365-
Object *unit = TheGameLogic->findObjectByID(goalUnitID);
4366-
if (unit) {
4367-
// order matters: we want to know if I consider it to be an ally, not vice versa
4368-
if (obj->getRelationship(unit) == ALLIES) {
4369-
return false; // Don't usurp your allies goals. jba.
4370-
}
4371-
if (cell->getFlags()==PathfindCell::UNIT_PRESENT_FIXED) {
4372-
Bool canCrush = obj->canCrushOrSquish(unit, TEST_CRUSH_OR_SQUISH);
4373-
if (!canCrush) {
4374-
return false; // Don't move to an occupied cell.
4375-
}
4376-
}
4377-
}
4353+
return false;
4354+
}
4355+
4356+
if (cell->getType()==PathfindCell::CELL_OBSTACLE) {
4357+
if (cell->isObstaclePresent( ignoreId ))
4358+
continue;
4359+
return false;
4360+
}
4361+
4362+
if (cell->getFlags() == PathfindCell::NO_UNITS) {
4363+
continue; // Nobody is here, so it's ok.
4364+
}
4365+
4366+
ObjectID goalUnitID = cell->getGoalUnit();
4367+
if (goalUnitID == objID) {
4368+
continue; // we got it.
4369+
}
4370+
4371+
if (goalUnitID == ignoreId) {
4372+
continue; // we are ignoring it.
4373+
}
4374+
4375+
if (goalUnitID == INVALID_ID) {
4376+
continue;
4377+
}
4378+
4379+
if (!obj) {
4380+
return false;
4381+
}
4382+
Object *unit = TheGameLogic->findObjectByID(goalUnitID);
4383+
if (!unit) {
4384+
continue;
4385+
}
4386+
4387+
// order matters: we want to know if I consider it to be an ally, not vice versa
4388+
if (obj->getRelationship(unit) == ALLIES) {
4389+
return false; // Don't usurp your allies goals. jba.
4390+
}
4391+
if (cell->getFlags()==PathfindCell::UNIT_PRESENT_FIXED) {
4392+
Bool canCrush = obj->canCrushOrSquish(unit, TEST_CRUSH_OR_SQUISH);
4393+
if (!canCrush) {
4394+
return false; // Don't move to an occupied cell.
43784395
}
4379-
} else {
4380-
return false; // off the map, so can't place here.
43814396
}
43824397
}
43834398
}
4384-
43854399
return true;
43864400
}
43874401

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

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4623,52 +4623,67 @@ Bool Pathfinder::checkDestination(const Object *obj, Int cellX, Int cellY, Pathf
46234623
for (i=cellX-iRadius; i<cellX+numCellsAbove; i++) {
46244624
for (j=cellY-iRadius; j<cellY+numCellsAbove; j++) {
46254625
PathfindCell *cell = getCell(layer, i, j);
4626-
if (cell) {
4627-
if (checkForAircraft) {
4628-
if (!cell->isAircraftGoal()) continue;
4629-
if (cell->getGoalAircraft()==objID) continue;
4630-
return false;
4631-
}
4632-
if (cell->getType()==PathfindCell::CELL_OBSTACLE) {
4633-
if (cell->isObstaclePresent( ignoreId ))
4634-
continue;
4635-
return false;
4636-
}
4637-
if (IS_IMPASSABLE(cell->getType())) {
4638-
return false;
4626+
if (!cell) {
4627+
return false; // off the map, so can't place here.
4628+
}
4629+
4630+
if (checkForAircraft) {
4631+
if (!cell->isAircraftGoal()) {
4632+
continue;
46394633
}
4640-
if (cell->getFlags() == PathfindCell::NO_UNITS) {
4641-
continue; // Nobody is here, so it's ok.
4634+
if (cell->getGoalAircraft() == objID) {
4635+
continue;
46424636
}
4643-
ObjectID goalUnitID = cell->getGoalUnit();
4644-
if (goalUnitID==objID) {
4645-
continue; // we got it.
4646-
} else if (ignoreId==goalUnitID) {
4647-
continue; // we are ignoring it.
4648-
} else if (goalUnitID!=INVALID_ID) {
4649-
if (obj==NULL) {
4650-
return false;
4651-
}
4652-
Object *unit = TheGameLogic->findObjectByID(goalUnitID);
4653-
if (unit) {
4654-
// order matters: we want to know if I consider it to be an ally, not vice versa
4655-
if (obj->getRelationship(unit) == ALLIES) {
4656-
return false; // Don't usurp your allies goals. jba.
4657-
}
4658-
if (cell->getFlags()==PathfindCell::UNIT_PRESENT_FIXED) {
4659-
Bool canCrush = obj->canCrushOrSquish(unit, TEST_CRUSH_OR_SQUISH);
4660-
if (!canCrush) {
4661-
return false; // Don't move to an occupied cell.
4662-
}
4663-
}
4664-
}
4637+
return false;
4638+
}
4639+
4640+
if (cell->getType()==PathfindCell::CELL_OBSTACLE) {
4641+
if (cell->isObstaclePresent( ignoreId ))
4642+
continue;
4643+
return false;
4644+
}
4645+
4646+
if (IS_IMPASSABLE(cell->getType())) {
4647+
return false;
4648+
}
4649+
4650+
if (cell->getFlags() == PathfindCell::NO_UNITS) {
4651+
continue; // Nobody is here, so it's ok.
4652+
}
4653+
4654+
ObjectID goalUnitID = cell->getGoalUnit();
4655+
if (goalUnitID == objID) {
4656+
continue; // we got it.
4657+
}
4658+
4659+
if (goalUnitID == ignoreId) {
4660+
continue; // we are ignoring it.
4661+
}
4662+
4663+
if (goalUnitID == INVALID_ID) {
4664+
continue;
4665+
}
4666+
4667+
if (!obj) {
4668+
return false;
4669+
}
4670+
Object *unit = TheGameLogic->findObjectByID(goalUnitID);
4671+
if (!unit) {
4672+
continue;
4673+
}
4674+
4675+
// order matters: we want to know if I consider it to be an ally, not vice versa
4676+
if (obj->getRelationship(unit) == ALLIES) {
4677+
return false; // Don't usurp your allies goals. jba.
4678+
}
4679+
if (cell->getFlags()==PathfindCell::UNIT_PRESENT_FIXED) {
4680+
Bool canCrush = obj->canCrushOrSquish(unit, TEST_CRUSH_OR_SQUISH);
4681+
if (!canCrush) {
4682+
return false; // Don't move to an occupied cell.
46654683
}
4666-
} else {
4667-
return false; // off the map, so can't place here.
46684684
}
46694685
}
46704686
}
4671-
46724687
return true;
46734688
}
46744689

0 commit comments

Comments
 (0)