Skip to content

Commit 4d6fd78

Browse files
committed
refactor(pathfinder): Simplify and improve readability of Pathfinder::moveAllies
1 parent f40dc5d commit 4d6fd78

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

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

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4719,7 +4719,6 @@ Bool Pathfinder::checkForMovement(const Object *obj, TCheckMovementInfo &info)
47194719
}
47204720

47214721
PathfindCell::CellFlags flags = cell->getFlags();
4722-
ObjectID posUnit = cell->getPosUnit();
47234722
if ((flags == PathfindCell::UNIT_GOAL) || (flags == PathfindCell::UNIT_GOAL_OTHER_MOVING)) {
47244723
info.allyGoal = true;
47254724
}
@@ -4728,6 +4727,7 @@ Bool Pathfinder::checkForMovement(const Object *obj, TCheckMovementInfo &info)
47284727
continue; // Nobody is here, so it's ok.
47294728
}
47304729

4730+
ObjectID posUnit = cell->getPosUnit();
47314731
if (posUnit == obj->getID()) {
47324732
continue; // we got it.
47334733
}
@@ -9972,10 +9972,14 @@ if (g_UT_startTiming) return false;
99729972
#endif
99739973
if (!obj->isKindOf(KINDOF_DOZER) && !obj->isKindOf(KINDOF_HARVESTER)) {
99749974
// Harvesters & dozers want a clear path.
9975-
if (!path->getBlockedByAlly()) return FALSE; // Only move units if it is required.
9975+
if (!path->getBlockedByAlly()) {
9976+
return FALSE; // Only move units if it is required.
9977+
}
99769978
}
99779979
LatchRestore<Int> recursiveDepth(m_moveAlliesDepth, m_moveAlliesDepth+1);
9978-
if (m_moveAlliesDepth > 2) return false;
9980+
if (m_moveAlliesDepth > 2) {
9981+
return false;
9982+
}
99799983

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

10026-
//Kris: Patch 1.01 November 3, 2003
10027-
//Black Lotus exploit fix -- moving while hacking.
10028-
if( otherObj->testStatus( OBJECT_STATUS_IS_USING_ABILITY ) || otherObj->getAI()->isBusy() )
10029-
{
10030-
continue; // Packing or unpacking objects for example
10031-
}
10037+
if (!otherObj->getAI() || otherObj->getAI()->isMoving()) {
10038+
continue;
10039+
}
1003210040

10033-
//DEBUG_LOG(("Moving ally"));
10034-
otherObj->getAI()->aiMoveAwayFromUnit(obj, CMD_FROM_AI);
10035-
}
10041+
if (otherObj->getAI()->isAttacking()) {
10042+
continue; // Don't move units that are attacking. [8/14/2003]
10043+
}
10044+
10045+
//Kris: Patch 1.01 November 3, 2003
10046+
//Black Lotus exploit fix -- moving while hacking.
10047+
if( otherObj->testStatus( OBJECT_STATUS_IS_USING_ABILITY ) || otherObj->getAI()->isBusy() ) {
10048+
continue; // Packing or unpacking objects for example
1003610049
}
10050+
10051+
//DEBUG_LOG(("Moving ally"));
10052+
otherObj->getAI()->aiMoveAwayFromUnit(obj, CMD_FROM_AI);
1003710053
}
1003810054
}
1003910055
}

0 commit comments

Comments
 (0)