print_title("Buildings & Inventions", "All buildings, vessels and inventions currently known in the game are shown on this page. Only the details of the items you already discovered are available.");
$cmd = input_check("show", "uid", 0, "showvid", "vid", 0, "showbid", "bid", 0, "showiid", "iid", 0);
if ($cmd == "show") {
    if ($uid == "") {
        $uid = user_ourself();
    }
    print_disoveries($uid);
}
if ($cmd == "showbid") {
    building_show_details($bid, 0, 0, "");
}
if ($cmd == "showvid") {
    vessel_show_type_details($vid, 0, 0, "");
}
if ($cmd == "showiid") {
    invention_show_details($iid, 0, 0, "");
}
print_footer();
exit;
// ============================================================================================
//
//
// Description:
//
//
// Parameters:
//
//
// Returns:
//
//
function show_inventions($anomaly_id)
{
    assert(!empty($anomaly_id));
    // Get global information stuff
    $planet = anomaly_get_anomaly($anomaly_id);
    $user = user_get_user($planet['user_id']);
    // And get the ores from the planet
    $result = sql_query("SELECT * FROM g_ores WHERE planet_id=" . $anomaly_id);
    $ores = sql_fetchrow($result);
    $stock_ores = $ores['stock_ores'];
    // Get all buildings that are currently build on the planet
    $surface = planet_get_surface($anomaly_id);
    $current_buildings = csl($surface['csl_building_id']);
    $current_cargo = $surface['csl_cargo_id'];
    print_subtitle("Produceable on planet " . $planet['name']);
    // Get all items, compare wether or not we may build them...
    $result = sql_query("SELECT * FROM s_inventions ORDER BY type, id");
    while ($item = sql_fetchrow($result)) {
        // Default, we can build this
        $cannot_build = false;
        // Stage 1: Item Count Check
        if ($item['max'] > 0) {
            $times_already_build = 0;
            for ($i = 0; $i != count($current_cargo); $i++) {
                if ($current_cargo[$i] == $item['id']) {
                    $times_already_build++;
                }
            }
            // Cannot build cause we already have MAX items of this kind.. :(
            if ($times_already_build == $item['max']) {
                $cannot_build = true;
            }
        }
        // Stage 2: Dependency Check
        // Get all dependencies
        $items_needed = csl($item['csl_depends']);
        // Do we need them? If not, skip dependency-check.
        if (!empty($item['csl_depends'])) {
            $deps_found = count($items_needed);
            // Count to zero...
            while (list($key, $item_dep_id) = each($items_needed)) {
                if ($item_dep_id == "") {
                    $deps_found--;
                    continue;
                }
                // Get all dependencies
                if (in_array($item_dep_id, $current_buildings)) {
                    $deps_found--;
                    // Found in current_items?
                    // Decrease counter
                }
            }
        } else {
            // No need for deps
            $deps_found = 0;
            // Zero is good...
        }
        // Not all dependencies found, we cannot build it.. :(
        if ($deps_found > 0) {
            $cannot_build = true;
        }
        // Stage 3: Show building if we can build it..
        if ($cannot_build == false) {
            invention_show_details($item['id'], $planet['id'], $user['user_id'], $stock_ores);
        }
    }
}