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

\newif\ifmarkedred
\newif\ifmarkednew
\let\thismarkred\relax
\let\thismarknew\relax


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Commands for using the data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% ...in a game or gameset
%%%
\def\do@usegamehopstep@evalmarks#1{%
  %%% \typeout{do@usegamehopstep@evalmarks #1}% DEBUG
  %\edef\next{\noexpand\markexecute{red:#1}}\next%
  \markexecute{red:#1}%
  \ifmarked\markedredtrue\let\thisredcolor\thismark\fi
  \markexecute{#1}%\edef\next{\noexpand\markexecute{#1}}\next%
  \ifmarked\markednewtrue\let\thisnewcolor\thismark\fi
  \ifmarkedred\markedtrue\fi
}
\newcommand{\gamesteplabel}[1]{%
  {\let\label\orig@label\Label{#1}{\protect\ref{gamestep:#1}}}%
}
\newcommand{\usegamestep}[1]{%
  \markedredfalse
  \markednewfalse
  \let\thisnewcolor\relax
  \let\thisredcolor\relax
  \@ifundefined{gamestep:#1}{%
    \textbf{Gamehop step #1 undefined.}%
  }{{%
    %%% \def\do{\protect\do}\typeout{usegamestep #1 = \@nameuse{gamestep:#1:gameids}}% DEBUG
    \let\do\do@usegamehopstep@evalmarks
    \@nameuse{gamestep:#1:gameids}%
    \let\do\relax
    %%% for debugging:
    %%% \ifmarked\else\textcolor{magenta}{NOTMARKED}\fi
    %%% \ifmarked\textcolor{magenta}{MARKED(\thismark)~}\fi
    %%% \ifmarkednew\textcolor{magenta}{MARKEDNEW(\thisnewcolor)~}\fi
    %%% \ifmarkedred\textcolor{magenta}{MARKEDRED(\thisredcolor)~}\fi
    \let\label\@gobble
    %%% 
    \@nameuse{gamestep:#1}%
  }}%
}

%%% List an entire gameset (standard form)
\def\@ifnumber#1{%
  \if!\ifnum9<1#1!\else_\fi
  \let\next\@firstoftwo
  \else\let\next\@secondoftwo
  \fi\next
}%
\def\gamesetsteplist@dodo#1#2{%
  %%% \typeout{dodo{#1}{#2};^^J  r@gamestep:#2 =^^J  \expandafter\meaning\csname r@gamestep:#2\endcsname}% DEBUG
  \@ifequal{#1}{{}{}}{%
    \begin{meta}
      \usegamestep{#2}
    \end{meta}
    %%% 
  }{%
    \@ifequal{\emptygamesteplabel}{#1}{%
      \def\next{\item}%
    }{%
      \@ifnumber{#1}{\def\next{.}}{\@ifempty{#1}{\def\next{}}{\def\next{:}}}%
      \edef\next{\noexpand\item[#1\next]}%
      %%% \item[#1\next]
    }%
    \next\usegamestep{#2}%
  }%
}%
\def\gamesetsteplist@dosub#1{%
  %%% \typeout{dosub{#1}}% DEBUG
  \begin{indention}
    \csname #1\endcsname
  \end{indention}
}%
\newcommand{\gamesetsteplist}[2][]{%
  \begin{algosteplist}
    #1%
    \let\dodo\gamesetsteplist@dodo
    \let\dosub\gamesetsteplist@dosub
    \csname gamealgo:#2\endcsname
  \end{algosteplist}
}

\newcommand{\gamesetsteps}[2][]{%
  {%
    #1%
    \let\dodo\gamesetsteplist@dodo
    \let\dosub\gamesetsteplist@dosub
    \csname gamealgo:#2\endcsname
  }%
}

\def\@firstofany#1#2\@@@{#1}%
\def\gamehop@labelofgamestep#1{%
  \edef\gamestep@base{\csname gamestep:#1:base\endcsname}%
  %%% \typeout{labelofgamestep{#1} gamestep:#1:base = \meaning\gamestep@base}% DEBUG
  \@ifundefined{r@\gamestep@base}{%
    \PackageWarning{gamehop}{Label \gamestep@base\MessageBreak
      (for gamestep #1) not defined:\MessageBreak
      \csname r@\gamestep@base\endcsname\MessageBreak}%
    \def\next{??}%
  }{%
    %%% \expandafter\show\csname r@gamestep:#1\endcsname
    %%% \typeout{labelofgamestep{#1};^^J r@gamestep:#1 = \csname r@gamestep:#1\endcsname}% DEBUG
    \edef\next{\noexpand\@firstofany\csname r@\gamestep@base\endcsname\noexpand\@@@}%
    %%% \typeout{Label \gamestep@base\space(for gamestep #1): \meaning\next}% DEBUG
    \expandafter\edef\expandafter\next\expandafter{\next}%
    %%% \typeout{Label \gamestep@base\space(for gamestep #1): \meaning\next}% DEBUG
  }%
  %%% \typeout{labelofgamestep{#1} = \meaning\next}% DEBUG
}
\newcommand{\labelofgamestep}[1]{%
  \gamehop@labelofgamestep{#1}\next
}
\newcommand{\itemgamestep}[1]{%
  \gamehop@labelofgamestep{#1}%
  \expandafter\gamesetsteplist@dodo\expandafter{\next}{#1}%
}

\def\gamehopsteplist@do#1{%
  \gamehop@labelofgamestep{#1}%
  \expandafter\dodo\expandafter{\next}{#1}
}
\newcommand{\gamehopsteplist}[2][]{%
  \begin{algosteplist}
    #1%
    \let\do\gamehopsteplist@do
    \let\dodo\gamesetsteplist@dodo
    \let\dosub\gamesetsteplist@dosub
    \csname game:#2:steps\endcsname
  \end{algosteplist}
}
\endinput
