Skip to content

Commit 0e78705

Browse files
committed
refactor(pathfinding): simplify function to improve readability Pathfinder::checkForMovement
1 parent d8fd57d commit 0e78705

File tree

1 file changed

+76
-68
lines changed

1 file changed

+76
-68
lines changed

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

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4703,7 +4703,10 @@ Bool Pathfinder::checkForMovement(const Object *obj, TCheckMovementInfo &info)
47034703
ObjectID allies[maxAlly];
47044704
Int numAlly = 0;
47054705

4706-
if (!obj) return true; // not object can move there.
4706+
if (!obj) {
4707+
return true; // not object can move there.
4708+
}
4709+
47074710
ObjectID ignoreId = INVALID_ID;
47084711
if (obj->getAIUpdateInterface()) {
47094712
ignoreId = obj->getAIUpdateInterface()->getIgnoredObstacleID();
@@ -4716,78 +4719,83 @@ Bool Pathfinder::checkForMovement(const Object *obj, TCheckMovementInfo &info)
47164719
for (i=info.cell.x-info.radius; i<info.cell.x+numCellsAbove; i++) {
47174720
for (j=info.cell.y-info.radius; j<info.cell.y+numCellsAbove; j++) {
47184721
PathfindCell *cell = getCell(info.layer,i, j);
4719-
if (cell) {
4720-
enum PathfindCell::CellFlags flags = cell->getFlags();
4721-
ObjectID posUnit = cell->getPosUnit();
4722-
if ((flags == PathfindCell::UNIT_GOAL) || (flags == PathfindCell::UNIT_GOAL_OTHER_MOVING)) {
4723-
info.allyGoal = true;
4724-
}
4725-
if (flags == PathfindCell::NO_UNITS) {
4726-
continue; // Nobody is here, so it's ok.
4727-
} else if (posUnit==obj->getID()) {
4728-
continue; // we got it.
4729-
} else if (posUnit==ignoreId) {
4730-
continue; // we are ignoring this one.
4731-
} else {
4732-
Bool check = false;
4733-
Object *unit = NULL;
4734-
if (flags==PathfindCell::UNIT_PRESENT_MOVING || flags==PathfindCell::UNIT_GOAL_OTHER_MOVING) {
4735-
unit = TheGameLogic->findObjectByID(posUnit);
4736-
// order matters: we want to know if I consider it to be an ally, not vice versa
4737-
if (unit && obj->getRelationship(unit) == ALLIES) {
4738-
info.allyMoving = true;
4739-
}
4740-
if (info.considerTransient) {
4741-
check = true;
4742-
}
4743-
}
4744-
if (flags == PathfindCell::UNIT_PRESENT_FIXED) {
4745-
check = true;
4746-
unit = TheGameLogic->findObjectByID(posUnit);
4747-
}
4748-
if (check && unit!=NULL) {
4749-
if (obj->getAIUpdateInterface() && obj->getAIUpdateInterface()->getIgnoredObstacleID()==unit->getID()) {
4750-
// Don't check if it's the ignored obstacle.
4751-
check = false;
4752-
}
4753-
}
4754-
if (check && unit) {
4722+
if (!cell) {
4723+
return false; // off the map, so can't move here.
4724+
}
4725+
4726+
PathfindCell::CellFlags flags = cell->getFlags();
4727+
if ((flags == PathfindCell::UNIT_GOAL) || (flags == PathfindCell::UNIT_GOAL_OTHER_MOVING)) {
4728+
info.allyGoal = true;
4729+
} else if (flags == PathfindCell::NO_UNITS) {
4730+
continue; // Nobody is here, so it's ok.
4731+
}
4732+
4733+
ObjectID posUnit = cell->getPosUnit();
4734+
if (posUnit == obj->getID()) {
4735+
continue; // we got it.
4736+
}
4737+
4738+
if (posUnit == ignoreId) {
4739+
continue; // we are ignoring this one.
4740+
}
4741+
4742+
Bool check = false;
4743+
Object *unit = NULL;
4744+
if (flags == PathfindCell::UNIT_PRESENT_MOVING || flags == PathfindCell::UNIT_GOAL_OTHER_MOVING) {
4745+
unit = TheGameLogic->findObjectByID(posUnit);
4746+
// order matters: we want to know if I consider it to be an ally, not vice versa
4747+
if (unit && obj->getRelationship(unit) == ALLIES) {
4748+
info.allyMoving = true;
4749+
}
4750+
if (info.considerTransient) {
4751+
check = true;
4752+
}
4753+
}
4754+
if (flags == PathfindCell::UNIT_PRESENT_FIXED) {
4755+
check = true;
4756+
unit = TheGameLogic->findObjectByID(posUnit);
4757+
}
4758+
if (check && unit!=NULL) {
4759+
if (obj->getAIUpdateInterface() && obj->getAIUpdateInterface()->getIgnoredObstacleID()==unit->getID()) {
4760+
// Don't check if it's the ignored obstacle.
4761+
check = false;
4762+
}
4763+
}
4764+
if (!check || !unit) {
4765+
continue;
4766+
}
4767+
47554768
#ifdef INFANTRY_MOVES_THROUGH_INFANTRY
4756-
if (obj->isKindOf(KINDOF_INFANTRY) && unit->isKindOf(KINDOF_INFANTRY)) {
4757-
// Infantry can run through infantry.
4758-
continue; //
4759-
}
4769+
if (obj->isKindOf(KINDOF_INFANTRY) && unit->isKindOf(KINDOF_INFANTRY)) {
4770+
// Infantry can run through infantry.
4771+
continue; //
4772+
}
47604773
#endif
4761-
// See if it is an ally.
4762-
// order matters: we want to know if I consider it to be an ally, not vice versa
4763-
if (obj->getRelationship(unit) == ALLIES) {
4764-
if (!unit->getAIUpdateInterface()) {
4765-
return false; // can't path through not-idle units.
4766-
}
4767-
Bool found = false;
4768-
Int k;
4769-
for (k=0; k<numAlly; k++) {
4770-
if (allies[k] == unit->getID()) {
4771-
found = true;
4772-
}
4773-
}
4774-
if (!found) {
4775-
info.allyFixedCount++;
4776-
if (numAlly < maxAlly) {
4777-
allies[numAlly] = unit->getID();
4778-
numAlly++;
4779-
}
4780-
}
4781-
} else {
4782-
Bool canCrush = obj->canCrushOrSquish( unit, TEST_CRUSH_OR_SQUISH );
4783-
if (!canCrush) {
4784-
info.enemyFixed = true;
4785-
}
4786-
}
4774+
// See if it is an ally.
4775+
// order matters: we want to know if I consider it to be an ally, not vice versa
4776+
if (obj->getRelationship(unit) == ALLIES) {
4777+
if (!unit->getAIUpdateInterface()) {
4778+
return false; // can't path through not-idle units.
4779+
}
4780+
Bool found = false;
4781+
Int k;
4782+
for (k=0; k<numAlly; k++) {
4783+
if (allies[k] == unit->getID()) {
4784+
found = true;
4785+
}
4786+
}
4787+
if (!found) {
4788+
info.allyFixedCount++;
4789+
if (numAlly < maxAlly) {
4790+
allies[numAlly] = unit->getID();
4791+
numAlly++;
47874792
}
47884793
}
47894794
} else {
4790-
return false; // off the map, so can't place here.
4795+
Bool canCrush = obj->canCrushOrSquish( unit, TEST_CRUSH_OR_SQUISH );
4796+
if (!canCrush) {
4797+
info.enemyFixed = true;
4798+
}
47914799
}
47924800
}
47934801
}

0 commit comments

Comments
 (0)