示例#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);
        }
示例#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);
        }
示例#3
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);
        }
示例#4
0
        public HashSet <uint> BuildRegionHashSet(out string errMsg)
        {
            var bezierCurve = _map.GetBezierCurve(_region.BezierCurveId);

            if (bezierCurve == null)
            {
                errMsg = "RegionBuilder.BuildRegionHashSet: bezierCurve is null! BezierCurveId: " + _region.BezierCurveId;
                return(null);
            }
            if (!bezierCurve.Closed)
            {
                errMsg = "RegionBuilder.BuildRegionHashSet: bezierCurve is not Closed! BezierCurveId: " + _region.BezierCurveId;
                return(null);
            }

            errMsg = "";

            float   fInvGridSize = 1.0f / _mapGridSize;
            Vector2 mins         = _mapRect.max;
            Vector2 maxs         = _mapRect.min;

            _polygon.Clear();
            foreach (var point in bezierCurve.BezierNodes)
            {
                Vector2 v = new Vector2(point.PositionX, point.PositionZ);
                _polygon.Add(v);

                if (v.x < mins.x)
                {
                    mins.x = v.x;
                }

                if (v.y < mins.y)
                {
                    mins.y = v.y;
                }

                if (v.x > maxs.x)
                {
                    maxs.x = v.x;
                }

                if (v.y > maxs.y)
                {
                    maxs.y = v.y;
                }
            }

            Rect rcRegion = new Rect(mins, maxs - mins);

            int            nWidth    = (int)(_mapXLen * fInvGridSize);
            int            nHeight   = (int)(_mapZLen * fInvGridSize);
            HashSet <uint> regionSet = new HashSet <uint>();
            Vector2        vOrg      = new Vector2(_mapRect.xMin + 0.5f * _mapGridSize, _mapRect.yMax - 0.5f * _mapGridSize);

            for (int h = 0; h < nHeight; ++h)
            {
                for (int w = 0; w < nWidth; ++w)
                {
                    Vector2 vPos = vOrg + new Vector2(w * _mapGridSize, -h * _mapGridSize);
                    if (!RegionUtility.IsPointInRect(vPos, rcRegion))     //快速判断点是否在region的rect内
                    {
                        continue;
                    }

                    if (PolygonFunc.IsInPolygon2(vPos.x, vPos.y, _polygon))
                    {
                        uint col  = (uint)w;
                        uint row  = (uint)h;
                        uint data = (col | row << 16);
                        regionSet.Add(data);
                    }
                }
            }

            return(regionSet);
        }
示例#5
0
        public HashSet <uint> BuildRegionHashSet(out string errMsg)
        {
            if (_template == null)
            {
                errMsg = "ObstacleBuilder.BuildRegionHashSet: _template is null! ObstacleId: " + _obstacle.EntityInfos[0].EntityId;
                return(null);
            }

            errMsg = "";

            //欧拉角到方向
            var     quaternion = Quaternion.Euler(_obstacle.RotationX, _obstacle.RotationY, _obstacle.RotationZ);
            Vector3 vDir       = quaternion * Vector3.forward;

            vDir.y = 0;
            vDir.Normalize();
            Vector3 vCenter = new Vector3(_obstacle.PositionX, _obstacle.PositionY, _obstacle.PositionZ);

            Vector3 ExtX;
            Vector3 ExtY;
            Vector3 ExtZ;
            Vector3 vExtent = new Vector3(_obstacle.Length * 0.5f, _template.Height * 0.5f, _template.Width * 0.5f);

            Matrix4x4 mat;

            RegionUtility.a3d_TransformMatrix(vDir, Vector3.up, vCenter, out mat);
            ExtX = new Vector3(mat.m00, mat.m10, mat.m20) * vExtent.x;
            ExtY = new Vector3(mat.m01, mat.m11, mat.m21) * vExtent.y;
            ExtZ = new Vector3(mat.m02, mat.m12, mat.m22) * vExtent.z;

            float   fInvGridSize = 1.0f / _mapGridSize;
            Vector2 mins         = _mapRect.max;
            Vector2 maxs         = _mapRect.min;

            Vector3[] points = new Vector3[4];
            points[0] = vCenter - ExtX + ExtY + ExtZ;
            points[1] = points[0] + 2 * ExtX;
            points[2] = points[1] - 2 * ExtZ;
            points[3] = points[2] - 2 * ExtX;

            _polygon.Clear();
            foreach (var point in points)
            {
                Vector2 v = new Vector2(point.x, point.z);
                _polygon.Add(v);

                if (v.x < mins.x)
                {
                    mins.x = v.x;
                }

                if (v.y < mins.y)
                {
                    mins.y = v.y;
                }

                if (v.x > maxs.x)
                {
                    maxs.x = v.x;
                }

                if (v.y > maxs.y)
                {
                    maxs.y = v.y;
                }
            }
            Rect rcRegion = new Rect(mins, maxs - mins);

            int            nWidth    = (int)(_mapXLen * fInvGridSize);
            int            nHeight   = (int)(_mapZLen * fInvGridSize);
            HashSet <uint> regionSet = new HashSet <uint>();
            Vector2        vOrg      = new Vector2(_mapRect.xMin + 0.5f * _mapGridSize, _mapRect.yMax - 0.5f * _mapGridSize);

            for (int h = 0; h < nHeight; ++h)
            {
                for (int w = 0; w < nWidth; ++w)
                {
                    Vector2 vPos = vOrg + new Vector2(w * _mapGridSize, -h * _mapGridSize);
                    if (!RegionUtility.IsPointInRect(vPos, rcRegion))     //快速判断点是否在region的rect内
                    {
                        continue;
                    }

                    if (PolygonFunc.IsInPolygon2(vPos.x, vPos.y, _polygon))
                    {
                        uint col  = (uint)w;
                        uint row  = (uint)h;
                        uint data = (col | row << 16);
                        regionSet.Add(data);
                    }
                }
            }

            if (regionSet.Count == 0)
            {
                errMsg = HobaText.Format("ObstacleBuilder.BuildRegionHashSet: RegionSet Count is zero! ObstacleId: {0}, EntityGeneratorId: {1} generator len: {2}, template wid: {3}, template hei: {4}",
                                         _obstacle.EntityInfos[0].EntityId, _obstacle.Id, _obstacle.Length, _template.Width, _template.Height);
            }

            return(regionSet);
        }