コード例 #1
0
ファイル: WorldArchive.cs プロジェクト: EEmulator/EEmulator
        public static Bitmap GenerateMinimap(this DatabaseObject world)
        {
            if (world == null)
            {
                throw new ArgumentNullException(nameof(world));
            }

            var colors = JObject.Parse(File.ReadAllText("includes//colors.json"));
            var width  = world.GetInt("width", 200);
            var height = world.GetInt("height", 200);

            Color GetColor(int bid) => Color.FromArgb(
                colors[bid.ToString()].SelectToken("a").Value <int>(),
                colors[bid.ToString()].SelectToken("r").Value <int>(),
                colors[bid.ToString()].SelectToken("g").Value <int>(),
                colors[bid.ToString()].SelectToken("b").Value <int>());

            Color GetColorFromARGB(uint color) =>
            Color.FromArgb((byte)(color >> 24),
                           (byte)((color >> 16) & 0b11111111),
                           (byte)((color >> 8) & 0b11111111),
                           (byte)(color & 0b11111111));

            var bitmap = new Bitmap(width, height);

            using (var fb = bitmap.FastLock())
            {
                if (world.ContainsKey("backgroundColor"))
                {
                    fb.Clear(GetColorFromARGB(world.GetUInt("backgroundColor")));
                }
                else
                {
                    fb.Clear(Color.Black);
                }
            }

            var world_data = world.GetArray("worlddata", null);

            // complex object
            if (world_data != null)
            {
                using var fb = bitmap.FastLock();
                var background = new List <(int x, int y, int type)>();
                var foreground = new List <(int x, int y, int type)>();

                for (var a = 0u; a < world_data.Count; a++)
                {
                    if (world_data.GetObject(a).Count == 0)
                    {
                        continue;
                    }

                    var ct       = world_data.GetObject(a, null);
                    var type     = (uint)ct["type"];
                    var layerNum = ct.GetInt("layer", 0);
                    var xs       = ct.GetBytes("x", new byte[0]);
                    var ys       = ct.GetBytes("y", new byte[0]);
                    var x1S      = ct.GetBytes("x1", new byte[0]);
                    var y1S      = ct.GetBytes("y1", new byte[0]);

                    if (type == 0)
                    {
                        continue;
                    }

                    for (var b = 0; b < x1S.Length; b++)
                    {
                        var nx = x1S[b];
                        var ny = y1S[b];

                        if (layerNum == 0)
                        {
                            foreground.Add((nx, ny, (int)type));
                        }
                        if (layerNum == 1)
                        {
                            background.Add((nx, ny, (int)type));
                        }
                    }

                    for (var b = 0; b < xs.Length; b += 2)
                    {
                        var nx = (uint)((xs[b] << 8) + xs[b + 1]);
                        var ny = (uint)((ys[b] << 8) + ys[b + 1]);

                        if (layerNum == 0)
                        {
                            foreground.Add(((int)nx, (int)ny, (int)type));
                        }
                        if (layerNum == 1)
                        {
                            background.Add(((int)nx, (int)ny, (int)type));
                        }
                    }
                }

                foreach (var(x, y, type) in background)
                {
                    var color = GetColor(type);

                    if (color.A != 255)
                    {
                        continue;
                    }

                    fb.SetPixel(x, y, color);
                }

                foreach (var(x, y, type) in foreground)
                {
                    var color = GetColor(type);

                    if (color.A != 255)
                    {
                        continue;
                    }

                    fb.SetPixel(x, y, color);
                }
            }
            else // world bytes (legacy)
            {
                using var fb = bitmap.FastLock();
                {
                    // draw border
                    var maxX  = (uint)(width - 1);
                    var maxY  = (uint)(height - 1);
                    var color = GetColor(world.GetInt("BorderType", 9));
                    for (uint y = 0; y <= maxY; y++)
                    {
                        fb.SetPixel(0, (int)y, color);
                        fb.SetPixel((int)maxX, (int)y, color);
                    }
                    for (uint x = 0; x <= maxX; x++)
                    {
                        fb.SetPixel((int)x, 0, color);
                        fb.SetPixel((int)x, (int)maxY, color);
                    }
                }

                var world_bytes = world.GetBytes("world", null);

                if (world_bytes != null)
                {
                    for (var y = 0U; y < height; y++)
                    {
                        for (var x = 0U; x < height; x++)
                        {
                            var color = GetColor(world_bytes[y * width + x]);

                            if (color.A != 255)
                            {
                                continue;
                            }

                            fb.SetPixel((int)x, (int)y, color);
                        }
                    }
                }
            }

            return(bitmap);
        }