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 @@ -2066,16 +2066,16 @@ this.gatheringTarget = this.order.data.target; // temporary, deleted in "leave". // check that we can gather from the resource we're supposed to gather from. - var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); - var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); - var cmpMirage = Engine.QueryInterface(this.gatheringTarget, IID_Mirage); + 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))) + (!cmpSupply || !cmpOwnership || !cmpSupply.AddGatherer(cmpOwnership.GetOwner(), this.entity))) { // Save the current order's data in case we need it later - var oldType = this.order.data.type; - var oldTarget = this.order.data.target; - var oldTemplate = this.order.data.template; + let oldType = this.order.data.type; + let oldTarget = this.order.data.target; + let oldTemplate = this.order.data.template; // Try the next queued order if there is any if (this.FinishOrder()) @@ -2083,7 +2083,7 @@ // Try to find another nearby target of the same specific type // Also don't switch to a different type of huntable animal - var nearby = this.FindNearbyResource(function(ent, type, template) { + let nearbyResource = this.FindNearbyResource(function(ent, type, template) { return ( ent != oldTarget && ((type.generic == "treasure" && oldType.generic == "treasure") @@ -2091,32 +2091,26 @@ && (type.specific != "meat" || oldTemplate == template))) ); }, oldTarget); - if (nearby) - { - this.PerformGather(nearby, false, false); - return true; - } + + if (nearbyResource) + this.PerformGather(nearbyResource, false, false); else { // It's probably better in this case, to avoid units getting stuck around a dropsite // in a "Target is far away, full, nearby are no good resources, return to dropsite" loop // to order it to GatherNear the resource position. - var cmpPosition = Engine.QueryInterface(oldTarget, IID_Position); + let cmpPosition = Engine.QueryInterface(oldTarget, IID_Position); if (cmpPosition) { - var pos = cmpPosition.GetPosition(); + let pos = cmpPosition.GetPosition(); this.GatherNearPosition(pos.x, pos.z, oldType, oldTemplate); - return true; } else { // we're kind of stuck here. Return resource. - var nearby = this.FindNearestDropsite(oldType.generic); - if (nearby) - { - this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); - return true; - } + let nearestDropsite = this.FindNearestDropsite(oldType.generic); + if (nearestDropsite) + this.PushOrderFront("ReturnResource", { "target": nearestDropsite, "force": false }); } } return true; @@ -2130,8 +2124,6 @@ } this.SelectAnimation("move"); - - return false; }, "MoveCompleted": function(msg) { @@ -2140,25 +2132,25 @@ // We failed to reach the target // remove us from the list of entities gathering from Resource. - var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); - var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); + let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); + let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); if (cmpSupply && cmpOwnership) cmpSupply.RemoveGatherer(this.entity, cmpOwnership.GetOwner()); else if (cmpSupply) cmpSupply.RemoveGatherer(this.entity); // Save the current order's data in case we need it later - var oldType = this.order.data.type; - var oldTarget = this.order.data.target; - var oldTemplate = this.order.data.template; + let oldType = this.order.data.type; + let oldTarget = this.order.data.target; + let oldTemplate = this.order.data.template; // Try the next queued order if there is any if (this.FinishOrder()) - return; + return false; // Try to find another nearby target of the same specific type // Also don't switch to a different type of huntable animal - var nearby = this.FindNearbyResource(function(ent, type, template) { + let nearbyResource = this.FindNearbyResource(function(ent, type, template) { return ( ent != oldTarget && ((type.generic == "treasure" && oldType.generic == "treasure") @@ -2166,16 +2158,16 @@ && (type.specific != "meat" || oldTemplate == template))) ); }); - if (nearby) + if (nearbyResource) { - this.PerformGather(nearby, false, false); - return; + this.PerformGather(nearbyResource, false, false); + return false; } // Couldn't find anything else. Just try this one again, // maybe we'll succeed next time this.PerformGather(oldTarget, false, false); - return; + return false; } // We reached the target - start gathering from it now @@ -2187,10 +2179,10 @@ this.SetDefaultAnimationVariant(); if (!this.gatheringTarget) - return; - // don't use ownership because this is called after a conversion/resignation + return false; + // Don't use ownership because this is called after a conversion/resignation // and the ownership would be invalid then. - var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); + let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); if (cmpSupply) cmpSupply.RemoveGatherer(this.entity); delete this.gatheringTarget; @@ -2203,7 +2195,7 @@ if (!this.MoveTo(this.order.data)) { this.FinishOrder(); - return; + return true; } this.SelectAnimation("move"); }, @@ -2213,12 +2205,12 @@ }, "MoveCompleted": function(msg) { - var resourceType = this.order.data.type; - var resourceTemplate = this.order.data.template; + let resourceType = this.order.data.type; + let resourceTemplate = this.order.data.template; // Try to find another nearby target of the same specific type // Also don't switch to a different type of huntable animal - var nearby = this.FindNearbyResource(function(ent, type, template) { + let nearbyResource = this.FindNearbyResource(function(ent, type, template) { return ( (type.generic == "treasure" && resourceType.generic == "treasure") || (type.specific == resourceType.specific @@ -2227,22 +2219,22 @@ }); // If there is a nearby resource start gathering - if (nearby) + if (nearbyResource) { - this.PerformGather(nearby, false, false); - return; + this.PerformGather(nearbyResource, false, false); + return false; } // Couldn't find nearby resources, so give up if (this.FinishOrder()) - return; + return false; // Nothing better to do: go back to dropsite - var nearby = this.FindNearestDropsite(resourceType.generic); - if (nearby) + let nearestDropsite = this.FindNearestDropsite(resourceType.generic); + if (nearestDropsite) { - this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); - return; + this.PushOrderFront("ReturnResource", { "target": nearestDropsite, "force": false }); + return false; } // No dropsites, just give up @@ -2258,9 +2250,9 @@ { // 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 cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); - var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); - if (!cmpSupply || !cmpSupply.AddGatherer(cmpOwnership.GetOwner(), this.entity)) + let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); + let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); + if (!cmpSupply || !cmpOwnership || !cmpSupply.AddGatherer(cmpOwnership.GetOwner(), this.entity)) { this.gatheringTarget = INVALID_ENTITY; this.StartTimer(0); @@ -2275,8 +2267,8 @@ // Calculate timing based on gather rates // This allows the gather rate to control how often we gather, instead of how much. - var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); - var rate = cmpResourceGatherer.GetTargetGatherRate(this.gatheringTarget); + let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); + let rate = cmpResourceGatherer.GetTargetGatherRate(this.gatheringTarget); if (!rate) { @@ -2295,9 +2287,8 @@ // Scale timing interval based on rate, and start timer // The offset should be at least as long as the repeat time so we use the same value for both. - var offset = 1000/rate; - var repeat = offset; - this.StartTimer(offset, repeat); + let offset = 1000 / rate; + this.StartTimer(offset, offset); // We want to start the gather animation as soon as possible, // but only if we're actually at the target and it's still alive @@ -2308,7 +2299,7 @@ { this.StopMoving(); this.SetDefaultAnimationVariant(); - var typename = "gather_" + this.order.data.type.specific; + let typename = "gather_" + this.order.data.type.specific; this.SelectAnimation(typename); } return false; @@ -2317,9 +2308,9 @@ "leave": function() { this.StopTimer(); - // don't use ownership because this is called after a conversion/resignation + // Don't use ownership because this is called after a conversion/resignation // and the ownership would be invalid then. - var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); + let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); if (cmpSupply) cmpSupply.RemoveGatherer(this.entity); delete this.gatheringTarget; @@ -2329,26 +2320,25 @@ }, "Timer": function(msg) { - var resourceTemplate = this.order.data.template; - var resourceType = this.order.data.type; - - var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); + let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); if (!cmpOwnership) - return; + return false; + + let resourceTemplate = this.order.data.template; + let resourceType = this.order.data.type; - var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); + let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); if (cmpSupply && cmpSupply.IsAvailable(cmpOwnership.GetOwner(), this.entity)) { // Check we can still reach and gather from the target - if (this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer) && this.CanGather(this.gatheringTarget)) + if (this.CanGather(this.gatheringTarget) && this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer)) { // Gather the resources: - - var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); + let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); // Try to gather treasure if (cmpResourceGatherer.TryInstantGather(this.gatheringTarget)) - return; + return false; // 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 @@ -2356,29 +2346,29 @@ cmpResourceGatherer.DropResources(); // Collect from the target - var status = cmpResourceGatherer.PerformGather(this.gatheringTarget); + let status = cmpResourceGatherer.PerformGather(this.gatheringTarget); // If we've collected as many resources as possible, // return to the nearest dropsite if (status.filled) { - var nearby = this.FindNearestDropsite(resourceType.generic); - if (nearby) + let nearestDropsite = this.FindNearestDropsite(resourceType.generic); + if (nearestDropsite) { // (Keep this Gather order on the stack so we'll // continue gathering after returning) - this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); - return; + this.PushOrderFront("ReturnResource", { "target": nearestDropsite, "force": false }); + return false; } // Oh no, couldn't find any drop sites. Give up on gathering. this.FinishOrder(); - return; + return false; } // We can gather more from this target, do so in the next timer if (!status.exhausted) - return; + return false; } else { @@ -2386,7 +2376,7 @@ if (this.MoveToTargetRange(this.gatheringTarget, IID_ResourceGatherer)) { this.SetNextState("APPROACHING"); - return; + return false; } // Can't reach the target, or it doesn't exist any more @@ -2395,67 +2385,66 @@ // 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.MoveToPointRange(this.order.data.lastPos.x, this.order.data.lastPos.z, 0, 8)) { this.SetNextState("APPROACHING"); - return; + return false; } } } // We're already in range, can't get anywhere near it or the target is exhausted. - var herdPos = this.order.data.initPos; + let herdPos = this.order.data.initPos; // Give up on this order and try our next queued order // but first check what is our next order and, if needed, insert a returnResource order - var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); + let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); if (cmpResourceGatherer.IsCarrying(resourceType.generic) && this.orderQueue.length > 1 && this.orderQueue[1] !== "ReturnResource" && (this.orderQueue[1].type !== "Gather" || this.orderQueue[1].data.type.generic !== resourceType.generic)) { - let nearby = this.FindNearestDropsite(resourceType.generic); - if (nearby) - this.orderQueue.splice(1, 0, { "type": "ReturnResource", "data": { "target": nearby, "force": false } }); + let nearestDropsite = this.FindNearestDropsite(resourceType.generic); + if (nearestDropsite) + this.orderQueue.splice(1, 0, { "type": "ReturnResource", "data": { "target": nearestDropsite, "force": false } }); } if (this.FinishOrder()) - return; + return false; // No remaining orders - pick a useful default behaviour // Try to find a new resource of the same specific type near our current position: // Also don't switch to a different type of huntable animal - var nearby = this.FindNearbyResource(function(ent, type, template) { + let nearbyResource = this.FindNearbyResource(function(ent, type, template) { return ( (type.generic == "treasure" && resourceType.generic == "treasure") || (type.specific == resourceType.specific && (type.specific != "meat" || resourceTemplate == template)) ); }); - if (nearby) + if (nearbyResource) { - this.PerformGather(nearby, false, false); - return; + this.PerformGather(nearbyResource, false, false); + return false; } // If hunting, try to go to the initial herd position to see if we are more lucky if (herdPos) { this.GatherNearPosition(herdPos.x, herdPos.z, resourceType, resourceTemplate); - return; + return false; } // Nothing else to gather - if we're carrying anything then we should // drop it off, and if not then we might as well head to the dropsite // anyway because that's a nice enough place to congregate and idle - var nearby = this.FindNearestDropsite(resourceType.generic); - if (nearby) + let nearestDropsite = this.FindNearestDropsite(resourceType.generic); + if (nearestDropsite) { - this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); - return; + this.PushOrderFront("ReturnResource", { "target": nearestDropsite, "force": false }); + return false; } // No dropsites - just give up @@ -5138,8 +5127,8 @@ // Save the resource type now, so if the resource gets destroyed // before we process the order then we still know what resource // type to look for more of - var type; - var cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply); + let type; + let cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply); if (cmpResourceSupply) type = cmpResourceSupply.GetType(); else @@ -5147,8 +5136,8 @@ // Also save the target entity's template, so that if it's an animal, // we won't go from hunting slow safe animals to dangerous fast ones - var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); - var template = cmpTemplateManager.GetCurrentTemplateName(target); + let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); + let template = cmpTemplateManager.GetCurrentTemplateName(target); // Remove "resource|" prefix from template name, if present. if (template.indexOf("resource|") != -1) @@ -5156,8 +5145,8 @@ // Remember the position of our target, if any, in case it disappears // later and we want to head to its last known position - var lastPos = undefined; - var cmpPosition = Engine.QueryInterface(target, IID_Position); + let lastPos = undefined; + let cmpPosition = Engine.QueryInterface(target, IID_Position); if (cmpPosition && cmpPosition.IsInWorld()) lastPos = cmpPosition.GetPosition(); @@ -5751,7 +5740,7 @@ if (this.IsTurret()) return false; // The target must be a valid resource supply, or the mirage of one. - var cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply); + let cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply); if (!cmpResourceSupply) return false; @@ -5761,7 +5750,7 @@ return true; // Verify that we're able to respond to Gather commands - var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); + let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); if (!cmpResourceGatherer) return false;