Beispiel #1
0
 private void useThisMap(MapInfo map)
 {
     mapPreviewUserControl1.MapInfo = map;
     mapServer.MapInfo = map;
 }
Beispiel #2
0
        private void buttonAnalyze_Click(object sender, EventArgs e)
        {
            Bitmap publicImage = null;
            Bitmap secretImage = null;

            if (String.IsNullOrWhiteSpace(fileSelectorPublic.FileName))
            {
                ShowMsg("publicImageNull");
                return;
            }

            publicImage = (Bitmap)Bitmap.FromFile(fileSelectorPublic.FileName);
            if (!String.IsNullOrWhiteSpace(fileSelectorSecret.FileName))
            { secretImage = (Bitmap)Bitmap.FromFile(fileSelectorSecret.FileName); }
            if (secretImage == null)
            { secretImage = publicImage; }

            publicImage.SetResolution(96, 96);
            secretImage.SetResolution(96, 96);

            DirectoryInfo saveTiles = GetDefaultTilesFolder();

            if (!saveTiles.Exists)
            { saveTiles.Create(); }
            else
            {
                foreach (FileInfo file in saveTiles.GetFiles())
                { if (file != null && file.Extension == ".jpg") { file.Delete(); } }
            }

            MapInfo mapInfo = new MapInfo() { GridConfig = gridConfigUserControl1.GridConfig };

            mapInfo.TileFolder = saveTiles;
            mapInfo.PublicImage = publicImage;
            mapInfo.SecretImage = secretImage;

            if (publicImage.Width != secretImage.Width || publicImage.Height != secretImage.Height)
            {
                ShowMsg("publicSecretDimensionsDiffer");
                return;
            }

            AsyncAnalysisReport result = TileAnalyser.BeginAnalyzeWholeImage(publicImage, secretImage, mapInfo, saveTiles);
            AnalysisProgressDialog waitTillDone = new AnalysisProgressDialog();
            waitTillDone.AsyncResult = result;
            waitTillDone.ShowDialog();

            useThisMap(mapInfo);
        }
 public static AsyncAnalysisReport BeginAnalyzeWholeImage(Bitmap publicLayer, Bitmap secretLayer, MapInfo mapInfo, DirectoryInfo saveTiles = null)
 {
     AsyncAnalysisReport result = new AsyncAnalysisReport();
     result.publicLayer = publicLayer;
     result.secretLayer = secretLayer;
     result.mapInfo = mapInfo;
     result.saveTiles = saveTiles;
     ThreadPool.QueueUserWorkItem(new WaitCallback(DoAnalyzeWholeImage), result);
     return result;
 }
Beispiel #4
0
 private void toolStripButtonNew_Click(object sender, EventArgs e)
 {
     MapInfo map = new MapInfo();
     map.TileFolder = GetDefaultTilesFolder();
     lockImageControls(false);
     useThisMap(map);
 }
 public static TileCollection AnalyzeWholeImage(Bitmap publicLayer, Bitmap secretLayer, MapInfo mapInfo, DirectoryInfo saveTiles = null)
 {
     IAsyncResult result = BeginAnalyzeWholeImage(publicLayer, secretLayer, mapInfo, saveTiles);
     return EndAnalyzeWholeImage(result);
 }
        public static MapInfo Deserialize(Stream stream, DirectoryInfo tileFolder, bool isCompressed = true)
        {
            Contract.Requires(stream != null);
            Contract.Requires(tileFolder != null);

            if (!tileFolder.Exists)
            { tileFolder.Create(); }
            foreach (FileInfo tile in tileFolder.GetFiles())
            { tile.Delete(); }
            MapInfoSerializeContainer container;
            if (isCompressed)
            {
                using (DeflateStream ds = new DeflateStream(stream, CompressionMode.Decompress))
                { container = Serializer.Deserialize<MapInfoSerializeContainer>(ds); }
            }
            else
            { container = Serializer.Deserialize<MapInfoSerializeContainer>(stream); }
            foreach (String tile in container.TileImages.Keys)
            {
                using (FileStream ts = new FileStream(Path.Combine(tileFolder.FullName, tile), FileMode.Create))
                {
                    byte[] bytes = container.TileImages[tile];
                    ts.Write(bytes, 0, bytes.Length);
                }
            }
            MapInfo map = new MapInfo();
            map.TileFolder = tileFolder;

            map.GridConfig.TileWidth = container.Grid.Width;
            map.GridConfig.TileHeight = container.Grid.Height;
            map.GridConfig.TileOffsetX = container.Grid.OffsetX;
            map.GridConfig.TileOffsetY = container.Grid.OffsetY;
            map.GridConfig.TileBorder = container.Grid.Border;

            map.PublicImage = LoadImage(container.PublicImage);
            map.SecretImage = LoadImage(container.SecretImage);

            Dictionary<MapInfoSerializeContainer.TileClass, Tile> convertedTiles = new Dictionary<MapInfoSerializeContainer.TileClass, Tile>();
            if (container.Tiles != null)
            {
                foreach (MapInfoSerializeContainer.TileClass serializedTile in container.Tiles)
                {
                    Tile tile = new Tile();
                    tile.X = serializedTile.X;
                    tile.Y = serializedTile.Y;
                    tile.IsEmptyPublic = serializedTile.EmptyPublic;
                    tile.IsEmptySecret = serializedTile.EmptySecret;
                    tile.HasSecret = serializedTile.HasSecret;
                    tile.IsRevealed = serializedTile.IsRevealed;
                    tile.ImageNamePublic = serializedTile.NamePublic;
                    tile.ImageNameSecret = serializedTile.NameSecret;
                    tile.LightLevel = serializedTile.LightLevel;
                    tile.LightSource = serializedTile.LightSource;
                    convertedTiles.Add(serializedTile, tile);
                    map.Tiles.Add(tile);
                }
                foreach (MapInfoSerializeContainer.TileClass serializedTile in container.Tiles)
                {
                    if (serializedTile.Adjacent == null)
                    { continue; }
                    Tile tile = convertedTiles[serializedTile];
                    foreach (MapInfoSerializeContainer.TileClass adjacent in serializedTile.Adjacent)
                    { tile.AdjacentTiles.Add(convertedTiles[adjacent]); }
                    if (serializedTile.NoLOS != null)
                    {
                        foreach (MapInfoSerializeContainer.TileClass noLOS in serializedTile.NoLOS)
                        { tile.NoLOSBetweenTiles.Add(convertedTiles[noLOS]); }
                    }
                }
            }
            int playerNum = 1;
            if (container.PlayerPositions != null)
            {
                foreach (MapInfoSerializeContainer.TileClass position in container.PlayerPositions)
                {
                    map.MovePlayer(playerNum++, convertedTiles[position]);
                }
            }
            return map;
        }
 public static void Serialize(MapInfo map, String destination, bool compress = true)
 {
     Serialize(map, new FileInfo(destination), compress);
 }
 public static void Serialize(MapInfo map, FileInfo destination, bool compress = true)
 {
     using (FileStream output = destination.OpenWrite())
     { Serialize(map, output, compress); }
 }
        public static void Serialize(MapInfo map, Stream destination, bool compress = true)
        {
            Contract.Requires(map != null);
            Contract.Requires(destination != null);
            MapInfoSerializeContainer container = new MapInfoSerializeContainer();
            container.Grid = new MapInfoSerializeContainer.GridClass();
            container.Tiles = new List<MapInfoSerializeContainer.TileClass>();
            container.TileImages = new Dictionary<string, byte[]>();
            container.PlayerPositions = new List<MapInfoSerializeContainer.TileClass>();

            container.Grid.Width = map.GridConfig.TileWidth;
            container.Grid.Height = map.GridConfig.TileHeight;
            container.Grid.OffsetX = map.GridConfig.TileOffsetX;
            container.Grid.OffsetY = map.GridConfig.TileOffsetY;
            container.Grid.Border = map.GridConfig.TileBorder;
            container.PublicImage = SaveImage(map.PublicImage);
            container.SecretImage = SaveImage(map.SecretImage);

            Dictionary<Tile, MapInfoSerializeContainer.TileClass> convertedTiles = new Dictionary<Tile, MapInfoSerializeContainer.TileClass>();
            foreach (Tile tile in map.Tiles)
            {
                MapInfoSerializeContainer.TileClass serializedTile = new MapInfoSerializeContainer.TileClass();
                serializedTile.Adjacent = new List<MapInfoSerializeContainer.TileClass>();
                serializedTile.NoLOS = new List<MapInfoSerializeContainer.TileClass>();

                serializedTile.X = tile.X;
                serializedTile.Y = tile.Y;
                serializedTile.EmptyPublic = tile.IsEmptyPublic;
                serializedTile.EmptySecret = tile.IsEmptySecret;
                serializedTile.HasSecret = tile.HasSecret;
                serializedTile.IsRevealed = tile.IsRevealed;
                serializedTile.NamePublic = tile.ImageNamePublic;
                serializedTile.NameSecret = tile.ImageNameSecret;
                serializedTile.LightLevel = tile.LightLevel;
                serializedTile.LightSource = tile.LightSource;
                convertedTiles.Add(tile, serializedTile);
                container.Tiles.Add(serializedTile);
            }
            foreach (Tile tile in map.Tiles)
            {
                MapInfoSerializeContainer.TileClass serializedTile = convertedTiles[tile];
                foreach (Tile adjacent in tile.AdjacentTiles)
                {
                    serializedTile.Adjacent.Add(convertedTiles[adjacent]);
                }
                foreach (Tile noLOS in tile.NoLOSBetweenTiles)
                {
                    serializedTile.NoLOS.Add(convertedTiles[noLOS]);
                }
            }
            foreach (Tile tile in map.PositionOfPlayers())
            { container.PlayerPositions.Add(convertedTiles[tile]); }

            DirectoryInfo dir = map.TileFolder;
            if (dir.Exists)
            {
                foreach (FileInfo file in dir.GetFiles())
                {
                    using (FileStream fs = file.OpenRead())
                    {
                        byte[] bytes = new byte[file.Length];
                        fs.Read(bytes, 0, bytes.Length);
                        container.TileImages.Add(file.Name, bytes);
                    }
                }
            }
            if (compress)
            {
                using (DeflateStream ds = new DeflateStream(destination, CompressionMode.Compress))
                { Serializer.Serialize<MapInfoSerializeContainer>(ds, container); }
            }
            else
            { Serializer.Serialize<MapInfoSerializeContainer>(destination, container); }
        }