% \iffalse
% --------------------------------------------------------------------
%<*hex>
% \fi
% 
% \subsubsection{Terrain}
% \label{sec:impl:hex:terrain}
%
% With the above main routine for making hexes, we turn to decorating
% a hex  with a terrain.
% 
% \begin{HexKey*}{hex/terrain/image,
%   hex/terrain/pic,
%   hex/terrain/code,
%   hex/terrain/clip}
%
%   We make the namespace \spec{/hex/terrain} to hold the
%   specific terrain keys.  Keys used by terrain identifiers are
%
%   \begin{tabular}{|l|p{.7\linewidth}|} 
%       \hline
%       \rowcolor{headbg}
%       {\color{headfg}\textbf{Name}}
%       & {\color{headfg}\textbf{Description}}\\
%       \hline 
%       \spec{image} & Terrain tile image\\
%       \spec{pic} & Terrain \TikZ{} \spec{pic}ture\\
%       \spec{code} & Arbitary \TikZ{} code\\
%       \spec{clip} & \TikZ{} path to clip terrain\\
%       \hline
%   \end{tabular}
%   
%   Now, we have the keys we'll need for selecting the terrain. These
%   live in the namespace \spec{/hex/terrain}, and we can select
%   between \spec{pic}tures or \spec{image}s (external graphics files)
%   for making the terrain.  We define some short hands to easily
%   select the common terrains.
% 
%    \begin{macrocode}
\newif\if@hex@t@rot\@hex@t@rotfalse%
\tikzset{%
  /hex/terrain/.search also={/tikz},%
  /hex/terrain/.cd,%
  pic/.store in=\hex@t@pic,%
  image/.store in=\hex@t@image,%
  code/.store in=\hex@t@code,%
  clip/.store in=\hex@t@clip,%
  random rotation/.is if=@hex@t@rot,
  rotate/.store in=\hex@t@s@angle,
  pic/.default=,
  image/.default=,
  code/.default=,
  clip/.default=,
  rotate/.default=,
}
\iffalse
\tikzset{
  /hex/terrain/.cd,%
  beach/.style={pic=hex/terrain/beach},
  fields/.style={pic=hex/terrain/fields},
  speckle/.style={pic=hex/terrain/speckle},
  light woods/.style={pic=hex/terrain/light woods},
  woods/.style={pic=hex/terrain/woods},
  swamp/.style={pic=hex/terrain/swamp},
  rough/.style={pic=hex/terrain/rough},
  mountains/.style={pic=hex/terrain/mountains},
  village/.style={pic=hex/terrain/village},
  town/.style={pic=hex/terrain/town},
  city/.style={pic=hex/terrain/city},
}
\else
\tikzset{
  /hex/terrain/.cd,%
  beach/.style={image=wargame.beach},
  fields/.style={image=wargame.fields},
  speckle/.style={image=wargame.speckle},
  light woods/.style={image=wargame.light_woods},
  woods/.style={image=wargame.woods},
  swamp/.style={image=wargame.swamp},
  rough/.style={image=wargame.rough},
  mountains/.style={image=wargame.mountains},
  village/.style={image=wargame.village},
  town/.style={image=wargame.town},
  city/.style={image=wargame.city},
}
\fi
%    \end{macrocode}
% \end{HexKey*}
%
%
% Before we go on, we define the macro that actually generates the
% terrain of a hex.
%
% \begin{Macro}{\hex@do@terrain}
%   If we do have a terrain specified, we start a new scope, this time
%   to clip the terrain by the clipping path specified by
%   \spec{hex=\{terrain=\{clip=...\}\}}.  The first thing into the new
%   scope is to process the keys specified in
%   \spec{hex=\{terrain=...\}}.  This will set the terrain and the
%   clipping of the terrain.
% 
%    \begin{macrocode}
\def\hex@do@terrain{%
  \hex@dbg{5}{Terrain: \meaning\hex@terrain}%
  \edef\hex@t@tmp{[/hex/terrain/.cd,\hex@terrain]}%
  \expandafter\scope\hex@t@tmp% Scope for terrain clipping.
    \hex@dbg{5}{Terrain:
      ^^J  pic:   \meaning\hex@t@pic
      ^^J  image: \meaning\hex@t@image
      ^^J  code:  \meaning\hex@t@code
      ^^J  clip:  \meaning\hex@t@clip}
%    \end{macrocode}
%
% We check to see if we have any clipping \spec{pic}tures.  If so, we
% process these in turn and append the soft path to a macro.  Once
% this is done, we use the soft path as a clipping path for the rest
% of the (terrain) scope.
% 
%    \begin{macrocode}
    \@ifundefined{hex@t@clip}{\let\hex@t@clip\empty}{}
    \ifx\hex@t@clip\empty\else%
      \edef\hex@t@cc{\hex@t@clip}%
      \def\hex@t@c{}
      \foreach \c in \hex@t@cc{%
        \hex@dbg{5}{Clipping to `\c'}
        \expandafter\wg@pic\c\@endwg@pic {}{\wg@tmpa,\wg@tmpb}{%
          save path=\hex@t@tmp}%
        \wg@addto@macro\hex@t@c\hex@t@tmp % Append to clippping
      }%
      \pgfsyssoftpath@setcurrentpath{\hex@t@c}% Set path
      \clip;% Clip to the path
    \fi % End of clipping terrain 
%    \end{macrocode}
% 
% We're new ready to make the terrain.  First, we check to see if the
% relevant storage macros are undefined and if so, \cs{let} them to
% \cs{empty} so that we can deal more easily with the various cases.
% 
%    \begin{macrocode}
    %% Now switch between how to draw the terrain.  If some of the
    %% macros are undefined, define them to be empty
    \@ifundefined{hex@t@pic}{\let\hex@t@pic\empty}{}
    \@ifundefined{hex@t@image}{\let\hex@t@image\empty}{}
    \@ifundefined{hex@t@code}{\let\hex@t@code\empty}{}
    \@ifundefined{hex@t@code}{\let\hex@t@code\empty}{}
%    \end{macrocode}
%
% Possible make rotation. We define a scope and rotate within that.
% 
%    \begin{macrocode}
    \def\hex@t@angle{0}%
    \if@hex@t@rot%
      \pgfmathrandominteger{\hex@t@angle}{0}{5}
      \pgfmathparse{int(60*\hex@t@angle)}\edef\hex@t@angle{\pgfmathresult}%
    \else%
      \@ifundefined{hex@t@s@angle}{}{
        \ifx\hex@t@s@angle\@empty%
        \else%
          \edef\hex@t@angle{\hex@t@s@angle}%
        \fi}%
    \fi%
    \hex@dbg{5}{Will rotate terrain by `\hex@t@angle'}%
%    \end{macrocode}
%
% If we have specified code for the terrain, then execute that.
% 
%    \begin{macrocode}
    \scope[rotate=\hex@t@angle]%  
      \ifx\hex@t@code\empty\else\hex@t@code\fi%
    \endscope% End rotate code
%    \end{macrocode}
% First we check if we have not got terrain images, but terrain
% pictures.  If we have that, we process these in turn.  Note, the
% user can give options to each terrain picture by preceding the
% \spec{pic}ture name with \oarg{options}.
% 
%    \begin{macrocode}
    % If we have no image, check if we have pictures.
    \ifx\hex@t@image\empty%
      \hex@dbg{8}{No terrain images}%
      \ifx\hex@t@pic\empty\else%
        % We have pictures
        \hex@dbg{5}{Terrain pictures}%
        \pgfpointorigin\wg@tmpa=\pgf@x\wg@tmpb=\pgf@y%
        \foreach \i in \hex@t@pic{%
          \wg@pic@all{\i}{}{\the\wg@tmpa,\the\wg@tmpb}{%
            rotate=\hex@t@angle,
            transform shape}}%
      \fi% We have pictures.
%    \end{macrocode}
%    
%
% If the user specified images rather than \spec{pic}tures, then we
% process these in turn.  Again, the user can specify options to each
% terrain image by preceding the image file name with \oarg{options}.
% 
%    \begin{macrocode}
      \else % We have images
        \hex@dbg{5}{Terrain images}%
        \pgfpointorigin\wg@tmpa=\pgf@x\wg@tmpb=\pgf@y%
        \foreach \i in \hex@t@image{%
          \hex@dbg{10}{Terrain image: `\meaning\i'}
          \expandafter\wg@node{%
            \includegraphics[width=2cm]{\i}}\@endwg@node %
          {}{\wg@tmpa,\wg@tmpb}{%
            rotate=\hex@t@angle,%
            shape=rectangle,%
            anchor=center,%
            transform shape,%
            draw=none}%
        }
    \fi%
  \endscope% End of terrain scope
}% End of terrain
%    \end{macrocode}
% \end{Macro}
% 
% 
% 
%

%
% Next, we define some example clippings of the terrain images.
% Specifcally, we make clippings to sextants.  We do this by first
% defining a macro.
%
% \begin{Macro}{\hex@make@sextants}
%
%   When executed this macro will generate some paths that will clip
%   to sextants.  The first argument is the inner radius of the
%   sextant and the second argument is the (possible empty) prefix to
%   put in front of the \spec{sextant} name.
%   
%    \begin{macrocode}
\def\hex@x@r{.7}
\def\hex@make@sextants#1#2{%
  \tikzset{%
    pics/hex/#2sextant/.is choice,
    pics/hex/#2sextant/north east/.style={
      code={
        \path[pic actions](  0:1)--( 60:1)--( 60:#1)--(  0:#1)--cycle;}},
    pics/hex/#2sextant/north/.style={
      code={
        \path[pic actions]( 60:1)--(120:1)--(120:#1)--( 60:#1)--cycle;}},
    pics/hex/#2sextant/north west/.style={
      code={
        \path[pic actions](120:1)--(180:1)--(180:#1)--(120:#1)--cycle;}},
    pics/hex/#2sextant/south west/.style={
      code={
        \path[pic actions](180:1)--(240:1)--(240:#1)--(180:#1)--cycle;}},
    pics/hex/#2sextant/south/.style={
      code={
        \path[pic actions](240:1)--(300:1)--(300:#1)--(240:#1)--cycle;}},
    pics/hex/#2sextant/south east/.style={
      code={
        \path[pic actions](300:1)--(360:1)--(360:#1)--(300:#1)--cycle;}},
    pics/hex/#2sextant/center/.style={
      code={
        \path[pic actions]
        (0:#1)--
        (60:#1)--
        (120:#1)--
        (180:#1)--
        (240:#1)--
        (300:#1)--cycle;}},
    pics/hex/#2sextant/NE/.style=hex/#2sextant/north east,
    pics/hex/#2sextant/NE/.style=hex/#2sextant/north east,
    pics/hex/#2sextant/N/.style=hex/#2sextant/north,
    pics/hex/#2sextant/NW/.style=hex/#2sextant/north west,
    pics/hex/#2sextant/SW/.style=hex/#2sextant/south west,
    pics/hex/#2sextant/S/.style=hex/#2sextant/south,
    pics/hex/#2sextant/SE/.style=hex/#2sextant/south east,
    pics/hex/#2sextant/C/.style=hex/#2sextant/center,
  }%
}
%    \end{macrocode}
% \end{Macro} 
% 
%    \begin{macrocode}
\hex@make@sextants{.7}{}
\hex@make@sextants{.3}{large }
\hex@make@sextants{0}{full }
%    \end{macrocode}
% \iffalse
%</hex>
% --------------------------------------------------------------------
% \fi 
