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): send the whole group through the tunnel, draw both legs

Olcay Seygan0bfbdda4376a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bd5d7e9c6..20aa321a6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -398,7 +398,7 @@ found and fixed — EA's own, not port damage.**
- Circular range checks are circles now, not the square drawn around them.
- A tunnel network heals at one rate, however many entrances it has.
-- GLA units take the tunnels on their own. Give a move order across the map and every unit whose way through your network is clearly shorter than the walk goes down the nearest tunnel and comes up at the one nearest where you sent it, then walks the rest. 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. 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.
+- GLA units take the tunnels on their own. Give a move order across the map, and if the way through your network is clearly 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. 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. 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.
## The power bar tells the truth
diff --git a/GeneralsMD/Code/Data/Scenarios/tunnelsplit.txt b/GeneralsMD/Code/Data/Scenarios/tunnelsplit.txt
new file mode 100644
index 000000000..734713749
--- /dev/null
+++ b/GeneralsMD/Code/Data/Scenarios/tunnelsplit.txt
@@ -0,0 +1,21 @@
+# Tunnel shortcut, the owner's case: fifteen selected, six already inside to stay, and a trip short
+# enough that the old member-by-member choice sent the front of the group underground and walked
+# the back of it. The group decides once now, and every member goes through.
+#
+# Measured 2026-09-22 with seed 3: the member-by-member choice charged each rebel a body length for
+# every one sent in before it, and on this trip (about 900 long, 430 through the tunnels) it sent ten
+# through and walked five. The group choice sends all fifteen; the last is in after 820 frames.
+#
+# generals.exe -headless -quickstart -noshellmap -observer -multiInstance -noFPSLimit
+# -randommap 3 2 small -autoskirmish 2 -takeover -side 0 FactionGLA -side 1 FactionAmerica
+# -seed 3 -maxframes 4800 -scenario tunnelsplit -logPrefix tunnelsplit_
+
+30 spawn 0 GLATunnelNetwork 1 start0:0:300
+30 spawn 0 GLATunnelNetwork 1 800 1000
+# two here and the two each tunnel comes with make six
+30 spawn 0 GLAInfantryTunnelDefender 2 start0:0:420 25
+60 enter 0 GLAInfantryTunnelDefender 0 GLATunnelNetwork
+
+2940 spawn 0 GLAInfantryRebel 15 start0:250:300 25
+3000 playermove 0 GLAInfantryRebel 650 900
+3000 arrive 0 GLAInfantryRebel 650 900 150
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
index 34fe82733..ab0aa4986 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
@@ -741,6 +741,7 @@ public:
// this is intended for use ONLY by AIGroup's move order, which sends a unit through its tunnel network.
void friend_setTunnelTrip( const Coord3D *goal );
Bool hasTunnelTrip() const { return m_hasTunnelTrip; } ///< on its way through the tunnel network to a move order's goal
+ const Coord3D *getTunnelTripGoal() const { return &m_tunnelTripGoal; } ///< where the trip ends, once out of the far mouth
#if defined(_DEBUG) || defined(_INTERNAL)
inline const Coord3D *friend_getRequestedDestination() const { return &m_requestedDestination; }
inline const Coord3D *friend_getRequestedDestination2() const { return &m_requestedDestination2; }
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 0d705150f..e9326a7a0 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -47,6 +47,7 @@
#include "Common/Team.h"
#include "Common/ThingFactory.h"
#include "Common/ThingTemplate.h"
+#include "Common/TunnelTracker.h"
#include "Common/BuildAssistant.h"
#include "Common/Recorder.h"
#include "Common/BuildAssistant.h"
@@ -4123,6 +4124,28 @@ void InGameUI::collectOrderHints( void )
break;
case AI_ENTER:
+ {
+ // A move order that is going through the tunnels is still a move: one green thread to
+ // the mouth it goes down and one from the mouth it comes up at to where it was sent.
+ // The far mouth is worked out the way the unit will work it out when it gets there.
+ const Object *entrance = ai->getGoalObject();
+ if( ai->hasTunnelTrip() && entrance != NULL )
+ {
+ hint.kind = ORDER_HINT_MOVE;
+ hint.from = *obj->getPosition();
+ hint.to = *entrance->getPosition();
+ addOrderHint( hint, previous );
+
+ const Object *exit = local->getTunnelSystem()->findQuietTunnelNear( ai->getTunnelTripGoal() );
+ hint.from = ( exit != NULL ) ? *exit->getPosition() : hint.to;
+ hint.to = *ai->getTunnelTripGoal();
+ addOrderHint( hint, previous );
+ continue;
+ }
+ hint.kind = ORDER_HINT_ENTER;
+ break;
+ }
+
case AI_RAPPEL_INTO:
case AI_COMBATDROP:
hint.kind = ORDER_HINT_ENTER;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
index f366946f2..6d108161a 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
@@ -1978,44 +1978,45 @@ static void crowdSeedLanesAlongWaypoint( AIGroup *group, std::list<Object *>& me
static const Real TUNNEL_SHORTCUT_SHARE = 0.7f; ///< the longest a way through the tunnels may be, as a share of the walk
-/** The tunnel mouth a player's move order sends `unit` into on its way to `dest`, or NULL when it
- walks. A network with no free place is not looked at at all: whatever fills it was put there to
- stay. With one free place the network still carries everybody, one after the other, since a unit
- passing through leaves by the far mouth on the frame after it arrives; what that costs is the
- queue at the near mouth, a body length for every member this order already sent there.
+/** The tunnel mouth a player's move order sends its group into on the way from `center` to `goal`,
+ or NULL when the group walks. The group decides once, from its middle, and every member that can
+ take a tunnel takes it: deciding member by member split a selection, the back of it walking while
+ the front went underground. A network with no free place is not looked at at all, whatever fills
+ it was put there to stay. One free place is enough for the whole group, since a unit passing
+ through leaves by the far mouth the frame after it arrives: sixteen went through one place as
+ fast as through ten (tunnelqueue.txt against tunnelshortcut.txt).
All three legs are straight lines, like the walk they are measured against.
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 per member when that matters. */
-static Object *pickTunnelEntrance( Object *unit, const Coord3D *dest, std::vector<ObjectID> &sentThrough )
+ looks no better than one across open ground. A path search when that matters. */
+static Object *pickTunnelEntrance( const TunnelTracker *network, const Coord3D *center, const Coord3D *goal )
{
- if (!unit->getAIUpdateInterface()->isDoingGroundMovement())
- return NULL;
-
- const TunnelTracker *network = unit->getControllingPlayer()->getTunnelSystem();
if ((Int)network->getContainCount() >= network->getContainMax())
return NULL;
- Object *entrance = network->findQuietTunnelNear( unit->getPosition() );
- Object *exit = network->findQuietTunnelNear( dest );
+ Object *entrance = network->findQuietTunnelNear( center );
+ Object *exit = network->findQuietTunnelNear( goal );
if (entrance == NULL || exit == entrance)
return NULL;
- if (!TheActionManager->canEnterObject( unit, entrance, CMD_FROM_PLAYER, DONT_CHECK_CAPACITY ))
- return NULL;
-
- const Int queued = (Int)std::count( sentThrough.begin(), sentThrough.end(), entrance->getID() );
- const Real queue = queued * 2.0f * unit->getGeometryInfo().getBoundingCircleRadius();
- const Real toEntrance = (Real)sqrt( ThePartitionManager->getDistanceSquared( unit, entrance, FROM_CENTER_2D ) );
- const Real fromExit = (Real)sqrt( ThePartitionManager->getDistanceSquared( exit, dest, FROM_CENTER_2D ) );
- const Real walk = (Real)sqrt( ThePartitionManager->getDistanceSquared( unit, dest, FROM_CENTER_2D ) );
- if (toEntrance + queue + fromExit > walk * TUNNEL_SHORTCUT_SHARE)
+ const Real toEntrance = (Real)sqrt( ThePartitionManager->getDistanceSquared( entrance, center, FROM_CENTER_2D ) );
+ const Real fromExit = (Real)sqrt( ThePartitionManager->getDistanceSquared( exit, goal, FROM_CENTER_2D ) );
+ const Real walkX = goal->x - center->x;
+ const Real walkY = goal->y - center->y;
+ const Real walk = (Real)sqrt( walkX * walkX + walkY * walkY );
+ if (toEntrance + fromExit > walk * TUNNEL_SHORTCUT_SHARE)
return NULL;
- sentThrough.push_back( entrance->getID() );
return entrance;
}
+/** Can this member of a group sent through `entrance` go in with the rest of it? */
+static Bool canTakeTunnel( Object *unit, Object *entrance )
+{
+ return unit->getAIUpdateInterface()->isDoingGroundMovement()
+ && TheActionManager->canEnterObject( unit, entrance, CMD_FROM_PLAYER, DONT_CHECK_CAPACITY );
+}
+
/**
* Move to given position(s)
*/
@@ -2403,7 +2404,11 @@ void AIGroup::groupMoveToPosition( const Coord3D *p_posIn, Bool addWaypoint, Com
}
Int floodCursor = 0;
Int airCursor = 0;
- std::vector<ObjectID> sentThroughTunnels;
+
+ // through the tunnel network when that is shorter; see pickTunnelEntrance
+ Object *tunnelEntrance = NULL;
+ if (gatherOnPoint && iter->first() != NULL)
+ tunnelEntrance = pickTunnelEntrance( iter->first()->getControllingPlayer()->getTunnelSystem(), &groupCenter, &goalPos );
// Works better if you let the near units get the first paths... jba.
// Move the ones nearest the goal first. Reduces collision problems later.
@@ -2433,12 +2438,10 @@ void AIGroup::groupMoveToPosition( const Coord3D *p_posIn, Bool addWaypoint, Com
else if (airCursor < (Int)airMembers.size() && airMembers[airCursor] == theUnit)
dest = airGoals[airCursor++];
- // through the tunnel network when that is shorter; see pickTunnelEntrance
- Object *entrance = pickTunnelEntrance( theUnit, &dest, sentThroughTunnels );
- if (entrance != NULL)
+ if (tunnelEntrance != NULL && canTakeTunnel( theUnit, tunnelEntrance ))
{
ai->clearCrowdLane();
- ai->aiEnter( entrance, cmdSource );
+ ai->aiEnter( tunnelEntrance, cmdSource );
ai->friend_setTunnelTrip( &dest );
continue;
}