Esempio n. 1
0
        public bool ReadFromMemory(byte[] filebuffer, bool skipId0 = false)
        {
            _regionOrderList.Clear();
            _regionMap.Clear();
            try
            {
                byte[] bytes;
                using (MemoryStream fs = new MemoryStream(filebuffer))
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        bytes  = br.ReadBytes(Marshal.SizeOf(typeof(SMapRegionHeader)));
                        Header = (SMapRegionHeader)RegionUtility.ByteToStruct(bytes, typeof(SMapRegionHeader));

                        for (int i = 0; i < Header.iNumRegion; ++i)
                        {
                            int id       = br.ReadInt32();
                            int nGridNum = br.ReadInt32();
                            if (id == 0 && skipId0)                 //跳过id0
                            {
                                //跳过读取数据
                                br.ReadBytes(nGridNum * Marshal.SizeOf(typeof(uint)));
                                continue;
                            }

                            //Vector2 mins = new Vector2(99999.0f, 99999.0f);
                            //Vector2 maxs = new Vector2(-99999.0f, -99999.0f);
                            HashSet <uint> gridSet = new HashSet <uint>();
                            for (int k = 0; k < nGridNum; ++k)
                            {
                                uint data = br.ReadUInt32();
                                gridSet.Add(data);
                            }

                            FileRegion fileRegion = new FileRegion();
                            fileRegion.Id            = id;
                            fileRegion.GridNum       = nGridNum;
                            fileRegion.RegionGridSet = gridSet;

                            //添加region
                            _regionMap.Add(fileRegion.Id, fileRegion);
                            _regionOrderList.Add(fileRegion);
                        }
                    }
                }
            }
            catch (IOException)
            {
                return(false);
            }

            _regionOrderList.Sort(CompareFileRegion);

            _fInvGridSize = 1.0f / Header.fGridSize;
            _fLeft        = -Header.fWorldWid * 0.5f;
            _fTop         = Header.fWorldLen * 0.5f;

            return(true);
        }
Esempio n. 2
0
        public bool ReadFile(string filename)
        {
            _regionOrderList.Clear();
            _regionMap.Clear();

            if (!File.Exists(filename))
            {
                return(false);
            }

            try
            {
                byte[] bytes;
                using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        bytes  = br.ReadBytes(Marshal.SizeOf(typeof(SMapRegionHeader)));
                        Header = (SMapRegionHeader)RegionUtility.ByteToStruct(bytes, typeof(SMapRegionHeader));

                        for (int i = 0; i < Header.iNumRegion; ++i)
                        {
                            FileRegion fileRegion = new FileRegion();

                            fileRegion.Id      = br.ReadInt32();
                            fileRegion.GridNum = br.ReadInt32();

                            HashSet <uint> gridSet = new HashSet <uint>();
                            for (int k = 0; k < fileRegion.GridNum; ++k)
                            {
                                uint data = br.ReadUInt32();
                                gridSet.Add(data);
                            }
                            fileRegion.RegionGridSet = gridSet;

                            //添加region
                            _regionMap.Add(fileRegion.Id, fileRegion);
                            _regionOrderList.Add(fileRegion);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(false);
            }

            _regionOrderList.Sort(CompareFileRegion);

            _fInvGridSize = 1.0f / Header.fGridSize;
            _fLeft        = -Header.fWorldWid * 0.5f;
            _fTop         = Header.fWorldLen * 0.5f;

            return(true);
        }
Esempio n. 3
0
 private int CompareFileRegion(FileRegion a, FileRegion b)
 {
     if (a.Id < b.Id)
     {
         return(-1);
     }
     else if (a.Id > b.Id)
     {
         return(1);
     }
     else
     {
         return(0);
     }
 }
Esempio n. 4
0
        public FileMapRegion CreateFileMapRegion(float fGridSize, out string errMsg)
        {
            FileMapRegion fileMapRegion = new FileMapRegion();

            var obstacles = _map.EntityGeneratorRoot.EntityGenerators;

            //build header
            fileMapRegion.Header.magic     = new byte[] { (byte)'M', (byte)'R', (byte)'G', (byte)'N' };
            fileMapRegion.Header.version   = FileMapRegion.VERSION;
            fileMapRegion.Header.fWorldWid = _map.Width;
            fileMapRegion.Header.fWorldLen = _map.Length;
            fileMapRegion.Header.fGridSize = fGridSize;

            errMsg = "";
            foreach (var obstacle in obstacles)
            {
                if (obstacle.EntityType != (int)EntityType.Obstacle)
                {
                    continue;
                }

                string          err;
                ObstacleBuilder builder = new ObstacleBuilder(_map, obstacle, fGridSize);
                HashSet <uint>  gridSet = builder.BuildRegionHashSet(out err);
                if (gridSet == null || gridSet.Count == 0)
                {
                    errMsg += err;
                    errMsg += "\n";
                    continue;
                }

                FileRegion fileRegion = new FileRegion();
                fileRegion.Id            = obstacle.Id;
                fileRegion.GridNum       = gridSet.Count;
                fileRegion.RegionGridSet = gridSet;

                //add file region
                fileMapRegion.AddFileRegion(fileRegion);
            }
            fileMapRegion.Header.iNumRegion = fileMapRegion.RegionMap.Count;

            return(fileMapRegion);
        }
Esempio n. 5
0
        public FileMapRegion CreateFileMapRegion(float fGridSize, out string errMsg)
        {
            FileMapRegion fileMapRegion = new FileMapRegion();

            var regions = _map.RegionRoot.Regions;

            //build header
            fileMapRegion.Header.magic      = new byte[] { (byte)'M', (byte)'R', (byte)'G', (byte)'N' };
            fileMapRegion.Header.version    = 1;
            fileMapRegion.Header.fWorldWid  = _map.Width;
            fileMapRegion.Header.fWorldLen  = _map.Length;
            fileMapRegion.Header.fGridSize  = fGridSize;
            fileMapRegion.Header.iNumRegion = regions.Count;

            errMsg = "";
            foreach (var region in regions)
            {
                string         err;
                RegionBuilder  builder = new RegionBuilder(_map, region, fGridSize);
                HashSet <uint> gridSet = builder.BuildRegionHashSet(out err);
                if (gridSet == null)
                {
                    errMsg += err;
                    errMsg += "\n";
                    continue;
                }

                FileRegion fileRegion = new FileRegion();
                fileRegion.Id            = region.Id;
                fileRegion.GridNum       = gridSet.Count;
                fileRegion.RegionGridSet = gridSet;

                //add file region
                fileMapRegion.AddFileRegion(fileRegion);
            }

            return(fileMapRegion);
        }
Esempio n. 6
0
        private bool IsPosInAnyRegion(uint pos, out int minRegionId)
        {
            bool bFind = false;

            minRegionId = int.MaxValue;
            if (_PtToRegionSetMap.Count > 0)         //use pt to region map
            {
                minRegionId = 0;
                foreach (var v in _PtToRegionSetMap.Values)
                {
                    if (v.ContainsKey(pos) == false)
                    {
                        break;
                    }

                    int id = v[pos];
                    if (id < minRegionId)
                    {
                        minRegionId = id;
                    }
                    bFind = true;
                }
            }
            else              //iterate fileregion
            {
                for (int i = 0; i < _regionOrderList.Count; ++i)
                {
                    FileRegion fileRegion = _regionOrderList[i];
                    if (fileRegion.RegionGridSet.Contains(pos))
                    {
                        minRegionId = fileRegion.Id;
                        bFind       = true;
                        break;
                    }
                }
            }
            return(bFind);
        }
Esempio n. 7
0
        public bool WriteFile(string filename)
        {
            if (Header.version < FileMapRegion.VERSION)
            {
                return(false);
            }

            try
            {
                FileStream   fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite);
                BinaryWriter bw = new BinaryWriter(fs);

                byte[] headerBytes = RegionUtility.StructToBytes(Header, Marshal.SizeOf(typeof(SMapRegionHeader)));
                bw.Write(headerBytes);

                for (int i = 0; i < _regionOrderList.Count; ++i)
                {
                    FileRegion fileRegion = _regionOrderList[i];

                    bw.Write(fileRegion.Id);
                    bw.Write(fileRegion.GridNum);
                    foreach (uint d in fileRegion.RegionGridSet)
                    {
                        bw.Write(d);
                    }
                }

                bw.Close();
                fs.Close();
            }
            catch (IOException)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 8
0
 public void AddFileRegion(FileRegion fileRegion)
 {
     _regionMap.Add(fileRegion.Id, fileRegion);
     _regionOrderList.Add(fileRegion);
     _regionOrderList.Sort(CompareFileRegion);
 }