<?php Header("Content-Type: text/html"); ?>

<?php
function get($name, $def)
{
  global $_GET;
  if(isset($_GET[$name])) return $_GET[$name];
  else return $def;
}

function usableBlocks1D($len, $max)
{
  // If we divide a 1x1x$len area into strips $max up to long, with at least 1 unused block between strips,
  // how many blocks are usable?

  $numStrips = ceil(($len + 1) / ($max + 1));

  if(($len % ($max + 1)) == 0)
    return $len / ($max + 1) * $max;
  else
    return $len - $numStrips + 1;
}

/*function test($len, $max, $exp)
{
  print("$len $max -> " . usableBlocks1D($len, $max) . " $exp<BR>");
}
test(1, 1, 1);
test(1, 2, 1);
test(1, 3, 1);
test(5, 1, 3);
test(6, 1, 3);
test(6, 2, 4);
test(6, 3, 5);
test(6, 4, 4);
test(6, 5, 5);
test(6, 6, 6);
test(7, 3, 6);
test(8, 3, 6); // */

function nextLowerOdd($a)
{
  if(($a % 2) == 0) $a--;
  return $a;
}

function rcBlocks1D($len, $max)
{
  // Same as above but length of each strip must be odd and at least 3
  if($len < 3) return 0;

  $max = nextLowerOdd($max);

  // Divide into strips of length $max
  $nfull = floor(($len + 1) / ($max + 1));
  $full = $nfull * $max;

  // Add excess if possible
  $excess = $len - $full - $nfull;
  if($excess >= 3) // can form another tank from the excess
    return $full + nextLowerOdd($excess);
  else if($nfull > 0 && $max > 3 && $excess > 0) // can form another tank from the excess and by shrinking one existing tank
    return $full + nextLowerOdd($excess);
  else // can't form another tank
    return $full;
}

/* function test($len, $max, $exp)
{
  print("$len $max -> " . rcBlocks1D($len, $max) . " $exp<BR>");
}

test(1, 1, 0);
test(1, 2, 0);
test(1, 3, 0);
test(5, 3, 3);
test(6, 3, 3);
test(6, 6, 5);
test(6, 7, 5);
test(11, 7, 10);
test(20, 7, 17);
test(23, 7, 21);
test(22, 7, 19);
test(21, 7, 19); // */

function rcBlocks($x, $y, $z)
{
  if($y < 4) return 0;
  return rcBlocks1D($x, 7) * usableBlocks1D($y, 8) * rcBlocks1D($z, 7);
}

function xyBlocks($x, $y, $z)
{
  return usableBlocks1D($x-2, 10) * usableBlocks1D($y-2, 10) * usableBlocks1D($z-2, 10);
}
?>

<html>

<head>
<title>Tank density calculator</title>
<style type="text/css">
body {font-family: sans-serif}
td {border-bottom: solid #CCC 1px; border-top: solid #EEE 1px; text-align: center;}
th {text-align: left;}
</style>
</head>

<body>
<!--<h1>Tank size calculator</h1>-->

<!--
<form action="tankcalc.php" method="GET">
<input type="hidden" name="mode" value="vol">
<table>
<tr><th colspan="3">How big does my tank need to be to store this much liquid?</td></tr>
<tr><td style="width:100px;"></td><td>Liquid amount (mB)</td><td><input type="text" name="volume" value="<?php print get("volume", 160000); ?>"></td></tr>
<tr><td style="width:100px;"></td><td>Max space X (blocks)</td><td><input type="text" name="x" value="<?php print get("x", 12); ?>"></td></tr>
<tr><td style="width:100px;"></td><td>Max space Y (blocks)</td><td><input type="text" name="y" value="<?php print get("y", 12); ?>"></td></tr>
<tr><td style="width:100px;"></td><td>Max space Z (blocks)</td><td><input type="text" name="z" value="<?php print get("z", 12); ?>"></td></tr>
<tr><td style="width:100px;"></td><td colspan="2"><input type="submit" value="Go"></td></tr>

<?php if(isset($_GET["mode"]) && $_GET["mode"] == "vol") {

print "Not implemented.";

} ?>

</table>
</form>-->

<form action="tankcalc.php" method="GET">
<input type="hidden" name="mode" value="space">
<table>
<tr><th colspan="3">How much liquid can I store in this space?</td></tr>
<tr><td style="width:100px;"></td><td>X size (blocks)</td><td><input type="text" name="xs" value="<?php print get("xs", 5); ?>"></td></tr>
<tr><td style="width:100px;"></td><td>Y size (blocks)</td><td><input type="text" name="ys" value="<?php print get("ys", 5); ?>"></td></tr>
<tr><td style="width:100px;"></td><td>Z size (blocks)</td><td><input type="text" name="zs" value="<?php print get("zs", 5); ?>"></td></tr>
<tr><td style="width:100px;"></td><td colspan="2"><input type="submit" value="Go"></td></tr>

<?php if(isset($_GET["mode"]) && $_GET["mode"] == "space") { ?>

<?php
$x = intval($_GET["xs"]);
$y = intval($_GET["ys"]);
$z = intval($_GET["zs"]);

$bc = max(0, $x * ($y - 3) * $z * 16);
$iron = max(0, rcBlocks($x, $y, $z) * 16);
$steel = max(0, rcBlocks($x, $y, $z) * 32);
$xy = max(0, xyBlocks($x, $y, $z) * 16);

$size = $x*$y*$z;
?>

<tr><td style="width:100px;"></td><td>Buildcraft*</td><td><?php print $bc; ?> buckets</td><td><?php print intval($bc/$size*1000);?> mB/block</td></tr>
<tr><td style="width:100px;"></td><td>Railcraft (iron)</td><td><?php print $iron; ?> buckets</td><td><?php print intval($iron/$size*1000);?> mB/block</td></tr>
<tr><td style="width:100px;"></td><td>Railcraft (steel)</td><td><?php print $steel; ?> buckets</td><td><?php print intval($steel/$size*1000);?> mB/block</td></tr>
<tr><td style="width:100px;"></td><td>Xycraft</td><td><?php print $xy; ?> buckets</td><td><?php print intval($xy/$size*1000);?> mB/block</td></tr>

<tr><td colspan="3">* Buildcraft figure allows 3 vertical layers for piping</td></tr>

<?php } ?>

</table>
</form>
</body>

</html>
