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

\makeindex[name=talismans,title={Talisman Summaries},columns=2]

% Some spells inflict X fatigue, where X is the spell level plus 2.
\newcounter{spellCost}
  \setcounter{spellCost}{1}
\newcounter{spellPlusOne}
  \setcounter{spellPlusOne}{1}
\newcounter{spellPlusTwo}
  \setcounter{spellPlusTwo}{1}
\newcounter{spellPlusThree}
  \setcounter{spellPlusThree}{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{spellCost}\relax%
      \renewcommand\spellRange{Error}%
      \or%
      \renewcommand\spellRange{Error}%
      \or%
        \renewcommand\spellRange{throwing distance}%
      \or%
        \renewcommand\spellRange{shouting distance}%
      \or%
        \renewcommand\spellRange{the horizon}%
      \else%
        \renewcommand\spellRange{line of sight}%
      \fi%
    }%
    {%
      \setcounter{track}{20}%
      \Repeat{\arabic{spellCost}}{\addtocounter{track}{-4}}%
      \ifnum\value{track}<1%
        \renewcommand\spellRange{touch}%
      \else%
        \renewcommand\spellRange{\arabic{track}~steps}%
      \fi%
    }%
}


%%%

\newtoggle{fullSpells}
  \settoggle{fullSpells}{true}
\newtoggle{talisman}
  \settoggle{talisman}{false}
\newtoggle{insubstantial}
  \settoggle{insubstantial}{false}
\newtoggle{distant}
  \settoggle{distant}{false}
\newtoggle{duplicated}
  \settoggle{duplicated}{false}
\newtoggle{opposedSpell}
  \settoggle{opposedSpell}{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{spellPlusOne}{\value{spellCost}}%
    \addtocounter{spellPlusOne}{1}%
  \setcounter{spellPlusTwo}{\value{spellCost}}%
    \addtocounter{spellPlusTwo}{2}%
  \setcounter{spellPlusThree}{\value{spellCost}}%
    \addtocounter{spellPlusThree}{3}%
  \conDmg{spellPlusTwo}%
  \ifstrequal{#2}{}{\renewcommand\spellEnhancements{}}{\renewcommand\spellEnhancements{#2,\space}}%
  \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}%
  \ifdefmacro{#5}{\toggletrue{opposedSpell}}{\togglefalse{opposedSpell}}%
  \renewcommand\spellSum{#6}%
  \renewcommand\spellNotes{#7}%
  \setRange%
  \iftoggle{duplicated}{%
    \foreach \i in {1,...,\arabic{spellCost}}%
      {\multiply\value{spellTargets} by \value{spellCost}\addtocounter{spellTargets}{0}}%
  }{}%
  \iftoggle{talisman}{%
    \index[talismans]{\textbf{#1} (\spellEnhancements\ \spellAction~\spellSphere) Range: \spellRange, Area: \arabic{spellTargets}, Resisted by: \detokenize{#5}, Bonus: \arabic{spellCost}. \spellSum|adfclosedflourishleft~p.~}%
  }{%
    \index[spells]{\textbf{#1} {\sffamily (\spellEnhancements \spellAction~\spellSphere)} \textit{Cost: \arabic{spellCost}, Range: \spellRange, Targets: \arabic{spellTargets}, Resisted by: \detokenize{#5}.} \spellSum|adfclosedflourishleft~p.~}%
  }%
}

\newcommand\fullSpell{%
  \needspace{4em}
  \begin{tabularx}{\linewidth}{m{.28\linewidth}X}
  \raggedright
  \textbf{\titleFont\large\spellName}
  \iftoggle{examplecharacter}{}{\par\footnotesize\hint{\spellEnhancements\ \spellAction}}
  \label{\spellName}
  &
    \small
    \textbf{\scshape Requirements:}
    \spellSphere~\arabic{spellCost}

    \textbf{\scshape Resisted~by:}
    \spellResistance

    \textbf{\scshape Range:}
    \spellRange%
    \ifnumcomp{\value{spellTargets}}{>}{1}{
      \textbf{\scshape Targets:}
      \arabic{spellTargets}%
    }{}
  \\
  \end{tabularx}
  \noindent
  \spellSum.
  \spellNotes
  \vspace{1em}
}

\newcommand\shortSpell{%
  \item[\spellName]
  \spellSum.

  (\textbf{Cost:}~\arabic{spellCost},
  \textbf{Range:}~\spellRange,
  \ifnum\value{spellTargets}>1%
    \textbf{Targets:}~\arabic{spellTargets},
  \fi%
  \iftoggle{opposedSpell}{%
    \addtocounter{castingBonus}{7}%
    \textbf{Resisted~by:} \spellResistance, \tn[\arabic{castingBonus}]%
  }{%
    \textbf{Roll:} \absNum{castingBonus}, \gls{tn} set by \spellResistance.%
  })%
}

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

\newcommand\showStdSpells[1][]{
  \setcounter{enc}{0}
  \setHighSpheres
  \needspace{2em}
  \begin{center}%
  \textcolor{\pageSideColor}{\large\Pentagram} \textbf{Spells} \textcolor{\pageSideColor}{\large\Pentagram}
  \end{center}%
  \begin{description}
    \settoggle{fullSpells}{false}
    #1
    \foreach \n in {3,2,1}{
      \foreach \s in {Light,Death,Life,Mind,Force,Earth,Air,Fire,Water,Fate}{
        \ifnumcomp{\value{\s}}{=}{\n}{
          \ifnum\value{enc}<3
            \input{config/spells/\s\n.tex}
          \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{
  \iftoggle{creatureBox}{%
    \spellName\ talisman%
    \setcounter{track}{\value{treasure}}%
  }{%
    \paragraph{\adforn{25}~\spellName~\adforn{53}}%
    \spellSum.
    \spellNotes
    \par
    \noindent
    {\it(\iftoggle{opposedSpell}{%
      \addtocounter{castingBonus}{7}%
      Resisted by: \spellResistance\ (\tn[\arabic{castingBonus}]), %
    }{%
      Roll: \absNum{castingBonus}, \gls{tn} set by \spellResistance, %
    }%
    Range: \spellRange%
    \ifnum\value{spellTargets}>1%
      , Area of Effect:~\arabic{spellTargets}
    \else%
    \fi)}
  }%
}

\newcommand\talisman[7]{%
  \toggletrue{talisman}%
  \setcounter{Charisma}{0}%
  \calcSpell{#1}{#2}{#3}{#4}{#5}{#6}{#7}%
  \setcounter{castingBonus}{\value{spellCost}}%
}

\newcommand\explainTalisman{%
  \setcounter{treasure}{\value{track}}%
  \lootMagic%
}

\newcommand\artefact[7]{
  \settoggle{fullSpells}{false}
  \clean
  \foreach \s in {Air, Fire, Earth, Water, Fate}%
    {\setcounter{\s}{0}}
  \setcounter{Intelligence}{#3}
  \setcounter{Wits}{#4}
  \setcounter{Charisma}{#5}
  #7
  \paragraph{\adforn{54}~#1~\adforn{26}\hspace{1em}}
  \label{#1}
  \index{Artefacts!#1}
  #2
  \par
  \noindent
  \begin{tabularx}{\linewidth}{rX}
  \hiderowcolors
  \textbf{Mission:} & \emph{#6!} \\
  \textbf{Mind:} & 
    Intelligence \arabic{Intelligence}, Wits \arabic{Wits}, Charisma \arabic{Charisma}
      \\
  \textbf{Spheres:} & 
    \foreach \s in {Air, Fire, Earth, Water, Fate}%
      {\ifnum\value{\s}>0%
        \s~\arabic{\s},
        \addtocounter{mp}{3}%
      \fi}%
      \\%
    \textbf{Skills:} & \showSkills \\
    \textbf{\glspl{mp}:} &
    \ifnum\value{mp}>0
      \boxStat{mp}
    \else
      0
    \fi \\
  \end{tabularx}
  \setcounter{wounds}{0}
}