Index: binary-improvements/MapRendering/MapRendering/MapRendering.cs
===================================================================
--- binary-improvements/MapRendering/MapRendering/MapRendering.cs	(revision 299)
+++ binary-improvements/MapRendering/MapRendering/MapRendering.cs	(revision 325)
@@ -1,15 +1,46 @@
-using AllocsFixes.JSON;
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Text;
 using System.Threading;
+using System.Timers;
+using AllocsFixes.FileCache;
+using AllocsFixes.JSON;
 using UnityEngine;
-
-namespace AllocsFixes.MapRendering
-{
-	public class MapRendering
-	{
+using Object = UnityEngine.Object;
+
+namespace AllocsFixes.MapRendering {
+	public class MapRendering {
 		private static MapRendering instance;
+
+		private static readonly object lockObject = new object ();
+		public static bool renderingEnabled = true;
+		private readonly MapTileCache cache = new MapTileCache (Constants.MAP_BLOCK_SIZE);
+		private readonly Dictionary<Vector2i, Color32[]> dirtyChunks = new Dictionary<Vector2i, Color32[]> ();
+		private readonly MicroStopwatch msw = new MicroStopwatch ();
+		private readonly MapRenderBlockBuffer[] zoomLevelBuffers;
+		private Coroutine renderCoroutineRef;
+		private bool renderingFullMap;
+		private float renderTimeout = float.MaxValue;
+
+		private MapRendering () {
+			Constants.MAP_DIRECTORY = GameUtils.GetSaveGameDir () + "/map";
+
+			lock (lockObject) {
+				if (!LoadMapInfo ()) {
+					WriteMapInfo ();
+				}
+			}
+
+			cache.SetZoomCount (Constants.ZOOMLEVELS);
+
+			zoomLevelBuffers = new MapRenderBlockBuffer[Constants.ZOOMLEVELS];
+			for (int i = 0; i < Constants.ZOOMLEVELS; i++) {
+				zoomLevelBuffers [i] = new MapRenderBlockBuffer (i, cache);
+			}
+
+			renderCoroutineRef = ThreadManager.StartCoroutine (renderCoroutine ());
+		}
 
 		public static MapRendering Instance {
@@ -18,43 +49,14 @@
 					instance = new MapRendering ();
 				}
+
 				return instance;
 			}
 		}
 
-		private static object lockObject = new object ();
-		private MapRenderBlockBuffer[] zoomLevelBuffers;
-		private Dictionary<Vector2i, Color32[]> dirtyChunks = new Dictionary<Vector2i, Color32[]> ();
-		private bool renderingFullMap = false;
-		public static bool renderingEnabled = true;
-		private MicroStopwatch msw = new MicroStopwatch ();
-		private AllocsFixes.FileCache.MapTileCache cache = new AllocsFixes.FileCache.MapTileCache (Constants.MAP_BLOCK_SIZE);
-		private float renderTimeout = float.MaxValue;
-		private Coroutine renderCoroutineRef;
-
-		public static AllocsFixes.FileCache.MapTileCache GetTileCache() {
+		public static MapTileCache GetTileCache () {
 			return Instance.cache;
 		}
 
-		private MapRendering ()
-		{
-			Constants.MAP_DIRECTORY = GameUtils.GetSaveGameDir () + "/map";
-
-			lock (lockObject) {
-				if (!LoadMapInfo ())
-					WriteMapInfo ();
-			}
-
-			cache.SetZoomCount (Constants.ZOOMLEVELS);
-
-			zoomLevelBuffers = new MapRenderBlockBuffer[Constants.ZOOMLEVELS];
-			for (int i = 0; i < Constants.ZOOMLEVELS; i++) {
-				zoomLevelBuffers [i] = new MapRenderBlockBuffer (i, cache);
-			}
-
-			renderCoroutineRef = ThreadManager.StartCoroutine (renderCoroutine ());
-		}
-
-		public static void Shutdown ()
-		{
+		public static void Shutdown () {
 			if (Instance.renderCoroutineRef != null) {
 				ThreadManager.StopCoroutine (Instance.renderCoroutineRef);
@@ -63,23 +65,25 @@
 		}
 
-		public static void RenderSingleChunk (Chunk chunk)
-		{
+		public static void RenderSingleChunk (Chunk chunk) {
 			if (renderingEnabled) {
-				ThreadPool.UnsafeQueueUserWorkItem ((o) =>
-				{
+				ThreadPool.UnsafeQueueUserWorkItem (o => {
 					try {
 						if (!Instance.renderingFullMap) {
 							lock (lockObject) {
-								Chunk c = (Chunk)o;
+								Chunk c = (Chunk) o;
 								Vector3i cPos = c.GetWorldPos ();
-								Vector2i cPos2 = new Vector2i (cPos.x / Constants.MAP_CHUNK_SIZE, cPos.z / Constants.MAP_CHUNK_SIZE);
+								Vector2i cPos2 = new Vector2i (cPos.x / Constants.MAP_CHUNK_SIZE,
+									cPos.z / Constants.MAP_CHUNK_SIZE);
 
 								ushort[] mapColors = c.GetMapColors ();
 								if (mapColors != null) {
-									Color32[] realColors = new Color32[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE];
+									Color32[] realColors =
+										new Color32[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE];
 									for (int i_colors = 0; i_colors < mapColors.Length; i_colors++) {
 										realColors [i_colors] = shortColorToColor32 (mapColors [i_colors]);
 									}
+
 									Instance.dirtyChunks [cPos2] = realColors;
+
 									//Log.Out ("Add Dirty: " + cPos2);
 								}
@@ -93,6 +97,5 @@
 		}
 
-		public void RenderFullMap ()
-		{
+		public void RenderFullMap () {
 			MicroStopwatch microStopwatch = new MicroStopwatch ();
 
@@ -101,14 +104,16 @@
 			Texture2D fullMapTexture = null;
 
-			Vector2i minChunk = default(Vector2i), maxChunk = default(Vector2i);
-			Vector2i minPos = default(Vector2i), maxPos = default(Vector2i);
+			Vector2i minChunk = default (Vector2i), maxChunk = default (Vector2i);
+			Vector2i minPos = default (Vector2i), maxPos = default (Vector2i);
 			int widthChunks, heightChunks, widthPix, heightPix;
-			getWorldExtent (rfm, out minChunk, out maxChunk, out minPos, out maxPos, out widthChunks, out heightChunks, out widthPix, out heightPix);
-
-			Log.Out (String.Format ("RenderMap: min: {0}, max: {1}, minPos: {2}, maxPos: {3}, w/h: {4}/{5}, wP/hP: {6}/{7}",
-			                        minChunk.ToString (), maxChunk.ToString (),
-			                        minPos.ToString (), maxPos.ToString (),
-			                        widthChunks, heightChunks,
-			                        widthPix, heightPix)
+			getWorldExtent (rfm, out minChunk, out maxChunk, out minPos, out maxPos, out widthChunks, out heightChunks,
+				out widthPix, out heightPix);
+
+			Log.Out (string.Format (
+				"RenderMap: min: {0}, max: {1}, minPos: {2}, maxPos: {3}, w/h: {4}/{5}, wP/hP: {6}/{7}",
+				minChunk.ToString (), maxChunk.ToString (),
+				minPos.ToString (), maxPos.ToString (),
+				widthChunks, heightChunks,
+				widthPix, heightPix)
 			);
 
@@ -121,17 +126,21 @@
 					Directory.Delete (Constants.MAP_DIRECTORY, true);
 				}
+
 				WriteMapInfo ();
 
 				renderingFullMap = true;
 
-				if (widthPix <= 8192 && heightPix <= 8192)
+				if (widthPix <= 8192 && heightPix <= 8192) {
 					fullMapTexture = new Texture2D (widthPix, heightPix);
-
-				Vector2i curFullMapPos = default(Vector2i);
-				Vector2i curChunkPos = default(Vector2i);
+				}
+
+				Vector2i curFullMapPos = default (Vector2i);
+				Vector2i curChunkPos = default (Vector2i);
 				for (curFullMapPos.x = 0; curFullMapPos.x < widthPix; curFullMapPos.x += Constants.MAP_CHUNK_SIZE) {
-					for (curFullMapPos.y = 0; curFullMapPos.y < heightPix; curFullMapPos.y += Constants.MAP_CHUNK_SIZE) {
-						curChunkPos.x = (curFullMapPos.x / Constants.MAP_CHUNK_SIZE) + minChunk.x;
-						curChunkPos.y = (curFullMapPos.y / Constants.MAP_CHUNK_SIZE) + minChunk.y;
+					for (curFullMapPos.y = 0;
+						curFullMapPos.y < heightPix;
+						curFullMapPos.y += Constants.MAP_CHUNK_SIZE) {
+						curChunkPos.x = curFullMapPos.x / Constants.MAP_CHUNK_SIZE + minChunk.x;
+						curChunkPos.y = curFullMapPos.y / Constants.MAP_CHUNK_SIZE + minChunk.y;
 
 						try {
@@ -141,11 +150,15 @@
 								ushort[] mapColors = c.GetMapColors ();
 								if (mapColors != null) {
-									Color32[] realColors = new Color32[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE];
+									Color32[] realColors =
+										new Color32[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE];
 									for (int i_colors = 0; i_colors < mapColors.Length; i_colors++) {
 										realColors [i_colors] = shortColorToColor32 (mapColors [i_colors]);
 									}
+
 									dirtyChunks [curChunkPos] = realColors;
-									if (fullMapTexture != null)
-										fullMapTexture.SetPixels32 (curFullMapPos.x, curFullMapPos.y, Constants.MAP_CHUNK_SIZE, Constants.MAP_CHUNK_SIZE, realColors);
+									if (fullMapTexture != null) {
+										fullMapTexture.SetPixels32 (curFullMapPos.x, curFullMapPos.y,
+											Constants.MAP_CHUNK_SIZE, Constants.MAP_CHUNK_SIZE, realColors);
+									}
 								}
 							}
@@ -159,5 +172,6 @@
 					}
 
-					Log.Out (String.Format ("RenderMap: {0}/{1} ({2}%)", curFullMapPos.x, widthPix, (int)((float)curFullMapPos.x / widthPix * 100)));
+					Log.Out (string.Format ("RenderMap: {0}/{1} ({2}%)", curFullMapPos.x, widthPix,
+						(int) ((float) curFullMapPos.x / widthPix * 100)));
 				}
 			}
@@ -166,5 +180,5 @@
 				byte[] array = fullMapTexture.EncodeToPNG ();
 				File.WriteAllBytes (Constants.MAP_DIRECTORY + "/map.png", array);
-				UnityEngine.Object.Destroy (fullMapTexture);
+				Object.Destroy (fullMapTexture);
 				fullMapTexture = null;
 			}
@@ -176,6 +190,5 @@
 		}
 
-		private void SaveAllBlockMaps (object source, System.Timers.ElapsedEventArgs e)
-		{
+		private void SaveAllBlockMaps (object source, ElapsedEventArgs e) {
 			for (int i = 0; i < Constants.ZOOMLEVELS; i++) {
 				zoomLevelBuffers [i].SaveBlock ();
@@ -183,5 +196,5 @@
 		}
 
-		private System.Collections.IEnumerator renderCoroutine () {
+		private IEnumerator renderCoroutine () {
 			while (true) {
 				lock (lockObject) {
@@ -189,14 +202,15 @@
 						renderTimeout = Time.time + 0.5f;
 					}
+
 					if (Time.time > renderTimeout || dirtyChunks.Count > 200) {
 						RenderDirtyChunks ();
 					}
 				}
+
 				yield return new WaitForSeconds (0.2f);
 			}
 		}
 
-		private void RenderDirtyChunks ()
-		{
+		private void RenderDirtyChunks () {
 			msw.ResetAndRestart ();
 
@@ -210,26 +224,30 @@
 				//Log.Out ("Start Dirty: " + chunkPos);
 
-				Vector2i block = default(Vector2i), blockOffset = default(Vector2i);
-				getBlockNumber (chunkPos, out block, out blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, Constants.MAP_CHUNK_SIZE);
+				Vector2i block = default (Vector2i), blockOffset = default (Vector2i);
+				getBlockNumber (chunkPos, out block, out blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV,
+					Constants.MAP_CHUNK_SIZE);
 
 				zoomLevelBuffers [Constants.ZOOMLEVELS - 1].LoadBlock (block);
 
-				Vector2i v_block = default(Vector2i), v_blockOffset = default(Vector2i);
+				Vector2i v_block = default (Vector2i), v_blockOffset = default (Vector2i);
 				foreach (Vector2i v in keys) {
-					getBlockNumber (v, out v_block, out v_blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, Constants.MAP_CHUNK_SIZE);
+					getBlockNumber (v, out v_block, out v_blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV,
+						Constants.MAP_CHUNK_SIZE);
 					if (v_block.Equals (block)) {
 						//Log.Out ("Dirty: " + v + " render: true");
 						chunksDone.Add (v);
 						if (dirtyChunks [v].Length != Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE) {
-							Log.Error (string.Format ("Rendering chunk has incorrect data size of {0} instead of {1}", dirtyChunks [v].Length, Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE));
-						}
-						zoomLevelBuffers [Constants.ZOOMLEVELS - 1].SetPart (v_blockOffset, Constants.MAP_CHUNK_SIZE, dirtyChunks [v]);
-					} else {
-						//Log.Out ("Dirty: " + v + " render: false");
-					}
-				}
-
-				foreach (Vector2i v in chunksDone)
+							Log.Error (string.Format ("Rendering chunk has incorrect data size of {0} instead of {1}",
+								dirtyChunks [v].Length, Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE));
+						}
+
+						zoomLevelBuffers [Constants.ZOOMLEVELS - 1]
+							.SetPart (v_blockOffset, Constants.MAP_CHUNK_SIZE, dirtyChunks [v]);
+					}
+				}
+
+				foreach (Vector2i v in chunksDone) {
 					dirtyChunks.Remove (v);
+				}
 
 				RenderZoomLevel (Constants.ZOOMLEVELS - 1, block);
@@ -239,12 +257,12 @@
 		}
 
-		private void RenderZoomLevel (int level, Vector2i innerBlock)
-		{
+		private void RenderZoomLevel (int level, Vector2i innerBlock) {
 			if (level > 0) {
-				Vector2i block = default(Vector2i), blockOffset = default(Vector2i);
+				Vector2i block = default (Vector2i), blockOffset = default (Vector2i);
 				getBlockNumber (innerBlock, out block, out blockOffset, 2, Constants.MAP_BLOCK_SIZE / 2);
 
 				zoomLevelBuffers [level - 1].LoadBlock (block);
-				zoomLevelBuffers [level - 1].SetPart (blockOffset, Constants.MAP_BLOCK_SIZE / 2, zoomLevelBuffers [level].GetHalfScaled ());
+				zoomLevelBuffers [level - 1].SetPart (blockOffset, Constants.MAP_BLOCK_SIZE / 2,
+					zoomLevelBuffers [level].GetHalfScaled ());
 
 				RenderZoomLevel (level - 1, block);
@@ -252,16 +270,15 @@
 		}
 
-		private void getBlockNumber (Vector2i innerPos, out Vector2i block, out Vector2i blockOffset, int scaleFactor, int offsetSize)
-		{
-			block = default(Vector2i);
-			blockOffset = default(Vector2i);
-			block.x = ((innerPos.x + 16777216) / scaleFactor) - (16777216 / scaleFactor);
-			block.y = ((innerPos.y + 16777216) / scaleFactor) - (16777216 / scaleFactor);
-			blockOffset.x = ((innerPos.x + 16777216) % scaleFactor) * offsetSize;
-			blockOffset.y = ((innerPos.y + 16777216) % scaleFactor) * offsetSize;
-		}
-
-		private void WriteMapInfo ()
-		{
+		private void getBlockNumber (Vector2i innerPos, out Vector2i block, out Vector2i blockOffset, int scaleFactor,
+			int offsetSize) {
+			block = default (Vector2i);
+			blockOffset = default (Vector2i);
+			block.x = (innerPos.x + 16777216) / scaleFactor - 16777216 / scaleFactor;
+			block.y = (innerPos.y + 16777216) / scaleFactor - 16777216 / scaleFactor;
+			blockOffset.x = (innerPos.x + 16777216) % scaleFactor * offsetSize;
+			blockOffset.y = (innerPos.y + 16777216) % scaleFactor * offsetSize;
+		}
+
+		private void WriteMapInfo () {
 			JSONObject mapInfo = new JSONObject ();
 			mapInfo.Add ("blockSize", new JSONNumber (Constants.MAP_BLOCK_SIZE));
@@ -272,6 +289,5 @@
 		}
 
-		private bool LoadMapInfo ()
-		{
+		private bool LoadMapInfo () {
 			if (File.Exists (Constants.MAP_DIRECTORY + "/mapinfo.json")) {
 				string json = File.ReadAllText (Constants.MAP_DIRECTORY + "/mapinfo.json", Encoding.UTF8);
@@ -279,9 +295,13 @@
 					JSONNode node = Parser.Parse (json);
 					if (node is JSONObject) {
-						JSONObject jo = (JSONObject)node;
-						if (jo.ContainsKey ("blockSize"))
-							Constants.MAP_BLOCK_SIZE = ((JSONNumber)jo ["blockSize"]).GetInt ();
-						if (jo.ContainsKey ("maxZoom"))
-							Constants.ZOOMLEVELS = ((JSONNumber)jo ["maxZoom"]).GetInt () + 1;
+						JSONObject jo = (JSONObject) node;
+						if (jo.ContainsKey ("blockSize")) {
+							Constants.MAP_BLOCK_SIZE = ((JSONNumber) jo ["blockSize"]).GetInt ();
+						}
+
+						if (jo.ContainsKey ("maxZoom")) {
+							Constants.ZOOMLEVELS = ((JSONNumber) jo ["maxZoom"]).GetInt () + 1;
+						}
+
 						return true;
 					}
@@ -292,34 +312,41 @@
 				}
 			}
+
 			return false;
 		}
 
 		private void getWorldExtent (RegionFileManager rfm, out Vector2i minChunk, out Vector2i maxChunk,
-	                                    out Vector2i minPos, out Vector2i maxPos,
-	                                    out int widthChunks, out int heightChunks,
-	                                    out int widthPix, out int heightPix)
-		{
-			minChunk = default(Vector2i);
-			maxChunk = default(Vector2i);
-			minPos = default(Vector2i);
-			maxPos = default(Vector2i);
+			out Vector2i minPos, out Vector2i maxPos,
+			out int widthChunks, out int heightChunks,
+			out int widthPix, out int heightPix) {
+			minChunk = default (Vector2i);
+			maxChunk = default (Vector2i);
+			minPos = default (Vector2i);
+			maxPos = default (Vector2i);
 
 			long[] keys = rfm.GetAllChunkKeys ();
-			int minX = Int32.MaxValue;
-			int minY = Int32.MaxValue;
-			int maxX = Int32.MinValue;
-			int maxY = Int32.MinValue;
+			int minX = int.MaxValue;
+			int minY = int.MaxValue;
+			int maxX = int.MinValue;
+			int maxY = int.MinValue;
 			foreach (long key in keys) {
 				int x = WorldChunkCache.extractX (key);
 				int y = WorldChunkCache.extractZ (key);
 
-				if (x < minX)
+				if (x < minX) {
 					minX = x;
-				if (x > maxX)
+				}
+
+				if (x > maxX) {
 					maxX = x;
-				if (y < minY)
+				}
+
+				if (y < minY) {
 					minY = y;
-				if (y > maxY)
+				}
+
+				if (y > maxY) {
 					maxY = y;
+				}
 			}
 
@@ -343,14 +370,12 @@
 		}
 
-		private static Color shortColorToColor (ushort col)
-		{
-			return new Color (((float)(col >> 10 & 31) / 31f), ((float)(col >> 5 & 31) / 31f), ((float)(col & 31) / 31f), 255);
-		}
-
-		private static Color32 shortColorToColor32 (ushort col)
-		{
-			byte r = (byte)(256 * (col >> 10 & 31) / 32);
-			byte g = (byte)(256 * (col >> 5 & 31) / 32);
-			byte b = (byte)(256 * (col & 31) / 32);
+		private static Color shortColorToColor (ushort col) {
+			return new Color (((col >> 10) & 31) / 31f, ((col >> 5) & 31) / 31f, (col & 31) / 31f, 255);
+		}
+
+		private static Color32 shortColorToColor32 (ushort col) {
+			byte r = (byte) (256 * ((col >> 10) & 31) / 32);
+			byte g = (byte) (256 * ((col >> 5) & 31) / 32);
+			byte b = (byte) (256 * (col & 31) / 32);
 			byte a = 255;
 			return new Color32 (r, g, b, a);
