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

// Token: 0x02000010 RID: 16
[ExecuteInEditMode]
public class MoonlightSystem : MonoBehaviour
{
	// Token: 0x1700000B RID: 11
	// (get) Token: 0x060000EC RID: 236 RVA: 0x00011BA4 File Offset: 0x0000FDA4
	public Texture2D texture0
	{
		get
		{
			return this.mTexture0;
		}
	}

	// Token: 0x1700000C RID: 12
	// (get) Token: 0x060000ED RID: 237 RVA: 0x00011BAC File Offset: 0x0000FDAC
	public Texture2D texture1
	{
		get
		{
			return this.mTexture1;
		}
	}

	// Token: 0x1700000D RID: 13
	// (get) Token: 0x060000EE RID: 238 RVA: 0x00011BB4 File Offset: 0x0000FDB4
	public float blendFactor
	{
		get
		{
			return this.mBlendFactor;
		}
	}

	// Token: 0x060000EF RID: 239 RVA: 0x00011BBC File Offset: 0x0000FDBC
	private void Awake()
	{
		if (!this.interiors)
		{
			MoonlightSystem.instance = this;
			MoonlightSystem.otherInstance = this.interiorLightingSystem;
		}
	}

	// Token: 0x060000F0 RID: 240 RVA: 0x00011BD7 File Offset: 0x0000FDD7
	private IEnumerator Start()
	{
		if (this.interiors)
		{
			while (!MoonlightSystem.readyToCreateInteriors)
			{
				yield return null;
			}
		}
		else
		{
			while (!MoonlightSystem.readyToCreate)
			{
				yield return null;
			}
		}
		this.DoStart();
		yield break;
	}

	// Token: 0x060000F1 RID: 241 RVA: 0x00011BE6 File Offset: 0x0000FDE6
	private void OnEnable()
	{
		this.DoStart();
	}

	// Token: 0x060000F2 RID: 242 RVA: 0x00011BEE File Offset: 0x0000FDEE
	public void SetInstance(MoonlightSystem inst)
	{
		this.interiorLightingSystem = inst;
	}

	// Token: 0x060000F3 RID: 243 RVA: 0x00011BF8 File Offset: 0x0000FDF8
	public void DoStart()
	{
		if (!this.interiors)
		{
			MoonlightSystem.instance = this;
			MoonlightSystem.otherInstance = this.interiorLightingSystem;
		}
		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;
		this.mBuffer0 = new Color32[num];
		this.mBuffer1 = new Color32[num];
		this.mBuffer2 = new Color32[num];
		this.CreateGrid();
		if (MoonlightSystem.onLoad != null)
		{
			MoonlightSystem.onLoad();
		}
		this.UpdateBuffer();
		this.RebuildTextures();
		this.mNextUpdate = Time.time + this.updateFrequency;
		float num2 = 1f / (float)this.worldSize;
		float num3 = this.mTrans.position.x - (float)this.worldSize * 0.5f;
		float num4 = this.mTrans.position.z - (float)this.worldSize * 0.5f;
		Vector4 vector = new Vector4(-num3 * num2, -num4 * num2, num2, this.mBlendFactor);
		if (this.interiors)
		{
			Shader.SetGlobalVector("_MoonlightsParamsInteriors", vector);
			Shader.SetGlobalTexture("_MoonlightsTex0Interiors", this.mTexture0);
			Shader.SetGlobalTexture("_MoonlightsTex1Interiors", this.mTexture1);
		}
		else
		{
			Shader.SetGlobalVector("_MoonlightsParams", vector);
			Shader.SetGlobalTexture("_MoonlightsTex0", this.mTexture0);
			Shader.SetGlobalTexture("_MoonlightsTex1", this.mTexture1);
		}
		if (this.mThread != null)
		{
			this.mThread.Abort();
			while (this.mThread.IsAlive)
			{
				Thread.Sleep(1);
			}
			this.mThread = null;
		}
		if (this.interiors)
		{
			this.mThread = new Thread(new ThreadStart(this.ThreadUpdateInterior));
		}
		else
		{
			this.mThread = new Thread(new ThreadStart(this.ThreadUpdate));
		}
		this.mThread.Start();
	}

	// Token: 0x060000F4 RID: 244 RVA: 0x00011E57 File Offset: 0x00010057
	private void OnDisable()
	{
		if (this.interiors)
		{
			MoonlightSystem.mRevealersInteriors.Clear();
			return;
		}
		MoonlightSystem.mRevealers.Clear();
	}

	// Token: 0x060000F5 RID: 245 RVA: 0x00011E78 File Offset: 0x00010078
	public static void UpdateActiveState(bool interiors)
	{
		if (!interiors)
		{
			if (MoonlightSystem.otherInstance)
			{
				MoonlightSystem.otherInstance.active = false;
			}
			MoonlightSystem.instance.active = true;
			return;
		}
		if (MoonlightSystem.otherInstance)
		{
			MoonlightSystem.otherInstance.active = true;
		}
		MoonlightSystem.instance.active = false;
	}

	// Token: 0x060000F6 RID: 246 RVA: 0x00011ED0 File Offset: 0x000100D0
	private void OnDestroy()
	{
		if (this.mThread != null)
		{
			this.mThread.Abort();
			while (this.mThread.IsAlive)
			{
				Thread.Sleep(1);
			}
			this.mThread = null;
		}
		if (MoonlightSystem.onSave != null)
		{
			MoonlightSystem.onSave();
		}
		MoonlightSystem.onLoad = null;
		MoonlightSystem.onSave = null;
		MoonlightSystem.instance = null;
	}

	// Token: 0x060000F7 RID: 247 RVA: 0x00011F30 File Offset: 0x00010130
	private void Update()
	{
		if (this.mTrans == null)
		{
			return;
		}
		if (!this.active)
		{
			return;
		}
		if (this.textureBlendTime > 0f)
		{
			this.mBlendFactor = Mathf.Clamp01(this.mBlendFactor + Time.deltaTime / this.textureBlendTime);
		}
		else
		{
			this.mBlendFactor = 1f;
		}
		Shader.SetGlobalVector("_ShadowColor", this.shadowColor);
		if (this.mState == MoonlightSystem.State.Blending)
		{
			float time = Time.time;
			if (this.mNextUpdate < time)
			{
				this.mNextUpdate = time + this.updateFrequency;
				this.mState = MoonlightSystem.State.NeedUpdate;
			}
		}
		else if (this.mState != MoonlightSystem.State.NeedUpdate)
		{
			if (this.mScreenHeight != Screen.height || this.mTexture0 == null)
			{
				this.RebuildTextures();
			}
			else if (this.mState == MoonlightSystem.State.UpdateTexture0)
			{
				this.mTexture0.SetPixels32(this.mBuffer0);
				this.mTexture0.Apply();
				this.mState = MoonlightSystem.State.UpdateTexture1;
				this.mBlendFactor = 0f;
			}
			else if (this.mState == MoonlightSystem.State.UpdateTexture1)
			{
				this.mTexture1.SetPixels32(this.mBuffer1);
				this.mTexture1.Apply();
				this.mState = MoonlightSystem.State.Blending;
			}
		}
		else if (this.mState == MoonlightSystem.State.NeedUpdate && !Application.isPlaying)
		{
			this.UpdateBuffer();
			this.mState = MoonlightSystem.State.UpdateTexture0;
		}
		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);
		if (this.interiors)
		{
			Shader.SetGlobalVector("_MoonlightsParamsInteriors", vector);
			Shader.SetGlobalTexture("_MoonlightsTex0Interiors", this.mTexture0);
			Shader.SetGlobalTexture("_MoonlightsTex1Interiors", this.mTexture1);
		}
		else
		{
			Shader.SetGlobalVector("_MoonlightsParams", vector);
			Shader.SetGlobalTexture("_MoonlightsTex0", this.mTexture0);
			Shader.SetGlobalTexture("_MoonlightsTex1", this.mTexture1);
		}
		if (this.joseph)
		{
			if (this.interiors)
			{
				Shader.SetGlobalTexture("_MoonlightsInteriors", this.mTexture1);
				return;
			}
			Shader.SetGlobalTexture("_Moonlights", this.mTexture1);
		}
	}

	// Token: 0x060000F8 RID: 248 RVA: 0x00012184 File Offset: 0x00010384
	private void RebuildTextures()
	{
		this.mScreenHeight = Screen.height;
		if (Application.isPlaying)
		{
			if (this.mTexture0 != null)
			{
				Object.Destroy(this.mTexture0);
			}
			if (this.mTexture1 != null)
			{
				Object.Destroy(this.mTexture1);
			}
		}
		else
		{
			if (this.mTexture0 != null)
			{
				Object.DestroyImmediate(this.mTexture0);
			}
			if (this.mTexture1 != null)
			{
				Object.DestroyImmediate(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 = MoonlightSystem.State.Blending;
	}

	// Token: 0x060000F9 RID: 249 RVA: 0x000122A0 File Offset: 0x000104A0
	private void ThreadUpdate()
	{
		Stopwatch stopwatch = new Stopwatch();
		for (;;)
		{
			if (this.mState == MoonlightSystem.State.NeedUpdate && this.active)
			{
				stopwatch.Reset();
				stopwatch.Start();
				this.UpdateBuffer();
				stopwatch.Stop();
				if (this.debug)
				{
					Debug.Log(stopwatch.ElapsedMilliseconds);
				}
				this.mElapsed = 0.001f * (float)stopwatch.ElapsedMilliseconds;
				this.mState = MoonlightSystem.State.UpdateTexture0;
			}
			if (this.active)
			{
				Thread.Sleep(1);
			}
		}
	}

	// Token: 0x060000FA RID: 250 RVA: 0x00012320 File Offset: 0x00010520
	private void ThreadUpdateInterior()
	{
		Stopwatch stopwatch = new Stopwatch();
		for (;;)
		{
			if (this.mState == MoonlightSystem.State.NeedUpdate && this.active)
			{
				stopwatch.Reset();
				stopwatch.Start();
				this.UpdateBuffer();
				stopwatch.Stop();
				if (this.debug)
				{
					Debug.Log(stopwatch.ElapsedMilliseconds);
				}
				this.mElapsed = 0.001f * (float)stopwatch.ElapsedMilliseconds;
				this.mState = MoonlightSystem.State.UpdateTexture0;
			}
			if (this.active)
			{
				Thread.Sleep(1);
			}
		}
	}

	// Token: 0x060000FB RID: 251 RVA: 0x000123A0 File Offset: 0x000105A0
	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: 0x060000FC RID: 252 RVA: 0x00012424 File Offset: 0x00010624
	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: 0x060000FD RID: 253 RVA: 0x000124F3 File Offset: 0x000106F3
	public int WorldToGridHeight(float height)
	{
		return Mathf.Clamp(Mathf.RoundToInt(height / this.mSize.y * 255f), 0, 255);
	}

	// Token: 0x060000FE RID: 254 RVA: 0x00012518 File Offset: 0x00010718
	public virtual void CreateGrid()
	{
		Vector3 vector = this.mOrigin;
		vector.y += this.mSize.y;
		float num = (float)this.worldSize / (float)this.textureSize;
		bool flag = this.raycastRadius > 0f;
		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;
				if (this.raycastMask == 0)
				{
					goto IL_0135;
				}
				if (flag)
				{
					RaycastHit raycastHit;
					if (!Physics.SphereCast(new Ray(vector, Vector3.down), this.raycastRadius, out raycastHit, this.mSize.y, this.raycastMask))
					{
						goto IL_0135;
					}
					this.mHeights[j, i] = this.WorldToGridHeight(vector.y - raycastHit.distance - this.raycastRadius);
				}
				else
				{
					RaycastHit raycastHit;
					if (!Physics.Raycast(new Ray(vector, Vector3.down), out raycastHit, this.mSize.y, this.raycastMask))
					{
						goto IL_0135;
					}
					this.mHeights[j, i] = this.WorldToGridHeight(vector.y - raycastHit.distance);
				}
				IL_0144:
				j++;
				continue;
				IL_0135:
				this.mHeights[j, i] = 0;
				goto IL_0144;
			}
		}
	}

	// Token: 0x060000FF RID: 255 RVA: 0x0001268C File Offset: 0x0001088C
	private void UpdateBuffer()
	{
		if (this.interiors)
		{
			if (MoonlightSystem.mAddedInteriors.Count > 0)
			{
				List<MoonlightSystem.Revealer> list = MoonlightSystem.mAddedInteriors;
				lock (list)
				{
					while (MoonlightSystem.mAddedInteriors.Count > 0)
					{
						int num = MoonlightSystem.mAddedInteriors.Count - 1;
						MoonlightSystem.mRevealersInteriors.Add(MoonlightSystem.mAddedInteriors[num]);
						MoonlightSystem.mAddedInteriors.RemoveAt(num);
					}
				}
			}
			if (MoonlightSystem.mRemovedInteriors.Count > 0)
			{
				List<MoonlightSystem.Revealer> list = MoonlightSystem.mRemovedInteriors;
				lock (list)
				{
					while (MoonlightSystem.mRemovedInteriors.Count > 0)
					{
						int num2 = MoonlightSystem.mRemovedInteriors.Count - 1;
						MoonlightSystem.mRevealersInteriors.Remove(MoonlightSystem.mRemovedInteriors[num2]);
						MoonlightSystem.mRemovedInteriors.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;
				this.mBuffer1[i].g = 0;
				this.mBuffer1[i].b = 0;
				i++;
			}
			float num5 = (float)this.textureSize / (float)this.worldSize;
			int j = 0;
			int count = MoonlightSystem.mRevealersInteriors.Count;
			while (j < count)
			{
				if (j < MoonlightSystem.mRevealersInteriors.Count)
				{
					MoonlightSystem.Revealer revealer = MoonlightSystem.mRevealersInteriors[j];
					if (revealer.isActive)
					{
						MoonlightSystem.Revealer revealer2 = revealer;
						lock (revealer2)
						{
							if (revealer.los == MoonlightSystem.LOSChecks.None)
							{
								this.RevealUsingRadius(revealer, num5);
							}
							else if (revealer.los == MoonlightSystem.LOSChecks.OnlyOnce)
							{
								this.RevealUsingCache(revealer, num5);
							}
						}
					}
				}
				j++;
			}
			int k = 0;
			int count2 = MoonlightSystem.mRevealersInteriors.Count;
			while (k < count2)
			{
				if (k < MoonlightSystem.mRevealersInteriors.Count)
				{
					MoonlightSystem.Revealer revealer3 = MoonlightSystem.mRevealersInteriors[k];
					if (revealer3.isActive)
					{
						MoonlightSystem.Revealer revealer2 = revealer3;
						lock (revealer2)
						{
							if (revealer3.los != MoonlightSystem.LOSChecks.None)
							{
								if (revealer3.los != MoonlightSystem.LOSChecks.OnlyOnce)
								{
									this.RevealUsingLOS(revealer3, num5);
								}
							}
						}
					}
				}
				k++;
			}
			for (int l = 0; l < this.blurIterations; l++)
			{
				this.BlurVisibility();
			}
			this.RevealVisible();
			return;
		}
		if (MoonlightSystem.mAdded.Count > 0)
		{
			List<MoonlightSystem.Revealer> list = MoonlightSystem.mAdded;
			lock (list)
			{
				while (MoonlightSystem.mAdded.Count > 0)
				{
					int num6 = MoonlightSystem.mAdded.Count - 1;
					MoonlightSystem.mRevealers.Add(MoonlightSystem.mAdded[num6]);
					MoonlightSystem.mAdded.RemoveAt(num6);
				}
			}
		}
		if (MoonlightSystem.mRemoved.Count > 0)
		{
			List<MoonlightSystem.Revealer> list = MoonlightSystem.mRemoved;
			lock (list)
			{
				while (MoonlightSystem.mRemoved.Count > 0)
				{
					int num7 = MoonlightSystem.mRemoved.Count - 1;
					MoonlightSystem.mRevealers.Remove(MoonlightSystem.mRemoved[num7]);
					MoonlightSystem.mRemoved.RemoveAt(num7);
				}
			}
		}
		float num8 = ((this.textureBlendTime > 0f) ? Mathf.Clamp01(this.mBlendFactor + this.mElapsed / this.textureBlendTime) : 1f);
		int m = 0;
		int num9 = this.mBuffer0.Length;
		while (m < num9)
		{
			this.mBuffer0[m] = Color32.Lerp(this.mBuffer0[m], this.mBuffer1[m], num8);
			this.mBuffer1[m].r = 0;
			this.mBuffer1[m].g = 0;
			this.mBuffer1[m].b = 0;
			m++;
		}
		float num10 = (float)this.textureSize / (float)this.worldSize;
		int n = 0;
		int count3 = MoonlightSystem.mRevealers.Count;
		while (n < count3)
		{
			MoonlightSystem.Revealer revealer4 = MoonlightSystem.mRevealers[n];
			if (revealer4.isActive)
			{
				MoonlightSystem.Revealer revealer2 = revealer4;
				lock (revealer2)
				{
					if (revealer4.los == MoonlightSystem.LOSChecks.None)
					{
						this.RevealUsingRadius(revealer4, num10);
					}
					else if (revealer4.los == MoonlightSystem.LOSChecks.OnlyOnce)
					{
						this.RevealUsingCache(revealer4, num10);
					}
				}
			}
			n++;
		}
		int num11 = 0;
		int count4 = MoonlightSystem.mRevealers.Count;
		while (num11 < count4)
		{
			MoonlightSystem.Revealer revealer5 = MoonlightSystem.mRevealers[num11];
			if (revealer5.isActive)
			{
				MoonlightSystem.Revealer revealer2 = revealer5;
				lock (revealer2)
				{
					if (revealer5.los != MoonlightSystem.LOSChecks.None)
					{
						if (revealer5.los != MoonlightSystem.LOSChecks.OnlyOnce)
						{
							this.RevealUsingLOS(revealer5, num10);
						}
					}
				}
			}
			num11++;
		}
		for (int num12 = 0; num12 < this.blurIterations; num12++)
		{
			this.BlurVisibility();
		}
	}

	// Token: 0x06000100 RID: 256 RVA: 0x00012C58 File Offset: 0x00010E58
	private void RevealUsingRadius(MoonlightSystem.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;
							this.mBuffer1[j + num9].g = byte.MaxValue;
							this.mBuffer1[j + num9].b = byte.MaxValue;
						}
					}
				}
			}
		}
	}

	// Token: 0x06000101 RID: 257 RVA: 0x00012DE0 File Offset: 0x00010FE0
	private void RevealUsingLOS(MoonlightSystem.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 lightColor = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
		lightColor = r.lightColor;
		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;
						Color32 color = this.mBuffer1[num15];
						Color32 color2 = color;
						if (color.r > 0 || color.g > 0 || color.b > 0)
						{
							int num16 = (int)(lightColor.r + color2.r);
							int num17 = (int)(lightColor.g + color2.g);
							int num18 = (int)(lightColor.b + color2.b);
							color.r = (byte)(num16 / 2);
							color.g = (byte)(num17 / 2);
							color.b = (byte)(num18 / 2);
							color2 = color;
						}
						else
						{
							color2 = lightColor;
						}
						if (num14 < num7 || (num5 == j && num6 == i))
						{
							this.mBuffer1[num15] = color2;
						}
						else if (num14 < num8)
						{
							Vector2 vector2 = new Vector2((float)num12, (float)num13);
							vector2.Normalize();
							vector2 *= r.inner;
							int num19 = num5 + Mathf.RoundToInt(vector2.x);
							int num20 = num6 + Mathf.RoundToInt(vector2.y);
							if (num19 > -1 && num19 < this.textureSize && num20 > -1 && num20 < this.textureSize && this.IsVisible(num19, num20, j, i, Mathf.Sqrt((float)num14), num9, num10))
							{
								this.mBuffer1[num15] = color2;
							}
						}
					}
				}
			}
		}
	}

	// Token: 0x06000102 RID: 258 RVA: 0x0001313C File Offset: 0x0001133C
	private void RevealUsingCache(MoonlightSystem.Revealer r, float worldToTex)
	{
		if (!r.isValid)
		{
			this.RevealIntoCache(r, worldToTex);
		}
		Color32 lightColor = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
		lightColor = r.lightColor;
		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])
						{
							this.mBuffer1[j + num3] = lightColor;
						}
					}
					j++;
				}
			}
			i++;
		}
	}

	// Token: 0x06000103 RID: 259 RVA: 0x00013224 File Offset: 0x00011424
	private void RevealIntoCache(MoonlightSystem.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: 0x06000104 RID: 260 RVA: 0x00013508 File Offset: 0x00011708
	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;
				num7 += (int)this.mBuffer1[num4 + num2].g;
				num7 += (int)this.mBuffer1[num5 + num2].g;
				num7 += (int)this.mBuffer1[num4 + num3].g;
				num7 += (int)this.mBuffer1[num5 + num3].g;
				color.g = (byte)(num7 / 9);
				num7 = (int)this.mBuffer1[num6].b;
				num7 += (int)this.mBuffer1[num4 + num].b;
				num7 += (int)this.mBuffer1[num5 + num].b;
				num7 += (int)this.mBuffer1[j + num2].b;
				num7 += (int)this.mBuffer1[j + num3].b;
				num7 += (int)this.mBuffer1[num4 + num2].b;
				num7 += (int)this.mBuffer1[num5 + num2].b;
				num7 += (int)this.mBuffer1[num4 + num3].b;
				num7 += (int)this.mBuffer1[num5 + num3].b;
				color.b = (byte)(num7 / 9);
				this.mBuffer2[num6] = color;
			}
		}
		Color32[] array = this.mBuffer1;
		this.mBuffer1 = this.mBuffer2;
		this.mBuffer2 = array;
	}

	// Token: 0x06000105 RID: 261 RVA: 0x000138B0 File Offset: 0x00011AB0
	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.a < color.r)
				{
					color.a = color.r;
					this.mBuffer1[num2] = color;
				}
			}
		}
	}

	// Token: 0x06000106 RID: 262 RVA: 0x00013928 File Offset: 0x00011B28
	public static byte[] GetRevealedBuffer()
	{
		if (MoonlightSystem.instance == null || MoonlightSystem.instance.mBuffer1 == null)
		{
			return null;
		}
		int num = MoonlightSystem.instance.textureSize * MoonlightSystem.instance.textureSize;
		byte[] array = new byte[num];
		for (int i = 0; i < num; i++)
		{
			array[i] = MoonlightSystem.instance.mBuffer1[i].g;
		}
		return array;
	}

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

	// Token: 0x06000108 RID: 264 RVA: 0x00013A60 File Offset: 0x00011C60
	public static void RevealAll()
	{
		if (MoonlightSystem.instance == null)
		{
			return;
		}
		int num = MoonlightSystem.instance.textureSize * MoonlightSystem.instance.textureSize;
		if (MoonlightSystem.instance.mBuffer0 == null)
		{
			MoonlightSystem.instance.mBuffer0 = new Color32[num];
			MoonlightSystem.instance.mBuffer1 = new Color32[num];
		}
		for (int i = 0; i < num; i++)
		{
			MoonlightSystem.instance.mBuffer0[i].a = byte.MaxValue;
			MoonlightSystem.instance.mBuffer1[i].a = byte.MaxValue;
		}
	}

	// Token: 0x06000109 RID: 265 RVA: 0x00013B00 File Offset: 0x00011D00
	public static MoonlightSystem.Revealer CreateRevealer()
	{
		MoonlightSystem.Revealer revealer = new MoonlightSystem.Revealer();
		revealer.isActive = false;
		List<MoonlightSystem.Revealer> list = MoonlightSystem.mAdded;
		lock (list)
		{
			MoonlightSystem.mAdded.Add(revealer);
		}
		return revealer;
	}

	// Token: 0x0600010A RID: 266 RVA: 0x00013B54 File Offset: 0x00011D54
	public static void DeleteRevealer(MoonlightSystem.Revealer rev)
	{
		List<MoonlightSystem.Revealer> list = MoonlightSystem.mRemoved;
		lock (list)
		{
			MoonlightSystem.mRemoved.Add(rev);
		}
	}

	// Token: 0x0600010B RID: 267 RVA: 0x00013B98 File Offset: 0x00011D98
	public MoonlightSystem.Revealer CreateInteriorRevealer()
	{
		MoonlightSystem.Revealer revealer = new MoonlightSystem.Revealer();
		revealer.isActive = false;
		List<MoonlightSystem.Revealer> list = MoonlightSystem.mAddedInteriors;
		lock (list)
		{
			MoonlightSystem.mAddedInteriors.Add(revealer);
		}
		return revealer;
	}

	// Token: 0x0600010C RID: 268 RVA: 0x00013BEC File Offset: 0x00011DEC
	public void DeleteInteriorRevealer(MoonlightSystem.Revealer rev)
	{
		List<MoonlightSystem.Revealer> list = MoonlightSystem.mRemovedInteriors;
		lock (list)
		{
			MoonlightSystem.mRemovedInteriors.Add(rev);
		}
	}

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

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

	// Token: 0x0400025E RID: 606
	public int worldSize = 256;

	// Token: 0x0400025F RID: 607
	public bool interiors;

	// Token: 0x04000260 RID: 608
	public static MoonlightSystem otherInstance;

	// Token: 0x04000261 RID: 609
	public MoonlightSystem interiorLightingSystem;

	// Token: 0x04000262 RID: 610
	private Color shadowColor = Color.black;

	// Token: 0x04000263 RID: 611
	public bool active = true;

	// Token: 0x04000264 RID: 612
	public bool joseph;

	// Token: 0x04000265 RID: 613
	public static MoonlightSystem.OnSaveOrLoad onSave;

	// Token: 0x04000266 RID: 614
	public static MoonlightSystem.OnSaveOrLoad onLoad;

	// Token: 0x04000267 RID: 615
	public static MoonlightSystem instance;

	// Token: 0x04000268 RID: 616
	public static bool readyToCreate = true;

	// Token: 0x04000269 RID: 617
	public static bool readyToCreateInteriors = true;

	// Token: 0x0400026A RID: 618
	private static List<MoonlightSystem.Revealer> mRevealers = new List<MoonlightSystem.Revealer>();

	// Token: 0x0400026B RID: 619
	private static List<MoonlightSystem.Revealer> mAdded = new List<MoonlightSystem.Revealer>();

	// Token: 0x0400026C RID: 620
	private static List<MoonlightSystem.Revealer> mRemoved = new List<MoonlightSystem.Revealer>();

	// Token: 0x0400026D RID: 621
	private static List<MoonlightSystem.Revealer> mRevealersInteriors = new List<MoonlightSystem.Revealer>();

	// Token: 0x0400026E RID: 622
	private static List<MoonlightSystem.Revealer> mAddedInteriors = new List<MoonlightSystem.Revealer>();

	// Token: 0x0400026F RID: 623
	private static List<MoonlightSystem.Revealer> mRemovedInteriors = new List<MoonlightSystem.Revealer>();

	// Token: 0x04000270 RID: 624
	protected int[,] mHeights;

	// Token: 0x04000271 RID: 625
	protected Transform mTrans;

	// Token: 0x04000272 RID: 626
	protected Vector3 mOrigin = Vector3.zero;

	// Token: 0x04000273 RID: 627
	protected Vector3 mSize = Vector3.one;

	// Token: 0x04000274 RID: 628
	protected Color32[] mBuffer0;

	// Token: 0x04000275 RID: 629
	protected Color32[] mBuffer1;

	// Token: 0x04000276 RID: 630
	protected Color32[] mBuffer2;

	// Token: 0x04000277 RID: 631
	protected int[] bufferAmount;

	// Token: 0x04000278 RID: 632
	public Texture2D mTexture0;

	// Token: 0x04000279 RID: 633
	public Texture2D mTexture1;

	// Token: 0x0400027A RID: 634
	protected float mBlendFactor;

	// Token: 0x0400027B RID: 635
	protected float mNextUpdate;

	// Token: 0x0400027C RID: 636
	protected int mScreenHeight;

	// Token: 0x0400027D RID: 637
	protected MoonlightSystem.State mState;

	// Token: 0x0400027E RID: 638
	private Thread mThread;

	// Token: 0x0400027F RID: 639
	private float mElapsed;

	// Token: 0x04000280 RID: 640
	private Color unexploredColor = new Color(0f, 0f, 0f, 1f);

	// Token: 0x04000281 RID: 641
	private Color exploredColor = new Color(0.28627f, 0.28627f, 0.28627f, 1f);

	// Token: 0x04000282 RID: 642
	public int textureSize = 128;

	// Token: 0x04000283 RID: 643
	public float updateFrequency = 0.05f;

	// Token: 0x04000284 RID: 644
	public float textureBlendTime = 0.5f;

	// Token: 0x04000285 RID: 645
	public int blurIterations = 2;

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

	// Token: 0x04000287 RID: 647
	private LayerMask raycastMask = 529;

	// Token: 0x04000288 RID: 648
	public float raycastRadius = 0.1f;

	// Token: 0x04000289 RID: 649
	public float margin = 0.4f;

	// Token: 0x0400028A RID: 650
	private bool debug;

	// Token: 0x0200012A RID: 298
	public enum LOSChecks
	{
		// Token: 0x0400273F RID: 10047
		None,
		// Token: 0x04002740 RID: 10048
		OnlyOnce,
		// Token: 0x04002741 RID: 10049
		EveryUpdate
	}

	// Token: 0x0200012B RID: 299
	public class Revealer
	{
		// Token: 0x04002742 RID: 10050
		public bool isValid;

		// Token: 0x04002743 RID: 10051
		public bool isActive;

		// Token: 0x04002744 RID: 10052
		public MoonlightSystem.LOSChecks los;

		// Token: 0x04002745 RID: 10053
		public Vector3 pos = Vector3.zero;

		// Token: 0x04002746 RID: 10054
		public float inner;

		// Token: 0x04002747 RID: 10055
		public float outer;

		// Token: 0x04002748 RID: 10056
		public bool[] cachedBuffer;

		// Token: 0x04002749 RID: 10057
		public int cachedSize;

		// Token: 0x0400274A RID: 10058
		public int cachedX;

		// Token: 0x0400274B RID: 10059
		public int cachedY;

		// Token: 0x0400274C RID: 10060
		public Color32 lightColor;

		// Token: 0x0400274D RID: 10061
		public Color32 doubleLightColor;
	}

	// Token: 0x0200012C RID: 300
	public enum State
	{
		// Token: 0x0400274F RID: 10063
		Blending,
		// Token: 0x04002750 RID: 10064
		NeedUpdate,
		// Token: 0x04002751 RID: 10065
		UpdateTexture0,
		// Token: 0x04002752 RID: 10066
		UpdateTexture1
	}

	// Token: 0x0200012D RID: 301
	// (Invoke) Token: 0x06001701 RID: 5889
	public delegate void OnSaveOrLoad();
}
