Skip to content

Commit 10189db

Browse files
committed
refactor(pathfinding): simplify function to improve readability Pathfinder::checkForMovement
1 parent 4b1653f commit 10189db

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

0 commit comments

Comments
 (0)