function export()
{
    header('Content-Type: text/plain');
    if (empty($_REQUEST['request']) || $_REQUEST['request'] != 'round' && empty($_REQUEST['round_id'])) {
        exit('Empty query');
    }
    $request = request_variable('request');
    $round_id = abs((int) request_variable('round_id'));
    if (!in_array($request, array('kingdom', 'player', 'round'))) {
        exit('Invalid request: round, kingdom, or player');
    }
    if ($request != 'round' && empty($round_id)) {
        exit('Invalid round id');
    }
    $sql = new SQL_Generator();
    if ($request == 'round') {
        $sql->select(array(array('rounds', 'round_id'), array('rounds', 'name'), array('rounds', 'starttime'), array('rounds', 'stoptime')));
        $sql->where(array('rounds', 'public', 1));
    } else {
        $sql->select(array($request . 's', $request . '_id'));
        if ($request == 'player') {
            $sql->select(array($request . 's', 'kingdom_id'));
        }
        $sql->select(array(array($request . 's', 'name'), array($request . 's', 'score'), array($request . 's', 'score_peak')));
        $sql->where(array($request . 's', 'round_id', $round_id));
    }
    $sql->orderby(array($request . 's', $request . '_id', 'asc'));
    $db_result = $sql->execute();
    while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
        $output = implode(',', $db_row) . ",\n";
        if (substr_count($output, ',') > count($db_row)) {
            $output = '';
            $multiple = false;
            foreach ($db_row as $value) {
                if ($multiple) {
                    $output .= ',';
                } else {
                    $multiple = true;
                }
                $output .= str_replace(',', '', $value);
            }
            $output .= ",\n";
        }
        echo $output;
    }
    exit;
}
 function Propositions()
 {
     global $data, $smarty;
     $this->data =& $data;
     $this->smarty =& $smarty;
     $this->sql = new SQL_Generator();
     $player = $this->data->player($_SESSION['player_id']);
     if ($player['rank'] < RANK_SENATOR) {
         $this->access_error();
     }
     if (!empty($_REQUEST['proposition_id'])) {
         $this->proposition_id = abs((int) $_REQUEST['proposition_id']);
         $this->proposition =& $this->data->proposition($this->proposition_id);
         if (empty($this->proposition) || $this->proposition['kingdom_id'] != $_SESSION['kingdom_id']) {
             $this->access_error();
         }
     } else {
         $this->statement = request_variable('statement', NULL, '');
         $this->proposition_id = 0;
     }
 }
 function upgrade()
 {
     $planet_id = $this->planet_id;
     if (isset($_POST['mode']) && $_POST['mode'] == 'js') {
         $output_mode = 'javascript';
     } else {
         $output_mode = '';
     }
     permissions_check(PERMISSION_PLANET, $planet_id, 'research');
     $designs['main'] = array(0 => 'army', 1 => 'navy', 2 => 'weapon');
     foreach ($designs['main'] as $key => $value) {
         if (!empty($_POST[$value . 'design_id'])) {
             $design_id = (int) $_POST[$value . 'design_id'];
             $design_name = $value;
             $type = $key;
             break;
         }
     }
     $attribute = $_POST['attribute'];
     if (empty($design_name) || empty($attribute)) {
         $status[] = 'No upgrade selected.';
         if ($output_mode == 'javascript') {
             echo 'alert(\'' . implode("\n", $status) . '\'); varUpgrading = false; varError = true;';
             exit;
         }
         $this->smarty->append('status', $status);
         $this->overview();
         exit;
     }
     if ($attribute != mysql_real_escape_string($attribute)) {
         error(__FILE__, __LINE__, 'DATA_INVALID', 'Invalid attribute selected');
     }
     $db_query = "\n\t\t\t\tSELECT \n\t\t\t\t\t`time`, \n\t\t\t\t\t`workers`, \n\t\t\t\t\t`energy`, \n\t\t\t\t\t`minerals`, \n\t\t\t\t\t`mineralspread`, \n\t\t\t\t\t`techlevel_current`, \n\t\t\t\t\t`techlevel_max`, \n\t\t\t\t\t`" . $attribute . "_base`, \n\t\t\t\t\t`" . $attribute . "_max`, \n\t\t\t\t\t`" . $attribute . "_inc`, \n\t\t\t\t\t`" . $attribute . "_per`, \n\t\t\t\t\t`" . $attribute . "_size`, \n\t\t\t\t\t`" . $attribute . "_sizeinc`, \n\t\t\t\t\t`size_base`, \n\t\t\t\t\t`size_max` \n\t\t\t\tFROM \n\t\t\t\t\t`" . $design_name . "designs` \n\t\t\t\tWHERE \n\t\t\t\t\t`kingdom_id` = '" . $_SESSION['kingdom_id'] . "' AND \n\t\t\t\t\t`" . $design_name . "design_id` = '" . $design_id . "' \n\t\t\t\tLIMIT 1";
     $db_result = mysql_query($db_query);
     if (mysql_num_rows($db_result) == 0) {
         error(__FILE__, __LINE__, 'DATA_INVALID', 'Invalid design selected');
     }
     $design = mysql_fetch_array($db_result, MYSQL_ASSOC);
     $design['mineralspread'] = unserialize($design['mineralspread']);
     if ($design['techlevel_current'] >= $design['techlevel_max']) {
         $status[] = 'The tech level has been maxed out.';
     }
     if ($design[$attribute . '_base'] == $design[$attribute . '_max']) {
         $status[] = 'That attribute has been maxed out.';
     }
     if (!empty($status)) {
         if ($output_mode == 'javascript') {
             echo 'alert(\'' . implode("\n", $status) . '\'); varUpgrading = false; varError = true;';
             exit;
         }
         $this->smarty->append('status', $status);
         $this->overview();
         exit;
     }
     $db_query = "SELECT `planet_id` FROM `tasks` WHERE `kingdom_id` = '" . $_SESSION['kingdom_id'] . "' AND `type` IN ('2', '3') AND (`planet_id` = '" . $planet_id . "' OR `design_id` = '" . $design_id . "') LIMIT 2";
     $db_result = mysql_query($db_query);
     while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
         if ($db_row['planet_id'] == $planet_id) {
             $status[] = 'That planet is already researching something.';
         } else {
             $status[] = 'That design is already being researched elsewhere';
         }
     }
     if (!empty($status)) {
         if ($output_mode == 'javascript') {
             echo 'alert(\'' . implode("\n", $status) . '\'); varUpgrading = false; varError = true;';
             exit;
         }
         $this->smarty->append('status', $status);
         $this->overview();
         exit;
     }
     $db_query = "SELECT `player_id`, `workers`, `energy`, `minerals`, `researchbonus` FROM `planets` WHERE `planet_id` = '" . $planet_id . "' LIMIT 1";
     $db_result = mysql_query($db_query);
     $planet = mysql_fetch_array($db_result, MYSQL_ASSOC);
     $planet['minerals'] = unserialize($planet['minerals']);
     $resources = array('workers', 'energy');
     foreach ($resources as $value) {
         $planet[$value] -= $design[$value];
         if ($planet[$value] < 0) {
             $status[] = 'Not enough ' . $value . '.';
         }
     }
     if (!empty($design['mineralspread'])) {
         $mineralnames = array(0 => 'fe', 1 => 'o', 2 => 'si', 3 => 'mg', 4 => 'ni', 5 => 's', 6 => 'he', 7 => 'h');
         foreach ($design['mineralspread'] as $key => $value) {
             $mineral = $value / 100 * $design['minerals'];
             $planet['minerals'][$key] -= $mineral;
             if ($planet['minerals'][$key] < 0) {
                 $status[] = 'Not enough ' . $mineralnames[$key] . '.';
             }
         }
     }
     if (!empty($status)) {
         if ($output_mode == 'javascript') {
             echo 'alert(\'' . implode("\n", $status) . '\'); varUpgrading = false; varError = true;';
             exit;
         }
         $this->smarty->append('status', $status);
         $this->overview();
         exit;
     }
     $completion = $design['time'] * $_SESSION['round_speed'] * ((100 - $planet['researchbonus']) / 100);
     $warptime = request_variable('warptime');
     if (!is_null($warptime)) {
         data::initialize();
         $data_planet =& $this->data->planet($planet_id);
         if ($data_planet['warptime_research'] > $completion) {
             $data_planet['warptime_research'] -= $completion;
             $completion = 0;
         } else {
             $completion -= $data_planet['warptime_research'];
             $data_planet['warptime_research'] = 0;
         }
         $this->data->save();
     }
     $insert_design = array('round_id' => $_SESSION['round_id'], 'kingdom_id' => $_SESSION['kingdom_id'], 'planet_id' => $planet_id, 'type' => 3, 'design_id' => $design_id, 'attribute' => $attribute, 'number' => $type, 'start' => microfloat(), 'completion' => microfloat() + $completion);
     $db_result = $this->sql->execute('tasks', $insert_design);
     $this->sql->set(array(array('planets', 'researching', 1), array('planets', 'workers', $planet['workers']), array('planets', 'energy', $planet['energy']), array('planets', 'minerals', serialize($planet['minerals']))));
     $this->sql->where(array('planets', 'planet_id', $planet_id));
     $this->sql->limit(1);
     $db_result = $this->sql->execute();
     if ($output_mode == 'javascript') {
         echo 'varUpgrading = true; varError = false;';
         exit;
     }
     $_SESSION['status'][] = 'Upgrade successfully started.';
     redirect('designs.php');
 }
 function commission()
 {
     if (isset($_POST['mode']) && $_POST['mode'] == 'js') {
         $this->output_mode = 'javascript';
     } else {
         $this->output_mode = '';
     }
     $units = abs((int) request_variable('units', 'post'));
     if (empty($units) || empty($this->unit_id) || empty($this->unit_type)) {
         $status[] = 'Must commission at least one unit.';
     }
     if ($this->commission_errorcheck($status)) {
         return;
     }
     $planet = $this->data->planet($this->planet_id);
     $unit = $this->data->blueprint($this->unit_type, $this->unit_id);
     if ($unit['kingdom_id'] != $planet['kingdom_id']) {
         $status[] = 'You do not own the blueprint to this unit.';
     }
     if ($this->commission_errorcheck($status)) {
         return;
     }
     if (empty($planet['production'][$this->unit_type][$unit[$this->unit_type . 'concept_id']])) {
         $status[] = 'No buildings to produce this unit.';
     }
     $resources = array('workers', 'energy');
     foreach ($resources as $value) {
         if ($planet[$value] - $unit[$value] * $units < 0) {
             $status[] = 'Not enough ' . $value . '.';
         }
     }
     if (!empty($unit['mineralspread'])) {
         $mineralnames = unserialize(MINERALS_ARRAY);
         foreach ($unit['mineralspread'] as $key => $value) {
             if ($planet['minerals'][$key] - $value / 100 * $unit['minerals'] * $units < 0) {
                 $status[] = 'Not enough ' . $mineralnames[$key] . '.';
             }
         }
     }
     if ($this->commission_errorcheck($status)) {
         return;
     }
     $planet =& $this->data->planet($this->planet_id);
     $resources = array('workers', 'energy');
     foreach ($resources as $value) {
         $planet[$value] -= $unit[$value] * $units;
     }
     if (!empty($unit['mineralspread'])) {
         foreach ($unit['mineralspread'] as $key => $value) {
             $planet['minerals'][$key] -= $value / 100 * $unit['minerals'] * $units;
         }
     }
     $completion = $units * ($unit['time'] * $_SESSION['round_speed']) / $planet['production'][$this->unit_type][$unit[$this->unit_type . 'concept_id']];
     $warptime = request_variable('warptime');
     if (!is_null($warptime) && $planet['player_id'] == $_SESSION['player_id']) {
         if ($planet['warptime_construction'] > $completion) {
             $planet['warptime_construction'] -= $completion;
             $completion = 0;
         } else {
             $completion -= $planet['warptime_construction'];
             $planet['warptime_construction'] = 0;
         }
     }
     $now = microfloat();
     $db_query = "SELECT `completion` FROM `tasks` WHERE `planet_id` = '" . $this->planet_id . "' AND `type` = '" . TASK_UNIT . "' AND `attribute` = '" . $this->unit_type . "' AND `planning` = '" . $unit[$this->unit_type . 'concept_id'] . "' ORDER BY `completion` DESC LIMIT 1";
     $db_result = mysql_query($db_query);
     if (mysql_num_rows($db_result) > 0) {
         $db_row = mysql_fetch_array($db_result, MYSQL_ASSOC);
         $start = $db_row['completion'];
     } else {
         $start = $now;
     }
     // Add the task.
     $task_insert = array('round_id' => $_SESSION['round_id'], 'kingdom_id' => $_SESSION['kingdom_id'], 'planet_id' => $this->planet_id, 'type' => TASK_UNIT, 'unit_id' => $this->unit_id, 'attribute' => $this->unit_type, 'number' => $units, 'planning' => $unit[$this->unit_type . 'concept_id'], 'completion' => $start + $completion, 'start' => $start);
     $this->sql->execute('tasks', $task_insert);
     $this->data->save();
     if ($output_mode == 'javascript') {
         echo 'varCommissioned = true; varError = false;';
         exit;
     }
     $this->smarty->append('status', 'The unit is being commissioned.');
     $this->overview();
     exit;
 }
 function process_create()
 {
     $player = $this->data->player($_SESSION['player_id']);
     if ($player['rank'] < RANK_COMMANDER) {
         $this->smarty->append('status', 'Insufficient rank to create military groups.');
         $this->create();
         exit;
     }
     $planet_id = abs((int) request_variable('planet_id', 'post', 0));
     $name = request_variable('name', 'post', '');
     if (empty($planet_id)) {
         $status[] = 'Must select a planet for group.';
     } else {
         $permission = permissions_check(PERMISSION_PLANET, $planet_id, 'military', false);
         if ($permission['military'] == false) {
             $status[] = 'You do not have permission to create groups on that planet';
         }
     }
     if ($error = str_check($name, array(3, 20, REGEXP_NAME_PLANET))) {
         $status[] = 'Group name error: ' . implode(' ', $error) . '<br />';
     }
     if (!empty($status)) {
         $this->smarty->append('status', $status);
         $this->create();
         exit;
     }
     $planet = $this->data->planet($planet_id);
     $insert_group = array('round_id' => $_SESSION['round_id'], 'kingdom_id' => $_SESSION['kingdom_id'], 'player_id' => $planet['player_id'], 'planet_id' => $planet['planet_id'], 'name' => $name, 'units' => array());
     if ($this->group_type == 'navy') {
         $this->sql->select(array(array('quadrants', 'x', 'x_quadrant'), array('quadrants', 'y', 'y_quadrant'), array('starsystems', 'x', 'x_starsystem'), array('starsystems', 'y', 'y_starsystem')));
         $this->sql->where(array(array('starsystems', 'starsystem_id', $planet['starsystem_id']), array('quadrants', 'quadrant_id', $planet['quadrant_id'])));
         $db_result = $this->sql->execute();
         $db_row = mysql_fetch_array($db_result, MYSQL_ASSOC);
         $x = $db_row['x_quadrant'] * 49 + $db_row['x_starsystem'] * 7 + $planet['x'];
         $y = $db_row['y_quadrant'] * 49 + $db_row['y_starsystem'] * 7 + $planet['y'];
         $insert_group = $insert_group + array('x_current' => $x, 'y_current' => $y, 'x_destination' => $x, 'y_destination' => $y, 'cargo' => array());
     }
     $this->sql->execute($this->group_type . 'groups', $insert_group);
     $this->group_id = mysql_insert_id();
     redirect('groups.php?fn=groups_view&group_id=' . $this->group_id . '&group_type=' . $this->group_type);
 }
 function research()
 {
     // FIXME
     $concept_id = $this->concept_id;
     $planet_id = $this->planet_id;
     if (isset($_POST['mode']) && $_POST['mode'] == 'js') {
         $output_mode = 'javascript';
     } else {
         $output_mode = '';
     }
     $this->output_mode = $output_mode;
     $status = array();
     permissions_check(PERMISSION_PLANET, $planet_id, 'research');
     $kingdom =& $this->data->kingdom($_SESSION['kingdom_id']);
     if (!isset($kingdom['concepts'][$concept_id])) {
         $status[] = 'The concept you selected does not exist, has already been researched, or is outside of your grasp.';
         $this->research_checkerror($status);
     }
     $db_query = "SELECT `planet_id` FROM `tasks` WHERE `kingdom_id` = '" . $_SESSION['kingdom_id'] . "' AND `type` IN ('" . TASK_RESEARCH . "', '" . TASK_UPGRADE . "') AND (`planet_id` = '" . $planet_id . "' OR `concept_id` = '" . $concept_id . "') LIMIT 2";
     $db_result = mysql_query($db_query);
     while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
         if ($db_row['planet_id'] == $planet_id) {
             $status[] = 'That planet is already researching something.';
         } else {
             $status[] = 'That concept is already being researched elsewhere';
         }
     }
     $this->research_checkerror($status);
     $planet =& $this->data->planet($planet_id);
     $concept = $this->data->concept($concept_id);
     $score = 0;
     $resources = array('workers' => SCORE_WORKERS, 'energy' => SCORE_ENERGY);
     foreach ($resources as $key => $value) {
         $score += $concept[$key] * $value;
         $planet[$key] -= $concept[$key];
         $kingdom[$key] -= $concept[$key];
         if ($planet[$key] < 0) {
             $status[] = 'Not enough ' . $key . '.';
         }
     }
     if (!empty($concept['mineralspread'])) {
         $mineralnames = array(0 => 'fe', 1 => 'o', 2 => 'si', 3 => 'mg', 4 => 'ni', 5 => 's', 6 => 'he', 7 => 'h');
         foreach ($concept['mineralspread'] as $key => $value) {
             $mineral = $value / 100 * $concept['minerals'];
             $score += $mineral * SCORE_MINERALS;
             $planet['minerals'][$key] -= $mineral;
             if ($planet['minerals'][$key] < 0) {
                 $status[] = 'Not enough ' . $mineralnames[$key] . '.';
             }
         }
         $kingdom['minerals'] -= $concept['minerals'];
     }
     $this->research_checkerror($status);
     $player =& $this->data->player($planet['player_id']);
     $research_bonus = $planet['researchbonus'] < 85 ? $planet['researchbonus'] : 85;
     $completion = $concept['time'] * $_SESSION['round_speed'] * ((100 - $research_bonus) / 100);
     $planet['researching']++;
     $planet['score'] -= $score;
     $player['score'] -= $score;
     $warptime = request_variable('warptime');
     if (!is_null($warptime)) {
         if ($planet['warptime_research'] > $completion) {
             $planet['warptime_research'] -= $completion;
             $completion = 0;
         } else {
             $completion -= $planet['warptime_research'];
             $planet['warptime_research'] = 0;
         }
     }
     $now = microfloat();
     $task_insert = array('round_id' => $_SESSION['round_id'], 'kingdom_id' => $_SESSION['kingdom_id'], 'player_id' => $planet['player_id'], 'planet_id' => $planet['planet_id'], 'type' => TASK_RESEARCH, 'concept_id' => $this->concept_id, 'completion' => $now + $completion, 'start' => $now);
     $this->sql->execute('tasks', $task_insert);
     $this->data->save();
     if ($output_mode == 'javascript') {
         echo 'varResearching = true; varError = false;';
         exit;
     }
     if ($completion == 0) {
         $_SESSION['status'][] = 'Research successfully completed.';
     } else {
         $_SESSION['status'][] = 'Research successfully started.';
     }
     redirect('research.php');
 }
 function post_process()
 {
     $subject = trim(request_variable('subject', NULL, ''));
     $message = trim(request_variable('message', NULL, ''));
     $forum_topic_id = abs((int) request_variable('forum_topic_id', NULL, 0));
     if (empty($_REQUEST['message'])) {
         $this->smarty->append('status', 'No message entered.');
         if ($forum_topic_id == 0) {
             $this->smarty->assign('subject', htmlentities($subject));
             $this->topics();
         } else {
             $this->messages();
         }
         exit;
     }
     if ($forum_topic_id == 0) {
         if (strlen($subject) < 3 || strlen($subject) > 64) {
             $this->smarty->assign('message', htmlentities($message));
             $this->smarty->assign('subject', htmlentities($subject));
             $this->smarty->append('status', 'No subject entered.');
             $this->topics();
             exit;
         } else {
             $topicinsert = array('round_id' => $_SESSION['round_id'], 'kingdom_id' => $_SESSION['kingdom_id'], 'lastposter_id' => $_SESSION['player_id'], 'subject' => $subject, 'time_lastpost' => microfloat());
             $db_query = $this->sql->insert('forum_topics', $topicinsert);
             $db_result = mysql_query($db_query);
             $forum_topic_id = mysql_insert_id();
         }
     } else {
         $db_query = "SELECT * FROM `forum_topics` WHERE `forum_topic_id` = '" . $forum_topic_id . "' AND `kingdom_id` = '" . $_SESSION['kingdom_id'] . "' LIMIT 1";
         $db_result = mysql_query($db_query);
         if (!$db_result || mysql_num_rows($db_result) == 0) {
             error(__FILE__, __LINE__, 'DB_DATA', 'Could not select valid forum_topic_id');
         }
         $db_row = mysql_fetch_array($db_result, MYSQL_ASSOC);
         $db_query = "\n\t\t\t\t\tUPDATE `forum_topics` \n\t\t\t\t\tSET \n\t\t\t\t\t\t`lastposter_id` = '" . $_SESSION['player_id'] . "', \n\t\t\t\t\t\t`replies` = `replies` + '1', \n\t\t\t\t\t\t`time_lastpost` = '" . microfloat() . "' \n\t\t\t\t\tWHERE `forum_topic_id` = '" . $forum_topic_id . "' \n\t\t\t\t\tLIMIT 1";
         $db_result = mysql_query($db_query);
     }
     $messageinsert = array('forum_topic_id' => $forum_topic_id, 'round_id' => $_SESSION['round_id'], 'kingdom_id' => $_SESSION['kingdom_id'], 'poster_id' => $_SESSION['player_id'], 'message' => $message, 'posttime' => microfloat(), 'marked' => array());
     $db_result = $this->sql->execute('forum_messages', $messageinsert);
     $db_query = "\n\t\t\t\tUPDATE `players` \n\t\t\t\tSET `forum` = '1' \n\t\t\t\tWHERE \n\t\t\t\t\t`round_id` = '" . $_SESSION['round_id'] . "' AND \n\t\t\t\t\t`kingdom_id` = '" . $_SESSION['kingdom_id'] . "' AND \n\t\t\t\t\t`player_id` <> '" . $_SESSION['player_id'] . "' AND \n\t\t\t\t\t`rank` > '0'";
     $db_result = mysql_query($db_query);
     $this->smarty->append('status', 'Message posted.');
     $this->messages();
     exit;
 }
 function build()
 {
     $status = array();
     $cranes = abs((int) $_POST['cranes']);
     $planning = abs((int) $_POST['planning']);
     if (isset($_POST['mode']) && $_POST['mode'] == 'js') {
         $output_mode = 'javascript';
     } else {
         $output_mode = '';
     }
     $this->output_mode = $output_mode;
     $planet = $this->data->planet($this->planet_id);
     $kingdom = $this->data->kingdom($planet['kingdom_id']);
     $building = $this->data->building($this->building_id);
     // check construction requirements {
     if ($cranes == 0 || $planning == 0) {
         $status[] = 'Must use at least one crane and planning facility.';
     } elseif ($planet['cranes'] < $cranes || $planet['planning'] < $planning) {
         $status[] = 'Not enough available cranes or planning buildings.';
     }
     if ($planning > 3) {
         $planning = 3;
     }
     if ($cranes > 15) {
         $cranes = 15;
     }
     if (!isset($kingdom['buildings'][$this->building_id])) {
         $status[] = 'You do not have the ability to create that building.';
     }
     if ($this->build_checkerror($status)) {
         return;
     }
     if ($building['maxbuildable'] > 0) {
         $db_query = "\n\t\t\t\t\tSELECT SUM(`number` * `planning`) as 'currentlybuilding' \n\t\t\t\t\tFROM `tasks` \n\t\t\t\t\tWHERE \n\t\t\t\t\t\t`type` = '" . TASK_BUILD . "' AND \n\t\t\t\t\t\t`building_id` = '" . $this->building_id . "' AND \n\t\t\t\t\t\t`planet_id` = '" . $this->planet_id . "'";
         $db_result = mysql_query($db_query);
         $db_row = mysql_fetch_array($db_result, MYSQL_ASSOC);
         if (!isset($planet['buildings'][$this->building_id])) {
             $planet['buildings'][$this->building_id] = 0;
         }
         if ($db_row['currentlybuilding'] + $planet['buildings'][$this->building_id] + $cranes * $planning > $building['maxbuildable']) {
             $status[] = 'Cannot build more than ' . $building['maxbuildable'] . ' of this building.';
         }
     }
     if ($this->build_checkerror($status)) {
         return;
     }
     // }
     // calculate and check resource requirements {
     $resources = array('workers', 'energy');
     $resource_values = array();
     foreach ($resources as $value) {
         $resource_values[$value] = $building[$value] * $cranes * $planning;
         if ($planet[$value] - $resource_values[$value] < 0) {
             $status[] = 'Not enough ' . $value . '.';
         }
     }
     if (!empty($building['mineralspread'])) {
         $mineralnames = array(0 => 'fe', 1 => 'o', 2 => 'si', 3 => 'mg', 4 => 'ni', 5 => 's', 6 => 'he', 7 => 'h');
         $mineral_values = array();
         foreach ($building['mineralspread'] as $key => $value) {
             if ($value <= 0) {
                 continue;
             }
             $mineral_values[$key] = $value / 100 * $building['minerals'] * $cranes * $planning;
             if ($planet['minerals'][$key] - $mineral_values[$key] < 0) {
                 $status[] = 'Not enough ' . $mineralnames[$key] . '.';
             }
         }
         $kingdom['minerals'] -= $building['minerals'];
     }
     if ($this->build_checkerror($status)) {
         return;
     }
     // }
     // deduct required resources {
     // Make data writable before making changes
     $planet =& $this->data->planet($this->planet_id);
     $kingdom =& $this->data->kingdom($planet['kingdom_id']);
     foreach ($resources as $value) {
         $planet[$value] -= $resource_values[$value];
         $kingdom[$value] -= $resource_values[$value];
     }
     if (!empty($building['mineralspread'])) {
         foreach ($building['mineralspread'] as $key => $value) {
             if ($value <= 0) {
                 continue;
             }
             $planet['minerals'][$key] -= $mineral_values[$key];
         }
     }
     $planet['cranes'] -= $cranes;
     // }
     // calculate start and completion time {
     // Someone has found a way to force the building bonus to go higher than it should be. Stop-gap it here. Luser.
     $building_bonus = $planet['buildingbonus'] < 75 ? $planet['buildingbonus'] : 75;
     $completion = $planning * ($building['time'] * $_SESSION['round_speed']) * ((100 - $building_bonus) / 100);
     $now = microfloat();
     $warptime = request_variable('warptime');
     if (!is_null($warptime)) {
         if ($planet['warptime_construction'] > $completion) {
             $planet['warptime_construction'] -= $completion;
             $completion = 0;
         } else {
             $completion -= $planet['warptime_construction'];
             $planet['warptime_construction'] = 0;
         }
     }
     // }
     // add task to system {
     $taskinsert = array('round_id' => $_SESSION['round_id'], 'kingdom_id' => $_SESSION['kingdom_id'], 'player_id' => $planet['player_id'], 'planet_id' => $this->planet_id, 'type' => TASK_BUILD, 'building_id' => $this->building_id, 'number' => $cranes, 'planning' => $planning, 'completion' => $now + $completion, 'start' => $now);
     $this->sql->execute('tasks', $taskinsert);
     $this->data->save();
     // }
     if ($this->quiet) {
         return;
     }
     if ($output_mode == 'javascript') {
         echo 'varAvailableCranes = ' . $planet['cranes'] . '; varAvailablePlanning = ' . $planet['planning'] . '; varBuilt = true; varError = false;';
         exit;
     } else {
         if ($completion == 0) {
             $_SESSION['status'][] = 'The building has been constructed.';
         } else {
             $_SESSION['status'][] = 'The building is now under construction.';
         }
         redirect('buildings.php');
     }
 }