diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js index f25e4691e0..54ea5fbd40 100644 --- a/binaries/data/mods/public/simulation/components/UnitAI.js +++ b/binaries/data/mods/public/simulation/components/UnitAI.js @@ -1512,7 +1512,7 @@ var UnitFsmSpec = { "APPROACHING": { "enter": function() { this.SelectAnimation("move"); - + this.gatheringTarget = this.order.data.target; // temporary, deleted in "leave". // check that we can gather from the resource we're supposed to gather from. @@ -1657,20 +1657,21 @@ var UnitFsmSpec = { "GATHERING": { "enter": function() { this.gatheringTarget = this.order.data.target; // deleted in "leave". - + // Check if the resource is full. - if (this.gatheringTarget) { + if (this.gatheringTarget) + { // Check that we can gather from the resource we're supposed to gather from. // Will only be added if we're not already in. var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); if (!cmpSupply || !cmpSupply.AddGatherer(this.entity)) { - this.gatheringTarget = -1; + this.gatheringTarget = INVALID_ENTITY; this.StartTimer(0); return false; } } - + // If this order was forced, the player probably gave it, but now we've reached the target // switch to an unforced order (can be interrupted by attacks) this.order.data.force = false; @@ -1690,7 +1691,7 @@ var UnitFsmSpec = { this.StartTimer(0); return false; } - + // No rate, give up on gathering this.FinishOrder(); return true; @@ -1717,13 +1718,12 @@ var UnitFsmSpec = { "leave": function() { this.StopTimer(); - + var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); if (cmpSupply) cmpSupply.RemoveGatherer(this.entity); - this.gatheringTarget = -1; delete this.gatheringTarget; - + // Show the carried resource, if we've gathered anything. this.SetGathererAnimationOverride(); }, @@ -1731,72 +1731,75 @@ var UnitFsmSpec = { "Timer": function(msg) { var resourceTemplate = this.order.data.template; var resourceType = this.order.data.type; - + var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); - - // Check we can still reach and gather from the target - if (cmpSupply && this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer) && this.CanGather(this.gatheringTarget) && cmpSupply.IsAvailable(this.entity)) + + if (cmpSupply && cmpSupply.IsAvailable(this.entity)) { - // Gather the resources: - - var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); - - // Try to gather treasure - if (cmpResourceGatherer.TryInstantGather(this.gatheringTarget)) - return; - - // If we've already got some resources but they're the wrong type, - // drop them first to ensure we're only ever carrying one type - if (cmpResourceGatherer.IsCarryingAnythingExcept(resourceType.generic)) - cmpResourceGatherer.DropResources(); - - // Collect from the target - var status = cmpResourceGatherer.PerformGather(this.gatheringTarget); - - // If we've collected as many resources as possible, - // return to the nearest dropsite - if (status.filled) + // Check we can still reach and gather from the target + if (this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer) && this.CanGather(this.gatheringTarget)) { - var nearby = this.FindNearestDropsite(resourceType.generic); - if (nearby) + // Gather the resources: + + var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); + + // Try to gather treasure + if (cmpResourceGatherer.TryInstantGather(this.gatheringTarget)) + return; + + // If we've already got some resources but they're the wrong type, + // drop them first to ensure we're only ever carrying one type + if (cmpResourceGatherer.IsCarryingAnythingExcept(resourceType.generic)) + cmpResourceGatherer.DropResources(); + + // Collect from the target + var status = cmpResourceGatherer.PerformGather(this.gatheringTarget); + + // If we've collected as many resources as possible, + // return to the nearest dropsite + if (status.filled) { - // (Keep this Gather order on the stack so we'll - // continue gathering after returning) - this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); + var nearby = this.FindNearestDropsite(resourceType.generic); + if (nearby) + { + // (Keep this Gather order on the stack so we'll + // continue gathering after returning) + this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); + return; + } + + // Oh no, couldn't find any drop sites. Give up on gathering. + this.FinishOrder(); return; } - // Oh no, couldn't find any drop sites. Give up on gathering. - this.FinishOrder(); - return; + // We can gather more from this target, do so in the next timer + if (!status.exhausted) + return; } - - // We can gather more from this target, do so in the next timer - if (!status.exhausted) - return; - } - else if (cmpSupply && cmpSupply.IsAvailable(this.entity)) - { - // Try to follow the target - if (this.MoveToTargetRange(this.gatheringTarget, IID_ResourceGatherer)) + else { - this.SetNextState("APPROACHING"); - return; - } + // Try to follow the target + if (this.MoveToTargetRange(this.gatheringTarget, IID_ResourceGatherer)) + { + this.SetNextState("APPROACHING"); + return; + } - // Can't reach the target, or it doesn't exist any more + // Can't reach the target, or it doesn't exist any more - // We want to carry on gathering resources in the same area as - // the old one. So try to get close to the old resource's - // last known position + // We want to carry on gathering resources in the same area as + // the old one. So try to get close to the old resource's + // last known position - var maxRange = 8; // get close but not too close - if (this.order.data.lastPos && - this.MoveToPointRange(this.order.data.lastPos.x, this.order.data.lastPos.z, - 0, maxRange)) - { - this.SetNextState("APPROACHING"); - return; + var maxRange = 8; // get close but not too close + if (this.order.data.lastPos && + this.MoveToPointRange(this.order.data.lastPos.x, this.order.data.lastPos.z, + 0, maxRange)) + { + this.SetNextState("APPROACHING"); + return; + } } }