% \iffalse meta-comment
%%% File: gamehop-mark.sty
%% Copyright (C) 2017 Michael N\"usken.
% 
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3a
% of this license or (at your option) any later version.
% The latest version of this license is in
%    http://www.latex-project.org/lppl.txt
% and version 1.2 or later is part of all distributions of LaTeX 
% version 1999/12/01 or later.
%
% -----------------------------------------------------------
% 
%<*chartab>
%\fi
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
% \iffalse meta-comment
%</chartab>
% \fi
% \iffalse
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{gamehop-mark}[2017/04/21 v0.94 %
Gamehopping helpers, marks. MNuesken]
% \fi
%
% \CheckSum{0}
%

\RequirePackage{gamehop-base}

\newif\ifmarked
\let\thismark\relax

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Commands for color markups 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\markclearall}{%
  \g@listdef{game:marks}{}%
}
\markclearall
\newcommand{\markgame}[2]{%
  % #1 = game
  % #2 = color
  %%% \typeout{MARK #1 with #2.}% DEBUG
  \g@listadd{game:marks}{%
    \markdo{#1}{#2}%
  }%
%%% \expandafter\show\csname game:marks\endcsname
}
\newcommand{\markgamesubtree}[2]{%
  % #1 = game
  % #2 = color  
  \def\do##1{\markgame{##1}{#2}{\markgamesubtree{##1}{#2}}\@tempswatrue}%
  \@ifundefined{game:#1:children}{%
    \PackageWarning{gamehop}{%
      The node #1 has no children.^^J%
      Is that ok or did you forget loading the game definitions before?^^J%
    }%
  }{%
    \@tempswafalse
    \@nameuse{game:#1:children}%
    \ifleaf
    \if@tempswa
    \PackageWarning{gamehop}{%
      The node #1 has children and is marked as a leaf.^^J%
      This is a contradiction.^^J%
    }%    
    \fi
    \else
    \if@tempswa\else
    \PackageWarning{gamehop}{%
      The node #1 has no children and is not marked as a leaf.^^J%
      This is a contradiction.^^J%
    }%    
    \fi
    \fi
  }%
}
\newcommand{\markgameandsubtree}[2]{%
  \markgame{#1}{#2}%
  \markgamesubtree{#1}{#2}%
}
\newcommand{\markgamesubtrees}[2]{%
  % #1 = game
  % #2 = color  
  \def\do##1{\markgame{##1}{#2}\markgame{red:##1}{#2}{\markgamesubtrees{##1}{#2}}\@tempswatrue}%
  \@ifundefined{game:#1:children}{%
    \PackageWarning{gamehop}{%
      The node #1 has no children.^^J%
      Does that node exist?^^J%
    }%    
  }{%
    \@tempswafalse
    \@nameuse{game:#1:children}%
    \ifleaf
    \if@tempswa
    \PackageWarning{gamehop}{%
      The node #1 has children and is marked as a leaf.^^J%
      This is a contradiction.^^J%
    }%    
    \fi
    \else
    \if@tempswa\else
    \PackageWarning{gamehop}{%
      The node #1 has no children and is not marked as a leaf.^^J%
      This is a contradiction.^^J%
    }%    
    \fi
    \fi
  }%
}
\newcommand{\markgameandsubtrees}[2]{%
  \markgame{#1}{#2}%
  \markgamesubtrees{#1}{#2}%
}
%%% markgamechildren is NOT recursive!
\newcommand{\markgamechildren}[2]{%
  % #1 = game
  % #2 = color  
  %%% \typeout{MARK children(#1) with #2.}% DEBUG
  \def\do##1{{\markgame{##1}{#2}}\@tempswatrue}%
  \@ifundefined{game:#1:children}{%
    \PackageWarning{gamehop}{%
      The node #1 has no children.^^J%
      Does that node exist?^^J%
    }%    
  }{%
    %%% \show\do\expandafter\show\csname gametree:nodesdo:#1\endcsname
    \@tempswafalse
    \@nameuse{game:#1:children}%
    \ifleaf
    \if@tempswa
    \PackageWarning{gamehop}{%
      The node #1 has children and is marked as a leaf.^^J%
      This is a contradiction.^^J%
    }%    
    \fi
    \else
    \if@tempswa\else
    \PackageWarning{gamehop}{%
      The node #1 has no children and is not marked as a leaf.^^J%
      This is a contradiction.^^J%
    }%    
    \fi
    \fi
    %%% {\tracingall\csname gametree:nodesdo:#1\endcsname}%
  }%
}
\newcommand{\markgameandchildren}[2]{%
  \markgame{#1}{#2}%
  \markgamechildren{#1}{#2}%
}
\newcommand{\markgameredandchildren}[2]{%
  \markgame{#1}{#2}%
  \def\do##1{{\markgame{red:##1}{#2}}}%
  \@nameuse{game:#1:children}%
  \markgamechildren{#1}{#2}%
}
\newcommand{\markgametrunk}[2]{%
  % #1 = game
  % #2 = color  
  \def\do##1{\markgame{##1}{#2}\markgametrunk{##1}{#2}}%
  \@ifundefined{game:#1:parent}{}{%
    \@nameuse{game:#1:parent}%
  }%
}
\newcommand{\markgameandtrunk}[2]{%
  \markgame{#1}{#2}%
  \markgametrunk{#1}{#2}%
}
\newcommand{\markgamehop}[1]{%
  % #1 = game
  \markgametrunk{#1}{yellow!20}%
  \markgame{red:#1}{red!20}%
  \markgame{#1}{green!20}%
}
\def\popcolor{\expandafter\@popcolor\colorlist\@nil}
\def\@popcolor#1,#2\@nil{%
  \@ifequal{#1}{}{
    \def\thisnewcolor{}\def\colorlist{}%
  }{%
    \def\thisnewcolor{#1}%
    \def\colorlist{#2}%
  }%
}
\newcommand{\markgametrunkspecial}[1]{%
  \markgameandtrunk{#1}{yellow!20}%
  \def\colorlist{red!20,green!20,blue!20,magenta!20,,}%
  \def\do##1{\popcolor\markgame{##1}{\thisnewcolor}}%
  \@ifundefined{game:#1:children}{}{%
    \csname game:#1:children\endcsname
  }%  
}
\def\markcmdfill#1{\psset{fillstyle=solid,fillcolor=#1}}
\def\markcmdswitch#1{\def\thismark{#1}\markedtrue}
\def\defmarkdo#1{%
  % #1 = game
  \def\markdo##1##2{\@ifequal{#1}{##1}{\markcmd{##2}}{}}%
}
\newcommand{\markexecutenoexpand}[2][\markcmdswitch]{%
  % #1 = \markcmd
  % #2 = game
  \defmarkdo{#2}%
  \markedfalse\let\markcmd#1\relax
  \csname game:marks\endcsname
  %%% \let\markdo\undefined\let\markcmd\undefined
}
\newcommand{\markexecute}[2][\markcmdswitch]{%
  % #1 = \markcmd
  % #2 = game
  \@expandonearg\defmarkdo{#2}%
  \markedfalse\let\markcmd#1\relax
  \csname game:marks\endcsname
  %%% \let\markdo\undefined\let\markcmd\undefined
}
%%% \newcommand{\markexecutecolor}{\markexecute[\markcmdfill]}
\endinput
