示例#1
0
        private ShortPoint GetBorders(int x, int y, Direction direction, TileType areaType,
                                      ServerMap map, out bool invalid)
        {
            invalid = true;

            ushort w = map.Width, h = map.Height;

            if (TileBorders.IsNotValidTile(x, y, w, h, areaType, map))
            {
                switch (direction)
                {
                case Direction.W:
                    y += 1;
                    break;

                case Direction.E:
                case Direction.S:
                    x += 1;
                    break;
                }
            }

            if (x < 0 || x >= w || y < 0 || y >= h)
            {
                return(ShortPoint.Empty);
            }

            invalid = TileBorders.GetBorders(x, y, w, h, areaType, map) == 0;
            return(new ShortPoint(x, y));
        }
示例#2
0
        public static ShortPoint? FindPlayerStartPoint(ServerMap map, Player player,
            int minAreaSpace)
        {
            var areas = map.SpaceAreas;
            int iStart = _rnd.Next(areas.Count), iEnd = areas.Count;

            bool secondIter = false;
            for (int i = iStart; i <= iEnd; i++)
            {
                if (i == iEnd)
                {
                    if (secondIter)
                    {
                        return null;
                    }
                    i = 0;
                    iEnd = iStart - 1;
                    secondIter = true;
                }
                MapArea area = areas[i];
                if (area.CellsCount * ConstMap.PIXEL_SIZE_SQR >= minAreaSpace)
                {
                    int playerMargin = (int)Math.Floor((float)player.Size.HighValue / ConstMap.PIXEL_SIZE);
                    ShortPoint? cell = area.FindFreeCell(map, playerMargin, _rnd);
                    if (cell != null)
                    {
                        return cell;
                    }
                }
            }

            return null;
        }
示例#3
0
 private void AcceptBorders(ShortPoint borderPoint, bool invalid, ServerMap map, HashSet <int> unique)
 {
     if (!invalid && !unique.Contains(borderPoint.Mark))
     {
         Add(borderPoint);
         unique.Add(borderPoint.Mark);
     }
 }
示例#4
0
        internal MapArea(List <TraceCell> closedTrace, TileType areaType, ServerMap map)
            : base(closedTrace.Count)
        {
            TraceCell     prevTraceCell = null;
            HashSet <int> unique        = new HashSet <int>();

            foreach (TraceCell traceCell in closedTrace)
            {
                ParseArea(traceCell, prevTraceCell, areaType, map, unique);
                prevTraceCell = traceCell;
            }
            CalculateCellsCount();
        }
示例#5
0
        private void ParseArea(TraceCell traceCell, TraceCell prevTraceCell, TileType areaType,
                               ServerMap map, HashSet <int> unique)
        {
            bool       invalid;
            ShortPoint borderPoint;

            if (prevTraceCell != null && prevTraceCell.Direction == Direction.S && traceCell.Direction == Direction.W)
            {
                borderPoint = GetBorders(traceCell.X, traceCell.Y, Direction.S, areaType, map, out invalid);
                AcceptBorders(borderPoint, invalid, map, unique);
            }
            borderPoint = GetBorders(traceCell.X, traceCell.Y, traceCell.Direction, areaType, map, out invalid);
            AcceptBorders(borderPoint, invalid, map, unique);
        }
示例#6
0
        public ShortPoint?FindFreeCell(ServerMap map, int margin, Random rnd)
        {
            margin += 3;

            int iStart = rnd.Next(Count), iEnd = Count;

            bool secondIter = false;

            for (int i = iStart; i <= iEnd; i++)
            {
                if (i == iEnd)
                {
                    if (secondIter)
                    {
                        return(null);
                    }
                    i          = 0;
                    iEnd       = iStart - 1;
                    secondIter = true;
                }
                ShortPoint cell = this[i];
                int        x1 = cell.X, x2 = cell.X + margin;
                int        y1 = cell.Y, y2 = cell.Y + margin;
                if (IsPointInside(x2, y2))
                {
                    bool success = true;
                    for (int y = y1; y <= y2; y++)
                    {
                        for (int x = x1; x <= x2; x++)
                        {
                            success &= (*map[(ushort)x, (ushort)y]).Type == TileType.Nothing;
                            if (!success)
                            {
                                break;
                            }
                        }
                        if (!success)
                        {
                            break;
                        }
                    }
                    if (success)
                    {
                        return(new ShortPoint(x2 - 2, y2 - 2));
                    }
                }
            }
            return(null);
        }
示例#7
0
        internal void TestGameMapPerf()
        {
            const ushort mapWidth = 1000, mapHeight = 1000;

            int count = mapWidth * mapHeight;
            HRTimer timer = HRTimer.CreateAndStart();

#if UNSAFE_ARRAY
            int sizeOfTile = Marshal.SizeOf(typeof(Tile));
            Tile* pTiles = (Tile*)Memory.HeapAlloc(count * sizeOfTile);
#else
            Tile[,] aTiles = new Tile[MapWidth, MapHeight];
#endif
            System.Console.WriteLine(timer.StopWatch());

            timer = HRTimer.CreateAndStart();
            for (int i = 0; i < count; i++)
            {
#if !UNSAFE_ARRAY
                fixed (Tile* pTiles = aTiles)
#endif
                {
                    pTiles[i].Type = TileType.Wall;
                    pTiles[i].TypeIndex = 100;
                }
            }
            System.Console.WriteLine(timer.StopWatch());
#if UNSAFE_ARRAY
            Memory.HeapFree(pTiles);
#endif

            using (ServerMap map = new ServerMap(mapWidth, mapHeight, 0))
            {
                timer = HRTimer.CreateAndStart();

                for (ushort y = 0; y < mapHeight; y++)
                {
                    for (ushort x = 0; x < mapWidth; x++)
                    {
                        Tile* tile = map[x, y];
                        (*tile).Type = TileType.Nothing;
                        (*tile).TypeIndex = 1;
                    }
                }
                System.Console.WriteLine(timer.StopWatch());
            }
        }
示例#8
0
        public static ServerMap LoadFromMemory(byte[] data)
        {
            ServerMap map = new ServerMap();

            using (MemoryStream ms = new MemoryStream(data))
            {
                using (BinaryReader br = new BinaryReader(ms))
                {
                    short magicNum = br.ReadInt16();
                    if (magicNum != MAGICNUM)
                    {
                        return(null);
                    }

                    while (br.BaseStream.Position < br.BaseStream.Length)
                    {
                        SectionType type        = (SectionType)br.ReadByte();
                        int         sectionSize = br.ReadInt32();
                        if (map._sections.ContainsKey(type))
                        {
                            if (!ReadSection(map._sections[type], sectionSize, br))
                            {
                                return(null);
                            }
                        }
                        else
                        {
                            br.BaseStream.Seek(sectionSize, SeekOrigin.Current);
                        }
                    }
                }
            }

            map.RecreateMiniMap();
            map.DetectAreas();

            return(map);
        }
示例#9
0
        public static int DirectionValue(int x, int y, TileType areaType, ServerMap map)
        {
            int    flag = 0;
            ushort w = map.Width, h = map.Height;

            if (IsValidTile(x, y, w, h, areaType, map))
            {
                flag |= 1;
            }
            if (IsValidTile(x + 1, y, w, h, areaType, map))
            {
                flag |= 2;
            }
            if (IsValidTile(x, y + 1, w, h, areaType, map))
            {
                flag |= 4;
            }
            if (IsValidTile(x + 1, y + 1, w, h, areaType, map))
            {
                flag |= 8;
            }
            return(flag);
        }
示例#10
0
        private unsafe void BtnGenerateLabyrinthClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            FractalType type = (FractalType) cbLabyrinthType.SelectedItem;
            BasisTypes basis = (BasisTypes) cbLabyrinthBasis.SelectedItem;
            InterpTypes interp = (InterpTypes) cbLabyrinthInterp.SelectedItem;
            int? octaves = !string.IsNullOrEmpty(tbLabyrinthOctaves.Text)
                ? int.Parse(tbLabyrinthOctaves.Text)
                : (int?) null;
            double? frequency = !string.IsNullOrEmpty(tbLabyrinthFrequency.Text)
                ? double.Parse(tbLabyrinthFrequency.Text)
                : (double?) null;
            double? angle = !string.IsNullOrEmpty(tbLabyrinthAngle.Text)
                ? double.Parse(tbLabyrinthAngle.Text)
                : (double?) null;
            double? lacunarity = !string.IsNullOrEmpty(tbLabyrinthLacunarity.Text)
                ? double.Parse(tbLabyrinthLacunarity.Text)
                : (double?) null;
            _labOldSeed = cbLabyrinthRnd.Checked ? (uint?) Environment.TickCount : _labOldSeed;

            ModuleBase moduleBase = new Fractal(type, basis, interp, octaves, frequency, _labOldSeed,
                angle, lacunarity);

            if (chkLabyrinthAC.Checked)
            {
                double acLow = double.Parse(tbLabyrinthACLow.Text);
                double acHigh = double.Parse(tbLabyrinthACHigh.Text);
                CombinerTypes combType = (CombinerTypes) cbLabyrinthACCombine.SelectedItem;
                AutoCorrect correct = new AutoCorrect(moduleBase, acLow, acHigh);
                moduleBase = new Combiner(combType, correct, moduleBase);
            }

            //            Bias bias = new Bias(moduleBase, 0.01);
            //            Gradient gradient = new Gradient(0, 0, 50, 100);
            //            moduleBase = new TranslatedDomain(moduleBase, gradient, bias);

            if (chkLabyrinthSel.Checked)
            {
                double selLow = double.Parse(tbLabyrinthSelLow.Text);
                double selHigh = double.Parse(tbLabyrinthSelHigh.Text);
                double selThreshold = double.Parse(tbLabyrinthSelThreshold.Text);
                double? selFalloff = !string.IsNullOrEmpty(tbLabyrinthSelFalloff.Text)
                    ? double.Parse(tbLabyrinthSelFalloff.Text)
                    : (double?) null;
                moduleBase = new Select(moduleBase, selLow, selHigh, selThreshold, selFalloff);
            }

            if (pbLabyrinth.Image != null)
            {
                pbLabyrinth.Image.Dispose();
            }

            ushort width = 500, height = 500;
            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
            BitmapData data = bitmap.LockBits(new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            byte* pRoughMap = (byte*)Memory.HeapAlloc(width * height);
            Parallel.For(0, height, y =>
            {
                int* row = (int*) data.Scan0 + (y*data.Stride)/4;
                Parallel.For(0, width, x =>
                {
                    double p = (double) x/width;
                    double q = (double) y/height;
                    double val = moduleBase.Get(p, q);
                    pRoughMap[y * width + x] = (byte)Math.Abs(val - 1);
                    Color color = Color.Black.Lerp(Color.White, val);
                    row[x] = color.ToArgb();
                });
            });

            using (ServerMap map = new ServerMap(width, height, 0, pRoughMap))
            {
                map.SaveToFile("RK.save");
            }
            Memory.HeapFree(pRoughMap);

            bitmap.UnlockBits(data);
            pbLabyrinth.Image = bitmap;

            Cursor = DefaultCursor;
        }
示例#11
0
 public TilesSection(ServerMap map) : base(map)
 {
 }
示例#12
0
 public MapSection(ServerMap map) : base(map)
 {
 }
示例#13
0
 public HeaderSection(ServerMap map) : base(map)
 {
 }
示例#14
0
 protected SectionBase(ServerMap map)
 {
     Map = map;
 }
示例#15
0
 public TilesSection(ServerMap map)
     : base(map)
 {
 }
示例#16
0
 protected SectionBase(ServerMap map)
 {
     Map = map;
 }
示例#17
0
 public MapSection(ServerMap map)
     : base(map)
 {
 }
示例#18
0
 public HeaderSection(ServerMap map)
     : base(map)
 {
 }
示例#19
0
        public static ServerMap LoadFromMemory(byte[] data)
        {
            ServerMap map = new ServerMap();
            using (MemoryStream ms = new MemoryStream(data))
            {
                using (BinaryReader br = new BinaryReader(ms))
                {
                    short magicNum = br.ReadInt16();
                    if (magicNum != MAGICNUM)
                    {
                        return null;
                    }

                    while (br.BaseStream.Position < br.BaseStream.Length)
                    {
                        SectionType type = (SectionType) br.ReadByte();
                        int sectionSize = br.ReadInt32();
                        if (map._sections.ContainsKey(type))
                        {
                            if (!ReadSection(map._sections[type], sectionSize, br))
                            {
                                return null;
                            }
                        }
                        else
                        {
                            br.BaseStream.Seek(sectionSize, SeekOrigin.Current);
                        }
                    }
                }
            }

            map.RecreateMiniMap();
            map.DetectAreas();

            return map;
        }
示例#20
0
 public MapAreas(ServerMap map, TileType areaType)
 {
     _map      = map;
     _areaType = areaType;
 }
示例#21
0
 private static bool IsValidTile(int x, int y, ushort w, ushort h, TileType areaType, ServerMap map)
 {
     return(x >= 0 && x < w && y >= 0 && y < h && (*map[(ushort)x, (ushort)y]).Type == areaType);
 }