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,9 +2091,9 @@ && (type.specific != "meat" || oldTemplate == template))) ); }, oldTarget); - if (nearby) + if (nearbyResource) { - this.PerformGather(nearby, false, false); + this.PerformGather(nearbyResource, false, false); return true; } else @@ -2101,20 +2101,20 @@ // 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) + let nearestDropsite = this.FindNearestDropsite(oldType.generic); + if (nearestDropsite) { - this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); + this.PushOrderFront("ReturnResource", { "target": nearestDropsite, "force": false }); return true; } } @@ -2140,17 +2140,17 @@ // 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()) @@ -2158,7 +2158,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") @@ -2166,9 +2166,9 @@ && (type.specific != "meat" || oldTemplate == template))) ); }); - if (nearby) + if (nearbyResource) { - this.PerformGather(nearby, false, false); + this.PerformGather(nearbyResource, false, false); return; } @@ -2190,7 +2190,7 @@ return; // 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; @@ -2213,12 +2213,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,9 +2227,9 @@ }); // If there is a nearby resource start gathering - if (nearby) + if (nearbyResource) { - this.PerformGather(nearby, false, false); + this.PerformGather(nearbyResource, false, false); return; } @@ -2238,10 +2238,10 @@ return; // 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 }); + this.PushOrderFront("ReturnResource", { "target": nearestDropsite, "force": false }); return; } @@ -2258,9 +2258,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 +2275,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 +2295,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 +2307,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 +2316,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,22 +2328,21 @@ }, "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; - var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); + let resourceTemplate = this.order.data.template; + let resourceType = this.order.data.type; + + 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)) @@ -2356,18 +2354,18 @@ 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 }); + this.PushOrderFront("ReturnResource", { "target": nearestDropsite, "force": false }); return; } @@ -2395,9 +2393,8 @@ // 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; @@ -2407,18 +2404,18 @@ // 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; @@ -2427,16 +2424,16 @@ // 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); + this.PerformGather(nearbyResource, false, false); return; } @@ -2451,10 +2448,10 @@ // 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 }); + this.PushOrderFront("ReturnResource", { "target": nearestDropsite, "force": false }); return; } @@ -5138,8 +5135,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 +5144,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 +5153,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 +5748,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 +5758,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;