private void InitializeMap()
        {
            byte[] bmap   = null;
            int    startX = 0;
            int    startY = 0;
            int    endX   = 0;
            int    endY   = 0;

            //
            m_nWidth  = m_nCnstWidth;
            m_nHeight = m_nCnstHeight;
            //
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
            this.UpdateStyles();
            //
            m_bmpDrawingSpace = new Bitmap(m_nWidth * m_nBlockPixel, m_nHeight * m_nBlockPixel);
            //
            if (DllAPI.RobotCreateEmptyMap(m_nWidth, m_nHeight, ref startX, ref startY, ref endX, ref endY) != 0)
            {
                return;
            }
            m_pntRobot.X    = startX;
            m_pntRobot.Y    = startY;
            m_pntEndPoint.X = endX;
            m_pntEndPoint.Y = endY;
            if (DllAPI.getMap(ref bmap) != 0)
            {
                return;
            }
            RePaint(bmap);
            ReSizeForm();
        }
        private void 随机生成地图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            byte[] bmap   = null;
            int    startX = 0;
            int    startY = 0;
            int    endX   = 0;
            int    endY   = 0;

            ChangeState(State.Edit);
            //
            m_nWidth  = m_nCnstWidth;
            m_nHeight = m_nCnstHeight;
            //
            if (DllAPI.RobotCreateRandomMap(m_nWidth, m_nHeight, ref startX, ref startY, ref endX, ref endY, 0.2f) != 0)
            {
                return;
            }
            m_pntRobot.X    = startX;
            m_pntRobot.Y    = startY;
            m_pntEndPoint.X = endX;
            m_pntEndPoint.Y = endY;
            if (DllAPI.getMap(ref bmap) != 0)
            {
                return;
            }
            RePaint(bmap);
            ReSizeForm();
            this.Invalidate();
            this.Update();
        }
 private void ReFindPath()
 {
     //m_aPath = null;
     //m_nPathLen = 0;
     //m_nPos = 0;
     //清除前面的路径标记
     for (int i = 0; i < m_nPathLen - 2; i += 2)
     {
         byte _data = 0;
         DllAPI.ReadMapByte(m_aPath[i], m_aPath[i + 1], ref _data);
         if (_data == (byte)BlockType.Blank)
         {
             RePaint(m_aPath[i], m_aPath[i + 1], (byte)BlockType.Blank);
         }
     }
     //
     DllAPI.setRobotPoint(m_pntRobot.X, m_pntRobot.Y);
     DllAPI.setEndPoint(m_pntEndPoint.X, m_pntEndPoint.Y);
     if (DllAPI.RobotPathPlan(ref m_aPath, ref m_nPathLen) != 0)
     {
         return;
     }
     //显示路径标记
     for (int i = 0; i < m_nPathLen - 2; i += 2)
     {
         RePaint(m_aPath[i], m_aPath[i + 1], (byte)BlockType.Path);
     }
     this.Invalidate();
     this.Update();
 }
        private void UpdateBlock(int x, int y, BlockType data)
        {
            byte _data = 0;

            if (x < 0 || x >= m_nWidth || y < 0 || y >= m_nHeight)
            {
                return;
            }
            DllAPI.ReadMapByte(x, y, ref _data);
            if (data == BlockType.Robot)
            {
                if (x == m_pntEndPoint.X && y == m_pntEndPoint.Y || _data != (byte)BlockType.Blank)
                {
                    return;
                }
                RePaint(m_pntRobot.X, m_pntRobot.Y, (byte)BlockType.Blank);
                m_pntRobot.X = x;
                m_pntRobot.Y = y;
                RePaint(x, y, (byte)BlockType.Robot);
                DllAPI.setRobotPoint(x, y);
            }
            else if (data == BlockType.Goal)
            {
                if (x == m_pntRobot.X && y == m_pntRobot.Y || _data != (byte)BlockType.Blank)
                {
                    return;
                }
                RePaint(m_pntEndPoint.X, m_pntEndPoint.Y, (byte)BlockType.Blank);
                m_pntEndPoint.X = x;
                m_pntEndPoint.Y = y;
                RePaint(x, y, (byte)BlockType.Goal);
                DllAPI.setEndPoint(x, y);
            }
            else if (data == BlockType.Blank)
            {
                if (x == m_pntRobot.X && y == m_pntRobot.Y || x == m_pntEndPoint.X && y == m_pntEndPoint.Y)
                {
                    return;
                }
                RePaint(x, y, (byte)BlockType.Blank);
                DllAPI.UpdateMap(x, y, (byte)data);
            }
            else if (data == BlockType.Wall)
            {
                if (x == m_pntRobot.X && y == m_pntRobot.Y || x == m_pntEndPoint.X && y == m_pntEndPoint.Y)
                {
                    return;
                }
                RePaint(x, y, (byte)BlockType.Wall);
                DllAPI.UpdateMap(x, y, (byte)BlockType.Wall);
            }
            this.Invalidate();
            this.Update();
        }
Example #5
0
        /// <summary>
        /// 只需要对Blank和Wall更新,Robot和EndPoint用SetRobotPoint/SetEndPoint就行了
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static int UpdateMap(int x, int y, byte data)
        {
            IntPtr pMap    = IntPtr.Zero;
            int    _width  = DllAPI.getWidth();
            int    _height = DllAPI.getHeight();

            if (x < 0 || x >= _width)
            {
                return(-1);
            }
            if (y < 0 || y >= _height)
            {
                return(-1);
            }
            DllAPI.getMap(ref pMap);
            Marshal.WriteByte(pMap, x + y * _width, data);
            return(0);
        }
Example #6
0
        public static int getMap(ref byte[] bmap)
        {
            int    nRet;
            IntPtr pMap = IntPtr.Zero;
            int    nLen = DllAPI.getWidth() * DllAPI.getHeight();

            if (nLen <= 0)
            {
                return(1);
            }
            bmap = new byte[nLen];
            if ((nRet = DllAPI.getMap(ref pMap)) != 0)
            {
                return(nRet);
            }
            Marshal.Copy(pMap, bmap, 0, nLen);
            return(0);
        }
Example #7
0
        public static int ReadMapByte(int x, int y, ref byte data)
        {
            IntPtr pMap    = IntPtr.Zero;
            int    _width  = DllAPI.getWidth();
            int    _height = DllAPI.getHeight();

            if (x < 0 || x >= _width)
            {
                return(-1);
            }
            if (y < 0 || y >= _height)
            {
                return(-1);
            }
            DllAPI.getMap(ref pMap);
            data = Marshal.ReadByte(pMap, x + y * _width);
            return(0);
        }
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            string         filePath;

            ChangeState(State.Edit);
            dialog.Filter           = "地图文件|*.mp";
            dialog.CheckPathExists  = true;
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                filePath = dialog.FileName;
                if (DllAPI.RobotSaveMap(filePath) != 0)
                {
                    return;
                }
            }
        }
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            string         filePath;

            byte[] bMap = null;
            int    startX = 0, startY = 0, endX = 0, endY = 0;

            //
            ChangeState(State.Edit);
            //
            dialog.Filter           = "地图文件|*.mp|所有文件|*.*";
            dialog.CheckFileExists  = true;
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                filePath = dialog.FileName;
                DllAPI.RobotLoadMap(filePath, ref startX, ref startY, ref endX, ref endY);
                m_pntRobot.X    = startX;
                m_pntRobot.Y    = startY;
                m_pntEndPoint.X = endX;
                m_pntEndPoint.Y = endY;
                m_nWidth        = DllAPI.getWidth();
                m_nHeight       = DllAPI.getHeight();
                if (DllAPI.getMap(ref bMap) != 0)
                {
                    return;
                }
                //
                m_bmpDrawingSpace = new Bitmap(m_nWidth * m_nBlockPixel, m_nHeight * m_nBlockPixel);
                //
                RePaint(bMap);
                ReSizeForm();
                this.Invalidate();
                this.Update();
            }
        }