function StartBuild($ACTUAL_TICK, $building, $planet) { global $MAX_BUILDING_LVL; $res = BUILD_ERR_DB; // Building queue full? $this->db->queryrow('SELECT installation_type FROM scheduler_instbuild WHERE planet_id=' . $planet['planet_id']); if ($this->db->num_rows() >= BUILDING_QUEUE_LEN) { return BUILD_ERR_QUEUE; } // Retrieve some BOT infos $sql = 'SELECT user_race, user_capital, pending_capital_choice FROM user WHERE user_id=' . $this->bot['user_id']; $userdata = $this->db->queryrow($sql); $race = $userdata['user_data']; // Planet selected is capital or not? $capital = $userdata['user_capital'] == $planet['planet_id'] ? 1 : 0; if ($userdata['pending_capital_choice']) { $capital = 0; } // Check if building hasn't reached max level if ($planet['building_' . ($building + 1)] == $MAX_BUILDING_LVL[$capital][$building]) { return BUILD_ERR_MAXLEVEL; } // Calculate resources needed for the building $resource_1 = GetBuildingPrice($building, 0, $planet, $race); $resource_2 = GetBuildingPrice($building, 1, $planet, $race); $resource_3 = GetBuildingPrice($building, 2, $planet, $race); // Check resources availability if ($planet['resource_1'] >= $resource_1 && $planet['resource_2'] >= $resource_2 && $planet['resource_3'] >= $resource_3) { // Calculate planet power consumption $buildings = $planet['building_1'] + $planet['building_2'] + $planet['building_3'] + $planet['building_4'] + $planet['building_10'] + $planet['building_6'] + $planet['building_7'] + $planet['building_8'] + $planet['building_9'] + $planet['building_11'] + $planet['building_12'] + $planet['building_13']; /* I think we don't need this check here... if (($building==11 && $planet['building_1']<4) || ($building==10 && $planet['building_1']<3) || ($building==6 && $planet['building_1']<5) || ($building==8 && $planet['building_1']<9) || ($building==7 && $planet['building_7']<1) || ($building==9 && ($planet['building_6']<5 ||$planet['building_7']<1)) || ($building==12 && ($planet['building_6']<1 || $planet['building_7']<1)) ) { return BUILD_ERR_REQUIRED; }*/ // If we are building a power plant, or there is energy still available if ($building == 4 || $buildings <= ($capital ? $planet['building_5'] * 11 + 14 : $planet['building_5'] * 15 + 3)) { // Remove resources needed from the planet $sql = 'UPDATE planets SET resource_1=resource_1-' . $resource_1 . ', resource_2=resource_2-' . $resource_2 . ', resource_3=resource_3-' . $resource_3 . ' WHERE planet_id= ' . $planet['planet_id']; if (!$this->db->query($sql)) { $this->sdl->log('<b>Error:</b> Cannot remove resources need for construction from planet #' . $planet['planet_id'] . '!', TICK_LOG_FILE_NPC); } // Check planet activity $sql = 'SELECT build_finish FROM scheduler_instbuild WHERE planet_id=' . $planet['planet_id'] . ' ORDER BY build_finish DESC'; $userquery = $this->db->queryrow($sql); if ($this->db->num_rows() > 0) { $build_start = $userquery['build_finish']; // BOT take a little bonus here: future level of the building is not // considered, also for simplicity. $build_finish = $build_start + GetBuildingTimeTicks($building, $planet, $race); } else { $build_start = $ACTUAL_TICK; $build_finish = $build_start + GetBuildingTimeTicks($building, $planet, $race); } $sql = 'INSERT INTO scheduler_instbuild (installation_type,planet_id,build_start,build_finish) VALUES ("' . $building . '", "' . $planet['planet_id'] . '", "' . $build_start . '", "' . $build_finish . '")'; if (!$this->db->query($sql)) { $this->sdl->log('<b>Error:</b> cannot add building <b>#' . $building . '</b> to the planet <b>#' . $planet['planet_id'] . '</b>', TICK_LOG_FILE_NPC); } else { $this->sdl->log('Construction of <b>#' . $building . '</b> started on planet <b>#' . $planet['planet_id'] . '</b>', TICK_LOG_FILE_NPC); $res = BUILD_SUCCESS; } } else { $this->sdl->log('Insufficient energy on planet <b>#' . $planet['planet_id'] . '</b> for building <b>#' . $building . '</b>', TICK_LOG_FILE_NPC); $res = BUILD_ERR_ENERGY; } } else { $this->sdl->log('Insufficient resources on planet <b>#' . $planet['planet_id'] . '</b> for building <b>#' . $building . '</b>', TICK_LOG_FILE_NPC); $res = BUILD_ERR_RESOURCES; } return $res; }
if (($q_inst = $db->query($sql)) === false) { echo "Error: could not query scheduler instbuild data!\n"; } else { if ($db->num_rows() > 0) { $n_instbuild = 0; while ($build = $db->fetchrow($q_inst)) { $building_name = 'building_' . ($build['installation_type'] + 1); echo "Building being built : " . $build['installation_type'] . " (name: " . $building_name . ") Level: " . $build[$building_name] . "\n"; echo "Building will be built: " . ($build['building_queue'] - 1) . "\n"; // In order to calculate times correctly, I need to consider also the building // being built at the moment if ($build['installation_type'] == $build['building_queue'] - 1) { $build[$building_name]++; echo "New level: " . $build[$building_name] . "\n"; } $time = GetBuildingTimeTicks($build['building_queue'] - 1, $build, $build['user_race']); // Insert the queued building using the new method $sql = 'INSERT INTO scheduler_instbuild (installation_type,planet_id,build_start,build_finish) VALUES ("' . ($build['building_queue'] - 1) . '", "' . $build['planet_id'] . '", "' . $build['build_finish'] . '", "' . ($build['build_finish'] + $time) . '")'; //echo "Second query:\n".$sql."\n"; if (!$db->query($sql)) { echo "Error: coudl not insert new row in scheduler_instbuild table!\n"; break; } echo "Queue new building: " . ($build['building_queue'] - 1) . " start: " . $build['build_finish'] . " finish: " . ($build['build_finish'] + $time) . "\n"; // Remove the queued building from the planet $sql = 'UPDATE planets SET building_queue = 0
function Start_Build() { global $db, $game, $MAX_BUILDING_LVL, $ACTUAL_TICK; $toConstruct = (int) $_REQUEST['id']; $done = 0; // New: Table locking $game->init_player(11); // Calculate the raw materials needed $metNeeded = GetBuildingPrice($toConstruct, 0); $minNeeded = GetBuildingPrice($toConstruct, 1); $latNeeded = GetBuildingPrice($toConstruct, 2); // Check if the active planet is the user's capital $capital = $game->player['user_capital'] == $game->planet['planet_id'] ? 1 : 0; if ($game->player['pending_capital_choice']) { $capital = 0; } // Retrieve construction queue for the planet $userquery = $db->query('SELECT * FROM scheduler_instbuild WHERE planet_id="' . $game->planet['planet_id'] . '"'); // If queue max length if ($db->num_rows() == BUILDING_QUEUE_LEN) { $game->out('<span class="text_large">' . constant($game->sprache("TEXT3")) . '</span><br>'); } else { if ($db->num_rows() > 0) { Start_Queued(); } else { if ($game->planet['resource_1'] >= $metNeeded && $game->planet['resource_2'] >= $minNeeded && $game->planet['resource_3'] >= $latNeeded && $game->planet['building_' . ($toConstruct + 1)] < $MAX_BUILDING_LVL[$capital][$toConstruct]) { // Check if the planet matches the requirements needed // // NOTE: maybe this is redundant since Show_Main() has already // built the list of available buildings performing this check if (areRequirementsMet($toConstruct)) { // Check the required energy if we aren't building a power plant if ($toConstruct != 4 && isPowerOverloaded()) { $game->out('<span class="text_large">' . constant($game->sprache("TEXT1")) . '<br>' . constant($game->sprache("TEXT2")) . '</span><br>'); } else { // Remove required raw materials from the planet $sql = 'UPDATE planets SET resource_1=resource_1-' . $metNeeded . ', resource_2=resource_2-' . $minNeeded . ', resource_3=resource_3-' . $latNeeded . ' WHERE planet_id= "' . $game->planet['planet_id'] . '"'; if ($db->query($sql) == true) { $game->planet['resource_1'] -= $metNeeded; $game->planet['resource_2'] -= $minNeeded; $game->planet['resource_3'] -= $latNeeded; // Calculate start/finish time for the building $start_time = $ACTUAL_TICK; $finish_time = $ACTUAL_TICK + GetBuildingTimeTicks($toConstruct); // Insert new construction in the scheduler $sql = 'INSERT INTO scheduler_instbuild (installation_type,planet_id,build_start,build_finish) VALUES (' . $toConstruct . ', ' . $game->planet['planet_id'] . ', ' . $start_time . ', ' . $finish_time . ')'; if ($db->query($sql) == false) { message(DATABASE_ERROR, 'building_query: Could not call INSERT INTO in scheduler_instbuild '); exit; } $done = 1; } } } } } } if ($done) { redirect('a=building'); } }