Skip to content

Commit e918046

Browse files
committed
refactor(pathfinding): simplify function to improve readability Pathfinder::checkForMovement
1 parent 20e9367 commit e918046

File tree

1 file changed

+78
-68
lines changed

1 file changed

+78
-68
lines changed

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

Lines changed: 78 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,85 @@ 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+
ObjectID posUnit = cell->getPosUnit();
4728+
if ((flags == PathfindCell::UNIT_GOAL) || (flags == PathfindCell::UNIT_GOAL_OTHER_MOVING)) {
4729+
info.allyGoal = true;
4730+
}
4731+
4732+
if (flags == PathfindCell::NO_UNITS) {
4733+
continue; // Nobody is here, so it's ok.
4734+
}
4735+
4736+
if (posUnit == obj->getID()) {
4737+
continue; // we got it.
4738+
}
4739+
4740+
if (posUnit == ignoreId) {
4741+
continue; // we are ignoring this one.
4742+
}
4743+
4744+
Bool check = false;
4745+
Object *unit = NULL;
4746+
if (flags == PathfindCell::UNIT_PRESENT_MOVING || flags == PathfindCell::UNIT_GOAL_OTHER_MOVING) {
4747+
unit = TheGameLogic->findObjectByID(posUnit);
4748+
// order matters: we want to know if I consider it to be an ally, not vice versa
4749+
if (unit && obj->getRelationship(unit) == ALLIES) {
4750+
info.allyMoving = true;
4751+
}
4752+
if (info.considerTransient) {
4753+
check = true;
4754+
}
4755+
}
4756+
if (flags == PathfindCell::UNIT_PRESENT_FIXED) {
4757+
check = true;
4758+
unit = TheGameLogic->findObjectByID(posUnit);
4759+
}
4760+
if (check && unit!=NULL) {
4761+
if (obj->getAIUpdateInterface() && obj->getAIUpdateInterface()->getIgnoredObstacleID()==unit->getID()) {
4762+
// Don't check if it's the ignored obstacle.
4763+
check = false;
4764+
}
4765+
}
4766+
if (!check || !unit) {
4767+
continue;
4768+
}
4769+
47554770
#ifdef INFANTRY_MOVES_THROUGH_INFANTRY
4756-
if (obj->isKindOf(KINDOF_INFANTRY) && unit->isKindOf(KINDOF_INFANTRY)) {
4757-
// Infantry can run through infantry.
4758-
continue; //
4759-
}
4771+
if (obj->isKindOf(KINDOF_INFANTRY) && unit->isKindOf(KINDOF_INFANTRY)) {
4772+
// Infantry can run through infantry.
4773+
continue; //
4774+
}
47604775
#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-
}
4776+
// See if it is an ally.
4777+
// order matters: we want to know if I consider it to be an ally, not vice versa
4778+
if (obj->getRelationship(unit) == ALLIES) {
4779+
if (!unit->getAIUpdateInterface()) {
4780+
return false; // can't path through not-idle units.
4781+
}
4782+
Bool found = false;
4783+
Int k;
4784+
for (k=0; k<numAlly; k++) {
4785+
if (allies[k] == unit->getID()) {
4786+
found = true;
4787+
}
4788+
}
4789+
if (!found) {
4790+
info.allyFixedCount++;
4791+
if (numAlly < maxAlly) {
4792+
allies[numAlly] = unit->getID();
4793+
numAlly++;
47874794
}
47884795
}
47894796
} else {
4790-
return false; // off the map, so can't place here.
4797+
Bool canCrush = obj->canCrushOrSquish( unit, TEST_CRUSH_OR_SQUISH );
4798+
if (!canCrush) {
4799+
info.enemyFixed = true;
4800+
}
47914801
}
47924802
}
47934803
}

0 commit comments

Comments
 (0)