% \iffalse meta-comment
%%% File: gamehop-tree.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-tree}[2017/04/21 v0.94 %
Gamehopping helpers, tree. MNuesken]
% \fi
%
% \CheckSum{0}
%

\RequirePackage{gamehop-base}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Commands for constructing and displaying game tree
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{pst-tree}
\def\gamehoptree@init{%
  \psset{treefit=loose}%
  \psset{treemode=D,treesep=0.3em,levelsep=7ex}%
}
% transitiontitle macros get:
% #1 = newgame
% #2 = oldgame (probably superfluous)
% transition macros get:
% #1 = do before drawing node
% #2 = node title
% #3 = do before drawing side node
% #4 = side node title
\def\transitionROOT#1#2#3#4{%
  #1\Toval{#2}%
}
%%% \def\TZZ#1~#2{%
%%%   \Toval{#1}~{#2}%
%%% }
% Generic variant for splitting or other (empty type)
%%% \newcommand{\transitiontitle}[2]{%
%%%   % #1 = newgame
%%%   % #2 = oldgame
%%%   \ensuremath{%
%%%     \mathcal{R}%
%%%     ^{\text{\csname game:#1:transinfo\endcsname}}%
%%%     _{\text{\ref{shortbase:#1}}\to\text{\ref{shortgame:#1}}}%
%%%   }%
%%% }%
\def\transition#1#2#3#4{%
  #1\Toval{#2}%
}
% Variant A for Game Hopping Lemma (i)
\def\transitiontitleA{\expandafter\transitiontitle\expandafter}
\def\transitionA#1#2#3#4{%
  \pstree[treemode=R,levelsep=9em,treesep=8ex]{%
    #1\Toval{#2}%
  }{%
    \Tn{}%
    #3\Toval{#4}%
  }%
}
% Variant Br for Game Hopping Lemma (ii.b)
\def\transitiontitleBr{\expandafter\transitiontitle\expandafter}
\def\transitionBr#1#2#3#4{% (ii) with reduction
  \pstree[treemode=R,levelsep=8em,treesep=8ex,linestyle=dotted]{%
    #1\Toval{#2}%
  }{%
    \Tn{}%
    #3\Toval[linestyle=solid]{#4}%
  }%
}
% Variant Bx for Game Hopping Lemma (ii.a)
\def\transitiontitleBx#1{\@nameuse{game:#1:transinfo}}
\def\transitionBx#1#2#3#4{% (ii) with explicit bound
  \pstree[treemode=R,levelsep=8em,treesep=8ex,linestyle=dotted]{%
    #1\Toval{#2}%
  }{%
    \Tn{}%
    #3\Toval[linestyle=none]{#4}%
  }%
}
% Variant C for Game Hopping Lemma (iii)
\def\transitiontitleC#1{\@nameuse{game:#1:transinfo}}
\def\transitionC#1#2#3#4{%
  \pstree[treemode=R,levelsep=8em,treesep=8ex]{%
    #1\Toval{#2}%
  }{%
    \Tn{}%
    #3\Toval[linestyle=none]{#4}%
  }%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\dodeflast#1{\advance\@tempcnta\@ne\def\lastchild{#1}}
\newcommand{\gametree}[1]{%
  {\gamehoptree@init
    \begin{psTree}{%
        \psset{fillstyle=none}%
        %
        \def\lastchild{}\@tempcnta\z@\let\do\dodeflast%
        \csname game:#1:children\endcsname%
        \ifleaf
        \edef\next{}%
        \else
        \edef\next{\csname game:\lastchild:transtype\endcsname}%
        \ifnum\@tempcnta=\@ne
        \else\ifx\next\@empty\else
        \PackageWarning{gamehop}{%
          The node #1 is no leaf and has not one child,\MessageBreak%
          but non-trivial transition type
          \expandafter\strip@prefix\meaning\next.\MessageBreak%
        }%
        \fi\fi
        \fi
        %%% \typeout{NEXT #1 -> \meaning\lastchild, \meaning\next}% DEBUG
        \csname transition\next\endcsname%
        {\markexecute[\markcmdfill]{#1}}{%
          \csname game:#1:title\endcsname%
        }%
        {%
          \markexecute[\markcmdfill]{red:\lastchild}%
        }{%
          \csname transitiontitle\next\endcsname{\lastchild}{}%
        }%
        %
      }%
      \let\do\gametree%
      \csname game:#1:children\endcsname%
    \end{psTree}%  
  }%
}

