Skip to content

Commit fa7bc60

Browse files
committed
refactor(pathfinder): Simplify and improve readability of Pathfinder::moveAllies (#1645)
1 parent 9df91d8 commit fa7bc60

File tree

2 files changed

+98
-63
lines changed

2 files changed

+98
-63
lines changed

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

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9555,10 +9555,14 @@ if (g_UT_startTiming) return false;
95559555
#endif
95569556
if (!obj->isKindOf(KINDOF_DOZER) && !obj->isKindOf(KINDOF_HARVESTER)) {
95579557
// Harvesters & dozers want a clear path.
9558-
if (!path->getBlockedByAlly()) return FALSE; // Only move units if it is required.
9558+
if (!path->getBlockedByAlly()) {
9559+
return FALSE; // Only move units if it is required.
9560+
}
95599561
}
95609562
LatchRestore<Int> recursiveDepth(m_moveAlliesDepth, m_moveAlliesDepth+1);
9561-
if (m_moveAlliesDepth > 2) return false;
9563+
if (m_moveAlliesDepth > 2) {
9564+
return false;
9565+
}
95629566

95639567
Bool centerInCell;
95649568
Int radius;
@@ -9577,33 +9581,48 @@ if (g_UT_startTiming) return false;
95779581
for (i=curCell.x-radius; i<curCell.x+numCellsAbove; i++) {
95789582
for (j=curCell.y-radius; j<curCell.y+numCellsAbove; j++) {
95799583
PathfindCell *cell = getCell(node->getLayer(), i, j);
9580-
if (cell) {
9581-
if (cell->getPosUnit()==INVALID_ID) {
9584+
if (!cell) {
9585+
continue; // Cell is not on the pathfinding grid
9586+
}
9587+
9588+
ObjectID unitId = cell->getPosUnit();
9589+
if (unitId==INVALID_ID) {
9590+
continue;
9591+
}
9592+
9593+
if (unitId==obj->getID()) {
9594+
continue; // It's us.
9595+
}
9596+
9597+
if (unitId==ignoreId) {
9598+
continue; // It's the one we are ignoring.
9599+
}
9600+
9601+
Object *otherObj = TheGameLogic->findObjectByID(unitId);
9602+
if (!otherObj) {
9603+
continue;
9604+
}
9605+
9606+
if (obj->getRelationship(otherObj)!=ALLIES) {
9607+
continue; // Only move allies.
9608+
}
9609+
9610+
if (obj->isKindOf(KINDOF_INFANTRY) && otherObj->isKindOf(KINDOF_INFANTRY)) {
9611+
continue; // infantry can walk through other infantry, so just let them.
9612+
}
9613+
if (obj->isKindOf(KINDOF_INFANTRY) && !otherObj->isKindOf(KINDOF_INFANTRY)) {
9614+
// If this is a general clear operation, don't let infantry push vehicles.
9615+
if (!path->getBlockedByAlly()) {
95829616
continue;
95839617
}
9584-
if (cell->getPosUnit()==obj->getID()) {
9585-
continue; // It's us.
9586-
}
9587-
if (cell->getPosUnit()==ignoreId) {
9588-
continue; // It's the one we are ignoring.
9589-
}
9590-
Object *otherObj = TheGameLogic->findObjectByID(cell->getPosUnit());
9591-
if (obj->getRelationship(otherObj)!=ALLIES) {
9592-
continue; // Only move allies.
9593-
}
9594-
if (otherObj==NULL) continue;
9595-
if (obj->isKindOf(KINDOF_INFANTRY) && otherObj->isKindOf(KINDOF_INFANTRY)) {
9596-
continue; // infantry can walk through other infantry, so just let them.
9597-
}
9598-
if (obj->isKindOf(KINDOF_INFANTRY) && !otherObj->isKindOf(KINDOF_INFANTRY)) {
9599-
// If this is a general clear operation, don't let infantry push vehicles.
9600-
if (!path->getBlockedByAlly()) continue;
9601-
}
9602-
if (otherObj && otherObj->getAI() && !otherObj->getAI()->isMoving()) {
9603-
//DEBUG_LOG(("Moving ally"));
9604-
otherObj->getAI()->aiMoveAwayFromUnit(obj, CMD_FROM_AI);
9605-
}
96069618
}
9619+
9620+
if (!otherObj->getAI() || otherObj->getAI()->isMoving()) {
9621+
continue;
9622+
}
9623+
9624+
//DEBUG_LOG(("Moving ally"));
9625+
otherObj->getAI()->aiMoveAwayFromUnit(obj, CMD_FROM_AI);
96079626
}
96089627
}
96099628
}

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

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9967,10 +9967,14 @@ if (g_UT_startTiming) return false;
99679967
#endif
99689968
if (!obj->isKindOf(KINDOF_DOZER) && !obj->isKindOf(KINDOF_HARVESTER)) {
99699969
// Harvesters & dozers want a clear path.
9970-
if (!path->getBlockedByAlly()) return FALSE; // Only move units if it is required.
9970+
if (!path->getBlockedByAlly()) {
9971+
return FALSE; // Only move units if it is required.
9972+
}
99719973
}
99729974
LatchRestore<Int> recursiveDepth(m_moveAlliesDepth, m_moveAlliesDepth+1);
9973-
if (m_moveAlliesDepth > 2) return false;
9975+
if (m_moveAlliesDepth > 2) {
9976+
return false;
9977+
}
99749978

99759979
Bool centerInCell;
99769980
Int radius;
@@ -9989,46 +9993,58 @@ if (g_UT_startTiming) return false;
99899993
for (i=curCell.x-radius; i<curCell.x+numCellsAbove; i++) {
99909994
for (j=curCell.y-radius; j<curCell.y+numCellsAbove; j++) {
99919995
PathfindCell *cell = getCell(node->getLayer(), i, j);
9992-
if (cell) {
9993-
if (cell->getPosUnit()==INVALID_ID) {
9996+
if (!cell) {
9997+
continue; // Cell is not on the pathfinding grid
9998+
}
9999+
10000+
ObjectID unitId = cell->getPosUnit();
10001+
if (unitId==INVALID_ID) {
10002+
continue;
10003+
}
10004+
10005+
if (unitId==obj->getID()) {
10006+
continue; // It's us.
10007+
}
10008+
10009+
if (unitId==ignoreId) {
10010+
continue; // It's the one we are ignoring.
10011+
}
10012+
10013+
Object *otherObj = TheGameLogic->findObjectByID(unitId);
10014+
if (!otherObj) {
10015+
continue;
10016+
}
10017+
10018+
if (obj->getRelationship(otherObj)!=ALLIES) {
10019+
continue; // Only move allies.
10020+
}
10021+
10022+
if (obj->isKindOf(KINDOF_INFANTRY) && otherObj->isKindOf(KINDOF_INFANTRY)) {
10023+
continue; // infantry can walk through other infantry, so just let them.
10024+
}
10025+
if (obj->isKindOf(KINDOF_INFANTRY) && !otherObj->isKindOf(KINDOF_INFANTRY)) {
10026+
// If this is a general clear operation, don't let infantry push vehicles.
10027+
if (!path->getBlockedByAlly()) {
999410028
continue;
999510029
}
9996-
if (cell->getPosUnit()==obj->getID()) {
9997-
continue; // It's us.
9998-
}
9999-
if (cell->getPosUnit()==ignoreId) {
10000-
continue; // It's the one we are ignoring.
10001-
}
10002-
Object *otherObj = TheGameLogic->findObjectByID(cell->getPosUnit());
10003-
if (obj->getRelationship(otherObj)!=ALLIES) {
10004-
continue; // Only move allies.
10005-
}
10006-
if (otherObj==NULL) continue;
10007-
if (obj->isKindOf(KINDOF_INFANTRY) && otherObj->isKindOf(KINDOF_INFANTRY)) {
10008-
continue; // infantry can walk through other infantry, so just let them.
10009-
}
10010-
if (obj->isKindOf(KINDOF_INFANTRY) && !otherObj->isKindOf(KINDOF_INFANTRY)) {
10011-
// If this is a general clear operation, don't let infantry push vehicles.
10012-
if (!path->getBlockedByAlly()) continue;
10013-
}
10014-
if( otherObj && otherObj->getAI() && !otherObj->getAI()->isMoving() )
10015-
{
10016-
if( otherObj->getAI()->isAttacking() )
10017-
{
10018-
continue; // Don't move units that are attacking. [8/14/2003]
10019-
}
10030+
}
1002010031

10021-
//Kris: Patch 1.01 November 3, 2003
10022-
//Black Lotus exploit fix -- moving while hacking.
10023-
if( otherObj->testStatus( OBJECT_STATUS_IS_USING_ABILITY ) || otherObj->getAI()->isBusy() )
10024-
{
10025-
continue; // Packing or unpacking objects for example
10026-
}
10032+
if (!otherObj->getAI() || otherObj->getAI()->isMoving()) {
10033+
continue;
10034+
}
1002710035

10028-
//DEBUG_LOG(("Moving ally"));
10029-
otherObj->getAI()->aiMoveAwayFromUnit(obj, CMD_FROM_AI);
10030-
}
10036+
if (otherObj->getAI()->isAttacking()) {
10037+
continue; // Don't move units that are attacking. [8/14/2003]
10038+
}
10039+
10040+
//Kris: Patch 1.01 November 3, 2003
10041+
//Black Lotus exploit fix -- moving while hacking.
10042+
if( otherObj->testStatus( OBJECT_STATUS_IS_USING_ABILITY ) || otherObj->getAI()->isBusy() ) {
10043+
continue; // Packing or unpacking objects for example
1003110044
}
10045+
10046+
//DEBUG_LOG(("Moving ally"));
10047+
otherObj->getAI()->aiMoveAwayFromUnit(obj, CMD_FROM_AI);
1003210048
}
1003310049
}
1003410050
}

0 commit comments

Comments
 (0)