changeset 214:54c65ca9af76 misc

log2template: Add -L option for specifying path for *.loc files. If enabled, locfiles will be used to "correct" area names.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 23 Jul 2011 20:07:06 +0300
parents 930862631d57
children e0a4ddf06d79
files log2template.pl
diffstat 1 files changed, 65 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/log2template.pl	Sat Jul 23 18:32:22 2011 +0300
+++ b/log2template.pl	Sat Jul 23 20:07:06 2011 +0300
@@ -33,6 +33,8 @@
 my $opt_debug = 0;	# 1 = enable debug output
 my $opt_pkills_crop = 60;
 my $opt_outfile;
+my $opt_getlocs = 0;
+my $opt_locpath;
 my $opt_identify = 1;
 my $opt_npc = 1;
 
@@ -95,6 +97,9 @@
   elsif ($arg eq "-o") {
     $opt_outfile = shift or die("Output file option -o requires an argument.\n");
   }
+  elsif ($arg eq "-L") {
+    $opt_locpath = shift or die("Locfile path option -L requires an argument.\n");
+  }
   else {
     die("Invalid option '$arg'.
 Usage: $0 [options] < input.log > output.txt
@@ -103,6 +108,7 @@
     -d                    Enable debug mode.
     -i                    Disable identify parser.
     -n                    Disable NPC parser.
+    -L <locfile_path>     Path to <continent_name>.loc files.
 
 ");
   }
@@ -114,6 +120,11 @@
   open(STDOUT, '>', $opt_outfile) or die("Could not open output file '$opt_outfile'.\n");
 }
 
+if (defined($opt_locpath)) {
+  $opt_getlocs = 1;
+  print STDERR "Locfile path '".$opt_locpath."' set.\n";
+}
+
 
 ### Translate stats
 sub trans_stat($)
@@ -126,12 +137,40 @@
 }
 
 
+my %locations = ();
+sub parse_locfile($)
+{
+  my ($continent) = @_;
+
+  return if defined($locations{$continent}{"OK"});
+
+  my $nline = 0;
+  my $filename = $opt_locpath."/".$continent.".loc";
+
+  open(INFILE, '<', $filename) or die("Could not open locfile '$filename'.\n");
+  while (defined(my $line = <INFILE>)) {
+    $nline++;
+    chomp($line);
+    if ($line =~ /^(#|\s*$)/) {
+    } elsif ($line =~ /^\s*(\d+)\s*;\s*(\d+)\s*;\s*(\d\S*)\s*;\s*([^;]+);(.*)/) {
+      my ($xc, $yc, $flags, $namestr, $rest) = ($1,$2,$3,$4,$5);
+      my @names = split(/\s*\|\s*/, $namestr);
+      $locations{$continent}{$xc}{$yc} = \@names;
+    } else {
+      print STDERR "Error on LOCfile '$filename' #$nline.\n";
+    }
+  }
+  
+  close(INFILE);
+  $locations{$continent}{"OK"} = 1;
+}
+
+
 ### Globals
 my %monsters = ();
 my %aligns = ();
 my %spells = ();
 my %races = ();
-my %areas = ();
 my %ids = ();
 
 my $line = 0;
@@ -173,19 +212,15 @@
     } elsif ($s =~ /^You are in '.*?' in (.+?) on the continent of ([A-Z][a-z]+). \(Coordinates: (\d+)x, (\d+)y\)$/o) {
       # Area, continent
       $$mob{"area"} = $1;
-      $$mob{"cont"} = $2;
-
-      $areas{$1}{"cont"} = $2;
-      $areas{$1}{"x"} = $3;
-      $areas{$1}{"y"} = $4;
+      $$mob{"continent"} = $2;
+      $$mob{"xc"} = $3;
+      $$mob{"yc"} = $4;
     } elsif ($s =~ /^You are in '.*?', which is on the continent of ([A-Z][a-z]+). \(Coordinates: (\d)+x, (\d)+y\)$/o) {
       # Area, continent
       $$mob{"area"} = $1;
-      $$mob{"cont"} = $1;
-
-      $areas{$1}{"cont"} = $1;
-      $areas{$1}{"x"} = $2;
-      $areas{$1}{"y"} = $3;
+      $$mob{"continent"} = $1;
+      $$mob{"xc"} = $2;
+      $$mob{"yc"} = $3;
     } elsif ($s =~ /^You $sayregex '(-\?\?-|--\?\?--\?\?--\?\?--\?\?--)\.'$/o) {
       set_state(2, $s);
     } else {
@@ -667,7 +702,25 @@
 }
 
   print "
-| area = ".$$mob{"area"}."
+| area = ";
+
+  if ($opt_getlocs) {
+    my $cont = lc($$mob{"continent"});
+    my $xc = $$mob{"xc"};
+    my $yc = $$mob{"yc"};
+    parse_locfile($cont);
+    if (defined($locations{$cont}{$xc}{$yc})) {
+      my $nm = $locations{$cont}{$xc}{$yc}[0];
+      $nm =~ s/^[\@]//;
+      print $nm;
+    } else {
+      print $$mob{"area"};
+    }
+  } else {
+    print $$mob{"area"};
+  }
+
+  print "
 | skill = 
 | skill# = 
 | exp = ";