/// <summary>
 /// Adds a face with the given neighbor data instances as neighbors.
 /// </summary>
 /// <param name="neighbor0">The neighbor data of the first neighbor.</param>
 /// <param name="neighbor1">The neighbor data of the second neighbor.</param>
 /// <param name="neighbor2">The neighbor data of the third neighbor.</param>
 /// <returns>The index of the face just added.</returns>
 public int AddFace(NeighborData neighbor0, NeighborData neighbor1, NeighborData neighbor2)
 {
     _faceNeighborCounts[_faceIndex]       = 3;
     _faceFirstNeighborIndices[_faceIndex] = _neighborDataIndex;
     _neighborData[_neighborDataIndex++]   = neighbor0;
     _neighborData[_neighborDataIndex++]   = neighbor1;
     _neighborData[_neighborDataIndex++]   = neighbor2;
     return(_faceIndex++);
 }
 /// <summary>
 /// Adds a face with the given vertex indices as neighbors.
 /// </summary>
 /// <param name="vertex0Index">The index of the first neighbor vertex.</param>
 /// <param name="vertex1Index">The index of the second neighbor vertex.</param>
 /// <param name="vertex2Index">The index of the third neighbor vertex.</param>
 /// <returns>The index of the face just added.</returns>
 public int AddFace(int vertex0Index, int vertex1Index, int vertex2Index)
 {
     _faceNeighborCounts[_faceIndex]       = 3;
     _faceFirstNeighborIndices[_faceIndex] = _neighborDataIndex;
     _neighborData[_neighborDataIndex++]   = new NeighborData(vertex0Index);
     _neighborData[_neighborDataIndex++]   = new NeighborData(vertex1Index);
     _neighborData[_neighborDataIndex++]   = new NeighborData(vertex2Index);
     return(_faceIndex++);
 }
        public int ExtractMin()
        {
            NeighborData head = _heap[0];

            Swap(0, --_heapSize);
            MinHeapify(0);

            return(head.Vertex);
        }
        public void DecreaseDistance(int vertex, int distance)
        {
            int index = _vertexIndex[vertex];

            NeighborData data = _heap[index];

            data.Distance = distance;

            DecreaseKey(index);
        }
        private void Swap(int firstIndex, int secondIndex)
        {
            NeighborData firstData  = _heap[firstIndex];
            NeighborData secondData = _heap[secondIndex];

            _heap[firstIndex]  = secondData;
            _heap[secondIndex] = firstData;

            _vertexIndex[firstData.Vertex]  = secondIndex;
            _vertexIndex[secondData.Vertex] = firstIndex;
        }
 /// <summary>
 /// Adds a face with the given vertex indices as neighbors.
 /// </summary>
 /// <param name="vertexIndices">The indices of the neighbor vertices.</param>
 /// <returns>The index of the face just added.</returns>
 public int AddFace(params int[] vertexIndices)
 {
     if (vertexIndices.Length < 3)
     {
         throw new ArgumentException("A face must have at least 3 neighbors.", "vertexIndices");
     }
     _faceNeighborCounts[_faceIndex]       = (ushort)vertexIndices.Length;
     _faceFirstNeighborIndices[_faceIndex] = _neighborDataIndex;
     foreach (var vertexIndex in vertexIndices)
     {
         _neighborData[_neighborDataIndex++] = new NeighborData(vertexIndex);
     }
     return(_faceIndex++);
 }
    NeighborData CheckNeighbors(int x, int y)
    {
        NeighborData Data = new NeighborData();

        Data.AvailableNeighbors = 8;
        Data.NumNeighbors       = 0;

        int OffsetX, OffsetY;

        OffsetX = 0;
        OffsetY = 1;

        for (int j = 0; j < 8; ++j)
        {
            if (x + OffsetX >= 0 && x + OffsetX < RoomWidth && y + OffsetY >= 0 && y + OffsetY < RoomHeight)
            {
                if (Tiles[x + OffsetX, y + OffsetY].State == 1)
                {
                    Data.NumNeighbors++;
                }
            }
            else
            {
                Data.AvailableNeighbors--;
            }
            switch (j)
            {
            case 0:
                OffsetX += 1;
                break;

            case 1:
            case 2:
                OffsetY -= 1;
                break;

            case 3:
            case 4:
                OffsetX -= 1;
                break;

            case 5:
            case 6:
            case 7:
                OffsetY += 1;
                break;
            }
        }
        return(Data);
    }
 //Loop through all the tiles and change their state
 public void Iterate()
 {
     for (int i = 0; i < RoomWidth; ++i)
     {
         for (int j = 0; j < RoomHeight; ++j)
         {
             NeighborData Neighbors = CheckNeighbors(i, j);
             if (!Tiles[i, j].locked)
             {
                 if (Tiles[i, j].State == 1) //If the tile is a wall
                 {
                     if (Neighbors.AvailableNeighbors <= 5 && Neighbors.NumNeighbors > 0)
                     {
                         Tiles[i, j].NewState = 1;
                     }
                     else if (Neighbors.NumNeighbors >= WallThreshold)
                     {
                         Tiles[i, j].NewState = 1;
                     }
                     else
                     {
                         Tiles[i, j].NewState = 0;
                     }
                 }
                 else if (Tiles[i, j].State == 0) //If the tile is a floor
                 {
                     if (Neighbors.AvailableNeighbors <= 5 && Neighbors.NumNeighbors > 1)
                     {
                         Tiles[i, j].NewState = 1;
                     }
                     else if (Neighbors.NumNeighbors >= FloorThreshold)
                     {
                         Tiles[i, j].NewState = 1;
                     }
                     else
                     {
                         Tiles[i, j].NewState = 0;
                     }
                 }
             }
         }
     }
 }