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): take the tunnels when the goal cannot be walked to

Olcay Seygana88534474c84
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b274be80c..66bcaba07 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -676,6 +676,7 @@ A third pass went through the generals' powers, promotions and unit abilities.
- GLA units take the tunnels on their own. Give a move order across the map, and if the way through your network is any shorter than the walk, the whole selection goes down the tunnel nearest it and comes up at the one nearest where you sent it, then walks the rest. An attack move does the same and comes up still attack-moving, and fresh units heading for a rally point across the map take the tunnels too, fighting on the way if they would have walked there fighting. The tunnels used to have to be almost a third shorter before anybody took them, so fresh units with a tunnel beside their barracks and one beside the rally point mostly walked anyway. Passing through does not count as resting: only units you put inside yourself are healed. The group decides together: fifteen Rebels with six places already taken all go through, where a first version sent ten underground and walked the other five. The green order line shows it: one line to the tunnel they go down and one from the tunnel they come up at to where you sent them. Twelve Rebels crossing a small map with a tunnel at each end got there in 21 seconds instead of 50, four Scorpions in 9 instead of 21. A tunnel that has just been hit is skipped, so nobody surfaces into the fire. So is one still going up or being sold: a tunnel you had only just placed used to count straight away, and units sent across the map queued up at a building site. A tunnel rising again out of a GLA hole counts as going up until it is finished, and units underground when the tunnel they went down is destroyed no longer come up at one. The computer does it too: a GLA team losing a fight near one of its tunnels falls back underground and comes up at home, which happened 10 to 25 times a match in four Hard GLA mirrors on Twilight Flame, and an attack wave takes the tunnels when they come up nearer your base than its road does. And the computer now digs them on purpose: a tunnel at home, one at the expansion it holds, and one three quarters of the way down the road its next wave will take, as long as nothing it has seen there can shoot. It does not wait for a fat bank to do it: a tunnel costs 800 and pays back on the first wave. In four Hard GLA mirrors on Twilight Flame, 10 of 49 attack waves went underground, where none had before, and beaten teams fell back through a tunnel 250 times. Units you put in a tunnel yourself stay put, and a network they have filled is left alone: the order walks. With one place free, everybody still goes through it one after the other, since a unit passing through leaves the moment it arrives. Replays recorded before this change play out differently.
- Units a move order takes through the tunnels come out still selected. Going underground took each one off your selection and nothing put it back, so a group sent across the map surfaced as nobody's and had to be picked up again by hand. A trip you order by clicking a tunnel still leaves the selection the way it always has, and picking something else while they are underground means they stay out of it.
- Builders take the tunnels to work. A GLA Worker sent to build, repair or finish a structure goes through the network when that is shorter than the walk, and carries on with the job at the far end. The same holds for the builder of any side that has tunnels. Replays recorded before this change play out differently.
+- A move order onto ground your units cannot walk to goes through the tunnels. Up a cliff, across water with no bridge, the plateaus of the GLA campaign: with a tunnel on your side and one on theirs, the selection goes down and comes up there, where it used to walk to the foot of the cliff and stop, and you had to put them in the tunnel and empty it on the far side by hand. The tunnels picked are the nearest ones your units can actually walk to and from, so nobody comes up on an island next to where you sent them and stands there, and a tunnel across a river no longer counts as the way in for units on this bank. Four Rebels sent into a walled-off hollow on Dark Night, 700 short of it and 900 from the tunnel behind them, came up in it 49 seconds later. Replays recorded before this change play out differently.
## The power bar tells the truth
diff --git a/GeneralsMD/Code/Data/Scenarios/tunnelgap.txt b/GeneralsMD/Code/Data/Scenarios/tunnelgap.txt
new file mode 100644
index 000000000..f6df2a177
--- /dev/null
+++ b/GeneralsMD/Code/Data/Scenarios/tunnelgap.txt
@@ -0,0 +1,23 @@
+# Tunnel across a gap: a GLA move order onto ground the selection cannot walk to, with a tunnel on
+# each side.
+#
+# Dark Night has a pocket of open ground round 1300,1740, about 200 across, that a Rebel's
+# pathfinding puts in a terrain zone apart from the GLA start's: nothing on the start's side walks
+# into it. A tunnel stands in it and another behind the GLA start, and Rebels standing 710 from the
+# pocket are sent to a spot in it beside the tunnel. In straight lines the walk (710) is shorter
+# than the trip back to the start tunnel and out (about 980), so the old compare walked them at the
+# wall; arriving at all means the order went through the network. TunnelTracker::findTunnelShortcut
+# logs TUNNELSHORTCUT when it takes the tunnels because there is no walk.
+#
+# generals.exe -headless -quickstart -noshellmap -observer -multiInstance -noFPSLimit
+# -map "Maps\Dark Night\Dark Night.map" -autoskirmish 2 -takeover
+# -side 0 FactionGLA -side 1 FactionAmerica -seed 1 -maxframes 3000
+# -scenario tunnelgap -logPrefix tunnelgap_
+
+30 spawn 0 GLATunnelNetwork 1 start0:-200:0
+30 spawn 0 GLATunnelNetwork 1 1285 1725
+30 spawn 0 GLAInfantryRebel 4 900 1200 25
+# the order waits for the pathfinder's zones to take the tunnels in, which happens up to 300 frames
+# after a structure is placed; a mouth not yet in a zone answers yes to every reachability question
+400 playermove 0 GLAInfantryRebel 1330 1770
+400 arrive 0 GLAInfantryRebel 1330 1770 40
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/TunnelTracker.h b/GeneralsMD/Code/GameEngine/Include/Common/TunnelTracker.h
index 3b9a4bfdb..1c15ac8fe 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/TunnelTracker.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/TunnelTracker.h
@@ -69,10 +69,10 @@ public:
const std::list< ObjectID > *getContainerList() const {return &m_tunnelIDs;}
- Object *findQuietTunnelNear( const Coord3D *pos ) const; ///< the tunnel nearest pos that has not been shot at lately, or NULL
+ Object *findQuietTunnelNear( const Coord3D *pos, const Object *walker = NULL, const Coord3D *reachPos = NULL ) const; ///< the tunnel nearest pos that has not been shot at lately and, given a walker, that it can walk between and reachPos (pos by default), or NULL
Bool hasTunnelTraveller() const; ///< is anybody inside only passing through, and so about to free their place?
Int getResidentCount() const; ///< the ones inside to stay: what the network shows, since a traveller is gone the next frame
- Object *findTunnelShortcut( const Coord3D *from, const Coord3D *to, Real walk ) const; ///< the mouth to go down to get from `from` to `to` quicker than a walk `walk` long, or NULL
+ Object *findTunnelShortcut( const Object *walker, const Coord3D *from, const Coord3D *to, Real walk ) const; ///< the mouth for `walker` to go down to get from `from` to `to` quicker than a walk `walk` long, or where no walk gets there, or NULL
Object *getCurNemesis(void);
void updateNemesis(const Object *target);
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp
index 3f256a4a9..e3ef675b8 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp
@@ -280,8 +280,14 @@ void TunnelTracker::onTunnelDestroyed( const Object *deadTunnel )
// into whatever is shooting at it.
static const UnsignedInt TUNNEL_UNDER_FIRE_FRAMES = 2 * LOGICFRAMES_PER_SECOND;
-Object *TunnelTracker::findQuietTunnelNear( const Coord3D *pos ) const
+// With a walker, a mouth that is on the other side of a cliff or a river from reachPos (pos when
+// none is given) is no mouth at all. The question is asked in terrain zones, which read a structure
+// as the ground under it: the mouth is one, and a base wall is not what keeps a walk from getting
+// somewhere.
+Object *TunnelTracker::findQuietTunnelNear( const Coord3D *pos, const Object *walker, const Coord3D *reachPos ) const
{
+ if( reachPos == NULL )
+ reachPos = pos;
const UnsignedInt now = TheGameLogic->getFrame();
Object *nearest = NULL;
Real nearestSqr = 0.0f;
@@ -299,11 +305,15 @@ Object *TunnelTracker::findQuietTunnelNear( const Coord3D *pos ) const
continue;
const Real distSqr = ThePartitionManager->getDistanceSquared( tunnel, pos, FROM_CENTER_2D );
- if( nearest == NULL || distSqr < nearestSqr )
- {
- nearest = tunnel;
- nearestSqr = distSqr;
- }
+ if( nearest != NULL && distSqr >= nearestSqr )
+ continue;
+
+ if( walker != NULL &&
+ !TheAI->pathfinder()->clientSafeQuickDoesPathExistForUI( walker->getAI()->getLocomotorSet(), tunnel->getPosition(), reachPos ) )
+ continue;
+
+ nearest = tunnel;
+ nearestSqr = distSqr;
}
return nearest;
}
@@ -343,20 +353,34 @@ Int TunnelTracker::getResidentCount() const
Any way through that is shorter than the walk is taken. It used to have to come in under 70% of
it, and a rally point with a tunnel beside the factory and another beside the point still walked.
- The legs to and from the tunnels are straight lines. `walk` is the caller's to measure: the
- straight line for a move order, the length of the path for a wave that follows one.
+ The mouths are the nearest ones the walker's feet reach: the way in nearest `from`, the way out
+ nearest `to`. The ground is asked about from where the walker stands, not from `from`: the
+ middle of a group can fall on a cliff face, which belongs to no walk at all. When there is no walk at all - the goal up a cliff, across water with no bridge, the GLA
+ campaign's plateaus - any such pair wins, and before that the order walked into the wall while the
+ tunnels it was meant to use stood on both sides of it. Otherwise the legs to and from the tunnels
+ are straight lines, and `walk` is the caller's to measure: the straight line for a move order, the
+ length of the path for a wave that follows one. The trip leaves by the same far mouth
+ (AIUpdateInterface::update asks findQuietTunnelNear the same question).
ponytail: straight lines, not path lengths; a tunnel across a river the walk has to go round
- looks no better than one across open ground. A path search when that matters. */
-Object *TunnelTracker::findTunnelShortcut( const Coord3D *from, const Coord3D *to, Real walk ) const
+ looks no better than one across open ground. A path search when that matters. The walker is
+ one member of a group, so a mixed group is judged by that member's feet. */
+Object *TunnelTracker::findTunnelShortcut( const Object *walker, const Coord3D *from, const Coord3D *to, Real walk ) const
{
if( (Int)getContainCount() >= getContainMax() )
return NULL;
- Object *entrance = findQuietTunnelNear( from );
- Object *exit = findQuietTunnelNear( to );
- if( entrance == NULL || exit == entrance )
+ Object *entrance = findQuietTunnelNear( from, walker, walker->getPosition() );
+ Object *exit = findQuietTunnelNear( to, walker );
+ if( entrance == NULL || exit == NULL || exit == entrance )
return NULL;
+ if( !TheAI->pathfinder()->clientSafeQuickDoesPathExistForUI( walker->getAI()->getLocomotorSet(), walker->getPosition(), to ) )
+ {
+ DEBUG_LOG(("TUNNELSHORTCUT frame %d: %d cannot walk to (%.0f,%.0f), goes down %d and up %d\n", TheGameLogic->getFrame(),
+ walker->getID(), to->x, to->y, entrance->getID(), exit->getID()));
+ return entrance;
+ }
+
const Real toEntrance = (Real)sqrt( ThePartitionManager->getDistanceSquared( entrance, from, FROM_CENTER_2D ) );
const Real fromExit = (Real)sqrt( ThePartitionManager->getDistanceSquared( exit, to, FROM_CENTER_2D ) );
if( toEntrance + fromExit >= walk )
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index aa148890c..041b5466e 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -5779,7 +5779,7 @@ void InGameUI::collectOrderHints( void )
hint.onObject = TRUE;
addOrderHint( hint, previous );
- const Object *exit = local->getTunnelSystem()->findQuietTunnelNear( ai->getTunnelTripGoal() );
+ const Object *exit = local->getTunnelSystem()->findQuietTunnelNear( ai->getTunnelTripGoal(), obj );
hint.from = ( exit != NULL ) ? *exit->getPosition() : hint.to;
hint.to = *ai->getTunnelTripGoal();
hint.onObject = FALSE;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
index c68f4eee0..bf59b6fe4 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
@@ -1978,6 +1978,16 @@ static void crowdSeedLanesAlongWaypoint( AIGroup *group, std::list<Object *>& me
crowdSeedLanes( members, center, crowdWaypointAim( center, way ) );
}
+/** The member whose feet decide a tunnel shortcut for the group (TunnelTracker::findTunnelShortcut):
+ the first that moves at all, since a selection can hold a structure. */
+static const Object *tunnelWalker( const std::list<Object *> &members )
+{
+ for( std::list<Object *>::const_iterator it = members.begin(); it != members.end(); ++it )
+ if( (*it)->getAI() != NULL && !(*it)->isKindOf( KINDOF_IMMOBILE ) )
+ return *it;
+ return NULL;
+}
+
/**
* Move to given position(s)
*/
@@ -2371,11 +2381,12 @@ void AIGroup::groupMoveToPosition( const Coord3D *p_posIn, Bool addWaypoint, Com
// through the tunnel network when that is shorter; see TunnelTracker::findTunnelShortcut
Object *tunnelEntrance = NULL;
- if (gatherOnPoint && iter->first() != NULL)
+ const Object *walker = gatherOnPoint ? tunnelWalker( m_memberList ) : NULL;
+ if (walker != NULL)
{
const Real walkX = goalPos.x - groupCenter.x;
const Real walkY = goalPos.y - groupCenter.y;
- tunnelEntrance = iter->first()->getControllingPlayer()->getTunnelSystem()->findTunnelShortcut( &groupCenter, &goalPos,
+ tunnelEntrance = walker->getControllingPlayer()->getTunnelSystem()->findTunnelShortcut( walker, &groupCenter, &goalPos,
(Real)sqrt( walkX * walkX + walkY * walkY ) );
}
@@ -3064,11 +3075,12 @@ void AIGroup::groupAttackMoveToPosition( const Coord3D *pos, Int maxShotsToFire,
// TunnelTracker::findTunnelShortcut. A player's order or a computer's, never a script's: a mission
// script sends its units the way the mission was written for.
Object *tunnelEntrance = NULL;
- if ((cmdSource == CMD_FROM_PLAYER || cmdSource == CMD_FROM_AI) && !m_memberList.empty())
+ const Object *walker = (cmdSource == CMD_FROM_PLAYER || cmdSource == CMD_FROM_AI) ? tunnelWalker( m_memberList ) : NULL;
+ if (walker != NULL)
{
const Real walkX = pos->x - center.x;
const Real walkY = pos->y - center.y;
- tunnelEntrance = m_memberList.front()->getControllingPlayer()->getTunnelSystem()->findTunnelShortcut( &center, pos,
+ tunnelEntrance = walker->getControllingPlayer()->getTunnelSystem()->findTunnelShortcut( walker, &center, pos,
(Real)sqrt( walkX * walkX + walkY * walkY ) );
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 27b77c53f..0f269879e 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -7799,14 +7799,16 @@ void AIPlayer::sendWaveThroughTunnels( AIGroup *wave, const Coord3D *center, Way
end = next;
}
- Object *entrance = m_player->getTunnelSystem()->findTunnelShortcut( center, end->getLocation(), walk );
+ // a copy: a member that goes into the tunnel leaves the group
+ const VecObjectID members = wave->getAllIDs();
+ const Object *walker = members.empty() ? NULL : TheGameLogic->findObjectByID( members.front() );
+ Object *entrance = walker == NULL || walker->getAI() == NULL ? NULL
+ : m_player->getTunnelSystem()->findTunnelShortcut( walker, center, end->getLocation(), walk );
if( entrance == NULL )
return;
- // a copy: a member that goes into the tunnel leaves the group
Int sent = 0;
const Int waveSize = wave->getCount();
- const VecObjectID members = wave->getAllIDs();
for( VecObjectID::const_iterator it = members.begin(); it != members.end(); ++it )
{
Object *obj = TheGameLogic->findObjectByID( *it );
@@ -9235,6 +9237,7 @@ void AIPlayer::doRetreats( void )
Real count = 0.0f;
Coord3D centre;
centre.zero();
+ const Object *walker = NULL; // whose feet judge the tunnel home
for( DLINK_ITERATOR<Object> objIter = team->iterate_TeamMemberList(); !objIter.done(); objIter.advance() )
{
Object *obj = objIter.cur();
@@ -9242,6 +9245,8 @@ void AIPlayer::doRetreats( void )
continue;
if( obj->isKindOf( KINDOF_PROJECTILE ) || !retreatCanOrderHome( obj ) )
continue; // only the units that could actually be pulled out place the fight
+ if( walker == NULL )
+ walker = obj;
centre.x += obj->getPosition()->x;
centre.y += obj->getPosition()->y;
count += 1.0f;
@@ -9296,7 +9301,7 @@ void AIPlayer::doRetreats( void )
fallback.z = TheTerrainLogic->getGroundHeight( fallback.x, fallback.y );
const Real homeX = m_baseCenter.x - centre.x;
const Real homeY = m_baseCenter.y - centre.y;
- Object *homeTunnel = shortOfHome ? NULL : m_player->getTunnelSystem()->findTunnelShortcut( &centre, &m_baseCenter,
+ Object *homeTunnel = shortOfHome ? NULL : m_player->getTunnelSystem()->findTunnelShortcut( walker, &centre, &m_baseCenter,
(Real)sqrt( homeX * homeX + homeY * homeY ) );
if( homeTunnel )
DEBUG_LOG(("AI RETREAT frame %d player %d falls back through tunnel %d\n", TheGameLogic->getFrame(),
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
index 2fd3dfd0e..f9bb20d33 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
@@ -1279,7 +1279,7 @@ UpdateSleepTime AIUpdateInterface::update( void )
// a rally point across the map is reached through the tunnels when they are shorter
const Real walkX = rallyPoint.x - getObject()->getPosition()->x;
const Real walkY = rallyPoint.y - getObject()->getPosition()->y;
- Object *entrance = getObject()->getControllingPlayer()->getTunnelSystem()->findTunnelShortcut( getObject()->getPosition(),
+ Object *entrance = getObject()->getControllingPlayer()->getTunnelSystem()->findTunnelShortcut( getObject(), getObject()->getPosition(),
&rallyPoint, (Real)sqrt( walkX * walkX + walkY * walkY ) );
const Bool tunnelled = entrance != NULL
&& takeTunnelTrip( entrance, &rallyPoint, fights ? TUNNEL_TRIP_ATTACK_MOVE : TUNNEL_TRIP_MOVE, CMD_FROM_AI );
@@ -1306,15 +1306,16 @@ UpdateSleepTime AIUpdateInterface::update( void )
}
// A move order that a tunnel shortened: idle inside the network means the enter just finished, so
- // leave by the mouth nearest the goal; idle outside means the exit is done, or the enter gave up,
- // and either way what is left is the walk to the goal.
+ // leave by the mouth nearest the goal that the goal can be walked to from, the one the shortcut was
+ // chosen for; idle outside means the exit is done, or the enter gave up, and either way what is left
+ // is the walk to the goal.
if (m_hasTunnelTrip && getAIStateType() == AI_IDLE)
{
Object *me = getObject();
Object *tunnel = me->getContainedBy();
if (tunnel != NULL && tunnel->getContain()->isTunnelContain())
{
- Object *exit = me->getControllingPlayer()->getTunnelSystem()->findQuietTunnelNear( &m_tunnelTripGoal );
+ Object *exit = me->getControllingPlayer()->getTunnelSystem()->findQuietTunnelNear( &m_tunnelTripGoal, me );
privateExit( exit != NULL ? exit : tunnel, CMD_FROM_AI );
}
else
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp
index dc50338b0..610cfe8e9 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp
@@ -264,20 +264,10 @@ StateReturnType DozerActionPickActionPosState::update( void )
const Real walkX = goalPos.x - dozer->getPosition()->x;
const Real walkY = goalPos.y - dozer->getPosition()->y;
const TunnelTracker *tunnels = dozer->getControllingPlayer()->getTunnelSystem();
- Object *entrance = tunnels->findTunnelShortcut( dozer->getPosition(), &goalPos,
+ Object *entrance = tunnels->findTunnelShortcut( dozer, dozer->getPosition(), &goalPos,
(Real)sqrt( walkX * walkX + walkY * walkY ) );
- //
- // The shortcut is measured in straight lines, and the site was only ever known to be reachable
- // from where the builder stood. A far mouth on the other side of a cliff or on another island
- // would leave it walking at a wall with the job still in hand, so the ground between that mouth
- // and the site is asked about first. The mouth is a structure, which the zone check reads as the
- // terrain under it.
- //
- if( entrance != NULL &&
- TheAI->pathfinder()->clientSafeQuickDoesPathExist( ai->getLocomotorSet(),
- tunnels->findQuietTunnelNear( &goalPos )->getPosition(), &goalPos ) &&
- ai->takeTunnelTrip( entrance, &goalPos, TUNNEL_TRIP_MOVE, CMD_FROM_AI ) )
+ if( entrance != NULL && ai->takeTunnelTrip( entrance, &goalPos, TUNNEL_TRIP_MOVE, CMD_FROM_AI ) )
{
DEBUG_LOG(("TUNNELBUILD frame %d: builder %d takes tunnel %d to task %d on %d\n", TheGameLogic->getFrame(),
dozer->getID(), entrance->getID(), m_task, goalObject->getID()));