use POSIX qw(strftime);

sub arctic_input
{
  if ($_ =~ /A (.*)/)
  {
    $mon = $1;
    for ($i = 0; $i < $attacks - 1; $i++)
    {
      mcl_send("a $mon");
    }
  }
}

sub do_stat
{
  my ($line, $val) = @_;

  $line =~ /([A-Za-z]+):\s*([0-9]+)/;
  $stat[$val] = $2;

  if ($rerolling && $val == 4)
  {
    $need_reroll = 0;
    for ($i = 0; $i < 5; $i++)
    {
      $need_reroll = 1 if $stat[$i] < $statmin[$i];
    }
  }
}

sub remove_monster
{
  my ($monster) = $_[0];

  for ($i = 0; $i < @monsters; $i++)
  {
    if ($monsters[$i] eq $monster)
    {
      splice(@monsters, $i, 1);
      $i = @monsters;
    }
  }
}

sub arctic_output
{
    my ($line) = $_;

    $line =~ s/${ColorCode}.//gs;

  $_ = "" if $_ eq "***";

  if ($logo ne "")
  {
    open(FP, ">>$logo");
    print FP $_;
    print FP "\n";
    close FP;
  }

  foreach $sound (keys %sounds)
  {
    `aplay /home/mgorse/$sounds{$sound} >/dev/null 2>&1 &` if $line =~ /$sound/ and $sounds;
  }

  if ($line =~ /re hungry/)
  {
    $hungry = 1;
    return;
  }

  if ($line =~ /re thirsty/)
  {
    $thirsty = 1;
    return;
  }

  do_stat($line, 0) if $line =~ /^Intellect:/;
  do_stat($line, 1) if $line =~ /^Knowledge:/;
  do_stat($line, 2) if $line =~ /^Physique:/;
  do_stat($line, 3) if $line =~ /^Stamina:/;
  do_stat($line, 4) if $line =~ /^Agility:/;

  if ($line =~ /An? (\S+).*(enters the arena|appears in a puff)/)
  {
    $monsters[@monsters] = $1;
  }

  if ($line =~ /The (\S*).*has just gone to the (.*).$/)
  {
    $monflee = $2;
    remove_monster($1);
  }

  if ($line =~ /An? (\S*).*(returned with a perplexed|has just arrived)/)
  {
    $monflee = "";
    $monsters[@monsters] = $1;
  }
  elsif ($line =~ /just arrived from the/)
  {
    $monflee = "";
    mcl_send("");
  }

  if ($line =~ /^You're |You are in /)
  {
    @monsters = ();
  }

  if ($line =~ /falls to the ground lifeless/)
  {
    # TODO: this is too simplistic
    shift @monsters;
  }

  if ($line =~ /you don't see/)
  {
    # TODO
    shift @monsters;
  }

  if ($line =~ /^The .*you.*for ([0-9]+) damage/)
  {
    $hp -= $1;
    mcl_send("") if !@monsters;
  }
  elsif ($line =~ /attacked you with/)
  {
    mcl_send("he");
  }

  if ($line =~ /There (is|are).*here/)
  {
    @words = split(/ /, $line);
    $number = 0;
    for ($i = 2; $i < @words; $i++)
    {
      $word = $words[$i];
      $word =~ s/,$//;
      if ($word eq "a" or $word eq "an")
      {
	$number = 1;
      }
      elsif ($word eq "two")
      {
	$number = 2;
      }
      elsif ($word eq "three")
      {
	$number = 3;
      }
      elsif ($word eq "four")
      {
	$number = 4;
      }
      else
      {
	$word =~ s/s$// if $number > 1;
	$word =~ s/affreeti/affreet/;
	for ($j = 0; $j < $number; $j++)
	{
	  $monsters[@monsters] = $word if $word !~ /^(shop|barkeep|barmaid|master|blue)$/;
	}
	$number = 0;
      }
    }
  }

  $pdelay = 0 if $line =~ /Attack Ready/;
  $sdelay = 0 if $line =~ /Spell Ready/;

  if ($line =~ /^Mana:\s*([0-9]*)\s/)
  {
    $mana = $1;
  }

  if ($line =~ /^Vitality:\s*([0-9]*)\s/)
  {
    $hp = $1;
    $hprec = $hp if $hp < $hprec;
  }

  if ($line =~ /is to the (.*).$/)
  {
    $look = "";
    $nextdir = $1;
  }

  if ($line =~ /hostility is not permitted/)
  {
    @monsters = ();
    $pdelay = 0;
  }

  if ($line =~ /You found (.*) gold crowns/)
  {
    $gold += $1;
    $mode = 1 if $mode == 0 && $gold >= $gold_threshold && @loop > 0 && @pre_loop > 0;
  }

  $gold = $1 if $line =~ /You are carrying (.*) gold/;
}

sub arctic_callout
{
  $keepalive_delay++;
  if ($keepalive_delay >= ($bot > 0 ? $bot_keepalive_delay : 100) || $hp == 0)
  {
    mcl_send("he") if $bot >= 0;
    $hp = $minheal;
    $keepalive_delay = 0;
  }

  if ($need_reroll)
  {
    mcl_send("reroll");
    $need_reroll = 0;
    return;
  }

  $pdelay-- if $pdelay > 0;
  $sdelay-- if $sdelay > 0;

  return if $bot <= 0;

  if ($look && $look_delay == 0)
  {
    mcl_send("look $look");
    $look_delay = 30;
    return;
  }
  $look_delay-- if $look_delay > 0;

  if ($hp < $minhp && @monsters)
  {
    $need_flee = 1;
  }

  if ($hp < $minheal && $mana > 0 && $sdelay == 0 && !$need_flee)
  {
    if ($class eq "druid")
    {
      mcl_send("c fadi $player");
      $hp += $level * 2;
      $sdelay = ($platinum? 30: 15);
      $mana--;
    }
  }

  if (@monsters > 0 && $pdelay == 0 && !$need_flee)
  {
    for ($i = 0; $i < $attacks; $i++)
    {
      mcl_send("a $monsters[0]");
    }
    $pdelay = ($platinum? 30: 15);
  }

  if ($need_flee && !$pdelay && !$sdelay)
  {
    if ($arena == 1)
    {
      mcl_send("w");
      $arena = -$arena;
    }
    elsif ($arena == 2)
    {
      mcl_send("s");
      $arena = -$arena;
    }
    else
    {
      if ($level >= 5 && $platinum)
      {
	mcl_send("recall");
	@loop = ();
	@pending_moves = ();
      }
      else
      {
	mcl_send("x");
	$bot = -1;
      }
    }
    $hp = 999; # hack
    $need_flee = 0;
    return;
  }

  if ($arena == -1 && $hp >= $minheal && !$pdelay && !$sdelay)
  {
    mcl_send("e");
    $arena = -$arena;
    return;
  }
  elsif ($arena == -2 && $hp >= $minheal && !$pdelay && !$sdelay)
  {
    mcl_send("n");
    $arena = -$arena;
    return;
  }

  if ($arena == 1 && $pdelay == 0 && $sdelay == 0 && ($hungry || $thirsty))
  {
    mcl_send("w");
    mcl_send("ne");
    mcl_send("b meal") if $hungry;
    mcl_send("b drink") if $thirsty;
    mcl_send("sw");
    mcl_send("e");
    $hungry = $thirsty = 0;
  }

  if ($sdelay == 0 && !@monsters && ($hungry || $thirsty) && $class eq "druid" && $level >= 4 && $mana >= 2)
  {
    mcl_send("c kotari $player");
    $sdelay = 30;
    $hungry = $thirsty = 0;
  }

  if ($arena > 0 && !@monsters && $hp >= $minheal && $pdelay == 0)
  {
    mcl_send("ri g");
    $pdelay = 5;
  }

  if (!$pdelay && !$sdelay && !@monsters)
  {
    if ($mode == 1 && !@pending_moves)
    {
      @pending_moves = ("recall", "e", "n", "d", "i");
      $mode = 2;
    }
    elsif ($mode == 2 && !@pending_moves)
    {
      @pending_moves = ("de $gold", "u", "s");
      $gold = 0;
      $mode = 3;
    }
    elsif ($mode == 3 && !@pending_moves)
    {
      @pending_moves = @pre_loop;
      $loop_count = 0;
      $mode = 0;
    }

    if ($monflee)
    {
      unshift(@pending_moves, $rev{$monflee});
      unshift(@pending_moves, $monflee);
      $monflee = "";
    }

    if ($nextdir && $nextdir ne $loop[$loop_count])
    {
      unshift(@pending_moves, $rev{$nextdir});
      unshift(@pending_moves, $nextdir);
    }
    $nextdir = "";

    if (@pending_moves > 0)
    {
      $move = shift @pending_moves;
    }
    elsif (@loop > 0 && !$look)
    {
      $move = $loop[$loop_count++];
      $loop_count = 0 if $loop_count == @loop;
    }
    if ($move)
    {
      mcl_send($move);
      if ($move =~ /^look (.*)/)
      {
	$look = $1;
	$look_delay = 30;
      }
      $last_move = $move;
      $move = "";
    }
  }
}

sub sloop
{
  my($mon, $dir) = @_;

  $loop_count = 0;
  @loop = @pre_loop = ();
  $loop[0] = "look $mon";
  $loop[1] = $dir;
  $loop[2] = $rev{$dir};
}

sub loop
{
  my ($l) = $_[0];

  @loop = split(/\n/, $loop{$l});
  @pre_loop = split(/\n/, $pre_loop{$l});
}

sub enable_arctic
{
    userinput_add("arctic_input");
    output_add("arctic_output");
    callout_add("arctic_callout", 1);
}

connect_add("enable_arctic");

# user vars
$arena = 0;
$attacks = 5;
$bot = 0;
$bot_keepalive_delay = 40;
$class = "druid";
$gold_threshold = 3000;
$level = 21;
$logo = "";
$look = "";
$minheal = 365;
$minhp = 33;
$platinum = 1;
$player = "vortex";
$rerolling = 0;
$sounds = 1;

# state vars
$gold = 0;
$hp = 0;
$hprec = 999;
$hungry = 0;
$keepalive_delay = 0;
$look_delay = 0;
@loop = ();
$loop_count = 0;
$mode = 0;
$monflee = "";
$need_flee = 0;
$need_reroll = 0;
$nextdir = "";
@monsters = ();
$pdelay = 0;
@pending_moves = ();
$sdelay = 0;
$thirsty = 0;

$sounds{"has just gone to the"} = "flee.au";
$sounds{"is to the"} = "spider.au";
$sounds{"is paging you"} = "crawler.au";
$sounds{"yelling"} = "crawler.au";

$rev{"north"} = "south";
$rev{"n"} = "s";
$rev{"northeast"} = "southwest";
$rev{"ne"} = "sw";
$rev{"east"} = "west";
$rev{"e"} = "w";
$rev{"southeast"} = "northwest";
$rev{"se"} = "nw";
$rev{"south"} = "north";
$rev{"s"} = "n";
$rev{"southwest"} = "northeast";
$rev{"sw"} = "ne";
$rev{"west"} = "east";
$rev{"w"} = "e";
$rev{"northwest"} = "southeast";
$rev{"nw"} = "se";
$rev{"u"} = "d";
$rev{"d"} = "u";

1;

# platinum loops
$loop{"minwar"} = "se
e
se
ne
n
ne
look minotaur
n
s
sw
s
sw
nw
w
nw";

$loop{"minchief"} = "ne
ne
se
look minotaur
s
n
nw
sw
sw
s
se
nw
n
nw
se";

$loop{"bandits"} = "se
se
sw
sw
sw
sw
nw
n
nw
w
n
n
nw
se
s
s
w
sw
sw
se
se
s
s
se
se
nw
nw
n
n
nw
nw
ne
ne
e
e
se
s
se
ne
ne
ne
ne
nw
nw";

$pre_loop{"bandits"} = "s
s
sw
sw
s
sw
se
sw
sw
nw
w
w
sw
s
s
sw
sw
se
sw
se
se
se
se
sw";

$loop{"t3full"} = "e
ne
se
se
se
ne
e
se
s
e
ne
se
se
ne
ne
ne
se
se
se
#key
nw
nw
nw
sw
s
sw
sw
se
se
s
sw
w
s
d
n
nw
nw
n
e
ne
ne
ne
n
ne
se
ne
n
#key
s
sw
nw
sw
s
sw
sw
sw
w
s
se
se
s
e
se
se
s
e
se
ne
e
#key
w
sw
nw
w
n
nw
nw
w
s
d
ne
ne
e
u
se
se
e
ne
n
nw
ne
ne
n
nw
w
n
ne
nw
nw
nw
n
w
w
u
u
u
e
e
s
s
s
w
w
w
s
sw
sw
sw
sw
w
s
sw
s
se
se
se
s
se
e
s
se
se
se
sw
se
# level 1 works
s
e
se
se
se
sw
sw
se
e
ne
pull lever
sw
w
sw
se
e
se
ne
ne
e
n
n
ne
se
push stone
nw
sw
s
s
w
sw
sw
s
sw
sw
s
e
e
e
e
s
se
e
se
push stone
e
e
e
d
#level 2 works
w
sw
w
sw
w
s
s
e
ne
e
se
sw
sw
se
sw
w
nw
sw
s
pull lever
n
ne
se
e
ne
nw
ne
ne
nw
w
sw
w
n
n
e
ne
nw
n
nw
ne
ne
e
ne
say arok
n
n
d
#level 3
w
sw
s
w
w
sw
nw
w
s
se
sw
sw
sw
se
se
ne
se
e
se
sw
w
s
s
pull lever
n
n
e
ne
ne
nw
w
nw
sw
nw
nw
ne
ne
nw
n
n
nw
nw
n
d
#level 4 works
se
s
se
se
e
s
s
se
ne
ne
n
ne
nw
ne
n
pull lever
s
sw
se
sw
s
sw
sw
nw
n
n
w
sw
sw
sw
w
s
se
se
s
d
#level 5 works
ne
ne
e
ne
ne
se
ne
e
se
s
sw
sw
se
s
pull lever
n
nw
ne
ne
n
nw
w
sw
nw
ne
se
e
ne
nw
nw
n
n
d
#level 6 works
e
e
e
e
e
e
s
e
e
e
s
s
s
s
w
w
s
s
s
s
s
w
w
w
w
w
s
s
w
w
w
w
n
n
n
n
n
n
pull lever
s
s
s
s
s
s
e
e
e
s
s
e
e
e
e
e
e
n
n
n
e
n
n
e
e
e
s
s
s
s
s
s
s
s
pull lever
n
n
n
n
n
n
n
n
w
w
w
s
s
w
s
s
s
s
s
s
w
s
s
w
w
w
w
w
w";

$moves{"t3express"} = "se
sw
s
se
se
sw
se
se
ne
ne
se
se
se
e
s
d
#level 2 sewers
s
d
#level 3 sewers
ne
ne
e
up
se
se
e
ne
n
nw
ne
ne
n
nw
w
n
ne
nw
nw
nw
n
w
drink verbena
w
up
up
up
#desert
e
e
s
s
s
w
w
w
s
sw
sw
sw
sw
w
s
sw
s
se
se
se
s
se
e
s
se
se
se
sw
se
#level 1 works
s
e
se
se
se
sw
sw
se
sw
se
e
se
s
sw
sw
s
e
e
e
e
s
se
e
se
push stone
Delay 25
e
e
e
d
#level 2 works
w
sw
w
nw
n
nw
ne
ne
e
ne
n
n
d
#level 3 works
w
sw
s
w
w
sw
nw
w
n
nw
nw
n
d
#level 4 works
se
s
se
se
sw
sw
sw
w
s
se
se
s
d
#level 5 works
ne
ne
e
ne
ne
ne
se
e
ne
nw
nw
n
n
d
#level 6 works
e
e
e
e
e
e
s
e
e
e
s
s
s
s
w
w
s
s
s
w
w
w
w
s
s
w
s
s
e
e
e
e
e
e
s
s
s
w
s
s
w
w
w
w
w
w
w";


$pre_loop{"tower"} = $pre_loop{"bandits"}."se
se
sw
sw
sw
sw
nw
n
nw
w
w
sw
sw
se
se
s
s
se
se
sw
sw
sw
w
w";

$loop{"tower"} = "s
sw
s
e
e
se
s
s
s
s
e
e
e
w
w
w
w
w
w
e
e
e
s
s
s
s
n
e
e
u
s
s
e
e
e
w
w
w
w
w
w
e
e
e
s
s
s
s
e
e
e
w
w
w
w
e
e
e
n
n
n
n
n
n
d
w
w
w
w
u
u
e
e
e
s
s
s
s
w
w
w
n
n
e
u
e
e
s
s
s
e
e
e
n
n
n
s
s
s
w
w
n
n
n
w
w
d
w
s
s
e
e
e
n
n
n
n
w
w
w
d
d
e
e
n
n
n
n
n
n
n
nw
w
w
n
ne
n";
