print "${BGreen}Loading spellup.pl\n";    

@Spells = (
	"c detect invis",
	"c levitate me",
	"c breathe me",
	"c shield",
	"c invis me",
	"c armor me",
	"c bless me",
	"c quick me",
	"c infra me",
	"c sense life",
	"c detect magic",
	"c detect evil",
	"c detect good",
);
	
$spell = -1;

#
# Command to commence spelling up.
#
sub cmd_sp {
  $spell = 0;
  cast($spell);
}

#
#
#
sub cast ($) {
  $a_spell = shift;
  print "${BBlue}Casting ${BGreen}$Spells[$a_spell]${BBlue}... ";
  mcl_send($Spells[$a_spell]);
}

#
# Cancels spellup in progress.
#
sub cancel_spellup {
  $spell = -1;
  print "\n${BYellow}Spellup Canceled.\n";
}

#
# Code to run after each spell has (possibly) been cast.
#
sub spell_cast {
  if (($spell == -1) || (@Spells == 0)) {
    return 1;
  }
  if ($spell ==  (@Spells - 1)) {
    print "\n${BMagenta}Spellup Completed.\n";
    $spell = -1;
    return 1;
  }

  # Success
  if (/You chant out the arcane words and are drained of/) {
    cast(++$spell);
  }

  # Fumbled spell
  if (/You fumble over the correct inflection used to cast/) {
    cast($spell);
  }

  # Fighting
  if (/The battle distracts you from your spell casting/) {
    cancel_spellup;
  }

  # Resting
  if (/You have to stand up before you can do that/) {
    cancel_spellup;
  }

  # Sitting
  if (/That is impossible to do while sitting/) {
    cancel_spellup;
  }

  # Medlink
  if (/What are you talking about/) {
    cancel_spellup;
  }
}

output_add(\&spell_cast);
1;
