Skip to content

Commit 8c02b22

Browse files
Maullerxezon
authored andcommitted
refactor(pathfinder): Simplify and improve readability of Pathfinder::checkForMovement (#1645)
1 parent c95e79b commit 8c02b22

File tree

2 files changed

+155
-139
lines changed

2 files changed

+155
-139
lines changed

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

Lines changed: 79 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4415,7 +4415,10 @@ Bool Pathfinder::checkForMovement(const Object *obj, TCheckMovementInfo &info)
44154415
ObjectID allies[maxAlly];
44164416
Int numAlly = 0;
44174417

4418-
if (!obj) return true; // not object can move there.
4418+
if (!obj) {
4419+
return true; // not object can move there.
4420+
}
4421+
44194422
ObjectID ignoreId = INVALID_ID;
44204423
if (obj->getAIUpdateInterface()) {
44214424
ignoreId = obj->getAIUpdateInterface()->getIgnoredObstacleID();
@@ -4428,81 +4431,86 @@ Bool Pathfinder::checkForMovement(const Object *obj, TCheckMovementInfo &info)
44284431
for (i=info.cell.x-info.radius; i<info.cell.x+numCellsAbove; i++) {
44294432
for (j=info.cell.y-info.radius; j<info.cell.y+numCellsAbove; j++) {
44304433
PathfindCell *cell = getCell(info.layer,i, j);
4431-
if (cell) {
4432-
enum PathfindCell::CellFlags flags = cell->getFlags();
4433-
ObjectID posUnit = cell->getPosUnit();
4434-
if ((flags == PathfindCell::UNIT_GOAL) || (flags == PathfindCell::UNIT_GOAL_OTHER_MOVING)) {
4435-
info.allyGoal = true;
4436-
}
4437-
if (flags == PathfindCell::NO_UNITS) {
4438-
continue; // Nobody is here, so it's ok.
4439-
} else if (posUnit==obj->getID()) {
4440-
continue; // we got it.
4441-
} else if (posUnit==ignoreId) {
4442-
continue; // we are ignoring this one.
4443-
} else {
4444-
Bool check = false;
4445-
Object *unit = NULL;
4446-
if (flags==PathfindCell::UNIT_PRESENT_MOVING || flags==PathfindCell::UNIT_GOAL_OTHER_MOVING) {
4447-
unit = TheGameLogic->findObjectByID(posUnit);
4448-
// order matters: we want to know if I consider it to be an ally, not vice versa
4449-
if (unit && obj->getRelationship(unit) == ALLIES) {
4450-
info.allyMoving = true;
4451-
}
4452-
if (info.considerTransient) {
4453-
check = true;
4454-
}
4455-
}
4456-
if (flags == PathfindCell::UNIT_PRESENT_FIXED) {
4457-
check = true;
4458-
unit = TheGameLogic->findObjectByID(posUnit);
4459-
}
4460-
if (check && unit!=NULL) {
4461-
if (obj->getAIUpdateInterface() && obj->getAIUpdateInterface()->getIgnoredObstacleID()==unit->getID()) {
4462-
// Don't check if it's the ignored obstacle.
4463-
check = false;
4464-
}
4465-
}
4466-
if (check && unit) {
4434+
if (!cell) {
4435+
return false; // off the map, so can't move here.
4436+
}
4437+
4438+
PathfindCell::CellFlags flags = cell->getFlags();
4439+
if ((flags == PathfindCell::UNIT_GOAL) || (flags == PathfindCell::UNIT_GOAL_OTHER_MOVING)) {
4440+
info.allyGoal = true;
4441+
} else if (flags == PathfindCell::NO_UNITS) {
4442+
continue; // Nobody is here, so it's ok.
4443+
}
4444+
4445+
ObjectID posUnit = cell->getPosUnit();
4446+
if (posUnit == obj->getID()) {
4447+
continue; // we got it.
4448+
}
4449+
4450+
if (posUnit == ignoreId) {
4451+
continue; // we are ignoring this one.
4452+
}
4453+
4454+
Bool check = false;
4455+
Object *unit = NULL;
4456+
if (flags == PathfindCell::UNIT_PRESENT_MOVING || flags == PathfindCell::UNIT_GOAL_OTHER_MOVING) {
4457+
unit = TheGameLogic->findObjectByID(posUnit);
4458+
// order matters: we want to know if I consider it to be an ally, not vice versa
4459+
if (unit && obj->getRelationship(unit) == ALLIES) {
4460+
info.allyMoving = true;
4461+
}
4462+
if (info.considerTransient) {
4463+
check = true;
4464+
}
4465+
}
4466+
if (flags == PathfindCell::UNIT_PRESENT_FIXED) {
4467+
check = true;
4468+
unit = TheGameLogic->findObjectByID(posUnit);
4469+
}
4470+
if (check && unit!=NULL) {
4471+
if (obj->getAIUpdateInterface() && obj->getAIUpdateInterface()->getIgnoredObstacleID()==unit->getID()) {
4472+
// Don't check if it's the ignored obstacle.
4473+
check = false;
4474+
}
4475+
}
4476+
if (!check || !unit) {
4477+
continue;
4478+
}
4479+
44674480
#ifdef INFANTRY_MOVES_THROUGH_INFANTRY
4468-
if (obj->isKindOf(KINDOF_INFANTRY) && unit->isKindOf(KINDOF_INFANTRY)) {
4469-
// Infantry can run through infantry.
4470-
continue; //
4471-
}
4481+
if (obj->isKindOf(KINDOF_INFANTRY) && unit->isKindOf(KINDOF_INFANTRY)) {
4482+
// Infantry can run through infantry.
4483+
continue; //
4484+
}
44724485
#endif
4473-
// See if it is an ally.
4474-
// order matters: we want to know if I consider it to be an ally, not vice versa
4475-
if (obj->getRelationship(unit) == ALLIES) {
4476-
if (!unit->getAIUpdateInterface()) {
4477-
return false; // can't path through not-idle units.
4478-
}
4479-
if (!unit->getAIUpdateInterface()->isIdle()) {
4480-
return false; // can't path through not-idle units.
4481-
}
4482-
Bool found = false;
4483-
Int k;
4484-
for (k=0; k<numAlly; k++) {
4485-
if (allies[k] == unit->getID()) {
4486-
found = true;
4487-
}
4488-
}
4489-
if (!found) {
4490-
info.allyFixedCount++;
4491-
if (numAlly < maxAlly) {
4492-
allies[numAlly] = unit->getID();
4493-
numAlly++;
4494-
}
4495-
}
4496-
} else {
4497-
Bool canCrush = obj->canCrushOrSquish( unit, TEST_CRUSH_OR_SQUISH );
4498-
if (!canCrush) {
4499-
info.enemyFixed = true;
4500-
}
4501-
}
4486+
// See if it is an ally.
4487+
// order matters: we want to know if I consider it to be an ally, not vice versa
4488+
if (obj->getRelationship(unit) == ALLIES) {
4489+
if (!unit->getAIUpdateInterface()) {
4490+
return false; // can't path through not-idle units.
4491+
}
4492+
if (!unit->getAIUpdateInterface()->isIdle()) {
4493+
return false; // can't path through not-idle units.
4494+
}
4495+
Bool found = false;
4496+
Int k;
4497+
for (k=0; k<numAlly; k++) {
4498+
if (allies[k] == unit->getID()) {
4499+
found = true;
4500+
}
4501+
}
4502+
if (!found) {
4503+
info.allyFixedCount++;
4504+
if (numAlly < maxAlly) {
4505+
allies[numAlly] = unit->getID();
4506+
numAlly++;
45024507
}
45034508
}
45044509
} else {
4505-
return false; // off the map, so can't place here.
4510+
Bool canCrush = obj->canCrushOrSquish( unit, TEST_CRUSH_OR_SQUISH );
4511+
if (!canCrush) {
4512+
info.enemyFixed = true;
4513+
}
45064514
}
45074515
}
45084516
}

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)