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.

feat(gameengine): let builders walk through structures still going up

Olcay Seygan40799b8dec74
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 943b35476..40d23f531 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -121,6 +121,8 @@ found and fixed — EA's own, not port damage.**
- Your units walk straight over a plan — nothing solid is there until the builder starts work.
- The plan turns solid the instant the first work goes in, so no building goes up inside its own ghost.
- The builder starts work from where it reaches the site instead of shuffling into place first.
+- Dozers and workers walk through anything still going up, so they never shut themselves in behind
+ their own work.
- The stop button prints its key on it, like the build buttons do.
## Bonuses that come and go when they should
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h
index 4a372c23b..1e30496a0 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h
@@ -767,6 +767,9 @@ public:
void adjustPathUsage( const PathNode *from, const PathNode *to, Int delta ); ///< [from, to) raw nodes, +1/-1
void setIgnoreObstacleID( ObjectID objID ); ///< if non-zero, the pathfinder will ignore the given obstacle
+ void setIgnoreUnderConstruction( Bool ignore ); ///< if true, half-built structures are not obstacles to this search
+ Bool builderMayCross( const PathfindCell *cell ) const; ///< is this obstacle cell a structure the builder now searching may walk over?
+ Bool builderMayCrossPinch( Int cellX, Int cellY, PathfindLayerEnum layer ); ///< is this cell pinched only by a site that builder may walk over?
Bool validMovementPosition( Bool isCrusher, LocomotorSurfaceTypeMask acceptableSurfaces, PathfindCell *toCell, PathfindCell *fromCell = NULL ); ///< Return true if given position is a valid movement location
Bool validMovementPosition( Bool isCrusher, PathfindLayerEnum layer, const LocomotorSet& locomotorSet, Int x, Int y ); ///< Return true if given position is a valid movement location
@@ -948,6 +951,7 @@ private:
Path *debugPath; ///< Used for visual debugging
ObjectID m_ignoreObstacleID; ///< Ignore the given obstacle
+ Bool m_ignoreUnderConstruction; ///< Ignore every structure that is still going up (builders only)
PathfindZoneManager m_zoneManager; ///< Handles the pathfind zones.
@@ -979,6 +983,11 @@ inline void Pathfinder::setIgnoreObstacleID( ObjectID objID )
m_ignoreObstacleID = objID;
}
+inline void Pathfinder::setIgnoreUnderConstruction( Bool ignore )
+{
+ m_ignoreUnderConstruction = ignore;
+}
+
inline void Pathfinder::worldToGrid( const Coord3D *pos, ICoord2D *cellIndex )
{
cellIndex->x = REAL_TO_INT(pos->x/PATHFIND_CELL_SIZE);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
index 1776b45f2..f1109ead2 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
@@ -4206,6 +4206,7 @@ void Pathfinder::reset( void )
m_closedList = NULL;
m_ignoreObstacleID = INVALID_ID;
+ m_ignoreUnderConstruction = false;
m_isTunneling = false;
m_moveAlliesDepth = 0;
@@ -5196,10 +5197,52 @@ void Pathfinder::cleanOpenAndClosedLists(void) {
}
+//-------------------------------------------------------------------------------------------------
+/** Is this obstacle cell one the builder now searching is allowed to walk over - a structure that
+ * is still going up? It is the one unit that has to be able to leave a site it has just filled
+ * in: a row of structures laid across the only way out of a base used to shut the dozer in behind
+ * its own work, and it can hardly demolish its way back out. Only builders get this,
+ * setIgnoreUnderConstruction is off for everyone else.
+ *
+ * Asked in two places that have to agree - whether the cell may be entered at all, and what
+ * entering it costs. If only the first said yes the search would open the cell and then price it
+ * like a wall, and "allowed through" comes out as "walks all the way around it". */
+//-------------------------------------------------------------------------------------------------
+Bool Pathfinder::builderMayCross( const PathfindCell *cell ) const
+{
+ if (!m_ignoreUnderConstruction || cell == NULL || cell->getType() != PathfindCell::CELL_OBSTACLE)
+ return false;
+
+ Object *obstacle = TheGameLogic->findObjectByID( cell->getObstacleID() );
+ return obstacle != NULL && obstacle->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION );
+}
+
+//-------------------------------------------------------------------------------------------------
+/** A footprint pinches the ring of clear cells around itself (MARK_BORDER_PINCHED in
+ * internal_classifyObjectFootprint), and a pinched cell is where Path::optimize stops
+ * straightening. That ring is what a building leans on to keep straightened lines out of itself -
+ * the line test has no cell-type check of its own - so it has to be lifted, and only lifted, where
+ * the building is one this builder may walk over. Left in place, the crossing came out of the
+ * search as the raw cell staircase, and a turn every cell is the stall the player sees. */
+//-------------------------------------------------------------------------------------------------
+Bool Pathfinder::builderMayCrossPinch( Int cellX, Int cellY, PathfindLayerEnum layer )
+{
+ if (!m_ignoreUnderConstruction)
+ return false;
+
+ // the orthogonal neighbours, which are the ones that set the border pinch
+ if (builderMayCross( getCell( layer, cellX - 1, cellY ) )) return true;
+ if (builderMayCross( getCell( layer, cellX + 1, cellY ) )) return true;
+ if (builderMayCross( getCell( layer, cellX, cellY - 1 ) )) return true;
+ if (builderMayCross( getCell( layer, cellX, cellY + 1 ) )) return true;
+
+ return false;
+}
+
//
// Return true if we can move onto this position
//
-Bool Pathfinder::validMovementPosition( Bool isCrusher, LocomotorSurfaceTypeMask acceptableSurfaces,
+Bool Pathfinder::validMovementPosition( Bool isCrusher, LocomotorSurfaceTypeMask acceptableSurfaces,
PathfindCell *toCell, PathfindCell *fromCell )
{
if (toCell == NULL)
@@ -5210,6 +5253,10 @@ Bool Pathfinder::validMovementPosition( Bool isCrusher, LocomotorSurfaceTypeMask
if (toCell->isObstaclePresent( m_ignoreObstacleID ))
return true;
+ // a builder walks through anything that is still going up
+ if (builderMayCross( toCell ))
+ return true;
+
if (isCrusher && toCell->isObstacleFence()) {
return true;
}
@@ -6794,7 +6841,13 @@ Int Pathfinder::examineNeighboringCells(PathfindCell *parentCell, PathfindCell *
if (notZonePassable) {
newCostSoFar += 100*COST_ORTHOGONAL;
}
- if (newCell->getType()==PathfindCell::CELL_OBSTACLE) {
+ //
+ // A hundred cells of detour per obstacle cell - it buys "go round unless there is no
+ // way round at all", which is right for a wall and wrong for a site the builder is
+ // entitled to cross: a footprint is many cells, so the builder was walking the length
+ // of the base to avoid its own half-built barracks. It crosses at plain ground cost.
+ //
+ if (newCell->getType()==PathfindCell::CELL_OBSTACLE && !builderMayCross(newCell)) {
newCostSoFar += 100*COST_ORTHOGONAL;
}
// check if this neighbor cell is already on the open (waiting to be tried)
@@ -7039,7 +7092,9 @@ Path *Pathfinder::internalFindPath( Object *obj, const LocomotorSet& locomotorSe
return NULL;
}
- if (goalCell->isObstaclePresent(m_ignoreObstacleID) || m_isTunneling) {
+ // the same screen again, inside the search: a builder crossing its own site is the third way of
+ // "moving into or out of a building" that this test already knows about
+ if (goalCell->isObstaclePresent(m_ignoreObstacleID) || m_isTunneling || m_ignoreUnderConstruction) {
// Use terrain zones instead of building zones, since we are moving into or out of a building.
zone2 = m_zoneManager.getEffectiveTerrainZone(zone2);
zone2 = m_zoneManager.getEffectiveZone(locomotorSet.getValidSurfaces(), isCrusher, zone2);
@@ -7309,7 +7364,15 @@ Int Pathfinder::clearCellForDiameter(Bool crusher, Int cellX, Int cellY, Pathfin
if (!crusher) {
clear = false;
}
- } else {
+ } else if (!builderMayCross(cell)) {
+ //
+ // This width test is what Path::optimizeGroundPath asks before it straightens
+ // a leg (buildGroundPath -> isGroundPathPassable -> here), so a site the
+ // builder is allowed to walk over has to read as clear here too. Left as an
+ // obstacle, no leg across it could ever be straightened and the builder was
+ // handed the raw cell-by-cell staircase the search came up with - a turn
+ // every cell, which is the stall at each crossing.
+ //
clear = false;
}
} else {
@@ -8644,8 +8707,16 @@ Bool Pathfinder::clientSafeQuickDoesPathExist( const LocomotorSet& locomotorSet,
if (goalCell->getType()==PathfindCell::CELL_CLIFF) {
return false; // No goals on cliffs.
}
- Bool doingTerrainZone = false;
- zone1 = m_zoneManager.getEffectiveZone(locomotorSet.getValidSurfaces(), false, parentCell->getZone());
+ //
+ // This zone screen is the front door of findPath and it runs before a single cell is looked at,
+ // so for a builder it has to be asked in terrain zones - the ones that merge straight across a
+ // structure. Otherwise the row of half-built structures the builder is allowed to walk through
+ // still cuts the base into two zones here, and the search it would have won is refused before
+ // it starts. Terrain zones ignore finished structures too, so this only makes the screen
+ // optimistic: the cell search still turns those away, one A* later.
+ //
+ Bool doingTerrainZone = m_ignoreUnderConstruction;
+ zone1 = m_zoneManager.getEffectiveZone(locomotorSet.getValidSurfaces(), false, parentCell->getZone());
if (parentCell->getType() == PathfindCell::CELL_OBSTACLE) {
doingTerrainZone = true;
@@ -10232,7 +10303,8 @@ struct LinePassableStruct
return 1; // bail out
}
- if (!d->allowPinched && to->getPinched()) {
+ if (!d->allowPinched && to->getPinched() &&
+ !pathfinder->builderMayCrossPinch( to_x, to_y, to->getLayer() )) {
return 1; // bail out.
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
index 38284d7d1..283d16a6b 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
@@ -1727,13 +1727,17 @@ Bool AIUpdateInterface::computePath( PathfindServicesInterface *pathServices, Co
Path *theNewPath = NULL;
TheAI->pathfinder()->setIgnoreObstacleID( getIgnoredObstacleID() );
+ TheAI->pathfinder()->setIgnoreUnderConstruction( getDozerAIInterface() != NULL );
Coord3D originalDestination = *destination;
// sanity check - if destination cell is invalid, don't bother pathing
LocomotorSurfaceTypeMask surfaces = m_locomotorSet.getValidSurfaces();
- if (!m_isFinalGoal && TheAI->pathfinder()->isLinePassable( getObject(), surfaces,
+ if (!m_isFinalGoal && TheAI->pathfinder()->isLinePassable( getObject(), surfaces,
getObject()->getLayer(), *getObject()->getPosition(), originalDestination, false, true)) {
+ // this way out skips the reset at the bottom, and a flag left on would answer for whoever
+ // asks the pathfinder next
+ TheAI->pathfinder()->setIgnoreUnderConstruction( FALSE );
return computeQuickPath(destination);
}
@@ -1760,6 +1764,7 @@ Bool AIUpdateInterface::computePath( PathfindServicesInterface *pathServices, Co
m_retryPath = true;
}
TheAI->pathfinder()->setIgnoreObstacleID( INVALID_ID );
+ TheAI->pathfinder()->setIgnoreUnderConstruction( FALSE );
if (theNewPath) {
// destroy previous path
destroyPath();
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/PhysicsUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/PhysicsUpdate.cpp
index 5b518e0cc..dd09fb2be 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/PhysicsUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/PhysicsUpdate.cpp
@@ -1237,6 +1237,23 @@ void PhysicsBehavior::onCollide( Object *other, const Coord3D *loc, const Coord3
return;
}
+ //
+ // A builder walks through anything that is still going up - the pathfinder routes it straight
+ // over half-built structures (Pathfinder::validMovementPosition), and without this the bounce
+ // off an immobile object below would shove it back out of the path it was given. Both
+ // directions, because the collision is reported to each side separately.
+ //
+ if (ai != NULL && ai->getDozerAIInterface() != NULL &&
+ other->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION ))
+ {
+ return;
+ }
+ if (aiOther != NULL && aiOther->getDozerAIInterface() != NULL &&
+ obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION ))
+ {
+ return;
+ }
+
if (isIgnoringCollisionsWith(other->getID()))
{
return;