% 
%  __  __             _      
% |  \/  | __ _  __ _(_) ___ 
% | |\/| |/ _` |/ _` | |/ __|
% | |  | | (_| | (_| | | (__ 
% |_|  |_|\__,_|\__, |_|\___|
%               |___/        


% Some spells inflict X fatigue, where X is the spell level plus 2.
\newcounter{spellCost}
  \setcounter{spellCost}{1}
\newcounter{spellPlusTwo}
  \setcounter{spellPlusTwo}{1}
\newcounter{castingBonus}
  \setcounter{castingBonus}{1}

\newcommand\spellName{}
\newcommand\spellEnhancements{}
\newcommand\spellAction{}
\newcommand\spellSphere{}
\newcommand\spellResistance{}
\newcommand\spellSum{}
\newcommand\spellNotes{}

\newcounter{spellTargets}
\setcounter{spellTargets}{1}

\newcommand\spellRange{}
\newcommand\setRange{%
  \iftoggle{distant}%
    {%
      \ifcase\value{spelllevel}\relax%
      \renewcommand\spellRange{Error}%
      \or%
      \renewcommand\spellRange{Error}%
      \or%
        \renewcommand\spellRange{throwing distance}%
      \or%
        \renewcommand\spellRange{shouting distance}%
      \or%
        \renewcommand\spellRange{horizon}%
      \else%
        \renewcommand\spellRange{line of sight}%
      \fi%
    }%
    {%
      \setcounter{track}{20}%
      \Repeat{\arabic{spelllevel}}{\addtocounter{track}{-4}}%
      \ifnum\value{track}<1%
        \renewcommand\spellRange{touch}%
      \else%
        \renewcommand\spellRange{\arabic{track}~steps}%
      \fi%
    }%
}


%%%

\newtoggle{fullSpells}
  \settoggle{fullSpells}{true}
\newtoggle{insubstantial}
  \settoggle{insubstantial}{false}
\newtoggle{distant}
  \settoggle{distant}{false}
\newtoggle{duplicated}
  \settoggle{duplicated}{false}



\newcommand\calcSpell[7]{%
  \renewcommand\spellName{#1}%
  \setcounter{spellCost}{1}%
  \setcounter{spellTargets}{1}%
  \togglefalse{distant}%
  \togglefalse{duplicated}%
  \foreach \d in {#2}{%
    \ifdefstring{\d}{distant}{\global\toggletrue{distant}}{}%
    \ifdefstring{\d}{duplicated}{\global\toggletrue{duplicated}}{}%
    \stepcounter{spellCost}%
  }%
  \setcounter{spellPlusTwo}{\value{spellCost}}%
    \addtocounter{spellPlusTwo}{2}%
  \renewcommand\spellEnhancements{#2}%
  \renewcommand\spellAction{#3}%
  \renewcommand\spellSphere{#4}%
  % What bonus does the caster receive?  Some spells will display the caster's current Bonus, so we must calculate it.  We start by setting it sky-high, then for each element in {Earth,Fire}, et c. we check if this is lower than the current castingBonus, if (if so), lower the bonus.
  \setcounter{castingBonus}{10}%
  \foreach \e in {#4}{%
    \ifnum\value{\e}<\value{castingBonus}%
      \setcounter{castingBonus}{\value{\e}}%
    \fi%
  }{}%
  \iftoggle{snapCaster}{%
    \addtocounter{castingBonus}{\value{Wits}}%
  }{%
    \addtocounter{castingBonus}{\value{Charisma}}%
  }%
  \renewcommand\spellResistance{#5}
  \renewcommand\spellSum{#6}
  \renewcommand\spellNotes{#7}
  \setRange%
    \multiply\value{spellTargets} by \value{spellCost}%
    \multiply\value{spellTargets} by \value{spellCost}%
    \addtocounter{spellTargets}{0}%
  \iftoggle{distant}{%
    \foreach \x in {1,...,\arabic{spellCost}}%
      {\multiply\value{spellTargets} by 10\addtocounter{spellTargets}{0}}%
  }{}%
  \index[spells]{\textbf{#1} Cost: \arabic{spellCost}, \spellEnhancements\ \spellAction~\spellSphere, Range: \spellRange, Targets: \arabic{spellTargets}, Resisted by: \detokenize{#5}, \spellSum}
}

\newcommand\fullSpell{%
  \paragraph{\spellName}

  Cost: \arabic{spellCost},
  \spellEnhancements,
  \spellAction~
  \spellSphere

  Resisted by: \spellResistance,
  Range: \spellRange
  \ifnum\value{spellTargets}>1%
    , Targets: \arabic{spellTargets}
  \fi

  \spellSum \ 
  \spellNotes
}

\newcommand\shortSpell{%
  \addtocounter{castingBonus}{7}%
  \item[\spellName]
  Cost: \arabic{spellCost},
  \spellEnhancements~
  \spellAction~
  \spellSphere

  Resisted by: \spellResistance\ (\tn[\arabic{castingBonus}])

  \spellSum \ 
  \spellNotes
}


\newcommand\spell[7]{%
  \calcSpell{#1}{#2}{#3}{#4}{#5}{#6}{#7}%
  \iftoggle{fullSpells}{%
    \fullSpell%
  }{%
    \shortSpell%
  }%
}

\newcommand\showStdSpells{
  \setcounter{enc}{0}
  \begin{description}
    \settoggle{fullSpells}{false}
    \foreach\n in {3,2,1}{
      \ifnum\value{enc}<3
        \foreach \s in {Earth,Air,Fire,Water,Fate}{
          \ifnumcomp{\value{\s}}{=}{\n}{
              \input{config/spells/\s\n.tex}
              \stepcounter{enc}
          }{}
        }
      \fi
    }
  \end{description}
  \textcolor{\pageSideColor}{\hrulefill\raisebox{-3pt}{\npcsymbol}}
}

\newcommand\showSpells[1]{
  \needspace{3em}
  \settoggle{fullSpells}{true}
  \foreach \s in {#1}{
    \input{config/spells/\s.tex}
  }
}

\newcommand\showTalisman{
  \addtocounter{castingBonus}{7}%
  \paragraph{\spellName}
  \spellSum

  Resisted by: \spellResistance\ (\tn[\arabic{castingBonus}])

  \spellNotes
}

\newcommand\talisman[7]{%
  \calcSpell{#1}{#2}{#3}{#4}{#5}{#6}{#7}%
  \index{Talismans!#1}
  \showTalisman
}

