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): allow one superweapon per four finished base defenses

Olcay Seygana57be94d752d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f3afae2d9..01515d668 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -127,7 +127,7 @@ found and fixed — EA's own, not port damage.**
- 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 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 the moment the money is in the bank; on Twilight Flame the first particle cannon was up at 4.3 minutes and the first nuke at 4.9. More follow as the base grows, one for every four guns defending it, so a computer holding twenty guns can stand six superweapons; over twelve four-player matches that came to about three each. The lobby's superweapon limit and Pro Rules still apply to the computer exactly as they do to you.
+- 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, the rule every player keeps (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, without a gun to its name; how much later it comes now has not been measured. 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.
@@ -933,6 +933,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.
+- Every superweapon is paid for in base defences now. Four finished defences let you put up one, eight let you put up two, and with fewer than four the buttons stay grey. Your Particle Cannons, silos and Scud Storms all count against one allowance, and a superweapon counts from the moment its foundation goes down, so you 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 one exception is the silo whose missile never launches, under No Superweapons or Pro Rules: it is only there for China's upgrades, so it goes up without a single tower. Lose your towers and the superweapons you have keep standing, but the next one waits. The lobby's limit still holds on top of this, and the computer plays by the same rule. Campaign and Generals Challenge missions are left as they were. Replays recorded before this change play out differently.
- 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 game
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/Player.h b/GeneralsMD/Code/GameEngine/Include/Common/Player.h
index 5cd82138c..d0ca4d611 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/Player.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/Player.h
@@ -153,6 +153,16 @@ Int UnitLimitPerPlayer( Int nonObserverPlayers );
// Whether a build that adds unitsItAdds (a transport and its payload) goes past the share. 0 is no cap.
Bool UnitCapRefuses( Int unitsTowardCap, Int unitsItAdds, UnsignedInt unitCap );
+// Superweapons are paid for in base defences: every DEFENSES_PER_SUPERWEAPON finished defences a
+// player stands allow him one superweapon, counted across every type. 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 };
+Bool SuperweaponDefenseCapRefuses( Int finishedDefenses, Int superweapons );
+// The silo whose missile No Superweapons or Pro Rules silences is a shop for China's upgrades, not a
+// weapon, and is not asked for defences.
+Bool SuperweaponNeedsDefenses( const AsciiString &buildingName, Bool proRules, Int superweaponRestriction );
+
// The lobby's income sharing, an IncomeSharing from GameInfo.h: whether a payment is split in this
// match, and each ally's cut when it is split evenly between sharers players. The earner keeps what
// the cuts leave, so rounding never loses a dollar.
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
index 5c09ae7d1..64d6f32f9 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
@@ -3285,6 +3285,38 @@ Bool UnitCapRefuses( Int unitsTowardCap, Int unitsItAdds, UnsignedInt unitCap )
return unitCap > 0 && (UnsignedInt)( unitsTowardCap + unitsItAdds ) > unitCap;
}
+//=============================================================================
+Bool SuperweaponDefenseCapRefuses( Int finishedDefenses, Int superweapons )
+{
+ return superweapons >= finishedDefenses / DEFENSES_PER_SUPERWEAPON;
+}
+
+Bool SuperweaponNeedsDefenses( const AsciiString &buildingName, Bool proRules, Int superweaponRestriction )
+{
+ return !( ProRulesExemptSuperweapon( buildingName )
+ && SuperweaponMissileSilenced( SPECIAL_NEUTRON_MISSILE, proRules, superweaponRestriction ) );
+}
+
+/* A defence counts once it stands finished, so a foundation put down and cancelled never unlocks
+ anything. A superweapon counts from its foundation, so two cannot be placed on one allowance. */
+struct SuperweaponDefenseCount
+{
+ Int defenses;
+ Int superweapons;
+};
+
+static void countSuperweaponDefenses( Object *obj, void *userData )
+{
+ if ( obj->isEffectivelyDead() )
+ return;
+
+ SuperweaponDefenseCount *count = (SuperweaponDefenseCount *)userData;
+ if ( obj->isKindOf( KINDOF_FS_SUPERWEAPON ) )
+ count->superweapons++;
+ else if ( obj->isKindOf( KINDOF_FS_BASE_DEFENSE ) && !obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION ) )
+ count->defenses++;
+}
+
//=============================================================================
Bool IncomeSharingSplits( Int incomeSharing, Bool fromTechBuilding )
{
@@ -3391,6 +3423,17 @@ Bool Player::canBuildMoreOfType( const ThingTemplate *whatToBuild, Int unitsPerO
maxSimultaneousOfType = (UnsignedInt)cap;
}
+ // the defence allowance holds in skirmish and network matches and their replays; a campaign or
+ // Generals Challenge mission was laid out without it and keeps EA's rules
+ if ( whatToBuild->isKindOf( KINDOF_FS_SUPERWEAPON ) && TheGameLogic && !TheGameLogic->isInSinglePlayerGame()
+ && SuperweaponNeedsDefenses( whatToBuild->getName(), proRules, (Int)TheGameLogic->getSuperweaponRestriction() ) )
+ {
+ SuperweaponDefenseCount count = { 0, 0 };
+ iterateObjects( countSuperweaponDefenses, &count );
+ if ( SuperweaponDefenseCapRefuses( count.defenses, count.superweapons ) )
+ return false;
+ }
+
if (maxSimultaneousOfType != 0)
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 940a8f584..f6b4adee2 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -4716,10 +4716,9 @@ 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, and one more
- * superweapon past the first per this many guns. */
+/** The base grows with the army: one gun allowed per this many fighting units. Superweapons follow
+ * the guns by the rule every player keeps, DEFENSES_PER_SUPERWEAPON in Player.h. */
static const Int ARMY_PER_DEFENSE = 4;
-static const Int DEFENSES_PER_SUPERWEAPON = 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;
@@ -5549,10 +5548,11 @@ void AIPlayer::buildAsap( const ThingTemplate *tmpl )
/** The skirmish scripts hold a Hard AI's superweapon back until an escalation counter, one point every
* ten seconds, reaches 75: twelve and a half minutes, or seven and a half once the enemy owns a tech
* building, and then one copy. A player who went seven Hard AIs against one saw a single nuke in the
- * whole match. The owner's call: no clock. As soon as a dozer can build a superweapon and the money
- * 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.
+ * whole match. The owner's call: no clock. As soon as a dozer can build a superweapon, the money
+ * is in the bank and DEFENSES_PER_SUPERWEAPON guns stand, the first one goes up. More follow as the
+ * base grows, one per that many guns, as the guns grow with the army; unrationed, they took the money
+ * the army needed. That ration is every player's rule now (Player::canBuildMoreOfType), and the
+ * lobby's superweapon setting and Pro Rules still bind beside it; canMakeUnit asks all three.
* 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. */
@@ -5596,12 +5596,9 @@ void AIPlayer::doSuperweapons( void )
return;
}
- if( superweapon )
- {
- const Bool baseHoldsAnother = tally.superweapons < 1 + tally.defenses / DEFENSES_PER_SUPERWEAPON;
- if( baseHoldsAnother && !priorityBuildPending( m_player, superweapon ) )
- buildAsap( superweapon );
- }
+ // buildableOfKind already asked canMakeUnit, which holds the defence ration
+ if( superweapon && !priorityBuildPending( m_player, superweapon ) )
+ buildAsap( superweapon );
}
//----------------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 4b8ab5a3c..648457c47 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -14449,6 +14449,35 @@ 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)
+{
+ // fewer than four buys nothing
+ CHECK( SuperweaponDefenseCapRefuses( 0, 0 ) );
+ CHECK( SuperweaponDefenseCapRefuses( 3, 0 ) );
+
+ // four buy the first, and a foundation already down spends it
+ CHECK( !SuperweaponDefenseCapRefuses( 4, 0 ) );
+ CHECK( SuperweaponDefenseCapRefuses( 4, 1 ) );
+ CHECK( SuperweaponDefenseCapRefuses( 7, 1 ) );
+
+ // eight the second
+ CHECK( !SuperweaponDefenseCapRefuses( 8, 1 ) );
+ CHECK( SuperweaponDefenseCapRefuses( 8, 2 ) );
+
+ // losing defences never pulls a standing superweapon down, it only stops the next one
+ CHECK( SuperweaponDefenseCapRefuses( 4, 2 ) );
+
+ // a silo whose missile is silenced sells China's upgrades and asks for no defences
+ CHECK( !SuperweaponNeedsDefenses( AsciiString( "ChinaNuclearMissileLauncher" ), FALSE, SUPERWEAPONS_NONE ) );
+ CHECK( !SuperweaponNeedsDefenses( AsciiString( "Tank_ChinaNuclearMissileLauncher" ), TRUE, SUPERWEAPONS_ALLOW ) );
+ CHECK( SuperweaponNeedsDefenses( AsciiString( "ChinaNuclearMissileLauncher" ), FALSE, SUPERWEAPONS_LIMIT ) );
+ CHECK( SuperweaponNeedsDefenses( AsciiString( "SupW_AmericaParticleCannonUplink" ), FALSE, SUPERWEAPONS_NONE ) );
+ CHECK( SuperweaponNeedsDefenses( AsciiString( "GLAScudStorm" ), TRUE, SUPERWEAPONS_ALLOW ) );
+}
+
/* The superweapon rule is a mode, and what a mode leaves you depends on who you are playing. The
USA Superweapon General fields three superweapons and pays for them in everything else, so Limit
leaves him four of each where it leaves everybody one, and No leaves him one where it bars