summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/wargame/source/hex/board.dtx
blob: fd0af295fbf46a1b285f1ae2d11b29e16b560e4b (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
249
250
251
252
253
% \iffalse
% --------------------------------------------------------------------
%<*hex>
% \fi
% 
% \subsubsection{Board clipping and frame}
%
% \begin{Macro}{\boardframe}
% 
%   Define the bounding box around the board
%   
%   \begin{Syntax}
%     \cs{boardframe}\oarg{margin}\parg{lower=left}\parg{upper-right}\marg{margin}
%   \end{Syntax}
%   
%   where \meta{lower-left} and \meta{upper-right} specifies the lower
%   left and upper right hexes (inclusive) of the board.  
%   
%
%    \begin{macrocode}
\tikzset{
  hex/board frame/.style={draw}
}
\def\boardframe{%
  \@ifnextchar[{\bo@rdframe}{\bo@rdframe[0]}%}
}
\def\bo@rdframe[#1](#2)(#3){%
  \hex@coords@conv{#2}
  \edef\llx{\hex@x}
  \edef\lly{\hex@y}
  \edef\llc{\hex@col}
  \edef\llr{\hex@row}
  \edef\ellc{\hex@eff@col}
  \edef\ellr{\hex@eff@row}
  %
  \hex@coords@conv{#3}
  \edef\urx{\hex@x}
  \edef\ury{\hex@y}
  \edef\urc{\hex@col}
  \edef\urr{\hex@row}
  \edef\eurc{\hex@eff@col}
  \edef\eurr{\hex@eff@row}
  %
  \def\margin{#1}
  %
  \hex@dbg{2}{%
    Board Hex range: (\llc,\llr)x(\urc,\urr)
    ^^JEffective range: (\ellc,\ellr)x(\eurc,\eurr)
    ^^JBB:              (\llx,\lly)x(\urx,\ury)}%
  \ifnum\hexdbglvl>1
    %\draw[red,very thick](hex cs:c=\llc,r=\llr) rectangle(hex cs:c=\urc,r=\urr);
    \draw[red,ultra thick,dashed](\llx,\lly) rectangle(\urx,\ury);
    \draw[->,very thick,blue] (0,0) -- (0,1) (0,0) -- (1,0);
  \fi
  % Calculate how many half hex hides to add to the "bottom"
  %
  \def\oddeven{isodd}
  \ifnum\hex@coords@row@fac<0\def\oddeven{iseven}\fi%
  \pgfmathparse{
    ifthenelse(\hex@got@bot@short(\ellc),
     ifthenelse(\hex@bot@short@col(\llc)*not(\oddeven(\ellc)),2,
      ifthenelse(\hex@bot@short@col(\llc),0,1)),
     ifthenelse(\oddeven(\ellc),1,2))}
  \edef\olly{\pgfmathresult}%
  \hex@dbg{2}{Delta lly: \olly half heights}
  % Calculate how many half hex heights to add to the "top"
  \def\oddeven{iseven}
  \ifnum\hex@coords@row@fac<0\def\oddeven{isodd}\fi%
  %
  \pgfmathparse{
    ifthenelse(\hex@got@top@short(\urc),
     ifthenelse(\hex@top@short@col(\urc)*\oddeven(\eurc),0,
      ifthenelse(\hex@top@short@col(\urc),2,1)),
     ifthenelse(\oddeven(\eurc),1,2))}
  \edef\oury{\pgfmathresult}%
  \hex@dbg{2}{Delta ury: \oury half heights}
  % Calculate new LLY and URY
  \pgfmathparse{\lly-\hex@coords@row@fac*(\olly*\hex@yy+\margin)}
  \edef\lly{\pgfmathresult}
  \pgfmathparse{\ury+\hex@coords@row@fac*(\oury*\hex@yy+\margin)}
  \edef\ury{\pgfmathresult}
  % Calculate new LLX and URX 
  \pgfmathparse{\llx-1-\margin}\edef\llx{\pgfmathresult}
  \pgfmathparse{\urx+1+\margin}\edef\urx{\pgfmathresult}
  % Calculate width and height 
  \pgfmathparse{\urx-\llx}\edef\w{\pgfmathresult}
  \pgfmathparse{\ury-\lly}\edef\h{\pgfmathresult}
  \hex@dbg{0}{Board Frame: (\llx,\lly)x(\urx,\ury) (\w x\h) (\llc,\llr)x(\urc,\urr)}
  \draw[hex/board frame/.try](\llx,\lly) rectangle(\urx,\ury);
  \xdef\boardXmin{\llx}%
  \xdef\boardYmin{\lly}%
  \xdef\boardXmax{\urx}%
  \xdef\boardYmax{\ury}%
}
  
%    \end{macrocode}
% \end{Macro}
%    
% \begin{Macro}{\boardclip}
% 
%   Clip the board to not show incomplete hexes 
% 
% \begin{Syntax}
%   \cs{boardclip}\marg{nx}\marg{ny}\marg{preaction}  
% \end{Syntax}
%    \begin{macrocode}
\def\boardpath(#1)(#2){%
  \hex@coords@reset%
  \tikzset{/hex/coords/.cd, #1}
  \edef\llx{\hex@col}
  \edef\lly{\hex@row}
  %%
  \hex@coords@reset%
  \tikzset{/hex/coords/.cd, #2}
  \edef\urx{\hex@col}
  \edef\ury{\hex@row}
  \let\board@odd\@undefined%
  \hex@dbg{1}{Board BB in hex: (\llx,\lly)x(\urx,\ury)}
  %%
  \def\fv{south}
  \def\sv{north}
  \ifnum\hex@coords@row@fac<0
    \def\fv{north}
    \def\sv{south}
  \fi

  \edef\hex@board@path{(hex cs:c=\llx,r=\lly,v=\fv\space west)}
  %% First the left side
  \foreach \r in {\lly,...,\ury} {%
    \edef\t{
      --(hex cs:c=\llx,r=\r,v=west)
      --(hex cs:c=\llx,r=\r,v=\sv\space west)}
    \wg@addto@macro{\hex@board@path}{\t}}
  %% Then for top of board
  \foreach \c in {\llx,...,\urx} {%
    % To be done
    %\pgfmathparse{int(ifthenelse(\hex@bot@short@col(\c),1,0))}
    %\edef\tmp{\pgfmathresult}
    %\ifnum\tmp>0
    %\edef\t{
    %  --(hex cs:c=\c,r=\ury,v=\sv\space west)
    %  --(hex cs:c=\c,r=\ury,v=\sv\space east)}
    %\else
    \edef\t{
      --(hex cs:c=\c,r=\ury,v=\fv\space east)
      --(hex cs:c=\c,r=\ury,v=\fv\space west)}
    %\fi
    \wg@addto@macro{\hex@board@path}{\t}}
  %% Then for right of board
  \foreach \r in {\ury,...,\lly} {%
    \edef\t{
      --(hex cs:c=\urx,r=\r,v=east)
      --(hex cs:c=\urx,r=\r,v=\fv\space east)}
    \wg@addto@macro{\hex@board@path}{\t}}
      
  %% Then for bottom of board
  \edef\t{--(hex cs:r=\lly,c=\urx,v=\fv\space west)}
  \wg@addto@macro{\hex@board@path}{\t}
  \foreach \c in {\urx,...,\llx} {%
    \pgfmathparse{int(ifthenelse(\hex@bot@short@col(\c),1,0))}
    \edef\tmp{\pgfmathresult}
    \ifnum\tmp>0
    \edef\t{
      --(hex cs:c=\c,r=\lly,v=\sv\space east)
      --(hex cs:c=\c,r=\lly,v=\sv\space west)}
    \else
    \edef\t{
      --(hex cs:c=\c,r=\lly,v=\fv\space east)
      --(hex cs:c=\c,r=\lly,v=\fv\space west)}
    \fi
    \wg@addto@macro{\hex@board@path}{\t}}
    
  \def\t{--cycle}
  \wg@addto@macro{\hex@board@path}{\t}
  \global\let\hexboardpath\hex@board@path
}
%\def\boardclip#1#2#3{%
%  \pgfmathparse{int(#1-1)}\xdef\board@range{\pgfmathresult,...,0}%
%  %% \show\board@range
%  \draw \ifx|#3|\else[preaction={#3}]\fi%
%  [clip]
%  % [decorate,decoration={show path construction,
%  % moveto code={\fill[red](\tikzinputsegmentfirst) circle(2pt)
%  % node [fill=none,below]{moveto};},
%  % lineto code={\draw[thick,blue,->](\tikzinputsegmentfirst)--
%  % (\tikzinputsegmentlast) node [above] {lineto};},
%  % curveto code={\draw[thick,green,->](\tikzinputsegmentfirst)..
%  % controls(\tikzinputsegmentsupporta) and
%  % (\tikzinputsegmentsupportb)
%  % ..(\tikzinputsegmentlast) node[above]{curveto};},
%  % closepath code={\draw[thick,orange,->](\tikzinputsegmentfirst)--
%  % (\tikzinputsegmentlast) node [above] {closepath};}
%  % }]
%  (hex cs:r=0,c=0,v=south west)
%  %% First the left side
%  \foreach \r in {0,1,...,#2} {%
%    --(hex cs:c=0,r=\r,v=west)--(hex cs:c=0,r=\r,v=north west)}
%  %% Then for top of board
%  \foreach \c in {0,1,...,#1} {%
%    --(hex cs:r=#2,c=\c,v=north west)--(hex cs:c=\c,r=#2,v=north east)}
%  %% Then for right of board
%  \foreach \r in {#2,...,0} {%
%    --(hex cs:c=#1,r=\r,v=east)--(hex cs:c=#1,r=\r,v=south east)}
%  %% Then for bottom of board
%  --(hex cs:r=0,c=#1,v=south west) \foreach \c in \board@range {%
%    --(hex cs:r=0,c=\c,v=south east) --(hex cs:c=\c,r=0,v=south west) }
%  --cycle; }
%% New definition - much simpler 
\def\boardclip(#1)(#2)#3{%
  \boardpath(#1)(#2)
  \draw \ifx|#3|\else[preaction={#3}]\fi%
  [clip] \hexboardpath;
}
  
%    \end{macrocode}
% \end{Macro}
%    
% 
% \begin{Macro}{\debuggrid}
%  
%   Show a debug grid.  This requires \cs{boardframe}.
%
%    \begin{macrocode}
\def\debuggrid{%
  \foreach \i in {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1} {%
    \pgfmathparse{\i*\boardW+\boardXmin}%
    \edef\debug@x{\pgfmathresult}%
    \draw [very thin,gray](\debug@x,\boardYmin) --
    (\debug@x,\boardYmax) node [below,rotate=90] at
    (\debug@x,\boardYmin) {$\i$}; }%
  \foreach \i in {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1} {%
    \pgfmathparse{\i*\boardH+\boardYmin}%
    \edef\debug@x{\pgfmathresult}%
    \draw [very thin,gray] (\boardXmin,\debug@x) --
    (\boardXmax,\debug@x) node [left,rotate=90] at
    (\boardXmin,\debug@x) {$\i$}; } }
%    \end{macrocode}
% \end{Macro}
%
% Some dummy styles.  These will be defined by the export class to
% facilitate getting information from the board.
% 
%    \begin{macrocode}
\tikzset{%
  zoned/.style={},
  zone scope/.style={},
  zone path/.style={}
}
%    \end{macrocode}
% \iffalse
%</hex>
% --------------------------------------------------------------------
% \fi