Debian Codegraph @

texmf-dist/source/latex/wargame/hex/board.dtx

raw
1 % \iffalse
2 % --------------------------------------------------------------------
3 %<*hex>
4 % \fi
5 %
6 % \subsubsection{Board clipping and frame}
7 %
8 % \begin{Macro}{\boardframe}
9 %
10 % Define the bounding box around the board
11 %
12 % \begin{Syntax}
13 % \cs{boardframe}\oarg{margin}\parg{lower=left}\parg{upper-right}
14 % \end{Syntax}
15 %
16 % where \meta{lower-left} and \meta{upper-right} specifies the lower
17 % left and upper right hexes (inclusive) of the board.
18 %
19 %
20 % \begin{macrocode}
21 \def\boardframe{%
22 \@ifnextchar[{\bo@rdframe}{\bo@rdframe[0]}%]
23 }
24 % \end{macrocode}
25 %
26 % Below is our new implementation of \cs{boardframe}. This is split
27 % into parts.
28 %
29 %
30 % First, a macro that will define the path around rectangular placed
31 % hexes. This takes 4 mandatory arguments: lower left column and row,
32 % and upper right column and row, in that order. It also accepts an
33 % optional argument. If this is not empty, then it is assumed to be a
34 % style to apply, and hexes will be drawn using that style. The style
35 % will be passed the hex coordinates and can react accordingly.
36 %
37 % \begin{macrocode}
38 \def\bo@rdfr@me{
39 \@ifnextchar[{\bo@rdfr@me@}{\bo@rdfr@me@[]}%]
40 }
41 \def\bo@rdfr@me@u(#1)#2#3#4#5{
42 \hex@coords@conv{#1}%
43 % \hex@dbg{0}{#1 -> `\hex@x',`\hex@y'}
44 \pgfmathparse{min(#2,\hex@x)}\xdef#2{\pgfmathresult}%
45 \pgfmathparse{min(#3,\hex@y)}\xdef#3{\pgfmathresult}%
46 \pgfmathparse{max(#4,\hex@x)}\xdef#4{\pgfmathresult}%
47 \pgfmathparse{max(#5,\hex@y)}\xdef#5{\pgfmathresult}%
48 \hex@dbg{2}{#1 -> ll=`#2',`#3', ur=`#4',`#5'}%
49 }
50 \def\bo@rdfr@me@[#1]#2#3#4#5{%
51 % Define rtmp and a ctmp to by directions
52 \pgfmathparse{int(\hex@coords@row@fac)}\edef\rtmp{\pgfmathresult}%
53 \pgfmathparse{int(\hex@coords@col@fac)}\edef\ctmp{\pgfmathresult}%
54 % Define vertices for path
55 \def\ctfv{SW}%
56 \def\ctsv{SE}%
57 \def\cbfv{NE}%
58 \def\cbsv{NW}%
59 \def\rrfv{E}%
60 \def\rrsv{NE}%
61 \def\rlfv{W}%
62 \def\rlsv{SW}%
63 % Swap around some definitions based on the row direction
64 \ifnum\rtmp<0%
65 \let\max@short\hex@bot@short@col%
66 \let\min@short\hex@top@short@col%
67 \let\swp\ctfv\let\ctfv\cbsv\let\cbsv\swp%
68 \let\swp\ctsv\let\ctsv\cbfv\let\cbfv\swp%
69 \def\rrsv{SE}%
70 \def\rlsv{NW}%
71 \else%
72 \let\max@short\hex@top@short@col%
73 \let\min@short\hex@bot@short@col%
74 \fi%
75 % Swap around some definitions based on the column direction
76 \ifnum\ctmp<0%
77 \let\swp\ctfv\let\ctfv\ctsv\let\ctsv\swp%
78 \let\swp\cbfv\let\cbfv\cbsv\let\cbsv\swp%
79 \let\swp\rrfv\let\rrfv\rlsv\let\rlsv\swp%
80 \let\swp\rrsv\let\rrsv\rlfv\let\rlfv\swp%
81 \fi%
82 % Define tmp = 0 if no shorts, 1 if top short, 2 if both
83 \pgfmathparse{ifthenelse(\hex@got@top@short,
84 ifthenelse(\hex@got@bot@short,2,1),0)}\edef\tmp{\pgfmathresult}%
85 % If top-short, set factors
86 \ifnum\tmp=1%
87 \def\mnf{-1}%
88 \def\mxf{-1}%
89 \def\mnn{}%
90 \def\mxn{}%
91 % If both short, set factors
92 \else\ifnum\tmp=2%
93 \def\mnf{\rtmp}%
94 \def\mxf{(-\rtmp)}%
95 % If inverse rows, set factors
96 \ifnum\rtmp<0%
97 \def\mnn{}%
98 \def\mxn{not}%
99 \else%
100 \def\mnn{not}%
101 \def\mxn{}%
102 \fi%
103 % If none is short
104 \else%
105 \def\mnf{1}%
106 \def\mxf{1}%
107 \def\mnn{not}%
108 \def\mxn{not}%
109 \fi\fi%
110 % Define row@mn to give least row of column
111 \def\row@mn##1{%
112 \pgfmathparse{int(#3+\mnf*
113 \hex@coords@row@fac*\min@short(##1)*
114 \mnn(\min@short(\hex@coords@col@off)))}%
115 \edef\lr{\pgfmathresult}}%
116 % Define row@mx to give largest row of column
117 \def\row@mx##1{%
118 \pgfmathparse{int(#5+\mxf*
119 \hex@coords@row@fac*\max@short(##1)*
120 \mxn(\max@short(\hex@coords@col@off)))}%
121 \edef\ur{\pgfmathresult}}%
122 %
123 %
124 % Below defines a path around the perimeter of the hexes.
125 %
126 \def\@llx{10000}%
127 \def\@lly{10000}%
128 \def\@urx{-10000}%
129 \def\@ury{-10000}%
130 % Start with an empty path
131 \def\p{}
132 % Loop across least row (can be top if \rtmp<0)
133 \foreach \c in {#2,...,#4}{%
134 \row@mn{\c}%
135 \row@mx{\c}%
136 % \message{^^JColumn: `\c' -> `\lr',`\ur' (#3,#5)}
137 }
138 \foreach \c in {#2,...,#4}{%
139 \row@mn{\c}%
140 \xdef\p{\p
141 (hex cs:c=\c,r=\lr,v=\ctfv)--
142 (hex cs:c=\c,r=\lr,v=\ctsv)--}%
143 \bo@rdfr@me@u(c=\c,r=\lr,v=\ctfv)\@llx\@lly\@urx\@ury%
144 \bo@rdfr@me@u(c=\c,r=\lr,v=\ctsv)\@llx\@lly\@urx\@ury%
145 }%
146 % Go up (down if \rtmp<0) right side
147 \row@mn{#4}%
148 \row@mx{#4}%
149 \foreach \r in {\lr,...,\ur}{%
150 \xdef\p{\p
151 (hex cs:c=#4,r=\r,v=\rrfv)--
152 (hex cs:c=#4,r=\r,v=\rrsv)--}%
153 \bo@rdfr@me@u(c=#4,r=\r,v=\rrfv)\@llx\@lly\@urx\@ury%
154 \bo@rdfr@me@u(c=#4,r=\r,v=\rrsv)\@llx\@lly\@urx\@ury%
155 }%
156 % Go across largest row (can be bottom if \rtmp<0)
157 \foreach \c in {#4,...,#2}{%
158 \row@mx{\c}%
159 % \message{^^JColumn: `\c', max:`\ur'}
160 \xdef\p{\p
161 (hex cs:c=\c,r=\ur,v=\cbfv)--
162 (hex cs:c=\c,r=\ur,v=\cbsv)--}%
163 \bo@rdfr@me@u(c=\c,r=\ur,v=\cbfv)\@llx\@lly\@urx\@ury%
164 \bo@rdfr@me@u(c=\c,r=\ur,v=\cbsv)\@llx\@lly\@urx\@ury%
165 }
166 % Go up (down if \rtmp<0) left side.
167 \row@mn{#2}%
168 \row@mx{#2}%
169 \foreach \r in {\ur,...,\lr}{%
170 \xdef\p{\p
171 (hex cs:c=#2,r=\r,v=\rlfv)--
172 (hex cs:c=#2,r=\r,v=\rlsv)--}%
173 \bo@rdfr@me@u(c=#2,r=\r,v=\rlfv)\@llx\@lly\@urx\@ury%
174 \bo@rdfr@me@u(c=#2,r=\r,v=\rlsv)\@llx\@lly\@urx\@ury%
175 }%
176 % End path with cycle
177 \edef\p{\p cycle}%
178 % Define global path
179 \global\let\hex@board@path\p%
180 \hex@dbg{3}{Hex board path: `\meaning\hex@board@path'}%
181 % If an optional argument was given, then use that to actually make
182 % hexes.
183 \ifx|#1|\else%
184 \foreach[count=\nc] \c in {#2,...,#4}{%
185 \row@mn{\c}%
186 \row@mx{\c}%
187 \foreach \r in {\lr,...,\ur}{%
188 \hex[#1={\c,\r}](c=\c,r=\r)%
189 }%
190 }%
191 \fi%
192 }%
193 % \end{macrocode}
194 %
195 %
196 % This is a no operations style used as default for the macro
197 % \cs{boardhexes} below.
198 %
199 % \begin{macrocode}
200 \tikzset{%
201 /hex/board/no op/.style args={#1,#2}{}}%
202 % \end{macrocode}
203 %
204 % This macro will make the actual hexes using the specified, optional,
205 % style. It builds on \cs{bo@rdfr@me} above.
206 %
207 % \begin{macrocode}
208 \def\boardhexes{%
209 \@ifnextchar[{\bo@rdhexes}{\bo@rdhexes[board/no op]}%]
210 }%
211 \def\bo@rdhexes[#1](#2)(#3){%
212 \hex@coords@conv{#2}%
213 \edef\llc{\hex@col}%
214 \edef\llr{\hex@row}%
215 \hex@coords@conv{#3}%
216 \edef\urc{\hex@col}%
217 \edef\urr{\hex@row}%
218 \bo@rdfr@me[#1]{\llc}{\llr}{\urc}{\urr}}%
219 % \end{macrocode}
220 %
221 % Creates a board frame using \cs{bo@rdfr@me}.
222 %
223 % \begin{macrocode}
224 \tikzset{board frame bb/.code={
225 \pgfkeys{
226 %/tikz/local bounding box=tmp board frame,
227 /tikz/transform shape,
228 /tikz/execute at end scope={%
229 % \hex@dbg{1}{Getting board frame BB}
230 %\wg@get@bb{tmp board frame}
231 \global\let\llx\@llx
232 \global\let\lly\@lly
233 \global\let\urx\@urx
234 \global\let\ury\@ury
235 % \hex@dbg{0}{Board bounding box (\llx,\lly)x(\urx,\ury)}
236 }}}}
237
238 \def\bo@rdframe[#1](#2)(#3){%
239 \hex@coords@conv{#2}%
240 \edef\llc{\hex@col}%
241 \edef\llr{\hex@row}%
242 %
243 \hex@coords@conv{#3}
244 \edef\urc{\hex@col}%
245 \edef\urr{\hex@row}%
246 %
247 \def\margin{#1}%
248 %
249 % This will store the bounding box in tmp node `board frame'
250 \bo@rdfr@me{\llc}{\llr}{\urc}{\urr}%
251 \begin{scope}[board frame bb]%
252 \expandafter\path\hex@board@path;%
253 \end{scope}%
254 \hex@dbg{1}{Board frame LL: -> `\llx',`\lly'}%
255 \pgfmathparse{\llx+ifthenelse(\llx<0,-1,1)*\margin}\edef\llx{\pgfmathresult}%
256 \pgfmathparse{\lly+ifthenelse(\lly<0,-1,1)*\margin}\edef\lly{\pgfmathresult}%
257 %
258 \hex@dbg{1}{Board frame UR: -> `\urx',`\ury'}%
259 \pgfmathparse{\urx+ifthenelse(\urx<0,-1,1)*\margin}\edef\urx{\pgfmathresult}%
260 \pgfmathparse{\ury+ifthenelse(\ury<0,-1,1)*\margin}\edef\ury{\pgfmathresult}%
261 %
262 \pgfmathparse{\urx-\llx}\edef\w{\pgfmathresult}%
263 \pgfmathparse{\ury-\lly}\edef\h{\pgfmathresult}%
264 %% Print to the log
265 \hex@dbg{0}{Board Frame: (\llx,\lly)x(\urx,\ury) (\w x\h) (\llc,\llr)x(\urc,\urr)}%
266 %% Possibly draw
267 \draw[hex/board frame/.try](\llx,\lly) rectangle(\urx,\ury);%
268 %% Store macros
269 \xdef\boardXmin{\llx}%
270 \xdef\boardYmin{\lly}%
271 \xdef\boardXmax{\urx}%
272 \xdef\boardYmax{\ury}%
273 \@ifnextchar;{\@gobble}{}%
274 }
275 % \end{macrocode}
276 %
277 % \end{Macro}
278 %
279 % \begin{Macro}{\boardclip}
280 %
281 % Clip the board to not show incomplete hexes
282 %
283 % \begin{Syntax}
284 % \cs{boardclip}\marg{nx}\marg{ny}\marg{preaction}
285 % \end{Syntax}
286 % \begin{macrocode}
287 \def\boardpath(#1)(#2){%
288 \hex@coords@reset%
289 \tikzset{/hex/coords/.cd, #1}%
290 \edef\llc{\hex@col}%
291 \edef\llr{\hex@row}%
292 %%
293 \hex@coords@reset%
294 \tikzset{/hex/coords/.cd, #2}%
295 \edef\urc{\hex@col}%
296 \edef\urr{\hex@row}%
297 % This will store the bounding box in tmp node `board frame'
298 \bo@rdfr@me{\llc}{\llr}{\urc}{\urr}%
299 %% Use the path to extract the bounding box
300 %\begin{scope}[local bounding box=board frame]
301 % \expandafter\path\hex@board@path;
302 %\end{scope}
303 \global\let\hexboardpath\hex@board@path%
304 \@ifnextchar;{\@gobble}{}%
305 }
306
307 % \end{macrocode}
308 % \begin{macrocode}
309 \def\boardclip(#1)(#2)#3{%
310 \boardpath(#1)(#2)%
311 \draw \ifx|#3|\else[preaction={#3}]\fi%
312 [clip] \hexboardpath;%
313 }
314
315 % \end{macrocode}
316 % \end{Macro}
317 %
318 %
319 % \begin{Macro}{\debuggrid}
320 %
321 % Show a debug grid. This requires \cs{boardframe}.
322 %
323 % \begin{macrocode}
324 \def\debuggrid{%
325 \foreach \i in {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1} {%
326 \pgfmathparse{\i*\boardW+\boardXmin}%
327 \edef\debug@x{\pgfmathresult}%
328 \draw [very thin,gray](\debug@x,\boardYmin) --
329 (\debug@x,\boardYmax) node [below,rotate=90] at
330 (\debug@x,\boardYmin) {$\i$}; }%
331 \foreach \i in {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1} {%
332 \pgfmathparse{\i*\boardH+\boardYmin}%
333 \edef\debug@x{\pgfmathresult}%
334 \draw [very thin,gray] (\boardXmin,\debug@x) --
335 (\boardXmax,\debug@x) node [left,rotate=90] at
336 (\boardXmin,\debug@x) {$\i$}; } }
337 % \end{macrocode}
338 % \end{Macro}
339 %
340 % Some dummy styles. These will be defined by the export class to
341 % facilitate getting information from the board.
342 %
343 % \begin{macrocode}
344 \tikzset{%
345 zoned/.style={},
346 zone scope/.style={},
347 zone path/.style={}
348 }
349 % \end{macrocode}
350 % \iffalse
351 %</hex>
352 % --------------------------------------------------------------------
353 % \fi