Mushroom Field Simulator Source Code:
<?
header('Content-Type: text/html; charset=utf-8');

$imgs = array("dirt",
        "spore","spore","spore",
        "knoll","knob","spooky",
        "spore","spore","spore",
        "warm","cool","pointy",
        "spore","spore","spore",
        "flaming","frozen","stinky",
        "spore","moon1","moon1",
        "gloomy","moon1","moon1");

$names = array("nothing",
        "a Knoll mushroom spore","a Knob mushroom spore","a spooky mushroom spore",
        "a Knoll mushroom","a Knob mushroom","a spooky mushroom",
        "a warm mushroom spore","a cool mushroom spore","a pointy mushroom spore",
        "a warm mushroom","a cool mushroom","a pointy mushroom",
        "a flaming mushroom spore","a frozen mushroom spore","a stinky mushroom spore",
        "a flaming mushroom","a frozen mushroom","a stinky mushroom",
        "a gloomy black mushroom spore","something unknown","something unknown",
        "a gloomy black mushroom","something unknown","something unknown");

$types = array(0,1,1,1,2,2,2,1,1,1,2,2,2,1,1,1,2,2,2,1,1,1,2,2,2);

$moons1 = array(1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8);
$moons2 = array(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8);
$moondesc = array(1=>"new", 2=>"waxing crescent", 3=>"first quarter", 4=>"waxing gibbous", 5=>"full", 6=>"waning gibbous", 7=>"third quarter", 8=>"waning crescent");

// fetches a field from the posted form, trims whitespace
function getFormVar ($var, $default = '')
{
    if (isset($_REQUEST[$var]))
    {
        if (is_array($_REQUEST[$var]))
            return key($_REQUEST[$var]);
        else    return trim($_REQUEST[$var]);
    }
    else    return $default;
}
// fetches an array from the posted form
function getFormArr ($var, $default = array())
{
    if (isset($_REQUEST[$var]))
        return $_REQUEST[$var];
    else    return $default;
}

$curgen = getFormVar('curgen', 0);
$moons = getFormVar('moons', 0);
$spore = getFormVar('spore', 1);

$w = getFormVar('w', 4);
$h = getFormVar('h', 4);
$p = getFormArr('p');

for ($y = 0; $y < $h; $y++)
{
    if (!isset($p[$y]))
        $p[$y] = array();
    for ($x = 0; $x < $w; $x++)
    {
        if (!isset($p[$y][$x]))
            $p[$y][$x] = 0;
    }
}

$l = getFormVar('l');

$action = getFormVar('action');

if (strlen($l) > 0)
{
    list($lx,$ly) = explode('-', $l);

    if ($action == 'plant')
        $p[$ly][$lx] = $spore;
    if ($action == 'pick')
    {
        $oldp = $p[$ly][$lx];
        $p[$ly][$lx] = 0;
    }
}
else    $lx = $ly = 0;

if (getFormVar('phase'))
{
    $moons++;
    if ($moons >= 16)
        $moons = 0;
}

function breed ($a, $b, $moon)
{
    if ($a > $b) { $c = $a; $a = $b; $b = $c; }
    if (($a == 4) && ($b == 4))    return 1; // Knoll
    if (($a == 5) && ($b == 5))    return 2; // Knob
    if (($a == 6) && ($b == 6))    return 3; // spooky 

    if (($a == 5) && ($b == 6))    return 7; // warm
    if (($a == 4) && ($b == 5))    return 8; // cool
    if (($a == 4) && ($b == 6))    return 9; // pointy

    if (($a == 10) && ($b == 10))    return 7; // warm
    if (($a == 11) && ($b == 11))    return 8; // cool
    if (($a == 12) && ($b == 12))    return 9; // pointy

    if (($a == 10) && ($b == 12))    return 13; // flaming
    if (($a == 11) && ($b == 12))    return 14; // frozen
    if (($a == 10) && ($b == 11))    return 15; // stinky

    if (($a == 16) && ($b == 16))    return 13; // flaming
    if (($a == 17) && ($b == 17))    return 14; // frozen
    if (($a == 18) && ($b == 18))    return 15; // stinky

    if (($a == 6) && ($b == 17) && ($moon == 0))    return 19; // gloomy black
    return 0;
}
function isvalid ($x, $y)
{
    global $w, $h;
    if (($x >= 0) && ($x < $w) && ($y >= 0) && ($y < $h))
        return true;
    else    return false;
}
function getneighbor (&$neighbors, $x, $y, $p)
{
    global $types;
    if (isvalid($x,$y) && ($types[$p[$y][$x]] == 2))
        $neighbors[] = $p[$y][$x];
}
function clearcell ($x, $y, &$old, &$new)
{
    global $types;
    if (isvalid($x,$y))
    {
        if ($types[$old[$y][$x]] == 1)    // if we just wiped out a spore
        {
            $old[$y][$x] = 0;    // clear it from the old generation
            $new[$y][$x] = 0;    // so it doesn't grow up
        }
        if ($types[$old[$y][$x]] == 2)    // otherwise, only remove a mushroom from
            $new[$y][$x] = 0;    // the new generation
    }
}

if (getFormVar('evolve'))
{
    $curgen++;
    $moons++;
    if ($moons >= 16)
        $moons = 0;
    $oldp = array();
    for ($y = 0; $y < $h; $y++)
        $oldp[] = $p[$y];

    // first pass - attempt to breed mushrooms
    for ($y = 0; $y < $h; $y++)
        for ($x = 0; $x < $w; $x++)
        {
            $neighbors = array();
            // step 0 - make sure there's room for a spore
            if ($types[$oldp[$y][$x]] == 2)
                continue;    // mushroom there - skip it
            // step 1 - count neighbors
            getneighbor($neighbors,$x,$y-1,$oldp);
            getneighbor($neighbors,$x,$y+1,$oldp);
            getneighbor($neighbors,$x-1,$y,$oldp);
            getneighbor($neighbors,$x+1,$y,$oldp);
            if (count($neighbors) != 2)
                continue;
            // step 2 - make sure the recipe is valid
            $newtype = breed($neighbors[0],$neighbors[1],$moons);
            if ($newtype == 0)
                continue;
            // step 3 - destroy old mushrooms (and spores)
            clearcell($x,$y-1,$oldp,$p);
            clearcell($x,$y+1,$oldp,$p);
            clearcell($x-1,$y,$oldp,$p);
            clearcell($x+1,$y,$oldp,$p);
            // step 4 - place new spore (deleting any old spore present)
            $oldp[$y][$x] = 0;
            $p[$y][$x] = $newtype;
        }

    // second pass - cause any old spores to mature
    for ($y = 0; $y < $h; $y++)
        for ($x = 0; $x < $w; $x++)
            if ($types[$oldp[$y][$x]] == 1)
                $p[$y][$x] = $oldp[$y][$x] + 3;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Kingdom of Loathing Degrassi Knoll Mushroom Field Simulator</title>
</head>
<body>
<div>
Generation: <?=$curgen?> <form method="get" action="?"><div><input type="submit" value="Reset" /></div></form><br />
Moons: <img src="moon<?=$moons1[$moons]?>.gif" alt="<?=$moondesc[$moons1[$moons]]?>" /><img src="moon<?=$moons2[$moons]?>.gif" alt="<?=$moondesc[$moons2[$moons]]?>" /><br />
<form method="post" action="?">
<div>Actions:<br />
<label><input type="radio" name="action"<?if ($action == 'view') print ' checked="checked"';?> value="view" /> View location</label><br />
<label><input type="radio" name="action"<?if ($action == 'plant') print ' checked="checked"';?> value="plant" /> Plant</label> <select name="spore"><option value="1"<?if ($spore == '1') print ' selected="selected"';?>>Knoll</option><option value="2"<?if ($spore == '2') print ' selected="selected"';?>>Knob</option><option value="3"<?if ($spore == '3') print ' selected="selected"';?>>Spooky</option></select> spore<br />
<label><input type="radio" name="action"<?if ($action == 'pick') print ' checked="checked"';?> value="pick" /> Pick mushroom</label><br />
Field: (click a square to perform the requested action)<br />
<?
for ($y = 0; $y < $h; $y++)
    for ($x = 0; $x < $w; $x++)
    {
        print '<input type="image" src="'.$imgs[$p[$y][$x]].(($imgs[$p[$y][$x]] == 'dirt') ? rand(1,6) : '').'.gif" alt="'.$names[$p[$y][$x]].'" name="l['.$x.'-'.$y.']" />';
        if ($x == $w-1)
            print "<br />\n";
        else    print "\n";
    }

?>
<input type="submit" name="evolve" value="Rollover" />
<input type="submit" name="phase" value="Adjust moon phase" /><br />
<input type="text" name="w" value="<?=$w?>" size="1" />x<input type="text" name="h" value="<?=$h?>" size="1" /><input type="submit" value="Refresh" />
<input type="hidden" name="moons" value="<?=$moons?>" />
<input type="hidden" name="curgen" value="<?=$curgen?>" />
<?
for ($y = 0; $y < $h; $y++)
    for ($x = 0; $x < $w; $x++)
        print '<input type="hidden" name="p['.$y.']['.$x.']" value="'.$p[$y][$x].'" />'."\n";
?>
</div>
</form>
<br />
<?
if (strlen($l) > 0)
{
    if ($action == 'view')
        print "The location you selected contains ".$names[$p[$ly][$lx]].".<br />";
    if ($action == 'plant')
        print "You plant ".$names[$p[$ly][$lx]].".<br />";
    if ($action == 'pick')
        print "You pick ".$names[$oldp].".<br />";
}
?>
<br />
<br />
Kingdom of Loathing Degrassi Knoll Mushroom Field Simulator, written by Quietust (#763168)<br>
Patched for browser compatibility by JaAchan (#1861933)<br />
Based on the Kingdom of Loathing Degrassi Knoll Mushroom Fields, copyright © 2004-2012 <a href="http://asymmetric.net/">Asymmetric Publications, LLC</a><br />
For the record, Jick thinks this interface is "convoluted", and "interesting, as a toy".
</div>
</body>
</html>

Return