summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/wargame/source/util/export.dtx
blob: fde067dfe8313b08c94556e29c249e469da4bc3c (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
% \iffalse
% ====================================================================
% \fi
% 
% \subsection{The \texttt{wgexport} class}
% \label{sec:impl:util}
%
% This document class is used for exporting game component to be used
% in a VASSAL module
% libraries.
% 
% \iffalse
%<*exportcls>
%\fi
%
% Class identification and load \texttt{wargame} package
% 
%    \begin{macrocode}
\ProvidesClass{wgexport}
\PassOptionsToClass{multi=tikzpicture,varwidth=false}{standalone}
\DeclareOption{noterrainpic}{%
  \PassOptionsToPackage{\CurrentOption}{wargame}}
\DeclareOption{terrainpic}{%
  \PassOptionsToPackage{\CurrentOption}{wargame}}
\DeclareOption*{%
  \PassOptionsToClass{\CurrentOption}{standalone}}
\ProcessOptions\relax
\LoadClass{standalone}
\RequirePackage{wargame}
%    \end{macrocode}
%
% We need a few utilities before we get to the actual environment.
% First, we need a tools to write out literal left and right curly
% braces.  We do a bit of catcode hackery to accomplish that. 
%
%    \begin{macrocode}
\begingroup
\catcode`\^^I=12
\def\@tabchar{^^I}
\catcode`<=1 \catcode`>=2
\catcode`{=12 \catcode`}=12
\gdef\@lbchar<{>
\gdef\@rbchar<}>
\endgroup
%    \end{macrocode}
%
% Above, we temporarily set the tab, and left and right curly brace
% characters to be regular letters (12), and the catcodes of less than
% and greater than to be those of left and right curly braces
% respectively.  We then define the macros \cs{@tabchar},
% \cs{@lbchar}, and \cs{@rbchar} to produce literal characters.
% \LaTeX already has \cs{@percentchar}. 
%
% Everything we do should go inside this environment.  The single
% optional argument is the file name stem of the output JSON file.
% 
%    \begin{macrocode}
\newenvironment{imagelist}[1][\jobname]{%
  \newwrite\mk@out%
  \def\mk@i{}%
  \def\mk@w{\immediate\write\mk@out}%
  \immediate\openout\mk@out=#1.json
  \mk@w{[}
}{
  \mk@w{\mk@i \@lbchar "name":"End of list", "category": "<<eol>>",
      "subcategory": "" \@rbchar }
  \mk@w{]}
  \immediate\closeout\mk@out
}
%    \end{macrocode}
% 
%
% Preceed all images (\textsf{tikzpicture}) with this command
%
% First argument is the name of the image.  This can be anything.
% Note that for counters, if the name ends in \texttt{flipped} then it
% is considered the backside of a counter.
%
% Second argument is the type of image. Recognised types are
%
% \begin{itemize}
% \item \texttt{board}   for boards
% \item \texttt{oob}     for OOBs
% \item \texttt{chart}   for charts
% \item \texttt{counter} for counters
% \item \texttt{front}   for front page
% \end{itemize}
%
% Other types can be used, and the images will be exported, but the
% Python script pays no particular attention to those then.  Use for
% example to prepare images for help or the like.
%
% The third argument is the sub type.  This is most relevant for the
% counters.  Sub types can be anything, but since the counters will
% receive different prototypes based on the sub type, it makes sense
% to divide into sub types a la
%
% \begin{itemize}
% \item factions
% \item common markers
% \end{itemize}
% 
% The faction sub types should just be the name of the faction.
% E.g., Allies, Axis, Soviet, NATO, Warsaw Pact.  Spaces should not
% matter.
%
% For common markers, there are a few names that are recognised
% specifically by the Python script.  These are
%
% \begin{itemize}
% \item \texttt{common}
% \item \texttt{all}
% \item \texttt{marker}
% \item \texttt{markers}
% \end{itemize}
% 
% Counters that has these sub-types will no be considered to belong
% to any faction.
%
% Note that the Python script uses the faction names to guess the
% players of the game, and uses them in several places.
%
% 
%    \begin{macrocode}
\def\info{%
  \@ifstar{\@@info{,}}{\@@info{\@rbchar,}}}
\def\@@info#1#2#3#4{%
  \chit@dbg{2}{Making image `#2' of type `#3'/`#4' on page \thepage}%
  \mk@w{ \@lbchar}%
  \mk@w{ \space "name": "#2",}%
  \mk@w{ \space "category": "#3",}%
  \mk@w{ \space "subcategory": "#4", }%
  \mk@w{ \space "number": \thepage #1}%
  \let\oldmk@i\mk@i%
  \ifx#1,\relax\edef\mk@i{\mk@i\space\space}\fi}
\def\end@info{%
  \let\mk@i\oldmk@i%
  \mk@w{ \space \@rbchar,}}
%    \end{macrocode}
% 
% Make separate images for each counter (single sided).
% 
%    \begin{macrocode}
\newcommand\chitimages[2][]{%
  \begingroup%
  \let\chit@report\do@chit@report%
  \let\natoapp@report\do@natoapp@report%
  \chit@dbg{2}{chits to make images of `#2'}%
  \foreach[count=\ti from 0] \t/\x in #2{%
    \chit@dbg{2}{^^JRow: `\t' (`\x')}
    \ifx\t\empty\else% Ignore empty rows
      \chit@dbg{5}{^^JSubcategory: `\x' (default `#1')}
      % Take sub-category or default
      \ifx\t\x\def\x{#1}\else\ifx\x\empty\def\x{#1}\fi\fi
      \foreach \u/\m in \t{% 
        \ifx\u\empty\else% Ignore empty cells 
          \ifx\u\chit@blank\else%
            \chit@dbg{2}{Next chit `\u' with possible multiplicity `\m'}%
            \ifx\m\@empty\def\m{1}\fi% If not multiplicity defined 
            \ifx\u\m\def\m{1}\fi% If the same as unit
            \chit@dbg{2}{Next chit `\u' multiplicity `\m'}%
            %% We only make one copy of the chit, since we can duplicate
            %% it in VASSAL
            \info*{\u}{counter}{\x}
            \begin{tikzpicture}
              \chit[\u=\ti]%
            \end{tikzpicture}
            \end@info%
            %% \foreach \n in {1,...,\m}{% Make a number of copies 
            %%   \ifx\u\chit@blank%
            %%     \chit@dbg{3}{Ignoring blank chit:\u}%
            %%   \else%
            %%     \info{\u}{counter}{#2}
            %%     \begin{tikzpicture}
            %%       \chit[\u=\ti](\c,\r)%
            %%     \end{tikzpicture}
            %%   \fi%
            %% }%
          \fi%
        \fi%
      }%
      \chit@dbg{2}{End of inner loop}%  
    \fi%
  }%
  \chit@dbg{2}{End of outer loop}%
  \endgroup%
}
%    \end{macrocode}
% 
% Make separate images for each counter (double sided).  The back-side
% counters must be defined by append `\texttt{ flipped}' the front
% face name
% 
%    \begin{macrocode}
\newcommand\doublechitimages[2][]{%
  \begingroup%
  \let\chit@report\do@chit@report%
  \let\natoapp@report\do@natoapp@report%
  \foreach[count=\ti from 0] \t/\x in #2{%
    \ifx\t\empty\else% Ignore empty rows
      \chit@dbg{5}{^^JSubcategory: `\x' (default `#1')}
      % Take sub-category or default
      \ifx\t\x\def\x{#1}\else\ifx\x\empty\def\x{#1}\fi\fi
      \foreach \u/\m in \t{% 
        \ifx\u\empty\else% Ignore empty cells 
          \ifx\u\chit@blank\else%
            \chit@dbg{2}{Next chit `\u' with possible multiplicity `\m'}%
            \ifx\m\@empty\def\m{1}\fi% If not multiplicity defined 
            \ifx\u\m\def\m{1}\fi% If the same as unit
            \chit@dbg{2}{Next chit `\u' multiplicity `\m'}%
            %% Flipped chit
            \edef\s{\u\space flipped}%
            %% We only make one copy of the chit, since we can duplicate
            %% it in VASSAL
            \info*{\u}{counter}{\x}%
            \begin{tikzpicture}%
              \chit[\u=\ti]%
            \end{tikzpicture}%
            \end@info%
            \info*{\s}{counter}{\x}%
            \begin{tikzpicture}%
              \chit[\s=\ti]%
            \end{tikzpicture}%
            \end@info%
            %% \foreach \n in {1,...,\m}{% Make a number of copies 
            %%   \ifx\u\chit@blank%
            %%     \chit@dbg{3}{Ignoring blank chit:\u}%
            %%   \else%
            %%     \info{\u}{counter}{#2}
            %%     \begin{tikzpicture}
            %%       \chit[\u=\ti](\c,\r)%
            %%     \end{tikzpicture}
            %%   \fi%
            %% }%
          \fi%
        \fi%
      }%
    \fi%
  }%
  \endgroup%
}
%    \end{macrocode}
%
% Special for boards, we have the environment \textsf{boardimage}.
% Like \cs{info} we must specify the name and sub-category of the
% board, but the category is assumed to be \texttt{board} (though the
% optional argument can specify a different category).
%
% Within this environment some specific styles are defined that allows
% the user to specify VASSAL zones on the board.  For this to work
% properly, the parent \textsf{tikzpicture} \emph{must} have the style
% \texttt{zoned}.  This style will record the bounding box of the
% picture which we will need to calculate VASSAL coordinates later
% on.
%
% Other styles are \texttt{zone scope}, to be applied to
% \texttt{scope}s in the picture, and \texttt{zone path} to be applied
% to \texttt{path}s (or \cs{draw}, \cs{fill}, or the like) in the
% picture.  These will record coordinates of these elements in side
% the picture.  The Python script will then define VASSAL zones based
% on these coordinates.
%
% For \texttt{zone scope} applied to a \texttt{scope}, what is
% recorded are
%
% \begin{itemize}
% \item The current coordinate transformation matrix
% \item The current translation
% \item The bounding box, within the current transformation and
%   translation. 
% \end{itemize}
%
% To define a zone in the board, simply enclose it in a
%
% \begin{verbatim}
% \begin{scope}[zone scope=name]
%   ...
% \end{scope}
% \end{verbatim}
%
% The \meta{name} will be the name of the scope.  If this contains the
% sub-string \texttt{hex} (upper, lower, or mixed case), then the zone
% will get a hex grid with numbering attached to it.
%
% If the \meta{name} contains the sub-string \texttt{turn} (any case),
% then it is assumed to be a turn track and a rectangular grid will be
% attached.  The column and row separator will be set to \texttt{T},
% so that it won't collide with the main zone.  Similar if \meta{name}
% contains \texttt{oob}, except the separator is set to \texttt{O}. 
%
% If \meta{name} contains the sub-string \texttt{pool}, then it is
% assumed to be a pool of counters, and \emph{no} grid is attached. 
%
% For \texttt{zone path} applied to a \texttt{path}, what is recorded
% is the path coordinates (as straight line segments) in the global
% coordinate system.
%
% Both styles take one argument --- the name of the zone.  If that
% name contains the sub-string \texttt{hex} anywhere in the name, then
% the zone is assumed to contain a hex grid.  Otherwise, a rectangular
% grid (of fixed size) will be applied to it.
%
% The environment \texttt{boardimage} also records the coordinate
% options currently in use (keys \texttt{hex/first row is},
% \texttt{hex/row direction is}, and so on), as well as the current
% label option (as defined by \texttt{every hex} or \texttt{every hex
% node}).
%
%
% The information extracted is written to the
% \cs{jobname}\texttt{.json} file as a sub-object (with name given by
% the first optional argument) of the image object.  In that way, we
% can later on easily get the information from our catalogue of
% images. 
%
% Note, the styles \texttt{zoned}, \texttt{zone scope}, and
% \texttt{zone path} are defined in \texttt{wargame} to be dummies so
% that one can have them in the definition of the board without
% impact. 
%    \begin{macrocode}
\def\mk@transform{%
  \pgfgettransformentries{\mxx}{\mxy}{\myx}{\myy}{\ptdx}{\ptdy}
  \wg@pt@to@cm{\ptdx}\edef\dx{\pgfmathresult}
  \wg@pt@to@cm{\ptdy}\edef\dy{\pgfmathresult}
  \mk@w{ \mk@i "xx": \mxx,}
  \mk@w{ \mk@i "xy": \mxy,}
  \mk@w{ \mk@i "yx": \myx,}
  \mk@w{ \mk@i "yy": \myy,}
  \mk@w{ \mk@i "dx": \dx,}
  \mk@w{ \mk@i "dy": \dy,}
}
%    \end{macrocode}
%    \begin{macrocode}
\def\mk@bb#1{%
  \wg@get@bb{#1}
  \mk@w{ \mk@i "lower left":  [\llx,\lly],}
  \mk@w{ \mk@i "upper right": [\urx,\ury],}
  \begingroup  
   \wg@get@global@nchor{#1}{south west}
   \mk@w{ \mk@i "global lower left":  [\tmp@x,\tmp@y],}
   \wg@get@global@nchor{#1}{north east}
   \mk@w{ \mk@i "global upper right": [\tmp@x,\tmp@y]}
 \endgroup
}
\def\mk@pos#1(#2){%
  \wg@dbg{10}{^^JMarking `#2' with `#1' - start}
  \coordinate[transform shape] (tmp) at (#2) {};
  \wg@get@nchor{tmp}{center}
  \wg@dbg{3}{^^JMarking `#2' with `#1' - `\tmp@x',\tmp@y'}
  \tikzset{zone point={#1}{\tmp@x}{\tmp@y}}
}
%    \end{macrocode}
%
% For the key \texttt{zone path} to work, we need to be able to record
% the path as it moves along.  To that end, we make a custom
% decoration that will do that for us, and, once the path is finished,
% write the path to our JSON file. 
%
%    \begin{macrocode}
\pgfdeclaredecoration{record path construction}{initial}{%
  \state{initial}[width=0pt,next state=more]{
    \begingroup
      \pgf@decorate@inputsegment@first
      \ptpoint@to@cm{\the\pgf@x}{\the\pgf@y}
      \xdef\wg@path{[\x,\y]}
    \endgroup
  }%
  \state{more}[width=\pgfdecoratedinputsegmentremainingdistance]{%
    \begingroup
      \pgf@decorate@inputsegment@last
      \ptpoint@to@cm{\the\pgf@x}{\the\pgf@y}
      \xdef\wg@path{\wg@path,[\x,\y]}
    \endgroup
  }
  \state{final}{%
    \begingroup
      \pgf@decorate@inputsegment@last
      \ptpoint@to@cm{\the\pgf@x}{\the\pgf@y}
      \xdef\wg@path{\wg@path,[\x,\y]}
    \endgroup
    \mk@w{ \mk@i "zone path \wg@record@path@name": \@lbchar}
    \mk@w{ \mk@i\space "path": [\wg@path] \@rbchar,}
  }
}%
%    \end{macrocode}
%    
% Now we can make our environment
%
% The first thing we do is to use the \cs{info} macro to mark the
% image. Then we open our JSON file. We make a short-hand macro for
% writing to that file.  The macro \cs{bd@i} records the current
% indention (which is important in JSON)
% 
%    \begin{macrocode}
\newenvironment{boardimage}[3][board]{%
  \def\bd@n{#2}
  \newcount\mk@point
  \mk@point=0
  \let\oomk@i\mk@i%
  \let\markpos\mk@pos%
%    \end{macrocode}
%
%
% Then, to extract the label option, we make a dummy \texttt{node}
% with the styles \texttt{every hex} and \texttt{every hex node}, so
% we can extract that option. 
%
%    \begin{macrocode}
  \info{dummy}{<<dummy>>}{}%
  %\tikz{}%
  \tikz{\scoped[%
    every hex/.try,every hex node/.try,
    ]{%
      \def\hex@col{0}%
      \def\hex@row{0}%
      \node[hex,inner sep=0,outer sep=0]{%
        \message{^^JHex label: `\meaning\hex@label'}%
        \global\let\mk@label\hex@label}}}%
%    \end{macrocode}
%
% The next thing we do is to make an object.  The first things we put
% in are the units used (``cm''), and the grid options.
%
%    \begin{macrocode}
  \info*{#2}{#1}{#3}%
  \mk@w{ \mk@i "zones": \@lbchar}%
  \edef\mk@i{\mk@i\space}
  %% Everything is made into centimeters
  \mk@w{ \mk@i "units": "cm",}
  \hex@dbg{0}{Label: `\meaning\mk@label'}
  \@ifundefined{mk@label}{}{\mk@w{ \mk@i "labels": "\mk@label",}}
  %% Write out coordinate options as "coords" object
  \mk@w{ \mk@i"coords": \@lbchar}%
  \mk@w{ \mk@i "row": \@lbchar}%
  \mk@w{ \mk@i\space "offset": \hex@coords@row@off,}%
  \mk@w{ \mk@i\space "factor": \hex@coords@row@fac \@rbchar,}%
  \mk@w{ \mk@i "column": \@lbchar}%
  \mk@w{ \mk@i\space "offset": \hex@coords@col@off,}%
  \mk@w{ \mk@i\space "factor": \hex@coords@col@fac,}%
  \mk@w{ \mk@i\space "top short": "\hex@top@short@col",}%
  \mk@w{ \mk@i\space "bottom short": "\hex@bot@short@col" \@rbchar}%
  \mk@w{ \mk@i\@rbchar,}%
%    \end{macrocode}
%
% We then monkey-patch \cs{boardframe} to also output coordinates to
% our JSON file.  Note that this will probably be embedded in a
% different object. 
%
%    \begin{macrocode}
  %%
  \let\oldbo@rdframe\bo@rdframe%
  \def\bo@rdframe[##1](##2)(##3){%
    \oldbo@rdframe[##1](##2)(##3)%
    \mk@w{ \mk@i"board frame": \@lbchar}
    \mk@w{ \mk@i\space "lower left": [\llx,\lly],}
    \mk@w{ \mk@i\space "upper right": [\urx,\ury],}
    \mk@w{ \mk@i\space "margin": \margin,}
    \mk@w{ \mk@i\space "width": \w,}
    \mk@w{ \mk@i\space "height": \h \@rbchar,}}%
%    \end{macrocode}
%
% Next, we make the style \texttt{zoned} to be applied to the
% \texttt{tikzpicture} environment.  This records the bounding box of
% the full picture. 
%
%    \begin{macrocode}
  \tikzset{
    zoned/.code={% Apply to whole picture
      \pgfkeys{%
        % This needs to be done in the picture!
        /tikz/execute at end picture={%
          \mk@w{ \mk@i "zoned": \@lbchar}
          \mk@transform%
          \mk@bb{current bounding box}
          \mk@w{ \mk@i \@rbchar,}
        }
      }
    },
%    \end{macrocode}
%
% The next style is the \texttt{zone scope}.  At the start of the
% scope we record the current transformation matrix.  Then we install
% a handler to extract the bounding box at the end of the scope.  Note
% that we increase indention here. 
%
%    \begin{macrocode}
    zone scope/.code={%
      \mk@w{ \mk@i"zone scope ##1": \@lbchar}
      \let\omk@i\mk@i
      \edef\mk@i{\mk@i\space}
      \mk@transform%
      %\bd@w{ \@rbchar,}
      \gdef\wg@export@box{##1}%
      \pgfkeys{%
        /tikz/local bounding box=wg export box,
        /tikz/execute at end scope={
          \mk@bb{wg export box}
          \let\mk@i\omk@i
          \mk@w{ \mk@i\@rbchar,}},
      } % pgfkeys
    }, % zone scope
%    \end{macrocode}
% The next style gets the global coordinates of the current (0,0)
% point - f.ex. in a node - and outputs that 
%    \begin{macrocode}
    zone point/.code n args={3}{
      \pgf@xa=##2 cm
      \pgf@ya=##3 cm
      \pgfpointtransformed{\pgfpoint{\pgf@xa}{\pgf@ya}}
      % \pgfpointtransformed{\pgfpoint{0pt}{0pt}}
      \pgf@xa=\pgf@x
      \pgf@ya=\pgf@y
      \wg@pt@to@cm{\the\pgf@xa}\edef\px{\pgfmathresult}
      \wg@pt@to@cm{\the\pgf@ya}\edef\py{\pgfmathresult}
      \advance\mk@point1
      \global\mk@point=\mk@point
      \mk@w{ \mk@i "point\the\mk@point": \@lbchar "name": "##1", "type": "point", "coords":  [\px,\py]
        \@rbchar, }
      %\message{^^JZone point \the\mk@point\space ##1: ##2,##3 -> \px,\py}
    },
    zone oob point/.code n args={3}{
      \pgf@xa=##2 cm
      \pgf@ya=##3 cm
      \advance\pgf@xa.1cm
      \advance\pgf@ya.1cm
      \pgfpointtransformed{\pgfpoint{\pgf@xa}{\pgf@ya}}
      % \pgfpointtransformed{\pgfpoint{0pt}{0pt}}
      \pgf@xa=\pgf@x
      \pgf@ya=\pgf@y
      \wg@pt@to@cm{\the\pgf@xa}\edef\px{\pgfmathresult}
      \wg@pt@to@cm{\the\pgf@ya}\edef\py{\pgfmathresult}
      \advance\mk@point1
      \global\mk@point=\mk@point
      \mk@w{ \mk@i "point\the\mk@point": \@lbchar "name": "##1",
        "parent": "\wg@export@box", "type": "point", "coords":  [\px,\py]
        \@rbchar, }
      %\message{^^JZone point \the\mk@point\space ##1: ##2,##3 -> \px,\py}
    },
    zone global point/.code n args={3}{
      \advance\mk@point1
      \global\mk@point=\mk@point
      \mk@w{ \mk@i "point\the\mk@point": \@lbchar "name": "##1", "type": "point", "coords":  [\px,\py]
        \@rbchar, }
    },      
%    \end{macrocode}
%
% The \texttt{zone path} style is a bit more simple, but only because
% the bulk of the work is done in a decoration.  We need to be able to
% pass a name to that decoration, s we make a key for that.  The user
% need not think about that though. 
%
%    \begin{macrocode}
    /pgf/decoration/record path name/.store in=\wg@record@path@name,
    zone path/.style={%
      postaction={decorate,decoration={
          record path construction,
          record path name=##1}}
    } % zone path
  }% tikzset
}
%    \end{macrocode}
%
% That finishes the first part of the environment.  At the end of the
% environment, we simple write the name of the picture, and close our
% JSON output. 
%
%    \begin{macrocode}
{%
  \mk@w{ \mk@i "name": "\bd@n" }%
  \let\mk@i\oomk@i%
  \mk@w{ \mk@i\@rbchar}%
  \end@info%
}
%    \end{macrocode}
%
% Make battle markers.  Mandatory argument is how many markers,
% optional is the group to add the markers to.
% 
%    \begin{macrocode}
\def\wg@gennumberm@rkers#1#2#3{
  \message{^^JNumbered markers: Type=`#1' Max=`#2' Category=`#3'}
  \def\markers{}
  \def\keys{}
  \foreach \i in {1,...,#2}{%
    \xdef\keys{/tikz/#1 \i/.style={/tikz/#1=\i},\keys}
    \xdef\markers{\markers,#1 \i}}
  {%
    \nopagecolor\pgfkeysalsofrom{\keys}\chitimages[#3]{\markers}}}%
\tikzset{
  wg hidden unit/.pic={},
  wg hidden unit/.style={
    chit={
      frame={draw=none,fill=none},
      full=wg hidden unit}}}
\DeclareRobustCommand\battlemarkers[2][BattleMarkers]{%
  \wg@gennumberm@rkers{battle marker}{#2}{#1}%
  \message{^^JMake a hidden unit and add to Markers category}
  {%
    \nopagecolor%
    \chitimages[Markers]{{wg hidden unit}}%
    %
    \info{battle-marker-icon}{icon}{}%
    \tikz[scale=.7,transform shape]{\pic{battle marker=0};}%
    \info{clear-battles-icon}{icon}{}
    \tikz[scale=.4,transform shape]{%
      \pic{eliminate icon};
      \pic[scale=.7,transform shape] at (-.3,0) {battle marker=0};}%
  }%
}
%    \end{macrocode}
% 
% Make odds markers.  Mandatory argument is a list of odds and fill colours.
% Optional is the group to add the markers to.
% 
%    \begin{macrocode}
\def\wg@gencolorm@rkers#1#2#3{%
  \def\markers{}
  \def\keys{}
  \foreach \o/\f in {#2}{%
    \ifx\o\f\def\f{white}\fi%
    \message{^^JOdds marker `#1 \o' w/fill `\f'}%
    \xdef\keys{/tikz/#1 \o/.style={/tikz/#1={\o,\f}},\keys}
    \xdef\markers{\markers,#1 \o}}
  {\nopagecolor\pgfkeysalsofrom{\keys}\chitimages[#3]{\markers}}}%
\DeclareRobustCommand\oddsmarkers[2][OddsMarkers]{%
  \wg@gencolorm@rkers{odds marker}{#2}{#1}%
  \info{odds-battles-icon}{icon}{}
  \tikz[scale=.5,transform shape]{\pic{odds marker={?:?,white}}}
  \info{resolve-battles-icon}{icon}{}
  \tikz[scale=.3,transform shape]{%
    \pic{dice};
    \pic[scale=1.2,transform shape] at (-.2,-.2) {battle marker=0};}%
}
%    \end{macrocode}
% 
% Make results markers.  Mandatory argument is a list of results and
% fill colours.  Optional is the group to add the markers to.
% 
%    \begin{macrocode}
\DeclareRobustCommand\resultmarkers[2][ResultMarkers]{%
  \wg@gencolorm@rkers{result marker}{#2}{#1}}
%    \end{macrocode}
% 
% Common icons used by many modules
% 
%    \begin{macrocode}
\DeclareRobustCommand\commonicons[2]{%
  \begingroup%
  \nopagecolor%
  \tikzset{icon/.style={scale=.4,transform shape}}%
  %
  \info{pool-icon}{icon}{}
  \tikz[icon]{\pic{pool icon};}
  % 
  \info{oob-icon}{icon}{}%
  \tikz[icon]{\pic{oob icon={#1}{#2}};}%
  %
  \info{flip-icon}{icon}{}%
  \tikz[icon]{\pic{flip icon};}%
  %
  \info{eliminate-icon}{icon}{}%
  \tikz[icon]{\pic{eliminate icon};}%
  %
  \info{restore-icon}{icon}{}%
  \tikz[icon]{\pic{restore icon};}%
  %
  \info{dice-icon}{icon}{}%
  \tikz[icon,scale=.9]{\pic{dice};}%  
  %
  \info{unit-icon}{icon}{}%
  \tikz[icon,scale=.7]{%
    \chit[fill=#1,
      symbol={[
        scale line widths,
        line width=1pt,
        faction=friend,
        command=land,
        main=infantry,
        scale=1.3](0,-.15)}]}%
    \endgroup%
}
%    \end{macrocode}
%    
% \subsubsection{Making dice}
% \begin{Syntax}
%   \cs{dice}\oarg{tikz-options}\oarg{node-options}\marg{name}\marg{name}\marg{list}
% \end{Syntax}
% \begin{enumerate}
% \item \meta{tikz-options}
% \item \meta{node-options}
% \item \meta{name} - an identifier - e.g., the same as \meta{shape}.
% \item \meta{shape} - one of \texttt{d4}, \texttt{d6}, \texttt{d8},
%   \texttt{d10}, \texttt{d12}, or \texttt{d20}.
% \item \meta{list} - list of pairs
%   \meta{value}\texttt{/}\meta{printed}, where \meta{value} is the
%   value, and \meta{printed} is the shown value. If \meta{printed} is
%   left out, then \meta{value} is used.
% \end{enumerate}
% 
%    \begin{macrocode}
\def\dice{%
  \@ifnextchar[{\wg@dice}{\wg@dice[]}%]
}
\def\wg@dice[#1]{%
  \@ifnextchar[{\wg@@dice{#1}}{\wg@@dice{#1}[]} %]
}
\def\wg@@dice#1[#2]#3#4#5{%
  \foreach \v/\p in {#5}{%
    \info{#3-\v}{die-roll}{#3}
    \tikz[#1]{
      \node[shape=#4,transform shape,draw=none,fill=black,opacity=.5]
      at (.05,-.03){};
      \node[shape=#4,#2,transform shape]{\p};}}}
%    \end{macrocode}
% 
% \subsubsection{Hooks into chits, etc.}
% 
% TO BE DONE: We could add hook the \texttt{hex} shape that would
% allow us to write out the settings for each of these.  This would
% allow us to make data files that contain the information available
% in the \LaTeX{} code.  
%
% If one then assumed that for example the upper left corner holds the
% start-up hex, then one could use that information.
% 
% The code below exports the chit information to the JSON file.
% Together with the battle, odds, and result markers stuff above, this
% allows the exporter to almost automatically set up battle odds and
% result calculations.   The fields exported are 
%
% \begin{itemize}
% \item Left and right identifiers
% \item Upper left, upper right, lower left, and lower right
%   identifiers. (some care must be taken if these contains graphics
%   and not just text.)
% \item Factors
% \item NATO symbol
%   \begin{itemize}
%   \item Faction, command, echelon
%   \item Mains
%   \item Left, right, top, and bottom attributes and modifiers
%   \item Below attribute 
%   \end{itemize}
% \end{itemize}
%
% The exporter can set up prototypes for NATO types, echelons,
% etc. The exporter can also set factors as marks on the units. 
%
%    \begin{macrocode}
\tikzset{
  zone turn/.store in=\zone@turn,
  zone mult/.store in=\zone@mult
}
\def\@chit@rep@line#1#2{%
  \@ifundefined{#2}{}{
    \edef\wg@chit@tmp{\csname #2\endcsname}
    {\escapechar=`/
      \xdef\tmp{\detokenize\expandafter{\wg@chit@tmp} \@empty}}
    % \message{^^J\meaning\@tmp -> \meaning\tmp}
    \mk@w{ \mk@i\space "#1": "\tmp",}}}
  
\def\do@chit@report{%
  \chit@dbg{3}{Start of Chit Report}
  \mk@w{ \mk@i "chit": \@lbchar}
  \chit@dbg{3}{Report - ID}
  \@ifundefined{id}{}         {\mk@w{ \mk@i\space "id":      "\id", }}%
  \chit@dbg{3}{Report - Symbol: `\meaning\chit@symbol'}
  \@ifundefined{chit@symbol}{}{\mk@w{ \mk@i\space "symbol":  "true", }}%
  \chit@dbg{3}{Report - Full: `\meaning\chit@full'}
  \@chit@rep@line{full}{chit@full}
  \chit@dbg{3}{Report - Factors: `\meaning\chit@factors'}
  \@chit@rep@line{factors}{chit@factors}%
  \chit@dbg{3}{Report - Left: `\meaning\chit@left'}
  \@chit@rep@line{left}{chit@left}%
  \chit@dbg{3}{Report - Right: : `\meaning\chit@right'}
  \@chit@rep@line{right}{chit@right}%
  \chit@dbg{3}{Report - Upper left: `\meaning\chit@upper@left'}
  \@chit@rep@line{upper left}{chit@upper@left}%
  \chit@dbg{3}{Report - Lower left: `\meaning\chit@lower@left'}
  \@chit@rep@line{lower left}{chit@lower@left}%
  \chit@dbg{3}{Report - Upper right: `\meaning\chit@upper@right}
  \@chit@rep@line{upper right}{chit@upper@right}%
  \chit@dbg{3}{Report - Lower right: `\meaning\chit@lower@right'}
  \@chit@rep@line{lower right}{chit@lower@right}%
  \chit@dbg{3}{Report - End comma}
  \mk@w{ \mk@i\space "end": 0}
  \@ifundefined{chit@symbol}{
    \mk@w{ \mk@i \@rbchar }
  }{
    \mk@w{ \mk@i \@rbchar, }% NATOAPP6c will follow
  }%
  \chit@dbg{3}{End of Chit Report}
}
%    \end{macrocode}
% Report out NATO App6 symbol settings
%    \begin{macrocode}
\def\do@natoapp@report{%
  \mk@w{ \mk@i "natoapp6c": \@lbchar}
  \@chit@rep@line{id}{\id}
  \@chit@rep@line{faction}{natoapp@fac}
  \@chit@rep@line{command}{natoapp@cmd}
  \@chit@rep@line{echelon}{natoapp@ech}
  \@chit@rep@line{main}{natoapp@main}
  \@chit@rep@line{left}{natoapp@left}
  \@chit@rep@line{right}{natoapp@right}
  \@chit@rep@line{upper}{natoapp@upper}
  \@chit@rep@line{lower}{natoapp@lower}
  \@chit@rep@line{below}{natoapp@below}
  \mk@w{ \mk@i\space "end": 0}
  \mk@w{ \mk@i \@rbchar}
}
%    \end{macrocode}
%
%
% \iffalse
%</exportcls>
%\fi