示例#1
0
 function _action_main()
 {
     account_log($this->move['user_id'], $this->dest['user_id'], 1);
     // #############################################################################
     // Update ships
     $sql = 'UPDATE ships
             SET user_id = ' . $this->dest['user_id'] . '
             WHERE fleet_id IN (' . $this->fleet_ids_str . ')';
     if (!$this->db->query($sql)) {
         return $this->log(MV_M_DATABASE, 'Could not update ships user data! SKIP');
     }
     // #############################################################################
     // Update fleets
     $sql = 'UPDATE ship_fleets
             SET planet_id = ' . $this->move['dest'] . ',
                 move_id = 0,
                 user_id = ' . $this->dest['user_id'] . '
             WHERE fleet_id IN (' . $this->fleet_ids_str . ')';
     if (!$this->db->query($sql)) {
         return $this->log(MV_M_DATABASE, 'Could not update fleets data! SKIP');
     }
     // #############################################################################
     // Check debts
     $sql = 'SELECT id, resource_1, resource_2, resource_3, ship_id
             FROM bidding_owed
             WHERE user = '******'user_id'] . ' AND
                   receiver = ' . $this->dest['user_id'] . '
             ORDER BY id ASC';
     if (!($q_debts = $this->db->query($sql))) {
         return $this->log(MV_M_DATABASE, 'Could not query bidding owed data! SKIP');
     }
     $n_debts = $this->db->num_rows($q_debts);
     if ($n_debts > 0) {
         $sql = 'SELECT SUM(resource_1) AS n1, SUM(resource_2) AS n2, SUM(resource_3) AS n3
                 FROM ship_fleets
                 WHERE fleet_id IN (' . $this->fleet_ids_str . ')';
         if (($rfleets = $this->db->queryrow($sql)) === false) {
             return $this->log(MV_M_DATABASE, 'Could not query fleets resources data! SKIP');
         }
         $ships = array();
         $sql = 'SELECT ship_id
                 FROM ships
                 WHERE fleet_id IN (' . $this->fleet_ids_str . ')';
         if (!($q_ships = $this->db->query($sql))) {
             return $this->log(MV_M_DATABASE, 'Could not query ships id data! SKIP');
         }
         while ($_ship = $this->db->fetchrow($q_ships)) {
             $ships[$_ship['ship_id']] = true;
         }
         while ($debt = $this->db->fetchrow($q_debts)) {
             if ($debt['resource_1'] > 0) {
                 if ($debt['resource_1'] >= $rfleets['n1']) {
                     $debt['resource_1'] -= $rfleets['n1'];
                     $rfleets['n1'] = 0;
                 } else {
                     $rfleets['n1'] -= $debt['resource_1'];
                     $debt['resource_1'] = 0;
                 }
             }
             if ($debt['resource_2'] > 0) {
                 if ($debt['resource_2'] >= $rfleets['n2']) {
                     $debt['resource_2'] -= $rfleets['n2'];
                     $rfleets['n2'] = 0;
                 } else {
                     $rfleets['n2'] -= $debt['resource_2'];
                     $debt['resource_2'] = 0;
                 }
             }
             if ($debt['resource_3'] > 0) {
                 if ($debt['resource_3'] >= $rfleets['n3']) {
                     $debt['resource_3'] -= $rfleets['n3'];
                     $rfleets['n3'] = 0;
                 } else {
                     $rfleets['n3'] -= $debt['resource_3'];
                     $debt['resource_3'] = 0;
                 }
             }
             if ($debt['ship_id'] > 0) {
                 if (!empty($ships[$debt['ship_id']])) {
                     $debt['ship_id'] = 0;
                 }
             }
             if ($debt['resource_1'] <= 0 && $debt['resource_2'] <= 0 && $debt['resource_3'] <= 0 && $debt['ship_id'] <= 0) {
                 $sql = 'DELETE FROM bidding_owed
                         WHERE id = ' . $debt['id'];
                 if (!$this->db->query($sql)) {
                     return $this->log(MV_M_DATABASE, 'Could not delete bidding owed data! SKIP');
                 }
             } else {
                 $sql = 'UPDATE bidding_owed
                         SET resource_1 = ' . $debt['resource_1'] . ',
                             resource_2 = ' . $debt['resource_2'] . ',
                             resource_3 = ' . $debt['resource_3'] . ',
                             ship_id = ' . $debt['ship_id'] . '
                         WHERE id = ' . $debt['id'];
                 if (!$this->db->query($sql)) {
                     return $this->log(MV_M_DATABASE, 'Could not update bidding owed data! SKIP');
                 }
             }
         }
     }
     // #############################################################################
     // Write Logbook
     $sql = 'SELECT st.name, st.ship_torso, st.race,
                    COUNT(s.ship_id) AS n_ships
             FROM ship_templates st, ship_fleets f, ships s
             WHERE f.fleet_id IN (' . $this->fleet_ids_str . ') AND
                   s.template_id = st.id AND
                   s.fleet_id = f.fleet_id
             GROUP BY st.id
             ORDER BY st.ship_torso ASC, st.race ASC';
     if (!($q_stpls = $this->db->query($sql))) {
         $this->log(MV_M_DATABASE, 'Could not query ships templates data! ABORTED LOGBOOK');
     }
     $log_data = array(23, $this->move['user_id'], $this->move['start'], $this->start['planet_name'], $this->start['user_id'], $this->move['dest'], $this->dest['planet_name'], $this->dest['user_id'], array());
     while ($stpl = $this->db->fetchrow($q_stpls)) {
         $log_data[8][] = array($stpl['name'], $stpl['ship_torso'], $stpl['race'], $stpl['n_ships']);
     }
     // #############################################################################
     // 31/03/08 - AC: Retrieve player language
     switch ($this->move['language']) {
         case 'GER':
             $log_title1 = 'Flottenverband ' . $this->dest['user_name'] . ' übergeben';
             break;
         case 'ITA':
             $log_title1 = 'Associazione flotta consegnata a ' . $this->dest['user_name'];
             break;
         default:
             $log_title1 = 'Fleet association handed over to ' . $this->dest['user_name'];
             break;
     }
     switch ($this->dest['language']) {
         case 'GER':
             $log_title2 = 'Flottenverband von ' . $this->move['user_name'] . ' hat sich ergeben';
             break;
         case 'ITA':
             $log_title2 = 'Ricevuta associazione flotta di ' . $this->move['user_name'];
             break;
         default:
             $log_title2 = 'Fleet association of ' . $this->move['user_name'] . ' has revealed';
             break;
     }
     add_logbook_entry($this->move['user_id'], LOGBOOK_TACTICAL, $log_title1, $log_data);
     add_logbook_entry($this->dest['user_id'], LOGBOOK_TACTICAL, $log_title2, $log_data);
     return MV_EXEC_OK;
 }
示例#2
0
 function _action_main()
 {
     // #############################################################################
     // Data from the attacker
     $sql = 'SELECT ' . $this->get_combat_query_fleet_columns() . ', f.resource_4, f.unit_1, f.unit_2, f.unit_3, f.unit_4
     FROM (ship_fleets f)
     INNER JOIN user u ON u.user_id = f.user_id
     WHERE f.fleet_id IN (' . $this->fleet_ids_str . ')';
     if (($atk_fleets = $this->db->queryrowset($sql)) === false) {
         return $this->log(MV_M_DATABASE, 'Could not query attackers fleets data! SKIP');
     }
     /* 01/07/08 -AC: I need to move this query here, in order to correctly report attacker
      *               losses in case he lost the battle.
      */
     $sql = 'SELECT ss.fleet_id,
            SUM(ss.unit_1 - st.min_unit_1) AS troop_1,
            SUM(ss.unit_2 - st.min_unit_2) AS troop_2,
            SUM(ss.unit_3 - st.min_unit_3) AS troop_3,
            SUM(ss.unit_4 - st.min_unit_4) AS troop_4
     FROM ships ss, ship_templates st
     WHERE ss.template_id = st.id
     AND ss.fleet_id IN (' . $this->fleet_ids_str . ')
     GROUP BY ss.fleet_id';
     if (($atk_crews = $this->db->queryrowset($sql)) === false) {
         return $this->log(MV_M_DATABASE, 'Could not query attacking fleet data! SKIP');
     }
     // #############################################################################
     // Data from the defender
     $user_id = $this->dest['user_id'];
     $planetary_attack = true;
     // The course could be optimized, but it should be possible
     // compatibility to provide much action_51.php maintained
     $cur_user = array('user_id' => $user_id, 'user_name' => $this->dest['user_name'], 'user_race' => $this->dest['user_race'], 'user_planets' => $this->dest['user_planets'], 'user_alliance' => $this->dest['user_alliance']);
     $sql = 'SELECT DISTINCT f.user_id,
            u.user_alliance,
            ud.ud_id, ud.accepted,
            ad.ad_id, ad.type, ad.status
     FROM (ship_fleets f)
     INNER JOIN user u ON u.user_id = f.user_id
     LEFT JOIN user_diplomacy ud ON ( ( ud.user1_id = ' . $user_id . ' AND ud.user2_id = f.user_id ) OR ( ud.user1_id = f.user_id AND ud.user2_id = ' . $user_id . ' ) )
     LEFT JOIN alliance_diplomacy ad ON ( ( ad.alliance1_id = ' . $cur_user['user_alliance'] . ' AND ad.alliance2_id = u.user_alliance) OR ( ad.alliance1_id = u.user_alliance AND ad.alliance2_id = ' . $cur_user['user_alliance'] . ' ) )
     WHERE f.planet_id = ' . $this->move['dest'] . ' AND
           f.user_id <> ' . $user_id . ' AND
           f.user_id <> ' . $this->move['user_id'] . ' AND
           f.alert_phase >= ' . ALERT_PHASE_YELLOW;
     if (!($q_st_uid = $this->db->query($sql))) {
         return $this->log(MV_M_DATABASE, 'Could not query stationated fleets user data! SKIP');
     }
     $st_user = array();
     while ($st_uid = $this->db->fetchrow($q_st_uid)) {
         $allied = false;
         if ($st_uid['user_alliance'] == $this->dest['user_alliance']) {
             $allied = true;
         }
         if (!empty($st_uid['ud_id'])) {
             if ($st_uid['accepted'] == 1) {
                 $allied = true;
             }
         }
         if (!empty($st_uid['ad_id'])) {
             if ($st_uid['type'] == ALLIANCE_DIPLOMACY_PACT && $st_uid['status'] == 0) {
                 $allied = true;
             }
         }
         if ($allied) {
             $st_user[] = $st_uid['user_id'];
         }
     }
     $n_st_user = count($st_user);
     if ($n_st_user > 0) {
         $sql = 'SELECT ' . $this->get_combat_query_fleet_columns() . '
          FROM (ship_fleets f)
          INNER JOIN user u ON u.user_id = f.user_id
          WHERE f.planet_id = ' . $this->move['dest'] . ' AND
                (
                  (
                    f.user_id = ' . $user_id . '
                  )
                  OR
                  (
                    f.user_id IN (' . implode(',', $st_user) . ') AND
                    f.alert_phase >= ' . ALERT_PHASE_YELLOW . '
                  )
                )';
     } else {
         $sql = 'SELECT ' . $this->get_combat_query_fleet_columns() . '
          FROM (ship_fleets f)
          INNER JOIN user u ON u.user_id = f.user_id
          WHERE f.planet_id = ' . $this->move['dest'] . ' AND
                f.user_id = ' . $user_id;
     }
     if (($dfd_fleets = $this->db->queryrowset($sql)) === false) {
         return $this->log(MV_M_DATABASE, 'Could not query defenders fleets data! SKIP');
     }
     $dfd_fleet_ids = array();
     foreach ($dfd_fleets as $i => $cur_fleet) {
         $dfd_fleet_ids[] = $cur_fleet['fleet_id'];
     }
     $dfd_fleet_ids_str = implode(',', $dfd_fleet_ids);
     if ($this->do_ship_combat($this->fleet_ids_str, $dfd_fleet_ids_str, MV_COMBAT_LEVEL_PLANETARY) == MV_EXEC_ERROR) {
         $this->log(MV_M_DATABASE, 'Move Action 55: Something went wrong with this fight!');
         return MV_EXEC_ERROR;
     }
     // #############################################################################
     // If the attacker has won attempt to start takeover
     // #############################################################################
     // 03/04/08 - AC: Retrieve player language
     switch ($this->dest['language']) {
         case 'GER':
             $dfd_title = 'Angriff auf ' . $this->dest['planet_name'];
             break;
         case 'ITA':
             $dfd_title = 'Attacco su ' . $this->dest['planet_name'];
             break;
         default:
             $dfd_title = 'Attack on ' . $this->dest['planet_name'];
             break;
     }
     $action_status = 0;
     /* 11/07/08 - AC: Initialize here arrays atk/dfd_losses */
     $atk_losses = array(0, 0, 0, 0, 0);
     $dfd_losses = array(0, 0, 0, 0, 0);
     if ($this->cmb[MV_CMB_WINNER] == MV_CMB_ATTACKER) {
         if (empty($this->action_data[0])) {
             return $this->log('Moves', 'action_55: Could not find required action_data entry [0]! SKIP');
         }
         $ship_id = (int) $this->action_data[0];
         // We make it quite safe here, to see whether the
         // ship is
         $sql = 'SELECT s.ship_id, s.user_id, s.unit_1, s.unit_2, s.unit_3, s.unit_4,
                st.ship_torso, st.race, st.min_unit_1, st.min_unit_2, st.min_unit_3, st.min_unit_4,
                f.fleet_id, f.move_id, f.n_ships
         FROM (ships s)
         INNER JOIN ship_templates st ON st.id = s.template_id
         INNER JOIN ship_fleets f ON f.fleet_id = s.fleet_id
         WHERE s.ship_id = ' . $ship_id;
         if (($cship = $this->db->queryrow($sql)) === false) {
             return $this->log(MV_M_DATABASE, 'Could not query cship data! SKIP');
         }
         $cship_exists = true;
         if (empty($cship['ship_id'])) {
             $cship_exists = false;
         } elseif ($cship['user_id'] != $this->move['user_id']) {
             $cship_exists = false;
         } elseif ($cship['ship_torso'] != SHIP_TYPE_COLO) {
             $cship_exists = false;
         } elseif ($cship['move_id'] != $this->mid) {
             $cship_exists = false;
         }
         if ($cship_exists) {
             $atk_units = array($cship['unit_1'] - $cship['min_unit_1'], $cship['unit_2'] - $cship['min_unit_2'], $cship['unit_3'] - $cship['min_unit_3'], $cship['unit_4'] - $cship['min_unit_4'], 0);
             // 20/06/08 - AC: Keep out workers from battle!
             for ($i = 0; $i < count($atk_fleets); ++$i) {
                 $atk_units[0] += $atk_fleets[$i]['unit_1'];
                 $atk_units[1] += $atk_fleets[$i]['unit_2'];
                 $atk_units[2] += $atk_fleets[$i]['unit_3'];
                 $atk_units[3] += $atk_fleets[$i]['unit_4'];
                 //$atk_units[4] += $atk_fleets[$i]['resource_4']; <-- What the hell is this field?? No workers in the battle!
                 // 23/06/08 - AC: Store the space occupied by workers
                 $worker_per_fleets[$atk_fleets[$i]['fleet_id']] = $atk_fleets[$i]['resource_4'];
             }
             // 090408 DC:  ------ Let those f*****g soldiers on ships do their job!!!!
             for ($i = 0; $i < count($atk_crews); ++$i) {
                 $atk_units[0] += $atk_crews[$i]['troop_1'];
                 $atk_units[1] += $atk_crews[$i]['troop_2'];
                 $atk_units[2] += $atk_crews[$i]['troop_3'];
                 $atk_units[3] += $atk_crews[$i]['troop_4'];
             }
             // 090409 DC:  ------
             $dfd_units = array($this->dest['unit_1'], $this->dest['unit_2'], $this->dest['unit_3'], $this->dest['unit_4'], $this->dest['resource_4']);
             // Are there some defenders?
             if (array_sum($dfd_units) > 0) {
                 $ucmb = UnitFight($atk_units, $this->move['user_race'], $dfd_units, $this->dest['user_race'], $this->mid);
                 $n_atk_alive = array_sum($ucmb[0]);
                 $atk_alive = $ucmb[0];
                 $dfd_alive = $ucmb[1];
             } else {
                 $n_atk_alive = 1;
                 $atk_alive = $atk_units;
                 $dfd_alive = array(0, 0, 0, 0, 0);
             }
             // 01/07/08 - AC: Count losses for each part
             for ($i = 0; $i < 5; ++$i) {
                 $atk_losses[$i] = $atk_units[$i] - $atk_alive[$i];
                 $dfd_losses[$i] = $dfd_units[$i] - $dfd_alive[$i];
             }
             // If there are no more attackers, the defender
             // always won even if the defending had no units
             if ($n_atk_alive == 0) {
                 // In order to eliminate the troops of the aggressor, simply reset all transporters
                 // 21/06/08 - AC: Keep out workers from battle!
                 $sql = 'UPDATE ship_fleets
                 SET unit_1 = 0,
                     unit_2 = 0,
                     unit_3 = 0,
                     unit_4 = 0
                 WHERE fleet_id IN (' . $this->fleet_ids_str . ')';
                 if (!$this->db->query($sql)) {
                     return $this->log(MV_M_DATABASE, 'Could not update ship fleets unit transport data! SKIP');
                 }
                 // Simply update the defender troops
                 $sql = 'UPDATE planets
                 SET unit_1 = ' . $ucmb[1][0] . ',
                     unit_2 = ' . $ucmb[1][1] . ',
                     unit_3 = ' . $ucmb[1][2] . ',
                     unit_4 = ' . $ucmb[1][3] . ',
                     resource_4 = ' . $ucmb[1][4] . '
                 WHERE planet_id = ' . $this->move['dest'];
                 if (!$this->db->query($sql)) {
                     return $this->log(MV_M_DATABASE, 'Could not update dest planet units data! SKIP');
                 }
                 // 090408 DC:  ------ Finally the f*****g soldiers have done their job!!!!
                 $this->log(MV_M_NOTICE, 'Update ships with the minimum troops value');
                 $sql = 'UPDATE ships ss, ship_templates st
                 SET ss.unit_1 = st.min_unit_1,
                     ss.unit_2 = st.min_unit_2,
                     ss.unit_3 = st.min_unit_3,
                     ss.unit_4 = st.min_unit_4
                 WHERE ss.template_id = st.id
             AND ss.fleet_id IN (' . $this->fleet_ids_str . ')';
                 if (!$this->db->query($sql)) {
                     return $this->log(MV_M_DATABASE, 'Could not update attacking ships! SKIP');
                 }
                 // 090408 DC:  ------
                 $action_status = -1;
                 // #############################################################################
                 // 03/04/08 - AC: Retrieve player language
                 switch ($this->move['language']) {
                     case 'GER':
                         $atk_title = 'Angriff auf ' . $this->dest['planet_name'] . ' teilweise erfolgreich';
                         break;
                     case 'ITA':
                         $atk_title = 'Attacco su ' . $this->dest['planet_name'] . ' parzialmente riuscito';
                         break;
                     default:
                         $atk_title = 'Attack on ' . $this->dest['planet_name'] . ' partially successful';
                         break;
                 }
             } else {
                 account_log($this->move['user_id'], $this->dest['user_id'], 4);
                 // Export a ruler switch
                 // We need the number of planets, which owns the colonizing
                 // (we want planet_owner_enum degree of certainty)
                 $sql = 'SELECT COUNT(planet_id) AS n_planets
                 FROM planets
                 WHERE planet_owner = ' . $this->move['user_id'];
                 if (($pcount = $this->db->queryrow($sql)) === false) {
                     $this->log(MV_M_DATABASE, 'Could not query planets count data! CONTINUE AND USE INSTABLE VALUE');
                     $n_planets = $this->move['user_planets'];
                 } else {
                     $n_planets = $pcount['n_planets'];
                 }
                 // We accommodate as much troops, as on the colony ship were, on the planet
                 $planet_units = array(0, 0, 0, 0);
                 // 21/06/08 - AC: Check available space on target planet
                 $sql = 'SELECT max_units FROM planets WHERE planet_id = ' . $this->move['dest'];
                 if (($plspace = $this->db->queryrow($sql)) === false) {
                     $this->log(MV_M_DATABASE, 'Could not query planet max units data! CONTINUE AND USE INSTABLE VALUE');
                     $plspace['max_units'] = 1300;
                     // Minimum size available
                 }
                 if ($atk_alive[0] * 2 + $atk_alive[1] * 3 + $atk_alive[2] * 4 + $atk_alive[3] * 4 > $plspace['max_units']) {
                     $this->log(MV_M_NOTICE, 'Alive units exceed planet maximum units, we need to recall aboard the surplus');
                     // AC: Recall aboard the surplus troops
                     $i = 0;
                     while ($n_atk_alive) {
                         if ($planet_units[0] * 2 + $planet_units[1] * 3 + $planet_units[2] * 4 + $planet_units[3] * 4 >= $plspace['max_units']) {
                             break;
                         }
                         if ($atk_alive[$i] > 0) {
                             $planet_units[$i]++;
                             $atk_alive[$i]--;
                             $n_atk_alive--;
                         } else {
                             $i++;
                         }
                     }
                     $this->log(MV_M_NOTICE, 'Troops that will return aboard: Lev1 ' . $atk_alive[0] . ' Lev2 ' . $atk_alive[1] . ' Lev3 ' . $atk_alive[2] . ' Lev4 ' . $atk_alive[3]);
                     // We pick ALL transporter of the fleets and then distribute
                     $sql = 'SELECT s.ship_id, s.fleet_id
                     FROM (ships s)
                     INNER JOIN (ship_templates st) ON s.template_id = st.id
                     WHERE s.fleet_id IN (' . $this->fleet_ids_str . ') AND
                           st.ship_torso = ' . SHIP_TYPE_TRANSPORTER;
                     if (!($q_ftrans = $this->db->query($sql))) {
                         $this->log(MV_M_DATABASE, 'Could not query fleets transporter data! CONTINUE');
                     }
                     // 27/10/08 - AC: First of all, loads all available transports
                     $space_per_fleets = array();
                     while ($_ship = $this->db->fetchrow($q_ftrans)) {
                         if (!isset($space_per_fleets[$_ship['fleet_id']])) {
                             $space_per_fleets[$_ship['fleet_id']] = $worker_per_fleets[$_ship['fleet_id']] * -1;
                         }
                         $space_per_fleets[$_ship['fleet_id']] += MAX_TRANSPORT_UNITS;
                     }
                     foreach ($space_per_fleets as $fleet_id => $free_space) {
                         if ($free_space == 0) {
                             continue;
                         }
                         $still_free = $free_space;
                         $new_load = array(0, 0, 0, 0);
                         for ($i = 0; $i < 4; ++$i) {
                             if ($atk_alive[$i] > 0) {
                                 $new_load[$i] = $atk_alive[$i] > $still_free ? $still_free : $atk_alive[$i];
                                 $atk_alive[$i] -= $new_load[$i];
                                 $still_free -= $new_load[$i];
                             }
                             if ($still_free == 0) {
                                 break;
                             }
                         }
                         $sql = 'UPDATE ship_fleets
                         SET unit_1 = ' . $new_load[0] . ',
                             unit_2 = ' . $new_load[1] . ',
                             unit_3 = ' . $new_load[2] . ',
                             unit_4 = ' . $new_load[3] . '
                         WHERE fleet_id = ' . $fleet_id;
                         if (!$this->db->query($sql)) {
                             $this->log(MV_M_DATABASE, 'Could not update fleets units data! CONTINUE');
                         }
                     }
                     // 27/10/08- AC: If there are still some extra troops on ground
                     $n_atk_still_to_board = array_sum($atk_alive);
                     if ($n_atk_still_to_board > 0) {
                         $this->log(MV_M_NOTICE, 'There are still: Lev1 ' . $atk_alive[0] . ' Lev2 ' . $atk_alive[1] . ' Lev3 ' . $atk_alive[2] . ' Lev4 ' . $atk_alive[3] . ' troops to embark!');
                         // Extra troops didn't really leave the ships so we have to remove the losses
                         $sql = 'SELECT ss.ship_id,
                                st.min_unit_1, st.max_unit_1,
                                st.min_unit_2, st.max_unit_2,
                                st.min_unit_3, st.max_unit_3,
                                st.min_unit_4, st.max_unit_4
                          FROM ships ss, ship_templates st
                          WHERE ss.template_id = st.id AND
                                ss.fleet_id IN (' . $this->fleet_ids_str . ')';
                         if (!($q_ships = $this->db->query($sql))) {
                             $this->log(MV_M_DATABASE, 'Could not query fleets ships data! CONTINUE');
                         }
                         // Distribute troops on available ships
                         while ($ship = $this->db->fetchrow($q_ships)) {
                             $new_crew = array(0, 0, 0, 0);
                             // For each type of troops
                             for ($i = 0; $i < 4; ++$i) {
                                 $j = $i + 1;
                                 // Are there still some troops?
                                 if ($atk_alive[$i] > 0) {
                                     $new_crew[$i] = $atk_alive[$i] > $ship['max_unit_' . $j] ? $ship['max_unit_' . $j] : $atk_alive[$i];
                                     $atk_alive[$i] -= $new_crew[$i];
                                 } else {
                                     $new_crew[$i] = $ship['min_unit_' . $j];
                                 }
                                 $n_atk_still_to_board -= $new_crew[$i];
                             }
                             // Update ship's crew
                             $sql = 'UPDATE ships
                             SET unit_1 = ' . $new_crew[0] . ',
                                 unit_2 = ' . $new_crew[1] . ',
                                 unit_3 = ' . $new_crew[2] . ',
                                 unit_4 = ' . $new_crew[3] . '
                             WHERE ship_id =' . $ship['ship_id'];
                             if (!$this->db->query($sql)) {
                                 $this->log(MV_M_DATABASE, 'Could not update ship crew data! CONTINUE');
                             }
                             // If we have finished boarding phase, we are ready to takeoff ;)
                             if ($n_atk_still_to_board == 0) {
                                 break;
                             }
                         }
                     }
                 } else {
                     for ($i = 0; $i <= 3; ++$i) {
                         $planet_units[$i] += $atk_alive[$i];
                     }
                     // 28/06/08 - AC: Snort! Delete landed troops!
                     $sql = 'UPDATE ship_fleets
                     SET unit_1 = 0,
                         unit_2 = 0,
                         unit_3 = 0,
                         unit_4 = 0
                     WHERE fleet_id IN (' . $this->fleet_ids_str . ')';
                     if (!$this->db->query($sql)) {
                         return $this->log(MV_M_DATABASE, 'Could not update ship fleets unit transport data! SKIP');
                     }
                     // 27/10/08 - AC: Delete also extra troops on ships...
                     $sql = 'UPDATE ships ss, ship_templates st
                     SET ss.unit_1 = st.min_unit_1,
                         ss.unit_2 = st.min_unit_2,
                         ss.unit_3 = st.min_unit_3,
                         ss.unit_4 = st.min_unit_4
                     WHERE ss.template_id = st.id AND
                           ss.fleet_id IN (' . $this->fleet_ids_str . ')';
                     if (!$this->db->query($sql)) {
                         return $this->log(MV_M_DATABASE, 'Could not update attacking ships to minimum crew! SKIP');
                     }
                 }
                 $this->log(MV_M_NOTICE, 'Troops that will remain on the planet: Lev1 ' . $planet_units[0] . ' Lev2 ' . $planet_units[1] . ' Lev3 ' . $planet_units[2] . ' Lev4 ' . $planet_units[3]);
                 $sql = 'DELETE FROM scheduler_instbuild
                 WHERE planet_id = ' . $this->move['dest'];
                 if (!$this->db->query($sql)) {
                     $this->log(MV_M_DATABASE, 'Could not delete scheduler instbuild data! CONTINUE');
                 }
                 $sql = 'DELETE FROM scheduler_research
                 WHERE planet_id = ' . $this->move['dest'];
                 if (!$this->db->query($sql)) {
                     $this->log(MV_M_DATABASE, 'Could not delete scheduler research data! CONTINUE');
                 }
                 $sql = 'DELETE FROM scheduler_resourcetrade
                 WHERE planet = ' . $this->move['dest'];
                 if (!$this->db->query($sql)) {
                     $this->log(MV_M_DATABASE, 'Could not delete resource data! CONTINUE');
                 }
                 $sql = 'DELETE FROM scheduler_shipbuild
                 WHERE planet_id = ' . $this->move['dest'];
                 if (!$this->db->query($sql)) {
                     $this->log(MV_M_DATABASE, 'Could not delete shipbuild data! CONTINUE');
                 }
                 global $NUM_BUILDING, $MAX_BUILDING_LVL;
                 // As someone yes number == max_index sets
                 $n_buildings = $NUM_BUILDING + 1;
                 $building_levels = array();
                 $building_damage = 0.01 * mt_rand(40, 60);
                 // Set new home world
                 if ($this->move['dest'] == $this->dest['user_capital']) {
                     for ($i = 0; $i < $n_buildings; ++$i) {
                         $j = $i + 1;
                         $building_levels[$i] = (int) ($this->dest['building_' . $j] > $MAX_BUILDING_LVL[0][$i]) ? $MAX_BUILDING_LVL[0][$i] : round($this->dest['building_' . $j] * $building_damage, 0);
                     }
                     if ($this->dest['user_planets'] == 1) {
                         // #############################################################################
                         // 03/04/08 - AC: Retrieve player language
                         switch ($this->dest['language']) {
                             case 'GER':
                                 $msg_title = 'Verlust aller deiner Planeten';
                                 $msg_body = 'Da du alle deine Planeten verloren hast, wurde f&uuml;r dich ein neuer Planet an einer zuf&uuml;ligen Stelle der Galaxie erstellt.';
                                 break;
                             case 'ITA':
                                 $msg_title = 'Perdita di tutti i pianeti';
                                 $msg_body = 'Dato che hai perduto tutti i pianeti ne &egrave; stato creato uno nuovo nella galassia.';
                                 break;
                             default:
                                 $msg_title = 'Loss of all your planets';
                                 $msg_body = 'Since you lost all your planets a new one have been created in the body of the galaxy.';
                                 break;
                         }
                         SystemMessage($this->dest['user_id'], $msg_title, $msg_body);
                     } else {
                         if (!$this->db->query('SET @i=0')) {
                             return $this->log(MV_M_DATABASE, 'Could not set sql iterator variable for planet owner enum! SKIP');
                         }
                         $sql = 'UPDATE planets
                         SET planet_owner_enum = (@i := (@i + 1))-1
                         WHERE planet_owner = ' . $this->dest['user_id'] . '
                         ORDER BY planet_owned_date ASC, planet_id ASC';
                         if (!$this->db->query($sql)) {
                             return $this->log(MV_M_DATABASE, 'Could not update planet owner enum data! SKIP');
                         }
                         $sql = 'SELECT planet_id
                         FROM planets
                         WHERE planet_owner = ' . $this->dest['user_id'] . '
                         ORDER BY planet_owner_enum ASC
                         LIMIT 1';
                         if (($first_planet = $this->db->queryrow($sql)) === false) {
                             return $this->log(MV_M_DATABASE, 'Could not query first planet data! SKIP');
                         }
                         $sql = 'UPDATE user
                         SET last_active = ' . time() . ',
                             last_ip = "0.0.0.0",
                             user_capital = ' . $first_planet['planet_id'] . ',
                             pending_capital_choice = 1
                         WHERE user_id = ' . $this->dest['user_id'];
                         if (!$this->db->query($sql)) {
                             return $this->log(MV_M_DATABASE, 'Could not update user capital data! SKIP');
                         }
                     }
                 } else {
                     for ($i = 0; $i < $n_buildings; ++$i) {
                         $building_levels[$i] = round($this->dest['building_' . ($i + 1)] * $building_damage, 0);
                     }
                 }
                 $sql = 'DELETE FROM resource_trade
                 WHERE planet = ' . $this->move['dest'];
                 if (!$this->db->query($sql)) {
                     $this->log(MV_M_DATABASE, 'Could not delete resource trade data! CONTINUE');
                 }
                 $sql = 'SELECT ship_id
                 FROM ship_trade
                 WHERE scheduler_processed = 0 AND
                       planet = ' . $this->move['dest'];
                 if (!($q_shipt = $this->db->query($sql))) {
                     $this->log(MV_M_DATABASE, 'Could not query ship trade data! CONTINUE');
                 } else {
                     $ship_ids = array();
                     while ($_ship = $this->db->fetchrow($q_shipt)) {
                         $ship_ids[] = $_ship['ship_id'];
                     }
                     if (count($ship_ids) > 0) {
                         $sql = 'UPDATE ships
                         SET ship_untouchable = 0
                         WHERE ship_id IN (' . implode(',', $ship_ids) . ')';
                         if (!$this->db->query($sql)) {
                             $this->log(MV_M_DATABASE, 'Could not update ships untouchable data! CONTINUE');
                         }
                     }
                 }
                 $sql = 'DELETE FROM ship_trade
                 WHERE scheduler_processed = 0 AND
                       planet = ' . $this->move['dest'];
                 if (!$this->db->query($sql)) {
                     $this->log(MV_M_DATABASE, 'Could not delete ship trade data! CONTINUE');
                 }
                 $sql = 'UPDATE planets
                 SET planet_owner = ' . $this->move['user_id'] . ',
                     planet_owned_date = ' . time() . ',
                     planet_owner_enum = ' . ($n_planets - 1) . ',
                     best_mood = 0,
                     best_mood_user = 0,
                     planet_available_points = ' . $this->get_structure_points($this->move['user_id'], $this->move['dest']) . ',
                     resource_4 = 0,
                     recompute_static = 1,
                     building_1 = ' . $building_levels[0] . ',
                     building_2 = ' . $building_levels[1] . ',
                     building_3 = ' . $building_levels[2] . ',
                     building_4 = ' . $building_levels[3] . ',
                     building_5 = ' . $building_levels[4] . ',
                     building_6 = ' . $building_levels[5] . ',
                     building_7 = ' . $building_levels[6] . ',
                     building_8 = ' . $building_levels[7] . ',
                     building_9 = ' . $building_levels[8] . ',
                     building_10 = ' . $building_levels[9] . ',
                     building_11 = ' . $building_levels[10] . ',
                     building_12 = ' . $building_levels[11] . ',
                     building_13 = ' . $building_levels[12] . ',
                     unit_1 = ' . $planet_units[0] . ',
                     unit_2 = ' . $planet_units[1] . ',
                     unit_3 = ' . $planet_units[2] . ',
                     unit_4 = ' . $planet_units[3] . ',
                     unit_5 = 0,
                     unit_6 = 0,
                     workermine_1 = 100,
                     workermine_2 = 100,
                     workermine_3 = 100,
                     catresearch_1 = 0,
                     catresearch_2 = 0,
                     catresearch_3 = 0,
                     catresearch_4 = 0,
                     catresearch_5 = 0,
                     catresearch_6 = 0,
                     catresearch_7 = 0,
                     catresearch_8 = 0,
                     catresearch_9 = 0,
                     catresearch_10 = 0,
                     unittrainid_1 = 0,
                     unittrainid_2 = 0,
                     unittrainid_3 = 0,
                     unittrainid_4 = 0,
                     unittrainid_5 = 0,
                     unittrainid_6 = 0,
                     unittrainid_7 = 0,
                     unittrainid_8 = 0,
                     unittrainid_9 = 0,
                     unittrainid_10 = 0,
                     unittrainnumber_1 = 0,
                     unittrainnumber_2 = 0,
                     unittrainnumber_3 = 0,
                     unittrainnumber_4 = 0,
                     unittrainnumber_5 = 0,
                     unittrainnumber_6 = 0,
                     unittrainnumber_7 = 0,
                     unittrainnumber_8 = 0,
                     unittrainnumber_9 = 0,
                     unittrainnumber_10 = 0,
                     unittrainnumberleft_1 = 0,
                     unittrainnumberleft_2 = 0,
                     unittrainnumberleft_3 = 0,
                     unittrainnumberleft_4 = 0,
                     unittrainnumberleft_5 = 0,
                     unittrainnumberleft_6 = 0,
                     unittrainnumberleft_7 = 0,
                     unittrainnumberleft_8 = 0,
                     unittrainnumberleft_9 = 0,
                     unittrainnumberleft_10 = 0,
                     unittrain_actual = 0,
                     unittrainid_nexttime=0,
                     planet_insurrection_time=0,
                     planet_surrender=0
                 WHERE planet_id = ' . $this->move['dest'];
                 //$this->log('SQL Debug', ''.$sql.'');
                 if (!$this->db->query($sql)) {
                     return $this->log(MV_M_DATABASE, 'Could not update planets data! SKIP');
                 }
                 // DC ---- History record in planet_details, with label '26'
                 $sql = 'INSERT INTO planet_details (planet_id, user_id, alliance_id, source_uid, source_aid, timestamp, log_code, defeat_uid, defeat_aid)
                 VALUES (' . $this->move['dest'] . ', ' . $this->move['user_id'] . ', ' . $this->move['user_alliance'] . ', ' . $this->move['user_id'] . ', ' . $this->move['user_alliance'] . ', ' . time() . ', 26,' . $this->dest['user_id'] . ', ' . $this->dest['user_alliance'] . ')';
                 if (!$this->db->query($sql)) {
                     $this->log(MV_M_DATABASE, 'Could not update planet details data! CONTINUE');
                 }
                 // If the attack was on a settlers planet, we get rid of the moods data!
                 if ($this->dest['user_id'] == INDEPENDENT_USERID) {
                     $this->log(MV_M_NOTICE, 'Colony: Settlers taken over!!! They gonna be no more...');
                     $sql = 'SELECT user_id, user_name, language FROM settlers_relations
                                                         INNER JOIN user USING user_id
                     WHERE planet_id = ' . $this->move['dest'] . ' AND user_id NOT = ' . $this->move['user_id'] . '
                     GROUP BY user_id';
                     if ($res = $this->db->queryrowset($sql)) {
                         foreach ($res as $res_item) {
                             $log_data = array($this->move['dest'], $this->dest['planet_name'], 0, '', 105, $res_item['user_name']);
                             switch ($res_item['language']) {
                                 case 'GER':
                                     $log_title = 'Automatic distress message from ';
                                     break;
                                 case 'ITA':
                                     $log_title = 'Richiesta automatica di soccorso dalla colonia ';
                                     break;
                                 default:
                                     $log_title = 'Automatic distress message from ';
                                     break;
                             }
                             add_logbook_entry($res_item['user_id'], LOGBOOK_SETTLERS, $log_title . $this->dest['planet_name'], $log_data);
                         }
                     }
                     $sql = 'DELETE FROM settlers_relations WHERE planet_id = ' . $this->dest['planet_id'];
                     if (!$this->db->query($sql)) {
                         $this->log(MV_M_DATABASE, 'Could not delete settlers moods! CONTINUE!');
                     }
                     $sql = 'SELECT planet_id FROM settlers_relations WHERE user_id = ' . $this->move['user_id'] . ' GROUP BY planet_id';
                     if ($res = $this->db->queryrowset($sql)) {
                         foreach ($res as $res_item) {
                             // Clear all + logs on the planet
                             $sql = 'DELETE FROM settlers_relations
                             WHERE user_id = ' . $this->move['user_id'] . '
                             AND planet_id = ' . $res_item['planet_id'] . '
                             AND log_code IN ("2", "3", "4")';
                             if (!$this->db->query($sql)) {
                                 $this->log(MV_M_DATABASE, 'Could not delete settlers relations data! ' . $sql . ' CONTINUE');
                             }
                             $sql = 'INSERT INTO settlers_relations SET planet_id = ' . $res_item['planet_id'] . ',
                                                                user_id = ' . $this->move['user_id'] . ',
                                                                timestamp = ' . time() . ', log_code = 12,
                                                                mood_modifier = -50';
                             $this->db->query($sql);
                             $this->check_best_mood($res_item['planet_id'], false);
                         }
                     }
                 }
                 // DC ----
                 $this->log('Update Planetdata', 'Limited resources');
                 //Concept of vessels destroyed by hostile takeover.
                 //Version 0.2b by Mojo1987 - Calculation adjusted
                 $this->log('Ship destruction', 'Begin ships deletion');
                 $sql = 'SELECT s.ship_id FROM (ships s) WHERE s.fleet_id = -' . $this->move['dest'] . '';
                 if (!($del_ship = $this->db->query($sql))) {
                     $this->log('Errors with ships get', 'Could not query planets ships! CONTINUE - ' . $sql . '');
                 }
                 while ($ship_wahl = $this->db->fetchrow($del_ship)) {
                     $zufall = mt_rand(6, 18);
                     if ($zufall >= 8) {
                         $sql = 'DELETE FROM ships WHERE ship_id = ' . $ship_wahl['ship_id'] . '';
                         if (!$this->db->query($sql)) {
                             $this->log('Error with ship deletion', 'Could not query deleted ship! CONTINUE');
                         } else {
                             $this->log('Ships removed', 'Ship_ID: ' . $ship_wahl['ship_id'] . ' Random number: ' . $zufall . ' <b>SUCCESS!</b>');
                         }
                     }
                 }
                 $this->log('Ship destruction', 'Deletion ended');
                 if (!$this->db->query('SET @i=0')) {
                     return $this->log(MV_M_DATABASE, 'Could not set sql iterator variable for planet owner enum (the invading player)! SKIP');
                 }
                 $sql = 'UPDATE planets
                 SET planet_owner_enum = (@i := (@i + 1))-1
                 WHERE planet_owner = ' . $this->move['user_id'] . '
                 ORDER BY planet_owned_date ASC, planet_id ASC';
                 if (!$this->db->query($sql)) {
                     return $this->log(MV_M_DATABASE, 'Could not update planet owner enum data (the invading player)! SKIP');
                 }
                 $sql = 'UPDATE ships
                 SET user_id = ' . $this->move['user_id'] . '
                 WHERE fleet_id = -' . $this->move['dest'];
                 if (!$this->db->query($sql)) {
                     return $this->log(MV_M_DATABASE, 'Could not update ships location data! SKIP');
                 }
                 if ($this->dest['user_id'] == BORG_USERID) {
                     if ((int) $this->move['user_alliance'] != 0) {
                         $sql = 'UPDATE alliance
                         SET borg_invade = borg_invade + 1
                         WHERE alliance_id = ' . $this->move['user_alliance'];
                         if (!$this->db->query($sql)) {
                             $this->log(MV_M_DATABASE, 'Could not update user alliance Borg honor data! CONTINUE');
                         }
                     }
                     $num_item = $this->dest['planet_type'] == "m" || $this->dest['planet_type'] == "o" || $this->dest['planet_type'] == "y" || $this->dest['planet_type'] == "x" || $this->dest['planet_type'] == "f" || $this->dest['planet_type'] == "g" ? 2 : 1;
                     send_premonition_to_user($this->move['user_id'], $num_item);
                     $sql = 'SELECT threat_level FROM borg_target WHERE user_id = ' . $this->move['user_id'];
                     $bot_target_data = $this->db->query($sql);
                     $already_acquired = $this->db->num_rows($bot_target_data);
                     if ($already_acquired < 1) {
                         $honor_prize = $this->dest['planet_type'] == "m" || $this->dest['planet_type'] == "o" || $this->dest['planet_type'] == "y" || $this->dest['planet_type'] == "x" || $this->dest['planet_type'] == "f" || $this->dest['planet_type'] == "g" ? 40 : 15;
                         $sql = 'INSERT INTO borg_target (user_id, planets_taken, battle_win) VALUES (' . $this->move['user_id'] . ', 1, 1)';
                         $this->db->query($sql);
                     } else {
                         $honor_bonus_data = $this->db->fetchrow($bot_target_data);
                         // Keep values align with those present in in borg.php
                         if ($honor_bonus_data['threat_level'] > 1400.0) {
                             $honor_bonus = 5.0;
                         } elseif ($honor_bonus_data['threat_level'] > 950.0) {
                             $honor_bonus = 3.0;
                         } elseif ($honor_bonus_data['threat_level'] > 450.0) {
                             $honor_bonus = 2.0;
                         } elseif ($honor_bonus_data['threat_level'] > 200.0) {
                             $honor_bonus = 1.5;
                         } else {
                             $honor_bonus = 1.0;
                         }
                         $honor_prize = ($this->dest['planet_type'] == "m" || $this->dest['planet_type'] == "n" || $this->dest['planet_type'] == "y" || $this->dest['planet_type'] == "e" || $this->dest['planet_type'] == "f" || $this->dest['planet_type'] == "g" ? 40 : 15) * $honor_bonus;
                         $sql = 'UPDATE borg_target SET planets_taken = planets_taken + 1, battle_win = battle_win +1 WHERE user_id = ' . $this->move['user_id'];
                         $this->db->query($sql);
                     }
                     $sql = 'UPDATE user SET user_honor = user_honor + ' . $honor_prize . ' WHERE user_id = ' . $this->move['user_id'];
                     if (!$this->db->query($sql)) {
                         $this->log(MV_M_DATABASE, 'Could not update user honor data! CONTINUE');
                     }
                     if (isset($this->move['user_alliance']) && !empty($this->move['user_alliance'])) {
                         $sql = 'UPDATE alliance SET alliance_honor = alliance_honor + ' . $honor_prize . ' WHERE alliance_id = ' . $this->move['user_alliance'];
                         if (!$this->db->query($sql)) {
                             $this->log(MV_M_DATABASE, 'Could not update alliance honor data! CONTINUE');
                         }
                     }
                     // #############################################################################
                     // 03/04/08 - AC: Retrieve player language
                     switch ($this->move['language']) {
                         case 'GER':
                             $msg_title = 'Verdienst im Kampf gegen die Borg';
                             $msg_text = 'F&uuml;e gerade erbrachten Verdienst gegen die neue Invasion des Borg-Kollektives befinden sich im Anflug auf euren Heimatplaneten ein speziell f&ouml;sen Kampf entwickeltes Schiff. Versucht nicht die enthaltene Technologie zu verstehen, sie wurde wirkungsvoll gegen euch abgeschirmt - nutzt sie stattdessen in eurem weiteren Kampf!';
                             break;
                         case 'ITA':
                             $msg_title = 'Merito in battaglia contro i Borg';
                             $msg_text = 'Proprio per rendere merito contro la nuova invasione del collettivo Borg si trova in approccio al tuo pianeta madre una nave appositamente progettata per combatterli. Non provare a capire la tecnologia inclusa, &egrave; stata efficacemente schermata contro di te - utilizzala invece ulteriormente nella tua lotta!';
                             break;
                         default:
                             $msg_title = 'Merit in the battle against the Borg';
                             $msg_text = 'For precisely rendered merit against the new invasion of the Borg Collective are located in the approach to your home planet fuse fight a specially designed ship. Do not try to understand the included technology, it was effectively shielded against you - instead uses it further in your fight!';
                             break;
                     }
                     $sql = 'INSERT INTO message (sender, receiver, subject, text, time)
                     VALUES (' . FUTURE_HUMANS_UID . ', ' . $this->move['user_id'] . ', "' . $msg_title . '", "' . $msg_text . '", ' . time() . ')';
                     if (!$this->db->query($sql)) {
                         $this->log(MV_M_DATABASE, 'Could not send message');
                     }
                 }
                 $action_status = 1;
                 // #############################################################################
                 // 03/04/08 - AC: Retrieve player language
                 switch ($this->move['language']) {
                     case 'GER':
                         $atk_title = 'Angriff auf ' . $this->dest['planet_name'] . ' erfolgreich';
                         break;
                     case 'ITA':
                         $atk_title = 'Attacco su ' . $this->dest['planet_name'] . ' riuscito';
                         break;
                     default:
                         $atk_title = 'Attack on ' . $this->dest['planet_name'] . ' successful';
                         break;
                 }
             }
             $sql = 'DELETE FROM ships
             WHERE ship_id = ' . $ship_id;
             if (!$this->db->query($sql)) {
                 return $this->log(MV_M_DATABASE, 'Could not delete ships data! SKIP');
             }
             if ($cship['n_ships'] == 1) {
                 $sql = 'DELETE FROM ship_fleets
                 WHERE fleet_id = ' . $cship['fleet_id'];
             } else {
                 $sql = 'UPDATE ship_fleets
                 SET n_ships = n_ships - 1
                 WHERE fleet_id = ' . $cship['fleet_id'];
             }
             if (!$this->db->query($sql)) {
                 $this->log(MV_M_DATABASE, 'Could not update/delete cships fleet data! CONTINUE');
             }
         } else {
             $action_status = -2;
             // #############################################################################
             // 03/04/08 - AC: Retrieve player language
             switch ($this->move['language']) {
                 case 'GER':
                     $atk_title = 'Angriff auf ' . $this->dest['planet_name'] . ' teilweise erfolgreich';
                     break;
                 case 'ITA':
                     $atk_title = 'Attacco su ' . $this->dest['planet_name'] . ' parzialmente riuscito';
                     break;
                 default:
                     $atk_title = 'Attack on ' . $this->dest['planet_name'] . ' partially successful';
                     break;
             }
         }
         $sql = 'UPDATE ship_fleets
         SET planet_id = ' . $this->move['dest'] . ',
             move_id = 0
         WHERE fleet_id IN (' . $this->fleet_ids_str . ')';
         if (!$this->db->query($sql)) {
             // Here one could also report and then go on
             return $this->log(MV_M_DATABASE, 'Could not update fleets location data! SKIP');
         }
     } else {
         // #############################################################################
         // 03/04/08 - AC: Retrieve player language
         switch ($this->move['language']) {
             case 'GER':
                 $atk_title = 'Angriff auf ' . $this->dest['planet_name'] . ' fehlgeschlagen';
                 break;
             case 'ITA':
                 $atk_title = 'Attacco su ' . $this->dest['planet_name'] . ' fallito';
                 break;
             default:
                 $atk_title = 'Attack on ' . $this->dest['planet_name'] . ' failed';
                 break;
         }
         // 01/07/08 - The attacker has lost the fleets
         for ($i = 0; $i < count($atk_fleets); ++$i) {
             $atk_losses[0] += $atk_fleets[$i]['unit_1'];
             $atk_losses[1] += $atk_fleets[$i]['unit_2'];
             $atk_losses[2] += $atk_fleets[$i]['unit_3'];
             $atk_losses[3] += $atk_fleets[$i]['unit_4'];
             $atk_losses[4] += $atk_fleets[$i]['resource_4'];
         }
         for ($i = 0; $i < count($atk_crews); ++$i) {
             $atk_losses[0] += $atk_crews[$i]['troop_1'];
             $atk_losses[1] += $atk_crews[$i]['troop_2'];
             $atk_losses[2] += $atk_crews[$i]['troop_3'];
             $atk_losses[3] += $atk_crews[$i]['troop_4'];
         }
     }
     // #############################################################################
     // Write logbook
     $log1_data = array(55, $this->move['user_id'], $this->move['dest'], $this->dest['planet_name'], $this->dest['user_id'], 0, 0, 0, MV_CMB_ATTACKER, $this->cmb[MV_CMB_WINNER] == MV_CMB_ATTACKER ? 1 : 0, 0, 0, $atk_fleets, $dfd_fleets);
     $log2_data = array(55, $this->move['user_id'], $this->move['dest'], $this->dest['planet_name'], $this->dest['user_id'], 0, 0, 0, MV_CMB_DEFENDER, $this->cmb[MV_CMB_WINNER] == MV_CMB_DEFENDER ? 1 : 0, 0, 0, $atk_fleets, $dfd_fleets);
     $log1_data[17] = $log2_data[17] = $action_status;
     $log1_data[20] = $this->dest['user_race'];
     $log2_data[20] = $this->move['user_race'];
     for ($i = 0; $i < 5; ++$i) {
         $log1_data[18][$i] = $log2_data[19][$i] = $atk_losses[$i];
         $log1_data[19][$i] = $log2_data[18][$i] = $dfd_losses[$i];
     }
     //$log1_data[19][4] = $log2_data[18][4] = (int)$dfd_losses[4]; already done by for above...
     $log2_data[14] = $this->cmb[MV_CMB_KILLS_PLANETARY];
     $log1_data[10] = $this->cmb[MV_CMB_KILLS_EXT];
     $log2_data[10] = $this->cmb[MV_CMB_KILLS_EXT];
     add_logbook_entry($this->move['user_id'], LOGBOOK_TACTICAL, $atk_title, $log1_data);
     add_logbook_entry($this->dest['user_id'], LOGBOOK_TACTICAL, $dfd_title, $log2_data);
     if ($n_st_user > 0) {
         $log2_data[2] = $this->move['start'];
         $log2_data[3] = $this->dest['planet_name'];
         $log2_data[4] = $user_id;
         $log2_data[5] = 0;
         $log2_data[6] = 0;
         $log2_data[7] = 0;
         $log2_data[16] = 1;
         for ($i = 0; $i < $n_st_user; ++$i) {
             // #############################################################################
             // 24/06/08 - AC: Retrieve player language
             $log_title = 'One of your allies defended ' . $this->dest['planet_name'];
             $sql = 'SELECT language FROM user WHERE user_id = ' . $st_user[$i];
             if (!($lang = $this->db->queryrow($sql))) {
                 $this->log(MV_M_DATABASE, 'Could not retrieve player language');
             } else {
                 switch ($lang['language']) {
                     case 'GER':
                         $log_title = 'Verb&uuml;ndeten bei ' . $this->dest['planet_name'] . ' verteidigt';
                         break;
                     case 'ITA':
                         $log_title = 'Difesa alleata presso ' . $this->dest['planet_name'];
                         break;
                 }
             }
             add_logbook_entry($st_user[$i], LOGBOOK_TACTICAL, $log_title, $log2_data);
         }
     }
     return MV_EXEC_OK;
 }
     $aaa = 1;
 }
 if ($_POST['res_2'] + $ftaxes[1] > $adata['taxes_2']) {
     $_POST['res_2'] = $adata['taxes_2'] - $ftaxes[1];
     $bbb = 1;
 }
 if ($_POST['res_3'] + $ftaxes[2] > $adata['taxes_3']) {
     $_POST['res_3'] = $adata['taxes_3'] - $ftaxes[2];
     $ccc = 1;
 }
 $sql = 'SELECT u.*,p.planet_id FROM (user u) LEFT JOIN (planets p) ON p.planet_id=' . $_POST['receiver'] . ' WHERE u.user_id=p.planet_owner AND u.user_alliance=' . $game->player['user_alliance'];
 if (($user = $db->queryrow($sql)) === false || !isset($user['planet_id'])) {
     redirect('a=alliance_taxes');
 }
 $planet = $user['planet_id'];
 account_log($game->player['user_id'], $user['user_id'], 3);
 if ($db->query('INSERT INTO scheduler_resourcetrade (planet,resource_1,resource_2,resource_3,resource_4,unit_1,unit_2,unit_3,unit_4,unit_5,unit_6,arrival_time) VALUES ("' . $planet . '","' . $_POST['res_1'] . '","' . $_POST['res_2'] . '","' . $_POST['res_3'] . '",0,0,0,0,0,0,0,"' . ($ACTUAL_TICK + $distance) . '")') == true) {
     $ships = ceil(($_POST['res_1'] + $_POST['res_2'] + $_POST['res_3']) / MAX_TRANSPORT_RESOURCES);
     send_fake_transporter(array(FERENGI_TRADESHIP_ID => $ships), FERENGI_USERID, 0, $planet, $ACTUAL_TICK + $distance);
     if ($aaa == 0) {
         $wert_1 = $_POST['res_1'] + $ftaxes[0];
     } else {
         $wert_1 = $_POST['res_1'];
     }
     if ($bbb == 0) {
         $wert_2 = $_POST['res_2'] + $ftaxes[1];
     } else {
         $wert_2 = $_POST['res_2'];
     }
     if ($ccc == 0) {
         $wert_3 = $_POST['res_3'] + $ftaxes[2];
    function _action_main()
    {
        // #############################################################################
        // Data from the attacker
        /*        
        $sql = 'SELECT '.$this->get_combat_query_fleet_columns().', f.resource_4, f.unit_1, f.unit_2, f.unit_3, f.unit_4
               FROM (ship_fleets f)
               INNER JOIN user u ON u.user_id = f.user_id
               INNER JOIN ships ON ships
               WHERE f.fleet_id IN ('.$this->fleet_ids_str.')';
        */
        $sql = 'SELECT ' . $this->get_combat_query_fleet_columns() . ',
		SUM(s.unit_1 - st.min_unit_1) AS unit_1,
                SUM(s.unit_2 - st.min_unit_2) AS unit_2,
                SUM(s.unit_3 - st.min_unit_3) AS unit_3,
                SUM(s.unit_4 - st.min_unit_4) AS unit_4
        FROM (ship_fleets f)
        INNER JOIN user u ON u.user_id = f.user_id
        INNER JOIN ships s USING (fleet_id)
        INNER JOIN ship_templates st ON s.template_id = st.id
        WHERE f.fleet_id IN (' . $this->fleet_ids_str . ')';
        if (($atk_fleets = $this->db->queryrowset($sql)) === false) {
            return $this->log(MV_M_DATABASE, 'Could not query attackers fleets data! SKIP');
        }
        // #############################################################################
        // Data from the defender
        $sql = 'SELECT DISTINCT f.user_id,
               u.user_alliance,
               ud.ud_id, ud.accepted,
               ad.ad_id, ad.type, ad.status
        FROM (ship_fleets f)
        INNER JOIN user u ON u.user_id = f.user_id
        LEFT JOIN user_diplomacy ud ON ( ( ud.user1_id = ' . $this->dest['user_id'] . ' AND ud.user2_id = f.user_id ) OR ( ud.user1_id = f.user_id AND ud.user2_id = ' . $this->dest['user_id'] . ' ) )
        LEFT JOIN alliance_diplomacy ad ON ( ( ad.alliance1_id = ' . $this->dest['user_alliance'] . ' AND ad.alliance2_id = u.user_alliance) OR ( ad.alliance1_id = u.user_alliance AND ad.alliance2_id = ' . $this->dest['user_alliance'] . ' ) )
        WHERE f.planet_id = ' . $this->move['dest'] . ' AND
              f.user_id <> ' . $this->dest['user_id'] . ' AND
              f.user_id <> ' . $this->move['user_id'] . ' AND
              f.alert_phase >= ' . ALERT_PHASE_YELLOW;
        if (!($q_st_uid = $this->db->query($sql))) {
            return $this->log(MV_M_DATABASE, 'Could not query stationated fleets user data! SKIP');
        }
        $st_user = array();
        while ($st_uid = $this->db->fetchrow($q_st_uid)) {
            $allied = false;
            if ($st_uid['user_alliance'] == $this->dest['user_alliance']) {
                $allied = true;
            }
            if (!empty($st_uid['ud_id'])) {
                if ($st_uid['accepted'] == 1) {
                    $allied = true;
                }
            }
            if (!empty($st_uid['ad_id'])) {
                if ($st_uid['type'] == ALLIANCE_DIPLOMACY_PACT && $st_uid['status'] == 0) {
                    $allied = true;
                }
            }
            if ($allied) {
                $st_user[] = $st_uid['user_id'];
            }
        }
        $n_st_user = count($st_user);
        if ($n_st_user > 0) {
            $sql = 'SELECT ' . $this->get_combat_query_fleet_columns() . '
             FROM (ship_fleets f)
             INNER JOIN user u ON u.user_id = f.user_id
             WHERE f.planet_id = ' . $this->move['dest'] . ' AND
                   (
                     (
                       f.user_id = ' . $this->dest['user_id'] . '
                     )
                     OR
                     (
                       f.user_id IN (' . implode(',', $st_user) . ') AND
                       f.alert_phase >= ' . ALERT_PHASE_YELLOW . '
                     )
                   )';
        } else {
            $sql = 'SELECT ' . $this->get_combat_query_fleet_columns() . '
             FROM (ship_fleets f)
             INNER JOIN user u ON u.user_id = f.user_id
             WHERE f.planet_id = ' . $this->move['dest'] . ' AND
                   f.user_id = ' . $this->dest['user_id'];
        }
        if (($dfd_fleets = $this->db->queryrowset($sql)) === false) {
            return $this->log(MV_M_DATABASE, 'Could not query defenders fleets data! SKIP');
        }
        $dfd_fleet_ids = array();
        foreach ($dfd_fleets as $i => $cur_fleet) {
            $dfd_fleet_ids[] = $cur_fleet['fleet_id'];
        }
        $dfd_fleet_ids_str = implode(',', $dfd_fleet_ids);
        if ($this->do_ship_combat($this->fleet_ids_str, $dfd_fleet_ids_str, MV_COMBAT_LEVEL_PLANETARY) == MV_EXEC_ERROR) {
            $this->log(MV_M_DATABASE, 'Move Action 46: Something went wrong with this fight!');
            return MV_EXEC_ERROR;
        }
        // #############################################################################
        // If the attacker has won attempt to start takeover
        // #############################################################################
        // 03/04/08 - AC: Retrieve player language
        switch ($this->dest['language']) {
            case 'GER':
                $dfd_title = 'Angriff der Borg auf ' . $this->dest['planet_name'];
                break;
            case 'ITA':
                $dfd_title = 'Attacco Borg su ' . $this->dest['planet_name'];
                break;
            default:
                $dfd_title = 'Borg attack on ' . $this->dest['planet_name'];
                break;
        }
        $action_status = 0;
        /* 11/07/08 - AC: Initialize here arrays atk/dfd_losses */
        $atk_losses = array(0, 0, 0, 0, 0);
        $dfd_losses = array(0, 0, 0, 0, 0);
        if ($this->cmb[MV_CMB_WINNER] == MV_CMB_ATTACKER) {
            $atk_units = array(0, 0, 0, 0, 0);
            // 20/06/08 - AC: Borg as no "workers"
            for ($i = 0; $i < count($atk_fleets); ++$i) {
                $atk_units[0] += $atk_fleets[$i]['unit_1'];
                $atk_units[1] += $atk_fleets[$i]['unit_2'];
                $atk_units[2] += $atk_fleets[$i]['unit_3'];
                $atk_units[3] += $atk_fleets[$i]['unit_4'];
            }
            $dfd_units = array($this->dest['unit_1'], $this->dest['unit_2'], $this->dest['unit_3'], $this->dest['unit_4'], $this->dest['resource_4']);
            // Are there some defenders?
            if (array_sum($dfd_units) > 0) {
                $ucmb = UnitFight($atk_units, $this->move['user_race'], $dfd_units, $this->dest['user_race'], $this->mid);
                $n_atk_alive = array_sum($ucmb[0]);
                $atk_alive = $ucmb[0];
                $dfd_alive = $ucmb[1];
            } else {
                $n_atk_alive = 1;
                $atk_alive = $atk_units;
                $dfd_alive = array(0, 0, 0, 0, 0);
            }
            // 01/07/08 - AC: Count losses for each part
            for ($i = 0; $i < 5; ++$i) {
                $atk_losses[$i] = $atk_units[$i] - $atk_alive[$i];
                $dfd_losses[$i] = $dfd_units[$i] - $dfd_alive[$i];
            }
            // If there are no more attackers, the defender
            // always won even if the defending had no units
            if ($n_atk_alive == 0) {
                // In order to eliminate the troops of the aggressor, simply reset all transporters
                // 21/06/08 - AC: Keep out workers from battle!
                $sql = 'UPDATE ships
                INNER JOIN ship_templates ON ships.template_id = ship_templates.id
                        SET unit_1 = ship_templates.min_unit_1,
                        unit_2 = ship_templates.min_unit_2,
                        unit_3 = ship_templates.min_unit_3,
                        unit_4 = ship_templates.min_unit_4
                WHERE fleet_id IN (' . $this->fleet_ids_str . ')';
                if (!$this->db->query($sql)) {
                    return $this->log(MV_M_DATABASE, 'Could not update ship fleets unit transport data! SKIP');
                }
                // Simply update the defender troops
                $sql = 'UPDATE planets
                SET unit_1 = ' . $ucmb[1][0] . ',
                    unit_2 = ' . $ucmb[1][1] . ',
                    unit_3 = ' . $ucmb[1][2] . ',
                    unit_4 = ' . $ucmb[1][3] . ',
                    resource_4 = ' . $ucmb[1][4] . '
                WHERE planet_id = ' . $this->move['dest'];
                if (!$this->db->query($sql)) {
                    return $this->log(MV_M_DATABASE, 'Could not update dest planet units data! SKIP');
                }
                $action_status = -1;
                // #############################################################################
                // 03/04/08 - AC: Retrieve player language
                switch ($this->move['language']) {
                    case 'GER':
                        $atk_title = 'Angriff auf ' . $this->dest['planet_name'] . ' teilweise erfolgreich';
                        break;
                    case 'ITA':
                        $atk_title = 'Attacco su ' . $this->dest['planet_name'] . ' parzialmente riuscito';
                        break;
                    default:
                        $atk_title = 'Attack on ' . $this->dest['planet_name'] . ' partially successful';
                        break;
                }
            } else {
                account_log($this->move['user_id'], $this->dest['user_id'], 4);
                // Export a ruler switch
                // We need the number of planets, which owns the colonizing
                // (we want planet_owner_enum degree of certainty)
                $sql = 'SELECT COUNT(planet_id) AS n_planets
                FROM planets
                WHERE planet_owner = ' . $this->move['user_id'];
                if (($pcount = $this->db->queryrow($sql)) === false) {
                    $this->log(MV_M_DATABASE, 'Could not query planets count data! CONTINUE AND USE INSTABLE VALUE');
                    $n_planets = $this->move['user_planets'];
                } else {
                    $n_planets = $pcount['n_planets'];
                }
                // We accommodate as much troops, as on the colony ship were, on the planet
                $planet_units = array(0, 0, 0, 0);
                // 25/06/08 - AC: Check available space on target planet
                $sql = 'SELECT max_units FROM planets WHERE planet_id = ' . $this->move['dest'];
                if (($plspace = $this->db->queryrow($sql)) === false) {
                    $this->log(MV_M_DATABASE, 'Could not query planet max units data! CONTINUE AND USE INSTABLE VALUE');
                    $plspace['max_units'] = 1300;
                    // Minimum size available
                }
                if ($atk_alive[0] * 2 + $atk_alive[1] * 3 + $atk_alive[2] * 4 + $atk_alive[3] * 4 > $plspace['max_units']) {
                    $this->log(MV_M_NOTICE, 'Alive units exceed planet maximum units, we need to recall aboard the surplus');
                    // AC: Recall aboard the surplus troops
                    $i = 0;
                    while ($n_atk_alive) {
                        if ($planet_units[0] * 2 + $planet_units[1] * 3 + $planet_units[2] * 4 + $planet_units[3] * 4 >= $plspace['max_units']) {
                            break;
                        }
                        if ($atk_alive[$i] > 0) {
                            $planet_units[$i]++;
                            $atk_alive[$i]--;
                            $n_atk_alive--;
                        } else {
                            $i++;
                        }
                    }
                    // Borg doesn't have cargo, simply use the Cube
                    /*
                                $sql = 'UPDATE ship_fleets
                           SET unit_1 = '.$atk_alive[0].',
                               unit_2 = '.$atk_alive[1].',
                               unit_3 = '.$atk_alive[2].',
                               unit_4 = '.$atk_alive[3].'
                           WHERE fleet_id = '.$this->fleet_ids_str;
                    
                                if(!$this->db->query($sql)) {
                       $this->log(MV_M_DATABASE, 'Could not update fleets units data! CONTINUE');
                                }
                    * 
                    */
                } else {
                    for ($i = 0; $i <= 3; ++$i) {
                        $planet_units[$i] += $atk_alive[$i];
                    }
                    /*
                                $sql = 'UPDATE ship_fleets
                           SET unit_1 = 0,
                               unit_2 = 0,
                               unit_3 = 0,
                               unit_4 = 0
                           WHERE fleet_id IN ('.$this->fleet_ids_str.')';
                    
                                if(!$this->db->query($sql)) {
                       return $this->log(MV_M_DATABASE, 'Could not update ship fleets unit transport data! SKIP');
                                }
                    * 
                    */
                }
                $sql = 'DELETE FROM scheduler_instbuild
                WHERE planet_id = ' . $this->move['dest'];
                if (!$this->db->query($sql)) {
                    $this->log(MV_M_DATABASE, 'Could not delete scheduler instbuild data! CONTINUE');
                }
                $sql = 'DELETE FROM scheduler_research
                WHERE planet_id = ' . $this->move['dest'];
                if (!$this->db->query($sql)) {
                    $this->log(MV_M_DATABASE, 'Could not delete scheduler research data! CONTINUE');
                }
                global $NUM_BUILDING, $MAX_BUILDING_LVL;
                // As someone yes number == max_index sets
                $n_buildings = $NUM_BUILDING + 1;
                $building_levels = array();
                $building_damage = 0.01 * mt_rand(40, 60);
                // Set new home world
                if ($this->move['dest'] == $this->dest['user_capital']) {
                    for ($i = 0; $i < $n_buildings; ++$i) {
                        $j = $i + 1;
                        $building_levels[$i] = (int) ($this->dest['building_' . $j] > $MAX_BUILDING_LVL[0][$i]) ? $MAX_BUILDING_LVL[0][$i] : round($this->dest['building_' . $j] * $building_damage, 0);
                    }
                    if ($this->dest['user_planets'] == 1) {
                        // #############################################################################
                        // 03/04/08 - AC: Retrieve player language
                        switch ($this->dest['language']) {
                            case 'GER':
                                $msg_title = 'Verlust aller deiner Planeten';
                                $msg_body = 'Da du alle deine Planeten verloren hast, wurde f&uuml;r dich ein neuer Planet an einer zuf&uuml;ligen Stelle der Galaxie erstellt.';
                                break;
                            case 'ITA':
                                $msg_title = 'Perdita di tutti i pianeti';
                                $msg_body = 'Dato che hai perduto tutti i pianeti ne &egrave; stato creato uno nuovo nella galassia.';
                                break;
                            default:
                                $msg_title = 'Loss of all your planets';
                                $msg_body = 'Since you lost all your planets a new one have been created in the body of the galaxy.';
                                break;
                        }
                        SystemMessage($this->dest['user_id'], $msg_title, $msg_body);
                    } else {
                        if (!$this->db->query('SET @i=0')) {
                            return $this->log(MV_M_DATABASE, 'Could not set sql iterator variable for planet owner enum! SKIP');
                        }
                        $sql = 'UPDATE planets
                        SET planet_owner_enum = (@i := (@i + 1))-1
                        WHERE planet_owner = ' . $this->dest['user_id'] . '
                        ORDER BY planet_owned_date ASC, planet_id ASC';
                        if (!$this->db->query($sql)) {
                            return $this->log(MV_M_DATABASE, 'Could not update planet owner enum data! SKIP');
                        }
                        $sql = 'SELECT planet_id
                        FROM planets
                        WHERE planet_owner = ' . $this->dest['user_id'] . '
                        ORDER BY planet_owner_enum ASC
                        LIMIT 1';
                        if (($first_planet = $this->db->queryrow($sql)) === false) {
                            return $this->log(MV_M_DATABASE, 'Could not query first planet data! SKIP');
                        }
                        $sql = 'UPDATE user
                        SET last_active = ' . time() . ',
                            last_ip = "0.0.0.0",
                            user_capital = ' . $first_planet['planet_id'] . ',
                            pending_capital_choice = 1
                        WHERE user_id = ' . $this->dest['user_id'];
                        if (!$this->db->query($sql)) {
                            return $this->log(MV_M_DATABASE, 'Could not update user capital data! SKIP');
                        }
                    }
                } else {
                    for ($i = 0; $i < $n_buildings; ++$i) {
                        $building_levels[$i] = round($this->dest['building_' . ($i + 1)] * $building_damage, 0);
                    }
                }
                $sql = 'DELETE FROM resource_trade
                WHERE planet = ' . $this->move['dest'];
                if (!$this->db->query($sql)) {
                    $this->log(MV_M_DATABASE, 'Could not delete resource trade data! CONTINUE');
                }
                $sql = 'SELECT ship_id
                FROM ship_trade
                WHERE scheduler_processed = 0 AND
                      planet = ' . $this->move['dest'];
                if (!($q_shipt = $this->db->query($sql))) {
                    $this->log(MV_M_DATABASE, 'Could not query ship trade data! CONTINUE');
                } else {
                    $ship_ids = array();
                    while ($_ship = $this->db->fetchrow($q_shipt)) {
                        $ship_ids[] = $_ship['ship_id'];
                    }
                    if (count($ship_ids) > 0) {
                        $sql = 'UPDATE ships
                        SET ship_untouchable = 0
                        WHERE ship_id IN (' . implode(',', $ship_ids) . ')';
                        if (!$this->db->query($sql)) {
                            $this->log(MV_M_DATABASE, 'Could not update ships untouchable data! CONTINUE');
                        }
                    }
                }
                $sql = 'DELETE FROM ship_trade
                WHERE scheduler_processed = 0 AND
                      planet = ' . $this->move['dest'];
                if (!$this->db->query($sql)) {
                    $this->log(MV_M_DATABASE, 'Could not delete ship trade data! CONTINUE');
                }
                $sql = 'DELETE FROM scheduler_shipbuild WHERE planet_id = ' . $this->move['dest'];
                if (!$this->db->query($sql)) {
                    $this->log(MV_M_DATABASE, 'Could not delete ship building queue! CONTINUE');
                }
                if ($this->dest['planet_type'] == "m" || $this->dest['planet_type'] == "o" || $this->dest['planet_type'] == "p" || $this->dest['planet_type'] == "x" || $this->dest['planet_type'] == "y" || $this->dest['planet_type'] == "f" || $this->dest['planet_type'] == "g") {
                    $def_tech_lev = 9;
                } else {
                    $def_tech_lev = 6;
                }
                $sql = 'UPDATE planets
                SET planet_owner = ' . $this->move['user_id'] . ',
                    planet_name = "Unimatrix #' . $this->move['dest'] . '",
                    best_mood = 0,
                    best_mood_user = 0,
                    planet_owned_date = ' . time() . ',
                    planet_owner_enum = ' . ($n_planets - 1) . ',
                    resource_4 = ' . $ucmb[1][4] . ',
                    planet_next_attack = 0,
                    planet_attack_ships = 0,
                    planet_attack_type = 0,
                    research_3 = ' . $def_tech_lev . ',
                    research_4 = ' . $def_tech_lev . ',
                    research_5 = ' . $def_tech_lev . ',
                    recompute_static = 1,
                    building_1 = 9,
                    building_2 = ' . $building_levels[1] . ',
                    building_3 = ' . $building_levels[2] . ',
                    building_4 = ' . $building_levels[3] . ',
                    building_5 = 9,
                    building_6 = 9,
                    building_7 = ' . $building_levels[6] . ',
                    building_8 = ' . $building_levels[7] . ',
                    building_9 = ' . $building_levels[8] . ',
                    building_10 = ' . $building_levels[9] . ',
                    building_11 = ' . $building_levels[10] . ',
                    building_12 = ' . $building_levels[11] . ',
                    building_13 = ' . $building_levels[12] . ',
                    unit_1 = ' . $planet_units[0] . ',
                    unit_2 = ' . $planet_units[1] . ',
                    unit_3 = ' . $planet_units[2] . ',
                    unit_4 = ' . $planet_units[3] . ',
                    unit_5 = 0,
                    unit_6 = 0,
                    workermine_1 = 100,
                    workermine_2 = 100,
                    workermine_3 = 100,
                    catresearch_1 = 0,
                    catresearch_2 = 0,
                    catresearch_3 = 0,
                    catresearch_4 = 0,
                    catresearch_5 = 0,
                    catresearch_6 = 0,
                    catresearch_7 = 0,
                    catresearch_8 = 0,
                    catresearch_9 = 0,
                    catresearch_10 = 0,
                    unittrainid_1 = 0, unittrainid_2 = 0, unittrainid_3 = 0, unittrainid_4 = 0, unittrainid_5 = 0, unittrainid_6 = 0, unittrainid_7 = 0, unittrainid_8 = 0, unittrainid_9 = 0, unittrainid_10 = 0, 
                    unittrainnumber_1 = 0, unittrainnumber_2 = 0, unittrainnumber_3 = 0, unittrainnumber_4 = 0, unittrainnumber_5 = 0, unittrainnumber_6 = 0, unittrainnumber_7 = 0, unittrainnumber_8 = 0, unittrainnumber_9 = 0, unittrainnumber_10 = 0, 
                    unittrainnumberleft_1 = 0, unittrainnumberleft_2 = 0, unittrainnumberleft_3 = 0, unittrainnumberleft_4 = 0, unittrainnumberleft_5 = 0, unittrainnumberleft_6 = 0, unittrainnumberleft_7 = 0, unittrainnumberleft_8 = 0, unittrainnumberleft_9 = 0, unittrainnumberleft_10 = 0, 
                    unittrainendless_1 = 0, unittrainendless_2 = 0, unittrainendless_3 = 0, unittrainendless_4 = 0, unittrainendless_5 = 0, unittrainendless_6 = 0, unittrainendless_7 = 0, unittrainendless_8 = 0, unittrainendless_9 = 0, unittrainendless_10 = 0, 
                    unittrain_actual = 0,
                    unittrainid_nexttime=0,
                    planet_insurrection_time=0
                WHERE planet_id = ' . $this->move['dest'];
                if (!$this->db->query($sql)) {
                    return $this->log(MV_M_DATABASE, 'Could not update planets data! SKIP');
                }
                // DC >>>> Historycal record in planet_details, with label '29'
                $sql = 'INSERT INTO planet_details (planet_id, user_id, alliance_id, source_uid, source_aid, timestamp, log_code, defeat_uid, defeat_aid)
                VALUES (' . $this->move['dest'] . ', 0, 0, 0, 0, ' . time() . ', 29,' . $this->dest['user_id'] . ', ' . $this->dest['user_alliance'] . ')';
                if (!$this->db->query($sql)) {
                    $this->log(MV_M_DATABASE, 'Could not update planet details data! CONTINUE');
                }
                // DC <<<<
                // DC >>>> Update borg_target table; the $this->dest['user_id'] entry may not exists, so do not check error
                $sql = 'UPDATE borg_target SET battle_lost = battle_lost + 1,
                                       planets_back = planets_back + 1,
                                       under_attack = under_attack - 1
                WHERE user_id = ' . $this->dest['user_id'];
                $this->db->query($sql);
                // DC <<<<
                // DC >>>>
                $sql = 'DELETE FROM borg_npc_target WHERE planet_id = ' . $this->move['dest'];
                $this->db->query($sql);
                $sql = 'UPDATE scheduler_shipmovement SET action_code = 11
                WHERE action_code = 46 AND move_id <> ' . $this->mid . '
                AND move_status = 0 AND user_id = ' . BORG_USERID . ' AND dest = ' . $this->move['dest'];
                $this->db->query($sql);
                // DC <<<<
                // DC >>>> Settlers management
                if ($this->dest['user_id'] == INDEPENDENT_USERID) {
                    $sql = 'SELECT sr.user_id, u.language FROM settlers_relations sr
                    LEFT JOIN user u ON u.user_id = sr.user_id
                    WHERE planet_id = ' . $this->move['dest'] . ' GROUP BY sr.user_id';
                    if ($m_u_q = $this->db->queryrowset($sql)) {
                        foreach ($m_u_q as $m_u) {
                            $log_data = array($this->move['dest'], $this->dest['planet_name'], 0, '', 103);
                            switch ($m_u['language']) {
                                case 'GER':
                                    $log_title = 'Automatic distress message from ';
                                    break;
                                case 'ITA':
                                    $log_title = 'Richiesta automatica di soccorso dalla colonia ';
                                    break;
                                default:
                                    $log_title = 'Automatic distress message from ';
                                    break;
                            }
                            add_logbook_entry($m_u['user_id'], LOGBOOK_SETTLERS, $log_title . $this->dest['planet_name'], $log_data);
                        }
                        $sql = 'DELETE FROM settlers_relations WHERE planet_id = ' . $this->move['dest'];
                        $this->db->query($sql);
                    }
                }
                // DC <<<< Settlers management
                if (!$this->db->query('SET @i=0')) {
                    return $this->log(MV_M_DATABASE, 'Could not set sql iterator variable for planet owner enum (the invading player)! SKIP');
                }
                $sql = 'UPDATE planets
                SET planet_owner_enum = (@i := (@i + 1))-1
                WHERE planet_owner = ' . $this->move['user_id'] . '
                ORDER BY planet_owned_date ASC, planet_id ASC';
                if (!$this->db->query($sql)) {
                    return $this->log(MV_M_DATABASE, 'Could not update planet owner enum data (the invading player)! SKIP');
                }
                $sql = 'UPDATE ships
                SET user_id = ' . $this->move['user_id'] . '
                WHERE fleet_id = -' . $this->move['dest'];
                if (!$this->db->query($sql)) {
                    return $this->log(MV_M_DATABASE, 'Could not update ships location data! SKIP');
                }
                $action_status = 1;
                // #############################################################################
                // 03/04/08 - AC: Retrieve player language
                switch ($this->move['language']) {
                    case 'GER':
                        $atk_title = 'Angriff auf ' . $this->dest['planet_name'] . ' erfolgreich';
                        break;
                    case 'ITA':
                        $atk_title = 'Attacco su ' . $this->dest['planet_name'] . ' riuscito';
                        break;
                    default:
                        $atk_title = 'Attack on ' . $this->dest['planet_name'] . ' successful';
                        break;
                }
            }
            // DC >>>>
            if ($this->dest['user_id'] == INDEPENDENT_USERID) {
                $sql = 'SELECT sr.user_id, u.language FROM settlers_relations sr
                LEFT JOIN user u ON u.user_id = sr.user_id
                WHERE planet_id = ' . $this->move['dest'] . ' GROUP BY sr.user_id';
                if ($m_u_q = $this->db->queryrowset($sql)) {
                    foreach ($m_u_q as $m_u) {
                        $log_data = array($this->move['dest'], $this->dest['planet_name'], 0, '', 104);
                        switch ($m_u['language']) {
                            case 'GER':
                                $log_title = 'Distress call from ';
                                break;
                            case 'ITA':
                                $log_title = 'Richiesta di soccorso dalla colonia ';
                                break;
                            default:
                                $log_title = 'Distress call from ';
                                break;
                        }
                        add_logbook_entry($m_u['user_id'], LOGBOOK_SETTLERS, $log_title . $this->dest['planet_name'], $log_data);
                    }
                }
            }
            // DC <<<<
            $sql = 'UPDATE ship_fleets
            SET planet_id = ' . $this->move['dest'] . ',
                move_id = 0
            WHERE fleet_id IN (' . $this->fleet_ids_str . ')';
            if (!$this->db->query($sql)) {
                // Here one could also report and then go on
                return $this->log(MV_M_DATABASE, 'Could not update fleets location data! SKIP');
            }
        } else {
            // #############################################################################
            // 03/04/08 - AC: Retrieve player language
            switch ($this->move['language']) {
                case 'GER':
                    $atk_title = 'Angriff auf ' . $this->dest['planet_name'] . ' fehlgeschlagen';
                    break;
                case 'ITA':
                    $atk_title = 'Attacco su ' . $this->dest['planet_name'] . ' fallito';
                    break;
                default:
                    $atk_title = 'Attack on ' . $this->dest['planet_name'] . ' failed';
                    break;
            }
            // DC >>>> Begin Borg Adaption Phase!!!
            $adaption_chance_array = array('no_event' => 90, 'adapt_PRI' => 13, 'adapt_SEC' => 12, 'adapt_SHI' => 7, 'adapt_ARM' => 9, 'adapt_REA' => 4, 'adapt_RED' => 5, 'adapt_AGI' => 3, 'adapt_ROF' => 4, 'adapt_ROF2' => 2);
            $adaption_array = array();
            foreach ($adaption_chance_array as $event => $probability) {
                for ($i = 0; $i < $probability; ++$i) {
                    $adaption_array[] = $event;
                }
            }
            $adaption_event = $adaption_array[array_rand($adaption_array)];
            if ($adaption_event == 'no_event') {
                $this->log(MV_M_NOTICE, 'Borg Adaption Phase not occured this time.');
            } else {
                $this->log(MV_M_NOTICE, 'Borg Adaption Phase begin NOW.');
                $borg_bot = $this->db->queryrow('SELECT ship_template2 FROM borg_bot WHERE id = 1');
                $borgcube_tp = $this->db->queryrow('SELECT * FROM ship_templates WHERE owner = ' . BORG_USERID . ' AND id = ' . $borg_bot['ship_template2']);
                $newvalue1 = $borgcube_tp['value_1'];
                $newvalue2 = $borgcube_tp['value_2'];
                $newvalue4 = $borgcube_tp['value_4'];
                $newvalue5 = $borgcube_tp['value_5'];
                $newvalue6 = $borgcube_tp['value_6'];
                $newvalue7 = $borgcube_tp['value_7'];
                $newvalue8 = $borgcube_tp['value_8'];
                $newrof = $borgcube_tp['rof'];
                $newrof2 = $borgcube_tp['rof2'];
                switch ($adaption_event) {
                    case 'adapt_PRI':
                        $newvalue1 = $newvalue1 * 1.015;
                        $this->log(MV_M_NOTICE, 'Borg Primary Attack Value ++.');
                        break;
                    case 'adapt_SEC':
                        $newvalue2 = $newvalue2 * 1.015;
                        $this->log(MV_M_NOTICE, 'Borg Secondary Attack Value ++.');
                        break;
                    case 'adapt_SHI':
                        $newvalue4 = $newvalue4 * 1.025;
                        $this->log(MV_M_NOTICE, 'Borg Armor Value ++.');
                        break;
                    case 'adapt_ARM':
                        $newvalue5 = $newvalue5 * 1.02;
                        $this->log(MV_M_NOTICE, 'Borg Shield Value ++.');
                        break;
                    case 'adapt_REA':
                        $newvalue6 = $newvalue6 + 1;
                        $this->log(MV_M_NOTICE, 'Borg Reaction Value ++.');
                        break;
                    case 'adapt_RED':
                        $newvalue7 = $newvalue7 + 1;
                        $this->log(MV_M_NOTICE, 'Borg Readiness Value ++.');
                        break;
                    case 'adapt_AGI':
                        $newvalue8 = $newvalue8 + 1;
                        $this->log(MV_M_NOTICE, 'Borg Agility Value ++.');
                        break;
                    case 'adapt_ROF':
                        $newrof = $newrof + 1;
                        $this->log(MV_M_NOTICE, '+++ Borg ROF Value +++.');
                        break;
                    case 'adapt_ROF2':
                        $newrof2 = $newrof2 + 1;
                        $this->log(MV_M_NOTICE, '+++ Borg ROF2 Value +++.');
                        break;
                }
                $oldversion = 0;
                sscanf($borgcube_tp['name'], "Adapted Cube#%u", $oldversion);
                $sql = 'INSERT INTO ship_templates (owner, timestamp, name, description, race, ship_torso, ship_class,
                                                component_1, component_2, component_3, component_4, component_5,
                                                component_6, component_7, component_8, component_9, component_10,
                                                value_1, value_2, value_3, value_4, value_5,
                                                value_6, value_7, value_8, value_9, value_10,
                                                value_11, value_12, value_13, value_14, value_15,
                                                resource_1, resource_2, resource_3, resource_4, unit_5, unit_6,
                                                min_unit_1, min_unit_2, min_unit_3, min_unit_4,
                                                max_unit_1, max_unit_2, max_unit_3, max_unit_4,
                                                buildtime, rof, rof2, max_torp)
                VALUES ("' . $borgcube_tp['owner'] . '","' . time() . '","Adapted Cube#' . ($oldversion + 1) . '","Assimilation ship","' . $borgcube_tp['race'] . '",9,3,
                            -1,-1,-1,-1,-1,
                            -1,-1,-1,-1,-1,
                            "' . $newvalue1 . '","' . $newvalue2 . '","140","' . $newvalue4 . '","' . $newvalue5 . '",
                            "' . $newvalue6 . '","' . $newvalue7 . '","' . $newvalue8 . '","50","10",
                            "42","0","300","300","0",
                            "500000","500000","500000","50000","39","16",
                            "294","0","0","6",
                            "9294","6000","3000","6",
                            0, ' . $newrof . ', ' . $newrof2 . ', 1000)';
                $this->db->query($sql);
                $newtpid = $this->db->insert_id();
                $this->db->query('UPDATE borg_bot SET ship_template2 = ' . $newtpid . ' WHERE id = 1 ');
                $this->log(MV_M_NOTICE, 'Borg Adaption Phase ends NOW.');
            }
            // DC <<<< End Borg Adaption Phase
            // DC >>>> Settlers management
            if ($this->dest['user_id'] == INDEPENDENT_USERID) {
                // Check for defenders
                if ($n_st_user > 0) {
                    $sql = 'SELECT user_id from borg_target WHERE user_id IN (' . implode(",", $st_user) . ')';
                    if (!($def_q = $this->db->query($sql))) {
                        $this->log(MV_M_DATABASE, 'Could not query borg_target data! ' . $sql);
                    }
                    $num_row = $this->db->num_rows($def_q);
                    if ($num_row > 0) {
                        // Update priority of this planet!!!
                        $sql = 'UPDATE borg_npc_target SET priority = priority + ' . 10 * $num_row . ' WHERE planet_id = ' . $this->move['dest'];
                        if (!$this->db->query($sql)) {
                            $this->log(MV_M_DATABASE, 'Could not update borg_npc_target data! ' . $sql);
                        }
                        $def_i_q = $this->db->fetchrowset($def_q);
                        foreach ($def_i_q as $item_def_q) {
                            $sql = 'UPDATE borg_target SET battle_win = battle_win + 1 WHERE user_id = ' . $item_def_q['user_id'];
                            if (!$this->db->query($sql)) {
                                $this->log(MV_M_DATABASE, 'Could not update borg_target data! ' . $sql);
                            }
                        }
                    }
                }
                $sql = 'UPDATE borg_npc_target SET tries = tries + 1,
                                           live_attack = live_attack - 1 
                WHERE planet_id = ' . $this->move['dest'];
                $this->db->query($sql);
                $sql = 'SELECT sr.user_id, u.language FROM settlers_relations sr
                LEFT JOIN user u ON u.user_id = sr.user_id
                WHERE planet_id = ' . $this->move['dest'] . ' GROUP BY sr.user_id';
                if ($m_u_q = $this->db->queryrowset($sql)) {
                    foreach ($m_u_q as $m_u) {
                        $log_data = array($this->move['dest'], $this->dest['planet_name'], 0, '', 104);
                        switch ($m_u['language']) {
                            case 'GER':
                                $log_title = 'Distress call from ';
                                break;
                            case 'ITA':
                                $log_title = 'Richiesta di soccorso dalla colonia ';
                                break;
                            default:
                                $log_title = 'Distress call from ';
                                break;
                        }
                        add_logbook_entry($m_u['user_id'], LOGBOOK_SETTLERS, $log_title . $this->dest['planet_name'], $log_data);
                    }
                }
            }
            // DC <<<< Settlers management
            // 01/07/08 - The attacker has lost the fleets
            for ($i = 0; $i < count($atk_fleets); ++$i) {
                $atk_losses[0] += $atk_fleets[$i]['unit_1'];
                $atk_losses[1] += $atk_fleets[$i]['unit_2'];
                $atk_losses[2] += $atk_fleets[$i]['unit_3'];
                $atk_losses[3] += $atk_fleets[$i]['unit_4'];
                $atk_losses[4] += $atk_fleets[$i]['resource_4'];
            }
        }
        // #############################################################################
        // Write logbook
        $log1_data = array(46, $this->move['user_id'], $this->move['start'], $this->start['planet_name'], $this->start['user_id'], $this->move['dest'], $this->dest['planet_name'], $this->dest['user_id'], MV_CMB_ATTACKER, $this->cmb[MV_CMB_WINNER] == MV_CMB_ATTACKER ? 1 : 0, 0, 0, $atk_fleets, $dfd_fleets);
        $log2_data = array(46, $this->move['user_id'], $this->move['start'], $this->start['planet_name'], $this->start['user_id'], $this->move['dest'], $this->dest['planet_name'], $this->dest['user_id'], MV_CMB_DEFENDER, $this->cmb[MV_CMB_WINNER] == MV_CMB_DEFENDER ? 1 : 0, 0, 0, $atk_fleets, $dfd_fleets);
        $log1_data[17] = $log2_data[17] = $action_status;
        $log1_data[20] = $this->dest['user_race'];
        $log2_data[20] = $this->move['user_race'];
        for ($i = 0; $i < 5; ++$i) {
            $log1_data[18][$i] = $log2_data[19][$i] = $atk_losses[$i];
            $log1_data[19][$i] = $log2_data[18][$i] = $dfd_losses[$i];
        }
        //$log1_data[19][4] = $log2_data[18][4] = (int)$dfd_losses[4]; already done by for above...
        $log2_data[14] = $this->cmb[MV_CMB_KILLS_PLANETARY];
        $log1_data[10] = $this->cmb[MV_CMB_KILLS_EXT];
        $log2_data[10] = $this->cmb[MV_CMB_KILLS_EXT];
        add_logbook_entry($this->move['user_id'], LOGBOOK_TACTICAL, $atk_title, $log1_data);
        add_logbook_entry($this->dest['user_id'], LOGBOOK_TACTICAL, $dfd_title, $log2_data);
        if ($n_st_user > 0) {
            $log2_data[2] = $this->move['dest'];
            $log2_data[3] = $this->dest['planet_name'];
            $log2_data[4] = $this->dest['user_id'];
            $log2_data[5] = 0;
            $log2_data[6] = 0;
            $log2_data[7] = 0;
            $log2_data[16] = 1;
            for ($i = 0; $i < $n_st_user; ++$i) {
                // #############################################################################
                // 03/04/08 - AC: Retrieve player language
                $log_title = 'One of your allies defended ' . $this->dest['planet_name'];
                $sql = 'SELECT language FROM user WHERE user_id = ' . $st_user[$i];
                if (!($lang = $this->db->queryrow($sql))) {
                    $this->log(MV_M_DATABASE, 'Could not retrieve player language');
                } else {
                    switch ($lang['language']) {
                        case 'GER':
                            $log_title = 'Verb&uuml;ndeten bei ' . $this->dest['planet_name'] . ' verteidigt';
                            break;
                        case 'ENG':
                            $log_title = 'Joined the defenders of the ' . $this->dest['planet_name'] . ' planet';
                            break;
                        case 'ITA':
                            $log_title = 'Difesa alleata presso ' . $this->dest['planet_name'];
                            break;
                    }
                }
                add_logbook_entry($st_user[$i], LOGBOOK_TACTICAL, $log_title, $log2_data);
            }
        }
        return MV_EXEC_OK;
    }
示例#5
0
 function _action_main()
 {
     account_log($this->move['user_id'], $this->dest['user_id'], 0);
     // #############################################################################
     // Determine the total raw
     $sql = 'SELECT fleet_id,
                    resource_1, resource_2, resource_3, resource_4,
                    unit_1, unit_2, unit_3, unit_4, unit_5, unit_6
             FROM ship_fleets
             WHERE fleet_id IN (' . $this->fleet_ids_str . ')';
     if (($q_rfleets = $this->db->query($sql)) === false) {
         return $this->log(MV_M_DATABASE, 'Could not query fleets resources data! SKIP');
     }
     // #############################################################################
     // Transferred resources
     $wares_names = array('resource_1', 'resource_2', 'resource_3', 'resource_4', 'unit_1', 'unit_2', 'unit_3', 'unit_4', 'unit_5', 'unit_6');
     // $pwares -> Wares on the planet
     // $fwares -> Wares on the fleet
     // $twares -> transferred goods (needed for debt calculation)
     $pwares = $fwares = $twares = array();
     // Push check Var initialized
     $push = false;
     for ($i = 0; $i < count($wares_names); ++$i) {
         $pwares[$i] = (int) $this->dest[$wares_names[$i]];
         $twares[$i] = 0;
     }
     $planet_overloaded = false;
     while ($rfleet = $this->db->fetchrow($q_rfleets)) {
         $fleet_id = $rfleet['fleet_id'];
         $n_fwares = $rfleet['resource_1'] + $rfleet['resource_2'] + $rfleet['resource_3'] + $rfleet['resource_4'] + $rfleet['unit_1'] + $rfleet['unit_2'] + $rfleet['unit_3'] + $rfleet['unit_4'] + $rfleet['unit_5'] + $rfleet['unit_6'];
         if ($n_fwares == 0) {
             continue;
         }
         // Check try to push
         if ($this->move['user_id'] != $this->dest['user_id']) {
             $push = true;
             continue;
         }
         foreach ($pwares as $i => $p_value) {
             $value = $fwares[$i] = (int) $rfleet[$wares_names[$i]];
             if ($value == 0) {
                 continue;
             }
             if ($i == 3) {
                 if ($pwares[$i] + $value > $this->dest['max_worker']) {
                     $value = $this->dest['max_worker'] - $pwares[$i];
                     $planet_overloaded = true;
                 }
             } elseif ($i <= 2) {
                 if ($pwares[$i] + $value > $this->dest['max_resources']) {
                     $value = $this->dest['max_resources'] - $pwares[$i];
                     $planet_overloaded = true;
                 }
             } else {
                 if ($pwares[$i] + $value > $this->dest['max_units']) {
                     $value = $this->dest['max_units'] - $pwares[$i];
                     $planet_overloaded = true;
                 }
             }
             $fwares[$i] -= $value;
             $pwares[$i] += $value;
             $twares[$i] += $value;
         }
         $sql = 'UPDATE ship_fleets
                 SET resource_1 = ' . $fwares[0] . ',
                     resource_2 = ' . $fwares[1] . ',
                     resource_3 = ' . $fwares[2] . ',
                     resource_4 = ' . $fwares[3] . ',
                     unit_1 = ' . $fwares[4] . ',
                     unit_2 = ' . $fwares[5] . ',
                     unit_3 = ' . $fwares[6] . ',
                     unit_4 = ' . $fwares[7] . ',
                     unit_5 = ' . $fwares[8] . ',
                     unit_6 = ' . $fwares[9] . '
                 WHERE fleet_id = ' . $fleet_id;
         if (!$this->db->query($sql)) {
             return $this->log(MV_M_DATABASE, 'Could not update resource data of fleet #' . $fleet_id . '! SKIP');
         }
     }
     $sql = 'UPDATE planets
             SET resource_1 = ' . $pwares[0] . ',
                 resource_2 = ' . $pwares[1] . ',
                 resource_3 = ' . $pwares[2] . ',
                 resource_4 = ' . $pwares[3] . ',
                 unit_1 = ' . $pwares[4] . ',
                 unit_2 = ' . $pwares[5] . ',
                 unit_3 = ' . $pwares[6] . ',
                 unit_4 = ' . $pwares[7] . ',
                 unit_5 = ' . $pwares[8] . ',
                 unit_6 = ' . $pwares[9] . '
             WHERE planet_id = ' . $this->move['dest'];
     if (!$this->db->query($sql)) {
         return $this->log(MV_M_DATABASE, 'Could not update planets resource data! SKIP');
     }
     // Reworking of $ twares to the expected size of the debt calculation
     // (one might even rewrite, but why bother when it works...)
     $rfleets = array('n1' => $twares[0], 'n2' => $twares[1], 'n3' => $twares[2]);
     // #############################################################################
     // Check debts
     $sql = 'SELECT id, resource_1, resource_2, resource_3, ship_id
             FROM bidding_owed
             WHERE user = '******'user_id'] . ' AND
                   receiver = ' . $this->dest['user_id'] . '
             ORDER BY id ASC';
     if (!($q_debts = $this->db->query($sql))) {
         return $this->log(MV_M_DATABASE, 'Could not query bidding owed data! SKIP');
     }
     $n_debts = $this->db->num_rows($q_debts);
     if ($n_debts > 0) {
         while ($debt = $this->db->fetchrow($q_debts)) {
             if ($debt['resource_1'] > 0) {
                 if ($debt['resource_1'] >= $rfleets['n1']) {
                     $debt['resource_1'] -= $rfleets['n1'];
                     $rfleets['n1'] = 0;
                 } else {
                     $rfleets['n1'] -= $debt['resource_1'];
                     $debt['resource_1'] = 0;
                 }
             }
             if ($debt['resource_2'] > 0) {
                 if ($debt['resource_2'] >= $rfleets['n2']) {
                     $debt['resource_2'] -= $rfleets['n2'];
                     $rfleets['n2'] = 0;
                 } else {
                     $rfleets['n2'] -= $debt['resource_2'];
                     $debt['resource_2'] = 0;
                 }
             }
             if ($debt['resource_3'] > 0) {
                 if ($debt['resource_3'] >= $rfleets['n3']) {
                     $debt['resource_3'] -= $rfleets['n3'];
                     $rfleets['n3'] = 0;
                 } else {
                     $rfleets['n3'] -= $debt['resource_3'];
                     $debt['resource_3'] = 0;
                 }
             }
             if ($debt['resource_1'] <= 0 && $debt['resource_2'] <= 0 && $debt['resource_3'] <= 0 && $debt['ship_id'] <= 0) {
                 $sql = 'DELETE FROM bidding_owed
                         WHERE id = ' . $debt['id'];
                 if (!$this->db->query($sql)) {
                     return $this->log(MV_M_DATABASE, 'Could not delete bidding owed data! SKIP');
                 }
             } else {
                 $sql = 'UPDATE bidding_owed
                         SET resource_1 = ' . $debt['resource_1'] . ',
                             resource_2 = ' . $debt['resource_2'] . ',
                             resource_3 = ' . $debt['resource_3'] . '
                         WHERE id = ' . $debt['id'];
                 if (!$this->db->query($sql)) {
                     return $this->log(MV_M_DATABASE, 'Could not update bidding owed data! SKIP');
                 }
             }
         }
     }
     // #############################################################################
     // Ships return
     $sql = 'INSERT INTO scheduler_shipmovement (user_id, move_status, move_exec_started, start, dest, total_distance, remaining_distance, tick_speed, move_begin, move_finish, n_ships, action_code, action_data)
             VALUES (' . $this->move['user_id'] . ', 0, 0, ' . $this->move['dest'] . ', ' . $this->move['start'] . ', ' . $this->move['total_distance'] . ', ' . $this->move['total_distance'] . ', ' . $this->move['tick_speed'] . ', ' . $this->CURRENT_TICK . ', ' . ($this->CURRENT_TICK + ($this->move['move_finish'] - $this->move['move_begin'])) . ', ' . $this->move['n_ships'] . ', 12, "' . serialize(31) . '")';
     if (!$this->db->query($sql)) {
         return $this->log(MV_M_DATABASE, 'Could not create new movement for return! SKIP');
     }
     $new_move_id = $this->db->insert_id();
     if (!$new_move_id) {
         return $this->log(MV_M_ERROR, 'Could not get new move id! SKIP');
     }
     $sql = 'UPDATE ship_fleets
             SET move_id = ' . $new_move_id . '
             WHERE fleet_id IN (' . $this->fleet_ids_str . ')';
     if (!$this->db->query($sql)) {
         return $this->log(MV_M_DATABASE, 'Could not update fleets movement data! SKIP');
     }
     // #############################################################################
     // Create a logbook entry
     $sql = 'SELECT st.name, st.ship_torso, st.race,
                    COUNT(s.ship_id) AS n_ships
             FROM ship_templates st, ship_fleets f, ships s
             WHERE f.fleet_id IN (' . $this->fleet_ids_str . ') AND
                   s.template_id = st.id AND
                   s.fleet_id = f.fleet_id
             GROUP BY st.id
             ORDER BY st.ship_torso ASC, st.race ASC';
     if (!($q_stpls = $this->db->query($sql))) {
         return $this->log(MV_M_DATABASE, 'Could not query ships templates data! SKIP');
     }
     $log_data = array(31, $this->move['user_id'], $this->move['start'], $this->start['planet_name'], $this->start['user_id'], $this->move['dest'], $this->dest['planet_name'], $this->dest['user_id'], array(), $twares, $planet_overloaded, $push);
     while ($stpl = $this->db->fetchrow($q_stpls)) {
         $log_data[8][] = array($stpl['name'], $stpl['ship_torso'], $stpl['race'], $stpl['n_ships']);
     }
     // #############################################################################
     // 31/03/08 - AC: Retrieve player language
     switch ($this->move['language']) {
         case 'GER':
             $log_title1 = 'Transport bei ' . $this->dest['planet_name'] . ' nicht durchgeführt';
             $log_title3 = 'Transport bei ' . $this->dest['planet_name'] . ' durchgeführt';
             break;
         case 'ITA':
             $log_title1 = 'Trasporto per ' . $this->dest['planet_name'] . ' non compiuto';
             $log_title3 = 'Trasporto per ' . $this->dest['planet_name'] . ' compiuto';
             break;
         default:
             $log_title1 = 'Transport for ' . $this->dest['planet_name'] . ' not accomplished';
             $log_title3 = 'Transport for ' . $this->dest['planet_name'] . ' accomplished';
             break;
     }
     switch ($this->dest['language']) {
         case 'GER':
             $log_title2 = 'Transport bei ' . $this->dest['planet_name'] . ' konnte nicht abgeladen werden';
             $log_title4 = 'Transport bei ' . $this->dest['planet_name'] . ' erhalten';
             break;
         case 'ITA':
             $log_title2 = 'Trasporto per ' . $this->dest['planet_name'] . ' non scaricato';
             $log_title4 = 'Trasporto per ' . $this->dest['planet_name'] . ' ricevuto';
             break;
         default:
             $log_title2 = 'Transport for ' . $this->dest['planet_name'] . ' could not be unloaded';
             $log_title4 = 'Transport for ' . $this->dest['planet_name'] . ' received';
             break;
     }
     if ($push) {
         add_logbook_entry($this->move['user_id'], LOGBOOK_TACTICAL, $log_title1, $log_data);
         add_logbook_entry($this->dest['user_id'], LOGBOOK_TACTICAL, $log_title2, $log_data);
     } else {
         add_logbook_entry($this->move['user_id'], LOGBOOK_TACTICAL, $log_title3, $log_data);
         add_logbook_entry($this->dest['user_id'], LOGBOOK_TACTICAL, $log_title4, $log_data);
     }
     return MV_EXEC_OK;
 }