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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
feat(ai): gla computer players dig tunnels at home, expansion and far road
Olcay Seyganfea602788018
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5f0f59249..e55d28fc9 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -400,7 +400,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. 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. 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. 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.+- 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. 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. 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. 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. 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 Brutal 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.## The power bar tells the truthdiff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.hindex 66615c7fb..b6ca0e0fb 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h@@ -328,6 +328,7 @@ protected:Bool enemyDirection(Coord3D *dir); ///< unit vector from this base towards the nearest enemy's best known addressBool isHeldExpansion(const Object *warehouse); ///< our supply center stands at it, and it is nearer our base than any enemy's+ void doTunnels(Object *dozer); ///< a tunnel at home, at the held expansion and far out on the next wave's roadvoid buildAsap(const ThingTemplate *tmpl); ///< the plan's own unbuilt entry if it has one, otherwise a new spot behind the basevoid buyMoneyUnits(void);diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cppindex 4362a14aa..28c746222 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp@@ -4858,6 +4858,17 @@ void AIPlayer::doEconomy( void )if( !m_player->getEnergy()->hasSufficientPower() )return;+ // a tunnel pays back in the time every wave and every retreat saves, so it does not wait for the+ // hoard: behind it, it was bought once in four matches, since an economy that works spends the+ // bank down to the threshold on the army+ if( m_player->getCanBuildBase() && m_baseCenterSet )+ {+ Object *dozer = NULL;+ m_player->iterateObjects( findAnyDozer, &dozer );+ if( dozer )+ doTunnels( dozer );+ }+// hackers and the buildings that pay out on a timer earn their price back, so they only wait for// the hoard threshold, and each of them is bought on every pass the bank allows one ...if( m_player->getMoney()->countMoney() <= profile->m_cashHoardThreshold )@@ -4928,6 +4939,136 @@ void AIPlayer::doEconomy( void )}}+/** How near a point one of this player's tunnels has to stand to count as covering it. */+static const Real TUNNEL_COVER_RADIUS = 350.0f;++/** How far out along the line to the nearest enemy the forward tunnel goes, as a share of the way.+ * A wave sets off from the edge of its own base, and a shortcut has to save 30% of the walk+ * (TunnelTracker::findTunnelShortcut), so an exit short of the enemy's doorstep never beats walking:+ * at 0.45 not one wave in four matches went underground. */+static const Real FORWARD_TUNNEL_SHARE = 0.75f;++/** A tunnel network the builder can put up right now, off its own buttons. The tunnel carries+ * FS_BASE_DEFENSE like the guns, so the kind cannot tell them apart; the module can. */+static const ThingTemplate *buildableTunnel( Object *builder )+{+ const CommandSet *commandSet = TheControlBar->findCommandSet( builder->getCommandSetString() );+ if( commandSet == NULL )+ return NULL;++ for( Int i = 0; i < MAX_COMMANDS_PER_SET; ++i )+ {+ const CommandButton *button = commandSet->getCommandButton( i );+ if( button == NULL || button->getCommandType() != GUI_COMMAND_DOZER_CONSTRUCT )+ continue;+ const ThingTemplate *tmpl = button->getThingTemplate();+ if( tmpl == NULL || TheBuildAssistant->canMakeUnit( builder, tmpl ) != CANMAKE_OK )+ continue;+ const ModuleInfo &modules = tmpl->getBehaviorModuleInfo();+ for( Int m = 0; m < modules.getCount(); ++m )+ {+ if( modules.getNthName( m ).compareNoCase( "TunnelContain" ) == 0 )+ return tmpl;+ }+ }+ return NULL;+}++static Bool hasTunnelNear( Player *player, const Coord3D *spot )+{+ const std::list<ObjectID> *tunnels = player->getTunnelSystem()->getContainerList();+ for( std::list<ObjectID>::const_iterator it = tunnels->begin(); it != tunnels->end(); ++it )+ {+ const Object *tunnel = TheGameLogic->findObjectByID( *it );+ if( tunnel && sqr( tunnel->getPosition()->x - spot->x ) + sqr( tunnel->getPosition()->y - spot->y ) <= sqr( TUNNEL_COVER_RADIUS ) )+ return TRUE;+ }+ return FALSE;+}++//----------------------------------------------------------------------------------------------------------+/** A GLA computer makes a habit of tunnels at the places that matter: one at home, one at the+ * expansion it holds, and one on the road the next wave takes, three quarters of the way along.+ * The move orders, waves and retreats take a tunnel when it is the shorter way+ * (TunnelTracker::findTunnelShortcut), and with these three a wave goes underground at home and comes+ * up outside the enemy's base, and a beaten team near the front comes up at home. One is asked for+ * a pass, the nearest to home first; the forward one only where nothing this AI has seen can shoot.+ * There is no cap: the spots are fixed, one a road, and a spot with a tunnel near it asks for none. */+//----------------------------------------------------------------------------------------------------------+void AIPlayer::doTunnels( Object *dozer )+{+ const ThingTemplate *tunnel = buildableTunnel( dozer );+ if( tunnel == NULL || priorityBuildPending( m_player, tunnel ) )+ return;++ const Int MAX_SPOTS = 3;+ Coord3D spots[ MAX_SPOTS ];+ Real innerRadius[ MAX_SPOTS ];+ Int count = 0;++ // at home, out of the middle, which the production buildings have taken+ spots[ count ] = m_baseCenter;+ innerRadius[ count++ ] = 0.5f * m_baseRadius;++ const Object *warehouse = TheGameLogic->findObjectByID( m_curWarehouseID );+ if( warehouse && isHeldExpansion( warehouse ) )+ {+ spots[ count ] = *warehouse->getPosition();+ innerRadius[ count++ ] = warehouse->getGeometryInfo().getBoundingCircleRadius();+ }++ // on the road the parked wave is about to take, which the script chose knowing where the enemy is;+ // this AI's own guess can be an empty start position for most of a match on a four-start map. A+ // wave parks for seconds at a time, so with nobody parked the last road taken stands in for it+ Int road = -1;+ for( Int held = 0; held < MAX_HELD_TEAMS; ++held )+ {+ if( m_heldLabel[ held ].isEmpty() )+ continue;+ if( road < 0 || m_heldUsed[ held ] )+ road = held;+ if( m_heldUsed[ held ] )+ break;+ }+ Waypoint *start = NULL;+ if( road >= 0 )+ {+ AsciiString pathLabel;+ pathLabel.format( "%s%d", m_heldLabel[ road ].str(), m_heldSuffix[ road ] );+ start = TheTerrainLogic->getClosestWaypointOnPath( &m_baseCenter, pathLabel );+ }+ Waypoint *end = start;+ for( Int step = 0; end != NULL && end->getNumLinks() > 0 && step < APPROACH_MAX_WAYPOINTS; ++step )+ end = end->getLink( 0 );+ if( end != NULL )+ {+ const Real reach = FORWARD_TUNNEL_SHARE * sqrt( sqr( end->getLocation()->x - m_baseCenter.x ) + sqr( end->getLocation()->y - m_baseCenter.y ) );+ Waypoint *way = start;+ for( Int step = 0; way != NULL && step < APPROACH_MAX_WAYPOINTS; ++step )+ {+ const Coord3D *at = way->getLocation();+ if( sqr( at->x - m_baseCenter.x ) + sqr( at->y - m_baseCenter.y ) >= sqr( reach ) )+ {+ if( reach > m_baseRadius && knownFirepowerNear( at ) <= 0.0f )+ {+ spots[ count ] = *at;+ innerRadius[ count++ ] = 0.0f;+ }+ break;+ }+ way = (way->getNumLinks() > 0) ? way->getLink( 0 ) : NULL;+ }+ }++ for( Int i = 0; i < count; ++i )+ {+ if( hasTunnelNear( m_player, &spots[ i ] ) )+ continue;+ placeNear( tunnel, &spots[ i ], innerRadius[ i ] );+ return;+ }+}+/** One of this player's supply centers stands within reach of a warehouse. */struct SupplyCenterSearch{