using System;
using System.Collections;
using System.Collections.Generic;
using Pathfinding;
using UnityEngine;

// Token: 0x0200002B RID: 43
public class EnvironmentNodes : MonoBehaviour
{
	// Token: 0x060006DD RID: 1757 RVA: 0x00093EE4 File Offset: 0x000920E4
	private void Start()
	{
		this.tr = base.transform;
		base.gameObject.layer = 1;
		if (this.tr.childCount > 0)
		{
			this.tr.GetChild(0).gameObject.SetActive(false);
		}
		this.sphere = base.gameObject.AddComponent<SphereCollider>();
		this.sphere.radius = (float)this.radius;
		this.sphere.isTrigger = true;
		base.gameObject.AddComponent<Rigidbody>().isKinematic = true;
		if (this.tr.parent)
		{
			this.character = this.tr.parent.gameObject.GetComponent<Character>();
		}
		if (this.character)
		{
			this.isDynamic = true;
		}
		this.prevPosition = new Vector3(1000000f, 100000f, 0f);
		this.count = Time.timeSinceLevelLoad;
		this.lerpingPosition = this.tr.position;
		this.thisObject = base.gameObject;
		if (this.needsNodes)
		{
			this.StartFindNodes();
			return;
		}
		if (this.isDynamic)
		{
			this.mainVisual = Links.x.cellar.GetPooledGameObject(64);
			this.mainVisual.SetActive(true);
		}
	}

	// Token: 0x060006DE RID: 1758 RVA: 0x0009402C File Offset: 0x0009222C
	private void OnTriggerEnter(Collider other)
	{
		GameObject gameObject = other.gameObject;
		if (gameObject && gameObject.layer == 1)
		{
			Character component = gameObject.GetComponent<Character>();
			if (component && !component.ghostVanishing && component.ghost)
			{
				component.Flee(this.targetNode, false, Links.x.gameplay.seconds, 50f);
			}
		}
	}

	// Token: 0x060006DF RID: 1759 RVA: 0x00094094 File Offset: 0x00092294
	public void Remove()
	{
		if (this.corout != null)
		{
			base.StopCoroutine(this.corout);
		}
		this.stop = true;
		if (this.updating)
		{
			for (int i = 0; i < this.tempNodes.Count; i++)
			{
				if (this.tempNodes[i] != null)
				{
					this.tempNodes[i].environment = this.tempNodes[i].originalEnvironment;
				}
			}
		}
		for (int j = 0; j < this.pickedGroundNodes.Count; j++)
		{
			this.pickedGroundNodes[j].environment = this.pickedGroundNodes[j].originalEnvironment;
		}
		for (int k = 0; k < this.fx.Count; k++)
		{
			if (this.fx[k] && this.fx[k] != this.thisObject)
			{
				Links.x.cellar.ReturnPooledGameObject(64, this.fx[k]);
			}
		}
		if (this.mainVisual)
		{
			this.mainVisual.transform.localScale = new Vector3(1f, 1f, 1f);
			Links.x.cellar.ReturnPooledGameObject(64, this.mainVisual);
		}
	}

	// Token: 0x060006E0 RID: 1760 RVA: 0x000941E9 File Offset: 0x000923E9
	public void StartFindNodes()
	{
		this.corout = this.FindNodes();
		base.StartCoroutine(this.corout);
	}

	// Token: 0x060006E1 RID: 1761 RVA: 0x00094204 File Offset: 0x00092404
	private void Update()
	{
		float num = 5f;
		this.lerpingPosition = Vector3.Lerp(this.lerpingPosition, this.tr.position, Time.deltaTime * num);
		if (this.mainVisual)
		{
			this.mainVisual.transform.position = this.lerpingPosition + new Vector3(0f, 2f, 0f);
			if (!this.mainVisual.activeSelf)
			{
				this.mainVisual.SetActive(true);
			}
		}
	}

	// Token: 0x060006E2 RID: 1762 RVA: 0x0009428F File Offset: 0x0009248F
	private IEnumerator FindNodes()
	{
		if (!Links.x.gaia.sceneLoaded)
		{
			this.lerpingPosition = this.tr.position;
		}
		if ((this.lerpingPosition - this.prevPosition).sqrMagnitude > 0.010000001f || Time.timeSinceLevelLoad < this.count + 2f)
		{
			int visualCount = 0;
			this.nodeConstraint.constrainWalkability = false;
			this.nodeConstraint.walkable = true;
			this.nodeConstraint.constrainTags = false;
			this.nodeConstraint.checkCircleID = 0;
			this.nodeConstraint.passID = 0;
			this.nodeConstraint.passID2 = 0;
			this.nodeConstraint.checkConnections = 0;
			this.nodeConstraint.constrainPenalty = 0;
			this.nodeConstraint.constrainToArea = -1;
			this.nodeConstraint.constrainToEnvironment = -1;
			if (Links.x.gaia.pathfindingReady && AstarPath.active != null)
			{
				this.targetNode = AstarPath.active.GetNearest(this.lerpingPosition, this.nodeConstraint).node;
				this.updating = true;
				if (this.isDynamic)
				{
					for (int i = 0; i < this.pickedGroundNodes.Count; i++)
					{
						this.tempNodes.Add(this.pickedGroundNodes[i]);
						this.tempNodesContinue.Add(false);
					}
				}
				this.pickedGroundNodes.Clear();
				this.newNodes.Clear();
				if (this.targetNode != null && Links.x.gaia.pathfindingReady)
				{
					this.count = Time.timeSinceLevelLoad;
					float num = (float)this.radius;
					ConstantPath constPath = null;
					float max = (num * Records.x.nodeSize + 0.1f) * (num * Records.x.nodeSize + 0.1f);
					constPath = ConstantPath.ConstructFast(this.targetNode, Records.x.GetConstantPathRadius(this.radius), null);
					constPath.nnConstraint = this.nodeConstraint;
					AstarPath.StartPath(constPath, false);
					yield return base.StartCoroutine(constPath.WaitForPath());
					constPath.Claim(this);
					this.allNodes = constPath.allNodes;
					int num2 = this.allNodes.Count;
					int num3 = 0;
					for (int j = 0; j < num2; j++)
					{
						if (((Vector3)this.allNodes[j].position - (Vector3)this.targetNode.position).sqrMagnitude < max && !this.stop)
						{
							GraphNode graphNode = this.allNodes[j];
							int num4 = this.tempNodes.IndexOf(graphNode);
							if (num4 > -1)
							{
								this.tempNodesContinue[num4] = true;
							}
							else
							{
								this.newNodes.Add(graphNode);
							}
							num3++;
						}
					}
					constPath.Release(this, false);
					Vector3 zero = Vector3.zero;
					num2 = this.tempNodes.Count;
					for (int k = 0; k < num2; k++)
					{
						if (this.tempNodesContinue[k])
						{
							if (this.tempNodes[k].originalEnvironment == 0)
							{
								this.tempNodes[k].environment = 1;
							}
						}
						else if (!this.stop)
						{
							this.tempNodes[k].environment = this.tempNodes[k].originalEnvironment;
							this.tempNodes[k] = null;
						}
					}
					this.tempNodes.RemoveAll((GraphNode item) => item == null);
					num2 = this.newNodes.Count;
					for (int l = num2 - 1; l >= 0; l--)
					{
						if (!this.stop)
						{
							GraphNode graphNode = this.newNodes[l];
							if (graphNode.originalEnvironment == 0 && graphNode.environment == 0)
							{
								graphNode.environment = 1;
							}
							if (visualCount == 0)
							{
								int num5 = visualCount;
								visualCount = num5 + 1;
							}
							this.tempNodes.Add(graphNode);
						}
					}
					num2 = this.tempNodes.Count;
					bool flag = false;
					for (int m = 0; m < num2; m++)
					{
						if (!this.stop)
						{
							GraphNode graphNode = this.tempNodes[m];
							this.pickedGroundNodes.Add(graphNode);
							if (graphNode.ID > 0 && !Records.x.pocketPause)
							{
								if (!flag)
								{
									this.gos = Links.x.diorama.characters;
									flag = true;
								}
								int num6 = this.gos.Count;
								for (int n = 0; n < num6; n++)
								{
									Character character = this.gos[n];
									if (character && character.ghost && (character.nodeStationaryID == graphNode.ID || character.nodeMovingID == graphNode.ID) && !character.dead && !character.stunned && character.IsSentient())
									{
										character.SetRotation(Quaternion.LookRotation(this.character.tr.position - character.tr.position), false, true);
										character.GhostFlee();
									}
								}
							}
						}
					}
					this.updating = false;
					this.tempNodes.Clear();
					this.tempNodesContinue.Clear();
					this.prevPosition = this.lerpingPosition;
					if (!this.mainVisual && this.isDynamic)
					{
						this.mainVisual = Links.x.cellar.GetPooledGameObject(64);
						this.mainVisual.SetActive(true);
					}
					if (!this.isDynamic && this.pickedGroundNodes.Count > 0 && this.nonDynamicCount > 5)
					{
						this.corout = null;
					}
					this.nonDynamicCount++;
					constPath = null;
				}
			}
		}
		this.corout = null;
		if ((this.isDynamic || (!this.isDynamic && this.nonDynamicCount <= 5)) && !this.stop)
		{
			float max = Time.timeSinceLevelLoad;
			while (Time.timeSinceLevelLoad < max + 0.01f)
			{
				yield return null;
			}
			this.StartFindNodes();
		}
		yield break;
	}

	// Token: 0x040009E8 RID: 2536
	public bool isDynamic;

	// Token: 0x040009E9 RID: 2537
	public bool incense = true;

	// Token: 0x040009EA RID: 2538
	public int radius = 3;

	// Token: 0x040009EB RID: 2539
	public bool needsNodes = true;

	// Token: 0x040009EC RID: 2540
	private Character character;

	// Token: 0x040009ED RID: 2541
	public List<GraphNode> pickedGroundNodes = new List<GraphNode>();

	// Token: 0x040009EE RID: 2542
	private List<GraphNode> tempNodes = new List<GraphNode>();

	// Token: 0x040009EF RID: 2543
	private List<GraphNode> newNodes = new List<GraphNode>();

	// Token: 0x040009F0 RID: 2544
	private List<bool> tempNodesContinue = new List<bool>();

	// Token: 0x040009F1 RID: 2545
	private NNConstraint nodeConstraint = new NNConstraint();

	// Token: 0x040009F2 RID: 2546
	private List<GraphNode> allNodes;

	// Token: 0x040009F3 RID: 2547
	private Transform tr;

	// Token: 0x040009F4 RID: 2548
	private bool updating;

	// Token: 0x040009F5 RID: 2549
	private bool stop;

	// Token: 0x040009F6 RID: 2550
	private IEnumerator corout;

	// Token: 0x040009F7 RID: 2551
	private Vector3 prevPosition;

	// Token: 0x040009F8 RID: 2552
	private SphereCollider sphere;

	// Token: 0x040009F9 RID: 2553
	private float count;

	// Token: 0x040009FA RID: 2554
	private Vector3 lerpingPosition;

	// Token: 0x040009FB RID: 2555
	private GameObject thisObject;

	// Token: 0x040009FC RID: 2556
	private int nonDynamicCount;

	// Token: 0x040009FD RID: 2557
	private GraphNode targetNode;

	// Token: 0x040009FE RID: 2558
	public List<GameObject> fx = new List<GameObject>();

	// Token: 0x040009FF RID: 2559
	public List<float> fxScaleTarget = new List<float>();

	// Token: 0x04000A00 RID: 2560
	public List<Vector3> fxLerpTarget = new List<Vector3>();

	// Token: 0x04000A01 RID: 2561
	public GameObject mainVisual;

	// Token: 0x04000A02 RID: 2562
	private List<Character> gos;
}
