\ProvidesPackage{money}

\RequirePackage{extractable}
\RequirePackage[mirror]{cards}

% Put the singular and plural forms of your currency here to go on bills.
% You'll also want to change the def of \cash in character.sty to
% put on the appropriate prefix if it's not $ (the prefix at start).
\newcommand\bill@one{Dollar}
\newcommand\bill@many{Dollars}

% Set the specific bills available here.  Format is
%% \def\usemoney{
%%	{1}{One}{\one}
%%	{5}{Five}{\five}
%%	{10}{Ten}{\ten}
%% }
% giving {value}{printed name}{macro}.
% The macro is only useful when using this style, of course;
% it's only really expected to be used in LaTeX/Central/misc-money.tex
% to print out pages of bills for general use.  For characters
% you'll want to use \cash in their charsheet to do extractable money.
%
% The values must be in ascending order.
\def\usemoney{
  % The top had better be 1 or \cash may not be able to make all combos!
  {1}{One}{\one}
  {5}{Five}{\five}
  {10}{Ten}{\ten}
  {20}{Twenty}{\twenty}
  {50}{Fifty}{\fifty}
  {100}{One Hundred}{\hundred}
  {500}{Five Hundred}{\fivehundred}
  {1000}{One Thousand}{\thousand}
}


% now we'll set up to make the bills as nice cards

\newlength\m@neyheight 
\m@neyheight=1.1in  % actual height is this plus twice \fboxsep
\fboxsep=5pt  % margin from frame
\fboxrule=0pt % leave visible frame to cards.sty
\parindent=0pt

\newbox\GameNameB@x \setbox\GameNameB@x=\hbox{\small\gamename}
\newbox\GameDateB@x \setbox\GameDateB@x=\hbox{\small\gamedate}
\newlength\Pic@Ht
\Pic@Ht=.95\m@neyheight
\advance\Pic@Ht by -\ht\GameNameB@x  \advance\Pic@Ht by -\dp\GameNameB@x
\advance\Pic@Ht by -\ht\GameDateB@x  \advance\Pic@Ht by -\dp\GameDateB@x
\newbox\PicB@x
\setbox\PicB@x=\hbox{\includegraphics[height=\Pic@Ht]{icon.eps}}

\newbox\sidebox
\newcommand\m@ney[2]{%
  \centering % center all text
  \setbox\sidebox=\vbox to \m@neyheight{\hsize=1in 
    {\Huge#1}\break\vfil{\Large#2\break\ifnum#1=1\bill@one\else\bill@many\fi}}%
  % *after* \sidebox is set with normal baseline spacing, kill line spacing:
  \lineskiplimit=10in  \lineskip=0pt
  \makecard{%
    \fbox{%
      \copy\sidebox
      \vbox to \m@neyheight{\hsize=1.6in
        \unhcopy\GameNameB@x \vfill \unhcopy\PicB@x \vfill \unhcopy\GameDateB@x
      }%
      \copy\sidebox
    }%
  }%
}


% now we'll go through the magic to define all the individual bill
% macros in case misc-money.tex wants to use them, and more importantly
% set up for \cash to be able to do its making-change calculations

\def\end@money{\end@money}

\newcount\n@cash
\newcount\m@ney@amount

\def\do@money#1#2#3{
  \def\tmp{#1}%
  \ifx\tmp\end@money
    \let\@NEXT\relax
  \else
    \def#3{\m@ney{#1}{#2}}
    \ifnum\n@cash>0 \advance\m@ney@amount by #1 \advance\m@ney@amount by #1 \fi
    \advance\n@cash by 1
    \expandafter\edef\csname MONEYLEVEL\the\n@cash\endcsname{\the\m@ney@amount}
    \expandafter\def\csname MONEYMACRO\the\n@cash\endcsname{#3}
    \expandafter\def\csname MONEYVALUE\the\n@cash\endcsname{#1}
    \advance\m@ney@amount by -#1
    \let\@NEXT\do@money
  \fi
  \@NEXT
}

\n@cash=0
\expandafter\do@money\usemoney\end@money\end@money\end@money
\edef\m@ney@max@n{\the\n@cash}

\newdef\cash[1]{
  \m@ney@amount=#1
  \n@cash=\m@ney@max@n
  \loop \ifnum\m@ney@amount>0
    \ifnum\m@ney@amount>\csname MONEYLEVEL\the\n@cash\endcsname
      \csname MONEYMACRO\the\n@cash\endcsname
      \advance\m@ney@amount by -\csname MONEYVALUE\the\n@cash\endcsname
    \else
      \advance\n@cash by -1
    \fi
  \repeat
}

