% \iffalse
% <*chit>
% --------------------------------------------------------------------
% \fi
% \subsubsection{Making order of battle charts}
% 
%
% Macros for making OOBs
%
% Style for turns
%
%    \begin{macrocode}
\tikzset{
  chit/oob turn/.pic={\node[pic actions]{#1};}}
%    \end{macrocode}
% 
% 
% current c, current r, n-columns, cell size, y
% 
%    \begin{macrocode}
\def\chit@oob@cellupdate(#1,#2)#3#4#5{%
  \edef\f{\ifwg@oob@inv-1\else1\fi}%
  \chit@dbg{1}{ \space Cell update `c=|#1|' vs `#4'*(`#3'-1)}
  \pgfmathparse{int(ifthenelse(abs(#1)>=#4*(#3-1),#5-1,#5))}%
  \xdef#5{\pgfmathresult}
  \pgfmathparse{ifthenelse(abs(#1)>=#4*(#3-1),#2-#4,#2)}%
  \xdef#2{\pgfmathresult}%
  \pgfmathparse{ifthenelse(abs(#1)>=#4*(#3-1),0,#1+\f*#4)}%
  \xdef#1{\pgfmathresult}%
  \chit@dbg{1}{ \space\space-> `\string#5'=#5 `\string#2'=#2 `\string#1'=#1}
}
%    \end{macrocode}
%
% current c, current r, cell size, extra vertical spacing
%
%    \begin{macrocode}
\def\chit@oob@rowupdate(#1,#2)#3#4{%
  \chit@dbg{2}{ Row update  c=`#1',r=`#2',s=`#3',e=`#4'}
  %\pgfmathparse{ifthenelse(#1>0,#2-#3,#2)}%
  \pgfmathparse{#2-#3-#4}%
  \xdef#2{\pgfmathresult}%
  \xdef#1{0}%\pgfmathresult}%
  %\xdef#1{0}
  \chit@dbg{2}{ \space\space-> update `\string#2'=#2}
}
%    \end{macrocode}
% current c, current r,  cell size, extra spacing
%
%    \begin{macrocode}
\def\chit@oob@turnupdate(#1,#2)#3#4{%
  \chit@dbg{2}{ Turn update c=`#1',r=`#2',s=`#3',e=`#4'}
  % \pgfmathparse{#2-ifthenelse(#1>0,#3,0)-#4}%
  \pgfmathparse{#2-#4-ifthenelse(abs(#1)>0.0001,#3,0)} 
  \xdef#2{\pgfmathresult}%
  \xdef#1{0}%
  \chit@dbg{2}{ \space\space-> update `\string#1'=#1,`\string#2'=#2}
}
%    \end{macrocode}
% chit list, n-colls, cell size, extra vertical spacing
%
% This expects a list of lists of chits, one list per turn; the
% maximum number of columns; the size of cells, extra spacing between
% turns.
%
% Note, the list of lists leaf elements should be styles for the
% chits. 
%
% This depends on the Tikz pic \texttt{chit/oob~turn} which takes the
% number as argument.
%
% 
%    \begin{macrocode}
\newif\ifwg@oob@inv\wg@oob@invfalse
\def\chit@oob@spacer{hspace}
\def\chit@oob@vspacer{vspace}
\def\wg@star@oob{\wg@oob@invtrue\wg@oob}
\def\wg@nostar@oob{\wg@oob@invfalse\wg@oob}
\def\oob{%
  \@ifstar{\wg@star@oob%
  }{\wg@nostar@oob%
  }%
}
%    \end{macrocode}
% 
% The inner macro of \cs{oob}.  The arguments are
% \begin{enumerate}
% \item The list of lists of chits styles
% \item The maximum number of columns
% \item The width of each cell
% \item Additional row spacing between turns
% \end{enumerate}
% 
%    \begin{macrocode}
\def\wg@oob#1#2#3#4{
  \def\r{0}
  \pgfmathparse{#3*(#2-1)}%
  \edef\a{\pgfmathresult}
  \chit@dbg{2}{OOB: `#1'}
  \foreach[count=\ti from 0] \t/\y in #1{
    \xdef\o{\r}
    \def\c{0}
    \ifx\t\y\def\y{0}\fi
    \chit@dbg{2}{Turn \ti\space(\r,\t,y=\y):'}
    \ifwg@oob@inv%
      \pic[transform shape] at ( .5*#3,\r) {chit/oob turn=\ti};% was dx=0.5
    \else
      \pic[transform shape] at (-.5*#3,\r) {chit/oob turn=\ti};% was dx=-0.5
    \fi%
    \ifx\t\empty\else%
      \foreach \u/\m in \t{
        %% \chit@dbg{2}{ `\u'=`\m'}
        \ifx\u\empty\else
          \ifx\m\@empty\def\m{1}\fi
          \ifx\u\m\def\m{1}\fi
          \foreach \n in {1,...,\m}{%
            \chit@dbg{2}{OOB Chit is `\u' `\chit@oob@spacer'}%
            \ifx\u\chit@oob@spacer%
              \chit@dbg{3}{Chit `\u' is spacer `\chit@oob@spacer'}
              \pgfmathparse{\c+#4}%
              \xdef\c{\pgfmathresult}%
            \else%
              \ifx\u\chit@oob@vspacer%
                \chit@dbg{3}{Chit `\u' is vspacer `\chit@oob@vspacer'}
                \pgfmathparse{ifthenelse(abs(\c)<0.0001,0,#3)}
                \xdef\ll{\pgfmathresult}
                \chit@dbg{2}{\string\ll=`\ll'}
                \chit@oob@rowupdate(\c,\r){\ll}{#4}
              \else
                \ifnum\chitdbglvl>2%
                  \node[minimum width=#3cm,minimum height=#3cm,
                        draw,transform shape] at (\c,\r) {};
                \fi
                \ifx\u\chit@blank\else%
                  \chit[\u=\ti,zone oob point={\u}{\c}{\r}](\c,\r);%
                \fi%
                \chit@oob@cellupdate(\c,\r){#2}{#3}{\y}
              \fi
            \fi
          }
        \fi
      }
    \fi
    \chit@dbg{1}{ End of chits in turn
      \ti\space(c=`\c',r=`\r',o='\o',y='\y')}
    % IF no units where given, then we force \c to be non-zero so that
    % \chit@oob@turnupdate increments the row 
    \ifx\t\@empty
      \def\c{#3}
      \chit@dbg{2}{ Turn is empty, set c=`\c'}
    \fi
    %\ifnum\y<0% No explicit number of rows given
    %  \def\c{#3}
    %  \chit@dbg{2}{ No explicit number of rows given, set c=`\c'}
    %\fi
    % In case the user gave and explicit number of rows, add the rows
    % that are missing.  \y is initially set to the number of
    % requested rows, and then decremented every time we go down one
    % row.  So if the number of rows we did so far is N, and the
    % requested number of rows is M, then the loop below adds M-N
    % rows.
    \ifnum\y>0%
      \chit@dbg{2}{ Looping rows from 2 to \y, break when row > \y}%
      \foreach \rr  in {2,...,\y}{
        %\ifnum\rr>\y% A little funny, but \y can be negative!
        %  \chit@dbg{2}{ \space Breaking loop \rr\space > \y}%
        %  \breakforeach%
        %\else%
          \chit@oob@rowupdate(\c,\r){#3}{0}% Extra spacing? 
        %\fi
      }
    \fi
    % This will zero \c.  However, if on entry |\c|>0, then we also
    % increment the row 
    \chit@oob@turnupdate(\c,\r){#3}{#4}
    \chit@dbg{2}{End of turn \ti\space(c=`\c',r=`\r',o='\o',y='\y')} 
  }
  \chit@dbg{3}{End of OOB (c=`\c',r=`\r',y=`\y')}
  \@ifnextchar;{\@gobble}{}}
%    \end{macrocode}
%
% Horizontal flow OOB
% 
%    \begin{macrocode}
\def\wg@star@hoob{\wg@oob@invtrue\wg@hoob}
\def\wg@nostar@hoob{\wg@oob@invfalse\wg@hoob}
\def\hoob{%
  \@ifstar{\wg@star@hoob%
  }{\wg@nostar@hoob%
  }%
}
%    \end{macrocode}
% 
% The inner macro of \cs{hoob}.  The arguments are
% \begin{enumerate}
% \item The list of lists of chits styles
% \item The maximum number of columns
% \item The width of each cell
% \item Additional row spacing between turns
% \end{enumerate}
% 
%    \begin{macrocode}
\def\wg@hoob#1#2#3#4{
  \def\r{0}
  \def\c{0}
  \pgfmathparse{#3*(#2-1)}%
  \edef\a{\pgfmathresult}
  \chit@dbg{2}{OOB: `#1'}
  \foreach[count=\ti from 0] \t/\y in #1{
    \xdef\o{\r}
    % \def\c{0}
    \ifx\t\y\def\y{0}\fi
    \chit@dbg{2}{Turn \ti\space(\r,\t,y=\y):'}
    \ifx\t\empty\else
      % Count how many are left for this turn
      \chit@dbg{2}{At start of turn \t\space\string\c=\c}
      \def\l{\c}%
      \let\ig\empty
      \foreach \u/\m in \t{
        \ifx\ig\empty
          \ifx\u\empty\else
            \ifx\u\m\def\m{1}\fi
            \ifx\u\chit@oob@spacer%
              \pgfmathparse{\l+\m*#4}\xdef\l{\pgfmathresult}
              \chit@dbg{2}{Got \m\space hspace (#4) -> \l}
            \else
              \ifx\u\chit@oob@vspace%
                \xdef\ig{1}
                \chit@dbg{2}{Got vspace -> \l (\ig)}
              \else
                \pgfmathparse{\l+\m*#3}
                \xdef\l{\pgfmathresult}
                \chit@dbg{2}{Got \m\space units -> \l}
              \fi
            \fi
          \fi
        \fi}
      % Check if there's enough room
      \chit@dbg{2}{To fill the rest of turn needs `\l' compared to
        `\a' (#3*(#2-1))}
      \pgfmathparse{ifthenelse(abs(\l)>=#3*(#2-1),0,1}%
      \xdef\l{\pgfmathresult}%
      \chit@dbg{2}{Break or not `\l'}
      \ifnum\l=0\chit@oob@turnupdate(\c,\r){#3}{#4}\fi
    \fi    
    \ifwg@oob@inv%
      \pic[transform shape] at (\c+.5*#3,\r) {chit/oob turn=\ti};% was dx=0.5
    \else
      \pic[transform shape] at (\c+-.5*#3,\r) {chit/oob turn=\ti};% was dx=-0.5
    \fi%
    %\chit@oob@cellupdate(\c,\r){#2}{#3}{\y}
    \ifx\t\empty\else%
      \def\lv{0}
      \foreach \u/\m in \t{
        %% \chit@dbg{2}{ `\u'=`\m'}
        \ifx\u\empty\else
          \ifx\m\@empty\def\m{1}\fi
          \ifx\u\m\def\m{1}\fi
          \foreach \n in {1,...,\m}{%
            \chit@dbg{2}{OOB Chit is `\u' `\chit@oob@spacer'}%
            \ifx\u\chit@oob@spacer%
              \chit@dbg{3}{Chit `\u' is spacer `\chit@oob@spacer'}
              \pgfmathparse{\c+#4}%
              \xdef\c{\pgfmathresult}%
            \else%
              \ifx\u\chit@oob@vspacer%
                \chit@dbg{3}{Chit `\u' is vspacer `\chit@oob@vspacer'}
                \pgfmathparse{ifthenelse(abs(\c)<0.0001,0,#3)}
                \xdef\ll{\pgfmathresult}
                \chit@dbg{2}{\string\ll=`\ll'}
                \chit@oob@rowupdate(\c,\r){\ll}{#4}
                \xdef\lv{1}
              \else
                \ifnum\chitdbglvl>2%
                  \node[minimum width=#3cm,minimum height=#3cm,
                        draw,transform shape] at (\c,\r) {};
                \fi
                \ifx\u\chit@blank\else%
                  \chit[\u=\ti,zone oob point={\u}{\c}{\r}](\c,\r);%
                \fi%
                \chit@oob@cellupdate(\c,\r){#2}{#3}{\y}
              \fi
            \fi
          }
        \fi
      }
    \fi
    \chit@dbg{2}{ End of chits in turn
      \ti\space(c=`\c',r=`\r',o='\o',y='\y')}
    % --- Not relevant, I think 
    % IF no units where given, then we force \c to be non-zero so that
    % \chit@oob@turnupdate increments the row 
    % \ifx\t\@empty
    %   \def\c{#3}
    %   \chit@dbg{2}{ Turn is empty, set c=`\c'}
    % \fi
    % ---
    %\ifnum\y<0% No explicit number of rows given
    %  \def\c{#3}
    %  \chit@dbg{2}{ No explicit number of rows given, set c=`\c'}
    %\fi
    % In case the user gave and explicit number of rows, add the rows
    % that are missing.  \y is initially set to the number of
    % requested rows, and then decremented every time we go down one
    % row.  So if the number of rows we did so far is N, and the
    % requested number of rows is M, then the loop below adds M-N
    % rows.
    \ifnum\y>0%
      \chit@dbg{2}{ Looping rows from 2 to \y, break when row > \y}%
      \foreach \rr  in {2,...,\y}{
        %\ifnum\rr>\y% A little funny, but \y can be negative!
        %  \chit@dbg{2}{ \space Breaking loop \rr\space > \y}%
        %  \breakforeach%
        %\else%
          \chit@oob@rowupdate(\c,\r){#3}{0}% Extra spacing?
        %\fi
      }
    \fi
    % --- Not relevant I think   
    % This will zero \c.  However, if on entry |\c|>0, then we also
    % increment the row 
    % \chit@oob@turnupdate(\c,\r){#3}{#4}
    % ---
    % Horizontal spacer
    
    %\pgfmathparse{ifthenelse(abs(\c)>=\a,1,0)}\xdef\l{\pgfmathresult}
    \pgfmathparse{\c+1.5*#4}%
    \xdef\c{\pgfmathresult}%
    \ifnum\lv=1%
      \pgfmathparse{\r-#4}
      \chit@oob@rowupdate(\c,\r){0}{#4}
    \else
      \chit@oob@cellupdate(\c,\r){#2}{#3}{\y}
      \ifnum\y<0
        \chit@oob@turnupdate(\c,\r){#3}{#4}
      \else
      \fi
    \fi
    % \xdef\y{0}
    \chit@dbg{2}{End of turn \ti\space(c=`\c',r=`\r',o='\o',y='\y')} 
  }
  \chit@dbg{3}{End of OOB (c=`\c',r=`\r',y=`\y')}
  \@ifnextchar;{\@gobble}{}} 
%    \end{macrocode}
% \iffalse
% </chit>
% --------------------------------------------------------------------
% \fi
