Index: binaries/data/mods/public/simulation/components/UnitAI.js
===================================================================
--- binaries/data/mods/public/simulation/components/UnitAI.js	(revision 18527)
+++ 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)
@@ -1376,7 +1377,7 @@
 
 		"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 i = 0; i < this.orderQueue.length; ++i)
 			{
 				if (this.orderQueue[i].type == "Guard")
 					break;
@@ -1831,7 +1832,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);
 					}
@@ -2054,14 +2055,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);
@@ -2082,7 +2081,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 });
@@ -2119,14 +2118,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);
@@ -2165,13 +2161,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)
@@ -2185,7 +2178,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 });
@@ -2288,9 +2281,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;
@@ -2307,7 +2299,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
@@ -2357,7 +2349,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))
@@ -2373,13 +2365,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);
@@ -2397,7 +2386,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 });
@@ -2565,7 +2554,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.
@@ -2581,7 +2570,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)
@@ -2776,13 +2765,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);
@@ -2861,9 +2848,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)
 						{
@@ -2871,7 +2857,7 @@
 							this.FinishOrder();
 							return true;
 						}
-						var target = this.alertGarrisoningTarget;
+						target = this.alertGarrisoningTarget;
 					}
 
 					// Check that we can garrison here
@@ -3243,7 +3229,7 @@
 
 UnitAI.prototype.IsUnderAlert = function()
 {
-	return this.alertRaiser != undefined;
+	return this.alertRaiser !== undefined;
 };
 
 UnitAI.prototype.ResetAlert = function()
@@ -3367,8 +3353,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() });
@@ -3427,7 +3413,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);
@@ -3729,13 +3715,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];
 	}
@@ -3792,7 +3778,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))
 				{
@@ -3804,7 +3790,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))
 		{
@@ -3816,7 +3802,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
@@ -3998,7 +3984,7 @@
 		return true;
 
 	var cmpHealth = QueryMiragedInterface(ent, IID_Health);
-	return cmpHealth && cmpHealth.GetHitpoints() != 0;
+	return cmpHealth && cmpHealth.GetHitpoints() !== 0;
 };
 
 /**
@@ -4336,17 +4322,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))
@@ -4408,8 +4390,7 @@
 	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;
 	}
 
@@ -4588,9 +4569,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;
@@ -5098,7 +5079,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();
@@ -5225,20 +5206,14 @@
 
 UnitAI.prototype.MoveToMarket = function(targetMarket)
 {
-	if (this.waypoints && this.waypoints.length > 1)
-	{
-		var point = this.waypoints.pop();
-		var ok = this.MoveToPoint(point.x, point.z);
-		if (!ok)
-			ok = this.MoveToMarket(targetMarket);
-	}
-	else
-	{
-		this.waypoints = undefined;
-		var ok = this.MoveToTarget(targetMarket);
-	}
-
-	return ok;
+    if (this.waypoints && this.waypoints.length > 1)
+    {
+        var point = this.waypoints.pop();
+        return this.MoveToPoint(point.x, point.z) || this.MoveToMarket(targetMarket);
+    }
+   
+    this.waypoints = undefined;
+    return this.MoveToTarget(targetMarket);
 };
 
 UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket)
@@ -5427,30 +5402,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;
 			}
 		}
@@ -5458,25 +5431,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;
 	}
 
@@ -5533,28 +5504,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;
@@ -5564,11 +5534,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;
 };
@@ -5865,8 +5835,7 @@
 UnitAI.prototype.IsAttackingAsFormation = function()
 {
 	var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
-	return cmpAttack && cmpAttack.CanAttackAsFormation()
-		&& this.GetCurrentState() == "FORMATIONCONTROLLER.COMBAT.ATTACKING";
+	return cmpAttack && cmpAttack.CanAttackAsFormation() && this.GetCurrentState() == "FORMATIONCONTROLLER.COMBAT.ATTACKING";
 };
 
 //// Animal specific functions ////
