# tick.pl
#
# Perl module to count seconds to next mud tick.
#
# by: Sean Butler
#

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

$old_hp = 0;
$new_hp = 0;
$new_mana      =  0; # The mana value we just got from the prompt.
$old_mana      =  0; # The previous mana value.
$hp = 0;
$mana = 0;

#--------------------------------------------------------
# Read values from the prompt and trigger a tick if they
# change.
#--------------------------------------------------------
sub parse_prompt {
  my $prompt = $_;
  my $is_prompt = 0;
  # remove the terminal color codes from the prompt.

  $prompt =~ s/${ColorCode}.//gs;  
  # $_ = sprintf("%-2d seconds> ", $secs_left);

  # match the stats values and save the mana.
  if ($prompt =~ /\<(\d*)hp (\d*)m (\d*)mv (\d*)br\>/) {
    $hp = $1;
    $mana = $2;
    $hp_change = $hp - $new_hp;
    $mana_change = $mana - $new_mana;
    $mtxt = "0";
    $hptxt = "0";

    if ($hp_change >  0)    {$hptxt = "$BBlue+$hp_change";}
    if ($hp_change <  0)    {$hptxt = "$BBlue$hp_change";}
    if ($hp_change >= 75)   {$hptxt = "$BCyan+$hp_change";}
    if ($hp_change >= 100)  {$hptxt = "$BMagenta+$hp_change";}
    if ($hp_change >= 200)  {$hptxt = "$BGreen+$hp_change";}
    if ($hp_change <= -75)  {$hptxt = "$Yellow$hp_change";}
    if ($hp_change <= -100) {$hptxt = "$BYellow$hp_change";}
    if ($hp_change <= -200) {$hptxt = "$BRed$hp_change";}

    if ($mana_change >  0)    {$mtxt = "$BBlue+$mana_change";}
    if ($mana_change <  0)    {$mtxt = "$BBlue$mana_change";}
    if ($mana_change >= 75)   {$mtxt = "$BCyan+$mana_change";}
    if ($mana_change >= 100)  {$mtxt = "$BMagenta+$mana_change";}
    if ($mana_change >= 200)  {$mtxt = "$BGreen+$mana_change";}
    if ($mana_change <= -75)  {$mtxt = "$Yellow$mana_change";}
    if ($mana_change <= -100) {$mtxt = "$BYellow$mana_change";}
    if ($mana_change <= -200) {$mtxt = "$BRed$mana_change";}

    print "$BBlue<$BBlue$hptxt$BBlue, $mtxt$BBlue>$White\n" unless ($hp_change == 0) && ($mana_change == 0);
  } 
  $old_mana = $new_mana;
  $new_mana = $mana;
  $old_hp = $new_hp;
  $new_hp = $hp;
}
prompt_add(\&parse_prompt);
