# !/usr/bin/perl -w

# 2000-04-08 0.0.1 first version
# 2000-04-26 0.0.2 Alex Fung: print archetype and difficulty settings

$VERSION = 0.02;
$| = 1; # disable out buffer

use Cowgen::Dromed;    # for some Thief constants and specifics
use Cowgen::Vbr;
use Cowgen::Bunch;

print "Listbooks v$VERSION (c) Tels 2000. All Rights Reserved.\n\n";
print "List all books/scrolls/plagues etc in a mission.\n\n";
print "Usage: perl listbooks.pl misfile\n\n";

# get commandline parameter
$infile = shift || "houses.mis";

print "Reading '$infile' ...";
$vbr = Cowgen::Vbr->create($infile); # make object from template mission
print "\n Input is a ",filetype2Name($vbr->fileType())," file from ",
      version2Name($vbr->version());
if ($vbr->gamesysName() ne "")
  {
  print " and uses '",$vbr->gamesysName(),"'.\n";
  $gf = Cowgen::Gamfile->create($vbr->gamesysName());
  $gf->decodeProperties(); # attach archetype properties to the archetypes
  }
else
  {
  print ".\n";
  }
my $b = Cowgen::Bunch->create ( "dummy", $vbr, $gf );

# now walk list of objects and pick out the proper ones
#print "\nName\t\tid\tfile\t\tart\n";

$b->iterate ( over => 'objects', call => \&printBook, );

print "\nDone. Have a nice day.\n\n";

# EOF
1;

sub printBook
  {
  # this sub is called for every object and if the object is a book, it will
  # print it's id and the text file name
  my ($b,$o) = @_;
  my @p;
  if (exists $o->{prop}->{booktext})
    {
#    print $o->{id}, "\t", $o->{prop}->{name}->[0] || "object", "\n";
#    print $o->{base} || "nobase","\n";
#    print $gf->archetypeName($o->{base}) || "no type", "\n";

     my $line = $o->{prop}->{booktext}->[0];
     $line =~ s/\.str//g;
    print $o->{id}, "\t";
    print $line, ".str", (length($line) < 12) ? "\t": "", "\t";
    if (exists $o->{prop}->{name})
      {
      print $o->{prop}->{name}->[0], (length($o->{prop}->{name}->[0]) < 16)
? "\t": "", (length($o->{prop}->{name}->[0]) < 8) ? "\t": "", "\t";
      }
    else
      {
      print "object\t\t\t";
      }
    print "is ", $gf->archetypeName($o->{base});
    if ($o->{prop}->{destroy}->[0] == 1) {
      print " Diff HE";
    } elsif ($o->{prop}->{destroy}->[0] == 2) {
      print " Diff NE";
    } elsif ($o->{prop}->{destroy}->[0] == 3) {
      print " Diff E";
    } elsif ($o->{prop}->{destroy}->[0] == 4) {
      print " Diff NH";
    } elsif ($o->{prop}->{destroy}->[0] == 5)

      print " Diff H";
    } elsif ($o->{prop}->{destroy}->[0] == 6) {
      print " Diff N";
    }
    print "\n";
    }
  }
