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(ai): place buildings and defenses by ground distance from the base

Olcay Seygan375af9c8730a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f688a6fde..a57fb4f12 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -192,6 +192,7 @@ found and fixed — EA's own, not port damage.**
- 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.
+- New buildings go up on the computer's own side of the hill. It used to try spots on circles round its base, measured in a straight line, so a war factory could land across a valley or on a ledge behind a cliff: close on the map, a long drive round, and the dozer was teleported there. It now walks outwards from the base over ground a truck can drive, and the nearest spot by the road wins. Its guns are aimed at the road in. A base defence used to be scored by how much ground in front of it lay nearer the enemy as the crow flies, which pointed guns at cliff faces the enemy can never climb. It now works out how the enemy actually drives to the base and weighs the ground on that route most, and a spot the base cannot drive to is not used at all. On every difficulty.
- 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.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
index a7fa708e7..19d216118 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
@@ -389,6 +389,42 @@ Int aiDefenseAllowance( Int army, UnsignedInt frame );
* so a spot at the back scores a fraction of one at the front, and none inside the base. */
Real aiFireSampleWeight( Real baseToThreat, Real sampleToThreat, Real reach, Bool outsideBase );
+/** Ground distance over the pathfind grid, handed out nearest first. A step to a side neighbour
+ * costs AI_GROUND_STEP and a diagonal one AI_GROUND_DIAGONAL, a 1.5 standing in for 1.41 so every
+ * distance is a small integer: the cells wait in four buckets that wrap (Dial's algorithm), with
+ * no heap and no tie broken by anything but the order the cells were reached, which is the same
+ * on every machine. Whether a cell may be entered is asked once, of the callback; a source is
+ * handed out even when it may not be entered itself, so a flood seeded on a cliff edge still
+ * reaches the ground beside it. */
+static const Int AI_GROUND_STEP = 2;
+static const Int AI_GROUND_DIAGONAL = 3;
+static const UnsignedShort AI_GROUND_UNREACHED = 0xFFFF;
+
+class AIGroundFlood
+{
+public:
+ typedef Bool (*WalkableFunc)( Int x, Int y, void *userData );
+ AIGroundFlood( Int width, Int height, WalkableFunc walkable, void *userData );
+ void addSource( Int x, Int y ); ///< distance 0; every source goes in before the first next()
+ void setMaxDistance( Int maxDistance ) { m_max = maxDistance; } ///< nothing further is entered
+ Bool next( Int *x, Int *y, Int *distance ); ///< the nearest cell not yet handed out, FALSE when none is left
+ std::vector<UnsignedShort> &field( void ) { return m_dist; } ///< y * width + x; final once next() has said FALSE
+
+private:
+ Bool walkable( Int x, Int y, Int index );
+ Int m_width;
+ Int m_height;
+ WalkableFunc m_walkable;
+ void *m_userData;
+ std::vector<UnsignedShort> m_dist;
+ std::vector<UnsignedByte> m_walk; ///< 0 not asked yet, 1 may be entered, 2 may not
+ std::vector<Int> m_bucket[ 4 ];
+ Int m_at;
+ Int m_cursor;
+ Int m_pending;
+ Int m_max;
+};
+
static const Int AI_SUPPORT_DEFENSE_WEIGHT = 3;
Int aiPickBaseDefense( const Int *standing, const Bool *armed, Int count );
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
index f46f8c13a..20b4dd5e3 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
@@ -385,10 +385,13 @@ protected:
void buyMoneyUnits(void);
Int moneyUnitRoom(void) const; ///< how many more money units this player's army and internet centers carry
Bool hasEnoughMoneyUnitsFor(TeamPrototype *proto) const; ///< this team is all hackers and there is no room for more
- Bool 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 fill
+ Bool placeNear(const ThingTemplate *tmpl, const Coord3D *center, Real innerRadius, Bool walkOutward); ///< the nearest legal, safe spot by the ground from center, queued for a dozer. walkOutward keeps searching further out as the near ground fills
Bool 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 none
- Bool placeDefense(const ThingTemplate *defense); ///< queue it on the ring spot whose clear field of fire covers the most ground toward the enemy
+ Bool placeDefense(const ThingTemplate *defense); ///< queue it on the ring spot whose clear field of fire covers the most ground the enemy drives in over
+ const std::vector<UnsignedShort> &groundFromBase(void); ///< AIGroundFlood distance of every pathfind cell from the base center, cached until the center moves
+ const std::vector<UnsignedShort> &groundFromThreats(const Coord3D *threats, Int count); ///< the same from the nearest of these, cached until they change
+ Bool reachableFromBase(const Coord3D *pos); ///< a vehicle at the base center could drive there; TRUE before the base is placed
Real knownFirepowerAlongPath(Waypoint *way); ///< what this AI has seen that can shoot, along an approach
AsciiString secondApproachLabel(const Coord3D *from, const AsciiString &taken, Int pathSuffix); ///< the quietest other road, or empty
Bool 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 filling
@@ -626,6 +629,17 @@ protected:
const ThingTemplate *m_buildSearchPlan; ///< the building whose footprint the search is trying
Coord3D m_buildProbePos;
+ /* Ground distance fields over the whole pathfind grid (groundFromBase, groundFromThreats), not
+ xferred for the same reason: each is a pure function of the map, the bridges standing and
+ the point it was flooded from, recomputed the first time it is asked for after the point
+ moves or a bridge falls or is put back. */
+ std::vector<UnsignedShort> m_groundFromBase;
+ Coord3D m_groundFromBaseAt;
+ UnsignedInt m_groundFromBaseBridges;
+ std::vector<UnsignedShort> m_groundFromThreats;
+ std::vector<Coord3D> m_groundThreatsAt;
+ UnsignedInt m_groundThreatsBridges;
+
GameDifficulty m_difficulty;
Int m_skillsetSelector;
@@ -633,7 +647,7 @@ protected:
Coord3D m_baseCenter; // Center of the initial build list of structures.
Bool m_baseCenterSet; // True if baseCenter is valid.
Real m_baseRadius; // Radius of the initial build list of structures.
- Int m_placementRing; ///< first ring placeNear tries when the search is allowed to walk outward
+ Int m_placementRing; ///< ground distance (AIGroundFlood units) placeNear starts trying spots at when it may walk outward
// Bridge repair info.
enum {MAX_STRUCTURES_TO_REPAIR = 2};
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
index 9264f580b..e2992b7a3 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
@@ -1413,6 +1413,88 @@ Real aiFireSampleWeight( Real baseToThreat, Real sampleToThreat, Real reach, Boo
return outsideBase ? weight + 0.1f : weight;
}
+//-------------------------------------------------------------------------------------------------
+AIGroundFlood::AIGroundFlood( Int width, Int height, WalkableFunc walkable, void *userData ) :
+ m_width( width ),
+ m_height( height ),
+ m_walkable( walkable ),
+ m_userData( userData ),
+ m_dist( width * height, AI_GROUND_UNREACHED ),
+ m_walk( width * height, 0 ),
+ m_at( 0 ),
+ m_cursor( 0 ),
+ m_pending( 0 ),
+ m_max( AI_GROUND_UNREACHED - 1 )
+{
+}
+
+//-------------------------------------------------------------------------------------------------
+void AIGroundFlood::addSource( Int x, Int y )
+{
+ if( x < 0 || y < 0 || x >= m_width || y >= m_height )
+ return;
+ const Int index = y * m_width + x;
+ if( m_dist[ index ] == 0 )
+ return;
+ m_dist[ index ] = 0;
+ m_bucket[ 0 ].push_back( index );
+ ++m_pending;
+}
+
+//-------------------------------------------------------------------------------------------------
+Bool AIGroundFlood::walkable( Int x, Int y, Int index )
+{
+ if( m_walk[ index ] == 0 )
+ m_walk[ index ] = m_walkable( x, y, m_userData ) ? 1 : 2;
+ return m_walk[ index ] == 1;
+}
+
+//-------------------------------------------------------------------------------------------------
+Bool AIGroundFlood::next( Int *x, Int *y, Int *distance )
+{
+ static const Int STEPS[ 8 ][ 3 ] = {
+ { 1, 0, AI_GROUND_STEP }, { -1, 0, AI_GROUND_STEP }, { 0, 1, AI_GROUND_STEP }, { 0, -1, AI_GROUND_STEP },
+ { 1, 1, AI_GROUND_DIAGONAL }, { -1, 1, AI_GROUND_DIAGONAL }, { 1, -1, AI_GROUND_DIAGONAL }, { -1, -1, AI_GROUND_DIAGONAL } };
+
+ while( m_pending > 0 )
+ {
+ std::vector<Int> &bucket = m_bucket[ m_at & 3 ];
+ if( m_cursor >= (Int)bucket.size() )
+ {
+ m_pending -= (Int)bucket.size();
+ bucket.clear();
+ m_cursor = 0;
+ ++m_at;
+ continue;
+ }
+ const Int index = bucket[ m_cursor++ ];
+ if( m_dist[ index ] != m_at )
+ continue; // reached again by a shorter way, and handed out from that bucket
+
+ const Int cx = index % m_width;
+ const Int cy = index / m_width;
+ for( Int n = 0; n < 8; ++n )
+ {
+ const Int nx = cx + STEPS[ n ][ 0 ];
+ const Int ny = cy + STEPS[ n ][ 1 ];
+ if( nx < 0 || ny < 0 || nx >= m_width || ny >= m_height )
+ continue;
+ const Int d = m_at + STEPS[ n ][ 2 ];
+ const Int nIndex = ny * m_width + nx;
+ if( d > m_max || d >= m_dist[ nIndex ] || !walkable( nx, ny, nIndex ) )
+ continue;
+ m_dist[ nIndex ] = (UnsignedShort)d;
+ m_bucket[ d & 3 ].push_back( nIndex );
+ ++m_pending;
+ }
+ *x = cx;
+ *y = cy;
+ *distance = m_at;
+ return TRUE;
+ }
+ return FALSE;
+}
+
//-------------------------------------------------------------------------------------------------
Int aiPickBaseDefense( const Int *standing, const Bool *armed, Int count )
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 824f50471..bb862c837 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -429,6 +429,9 @@ m_role(AIROLE_AGGRESSIVE)
}
m_repairDozerOrigin.zero();
m_buildProbePos.zero();
+ m_groundFromBaseAt.zero();
+ m_groundFromBaseBridges = 0;
+ m_groundThreatsBridges = 0;
m_baseCenter.zero();
m_baseCenterSet = false;
m_placementRing = 0;
@@ -1075,6 +1078,116 @@ static Bool isBuildSearchWalkable( Int cellX, Int cellY, const Coord3D *worldPos
return FALSE;
}
+/** The pathfind grid's size in cells, for an AIGroundFlood over all of it. */
+static void groundGridSize( Int *width, Int *height )
+{
+ const ICoord2D *hi = TheAI->pathfinder()->getExtent();
+ *width = hi->x + 1;
+ *height = hi->y + 1;
+}
+
+/** isBuildSearchWalkable for an AIGroundFlood. userData, when set, is a field from groundFromBase:
+ * a cell the base cannot drive to is not entered either. */
+static Bool groundFloodWalkable( Int x, Int y, void *userData )
+{
+ const std::vector<UnsignedShort> *fromBase = (const std::vector<UnsignedShort> *)userData;
+ if( fromBase )
+ {
+ Int width, height;
+ groundGridSize( &width, &height );
+ if( (*fromBase)[ y * width + x ] == AI_GROUND_UNREACHED )
+ return FALSE;
+ }
+ Coord3D at;
+ at.x = ( x + 0.5f ) * PATHFIND_CELL_SIZE_F;
+ at.y = ( y + 0.5f ) * PATHFIND_CELL_SIZE_F;
+ at.z = 0.0f;
+ return isBuildSearchWalkable( x, y, &at );
+}
+
+/** A flood over the whole grid from these points, run to the end, into field. */
+static void floodWholeGrid( const Coord3D *from, Int count, std::vector<UnsignedShort> &field )
+{
+ Int width, height;
+ groundGridSize( &width, &height );
+ AIGroundFlood flood( width, height, groundFloodWalkable, NULL );
+ for( Int i = 0; i < count; ++i )
+ {
+ ICoord2D cell;
+ TheAI->pathfinder()->worldToCell( &from[ i ], &cell );
+ flood.addSource( cell.x, cell.y );
+ }
+ Int x, y, d;
+ while( flood.next( &x, &y, &d ) )
+ ;
+ field.swap( flood.field() );
+}
+
+/** Which bridges are down, one bit each in list order, folded past 32. Part of every ground field's
+ * key: a field kept across a bridge falling would answer differently from one flooded after a
+ * savegame load, which starts with no field. */
+static UnsignedInt bridgesDownKey( void )
+{
+ UnsignedInt key = 0;
+ for( Bridge *bridge = TheTerrainLogic->getFirstBridge(); bridge; bridge = bridge->getNext() )
+ key = ( ( key << 1 ) | ( key >> 31 ) ) ^ ( bridge->peekBridgeInfo()->curDamageState == BODY_RUBBLE ? 1u : 0u );
+ return key;
+}
+
+/** The field's value at a world position, AI_GROUND_UNREACHED for ground not reached. */
+static Int groundAt( const std::vector<UnsignedShort> &field, const Coord3D *pos )
+{
+ Int width, height;
+ groundGridSize( &width, &height );
+ ICoord2D cell;
+ TheAI->pathfinder()->worldToCell( pos, &cell );
+ return field[ cell.y * width + cell.x ];
+}
+
+// ------------------------------------------------------------------------------------------------
+/** How far every cell of the map is from the base center by the ground. The building searches and
+ * the defence scoring all ask the same question, whether a spot is joined to the base by ground a
+ * vehicle can cross, and a ring of straight-line distances cannot answer it: the far side of a
+ * valley or a ledge behind a cliff is close on the map and a long drive round, or no drive at all.
+ * One flood of the whole map, then a lookup until the base center moves.
+ * Structures count as ground (isBuildSearchWalkable), so the base's own buildings do not cut it
+ * in two. A bridge falling or put back floods it again. */
+// ------------------------------------------------------------------------------------------------
+const std::vector<UnsignedShort> &AIPlayer::groundFromBase( void )
+{
+ const UnsignedInt bridges = bridgesDownKey();
+ if( m_groundFromBase.empty() || m_groundFromBaseAt.x != m_baseCenter.x || m_groundFromBaseAt.y != m_baseCenter.y ||
+ m_groundFromBaseBridges != bridges )
+ {
+ m_groundFromBaseAt = m_baseCenter;
+ m_groundFromBaseBridges = bridges;
+ floodWholeGrid( &m_baseCenter, 1, m_groundFromBase );
+ }
+ return m_groundFromBase;
+}
+
+// ------------------------------------------------------------------------------------------------
+const std::vector<UnsignedShort> &AIPlayer::groundFromThreats( const Coord3D *threats, Int count )
+{
+ const UnsignedInt bridges = bridgesDownKey();
+ Bool same = !m_groundFromThreats.empty() && (Int)m_groundThreatsAt.size() == count && m_groundThreatsBridges == bridges;
+ for( Int i = 0; same && i < count; ++i )
+ same = m_groundThreatsAt[ i ].x == threats[ i ].x && m_groundThreatsAt[ i ].y == threats[ i ].y;
+ if( !same )
+ {
+ m_groundThreatsAt.assign( threats, threats + count );
+ m_groundThreatsBridges = bridges;
+ floodWholeGrid( threats, count, m_groundFromThreats );
+ }
+ return m_groundFromThreats;
+}
+
+// ------------------------------------------------------------------------------------------------
+Bool AIPlayer::reachableFromBase( const Coord3D *pos )
+{
+ return !m_baseCenterSet || groundAt( groundFromBase(), pos ) != AI_GROUND_UNREACHED;
+}
+
// ------------------------------------------------------------------------------------------------
/** 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
@@ -1235,6 +1348,8 @@ Object *AIPlayer::buildStructureWithDozer(const ThingTemplate *bldgPlan, BuildLi
newPos.x = pos.x + dx*PATHFIND_CELL_SIZE_F;
newPos.y = pos.y + dy*PATHFIND_CELL_SIZE_F;
if (home && sqr(newPos.x - m_baseCenter.x) + sqr(newPos.y - m_baseCenter.y) > sqr(BASE_EDGE_REACH*m_baseRadius)) continue;
+ // a flood from a spot that is itself cut off from the base stays on that ground
+ if (isSkirmishAI() && !reachableFromBase(&newPos)) continue;
++probes;
valid = TheBuildAssistant->isLocationLegalToBuild( &newPos, bldgPlan, angle,
BuildAssistant::CLEAR_PATH |
@@ -5243,15 +5358,15 @@ static const Real DEFENSE_STANDOFF = 1.0f;
/** Dozers a Hard AI trains on its own when every one it has is on a building. */
static const Int MAX_ECONOMY_DOZERS = 4;
-/** Tries on each ring when looking for somewhere to put a purchase down. Every try is a legality
- * check that costs about a millisecond, so a call still only walks two rings: that is the spike,
- * not the reach of the search. A search allowed to walk outward remembers the ring it reached
- * and starts the next call there, so a full opening base does not end the search. */
+/** Legality checks placeNear may spend per call, as rings of tries: PLACEMENT_RINGS times
+ * PLACEMENT_ANGLES. Every try costs about a millisecond, so that is the spike, not the reach of
+ * the search. A search allowed to walk outward remembers the ground distance it reached and starts
+ * the next call there, so a full opening base does not end the search. */
static const Int PLACEMENT_ANGLES = 12;
static const Int PLACEMENT_RINGS = 2;
-/** Rings a call tries for a building that stays home (staysHome). It has no outward walk to carry
- * on with next time, and a spot past the base's edge costs no legality check. */
+/** Rings' worth of tries a call spends on a building that stays home (staysHome). It has no outward
+ * walk to carry on with next time, and a spot past the base's edge costs no legality check. */
static const Int HOME_PLACEMENT_RINGS = 6;
/** How far either side of a waypoint an approach counts the guns along it. */
@@ -5290,6 +5405,24 @@ static Bool groundLineClear( const Coord3D *from, const Coord3D *to )
return TRUE;
}
+/** Clear ground under the middle, the corners and the edges of a square half wide either side of pos:
+ * a look at nine pathfind cells, against the millisecond a legality check costs. */
+static Bool footprintOnClearGround( const Coord3D *pos, Real half )
+{
+ for( Int corner = 0; corner < 9; ++corner )
+ {
+ Coord3D at = *pos;
+ at.x += half * ( corner % 3 - 1 );
+ at.y += half * ( corner / 3 - 1 );
+ ICoord2D cell;
+ TheAI->pathfinder()->worldToCell( &at, &cell );
+ const PathfindCell *pathCell = TheAI->pathfinder()->getCell( LAYER_GROUND, cell.x, cell.y );
+ if( pathCell == NULL || pathCell->getType() != PathfindCell::CELL_CLEAR )
+ return FALSE;
+ }
+ return TRUE;
+}
+
/** Something of this kind the builder can make right now, off the builder's own buttons, so the
* answer is right for every faction and general without a table of names. moneyLater also takes one
* the bank cannot pay for yet, for a request the purchase queue saves up for. */
@@ -5392,13 +5525,21 @@ static const Int DEFENSE_SPOTS_PER_RING = 24;
static const Int DEFENSE_FIRE_SAMPLES = 16;
/** Best-scored spots asked for legality, each about a millisecond. */
static const Int DEFENSE_PLACE_TRIES = 12;
+/** Ground counts as the enemy's road in when the drive from him through it to the base center is no
+ * more than this much longer than his shortest one, and a sample on it counts this much more. */
+static const Real ROUTE_SLACK = 1.15f;
+static const Real ROUTE_BONUS = 1.0f;
//----------------------------------------------------------------------------------------------------------
/** A defence goes where its guns see the most of the ground an enemy comes over. A spot on a ring
* round the base is scored by the open ground inside its reach: each sample point on clear,
* drivable ground counts for how much nearer the enemy it lies than the middle of the base does
* (aiFireSampleWeight), plus a little for being outside the base at all, and counts nothing when a building of ours or a cliff stands
- * between it and the spot. Every defence already standing or queued within three quarters of the
+ * between it and the spot. Nearer is by the ground, off groundFromThreats: measured in a straight
+ * line, the cliff face behind a base looked like its front door, and the guns went up facing a
+ * mountain while the enemy drove in round the side. A sample on the enemy's shortest drive in
+ * (ROUTE_SLACK of it) counts ROUTE_BONUS more, so the guns cover the road he takes, and a spot the
+ * base cannot drive to is not scored at all. Every defence already standing or queued within three quarters of the
* reach divides the score, so the guns spread along the front instead of stacking. The enemy is
* where enemyStartGuess says, the same knowledge every other decision here uses. The spot that went
* up in front of a fixed point used to land behind the base's own buildings on a crowded map, and a
@@ -5430,6 +5571,13 @@ Bool AIPlayer::placeDefense( const ThingTemplate *defense )
threatCount = 1;
}
+ // an enemy who cannot drive here (an island, a cliff top) is weighed in a straight line as before
+ const std::vector<UnsignedShort> &fromBase = groundFromBase();
+ const std::vector<UnsignedShort> &fromThreat = groundFromThreats( threats, threatCount );
+ const Int routeLength = groundAt( fromThreat, &m_baseCenter );
+ const Bool byGround = routeLength != AI_GROUND_UNREACHED;
+ const Real GROUND_TO_WORLD = PATHFIND_CELL_SIZE_F / AI_GROUND_STEP;
+
std::vector<Coord3D> guns;
m_player->iterateObjects( collectDefensePositions, &guns );
for( BuildListInfo *info = m_player->getBuildList(); info; info = info->getNext() )
@@ -5471,18 +5619,10 @@ Bool AIPlayer::placeDefense( const ThingTemplate *defense )
// or on top of a gun already there or queued. Without this 11 of every 12 checks failed, most
// of them on restricted terrain under the footprint's edge, so the corners are looked at too
{
- Bool open = TRUE;
- for( Int corner = 0; corner < 9 && open; ++corner )
- {
- Coord3D at = spot[ s ];
- at.x += footprint * ( corner % 3 - 1 );
- at.y += footprint * ( corner / 3 - 1 );
- ICoord2D cell;
- TheAI->pathfinder()->worldToCell( &at, &cell );
- const PathfindCell *pathCell = TheAI->pathfinder()->getCell( LAYER_GROUND, cell.x, cell.y );
- open = pathCell != NULL && pathCell->getType() == PathfindCell::CELL_CLEAR;
- }
- if( !open )
+ if( !footprintOnClearGround( &spot[ s ], footprint ) )
+ continue;
+ // a ledge or the far side of a valley the base cannot drive to guards nothing the enemy drives over
+ if( groundAt( fromBase, &spot[ s ] ) == AI_GROUND_UNREACHED )
continue;
// the rest failed on the clearance kept round another building, ours or anybody's
PartitionFilterAcceptByKindOf structuresOnly( MAKE_KINDOF_MASK( KINDOF_STRUCTURE ), KINDOFMASK_NONE );
@@ -5524,20 +5664,34 @@ Bool AIPlayer::placeDefense( const ThingTemplate *defense )
// measured against the enemy nearest this ground, from the middle of the base: ground the
// enemy has to cross to reach us, not merely ground in front of this spot
- Real sampleToThreat = -1.0f;
- Int nearest = 0;
- for( Int t = 0; t < threatCount; ++t )
+ const Bool outsideBase = sqr( at.x - m_baseCenter.x ) + sqr( at.y - m_baseCenter.y ) > sqr( m_baseRadius );
+ Real weight;
+ if( byGround )
{
- const Real d = sqrt( sqr( threats[ t ].x - at.x ) + sqr( threats[ t ].y - at.y ) );
- if( sampleToThreat < 0.0f || d < sampleToThreat )
+ const Int index = cell.y * ( TheAI->pathfinder()->getExtent()->x + 1 ) + cell.x;
+ const Int sampleToThreat = fromThreat[ index ];
+ if( sampleToThreat == AI_GROUND_UNREACHED )
+ continue; // a pocket the enemy cannot drive into
+ weight = aiFireSampleWeight( routeLength * GROUND_TO_WORLD, sampleToThreat * GROUND_TO_WORLD, reach, outsideBase );
+ if( fromBase[ index ] != AI_GROUND_UNREACHED && sampleToThreat + fromBase[ index ] <= routeLength * ROUTE_SLACK )
+ weight += ROUTE_BONUS;
+ }
+ else
+ {
+ Real sampleToThreat = -1.0f;
+ Int nearest = 0;
+ for( Int t = 0; t < threatCount; ++t )
{
- sampleToThreat = d;
- nearest = t;
+ const Real d = sqrt( sqr( threats[ t ].x - at.x ) + sqr( threats[ t ].y - at.y ) );
+ if( sampleToThreat < 0.0f || d < sampleToThreat )
+ {
+ sampleToThreat = d;
+ nearest = t;
+ }
}
+ const Real baseToThreat = sqrt( sqr( threats[ nearest ].x - m_baseCenter.x ) + sqr( threats[ nearest ].y - m_baseCenter.y ) );
+ weight = aiFireSampleWeight( baseToThreat, sampleToThreat, reach, outsideBase );
}
- const Real baseToThreat = sqrt( sqr( threats[ nearest ].x - m_baseCenter.x ) + sqr( threats[ nearest ].y - m_baseCenter.y ) );
- const Real weight = aiFireSampleWeight( baseToThreat, sampleToThreat, reach,
- sqr( at.x - m_baseCenter.x ) + sqr( at.y - m_baseCenter.y ) > sqr( m_baseRadius ) );
if( weight <= 0.0f )
continue;
if( !groundLineClear( &spot[ s ], &at ) )
@@ -6730,10 +6884,17 @@ Bool AIPlayer::queueExtraFactory( Object *dozer, KindOfType kind, Bool unlimited
}
//----------------------------------------------------------------------------------------------------------
-/** Walk rings round center until a spot is legal and safe, and hand it to the dozers as a priority
- * build. A call still tries two rings, two dozen legality checks. walkOutward carries the first
- * ring in m_placementRing, so the search moves out across calls instead of giving up at the opening
- * base, and wraps back to the middle once that ring is past the pathfind extent. */
+/** Flood out from center over ground a vehicle can cross, nearest first by the ground, until a spot
+ * is legal and safe, and hand it to the dozers as a priority build. It used to try twelve angles
+ * on rings round center, and a ring is a straight-line distance: the first legal spot on it could
+ * be across a valley or on a ledge behind a cliff, near on the map and a long drive round, and a
+ * factory went up there with the dozer teleported to it. The flood only enters ground the base
+ * itself can drive to (groundFromBase), so the nearest spot it finds is the nearest by the road.
+ *
+ * A call still spends at most two rings' worth of legality checks, two dozen, and a footprint on
+ * anything but clear ground is turned away before one is spent. walkOutward carries the ground
+ * distance it reached in m_placementRing, so the search moves out across calls instead of giving
+ * up at the opening base, and goes back to the middle once the flood runs out of ground. */
//----------------------------------------------------------------------------------------------------------
Bool AIPlayer::placeNear( const ThingTemplate *tmpl, const Coord3D *center, Real innerRadius, Bool walkOutward )
{
@@ -6748,62 +6909,75 @@ Bool AIPlayer::placeNear( const ThingTemplate *tmpl, const Coord3D *center, Real
if( home )
walkOutward = FALSE;
- Int startRing = 0;
- if( walkOutward )
- {
- Region3D extent;
- TheTerrainLogic->getMaximumPathfindExtent( &extent );
- Real farthest = 0.0f;
- const Real cornerX[ 2 ] = { extent.lo.x, extent.hi.x };
- const Real cornerY[ 2 ] = { extent.lo.y, extent.hi.y };
- for( Int cx = 0; cx < 2; ++cx )
- {
- for( Int cy = 0; cy < 2; ++cy )
- {
- const Real dx = cornerX[ cx ] - center->x;
- const Real dy = cornerY[ cy ] - center->y;
- const Real dist = sqrt( dx * dx + dy * dy );
- if( dist > farthest )
- farthest = dist;
- }
+ // as far as the outermost ring reached, by the ground with half again for the way round; the
+ // outward walk goes as far as the ground does
+ const Real ringReach = innerRadius + structureRadius * ( 2 * rings + 1 );
+ const Int maxDistance = walkOutward ? AI_GROUND_UNREACHED - 1 : REAL_TO_INT_CEIL( ringReach * AI_GROUND_DIAGONAL / PATHFIND_CELL_SIZE_F );
+ const Int startDistance = walkOutward && m_placementRing > 0 ? m_placementRing : 0;
+ const Real innerSqr = innerRadius > 0.0f ? sqr( innerRadius + structureRadius ) : 0.0f;
+ // spots a structure's radius apart, as the rings were; never closer than every other cell
+ Int stride = REAL_TO_INT_FLOOR( structureRadius / PATHFIND_CELL_SIZE_F );
+ if( stride < 2 )
+ stride = 2;
+
+ Int width, height;
+ groundGridSize( &width, &height );
+ AIGroundFlood flood( width, height, groundFloodWalkable, m_baseCenterSet ? (void *)&groundFromBase() : NULL );
+ flood.setMaxDistance( maxDistance );
+ ICoord2D origin;
+ TheAI->pathfinder()->worldToCell( center, &origin );
+ flood.addSource( origin.x, origin.y );
+
+ Int tries = rings * PLACEMENT_ANGLES;
+ Int x, y, distance;
+ while( flood.next( &x, &y, &distance ) )
+ {
+ const Int dx = x - origin.x;
+ const Int dy = y - origin.y;
+ if( distance < startDistance || dx % stride != 0 || dy % stride != 0 )
+ continue;
+ if( TheAI->pathfinder()->getCell( LAYER_GROUND, x, y )->getType() != PathfindCell::CELL_CLEAR )
+ continue;
+ Coord3D pos = *center;
+ pos.x += dx * PATHFIND_CELL_SIZE_F;
+ pos.y += dy * PATHFIND_CELL_SIZE_F;
+ pos.z = 0;
+ if( sqr( pos.x - center->x ) + sqr( pos.y - center->y ) < innerSqr )
+ continue;
+ if( home && sqr( pos.x - m_baseCenter.x ) + sqr( pos.y - m_baseCenter.y ) > homeReachSqr )
+ continue;
+ // the square inside the footprint's circle; the corners of the circle's own square stick out
+ // past a building's corners and turned away spots that fit
+ if( !footprintOnClearGround( &pos, structureRadius * 0.7f ) )
+ continue;
+ if( tries == 0 )
+ {
+ // out of checks: the next call starts here, or one cell further when this call never
+ // got past the distance it started at, so a crowded ring cannot hold the walk forever
+ if( walkOutward )
+ m_placementRing = distance > startDistance ? distance : distance + 1;
+ return FALSE;
}
- const Real reach = innerRadius + structureRadius * (2 * m_placementRing + 1);
- if( m_placementRing < 0 || reach > farthest )
- m_placementRing = 0;
- startRing = m_placementRing;
- }
+ --tries;
- for( Int ring = startRing; ring < startRing + rings; ++ring )
- {
- const Real distance = innerRadius + structureRadius * (2 * ring + 1);
- for( Int step = 0; step < PLACEMENT_ANGLES; ++step )
- {
- const Real angle = 2.0f * PI * step / PLACEMENT_ANGLES;
- Coord3D pos = *center;
- pos.x += distance * Cos( angle );
- pos.y += distance * Sin( angle );
- pos.z = 0;
- if( home && sqr( pos.x - m_baseCenter.x ) + sqr( pos.y - m_baseCenter.y ) > homeReachSqr )
- continue;
-
- const Bool legal = TheBuildAssistant->isLocationLegalToBuild( &pos, tmpl, placeAngle,
- BuildAssistant::CLEAR_PATH | BuildAssistant::TERRAIN_RESTRICTIONS | BuildAssistant::NO_OBJECT_OVERLAP,
- NULL, m_player ) == LBC_OK;
- TheTerrainVisual->removeAllBibs(); // isLocationLegalToBuild adds bib feedback
- if( legal && isLocationSafe( &pos, tmpl ) )
- {
- if( walkOutward )
- m_placementRing = ring;
- DEBUG_LOG(("AI ECONOMY frame %d player %d builds '%s' at (%.0f,%.0f), %d in the bank, %.0f from a base of radius %.0f\n", TheGameLogic->getFrame(),
- m_player->getPlayerIndex(), tmpl->getName().str(), pos.x, pos.y, m_player->getMoney()->countMoney(),
- sqrt( sqr( pos.x - m_baseCenter.x ) + sqr( pos.y - m_baseCenter.y ) ), m_baseRadius));
- m_player->addToPriorityBuildList( tmpl->getName(), &pos, placeAngle );
- return TRUE;
- }
+ const Bool legal = TheBuildAssistant->isLocationLegalToBuild( &pos, tmpl, placeAngle,
+ BuildAssistant::CLEAR_PATH | BuildAssistant::TERRAIN_RESTRICTIONS | BuildAssistant::NO_OBJECT_OVERLAP,
+ NULL, m_player ) == LBC_OK;
+ TheTerrainVisual->removeAllBibs(); // isLocationLegalToBuild adds bib feedback
+ if( legal && isLocationSafe( &pos, tmpl ) )
+ {
+ if( walkOutward )
+ m_placementRing = distance;
+ DEBUG_LOG(("AI ECONOMY frame %d player %d builds '%s' at (%.0f,%.0f), %d in the bank, %.0f from a base of radius %.0f, %.0f by the ground from where it looked\n", TheGameLogic->getFrame(),
+ m_player->getPlayerIndex(), tmpl->getName().str(), pos.x, pos.y, m_player->getMoney()->countMoney(),
+ sqrt( sqr( pos.x - m_baseCenter.x ) + sqr( pos.y - m_baseCenter.y ) ), m_baseRadius,
+ distance * PATHFIND_CELL_SIZE_F / AI_GROUND_STEP));
+ m_player->addToPriorityBuildList( tmpl->getName(), &pos, placeAngle );
+ return TRUE;
}
}
if( walkOutward )
- m_placementRing = startRing + PLACEMENT_RINGS;
+ m_placementRing = 0; // every spot the ground holds was looked at: back to the middle
if( home )
DEBUG_LOG(("AI ECONOMY frame %d player %d has no room for '%s' within %.0f of its base, %d in the bank\n", TheGameLogic->getFrame(),
m_player->getPlayerIndex(), tmpl->getName().str(), BASE_EDGE_REACH * m_baseRadius, m_player->getMoney()->countMoney()));
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp
index 8cff2a69c..6da8bb08f 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp
@@ -833,7 +833,8 @@ void AISkirmishPlayer::buildAIBaseDefenseStructure(const AsciiString &thingName,
/* See if we can build there. */
Bool canBuild;
Real placeAngle = tTemplate->getPlacementViewAngle();
- canBuild = LBC_OK == TheBuildAssistant->isLocationLegalToBuild(&buildPos, tTemplate, placeAngle,
+ // a spot the base cannot drive to guards nothing: a ledge, or across a valley the path runs beside
+ canBuild = reachableFromBase(&buildPos) && LBC_OK == TheBuildAssistant->isLocationLegalToBuild(&buildPos, tTemplate, placeAngle,
BuildAssistant::TERRAIN_RESTRICTIONS|BuildAssistant::NO_OBJECT_OVERLAP, NULL, m_player);
TheTerrainVisual->removeAllBibs(); // isLocationLegalToBuild adds bib feedback, turn it off. jba.
if (flank) {
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 5f2d277d8..b71bb0cb7 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -12136,6 +12136,43 @@ TEST(ai_base_defenses_rotate_through_the_sides_types)
}
}
+/** A 7x5 grid split by a wall in column 3 with one gap at the bottom row. */
+static Bool groundFloodTestWalkable( Int x, Int y, void * )
+{
+ return x != 3 || y == 4;
+}
+
+/** The ground flood walks round a wall instead of through it, hands cells out nearest first, and
+ stops at its cap. */
+TEST(ground_flood_goes_round_the_wall_nearest_first)
+{
+ AIGroundFlood flood( 7, 5, groundFloodTestWalkable, NULL );
+ flood.addSource( 0, 0 );
+ Int x, y, d, last = 0, count = 0;
+ while( flood.next( &x, &y, &d ) )
+ {
+ CHECK( d >= last );
+ last = d;
+ ++count;
+ }
+ CHECK_EQ( count, 7 * 5 - 4 ); // the wall's four cells are never entered
+ const std::vector<UnsignedShort> &field = flood.field();
+ CHECK_EQ( (Int)field[ 0 * 7 + 1 ], AI_GROUND_STEP );
+ CHECK_EQ( (Int)field[ 1 * 7 + 1 ], AI_GROUND_DIAGONAL );
+ CHECK_EQ( (Int)field[ 0 * 7 + 3 ], (Int)AI_GROUND_UNREACHED );
+ // (4,0) is one cell past the wall: down to the gap at (3,4) and back up, 3 diagonals and 1 step
+ // to the gap, then 1 diagonal and 3 steps
+ CHECK_EQ( (Int)field[ 0 * 7 + 4 ], 4 * AI_GROUND_DIAGONAL + 4 * AI_GROUND_STEP );
+
+ AIGroundFlood capped( 7, 5, groundFloodTestWalkable, NULL );
+ capped.addSource( 0, 0 );
+ capped.setMaxDistance( AI_GROUND_STEP );
+ count = 0;
+ while( capped.next( &x, &y, &d ) )
+ ++count;
+ CHECK_EQ( count, 3 ); // the source and its two side neighbours
+}
+
/** 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)