% \iffalse
% --------------------------------------------------------------------
%<*hex>
% \fi
% 
% \subsubsection{Edges, borders, roads, rivers, and so on}
%
% Styles of drawing edges, borders, rivers, roads, and railroads. 
% 
%    \begin{macrocode}
% A decoration to extract outline of a path 
\pgfdeclaredecoration{outline}{init}
{%
  \state{init}[next state=tick,width=0pt]{
    \xdef\outlinerev{}}
  \state{tick}[%
  width=+\pgfdecorationsegmentlength]%
  {
    \pgfpathlineto{\pgfpointadd{\pgfpointorigin}{
        \pgfpointpolar{\pgfdecorationsegmentangle}{
          +\pgfdecorationsegmentamplitude}}}
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \message{^^J\the\pgf@x,\the\pgf@y}
    \pgfpointadd{\pgfpointorigin}{
      \pgfpointpolar{-\pgfdecorationsegmentangle}{
        \pgfdecorationsegmentamplitude}}
    \pgfpointtransformed{\pgfpoint{\pgf@x}{\pgf@y}}%
    \message{^^J\the\pgf@x,\the\pgf@y}
    \xdef\outlinerev{\the\pgf@x/\the\pgf@y,\outlinerev}
    \pgf@x=\pgf@xa
    \pgf@y=\pgf@ya
  }%
  \state{final}
  {
    \pgfpathlineto{\pgfpointdecoratedpathlast}
    \foreach \x/\y in \outlinerev{
      \ifx\x\empty\else
         \ifx\y\empty\else
           \pgf@xa=\x
           \pgf@ya=\y
           \pgf@nlt@lineto{\pgf@xa}{\pgf@ya}
        \fi
      \fi
    }
  }%
}%
%    \end{macrocode}
% 
% A decoration to make a fortification line 
% 
%    \begin{macrocode}
\pgfdeclaredecoration{fortification}{initial}
{
  \state{initial}[width=4\pgflinewidth]
  {
    \pgfpathlineto{\pgfpoint{2\pgflinewidth}{0}}
    \pgfpathlineto{\pgfpoint{2\pgflinewidth}{2\pgflinewidth}}
    \pgfpathlineto{\pgfpoint{4\pgflinewidth}{2\pgflinewidth}}
    \pgfpathlineto{\pgfpoint{4\pgflinewidth}{0}}
  }
  \state{final}
  {
    \pgfpathlineto{\pgfpointdecoratedpathlast}
  }
}
%    \end{macrocode}
% 
% Roads, railroads, rivers, borders. 
%
%    \begin{macrocode}
\tikzset{
  hex/road/.style={
    rounded corners=3\pgflinewidth,% .25cm,
    color=black,
    transform shape,
    scale line widths,
    thick,
    every hex road/.try,
  },
  hex/railroad/.style={
    %scale line widths,
    rounded corners=.25cm,
    color=gray!50!black,
    transform shape,
    every hex railroad/.try,
    postaction={draw,decorate},
    decoration={ticks,
      segment length=9\pgflinewidth,
      amplitude=3\pgflinewidth,%.1cm
    }
  },
  hex/river/.style={
    color=blue,
    scale line widths,
    scale rounded corners,
    line width=3pt,
    transform shape,
    every hex river/.try,
    decorate,
    decoration={random steps,
      segment length=3\pgflinewidth,
       amplitude=1.5\pgflinewidth,
       pre=lineto,
       post=lineto,
       pre length=.5\pgflinewidth,
       post length=.5\pgflinewidth},
     rounded corners=.75\pgflinewidth},
   hex/border/.style={
     color=gray,
     dashed,
     transform shape,
     scale line widths,
     very thick,
     rounded corners=3\pgflinewidth,
     every hex border/.try
   },
   % 
   % Fortification line
   % 
   hex/fortified line/.style={
     draw=brown!50!black,
     scale line widths,
     line width=2pt,
     every hex fortification line/.try,
     decoration={fortification,raise=-2\pgflinewidth},
     decorate},
   % every river/.style={},
   % every road/.style={},
   % every railroad/.style={},
   % every border/.style={},
}
%    \end{macrocode}
%
% \begin{Macro}{\road,\railroad,\river,\border}
%   
%    \begin{macrocode}
\def\road{%
  %\hex@dbg{3}{Road}
  \@ifnextchar[{\road@}{\road@[]}%]
}
\def\road@[#1]{\draw[hex/road,every hex road/.try,#1]}
\def\railroad{%
  %\hex@dbg{3}{Rail road}
  \@ifnextchar[{\railroad@}{\railroad@[]}%]
}
\def\railroad@[#1]{\draw[hex/railroad,every hex railroad/.try,#1]}
\def\river{%
  %\hex@dbg{3}{River}
  \@ifnextchar[{\river@}{\river@[]}%]
}
\def\river@[#1]{\draw[hex/river,#1]}
\def\border{%
  \hex@dbg{3}{Border}
  \@ifnextchar[{\border@}{\border@[]}%]
}
\def\border@[#1]{\draw[hex/border,every hex border/.try,#1]}
\def\fortifiedline{%
  \@ifnextchar[{\fortifiedline@}{\fortifiedline@[]}%]
}%
\def\fortifiedline@[#1]{%
  \draw[hex/fortified line,every hex fortified line/.try,#1]}
%    \end{macrocode}
% \end{Macro}
%
%
% \iffalse
% --------------------------------------------------------------------
% \fi
% 
% \subsubsection{Other paths}
%
% \begin{Macro}{\shiftScalePath}
%   Shifts and scales a path and defines a macro to contain the path
% 
%   \begin{Syntax}
%     \cs{shiftScalePath}\marg{macro}\marg{relative-coordinates}
%   \end{Syntax}
%   
%   where \meta{relative-coordinates} is a comma separated list of
%   relative coordinates (to the lower-left and upper-right corners)
%   
%   \begin{Syntax}
%     \meta{x}/\meta{y}
%   \end{Syntax}
%
%   Note, this requires that \cs{boardXmin},\cs{boardYmin} and
%   \cs{boardXmax},\cs{boardYmax} is defined.  This can be done using
%   the \cs{boardframe} macro.
%
%    \begin{macrocode}
\def\shiftScalePath#1#2{%
  \let\tmp@path\@undefined%
  \foreach \x/\y in {#2}{%
    \pgfmathparse{\x*\boardW+\boardXmin}\xdef\tmp@x{\pgfmathresult}%
    \pgfmathparse{\y*\boardH+\boardYmin}\xdef\tmp@y{\pgfmathresult}%
    \@ifundefined{tmp@path}{\def\tmp@path{}}{\xdef\tmp@path{\tmp@path--}}%
    \xdef\tmp@path{\tmp@path(\tmp@x,\tmp@y)}}%
  \expandafter\xdef\csname #1\endcsname{\tmp@path}}
%    \end{macrocode}
% \end{Macro}
%
% \iffalse
% --------------------------------------------------------------------
% \fi
% 
% \subsubsection{Move, attacks, retreats from hex to hex}
%
% \begin{Macro}{\hex@getscale}
%
% Get current scaling factor.
% 
%    \begin{macrocode}
\def\hex@getscale#1{%
  \begingroup
  \pgfgettransformentries{%
    \scaleA}{%
    \scaleB}{%
    \scaleC}{%
    \scaleD}{%
    \whatevs}{%
    \whatevs}%
  \pgfmathsetmacro{#1}{sqrt(abs(\scaleA*\scaleD-\scaleB*\scaleC))}%
  \expandafter
  \endgroup
  \expandafter\def\expandafter#1\expandafter{#1}%
}
%    \end{macrocode}
% \end{Macro}
%
% Key to get the scale
% 
%    \begin{macrocode}
\tikzset{%
  hex/get scale/.code={
    \hex@getscale{\hex@scale}},
}
%    \end{macrocode}
%
% Style for moves.  Use like
%
% \begin{Syntax}
%   \cs{path}[move] \meta{coordinates};
% \end{Syntax}
% 
%    \begin{macrocode}
\tikzset{%
  % Argument is colour
  hex/move/.style={
    hex/get scale,
    decorate,
    decoration={
      markings,
      mark=between positions 0 and 1 step 0.75*\hex@scale*\hex@dy with {
        \node [single arrow,
               single arrow head extend=.1*\hex@scale*\hex@dy,
               fill=#1,
               inner sep=0.05*\hex@scale*\hex@dy,
               minimum width=0.02*\hex@scale*\hex@dy,
               minimum height=\hex@scale*\hex@dy/2,
               transform shape]{};
      }
    },
  },
%    \end{macrocode}
%
% A short move style
%
% \begin{Syntax}
%   \cs{path}[short move] \meta{coordinates};
% \end{Syntax}
% 
% 
%    \begin{macrocode}
  % Argument is colour
  hex/short move/.style={
    hex/get scale,
    decorate,
    decoration={
      markings,
      mark=between positions 0 and 1 step 0.5*\hex@scale*\hex@dy with {
        \node [single arrow,
               single arrow head extend=.1*\hex@scale*\hex@dy,
               fill=#1,
               inner sep=0.05*\hex@scale*\hex@dy,
               minimum width=0.02*\hex@scale*\hex@dy,
               minimum height=\hex@scale*\hex@dy/3,
               transform shape]{};
      }
    },
  },
%    \end{macrocode}
%
% A short move style
%
% \begin{Syntax}
%   \cs{path}[long move] \meta{coordinates};
% \end{Syntax}
% 
% 
%    \begin{macrocode}
  % Argument is colour
  hex/long move/.style={
    hex/get scale,
    transform shape,
    decorate,
    decoration={
      markings,
      mark=between positions 0 and -.7*\hex@scale*\hex@dy
      step 2*\hex@scale*\hex@dy with {
        \node [single arrow,
               single arrow head extend=3pt,
               fill=#1,
               anchor=west,
               inner sep=\hex@scale*.25mm,
               outer sep=.3*\hex@scale*\hex@dy,
               minimum width=0.02*\hex@scale*\hex@dy,
               minimum height=1.4*\hex@scale*\hex@dy,
               transform shape]{};
      }
    },
  },
%    \end{macrocode}
%
% A short move style
%
% \begin{Syntax}
%   \cs{path}[move with start] \meta{coordinates};
% \end{Syntax}
% 
% 
%    \begin{macrocode}
  % Argument is colour
  hex/move with start/.style={
    hex/get scale,
    decorate,
    decoration={
      markings,
      mark=at position 0 with {
        \node [inner sep=0,
        circle,
        minimum size=\hex@scale*5mm,
        fill=#1,
        transform shape] {};},
      mark=between positions 0 and 1 step 0.75*\hex@scale*\hex@dy with {
        \node [single arrow,
               single arrow head extend=.1*\hex@scale*\hex@dy,
               fill=#1,
               inner sep=0.05*\hex@scale*\hex@dy,
               minimum width=0.02*\hex@scale*\hex@dy,
               minimum height=\hex@scale*\hex@dy/2,
               transform shape]{};
      }
    },
  },
  % Default fill colour is black
  hex/move/.default=black,
  hex/move with start/.default=black,
  hex/short move/.default=black,
  hex/long move/.default=black,
  % Arguments are draw and fill color
%    \end{macrocode}
%
% A move cost style 
%
% \begin{Syntax}
%   \cs{path}[move] ...(coordinate)node[hex/move cost]{} ...;
% \end{Syntax}
% 
%    \begin{macrocode}
  hex/move cost/.style 2 args={
    minimum size=1mm,
    inner sep=0.1mm,
    circle,
    fill=#2,
    transform shape,
    text=#1,
    font=\sffamily\bfseries\fontsize{14.4}{17}\selectfont},
  hex/move cost/.default={black}{none},
  % Argument is fill colour 
%    \end{macrocode}
%
% A short line style for retreates, advances, and so on
%
% \begin{Syntax}
%   \cs{path}[short line] (start)--(end);
% \end{Syntax}
% 
%    \begin{macrocode}
  hex/short line/.style={%
    hex/get scale,
    inherit options/.code={\csname tikz@options\endcsname},
    inherit options,
    decorate,
    decoration={
      markings,
      mark=between positions \hex@scale*\hex@dy
      and 1 step 2*\hex@scale*\hex@dy with {
        \node [single arrow,draw=black,fill=#1,
        single arrow head extend=\hex@scale*3pt,
        inner sep=1mm,
        minimum width=0.75*\hex@scale*\hex@dy,
        minimum height=\hex@scale*\hex@dy,
        transform shape]{};
      }
    },
  },
%    \end{macrocode}
%
% An attack indication style
%
% \begin{Syntax}
%   \cs{path}[attack] (start)--(end);
% \end{Syntax}
% 
%    \begin{macrocode}
  % Argument is fill color
  hex/attack/.style={
    hex/get scale,
    inherit options/.code={\csname tikz@options\endcsname},
    inherit options,
    decorate,
    decoration={
      markings,
      mark=between positions \hex@scale*\hex@dy
      and 1 step 2*\hex@scale*\hex@dy with {
        \node [regular polygon,
        fill=#1,
        draw=#1,
        regular polygon sides=3,
        inner sep=0,
        minimum size=0.75*\hex@scale*\hex@dy,
        rotate=-90,
        transform shape]{};
      }
    },
  },
%    \end{macrocode}
%
% Short hands
%
% \begin{Syntax}
%   \cs{path}[attack] (start)--(end);
% \end{Syntax}
% 
%    \begin{macrocode}
  % Default colour is red for attachs
  hex/attack/.default=red!70!black,
  %% 
  hex/retreat/.style={hex/short line=#1},
  hex/retreat/.default=white,
  %%
  hex/advance/.style={hex/short line=#1},
  hex/advance/.default={green!70!black},
}
%    \end{macrocode}
% \iffalse
%</hex>
% --------------------------------------------------------------------
% \fi