Index: binaries/data/mods/public/simulation/components/UnitAI.js
===================================================================
--- binaries/data/mods/public/simulation/components/UnitAI.js	(revision 19061)
+++ binaries/data/mods/public/simulation/components/UnitAI.js	(working copy)
@@ -356,8 +356,9 @@
 		// Check if we need to move     TODO implement a better way to know if we are on the shoreline
 		var needToMove = true;
 		var cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
-		if (this.lastShorelinePosition && cmpPosition && (this.lastShorelinePosition.x == cmpPosition.GetPosition().x)
-		    && (this.lastShorelinePosition.z == cmpPosition.GetPosition().z))
+		if (this.lastShorelinePosition && cmpPosition &&
+			this.lastShorelinePosition.x == cmpPosition.GetPosition().x && 
+			this.lastShorelinePosition.z == cmpPosition.GetPosition().z)
 		{
 			// we were already on the shoreline, and have not moved since
 			if (DistanceBetweenEntities(this.entity, this.order.data.target) < 50)
@@ -1454,11 +1455,11 @@
 
 		"GuardedAttacked": function(msg) {
 			// do nothing if we have a forced order in queue before the guard order
-			for (var i = 0; i < this.orderQueue.length; ++i)
+			for(let order of this.orderQueue)
 			{
-				if (this.orderQueue[i].type == "Guard")
+				if (order.type == "Guard")
 					break;
-				if (this.orderQueue[i].data && this.orderQueue[i].data.force)
+				if (order.data && order.data.force)
 					return;
 			}
 			// if we already are targeting another unit still alive, finish with it first
@@ -1946,7 +1947,7 @@
 					var animationName = "attack_" + this.order.data.attackType.toLowerCase();
 					if (this.IsFormationMember())
 					{
-						var cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation);
+						let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation);
 						if (cmpFormation)
 							animationName = cmpFormation.GetFormationAnimation(this.entity, animationName);
 					}
@@ -2169,14 +2170,12 @@
 
 						// 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) {
-							return (
-								ent != oldTarget
-								 && ((type.generic == "treasure" && oldType.generic == "treasure")
-								 || (type.specific == oldType.specific
-								 && (type.specific != "meat" || oldTemplate == template)))
-							);
-						}, oldTarget);
+						let nearby = this.FindNearbyResource((ent, type, template) =>
+								ent != oldTarget &&
+								(type.generic == "treasure" && oldType.generic == "treasure" ||
+								(type.specific == oldType.specific &&
+								(type.specific != "meat" || oldTemplate == template))),
+								oldTarget);
 						if (nearby)
 						{
 							this.PerformGather(nearby, false, false);
@@ -2197,7 +2196,7 @@
 							else
 							{
 								// we're kind of stuck here. Return resource.
-								var nearby = this.FindNearestDropsite(oldType.generic);
+								nearby = this.FindNearestDropsite(oldType.generic);
 								if (nearby)
 								{
 									this.PushOrderFront("ReturnResource", { "target": nearby, "force": false });
@@ -2234,14 +2233,11 @@
 
 						// 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) {
-							return (
-								ent != oldTarget
-								&& ((type.generic == "treasure" && oldType.generic == "treasure")
-								|| (type.specific == oldType.specific
-								&& (type.specific != "meat" || oldTemplate == template)))
-							);
-						});
+						let nearby = this.FindNearbyResource((ent, type, template) =>
+							ent != oldTarget &&
+							(type.generic == "treasure" && oldType.generic == "treasure" ||
+							(type.specific == oldType.specific &&
+							(type.specific != "meat" || oldTemplate == template))));
 						if (nearby)
 						{
 							this.PerformGather(nearby, false, false);
@@ -2280,13 +2276,10 @@
 
 					// 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) {
-						return (
-							(type.generic == "treasure" && resourceType.generic == "treasure")
-							|| (type.specific == resourceType.specific
-							&& (type.specific != "meat" || resourceTemplate == template))
-						);
-					});
+					let nearby = this.FindNearbyResource((ent, type, template) =>
+						(type.generic == "treasure" && resourceType.generic == "treasure") ||
+						(type.specific == resourceType.specific &&
+						(type.specific != "meat" || resourceTemplate == template)));
 
 					// If there is a nearby resource start gathering
 					if (nearby)
@@ -2300,7 +2293,7 @@
 						return;
 
 					// Nothing better to do: go back to dropsite
-					var nearby = this.FindNearestDropsite(resourceType.generic);
+					nearby = this.FindNearestDropsite(resourceType.generic);
 					if (nearby)
 					{
 						this.PushOrderFront("ReturnResource", { "target": nearby, "force": false });
@@ -2403,9 +2396,8 @@
 						if (this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer) && this.CanGather(this.gatheringTarget))
 						{
 							// Gather the resources:
+							let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
 
-							var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
-
 							// Try to gather treasure
 							if (cmpResourceGatherer.TryInstantGather(this.gatheringTarget))
 								return;
@@ -2422,7 +2414,7 @@
 							// return to the nearest dropsite
 							if (status.filled)
 							{
-								var nearby = this.FindNearestDropsite(resourceType.generic);
+								let nearby = this.FindNearestDropsite(resourceType.generic);
 								if (nearby)
 								{
 									// (Keep this Gather order on the stack so we'll
@@ -2472,7 +2464,7 @@
 
 					// 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))
@@ -2488,13 +2480,10 @@
 
 					// 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) {
-						return (
-							(type.generic == "treasure" && resourceType.generic == "treasure")
-							|| (type.specific == resourceType.specific
-							&& (type.specific != "meat" || resourceTemplate == template))
-						);
-					});
+					let nearby = this.FindNearbyResource((ent, type, template) =>
+						(type.generic == "treasure" && resourceType.generic == "treasure") ||
+						(type.specific == resourceType.specific &&
+						(type.specific != "meat" || resourceTemplate == template)));
 					if (nearby)
 					{
 						this.PerformGather(nearby, false, false);
@@ -2512,7 +2501,7 @@
 					// 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);
+					nearby = this.FindNearestDropsite(resourceType.generic);
 					if (nearby)
 					{
 						this.PushOrderFront("ReturnResource", { "target": nearby, "force": false });
@@ -2680,7 +2669,7 @@
 							// Dump any resources we can
 							var dropsiteTypes = cmpResourceDropsite.GetTypes();
 
-							var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
+							let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
 							cmpResourceGatherer.CommitResources(dropsiteTypes);
 
 							// Stop showing the carried resource animation.
@@ -2696,7 +2685,7 @@
 					// The dropsite was destroyed, or we couldn't reach it, or ownership changed
 					// Look for a new one.
 
-					var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
+					let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
 					var genericType = cmpResourceGatherer.GetMainCarryingType();
 					var nearby = this.FindNearestDropsite(genericType);
 					if (nearby)
@@ -2891,13 +2880,11 @@
 				// the build command should look for nearby resources to gather
 				if ((oldData.force || oldData.autoharvest) && this.CanReturnResource(msg.data.newentity, false))
 				{
-					var cmpResourceDropsite = Engine.QueryInterface(msg.data.newentity, IID_ResourceDropsite);
-					var types = cmpResourceDropsite.GetTypes();
+					cmpResourceDropsite = Engine.QueryInterface(msg.data.newentity, IID_ResourceDropsite);
+					let types = cmpResourceDropsite.GetTypes();
 					// TODO: Slightly undefined behavior here, we don't know what type of resource will be collected,
 					//   may cause problems for AIs (especially hunting fast animals), but avoid ugly hacks to fix that!
-					var nearby = this.FindNearbyResource(function (ent, type, template) {
-						return (types.indexOf(type.generic) != -1);
-					});
+					let nearby = this.FindNearbyResource((ent, type, template) => types.indexOf(type.generic) != -1);
 					if (nearby)
 					{
 						this.PerformGather(nearby, true, false);
@@ -2976,9 +2963,8 @@
 			"GARRISONED": {
 				"enter": function() {
 					// Target is not handled the same way with Alert and direct garrisoning
-					if (this.order.data.target)
-						var target = this.order.data.target;
-					else
+					let target = this.order.data.target;
+					if (!target)
 					{
 						if (!this.alertGarrisoningTarget)
 						{
@@ -2986,7 +2972,7 @@
 							this.FinishOrder();
 							return true;
 						}
-						var target = this.alertGarrisoningTarget;
+						target = this.alertGarrisoningTarget;
 					}
 
 					// Check that we can garrison here
@@ -3358,7 +3344,7 @@
 
 UnitAI.prototype.IsUnderAlert = function()
 {
-	return this.alertRaiser != undefined;
+	return this.alertRaiser !== undefined;
 };
 
 UnitAI.prototype.ResetAlert = function()
@@ -3488,8 +3474,8 @@
 	{
 		// Switch to a virgin state to let states execute their leave handlers.
 		// except if garrisoned or cheering or (un)packing, in which case we only clear the order queue
-		if (this.isGarrisoned || (this.orderQueue[0] && (this.orderQueue[0].type == "Cheering"
-			|| this.orderQueue[0].type == "Pack" || this.orderQueue[0].type == "Unpack")))
+		if (this.isGarrisoned || (this.orderQueue[0] && (this.orderQueue[0].type == "Cheering" ||
+			this.orderQueue[0].type == "Pack" || this.orderQueue[0].type == "Unpack")))
 		{
 			this.orderQueue.length = Math.min(this.orderQueue.length, 1);
 			Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() });
@@ -3553,7 +3539,7 @@
 	{
 		if (this.orderQueue[i].type != "PickupUnit" || this.orderQueue[i].data.target != msg.entity)
 			continue;
-		if (i == 0)
+		if (i === 0)
 			this.UnitFsm.ProcessMessage(this, {"type": "PickupCanceled", "data": msg});
 		else
 			this.orderQueue.splice(i, 1);
@@ -3838,13 +3824,13 @@
 	// TODO: maybe a better way of doing this would be to use priority levels
 	if (this.order && this.order.type == "Cheering")
 	{
-		var order = { "type": type, "data": data };
+		let order = { "type": type, "data": data };
 		var cheeringOrder = this.orderQueue.shift();
 		this.orderQueue = [cheeringOrder, order];
 	}
 	else if (this.IsPacking() && type != "CancelPack" && type != "CancelUnpack")
 	{
-		var order = { "type": type, "data": data };
+		let order = { "type": type, "data": data };
 		var packingOrder = this.orderQueue.shift();
 		this.orderQueue = [packingOrder, order];
 	}
@@ -3901,7 +3887,7 @@
 		var cmpUnitAI = Engine.QueryInterface(this.formationController, IID_UnitAI);
 		if (cmpUnitAI)
 		{
-			for (var i = 0; i < cmpUnitAI.orderQueue.length; ++i)
+			for (let i = 0; i < cmpUnitAI.orderQueue.length; ++i)
 			{
 				if (isWorkType(cmpUnitAI.orderQueue[i].type))
 				{
@@ -3913,7 +3899,7 @@
 	}
 
 	// If nothing found, take the unit orders
-	for (var i = 0; i < this.orderQueue.length; ++i)
+	for (let i = 0; i < this.orderQueue.length; ++i)
 	{
 		if (isWorkType(this.orderQueue[i].type))
 		{
@@ -3925,7 +3911,7 @@
 
 UnitAI.prototype.BackToWork = function()
 {
-	if (this.workOrders.length == 0)
+	if (!this.workOrders.length)
 		return false;
 
 	// Clear the order queue considering special orders not to avoid
@@ -4104,7 +4090,7 @@
 		return true;
 
 	var cmpHealth = QueryMiragedInterface(ent, IID_Health);
-	return cmpHealth && cmpHealth.GetHitpoints() != 0;
+	return cmpHealth && cmpHealth.GetHitpoints() !== 0;
 };
 
 /**
@@ -4437,17 +4423,13 @@
 	var t = targetCmpPosition.GetPosition();
 	// h is positive when I'm higher than the target
 	var h = s.y-t.y+range.elevationBonus;
-
+	let parabolicMaxRange = 0;
 	// No negative roots please
-	if (h>-range.max/2)
-		var parabolicMaxRange = Math.sqrt(range.max*range.max+2*range.max*h);
-	else
-		// return false? Or hope you come close enough?
-		var parabolicMaxRange = 0;
-		//return false;
+	if (h > -range.max / 2)
+		parabolicMaxRange = Math.sqrt(range.max * range.max + 2 * range.max * h);
 
 	// the parabole changes while walking, take something in the middle
-	var guessedMaxRange = (range.max + parabolicMaxRange)/2;
+	var guessedMaxRange = (range.max + parabolicMaxRange) / 2;
 
 	var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
 	if (cmpUnitMotion.MoveToTargetRange(target, range.min, guessedMaxRange))
@@ -4509,8 +4491,8 @@
 	if (this.IsFormationMember())
 	{
 		var cmpFormationUnitAI = Engine.QueryInterface(this.formationController, IID_UnitAI);
-		if (cmpFormationUnitAI && cmpFormationUnitAI.IsAttackingAsFormation()
-			&& cmpFormationUnitAI.order.data.target == target)
+		if (cmpFormationUnitAI && cmpFormationUnitAI.IsAttackingAsFormation() && 
+			cmpFormationUnitAI.order.data.target == target)
 			return true;
 	}
 
@@ -4689,9 +4671,9 @@
 UnitAI.prototype.AttackEntityInZone = function(ents, forceResponse)
 {
 	var target = ents.find(target =>
-		this.CanAttack(target, forceResponse)
-		&& this.CheckTargetDistanceFromHeldPosition(target, IID_Attack, this.GetBestAttackAgainst(target, true))
-		&& (this.GetStance().respondChaseBeyondVision || this.CheckTargetIsInVisionRange(target))
+		this.CanAttack(target, forceResponse) && 
+		this.CheckTargetDistanceFromHeldPosition(target, IID_Attack, this.GetBestAttackAgainst(target, true)) &&
+		(this.GetStance().respondChaseBeyondVision || this.CheckTargetIsInVisionRange(target))
 	);
 	if (!target)
 		return false;
@@ -5204,7 +5186,7 @@
 
 	// 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 lastPos;
 	var cmpPosition = Engine.QueryInterface(target, IID_Position);
 	if (cmpPosition && cmpPosition.IsInWorld())
 		lastPos = cmpPosition.GetPosition();
@@ -5527,30 +5509,28 @@
 	{
 		var cmpUnitAI;
 		var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
-		for each (var ent in cmpFormation.members)
+		for (let ent of cmpFormation.members)
 		{
 			if (!(cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI)))
 				continue;
-			var targets = cmpUnitAI.GetTargetsFromUnit();
-			for (var targ of targets)
+			let targets = cmpUnitAI.GetTargetsFromUnit();
+			for (let target of targets)
 			{
-				if (!cmpUnitAI.CanAttack(targ))
+				if (!cmpUnitAI.CanAttack(target))
 					continue;
 				if (this.order.data.targetClasses)
 				{
-					var cmpIdentity = Engine.QueryInterface(targ, IID_Identity);
-					var targetClasses = this.order.data.targetClasses;
-					if (targetClasses.attack && cmpIdentity
-						&& !MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.attack))
+					let cmpIdentity = Engine.QueryInterface(target, IID_Identity);
+					let targetClasses = this.order.data.targetClasses;
+					if (targetClasses.attack && cmpIdentity && !MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.attack))
 						continue;
-					if (targetClasses.avoid && cmpIdentity
-						&& MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.avoid))
+					if (targetClasses.avoid && cmpIdentity && MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.avoid))
 						continue;
 					// Only used by the AIs to prevent some choices of targets
-					if (targetClasses.vetoEntities && targetClasses.vetoEntities[targ])
+					if (targetClasses.vetoEntities && targetClasses.vetoEntities[target])
 						continue;
 				}
-				this.PushOrderFront("Attack", { "target": targ, "force": true, "allowCapture": true });
+				this.PushOrderFront("Attack", { "target": target, "force": true, "allowCapture": true });
 				return true;
 			}
 		}
@@ -5558,25 +5538,23 @@
 	}
 
 	var targets = this.GetTargetsFromUnit();
-	for (var targ of targets)
+	for (let target of targets)
 	{
-		if (!this.CanAttack(targ))
+		if (!this.CanAttack(target))
 			continue;
 		if (this.order.data.targetClasses)
 		{
-			var cmpIdentity = Engine.QueryInterface(targ, IID_Identity);
-			var targetClasses = this.order.data.targetClasses;
-			if (cmpIdentity && targetClasses.attack
-				&& !MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.attack))
+			let cmpIdentity = Engine.QueryInterface(target, IID_Identity);
+			let targetClasses = this.order.data.targetClasses;
+			if (cmpIdentity && targetClasses.attack && !MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.attack))
 				continue;
-			if (cmpIdentity && targetClasses.avoid
-				&& MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.avoid))
+			if (cmpIdentity && targetClasses.avoid && MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.avoid))
 				continue;
 			// Only used by the AIs to prevent some choices of targets
-			if (targetClasses.vetoEntities && targetClasses.vetoEntities[targ])
+			if (targetClasses.vetoEntities && targetClasses.vetoEntities[target])
 				continue;
 		}
-		this.PushOrderFront("Attack", { "target": targ, "force": true, "allowCapture": true });
+		this.PushOrderFront("Attack", { "target": target, "force": true, "allowCapture": true });
 		return true;
 	}
 
@@ -5633,28 +5611,27 @@
 	var ret = { "min": 0, "max": 0 };
 	if (this.GetStance().respondStandGround)
 	{
-		var cmpRanged = Engine.QueryInterface(this.entity, iid);
+		let cmpRanged = Engine.QueryInterface(this.entity, iid);
 		if (!cmpRanged)
 			return ret;
-		var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetFullAttackRange();
+		let range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetFullAttackRange();
 		ret.min = range.min;
 		ret.max = range.max;
 	}
 	else if (this.GetStance().respondChase)
 	{
-		var cmpVision = Engine.QueryInterface(this.entity, IID_Vision);
+		let cmpVision = Engine.QueryInterface(this.entity, IID_Vision);
 		if (!cmpVision)
 			return ret;
-		var range = cmpVision.GetRange();
-		ret.max = range;
+		ret.max = cmpVision.GetRange();
 	}
 	else if (this.GetStance().respondHoldGround)
 	{
-		var cmpRanged = Engine.QueryInterface(this.entity, iid);
+		let cmpRanged = Engine.QueryInterface(this.entity, iid);
 		if (!cmpRanged)
 			return ret;
-		var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetFullAttackRange();
-		var cmpVision = Engine.QueryInterface(this.entity, IID_Vision);
+		let range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetFullAttackRange();
+		let cmpVision = Engine.QueryInterface(this.entity, IID_Vision);
 		if (!cmpVision)
 			return ret;
 		var halfvision = cmpVision.GetRange() / 2;
@@ -5664,11 +5641,11 @@
 	// but as it is the default for healers we need to set it to something sane.
 	else if (iid === IID_Heal)
 	{
-		var cmpVision = Engine.QueryInterface(this.entity, IID_Vision);
+		let cmpVision = Engine.QueryInterface(this.entity, IID_Vision);
 		if (!cmpVision)
 			return ret;
-		var range = cmpVision.GetRange();
-		ret.max = range;
+		
+		ret.max = cmpVision.GetRange();
 	}
 	return ret;
 };
@@ -5965,7 +5942,7 @@
 UnitAI.prototype.IsAttackingAsFormation = function()
 {
 	var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
-	return cmpAttack && cmpAttack.CanAttackAsFormation()
+	return cmpAttack && cmpAttack.CanAttackAsFormation() 
 		&& this.GetCurrentState() == "FORMATIONCONTROLLER.COMBAT.ATTACKING";
 };
 
