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,36 @@ }, "GATHER": { + "enter": function() + { + let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); + if (cmpSupply) + { + 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.CanCarryMore(resourceType.generic)) + { + let nearestDropsite = this.FindNearestDropsite(resourceType.generic); + // Oh no, couldn't find any drop sites. Give up on gathering. + if (!nearestDropsite) + { + this.FinishOrder(); + return false; + } + + this.PushOrderFront("ReturnResource", { + "target": nearestDropsite, + "force": false, + "type": resourceType + }); + } + + return true; + } + return false; + }, "leave": function() { // Show the carried resource, if we've gathered anything. this.SetDefaultAnimationVariant(); @@ -2233,9 +2263,10 @@ "enter": function() { this.gatheringTarget = this.order.data.target; // temporary, deleted in "leave". + + let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); // Check that we can gather from the resource we're supposed to gather from. let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); - let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); let cmpMirage = Engine.QueryInterface(this.gatheringTarget, IID_Mirage); if ((!cmpMirage || !cmpMirage.Mirages(IID_ResourceSupply)) && (!cmpSupply || !cmpSupply.AddGatherer(cmpOwnership.GetOwner(), this.entity)) || @@ -5587,7 +5618,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)) {