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 base builder no longer stalls on an unplaceable build

Olcay Seygan54bb552e60ea
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 45100a0d0..29ca209c5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -37,6 +37,10 @@ found and fixed — EA's own, not port damage.**
- Difficulty and AI money change build pace again; every order was pinned at three seconds.
- Computer bases no longer grow buildings inside buildings. When a spot in its plan was taken and it could find no free ground nearby, the computer put the building down on the taken spot anyway, straight through its own. It now looks outwards from the planned spot over ground its trucks could drive, bridges included, so a new building lands next to the base and not across a cliff or a river from it, and when there is truly no room it waits. It still stacked them on sloping ground, which a player caught. The check for what already stands on a spot is taken at a height, and the computer's search took it at the height of the spot it started from, or at zero, so wherever the ground fell away a building standing there was below the check and did not count. A Hard USA put three pairs of supply drop zones exactly on top of each other in one match. Every building is checked at the ground it will stand on now: over the same four four-player matches, the pairs stacked or half inside each other went from five to none.
- An urgent building it could not put up yet no longer stops the whole base. The computer always builds its most urgent request first, and when that request needed something it did not have, it built nothing else either: a Hard China that asked for a bunker before its barracks stood sat on 35,000 until well into the match. It now skips to the first urgent request it can actually build.
+- A computer player no longer stands still with money in the bank. When its most urgent building had no room where it wanted it, it looked for room, found none, and looked again, and nothing else in its base went up in the meantime: one Hard GLA sat on 40,000 for most of a match because its Scud Storm did not fit. A request with no room is now dropped and asked for again somewhere else, and the rest of the base carries on. The check for room also no longer passes a spot on a hillside that the builder then refuses. Over eight Hard matches on Winter Wolf, the longest a base waited went from 18,000 frames to 5,400, and the time spent searching fell by six sevenths.
+- Its supply trucks go back to work. When a pile ran dry, the trucks parked next to it for the rest of the match and the computer bought new ones instead: a Hard China kept nine trucks, eight of them idle. Idle trucks are now sent to a supply centre with boxes beside it, and a centre whose pile is gone gets no new trucks.
+- Its base defences spread out. Every defence the computer asked for at a supply pile went up against the last one, and a Hard China stood seven or eight Gatling cannons shoulder to shoulder at one pile. It now puts at most two beside each pile.
+- Replays recorded before this change play back differently.
- Hard USA opens the way a player does. It used to start on a barracks and hold its first power plant until a second dozer existed, so its first harvest came in at the 74 second mark. It now puts down the power plant and the supply centre straight away and the barracks after them. Over 20 Hard USA against Hard China matches on Tournament Desert, seats swapped both ways, USA won 15 where it won 10. The same opening was tried on China and lost it 17 matches instead of 10, and on GLA it decided too few matches to say anything, so both keep the opening they shipped with.
- The computer no longer shoots at what it cannot see. Its units, and its base defences, used to auto-target through the fog of war - a hard exemption written into the code for computer players only. Stealth works against it now, and so does anything it has not scouted.
- It scouts. There was no such thing as scouting in the computer opponent - it never needed to look, because it could already see everything. One cheap unit now tours the enemy start positions and keeps going round for the rest of the match, replaced out of spare change when it dies. Every difficulty scouts; an opponent that never looks reads as broken, not as easy. And it tours on purpose rather than in a circle: it goes to whichever base it has gone longest without seeing, nearest first, and it does not walk across the map to look again at something it looked at a minute ago - it stays and watches instead. Two scouts never trail each other round the same lap.
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
index 727016d01..2a74d45fb 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
@@ -686,7 +686,10 @@ LegalBuildCode BuildAssistant::isLocationClearOfObjects( const Coord3D *worldPos
at the ground under worldPos whatever height the caller passed. The computer's placement searches
kept the height of the spot they started from, or passed 0, and a probe that floated above a
building's roof found it clear: a Hard USA put three pairs of supply drop zones exactly on top of
- each other in one match. */
+ each other in one match. The clearance pass round other buildings further down is asked there
+ too: it kept the caller's height, so the computer's searches at height 0 passed a spot on high
+ ground that its dozer then refused, and a Hard GLA asked for the same Scud Storm spot for
+ 17,000 frames. */
Coord3D groundPos = *worldPos;
groundPos.z = TheTerrainLogic->getGroundHeight( groundPos.x, groundPos.y );
ObjectIterator *iter =
@@ -848,7 +851,7 @@ LegalBuildCode BuildAssistant::isLocationClearOfObjects( const Coord3D *worldPos
}
myGeom.setMajorRadius(myFactoryExitWidth/2.0f);
if (myFactoryExitWidth>0) {
- myExitPos = *worldPos;
+ myExitPos = groundPos;
checkMyExit = true;
Real c = (Real)Cos(angle);
Real s = (Real)Sin(angle);
@@ -904,8 +907,8 @@ LegalBuildCode BuildAssistant::isLocationClearOfObjects( const Coord3D *worldPos
hisExitPos.x += c*offset;
hisExitPos.y += s*offset;
}
- if (ThePartitionManager->geomCollidesWithGeom(them->getPosition(), hisBounds, them->getOrientation(),
- worldPos, myBounds, angle)) {
+ if (ThePartitionManager->geomCollidesWithGeom(them->getPosition(), hisBounds, them->getOrientation(),
+ &groundPos, myBounds, angle)) {
TheTerrainVisual->addFactionBib(them, true);
return LBC_OBJECTS_IN_THE_WAY;
}
@@ -923,8 +926,8 @@ LegalBuildCode BuildAssistant::isLocationClearOfObjects( const Coord3D *worldPos
return LBC_OBJECTS_IN_THE_WAY;
}
// Check for overlap of his exit rectangle with my geom info
- if (checkHisExit && ThePartitionManager->geomCollidesWithGeom(&hisExitPos, hisGeom, them->getOrientation(),
- worldPos, myBounds, angle)) {
+ if (checkHisExit && ThePartitionManager->geomCollidesWithGeom(&hisExitPos, hisGeom, them->getOrientation(),
+ &groundPos, myBounds, angle)) {
TheTerrainVisual->addFactionBib(them, true);
return LBC_OBJECTS_IN_THE_WAY;
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index d1ba6bc9a..058517447 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -547,11 +547,72 @@ void AIPlayer::checkForSupplyCenter( BuildListInfo *info, Object *bldg )
}
}
+/** A warehouse with boxes left, not an enemy's, within reach of this supply center. */
+static Bool hasSuppliesNear( Player *player, const Object *supplyCenter )
+{
+ Coord3D center = *supplyCenter->getPosition();
+ Real radius = SUPPLY_CENTER_CLOSE_DIST + supplyCenter->getGeometryInfo().getBoundingCircleRadius();
+
+ PartitionFilterAcceptByKindOf f1(MAKE_KINDOF_MASK(KINDOF_SUPPLY_SOURCE), KINDOFMASK_NONE);
+ PartitionFilterPlayer f2(player, false); // Only find other.
+ PartitionFilterOnMap filterMapStatus;
+
+ PartitionFilter *filters[] = { &f1, &f2, &filterMapStatus, 0 };
+
+ Object *supplySource = ThePartitionManager->getClosestObject(&center, radius, FROM_BOUNDINGSPHERE_2D, filters);
+ if (!supplySource) {
+ return FALSE;
+ }
+ static const NameKeyType key_warehouseUpdate = NAMEKEY("SupplyWarehouseDockUpdate");
+ SupplyWarehouseDockUpdate *warehouseModule = (SupplyWarehouseDockUpdate*)supplySource->findUpdateModule( key_warehouseUpdate );
+ if( warehouseModule ) {
+ if (warehouseModule->getBoxesStored()*TheGlobalData->m_baseValuePerSupplyBox <= 0) return FALSE;
+ if( player->getRelationship(supplySource->getTeam()) == ENEMIES ) return FALSE;
+ }
+ return TRUE;
+}
+
+struct IdleTruckSearch
+{
+ Player *player;
+ const Object *center;
+ Object *truck;
+};
+
+static void considerIdleTruck( Object *obj, void *userData )
+{
+ IdleTruckSearch *search = (IdleTruckSearch *)userData;
+ if( search->truck || !obj->isKindOf( KINDOF_HARVESTER ) || obj->isEffectivelyDead() || obj->isContained() || obj->getAI() == NULL )
+ return;
+ AIUpdateInterface *ai = obj->getAI();
+ SupplyTruckAIInterface *truckAI = ai->getSupplyTruckAIInterface();
+ if( truckAI == NULL || !ai->isIdle() )
+ return;
+ DozerAIInterface *dozerAI = ai->getDozerAIInterface();
+ if( dozerAI && dozerAI->isAnyTaskPending() )
+ return; // a GLA worker between jobs
+ const Object *dock = TheGameLogic->findObjectByID( truckAI->getPreferredDockID() );
+ if( dock && dock != search->center && hasSuppliesNear( search->player, dock ) )
+ return; // its own center still has boxes to fetch
+ search->truck = obj;
+}
+
+/** A truck of this player's standing about with nothing to fetch. */
+static Object *findIdleTruck( Player *player, const Object *center )
+{
+ IdleTruckSearch search;
+ search.player = player;
+ search.center = center;
+ search.truck = NULL;
+ player->iterateObjects( considerIdleTruck, &search );
+ return search.truck;
+}
+
// ------------------------------------------------------------------------------------------------
/** Queue up a supply truck to be built. */
// ------------------------------------------------------------------------------------------------
void AIPlayer::queueSupplyTruck( void )
-{
+{
Bool truckInQueue = false;
for ( DLINK_ITERATOR<TeamInQueue> iter = iterate_TeamBuildQueue(); !iter.done(); iter.advance())
{
@@ -610,29 +671,9 @@ void AIPlayer::queueSupplyTruck( void )
continue; // don't consider rebuild holes.
}
// Make sure we have a supplies near it.
- Coord3D center = *supplyCenter->getPosition();
- Real radius = SUPPLY_CENTER_CLOSE_DIST + supplyCenter->getGeometryInfo().getBoundingCircleRadius();
-
- PartitionFilterAcceptByKindOf f1(MAKE_KINDOF_MASK(KINDOF_SUPPLY_SOURCE), KINDOFMASK_NONE);
- PartitionFilterPlayer f2(m_player, false); // Only find other.
- PartitionFilterOnMap filterMapStatus;
-
- PartitionFilter *filters[] = { &f1, &f2, &filterMapStatus, 0 };
-
- Object *supplySource = ThePartitionManager->getClosestObject(&center, radius, FROM_BOUNDINGSPHERE_2D, filters);
- if (!supplySource) {
- // No supplies.
+ if (!hasSuppliesNear(m_player, supplyCenter)) {
continue;
}
- static const NameKeyType key_warehouseUpdate = NAMEKEY("SupplyWarehouseDockUpdate");
- SupplyWarehouseDockUpdate *warehouseModule = (SupplyWarehouseDockUpdate*)supplySource->findUpdateModule( key_warehouseUpdate );
- if( warehouseModule ) {
- Int availableCash = warehouseModule->getBoxesStored()*TheGlobalData->m_baseValuePerSupplyBox;
- if (availableCash<=0) continue;
- if( m_player->getRelationship(supplySource->getTeam()) == ENEMIES ) {
- continue;
- }
- }
// Ok, it has supplies available near it.
checkForSupplyCenter(info, supplyCenter);
Int curGatherers = 0;
@@ -710,6 +751,23 @@ void AIPlayer::queueSupplyTruck( void )
}
}
}
+ /* A truck whose pile ran dry regroups at the nearest supply center, which is its own dry one,
+ and looks again only as far as its scan reaches, so the trucks of a picked-clean pile stood
+ round it for the rest of the match while this center bought new ones, three times what it
+ wanted: a Hard China kept nine trucks, eight of them idle, from frame 5000 on. An idle one
+ comes here first, and a center with nothing beside it gets no truck at all. */
+ Object *center = TheGameLogic->findObjectByID(info->getObjectID());
+ if (center && !center->isKindOf(KINDOF_REBUILD_HOLE)) {
+ if (!hasSuppliesNear(m_player, center)) {
+ continue;
+ }
+ Object *idleTruck = findIdleTruck(m_player, center);
+ if (idleTruck) {
+ info->setCurrentGatherers(max(info->getCurrentGatherers(), 0) + 1);
+ idleTruck->getAI()->aiDock(center, CMD_FROM_PLAYER); // from the player, so it stays his preferred dock (see above)
+ return;
+ }
+ }
if (totalHarvesters >= desiredGatherers*3) {
continue; // we got lotsa gatherers.
}
@@ -879,6 +937,24 @@ static Bool isBuildSearchWalkable( Int cellX, Int cellY, const Coord3D *worldPos
return FALSE;
}
+// ------------------------------------------------------------------------------------------------
+/** The entry's spot cannot take the building. The base builder takes the first priority entry on
+ * the list and nothing else that pass, so an entry that fails the same way every pass stops the
+ * whole base: a Hard GLA's Scud Storm flooded 7,406 cells, found nothing, and flooded again for
+ * 17,000 frames while 40,000 sat in the bank and nine buildings waited behind it. A priority
+ * entry is a one-off request, and whoever asked for it places it again with a fresh search, so
+ * it is blanked: every walk of the build list skips an entry with no template, and the base
+ * builder takes a priority entry whatever its rebuild count says. An entry of the map's own plan
+ * waits out the rebuild delay and lets the rest of the list go first. */
+// ------------------------------------------------------------------------------------------------
+static void giveUpOnSpot( BuildListInfo *info )
+{
+ if( info->isPriorityBuild() )
+ info->setTemplateName( AsciiString::TheEmptyString );
+ else
+ info->setObjectTimestamp( TheGameLogic->getFrame() + 1 );
+}
+
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
Object *AIPlayer::buildStructureWithDozer(const ThingTemplate *bldgPlan, BuildListInfo *info)
@@ -930,6 +1006,7 @@ Object *AIPlayer::buildStructureWithDozer(const ThingTemplate *bldgPlan, BuildLi
dozer, m_player ) != LBC_OK ) {
// If there's enemy units or structures, don't build/rebuild.
TheTerrainVisual->removeAllBibs(); // isLocationLegalToBuild adds bib feedback, turn it off. jba.
+ giveUpOnSpot(info);
return NULL;
}
@@ -962,8 +1039,8 @@ Object *AIPlayer::buildStructureWithDozer(const ThingTemplate *bldgPlan, BuildLi
When nothing fits, EA settled for the original spot, checked with
NO_ENEMY_OBJECT_OVERLAP alone. That option skips every structure that is not an
enemy's, the AI's own buildings included, and it is how a base grew buildings inside
- buildings. No spot now means no building this pass; the next pass floods again with
- whatever has been sold or destroyed since. */
+ buildings. No spot now means no building from this entry: giveUpOnSpot drops it or
+ sets it aside, so the rest of the list is not held behind it. */
static const Int NEIGHBOURS[8][2] = { {1,0}, {-1,0}, {0,1}, {0,-1}, {1,1}, {-1,1}, {1,-1}, {-1,-1} };
const Int searchRadius = isSkirmishAI() ? SKIRMISH_BUILD_SEARCH_CELLS : BUILD_SEARCH_CELLS;
const Int searchWidth = 2*searchRadius + 1;
@@ -1028,6 +1105,8 @@ Object *AIPlayer::buildStructureWithDozer(const ThingTemplate *bldgPlan, BuildLi
TheTerrainVisual->removeAllBibs(); // isLocationLegalToBuild adds bib feedback
if (searchUnfinished) {
m_buildDelay = 1; // try again next frame; doBaseBuilding leaves any value >= 1 alone
+ } else {
+ giveUpOnSpot(info);
}
return NULL;
}
@@ -2771,6 +2850,45 @@ void AIPlayer::buildUpgrade(const AsciiString &upgrade)
return;
}
+/** Defenses buildBySupplies puts beside one warehouse. */
+static const Int MAX_DEFENSES_PER_WAREHOUSE = 2;
+
+struct OwnedNearCount
+{
+ const ThingTemplate *tmpl;
+ Coord3D at;
+ Real radiusSqr;
+ Int count;
+};
+
+static void countOwnedNearObject( Object *obj, void *userData )
+{
+ OwnedNearCount *search = (OwnedNearCount *)userData;
+ if( !obj->isEffectivelyDead() && obj->getTemplate()->isEquivalentTo( search->tmpl ) &&
+ sqr( obj->getPosition()->x - search->at.x ) + sqr( obj->getPosition()->y - search->at.y ) <= search->radiusSqr )
+ ++search->count;
+}
+
+/** This player's copies of a building within radius of a point: standing, going up, or asked for
+ * and waiting for a dozer. */
+static Int countOwnedNear( Player *player, const ThingTemplate *tmpl, const Coord3D *at, Real radius )
+{
+ OwnedNearCount search;
+ search.tmpl = tmpl;
+ search.at = *at;
+ search.radiusSqr = radius * radius;
+ search.count = 0;
+ player->iterateObjects( countOwnedNearObject, &search );
+ for( BuildListInfo *info = player->getBuildList(); info; info = info->getNext() )
+ {
+ if( info->isPriorityBuild() && info->isBuildable() && info->getObjectID() == INVALID_ID &&
+ info->getTemplateName() == tmpl->getName() &&
+ sqr( info->getLocation()->x - at->x ) + sqr( info->getLocation()->y - at->y ) <= search.radiusSqr )
+ ++search.count;
+ }
+ return search.count;
+}
+
// ------------------------------------------------------------------------------------------------
/** Build a supply center near a supply source with minimumCash or more resources. */
// ------------------------------------------------------------------------------------------------
@@ -2786,6 +2904,13 @@ void AIPlayer::buildBySupplies(Int minimumCash, const AsciiString& thingName)
if (curWarehouse) {
bestSupplyWarehouse = curWarehouse;
}
+ // Every call aims at the same spot beside the warehouse and the wiggle below takes the nearest
+ // free ground to it, so each defense the scripts asked for went up against the last one: a
+ // Hard China stood nine Gattling cannons shoulder to shoulder at one pile.
+ if (bestSupplyWarehouse && countOwnedNear(m_player, tTemplate, bestSupplyWarehouse->getPosition(),
+ SUPPLY_CENTER_CLOSE_DIST + bestSupplyWarehouse->getGeometryInfo().getBoundingCircleRadius()) >= MAX_DEFENSES_PER_WAREHOUSE) {
+ return;
+ }
}
@@ -5227,7 +5352,19 @@ void AIPlayer::buildAsap( const ThingTemplate *tmpl )
spot.x -= dir.x * m_baseRadius * POWER_SETBACK;
spot.y -= dir.y * m_baseRadius * POWER_SETBACK;
}
- placeNear( tmpl, &spot, 0.0f );
+ if( placeNear( tmpl, &spot, 0.0f ) )
+ return;
+
+ /* A superweapon's footprint rarely fits one of placeNear's two dozen tries in a grown base, so the
+ request goes down behind the base anyway and the dozer's flood search looks for the room. A
+ base with none floods seven thousand cells for nothing, and giveUpOnSpot drops the request, so
+ it is only put down every sixth check: asked every check, one Hard GLA spent 3,687 frames of a
+ match flooding for a Scud Storm with the rest of its base waiting behind it. */
+ const Int FLOOD_EVERY_CHECKS = 6;
+ if( (TheGameLogic->getFrame() / SUPERWEAPON_CHECK_RATE) % FLOOD_EVERY_CHECKS != 0 )
+ return;
+ spot.z = 0;
+ m_player->addToPriorityBuildList( tmpl->getName(), &spot, tmpl->getPlacementViewAngle() );
}
//----------------------------------------------------------------------------------------------------------