\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

\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}}%
  \makecard{%
    \fbox{%
      \copy\sidebox
      \hfill 
      \vbox to \m@neyheight{\hsize=1.6in
        \vfil\includegraphics[height=.85\m@neyheight]{icon.eps}\break
        \gamename\break\vfil
      }%
      \hfill
      \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
}

