Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gameengine): make the hierarchical path corridor work and settle the pathing tweaks

Olcay Seyganfc7ea0218c86
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
index 7d1cc6a11..1773a8a34 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
@@ -1624,7 +1624,7 @@ void AIGroup::groupMoveToPosition( const Coord3D *p_posIn, Bool addWaypoint, Com
isFormation = false;
if (!addWaypoint) {
Int dx = (max.x-min.x)/PATHFIND_CELL_SIZE_F;
- Int dy = (max.y-min.y)/PATHFIND_CELL_SIZE_F;
+ Int dy = (max.x-min.x)/PATHFIND_CELL_SIZE_F; // retail typo kept on purpose: fixing it gathers groups far more often, which players read as wrong paths
Int cells = (dx*dy);
if (cells<2000) {
groupTightenToPosition(pos, false, cmdSource);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
index 6caf57974..8ac53e7d4 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
@@ -1111,7 +1111,10 @@ Real Path::computeFlightDistToGoal( const Coord3D *pos, Coord3D& goalPos )
//-----------------------------------------------------------------------------------
enum { PATHFIND_CELLS_PER_FRAME=5000}; // Number of cells we will search pathfinding per frame.
-enum {CELL_INFOS_TO_ALLOCATE = 30000};
+// A* open+closed sets draw from this pool; running dry aborts the search (no path) after
+// burning the time. Retail 30000 was sized for 2003 memory - a long detour around a
+// mountain on a large map can need more. 48 bytes each, so this is ~7 MB.
+enum {CELL_INFOS_TO_ALLOCATE = 150000};
PathfindCellInfo *PathfindCellInfo::s_infoArray = NULL;
PathfindCellInfo *PathfindCellInfo::s_firstFree = NULL;
/**
@@ -1684,7 +1687,11 @@ const Int COST_ORTHOGONAL = 10;
const Int COST_DIAGONAL = 14;
// ponytail: congestion knob, per other-unit path crossing a cell of our footprint. Rebuild to tune;
// move into AIData INI if it needs per-map tweaking.
-const Int PATH_CONGESTION_COST = COST_ORTHOGONAL;
+// 0 = off (retail pathing). In-game the lane spreading read as "wrong paths" and the extra search
+// as stutter even after the range/cap limits below, so it is off until it can be measured properly.
+const Int PATH_CONGESTION_COST = 0;
+const Int PATH_CONGESTION_RANGE = 30; ///< cells from the start within which other paths cost anything
+const Int PATH_CONGESTION_MAX_PATHS = 4; ///< the penalty stops growing past this many paths in a footprint
const Real COST_TO_DISTANCE_FACTOR = 1.0f/10.0f;
const Real COST_TO_DISTANCE_FACTOR_SQR = COST_TO_DISTANCE_FACTOR*COST_TO_DISTANCE_FACTOR;
@@ -6352,13 +6359,19 @@ Int Pathfinder::examineNeighboringCells(PathfindCell *parentCell, PathfindCell *
if (info.allyMoving && dx<10 && dy<10) {
newCostSoFar += 3*COST_DIAGONAL;
}
- if (m_pathUsage) {
- // Other units' live paths through our footprint: pay per path so we pick a parallel lane.
+ if (PATH_CONGESTION_COST > 0 && m_pathUsage && dx < PATH_CONGESTION_RANGE && dy < PATH_CONGESTION_RANGE) {
+ // Other units' live paths through our footprint: pay per path so we pick a parallel
+ // lane. Only near the start (where lanes form) and capped: the heuristic does not
+ // know about this cost, so an uncapped penalty along a long path inflates the
+ // search toward a full flood - nine tanks sent across the map used to exhaust the
+ // cell pool (no path) and hitch the frame.
Int usage = 0;
Int above = radius + (centerInCell ? 1 : 0);
for (Int ux = newCellCoord.x-radius; ux < newCellCoord.x+above; ux++)
for (Int uy = newCellCoord.y-radius; uy < newCellCoord.y+above; uy++)
usage += getPathUsage(ux, uy);
+ if (usage > PATH_CONGESTION_MAX_PATHS)
+ usage = PATH_CONGESTION_MAX_PATHS;
newCostSoFar += usage * PATH_CONGESTION_COST;
}
if (newCell->getType() == PathfindCell::CELL_CLIFF && !newCell->getPinched() ) {
@@ -6376,9 +6389,10 @@ Int Pathfinder::examineNeighboringCells(PathfindCell *parentCell, PathfindCell *
newCostSoFar += 7*COST_DIAGONAL;
}
} else if (newCell->getPinched()) {
- // Pinched = clear cell hugging an obstacle. Charge by footprint so wide
- // vehicles prefer open corridors while infantry threads the gap as before.
- newCostSoFar += COST_ORTHOGONAL*(1+radius);
+ // (a footprint-scaled penalty here was tried and reverted: along cliff and building
+ // edges it inflated vehicle path costs the heuristic knows nothing about, so long
+ // searches ballooned, exhausted the cell pool and hitched - retail flat cost)
+ newCostSoFar += COST_ORTHOGONAL;
}
newCell->setBlockedByAlly(false);
if (info.allyFixedCount>0) {
@@ -6477,6 +6491,7 @@ Path *Pathfinder::findPath( Object *obj, const LocomotorSet& locomotorSet, const
m_zoneManager.clearPassableFlags();
Path *hPat = findHierarchicalPath(isHuman, locomotorSet, from, rawTo, false);
+ Bool hadCorridor = (hPat != NULL);
if (hPat) {
hPat->deleteInstance();
} else {
@@ -6488,6 +6503,17 @@ Path *Pathfinder::findPath( Object *obj, const LocomotorSet& locomotorSet, const
return pat;
}
+ // the block corridor the hierarchical search laid out can be too tight for this unit's
+ // footprint (or simply wrong); a failed corridor search is cheap, so fall back to the
+ // unrestricted search rather than report no path
+ if (hadCorridor) {
+ m_zoneManager.setAllPassable();
+ pat = internalFindPath(obj, locomotorSet, from, rawTo);
+ if (pat!=NULL) {
+ return pat;
+ }
+ }
+
/* hierarchical build path code.
if (pat) {
@@ -6549,6 +6575,11 @@ Path *Pathfinder::internalFindPath( Object *obj, const LocomotorSet& locomotorSe
#if defined _DEBUG || defined _INTERNAL
Int startTimeMS = ::GetTickCount();
#endif
+ // release-visible timing (DebugLogFile.txt) for searches that cost a visible frame: how
+ // much was the A* itself and how much the path build/optimize after it
+ __int64 fpFreq64 = 0, fpT0 = 0;
+ QueryPerformanceFrequency((LARGE_INTEGER *)&fpFreq64);
+ QueryPerformanceCounter((LARGE_INTEGER *)&fpT0);
Bool centerInCell = true;
Int radius = 0;
if (obj) {
@@ -6719,7 +6750,23 @@ Path *Pathfinder::internalFindPath( Object *obj, const LocomotorSet& locomotorSe
m_isTunneling = false;
// construct and return path
+ __int64 fpT1 = 0, fpT2 = 0;
+ QueryPerformanceCounter((LARGE_INTEGER *)&fpT1);
Path *path = buildActualPath( obj, locomotorSet.getValidSurfaces(), from, goalCell, centerInCell, false );
+ QueryPerformanceCounter((LARGE_INTEGER *)&fpT2);
+ if (fpFreq64 > 0) {
+ Real searchMs = 1000.0f * (Real)(fpT1 - fpT0) / (Real)fpFreq64;
+ Real buildMs = 1000.0f * (Real)(fpT2 - fpT1) / (Real)fpFreq64;
+ if (searchMs + buildMs > 2.0f) {
+ Int nodes = 0;
+ if (path)
+ for (const PathNode *n = path->getFirstNode(); n; n = n->getNext())
+ nodes++;
+ DEBUG_LOG(("Pathfind frame %d '%s': %d cells, search %.1f ms, build+optimize %.1f ms, %d nodes, (%.0f,%.0f)->(%.0f,%.0f)\n",
+ TheGameLogic->getFrame(), obj ? obj->getTemplate()->getName().str() : "?",
+ cellCount, searchMs, buildMs, nodes, from->x, from->y, rawTo->x, rawTo->y));
+ }
+ }
parentCell->releaseInfo();
cleanOpenAndClosedLists();
return path;
@@ -6790,6 +6837,12 @@ Path *Pathfinder::internalFindPath( Object *obj, const LocomotorSet& locomotorSe
#endif
}
#endif
+ // release-visible trace (DebugLogFile.txt): a failed search is otherwise invisible outside
+ // debug builds, and "no path" reports need the cell count to tell a dry pool from a real
+ // dead end
+ DEBUG_LOG(("Pathfind FAILED frame %d unit '%s' from (%.0f,%.0f) to (%.0f,%.0f), %d cells\n",
+ TheGameLogic->getFrame(), obj ? obj->getTemplate()->getName().str() : "?",
+ from->x, from->y, to->x, to->y, cellCount));
m_isTunneling = false;
goalCell->releaseInfo();
cleanOpenAndClosedLists();
@@ -7435,27 +7488,47 @@ void Pathfinder::processHierarchicalCell( const ICoord2D &scanCell, const ICoord
return;
}
if (parentZone == m_zoneManager.getBlockZone(LOCOMOTORSURFACE_GROUND,
- crusher, scanCell.x, scanCell.y, m_map)) {
+ crusher, scanCell.x, scanCell.y, m_map)) {
PathfindCell *newCell = getCell(LAYER_GROUND, scanCell.x, scanCell.y);
+ if( newCell == NULL )
+ return;
+
+ //
+ // The shipped code returned here unless the cell ALREADY had an info record - which
+ // only a cell with a unit standing on it has - so the block-level search could never
+ // step out of a block and every long path fell back to a full-map A* (a goal 23 cells
+ // away across a ridge cost 55,000 cells). Allocate the record as the rest of this
+ // function does; release it again on the early-outs below so the pool does not bleed.
+ //
+ Bool allocatedHere = FALSE;
if( !newCell->hasInfo() )
{
- return;
+ if( !newCell->allocateInfo(scanCell) )
+ return;
+ allocatedHere = TRUE;
}
- if( newCell->getOpen() || newCell->getClosed() )
+ if( newCell->getOpen() || newCell->getClosed() )
return; // already looked at this one.
ICoord2D adjacentCell = scanCell;
//DEBUG_ASSERTCRASH(parentZone==newCell->getZone(), ("Different zones?"));
- if (parentZone!=newCell->getZone()) return;
+ if (parentZone!=newCell->getZone()) {
+ if (allocatedHere) newCell->releaseInfo();
+ return;
+ }
adjacentCell.x += delta.x;
adjacentCell.y += delta.y;
if (adjacentCell.x<m_extent.lo.x || adjacentCell.x>m_extent.hi.x ||
adjacentCell.y<m_extent.lo.y || adjacentCell.y>m_extent.hi.y) {
+ if (allocatedHere) newCell->releaseInfo();
return;
}
- PathfindCell *adjNewCell = getCell(LAYER_GROUND, adjacentCell.x, adjacentCell.y);
- if (adjNewCell->hasInfo() && (adjNewCell->getOpen() || adjNewCell->getClosed())) return; // already looked at this one.
+ PathfindCell *adjNewCell = getCell(LAYER_GROUND, adjacentCell.x, adjacentCell.y);
+ if (adjNewCell->hasInfo() && (adjNewCell->getOpen() || adjNewCell->getClosed())) {
+ if (allocatedHere) newCell->releaseInfo();
+ return; // already looked at this one.
+ }
zoneStorageType parentGlobalZone = m_zoneManager.getEffectiveZone(LOCOMOTORSURFACE_GROUND, crusher, parentZone);
/// @todo - somehow out of bounds or bogus newZone.
@@ -7463,6 +7536,7 @@ void Pathfinder::processHierarchicalCell( const ICoord2D &scanCell, const ICoord
crusher, adjacentCell.x, adjacentCell.y, m_map);
zoneStorageType newGlobalZone = m_zoneManager.getEffectiveZone(LOCOMOTORSURFACE_GROUND, crusher, newZone);
if (newGlobalZone != parentGlobalZone) {
+ if (allocatedHere) newCell->releaseInfo();
return; // can't step over. jba.
}
Int j;
@@ -7474,9 +7548,10 @@ void Pathfinder::processHierarchicalCell( const ICoord2D &scanCell, const ICoord
}
}
if (found) {
+ if (allocatedHere) newCell->releaseInfo();
return;
}
-
+
newCell->allocateInfo(scanCell);
if (!newCell->getClosed() && !newCell->getOpen()) {
m_closedList = newCell->putOnClosedList(m_closedList);
@@ -8845,7 +8920,11 @@ Path *Pathfinder::findClosestPath( Object *obj, const LocomotorSet& locomotorSet
m_zoneManager.setAllPassable(); // can't optimize.
} else {
m_zoneManager.clearPassableFlags();
- Path *hPat = findClosestHierarchicalPath(isHuman, locomotorSet, from, rawTo, false);
+ // The block corridor is used only by findPath (which falls back to a full search when
+ // the corridor fails). Here a misleading corridor would make "closest" land on the wrong
+ // side of an obstacle with no way to tell, so this stays on the full map - which is what
+ // it always was: the shipped hierarchical search never succeeded (processHierarchicalCell).
+ Path *hPat = NULL; // was findClosestHierarchicalPath(isHuman, locomotorSet, from, rawTo, false)
if (hPat) {
hPat->deleteInstance();
gotHierarchicalPath = true;
@@ -10725,7 +10804,9 @@ Path *Pathfinder::findAttackPath( const Object *obj, const LocomotorSet& locomot
isHuman = false; // computer gets to cheat.
}
m_zoneManager.clearPassableFlags();
- Path *hPat = findClosestHierarchicalPath(isHuman, locomotorSet, from, victimPos, isCrusher);
+ // full map, as it always effectively was (see the note in findClosestPath): an attack path
+ // confined to a wrong corridor would fail outright and the unit would refuse to approach
+ Path *hPat = NULL; // was findClosestHierarchicalPath(isHuman, locomotorSet, from, victimPos, isCrusher)
if (hPat) {
hPat->deleteInstance();
} else {
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
index e1f74692f..2ff28acfd 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
@@ -1527,20 +1527,9 @@ Bool AIUpdateInterface::processCollision(PhysicsBehavior *physics, Object *other
// If we are already pointing in the right direction, we may be stuck.
if (!otherMoving)
{
- // Before giving up and repathing, ask an idle allied blocker to step aside
- // (EA only did this for vehicles blocked by infantry). Same guards as the
- // 1.01 patch above: never disturb a busy unit or one using an ability.
- if (getObject()->getRelationship(other)==ALLIES && aiOther->isIdle() &&
- !other->testStatus(OBJECT_STATUS_IS_USING_ABILITY))
- {
- aiOther->aiMoveAwayFromUnit(getObject(), CMD_FROM_AI);
- if (getNumFramesBlocked() < LOGICFRAMES_PER_SECOND)
- {
- // Give it a moment to clear; AIStates' 2 second rule still repaths us
- // if it never does (immobile, held, or refused).
- return FALSE;
- }
- }
+ // (asking the idle allied blocker to step aside was tried here and reverted:
+ // at a group's destination every arriving unit shoved the parked ones, which
+ // shoved others - the group milled about and repathed without end)
// Intense logging jba
// DEBUG_LOG(("Blocked&Stuck !otherMoving\n"));
m_isBlockedAndStuck = TRUE;
@@ -1590,21 +1579,9 @@ Bool AIUpdateInterface::processCollision(PhysicsBehavior *physics, Object *other
Real dx = getObject()->getPosition()->x - otherPos.x;
Real dy = getObject()->getPosition()->y - otherPos.y;
Real curDSqr = dx*dx+dy*dy;
- // Two idle units count as stacked when their centers are within half of their
- // combined pathfind footprints — the old fixed half-cell test never separated
- // vehicles wider than a cell that ended up half overlapped. For a pair of
- // single-cell units this degenerates to exactly the old half-cell threshold,
- // and it stays below the legal parked separation, so no jitter for units
- // standing on properly allocated adjacent goal cells.
- Int sepCells = 1;
- Bool centerUnused;
- Int radiusCells;
- TheAI->pathfinder()->getRadiusAndCenter(getObject(), radiusCells, centerUnused);
- sepCells += radiusCells;
- TheAI->pathfinder()->getRadiusAndCenter(other, radiusCells, centerUnused);
- sepCells += radiusCells;
- Real minSep = 0.5f*PATHFIND_CELL_SIZE_F*sepCells;
- if (!otherMoving && curDSqr < minSep*minSep)
+ // (a footprint-scaled threshold was tried here and reverted: tanks parked closer than
+ // it by the group's own formation kept being pulled apart and never came to rest)
+ if (!otherMoving && curDSqr < PATHFIND_CELL_SIZE_F*PATHFIND_CELL_SIZE_F*0.25f)
{
if (this->getCurrentStateID() == AI_BUSY) {
return false;