Skip to content

Commit b1409a7

Browse files
committed
refactor(pathfinding): simplify and reduce branching in Pathfinder::checkDestination
1 parent 6c6e05b commit b1409a7

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

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

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4623,52 +4623,59 @@ 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;
4639-
}
4640-
if (cell->getFlags() == PathfindCell::NO_UNITS) {
4641-
continue; // Nobody is here, so it's ok.
4642-
}
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-
}
4665-
}
4666-
} else {
4626+
if (!cell) {
46674627
return false; // off the map, so can't place here.
46684628
}
4629+
4630+
if (checkForAircraft) {
4631+
if (!cell->isAircraftGoal()) continue;
4632+
if (cell->getGoalAircraft()==objID) continue;
4633+
return false;
4634+
}
4635+
if (cell->getType()==PathfindCell::CELL_OBSTACLE) {
4636+
if (cell->isObstaclePresent( ignoreId ))
4637+
continue;
4638+
return false;
4639+
}
4640+
if (IS_IMPASSABLE(cell->getType())) {
4641+
return false;
4642+
}
4643+
if (cell->getFlags() == PathfindCell::NO_UNITS) {
4644+
continue; // Nobody is here, so it's ok.
4645+
}
4646+
ObjectID goalUnitID = cell->getGoalUnit();
4647+
if (goalUnitID == objID) {
4648+
continue; // we got it.
4649+
}
4650+
4651+
if (ignoreId == goalUnitID) {
4652+
continue; // we are ignoring it.
4653+
}
4654+
4655+
if (goalUnitID == INVALID_ID) {
4656+
continue;
4657+
}
4658+
4659+
if (!obj) {
4660+
return false;
4661+
}
4662+
Object *unit = TheGameLogic->findObjectByID(goalUnitID);
4663+
if (!unit) {
4664+
continue;
4665+
}
4666+
4667+
// order matters: we want to know if I consider it to be an ally, not vice versa
4668+
if (obj->getRelationship(unit) == ALLIES) {
4669+
return false; // Don't usurp your allies goals. jba.
4670+
}
4671+
if (cell->getFlags()==PathfindCell::UNIT_PRESENT_FIXED) {
4672+
Bool canCrush = obj->canCrushOrSquish(unit, TEST_CRUSH_OR_SQUISH);
4673+
if (!canCrush) {
4674+
return false; // Don't move to an occupied cell.
4675+
}
4676+
}
46694677
}
46704678
}
4671-
46724679
return true;
46734680
}
46744681

0 commit comments

Comments
 (0)