Skip to content

Commit 3ce09e5

Browse files
Maullerxezon
authored andcommitted
refactor(pathfinder): Simplify and improve readability of Pathfinder::findAttackPath (#1620)
1 parent 455b8e8 commit 3ce09e5

File tree

2 files changed

+66
-56
lines changed

2 files changed

+66
-56
lines changed

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

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10007,7 +10007,8 @@ Path *Pathfinder::patchPath( const Object *obj, const LocomotorSet& locomotorSet
1000710007
Path *Pathfinder::findAttackPath( const Object *obj, const LocomotorSet& locomotorSet, const Coord3D *from,
1000810008
const Object *victim, const Coord3D* victimPos, const Weapon *weapon )
1000910009
{
10010-
if (m_isMapReady == false) return NULL; // Should always be ok.
10010+
if (!m_isMapReady)
10011+
return NULL; // Should always be ok.
1001110012

1001210013
Bool isCrusher = obj ? obj->getCrusherLevel() > 0 : false;
1001310014
Int radius;
@@ -10032,26 +10033,30 @@ Path *Pathfinder::findAttackPath( const Object *obj, const LocomotorSet& locomot
1003210033
ICoord2D cellNdx;
1003310034
worldToCell(&testPos, &cellNdx);
1003410035
PathfindCell *aCell = getCell(obj->getLayer(), cellNdx.x, cellNdx.y);
10035-
if (aCell==NULL) break;
10036-
if (!validMovementPosition( isCrusher, locomotorSet.getValidSurfaces(), aCell )) {
10036+
if (!aCell)
1003710037
break;
10038-
}
10039-
if (!checkDestination(obj, cellNdx.x, cellNdx.y, obj->getLayer(), radius, centerInCell)) {
10038+
10039+
if (!validMovementPosition(isCrusher, locomotorSet.getValidSurfaces(), aCell))
1004010040
break;
10041+
10042+
if (!checkDestination(obj, cellNdx.x, cellNdx.y, obj->getLayer(), radius, centerInCell))
10043+
break;
10044+
10045+
if (!weapon->isGoalPosWithinAttackRange(obj, &testPos, victim, victimPos))
10046+
continue;
10047+
10048+
if (isAttackViewBlockedByObstacle(obj, testPos, victim, *victimPos))
10049+
continue;
10050+
10051+
// return path.
10052+
Path *path = newInstance(Path);
10053+
path->prependNode( &testPos, obj->getLayer() );
10054+
path->prependNode( &curPos, obj->getLayer() );
10055+
path->getFirstNode()->setNextOptimized(path->getFirstNode()->getNext());
10056+
if (TheGlobalData->m_debugAI==AI_DEBUG_PATHS) {
10057+
setDebugPath(path);
1004110058
}
10042-
if (weapon->isGoalPosWithinAttackRange(obj, &testPos, victim, victimPos)) {
10043-
if (!isAttackViewBlockedByObstacle(obj, testPos, victim, *victimPos)) {
10044-
// return path.
10045-
Path *path = newInstance(Path);
10046-
path->prependNode( &testPos, obj->getLayer() );
10047-
path->prependNode( &curPos, obj->getLayer() );
10048-
path->getFirstNode()->setNextOptimized(path->getFirstNode()->getNext());
10049-
if (TheGlobalData->m_debugAI==AI_DEBUG_PATHS) {
10050-
setDebugPath(path);
10051-
}
10052-
return path;
10053-
}
10054-
}
10059+
return path;
1005510060
}
1005610061
}
1005710062

@@ -10084,18 +10089,19 @@ Path *Pathfinder::findAttackPath( const Object *obj, const LocomotorSet& locomot
1008410089
objPos.x += PATHFIND_CELL_SIZE_F/2.0f;
1008510090
objPos.y += PATHFIND_CELL_SIZE_F/2.0f;
1008610091
}
10092+
10093+
if (!obj->getAIUpdateInterface()) // shouldn't happen, but can't move without an ai.
10094+
return NULL;
10095+
1008710096
worldToCell(&objPos, &startCellNdx);
1008810097
PathfindCell *parentCell = getClippedCell( obj->getLayer(), &objPos );
10089-
if (parentCell == NULL)
10098+
if (!parentCell)
1009010099
return NULL;
10091-
if (!obj->getAIUpdateInterface()) {
10092-
return NULL; // shouldn't happen, but can't move it without an ai.
10093-
}
10094-
const PathfindCell *startCell = parentCell;
1009510100

10096-
if (!parentCell->allocateInfo(startCellNdx)) {
10101+
if (!parentCell->allocateInfo(startCellNdx))
1009710102
return NULL;
10098-
}
10103+
10104+
const PathfindCell *startCell = parentCell;
1009910105
parentCell->startPathfind(NULL);
1010010106

1010110107
// determine start cell
@@ -10104,7 +10110,7 @@ Path *Pathfinder::findAttackPath( const Object *obj, const LocomotorSet& locomot
1010410110

1010510111
// determine goal cell
1010610112
PathfindCell *goalCell = getCell( LAYER_GROUND, victimCellNdx.x, victimCellNdx.y );
10107-
if (goalCell == NULL)
10113+
if (!goalCell)
1010810114
return NULL;
1010910115

1011010116
if (!goalCell->allocateInfo(victimCellNdx)) {
@@ -10215,8 +10221,7 @@ Path *Pathfinder::findAttackPath( const Object *obj, const LocomotorSet& locomot
1021510221
return path;
1021610222
}
1021710223
}
10218-
if (checkDestination(obj, parentCell->getXIndex(), parentCell->getYIndex(),
10219-
parentCell->getLayer(), radius, centerInCell)) {
10224+
if (checkDestination(obj, parentCell->getXIndex(), parentCell->getYIndex(), parentCell->getLayer(), radius, centerInCell)) {
1022010225
if (validMovementPosition( isCrusher, locomotorSet.getValidSurfaces(), parentCell )) {
1022110226
Real dx = IABS(victimCellNdx.x-parentCell->getXIndex());
1022210227
Real dy = IABS(victimCellNdx.y-parentCell->getYIndex());

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

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10431,7 +10431,8 @@ Path *Pathfinder::patchPath( const Object *obj, const LocomotorSet& locomotorSet
1043110431
Path *Pathfinder::findAttackPath( const Object *obj, const LocomotorSet& locomotorSet, const Coord3D *from,
1043210432
const Object *victim, const Coord3D* victimPos, const Weapon *weapon )
1043310433
{
10434-
if (m_isMapReady == false) return NULL; // Should always be ok.
10434+
if (!m_isMapReady)
10435+
return NULL; // Should always be ok.
1043510436

1043610437
Bool isCrusher = obj ? obj->getCrusherLevel() > 0 : false;
1043710438
Int radius;
@@ -10456,26 +10457,30 @@ Path *Pathfinder::findAttackPath( const Object *obj, const LocomotorSet& locomot
1045610457
ICoord2D cellNdx;
1045710458
worldToCell(&testPos, &cellNdx);
1045810459
PathfindCell *aCell = getCell(obj->getLayer(), cellNdx.x, cellNdx.y);
10459-
if (aCell==NULL) break;
10460-
if (!validMovementPosition( isCrusher, locomotorSet.getValidSurfaces(), aCell )) {
10460+
if (!aCell)
1046110461
break;
10462-
}
10463-
if (!checkDestination(obj, cellNdx.x, cellNdx.y, obj->getLayer(), radius, centerInCell)) {
10462+
10463+
if (!validMovementPosition(isCrusher, locomotorSet.getValidSurfaces(), aCell))
1046410464
break;
10465+
10466+
if (!checkDestination(obj, cellNdx.x, cellNdx.y, obj->getLayer(), radius, centerInCell))
10467+
break;
10468+
10469+
if (!weapon->isGoalPosWithinAttackRange(obj, &testPos, victim, victimPos))
10470+
continue;
10471+
10472+
if (isAttackViewBlockedByObstacle(obj, testPos, victim, *victimPos))
10473+
continue;
10474+
10475+
// return path.
10476+
Path *path = newInstance(Path);
10477+
path->prependNode( &testPos, obj->getLayer() );
10478+
path->prependNode( &curPos, obj->getLayer() );
10479+
path->getFirstNode()->setNextOptimized(path->getFirstNode()->getNext());
10480+
if (TheGlobalData->m_debugAI==AI_DEBUG_PATHS) {
10481+
setDebugPath(path);
1046510482
}
10466-
if (weapon->isGoalPosWithinAttackRange(obj, &testPos, victim, victimPos)) {
10467-
if (!isAttackViewBlockedByObstacle(obj, testPos, victim, *victimPos)) {
10468-
// return path.
10469-
Path *path = newInstance(Path);
10470-
path->prependNode( &testPos, obj->getLayer() );
10471-
path->prependNode( &curPos, obj->getLayer() );
10472-
path->getFirstNode()->setNextOptimized(path->getFirstNode()->getNext());
10473-
if (TheGlobalData->m_debugAI==AI_DEBUG_PATHS) {
10474-
setDebugPath(path);
10475-
}
10476-
return path;
10477-
}
10478-
}
10483+
return path;
1047910484
}
1048010485
}
1048110486

@@ -10508,18 +10513,19 @@ Path *Pathfinder::findAttackPath( const Object *obj, const LocomotorSet& locomot
1050810513
objPos.x += PATHFIND_CELL_SIZE_F/2.0f;
1050910514
objPos.y += PATHFIND_CELL_SIZE_F/2.0f;
1051010515
}
10516+
10517+
if (!obj->getAIUpdateInterface()) // shouldn't happen, but can't move without an ai.
10518+
return NULL;
10519+
1051110520
worldToCell(&objPos, &startCellNdx);
1051210521
PathfindCell *parentCell = getClippedCell( obj->getLayer(), &objPos );
10513-
if (parentCell == NULL)
10522+
if (!parentCell)
1051410523
return NULL;
10515-
if (!obj->getAIUpdateInterface()) {
10516-
return NULL; // shouldn't happen, but can't move it without an ai.
10517-
}
10518-
const PathfindCell *startCell = parentCell;
1051910524

10520-
if (!parentCell->allocateInfo(startCellNdx)) {
10525+
if (!parentCell->allocateInfo(startCellNdx))
1052110526
return NULL;
10522-
}
10527+
10528+
const PathfindCell *startCell = parentCell;
1052310529
parentCell->startPathfind(NULL);
1052410530

1052510531
// determine start cell
@@ -10528,7 +10534,7 @@ Path *Pathfinder::findAttackPath( const Object *obj, const LocomotorSet& locomot
1052810534

1052910535
// determine goal cell
1053010536
PathfindCell *goalCell = getCell( LAYER_GROUND, victimCellNdx.x, victimCellNdx.y );
10531-
if (goalCell == NULL)
10537+
if (!goalCell)
1053210538
return NULL;
1053310539

1053410540
if (!goalCell->allocateInfo(victimCellNdx)) {
@@ -10696,8 +10702,7 @@ Path *Pathfinder::findAttackPath( const Object *obj, const LocomotorSet& locomot
1069610702
return path;
1069710703
}
1069810704
}
10699-
if (checkDestination(obj, parentCell->getXIndex(), parentCell->getYIndex(),
10700-
parentCell->getLayer(), radius, centerInCell)) {
10705+
if (checkDestination(obj, parentCell->getXIndex(), parentCell->getYIndex(), parentCell->getLayer(), radius, centerInCell)) {
1070110706
if (validMovementPosition( isCrusher, locomotorSet.getValidSurfaces(), parentCell )) {
1070210707
Real dx = IABS(victimCellNdx.x-parentCell->getXIndex());
1070310708
Real dy = IABS(victimCellNdx.y-parentCell->getYIndex());

0 commit comments

Comments
 (0)