summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/wargame/source/hex/terrain.dtx
blob: 765b38e759b3a1ed0d349ba48f4405f26f787b75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
% \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}
\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,%
  pic/.default=,
  image/.default=,
  code/.default=,
  clip/.default,
}
\iffalse
\tikzset{
  /hex/terrain/.cd,%
  beach/.style={pic=hex/terrain/beach},
  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=beach},
  light woods/.style={image=light_woods},
  woods/.style={image=woods},
  swamp/.style={image=swamp},
  rough/.style={image=rough},
  mountains/.style={image=mountains},
  village/.style={image=village},
  town/.style={image=town},
  city/.style={image=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%
      \def\hex@t@c{}
      \foreach \c in \hex@t@clip{%
        \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}{}
%    \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%
        \wg@pic@all{\hex@t@pic}{}{\the\wg@tmpa,\the\wg@tmpb}{}%
      \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}{%
            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