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 @@ -531,6 +531,33 @@ }, "Order.Gather": function(msg) { + if (!this.CanGather(this.order.data.target)) + { + this.SetNextState("INDIVIDUAL.GATHER.FINDINGNEWTARGET"); + return; + } + + // If the unit is full go to the nearest dropsite instead of trying to gather. + // Unless our target is a treasure which we cannot be full enough with (we can't carry treasures). + let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); + if (msg.data.type.generic !== "treasure" && cmpResourceGatherer && !cmpResourceGatherer.CanCarryMore(msg.data.type.generic)) + { + let nearestDropsite = this.FindNearestDropsite(msg.data.type.generic); + // Oh no, couldn't find any dropsites. Give up on gathering. + if (!nearestDropsite) + { + this.FinishOrder(); + return; + } + + this.PushOrderFront("ReturnResource", { + "target": nearestDropsite, + "force": false, + "type": msg.data.type + }); + return; + } + // If the target is still alive, we need to kill it first if (this.MustKillGatherTarget(this.order.data.target)) { @@ -2449,6 +2476,13 @@ // Collect from the target let status = cmpResourceGatherer.PerformGather(this.gatheringTarget); + // Find a new target if the current one is exhausted. + if (status.exhausted) + { + this.SetNextState("FINDINGNEWTARGET"); + return; + } + // If we've collected as many resources as possible, // return to the nearest dropsite if (status.filled) @@ -2458,10 +2492,6 @@ { // (Keep this Gather order on the stack so we'll // continue gathering after returning) - // However mark our target as invalid if it's exhausted, so we don't waste time - // trying to gather from it. - if (status.exhausted) - this.order.data.target = INVALID_ENTITY; this.PushOrderFront("ReturnResource", { "target": nearestDropsite, "force": false }); return; } @@ -2470,10 +2500,6 @@ this.FinishOrder(); return; } - - // Find a new target if the current one is exhausted - if (status.exhausted) - this.SetNextState("FINDINGNEWTARGET"); }, },