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): second factory only after a sustained queue backlog

Olcay Seyganff80b1d0eb22
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 14218a416..f0a593a1f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -258,7 +258,7 @@ found and fixed — EA's own, not port damage.**
- It no longer sits on a pile of cash. Past a level that depends on the difficulty, the more money it has the faster it builds - twice the pile, half the wait, and it stops at four times. It is spending sooner, not building faster: the rate itself is untouched.
- A full bank buys a bigger army. Late in a match the computer's plan allows one of each attack group, so once they were all on the map it only replaced its losses and banked the rest: a Hard USA sat on 117,000 on Twilight Flame. Now every 8,000 in the bank on Hard allows one more copy of each attack group, up to four of them.
-- Hard builds past its plan. A tank factory or a barracks gets another copy when units are waiting in its queue, and a factory that was destroyed is put back. Airfields keep going up for as long as the money is there. Supply drop zones and black markets go up one after another as soon as the bank is past a small cushion, whatever else it is buying; they used to wait until production had nothing to ask for, which on a busy map was never, and over twelve four-player matches the count went from 5 to 95. Internet centers follow that same rule. The spot search used to give up two rings outside the opening base, so a full base simply stopped. It now keeps looking further out until the map ends, for factories and power plants; the income buildings stay at home. Strategy centers, propaganda centers and palaces stop at three, so one of them blowing up leaves the tech tree standing. Extra factories still go beside the newest expansion it holds, nearer the fighting. China trains a hacker from every barracks that has room in its queue, not one barracks at a time: 120 hackers across those matches became 375. A hacker left standing still is put to work. It stops at six, an internet center's four plus a couple, because nothing counted them before: a Hard Tank General trained 67 over twenty thousand frames, about two thirds of everything it spent, and every one of them went through the barracks queue its army was waiting in. A player watching it from the other side of the map said China's tank general builds nothing but infantry, and he was right. Seven now, and the teams at the top of its build list are tanks. The limit grows with the match: one hacker for every four fighting units, never more than its Internet Centers have seats for plus four working outside, and never fewer than four. A computer that has not built an army does not buy an economy instead, and one with a full Internet Center and ninety units behind it keeps eight. Replays recorded before this change play out differently.
+- Hard builds past its plan. A war factory, barracks or airfield gets another copy only once every one of its kind has kept units waiting in its queue for 45 seconds, and a factory that was destroyed is put back. The second barracks used to be an opening buy. The Infantry General's plan holds four, and its scripts asked for the second 38 seconds in, before the first had trained anyone, then sat on an empty bank waiting for the third. Now a second factory waits for the queue on every difficulty, the plan's own copies included, and in a Hard Infantry General match on Twilight Flame the second barracks went up at eleven and a half minutes. Supply drop zones and black markets go up one after another as soon as the bank is past a small cushion, whatever else it is buying; they used to wait until production had nothing to ask for, which on a busy map was never, and over twelve four-player matches the count went from 5 to 95. Internet centers follow that same rule. The spot search used to give up two rings outside the opening base, so a full base simply stopped. It now keeps looking further out until the map ends, for factories and power plants; the income buildings stay at home. Strategy centers, propaganda centers and palaces stop at three, so one of them blowing up leaves the tech tree standing. Extra factories still go beside the newest expansion it holds, nearer the fighting. China trains a hacker from every barracks that has room in its queue, not one barracks at a time: 120 hackers across those matches became 375. A hacker left standing still is put to work. It stops at six, an internet center's four plus a couple, because nothing counted them before: a Hard Tank General trained 67 over twenty thousand frames, about two thirds of everything it spent, and every one of them went through the barracks queue its army was waiting in. A player watching it from the other side of the map said China's tank general builds nothing but infantry, and he was right. Seven now, and the teams at the top of its build list are tanks. The limit grows with the match: one hacker for every four fighting units, never more than its Internet Centers have seats for plus four working outside, and never fewer than four. A computer that has not built an army does not buy an economy instead, and one with a full Internet Center and ninety units behind it keeps eight. Replays recorded before this change play out differently.
- Hard saves up for what it needs most. It used to buy whatever the bank covered the moment it covered it, so the drop zone it needed waited while tank after tank rolled off the line. Every purchase is ranked now: income and power come first for the opening six minutes, the army after that, and when the top one cannot be paid for yet its price is set aside and anything that ranks lower waits until the bank holds more than that price. The saving never turns into a hoard. It lapses after forty seconds, and once the bank is past Hard's cash cushion nothing waits at all. The first drop zone and the first black market also go up in those opening minutes without waiting for the cushion. Over twenty matches with Hard computers on Winter Wolf, 27 computer players had a drop zone or black market standing where 19 had, the first one came at five and a half minutes instead of seven and a half, and 104 of them went up against 46. Replays recorded before this change play out differently.
- The computer researches upgrades on its own, and spends its bank. Upgrades used to come only from a fixed list in its plan, written before anyone knew what it would build. It now looks at the army it actually has and buys the upgrade that army gets the most out of for the price, and an army that beats what it has seen of yours counts for more. Buildings count for little there, so a GLA no longer opens with Fortified Structure. It also keeps a bank of so many seconds of its income and spends anything past that: an upgrade it would otherwise have waited on, extra units from idle factories, and on Hard another war factory or barracks whenever half a minute goes by without the bank coming down. Hard holds half a minute of income, spends what is over it on extra units at up to three times the rate it earns, builds up to eight factories, one every half minute whenever its bank passes twice what it means to keep, and starts upgrading at three minutes. Medium holds a minute, tops its army up at a tenth of Hard's rate, keeps to the factories its plan lays out and starts upgrading at seven. Easy holds two minutes, tops up at a twentieth of Hard's rate, keeps to its plan's factories too and waits twelve minutes and for a much bigger army before it buys an upgrade. Saving up for the most important purchase, which only Hard did, now runs on every difficulty, each below its own bank. Over six matches of Hard against Medium, on a build one small fix short of this one (Medium and Easy now count the factories their plan has yet to build), Hard ended on 1,600 in the bank on average where it had ended on about 12,000, and won all three that finished. Replays recorded before this change play out differently.
- Hard picks its road into your base. Of the three approaches it takes the one with the fewest guns it has seen along it, and keeps to the one its plan named until it has scouted something. Aiming at your money instead was tried first and walked it straight past your army.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
index 7d17789d6..d7121f3f4 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
@@ -352,14 +352,19 @@ AIFallbackDecision aiRetreatHoldDecision( Bool arrived, UnsignedInt heldFrames,
Bool aiShouldMass( Real waitingThreat, Real enemyVisibleThreat, Real massFraction,
Bool timeExpired, Bool baseUnderAttack );
+/** How long every finished factory of a kind has to keep a unit waiting behind the one it is building
+ * before the computer buys another. A script's team lands in the queue all at once, so a backlog
+ * the moment it appears is a batch, not a factory falling behind. */
+static const Int AI_FACTORY_BACKLOG_SECONDS = 45;
+
/** 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.
+ * way, shallowest is the shortest queue among the finished ones, and backedUpFor is how many frames
+ * every one of them has had a unit waiting behind the one it is building.
*
- * 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 );
+ * Another goes up only when every finished factory has had a unit waiting for
+ * AI_FACTORY_BACKLOG_SECONDS, and a replacement when none are left. One already on the way is the
+ * building this would ask for. */
+Bool aiWantsAnotherFactory( Int finished, Int constructing, Int shallowest, UnsignedInt backedUpFor );
/** 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. */
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
index 20b4dd5e3..ea03695a5 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
@@ -367,6 +367,7 @@ protected:
Int m_bankMark; ///< ... and the bank then
Real m_incomePerSecond; ///< smoothed over the windows measured so far
Int m_startFortune; ///< what the match started with over the default starting cash, 0 at the default
+ UnsignedInt m_factoryBackedUpSince[ 3 ]; ///< per war factory, barracks, airfield: the frame every finished one was first seen with a unit waiting, 0 for not
Bool purchaseAllowed(Int key, Int cost, AIPurchaseKind kind, Bool startable, const AsciiString &what);
void purchased(Int key, Int cost, AIPurchaseKind kind, const AsciiString &what);
Bool buyUnit(ProductionUpdateInterface *pu, const ThingTemplate *unit, AIPurchaseKind kind); ///< queueCreateUnit through the queue
@@ -386,7 +387,8 @@ protected:
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, Bool walkOutward); ///< the nearest legal, safe spot by the ground from center, queued for a dozer. walkOutward keeps searching further out as the near ground fills
- Bool queueExtraFactory(Object *dozer, KindOfType kind, Bool unlimited); ///< one more of this factory, beside a held expansion or around the base
+ Bool queueExtraFactory(Object *dozer, KindOfType kind); ///< one more of this factory, beside a held expansion or around the base, once the ones standing fall behind their queues
+ Bool holdsExtraFactory(const ThingTemplate *tmpl); ///< a factory of a kind already standing or going up, while the ones standing keep up with their queues
const ThingTemplate *nextBaseDefense(Object *dozer); ///< the base defence off this dozer's buttons that the base has fewest of, NULL for none
Bool placeDefense(const ThingTemplate *defense); ///< queue it on the ring spot whose clear field of fire covers the most ground the enemy drives in over
const std::vector<UnsignedShort> &groundFromBase(void); ///< AIGroundFlood distance of every pathfind cell from the base center, cached until the center moves
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
index 58080de8c..9130947fe 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
@@ -1437,17 +1437,15 @@ Bool aiShouldMass( Real waitingThreat, Real enemyVisibleThreat, Real massFractio
}
//-------------------------------------------------------------------------------------------------
-Bool aiWantsAnotherFactory( Int finished, Int constructing, Int idle, Int deepest, Bool unlimited )
+Bool aiWantsAnotherFactory( Int finished, Int constructing, Int shallowest, UnsignedInt backedUpFor )
{
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
+ if( shallowest < 2 )
+ return FALSE; // one of them has room: nothing is waiting behind what it builds
+ return backedUpFor >= (UnsignedInt)( AI_FACTORY_BACKLOG_SECONDS * LOGICFRAMES_PER_SECOND );
}
//-------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index bb862c837..c5dec9f21 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -393,6 +393,8 @@ m_role(AIROLE_AGGRESSIVE)
m_bankMark = 0;
m_incomePerSecond = 0.0f;
m_startFortune = 0;
+ for( Int k = 0; k < 3; ++k )
+ m_factoryBackedUpSince[ k ] = 0;
m_healerSeconds = 0;
m_healerClearSeconds = 0;
m_captureTimer = 1;
@@ -5766,11 +5768,14 @@ struct FactoryLoad
KindOfType kind;
Int finished;
Int constructing;
- Int idle;
- Int deepest;
+ Int shallowest;
};
-static void readFactoryLoad( Object *obj, void *userData )
+/** The production kinds the computer buys extra copies of, in m_factoryBackedUpSince's order. */
+static const Int PRODUCTION_KINDS = 3;
+static const KindOfType PRODUCTION_KIND[ PRODUCTION_KINDS ] = { KINDOF_FS_WARFACTORY, KINDOF_FS_BARRACKS, KINDOF_FS_AIRFIELD };
+
+static void countFactoryLoad( Object *obj, void *userData )
{
FactoryLoad *load = (FactoryLoad *)userData;
if( obj->isEffectivelyDead() || !obj->isKindOf( load->kind ) )
@@ -5783,12 +5788,32 @@ static void readFactoryLoad( Object *obj, void *userData )
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;
+ if( load->finished == 0 || queued < load->shallowest )
+ load->shallowest = queued;
+ ++load->finished;
+}
+
+static FactoryLoad readFactoryLoad( Player *player, KindOfType kind )
+{
+ FactoryLoad load = { kind, 0, 0, 0 };
+ player->iterateObjects( countFactoryLoad, &load );
+ return load;
+}
+
+/** Factories of this kind the build list has yet to put up, which go up before any extra one. */
+static Int plannedOfKind( Player *player, KindOfType kind )
+{
+ Int planned = 0;
+ for( BuildListInfo *info = player->getBuildList(); info; info = info->getNext() )
+ {
+ if( info->getObjectID() != INVALID_ID || !info->isBuildable() )
+ continue;
+ const ThingTemplate *tmpl = TheThingFactory->findTemplate( info->getTemplateName(), FALSE );
+ if( tmpl && tmpl->isKindOf( kind ) )
+ ++planned;
+ }
+ return planned;
}
struct TechCount
@@ -6269,13 +6294,12 @@ void AIPlayer::doEconomy( void )
const Int INCOME_KINDS = 3;
static const KindOfType INCOME[ INCOME_KINDS ] = {
KINDOF_FS_SUPPLY_DROPZONE, KINDOF_FS_BLACK_MARKET, KINDOF_FS_INTERNET_CENTER };
- Bool homeFull = FALSE; // an income building was wanted and the base had no room for it
for( Int i = 0; i < INCOME_KINDS; ++i )
{
const ThingTemplate *tmpl = buildableOfKind( dozer, GUI_COMMAND_DOZER_CONSTRUCT, INCOME[ i ] );
// from half way out to the edge: placeNear keeps these inside the base (staysHome)
- if( tmpl && !priorityBuildPending( m_player, tmpl ) && !placeNear( tmpl, &m_baseCenter, 0.5f * m_baseRadius, FALSE ) )
- homeFull = TRUE;
+ if( tmpl && !priorityBuildPending( m_player, tmpl ) )
+ placeNear( tmpl, &m_baseCenter, 0.5f * m_baseRadius, FALSE );
}
/* A gun on the side the trouble comes from, before the factories the pass buys. Production and
@@ -6308,30 +6332,15 @@ void AIPlayer::doEconomy( void )
if( m_player->getMoney()->countMoney() <= 2 * profile->m_cashHoardThreshold )
return;
- /* 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 )
- {
- const Bool airPass = (pass == 0) == airFirst;
- if( airPass )
- {
- if( queueExtraFactory( dozer, KINDOF_FS_AIRFIELD, TRUE ) )
- return;
- }
- else
- {
- // The income building the base had no room for used to go up out on the map, and that was
- // where a GLA's money went: it has no airfield to spend on, and one banked 136,939 by the
- // end of a match. With home full the money goes into production, a factory a pass whether
- // or not its queue is backing up, the way the airfields always have.
- for( Int i = 0; i < 2; ++i )
- if( queueExtraFactory( dozer, QUEUED_KINDS[ i ], homeFull ) )
- return;
- }
- }
+ /* Another factory only once the ones standing fall behind their queues (aiWantsAnotherFactory).
+ Airfields, and war factories and barracks while the base had no room for an income building,
+ used to get one a pass whatever their queues, and the opening spent itself on a second barracks
+ with the first one empty. The kind asked first turns with each pass, so a queue that never
+ empties does not keep the others waiting. */
+ const Int first = (Int)( ((TheGameLogic->getFrame() + phase) / ECONOMY_CHECK_RATE) % PRODUCTION_KINDS );
+ for( Int i = 0; i < PRODUCTION_KINDS; ++i )
+ if( queueExtraFactory( dozer, PRODUCTION_KIND[ (first + i) % PRODUCTION_KINDS ] ) )
+ return;
}
/** How near a point one of this player's tunnels has to stand to count as covering it. */
@@ -6857,22 +6866,13 @@ 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. */
+ * center, or the first crowded dock skips the base itself. Nothing while the build list still has one
+ * of this kind to put up, since that one goes first, and then only when holdsExtraFactory lets it. */
//----------------------------------------------------------------------------------------------------------
-Bool AIPlayer::queueExtraFactory( Object *dozer, KindOfType kind, Bool unlimited )
+Bool AIPlayer::queueExtraFactory( Object *dozer, KindOfType kind )
{
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 ) )
+ if( tmpl == NULL || plannedOfKind( m_player, kind ) > 0 || holdsExtraFactory( tmpl ) )
return FALSE;
const Object *warehouse = TheGameLogic->findObjectByID( m_curWarehouseID );
@@ -6883,6 +6883,27 @@ Bool AIPlayer::queueExtraFactory( Object *dozer, KindOfType kind, Bool unlimited
return placeNear( tmpl, &m_baseCenter, m_baseRadius, TRUE );
}
+//----------------------------------------------------------------------------------------------------------
+/** A second factory of a kind waits until every finished one has kept a unit waiting for
+ * AI_FACTORY_BACKLOG_SECONDS (aiWantsAnotherFactory; doSpending keeps that clock), and while one is
+ * going up. The build list's own copies wait the same way: the Infantry General's plan holds four
+ * barracks, and its scripts asked for the second 38 seconds in, before the first had trained anyone,
+ * and the bank went to nothing waiting on the third. */
+//----------------------------------------------------------------------------------------------------------
+Bool AIPlayer::holdsExtraFactory( const ThingTemplate *tmpl )
+{
+ for( Int k = 0; k < PRODUCTION_KINDS; ++k )
+ {
+ if( !tmpl->isKindOf( PRODUCTION_KIND[ k ] ) )
+ continue;
+ const FactoryLoad load = readFactoryLoad( m_player, PRODUCTION_KIND[ k ] );
+ const UnsignedInt since = m_factoryBackedUpSince[ k ];
+ return !aiWantsAnotherFactory( load.finished, load.constructing, load.shallowest,
+ since == 0 ? 0 : TheGameLogic->getFrame() - since );
+ }
+ return FALSE;
+}
+
//----------------------------------------------------------------------------------------------------------
/** Flood out from center over ground a vehicle can cross, nearest first by the ground, until a spot
* is legal and safe, and hand it to the dozers as a priority build. It used to try twelve angles
@@ -7732,6 +7753,17 @@ void AIPlayer::doSpending( void )
if( (now + computeUpdatePhase( m_player->getPlayerIndex(), ECONOMY_CHECK_RATE )) % ECONOMY_CHECK_RATE != 0 )
return;
+ // since when every finished factory of each kind has had a unit waiting behind the one it builds,
+ // read on every pass so a queue that ran dry in between starts the clock again (queueExtraFactory)
+ for( Int k = 0; k < PRODUCTION_KINDS; ++k )
+ {
+ const FactoryLoad load = readFactoryLoad( m_player, PRODUCTION_KIND[ k ] );
+ if( load.finished == 0 || load.shallowest < 2 )
+ m_factoryBackedUpSince[ k ] = 0;
+ else if( m_factoryBackedUpSince[ k ] == 0 )
+ m_factoryBackedUpSince[ k ] = now;
+ }
+
// the income, off what the score screen counts as earned: gathered, hacked, captured and given
const Int bank = (Int)m_player->getMoney()->countMoney();
const Int earned = m_player->getScoreKeeper()->getTotalMoneyEarned();
@@ -7888,45 +7920,28 @@ void AIPlayer::buyUpgradesForArmy( Bool overTarget )
* target (aiWantsMoreProduction): what stands is not spending what comes in. One at a time, and no more than the rung's m_maxProduction war
* factories, barracks and airfields together. A guessed spend rate a factory stood here first: a
* Hard AI earning 93 a second wanted the two factories it had. Unbuilt plan entries count as standing,
- * or the rule adds the factory the plan is about to put up. */
+ * or the rule adds the factory the plan is about to put up. A bank alone is no reason: the kind's
+ * own factories have to be falling behind their queues too (queueExtraFactory), or a match started on
+ * a big bank opened on a second barracks before the first had trained anyone. */
//----------------------------------------------------------------------------------------------------------
void AIPlayer::addProductionForIncome( Int bank, Int bankWindowAgo, Int target )
{
if( !m_player->getCanBuildBase() || !m_baseCenterSet )
return;
- const Int KINDS = 3;
- const KindOfType PRODUCTION[ KINDS ] = { KINDOF_FS_WARFACTORY, KINDOF_FS_BARRACKS, KINDOF_FS_AIRFIELD };
- Int standing[ KINDS ];
+ Int standing[ PRODUCTION_KINDS ];
Int total = 0;
- for( Int k = 0; k < KINDS; ++k )
- {
- FactoryLoad load;
- load.kind = PRODUCTION[ k ];
- load.finished = 0;
- load.constructing = 0;
- load.idle = 0;
- load.deepest = 0;
- m_player->iterateObjects( readFactoryLoad, &load );
- if( load.constructing > 0 )
- return; // one at a time
- standing[ k ] = load.finished;
- total += load.finished;
- }
// ... and the ones the plan has yet to put up count as standing: early on a Medium plan of two war
// factories and a barracks had two up, and the rule added the third the plan was about to build
Int planned = 0;
- for( BuildListInfo *info = m_player->getBuildList(); info; info = info->getNext() )
+ for( Int k = 0; k < PRODUCTION_KINDS; ++k )
{
- if( info->getObjectID() != INVALID_ID || !info->isBuildable() )
- continue;
- const ThingTemplate *tmpl = TheThingFactory->findTemplate( info->getTemplateName(), FALSE );
- for( Int k = 0; tmpl && k < KINDS; ++k )
- if( tmpl->isKindOf( PRODUCTION[ k ] ) )
- {
- ++planned;
- break;
- }
+ const FactoryLoad load = readFactoryLoad( m_player, PRODUCTION_KIND[ k ] );
+ if( load.constructing > 0 )
+ return; // one at a time
+ standing[ k ] = load.finished;
+ total += load.finished;
+ planned += plannedOfKind( m_player, PRODUCTION_KIND[ k ] );
}
total += planned;
const Int cap = getSkillProfile()->m_maxProduction;
@@ -7940,8 +7955,8 @@ void AIPlayer::addProductionForIncome( Int bank, Int bankWindowAgo, Int target )
const Int first = standing[ 1 ] < standing[ 0 ] ? 1 : 0;
for( Int k = 0; k < 2; ++k )
{
- const KindOfType kind = PRODUCTION[ k == 0 ? first : 1 - first ];
- if( queueExtraFactory( dozer, kind, TRUE ) )
+ const KindOfType kind = PRODUCTION_KIND[ k == 0 ? first : 1 - first ];
+ if( queueExtraFactory( dozer, kind ) )
{
DEBUG_LOG(("AI ECONOMY frame %d player %d puts up another %s, %d of at most %d standing (%d of them planned), %d in the bank (%d a window ago), bank target %d, %.0f a second of income\n",
TheGameLogic->getFrame(), m_player->getPlayerIndex(), kind == KINDOF_FS_WARFACTORY ? "war factory" : "barracks",
@@ -14437,7 +14452,7 @@ void AIPlayer::xfer( Xfer *xfer )
{
// version
- XferVersion currentVersion = 24; // 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 12: the pressure level 13: falling back 14: gunship Chinooks and the boarding wait 15: transport Chinooks and the lent one 16: healer and raider Helixes 17: the seen-enemy ledger 18: the fist 19: the Ambulances 20: the last Ambulance lost 21: the purchase queue's reservation 22: the measured income 23: the Ambulances bought 24: the starting fortune
+ XferVersion currentVersion = 25; // 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 12: the pressure level 13: falling back 14: gunship Chinooks and the boarding wait 15: transport Chinooks and the lent one 16: healer and raider Helixes 17: the seen-enemy ledger 18: the fist 19: the Ambulances 20: the last Ambulance lost 21: the purchase queue's reservation 22: the measured income 23: the Ambulances bought 24: the starting fortune 25: the factory backlog clocks
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
@@ -14842,6 +14857,8 @@ void AIPlayer::xfer( Xfer *xfer )
xfer->xferInt( &m_ambulancesBought );
if( version >= 24 )
xfer->xferInt( &m_startFortune );
+ if( version >= 25 )
+ xfer->xferUser( m_factoryBackedUpSince, sizeof( m_factoryBackedUpSince ) );
// 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/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp
index 6da8bb08f..5ee3ac540 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp
@@ -236,6 +236,11 @@ void AISkirmishPlayer::processBaseBuilding( void )
if (bldg) {
continue; // already built.
}
+ // A second war factory, barracks or airfield of the plan's waits until the ones standing
+ // fall behind their queues, as an extra one would.
+ if (holdsExtraFactory(curPlan)) {
+ continue;
+ }
// The safety check below is a range query over everything near the spot, most of a
// millisecond each late in a 4v4, and it ran for every unbuilt entry on every pass (36 of
// them, 28ms, in one frame). An entry that cannot change what this pass picks skips it.
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 4a04a20b7..bed6f5c99 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -12262,30 +12262,31 @@ TEST(ground_flood_goes_round_the_wall_nearest_first)
CHECK_EQ( count, 3 ); // the source and its two side neighbours
}
-/** 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. */
+/** Another factory only when every one standing has kept a unit waiting for AI_FACTORY_BACKLOG_SECONDS,
+ and another tech building until three are standing. */
TEST(extra_factory_follows_the_queue_and_tech_stops_at_three)
{
+ const UnsignedInt BACKLOG = AI_FACTORY_BACKLOG_SECONDS * LOGICFRAMES_PER_SECOND;
+
// nothing left standing: put one back
- CHECK( aiWantsAnotherFactory( 0, 0, 0, 0, FALSE ) );
+ CHECK( aiWantsAnotherFactory( 0, 0, 0, 0 ) );
// 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 ) );
+ CHECK( !aiWantsAnotherFactory( 0, 1, 0, 0 ) );
+ CHECK( !aiWantsAnotherFactory( 2, 1, 4, BACKLOG ) );
- // every finished factory is busy, and a unit is waiting behind the one being built
- CHECK( aiWantsAnotherFactory( 2, 0, 0, 2, FALSE ) );
+ // one with an empty queue, or only the unit it is building, has room
+ CHECK( !aiWantsAnotherFactory( 2, 0, 0, BACKLOG ) );
+ CHECK( !aiWantsAnotherFactory( 1, 0, 1, BACKLOG ) );
- // busy, but the queue is only the unit under construction
- CHECK( !aiWantsAnotherFactory( 1, 0, 0, 1, FALSE ) );
+ // a team landing in the queue all at once is a batch, not a factory falling behind: the
+ // opening's training no longer buys a second barracks
+ CHECK( !aiWantsAnotherFactory( 1, 0, 5, 0 ) );
+ CHECK( !aiWantsAnotherFactory( 1, 0, 5, BACKLOG - 1 ) );
- // 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 ) );
+ // every finished one has kept a unit waiting the whole time
+ CHECK( aiWantsAnotherFactory( 1, 0, 2, BACKLOG ) );
+ CHECK( aiWantsAnotherFactory( 3, 0, 2, BACKLOG + 600 ) );
CHECK( aiWantsAnotherTechBuilding( 0, 0 ) );
CHECK( aiWantsAnotherTechBuilding( AI_TECH_BUILDING_COPIES - 1, 0 ) );