//void PlaceDoors() // For a door to be valid
    //{
    //for (int y = 0; y < grid.Height; y++)
    //{
    //for (int x = 0; x < grid.Width; x++)
    //{
    //TileNode<NodeState> targetNode = grid.GetNode(x, y);
    //TileNode<NodeState>[] neighbours = new TileNode<NodeState>[8];

    //for (int i = 0; i < 8; i++)
    //{
    //neighbours[i] = grid.GetNeighbour(x, y, i);
    //}
    //if ()
    //}
    //}
    //}

    //NOTE (David) Ok , so this was overcomplicated to the extreme.
    //Simple solution: Create non colliding paths by just checking two neighbours away instead of one.
    //Sounds retarded but it works. Although for it to look proper the rooms must be positioned on an odd tile and be odd sizes

    private static void BuildPath(ref TileGrid <NodeState> tileGrid, TileNode <NodeState> currentNode, int prevDir)
    {
        currentNode.Visited = true;

        if (currentNode.Data != NodeState.Door)
        {
            currentNode.Data = NodeState.Path;
        }

        TileNode <NodeState>[] possibleChildren = new TileNode <NodeState> [4];
        for (int i = 0; i < possibleChildren.Length; i++)
        {
            TileNode <NodeState> tmp = tileGrid.GetNeighbour(currentNode.position.x, currentNode.position.y, i * 2);
            if (tmp != null)
            {
                possibleChildren[i] = tileGrid.GetNeighbour(tmp.position.x, tmp.position.y, i * 2);
            }
        }

        List <int> randomIndices = new List <int>();

        for (int i = 0; i < possibleChildren.Length; i++)
        {
            if (possibleChildren[i] != null && !possibleChildren[i].Visited)
            {
                randomIndices.Add(i);
            }
        }

        while (randomIndices.Count > 0)
        {
            int rand = Random.value > 0.55f ? randomIndices[Random.Range(0, randomIndices.Count)] : prevDir;
            randomIndices.Remove(rand);

            TileNode <NodeState> nextNode = possibleChildren[rand];

            if (nextNode != null && !nextNode.Visited && nextNode.Data == NodeState.Empty)
            {
                TileNode <NodeState> midNode = tileGrid.GetNeighbour(currentNode.position.x, currentNode.position.y, rand * 2);
                currentNode.AddChild(midNode);
                midNode.AddChild(nextNode);

                tileGrid.GetNeighbour(currentNode.position.x, currentNode.position.y, rand * 2).Visited = true;
                tileGrid.GetNeighbour(currentNode.position.x, currentNode.position.y, rand * 2).Data    = NodeState.Path;

                BuildPath(ref tileGrid, nextNode, rand);
            }
        }
    }
        public virtual bool Reduce(ref TileGrid <T> grid, T checkState, T setState)
        {
            bool save = false;

            for (int i = 0; i < 4; i++)
            {
                TileNode <T> node = grid.GetNeighbour(position.x, position.y, i * 2);
                if (node != null && node.Data.Equals(checkState))
                {
                    save = true;
                }
                //if (Data.Equals(checkState))
                //{
                //    return true;
                //}
            }
            if (children != null)
            {
                for (int i = 0; i < children.Count; i++)
                {
                    if (children[i] != null)
                    {
                        if (!children[i].Reduce(ref grid, checkState, setState))
                        {
                            children[i].RemoveChildren();
                            children[i] = null;
                        }
                        else
                        {
                            save = true;
                        }
                    }
                }
            }
            if (!save)
            {
                Data = setState;
            }
            return(save);
        }
    void PlaceDoors()
    {
        for (int i = 0; i < levelData.m_rooms.Count; i++)
        {
            Room room = levelData.m_rooms[i];

            int xStart = (int)room.Rect.xMin;
            int yStart = (int)room.Rect.yMin;
            int xEnd   = (int)room.Rect.xMax;
            int yEnd   = (int)room.Rect.yMax;
            for (int y = yStart - 1; y < yEnd + 1; y++)
            {
                for (int x = xStart - 1; x < xEnd + 1; x++)
                {
                    int conditions = 0;
                    conditions += x < xStart ? 1 : 0;
                    conditions += y < yStart ? 1 : 0;
                    conditions += x >= xEnd ? 1 : 0;
                    conditions += y >= yEnd ? 1 : 0;

                    if (conditions == 2)
                    {
                        levelData.data.SetNode(x, y, NodeState.Corner);

                        for (int j = 0; j < 4; j++)
                        {
                            grid.GetNeighbour(x, y, j * 2);
                            if (grid.GetNeighbour(x, y, j * 2) != null && grid.GetNeighbour(x, y, j * 2).Data == NodeState.Wall)
                            {
                                grid.GetNeighbour(x, y, j * 2).Data = NodeState.Corner;
                            }
                        }
                    }
                }
            }

            int placedDoors = 0;

            BitSet directions = new BitSet();

            while (placedDoors < 2)
            {
                int doorx = Random.Range(xStart, xEnd);
                int doory = Random.Range(yStart, yEnd);

                float r = Random.value;
                TileNode <NodeState> currentNode;

                if (r > 0.75f && grid.GetNode(doorx, yStart - 1).Data != NodeState.Door && grid.GetNode(doorx, yStart - 1).Data != NodeState.Corner && !directions.Get(0))
                {
                    currentNode = grid.GetNode(doorx, yStart - 1);

                    if (currentNode.Data != NodeState.Door && currentNode.Data != NodeState.Corner && !directions.Get(0))
                    {
                        grid.SetNode(doorx, yStart - 1, NodeState.Door);
                        directions.Set(0, 1);
                        placedDoors++;
                    }
                }
                else if (r > 0.5f)
                {
                    grid.SetNode(xStart - 1, doory, NodeState.Door);
                    placedDoors++;
                    directions.Set(1, 1);
                }
                else if (r > 0.25f)
                {
                    grid.SetNode(doorx, yEnd, NodeState.Door);
                    placedDoors++;
                    directions.Set(2, 1);
                }
                else
                {
                    grid.SetNode(xEnd, doory, NodeState.Door);
                    placedDoors++;
                    directions.Set(3, 1);
                }
            }
        }
        //for (int i = 0; i < levelData.m_rooms.Count; i++)
        //{
        //    Room room = levelData.m_rooms[i];

        //    float mergeRoomChance = Random.value;

        //    int xStart = (int)room.Rect.xMin;
        //    int yStart = (int)room.Rect.yMin;
        //    int xEnd = (int)room.Rect.xMax;
        //    int yEnd = (int)room.Rect.yMax;

        //    int doorCount = Random.Range(1, 2);

        //    List<TileNode<NodeState>> doorPositions = new List<TileNode<NodeState>>();

        //    for (int y = yStart - 1; y < yEnd + 1; y++)
        //    {
        //        for (int x = xStart - 1; x < xEnd + 1; x++)
        //        {
        //            int conditions = 0;
        //            conditions += x < xStart ? 1 : 0;
        //            conditions += y < yStart ? 1 : 0;
        //            conditions += x >= xEnd ? 1 : 0;
        //            conditions += y >= yEnd ? 1 : 0;

        //            TileNode<NodeState>[] neighbours = new TileNode<NodeState>[4];

        //            for (int j = 0; j < 4; j++)
        //            {
        //                neighbours[j] = grid.GetNeighbour(x, y, j * 2);
        //                //if (neighbours[j] != null && neighbours[j].Data == NodeState.Wall)
        //                //{
        //                //    neighbours[j].Data = NodeState.Corner;
        //                //}
        //            }

        //            if (conditions == 2)
        //            {
        //                levelData.data.SetNode(x, y, NodeState.Corner);
        //            }

        //            //if (conditions == 1 && mergeRoomChance > 0.5f)
        //            //{
        //            //    if (neighbours[0] != null && neighbours[2] != null && neighbours[0].Data == NodeState.Room && neighbours[2].Data == NodeState.Room)
        //            //    {
        //            //        levelData.data.SetNode(x, y, NodeState.Room);
        //            //    }
        //            //    if (neighbours[1] != null && neighbours[3] != null && neighbours[1].Data == NodeState.Room && neighbours[3].Data == NodeState.Room)
        //            //    {
        //            //        levelData.data.SetNode(x, y, NodeState.Room);
        //            //    }
        //            //}
        //            //else
        //            {
        //                if (levelData.data.GetNode(x, y).Data == NodeState.Wall && neighbours[0] != null && neighbours[2] != null && (neighbours[0].Data == NodeState.Room || neighbours[0].Data == NodeState.Path) && (neighbours[2].Data == NodeState.Room || neighbours[2].Data == NodeState.Path))
        //                {
        //                    doorPositions.Add(levelData.data.GetNode(x, y));
        //                }
        //                if (levelData.data.GetNode(x, y).Data == NodeState.Wall && neighbours[1] != null && neighbours[3] != null && (neighbours[1].Data == NodeState.Room || neighbours[1].Data == NodeState.Path) && (neighbours[3].Data == NodeState.Room || neighbours[3].Data == NodeState.Path))
        //                {
        //                    doorPositions.Add(levelData.data.GetNode(x, y));
        //                }
        //            }

        //            //if (conditions == 1 && levelData.data.GetNode(x, y).Data != NodeState.Corner)
        //            //{
        //            //    if (levelData.data.GetNeighbour(x, y, 0).Data == NodeState.Room && levelData.data.GetNeighbour(x, y, 4).Data == NodeState.Room)
        //            //    {
        //            //        levelData.data.SetNode(x, y, NodeState.Room);
        //            //    }
        //            //    if (levelData.data.GetNeighbour(x, y, 2).Data == NodeState.Room && levelData.data.GetNeighbour(x, y, 6).Data == NodeState.Room)
        //            //    {
        //            //        levelData.data.SetNode(x, y, NodeState.Room);
        //            //    }
        //            //    //    //levelData.data.SetNode(x, y, NodeState.Door);
        //            //    //    doorPositions.Add(grid.GetNode(x, y));
        //            //}
        //        }
        //    }

        //    for (int j = 0; j < doorPositions.Count; j++)
        //    {
        //        doorPositions[j].Data = NodeState.Door;
        //    }
        //}
    }