view materials/parsealloys.pl @ 54:47dc88dc732f misc

Sync.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Sep 2009 17:24:57 +0000
parents c8c2c545fa63
children f690940e9118
line wrap: on
line source

#!/usr/bin/perl -w
# Superklunssi by Ggr
# Old format of alloying skill output was somewhat different, but
# it has been tuned somewhere after 2005. New output format is:
#
#You mix obsidian and crystal and create a quantity of glass
#You mix nullium and illumium and create a quantity of dukonium
#You mix nullium and illumium and create a quantity of dukonium
use strict;

my ($opt_php);

if ($#ARGV >= 0) {
  if ($ARGV[0] eq "-php") { $opt_php = 1; }
}   

our %alloyNames = ("adamantium" => 0,"air" => 1,"alabaster" => 2,"alexandrite" => 3,"aluminium" => 4,"amber" => 5,"amethyst" => 6,"anipium" => 7,"aquamarine" => 8,"bamboo" => 9,"bark" => 10,"basalt" => 11,"batium" => 12,"birch" => 13,"bloodstone" => 14,"bone" => 15,"brass" => 16,"brick" => 17,"bronze" => 18,"burlap" => 19,"carnelian" => 20,"cedar" => 21,"cesium" => 22,"chalk" => 23,"charcoal" => 24,"chromium" => 25,"chrysoberyl" => 26,"clay" => 27,"cloth" => 28,"coal" => 29,"cobalt" => 30,"concrete" => 31,"copper" => 32,"coral" => 33,"cork" => 34,"cotton" => 35,"crystal" => 36,"darksteel" => 37,"diamond" => 38,"diggalite" => 39,"dragonscale" => 40,"dukonium" => 41,"duraluminium" => 42,"durandium" => 43,"earth" => 44,"ebony" => 45,"electrum" => 46,"elm" => 47,"emerald" => 48,"emulsion" => 49,"enchanted air" => 50,"feathers" => 51,"fire" => 52,"flesh" => 53,"food" => 54,"fur" => 55,"garnet" => 56,"glass" => 57,"gold" => 58,"granite" => 59,"graphite" => 60,"hematite" => 61,"hemp" => 62,"highsteel" => 63,"ice" => 64,"illumium" => 65,"indium" => 66,"iridium" => 67,"iron" => 68,"ivory" => 69,"jade" => 70,"kryptonite" => 71,"laen" => 72,"lead" => 73,"leather" => 74,"magnesium" => 75,"mahogany" => 76,"malachite" => 77,"mallorn" => 78,"maple" => 79,"marble" => 80,"marlor" => 81,"mithril" => 82,"molybdenum" => 83,"moonstone" => 84,"moss agate" => 85,"mowgles" => 86,"mowglite" => 87,"neo burlap" => 88,"nickel" => 89,"nullium" => 90,"oak" => 91,"obsidian" => 92,"olivine" => 93,"onyx" => 94,"opal" => 95,"osmium" => 96,"palladium" => 97,"paper" => 98,"pearl" => 99,"petrified wood" => 100,"pewter" => 101,"phosphorus" => 102,"platinum" => 103,"porcelain" => 104,"potassium" => 105,"pyrite" => 106,"quartz" => 107,"quicksilver" => 108,"rhodium" => 109,"rhodonite" => 110,"rubber" => 111,"ruby" => 112,"sapphire" => 113,"silicon" => 114,"silk" => 115,"silver" => 116,"slate" => 117,"starmetal" => 118,"steel" => 119,"steuben" => 120,"stone" => 121,"sulphur" => 122,"sunstone" => 123,"tadmium" => 124,"tin" => 125,"titanium" => 126,"topaz" => 127,"tormium" => 128,"tungsten" => 129,"turquoise" => 130,"uranium" => 131,"vanadium" => 132,"vegetation" => 133,"water" => 134,"wax" => 135,"weenite" => 136,"wood" => 137,"wool" => 138,"zhentorium" => 139,"zinc" => 140,"zircon" => 141,);
our @alloyNameTable = ("adamantium","air","alabaster","alexandrite","aluminium","amber","amethyst","anipium","aquamarine","bamboo","bark","basalt","batium","birch","bloodstone","bone","brass","brick","bronze","burlap","carnelian","cedar","cesium","chalk","charcoal","chromium","chrysoberyl","clay","cloth","coal","cobalt","concrete","copper","coral","cork","cotton","crystal","darksteel","diamond","diggalite","dragonscale","dukonium","duraluminium","durandium","earth","ebony","electrum","elm","emerald","emulsion","enchanted air","feathers","fire","flesh","food","fur","garnet","glass","gold","granite","graphite","hematite","hemp","highsteel","ice","illumium","indium","iridium","iron","ivory","jade","kryptonite","laen","lead","leather","magnesium","mahogany","malachite","mallorn","maple","marble","marlor","mithril","molybdenum","moonstone","moss agate","mowgles","mowglite","neo burlap","nickel","nullium","oak","obsidian","olivine","onyx","opal","osmium","palladium","paper","pearl","petrified wood","pewter","phosphorus","platinum","porcelain","potassium","pyrite","quartz","quicksilver","rhodium","rhodonite","rubber","ruby","sapphire","silicon","silk","silver","slate","starmetal","steel","steuben","stone","sulphur","sunstone","tadmium","tin","titanium","topaz","tormium","tungsten","turquoise","uranium","vanadium","vegetation","water","wax","weenite","wood","wool","zhentorium","zinc","zircon",);


## Parse data from input, ignoring fumbled alloys
my %data = ();
my $fumble = 0;
my $nline = 0;

sub addCombo($$$) {
  if (!$fumble) {
    push(@{$data{$_[0]}{$_[1]}}, $_[2]);
  } else {
    print STDERR "Ignoring fumbled combo $_[0] + $_[1] = $_[2]\n";
  }
  $fumble = 0;
}

while (<STDIN>) {
  chomp;
  if (/^You mix ([a-z ]+?) and ([a-z ]+?) and create a quantity of (.+)$/) {
    addCombo($1, $2, $3);
  } elsif (/^([a-zA-Z0-9]+)#([a-z ]+)\+([a-z ]+)=([a-z ]+)$/) {
    addCombo($2, $3, $4);
  } elsif (/^(You slip up and fumble your attempt.)/) {
    $fumble = 1;
  }
}


## Re-format the data into single "ordered" hash, using as
## close to alphabetical order as possible.
my %final = ();
foreach my $min1 (keys %data) {
  foreach my $min2 (keys %{$data{$min1}}) {
    if (!defined($final{$min1}{$min2}) && !defined($final{$min2}{$min1})) {
      # Get both (a, b) and (b, a) combinations results into same array
      my @tmp = ();
      if (defined($data{$min1}{$min2})) {
        push(@tmp, @{$data{$min1}{$min2}});
      }
      if (defined($data{$min2}{$min1})) {
        push(@tmp, @{$data{$min2}{$min1}});
      }
      
      # Determine if there are multiple results and which is the most likely.
      # Some alloy combinations are inherently "unstable".
      my %seen = ();
      foreach my $mat (@tmp) { $seen{$mat}++; }
      if ($opt_php) {
        my $n = -1;
        my $mult = 0;
        my $res = "";
        foreach my $mat (keys %seen) {
          $res = $mat;
          $mult++;
        }
        if ($mult > 1) { $res = "unstable"; }
        $final{$min1}{$min2} = $res;
        $final{$min2}{$min1} = $res;
      } else {
#        print STDERR "$min1 : $min2 == "; foreach my $a (keys %seen) { print STDERR "$a=$seen{$a}, "; } print STDERR "\n";
        if ($min1 lt $min2) {
          $final{$min1}{$min2} = \%seen;
        } else {
          $final{$min2}{$min1} = \%seen;
        }
      }
    }
  }
}


## Output the results
if ($opt_php) {
  print "<?\n".
  "\$alloyTable = array(\n";
  
  foreach my $min1 (@alloyNameTable) {
    print "  \"$min1\" => array(";
    foreach my $min2 (sort { $a cmp $b } keys %{$final{$min1}}) {
      print "\"$min2\" => \"".$final{$min1}{$min2}."\", ";
    }
    print "),\n";
  }
  
  print ");\n\n";
  
  # Create 'reverse' alloy table
  my %restab = ();
  foreach my $res (@alloyNameTable) {
    foreach my $min1 (keys %final) {
      foreach my $min2 (keys %{$final{$min1}}) {
        if ($final{$min1}{$min2} eq $res && !defined($restab{$res}{$min1}{$min2}) && !defined($restab{$res}{$min2}{$min1})) {
          $restab{$res}{$min1}{$min2} = 1;
        }
      }
    }
  }

  print "\$alloyRevTable = array(\n";
  foreach my $res (sort { $a cmp $b } keys %restab) {
    print "  \"$res\" => array(\n";
    foreach my $min1 (sort { $a cmp $b } keys %{$restab{$res}}) {
      if (scalar keys %{$restab{$res}{$min1}} > 0) {
        print "    \"$min1\" => array(";
        foreach my $min2 (sort { $a cmp $b } keys %{$restab{$res}{$min1}}) {
          print "\"$min2\",";
        }
        print "),\n";
      }
    }
    print "  ),\n";
  }
  
  print ");\n\n".
  "?>";
} else {
  my $num = 0;
  foreach my $min1 (sort { $a cmp $b } keys %final) {
    foreach my $min2 (sort { $a cmp $b } keys %{$final{$min1}}) {
      printf "%-35s = ", $min1." + ".$min2;
      my $n = 0;
      foreach my $res (sort { $final{$min1}{$min2}{$a} <=> $final{$min1}{$min2}{$b} } keys %{$final{$min1}{$min2}}) {
        print $res."[".$final{$min1}{$min2}{$res}."] ";
        $n++;
      }
      if ($n > 1) { print " (unstable)"; }
      print "\n";
      $num++;
    }
  }
  print "\n$num alloys total.\n";
}