/// <summary>
 /// Clears the blocks.
 /// </summary>
 public void ClearBlocks()
 {
     //Clear the blocks
     _blocks = new Block[Game1.MAXX,Game1.MAXY,Game1.MAXZ];
     for (int x = 0; x < Game1.MAXX; x++) {
         for (int y = 0; y < Game1.MAXY; y++) {
             for (int z = 0; z < Game1.MAXZ; z++) {
                 var b = new Block();
                 b.SetIndex(Blocks.AIR);
                 _blocks[x,y,z] = b;
             }
         }
     }
 }
        //TODO: Flip tiles and blocks to match rotations.
        /// <summary>
        /// Rotates the city to the right.
        /// </summary>
        public void RotateRight()
        {
            //To find the distance each piece of the grid should be to connect it together
            Vector2 spacing = new Vector2 (Tiles.GRIDWIDTH / 2f, Tiles.GRIDHEIGHT / 2f);
            spacing.X = (float)Math.Round (spacing.X, 0);
            spacing.Y = (float)Math.Round (spacing.Y, 0);

            //Direction for the orientation of the city. Is used to draw features correctly.
            Direction.RotateRight();
            Camera.Direction.RotateRight();
            Block[,,] blockresult = new Block[Game1.MAXX, Game1.MAXY, Game1.MAXZ];
            Tile[,] tileresult = new Tile[Game1.MAXX, Game1.MAXY];

            for (int y = 0; y < Game1.MAXY; y++)
                for (int x = 0; x < Game1.MAXX; x++) {
                    //Rotate the tiles
                    tileresult [x, y] = _tiles [Game1.MAXY - 1 - y, x];
                    //Recalculate the drawrect.
                    tileresult [x, y].DrawRect = GetTileDrawRect(x,y);

                    //Rotate the blocks
                    for (int z = 0; z < Game1.MAXZ; z++) {
                        blockresult [x, y, z] = _blocks [Game1.MAXY - 1 - y, x, z];

                    //Recalc block position
                    blockresult[x,y,z].DrawRect = new Rectangle(
                        tileresult[x,y].DrawRect.X,
                        tileresult[x,y].DrawRect.Y - (Blocks.BLOCKHEIGHT*(z+1)), 31,31);
                    }
                }
            _blocks =  (Block[,,])blockresult.Clone();
            _tiles = (Tile[,])tileresult.Clone();
            CheckRoadSanity();
        }
        /// <summary>
        /// Generates the test pattern.
        /// </summary>
        public void GenerateTestPattern()
        {
            //Clear the array
            ClearBlocks();
            List<Point> points = new List<Point> ();
            points.Add(new Point(2,2));
            points.Add(new Point(2,2).Add(Assets.FacingDirection(Facing.North)));
            points.Add(new Point(2,2).Add(Assets.FacingDirection(Facing.East)));
            points.Add(new Point(2,2).Add(Assets.FacingDirection(Facing.West)));
            points.Add(new Point(2,2).Add(Assets.FacingDirection(Facing.South)));

            Dictionary<Point, Color> colors = new Dictionary<Point, Color>(){
                { points[0], Color.White }, //Center Point
                { points[1], Color.Red }, //North
                { points[2], Color.Orange }, //East
                { points[3], Color.Green }, //West
                { points[4], Color.Yellow }  //South
            };

            //To get a color
            int i = 0;
            foreach (Point p in points) {
                Color c = colors[p];
                for(int z = 0; z < 6; z++){
                    Block b = new Block();
                    b.SetIndex(Blocks.BLOCK);
                    b.Color = c;
                    b.InfluenceColor = c;
                    b.DrawRect =
                        new Rectangle(_tiles[p.X,p.Y].DrawRect.X,
                                      _tiles[p.X,p.Y].DrawRect.Y - (Blocks.BLOCKHEIGHT*(z+1)),
                                      31,31);
                    _blocks[p.X,p.Y,z] = b;
                }
                i++;
            }
        }
        /// <summary>
        /// Rotates the city to the left.
        /// </summary>
        public void RotateLeft()
        {
            Vector2 spacing = new Vector2 (Tiles.GRIDWIDTH / 2f, Tiles.GRIDHEIGHT / 2f);
            spacing.X = (float)Math.Round (spacing.X, 0);
            spacing.Y = (float)Math.Round (spacing.Y, 0);
            Direction.RotateLeft();
            Camera.Direction.RotateRight();
            Block[,,] blockresult = new Block[Game1.MAXX, Game1.MAXY, Game1.MAXZ];
            Tile[,] tileresult = new Tile[Game1.MAXX, Game1.MAXY];

            for (int y = 0; y < Game1.MAXY; y++)
                for (int x = 0; x < Game1.MAXX; x++) {
                    //Rotate the tiles
                    tileresult [x, y] = _tiles [y, Game1.MAXX - 1 - x];
                    //Recalculate the drawrect.
                    tileresult [x, y].DrawRect = GetTileDrawRect(x,y);

                    //Rotate the blocks
                    for (int z = 0; z < Game1.MAXZ; z++) {
                    blockresult [x, y, z] = _blocks [y, Game1.MAXX - 1 - x, z];

                    //Recalc block position
                    blockresult[x,y,z].DrawRect = new Rectangle(
                        tileresult[x,y].DrawRect.X,
                        tileresult[x,y].DrawRect.Y - (Blocks.BLOCKHEIGHT*(z+1)), 31,31);
                    }
                }
            _blocks =  (Block[,,])blockresult.Clone();
            _tiles = (Tile[,])tileresult.Clone();
            CheckRoadSanity();
        }
        /// <summary>
        /// Generates the blocks.
        /// </summary>
        public void GenerateBlocks()
        {
            //Clear the blocks
            _blocks = new Block[Game1.MAXX,Game1.MAXY,Game1.MAXZ];
            for (int x = 0; x < Game1.MAXX; x++) {
                for (int y = 0; y < Game1.MAXY; y++) {
                    for (int z = 0; z < Game1.MAXZ; z++) {
                        var b = new Block();
                        b.SetIndex(Blocks.AIR);
                        b.ShowInfluence = InfluenceViewIsOn;
                        b.InfluenceColor = new Color ( 1f * GetInfluencePercent(new Point(x,y)),1f - GetInfluencePercent (new Point(x, y)),1f - GetInfluencePercent (new Point(x, y)));
                        _blocks[x,y,z] = b;
                    }
                }
            }

            //Generate buildings
            for (int y = 0; y < Game1.MAXY; y++) {
                for (int x = 0; x < Game1.MAXX; x++) {
                    float influencepercent = GetInfluencePercent(new Point(x,y));
                    Influence influence = Assets.Influence.Rural;
                    foreach(Influence i in Assets.Influence.Influences)
                    {
                        //Go through the list of influences and compare the land coverage percent to the
                        //distance from the influence point. The lowest one that fits in our number is
                        //one
                        if(influencepercent < i.LandCoverage && i.LandCoverage < influence.LandCoverage)
                            influence = i;
                    }

                    //Now we will try to build our building
                    if(_tiles[x,y].Buildable)
                        if(Assets.Random.Next(0,100) < influence.BuildRate)
                            GenerateBuilding(x,y, influence);
                }
            }
        }