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

my %targets = ();
my $current_target = "Axtrica";
my $method = "rec war";
my $method2 = "trip";
my $pkon = 1;
my $fixed_target = "Axtrica";


sub cmd_tg { print "${BYellow}Fixed Target${BBlack}:${White} $_\n"; $fixed_target = $_; pk_update_stat;}
sub cmd_at1 { print "${BYellow}Attack ${White}1${BBlack}(${BWhite}ALT-G${BBlack})${BWhite}:${White} $_\n"; $method = $_; pk_update_stat; }
sub cmd_at2 { print "${BYellow}Attack ${White}2${BBlack}(${BWhite}ALT-H${BBlack})${BWhite}:${White} $_\n"; $method2 = $_; pk_update_stat; }
sub cmd_hhh { run("$method2 $fixed_target"); }
sub cmd_bbb { run("bs $fixed_target"); }

sub cmd_sint { run("rec sin $fixed_target"); }
sub cmd_paint { run("rec pain $fixed_target"); }
sub cmd_lightt { run("rec pain $fixed_target"); }
sub cmd_skullt { run("hold skull\;use skull $fixed_target"); }
sub cmd_wandt { run("hold wand\;use wand $fixed_target"); }

# my stuff

sub cmd_t1 {  
  $fixed_target = $_;
  print "\nFixed target is now: $fixed_target\n";
}

sub cmd_b { run("bs $fixed_target"); }
sub cmd_j { run("scan"); }
sub cmd_h { run("trip $_"); }
sub cmd_plt { run("c plag $fixed_target"); }
sub cmd_pl { run("c plag $_"); }
sub cmd_wkt { run("c weak $fixed_target"); }
sub cmd_wk { run("c weak $_"); }
sub cmd_fft { run("c faerie fire $fixed_target"); }
sub cmd_ff { run("c faerie fire $_"); }
sub cmd_colt { run("c col $fixed_target"); }
sub cmd_col { run("c col $_"); }
sub cmd_blit { run("c blind $fixed_target"); }
sub cmd_bli { run("c blind $_"); }
sub cmd_hat { run("c harm $fixed_target"); }
sub cmd_ha { run("c harm $_"); }
sub cmd_swt { run("c shockwave $fixed_target"); }
sub cmd_sw { run("c shockwave $_"); }
sub cmd_frot { run("c frost $fixed_target"); }
sub cmd_fro { run("c frost $_"); }
sub cmd_healm { run("c heal me"); }
sub cmd_dit { run("c dispel magic $fixed_target"); }
sub cmd_di { run("c dispel magic $_"); }
sub cmd_mm { run("c magic missile $_"); }
sub cmd_mmt { run("c magic missile $fixed_target"); }
sub cmd_fog { run("c faerie fog"); }
sub cmd_cut { run("c curse $fixed_target"); }
sub cmd_cu { run("c curse $_"); }
sub cmd_tt { run("trip $_"); }
sub cmd_ttt { run("trip $fixed_target"); }

# finish my stuff


sub cmd_pk {
  s/\n//g;
  my ($count) = (0);
  if (/^(\d+)/) {
    # Remove a certain target
    foreach (keys %targets) {
      if (++$count == $1) {
	print "Deleted target: $_\n";
	delete $targets{$_};
	return;
      }
    }
    print "No such target (there are only $count!) - type pk to see a list.\n";
  } elsif (/^.{3,}$/) {
    $targets{$_} = 0;
    print "${BWhite}Target: '${BRed}$_${BWhite}' added to list.\n" unless $Loading;
  } else {
    print "\nFixed Target: $fixed_target\n";
    print "Attack 1: $method\n";
    print "Attack 2: $method2\n";   
    print "Following targets are active:\n";
    foreach (keys %targets) {
      printf "%2d) ${BRed}$_\n${White}", ++$count, $targets{$_};
    }
    print "\nUse pk <string> to add a target.\n";
    print "Use pk <number> to remove.\n";
  }  
}

sub cmd_ppk {
  $fixed_target = $_;
  print "\nFixed target is now: $fixed_target\n";
}

sub cmd_pkon {
  $pkon = 1;
  print "\n${BWhite}PK system turned ${BGreen}ON${White}!\n";
}

sub cmd_pkoff {
  $pkon = 0;
  print "\n${BWhite}PK system turned ${BBlue}OFF${White}!\n";
}

sub cmd_meth {
  s/\n//g;
  $method = $_;
}

sub cmd_pkclear {
  foreach (keys %targets) {
    print "Deleted target: $_\n";
    delete $targets{$_};
  }
}

sub pk_input_hook {
  if ($pkon == 0) {
    return;
  }
  foreach $target (keys %targets) {
    if (/$target/) {
      if ("$current_target" eq "$target") { return; }
      else {
       $current_target = $target;
       print "\n${BYellow}Target now: $current_target${White}";
      } 
     return;
    }
  }
}

sub pk_update_stat { run("pk"); }

sub cmd_ggg { run("$method $fixed_target"); }

sub cmd_gg { run("$method $current_target"); }

sub cmd_hh { run("$method2 $current_target"); }

sub cmd_ss { run("bs $current_target"); }

sub cmd_bb { run("bs $current_target"); }

sub save_targets {
  local (*FH) = $_[0];
  foreach (keys %targets) {
    print FH "Pk $_\n";
  }
}

save_add(\&save_targets);
output_add(\&pk_input_hook);
load_add("pk", \&cmd_pk);
