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(gameengine): AI builds a steady mix of base defences, 12 per superweapon
Olcay Seygan9e00bb0e08af
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f179afd92..3d50fe189 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -181,8 +181,9 @@ found and fixed — EA's own, not port damage.**- It puts more harvesters on a supply centre with more piles around it, instead of the same three numbers everywhere.- USA's Chinooks stop collecting over the command center. When the pile beside a supply centre ran out, its Chinooks flew home and hovered, and the computer went on buying new ones for its other centres while those sat idle. It only looked for an idle gatherer when a pile stood right next to the centre, a fraction of how far a Chinook looks. An idle gatherer is now sent to any of its supply centres with a pile inside that gatherer's own range, and nothing new is bought while one stands idle beside a pile that has no room for it.- It keeps its lights on, and keeps them out of reach. Its plan carries a fixed number of power plants and nothing ever adds one, so a raid that took two of them left the base underpowered - radar dark, base defences off, superweapon clock stopped - until a rebuild timer came round and put the new plant back on the spot the raid already knew the way to. It now buys a plant when the margin gets thin rather than when the meter reads zero, and puts it on the far side of the base from whoever it is fighting.-- Spare cash also buys a gun on the side the trouble comes from. Production and income go up around the middle of the base, the power goes behind it, and base defences go out in front, so money that keeps arriving thickens the base in the order a base wants to be thick in. Front means towards the nearest enemy, which is not the same as the one it has decided to attack - that one can be across the map, and a wall built facing it has its back to the neighbour. The walls grow with the army. One gun every minute and a half, never more than ten, used to be the rule; now the computer may stand one gun for every four fighting units it has, so a big army comes home to a fortress and a beaten one rebuilds its tanks before its bunkers. Letting it buy guns on every pass instead was tried first: 241 guns over twelve four-player matches, and they ate the army, down to 60 kills a match from 73. Tied to the army, 71 guns, and the army is the biggest of any version tried, 78 kills a match.-- Hard builds its superweapon as soon as it can pay for one and has the guns to stand it on, and then another. Its scripts held the first one back behind a clock that ran for twelve and a half minutes, seven and a half if you had built your own tech building first, and then allowed exactly one: a player who took on seven Hard computers at once saw a single nuke all match. There is no clock now. The tech building that unlocks it goes up first, and the superweapon follows once the money is in the bank and four of its guns stand finished, a rule only the computer plays by (see the lobby section). More follow as the base grows, one for every four guns, so a computer holding twenty guns can stand five superweapons. Before that rule it put its first particle cannon up at 4.3 minutes on Twilight Flame and its first nuke at 4.9, without a gun to its name. Over 24 matches between two Hard computers on the same map, the first nuke now goes up at 6.2 minutes as a rule, anywhere from 4.4 to 9.8, and the first Particle Cannon at 7.4. The Scud Storm comes last at 7.6 but never later than 8.7, and every GLA side had one inside ten minutes, where 4 of the 32 China and USA sides were still waiting for theirs. The lobby's superweapon limit and Pro Rules still apply to the computer exactly as they do to you.+- Spare cash also buys a gun on the side the trouble comes from. Production and income go up around the middle of the base, the power goes behind it, and base defences go out in front, so money that keeps arriving thickens the base in the order a base wants to be thick in. Front means towards the nearest enemy, which is not the same as the one it has decided to attack - that one can be across the map, and a wall built facing it has its back to the neighbour. The walls grow with the army. One gun every minute and a half, never more than ten, used to be the rule; now the computer may stand one gun for every four fighting units it has, so a big army comes home to a fortress and a beaten one rebuilds its tanks before its bunkers. Letting it buy guns on every pass instead was tried first: 241 guns over twelve four-player matches, and they ate the army, down to 60 kills a match from 73. Tied to the army, 71 guns, and the army is the biggest of any version tried, 78 kills a match. A small army no longer means a bare base, though: one more gun is allowed every 40 seconds of the match on top of what the army earns, up to 24, and the computer buys them as soon as its bank clears its saving threshold rather than twice that. When there is no room out in front, the next gun goes 45 degrees round to either side, then 90, and when the whole front is taken it goes up inside the base.+- Its guns come as a mix. It used to put up whichever defence came first on the worker's build bar, every time: a Hard USA base put up 22 Patriots for every Fire Base, a GLA one Stinger Sites, and China's own spare-cash guns were bunkers. Now each new gun is the type the base has fewest of, so Patriots and Fire Bases go up in turn, Stinger Sites alternate with Tunnel Networks, and China's Gattling Cannons get bunkers and Speaker Towers beside them. Those two shoot at nothing on their own, so the computer keeps about one of each for every three cannons. A type lost in a raid is the one rebuilt first. Every general's own versions count, and the planned defences its build scripts ask for follow the same rule. Over 48 one-on-one matches between Hard and Normal computers and 24 between two Hards, the same seeds before and after, a Hard USA side built 4.0 guns a match where it built 2.4, 1.3 of them Fire Bases where it built 0.1. China went from 4.0 to 6.2, GLA held at 4.9. Hard won 25 of those 48 matches against Normal, as it did before. Replays recorded before this change play out differently.+- Hard builds its superweapon as soon as it can pay for one and has the guns to stand it on, and then another. Its scripts held the first one back behind a clock that ran for twelve and a half minutes, seven and a half if you had built your own tech building first, and then allowed exactly one: a player who took on seven Hard computers at once saw a single nuke all match. There is no clock now. The tech building that unlocks it goes up first, and the superweapon follows once the money is in the bank and twelve of its guns stand finished, a rule only the computer plays by (see the lobby section). More follow as the base grows, one for every twelve guns, so a computer holding twenty-four guns can stand two superweapons. The numbers below were measured when four guns were enough. With twelve, a one-on-one between computers rarely lasts long enough: over 96 Hard sides in the same 72 matches, 11 had a superweapon up before and 5 after, the first at 6.8 minutes where it had been 4.6. Before that rule it put its first particle cannon up at 4.3 minutes on Twilight Flame and its first nuke at 4.9, without a gun to its name. Over 24 matches between two Hard computers on the same map, the first nuke now goes up at 6.2 minutes as a rule, anywhere from 4.4 to 9.8, and the first Particle Cannon at 7.4. The Scud Storm comes last at 7.6 but never later than 8.7, and every GLA side had one inside ten minutes, where 4 of the 32 China and USA sides were still waiting for theirs. The lobby's superweapon limit and Pro Rules still apply to the computer exactly as they do to you.- Hard trains more builders when it has work for them. It only ever made a new dozer after losing all of them, so it played every match on the two it had, and every building it bought queued behind those two. On Twilight Flame a third of the China Tank computers never got round to a war factory, never sent an attack, and ended up sitting in a ring of bunkers on a pile of cash. Now, with money in the bank and every dozer busy, it trains another, up to four. Over six four-player matches on that map, every computer built its war factories, the attack waves went from 39 to 82, and each one spent half as much again.- Hard no longer rebuilds its factories inside your base. A busy computer puts its next war factory beside its newest expansion, and it remembered that expansion forever: after you destroyed the supply centre there, even one it had built right next to your base, it kept putting the factory back up on the same spot. It now only builds beside an expansion it still holds, and only one nearer its own base than yours.- Hard keeps its lights on. It asked for a new power plant before running out, and the request then sat in a queue behind everything else its plan wanted: one China waited 95 seconds for a plant with 26,000 in the bank, every builder busy, and went dark halfway. A plant now jumps that queue once the margin is thin, a busy builder is taken off whatever it is doing to put it up, and while the base is dark the computer buys nothing else. Over eight four-player matches the time its bases spent without power fell by a third, from 42 minutes to 28 across all of them.@@ -1012,7 +1013,7 @@ A third pass went through the generals' powers, promotions and unit abilities.- No Superweapons can be picked. The dropdown sits low on the settings page, and its last row hung past the bottom edge of the panel: it drew, but a click on it went to nobody and the list stayed on whatever it said before. Any dropdown row past the edge of its panel takes the click now.- The USA Superweapon General is the exception to both, because the rule that reads as a handicap to everyone else reads as a deletion to him: three superweapons of his own are what he pays for weaker tanks, weaker infantry and a worse air force with. Under Limit he keeps four of each of his. Under No he keeps one. Play him against a lobby that has banned superweapons and you are playing the army you paid for, not an empty base.- No Superweapons leaves China its upgrades. The Nuclear Missile silo is where China buys Uranium Shells, Nuclear Tanks and Neutron Shells, so banning it took them off every China general. Each player may now put up one silo under No, the Superweapon General too, and its missile never launches. Nothing treats it as a superweapon either: no fire button, no shortcut, no countdown on anybody's screen, no announcer telling the enemy one is up, and the doors stay shut. The Particle Cannon and the Scud Storm are still banned. A replay of a No Superweapons match recorded before this, where a silo went up or a missile flew, will not play back the same.-- The computer pays for every superweapon in base defences. Four finished defences let it put up one, eight let it put up two, and with fewer than four it waits. Its Particle Cannons, silos and Scud Storms all count against one allowance, and a superweapon counts from the moment its foundation goes down, so it cannot start two on the same four towers. A defence counts only once it is finished: a scaffold put down and cancelled unlocks nothing. Tunnel Networks and Propaganda Towers count as defences; the tunnel a Sneak Attack drops for free does not. The silo whose missile never launches, under No Superweapons or Pro Rules, is only there for China's upgrades and goes up without a single tower. Lose its towers and the superweapons it has keep standing, but the next one waits. You are not asked for towers at all: for a human player the lobby's limit and Pro Rules are the whole rule, as they were before the towers rule came in, and the buttons never grey out for want of defences. Campaign and Generals Challenge missions are left as they were. A replay from a build that held humans to the towers too plays out differently if a human's superweapon was turned away in it.+- The computer pays for every superweapon in base defences. Twelve finished defences let it put up one, twenty-four let it put up two, and with fewer than twelve it waits. It was four until the computer learned to build its guns as a steady mix. Its Particle Cannons, silos and Scud Storms all count against one allowance, and a superweapon counts from the moment its foundation goes down, so it cannot start two on the same twelve towers. A defence counts only once it is finished: a scaffold put down and cancelled unlocks nothing. Tunnel Networks and Propaganda Towers count as defences; the tunnel a Sneak Attack drops for free does not. The silo whose missile never launches, under No Superweapons or Pro Rules, is only there for China's upgrades and goes up without a single tower. Lose its towers and the superweapons it has keep standing, but the next one waits. You are not asked for towers at all: for a human player the lobby's limit and Pro Rules are the whole rule, as they were before the towers rule came in, and the buttons never grey out for want of defences. Campaign and Generals Challenge missions are left as they were. A replay from a build that held humans to the towers too plays out differently if a human's superweapon was turned away in it.- The tick box was one number for everybody, which is why it could only ever say yes or no. A ticked box in a settings file from an older build reads back as No Superweapons.## Pro Rules, in every network gamediff --git a/GeneralsMD/Code/GameEngine/Include/Common/Player.h b/GeneralsMD/Code/GameEngine/Include/Common/Player.hindex af04093c8..fcfb407b9 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/Player.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/Player.h@@ -154,11 +154,12 @@ Int UnitLimitPerPlayer( Int nonObserverPlayers );Bool UnitCapRefuses( Int unitsTowardCap, Int unitsItAdds, UnsignedInt unitCap );// A computer player's superweapons are paid for in base defences: every DEFENSES_PER_SUPERWEAPON-// finished defences it stands allow it one superweapon, counted across every type. A human is held+// finished defences it stands allow it one superweapon, counted across every type (four until+// the owner raised it to twelve). A human is held// only by the lobby's rule and Pro Rules. Whether one more is refused,// given the finished defences and the superweapons he already has, foundations included. It sits// on top of the lobby's rule and never loosens it.-enum { DEFENSES_PER_SUPERWEAPON = 4 };+enum { DEFENSES_PER_SUPERWEAPON = 12 };Bool SuperweaponDefenseCapRefuses( Int finishedDefenses, Int superweapons );// Only a defence somebody paid for counts. The Sneak Attack tunnel, its Start and every general's// copy are the only FS_BASE_DEFENSE templates with no BuildCost: a power drops them for free.diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.hindex 4831ff385..789b9f3cd 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h@@ -337,6 +337,22 @@ 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 );+/** Base defences a Hard computer may stand: one per AI_ARMY_PER_DEFENSE fighting units, and however+ * small the army, one more every AI_DEFENSE_PACE_SECONDS of the match until two superweapons' worth+ * stand. The army ration alone left a 1v1 base on two or three guns, short of the+ * DEFENSES_PER_SUPERWEAPON a superweapon asks for. */+static const Int AI_ARMY_PER_DEFENSE = 4;+static const Int AI_DEFENSE_PACE_SECONDS = 40;+Int aiDefenseAllowance( Int army, UnsignedInt frame );++/** Which of the base defences a dozer can build goes up next, as an index into the candidates: the+ * one with the fewest standing, so a side's guns come as a mix rather than a row of whatever button+ * sits first. A defence with no gun or spawn of its own (an empty bunker, a speaker tower) counts+ * AI_SUPPORT_DEFENSE_WEIGHT times what it has standing. Ties go to an armed one, then to the first+ * candidate. -1 when there are none. */+static const Int AI_SUPPORT_DEFENSE_WEIGHT = 3;+Int aiPickBaseDefense( const Int *standing, const Bool *armed, Int count );+/** 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.hindex 29318ca34..b6157c6fa 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h@@ -345,6 +345,7 @@ protected:Bool hasEnoughMoneyUnitsFor(TeamPrototype *proto) const; ///< this team is all hackers and there is no room for moreBool 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 fillBool queueExtraFactory(Object *dozer, KindOfType kind, Bool unlimited); ///< one more of this factory, beside a held expansion or around the base+ const ThingTemplate *nextBaseDefense(Object *dozer); ///< the base defence off this dozer's buttons that the base has fewest of, NULL for noneReal knownFirepowerAlongPath(Waypoint *way); ///< what this AI has seen that can shoot, along an approachAsciiString secondApproachLabel(const Coord3D *from, const AsciiString &taken, Int pathSuffix); ///< the quietest other road, or emptyBool loadGunships(void); ///< infantry boards the transports it can shoot out of: the wave's anything at home, a team's its own; TRUE while a firing gunship is still fillingdiff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cppindex e423c719a..274e45c07 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp@@ -1254,6 +1254,33 @@ Bool aiWantsAnotherTechBuilding( Int standing, Int onTheWay )return standing < AI_TECH_BUILDING_COPIES;}+//-------------------------------------------------------------------------------------------------+Int aiDefenseAllowance( Int army, UnsignedInt frame )+{+ const Int byArmy = army / AI_ARMY_PER_DEFENSE;+ Int byClock = (Int)( frame / ( AI_DEFENSE_PACE_SECONDS * LOGICFRAMES_PER_SECOND ) );+ if( byClock > 2 * DEFENSES_PER_SUPERWEAPON )+ byClock = 2 * DEFENSES_PER_SUPERWEAPON;+ return byArmy > byClock ? byArmy : byClock;+}++//-------------------------------------------------------------------------------------------------+Int aiPickBaseDefense( const Int *standing, const Bool *armed, Int count )+{+ Int best = -1;+ Int bestWeight = 0;+ for( Int i = 0; i < count; ++i )+ {+ const Int weight = standing[ i ] * ( armed[ i ] ? 1 : AI_SUPPORT_DEFENSE_WEIGHT );+ if( best < 0 || weight < bestWeight || ( weight == bestWeight && armed[ i ] && !armed[ best ] ) )+ {+ best = i;+ bestWeight = weight;+ }+ }+ return best;+}+//-------------------------------------------------------------------------------------------------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.cppindex 31a2c90ea..8848369fb 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp@@ -4809,10 +4809,6 @@ static const Real POWER_SETBACK = 0.75f;/** And how far in front of it a bought base defense goes, on the same line the other way. */static const Real DEFENSE_STANDOFF = 1.0f;-/** The base grows with the army: one gun allowed per this many fighting units. Superweapons follow- * the guns by the computer players' rule, DEFENSES_PER_SUPERWEAPON in Player.h. */-static const Int ARMY_PER_DEFENSE = 4;-/** Dozers a Hard AI trains on its own when every one it has is on a building. */static const Int MAX_ECONOMY_DOZERS = 4;@@ -4883,6 +4879,53 @@ static const ThingTemplate *buildableOfKind( Object *builder, GUICommandType comreturn NULL;}+static Bool priorityBuildPending( Player *player, const ThingTemplate *tmpl );++//----------------------------------------------------------------------------------------------------------+/** The base defence this player has the fewest of, off the dozer's buttons (aiPickBaseDefense). Asking+ * buildableOfKind for one gave the first button every time: a USA base of nothing but Patriots, a+ * GLA one of Stinger Sites, and China bunkers nobody sat in beside the Gattling Cannons its scripts+ * asked for. One standing, going up or waiting on the build list counts alike. */+//----------------------------------------------------------------------------------------------------------+const ThingTemplate *AIPlayer::nextBaseDefense( Object *dozer )+{+ const CommandSet *commandSet = TheControlBar->findCommandSet( dozer->getCommandSetString() );+ if( commandSet == NULL )+ return NULL;++ const ThingTemplate *candidates[ MAX_COMMANDS_PER_SET ];+ Bool armed[ MAX_COMMANDS_PER_SET ];+ Int count = 0;+ 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 || !tmpl->isKindOf( KINDOF_FS_BASE_DEFENSE ) )+ continue;+ // short of money is still a candidate: the build list waits for it, and a pick made only from+ // what the bank covers today would lean on the cheapest+ const CanMakeType canMake = TheBuildAssistant->canMakeUnit( dozer, tmpl );+ if( canMake != CANMAKE_OK && canMake != CANMAKE_NO_MONEY )+ continue;+ candidates[ count ] = tmpl;+ armed[ count ] = tmpl->canPossiblyHaveAnyWeapon() || tmpl->isKindOf( KINDOF_SPAWNS_ARE_THE_WEAPONS );+ ++count;+ }+ if( count == 0 )+ return NULL;++ Int standing[ MAX_COMMANDS_PER_SET ];+ m_player->countObjectsByThingTemplate( count, candidates, TRUE, standing, FALSE );+ for( Int i = 0; i < count; ++i )+ if( priorityBuildPending( m_player, candidates[ i ] ) )+ ++standing[ i ];++ const Int pick = aiPickBaseDefense( standing, armed, count );+ return pick < 0 ? NULL : candidates[ pick ];+}+/** 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@@ -5237,8 +5280,8 @@ void AIPlayer::doPower( void )* 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.+ * can build, on every pass. Hackers from every factory that trains them. And base defenses, a+ * mix of the side's types, as many as aiDefenseAllowance gives the army and the clock.*///----------------------------------------------------------------------------------------------------------void AIPlayer::doEconomy( void )@@ -5322,31 +5365,49 @@ void AIPlayer::doEconomy( void )homeFull = TRUE;}- // ... and what does not pay back for twice it, so the opening build order is not what pays for it- if( m_player->getMoney()->countMoney() <= 2 * profile->m_cashHoardThreshold )- return;-- /* A gun on the side the trouble comes from, before anything else the pass buys. Production and+ /* A gun on the side the trouble comes from, before the factories the pass buys. Production andincome go round the base center, the power goes behind it, and this goes out in front, so thedefenses stand in front of everything else. It used to be one every minute and a half and nevermore than ten. Bought whenever the bank allowed, they took the money from the army: over twelvefour-player matches 23 guns became 241, and attack waves fell from 223 to 187. So the guns grow- with the army, one per ARMY_PER_DEFENSE fighting units, and a base whose army is gone rebuilds- its army before its walls. */+ with the army, and with the clock up to two superweapons' worth (aiDefenseAllowance), one at a+ time, each the type the base has fewest of. They go round the front: straight at the enemy,+ then 45 degrees either side, then 90, so the flanks are covered too, and a pass that finds one+ arc full tries the next arc on the next pass. They wait only for the hoard, like the income+ buildings: behind twice the hoard, a 1v1 Hard side built two to five a match. */const BaseTally tally = tallyBase( m_player );- const ThingTemplate *defense = buildableOfKind( dozer, GUI_COMMAND_DOZER_CONSTRUCT, KINDOF_FS_BASE_DEFENSE );- if( defense && tally.defenses < tally.army / ARMY_PER_DEFENSE && !priorityBuildPending( m_player, defense ) )- {+ const ThingTemplate *defense = nextBaseDefense( dozer );+ if( defense && tally.defenses < aiDefenseAllowance( tally.army, TheGameLogic->getFrame() )+ && !priorityBuildPending( m_player, defense ) )+ {+ // cos and sin of 0, +45, -45, +90 and -90 degrees, written out so no trig runs in logic+ const Int ARCS = 5;+ static const Real ARC_COS[ ARCS ] = { 1.0f, 0.70710678f, 0.70710678f, 0.0f, 0.0f };+ static const Real ARC_SIN[ ARCS ] = { 0.0f, 0.70710678f, -0.70710678f, 1.0f, -1.0f };Coord3D spot = m_baseCenter;Coord3D dir;if( enemyDirection( &dir ) ){- spot.x += dir.x * m_baseRadius * DEFENSE_STANDOFF;- spot.y += dir.y * m_baseRadius * DEFENSE_STANDOFF;+ const Int arc = (Int)( ( (UnsignedInt)tally.defenses + TheGameLogic->getFrame() / ECONOMY_CHECK_RATE ) % ARCS );+ const Real x = dir.x * ARC_COS[ arc ] - dir.y * ARC_SIN[ arc ];+ const Real y = dir.y * ARC_COS[ arc ] + dir.x * ARC_SIN[ arc ];+ spot.x += x * m_baseRadius * DEFENSE_STANDOFF;+ spot.y += y * m_baseRadius * DEFENSE_STANDOFF;}- placeNear( defense, &spot, 0.0f, FALSE );+ // a base on a small map has its arc off the edge or on a cliff: 680 of 790 tries missed over+ // 72 1v1 matches, so a miss goes round the middle of the base instead+ Bool placed = placeNear( defense, &spot, 0.0f, FALSE );+ if( !placed )+ placed = placeNear( defense, &m_baseCenter, 0.5f * m_baseRadius, FALSE );+ DEBUG_LOG(("AI ECONOMY frame %d player %d %s defense '%s', %d of %d standing, army %d\n", TheGameLogic->getFrame(),+ m_player->getPlayerIndex(), placed ? "puts up" : "has no room for", defense->getName().str(),+ tally.defenses, aiDefenseAllowance( tally.army, TheGameLogic->getFrame() ), tally.army));}+ // ... and what does not pay back for twice it, so the opening build order is not what pays for it+ 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 theairfield would wait forever. Even economy ticks ask for the airfield first, odd ticks ask forthe queues first, and a kind that wants nothing simply yields the pass. */@@ -6313,7 +6374,7 @@ void AIPlayer::defendHome( void )m_player->iterateObjects( findAnyDozer, &dozer );if( dozer && m_player->getCanBuildBase() ){- const ThingTemplate *defense = buildableOfKind( dozer, GUI_COMMAND_DOZER_CONSTRUCT, KINDOF_FS_BASE_DEFENSE );+ const ThingTemplate *defense = nextBaseDefense( dozer );Bool going = FALSE;m_player->iterateObjects( findDefenseUnderConstruction, &going );if( defense && !going && !priorityBuildPending( m_player, defense ) )diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cppindex 8e908f2a8..cb5d90b08 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp@@ -676,7 +676,13 @@ void AISkirmishPlayer::buildAIBaseDefense(Bool flank)}resInfo = resInfo->m_next;}- if (resInfo) {+ // the data names one defence per side, so a script asking for ten got ten of it: the side's mix+ // comes off a dozer's buttons instead, and the data's choice stands in when no dozer is left+ Object *dozer = findNearestDozer(&m_baseCenter);+ const ThingTemplate *mix = dozer ? nextBaseDefense(dozer) : NULL;+ if (mix) {+ buildAIBaseDefenseStructure(mix->getName(), flank);+ } else if (resInfo) {buildAIBaseDefenseStructure(resInfo->m_baseDefenseStructure1, flank);}}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 7ab20f8b3..07af47e2b 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -11139,6 +11139,49 @@ TEST(massing_waits_for_a_force_but_never_waits_for_ever)}+/** A computer's base defences come as a mix: the type it has fewest of goes up next, and a bunker or+ a speaker tower, which shoots at nothing on its own, counts three times what it has standing. */+TEST(ai_base_defenses_rotate_through_the_sides_types)+{+ CHECK_EQ( aiPickBaseDefense( NULL, NULL, 0 ), -1 );++ // USA: Patriot first on the bar, then Fire Base. Twelve picks go six and six, never two alike running+ {+ Int standing[ 2 ] = { 0, 0 };+ const Bool armed[ 2 ] = { TRUE, TRUE };+ Int last = -1;+ for( Int n = 0; n < 12; ++n )+ {+ const Int pick = aiPickBaseDefense( standing, armed, 2 );+ CHECK( pick != last );+ last = pick;+ ++standing[ pick ];+ }+ CHECK_EQ( standing[ 0 ], 6 );+ CHECK_EQ( standing[ 1 ], 6 );+ }++ // China: Bunker, Speaker Tower and Gattling Cannon on the bar, in that order. The cannon goes up+ // first although the bunker's button comes first, and twelve end seven guns, three bunkers, two towers+ {+ Int standing[ 3 ] = { 0, 0, 0 };+ const Bool armed[ 3 ] = { FALSE, FALSE, TRUE };+ CHECK_EQ( aiPickBaseDefense( standing, armed, 3 ), 2 );+ for( Int n = 0; n < 12; ++n )+ ++standing[ aiPickBaseDefense( standing, armed, 3 ) ];+ CHECK_EQ( standing[ 2 ], 7 );+ CHECK_EQ( standing[ 0 ], 3 );+ CHECK_EQ( standing[ 1 ], 2 );+ }++ // one type lost in a raid is the one put back+ {+ const Int standing[ 2 ] = { 5, 2 };+ const Bool armed[ 2 ] = { TRUE, TRUE };+ CHECK_EQ( aiPickBaseDefense( standing, armed, 2 ), 1 );+ }+}+/** 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)@@ -11169,6 +11212,16 @@ TEST(extra_factory_follows_the_queue_and_tech_stops_at_three)CHECK( !aiWantsAnotherTechBuilding( 2, 1 ) );CHECK( !aiWantsAnotherTechBuilding( AI_TECH_BUILDING_COPIES, 0 ) );+ // defences: a big army earns them, and the clock earns one per pace up to two superweapons' worth+ const UnsignedInt pace = AI_DEFENSE_PACE_SECONDS * LOGICFRAMES_PER_SECOND;+ CHECK_EQ( aiDefenseAllowance( 0, 0 ), 0 );+ CHECK_EQ( aiDefenseAllowance( 0, pace - 1 ), 0 );+ CHECK_EQ( aiDefenseAllowance( 0, pace ), 1 );+ CHECK_EQ( aiDefenseAllowance( 0, DEFENSES_PER_SUPERWEAPON * pace ), (Int)DEFENSES_PER_SUPERWEAPON );+ CHECK_EQ( aiDefenseAllowance( 0, 1000 * pace ), 2 * (Int)DEFENSES_PER_SUPERWEAPON );+ CHECK_EQ( aiDefenseAllowance( 40, pace ), 40 / AI_ARMY_PER_DEFENSE );+ CHECK_EQ( aiDefenseAllowance( 200, 1000 * pace ), 200 / AI_ARMY_PER_DEFENSE );+TAiData ladder;CHECK( !ladder.m_skill[ AISKILL_EASY ].m_economyBuildings );CHECK( !ladder.m_skill[ AISKILL_MEDIUM ].m_economyBuildings );@@ -15068,26 +15121,29 @@ TEST(an_open_dropdown_owns_the_rows_that_hang_past_its_panel)CHECK( !OpenWindowOwnsPoint( TRUE, boxX, boxY, boxWidth, boxHeightOpen, 900, 660 ) );}-/* Four finished base defences buy one superweapon, eight buy two, whatever mix of silo, uplink and- storm they go to. Player::canBuildMoreOfType counts and asks this; the lobby's cap is checked- beside it and still refuses on its own. */-TEST(four_finished_defenses_pay_for_each_superweapon)+/* Twelve finished base defences buy one superweapon, twenty-four buy two, whatever mix of silo,+ uplink and storm they go to. Player::canBuildMoreOfType counts and asks this; the lobby's cap is+ checked beside it and still refuses on its own. */+TEST(twelve_finished_defenses_pay_for_each_superweapon){- // fewer than four buys nothing+ CHECK_EQ( (Int)DEFENSES_PER_SUPERWEAPON, 12 );++ // fewer than twelve buys nothing, four included, which bought one beforeCHECK( SuperweaponDefenseCapRefuses( 0, 0 ) );- CHECK( SuperweaponDefenseCapRefuses( 3, 0 ) );+ CHECK( SuperweaponDefenseCapRefuses( 4, 0 ) );+ CHECK( SuperweaponDefenseCapRefuses( 11, 0 ) );- // four buy the first, and a foundation already down spends it- CHECK( !SuperweaponDefenseCapRefuses( 4, 0 ) );- CHECK( SuperweaponDefenseCapRefuses( 4, 1 ) );- CHECK( SuperweaponDefenseCapRefuses( 7, 1 ) );+ // twelve buy the first, and a foundation already down spends it+ CHECK( !SuperweaponDefenseCapRefuses( 12, 0 ) );+ CHECK( SuperweaponDefenseCapRefuses( 12, 1 ) );+ CHECK( SuperweaponDefenseCapRefuses( 23, 1 ) );- // eight the second- CHECK( !SuperweaponDefenseCapRefuses( 8, 1 ) );- CHECK( SuperweaponDefenseCapRefuses( 8, 2 ) );+ // twenty-four the second+ CHECK( !SuperweaponDefenseCapRefuses( 24, 1 ) );+ CHECK( SuperweaponDefenseCapRefuses( 24, 2 ) );// losing defences never pulls a standing superweapon down, it only stops the next one- CHECK( SuperweaponDefenseCapRefuses( 4, 2 ) );+ CHECK( SuperweaponDefenseCapRefuses( 12, 2 ) );// a Sneak Attack tunnel costs nothing and buys nothing; a Tunnel Network at 800 doesCHECK( !DefenseCountsForSuperweapons( 0 ) );@@ -15124,9 +15180,9 @@ TEST(superweapon_defense_allowance_binds_computer_players_only)CHECK( SuperweaponNeedsDefenses( AsciiString( "GLAScudStorm" ), FALSE, SUPERWEAPONS_ALLOW, TRUE ) );CHECK( SuperweaponNeedsDefenses( AsciiString( "AmericaParticleCannonUplink" ), FALSE, SUPERWEAPONS_LIMIT, TRUE ) );- // and for a computer a storm's waiting hole still spends the four towers it stood on- CHECK( SuperweaponDefenseCapRefuses( 4, RebuildHoleHoldsSuperweapon( TRUE, FALSE ) ? 1 : 0 ) );- CHECK( !SuperweaponDefenseCapRefuses( 4, RebuildHoleHoldsSuperweapon( FALSE, FALSE ) ? 1 : 0 ) );+ // and for a computer a storm's waiting hole still spends the twelve towers it stood on+ CHECK( SuperweaponDefenseCapRefuses( 12, RebuildHoleHoldsSuperweapon( TRUE, FALSE ) ? 1 : 0 ) );+ CHECK( !SuperweaponDefenseCapRefuses( 12, RebuildHoleHoldsSuperweapon( FALSE, FALSE ) ? 1 : 0 ) );}/* The superweapon rule is a mode, and what a mode leaves you depends on who you are playing. Thediff --git a/GeneralsMD/Code/Tests/test_main.cpp b/GeneralsMD/Code/Tests/test_main.cppindex 2c03d3819..17cd79704 100644--- a/GeneralsMD/Code/Tests/test_main.cpp+++ b/GeneralsMD/Code/Tests/test_main.cpp@@ -15,7 +15,7 @@ namespace/* Fixed-capacity registry: static-init order is unspecified, so astd::vector here would risk being constructed after the firstregistrar runs. A POD array is zero-initialised before any of it. */- const int MAX_TESTS = 512;+ const int MAX_TESTS = 1024;struct Entry{