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 Hard grow its base with the queues

Olcay Seygan3c3828c65ee4
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
index 90e301b36..922faad36 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
@@ -296,6 +296,22 @@ Real aiRetreatRatio( Real myHealth, Real myPower, Real enemyHealth, Real enemyPo
Bool aiShouldMass( Real waitingThreat, Real enemyVisibleThreat, Real massFraction,
Bool timeExpired, Bool baseUnderAttack );
+/** Another factory of this kind. finished are standing and done, constructing are already on the
+ * way, idle are finished factories with an empty queue, deepest is the longest queue among them.
+ *
+ * A queue-gated kind (tanks, infantry) gets another when every finished factory is busy and one of
+ * them has a unit waiting behind the one under construction, and a replacement when none are left.
+ * An unlimited kind (airfields, and the income buildings that follow the same rule) gets another
+ * whenever one is not already going up. One already on the way is the building this would ask for. */
+Bool aiWantsAnotherFactory( Int finished, Int constructing, Int idle, Int deepest, Bool unlimited );
+
+/** How many copies of the faction tech building (strategy center, propaganda center, palace) to
+ * keep up so one of them blowing up does not take the tree with it. */
+static const Int AI_TECH_BUILDING_COPIES = 3;
+
+/** Another of those, when fewer than AI_TECH_BUILDING_COPIES are standing and none is already on the way. */
+Bool aiWantsAnotherTechBuilding( Int standing, Int onTheWay );
+
/** How badly one place wants looking at, per step walked there. A3's scouting is never a search -
* the start positions are public, the lobby shows them - so the question is not "where is he" but
* "whose picture is worth the walk", and the answer is the stalest one per unit of distance:
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
index 2385681dd..15e2fe9fb 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
@@ -318,9 +318,9 @@ protected:
Object *findHijacker(void); ///< a unit of ours that takes a vehicle by walking into it
Object *nearestStealableVehicle(const Coord3D *from, Real reach); ///< the closest enemy vehicle in sight; reach <= 0 is the map
- /** Past the hoard, buy what the build list never had: another production building beside the last
- * expansion when every one of a kind is busy, another income building when production keeps up,
- * and money units (China's hackers) from any factory standing idle. */
+ /** Past the hoard, buy what the build list never had. A tank factory or a barracks when that
+ * queue is backing up, an airfield with no count cap, income buildings the same way, and money
+ * units (China's hackers) from any factory with room in its queue. */
virtual void doEconomy(void);
/** Buy a power plant before the margin runs out rather than after, and put it on the far side of
@@ -328,7 +328,8 @@ protected:
virtual void doPower(void);
/** Hard: the superweapon as soon as it can be bought, and the tech building it waits on before
- * that, with no clock; as many as the game's rules allow. */
+ * that, with no clock; as many superweapons as the game's rules allow, and a few copies of the
+ * tech building so one of them blowing up leaves the tree standing. */
virtual void doSuperweapons(void);
Bool enemyDirection(Coord3D *dir); ///< unit vector from this base towards the nearest enemy's best known address
@@ -339,7 +340,8 @@ protected:
void buyMoneyUnits(void);
Int moneyUnitRoom(void) const; ///< how many more money units this player's army and internet centers carry
Bool hasEnoughMoneyUnitsFor(TeamPrototype *proto) const; ///< this team is all hackers and there is no room for more
- Bool placeNear(const ThingTemplate *tmpl, const Coord3D *center, Real innerRadius); ///< a legal, safe spot on a ring round center, queued for a dozer
+ Bool placeNear(const ThingTemplate *tmpl, const Coord3D *center, Real innerRadius, Bool walkOutward); ///< a legal, safe spot on a ring round center, queued for a dozer. walkOutward keeps searching further out as the inner rings fill
+ Bool queueExtraFactory(Object *dozer, KindOfType kind, Bool unlimited); ///< one more of this factory, beside a held expansion or around the base
Real knownFirepowerAlongPath(Waypoint *way); ///< what this AI has seen that can shoot, along an approach
AsciiString secondApproachLabel(const Coord3D *from, const AsciiString &taken, Int pathSuffix); ///< the quietest other road, or empty
void loadGunships(void); ///< the parked wave's infantry boards the gunships at home
@@ -476,6 +478,7 @@ protected:
Coord3D m_baseCenter; // Center of the initial build list of structures.
Bool m_baseCenterSet; // True if baseCenter is valid.
Real m_baseRadius; // Radius of the initial build list of structures.
+ Int m_placementRing; ///< first ring placeNear tries when the search is allowed to walk outward
// Bridge repair info.
enum {MAX_STRUCTURES_TO_REPAIR = 2};
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
index faaa49f07..b2f530ee4 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
@@ -1164,6 +1164,28 @@ Bool aiShouldMass( Real waitingThreat, Real enemyVisibleThreat, Real massFractio
return waitingThreat < massFraction * enemyVisibleThreat;
}
+//-------------------------------------------------------------------------------------------------
+Bool aiWantsAnotherFactory( Int finished, Int constructing, Int idle, Int deepest, Bool unlimited )
+{
+ if( constructing > 0 )
+ return FALSE; // one is already going up
+ if( unlimited )
+ return TRUE;
+ if( finished <= 0 )
+ return TRUE; // the last one is gone, so put one back
+ if( idle > 0 )
+ return FALSE; // a factory with nothing in it is the spare
+ return deepest >= 2; // busy, and a unit is waiting behind the one being built
+}
+
+//-------------------------------------------------------------------------------------------------
+Bool aiWantsAnotherTechBuilding( Int standing, Int onTheWay )
+{
+ if( onTheWay > 0 )
+ return FALSE;
+ return standing < AI_TECH_BUILDING_COPIES;
+}
+
//-------------------------------------------------------------------------------------------------
Real aiScoutScore( UnsignedInt now, UnsignedInt lastSeenFrame, Real distance, UnsignedInt freshFrames )
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 594258891..8077896a1 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -340,6 +340,7 @@ m_role(AIROLE_AGGRESSIVE)
m_buildProbePos.zero();
m_baseCenter.zero();
m_baseCenterSet = false;
+ m_placementRing = 0;
m_difficulty = TheScriptEngine->getGlobalDifficulty();
m_skillLevel = skillLevelForDifficulty( m_difficulty );
@@ -4588,7 +4589,9 @@ static const Int DEFENSES_PER_SUPERWEAPON = 4;
static const Int MAX_ECONOMY_DOZERS = 4;
/** Tries on each ring when looking for somewhere to put a purchase down. Every try is a legality
- * check that costs about a millisecond, so this bounds the spike rather than the search. */
+ * check that costs about a millisecond, so a call still only walks two rings: that is the spike,
+ * not the reach of the search. A search allowed to walk outward remembers the ring it reached
+ * and starts the next call there, so a full opening base does not end the search. */
static const Int PLACEMENT_ANGLES = 12;
static const Int PLACEMENT_RINGS = 2;
@@ -4681,26 +4684,54 @@ static void upgradeTransportHelicopter( Object *obj, void * )
}
}
-/** Every finished production building of this kind has something in its queue. One still going up
- * counts as spare: it is the answer already on its way. */
-static Bool everyFactoryBusy( Player *player, KindOfType kind )
+/** How loaded this player's factories of one kind are. Counted off the objects, because a factory
+ * bought past the build list is still a factory. */
+struct FactoryLoad
{
- Int factories = 0;
- for( BuildListInfo *info = player->getBuildList(); info; info = info->getNext() )
+ KindOfType kind;
+ Int finished;
+ Int constructing;
+ Int idle;
+ Int deepest;
+};
+
+static void readFactoryLoad( Object *obj, void *userData )
+{
+ FactoryLoad *load = (FactoryLoad *)userData;
+ if( obj->isEffectivelyDead() || !obj->isKindOf( load->kind ) )
+ return;
+ if( obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION ) )
{
- Object *factory = TheGameLogic->findObjectByID( info->getObjectID() );
- if( factory == NULL || factory->getControllingPlayer() != player || !factory->isKindOf( kind ) )
- continue;
- if( factory->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION ) )
- return FALSE;
- ProductionUpdateInterface *pu = factory->getProductionUpdateInterface();
- if( pu == NULL )
- continue;
- if( pu->getProductionCount() == 0 )
- return FALSE;
- ++factories;
+ ++load->constructing;
+ return;
}
- return factories > 0;
+ ProductionUpdateInterface *pu = obj->getProductionUpdateInterface();
+ if( pu == NULL )
+ return;
+ ++load->finished;
+ const Int queued = (Int)pu->getProductionCount();
+ if( queued == 0 )
+ ++load->idle;
+ if( queued > load->deepest )
+ load->deepest = queued;
+}
+
+struct TechCount
+{
+ KindOfType kind;
+ Int standing;
+ Int onTheWay;
+};
+
+static void countTechBuildings( Object *obj, void *userData )
+{
+ TechCount *tech = (TechCount *)userData;
+ if( obj->isEffectivelyDead() || !obj->isKindOf( tech->kind ) )
+ return;
+ if( obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION ) )
+ ++tech->onTheWay;
+ else
+ ++tech->standing;
}
/** Where this player's hackers go to work: into an internet center with room, or else the quiet side
@@ -4993,9 +5024,10 @@ void AIPlayer::doPower( void )
spot.y -= dir.y * m_baseRadius * POWER_SETBACK;
}
- // the back of a grown base fills up, and a plant with nowhere to go there goes anywhere in the base
- if( !placeNear( tmpl, &spot, 0.0f ) )
- placeNear( tmpl, &m_baseCenter, m_baseRadius );
+ // the back of a grown base fills up. the spot behind the base walks outward with it; a miss there
+ // still tries the inner rings of the base itself, and that try does not move the outward walk
+ if( !placeNear( tmpl, &spot, 0.0f, TRUE ) )
+ placeNear( tmpl, &m_baseCenter, m_baseRadius, FALSE );
}
//----------------------------------------------------------------------------------------------------------
@@ -5003,11 +5035,11 @@ void AIPlayer::doPower( void )
* list is a fixed plan - two war factories and a barracks - and the scripts never add to it, so
* once the plan stood the money had nowhere to go.
*
- * What the plan cannot buy. Another production building when every one of a kind is busy, put down
- * beside the last expansion so the army comes out nearer the fighting. Another income building - a
- * supply drop zone, a black market - on every pass, neither has a limit. Hackers from every factory
- * that trains them, because China's income building takes one copy and a hacker earns wherever it
- * stands. And base defenses.
+ * What the plan cannot buy. A tank factory or a barracks when that queue is backing up, and an
+ * airfield on every pass the money allows, with no cap on how many stand. Income buildings the
+ * same way: a supply drop zone, a black market, an internet center, whichever of those the faction
+ * can build, on every pass. Hackers from every factory that trains them. And base defenses,
+ * still one per four fighting units.
*/
//----------------------------------------------------------------------------------------------------------
void AIPlayer::doEconomy( void )
@@ -5075,13 +5107,14 @@ void AIPlayer::doEconomy( void )
if( dozer == NULL )
return;
- const Int INCOME_KINDS = 2;
- static const KindOfType INCOME[ INCOME_KINDS ] = { KINDOF_FS_SUPPLY_DROPZONE, KINDOF_FS_BLACK_MARKET };
+ const Int INCOME_KINDS = 3;
+ static const KindOfType INCOME[ INCOME_KINDS ] = {
+ KINDOF_FS_SUPPLY_DROPZONE, KINDOF_FS_BLACK_MARKET, KINDOF_FS_INTERNET_CENTER };
for( Int i = 0; i < INCOME_KINDS; ++i )
{
const ThingTemplate *tmpl = buildableOfKind( dozer, GUI_COMMAND_DOZER_CONSTRUCT, INCOME[ i ] );
if( tmpl && !priorityBuildPending( m_player, tmpl ) )
- placeNear( tmpl, &m_baseCenter, m_baseRadius );
+ placeNear( tmpl, &m_baseCenter, m_baseRadius, TRUE );
}
// ... and what does not pay back for twice it, so the opening build order is not what pays for it
@@ -5108,25 +5141,28 @@ void AIPlayer::doEconomy( void )
spot.x += dir.x * m_baseRadius * DEFENSE_STANDOFF;
spot.y += dir.y * m_baseRadius * DEFENSE_STANDOFF;
}
- placeNear( defense, &spot, 0.0f );
+ placeNear( defense, &spot, 0.0f, FALSE );
}
- const Int PRODUCTION_KINDS = 3;
- static const KindOfType PRODUCTION[ PRODUCTION_KINDS ] = { KINDOF_FS_WARFACTORY, KINDOF_FS_BARRACKS, KINDOF_FS_AIRFIELD };
- for( Int i = 0; i < PRODUCTION_KINDS; ++i )
+ /* Airfields have no count, so a tank queue that never empties would take every pass and the
+ airfield would wait forever. Even economy ticks ask for the airfield first, odd ticks ask for
+ the queues first, and a kind that wants nothing simply yields the pass. */
+ const KindOfType QUEUED_KINDS[ 2 ] = { KINDOF_FS_WARFACTORY, KINDOF_FS_BARRACKS };
+ const Bool airFirst = ((TheGameLogic->getFrame() + phase) / ECONOMY_CHECK_RATE) % 2 == 0;
+ for( Int pass = 0; pass < 2; ++pass )
{
- if( !everyFactoryBusy( m_player, PRODUCTION[ i ] ) )
- continue;
- const ThingTemplate *tmpl = buildableOfKind( dozer, GUI_COMMAND_DOZER_CONSTRUCT, PRODUCTION[ i ] );
- if( tmpl == NULL || priorityBuildPending( m_player, tmpl ) )
- continue;
-
- const Object *warehouse = TheGameLogic->findObjectByID( m_curWarehouseID );
- if( warehouse && isHeldExpansion( warehouse ) && placeNear( tmpl, warehouse->getPosition(),
- warehouse->getGeometryInfo().getBoundingCircleRadius() + SUPPLY_CENTER_CLOSE_DIST*0.5f ) )
- return;
- if( placeNear( tmpl, &m_baseCenter, m_baseRadius ) )
- return;
+ const Bool airPass = (pass == 0) == airFirst;
+ if( airPass )
+ {
+ if( queueExtraFactory( dozer, KINDOF_FS_AIRFIELD, TRUE ) )
+ return;
+ }
+ else
+ {
+ for( Int i = 0; i < 2; ++i )
+ if( queueExtraFactory( dozer, QUEUED_KINDS[ i ], FALSE ) )
+ return;
+ }
}
}
@@ -5255,7 +5291,7 @@ void AIPlayer::doTunnels( Object *dozer )
{
if( hasTunnelNear( m_player, &spots[ i ] ) )
continue;
- placeNear( tunnel, &spots[ i ], innerRadius[ i ] );
+ placeNear( tunnel, &spots[ i ], innerRadius[ i ], FALSE );
return;
}
}
@@ -5309,20 +5345,6 @@ Bool AIPlayer::isHeldExpansion( const Object *warehouse )
return TRUE;
}
-/** Whether this player owns anything of the kind, standing or going up. */
-struct KindSearch
-{
- KindOfType kind;
- Bool found;
-};
-
-static void findOwnedKind( Object *obj, void *userData )
-{
- KindSearch *search = (KindSearch *)userData;
- if( !search->found && obj->isKindOf( search->kind ) && !obj->isEffectivelyDead() )
- search->found = TRUE;
-}
-
/** The plan has an entry for this building that is not standing and not yet asked for. */
static Bool hasUnbuiltPlanEntry( Player *player, const ThingTemplate *tmpl )
{
@@ -5352,7 +5374,7 @@ void AIPlayer::buildAsap( const ThingTemplate *tmpl )
spot.x -= dir.x * m_baseRadius * POWER_SETBACK;
spot.y -= dir.y * m_baseRadius * POWER_SETBACK;
}
- if( placeNear( tmpl, &spot, 0.0f ) )
+ if( placeNear( tmpl, &spot, 0.0f, TRUE ) )
return;
/* A superweapon's footprint rarely fits one of placeNear's two dozen tries in a grown base, so the
@@ -5375,8 +5397,9 @@ void AIPlayer::buildAsap( const ThingTemplate *tmpl )
* is in the bank, the first one goes up. More follow as the base grows, one per
* DEFENSES_PER_SUPERWEAPON guns, as the guns grow with the army; unrationed, they took the money
* the army needed. The lobby's superweapon setting and Pro Rules still bind, canMakeUnit asks both.
- * If the tech building it needs is missing, that goes up first. Hard only, like the rest of the
- * economy. */
+ * If the tech building it needs is missing, that goes up first, and copies follow once the first
+ * superweapon is standing, up to AI_TECH_BUILDING_COPIES, so one of them blowing up leaves the tree
+ * standing. Hard only, like the rest of the economy. */
//----------------------------------------------------------------------------------------------------------
void AIPlayer::doSuperweapons( void )
{
@@ -5394,25 +5417,35 @@ void AIPlayer::doSuperweapons( void )
return;
const ThingTemplate *superweapon = buildableOfKind( dozer, GUI_COMMAND_DOZER_CONSTRUCT, KINDOF_FS_SUPERWEAPON );
+ const ThingTemplate *techBuilding = buildableOfKind( dozer, GUI_COMMAND_DOZER_CONSTRUCT, KINDOF_FS_ADVANCED_TECH );
+
+ TechCount tech;
+ tech.kind = KINDOF_FS_ADVANCED_TECH;
+ tech.standing = 0;
+ tech.onTheWay = 0;
+ m_player->iterateObjects( countTechBuildings, &tech );
+ if( techBuilding && priorityBuildPending( m_player, techBuilding ) )
+ ++tech.onTheWay;
+
+ /* The first tech building before any superweapon, the first superweapon before the spares, then
+ the tech building up to AI_TECH_BUILDING_COPIES, then more superweapons by the gun ratio. A
+ faction that cannot buy the tech building falls straight through to the superweapon. */
+ const BaseTally tally = tallyBase( m_player );
+ const Bool wantTech = techBuilding && aiWantsAnotherTechBuilding( tech.standing, tech.onTheWay );
+ const Bool firstTech = wantTech && tech.standing < 1;
+ const Bool spareTech = wantTech && tally.superweapons >= 1;
+ if( firstTech || spareTech )
+ {
+ buildAsap( techBuilding );
+ return;
+ }
+
if( superweapon )
{
- const BaseTally tally = tallyBase( m_player );
const Bool baseHoldsAnother = tally.superweapons < 1 + tally.defenses / DEFENSES_PER_SUPERWEAPON;
if( baseHoldsAnother && !priorityBuildPending( m_player, superweapon ) )
buildAsap( superweapon );
- return;
}
-
- KindSearch tech;
- tech.kind = KINDOF_FS_ADVANCED_TECH;
- tech.found = FALSE;
- m_player->iterateObjects( findOwnedKind, &tech );
- if( tech.found )
- return; // what is missing is money, or the lobby said no
-
- const ThingTemplate *techBuilding = buildableOfKind( dozer, GUI_COMMAND_DOZER_CONSTRUCT, KINDOF_FS_ADVANCED_TECH );
- if( techBuilding && !priorityBuildPending( m_player, techBuilding ) )
- buildAsap( techBuilding );
}
//----------------------------------------------------------------------------------------------------------
@@ -5452,17 +5485,72 @@ void AIPlayer::buyMoneyUnits( void )
}
}
+//----------------------------------------------------------------------------------------------------------
+/** One more factory of this kind, beside a held expansion or around the base. The expansion try is
+ * local: a miss out there must not walk the base's ring cursor past the ground around the command
+ * center, or the first crowded dock skips the base itself. */
+//----------------------------------------------------------------------------------------------------------
+Bool AIPlayer::queueExtraFactory( Object *dozer, KindOfType kind, Bool unlimited )
+{
+ const ThingTemplate *tmpl = buildableOfKind( dozer, GUI_COMMAND_DOZER_CONSTRUCT, kind );
+ if( tmpl == NULL || priorityBuildPending( m_player, tmpl ) )
+ return FALSE;
+
+ FactoryLoad load;
+ load.kind = kind;
+ load.finished = 0;
+ load.constructing = 0;
+ load.idle = 0;
+ load.deepest = 0;
+ m_player->iterateObjects( readFactoryLoad, &load );
+ if( !aiWantsAnotherFactory( load.finished, load.constructing, load.idle, load.deepest, unlimited ) )
+ return FALSE;
+
+ const Object *warehouse = TheGameLogic->findObjectByID( m_curWarehouseID );
+ if( warehouse && isHeldExpansion( warehouse ) &&
+ placeNear( tmpl, warehouse->getPosition(),
+ warehouse->getGeometryInfo().getBoundingCircleRadius() + SUPPLY_CENTER_CLOSE_DIST * 0.5f, FALSE ) )
+ return TRUE;
+ return placeNear( tmpl, &m_baseCenter, m_baseRadius, TRUE );
+}
+
//----------------------------------------------------------------------------------------------------------
/** Walk rings round center until a spot is legal and safe, and hand it to the dozers as a priority
- * build. ponytail: a fixed dozen angles on two rings, so a crowded spot can miss room that a wider
- * search would find; the next check tries again with whatever has changed. */
+ * build. A call still tries two rings, two dozen legality checks. walkOutward carries the first
+ * ring in m_placementRing, so the search moves out across calls instead of giving up at the opening
+ * base, and wraps back to the middle once that ring is past the pathfind extent. */
//----------------------------------------------------------------------------------------------------------
-Bool AIPlayer::placeNear( const ThingTemplate *tmpl, const Coord3D *center, Real innerRadius )
+Bool AIPlayer::placeNear( const ThingTemplate *tmpl, const Coord3D *center, Real innerRadius, Bool walkOutward )
{
const Real structureRadius = tmpl->getTemplateGeometryInfo().getBoundingCircleRadius();
const Real placeAngle = tmpl->getPlacementViewAngle();
- for( Int ring = 0; ring < PLACEMENT_RINGS; ++ring )
+ Int startRing = 0;
+ if( walkOutward )
+ {
+ Region3D extent;
+ TheTerrainLogic->getMaximumPathfindExtent( &extent );
+ Real farthest = 0.0f;
+ const Real cornerX[ 2 ] = { extent.lo.x, extent.hi.x };
+ const Real cornerY[ 2 ] = { extent.lo.y, extent.hi.y };
+ for( Int cx = 0; cx < 2; ++cx )
+ {
+ for( Int cy = 0; cy < 2; ++cy )
+ {
+ const Real dx = cornerX[ cx ] - center->x;
+ const Real dy = cornerY[ cy ] - center->y;
+ const Real dist = sqrt( dx * dx + dy * dy );
+ if( dist > farthest )
+ farthest = dist;
+ }
+ }
+ const Real reach = innerRadius + structureRadius * (2 * m_placementRing + 1);
+ if( m_placementRing < 0 || reach > farthest )
+ m_placementRing = 0;
+ startRing = m_placementRing;
+ }
+
+ for( Int ring = startRing; ring < startRing + PLACEMENT_RINGS; ++ring )
{
const Real distance = innerRadius + structureRadius * (2 * ring + 1);
for( Int step = 0; step < PLACEMENT_ANGLES; ++step )
@@ -5479,6 +5567,8 @@ Bool AIPlayer::placeNear( const ThingTemplate *tmpl, const Coord3D *center, Real
TheTerrainVisual->removeAllBibs(); // isLocationLegalToBuild adds bib feedback
if( legal && isLocationSafe( &pos, tmpl ) )
{
+ if( walkOutward )
+ m_placementRing = ring;
DEBUG_LOG(("AI ECONOMY frame %d player %d builds '%s' at (%.0f,%.0f), %d in the bank\n", TheGameLogic->getFrame(),
m_player->getPlayerIndex(), tmpl->getName().str(), pos.x, pos.y, m_player->getMoney()->countMoney()));
m_player->addToPriorityBuildList( tmpl->getName(), &pos, placeAngle );
@@ -5486,6 +5576,8 @@ Bool AIPlayer::placeNear( const ThingTemplate *tmpl, const Coord3D *center, Real
}
}
}
+ if( walkOutward )
+ m_placementRing = startRing + PLACEMENT_RINGS;
return FALSE;
}
@@ -8504,7 +8596,7 @@ void AIPlayer::xfer( Xfer *xfer )
{
// version
- XferVersion currentVersion = 10; // 2: scout 3: rung and role 4: scouting stamps 5: the capturer 6: parked waves 7: superweapon aims 8: the hijacker 9: tactical steps 10: the ferry and dropped riders
+ XferVersion currentVersion = 11; // 2: scout 3: rung and role 4: scouting stamps 5: the capturer 6: parked waves 7: superweapon aims 8: the hijacker 9: tactical steps 10: the ferry and dropped riders 11: the outward placement ring
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
@@ -8756,6 +8848,10 @@ void AIPlayer::xfer( Xfer *xfer )
for( UnsignedShort i = 0; i < dropped; ++i )
xfer->xferObjectID( &m_droppedRiders[ i ] );
}
+ // where the next outward search starts, so a loaded game does not pack its new buildings back
+ // into the opening rings the base has already filled
+ if( version >= 11 )
+ xfer->xferInt( &m_placementRing );
// the ladder rung and the role, which are rolled once and must come back the same way
if( version >= 3 )
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index f780dee85..93e1af79d 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -10080,6 +10080,43 @@ TEST(massing_waits_for_a_force_but_never_waits_for_ever)
}
+/** Another factory when the queue is backing up, and another tech building until three are standing.
+ Easy and Normal never reach the caller: economy buildings stay off below Brutal. */
+TEST(extra_factory_follows_the_queue_and_tech_stops_at_three)
+{
+ // nothing left standing: put one back
+ CHECK( aiWantsAnotherFactory( 0, 0, 0, 0, FALSE ) );
+
+ // one already going up is the building this would ask for
+ CHECK( !aiWantsAnotherFactory( 0, 1, 0, 0, FALSE ) );
+ CHECK( !aiWantsAnotherFactory( 2, 1, 0, 4, FALSE ) );
+
+ // a factory with an empty queue is the spare
+ CHECK( !aiWantsAnotherFactory( 2, 0, 1, 3, FALSE ) );
+
+ // every finished factory is busy, and a unit is waiting behind the one being built
+ CHECK( aiWantsAnotherFactory( 2, 0, 0, 2, FALSE ) );
+
+ // busy, but the queue is only the unit under construction
+ CHECK( !aiWantsAnotherFactory( 1, 0, 0, 1, FALSE ) );
+
+ // airfields and the income buildings: another whenever one is not already going up,
+ // even while a finished one sits idle
+ CHECK( aiWantsAnotherFactory( 3, 0, 2, 0, TRUE ) );
+ CHECK( !aiWantsAnotherFactory( 3, 1, 0, 5, TRUE ) );
+
+ CHECK( aiWantsAnotherTechBuilding( 0, 0 ) );
+ CHECK( aiWantsAnotherTechBuilding( AI_TECH_BUILDING_COPIES - 1, 0 ) );
+ CHECK( !aiWantsAnotherTechBuilding( 2, 1 ) );
+ CHECK( !aiWantsAnotherTechBuilding( AI_TECH_BUILDING_COPIES, 0 ) );
+
+ TAiData ladder;
+ CHECK( !ladder.m_skill[ AISKILL_EASY ].m_economyBuildings );
+ CHECK( !ladder.m_skill[ AISKILL_MEDIUM ].m_economyBuildings );
+ CHECK( ladder.m_skill[ AISKILL_BRUTAL ].m_economyBuildings );
+}
+
+
/** B2: which enemy to go after. EA's answer was the nearest one, plus a rule with the sign the
wrong way round - an enemy who had lost his units or his production had his distance set to half
the map, which is "ignore the one you are about to beat" and is what drags matches out. */