\ProvidesPackage{stat}

\RequirePackage[double]{cards}

\pagestyle{empty}

\newcommand\card@type{Statistics Card}
\newcommand\@transfer{Non-Transferable}

%%%%%% With the values given here by default, a card can have up
%%%%%% to 9 stats without having to go to two columns, and up
%%%%%% to 18 stats without having to resize the card or lower cards/page.
%%%%%% (That's a lotta stats!)  Remember, you'd like these to
%%%%%% fit in badge holders....

% should fit in badge holder; actual width = this + twice \fboxsep
\newdimen\card@width   
\card@width=3.0in

% the inside of the card will be at least this high; if there are lots
% of stats, it will get bigger.  Again, the total vertical span of
% the card is this with \fboxsep on each side; keep it < badgeholder height.
\newdimen\min@height   
\min@height=2.0in

\fboxsep=10pt  % margin of everything from sides of card
\fboxrule=0pt  % frame is zero-width; cards.sty will put visible frame on

% this is a good thing to change to fit more stats (to \normalsize first)
\newcommand\stat@text@size{\large}

% fraction of the card@width to use for each stat column;
% keep it less than .5, so that there'll be inter-column space.
\newcommand\@frac{.45}

% inter-line spaces on the Death Report Form are stretched by this factor
\newcommand\death@stretch{1.5}


%%%%%%%%%%%%%% That's all the things you'd usually want to tweak.
%%%%%%%%%%%%%% You can do other customizations below if you want.



% contents of the death report
\newcommand\death@report{%
  \vbox{%
    \hsize=\card@width
    \centering
    \normalsize
    \lineskiplimit=-100in  % always use \baselineskip, not \lineskip
    {\bf Death Report \\ \me{}\\}%
    \renewcommand\baselinestretch{\death@stretch}%
    \large  % force font switch so \baselinestretch takes effect
    \normalsize
    When: \hrulefill \break
    Where: \hrulefill \break
    How: \hrulefill \break
    By Whom: \hrulefill \break
    Why (you think): \hrulefill \break
    \null \hrulefill \\[.5\baselineskip]%
  }%
}

%%% After this it gets hairier...

\newbox\main@box
\newbox\aux@box
\newbox\tmp@box

% note all heights ignore \fboxsep; that appears uniformly around everything
\newdimen\card@height  % larger of (stat height, min height); ignores death ht
\newdimen\short@name@width
\newdimen\tmp@dim

% show short name for current stat, with game.cls magic
\def\short@name{\bf\get@array{stat-short}{\the\stat@count}}

% find the max width of the short names for stats
\stat@count=0
\short@name@width=0pt
\loop \ifnum\stat@count<\stat@total@count
  \settowidth\tmp@dim{\short@name}
  \ifdim\tmp@dim>\short@name@width \short@name@width=\tmp@dim \fi
  \advance\stat@count by 1
\repeat

% puts entry in for current stat (uses game.cls magic)
\def\an@entry{%
  \vskip .6ex
    \hbox to \use@width{%
      \hbox to \short@name@width{\short@name}%
      \tmp@dim=\use@width  \advance\tmp@dim by -\short@name@width
      \baselineskip=2.2ex
      \lineskiplimit=-100in % always use \baselineskip, not \lineskip
      \vtop{\hsize=\tmp@dim \centering \get@stat}%
    }%
  \vskip .6ex
}

% build all the stats into a single vbox at current appropriate width
\def\all@in@one{%
  \vbox{%
    \stat@count=0%
    \loop \ifnum\stat@count<\stat@total@count
      \an@entry
      \advance\stat@count by 1%
    \repeat
  }%
}

\parindent=0pt

% game.cls has set up some stat magic, based on custom.sty calling \usestats.
% when \name is called we'll produce a statcard for \me.
\renewcommand\doname{%
  \stat@text@size
  \lineskip=0pt
  \lineskiplimit=100in % always use \lineskip, not \baselineskip

  % first we see if we can fit all the stats in a single card-wide
  % column without having to stretch the card height
  \let\use@width\card@width
  \setbox\main@box\all@in@one
  \setbox\aux@box\vbox{}%
  \let\min@card@height\min@height % min card ht is probably min stat ht
  % check if death ht pushes the min card ht up
  \setbox\tmp@box\death@report
  \tmp@dim=0pt
  \advance\tmp@dim by \ht\tmp@box
  \advance\tmp@dim by \dp\tmp@box
  \ifdim\tmp@dim>\min@height \let\min@card@height\tmp@dim \fi

  % now check to see if single-column version would stretch the height
  \tmp@dim=0pt
  \advance\tmp@dim by \ht\main@box
  \advance\tmp@dim by \dp\main@box
  \setbox\tmp@box\stat@header
  \advance\tmp@dim by \ht\tmp@box
  \advance\tmp@dim by \dp\tmp@box
  \setbox\tmp@box\stat@footer
  \advance\tmp@dim by \ht\tmp@box
  \advance\tmp@dim by \dp\tmp@box
  \ifdim\tmp@dim>\min@card@height
    % yes, it would stretch it, so break into two columns

    \def\use@width{\@frac\card@width}%

    % at that width, how tall is the total?
    \setbox\main@box\all@in@one
    \card@height=0pt
    \advance\card@height by \ht\main@box
    \advance\card@height by \dp\main@box

    % build the left and right columns of stats
    \stat@count=0%
    \setbox\main@box\vtop{}%
    \loop 
      \tmp@dim=0pt 
      \advance\tmp@dim by \ht\main@box 
      \advance\tmp@dim by \dp\main@box
    \ifdim\tmp@dim<.5\card@height
      \setbox\main@box\vtop{\box\main@box\an@entry}%
      \advance\stat@count by 1%
    \repeat
    \setbox\aux@box\vtop{}%
    \loop \ifnum\stat@count<\stat@total@count
      \setbox\aux@box\vtop{\box\aux@box\an@entry}%
      \advance\stat@count by 1%
    \repeat
    \setbox\aux@box\vtop{\box\aux@box\vfill}%

    \setbox\main@box\hbox to \card@width{\box\main@box\hfill\box\aux@box}%
  \fi

  % ok, \main@box is now the whole stat box, however many columns

  % figure out size of stat box
  \tmp@dim=0pt
  \advance\tmp@dim by \ht\main@box
  \advance\tmp@dim by \dp\main@box
  \setbox\tmp@box\stat@header
  \advance\tmp@dim by \ht\tmp@box
  \advance\tmp@dim by \dp\tmp@box
  \setbox\tmp@box\stat@footer
  \advance\tmp@dim by \ht\tmp@box
  \advance\tmp@dim by \dp\tmp@box
  % card height is whichever size is bigger, stat height or min height
  \card@height = \ifdim\tmp@dim>\min@height \tmp@dim \else \min@height \fi

  \makecard
    {\fbox{%
       \boxmaxdepth=0pt
       \vbox to \card@height{%
         \stat@header 
         \break \vfil
         \box\main@box
         \break \vfil \vfil
         \stat@footer
       }%
    }}%
    {\death@report}%
}

\newcommand\stat@header{\hbox to \card@width{\small \me{}\hfill \card@type}}
\newcommand\stat@footer{\hbox to \card@width{\small \gamename\hfill\@transfer}}
