Esempio n. 1
0
    public static float DistanceBetweenTowns(Vector3 p1, Vector3 p2)
    {
        float distance = Vector3.Distance(p1, p2);

        distance *= MapScale.GetScale();
        distance  = Mathf.Round(distance * 100) / 100; // round 2 decimal points
        return(distance);
    }
Esempio n. 2
0
        /// <summary>
        ///     Extracts an image displaying the full map
        /// </summary>
        /// <param name="map">The map that should be extracted</param>
        /// <param name="scale">The scale value</param>
        /// <param name="FileName">The target filename</param>
        public static void ExtractMap(Maps map, MapScale scale, string FileName)
        {
            if (map == Maps.AllMaps)
            {
                throw new Exception("Cannot extract image for Maps.AllMaps");
            }

            var index = (int)map;

            var umap = Ultima.Map.Maps[index];

            if (umap == null)
            {
                throw new FileNotFoundException(string.Format("File {0} doens't exist. Impossible to extract the map.", map));
            }

            var step = (int)scale;
            var size = new Size(umap.Width, umap.Height);

            using (var bmp = new Bitmap(umap.Width, umap.Height, PixelFormat.Format16bppRgb555))
            {
                var b = new Rectangle(0, 0, size.Width >> 3, size.Height >> 3);

                umap.GetImage(0, 0, b.Width, b.Height, bmp, true);

                using (var tmp = new Bitmap(size.Width / step, size.Height / step))
                {
                    using (var g = Graphics.FromImage(tmp))
                    {
                        g.DrawImage(bmp, new Rectangle(0, 0, tmp.Width, tmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
                    }
                }

                bmp.Save(FileName, ImageFormat.Jpeg);
            }
        }
Esempio n. 3
0
        public virtual int GetZoom(MapScale scale)
        {
            int zoom = 0;
            switch (scale)
            {
                case MapScale.Big:
                    zoom = 17;
                    break;

                case MapScale.Medium:
                    zoom = 15;
                    break;

                case MapScale.Small:
                    zoom = 12;
                    break;
            }

            return zoom;
        }
Esempio n. 4
0
        /// <summary>
        /// Extracts an image displaying the full map
        /// </summary>
        /// <param name="map">The map that should be extracted</param>
        /// <param name="scale">The scale value</param>
        /// <param name="FileName">The target filename</param>
        public static unsafe void ExtractMap( Maps map, MapScale scale, string FileName )
        {
            if ( map == Maps.AllMaps )
            {
                throw new Exception( "Cannot extract image for Maps.AllMaps" );
            }

            int index = (int) map;

            string mapfile = m_MulManager[ "map{0}.mul", index ];
            string mapdif =m_MulManager[ "mapdif{0}.mul", index ];
            string mapdifl = m_MulManager[ "mapdifl{0}.mul", index ];
            string sta = m_MulManager[ "statics{0}.mul", index ];
            string staidx = m_MulManager[ "staidx{0}.mul", index ];
            string stadif = m_MulManager[ "stadif{0}.mul", index ];
            string stadifi = m_MulManager[ "stadifi{0}.mul", index ];
            string stadifl = m_MulManager[ "stadifl.mul", index ];
            string col = m_MulManager[ "radarcol.mul", index ];

            // Issue 15 - Trammel has own map after ML - http://code.google.com/p/pandorasbox3/issues/detail?id=15 - Kons
            if (index == 1 && (!File.Exists(mapfile)))
            {
                mapfile = m_MulManager["map0.mul"];
                sta = m_MulManager["statics0.mul"];
                staidx = m_MulManager["staidx0.mul"];
            }
            // Issue 15 - End.

            if ( !File.Exists( col ) )
            {
                throw new FileNotFoundException( "radarcol.mul not found." );
            }

            if ( !File.Exists( mapfile ) )
            {
                throw new FileNotFoundException( string.Format( "File {0} doens't exist. Impossible to extract the map.", mapfile ) );
            }

            bool mappatch = false;

            if ( File.Exists( mapdif ) && File.Exists( mapdifl ) )
                mappatch = true;

            bool statics = false;

            if ( File.Exists( sta ) && File.Exists( staidx ) )
                statics = true;

            bool stapatch = false;

            if ( File.Exists( stadif ) && File.Exists( stadifi ) && File.Exists( stadifl ) )
                stapatch = true;

            if ( scale == MapScale.Eigth || scale == MapScale.Sixteenth )
                statics = false;

            int blocksDelta = 1;
            if ( scale == MapScale.Sixteenth )
                blocksDelta = 2;

            #region ColorMap
            short[] ColorMap = new short[ 65536 ];

            FileStream colstream = new FileStream( col, FileMode.Open );

            int n = 0;

            fixed ( short* pColorMap = ColorMap )
            {
                // Issue 7 - Handle Warnings - http://code.google.com/p/pandorasbox3/issues/detail?id=7&can=1 - Kons
                ReadFile(colstream.SafeFileHandle.DangerousGetHandle(), pColorMap, 131072, &n, 0);
                // Issue 7 - End
            }

            colstream.Close();
            #endregion

            #region Streams, MapPatch and StaPatch

            FileStream mapstream = new FileStream( mapfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
            FileStream stastream = null;
            FileStream staidxstream = null;
            FileStream mapdifstream = null;
            FileStream mapdiflstream = null;
            FileStream stadifstream = null;
            FileStream stadifistream = null;
            FileStream stadiflstream = null;

            // Issue 10 - Update the code to Net Framework 3.5 - http://code.google.com/p/pandorasbox3/issues/detail?id=10 - Smjert
            Dictionary<int,int> MapPatch = null;
            Dictionary<int,int> StaPatch = null;
            // Issue 10 - End

            if ( statics )
            {
                stastream = new FileStream( sta, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
                staidxstream = new FileStream( staidx, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );

                if ( stapatch )
                {
                    stadifstream = new FileStream( stadif, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
                    stadifistream = new FileStream( stadifi, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
                    stadiflstream = new FileStream( stadifl, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );

                    // Issue 10 - Update the code to Net Framework 3.5 - http://code.google.com/p/pandorasbox3/issues/detail?id=10 - Smjert
                    StaPatch = new Dictionary<int, int>();
                    // Issue 10 - End

                    BinaryReader reader = new BinaryReader( stadiflstream );

                    int i = 0;

                    // Issue 1 - Char buffer too small Exception - http://code.google.com/p/pandorasbox3/issues/detail?id=1 - Smjert
                    while ( reader.BaseStream.Position < reader.BaseStream.Length )
                    // Issue 1 - End
                    {
                        int key = reader.ReadInt32();
                        StaPatch[ key ] = i++;
                    }
                }
            }

            if ( mappatch )
            {
                mapdifstream = new FileStream( mapdif, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
                mapdiflstream = new FileStream( mapdifl, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );

                // Issue 10 - Update the code to Net Framework 3.5 - http://code.google.com/p/pandorasbox3/issues/detail?id=10 - Smjert
                MapPatch = new Dictionary<int, int>();
                // Issue 10 - End

                BinaryReader reader = new BinaryReader( mapdiflstream );

                int i = 0;

                // Issue 1 - Char buffer too small Exception - http://code.google.com/p/pandorasbox3/issues/detail?id=1 - Smjert
                while (reader.BaseStream.Position < reader.BaseStream.Length)
                // Issue 1 - End
                {
                    int key = reader.ReadInt32();

                    MapPatch[ key ] = i++;
                }

                mapdiflstream.Close();
            }

            #endregion

            int PPB = 8 / (int) scale;
            if ( PPB < 1 )
                PPB = 1;

            int step = (int) scale;
            Size size = MapSizes.GetSize( index );

            int xblocks = size.Width / 8;
            int yblocks = size.Height / 8;

            size.Width = size.Width / step;
            size.Height = size.Height / step;

            Bitmap bmp = new Bitmap( size.Width, size.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555 );
            System.Drawing.Imaging.BitmapData bData = bmp.LockBits( new Rectangle( new Point( 0,0 ), size ), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format16bppRgb555 );

            short* pixelPtr = ( short* ) bData.Scan0;

            for ( int x = 0; x < xblocks; x += blocksDelta )
            {
                for ( int y = 0; y < yblocks; y += blocksDelta )
                {
                    int bindex = x * yblocks + y;

                    MapBlock bMap = null;

                    // Issue 10 - Update the code to Net Framework 3.5 - http://code.google.com/p/pandorasbox3/issues/detail?id=10 - Smjert
                    if ( mappatch && MapPatch.ContainsKey( bindex ) )
                    {
                        // Patched map block
                        mapdifstream.Seek( MapPatch[ bindex ] * MapBlock.Size, SeekOrigin.Begin );
                        // Issue 10 - End
                        bMap = MapBlock.ReadFromStream( mapdifstream );
                    }
                    else
                    {
                        mapstream.Seek( bindex * MapBlock.Size, SeekOrigin.Begin );
                        bMap = MapBlock.ReadFromStream( mapstream );
                    }

                    #region Statics

                    if ( statics )
                    {
                        StaticIdx idx;
                        StaticData[] sData = null;
                        int NumOfStatics = 0;

                        // Issue 10 - Update the code to Net Framework 3.5 - http://code.google.com/p/pandorasbox3/issues/detail?id=10 - Smjert
                        if ( stapatch && StaPatch.ContainsKey( bindex ) )
                        {
                            // Patch
                            stadifistream.Seek( StaPatch[ bindex ] * StaticIdx.Size, SeekOrigin.Begin );
                            // Issue 10 - End
                            staidxstream.Seek( StaticIdx.Size, SeekOrigin.Current );

                            idx = StaticIdx.ReadFromStream( stadifistream );

                            if ( idx.Start != -1 )
                            {
                                NumOfStatics = idx.Length / StaticData.Size;
                                stadifstream.Seek( idx.Start, SeekOrigin.Begin );
                                sData = StaticData.ReadFromStream( stadifstream, NumOfStatics );
                            }
                        }
                        else
                        {
                            // No patch
                            idx = StaticIdx.ReadFromStream( staidxstream );

                            if ( idx.Start != -1 )
                            {
                                NumOfStatics = idx.Length / StaticData.Size;
                                stastream.Seek( idx.Start, SeekOrigin.Begin );
                                sData = StaticData.ReadFromStream( stastream, NumOfStatics );
                            }
                        }

                        if ( NumOfStatics > 0 )
                        {
                            foreach ( StaticData sd in sData )
                            {
                                int sindex = sd.YOffset * 8 + sd.XOffest;

                                if ( bMap.Cells[ sindex ].Altitude <= sd.Altitude )
                                {
                                    bMap.Cells[ sindex ].Altitude = sd.Altitude;
                                    bMap.Cells[ sindex ].Color = (ushort) (sd.Color + StaticOffset);
                                }
                            }
                        }
                    }

                    #endregion

                    #region Drawing

                    if ( scale == MapScale.Sixteenth )
                    {
                        int x0 = x / 2;
                        int y0 = y / 2;

                        int delta = y0 * size.Width + x0;

                        pixelPtr[ delta ] = ColorMap[ bMap.Cells[0].Color ];
                    }
                    else
                    {
                        int x0 = x * PPB;
                        int y0 = y * PPB;

                        for ( int xc = 0; xc < PPB; xc++ )
                        {
                            for ( int yc = 0; yc < PPB; yc++ )
                            {
                                int cell = yc * step * 8 + xc * step;

                                int delta = ( y0 + yc ) * size.Width + x0 + xc;

                                pixelPtr[ delta ] = ColorMap[ bMap.Cells[ cell ].Color ];
                            }
                        }
                    }

                    #endregion
                }
            }

            bmp.UnlockBits( bData );

            bmp.Save( FileName, System.Drawing.Imaging.ImageFormat.Jpeg );

            bmp.Dispose();
        }
Esempio n. 5
0
        /// <summary>
        /// 地图鼠标移动事件
        /// </summary>
        private void picBoxMap_MouseMove(object sender, MouseEventArgs e)
        {
            isClick = false;
            switch (operationType)
            {
            case MapOperation.SelectElement:
                break;

            case MapOperation.ZoomIn:
                break;

            case MapOperation.ZoomOut:
                break;

            case MapOperation.Pan:      //漫游
                if (e.Button == MouseButtons.Left)
                {
                    int deltaX = e.X - mouseOldLoc.X;
                    int deltaY = e.Y - mouseOldLoc.Y;
                    centerXY     = ETCProjection.LngLat2XY(centerLngLat);
                    centerXY.X  -= deltaX * (float)ratio * (float)scaleChoice[scaleIndex];
                    centerXY.Y  += deltaY * (float)ratio * (float)scaleChoice[scaleIndex];     //由于屏幕坐标系是左上坐标系,和地理坐标系相反,所以这里应该是+
                    centerLngLat = ETCProjection.XY2LngLat(centerXY);
                    mouseOldLoc  = e.Location;
                    UpdateMapImg();
                }
                break;

            case MapOperation.SelectFeatures:      //选择要素
                if (e.Button == MouseButtons.Left)
                {
                    picBoxMap.Refresh();
                    int minX = Math.Min(startPoint.X, e.Location.X);
                    int maxX = Math.Max(startPoint.X, e.Location.X);
                    int minY = Math.Min(startPoint.Y, e.Location.Y);
                    int maxY = Math.Max(startPoint.Y, e.Location.Y);
                    DrawSelectBox(minX, minY, maxX - minX, maxY - minY);      //画选择盒
                }
                break;

            case MapOperation.Edit:      //编辑要素
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    int deltaX = e.X - mouseOldLoc.X;
                    int deltaY = e.Y - mouseOldLoc.Y;
                    myMap.Layers[EditingIndex].MoveSelectedFeature(deltaX, deltaY, picBoxMap.Bounds, centerLngLat, ratio * scaleChoice[scaleIndex]);
                    mouseOldLoc = e.Location;
                    UpdateMapImg();
                }
                break;

            case MapOperation.EditVertices:      //编辑要素顶点
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    picBoxMap.Cursor = Cursors.SizeAll;
                    myMap.Layers[EditingIndex].MoveVertex(ScreenToWGS84(e.Location));
                    UpdateMapImg();
                }
                break;

            case MapOperation.CreateFeatures:      //创建要素
                if (trackingPoints.Count > 0)
                {
                    picBoxMap.Refresh();
                    DrawTrackingFeature();
                    DrawRubberBand(e.Location);      //画橡皮筋
                }
                break;
            }
            //更新鼠标当前位置对应地理坐标
            MyPoint mouseLngLat = ScreenToWGS84(e.Location);

            StatusStripLblCoordinate.Text = mouseLngLat.X.ToString() + "," + mouseLngLat.Y.ToString();
            StatusStripLblScale.Text      = MapScale.ToString();
        }