changeset 288:e2f5b059e14d misc

Factor long mode dump into a function. Add -dumpfiles option.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 09 Mar 2015 13:30:07 +0200
parents 635da7cf43b6
children fb11575b634e
files materials/parsematerial.pl
diffstat 1 files changed, 97 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/materials/parsematerial.pl	Mon Mar 09 06:16:26 2015 +0200
+++ b/materials/parsematerial.pl	Mon Mar 09 13:30:07 2015 +0200
@@ -361,6 +361,7 @@
   elsif ($arg eq "-perl") { $opt_mode = $arg; }
   elsif ($arg eq "-php") { $opt_mode = $arg; }
   elsif ($arg eq "-table") { $opt_mode = $arg; }
+  elsif ($arg eq "-dumpfiles") { $opt_mode = $arg; $opt_strip = 0; }
   elsif ($arg eq "-dump") { $opt_mode = $arg; $opt_strip = 0; }
   else
   {
@@ -497,6 +498,86 @@
 }
 
 
+sub print_material_long($$$)
+{
+  my ($file, $mat, $name) = @_;
+
+  printf $file
+  "MATERIAL STATS (".$name."):\n".
+  "-------------------------------------------------------------------------------\n".
+  "Weight (g/l):  %-17s  Sturdyness:    (%s)\n".
+  "Magic:         %-17s  Flammability:  (%s)\n",
+  "(".pt($mat, 2).")", pt($mat, 3),
+  "(".pt($mat, 4).")", pt($mat, 5);
+  
+  printf $file
+  "Warmth:        %-17s  Toxicity:      (%s)\n".
+  "Resistivity:   %-17s  Malleability:  (%s)\n",
+  "(".pt($mat, 6).")", pt($mat, 7),
+  "(".pt($mat, 8).")", pt($mat, 9);
+  
+  printf $file
+  "Mentality:     %-17s  Reactivity:    (%s)\n".
+  "Weapon rarity: %-17s  Armour rarity: (%s)\n".
+  "Value (gp/kg): %-17s  Rarity:        (%s)\n",
+  "(".pt($mat, 10).")", pt($mat, 11),
+  "(".pt($mat, 12).")", pt($mat, 13),
+  "(".pt($mat, 14).")", pt($mat, 15);
+
+  printf $file
+  "Article:       %-17s       Type:          %s\n".
+  "Found nature:  %-17s       Lode name:     %s\n".
+  "Raw names:     %s\n".
+  "Refined names: %s\n",
+  $$mat{"article"}, pt($mat, 16),
+  pt($mat, 17), pt($mat, 18),
+  join(", ", pta($mat, 19)),
+  join(", ", pta($mat, 20));
+}
+
+
+sub get_alloys($$)
+{
+  my ($mats, $all) = @_;
+  my %target = ();
+  foreach my $mat (@$mats)
+  {
+    for (my $i = 3; $i <= 11; $i++)
+    {
+      $target{$i} += $data{$mat}{$i} / scalar(@$mats);
+    }
+  }
+
+  for (my $i = 3; $i <= 11; $i++)
+  {
+    print $target{$i}." ";
+  }
+  print "\n";
+
+  my $res = [];
+  my $best = 9999999;  
+  foreach my $mat (@$all)
+  {
+    next if (grep { $_ eq $mat } @$mats);
+
+    my $val = 0;
+    for (my $i = 3; $i <= 11; $i++)
+    {
+      $val += abs($target{$i} - $data{$mat}{$i});
+    }
+
+    push(@$res, $mat) if ($val == $best);
+    if ($val < $best)
+    {
+      $res = [$mat];
+      $best = $val;
+    }
+  }
+
+  return $res;
+}
+
+
 ###
 ### Output functionality begins here
 ###
@@ -520,48 +601,26 @@
 }
 
 # Dump normal data to files
+elsif ($opt_mode eq "-dumpfiles")
+{
+  foreach my $name (@entries)
+  {
+    my $filename = $name.".mat";
+    $filename =~ s/[^a-z0-9]/_/g;
+    open(OUTFILE, ">", $filename) or die("Error opening file '".$filename."' ".$!."\n");
+    print_material_long(\*OUTFILE, $data{$name}, $name);
+    close(OUTFILE);
+  }
+}
+
+# Dump normal data to stdout
 elsif ($opt_mode eq "-dump")
 {
   foreach my $name (@entries)
   {
-    my $mat = $data{$name};
-    my $filename = $name.".mat";
-    $filename =~ s/ /_/g;
-    open(OUTFILE, ">", $filename) or die("Error opening file '$filename' ".$!."\n");
-
-    printf OUTFILE
-    "MATERIAL STATS ($name):\n".
-    "-------------------------------------------------------------------------------\n".
-    "Weight (g/l):  %-17s  Sturdyness:    (%s)\n".
-    "Magic:         %-17s  Flammability:  (%s)\n",
-    "(".pt($mat, 2).")", pt($mat, 3),
-    "(".pt($mat, 4).")", pt($mat, 5);
-    
-    printf OUTFILE
-    "Warmth:        %-17s  Toxicity:      (%s)\n".
-    "Resistivity:   %-17s  Malleability:  (%s)\n",
-    "(".pt($mat, 6).")", pt($mat, 7),
-    "(".pt($mat, 8).")", pt($mat, 9);
-    
-    printf OUTFILE
-    "Mentality:     %-17s  Reactivity:    (%s)\n".
-    "Weapon rarity: %-17s  Armour rarity: (%s)\n".
-    "Value (gp/kg): %-17s  Rarity:        (%s)\n",
-    "(".pt($mat, 10).")", pt($mat, 11),
-    "(".pt($mat, 12).")", pt($mat, 13),
-    "(".pt($mat, 14).")", pt($mat, 15);
-
-    printf OUTFILE
-    "Article:       %-17s       Type:          %s\n".
-    "Found nature:  %-17s       Lode name:     %s\n".
-    "Raw names:     %s\n".
-    "Refined names: %s\n",
-    $$mat{"article"}, pt($mat, 16),
-    pt($mat, 17), pt($mat, 18),
-    join(", ", pta($mat, 19)),
-    join(", ", pta($mat, 20));
-
-    close(OUTFILE);
+    print "\n".
+    "-------------------------------------------------------------------------------\n";
+    print_material_long(\*STDOUT, $data{$name}, $name);
   }
 }