using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using UnityEngine;

// Token: 0x0200000E RID: 14
public class FOWSystemInterior : MonoBehaviour
{
	// Token: 0x17000008 RID: 8
	// (get) Token: 0x060000BF RID: 191 RVA: 0x0000F37A File Offset: 0x0000D57A
	public Texture2D texture0
	{
		get
		{
			return this.mTexture0;
		}
	}

	// Token: 0x17000009 RID: 9
	// (get) Token: 0x060000C0 RID: 192 RVA: 0x0000F382 File Offset: 0x0000D582
	public Texture2D texture1
	{
		get
		{
			return this.mTexture1;
		}
	}

	// Token: 0x1700000A RID: 10
	// (get) Token: 0x060000C1 RID: 193 RVA: 0x0000F38A File Offset: 0x0000D58A
	public float blendFactor
	{
		get
		{
			return this.mBlendFactor;
		}
	}

	// Token: 0x060000C2 RID: 194 RVA: 0x0000F392 File Offset: 0x0000D592
	private void Awake()
	{
		FOWSystemInterior.instance = this;
	}

	// Token: 0x060000C3 RID: 195 RVA: 0x0000F39A File Offset: 0x0000D59A
	private IEnumerator Start()
	{
		while (!FOWSystemInterior.readyToCreate)
		{
			yield return null;
		}
		this.saveFow = true;
		this.mTrans = base.transform;
		this.mHeights = new int[this.textureSize, this.textureSize];
		this.mSize = new Vector3((float)this.worldSize, this.heightRange.y - this.heightRange.x, (float)this.worldSize);
		this.mOrigin = this.mTrans.position;
		this.mOrigin.x = this.mOrigin.x - (float)this.worldSize * 0.5f;
		this.mOrigin.z = this.mOrigin.z - (float)this.worldSize * 0.5f;
		int num = this.textureSize * this.textureSize;
		if (!this.loaded)
		{
			this.mBuffer0 = new Color32[num];
			this.mBuffer1 = new Color32[num];
			this.mBuffer2 = new Color32[num];
		}
		this.CreateGrid();
		if (FOWSystemInterior.onLoad != null)
		{
			FOWSystemInterior.onLoad();
		}
		this.UpdateBuffer();
		this.RebuildTextures();
		this.mNextUpdate = Time.time + this.updateFrequency;
		this.mThread = new Thread(new ThreadStart(this.ThreadUpdate));
		this.mThread.Start();
		yield break;
	}

	// Token: 0x060000C4 RID: 196 RVA: 0x0000F3AC File Offset: 0x0000D5AC
	private void OnDestroy()
	{
		if (this.mThread != null)
		{
			this.mThread.Abort();
			while (this.mThread.IsAlive)
			{
				Thread.Sleep(1);
			}
			this.mThread = null;
		}
		if (FOWSystemInterior.onSave != null)
		{
			FOWSystemInterior.onSave();
		}
		FOWSystemInterior.onLoad = null;
		FOWSystemInterior.onSave = null;
		FOWSystemInterior.instance = null;
	}

	// Token: 0x060000C5 RID: 197 RVA: 0x0000F40C File Offset: 0x0000D60C
	private void Update()
	{
		if (this.mTrans == null)
		{
			return;
		}
		if (this.textureBlendTime > 0f)
		{
			this.mBlendFactor = Mathf.Clamp01(this.mBlendFactor + Time.unscaledDeltaTime / this.textureBlendTime);
		}
		else
		{
			this.mBlendFactor = 1f;
		}
		if (!this.cameraTarget)
		{
			this.cameraTarget = GameObject.Find("Character Follow").transform;
			GameObject gameObject = GameObject.Find("Main Camera");
			this.cam = gameObject.GetComponent<Camera>();
		}
		RaycastHit raycastHit;
		if (Physics.Raycast(this.cameraTarget.position + new Vector3(0f, 500f, 0f), Vector3.up * -1f, out raycastHit, 10000f, 1073741824))
		{
			this.currentInterior = this.interiorColliders.IndexOf(raycastHit.collider);
		}
		if (this.mState == FOWSystemInterior.State.Blending)
		{
			float time = Time.time;
			if (this.mNextUpdate < time)
			{
				this.mNextUpdate = time + this.updateFrequency;
				this.mState = FOWSystemInterior.State.NeedUpdate;
			}
		}
		else if (this.mState != FOWSystemInterior.State.NeedUpdate)
		{
			if (this.mScreenHeight != Screen.height || this.mTexture0 == null)
			{
				this.RebuildTextures();
			}
			else if (this.mState == FOWSystemInterior.State.UpdateTexture0)
			{
				this.mTexture0.SetPixels32(this.mBuffer0);
				this.mTexture0.Apply();
				this.mState = FOWSystemInterior.State.UpdateTexture1;
				this.mBlendFactor = 0f;
			}
			else if (this.mState == FOWSystemInterior.State.UpdateTexture1)
			{
				this.mTexture1.SetPixels32(this.mBuffer1);
				this.mTexture1.Apply();
				this.mState = FOWSystemInterior.State.Blending;
			}
		}
		float num = 1f / (float)this.worldSize;
		float num2 = this.mTrans.position.x - (float)this.worldSize * 0.5f;
		float num3 = this.mTrans.position.z - (float)this.worldSize * 0.5f;
		Vector4 vector = new Vector4(-num2 * num, -num3 * num, num, this.mBlendFactor);
		Shader.SetGlobalColor("_FOWUnexploredInterior", this.unexploredColor);
		Shader.SetGlobalColor("_FOWExploredInterior", this.exploredColor);
		Shader.SetGlobalVector("_FOWParamsInterior", vector);
		Shader.SetGlobalTexture("_FOWTex0Interior", this.mTexture0);
		Shader.SetGlobalTexture("_FOWTex1Interior", this.mTexture1);
	}

	// Token: 0x060000C6 RID: 198 RVA: 0x0000F674 File Offset: 0x0000D874
	public void SetGlobalShaderVariables()
	{
		this.isActive = true;
		float num = 1f / (float)this.worldSize;
		float num2 = this.mTrans.position.x - (float)this.worldSize * 0.5f;
		float num3 = this.mTrans.position.z - (float)this.worldSize * 0.5f;
		Vector4 vector = new Vector4(-num2 * num, -num3 * num, num, this.mBlendFactor);
		Shader.SetGlobalColor("_FOWUnexploredInterior", this.unexploredColor);
		Shader.SetGlobalColor("_FOWExploredInterior", this.exploredColor);
		Shader.SetGlobalVector("_FOWParamsInterior", vector);
		Shader.SetGlobalTexture("_FOWTex0Interior", this.mTexture0);
		Shader.SetGlobalTexture("_FOWTex1Interior", this.mTexture1);
	}

	// Token: 0x060000C7 RID: 199 RVA: 0x0000F738 File Offset: 0x0000D938
	public Vector4 GetParams()
	{
		float num = 1f / (float)this.worldSize;
		float num2 = this.mTrans.position.x - (float)this.worldSize * 0.5f;
		float num3 = this.mTrans.position.z - (float)this.worldSize * 0.5f;
		return new Vector4(-num2 * num, -num3 * num, num, this.mBlendFactor);
	}

	// Token: 0x060000C8 RID: 200 RVA: 0x0000F7A4 File Offset: 0x0000D9A4
	private void RebuildTextures()
	{
		this.mScreenHeight = Screen.height;
		if (this.mTexture0 != null)
		{
			Object.Destroy(this.mTexture0);
		}
		if (this.mTexture1 != null)
		{
			Object.Destroy(this.mTexture1);
		}
		bool flag = QualitySettings.activeColorSpace == ColorSpace.Linear;
		this.mTexture0 = new Texture2D(this.textureSize, this.textureSize, TextureFormat.ARGB32, false, flag);
		this.mTexture1 = new Texture2D(this.textureSize, this.textureSize, TextureFormat.ARGB32, false, flag);
		this.mTexture0.wrapMode = TextureWrapMode.Clamp;
		this.mTexture1.wrapMode = TextureWrapMode.Clamp;
		this.mTexture0.SetPixels32(this.mBuffer0);
		this.mTexture0.Apply();
		this.mTexture1.SetPixels32(this.mBuffer1);
		this.mTexture1.Apply();
		this.mState = FOWSystemInterior.State.Blending;
	}

	// Token: 0x060000C9 RID: 201 RVA: 0x0000F884 File Offset: 0x0000DA84
	private void ThreadUpdate()
	{
		Stopwatch stopwatch = new Stopwatch();
		for (;;)
		{
			if (this.mState == FOWSystemInterior.State.NeedUpdate)
			{
				stopwatch.Reset();
				stopwatch.Start();
				this.UpdateBuffer();
				stopwatch.Stop();
				if (this.debug)
				{
					Debug.Log(stopwatch.ElapsedMilliseconds);
				}
				this.mElapsed = 0.001f * (float)stopwatch.ElapsedMilliseconds;
				this.mState = FOWSystemInterior.State.UpdateTexture0;
			}
			Thread.Sleep(1);
		}
	}

	// Token: 0x060000CA RID: 202 RVA: 0x0000F8F4 File Offset: 0x0000DAF4
	private void OnDrawGizmosSelected()
	{
		Gizmos.matrix = base.transform.localToWorldMatrix;
		Gizmos.color = Color.yellow;
		Gizmos.DrawWireCube(new Vector3(0f, (this.heightRange.x + this.heightRange.y) * 0.5f, 0f), new Vector3((float)this.worldSize, this.heightRange.y - this.heightRange.x, (float)this.worldSize));
	}

	// Token: 0x060000CB RID: 203 RVA: 0x0000F978 File Offset: 0x0000DB78
	private bool IsVisible(int sx, int sy, int fx, int fy, float outer, int sightHeight, int variance)
	{
		int num = Mathf.Abs(fx - sx);
		int num2 = Mathf.Abs(fy - sy);
		int num3 = ((sx < fx) ? 1 : (-1));
		int num4 = ((sy < fy) ? 1 : (-1));
		int num5 = num - num2;
		float num6 = (float)sightHeight;
		float num7 = (float)this.mHeights[fx, fy];
		float num8 = 1f / outer;
		while (sx != fx || sy != fy)
		{
			int num9 = fx - sx;
			int num10 = fy - sy;
			float num11 = num8 * Mathf.Sqrt((float)(num9 * num9 + num10 * num10));
			if ((float)this.mHeights[sx, sy] > Mathf.Lerp(num7, num6, num11) + (float)variance)
			{
				return false;
			}
			int num12 = num5 << 1;
			if (num12 > -num2)
			{
				num5 -= num2;
				sx += num3;
			}
			if (num12 < num)
			{
				num5 += num;
				sy += num4;
			}
		}
		return true;
	}

	// Token: 0x060000CC RID: 204 RVA: 0x0000FA47 File Offset: 0x0000DC47
	public int WorldToGridHeight(float height)
	{
		return Mathf.Clamp(Mathf.RoundToInt(height / this.mSize.y * 255f), 0, 255);
	}

	// Token: 0x060000CD RID: 205 RVA: 0x0000FA6C File Offset: 0x0000DC6C
	public virtual void CreateGrid()
	{
		Vector3 vector = this.mOrigin;
		vector.y += this.mSize.y;
		float num = (float)this.worldSize / (float)this.textureSize;
		float num2 = this.raycastRadius;
		this.mInteriors = new int[this.mBuffer0.Length];
		Ray ray = new Ray(Vector3.zero, Vector3.down);
		this.interiors = GameObject.Find("Capture B");
		if (this.interiors)
		{
			this.interiorsTr = this.interiors.transform;
		}
		this.interiorColliders.Clear();
		GameObject gameObject = GameObject.Find("Character Follow");
		if (gameObject)
		{
			this.cameraTarget = gameObject.transform;
		}
		if (this.interiors)
		{
			foreach (object obj in this.interiorsTr)
			{
				Transform transform = (Transform)obj;
				if (transform != this.interiorsTr)
				{
					Collider component = transform.gameObject.GetComponent<Collider>();
					if (component && transform.childCount > 0)
					{
						this.interiorColliders.Add(component);
					}
				}
			}
		}
		int num3 = 0;
		for (int i = 0; i < this.textureSize; i++)
		{
			vector.z = this.mOrigin.z + (float)i * num;
			int j = 0;
			while (j < this.textureSize)
			{
				vector.x = this.mOrigin.x + (float)j * num;
				ray.origin = vector;
				RaycastHit raycastHit;
				if (Physics.Raycast(new Vector3(vector.x, 1000f, vector.z), Vector3.up * -1f, out raycastHit, 3000f, 1073741824))
				{
					this.mInteriors[num3] = this.interiorColliders.IndexOf(raycastHit.collider);
				}
				num3++;
				RaycastHit raycastHit2;
				if (!Physics.SphereCast(ray, this.raycastRadius, out raycastHit2, this.mSize.y, this.raycastMask))
				{
					goto IL_026B;
				}
				bool flag = false;
				if (raycastHit2.collider.gameObject.layer == 16 && raycastHit2.collider.gameObject.GetComponent<Puzzle>())
				{
					flag = true;
				}
				if (flag)
				{
					goto IL_026B;
				}
				this.mHeights[j, i] = this.WorldToGridHeight(vector.y - raycastHit2.distance - this.raycastRadius);
				IL_027B:
				j++;
				continue;
				IL_026B:
				this.mHeights[j, i] = 0;
				goto IL_027B;
			}
		}
	}

	// Token: 0x060000CE RID: 206 RVA: 0x0000FD2C File Offset: 0x0000DF2C
	public void UpdateGrid(Vector3 center, float radius)
	{
		Vector3 vector = this.mOrigin;
		vector.y += this.mSize.y;
		float num = (float)this.worldSize / (float)this.textureSize;
		float num2 = this.raycastRadius;
		Vector3 vector2 = this.mOrigin;
		Ray ray = new Ray(Vector3.zero, Vector3.down);
		for (int i = 0; i < this.textureSize; i++)
		{
			vector.z = this.mOrigin.z + (float)i * num;
			for (int j = 0; j < this.textureSize; j++)
			{
				vector.x = this.mOrigin.x + (float)j * num;
				bool flag = false;
				vector2 = this.mOrigin;
				vector2.x += (float)j;
				vector2.z += (float)i;
				vector2 = vector;
				center.y = vector2.y;
				ray.origin = vector;
				if ((vector2 - center).sqrMagnitude < radius * radius)
				{
					flag = true;
				}
				RaycastHit raycastHit;
				if (flag && Physics.SphereCast(ray, this.raycastRadius, out raycastHit, this.mSize.y, this.raycastMask))
				{
					bool flag2 = false;
					if (raycastHit.collider.gameObject.layer == 16 && raycastHit.collider.gameObject.GetComponent<Puzzle>())
					{
						flag2 = true;
					}
					if (!flag2)
					{
						this.mHeights[j, i] = this.WorldToGridHeight(vector.y - raycastHit.distance - this.raycastRadius);
					}
				}
			}
		}
	}

	// Token: 0x060000CF RID: 207 RVA: 0x0000FED4 File Offset: 0x0000E0D4
	private void UpdateBuffer()
	{
		if (FOWSystemInterior.mAdded.Count > 0)
		{
			List<FOWSystemInterior.Revealer> list = FOWSystemInterior.mAdded;
			lock (list)
			{
				while (FOWSystemInterior.mAdded.Count > 0)
				{
					int num = FOWSystemInterior.mAdded.Count - 1;
					FOWSystemInterior.mRevealers.Add(FOWSystemInterior.mAdded[num]);
					FOWSystemInterior.mAdded.RemoveAt(num);
				}
			}
		}
		if (FOWSystemInterior.mRemoved.Count > 0)
		{
			List<FOWSystemInterior.Revealer> list = FOWSystemInterior.mRemoved;
			lock (list)
			{
				while (FOWSystemInterior.mRemoved.Count > 0)
				{
					int num2 = FOWSystemInterior.mRemoved.Count - 1;
					FOWSystemInterior.mRevealers.Remove(FOWSystemInterior.mRemoved[num2]);
					FOWSystemInterior.mRemoved.RemoveAt(num2);
				}
			}
		}
		float num3 = ((this.textureBlendTime > 0f) ? Mathf.Clamp01(this.mBlendFactor + this.mElapsed / this.textureBlendTime) : 1f);
		int i = 0;
		int num4 = this.mBuffer0.Length;
		while (i < num4)
		{
			this.mBuffer0[i] = Color32.Lerp(this.mBuffer0[i], this.mBuffer1[i], num3);
			this.mBuffer1[i].r = 0;
			i++;
		}
		float num5 = (float)this.textureSize / (float)this.worldSize;
		int j = 0;
		int count = FOWSystemInterior.mRevealers.Count;
		while (j < count)
		{
			FOWSystemInterior.Revealer revealer = FOWSystemInterior.mRevealers[j];
			if (revealer.isActive)
			{
				FOWSystemInterior.Revealer revealer2 = revealer;
				lock (revealer2)
				{
					if (revealer.los == FOWSystemInterior.LOSChecks.None)
					{
						this.RevealUsingRadius(revealer, num5);
					}
					else if (revealer.los == FOWSystemInterior.LOSChecks.OnlyOnce)
					{
						this.RevealUsingCache(revealer, num5);
					}
					else
					{
						this.RevealUsingLOS(revealer, num5);
					}
				}
			}
			j++;
		}
		for (int k = 0; k < this.blurIterations; k++)
		{
			this.BlurVisibility();
		}
		this.RevealVisible();
	}

	// Token: 0x060000D0 RID: 208 RVA: 0x00010118 File Offset: 0x0000E318
	private void RevealUsingRadius(FOWSystemInterior.Revealer r, float worldToTex)
	{
		Vector3 vector = r.pos - this.mOrigin;
		int num = Mathf.RoundToInt((vector.x - r.outer) * worldToTex);
		int num2 = Mathf.RoundToInt((vector.z - r.outer) * worldToTex);
		int num3 = Mathf.RoundToInt((vector.x + r.outer) * worldToTex);
		int num4 = Mathf.RoundToInt((vector.z + r.outer) * worldToTex);
		int num5 = Mathf.RoundToInt(vector.x * worldToTex);
		int num6 = Mathf.RoundToInt(vector.z * worldToTex);
		num5 = Mathf.Clamp(num5, 0, this.textureSize - 1);
		num6 = Mathf.Clamp(num6, 0, this.textureSize - 1);
		int num7 = Mathf.RoundToInt(r.outer * r.outer * worldToTex * worldToTex);
		int num8 = this.textureSize - 1;
		for (int i = num2; i < num4; i++)
		{
			if (i > 0 && i < num8)
			{
				int num9 = i * this.textureSize;
				for (int j = num; j < num3; j++)
				{
					if (j > 0 && j < num8)
					{
						int num10 = j - num5;
						int num11 = i - num6;
						if (num10 * num10 + num11 * num11 < num7)
						{
							this.mBuffer1[j + num9].r = byte.MaxValue;
						}
					}
				}
			}
		}
	}

	// Token: 0x060000D1 RID: 209 RVA: 0x00010260 File Offset: 0x0000E460
	private void RevealUsingLOS(FOWSystemInterior.Revealer r, float worldToTex)
	{
		Vector3 vector = r.pos - this.mOrigin;
		int num = Mathf.RoundToInt((vector.x - r.outer) * worldToTex);
		int num2 = Mathf.RoundToInt((vector.z - r.outer) * worldToTex);
		int num3 = Mathf.RoundToInt((vector.x + r.outer) * worldToTex);
		int num4 = Mathf.RoundToInt((vector.z + r.outer) * worldToTex);
		num = Mathf.Clamp(num, 0, this.textureSize - 1);
		num3 = Mathf.Clamp(num3, 0, this.textureSize - 1);
		num2 = Mathf.Clamp(num2, 0, this.textureSize - 1);
		num4 = Mathf.Clamp(num4, 0, this.textureSize - 1);
		int num5 = Mathf.RoundToInt(vector.x * worldToTex);
		int num6 = Mathf.RoundToInt(vector.z * worldToTex);
		num5 = Mathf.Clamp(num5, 0, this.textureSize - 1);
		num6 = Mathf.Clamp(num6, 0, this.textureSize - 1);
		int num7 = Mathf.RoundToInt(r.inner * r.inner * worldToTex * worldToTex);
		int num8 = Mathf.RoundToInt(r.outer * r.outer * worldToTex * worldToTex);
		int num9 = this.WorldToGridHeight(r.pos.y);
		int num10 = Mathf.RoundToInt(Mathf.Clamp01(this.margin / (this.heightRange.y - this.heightRange.x)) * 255f);
		Color32 color = new Color32(byte.MaxValue, 0, byte.MaxValue, byte.MaxValue);
		int num11 = this.textureSize - 1;
		for (int i = num2; i < num4; i++)
		{
			if (i > 0 && i < num11)
			{
				for (int j = num; j < num3; j++)
				{
					if (j > 0 && j < num11)
					{
						int num12 = j - num5;
						int num13 = i - num6;
						int num14 = num12 * num12 + num13 * num13;
						int num15 = j + i * this.textureSize;
						if (num14 < num7 || (num5 == j && num6 == i))
						{
							color = this.mBuffer1[num15];
							color.r = 1;
							this.mBuffer1[num15] = color;
						}
						else if (num14 < num8)
						{
							Vector2 vector2 = new Vector2((float)num12, (float)num13);
							vector2.Normalize();
							vector2 *= r.inner;
							int num16 = num5 + Mathf.RoundToInt(vector2.x);
							int num17 = num6 + Mathf.RoundToInt(vector2.y);
							if (num16 > -1 && num16 < this.textureSize && num17 > -1 && num17 < this.textureSize && this.IsVisible(num16, num17, j, i, Mathf.Sqrt((float)num14), num9, num10))
							{
								color = this.mBuffer1[num15];
								color.r = byte.MaxValue;
								this.mBuffer1[num15] = color;
							}
						}
					}
				}
			}
		}
	}

	// Token: 0x060000D2 RID: 210 RVA: 0x00010550 File Offset: 0x0000E750
	private void RevealUsingCache(FOWSystemInterior.Revealer r, float worldToTex)
	{
		if (!r.isValid)
		{
			this.RevealIntoCache(r, worldToTex);
		}
		Color32 color = new Color32(byte.MaxValue, 0, byte.MaxValue, byte.MaxValue);
		bool[] cachedBuffer = r.cachedBuffer;
		int num = this.textureSize - 1;
		int i = r.cachedY;
		int num2 = r.cachedY + r.cachedSize;
		while (i < num2)
		{
			if (i > 0 && i < num)
			{
				int num3 = i * this.textureSize;
				int num4 = (i - r.cachedY) * r.cachedSize;
				int j = r.cachedX;
				int num5 = r.cachedX + r.cachedSize;
				while (j < num5)
				{
					if (j > 0 && j < num)
					{
						int num6 = j - r.cachedX + num4;
						if (cachedBuffer[num6])
						{
							color = this.mBuffer1[j + num3];
							color.r = byte.MaxValue;
							this.mBuffer1[j + num3] = color;
						}
					}
					j++;
				}
			}
			i++;
		}
	}

	// Token: 0x060000D3 RID: 211 RVA: 0x00010658 File Offset: 0x0000E858
	private void RevealIntoCache(FOWSystemInterior.Revealer r, float worldToTex)
	{
		Vector3 vector = r.pos - this.mOrigin;
		int num = Mathf.RoundToInt((vector.x - r.outer) * worldToTex);
		int num2 = Mathf.RoundToInt((vector.z - r.outer) * worldToTex);
		int num3 = Mathf.RoundToInt((vector.x + r.outer) * worldToTex);
		int num4 = Mathf.RoundToInt((vector.z + r.outer) * worldToTex);
		int num5 = Mathf.RoundToInt(vector.x * worldToTex);
		int num6 = Mathf.RoundToInt(vector.z * worldToTex);
		num5 = Mathf.Clamp(num5, 0, this.textureSize - 1);
		num6 = Mathf.Clamp(num6, 0, this.textureSize - 1);
		int num7 = Mathf.Max(Mathf.RoundToInt((float)(num3 - num)), Mathf.RoundToInt((float)(num4 - num2)));
		int num8 = num7 * num7;
		if (r.cachedBuffer == null || r.cachedBuffer.Length != num8)
		{
			r.cachedBuffer = new bool[num8];
		}
		int i = 0;
		int num9 = num7 * num7;
		while (i < num9)
		{
			r.cachedBuffer[i] = false;
			i++;
		}
		int num10 = Mathf.RoundToInt(r.inner * r.inner * worldToTex * worldToTex);
		int num11 = Mathf.RoundToInt(r.outer * r.outer * worldToTex * worldToTex);
		int num12 = Mathf.RoundToInt(Mathf.Clamp01(this.margin / (this.heightRange.y - this.heightRange.x)) * 255f);
		int num13 = this.WorldToGridHeight(r.pos.y);
		int num14 = this.textureSize - 1;
		for (int j = num2; j < num4; j++)
		{
			if (j > 0 && j < num14)
			{
				for (int k = num; k < num3; k++)
				{
					if (k > 0 && k < num14)
					{
						int num15 = k - num5;
						int num16 = j - num6;
						int num17 = num15 * num15 + num16 * num16;
						if (num17 < num10 || (num5 == k && num6 == j))
						{
							r.cachedBuffer[k - num + (j - num2) * num7] = true;
						}
						else if (num17 < num11)
						{
							Vector2 vector2 = new Vector2((float)num15, (float)num16);
							vector2.Normalize();
							vector2 *= r.inner;
							int num18 = num5 + Mathf.RoundToInt(vector2.x * worldToTex);
							int num19 = num6 + Mathf.RoundToInt(vector2.y * worldToTex);
							if (num18 > -1 && num18 < this.textureSize && num19 > -1 && num19 < this.textureSize && this.IsVisible(num18, num19, k, j, Mathf.Sqrt((float)num17), num13, num12))
							{
								r.cachedBuffer[k - num + (j - num2) * num7] = true;
							}
						}
					}
				}
			}
		}
		r.cachedSize = num7;
		r.cachedX = num;
		r.cachedY = num2;
		r.isValid = true;
	}

	// Token: 0x060000D4 RID: 212 RVA: 0x0001093C File Offset: 0x0000EB3C
	private void BlurVisibility()
	{
		for (int i = 0; i < this.textureSize; i++)
		{
			int num = i * this.textureSize;
			int num2 = i - 1;
			if (num2 < 0)
			{
				num2 = 0;
			}
			int num3 = i + 1;
			if (num3 == this.textureSize)
			{
				num3 = i;
			}
			num2 *= this.textureSize;
			num3 *= this.textureSize;
			for (int j = 0; j < this.textureSize; j++)
			{
				int num4 = j - 1;
				if (num4 < 0)
				{
					num4 = 0;
				}
				int num5 = j + 1;
				if (num5 == this.textureSize)
				{
					num5 = j;
				}
				int num6 = j + num;
				int num7 = (int)this.mBuffer1[num6].r;
				num7 += (int)this.mBuffer1[num4 + num].r;
				num7 += (int)this.mBuffer1[num5 + num].r;
				num7 += (int)this.mBuffer1[j + num2].r;
				num7 += (int)this.mBuffer1[j + num3].r;
				num7 += (int)this.mBuffer1[num4 + num2].r;
				num7 += (int)this.mBuffer1[num5 + num2].r;
				num7 += (int)this.mBuffer1[num4 + num3].r;
				num7 += (int)this.mBuffer1[num5 + num3].r;
				Color32 color = this.mBuffer2[num6];
				color.r = (byte)(num7 / 9);
				num7 = (int)this.mBuffer1[num6].g;
				num7 += (int)this.mBuffer1[num4 + num].g;
				num7 += (int)this.mBuffer1[num5 + num].g;
				num7 += (int)this.mBuffer1[j + num2].g;
				num7 += (int)this.mBuffer1[j + num3].g;
				color.g = (byte)(num7 / 5);
				this.mBuffer2[num6] = color;
			}
		}
		Color32[] array = this.mBuffer1;
		this.mBuffer1 = this.mBuffer2;
		this.mBuffer2 = array;
	}

	// Token: 0x060000D5 RID: 213 RVA: 0x00010B8C File Offset: 0x0000ED8C
	public void UpdateInterior(BoxCollider coll)
	{
		BoxCollider boxCollider = null;
		if (coll)
		{
			boxCollider = coll.gameObject.transform.parent.gameObject.GetComponent<BoxCollider>();
		}
		this.currentInterior = this.interiorColliders.IndexOf(boxCollider);
		this.RevealVisible();
		this.mTexture0.SetPixels32(this.mBuffer1);
		this.mTexture0.Apply();
		this.mTexture1.SetPixels32(this.mBuffer1);
		this.mTexture1.Apply();
	}

	// Token: 0x060000D6 RID: 214 RVA: 0x00010C0E File Offset: 0x0000EE0E
	public void UpdateInteriorFromChange()
	{
		this.RevealVisible();
		this.mTexture0.SetPixels32(this.mBuffer1);
		this.mTexture0.Apply();
		this.mTexture1.SetPixels32(this.mBuffer1);
		this.mTexture1.Apply();
	}

	// Token: 0x060000D7 RID: 215 RVA: 0x00010C50 File Offset: 0x0000EE50
	private void RevealVisible()
	{
		for (int i = 0; i < this.textureSize; i++)
		{
			int num = i * this.textureSize;
			for (int j = 0; j < this.textureSize; j++)
			{
				int num2 = j + num;
				Color32 color = this.mBuffer1[num2];
				if (color.b < color.r && this.saveFow)
				{
					color.b = color.r;
				}
				if (this.currentInterior == this.mInteriors[num2])
				{
					color.g = byte.MaxValue;
				}
				else
				{
					color.g = 0;
				}
				this.mBuffer1[num2] = color;
			}
		}
	}

	// Token: 0x060000D8 RID: 216 RVA: 0x00010CFC File Offset: 0x0000EEFC
	public void Save()
	{
		FOWSystemInterior.GetRevealedBuffer();
		int num = FOWSystemInterior.instance.textureSize * FOWSystemInterior.instance.textureSize;
		List<float> list = new List<float>();
		for (int i = 0; i < num; i++)
		{
			list.Add((float)FOWSystemInterior.instance.mBuffer0[i].b);
		}
		if (Records.x.inOverworldMap)
		{
			Links.x.gaia.UpdateOverworldFow(list);
			return;
		}
		ES3.Save<List<float>>("FowInterior", list, Records.x.openBook + "/" + Links.x.diorama.sceneName + "_FOW");
	}

	// Token: 0x060000D9 RID: 217 RVA: 0x00010DA4 File Offset: 0x0000EFA4
	public void Load()
	{
		string text = Records.x.openBook + "/" + Links.x.diorama.sceneName + "_FOW";
		if (ES3.FileExists(text))
		{
			List<float> list = ES3.Load<List<float>>("FowInterior", text);
			int num = FOWSystemInterior.instance.textureSize * FOWSystemInterior.instance.textureSize;
			if (list.Count != num)
			{
				Debug.LogError("Buffer size mismatch. Fog is " + num.ToString() + ", but passed array is " + list.Count.ToString());
			}
			else
			{
				if (FOWSystemInterior.instance.mBuffer0 == null)
				{
					FOWSystemInterior.instance.mBuffer0 = new Color32[num];
					FOWSystemInterior.instance.mBuffer1 = new Color32[num];
					FOWSystemInterior.instance.mBuffer2 = new Color32[num];
				}
				this.loaded = true;
				Color color = default(Color);
				for (int i = 0; i < num; i++)
				{
					color.r = (float)FOWSystemInterior.instance.mBuffer0[i].r;
					color.b = list[i];
					color.g = (float)FOWSystemInterior.instance.mBuffer0[i].g;
					color.a = (float)FOWSystemInterior.instance.mBuffer0[i].a;
					FOWSystemInterior.instance.mBuffer0[i] = color;
					color.r = (float)FOWSystemInterior.instance.mBuffer1[i].r;
					color.b = list[i];
					color.g = (float)FOWSystemInterior.instance.mBuffer1[i].g;
					color.a = (float)FOWSystemInterior.instance.mBuffer1[i].a;
					FOWSystemInterior.instance.mBuffer1[i] = color;
				}
			}
			this.RebuildTextures();
			for (int j = 0; j < this.textureSize; j++)
			{
				int num2 = j * this.textureSize;
				int num3 = j - 1;
				if (num3 < 0)
				{
					num3 = 0;
				}
				int num4 = j + 1;
				if (num4 == this.textureSize)
				{
					num4 = j;
				}
				num3 *= this.textureSize;
				num4 *= this.textureSize;
				for (int k = 0; k < this.textureSize; k++)
				{
					int num5 = k - 1;
					if (num5 < 0)
					{
						num5 = 0;
					}
					int num6 = k + 1;
					if (num6 == this.textureSize)
					{
						num6 = k;
					}
					int num7 = k + num2;
					int num8 = (int)FOWSystemInterior.instance.mBuffer1[num7].b;
					num8 += (int)FOWSystemInterior.instance.mBuffer1[num5 + num2].b;
					num8 += (int)FOWSystemInterior.instance.mBuffer1[num6 + num2].b;
					num8 += (int)FOWSystemInterior.instance.mBuffer1[k + num3].b;
					num8 += (int)FOWSystemInterior.instance.mBuffer1[k + num4].b;
					num8 += (int)FOWSystemInterior.instance.mBuffer1[num5 + num3].b;
					num8 += (int)FOWSystemInterior.instance.mBuffer1[num6 + num3].b;
					num8 += (int)FOWSystemInterior.instance.mBuffer1[num5 + num4].b;
					num8 += (int)FOWSystemInterior.instance.mBuffer1[num6 + num4].b;
					Color32 color2 = FOWSystemInterior.instance.mBuffer2[num7];
					color2.b = (byte)(num8 / 9);
					FOWSystemInterior.instance.mBuffer2[num7] = color2;
				}
			}
			Color32[] array = FOWSystemInterior.instance.mBuffer1;
			FOWSystemInterior.instance.mBuffer1 = FOWSystemInterior.instance.mBuffer2;
			FOWSystemInterior.instance.mBuffer2 = array;
		}
	}

	// Token: 0x060000DA RID: 218 RVA: 0x000111B0 File Offset: 0x0000F3B0
	public static byte[] GetRevealedBuffer()
	{
		if (FOWSystemInterior.instance == null || FOWSystemInterior.instance.mBuffer1 == null)
		{
			return null;
		}
		int num = FOWSystemInterior.instance.textureSize * FOWSystemInterior.instance.textureSize;
		byte[] array = new byte[num];
		for (int i = 0; i < num; i++)
		{
			array[i] = FOWSystemInterior.instance.mBuffer1[i].b;
		}
		return array;
	}

	// Token: 0x060000DB RID: 219 RVA: 0x0001121C File Offset: 0x0000F41C
	public static void SetRevealedBuffer(byte[] arr)
	{
		if (FOWSystemInterior.instance == null || arr == null)
		{
			return;
		}
		int num = FOWSystemInterior.instance.textureSize * FOWSystemInterior.instance.textureSize;
		if (arr.Length != num)
		{
			Debug.LogError("Buffer size mismatch. Fog is " + num.ToString() + ", but passed array is " + arr.Length.ToString());
			return;
		}
		if (FOWSystemInterior.instance.mBuffer0 == null)
		{
			FOWSystemInterior.instance.mBuffer0 = new Color32[num];
			FOWSystemInterior.instance.mBuffer1 = new Color32[num];
		}
		for (int i = 0; i < num; i++)
		{
			FOWSystemInterior.instance.mBuffer0[i].b = arr[i];
			FOWSystemInterior.instance.mBuffer1[i].b = arr[i];
		}
	}

	// Token: 0x060000DC RID: 220 RVA: 0x000112E8 File Offset: 0x0000F4E8
	public static void RevealAll()
	{
		if (FOWSystemInterior.instance == null)
		{
			return;
		}
		int num = FOWSystemInterior.instance.textureSize * FOWSystemInterior.instance.textureSize;
		if (FOWSystemInterior.instance.mBuffer0 == null)
		{
			FOWSystemInterior.instance.mBuffer0 = new Color32[num];
			FOWSystemInterior.instance.mBuffer1 = new Color32[num];
		}
		for (int i = 0; i < num; i++)
		{
			FOWSystemInterior.instance.mBuffer0[i].g = byte.MaxValue;
			FOWSystemInterior.instance.mBuffer1[i].g = byte.MaxValue;
		}
	}

	// Token: 0x060000DD RID: 221 RVA: 0x00011388 File Offset: 0x0000F588
	public static FOWSystemInterior.Revealer CreateRevealer()
	{
		FOWSystemInterior.Revealer revealer = new FOWSystemInterior.Revealer();
		revealer.isActive = false;
		List<FOWSystemInterior.Revealer> list = FOWSystemInterior.mAdded;
		lock (list)
		{
			FOWSystemInterior.mAdded.Add(revealer);
		}
		return revealer;
	}

	// Token: 0x060000DE RID: 222 RVA: 0x000113DC File Offset: 0x0000F5DC
	public static void DeleteRevealer(FOWSystemInterior.Revealer rev)
	{
		List<FOWSystemInterior.Revealer> list = FOWSystemInterior.mRemoved;
		lock (list)
		{
			FOWSystemInterior.mRemoved.Add(rev);
		}
	}

	// Token: 0x060000DF RID: 223 RVA: 0x00011420 File Offset: 0x0000F620
	public bool IsVisible(Vector3 pos)
	{
		FOWSystemInterior fowsystemInterior = FOWSystemInterior.instance;
		if (fowsystemInterior == null)
		{
			return true;
		}
		if (fowsystemInterior.mBuffer0 == null)
		{
			return false;
		}
		pos -= fowsystemInterior.mOrigin;
		float num = (float)fowsystemInterior.textureSize / (float)fowsystemInterior.worldSize;
		int num2 = Mathf.RoundToInt(pos.x * num);
		int num3 = Mathf.RoundToInt(pos.z * num);
		int num4 = Mathf.Clamp(num2, 0, fowsystemInterior.textureSize - 1);
		num3 = Mathf.Clamp(num3, 0, fowsystemInterior.textureSize - 1);
		int num5 = num4 + num3 * fowsystemInterior.textureSize;
		return fowsystemInterior.mBuffer1[num5].r > 0 || fowsystemInterior.mBuffer0[num5].r > 0;
	}

	// Token: 0x060000E0 RID: 224 RVA: 0x000114D4 File Offset: 0x0000F6D4
	public bool IsVisibleWorm(Vector3 pos)
	{
		FOWSystemInterior fowsystemInterior = FOWSystemInterior.instance;
		if (fowsystemInterior == null)
		{
			return true;
		}
		if (fowsystemInterior.mBuffer0 == null)
		{
			return false;
		}
		pos -= fowsystemInterior.mOrigin;
		float num = (float)fowsystemInterior.textureSize / (float)fowsystemInterior.worldSize;
		int num2 = Mathf.RoundToInt(pos.x * num);
		int num3 = Mathf.RoundToInt(pos.z * num);
		int num4 = Mathf.Clamp(num2, 0, fowsystemInterior.textureSize - 1);
		num3 = Mathf.Clamp(num3, 0, fowsystemInterior.textureSize - 1);
		int num5 = num4 + num3 * fowsystemInterior.textureSize;
		return fowsystemInterior.mBuffer1[num5].b > 0 || fowsystemInterior.mBuffer0[num5].b > 0;
	}

	// Token: 0x060000E1 RID: 225 RVA: 0x00011588 File Offset: 0x0000F788
	public static bool IsExplored(Vector3 pos)
	{
		FOWSystemInterior fowsystemInterior = FOWSystemInterior.instance;
		if (fowsystemInterior == null)
		{
			return true;
		}
		if (fowsystemInterior.mBuffer0 == null)
		{
			return false;
		}
		pos -= fowsystemInterior.mOrigin;
		float num = (float)fowsystemInterior.textureSize / (float)fowsystemInterior.worldSize;
		int num2 = Mathf.RoundToInt(pos.x * num);
		int num3 = Mathf.RoundToInt(pos.z * num);
		num2 = Mathf.Clamp(num2, 0, fowsystemInterior.textureSize - 1);
		num3 = Mathf.Clamp(num3, 0, fowsystemInterior.textureSize - 1);
		return fowsystemInterior.mBuffer0[num2 + num3 * fowsystemInterior.textureSize].g > 0;
	}

	// Token: 0x04000227 RID: 551
	[Header("Joseph")]
	public int worldSize = 256;

	// Token: 0x04000228 RID: 552
	public int index;

	// Token: 0x04000229 RID: 553
	public bool isActive;

	// Token: 0x0400022A RID: 554
	public static FOWSystemInterior.OnSaveOrLoad onSave;

	// Token: 0x0400022B RID: 555
	public static FOWSystemInterior.OnSaveOrLoad onLoad;

	// Token: 0x0400022C RID: 556
	public static FOWSystemInterior instance;

	// Token: 0x0400022D RID: 557
	public static bool readyToCreate = true;

	// Token: 0x0400022E RID: 558
	private static List<FOWSystemInterior.Revealer> mRevealers = new List<FOWSystemInterior.Revealer>();

	// Token: 0x0400022F RID: 559
	private static List<FOWSystemInterior.Revealer> mAdded = new List<FOWSystemInterior.Revealer>();

	// Token: 0x04000230 RID: 560
	private static List<FOWSystemInterior.Revealer> mRemoved = new List<FOWSystemInterior.Revealer>();

	// Token: 0x04000231 RID: 561
	protected int[,] mHeights;

	// Token: 0x04000232 RID: 562
	protected Transform mTrans;

	// Token: 0x04000233 RID: 563
	protected Vector3 mOrigin = Vector3.zero;

	// Token: 0x04000234 RID: 564
	protected Vector3 mSize = Vector3.one;

	// Token: 0x04000235 RID: 565
	protected Color32[] mBuffer0;

	// Token: 0x04000236 RID: 566
	protected Color32[] mBuffer1;

	// Token: 0x04000237 RID: 567
	protected Color32[] mBuffer2;

	// Token: 0x04000238 RID: 568
	protected int[] mInteriors;

	// Token: 0x04000239 RID: 569
	public Texture2D mTexture0;

	// Token: 0x0400023A RID: 570
	public Texture2D mTexture1;

	// Token: 0x0400023B RID: 571
	protected float mBlendFactor;

	// Token: 0x0400023C RID: 572
	protected float mNextUpdate;

	// Token: 0x0400023D RID: 573
	protected int mScreenHeight;

	// Token: 0x0400023E RID: 574
	protected FOWSystemInterior.State mState;

	// Token: 0x0400023F RID: 575
	private Thread mThread;

	// Token: 0x04000240 RID: 576
	private float mElapsed;

	// Token: 0x04000241 RID: 577
	[Header("Hannah")]
	public Color unexploredColor = new Color(0f, 0f, 0f, 1f);

	// Token: 0x04000242 RID: 578
	public Color exploredColor = new Color(0.28627f, 0.28627f, 0.28627f, 1f);

	// Token: 0x04000243 RID: 579
	public int textureSize = 256;

	// Token: 0x04000244 RID: 580
	private float updateFrequency = 0.01f;

	// Token: 0x04000245 RID: 581
	public float textureBlendTime = 0.5f;

	// Token: 0x04000246 RID: 582
	public int blurIterations = 2;

	// Token: 0x04000247 RID: 583
	public Vector2 heightRange = new Vector2(0f, 10f);

	// Token: 0x04000248 RID: 584
	private LayerMask raycastMask = 539165185;

	// Token: 0x04000249 RID: 585
	public float raycastRadius = 0.1f;

	// Token: 0x0400024A RID: 586
	public float margin = 0.4f;

	// Token: 0x0400024B RID: 587
	private bool debug;

	// Token: 0x0400024C RID: 588
	public int currentInterior;

	// Token: 0x0400024D RID: 589
	private GameObject interiors;

	// Token: 0x0400024E RID: 590
	private Transform interiorsTr;

	// Token: 0x0400024F RID: 591
	private List<Collider> interiorColliders = new List<Collider>();

	// Token: 0x04000250 RID: 592
	public Transform cameraTarget;

	// Token: 0x04000251 RID: 593
	private bool loaded;

	// Token: 0x04000252 RID: 594
	private Camera cam;

	// Token: 0x04000253 RID: 595
	public bool saveFow = true;

	// Token: 0x02000125 RID: 293
	public enum LOSChecks
	{
		// Token: 0x04002729 RID: 10025
		None,
		// Token: 0x0400272A RID: 10026
		OnlyOnce,
		// Token: 0x0400272B RID: 10027
		EveryUpdate
	}

	// Token: 0x02000126 RID: 294
	public class Revealer
	{
		// Token: 0x0400272C RID: 10028
		public bool isValid;

		// Token: 0x0400272D RID: 10029
		public bool isActive;

		// Token: 0x0400272E RID: 10030
		public FOWSystemInterior.LOSChecks los;

		// Token: 0x0400272F RID: 10031
		public Vector3 pos = Vector3.zero;

		// Token: 0x04002730 RID: 10032
		public float inner;

		// Token: 0x04002731 RID: 10033
		public float outer;

		// Token: 0x04002732 RID: 10034
		public bool[] cachedBuffer;

		// Token: 0x04002733 RID: 10035
		public int cachedSize;

		// Token: 0x04002734 RID: 10036
		public int cachedX;

		// Token: 0x04002735 RID: 10037
		public int cachedY;
	}

	// Token: 0x02000127 RID: 295
	public enum State
	{
		// Token: 0x04002737 RID: 10039
		Blending,
		// Token: 0x04002738 RID: 10040
		NeedUpdate,
		// Token: 0x04002739 RID: 10041
		UpdateTexture0,
		// Token: 0x0400273A RID: 10042
		UpdateTexture1
	}

	// Token: 0x02000128 RID: 296
	// (Invoke) Token: 0x060016F6 RID: 5878
	public delegate void OnSaveOrLoad();
}
