Пример #1
0
        public static void Main(string[] args)
        {
            // Create algorithm setup.
            var algorithmZoom1       = new RuntimeLayer(new AlgorithmZoom2D());
            var algorithmZoom2       = new RuntimeLayer(new AlgorithmZoom2D());
            var algorithmZoom3       = new RuntimeLayer(new AlgorithmZoom2D());
            var algorithmZoom4       = new RuntimeLayer(new AlgorithmZoom2D());
            var algorithmInitialLand = new RuntimeLayer(new AlgorithmInitialBool());

            algorithmZoom4.SetInput(0, algorithmInitialLand);
            algorithmZoom3.SetInput(0, algorithmZoom4);
            algorithmZoom2.SetInput(0, algorithmZoom3);
            algorithmZoom1.SetInput(0, algorithmZoom2);

            StorageLayer[] storage = null;
            Console.WriteLine("Storing...");
            using (var writer = new StreamWriter("WorldConfig.xml", false))
                StorageAccess.SaveStorage(
                    new StorageLayer[] { StorageAccess.FromRuntime(algorithmZoom1) }, writer);

            Console.WriteLine("Loading...");
            using (var reader = new StreamReader("WorldConfig.xml"))
                storage = StorageAccess.LoadStorage(reader);
            foreach (var l in storage)
            {
                Console.WriteLine(l.Algorithm.GetType().FullName);
            }
        }
        private static Bitmap Regenerate3DImageForLayer(RuntimeLayer runtimeLayer, long ox, long oy, long oz, int width, int height, int depth, IGenerator compiledLayer = null)
        {
            int owidth  = width;
            int oheight = height;

            width  = 128;
            height = 192; // this affects bitmaps and rendering and stuff :(
            depth  = 128;

            // ARGHGHG FIXME
            Bitmap   b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(b);

            g.Clear(Color.White);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
            int     computations = 0;
            dynamic data;

            if (compiledLayer != null)
            {
                data = compiledLayer.GenerateData(ox, oy, oz, RenderWidth, RenderHeight, RenderDepth, out computations);
            }
            else
            {
                data = runtimeLayer.GenerateData(ox, oy, oz, RenderWidth, RenderHeight, RenderDepth, out computations);
            }

            StorageLayer parent;

            if (runtimeLayer.GetInputs().Length == 0)
            {
                parent = null;
            }
            else
            {
                parent = StorageAccess.FromRuntime(runtimeLayer.GetInputs()[0]);
            }

            int[] render  = GetCellRenderOrder(RenderToNE, RenderWidth, RenderHeight);
            int   ztop    = runtimeLayer.Algorithm.Is2DOnly ? 1 : RenderDepth;
            int   zbottom = 0;

            for (int z = zbottom; z < ztop; z++)
            {
                int rcx = width / 2 - 1;
                int rcy = height / 2 - 31;
                int rw  = 2;
                int rh  = 1;
                for (int i = 0; i < render.Length; i++)
                {
                    // Calculate the X / Y of the tile in the grid.
                    int x = render[i] % RenderWidth;
                    int y = render[i] / RenderWidth;

                    // Calculate the render position on screen.
                    int rx = rcx + (int)((x - y) / 2.0 * rw);// (int)(x / ((RenderWidth + 1) / 2.0) * rw);
                    int ry = rcy + (x + y) * rh - (rh / 2 * (RenderWidth + RenderHeight)) - (z - zbottom) * 1;

                    while (true)
                    {
                        try
                        {
                            Color lc = runtimeLayer.Algorithm.GetColorForValue(
                                parent,
                                data[x + y * owidth + z * owidth * oheight]);
                            SolidBrush sb = new SolidBrush(Color.FromArgb(lc.A, lc.R, lc.G, lc.B));
                            g.FillRectangle(
                                sb,
                                new Rectangle(rx, ry, rw, rh)
                                );
                            break;
                        }
                        catch (InvalidOperationException)
                        {
                            // Graphics can be in use elsewhere, but we don't care; just try again.
                        }
                    }
                }
            }

            return(b);
        }
Пример #3
0
        protected override void ProcessGeneration(GenerationResult result, HttpContext context)
        {
            var cache = this.GetCacheName(result.Request, context, "json");

            context.Response.ContentType = "application/json";
            using (var cacheWriter = new StreamWriter(cache))
            {
                using (var webWriter = new StreamWriter(context.Response.OutputStream))
                {
                    if (result.Request.Packed)
                    {
                        cacheWriter.Write("{\"empty\":false,\"time\":\"" + result.TotalTime + "\",\"packed\":true,\"data\":[");
                        webWriter.Write("{\"empty\":false,\"time\":\"" + result.TotalTime + "\",\"packed\":true,\"data\":[");
                        {
                            var continuousValue = 0;
                            var continuousCount = 1;
                            var first           = true;
                            var i = 0;
                            do
                            {
                                // Store the value into our continuity tracker.
                                if (i != result.Data.Length)
                                {
                                    continuousValue = result.Data[i];
                                }

                                // Increment to the next position.
                                i++;

                                if (i == result.Data.Length ||
                                    continuousValue != result.Data[i])
                                {
                                    // Output in the most efficient manner.
                                    if (("[" + continuousCount + "," + continuousValue + "]").Length >
                                        ((continuousValue.ToString().Length + 1) * continuousCount) - 1)
                                    {
                                        // Single value.
                                        for (var a = 0; a < continuousCount; a++)
                                        {
                                            if (!first)
                                            {
                                                cacheWriter.Write(",");
                                                webWriter.Write(",");
                                            }
                                            first = false;

                                            cacheWriter.Write(continuousValue);
                                            webWriter.Write(continuousValue);
                                        }
                                    }
                                    else
                                    {
                                        if (!first)
                                        {
                                            cacheWriter.Write(",");
                                            webWriter.Write(",");
                                        }
                                        first = false;

                                        // Multiple copies of the same
                                        // value in a row.
                                        cacheWriter.Write("[" + continuousCount + "," + continuousValue + "]");
                                        webWriter.Write("[" + continuousCount + "," + continuousValue + "]");
                                    }

                                    // Reset the continity count.
                                    continuousCount = 1;
                                }
                                else
                                {
                                    continuousCount++;
                                }
                            } while (i < result.Data.Length);
                        }
                    }
                    else
                    {
                        cacheWriter.Write("{\"empty\":false,\"time\":\"" + result.TotalTime + "\",\"packed\":false,\"data\":[");
                        webWriter.Write("{\"empty\":false,\"time\":\"" + result.TotalTime + "\",\"packed\":false,\"data\":[");
                        {
                            var first = true;
                            for (var i = 0; i < result.Data.Length; i++)
                            {
                                if (!first)
                                {
                                    cacheWriter.Write(",");
                                    webWriter.Write(",");
                                }
                                first = false;
                                cacheWriter.Write(result.Data[i]);
                                webWriter.Write(result.Data[i]);
                            }
                        }
                    }
                    cacheWriter.Write("],\"mappings\":{");
                    webWriter.Write("],\"mappings\":{");
                    var mappings    = new Dictionary <int, Color>();
                    var parentLayer = StorageAccess.FromRuntime(result.Layer.GetInputs()[0]);
                    for (var i = 0; i < result.Data.Length; i++)
                    {
                        if (!mappings.ContainsKey(result.Data[i]))
                        {
                            mappings.Add(result.Data[i],
                                         result.Layer.Algorithm.GetColorForValue(
                                             parentLayer,
                                             result.Data[i]));
                        }
                    }
                    {
                        var first = true;
                        foreach (var kv in mappings)
                        {
                            if (!first)
                            {
                                cacheWriter.Write(",");
                                webWriter.Write(",");
                            }
                            first = false;

                            var colorString =
                                "[" + kv.Value.A +
                                "," + kv.Value.R +
                                "," + kv.Value.G +
                                "," + kv.Value.B + "]";
                            cacheWriter.Write("\"" + kv.Key + "\":" + colorString);
                            webWriter.Write("\"" + kv.Key + "\":" + colorString);
                        }
                    }
                    cacheWriter.Write("}}");
                    webWriter.Write("}}");
                }
            }
        }
Пример #4
0
        private static Bitmap RenderPartial3D(RuntimeLayer layer, int sx, int sy, int sz, int width, int height, int depth)
        {
            var bitmap   = new Bitmap(width * 2, height * 3);
            var graphics = Graphics.FromImage(bitmap);

            graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
            int[] data;
            try
            {
                int computations;
                data = layer.GenerateData(TemporaryCrapBecauseIDidNotReallyDesignThingsVeryWell.X + sx, TemporaryCrapBecauseIDidNotReallyDesignThingsVeryWell.Y + sy, TemporaryCrapBecauseIDidNotReallyDesignThingsVeryWell.Z + sz, width, height, depth, out computations);

                var render  = GetCellRenderOrder(RenderToNE, width, height);
                var ztop    = layer.Algorithm.Is2DOnly ? 1 : depth;
                var zbottom = 0;
                for (var z = zbottom; z < ztop; z++)
                {
                    var rcx = width / 2 - 1 + 16;
                    var rcy = height / 2 - 15 + 32;
                    var rw  = 2;
                    var rh  = 1;
                    for (var i = 0; i < render.Length; i++)
                    {
                        // Calculate the X / Y of the tile in the grid.
                        var x = render[i] % width;
                        var y = render[i] / width;

                        // Calculate the render position on screen.
                        var rx = rcx + (int)((x - y) / 2.0 * rw);// (int)(x / ((RenderWidth + 1) / 2.0) * rw);
                        var ry = rcy + (x + y) * rh - (rh / 2 * (width + height)) - (z - zbottom) * 1;

                        while (true)
                        {
                            try
                            {
                                Color lc;
                                if (layer.GetInputs().Length > 0)
                                {
                                    lc = layer.Algorithm.GetColorForValue(
                                        StorageAccess.FromRuntime(layer.GetInputs()[0]),
                                        data[x + y * width + z * width * height]);
                                }
                                else
                                {
                                    lc = layer.Algorithm.GetColorForValue(
                                        null,
                                        data[x + y * width + z * width * height]);
                                }
                                var sb = new SolidBrush(Color.FromArgb(lc.A, lc.R, lc.G, lc.B));
                                graphics.FillRectangle(
                                    sb,
                                    new Rectangle(rx, ry, rw, rh)
                                    );
                                break;
                            }
                            catch (InvalidOperationException)
                            {
                                // Graphics can be in use elsewhere, but we don't care; just try again.
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }

            return(bitmap);
        }
        public static Bitmap RenderTraceResult(
            RuntimeLayer layer,
            dynamic data,
            int width,
            int height,
            int depth)
        {
            int owidth  = width;
            int oheight = height;

            width  = 128 * TraceScale;
            height = 128 * TraceScale;
            depth  = 128 * TraceScale;

            Bitmap   b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(b);

            g.Clear(Color.White);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;

            StorageLayer parent;

            if (layer.GetInputs().Length == 0)
            {
                parent = null;
            }
            else
            {
                parent = StorageAccess.FromRuntime(layer.GetInputs()[0]);
            }

            int[] render  = GetCellRenderOrder(RenderToNE, TraceRenderWidth, TraceRenderHeight);
            int   ztop    = layer.Algorithm.Is2DOnly ? 1 : 128;
            int   zbottom = 0;

            for (int z = zbottom; z < ztop; z++)
            {
                int rcx = width / 2 - 1;
                int rcy = height / 2 - (height / 2 - 1);
                int rw  = 2;
                int rh  = 1;
                for (int i = 0; i < render.Length; i++)
                {
                    // Calculate the X / Y of the tile in the grid.
                    int x = render[i] % TraceRenderWidth;
                    int y = render[i] / TraceRenderWidth;

                    // Calculate the render position on screen.
                    int rx = rcx + (int)((x - y) / 2.0 * rw);// (int)(x / ((RenderWidth + 1) / 2.0) * rw);
                    int ry = rcy + (x + y) * rh - (rh / 2 * (TraceRenderWidth + TraceRenderHeight)) - (z - zbottom) * 1;

                    while (true)
                    {
                        try
                        {
                            Color lc = layer.Algorithm.GetColorForValue(
                                parent,
                                data[x + y * owidth + z * owidth * oheight]);
                            SolidBrush sb = new SolidBrush(Color.FromArgb(lc.A, lc.R, lc.G, lc.B));
                            g.FillRectangle(
                                sb,
                                new Rectangle(rx, ry, rw, rh)
                                );
                            break;
                        }
                        catch (InvalidOperationException)
                        {
                            // Graphics can be in use elsewhere, but we don't care; just try again.
                        }
                    }
                }
            }

            return(b);
        }
Пример #6
0
        private static Bitmap RenderPartial3D(GenerationResult result)
        {
            var    width  = result.Request.Size;
            var    height = result.Request.Size;
            Bitmap bitmap;

            if (result.Request.AsSquare)
            {
                bitmap = new Bitmap(width, height - 1);
            }
            else
            {
                bitmap = new Bitmap(width * 2, height * 3);
            }
            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
                var depth = result.Layer.Algorithm.Is2DOnly ? 1 : result.Request.Size;
                try
                {
                    var render      = GetCellRenderOrder(RenderToNE, width, height);
                    var ztop        = depth;
                    var zbottom     = 0;
                    var parentLayer = StorageAccess.FromRuntime(result.Layer.GetInputs()[0]);
                    for (int z = zbottom; z < ztop; z++)
                    {
                        int rcx = width / 2 - 1 + 32;
                        int rcy = height / 2 - 15 + 32;
                        int rw  = 2;
                        int rh  = 1;
                        for (int i = 0; i < render.Length; i++)
                        {
                            // Calculate the X / Y of the tile in the grid.
                            int x = render[i] % width;
                            int y = render[i] / width;

                            // Calculate the render position on screen.
                            int rx = rcx + (int)((x - y) / 2.0 * rw);
                            int ry = rcy + (x + y) * rh - (rh / 2 * (width + height)) - (z - zbottom) * 1;

                            // Adjust for square mode.
                            if (result.Request.AsSquare)
                            {
                                rx = (rx - rcx) + width / 2;
                                ry = (ry - rcy) - height / 2;
                                if (rx < -1 || ry < -1 ||
                                    rx > width + 1 || ry > height + 1)
                                {
                                    continue;
                                }
                            }

                            while (true)
                            {
                                try
                                {
                                    Color lc = result.Layer.Algorithm.GetColorForValue(
                                        parentLayer,
                                        result.Data[x + y * width + z * width * height]);
                                    var sb = new SolidBrush(Color.FromArgb(lc.A, lc.R, lc.G, lc.B));
                                    graphics.FillRectangle(sb, new Rectangle(rx, ry, rw, rh));
                                    break;
                                }
                                catch (InvalidOperationException)
                                {
                                    // Graphics can be in use elsewhere, but we don't care; just try again.
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
            return(bitmap);
        }