private void Apply(Point pos, Match rule)
        {
            if (!IsMatch(pos, rule))
            {
                return;
            }

            foreach (var subMatch in rule.SubMatches)
            {
                Apply(pos, subMatch);
            }

            foreach (var piece in rule.Pieces)
            {
                DrawPiece(piece, pos);
            }
        }
        private Func <MatchPoint, bool> DoesMatch(Point pos)
        {
            return(a =>
            {
                var p = a.Position + new Point((int)pos.X, (int)pos.Y);
                if (p.X < 0 || p.X >= Array.GetLength(0))
                {
                    return false;
                }
                if (p.Y < 0 || p.Y >= Array.GetLength(1))
                {
                    return false;
                }
                if (a.MatchType == "Shadows" || a.MatchType == "NotShadows")
                {
                    return false;
                }

                return Array[p.X, p.Y] == (a.MatchType == "EqualsSelf");
            });
        }
        private void DrawPiece(ReplacementPiece piece, Point pos)
        {
            var p    = pos * 8 + piece.Position;
            var tile = GetTile(piece.Piece, pos);

            GL.PushMatrix();
            GL.Translate(p.X, p.Y, 0);
            GL.Begin(BeginMode.Quads);

            GL.TexCoord2(TexCoord(tile, 0, 1));
            GL.Vertex2(Scale(tile, 0, 0));
            GL.TexCoord2(TexCoord(tile, 1, 1));
            GL.Vertex2(Scale(tile, 1, 0));
            GL.TexCoord2(TexCoord(tile, 1, 0));
            GL.Vertex2(Scale(tile, 1, 1));
            GL.TexCoord2(TexCoord(tile, 0, 0));
            GL.Vertex2(Scale(tile, 0, 1));

            GL.End();
            GL.PopMatrix();
        }
 private int GetVariant(Point pos)
 {
     return((int)(pos.X * 2473 + pos.Y) % Material.Variants);
 }
 private bool IsMatch(Point pos, Match rule)
 {
     return(rule.MatchAllPoints.All(DoesMatch(pos)));
 }
        private Rectangle GetTile(string pieceName, Point pos)
        {
            var piece = Material.Pieces[pieceName];

            return((piece.TexturePosition + piece.ColorStride * ColorIndex + piece.VariantStride * GetVariant(pos)).ToRect(piece.TextureSize));
        }