public AutoTileFormat[,] GetTileLinks()
 {
     AutoTileFormat[,] tiles = new AutoTileFormat[this.width, this.height];
     for (int i = 0; i < this.width; i++)
     {
         for (int j = 0; j < this.height; j++)
         {
             tiles[i, j] = GetAutoTileFormat(i, j);
         }
     }
     return(tiles);
 }
        public static Bitmap[] GenerateAutotileBitmaps(AutotileImageFormat format, Image source, int tilesize)
        {
            Bitmap bitmap = new Bitmap(source);
            int    half   = tilesize / 2;

            Bitmap[] outputs = new Bitmap[256];
            Point    locNW   = Point.Empty;
            Point    locNE   = new Point(half, 0);
            Point    locSW   = new Point(0, half);
            Point    locSE   = new Point(half, half);

            if (format == AutotileImageFormat.RMMV)
            {
                //Corners
                Bitmap cornerNW = SubImage(bitmap, new Rectangle(0, tilesize, half, half));
                Bitmap cornerNE = SubImage(bitmap, new Rectangle(tilesize + half, tilesize, half, half));
                Bitmap cornerSW = SubImage(bitmap, new Rectangle(0, tilesize * 2 + half, half, half));
                Bitmap cornerSE = SubImage(bitmap, new Rectangle(tilesize + half, tilesize * 2 + half, half, half));
                //Inners
                Bitmap innerNW = SubImage(bitmap, new Rectangle(tilesize, 2 * tilesize, half, half));
                Bitmap innerNE = SubImage(bitmap, new Rectangle(half, 2 * tilesize, half, half));
                Bitmap innerSW = SubImage(bitmap, new Rectangle(tilesize, tilesize + half, half, half));
                Bitmap innerSE = SubImage(bitmap, new Rectangle(half, tilesize + half, half, half));
                //Outers
                Bitmap outerNW = SubImage(bitmap, new Rectangle(tilesize, 0, half, half));
                Bitmap outerNE = SubImage(bitmap, new Rectangle(tilesize + half, 0, half, half));
                Bitmap outerSW = SubImage(bitmap, new Rectangle(tilesize, half, half, half));
                Bitmap outerSE = SubImage(bitmap, new Rectangle(tilesize + half, half, half, half));
                //Verticals
                Bitmap vertNW = SubImage(bitmap, new Rectangle(0, tilesize * 2, half, half));
                Bitmap vertNE = SubImage(bitmap, new Rectangle(tilesize + half, tilesize * 2, half, half));
                Bitmap vertSW = SubImage(bitmap, new Rectangle(0, tilesize + half, half, half));
                Bitmap vertSE = SubImage(bitmap, new Rectangle(tilesize + half, tilesize + half, half, half));
                //Horizontals
                Bitmap horizNW = SubImage(bitmap, new Rectangle(tilesize, tilesize, half, half));
                Bitmap horizNE = SubImage(bitmap, new Rectangle(half, tilesize, half, half));
                Bitmap horizSW = SubImage(bitmap, new Rectangle(tilesize, tilesize * 2 + half, half, half));
                Bitmap horizSE = SubImage(bitmap, new Rectangle(half, tilesize * 2 + half, half, half));

                for (int i = 0; i < 256; i++)
                {
                    AutoTileFormat f         = (AutoTileFormat)i;
                    bool           connectN  = (f & AutoTileFormat.ConnectNorth) == AutoTileFormat.ConnectNorth;
                    bool           connectS  = (f & AutoTileFormat.ConnectSouth) == AutoTileFormat.ConnectSouth;
                    bool           connectW  = (f & AutoTileFormat.ConnectWest) == AutoTileFormat.ConnectWest;
                    bool           connectE  = (f & AutoTileFormat.ConnectEast) == AutoTileFormat.ConnectEast;
                    bool           connectNW = (f & AutoTileFormat.ConnectNorthWest) == AutoTileFormat.ConnectNorthWest;
                    bool           connectNE = (f & AutoTileFormat.ConnectNorthEast) == AutoTileFormat.ConnectNorthEast;
                    bool           connectSW = (f & AutoTileFormat.ConnectSouthWest) == AutoTileFormat.ConnectSouthWest;
                    bool           connectSE = (f & AutoTileFormat.ConnectSouthEast) == AutoTileFormat.ConnectSouthEast;
                    Bitmap         nw        = connectN && connectW ? connectNW ? innerNW : outerNW : connectN ? vertNW : connectW ? horizNW : cornerNW;
                    Bitmap         ne        = connectN && connectE ? connectNE ? innerNE : outerNE : connectN ? vertNE: connectE ? horizNE : cornerNE;
                    Bitmap         sw        = connectS && connectW ? connectSW ? innerSW : outerSW : connectS ? vertSW : connectW ? horizSW : cornerSW;
                    Bitmap         se        = connectS && connectE ? connectSE ? innerSE : outerSE : connectS ? vertSE : connectE ? horizSE : cornerSE;
                    outputs[i] = new Bitmap(tilesize, tilesize);
                    using (Graphics g = Graphics.FromImage(outputs[i])) {
                        g.DrawImage(nw, locNW);
                        g.DrawImage(ne, locNE);
                        g.DrawImage(sw, locSW);
                        g.DrawImage(se, locSE);
                    }
                }
                #region Memory cleanup
                cornerNW.Dispose();
                cornerNE.Dispose();
                cornerSW.Dispose();
                cornerSE.Dispose();
                //Inners
                innerNW.Dispose();
                innerNE.Dispose();
                innerSW.Dispose();
                innerSE.Dispose();
                //Outers
                outerNW.Dispose();
                outerNE.Dispose();
                outerSW.Dispose();
                outerSE.Dispose();
                //Verticals
                vertNW.Dispose();
                vertNE.Dispose();
                vertSW.Dispose();
                vertSE.Dispose();
                //Horizontals
                horizNW.Dispose();
                horizNE.Dispose();
                horizSW.Dispose();
                horizSE.Dispose();
                #endregion
            }
            else if (format == AutotileImageFormat.RMXP)
            {
            }
            return(outputs);
        }
        public AutoTileFormat GetAutoTileFormat(int x, int y)
        {
            List <TileData> tileset = Master.TilesData;
            int             target  = this.tiles[x, y];

            if (!tileset[target].Autotile)
            {
                return(AutoTileFormat.None);
            }
            int n  = y > 0 ? this.tiles[x, y - 1] : target;
            int s  = y < height - 1 ? this.tiles[x, y + 1] : target;
            int w  = x > 0 ? this.tiles[x - 1, y] : target;
            int e  = x < width - 1 ? this.tiles[x + 1, y] : target;
            int nw = y > 0 && x > 0 ? this.tiles[x - 1, y - 1] : target;
            int ne = y > 0 && x < width - 1 ? this.tiles[x + 1, y - 1] : target;
            int sw = y < height - 1 && x > 0 ? this.tiles[x - 1, y + 1] : target;
            int se = y < height - 1 && x < width - 1 ? this.tiles[x + 1, y + 1] : target;

            target = tileset[target].BaseID;
            n      = tileset[n].BaseID;
            s      = tileset[s].BaseID;
            w      = tileset[w].BaseID;
            e      = tileset[e].BaseID;
            nw     = tileset[nw].BaseID;
            ne     = tileset[ne].BaseID;
            sw     = tileset[sw].BaseID;
            se     = tileset[se].BaseID;
            AutoTileFormat format = AutoTileFormat.None;

            if (n == target)
            {
                format |= AutoTileFormat.ConnectNorth;
            }
            if (s == target)
            {
                format |= AutoTileFormat.ConnectSouth;
            }
            if (w == target)
            {
                format |= AutoTileFormat.ConnectWest;
            }
            if (e == target)
            {
                format |= AutoTileFormat.ConnectEast;
            }
            if (nw == target)
            {
                format |= AutoTileFormat.ConnectNorthWest;
            }
            if (ne == target)
            {
                format |= AutoTileFormat.ConnectNorthEast;
            }
            if (sw == target)
            {
                format |= AutoTileFormat.ConnectSouthWest;
            }
            if (se == target)
            {
                format |= AutoTileFormat.ConnectSouthEast;
            }
            return(format);
        }