Index: binaries/data/mods/public/simulation/components/UnitAI.js =================================================================== --- binaries/data/mods/public/simulation/components/UnitAI.js +++ binaries/data/mods/public/simulation/components/UnitAI.js @@ -2983,8 +2983,15 @@ } } - // Look for a nearby foundation to help with - let nearbyFoundation = this.FindNearbyFoundation(); + // Look for a nearby foundation to help with. + let nearbyFoundation = []; + // Search near the target first, as the orderer wanted to send us there. + let cmpPosition = Engine.QueryInterface(msg.data.newentity, IID_Position); + if (cmpPosition && cmpPosition.IsInWorld()) + nearbyFoundation = this.FindNearbyFoundation(cmpPosition.GetPosition2D()); + // Otherwise, look nearby (this is still better than walking to the target.) + if (!nearbyFoundation) + nearbyFoundation = this.FindNearbyFoundation(); if (nearbyFoundation) { this.AddOrder("Repair", { "target": nearbyFoundation, "autocontinue": oldData.autocontinue, "force": false }, true); @@ -4377,7 +4384,7 @@ * Returns the entity ID of the nearest building that needs to be constructed, * or undefined if none can be found close enough. */ -UnitAI.prototype.FindNearbyFoundation = function() +UnitAI.prototype.FindNearbyFoundation = function(position = null) { var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); if (!cmpOwnership || cmpOwnership.GetOwner() == INVALID_PLAYER) @@ -4389,7 +4396,12 @@ var range = 64; // TODO: what's a sensible number? var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); - var nearby = cmpRangeManager.ExecuteQuery(this.entity, 0, range, players, IID_Foundation); + + let nearby = null; + if (!position) + nearby = cmpRangeManager.ExecuteQuery(this.entity, 0, range, players, IID_Foundation); + else + nearby = cmpRangeManager.ExecuteQueryAroundPos(position, 0, range, players, IID_Foundation); // Skip foundations that are already complete. (This matters since // we process the ConstructionFinished message before the foundation