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(ai): gla computer players retreat and attack through their tunnels

Olcay Seygan4e595638e5eb
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 879714e79..c2b24f1e3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -399,7 +399,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 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.
+- 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. 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 Brutal GLA mirrors on Twilight Flame, and an attack wave takes the tunnels when they come up nearer your base than its road does. 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/GameEngine/Include/Common/TunnelTracker.h b/GeneralsMD/Code/GameEngine/Include/Common/TunnelTracker.h
index 2583e27b0..8e05f8a77 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/TunnelTracker.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/TunnelTracker.h
@@ -70,6 +70,7 @@ public:
Object *findQuietTunnelNear( const Coord3D *pos ) const; ///< the tunnel nearest pos that has not been shot at lately, or NULL
Bool hasTunnelTraveller() const; ///< is anybody inside only passing through, and so about to free their place?
+ 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 *getCurNemesis(void);
void updateNemesis(const Object *target);
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
index 90d2c0700..2d1326d67 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
@@ -340,6 +340,7 @@ protected:
Real knownFirepowerNear(const Coord3D *pos); ///< what this AI has seen that can shoot, near a point
Bool forwardHoldPoint(const AsciiString &approach, Int pathSuffix, const Coord3D *enemyPos, Coord3D *hold); ///< where a wave gathers on its road
void sendWave(AIGroup *wave, const AsciiString &approach, Int pathSuffix, Int teams, Real power, UnsignedInt heldFrames);
+ void sendWaveThroughTunnels(AIGroup *wave, const Coord3D *center, Waypoint *way); ///< whoever can goes by tunnel when the path is the long way round
virtual void doBaseBuilding(void);
virtual void checkReadyTeams(void);
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
index ab0aa4986..ea598c88d 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
@@ -99,6 +99,15 @@ enum GuardTargetType
GUARDTARGET_NONE // Currently not guarding
};
+//-------------------------------------------------------------------------------------------------
+// How a unit that went through the tunnel network walks from the far mouth to where it was sent.
+// Written out in save/load xfer, don't change these numbers.
+enum TunnelTripEnd
+{
+ TUNNEL_TRIP_MOVE = 0, // a move order: walk there
+ TUNNEL_TRIP_ATTACK_MOVE = 1 // a computer's attack: fight whatever is on the way
+};
+
#ifdef DEFINE_LOCOMOTORSET_NAMES
static const char *TheLocomotorSetNames[] =
{
@@ -738,8 +747,9 @@ public:
// this is intended for use ONLY by the salvage collection pass in GameLogic.
void friend_setSalvageReturnPosition( const Coord3D *pos );
- // 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 );
+ /** Down `entrance`, out of the mouth nearest `goal`, and on to `goal` the way `end` says. FALSE,
+ and nothing ordered, for a unit that cannot go into a tunnel. */
+ Bool takeTunnelTrip( Object *entrance, const Coord3D *goal, TunnelTripEnd end, CommandSourceType cmdSource );
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)
@@ -925,6 +935,7 @@ private:
Bool m_hasSalvageReturnPosition; ///< True while the walk back to m_salvageReturnPosition is still owed.
Coord3D m_tunnelTripGoal; ///< Where a move order sent us before a tunnel shortened the way there.
Bool m_hasTunnelTrip; ///< True from the order to enter a tunnel until the walk from the exit to m_tunnelTripGoal is ordered.
+ TunnelTripEnd m_tunnelTripEnd; ///< How the last leg from the far mouth to m_tunnelTripGoal is walked.
// Locomotors -------------------------------------------------------------------------------------------------
enum LocoGoalType // Note - written out in save/load xfer, don't change these numbers. jba.
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp
index db7104dde..14d2134f7 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp
@@ -295,6 +295,38 @@ Bool TunnelTracker::hasTunnelTraveller() const
return FALSE;
}
+// ------------------------------------------------------------------------
+static const Real TUNNEL_SHORTCUT_SHARE = 0.7f; ///< the longest a way through the tunnels may be, as a share of the walk
+
+/** Whoever moves - a player's selection, a computer's wave, a unit falling back - decides once for
+ the whole group, from its middle: 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 any 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).
+
+ 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.
+ 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
+{
+ if( (Int)getContainCount() >= getContainMax() )
+ return NULL;
+
+ Object *entrance = findQuietTunnelNear( from );
+ Object *exit = findQuietTunnelNear( to );
+ if( entrance == NULL || exit == entrance )
+ return NULL;
+
+ 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 * TUNNEL_SHORTCUT_SHARE )
+ return NULL;
+
+ return entrance;
+}
+
// ------------------------------------------------------------------------
void TunnelTracker::destroyObject( Object *obj, void * )
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
index 6d108161a..f0378714b 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp
@@ -1976,47 +1976,6 @@ static void crowdSeedLanesAlongWaypoint( AIGroup *group, std::list<Object *>& me
crowdSeedLanes( members, center, crowdWaypointAim( center, way ) );
}
-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 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 when that matters. */
-static Object *pickTunnelEntrance( const TunnelTracker *network, const Coord3D *center, const Coord3D *goal )
-{
- if ((Int)network->getContainCount() >= network->getContainMax())
- return NULL;
-
- Object *entrance = network->findQuietTunnelNear( center );
- Object *exit = network->findQuietTunnelNear( goal );
- if (entrance == NULL || exit == entrance)
- return NULL;
-
- 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;
-
- 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)
*/
@@ -2405,10 +2364,15 @@ void AIGroup::groupMoveToPosition( const Coord3D *p_posIn, Bool addWaypoint, Com
Int floodCursor = 0;
Int airCursor = 0;
- // through the tunnel network when that is shorter; see pickTunnelEntrance
+ // through the tunnel network when that is shorter; see TunnelTracker::findTunnelShortcut
Object *tunnelEntrance = NULL;
if (gatherOnPoint && iter->first() != NULL)
- tunnelEntrance = pickTunnelEntrance( iter->first()->getControllingPlayer()->getTunnelSystem(), &groupCenter, &goalPos );
+ {
+ const Real walkX = goalPos.x - groupCenter.x;
+ const Real walkY = goalPos.y - groupCenter.y;
+ tunnelEntrance = iter->first()->getControllingPlayer()->getTunnelSystem()->findTunnelShortcut( &groupCenter, &goalPos,
+ (Real)sqrt( walkX * walkX + walkY * walkY ) );
+ }
// Works better if you let the near units get the first paths... jba.
// Move the ones nearest the goal first. Reduces collision problems later.
@@ -2438,11 +2402,9 @@ void AIGroup::groupMoveToPosition( const Coord3D *p_posIn, Bool addWaypoint, Com
else if (airCursor < (Int)airMembers.size() && airMembers[airCursor] == theUnit)
dest = airGoals[airCursor++];
- if (tunnelEntrance != NULL && canTakeTunnel( theUnit, tunnelEntrance ))
+ if (tunnelEntrance != NULL && ai->takeTunnelTrip( tunnelEntrance, &dest, TUNNEL_TRIP_MOVE, cmdSource ))
{
ai->clearCrowdLane();
- ai->aiEnter( tunnelEntrance, cmdSource );
- ai->friend_setTunnelTrip( &dest );
continue;
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 48a5cbec6..8a682d309 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -40,6 +40,7 @@
#include "Common/PlayerList.h"
#include "Common/BuildAssistant.h"
#include "Common/ThingTemplate.h"
+#include "Common/TunnelTracker.h"
#include "Common/Upgrade.h"
#include "Common/WellKnownKeys.h"
#include "Common/Xfer.h"
@@ -5691,8 +5692,48 @@ void AIPlayer::sendWave( AIGroup *wave, const AsciiString &approach, Int pathSuf
Waypoint *way = TheTerrainLogic->getClosestWaypointOnPath( &center, pathLabel );
DEBUG_LOG(("AI WAVE frame %d player %d sends %d teams, %d units, %.0f power, after %d s, down %s\n", TheGameLogic->getFrame(),
m_player->getPlayerIndex(), teams, wave->getCount(), power, heldFrames / LOGICFRAMES_PER_SECOND, pathLabel.str()));
- if( way )
- wave->groupFollowWaypointPathAsTeam( way, CMD_FROM_AI );
+ if( way == NULL )
+ return;
+
+ wave->groupFollowWaypointPathAsTeam( way, CMD_FROM_AI );
+ sendWaveThroughTunnels( wave, &center, way );
+}
+
+//----------------------------------------------------------------------------------------------------------
+/** A GLA wave whose tunnels come up nearer the end of its approach path than the path itself goes
+ * takes them, the way a player's move order does (TunnelTracker::findTunnelShortcut), and comes up
+ * fighting. The path is ordered first and the tunnel replaces it for whoever can go in, so a
+ * member that cannot - an aircraft, anything the tunnel refuses - still has the path. */
+//----------------------------------------------------------------------------------------------------------
+static const Int WAVE_PATH_MAX_POINTS = 256; ///< a path that links back on itself stops being walked here
+
+void AIPlayer::sendWaveThroughTunnels( AIGroup *wave, const Coord3D *center, Waypoint *way )
+{
+ Real walk = (Real)sqrt( sqr( way->getLocation()->x - center->x ) + sqr( way->getLocation()->y - center->y ) );
+ Waypoint *end = way;
+ for( Int i = 0; i < WAVE_PATH_MAX_POINTS && end->getNumLinks() > 0; ++i )
+ {
+ Waypoint *next = end->getLink( 0 );
+ walk += (Real)sqrt( sqr( next->getLocation()->x - end->getLocation()->x ) + sqr( next->getLocation()->y - end->getLocation()->y ) );
+ end = next;
+ }
+
+ Object *entrance = m_player->getTunnelSystem()->findTunnelShortcut( 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 );
+ if( obj && obj->getAI() && obj->getAI()->takeTunnelTrip( entrance, end->getLocation(), TUNNEL_TRIP_ATTACK_MOVE, CMD_FROM_AI ) )
+ ++sent;
+ }
+ DEBUG_LOG(("AI WAVE frame %d player %d sends %d of %d units through tunnel %d, the path is %.0f long\n", TheGameLogic->getFrame(),
+ m_player->getPlayerIndex(), sent, waveSize, entrance->getID(), walk));
}
//----------------------------------------------------------------------------------------------------------
@@ -5864,8 +5905,16 @@ void AIPlayer::doRetreats( void )
//
// Losing. The whole team goes home if this rung knows how; otherwise the members that
// are personally finished go, which saves the units that would otherwise die inside a
- // fight the team as a whole is still winning.
+ // fight the team as a whole is still winning. Through the tunnels, when there is one
+ // near the fight and one near home.
//
+ const Real homeX = m_baseCenter.x - centre.x;
+ const Real homeY = m_baseCenter.y - centre.y;
+ Object *homeTunnel = m_player->getTunnelSystem()->findTunnelShortcut( &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(),
+ m_player->getPlayerIndex(), homeTunnel->getID()));
for( DLINK_ITERATOR<Object> objIter = team->iterate_TeamMemberList(); !objIter.done(); objIter.advance() )
{
Object *obj = objIter.cur();
@@ -5883,7 +5932,12 @@ void AIPlayer::doRetreats( void )
continue;
}
- obj->getAI()->aiMoveToPosition( &m_baseCenter, CMD_FROM_AI );
+ // one already on its way down a tunnel is on its way home; ordering it again would turn it
+ // round at the mouth
+ if( obj->getAI()->hasTunnelTrip() )
+ continue;
+ if( homeTunnel == NULL || !obj->getAI()->takeTunnelTrip( homeTunnel, &m_baseCenter, TUNNEL_TRIP_MOVE, CMD_FROM_AI ) )
+ obj->getAI()->aiMoveToPosition( &m_baseCenter, CMD_FROM_AI );
}
}
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
index dabb3ba79..efe9551a6 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
@@ -356,6 +356,7 @@ AIUpdateInterface::AIUpdateInterface( Thing *thing, const ModuleData* moduleData
m_hasSalvageReturnPosition = FALSE;
m_tunnelTripGoal.zero();
m_hasTunnelTrip = FALSE;
+ m_tunnelTripEnd = TUNNEL_TRIP_MOVE;
m_locomotorSet.clear();
m_curLocomotor = NULL;
m_curLocomotorSet = LOCOMOTORSET_INVALID;
@@ -1221,7 +1222,10 @@ UpdateSleepTime AIUpdateInterface::update( void )
{
Coord3D goal = m_tunnelTripGoal;
m_hasTunnelTrip = FALSE;
- privateMoveToPosition( &goal, CMD_FROM_AI );
+ if (m_tunnelTripEnd == TUNNEL_TRIP_ATTACK_MOVE && me->isAbleToAttack() && hasFightingWeapon(me))
+ privateAttackMoveToPosition( &goal, NO_MAX_SHOTS_LIMIT, CMD_FROM_AI );
+ else
+ privateMoveToPosition( &goal, CMD_FROM_AI );
}
stRet = STATE_CONTINUE;
}
@@ -5209,13 +5213,20 @@ void AIUpdateInterface::friend_setSalvageReturnPosition( const Coord3D *pos )
//----------------------------------------------------------------------------------------
/**
- * Remember the goal of the move order that was just turned into an enter into a tunnel. update()
- * takes it from there: out of the mouth nearest the goal, then the walk to it.
+ * Turn an order to go to `goal` into an enter into `entrance`. update() takes it from there: out of
+ * the mouth nearest the goal, then the last leg to it.
*/
-void AIUpdateInterface::friend_setTunnelTrip( const Coord3D *goal )
+Bool AIUpdateInterface::takeTunnelTrip( Object *entrance, const Coord3D *goal, TunnelTripEnd end, CommandSourceType cmdSource )
{
+ if (!isDoingGroundMovement() || !TheActionManager->canEnterObject( getObject(), entrance, cmdSource, DONT_CHECK_CAPACITY ))
+ return FALSE;
+
+ // a player's enter ends any trip (aiDoCommand), so the trip is set after it
+ aiEnter( entrance, cmdSource );
m_tunnelTripGoal = *goal;
+ m_tunnelTripEnd = end;
m_hasTunnelTrip = TRUE;
+ return TRUE;
}
//----------------------------------------------------------------------------------------
@@ -7059,12 +7070,13 @@ void AIUpdateInterface::crc( Xfer *x )
* 12: m_allowedToChase
* 13: m_pathfindFoundNothing
* 14: the salvage return position and its flag
- * 16: the tunnel trip's goal and its flag */
+ * 16: the tunnel trip's goal and its flag
+ * 17: how the tunnel trip's last leg is walked */
// ------------------------------------------------------------------------------------------------
void AIUpdateInterface::xfer( Xfer *xfer )
{
// version
- const XferVersion currentVersion = 16;
+ const XferVersion currentVersion = 17;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
@@ -7389,6 +7401,14 @@ void AIUpdateInterface::xfer( Xfer *xfer )
xfer->xferBool(&m_hasTunnelTrip);
}
+ if (version >= 17)
+ {
+ // a computer's wave comes out fighting, a move order does not
+ Int end = (Int)m_tunnelTripEnd;
+ xfer->xferInt(&end);
+ m_tunnelTripEnd = (TunnelTripEnd)end;
+ }
+
} // end xfer
// ------------------------------------------------------------------------------------------------