changeset 10:cc85bbc6d61b

Refactor code, modularize, clean up. Logic stays mostly the same, though.
author ccr@tnsp.org
date Thu, 30 Dec 2010 18:10:56 +0200
parents 71631fc7afad
children b76d8d6aed50
files index.php
diffstat 1 files changed, 310 insertions(+), 496 deletions(-) [+]
line wrap: on
line diff
--- a/index.php	Thu Dec 30 18:10:04 2010 +0200
+++ b/index.php	Thu Dec 30 18:10:56 2010 +0200
@@ -1,536 +1,350 @@
-<?php
+<?
+$dataFile = "herb.txt";
+$cacheFile = "cache2.php";
+
+
 $tick = array_sum(explode(" ", microtime()));
+
+$dbModes = array(
+  "min"  => array("Mineral", 0),
+  "org"  => array("Organ", 1),
+  "herb" => array("Herb", 2),
+);
+
+$dbNames = array(
+  array(
+    "adamantium", "aluminium", "anipium", "batium", "brass", "bronze",
+    "cesium", "chromium", "cobalt", "copper", "darksteel", "diggalite",
+    "dukonium", "duraluminium", "durandium", "electrum", "gold",
+    "graphite", "hematite", "highsteel", "illumium", "indium", "iridium",
+    "iron", "kryptonite", "lead", "magnesium", "mithril", "molybdenum",
+    "mowgles", "mowglite", "nickel", "nullium", "osmium", "palladium",
+    "pewter", "platinum", "potassium", "pyrite", "quicksilver", "rhodium",
+    "silicon", "silver", "starmetal", "steel", "tadmium", "tin", "titanium",
+    "tormium", "tungsten", "uranium", "vanadium", "zhentorium", "zinc"
+  ),
+
+  array(
+    "antenna", "arm", "beak", "bladder", "brain", "ear", "eye", "foot",
+    "gill", "heart", "horn", "kidney", "leg", "liver", "lung", "nose",
+    "paw", "snout", "spleen", "stomach", "tail", "tendril", "wing"
+  ),
+
+  array(
+    array("apple",       "wormwood"),       array("barberry",    "yarrow"),
+    array("blueberry",   "wolfbane"),       array("burdock",     "chickweed"),
+    array("cabbage",     "arnica"),         array("carrot",      "thistle"),
+    array("cauliflower", "costmary"),       array("chicory",     "borage"),
+    array("cotton",      "mysticspinach"),  array("crystalline", "jaslah"),
+    array("elder",       "honeysuckle"),    array("foxglove",    "holly"),
+    array("garlic",      "nightshade"),     array("ginseng",     "mistletoe"),
+    array("hemlock",     "tomato"),         array("henbane",     "jimsonweed"),
+    array("lettuce",     "waterlily"),      array("lobelia",     "comfrey"),
+    array("mushroom",    "mangrel"),        array("onion",       "moss"),
+    array("pear",        "boneset"),        array("plum",        "sweetflag"),
+    array("potato",      "mandrake"),       array("raspberry",   "bloodroot"),
+    array("rhubarb",     "soapwort"),       array("spinach",     "hcliz"),
+    array("strawberry",  "mugwort"),        array("turnip",      "mysticcarrot"),
+    array("vine_seed",   "lungwort")
+  ),
+);
+
+
+$dbDims = array();
+foreach ($dbNames as $key => $data)
+  $dbDims[$key] = count($data);
+
+$db = array();
+
+
+function alch_matrix_clear($val = ".")
+{
+  global $dbDims, $db;
+  
+  for ($x = 0; $x < $dbDims[0]; $x++)
+    for ($y = 0; $y < $dbDims[1]; $y++)
+      for ($z = 0; $z < $dbDims[2]; $z++)
+        $db[$x][$y][$z] = $val;
+}
+
+
+function alch_matrix_set_col_if($x, $y, $z, $idx, $ifval, $setval, $equals = TRUE)
+{
+  global $dbDims, $db;
+
+  if ($x < 0) { $col = 0; $x = &$i; } else
+  if ($y < 0) { $col = 1; $y = &$i; } else 
+              { $col = 2; $z = &$i; }
+
+  for ($i = 0; $i < $dbDims[$col]; $i++) {
+    if (($db[$x][$y][$z][$idx] == $ifval) == $equals)
+         $db[$x][$y][$z][$idx] = $setval;
+  }
+}
+
+
+function alch_find_pairs($item, $key)
+{
+  foreach ($item as $key => $val) {
+    $minTab[$val[0]]++;
+    $orgTab[$val[1]]++;
+    $herbTab[$val[2]]++;
+    
+    // why?
+    if ($val[0] == 255) $t = true;
+  }
+  
+  // why?
+  if ($t) return 0;
+
+
+  // ??? is it necessary to go through the whole array?
+  foreach ($minTab as $key => $val) if ($val > 1) $min = $key;
+  foreach ($orgTab as $key => $val) if ($val > 1) $org = $key;
+  foreach ($herbTab as $key => $val) if ($val > 1) $her = $key;
+
+  if ($min != "" && $org != "") alch_matrix_set_col_if($min, $org,   -1, 1, "", "*", TRUE);
+  if ($min != "" && $her != "") alch_matrix_set_col_if($min,   -1, $her, 1, "", "*", TRUE);
+  if ($org != "" && $her != "") alch_matrix_set_col_if(  -1, $org, $her, 1, "", "*", TRUE);
+}
+
+
+function array_search_multi($val, $arr)
+{
+  foreach ($arr as $key => $pair)
+    if ($pair[0] == $val || $pair[1] == $val)
+      return $key;
+
+  return false;
+}
+
+
+function alch_matrix_cell($r)
+{
+  $res = $r[0];
+  switch ($r[0]) {
+    case "!": $class = "accepted"; break;
+
+    case "?": $class = ($r[1] == "*") ? "pairclose" : "close"; break;
+
+    case "-": if ($r[1] == "-") $class = "nomatch";
+              else if ($r[1] == "*") $class = "pairno";
+              else if ($r[1] !== "") $class = "noclose";
+              else $class = "nomark";
+              break;
+
+    case ".": if ($r[1] == "*") $class = "pair"; 
+              $res = "&nbsp;";
+              break;
+    default: $class = "notres"; break;
+  }
+  echo "<td class=\"".$class."\" title=\"".$r."\">".$res."</td>";
+}
+
+
+function alch_matrix_print($mode, $active)
+{
+  global $dbNames, $dbModes, $db;
+
+  $pkey = $dbModes[$mode][1];
+  
+  if ($pkey == 0) { $key1 = 2; $key2 = 1; $px =  $active; $py = &$colKey; $pz = &$rowKey; } else
+  if ($pkey == 1) { $key1 = 2; $key2 = 0; $px = &$colKey; $py =  $active; $pz = &$rowKey; } else
+                  { $key1 = 1; $key2 = 0; $px = &$colKey; $py = &$rowKey; $pz =  $active; }
+
+  $pdata = $dbNames[$pkey];
+  $data1 = $dbNames[$key1];
+  $data2 = $dbNames[$key2];
+
+  // Determine number of columns by number of cells
+  $max = count($pdata);
+  if ($max / 3 > 10)
+    $div = $max / 3;
+  else
+    $div = $max / 2;
+    
+  // Print selection for primary key
+  echo "<table border=\"1\" cellspacing=\"0\">\n";
+  $n = 0;
+  foreach ($pdata as $key => $val) {
+    if ($n == 0) echo " <tr>\n";
+
+    echo "  <td class=\"".
+    ($key == $active ? "atab" : "tab").
+    "\"><a href=\"?".$mode."=".$key."\">";
+    
+    if (is_array($val))
+      echo htmlentities($val[0])."<br />".htmlentities($val[1]);
+    else 
+      echo htmlentities($val);
+
+    echo "</a></td>\n";
+
+    if (++$n >= $div) {
+      echo " </tr>\n";
+      $n = 0;
+    }
+  }
+  if ($n != 0)
+    echo " </tr>\n";
+
+
+  // Print mix table header
+  $str = is_array($pdata[0]) ? $pdata[$active][0]." or ".$pdata[$active][1] : $pdata[$active];
+
+  echo "</table>\n".
+  "<h1>".htmlentities($str)."</h1>\n".
+  "<table class=\"mixtable\" border=\"1\" cellspacing=\"0\">\n".
+  " <tr>\n".
+  "  <th></th>\n";
+
+  foreach ($data2 as $key => $val) {
+    $str = is_array($val) ? $val[0]." / ".$val[1] : $val;
+    echo "  <th><img src=\"kuva.php?text=".urlencode($str)."\" alt=\"".htmlentities($str)."\" width=\"12\" height=\"80\"></th>\n";
+  }  
+  echo " </tr>\n";
+
+  // Print the table itself
+  foreach ($data1 as $rowKey => $rowVal) {
+    $str = is_array($rowVal) ? $rowVal[0]." / ".$rowVal[1] : $rowVal;
+    echo " <tr><th>".htmlentities($str)."</th>";
+    foreach ($data2 as $colKey => $colVal) {
+      alch_matrix_cell($db[$px][$py][$pz]);
+    }
+    echo "</tr>\n";
+  }
+  echo "</table>\n";
+}
+
+
+$mode = "min";
+$active = 0;
+foreach ($dbModes as $name => $idx) {
+  if (isset($_GET[$name])) {
+    $active = intval($_GET[$name]);
+    if ($active < 0 || $active >= $dbDims[$idx[1]])
+      $active = 0;
+    $mode = $name;
+    break;
+  }
+}
+
 ?>
 <html>
  <head>
-  <title>alch tools</title>
+  <title>Potion research tool - <? echo $dbModes[$mode][0]." view (".$dbNames[$dbModes[$mode][1]][$active].")"; ?></title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <link href="alch.css" title="compact" rel="stylesheet" type="text/css">
  </head>
 <body>
-<?php
+<?
+
+alch_matrix_clear();
 
-$minerals=array("void",
-                "adamantium",
-                "aluminium",
-                "anipium",
-                "batium",
-                "brass",
-                "bronze",
-                "cesium",
-                "chromium",
-                "cobalt",
-                "copper",
-                "darksteel",
-                "diggalite",
-                "dukonium",
-                "duraluminium",
-                "durandium",
-                "electrum",
-                "gold",
-                "graphite",
-                "hematite",
-                "highsteel",
-                "illumium",
-                "indium",
-                "iridium",
-                "iron",
-                "kryptonite",
-                "lead",
-                "magnesium",
-                "mithril",
-                "molybdenum",
-                "mowgles",
-                "mowglite",
-                "nickel",
-                "nullium",
-                "osmium",
-                "palladium",
-                "pewter",
-                "platinum",
-                "potassium",
-                "pyrite",
-                "quicksilver",
-                "rhodium",
-                "silicon",
-                "silver",
-                "starmetal",
-                "steel",
-                "tadmium",
-                "tin",
-                "titanium",
-                "tormium",
-                "tungsten",
-                "uranium",
-                "vanadium",
-                "zhentorium",
-                "zinc");
+if (1)
+#if (filemtime($dataFile) > filemtime($cacheFile))
+{
+  $file = fopen($dataFile, "r");
 
-$organs=array("void",
-              "antenna",
-              "arm",
-              "beak",
-              "bladder",
-              "brain",
-              "ear",
-              "eye",
-              "foot",
-              "gill",
-              "heart",
-              "horn",
-              "kidney",
-              "leg",
-              "liver",
-              "lung",
-              "nose",
-              "paw",
-              "snout",
-              "spleen",
-              "stomach",
-              "tail",
-              "tendril",
-              "wing");
+  while (($line = fgets($file)) !== FALSE) {
+    list ($m, $o, $h, $r) = preg_split("/\s+/", trim($line), 4);
+
+    // Get indexes
+    $mn = array_search($m, $dbNames[0]);
+    $on = array_search($o, $dbNames[1]);
+    $hn = array_search_multi($h, $dbNames[2]);
 
-$herbs=array( array("void","void"),
-              array("apple","wormwood"),
-              array("barberry","yarrow"),
-              array("blueberry","wolfbane"),
-              array("burdock","chickweed"),
-              array("cabbage","arnica"),
-              array("carrot","thistle"),
-              array("cauliflower","costmary"),
-              array("chicory","borage"),
-              array("cotton","mysticspinach"),
-              array("crystalline","jaslah"),
-              array("elder","honeysuckle"),
-              array("foxglove","holly"),
-              array("garlic","nightshade"),
-              array("ginseng","mistletoe"),
-              array("hemlock","tomato"),
-              array("henbane","jimsonweed"),
-              array("lettuce","waterlily"),
-              array("lobelia","comfrey"),
-              array("mushroom","mangrel"),
-              array("onion","moss"),
-              array("pear","boneset"),
-              array("plum","sweetflag"),
-              array("potato","mandrake"),
-              array("raspberry","bloodroot"),
-              array("rhubarb","soapwort"),
-              array("spinach","hcliz"),
-              array("strawberry","mugwort"),
-              array("turnip","mysticcarrot"),
-              array("vine_seed","lungwort"));
-
-
-function multi_array_search($search_value, $the_array)
-{
-  if (is_array($the_array))
-  {
-    foreach ($the_array as $key => $value)
-    {
-      $result = multi_array_search($search_value, $value);
-      if (is_array($result))
-      {
-        $return = $result;
-        array_unshift($return, $key);
-        return $return;
-      }
-      elseif ($result == true)
-      {
-        $return[] = $key;
-        return $return;
-      }
+    if ($mn === false || $on === false || $hn === false) {
+      echo "# Invalid data: '$line'<br />\n";
+      continue;
     }
-    return false;
-  }
-  else
-  {
-    if ($search_value == $the_array)
-    {
-      return true;
-    }
-    else return false;
-  }
-}
-
-function find_pairs($item,$key)
-{
-  global $minerals,$herbs,$organs,$db;
 
-  for ($i=0;$i<count($item);$i++) 
-  {
-    $min[$item[$i][0]]++;
-    $org[$item[$i][1]]++;
-    $her[$item[$i][2]]++;
-    if ($item[$i][0]==255) $t=1;
-  }
-  if ($t==1) return 0;
-  $keys=array_keys($min);
-  for ($i=0;$i<=count($keys);$i++)
-  {
-    if ($min[$keys[$i]]>1) $mineral=$keys[$i];
-  }
+    if ($r== "-") {
+      alch_matrix_set_col_if( -1, $on, $hn, 0, "!", "-", FALSE);
+      alch_matrix_set_col_if($mn,  -1, $hn, 0, "!", "-", FALSE);
+      alch_matrix_set_col_if($mn, $on,  -1, 0, "!", "-", FALSE);
 
-  $keys=array_keys($org);
-  for ($i=0;$i<=count($keys);$i++)
-  {
-    if ($org[$keys[$i]]>1) $organ=$keys[$i];
-  }
-
-  $keys=array_keys($her);
-  for ($i=0;$i<=count($keys);$i++)
-  {
-    if ($her[$keys[$i]]>1) $herb=$keys[$i];
-  }
-  if ($mineral != "" && $organ != "")
-  {
-    for ($z=1;$z<31;$z++)
-    {
-      if ($db[$mineral][$organ][$z][1]=="")
-        $db[$mineral][$organ][$z][1]="*";
+      $db[$mn][$on][$hn][1] = "-";
     }
-  }
-  if ($mineral != "" && $herb != "")
-  {
-    for ($y=1;$y<24;$y++)
-    {
-      if ($db[$mineral][$y][$herb][1]=="")
-        $db[$mineral][$y][$herb][1]="*";
-    }
-  }
-  if ($organ != "" && $herb != "")
-  {
-    for ($x=1;$x<55;$x++)
-    {
-      if ($db[$x][$organ][$herb][1]=="")
-        $db[$x][$organ][$herb][1]="*";
-    }
-  }
-}
-
-for ($x=0;$x<55;$x++) {
-  for ($y=0;$y<24;$y++) {
-    for ($z=0;$z<31;$z++) {
-      $db[$x][$y][$z]=".";
-    }
-  }
-}
-
-if (filemtime("herb.txt") > filemtime("cache.php"))
-{
-$file = fopen("herb.txt", "r");
-while (($line = fgets($file)) !== FALSE) {
- list ($m, $o, $h, $r) = preg_split("/\s+/", trim($line), 4);
-
-#echo "'$m' : '$o' : '$h' : '$r'<br>\n";
+    else
+    if ($r[0] == "?") {
+      if ($r[1] != "?")
+        $mat[$r][] = array($mn, $on, $hn);
 
- $mn = array_search($m, $minerals);
- $on = array_search($o, $organs);
- $hn = multi_array_search($h, $herbs);
- $hn = $hn[0];
-
- if ($r=="-") {
-   for ($x=1;$x<55;$x++)
-     if ($db[$x][$on][$hn][0]!="!")
-       $db[$x][$on][$hn][0]="-";
-
-   for ($y=1;$y<24;$y++)
-     if ($db[$mn][$y][$hn][0]!="!")
-       $db[$mn][$y][$hn][0]="-";
-
-   for ($z=1;$z<31;$z++)
-     if ($db[$mn][$on][$z][0]!="!")
-       $db[$mn][$on][$z][0]="-";
-
-   $db[$mn][$on][$hn][1]="-";
- }
- if ($r[0]=="?") {
-   if ($r[1]!="?")
-     $mat[$r][]=array($mn,$on,$hn);
-   if ($db[$mn][$on][$hn][0]=="-")
-   {
-     $db[$mn][$on][$hn]=$r;
-     $db[$mn][$on][$hn][0]="-";
-   }
-   else
-   {
-     $db[$mn][$on][$hn]=$r;
-   }
- }
- if ($r[0]=="!") {
-   $t=$r;$t[0]="?";
-   $mat[$t][]=array(255,255,255);
-   $db[$mn][$on][$hn]=$r;
- }
-}
-
-array_walk($mat, "find_pairs");
-
-
+      if ($db[$mn][$on][$hn][0] == "-") {
+        $db[$mn][$on][$hn] = $r;
+        $db[$mn][$on][$hn][0] = "-";
+      } else
+        $db[$mn][$on][$hn] = $r;
+    }
+    else
+    if ($r[0] == "!") {
+      $t = $r;
+      $t[0] = "?";
+   
+      $mat[$t][] = array(255, 255, 255);
+      $db[$mn][$on][$hn] = $r;
+    }
+  } // while
 
-$cache=fopen("cache.php","w");
-$buf=serialize($db);
-fwrite($cache,$buf);
-fclose($cache);
-
-echo "Made a new cache. <br>";
-
-}else {
-
-$cache=fopen("cache.php","r");
-$buf=fread($cache,filesize("cache.php"));
-$db=unserialize($buf);
-fclose($cache);
-
-echo "Using cache made at " . date ("F d Y H:i:s.", filemtime("cache.php"))."<br>";
-
-}
-$active_herb=$_GET["herb"];
-$active_min=$_GET["min"];
-$active_org=$_GET["org"];
-
-
-$accept=0;
-$close=0;
-$unknown=0;
-$no=0;
-for ($x=1;$x<55;$x++)
-{
-  for ($y=1;$y<24;$y++)
-  {
-    for ($z=1;$z<30;$z++)
-    {
-      $r=$db[$x][$y][$z][0];
-      if ($r=="!") $accept++;
-      if ($r=="?") $close++;
-      if ($r=="-") $no++;
-      if ($r==".") $unknown++;
-    }
-  }
-}
-
-echo $accept." found potions, ".$close." active partial matches, ".$no." combinations eliminated, ".$unknown." combinations untested.\n";
-
-
-$base="herb";
-if ($active_herb!="") $base="herb";
-if ($active_min!="") $base="min";
-if ($active_org!="") $base="org";
-
-if ($base=="herb") {
+  array_walk($mat, "alch_find_pairs");
 
-  if ($active_herb==""|$active_herb<0 ||$active_herb>29) $active_herb=0;
-  echo "<table border=1 cellspacing=0>";
-  echo "<tr>";
-  for ($z=1;$z<16;$z++) {
-    if ($z==$active_herb) 
-      echo "<td class=\"atab\">";
-    else 
-      echo "<td class=\"tab\">";
-    echo "<a href=\"?herb=$z\">";
-    echo $herbs[$z][0] ."<br>". $herbs[$z][1] ."</a></td>";
-  }
-  echo "</tr>\n<tr>";
-  for ($z=16;$z<30;$z++) {
-    if ($z==$active_herb) 
-      echo "<td class=\"atab\">";
-    else 
-      echo "<td class=\"tab\">";
-    echo "<a href=\"?herb=$z\">";
-    echo $herbs[$z][0] ."<br>". $herbs[$z][1] ."</a></td>";
-  }
-  echo "</tr></table>\n";
-
-  $z=$active_herb;
-  echo ("<h1>". $herbs[$z][0] ." or ". $herbs[$z][1] ."</h1>");
-  echo ("<table border=1 cellspacing=0><tr><td></td>\n");
-  for ($x=1;$x<55;$x++) {
-    echo ("<th><img src=\"kuva.php?text=". $minerals[$x] ."\" alt=\"". $minerals[$x] ."\" width=12 height=80></th>");
-  }
-  echo ("</tr>\n");
-  for ($y=1;$y<24;$y++) {
-    echo ("<tr><th>". $organs[$y] ."</th>");
-    for ($x=1;$x<55;$x++) {
-      $r=$db[$x][$y][$z][0];
-      $class="";
-      if ($r=="!") $class="class=\"accepted\"";
-      if ($r=="?") {
-        $class="class=\"close\"";
-        if ($db[$x][$y][$z][1]=="*") $class="class=\"pairclose\"";
-      }
-      if ($r=="-") {
-        $class="class=\"nomark\"";
-        if ($db[$x][$y][$z][1]!=="") $class="class=\"noclose\"";
-        if ($db[$x][$y][$z][1]=="-") $class="class=\"nomatch\"";
-        if ($db[$x][$y][$z][1]=="*") $class="class=\"pairno\"";
-      }
-      if ($r==".") {
-        $r="&nbsp;";
-        if ($db[$x][$y][$z][1]=="*") $class="class=\"pair\"";
-      }
-      if ($r=="!"||$r=="?"||$db[$x][$y][$z][2]!="")
-        echo ("<td $class><span title=\"".$db[$x][$y][$z]."\">".$r."</span></td>");
-      else 
-        echo ("<td $class>".$r."</td>"); 
-    }
-    echo ("</tr>\n");
-  }
-  echo ("<table>");
-}
-
-if ($base=="min") {
+  $cache = @fopen($cacheFile, "w");
+  if ($cache !== false) {
+    fwrite($cache, serialize($db));
+    fclose($cache);
+    echo "Made a new cache.<br />\n";
+  } else
+    echo "Error writing cache file!<br />\n";
 
-  if ($active_min==""||$active_min<0 ||$active_min>54) $active_min=0;
-  echo "<table border=1 cellspacing=0>";
-  echo "<tr>";
-  for ($z=1;$z<19;$z++) {
-    if ($z==$active_min) 
-      echo "<td class=\"atab\">";
-    else 
-      echo "<td class=\"tab\">";
-    echo "<a href=\"?min=$z\">";
-    echo $minerals[$z] ."</a></td>";
-  }
-  echo "</tr>\n<tr>";
-  for ($z=19;$z<37;$z++) {
-    if ($z==$active_min) 
-      echo "<td class=\"atab\">";
-    else 
-      echo "<td class=\"tab\">";
-    echo "<a href=\"?min=$z\">";
-    echo $minerals[$z] ."</a></td>";
-  }
-  echo "</tr>\n<tr>";
-  for ($z=37;$z<55;$z++) {
-    if ($z==$active_min) 
-      echo "<td class=\"atab\">";
-    else 
-      echo "<td class=\"tab\">";
-    echo "<a href=\"?min=$z\">";
-    echo $minerals[$z] ."</a></td>";
-  }
-  echo "</tr></table>\n";
-  $z=$active_min;
-  echo ("<h1>". $minerals[$z] ."</h1>\n");
-  echo ("<table border=1 cellspacing=0><tr><td></td>");
-  for ($x=1;$x<24;$x++) {
-    echo ("<th><img src=\"kuva.php?text=". $organs[$x] ."\" alt=\"". $organs[$x] ."\" width=12 height=80></th>");
-  }
-  echo ("</tr>\n");
-  for ($y=1;$y<30;$y++) {
-    echo ("<tr><th>". $herbs[$y][0] ." / ".$herbs[$y][1]."</th>");
-    for ($x=1;$x<24;$x++) {
-      $r=$db[$z][$x][$y][0];
-      $class="";
-      if ($r=="!") $class="class=\"accepted\"";
-      if ($r=="?") {
-        $class="class=\"close\"";
-        if ($db[$z][$x][$y][1]=="*") $class="class=\"pairclose\"";
-      }
-      if ($r=="-") {
-        $class="class=\"nomark\"";
-        if ($db[$z][$x][$y][1]!=="") $class="class=\"noclose\"";
-        if ($db[$z][$x][$y][1]=="-") $class="class=\"nomatch\"";
-        if ($db[$z][$x][$y][1]=="*") $class="class=\"pairno\"";
-      }
-      if ($r==".") {
-        $r="&nbsp;";
-        if ($db[$z][$x][$y][1]=="*") $class="class=\"pair\"";
-      }
-      if ($r=="!"||$r=="?"||$db[$z][$x][$y][2]!="")
-        echo ("<td $class><span title=\"".$db[$z][$x][$y]."\">".$r."</span></td>");
-      else 
-        echo ("<td $class>".$r."</td>"); 
+} else {
+
+  $cache = @file_get_contents($cacheFile);
 
-    }
-    echo ("</tr>\n");
-  }
-  echo ("<table>\n");
+  if ($cache !== false)
+    $db = unserialize($cache);
+
+  if (isset($db) && $db !== false)
+    echo "Using cache made at " . date ("F d Y H:i:s.", filemtime($cacheFile))."<br>";
 }
 
 
-if ($base=="org") {
-
-  if ($active_org==""|$active_org<0 ||$active_org>28) $active_org=0;
-  echo "<table border=1 cellspacing=0>";
-  echo "<tr>";
-  for ($z=1;$z<13;$z++) {
-    if ($z==$active_org) 
-      echo "<td class=\"atab\">";
-    else 
-      echo "<td class=\"tab\">";
-    echo "<a href=\"?org=$z\">";
-    echo $organs[$z] ."</a></td>";
-  }
-  echo "</tr><tr>";
-  for ($z=13;$z<24;$z++) {
-    if ($z==$active_org) 
-      echo "<td class=\"atab\">";
-    else 
-      echo "<td class=\"tab\">";
-    echo "<a href=\"?org=$z\">";
-    echo $organs[$z]."</a></td>";
-  }
-  echo "</tr></table>";
+$results = array();
 
-  $z=$active_org;
-  echo ("<h1>". $organs[$z] ."</h1>");
-  echo ("<table border=1 cellspacing=0><tr><td></td>");
-  for ($x=1;$x<55;$x++) {
-    echo ("<th><img src=\"kuva.php?text=". $minerals[$x] ."\" alt=\"". $minerals[$x] ."\" width=12 height=80></th>");
-  }
-  echo ("</tr>");
-  for ($y=1;$y<30;$y++) {
-    echo ("<tr><th>". $herbs[$y][0]." / ".$herbs[$y][1] ."</th>");
-    for ($x=1;$x<55;$x++) {
-      $r=$db[$x][$z][$y][0];
-      $class="";
-      if ($r=="!") $class="class=\"accepted\"";
-      if ($r=="?") {
-        $class="class=\"close\"";
-        if ($db[$x][$z][$y][1]=="*") $class="class=\"pairclose\"";
-      }
-      if ($r=="-") {
-        $class="class=\"nomark\"";
-        if ($db[$x][$z][$y][1]!=="") $class="class=\"noclose\"";
-        if ($db[$x][$z][$y][1]=="-") $class="class=\"nomatch\"";
-        if ($db[$x][$z][$y][1]=="*") $class="class=\"pairno\"";
-      }
-      if ($r==".") {
-        $r="&nbsp;";
-        if ($db[$x][$z][$y][1]=="*") $class="class=\"pair\"";
-      }
-      if ($r=="!"||$r=="?"||$db[$x][$z][$y][2]!="")
-        echo ("<td $class><span title=\"".$db[$x][$z][$y]."\">".$r."</span></td>");
-      else 
-        echo ("<td $class>".$r."</td>"); 
+for ($x = 0; $x < $dbDims[0]; $x++)
+for ($y = 0; $y < $dbDims[1]; $y++)
+for ($z = 0; $z < $dbDims[2]; $z++)
+  $results[$db[$x][$y][$z][0]]++;
 
-    }
-    echo ("</tr>");
-  }
-  echo ("<table>");
-}
+echo $results["!"]." found potions, ".
+  $results["?"]." active partial matches, ".
+  $results["-"]." combinations eliminated, ".
+  $results["."]." combinations untested.<br />\n";
 
 
+alch_matrix_print($mode, $active,
+  $dbModes[$mode]["pkey"],
+  $dbModes[$mode]["key1"],
+  $dbModes[$mode]["key2"]);
 ?>
-<br>
-Change index key to [<a href="?org=1">organs</a>] [<a href="?min=1">minerals</a>] [<a href="?herb=1">herbs</a>]
-<br>
-<br>
+
+<br />
+Change index key to [<a href="?org=0">organs</a>] [<a href="?min=0">minerals</a>] [<a href="?herb=0">herbs</a>]
+<br />
+<br />
 <table border="1" cellspacing="1">
  <tr><th colspan="2" class="noframe">Legend</th></tr>
- <tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><th class="noframe">Not researched yet</th></tr>
+ <tr><td width="10%" class="notres">&nbsp;</td><th class="noframe">Not researched yet</th></tr>
  <tr><td class="nomatch">-</td><th class="noframe">No match</th></tr>
  <tr><td class="nomark">-</td><th class="noframe">Marked to no potion by another no match.</th></tr>
  <tr><td class="noclose">-</td><th class="noframe">2 materials match, marked as no potion by another no match.</th></tr>
  <tr><td class="close">?</td><th class="noframe">2 materials match</th></tr>
- <tr><td class="acceptex">!</td><th class="noframe">Accepted potion</th></tr>
+ <tr><td class="accepted">!</td><th class="noframe">Accepted potion</th></tr>
  <tr><td class="pair">&nbsp</td><th class="noframe">Known to be possible potion by other attempts</th></tr>
 </table>
-<?php
+<?
 $tock = array_sum(explode(" ",microtime()));
-echo "Page took " . round(($tock - $tick),2) . " seconds to process.<br>";
+echo "Page took " . round(($tock - $tick),2) . " seconds to process.<br />";
 ?>
 </body>
 </html>