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 @@ -2214,7 +2214,7 @@ "MovementUpdate": function(msg) { // If it looks like the path is failing, and we are close enough (3 tiles) from wanted range // stop anyways. This avoids pathing for an unreachable goal and reduces lag considerably. - if (msg.likelyFailure || + if (msg.likelyFailure || msg.obstructed && this.RelaxedMaxRangeCheck(this.order.data, this.order.data.max + this.DefaultRelaxedMaxRange) || !msg.obstructed && this.CheckRange(this.order.data)) this.FinishOrder(); @@ -2224,6 +2224,35 @@ }, "GATHER": { + "enter": function() { + let cmpSupply = Engine.QueryInterface(this.order.data.target, IID_ResourceSupply); + if (!cmpSupply) + return false; + + let resourceType = cmpSupply.GetType(); + let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); + + // If the unit is full go to the nearest dropsite instead of trying to gather. + if (cmpResourceGatherer && cmpResourceGatherer.CanCarryMore(resourceType.generic)) + return false; + + let nearestDropsite = this.FindNearestDropsite(resourceType.generic); + // Oh no, couldn't find any drop sites. Give up on gathering. + if (!nearestDropsite) + { + this.FinishOrder(); + return true; + } + + this.PushOrderFront("ReturnResource", { + "target": nearestDropsite, + "force": false, + "type": resourceType + }); + + return true; + }, + "leave": function() { // Show the carried resource, if we've gathered anything. this.SetDefaultAnimationVariant(); @@ -5587,7 +5616,7 @@ this.workOrders.length && this.workOrders[0].type == "Trade") { let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); - if (cmpTrader.HasBothMarkets() && + if (cmpTrader.HasBothMarkets() && (cmpTrader.GetFirstMarket() == target && cmpTrader.GetSecondMarket() == source || cmpTrader.GetFirstMarket() == source && cmpTrader.GetSecondMarket() == target)) {