Skip to content

Commit b331e23

Browse files
committed
refactor(pathfinding): simplify function to improve readability Pathfinder::checkForMovement
1 parent 861c09d commit b331e23

File tree

1 file changed

+72
-68
lines changed

1 file changed

+72
-68
lines changed

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

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4711,78 +4711,82 @@ Bool Pathfinder::checkForMovement(const Object *obj, TCheckMovementInfo &info)
47114711
for (i=info.cell.x-info.radius; i<info.cell.x+numCellsAbove; i++) {
47124712
for (j=info.cell.y-info.radius; j<info.cell.y+numCellsAbove; j++) {
47134713
PathfindCell *cell = getCell(info.layer,i, j);
4714-
if (cell) {
4715-
enum PathfindCell::CellFlags flags = cell->getFlags();
4716-
ObjectID posUnit = cell->getPosUnit();
4717-
if ((flags == PathfindCell::UNIT_GOAL) || (flags == PathfindCell::UNIT_GOAL_OTHER_MOVING)) {
4718-
info.allyGoal = true;
4719-
}
4720-
if (flags == PathfindCell::NO_UNITS) {
4721-
continue; // Nobody is here, so it's ok.
4722-
} else if (posUnit==obj->getID()) {
4723-
continue; // we got it.
4724-
} else if (posUnit==ignoreId) {
4725-
continue; // we are ignoring this one.
4726-
} else {
4727-
Bool check = false;
4728-
Object *unit = NULL;
4729-
if (flags==PathfindCell::UNIT_PRESENT_MOVING || flags==PathfindCell::UNIT_GOAL_OTHER_MOVING) {
4730-
unit = TheGameLogic->findObjectByID(posUnit);
4731-
// order matters: we want to know if I consider it to be an ally, not vice versa
4732-
if (unit && obj->getRelationship(unit) == ALLIES) {
4733-
info.allyMoving = true;
4734-
}
4735-
if (info.considerTransient) {
4736-
check = true;
4737-
}
4738-
}
4739-
if (flags == PathfindCell::UNIT_PRESENT_FIXED) {
4740-
check = true;
4741-
unit = TheGameLogic->findObjectByID(posUnit);
4742-
}
4743-
if (check && unit!=NULL) {
4744-
if (obj->getAIUpdateInterface() && obj->getAIUpdateInterface()->getIgnoredObstacleID()==unit->getID()) {
4745-
// Don't check if it's the ignored obstacle.
4746-
check = false;
4747-
}
4748-
}
4749-
if (check && unit) {
4714+
if (!cell) {
4715+
return false; // off the map, so can't move here.
4716+
}
4717+
4718+
enum PathfindCell::CellFlags flags = cell->getFlags();
4719+
ObjectID posUnit = cell->getPosUnit();
4720+
if ((flags == PathfindCell::UNIT_GOAL) || (flags == PathfindCell::UNIT_GOAL_OTHER_MOVING)) {
4721+
info.allyGoal = true;
4722+
}
4723+
4724+
if (flags == PathfindCell::NO_UNITS)
4725+
continue; // Nobody is here, so it's ok.
4726+
4727+
if (posUnit==obj->getID())
4728+
continue; // we got it.
4729+
4730+
if (posUnit==ignoreId) {
4731+
continue; // we are ignoring this one.
4732+
}
4733+
4734+
Bool check = false;
4735+
Object *unit = NULL;
4736+
if (flags==PathfindCell::UNIT_PRESENT_MOVING || flags==PathfindCell::UNIT_GOAL_OTHER_MOVING) {
4737+
unit = TheGameLogic->findObjectByID(posUnit);
4738+
// order matters: we want to know if I consider it to be an ally, not vice versa
4739+
if (unit && obj->getRelationship(unit) == ALLIES) {
4740+
info.allyMoving = true;
4741+
}
4742+
if (info.considerTransient) {
4743+
check = true;
4744+
}
4745+
}
4746+
if (flags == PathfindCell::UNIT_PRESENT_FIXED) {
4747+
check = true;
4748+
unit = TheGameLogic->findObjectByID(posUnit);
4749+
}
4750+
if (check && unit!=NULL) {
4751+
if (obj->getAIUpdateInterface() && obj->getAIUpdateInterface()->getIgnoredObstacleID()==unit->getID()) {
4752+
// Don't check if it's the ignored obstacle.
4753+
check = false;
4754+
}
4755+
}
4756+
if (!check || !unit)
4757+
continue;
4758+
47504759
#ifdef INFANTRY_MOVES_THROUGH_INFANTRY
4751-
if (obj->isKindOf(KINDOF_INFANTRY) && unit->isKindOf(KINDOF_INFANTRY)) {
4752-
// Infantry can run through infantry.
4753-
continue; //
4754-
}
4760+
if (obj->isKindOf(KINDOF_INFANTRY) && unit->isKindOf(KINDOF_INFANTRY)) {
4761+
// Infantry can run through infantry.
4762+
continue; //
4763+
}
47554764
#endif
4756-
// See if it is an ally.
4757-
// order matters: we want to know if I consider it to be an ally, not vice versa
4758-
if (obj->getRelationship(unit) == ALLIES) {
4759-
if (!unit->getAIUpdateInterface()) {
4760-
return false; // can't path through not-idle units.
4761-
}
4762-
Bool found = false;
4763-
Int k;
4764-
for (k=0; k<numAlly; k++) {
4765-
if (allies[k] == unit->getID()) {
4766-
found = true;
4767-
}
4768-
}
4769-
if (!found) {
4770-
info.allyFixedCount++;
4771-
if (numAlly < maxAlly) {
4772-
allies[numAlly] = unit->getID();
4773-
numAlly++;
4774-
}
4775-
}
4776-
} else {
4777-
Bool canCrush = obj->canCrushOrSquish( unit, TEST_CRUSH_OR_SQUISH );
4778-
if (!canCrush) {
4779-
info.enemyFixed = true;
4780-
}
4781-
}
4765+
// See if it is an ally.
4766+
// order matters: we want to know if I consider it to be an ally, not vice versa
4767+
if (obj->getRelationship(unit) == ALLIES) {
4768+
if (!unit->getAIUpdateInterface()) {
4769+
return false; // can't path through not-idle units.
4770+
}
4771+
Bool found = false;
4772+
Int k;
4773+
for (k=0; k<numAlly; k++) {
4774+
if (allies[k] == unit->getID()) {
4775+
found = true;
47824776
}
47834777
}
4784-
} else {
4785-
return false; // off the map, so can't place here.
4778+
if (!found) {
4779+
info.allyFixedCount++;
4780+
if (numAlly < maxAlly) {
4781+
allies[numAlly] = unit->getID();
4782+
numAlly++;
4783+
}
4784+
}
4785+
} else {
4786+
Bool canCrush = obj->canCrushOrSquish( unit, TEST_CRUSH_OR_SQUISH );
4787+
if (!canCrush) {
4788+
info.enemyFixed = true;
4789+
}
47864790
}
47874791
}
47884792
}

0 commit comments

Comments
 (0)