示例#1
0
        public void UnforceWireAt(int x, int y)
        {
            Wire wire = WireMap[x, y];

            wire?.UnForce();
        }
示例#2
0
        public static CircuitBoard FromImage(Image image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            int width  = image.Width;
            int height = image.Height;

            var highImage = new Image(width, height);
            var lowImage  = new Image(width, height);

            var board = new CircuitBoard(image, highImage, lowImage);

            if (width == 0 || height == 0)
            {
                return(board);
            }

            var map = board.WireMap;

            if (image.IsWire(0, 0))
            {
                var t = new Wire(false, new Point(0, 0));
                map[0, 0] = t;
                board.Wires.Add(t);
            }

            for (int x = 1; x < width; x++)
            {
                if (image.IsWire(x, 0))
                {
                    var point = new Point(x, 0);

                    Wire wire = map[x - 1, 0];

                    if (wire != null)
                    {
                        map[x, 0] = wire;
                        wire.Points.Add(point);
                    }
                    else
                    {
                        var t = new Wire(false, point);
                        map[x, 0] = t;
                        board.Wires.Add(t);
                    }
                }
            }

            for (int y = 1; y < height; y++)
            {
                if (image.IsWire(0, y))
                {
                    var  point = new Point(0, y);
                    Wire wire  = map[0, y - 1];

                    if (wire != null)
                    {
                        map[0, y] = wire;
                        wire.Points.Add(point);
                    }
                    else
                    {
                        var t = new Wire(false, point);
                        map[0, y] = t;
                        board.Wires.Add(t);
                    }
                }

                for (int x = 1; x < width; x++)
                {
                    if (image.IsWire(x, y))
                    {
                        var  point = new Point(x, y);
                        Wire wire  = map[x, y - 1];

                        if (wire != null)
                        {
                            map[x, y] = wire;
                            wire.Points.Add(point);
                            var wire2 = map[x - 1, y];

                            if (wire2 != null && wire != wire2)
                            {
                                foreach (Point p in wire2.Points)
                                {
                                    map[p.X, p.Y] = wire;
                                    wire.Points.Add(p);
                                }

                                board.Wires.Remove(wire2);
                            }
                        }
                        else
                        {
                            wire = map[x - 1, y];

                            if (wire != null)
                            {
                                map[x, y] = wire;
                                wire.Points.Add(point);
                            }
                            else
                            {
                                var t = new Wire(false, point);
                                map[x, y] = t;
                                board.Wires.Add(t);
                            }
                        }
                    }
                }
            }

            for (int y = 1; y < height - 1; y++)
            {
                for (int x = 1; x < width - 1; x++)
                {
                    Wire t1 = map[x - 1, y - 1];
                    Wire t2 = map[x, y - 1];
                    Wire t3 = map[x + 1, y - 1];
                    Wire t4 = map[x - 1, y];
                    Wire t5 = map[x, y];
                    Wire t6 = map[x + 1, y];
                    Wire t7 = map[x - 1, y + 1];
                    Wire t8 = map[x, y + 1];
                    Wire t9 = map[x + 1, y + 1];

                    if (t1 == null && t2 != null && t3 == null && t4 != null && t5 == null && t6 != null && t7 == null && t8 != null && t9 == null)
                    {
                        if (t2 != t8)
                        {
                            board.MergeWires(t2, t8);
                        }

                        if (t4 != t6)
                        {
                            board.MergeWires(t4, t6);
                        }
                    }
                }
            }

            for (int y = 1; y < height - 1; y++)
            {
                for (int x = 1; x < width - 1; x++)
                {
                    Wire t1 = map[x - 1, y - 1];
                    Wire t2 = map[x, y - 1];
                    Wire t3 = map[x + 1, y - 1];
                    Wire t4 = map[x - 1, y];
                    Wire t5 = map[x, y];
                    Wire t6 = map[x + 1, y];
                    Wire t7 = map[x - 1, y + 1];
                    Wire t8 = map[x, y + 1];
                    Wire t9 = map[x + 1, y + 1];

                    if (t1 == null && t2 != null && t3 == null && t4 == null && t5 == null && t6 == null && t7 != null && t8 != null && t9 != null)
                    {
                        t2.SourceWires.Add(t8);
                    }
                    else if (t1 == null && t2 == null && t3 != null && t4 != null && t5 == null && t6 != null && t7 == null && t8 == null && t9 != null)
                    {
                        t4.SourceWires.Add(t6);
                    }
                    else if (t1 != null && t2 != null && t3 != null && t4 == null && t5 == null && t6 == null && t7 == null && t8 != null && t9 == null)
                    {
                        t8.SourceWires.Add(t2);
                    }
                    else if (t1 != null && t2 == null && t3 == null && t4 != null && t5 == null && t6 != null && t7 != null && t8 == null && t9 == null)
                    {
                        t6.SourceWires.Add(t4);
                    }


                    //if (t1 == null && t2 != null && t3 == null && t4 != null && t5 == null && t6 != null && t7 != null && t8 != null && t9 != null)
                    //{
                    //    t2.SourceWires.Add(t8);
                    //}
                    //else if (t1 == null && t2 != null && t3 != null && t4 != null && t5 == null && t6 != null && t7 == null && t8 != null && t9 != null)
                    //{
                    //    t4.SourceWires.Add(t6);
                    //}
                    //else if (t1 != null && t2 != null && t3 != null && t4 != null && t5 == null && t6 != null && t7 == null && t8 != null && t9 == null)
                    //{
                    //    t8.SourceWires.Add(t2);
                    //}
                    //else if (t1 != null && t2 != null && t3 == null && t4 != null && t5 == null && t6 != null && t7 != null && t8 != null && t9 == null)
                    //{
                    //    t6.SourceWires.Add(t4);
                    //}
                }
            }

            foreach (Wire wire in board.Wires)
            {
                int active = 0;
                foreach (Point point in wire.Points)
                {
                    int    x     = point.X;
                    int    y     = point.Y;
                    Bgra32 color = image[x, y];

                    byte r = color.R;
                    byte g = color.G;
                    byte b = color.B;
                    byte a = color.A;

                    highImage[x, y] = new Bgra32(r, g, b);
                    lowImage[x, y]  = new Bgra32((byte)(r / 2), (byte)(g / 2), (byte)(b / 2));

                    if (a >= 128)
                    {
                        active++;
                    }
                    else
                    {
                        active--;
                    }
                }

                wire.IsActive = active > 0;

                Image i = wire.IsActive ? board.highImage : board.lowImage;

                foreach (Point point in wire.Points)
                {
                    int x = point.X;
                    int y = point.Y;
                    image[x, y] = i[x, y];
                }
            }

            return(board);
        }
示例#3
0
        public void ForceWireAt(int x, int y)
        {
            Wire wire = WireMap[x, y];

            wire?.Force(true);
        }