% \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)}%
  \xdef#2{\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\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}
  \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'}%
            \ifx\u\chit@oob@spacer%
              \chit@dbg{3}{Chit `\u' is spacer `\chit@oob@spacer'}
              \pgfmathparse{\c+#4}%
              \xdef\c{\pgfmathresult}%
            \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
    \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}{#4}
        %\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}
%
% \iffalse
% </chit>
% --------------------------------------------------------------------
% \fi
