% \iffalse
% --------------------------------------------------------------------
%<*chit>
% \fi
% \subsubsection{Other pictures}
% Pictures for frame, factors, left, right, and below. 
%    \begin{macrocode}
\tikzset{
  pics/chit/shade/.style={
    code={%
      \path[fill=white,opacity=#1,pic actions] (-.6,-.6) rectangle(.6,.6);}},
  pics/chit/eliminate/.style={
    code={%
      \path[fill=red,opacity=#1,pic actions] (-.6,-.6) rectangle(.6,.6);}},
  pics/chit/shade/.default=0.5,
  pics/chit/eliminate/.default=0.25,
}
\def\shadechit(#1){%
  \pic at (#1) {chit/shade};}
\def\eliminatechit(#1){%
  \pic at (#1) {chit/eliminate};}
%    \end{macrocode}
% 
%
% \iffalse
% --------------------------------------------------------------------
% \fi
% \subsubsection{Stacking of chits}
%
% Stacking of chits.  The key \spec{chit/stack direction} sets the
% default direction to make the stack in.
% 
%    \begin{macrocode}
% offset, location, direction, list
\tikzset{%
  chit/stack direction/.store in=\chit@stack@dir,
  chit/stack direction/.initial={(.3,.3)},
}
%    \end{macrocode}
%
% Now the code 
%
%    \begin{macrocode}
\def\chit@stack@dir{(.3,.3)}
\def\stackchits(#1){%
  \@ifnextchar({\st@ckchits{#1}}{\st@ckchits{#1}(.3,.3)}%)
}
\def\st@ckchits#1(#2)#3{
  \chit@dbg{2}{Stacking chits `#1', `#2', `#3'}
  \edef\xy{#1}
  \chit@dbg{4}{Stack start at \xy}
  \foreach[count=\i from 0] \c/\o in {#3} {%
    \ifx\c\empty\else%
      \edef\ccc{\c}
      \chit@dbg{2}{Adding \meaning\ccc\space to stack at (\xy)' `\o'}
      \expandafter\ccc(\xy)
      %%
      \ifx\c\o\else
        %\chit@dbg{0}{Option: \o}
        \edef\ccc{\o}
        \expandafter\ccc(\xy)
      \fi
      \expandafter\ccc(\xy)
      \tikzmath{%
        coordinate \cc;
        \cc = (\xy) + (#2);}
      \xdef\xy{\cc}
    \fi
  }
}
%    \end{macrocode}
%
% \iffalse
% --------------------------------------------------------------------
% \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\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%
  }%
}
\def\wg@oob#1#2#3#4{
  \def\r{0}
  \chit@dbg{1}{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{1}{Turn \ti\space(\r,\t,y=\y):'}
    \ifwg@oob@inv%
      \pic at ( .5,\r) {chit/oob turn=\ti};%
    \else
      \pic at (-.5,\r) {chit/oob turn=\ti};%
    \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}{%
            \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
    \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{1}{End of turn \ti\space(c=`\c',r=`\r',o='\o',y='\y')} 
  }
  \chit@dbg{2}{End of OOB (c=`\c',r=`\r',y=`\y')}
}
%    \end{macrocode}
%
% \iffalse
% --------------------------------------------------------------------
% \fi
% \subsubsection{Table of chits}
%
%    \begin{macrocode}
\tikzset{
  chit/cell background/.style={fill=black},
  blank chit/.style={/chit/frame={draw=none,fill=none}},
}
%    \end{macrocode}
% 
% These macros are used when we set tables of chits.  This allows us
% to define blank spaces in the table by giving the element
% \texttt{blank~chit}.
% 
%    \begin{macrocode}
\def\chit@blank{blank chit}
\def\chit@cellbg(#1,#2)#3{%
  \draw[chit/cell background](#1-#3/2,#2-#3/2) rectangle++(#3,#3);
}
%    \end{macrocode}
% 
% \begin{Macro}{\ifchits@reset}
%
%   This `if' controls whether to reset the coordinates to the origin
%   when \cs{chits} is called.  If true, then reset for a new table.
%   
%    \begin{macrocode}
\newif\ifchits@reset\chits@resettrue
%    \end{macrocode}
% \end{Macro}
% 
% \begin{Macro}{\chits,\@chits,\chit@sng@cellupdate}
%    \begin{macrocode}
\def\chit@sng@cellupdate(#1,#2)#3#4{%
  \chit@dbg{2}{Current `#1' vs `#4'*(`#3'+1)}
  \pgfmathparse{ifthenelse(#1>=#4*(#3-1),#2-#4,#2)}%
  \xdef#2{\pgfmathresult}%
  \pgfmathparse{ifthenelse(#1>=#4*(#3-1),0,#1+#4)}%
  \xdef#1{\pgfmathresult}%
}
%    \end{macrocode}
%
% The stared version (\cs{chits*}) of this macro continues the
% previously set chit table.
% 
%    \begin{macrocode}
\def\chits{%
  \@ifstar{\chits@resetfalse\@chits}{\chits@resettrue\@chits}}
%    \end{macrocode}
% 
%    \begin{macrocode}
\def\@chits#1#2#3{
  \ifchits@reset
    \def\r{0}%
    \def\c{0}%
  \fi
  \chit@dbg{1}{Chits to make: #1}%
  \foreach[count=\ti from 0] \t/\x in #1{%
    \chit@dbg{2}{Turn `\t' with option `\x'}
    \ifx\t\empty\else%
      \foreach \u/\m in \t{%
        \ifx\u\empty\else%
          \chit@dbg{2}{Next chit `\u' with possible multiplicity `\m'}%
          \ifx\m\@empty\def\m{1}\fi%
          \ifx\u\m\def\m{1}\fi%
          \chit@dbg{2}{Next chit `\u' multiplicity `\m'}%
          \foreach \n in {1,...,\m}{%
            \ifx\u\chit@blank%
              \chit@dbg{3}{Ignoring blank chit:\u}%
            \else%
              \chit@cellbg(\c,\r){#3}%
              \chit[\u=\ti](\c,\r)%
              \chit@sng@cellupdate(\c,\r){#2}{#3}%
            \fi%
          }%
        \fi%
      }%
    \fi%
  }%
}
%    \end{macrocode}
% \end{Macro}
%
% \begin{Macro}{\doublechits,
%   \@doublechits,
%   \chit@dbl@cellupdate,
%   \chit@dbl@flip}
% \begin{enumerate}
% \item coordinates
% \item coordinates
% \item cell-size
% \end{enumerate}
% 
%    \begin{macrocode}
\def\chit@dbl@flip(#1,#2)#3{%
  \pgfmathparse{-#1}%
  \xdef\mc{\pgfmathresult}%
}
%    \end{macrocode}
% 
%   \begin{enumerate}
%   \item coordinates
%   \item coordinates
%   \item Number of columns
%   \item cell-size
%   \end{enumerate}
%
%    \begin{macrocode}
\def\chit@dbl@cellupdate(#1,#2)#3#4{%
  \pgfmathparse{ifthenelse(#1<-#4/2,#2,#4+#2)}%
  \xdef#2{\pgfmathresult}%
  \pgfmathparse{ifthenelse(#1<-#4/2,#4+#1,-(#3-.5)*#4)}%
  \xdef#1{\pgfmathresult}%
}
%    \end{macrocode}
%
%   \begin{enumerate}
%   \item List of list of keys
%   \item Number of columns
%   \item size of each cell
%   \end{enumerate}
% 
%   The stared version (\cs{doublechits*}) of this macro continues the
%   previously set chit table.
% 
%    \begin{macrocode}
\def\doublechits{%
  \@ifstar{\chits@resetfalse\@doublechits}{\chits@resettrue\@doublechits}}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@doublechits#1#2#3{%
  \chit@dbg{1}{Setting double-sided chits: #1}
  \ifchits@reset
    \pgfmathparse{-(#2-.5)*#3}
    \xdef\c{\pgfmathresult}
    \def\r{0}
  \fi
  
  \foreach[count=\ti from 0] \t/\x in #1{
    \ifx\t\empty\else%
      \foreach \u/\m in \t{
        \ifx\u\empty\else
          \ifx\m\@empty\def\m{1}\else%
            \ifx\u\m\def\m{1}\fi\fi
          \chit@dbg{2}{`\u'=`\m' (\c,\r)}
          \foreach \n in {1,...,\m}{%
            \ifx\u\chit@blank
              \chit@dbg{3}{Ignoring blank chit:\u}
            \else
              \chit@cellbg(\c,\r){#3}
              \chit[\u=\ti](\c,\r)
              \chit@dbl@flip(\c,\r){#3}
              \chit@cellbg(\mc,\r){#3}
              \chit[\u\space flipped=\ti,zone turn=\t,zone mult=\n](\mc,\r)
              \chit@dbl@cellupdate(\c,\r){#2}{#3}
            \fi
          }
        \fi
      }
    \fi
  }
  \draw[dashed](0,-3*#3/4)--(0,\r-#3/4);
  \draw[dashed,<-] (#3/5,-2*#3/3)--(#3/2,-2*#3/3) node[anchor=west]{Back};
  \draw[dashed,<-] (-#3/5,-2*#3/3)--(-#3/2,-2*#3/3) node[anchor=east]{Front};
}  
%    \end{macrocode}
% \end{Macro}
%    
% \iffalse
% --------------------------------------------------------------------
% \fi
% \subsubsection{Some utilities}
%
%
% Game turn marker
%
%    \begin{macrocode}
\tikzset{
  number chit/.pic={
    \node[shape=rectangle,font=\sffamily\bfseries\LARGE]{%
      \begin{tabular}{c} #1\end{tabular}};},
  game turn/.pic={
    \node[shape=rectangle,font=\sffamily\bfseries]{%
      \begin{tabular}{c} Game\\Turn\end{tabular}};},
  game turn/.style={
    /chit/full={game turn},
    color=black,
    fill=white},
  game turn flipped/.style={game turn},
  dummy chit/.style={fill=white},
}
%    \end{macrocode}
%
% Marks of chits
% 
%    \begin{macrocode}
\providecommand\chitmark[2][]{\tikz[scale=.25,#1]{\chit[#2]}}
%    \end{macrocode}
%
% Stacking mark
% 
%    \begin{macrocode}
\tikzset{
  wg stacking/.style={fill=white,
    /chit/symbol={[faction=friendly,command=land]}},
}
\DeclareRobustCommand\stackmark[1][]{%
  \tikz[baseline=(current bounding box.center),scale=.3,#1]{
    \stackchits(0,0)(.3,-.3){%
      \noexpand\chit[wg stacking],
      \noexpand\chit[wg stacking],
      \noexpand\chit[wg stacking]}}}
%    \end{macrocode}
%
%  ZOC mark
%
%    \begin{macrocode}
\DeclareRobustCommand\zocmark[1][]{%
  \tikz[baseline=(current bounding box.center),scale=.1,#1]{%
    \begin{scope}[hex/first row and column are=0,
      hex/row direction is=normal,
      hex/column direction is=normal,
      hex/short columns=none]
      \hex[label=,fill=gray](c=1,r=1)%
      \hex[label=,fill=white](c=1,r=2)%
      \hex[label=,fill=white](c=1,r=0)%
      \hex[label=,fill=white](c=0,r=0)%
      \hex[label=,fill=white](c=0,r=1)%
      \hex[label=,fill=white](c=2,r=1)%
      \hex[label=,fill=white](c=2,r=0)
    \end{scope}}}
%    \end{macrocode}
%
%    \begin{macrocode}
\tikzset{
  zone point/.code n args={3}{},
  zone oob point/.code n args={3}{}}
%    \end{macrocode}
%
% \iffalse
%</chit>
% \fi
