#-----------------------------------------------
# ITEMID script.
# Sean Butler (Xin)
#
# $ItemStats is the main string that gets built
# by the IDer.
#-----------------------------------------------

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

my $ItemStats = "";

my $ItemAffectCount = 0;
my $ItemAffectOne = "";
my $ItemAffectTwo = "";
my $ItemAC = "";
my $ItemAttr = "";
my $ItemDice = "";
my $ItemEquip = "";
my $ItemType = "";
my $ItemEffects = "";
my $ItemSpell = "";
my $ItemLevel = "";
my $ItemName = "";
my $ItemCondition = "";
my $ItemWeight = "";
my $ItemDaysLeft = "";

# Main set of triggers for collecting item info.
sub stat_item {
  my $text = $_;
  my $tmp = "";
  #--------------------------- 
  # First strip ansi chars,
  # then get stats.
  #---------------------------
  $text =~ s/(\xEA.|\xEA\n)//g;
  if ($text =~ /^    (.*) to (.*)/) {
    $ItemAffectCount++;
    if ($ItemAffectCount == 1) {
      $ItemAffectOne = lc "${col_B}${2}(${col_C}${1}${col_B}) ";
    }
    else {
      $ItemAffectTwo = lc "${col_B}${2}(${col_C}${1}${col_B}) ";
    }      
  }
  elsif ($text =~ /^AC-apply of (\d*)$/) {
    $ItemAC = $1;
    $ItemAC =~ s/\s//g;
    $ItemAC = "${col_B}Ac(${col_C}${1}${col_B}) ";
  }
  elsif ($text =~ /^AC-apply of (\d*)     Class Restrictions: (.*)/) {
    #open (IDLOG, ">>idlog.txt");
    #print IDLOG "ac $1 cls $2\n";
    #close IDLOG;
    $ItemAC = "${col_B}Ac(${col_C}${1}${col_B}) ";
    $ItemAttr = $2;	
    $ItemAttr =~ s/ANTI_MAGE/${col_W}!M/g;
    $ItemAttr =~ s/ANTI_THIEF/${col_W}!T/g;
    $ItemAttr =~ s/ANTI_WARRIOR/${col_W}!W/g;
    $ItemAttr =~ s/ANTI_CLERIC/${col_W}!C/g;
    $ItemAttr =~ s/TWO_HANDED/${col_W}2H/g;
  }
  elsif ($text =~ /^Attributes: (.*)/) {
    $ItemAttr = $1;
    $ItemAttr =~ s/ANTI_MAGE/${col_W}!M/g;
    $ItemAttr =~ s/ANTI_THIEF/${col_W}!T/g;
    $ItemAttr =~ s/ANTI_WARRIOR/${col_W}!W/g;
    $ItemAttr =~ s/ANTI_CLERIC/${col_W}!C/g;
    $ItemAttr =~ s/TWO_HANDED/${col_W}2H/g;
  }
  elsif ($text =~ /Class Restrictions: (.*)/) {
    $ItemAttr = $1;
    $ItemAttr =~ s/ANTI_MAGE/${col_W}!M/g;
    $ItemAttr =~ s/ANTI_THIEF/${col_W}!T/g;
    $ItemAttr =~ s/ANTI_WARRIOR/${col_W}!W/g;
    $ItemAttr =~ s/ANTI_CLERIC/${col_W}!C/g;
    $ItemAttr =~ s/TWO_HANDED/${col_W}2H/g;
  }
  elsif ($text =~ /^Damage Dice of (.*)/) {
    $ItemDice = "${col_G}$1 ";
  }
  elsif ($text =~ /^Equipable Location\(s\): TAKE (.*)\s/) {
    $ItemEquip = lc $1;
    $ItemEquip = "${col_B}Loc(${col_C}${ItemEquip}${col_B}) ";
  }
  elsif ($text =~ /^Item Type: (.*)   Effects: (.*)/) {
    $ItemType = lc $1;
    $ItemType = "${col_B}Type(${col_C}${ItemType}${col_B}) ";
    $tmp = $2;
    if ($tmp =~ /ANTI-GOOD/) {
      $ItemEffects = "$col_W${ItemEffects}AG ";
    }
    if ($tmp =~ /ANTI-EVIL/) {
      $ItemEffects = "$col_W${ItemEffects}AE ";
    }
    if ($tmp =~ /ANTI-NEUTRAL/) {
      $ItemEffects = "$col_W${ItemEffects}AN ";
    }
  }
  elsif ($text =~ /Level (.*) spell of (.*).  Holds (.*) charges and has (.*) charges left./) {
    $ItemSpell = "${col_B}Spell(${col_C}${2}${col_B}\[${col_C}${1}${col_B}\]${col_W}: ${col_C}${4} ${col_B}of ${col_C}${3}$col_B) ";
  }


  elsif ($text =~ /Regenerates level (\d+) spell of (.+)\.  Has (\d+) maximum charges./) { 
	  $ItemSpell = "${col_B}Regens(${col_C}${2}${col_B}\[${col_C}${1}${col_B}\]${col_W}: ${col_C}${3} ${col_B}charges) ";
  } 
  elsif ($text =~ /Level (\d+) spells of (.*)/) {
	  $ItemSpell = "${col_B}Spells(lvl ${col_C}$1${col_W}: ${col_C}$2${col_B}) ";
  }
  elsif ($text =~ /Level Restriction: (.*)/) {
    $ItemLevel = "${col_B}Level(${col_C}${1}${col_B}) ";
  }
  elsif ($text =~ /^Object: (.*) \[(.*)\]/) {
    $ItemName = "$col_C${1} ";
  }
  elsif ($text =~ /^Weight: (.*) Value: (.*) Level Restriction: (.*)/) {
    $ItemLevel = "${col_B}Level(${col_C}${3}${col_B}) ";
  }
#-----------------------------
# Now for the item conditions:
#-----------------------------
  elsif ($text =~ /The object appears to be in perfect pristine condition./) {
	  $ItemCondition = "${col_G}pristine";
	  $ItemCondition = "${col_B}Cond($ItemCondition${col_B}) ";
  }
  elsif ($text =~ /The object appears to be in excellent condition./) {
	  $ItemCondition = "${col_G}excellent";
	  $ItemCondition = "${col_B}Cond($ItemCondition${col_B}) ";
  }
  elsif ($text =~ /The object appears to be in fair condition./) {
	  $ItemCondition = "${col_G}fair";
	  $ItemCondition = "${col_B}Cond($ItemCondition${col_B}) ";
  }
  elsif ($text =~ /The object appears to be in good condition./) {
	  $ItemCondition = "${col_G}good";
	  $ItemCondition = "${col_B}Cond($ItemCondition${col_B}) ";
  }
  elsif ($text =~ /The object is in fair condition but has some scratches./) {
	  $ItemCondition = "${col_Y}scratched";
	  $ItemCondition = "${col_B}Cond($ItemCondition${col_B}) ";
  }
  elsif ($text =~ /The object clearly shows major signs of wear and tear./) {
	  $ItemCondition = "${col_Y}worn";
	  $ItemCondition = "${col_B}Cond($ItemCondition${col_B}) ";	  
  }
  elsif ($text =~ /The object is visibly worn down with major wear./) {
	  $ItemCondition = "${col_R}worndown";
	  $ItemCondition = "${col_B}Cond($ItemCondition${col_B}) ";
  }
  elsif ($text =~ /The life of this object is clearly coming to an end soon./) {
	  $ItemCondition = "${col_R}ending";
  }
  elsif ($text =~ /The object looks as if it will fall apart any day now./) {
	  $ItemCondition = "${col_R}anyday";
	  $ItemCondition = "${col_B}Cond($ItemCondition${col_B}) ";
  }
  elsif ($text =~ /The object is visibly crumbling and decaying..../) {
	  $ItemCondition = "${col_r}crumbling";
	  $ItemCondition = "${col_B}Cond($ItemCondition${col_B}) ";
  }
  elsif ($text =~ /This object has been blessed by the Gods and seems indestructible./) {	  
	  $ItemCondition = "${col_C}nodet";
	  $ItemCondition = "${col_B}Cond($ItemCondition${col_B}) ";
  }
  elsif ($text =~ /^Your divination is complete.../) {
    item_clear();
  }
  elsif ($text =~ /^Days Left: (.*)/) {
	$ItemDaysLeft = "${col_B}Days(${col_C}${1}${col_B})";
  }  
}

# This collects the info into a meaningful short string that can
# be repeated on any mud channel.
sub item_string_construct {
  $ItemStats = "${ItemName}${ItemType}${ItemLevel}${ItemAC}${ItemDice}${ItemEquip}${ItemSpell}${ItemAffectOne}${ItemAffectTwo}${ItemEffects}${ItemAttr}${ItemCondition}${ItemDaysLeft}";
}

# Resets the item variables.  Occurs just as the person casts ID.
sub item_clear {
  $ItemStats = "";
  $ItemAffectCount = 0;
  $ItemAffectOne = "";
  $ItemAffectTwo = "";
  $ItemAC = "";
  $ItemAttr = "";
  $ItemDice = "";
  $ItemEquip = "";
  $ItemType = "";
  $ItemEffects = "";
  $ItemSpell = "";
  $ItemLevel = "";
  $ItemName = "";
  $ItemCondition = "";
  $ItemWeight = "";
  $ItemDaysLeft = "";
}

#------------------------------
# The user commands to echo the
# item's stats.
#------------------------------
sub cmd_sayidb {
  item_string_construct();
  strip_ansi();
  run("say $ItemStats");
}
sub cmd_clidb {
  item_string_construct();
  strip_ansi();
  run("clan $ItemStats");
}
sub cmd_townidb {
  item_string_construct();
  strip_ansi();
  run("town $ItemStats");
}

sub cmd_chidb {
  item_string_construct();
  run("${commandCharacter}chatall $ItemStats");
}
sub cmd_fidb {
  item_string_construct();
  strip_ansi();
  run("ft $ItemStats");
}
sub cmd_shidb {
  item_string_construct();
  strip_ansi();
  run("shout $ItemStats");
}
sub cmd_tidb {
  s/\n//g;
  item_string_construct();
  strip_ansi();
  run("tel $_ $ItemStats");
}
sub cmd_imidb {
  item_string_construct();
  strip_ansi();
  run("imm $ItemStats");
}

sub strip_ansi {
	$ItemStats =~ s/\e\[\d+m//g;
}

output_add(\&stat_item);
