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): ai skips supply piles another side already holds

Olcay Seygane56e6d2351a4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7bf65643d..24493f963 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -161,6 +161,7 @@ found and fixed — EA's own, not port damage.**
- On Hard the expansion comes with a defence structure facing the enemy, placed in the same job. An undefended expansion is a gift.
- Measured: a third more army in the field and a quarter more money spent over the match.
- With Share Income set to All Income it leaves its allies' piles alone. Every payment is already split across the team, so a computer that put a supply centre beside its ally's pile added nothing and emptied that pile twice as fast. It now expands only to piles nobody on its side is working. Over eight 2v2 matches on Twilight Flame, loads taken from an ally's pile went from 396 to 54 and the team's income stayed where it was. Replays recorded before this change play out differently.
+- It leaves a pile alone when someone else is already working it. The computer only checked for its own supply centre beside a pile, so a pile inside an ally's base, or a third player's, looked free, and it walked a supply centre in next to theirs with a Patriot or a Gatling cannon after it. It now skips any pile with another side's building beside it, friend or foe. Over eight 2v2 matches on Twilight Flame, buildings it put down at a pile someone else held went from 116 to 2, and over eight four-way matches from 8 to 4, with spending where it was. Replays recorded before this change play out differently.
- It keeps its money-makers and its superweapons at home. A Hard computer with a full base looked for room further and further out until the map ended, and a black market or a Scud Storm could go up alone on ground nobody held. Those buildings, and the tech building a superweapon waits on, now go inside the base or at its edge, and when there is no room they are not built: the money goes into another factory. Expansions are held to the same standard: a supply centre only goes to a pile on its own side of the map and within reach of home, never to one with an enemy standing at it, and the next expansion waits until the last one has a gun or a guard beside it. Replays recorded before this change play out differently.
## The computer spends its money
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 804f57dbe..d89b4bcba 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -3034,6 +3034,20 @@ static Int countOwnedNear( Player *player, const ThingTemplate *tmpl, const Coor
return search.count;
}
+/** The nearest standing structure of another side, ally or enemy, beside a dock: whoever built it
+ * has the pile already, or sits on it. Civilian buildings are neutral and do not count. */
+static Object *otherSideStructureNear( Player *player, const Object *dock )
+{
+ PartitionFilterAcceptByKindOf fStructure(MAKE_KINDOF_MASK(KINDOF_STRUCTURE), KINDOFMASK_NONE);
+ PartitionFilterPlayer fNotMine(player, false);
+ PartitionFilterPlayerAffiliation fSide(player, ALLOW_ALLIES | ALLOW_ENEMIES, true);
+ PartitionFilterAlive fAlive;
+ PartitionFilterOnMap fOnMap;
+ PartitionFilter *filters[] = { &fStructure, &fNotMine, &fSide, &fAlive, &fOnMap, 0 };
+ return ThePartitionManager->getClosestObject( dock->getPosition(),
+ SUPPLY_CENTER_CLOSE_DIST + dock->getGeometryInfo().getBoundingCircleRadius(), FROM_BOUNDINGSPHERE_2D, filters );
+}
+
// ------------------------------------------------------------------------------------------------
/** Build a supply center near a supply source with minimumCash or more resources. */
// ------------------------------------------------------------------------------------------------
@@ -3171,10 +3185,11 @@ void AIPlayer::buildBySupplies(Int minimumCash, const AsciiString& thingName, Bo
return;
}
location.z = 0; // All build list locations are ground relative.
- DEBUG_LOG(("AI EXPAND frame %d player %d builds '%s' at (%.0f,%.0f) by dock %d, %d in the bank, %.0f from a base of radius %.0f%s\n",
+ const Object *claimant = otherSideStructureNear( m_player, bestSupplyWarehouse );
+ DEBUG_LOG(("AI EXPAND frame %d player %d builds '%s' at (%.0f,%.0f) by dock %d, %d in the bank, %.0f from a base of radius %.0f%s, held by player %d\n",
TheGameLogic->getFrame(), m_player->getPlayerIndex(), thingName.str(), location.x, location.y, bestSupplyWarehouse->getID(),
m_player->getMoney()->countMoney(), sqrt( sqr( location.x - m_baseCenter.x ) + sqr( location.y - m_baseCenter.y ) ), m_baseRadius,
- holdableOnly ? ", its own choice" : ""));
+ holdableOnly ? ", its own choice" : "", claimant ? claimant->getControllingPlayer()->getPlayerIndex() : -1));
m_player->addToPriorityBuildList(thingName, &location, angle);
m_curWarehouseID = bestSupplyWarehouse->getID();
}
@@ -3426,24 +3441,22 @@ Object *AIPlayer::findSupplyCenter(Int minimumCash, Bool holdableOnly)
Real radius = SUPPLY_CENTER_CLOSE_DIST + obj->getGeometryInfo().getBoundingCircleRadius();
PartitionFilterAcceptByKindOf f1(MAKE_KINDOF_MASK(KINDOF_CASH_GENERATOR), KINDOFMASK_NONE);
- //
- // "Do I already have a centre here" - and, for a supportive AI, "does my ally". Two
- // allied AIs racing each other to the same warehouse is one of the most visibly
- // stupid things AI teammates do, and this filter is the whole of the fix.
- //
PartitionFilterPlayer f2(m_player, true); // Only find your own units.
- PartitionFilterPlayerAffiliation f2Ally(m_player, ALLOW_SAME_PLAYER | ALLOW_ALLIES, true);
PartitionFilterOnMap filterMapStatus;
-
- PartitionFilter *mine[] = { &f1, &f2, &filterMapStatus, 0 };
- PartitionFilter *ours[] = { &f1, &f2Ally, &filterMapStatus, 0 };
- PartitionFilter **filters = (m_role == AIROLE_SUPPORTIVE || TheGameLogic->getIncomeSharing() == INCOME_SHARING_ALL) ? ours : mine;
+ PartitionFilter *filters[] = { &f1, &f2, &filterMapStatus, 0 };
Object *supplyCenter = ThePartitionManager->getClosestObject(&center, radius, FROM_BOUNDINGSPHERE_2D, filters);
if (supplyCenter) {
// We already have a supply center.
continue;
}
+ // Another side's pile: its supply center works it, or its base stands round it. Only
+ // our own center was looked for (an ally's too, for a supportive AI), so a dock inside
+ // an ally's base or a second enemy's read as free and the AI put a center, then guns
+ // and factories, against theirs.
+ if (otherSideStructureNear(m_player, obj)) {
+ continue;
+ }
Real dx, dy;
dx = obj->getPosition()->x - m_baseCenter.x;