public Tile SpawnTileBasedOnTerrain(int id, int x, int y)
 {
     Tile t = new Tile(id, x, y);
     return t;
 }
        public void RandomTile(MainMap m, int dir, List<Tile> l)
        {
            List<Tile> Around = new List<Tile>();
            Tile ti = new Tile(0, 0, 0);
            List<Tile> Reverse = new List<Tile>();
            for (int i = 0; i < l.Count; i++)
            {
                Reverse.Add(l[l.Count - 1 - i]);
            }
            outerloop:
            for (int i = Reverse.Count - 1; i > -1; i--)
            {

                int tx = Reverse[i].X;
                int ty = Reverse[i].Y;

                if (dir == 0)
                {
                    tx = Reverse[i].X - 1;
                    if (m.MinX == 0)
                        Reverse[i].X = 0 - Reverse[i].X;
                }
                else if (dir == 1)
                {
                    ty = Reverse[i].Y - 1;
                    if (m.MinY == 0)
                        Reverse[i].Y = 0 - Reverse[i].Y;
                }
                else if (dir == 2)
                {
                    tx = Reverse[i].X + 1;
                }
                else if (dir == 3)
                {
                    ty = Reverse[i].Y + 1;
                }

                Around = AroundATile(m, tx, ty, dir);
                Around = OptimizeList(Around, m);

                bool Generated = false;

                if (Around.Count == 0)
                {
                    if (TileSpawned(m, Reverse[i].X, Reverse[i].Y, tx, ty, dir))
                    {
                        Reverse.RemoveAt(i);
                        Generated = true;
                        goto outerloop;
                    }
                    else
                        goto falseloop;
                }
                else
                {
                    for (int a = 0; a < Around.Count; a++)
                    {
                        if (TileSpawned(m, Around[a].X, Around[a].Y, tx, ty, dir))
                        {
                            Reverse.RemoveAt(i);
                            Generated = true;
                            goto outerloop;
                        }
                    }
                    goto falseloop;
                }
            falseloop:
                if (!Generated)
                {
                    int bonusx = 0;
                    int bonusy = 0;
                    if (dir == 0)
                        bonusx = 1;
                    //    tx++;
                    if (dir == 1)
                        bonusy = 1;
                    //    ty++;

                    RandomTerrain(m, tx, ty);

                    m.Y[ty + m.MinY + bonusy].Tile[tx + m.MinX + bonusx] = new Tile(m.SpawnedTerrain.Count - 1, tx, ty);
                    //if (m.SpawnedTerrain[m.Y[ty + m.MinY + bonusy].Tile[tx + m.MinX + bonusx].ID].MaxVertical < ty + m.MinY)
                        m.SpawnedTerrain[m.Y[ty + m.MinY + bonusy].Tile[tx + m.MinX + bonusx].ID].MaxVertical = ty + m.MinY;
                    //if (m.SpawnedTerrain[m.Y[ty + m.MinY + bonusy].Tile[tx + m.MinX + bonusx].ID].MinVertical > ty + m.MinY)
                        m.SpawnedTerrain[m.Y[ty + m.MinY + bonusy].Tile[tx + m.MinX + bonusx].ID].MinVertical = ty + m.MinY;
                    //if (m.SpawnedTerrain[m.Y[ty + m.MinY + bonusy].Tile[tx + m.MinX + bonusx].ID].MaxHorizontal < tx + m.MinX)
                        m.SpawnedTerrain[m.Y[ty + m.MinY + bonusy].Tile[tx + m.MinX + bonusx].ID].MaxHorizontal = tx + m.MinX;
                    //if (m.SpawnedTerrain[m.Y[ty + m.MinY + bonusy].Tile[tx + m.MinX + bonusx].ID].MinHorizontal > tx + m.MinX)
                        m.SpawnedTerrain[m.Y[ty + m.MinY + bonusy].Tile[tx + m.MinX + bonusx].ID].MinHorizontal = tx + m.MinX;
                    Reverse.RemoveAt(i);
                    Reverse = ReevaluatePriorityDueToNewTerrain(m, Reverse, tx + m.MinX, ty + m.MinY, dir);
                    goto outerloop;
                }
            }
        }