Index: binaries/data/mods/public/simulation/components/BuildRestrictions.js
===================================================================
--- binaries/data/mods/public/simulation/components/BuildRestrictions.js	(revision 18614)
+++ binaries/data/mods/public/simulation/components/BuildRestrictions.js	(working copy)
@@ -79,10 +79,10 @@
  */
 BuildRestrictions.prototype.CheckPlacement = function()
 {
-	var cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity);
-	var name = cmpIdentity ? cmpIdentity.GetGenericName() : "Building";
+	let cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity);
+	let name = cmpIdentity ? cmpIdentity.GetGenericName() : "Building";
 
-	var result = {
+	let result = {
 		"success": false,
 		"message": markForTranslation("%(name)s cannot be built due to unknown error"),
 		"parameters": {
@@ -93,16 +93,16 @@
 	};
 
 	// TODO: AI has no visibility info
-	var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
+	let cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
 	if (!cmpPlayer.IsAI())
 	{
 		// Check whether it's in a visible or fogged region
-		var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
-		var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
+		let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
+		let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
 		if (!cmpRangeManager || !cmpOwnership)
 			return result; // Fail
 
-		var explored = (cmpRangeManager.GetLosVisibility(this.entity, cmpOwnership.GetOwner()) != "hidden");
+		let explored = (cmpRangeManager.GetLosVisibility(this.entity, cmpOwnership.GetOwner()) != "hidden");
 		if (!explored)
 		{
 			result.message = markForTranslation("%(name)s cannot be built in unexplored area");
@@ -111,7 +111,7 @@
 	}
 
 	// Check obstructions and terrain passability
-	var passClassName = "";
+	let passClassName = "";
 	switch (this.template.PlacementType)
 	{
 	case "shore":
@@ -129,21 +129,12 @@
 		passClassName = "building-land";
 	}
 
-	var cmpObstruction = Engine.QueryInterface(this.entity, IID_Obstruction);
+	let cmpObstruction = Engine.QueryInterface(this.entity, IID_Obstruction);
 	if (!cmpObstruction)
 		return result; // Fail
 
+	let ret = cmpObstruction.CheckFoundation(passClassName, this.template.Category == "Wall")
 
-	if (this.template.Category == "Wall")
-	{
-		// for walls, only test the center point
-		var ret = cmpObstruction.CheckFoundation(passClassName, true);
-	}
-	else
-	{
-		var ret = cmpObstruction.CheckFoundation(passClassName, false);
-	}
-
 	if (ret != "success")
 	{
 		switch (ret)
@@ -163,21 +154,18 @@
 	}
 
 	// Check territory restrictions
-	var cmpTerritoryManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TerritoryManager);
-	var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
-	var cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
+	let cmpTerritoryManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TerritoryManager);
+	let cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
+	let cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
 	if (!(cmpTerritoryManager && cmpPlayer && cmpPosition && cmpPosition.IsInWorld()))
 		return result;	// Fail
 
-	var pos = cmpPosition.GetPosition2D();
-	var tileOwner = cmpTerritoryManager.GetOwner(pos.x, pos.y);
-	var isConnected = !cmpTerritoryManager.IsTerritoryBlinking(pos.x, pos.y);
-	var isOwn = tileOwner == cmpPlayer.GetPlayerID();
-	var isMutualAlly = cmpPlayer.IsExclusiveMutualAlly(tileOwner);
-	var isNeutral = tileOwner == 0;
+	let pos = cmpPosition.GetPosition2D();
+	let tileOwner = cmpTerritoryManager.GetOwner(pos.x, pos.y);
+	let isConnected = !cmpTerritoryManager.IsTerritoryBlinking(pos.x, pos.y);
 
-	var invalidTerritory = "";
-	if (isOwn)
+	let invalidTerritory = "";
+	if (tileOwner == cmpPlayer.GetPlayerID())
 	{
 		if (!this.HasTerritory("own"))
 			// Translation: territoryType being displayed in a translated sentence in the form: "House cannot be built in %(territoryType)s territory.".
@@ -186,7 +174,7 @@
 			// Translation: territoryType being displayed in a translated sentence in the form: "House cannot be built in %(territoryType)s territory.".
 			invalidTerritory = markForTranslationWithContext("Territory type", "unconnected own");
 	}
-	else if (isMutualAlly)
+	else if (cmpPlayer.IsExclusiveMutualAlly(tileOwner))
 	{
 		if (!this.HasTerritory("ally"))
 			// Translation: territoryType being displayed in a translated sentence in the form: "House cannot be built in %(territoryType)s territory.".
@@ -195,7 +183,7 @@
 			// Translation: territoryType being displayed in a translated sentence in the form: "House cannot be built in %(territoryType)s territory.".
 			invalidTerritory = markForTranslationWithContext("Territory type", "unconnected allied");
 	}
-	else if (isNeutral)
+	else if (!tileOwner)
 	{
 		if (!this.HasTerritory("neutral"))
 			// Translation: territoryType being displayed in a translated sentence in the form: "House cannot be built in %(territoryType)s territory.".
@@ -227,26 +215,26 @@
 		//		1. ships can be spawned "nearby"
 		//		2. builders can pass the terrain where the dock is placed (don't worry about paths)
 		//	so it's correct even if the criteria changes for these units
-		var cmpFootprint = Engine.QueryInterface(this.entity, IID_Footprint);
+		let cmpFootprint = Engine.QueryInterface(this.entity, IID_Footprint);
 		if (!cmpFootprint)
 			return result;	// Fail
 
 		// Get building's footprint
-		var shape = cmpFootprint.GetShape();
-		var halfSize = 0;
+		let shape = cmpFootprint.GetShape();
+		let halfSize = 0;
 		if (shape.type == "square")
 			halfSize = shape.depth/2;
 		else if (shape.type == "circle")
 			halfSize = shape.radius;
 
-		var cmpTerrain = Engine.QueryInterface(SYSTEM_ENTITY, IID_Terrain);
-		var cmpWaterManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_WaterManager);
+		let cmpTerrain = Engine.QueryInterface(SYSTEM_ENTITY, IID_Terrain);
+		let cmpWaterManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_WaterManager);
 		if (!cmpTerrain || !cmpWaterManager)
 			return result;	// Fail
 
-		var ang = cmpPosition.GetRotation().y;
-		var sz = halfSize * Math.sin(ang);
-		var cz = halfSize * Math.cos(ang);
+		let ang = cmpPosition.GetRotation().y;
+		let sz = halfSize * Math.sin(ang);
+		let cz = halfSize * Math.cos(ang);
 		if ((cmpWaterManager.GetWaterLevel(pos.x + sz, pos.y + cz) - cmpTerrain.GetGroundLevel(pos.x + sz, pos.y + cz)) < 1.0 // front
 			|| (cmpWaterManager.GetWaterLevel(pos.x - sz, pos.y - cz) - cmpTerrain.GetGroundLevel(pos.x - sz, pos.y - cz)) > 2.0) // back
 		{
@@ -258,23 +246,23 @@
 	// Check distance restriction
 	if (this.template.Distance)
 	{
-		var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
-		var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
-		var cat = this.template.Distance.FromClass;
+		let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
+		let cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
+		let cat = this.template.Distance.FromClass;
 
-		var filter = function(id)
+		let filter = function(id)
 		{
-			var cmpIdentity = Engine.QueryInterface(id, IID_Identity);
+			let cmpIdentity = Engine.QueryInterface(id, IID_Identity);
 			return cmpIdentity.GetClassesList().indexOf(cat) > -1;
 		};
 		
 		if (this.template.Distance.MinDistance)
 		{
-			var dist = +this.template.Distance.MinDistance;
-			var nearEnts = cmpRangeManager.ExecuteQuery(this.entity, 0, dist, [cmpPlayer.GetPlayerID()], IID_BuildRestrictions).filter(filter);
+			let dist = +this.template.Distance.MinDistance;
+			let nearEnts = cmpRangeManager.ExecuteQuery(this.entity, 0, dist, [cmpPlayer.GetPlayerID()], IID_BuildRestrictions).filter(filter);
 			if (nearEnts.length)
 			{
-				var result = markForPluralTranslation(
+				let result = markForPluralTranslation(
 					"%(name)s too close to a %(category)s, must be at least %(distance)s meter away",
 					"%(name)s too close to a %(category)s, must be at least %(distance)s meters away",
 					+this.template.Distance.MinDistance);
@@ -292,11 +280,11 @@
 		}
 		if (this.template.Distance.MaxDistance)
 		{
-			var dist = +this.template.Distance.MaxDistance;
-			var nearEnts = cmpRangeManager.ExecuteQuery(this.entity, 0, dist, [cmpPlayer.GetPlayerID()], IID_BuildRestrictions).filter(filter);
+			let dist = +this.template.Distance.MaxDistance;
+			let nearEnts = cmpRangeManager.ExecuteQuery(this.entity, 0, dist, [cmpPlayer.GetPlayerID()], IID_BuildRestrictions).filter(filter);
 			if (!nearEnts.length)
 			{
-				var result = markForPluralTranslation(
+				let result = markForPluralTranslation(
 					"%(name)s too far from a %(category)s, must be within %(distance)s meter",
 					"%(name)s too far from a %(category)s, must be within %(distance)s meters",
 					+this.template.Distance.MinDistance);
