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
fix(gameengine): count a factory busy if it let a unit out since the last pass
Olcay Seyganff1dfa8c5b8e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex cec2accc1..26335c9fc 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -365,7 +365,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 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.-- The computer builds more than one barracks, war factory and airfield again, on every difficulty. Since 3.2.0 it stood one of each all match, even where its own plan had two war factories or four barracks: another one waited for units to queue up behind the one being built, and the computer never queues them that way. Every ten seconds it looks at each kind, and once every one it has was busy on four looks in five over the last 90 seconds on Easy, 75 on Medium and 45 on Hard, another goes up; a new one has to fill a whole stretch like that before the next. A factory caught between two units no longer starts the wait again, which is why Easy, whose units come out inside ten seconds, never got a second one. An airfield goes by its parking instead, since a full one takes nothing into its queue: it counts as busy while every airfield has its places taken and a plane is waiting for one. There is no limit on how many: an army that keeps all its factories busy gets more of them. The old cap of three, five and eight production buildings and the bank rule that added them are gone. Replays recorded before this change play out differently.+- The computer builds more than one barracks, war factory and airfield again, on every difficulty. Since 3.2.0 it stood one of each all match, even where its own plan had two war factories or four barracks: another one waited for units to queue up behind the one being built, and the computer never queues them that way. Every ten seconds it looks at each kind, and once every one it has was busy on four looks in five over the last 90 seconds on Easy, 75 on Medium and 45 on Hard, another goes up; a new one has to fill a whole stretch like that before the next. A factory counts as busy on a look when it has something in its queue or let a unit out since the last look, so one caught between two units no longer starts the wait again; Easy, whose soldiers come out inside ten seconds, was found idle on most looks and never got a second one. An airfield goes by its parking instead, since a full one takes nothing into its queue: it counts as busy while every airfield has its places taken and a plane is waiting for one. There is no limit on how many: an army that keeps all its factories busy gets more of them. The old cap of three, five and eight production buildings and the bank rule that added them are gone. 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.hindex 2c9b52798..d840d8788 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h@@ -432,11 +432,12 @@ inline void aiSampleFactories( UnsignedInt *history, Int *count, Bool full, Int++*count;}-/** A barracks or war factory is full while it is busy: shallowest is the shortest queue among the- * finished ones. Busy, not backed up: the scripts train only into an idle factory and the surplus- * fills a queue one deep, so a unit waiting behind the one being built almost never happened, and- * the computer stood one barracks and one war factory all match. */-inline Bool aiQueueKindFull( Int finished, Int shallowest ) { return finished > 0 && shallowest >= 1; }+/** Barracks or war factories are full on a pass when every finished one (busy of finished) was busy:+ * something in its queue, or a unit let out since the last pass. Busy, not backed up: the scripts+ * train only into an idle factory and the surplus fills a queue one deep, so a unit waiting behind+ * the one being built almost never happened. And busy over the pass, not at its instant: units that+ * finish inside a pass left an Easy factory idle on most looks. */+inline Bool aiQueueKindFull( Int finished, Int busy ) { return finished > 0 && busy >= finished; }/** An airfield is full when its parking is: every finished one (parkingFull of finished) refuses an* aircraft to its queue, and the computer has wanted one lately (wanted, a buy refused for parking).diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.hindex 0a80e0263..037ed355f 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h@@ -382,6 +382,7 @@ protected:Int m_startFortune; ///< what the match started with over the default starting cash, 0 at the defaultUnsignedInt m_factoryFullHistory[ 3 ]; ///< per war factory, barracks, airfield: a bit a spend pass, newest lowest, set when every finished one was full (busy, or an airfield's parking) (aiSampleFactories)Int m_factoryFullCount[ 3 ]; ///< ... and how many passes that holds since one of the kind last went up+ std::vector<ObjectID> m_producedSincePass; ///< the barracks and war factories that let a unit out since the last spend pass (onUnitProduced)UnsignedInt m_parkingBlockedFrame; ///< the last frame a buy of an aircraft was refused because the airfield's parking was full, 0 for neverstd::vector<AISiteWatch> m_siteWatches; ///< the unfinished buildings this AI placed, in the order first seenstd::vector<AICooledSpot> m_cooledSpots; ///< spots of sites it gave up on, left alone for a whilediff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cppindex 32e573350..28aeafb95 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp@@ -1988,6 +1988,12 @@ void AIPlayer::onUnitProduced( Object *factory, Object *unit )return;}+ // a barracks or war factory that let a unit out was busy on this spend pass, whatever its queue+ // reads when the pass looks (doSpending, which empties the list and runs only with the purchase queue)+ if( purchaseQueueOn() && ( factory->isKindOf( KINDOF_FS_WARFACTORY ) || factory->isKindOf( KINDOF_FS_BARRACKS ) ) &&+ std::find( m_producedSincePass.begin(), m_producedSincePass.end(), factory->getID() ) == m_producedSincePass.end() )+ m_producedSincePass.push_back( factory->getID() );+for (DLINK_ITERATOR<TeamInQueue> iter = iterate_TeamBuildQueue(); !iter.done(); iter.advance()){TeamInQueue *team = iter.cur();@@ -6533,8 +6539,9 @@ struct FactoryLoadKindOfType kind;Int finished;Int constructing;- Int shallowest;+ Int busy; ///< finished ones with something in the queue, or that let a unit out since the last spend passInt parkingFull; ///< finished ones with no parking place left, which refuse an aircraft to their queue+ const std::vector<ObjectID> *producedSincePass; ///< the factories that let a unit out since the last spend pass};/** The production kinds the computer buys extra copies of, in m_factoryBackedUpSince's order. */@@ -6611,9 +6618,11 @@ static void countFactoryLoad( Object *obj, void *userData )ProductionUpdateInterface *pu = obj->getProductionUpdateInterface();if( pu == NULL )return;- const Int queued = (Int)pu->getProductionCount();- if( load->finished == 0 || queued < load->shallowest )- load->shallowest = queued;+ // busy at this instant, or between two units: an Easy barracks that trains a Red Guard inside a pass+ // was found idle on most of them and never counted as busy+ const std::vector<ObjectID> &produced = *load->producedSincePass;+ if( pu->getProductionCount() > 0 || std::find( produced.begin(), produced.end(), obj->getID() ) != produced.end() )+ ++load->busy;++load->finished;// the same question ProductionUpdate::canQueueCreateUnit asks before an aircraft goes in the queue;// the answer depends only on whether the thing asked about is made at a helipad, and the airfield@@ -6629,19 +6638,22 @@ static void countFactoryLoad( Object *obj, void *userData )}}-static FactoryLoad readFactoryLoad( Player *player, KindOfType kind )+static const std::vector<ObjectID> s_noneProduced;++static FactoryLoad readFactoryLoad( Player *player, KindOfType kind, const std::vector<ObjectID> *producedSincePass = &s_noneProduced ){- FactoryLoad load = { kind, 0, 0, 0, 0 };+ FactoryLoad load = { kind, 0, 0, 0, 0, producedSincePass };player->iterateObjects( countFactoryLoad, &load );return load;}-/** Whether every finished factory of the kind is full now: a barracks or war factory by its queue, an- * airfield by its parking while a buy was refused for it within AI_PARKING_WANT_FRAMES. */+/** Whether every finished factory of the kind was full on this pass: a barracks or war factory busy+ * now or since the last pass, an airfield by its parking while a buy was refused for it within+ * AI_PARKING_WANT_FRAMES. */static Bool factoryKindFull( const FactoryLoad &load, UnsignedInt parkingBlockedFrame ){if( load.kind != KINDOF_FS_AIRFIELD )- return aiQueueKindFull( load.finished, load.shallowest );+ return aiQueueKindFull( load.finished, load.busy );const Bool wanted = parkingBlockedFrame != 0 && TheGameLogic->getFrame() - parkingBlockedFrame <= AI_PARKING_WANT_FRAMES;return aiAirfieldsFull( load.finished, load.parkingFull, wanted );}@@ -8802,12 +8814,24 @@ void AIPlayer::doSpending( void )// whether every finished factory of each kind is full on this pass, into a rolling window the gate// reads as a share (queueExtraFactory); one going up empties it, so the new one has to prove itself// full too+ const Int window = aiBacklogSamples( getSkillProfile()->m_factoryBacklogSeconds, ECONOMY_CHECK_RATE );for( Int k = 0; k < PRODUCTION_KINDS; ++k ){- const FactoryLoad load = readFactoryLoad( m_player, PRODUCTION_KIND[ k ] );+ const FactoryLoad load = readFactoryLoad( m_player, PRODUCTION_KIND[ k ], &m_producedSincePass );aiSampleFactories( &m_factoryFullHistory[ k ], &m_factoryFullCount[ k ],factoryKindFull( load, m_parkingBlockedFrame ), load.constructing );+ if( load.finished > 0 && m_factoryFullCount[ k ] > 0 && m_factoryFullCount[ k ] % window == 0 )+ {+ Int full = 0;+ for( Int i = 0; i < window; ++i )+ if( m_factoryFullHistory[ k ] & ( 1u << i ) )+ ++full;+ DEBUG_LOG(("AI FACTORY frame %d player %d %s full on %d of the last %d passes, %d standing, %d in the bank\n",+ now, m_player->getPlayerIndex(), k == 0 ? "war factory" : k == 1 ? "barracks" : "airfield",+ full, window, load.finished, m_player->getMoney()->countMoney()));+ }}+ m_producedSincePass.clear();// the income, off what the score screen counts as earned: gathered, hacked, captured and givenconst Int bank = (Int)m_player->getMoney()->countMoney();@@ -16699,7 +16723,7 @@ void AIPlayer::xfer( Xfer *xfer ){// version- XferVersion currentVersion = 30; // 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 26: the personality 27: the team request inbox 28: the last aircraft refused for parking 29: the watched sites and the cooled spots 30: the factory backlog as a window of passes+ XferVersion currentVersion = 31; // 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 26: the personality 27: the team request inbox 28: the last aircraft refused for parking 29: the watched sites and the cooled spots 30: the factory backlog as a window of passes 31: the factories that let a unit out since the last passXferVersion version = currentVersion;xfer->xferVersion( &version, currentVersion );@@ -17189,6 +17213,16 @@ void AIPlayer::xfer( Xfer *xfer )}}+ // the factories that let a unit out since the last spend pass, which that pass counts as busy+ if( version >= 31 )+ {+ UnsignedShort count = (UnsignedShort)m_producedSincePass.size();+ xfer->xferUnsignedShort( &count );+ m_producedSincePass.resize( count );+ for( UnsignedShort i = 0; i < count; ++i )+ xfer->xferObjectID( &m_producedSincePass[ i ] );+ }+} // end xfer// ------------------------------------------------------------------------------------------------diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 2916b9b13..637d59b11 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -12649,11 +12649,12 @@ TEST(extra_factory_follows_the_queue)CHECK( !aiWantsAnotherFactory( 0, 1, 0, 0, HARD ) );CHECK( !aiWantsAnotherFactory( 2, 1, 0x1F, HARD, HARD ) );- // a barracks or war factory with an empty queue is the spare; busy, only the unit it builds in the- // queue, is full: the scripts never queue behind it, and a second factory never went up+ // barracks or war factories are full on a pass when every one was busy on it, its queue or a unit+ // let out since the last pass; one idle the whole pass is the spareCHECK( !aiQueueKindFull( 2, 0 ) );- CHECK( aiQueueKindFull( 2, 1 ) );- CHECK( !aiQueueKindFull( 0, 3 ) );+ CHECK( !aiQueueKindFull( 2, 1 ) );+ CHECK( aiQueueKindFull( 2, 2 ) );+ CHECK( !aiQueueKindFull( 0, 0 ) );// the window has to be in: four full passes are not a 45 s window, five areUnsignedInt history = 0;