public static function generate_bonus($this_prototype_data, $this_robot_count = 8, $this_robot_class = 'master')
 {
     // Pull in global variables for this function
     global $mmrpg_index, $db;
     // Collect the robot index for calculation purposes
     $this_robot_index = $db->get_array_list("SELECT * FROM mmrpg_index_robots WHERE robot_flag_complete = 1;", 'robot_token');
     // Populate the battle options with the starter battle option
     $temp_rand_num = $this_robot_count;
     $temp_battle_token = $this_prototype_data['phase_battle_token'] . '-prototype-bonus-' . $this_robot_class;
     if ($this_robot_class == 'mecha') {
         $temp_battle_omega = rpg_battle::get_index_info('bonus-prototype-complete');
         $temp_battle_omega['battle_field_info']['field_name'] = 'Bonus Field';
     } elseif ($this_robot_class == 'master') {
         $temp_battle_omega = rpg_battle::get_index_info('bonus-prototype-complete-2');
         $temp_battle_omega['battle_field_info']['field_name'] = 'Bonus Field II';
     }
     // Populate the player's target robots with compatible class matches
     $temp_battle_omega['battle_target_player']['player_robots'] = array();
     $temp_counter = 0;
     foreach ($this_robot_index as $token => $info) {
         if (empty($info['robot_flag_complete']) || $info['robot_class'] != $this_robot_class) {
             continue;
         }
         $temp_counter++;
         $temp_robot_info = array();
         $temp_robot_info['robot_id'] = MMRPG_SETTINGS_TARGET_PLAYERID + $temp_counter;
         $temp_robot_info['robot_token'] = $info['robot_token'];
         $temp_robot_info['robot_core'] = $info['robot_core'];
         $temp_robot_info['robot_core2'] = $info['robot_core2'];
         $temp_battle_omega['battle_target_player']['player_robots'][] = $temp_robot_info;
     }
     //die('<pre>player_robots '.print_r($temp_battle_omega['battle_target_player']['player_robots'], true).'</pre>');
     // Continue defining battle variables for this mission
     $temp_battle_omega['flags']['bonus_battle'] = true;
     $temp_battle_omega['battle_token'] = $temp_battle_token;
     $temp_battle_omega['battle_size'] = '1x4';
     $temp_battle_omega['battle_phase'] = $this_prototype_data['battle_phase'];
     //if ($this_robot_class == 'mecha'){ $temp_battle_omega['battle_turns_limit'] = MMRPG_SETTINGS_BATTLETURNS_PERMECHA * $this_robot_count; }
     //elseif ($this_robot_class == 'master'){ $temp_battle_omega['battle_turns_limit'] = MMRPG_SETTINGS_BATTLETURNS_PERROBOT * $this_robot_count; }
     //$temp_battle_omega['battle_points'] = ceil(($this_prototype_data['battles_complete'] > 1 ? 100 : 1000) * $temp_rand_num);
     //shuffle($temp_battle_omega['battle_target_player']['player_robots']);
     // Create the randomized field multupliers
     $temp_types = $mmrpg_index['types'];
     $temp_allow_special = array();
     //, 'damage', 'recovery', 'experience'
     foreach ($temp_types as $key => $temp_type) {
         if (!empty($temp_type['type_class']) && $temp_type['type_class'] == 'special' && !in_array($temp_type['type_token'], $temp_allow_special)) {
             unset($temp_types[$key]);
         }
     }
     //$temp_battle_omega['battle_field_info']['field_multipliers']['experience'] = round((mt_rand(200, 300) / 100), 1);
     //$temp_battle_omega['battle_field_info']['field_type'] = $temp_types[array_rand($temp_types)]['type_token'];
     //do { $temp_battle_omega['battle_field_info']['field_type2'] = $temp_types[array_rand($temp_types)]['type_token'];
     //} while($temp_battle_omega['battle_field_info']['field_type2'] == $temp_battle_omega['battle_field_info']['field_type']);
     $temp_battle_omega['battle_field_info']['field_multipliers'] = array();
     while (count($temp_battle_omega['battle_field_info']['field_multipliers']) < 6) {
         $temp_type = $temp_types[array_rand($temp_types)];
         $temp_multiplier = 1;
         while ($temp_multiplier == 1) {
             $temp_multiplier = round(mt_rand(10, 990) / 100, 1);
         }
         $temp_battle_omega['battle_field_info']['field_multipliers'][$temp_type['type_token']] = $temp_multiplier;
         //if (count($temp_battle_omega['battle_field_info']['field_multipliers']) >= 6){ break; }
     }
     // Update the field type based on multipliers
     $temp_multipliers = $temp_battle_omega['battle_field_info']['field_multipliers'];
     asort($temp_multipliers);
     $temp_multipliers = array_keys($temp_multipliers);
     $temp_battle_omega['battle_field_info']['field_type'] = array_pop($temp_multipliers);
     $temp_battle_omega['battle_field_info']['field_type2'] = array_pop($temp_multipliers);
     // Collect the field types into a simple array
     $temp_field_types = array($temp_battle_omega['battle_field_info']['field_type'], $temp_battle_omega['battle_field_info']['field_type2']);
     // Give the robots a quick shuffle before sorting by core
     shuffle($temp_battle_omega['battle_target_player']['player_robots']);
     //die('<pre>player_robots '.print_r($temp_battle_omega['battle_target_player']['player_robots'], true).'</pre>');
     // Sort the robots by their relevance to the field type
     usort($temp_battle_omega['battle_target_player']['player_robots'], function ($r1, $r2) use($temp_field_types, $this_robot_index) {
         //global $temp_field_types, $this_robot_index;
         $r1_core = !empty($r1['robot_core']) ? $r1['robot_core'] : '';
         $r2_core = !empty($r2['robot_core']) ? $r2['robot_core'] : '';
         if (in_array($r1_core, $temp_field_types) && !in_array($r2_core, $temp_field_types)) {
             return -1;
         } elseif (!in_array($r1_core, $temp_field_types) && in_array($r2_core, $temp_field_types)) {
             return 1;
         } else {
             return 0;
         }
     });
     //die('<pre>field_types = '.implode(', ', $temp_field_types).' | player_robots '.print_r($temp_battle_omega['battle_target_player']['player_robots'], true).'</pre>');
     $temp_battle_omega['battle_target_player']['player_robots'] = array_slice($temp_battle_omega['battle_target_player']['player_robots'], 0, $this_robot_count);
     // Calculate what level these bonus robots should be in the range of
     $temp_player_rewards = rpg_game::player_rewards($this_prototype_data['this_player_token']);
     $temp_total_level = 0;
     $temp_total_robots = 0;
     $temp_bonus_level_min = 100;
     $temp_bonus_level_max = 1;
     if (!empty($temp_player_rewards['player_robots'])) {
         foreach ($temp_player_rewards['player_robots'] as $token => $info) {
             $temp_level = !empty($info['robot_level']) ? $info['robot_level'] : 1;
             if ($temp_level > $temp_bonus_level_max) {
                 $temp_bonus_level_max = $temp_level;
             }
             if ($temp_level < $temp_bonus_level_min) {
                 $temp_bonus_level_min = $temp_level;
             }
             $temp_total_robots++;
         }
         //$temp_bonus_level_max = ceil($temp_total_level / $temp_total_robots);
         //$temp_bonus_level_min = ceil($temp_bonus_level_max / 3);
     }
     // Start all the point-based battle vars at zero
     $temp_battle_omega['battle_points'] = 0;
     $temp_battle_omega['battle_zenny'] = 0;
     $temp_battle_omega['battle_turns_limit'] = 0;
     $temp_battle_omega['battle_robots_limit'] = 0;
     // Define the possible items for bonus mission robot masters
     $possible_master_items = array('item-energy-upgrade', 'item-weapon-upgrade', 'item-target-module', 'item-charge-module', 'item-fortune-module', 'item-field-booster', 'item-attack-booster', 'item-defense-booster', 'item-speed-booster');
     foreach ($mmrpg_index['types'] as $token => $info) {
         if (!empty($info['type_class']) && $info['type_class'] == 'special') {
             continue;
         } elseif (in_array($token, array('copy', 'empty'))) {
             continue;
         }
         $possible_master_items[] = 'item-core-' . $token;
     }
     $possible_master_items_last_key = count($possible_master_items) - 1;
     // Loop through each of the bonus robots and update their levels
     foreach ($temp_battle_omega['battle_target_player']['player_robots'] as $key => $robot) {
         $robot['robot_level'] = mt_rand($temp_bonus_level_min, $temp_bonus_level_max);
         $index = rpg_robot::parse_index_info($this_robot_index[$robot['robot_token']]);
         if ($this_robot_class != 'mecha') {
             $robot['robot_item'] = $possible_master_items[mt_rand(0, $possible_master_items_last_key)];
         } else {
             $robot['robot_item'] = '';
         }
         $robot['robot_abilities'] = rpg_prototype::generate_abilities($index, $robot['robot_level'], 8, $robot['robot_item']);
         // Increment the battle's turn limit based on the class of target robot
         if ($index['robot_class'] == 'master') {
             $temp_battle_omega['battle_turns_limit'] += MMRPG_SETTINGS_BATTLETURNS_PERROBOT;
         } elseif ($index['robot_class'] == 'mecha') {
             $temp_battle_omega['battle_turns_limit'] += MMRPG_SETTINGS_BATTLETURNS_PERMECHA;
         } elseif ($index['robot_class'] == 'boss') {
             $temp_battle_omega['battle_turns_limit'] += MMRPG_SETTINGS_BATTLETURNS_PERBOSS;
         }
         // Increment the battle's point reward based on the class of target robot
         if ($index['robot_class'] == 'master') {
             $temp_battle_omega['battle_points'] += $robot['robot_level'] * MMRPG_SETTINGS_BATTLEPOINTS_PERROBOT;
         } elseif ($index['robot_class'] == 'mecha') {
             $temp_battle_omega['battle_points'] += $robot['robot_level'] * MMRPG_SETTINGS_BATTLEPOINTS_PERMECHA;
         } elseif ($index['robot_class'] == 'boss') {
             $temp_battle_omega['battle_points'] += $robot['robot_level'] * MMRPG_SETTINGS_BATTLEPOINTS_PERBOSS;
         }
         // Increment the battle's zenny reward based on the class of target robot
         if ($index['robot_class'] == 'master') {
             $temp_battle_omega['battle_zenny'] += $robot['robot_level'] * MMRPG_SETTINGS_BATTLEZENNY_PERROBOT;
         } elseif ($index['robot_class'] == 'mecha') {
             $temp_battle_omega['battle_zenny'] += $robot['robot_level'] * MMRPG_SETTINGS_BATTLEZENNY_PERMECHA;
         } elseif ($index['robot_class'] == 'boss') {
             $temp_battle_omega['battle_zenny'] += $robot['robot_level'] * MMRPG_SETTINGS_BATTLEZENNY_PERBOSS;
         }
         // Increment the battle's robot limit based on the class of target robot
         if ($index['robot_class'] == 'master') {
             $temp_battle_omega['battle_robots_limit'] += MMRPG_SETTINGS_BATTLEROBOTS_PERROBOT;
         } elseif ($index['robot_class'] == 'mecha') {
             $temp_battle_omega['battle_robots_limit'] += MMRPG_SETTINGS_BATTLEROBOTS_PERMECHA;
         } elseif ($index['robot_class'] == 'boss') {
             $temp_battle_omega['battle_robots_limit'] += MMRPG_SETTINGS_BATTLEROBOTS_PERBOSS;
         }
         $temp_battle_omega['battle_target_player']['player_robots'][$key] = $robot;
     }
     // Fix any zero or invalid battle values
     if ($temp_battle_omega['battle_points'] < 1) {
         $temp_battle_omega['battle_points'] = 1;
     } else {
         $temp_battle_omega['battle_points'] = ceil($temp_battle_omega['battle_points']);
     }
     if ($temp_battle_omega['battle_turns_limit'] < 1) {
         $temp_battle_omega['battle_turns_limit'] = 1;
     } else {
         $temp_battle_omega['battle_turns_limit'] = ceil($temp_battle_omega['battle_turns_limit']);
     }
     if ($temp_battle_omega['battle_robots_limit'] < 1) {
         $temp_battle_omega['battle_robots_limit'] = 1;
     } else {
         $temp_battle_omega['battle_robots_limit'] = ceil($temp_battle_omega['battle_robots_limit']);
     }
     // Multiply battle points and zenny by ten for bonus amount (basically a cheating stage)
     $temp_battle_omega['battle_points'] = ceil($temp_battle_omega['battle_points'] / 10);
     $temp_battle_omega['battle_zenny'] = ceil($temp_battle_omega['battle_zenny'] / 10);
     //if ($this_robot_class == 'mecha'){ $temp_battle_omega['battle_points'] = ceil($temp_battle_omega['battle_points'] / 100); }
     //elseif ($this_robot_class == 'master'){ $temp_battle_omega['battle_points'] = ceil($temp_battle_omega['battle_points'] / 10); }
     //elseif ($this_robot_class == 'master'){ $temp_battle_omega['battle_points'] = ceil($temp_battle_omega['battle_points'] / 10); }
     //if ($this_robot_class == 'mecha'){ $temp_battle_omega['battle_points'] = ceil($temp_battle_omega['battle_points'] / 100); }
     //elseif ($this_robot_class == 'master'){ $temp_battle_omega['battle_points'] = ceil($temp_battle_omega['battle_points'] / 10); }
     //elseif ($this_robot_class == 'master'){ $temp_battle_omega['battle_points'] = ceil($temp_battle_omega['battle_points'] / 10); }
     // types used to be here
     // Update the field music to a random boss theme from MM1-10 + MM&B
     $temp_music_number = mt_rand(1, 11);
     $temp_music_name = 'boss-theme-mm' . str_pad($temp_music_number, 2, '0', STR_PAD_LEFT);
     $temp_battle_omega['battle_field_info']['field_music'] = $temp_music_name;
     // Add some random item drops to the starter battle
     $temp_battle_omega['battle_rewards_items'] = array(array('chance' => 2, 'token' => 'item-energy-tank'), array('chance' => 2, 'token' => 'item-weapon-tank'), array('chance' => 1, 'token' => 'item-yashichi'), array('chance' => 1, 'token' => 'item-extra-life'));
     // Return the generated battle data
     return $temp_battle_omega;
 }
    $temp_player_battles = rpg_prototype::battles_complete($player_token);
    $temp_player_transfer = $temp_player_battles >= 1 ? true : false;
    $item_options_markup .= '<option value="'.$player_info['player_token'].'" data-label="'.$player_info['player_token'].'" title="'.$player_info['player_name'].'" '.(!$temp_player_transfer ? 'disabled="disabled"' : '').'>'.$player_info['player_name'].'</option>';
}
*/
// Loop through the allowed edit data for all players
$key_counter = 0;
// Loop through and count each player's robot totals
$temp_robot_totals = array();
foreach ($allowed_edit_data as $player_token => $player_info) {
    $temp_robot_totals[$player_token] = !empty($player_info['player_robots']) ? count($player_info['player_robots']) : 0;
}
// Loop through the players in the ability edit data
foreach ($allowed_edit_data as $player_token => $player_info) {
    // Collect the rewards for this player
    $player_rewards = rpg_game::player_rewards($player_token);
    // Check how many robots this player has and see if they should be able to transfer
    $counter_player_robots = !empty($player_info['player_robots']) ? count($player_info['player_robots']) : false;
    $counter_player_missions = rpg_prototype::battles_complete($player_info['player_token']);
    $allow_player_selector = $allowed_edit_player_count > 1 && $counter_player_missions > 0 ? true : false;
    // Loop through the player robots and display their edit boxes
    foreach ($player_info['player_robots'] as $robot_token => $robot_info) {
        // Update the robot key to the current counter
        $robot_key = $key_counter;
        // Make a backup of the player selector
        $allow_player_selector_backup = $allow_player_selector;
        // Collect this player's ability rewards and add them to the dropdown
        if (!empty($_SESSION[$session_token]['values']['battle_abilities'])) {
            $player_ability_rewards = $_SESSION[$session_token]['values']['battle_abilities'];
        } elseif (!empty($player_rewards['player_abilities'])) {
            $player_ability_rewards = $player_rewards['player_abilities'];
 /**
  * Trigger the battle complete action and end the current mission
  * @param rpg_player this_player
  * @param rpg_robot this_robot
  * @param rpg_player target_player
  * @param rpg_robot target_robot
  */
 public function trigger_complete(rpg_player $this_player, rpg_robot $this_robot, rpg_player $target_player, rpg_robot $target_robot)
 {
     // Collect references to global objects
     $db = cms_database::get_database();
     $this_battle = self::get_battle();
     $this_field = rpg_field::get_field();
     // Default the return variable to false
     $this_return = false;
     //$this_battle->events_create(false, false, 'DEBUG', 'Battle complete trigger triggered!');
     // Return false if anything is missing
     if (empty($this_player) || empty($this_robot)) {
         return false;
     }
     if (empty($target_player) || empty($target_robot)) {
         return false;
     }
     // Return true if the battle status is already complete
     if ($this_battle->battle_status == 'complete') {
         return true;
     }
     // Update the battle status to complete
     $this_battle->set_info('battle_status', 'complete');
     if ($this_battle->battle_result == 'pending') {
         $this_battle->set_info('battle_result', $target_player->player_side == 'right' ? 'victory' : 'defeat');
         $event_options = array();
         if ($this_battle->battle_result == 'victory') {
             $event_options['event_flag_victory'] = true;
         } elseif ($this_battle->battle_result == 'defeat') {
             $event_options['event_flag_defeat'] = true;
         }
         $this_battle->events_create(false, false, '', '', $event_options);
     }
     // -- CALCULATE REWARDS -- //
     // Define variables for the human's rewards in this scenario
     $temp_human_token = $target_player->player_side == 'left' ? $target_player->player_token : $this_player->player_token;
     $temp_human_info = $target_player->player_side == 'left' ? $target_player->export_array() : $this_player->export_array();
     $temp_human_rewards = array();
     $temp_human_rewards['battle_points'] = 0;
     $temp_human_rewards['battle_zenny'] = 0;
     $temp_human_rewards['battle_complete'] = isset($_SESSION['GAME']['values']['battle_complete'][$temp_human_token][$this_battle->battle_token]['battle_count']) ? $_SESSION['GAME']['values']['battle_complete'][$temp_human_token][$this_battle->battle_token]['battle_count'] : 0;
     $temp_human_rewards['battle_failure'] = isset($_SESSION['GAME']['values']['battle_failure'][$temp_human_token][$this_battle->battle_token]['battle_count']) ? $_SESSION['GAME']['values']['battle_failure'][$temp_human_token][$this_battle->battle_token]['battle_count'] : 0;
     $temp_human_rewards['checkpoint'] = 'start: ';
     // Calculate the base point and zenny rewards for this battle
     $temp_reward_points_base = !empty($this_battle->battle_rewards_points) ? $this_battle->battle_rewards_points : 0;
     $temp_reward_zenny_base = !empty($this_battle->battle_rewards_zenny) ? $this_battle->battle_rewards_zenny : 0;
     // Default the bonus to zero and calulate based on turns
     $temp_turn_bonus = 0;
     if ($this_battle->counters['battle_turn'] < $this_battle->battle_turns_limit) {
         $temp_turn_bonus = round(($this_battle->battle_turns_limit - $this_battle->counters['battle_turn']) * 10);
     } elseif ($this_battle->counters['battle_turn'] > $this_battle->battle_turns_limit) {
         $temp_turn_bonus = round(($this_battle->counters['battle_turn'] - $this_battle->battle_turns_limit) * 10) * -1;
     }
     // Default the bonus to zero and calulate based on turns
     $temp_robot_bonus = 0;
     if ($temp_human_info['counters']['robots_masters_total'] < $this_battle->battle_robots_limit) {
         $temp_robot_bonus = round(($this_battle->battle_robots_limit - $temp_human_info['counters']['robots_masters_total']) * 10);
     } elseif ($temp_human_info['counters']['robots_masters_total'] > $this_battle->battle_robots_limit) {
         $temp_robot_bonus = $temp_robot_bonus = round(($temp_human_info['counters']['robots_masters_total'] - $this_battle->battle_robots_limit) * 10) * -1;
     }
     // Calculate the bonus points and zenny for the turns
     $temp_turn_bonus_points = (int) ($temp_reward_points_base * ($temp_turn_bonus / 100));
     $temp_turn_bonus_zenny = (int) ($temp_reward_zenny_base * ($temp_turn_bonus / 100));
     // Calculate the bonus points and zenny for the turns
     $temp_robot_bonus_points = (int) ($temp_reward_points_base * ($temp_robot_bonus / 100));
     $temp_robot_bonus_zenny = (int) ($temp_reward_zenny_base * ($temp_robot_bonus / 100));
     // Calculate the final reward points based on above
     if ($this_battle->battle_result == 'victory') {
         $temp_reward_points_final = $temp_reward_points_base + $temp_turn_bonus_points + $temp_robot_bonus_points;
         $temp_reward_zenny_final = $temp_reward_zenny_base + $temp_turn_bonus_zenny + $temp_robot_bonus_zenny;
         if ($temp_reward_points_final < 0) {
             $temp_reward_points_final = 0;
         }
         if ($temp_reward_zenny_final < 0) {
             $temp_reward_zenny_final = 0;
         }
     } else {
         $temp_reward_points_final = 0;
         $temp_reward_zenny_final = 0;
     }
     // Define the number of stars to show for this mission
     $temp_rating_stars = 0;
     if ($this_battle->battle_result == 'victory') {
         $temp_rating_stars += 1;
         if ($temp_turn_bonus >= 0) {
             $temp_rating_stars += 1;
         }
         if ($temp_robot_bonus >= 0) {
             $temp_rating_stars += 1;
         }
         if (empty($temp_human_info['counters']['robots_disabled'])) {
             $temp_rating_stars += 1;
         }
         if (empty($temp_human_info['counters']['items_used_this_battle'])) {
             $temp_rating_stars += 1;
         }
     }
     // Generate the markup for this stars
     $temp_rating_stars_markup = '';
     for ($i = 1; $i <= 5; $i++) {
         $temp_rating_stars_markup .= $i <= $temp_rating_stars ? '&#9733;' : '&#9734;';
     }
     // (HUMAN) TARGET DEFEATED
     // Check if the target was the human character
     if ($target_player->player_side == 'left') {
         // Increment the main game's points total with the battle points
         $_SESSION['GAME']['counters']['battle_points'] += $temp_reward_points_final;
         $_SESSION['GAME']['counters']['battle_zenny'] += $temp_reward_zenny_final;
         // Increment this player's points total with the battle points
         if (!isset($_SESSION['GAME']['values']['battle_rewards'][$target_player->player_token]['player_points'])) {
             $_SESSION['GAME']['values']['battle_rewards'][$target_player->player_token]['player_points'] = 0;
         }
         if (!isset($_SESSION['GAME']['values']['battle_rewards'][$target_player->player_token]['player_zenny'])) {
             $_SESSION['GAME']['values']['battle_rewards'][$target_player->player_token]['player_zenny'] = 0;
         }
         $_SESSION['GAME']['values']['battle_rewards'][$target_player->player_token]['player_points'] += $temp_reward_points_final;
         $_SESSION['GAME']['values']['battle_rewards'][$target_player->player_token]['player_zenny'] += $temp_reward_zenny_final;
         // Update the global variable with the points reward
         $temp_human_rewards['battle_points'] = $temp_reward_points_final;
         $temp_human_rewards['battle_zenny'] = $temp_reward_zenny_final;
         // Update the GAME session variable with the failed battle token
         $save_records = $this_battle->has_flag('save_records') ? $this_battle->get_flag('save_records') : true;
         if ($save_records) {
             $bak_session_array = isset($_SESSION['GAME']['values']['battle_failure'][$target_player->player_token][$this_battle->battle_token]) ? $_SESSION['GAME']['values']['battle_failure'][$target_player->player_token][$this_battle->battle_token] : array();
             $new_session_array = array('battle_token' => $this_battle->battle_token, 'battle_count' => 0, 'battle_level' => 0);
             if (!empty($bak_session_array['battle_count'])) {
                 $new_session_array['battle_count'] = $bak_session_array['battle_count'];
             }
             if (!empty($bak_session_array['battle_level'])) {
                 $new_session_array['battle_level'] = $bak_session_array['battle_level'];
             }
             $new_session_array['battle_level'] = $this_battle->battle_level;
             $new_session_array['battle_count']++;
             $_SESSION['GAME']['values']['battle_failure'][$target_player->player_token][$this_battle->battle_token] = $new_session_array;
             $temp_human_rewards['battle_failure'] = $_SESSION['GAME']['values']['battle_failure'][$target_player->player_token][$this_battle->battle_token]['battle_count'];
         }
     }
     // NON-INVISIBLE PLAYER DEFEATED
     // Display the defeat message for the target character if not default/hidden
     if ($target_player->player_token != 'player') {
         // (HUMAN) TARGET DEFEATED BY (INVISIBLE/COMPUTER)
         // If this was a player battle and the human user lost against the ghost target (this/computer/victory | target/human/defeat)
         if ($this_player->player_id == MMRPG_SETTINGS_TARGET_PLAYERID && $target_player->player_side == 'left' && $this_robot->robot_class != 'mecha') {
             // Calculate how many points the other player is rewarded for winning
             $target_player_robots = $target_player->values['robots_disabled'];
             $target_player_robots_count = count($target_player_robots);
             $other_player_points = 0;
             $other_player_turns = $target_player_robots_count * MMRPG_SETTINGS_BATTLETURNS_PERROBOT;
             foreach ($target_player_robots as $disabled_robotinfo) {
                 $other_player_points += $disabled_robotinfo['robot_level'] * MMRPG_SETTINGS_BATTLEPOINTS_PERROBOT * MMRPG_SETTINGS_BATTLEPOINTS_PLAYERBATTLE_MULTIPLIER;
             }
             // Create the victory event for the target player
             $this_robot->robot_frame = 'victory';
             $this_robot->update_session();
             $event_header = $this_robot->robot_name . ' Undefeated';
             $event_body = '';
             $event_body .= $this_robot->print_name() . ' could not be defeated! ';
             $event_body .= '<br />';
             $event_options = array();
             $event_options['console_show_this_robot'] = true;
             $event_options['console_show_target'] = false;
             $event_options['event_flag_defeat'] = true;
             $event_options['this_header_float'] = $event_options['this_body_float'] = $this_robot->player->player_side;
             if ($this_robot->robot_token != 'robot' && isset($this_robot->robot_quotes['battle_victory'])) {
                 $this_find = array('{target_player}', '{target_robot}', '{this_player}', '{this_robot}');
                 $this_replace = array($target_player->player_name, $target_robot->robot_name, $this_player->player_name, $this_robot->robot_name);
                 $event_body .= $this_robot->print_quote('battle_victory', $this_find, $this_replace);
             }
             $this_battle->events_create($this_robot, $target_robot, $event_header, $event_body, $event_options);
         }
         $target_player->set_frame('defeat');
         $target_robot->update_session();
         $event_header = $target_player->player_name . ' Defeated';
         $event_body = $target_player->print_name() . ' was defeated' . ($target_player->player_side == 'left' ? '&hellip;' : '!') . ' ';
         $event_body .= '<br />';
         $event_options = array();
         $event_options['console_show_this_player'] = true;
         $event_options['console_show_target'] = false;
         $event_options['event_flag_defeat'] = true;
         $event_options['this_header_float'] = $event_options['this_body_float'] = $target_player->player_side;
         if ($target_player->player_token != 'player' && isset($target_player->player_quotes['battle_defeat'])) {
             $this_find = array('{target_player}', '{target_robot}', '{this_player}', '{this_robot}');
             $this_replace = array($this_player->player_name, $this_robot->robot_name, $target_player->player_name, $target_robot->robot_name);
             $this_quote_text = str_replace($this_find, $this_replace, $target_player->player_quotes['battle_defeat']);
             $event_body .= $target_player->print_quote('battle_defeat', $this_find, $this_replace);
         }
         $this_battle->events_create($target_robot, $this_robot, $event_header, $event_body, $event_options);
         // (HUMAN) TARGET DEFEATED BY (GHOST/COMPUTER)
         // If this was a player battle and the human user lost against the ghost target (this/computer/victory | target/human/defeat)
         if ($this_player->player_id != MMRPG_SETTINGS_TARGET_PLAYERID && $target_player->player_side == 'left') {
             // Calculate how many points the other player is rewarded for winning
             $target_player_robots = $target_player->values['robots_disabled'];
             $target_player_robots_count = count($target_player_robots);
             $other_player_points = 0;
             $other_player_turns = $target_player_robots_count * MMRPG_SETTINGS_BATTLETURNS_PERROBOT;
             foreach ($target_player_robots as $disabled_robotinfo) {
                 $other_player_points += $disabled_robotinfo['robot_level'] * MMRPG_SETTINGS_BATTLEPOINTS_PERROBOT * MMRPG_SETTINGS_BATTLEPOINTS_PLAYERBATTLE_MULTIPLIER;
             }
             // Create the victory event for the target player
             $this_player->set_frame('victory');
             $target_robot->update_session();
             $event_header = $this_player->player_name . ' Victorious';
             $event_body = $this_player->print_name() . ' was victorious! ';
             $event_body .= $this_player->print_name() . ' could not be defeated!';
             $event_body .= '<br />';
             $event_options = array();
             $event_options['console_show_this_player'] = true;
             $event_options['console_show_target'] = false;
             $event_options['event_flag_defeat'] = true;
             $event_options['this_header_float'] = $event_options['this_body_float'] = $this_player->player_side;
             if ($this_player->player_token != 'player' && isset($this_player->player_quotes['battle_victory'])) {
                 $this_find = array('{target_player}', '{target_robot}', '{this_player}', '{this_robot}');
                 $this_replace = array($target_player->player_name, $target_robot->robot_name, $this_player->player_name, $this_robot->robot_name);
                 $event_body .= $this_player->print_quote('battle_victory', $this_find, $this_replace);
             }
             $this_battle->events_create($this_robot, $target_robot, $event_header, $event_body, $event_options);
         }
     }
     // (HUMAN) TARGET DEFEATED BY (COMPUTER)
     // Check if the target was the human character (and they LOST)
     if ($target_player->player_side == 'left') {
         // Collect the robot info array
         $temp_player_info = $target_player->export_array();
         // Collect or define the player points and player rewards variables
         $temp_player_token = $temp_player_info['player_token'];
         $temp_player_points = rpg_game::player_points($temp_player_info['player_token']);
         $temp_player_rewards = rpg_game::player_rewards($temp_player_info['player_token']);
         //!empty($temp_player_info['player_rewards']) ? $temp_player_info['player_rewards'] : array();
         // -- ABILITY REWARDS for HUMAN PLAYER -- //
         // Loop through the ability rewards for this robot if set
         if (!empty($temp_player_rewards['abilities']) && rpg_game::is_user()) {
             $temp_abilities_index = $db->get_array_list("SELECT * FROM mmrpg_index_abilities WHERE ability_flag_complete = 1;", 'ability_token');
             foreach ($temp_player_rewards['abilities'] as $ability_reward_key => $ability_reward_info) {
                 // If this ability is already unlocked, continue
                 if (rpg_game::ability_unlocked($target_player->player_token, false, $ability_reward_info['token'])) {
                     continue;
                 }
                 // Check if the required level has been met by this robot
                 if ($temp_player_points >= $ability_reward_info['points'] && rpg_game::is_user()) {
                     // Collect the ability info from the index
                     $ability_info = array('ability_id' => MMRPG_SETTINGS_BATTLEABILITIES_PERROBOT_MAX + $ability_reward_key, 'ability_token' => $ability_reward_info['token']);
                     // Create the temporary ability object for event creation
                     $temp_ability = new rpg_ability($target_player, $target_robot, $ability_info);
                     // Collect or define the ability variables
                     $temp_ability_token = $ability_info['ability_token'];
                     // Display the robot reward message markup
                     $event_header = $temp_ability->ability_name . ' Unlocked';
                     $event_body = rpg_functions::get_random_positive_word() . ' <span class="player_name">' . $temp_player_info['player_name'] . '</span> unlocked new ability data!<br />';
                     $event_body .= '<span class="ability_name">' . $temp_ability->ability_name . '</span> can now be used in battle!';
                     $event_options = array();
                     $event_options['console_show_target'] = false;
                     $event_options['this_header_float'] = $target_player->player_side;
                     $event_options['this_body_float'] = $target_player->player_side;
                     $event_options['this_ability'] = $temp_ability;
                     $event_options['this_ability_image'] = 'icon';
                     $event_options['event_flag_victory'] = true;
                     $event_options['console_show_this_player'] = false;
                     $event_options['console_show_this_robot'] = false;
                     $event_options['console_show_this_ability'] = true;
                     $event_options['canvas_show_this_ability'] = false;
                     $target_player->set_frame($ability_reward_key % 2 == 0 ? 'victory' : 'taunt');
                     $temp_ability->ability_frame = 'base';
                     $temp_ability->update_session();
                     $this_battle->events_create($target_robot, $target_robot, $event_header, $event_body, $event_options);
                     // Automatically unlock this ability for use in battle
                     $this_reward = array('ability_token' => $temp_ability_token);
                     $show_event = !rpg_game::ability_unlocked('', '', $temp_ability_token) ? true : false;
                     rpg_game::unlock_ability($temp_player_info, false, $this_reward, $show_event);
                 }
             }
         }
     }
     // (COMPUTER) TARGET DEFEATED BY (HUMAN)
     // Check if this player was the human player (and they WON)
     if ($this_player->player_side == 'left') {
         // Increment the main game's points total with the battle points
         $_SESSION['GAME']['counters']['battle_points'] += $temp_reward_points_final;
         $_SESSION['GAME']['counters']['battle_zenny'] += $temp_reward_zenny_final;
         // Reference the number of points this player gets
         $this_player_points = $temp_reward_points_final;
         $this_player_zenny = $temp_reward_zenny_final;
         // Increment this player's points total with the battle points
         $player_token = $this_player->player_token;
         $player_info = $this_player->export_array();
         if (!isset($_SESSION['GAME']['values']['battle_rewards'][$player_token]['player_points'])) {
             $_SESSION['GAME']['values']['battle_rewards'][$player_token]['player_points'] = 0;
         }
         $_SESSION['GAME']['values']['battle_rewards'][$player_token]['player_points'] += $this_player_points;
         if (!isset($_SESSION['GAME']['values']['battle_rewards'][$player_token]['player_zenny'])) {
             $_SESSION['GAME']['values']['battle_rewards'][$player_token]['player_zenny'] = 0;
         }
         $_SESSION['GAME']['values']['battle_rewards'][$player_token]['player_zenny'] += $this_player_zenny;
         // Update the global variable with the points reward
         $temp_human_rewards['battle_points'] = $this_player_points;
         $temp_human_rewards['battle_zenny'] = $this_player_zenny;
         // Display the win message for this player with battle points
         $this_robot->robot_frame = 'victory';
         $this_player->set_frame('victory');
         $this_robot->update_session();
         $event_header = $this_player->player_name . ' Victorious';
         $event_body = $this_player->print_name() . ' was victorious! ';
         $event_body .= 'The ' . ($target_player->counters['robots_disabled'] > 1 ? 'targets were' : 'target was') . ' defeated!';
         $event_body .= '<br />';
         $event_options = array();
         $event_options['console_show_this_player'] = true;
         $event_options['console_show_target'] = false;
         $event_options['event_flag_victory'] = true;
         $event_options['this_header_float'] = $event_options['this_body_float'] = $this_player->player_side;
         if ($this_player->player_token != 'player' && isset($this_player->player_quotes['battle_victory'])) {
             $this_find = array('{target_player}', '{target_robot}', '{this_player}', '{this_robot}');
             $this_replace = array($target_player->player_name, $target_robot->robot_name, $this_player->player_name, $this_robot->robot_name);
             $event_body .= $this_player->print_quote('battle_victory', $this_find, $this_replace);
         }
         $this_battle->events_create($this_robot, $target_robot, $event_header, $event_body, $event_options);
         /*
          * PLAYER REWARDS
          */
         // Check if the the player was a human character
         if ($this_player->player_side == 'left') {
             // Collect the robot info array
             $temp_player_info = $this_player->export_array();
             // Collect or define the player points and player rewards variables
             $temp_player_token = $temp_player_info['player_token'];
             $temp_player_points = rpg_game::player_points($temp_player_info['player_token']);
             $temp_player_rewards = !empty($temp_player_info['player_rewards']) ? $temp_player_info['player_rewards'] : array();
             // -- ABILITY REWARDS for HUMAN PLAYER -- //
             // Loop through the ability rewards for this player if set
             if (!empty($temp_player_rewards['abilities']) && rpg_game::is_user()) {
                 $temp_abilities_index = $db->get_array_list("SELECT * FROM mmrpg_index_abilities WHERE ability_flag_complete = 1;", 'ability_token');
                 foreach ($temp_player_rewards['abilities'] as $ability_reward_key => $ability_reward_info) {
                     // If this ability is already unlocked, continue
                     if (rpg_game::ability_unlocked($this_player->player_token, false, $ability_reward_info['token'])) {
                         continue;
                     }
                     // Check if the required level has been met by this robot
                     if ($temp_player_points >= $ability_reward_info['points']) {
                         // Collect the ability info from the index
                         $ability_info = rpg_ability::parse_index_info($temp_abilities_index[$ability_reward_info['token']]);
                         // Create the temporary ability object for event creation
                         $temp_ability = new rpg_ability($this_player, $this_robot, $ability_info);
                         // Collect or define the ability variables
                         $temp_ability_token = $ability_info['ability_token'];
                         // Display the robot reward message markup
                         $event_header = $ability_info['ability_name'] . ' Unlocked';
                         $event_body = rpg_functions::get_random_positive_word() . ' <span class="player_name">' . $temp_player_info['player_name'] . '</span> unlocked new ability data!<br />';
                         $event_body .= '<span class="ability_name">' . $ability_info['ability_name'] . '</span> can now be used in battle!';
                         $event_options = array();
                         $event_options['console_show_target'] = false;
                         $event_options['this_header_float'] = $this_player->player_side;
                         $event_options['this_body_float'] = $this_player->player_side;
                         $event_options['this_ability'] = $temp_ability;
                         $event_options['this_ability_image'] = 'icon';
                         $event_options['event_flag_victory'] = true;
                         $event_options['console_show_this_player'] = false;
                         $event_options['console_show_this_robot'] = false;
                         $event_options['console_show_this_ability'] = true;
                         $event_options['canvas_show_this_ability'] = false;
                         $this_player->set_frame($ability_reward_key % 2 == 0 ? 'victory' : 'taunt');
                         $this_robot->robot_frame = $ability_reward_key % 2 == 0 ? 'taunt' : 'base';
                         $this_robot->update_session();
                         $temp_ability->ability_frame = 'base';
                         $temp_ability->update_session();
                         $this_battle->events_create($this_robot, $this_robot, $event_header, $event_body, $event_options);
                         // Automatically unlock this ability for use in battle
                         $this_reward = array('ability_token' => $temp_ability_token);
                         $show_event = !rpg_game::ability_unlocked('', '', $temp_ability_token) ? true : false;
                         rpg_game::unlock_ability($temp_player_info, false, $this_reward, $show_event);
                     }
                 }
             }
         }
     }
     /*
      * BATTLE REWARDS
      */
     // Collect or define the player variables
     $this_player_token = $this_player->player_token;
     $this_player_info = $this_player->export_array();
     // Collect or define the target player variables
     $target_player_token = $target_player->player_token;
     $target_player_info = $target_player->export_array();
     // Check if this player was the human player
     if ($this_player->player_side == 'left') {
         // Update the GAME session variable with the completed battle token
         $save_records = $this_battle->has_flag('save_records') ? $this_battle->get_flag('save_records') : true;
         if ($save_records) {
             // Back up the current session array for this battle complete counter
             $bak_session_array = isset($_SESSION['GAME']['values']['battle_complete'][$this_player->player_token][$this_battle->battle_token]) ? $_SESSION['GAME']['values']['battle_complete'][$this_player->player_token][$this_battle->battle_token] : array();
             // Create the new session array from scratch to ensure all values exist
             $new_session_array = array('battle_token' => $this_battle->battle_token, 'battle_count' => 0, 'battle_min_level' => 0, 'battle_max_level' => 0, 'battle_min_turns' => 0, 'battle_max_turns' => 0, 'battle_min_points' => 0, 'battle_max_points' => 0, 'battle_min_robots' => 0, 'battle_max_robots' => 0);
             // Recollect applicable battle values from the backup session array
             if (!empty($bak_session_array['battle_count'])) {
                 $new_session_array['battle_count'] = $bak_session_array['battle_count'];
             }
             if (!empty($bak_session_array['battle_level'])) {
                 $new_session_array['battle_min_level'] = $bak_session_array['battle_level'];
             }
             // LEGACY
             if (!empty($bak_session_array['battle_min_level'])) {
                 $new_session_array['battle_min_level'] = $bak_session_array['battle_min_level'];
             }
             if (!empty($bak_session_array['battle_max_level'])) {
                 $new_session_array['battle_max_level'] = $bak_session_array['battle_max_level'];
             }
             if (!empty($bak_session_array['battle_min_turns'])) {
                 $new_session_array['battle_min_turns'] = $bak_session_array['battle_min_turns'];
             }
             if (!empty($bak_session_array['battle_max_turns'])) {
                 $new_session_array['battle_max_turns'] = $bak_session_array['battle_max_turns'];
             }
             if (!empty($bak_session_array['battle_min_points'])) {
                 $new_session_array['battle_min_points'] = $bak_session_array['battle_min_points'];
             }
             if (!empty($bak_session_array['battle_max_points'])) {
                 $new_session_array['battle_max_points'] = $bak_session_array['battle_max_points'];
             }
             if (!empty($bak_session_array['battle_min_robots'])) {
                 $new_session_array['battle_min_robots'] = $bak_session_array['battle_min_robots'];
             }
             if (!empty($bak_session_array['battle_max_robots'])) {
                 $new_session_array['battle_max_robots'] = $bak_session_array['battle_max_robots'];
             }
             // Update and/or increment the appropriate battle variables in the new array
             if ($new_session_array['battle_max_level'] == 0 || $this_battle->battle_level > $new_session_array['battle_max_level']) {
                 $new_session_array['battle_max_level'] = $this_battle->battle_level;
             }
             if ($new_session_array['battle_min_level'] == 0 || $this_battle->battle_level < $new_session_array['battle_min_level']) {
                 $new_session_array['battle_min_level'] = $this_battle->battle_level;
             }
             if ($new_session_array['battle_max_turns'] == 0 || $this_battle->counters['battle_turn'] > $new_session_array['battle_max_turns']) {
                 $new_session_array['battle_max_turns'] = $this_battle->counters['battle_turn'];
             }
             if ($new_session_array['battle_min_turns'] == 0 || $this_battle->counters['battle_turn'] < $new_session_array['battle_min_turns']) {
                 $new_session_array['battle_min_turns'] = $this_battle->counters['battle_turn'];
             }
             if ($new_session_array['battle_max_points'] == 0 || $temp_human_rewards['battle_points'] > $new_session_array['battle_max_points']) {
                 $new_session_array['battle_max_points'] = $temp_human_rewards['battle_points'];
             }
             if ($new_session_array['battle_min_points'] == 0 || $temp_human_rewards['battle_points'] < $new_session_array['battle_min_points']) {
                 $new_session_array['battle_min_points'] = $temp_human_rewards['battle_points'];
             }
             if ($new_session_array['battle_max_robots'] == 0 || $this_player->counters['robots_total'] > $new_session_array['battle_max_robots']) {
                 $new_session_array['battle_max_robots'] = $this_player->counters['robots_total'];
             }
             if ($new_session_array['battle_min_robots'] == 0 || $this_player->counters['robots_total'] < $new_session_array['battle_min_robots']) {
                 $new_session_array['battle_min_robots'] = $this_player->counters['robots_total'];
             }
             $new_session_array['battle_count']++;
             // Update the session variable for this player with the updated battle values
             $_SESSION['GAME']['values']['battle_complete'][$this_player->player_token][$this_battle->battle_token] = $new_session_array;
             $temp_human_rewards['battle_complete'] = $_SESSION['GAME']['values']['battle_complete'][$this_player->player_token][$this_battle->battle_token]['battle_count'];
         }
         // Refresh the player info array
         $this_player_info = $this_player->export_array();
         // ROBOT REWARDS
         // Loop through any robot rewards for this battle
         $this_robot_rewards = $this_battle->get_robot_rewards();
         if (!empty($this_robot_rewards) && rpg_game::is_user()) {
             foreach ($this_robot_rewards as $robot_reward_key => $robot_reward_info) {
                 // If this robot has already been unlocked by anyone, continue
                 if (rpg_game::robot_unlocked(false, $robot_reward_info['token'])) {
                     continue;
                 }
                 // Collect the robot info from the index
                 $robot_info = rpg_robot::get_index_info($robot_reward_info['token']);
                 // Search this player's base robots for the robot ID
                 $robot_info['robot_id'] = 0;
                 foreach ($this_player->player_base_robots as $base_robot) {
                     if ($robot_info['robot_token'] == $base_robot['robot_token']) {
                         $robot_info['robot_id'] = $base_robot['robot_id'];
                         break;
                     }
                 }
                 // Create the temporary robot object for event creation
                 $temp_robot = new rpg_robot($this_player, $robot_info);
                 // Collect or define the robot points and robot rewards variables
                 $this_robot_token = $robot_reward_info['token'];
                 $this_robot_level = !empty($robot_reward_info['level']) ? $robot_reward_info['level'] : 1;
                 $this_robot_experience = !empty($robot_reward_info['experience']) ? $robot_reward_info['experience'] : 0;
                 $this_robot_rewards = !empty($robot_info['robot_rewards']) ? $robot_info['robot_rewards'] : array();
                 // Automatically unlock this robot for use in battle
                 $this_reward = $robot_info;
                 $this_reward['robot_level'] = $this_robot_level;
                 $this_reward['robot_experience'] = $this_robot_experience;
                 rpg_game::unlock_robot($this_player_info, $this_reward, true, true);
             }
         }
         // ABILITY REWARDS
         // Loop through any ability rewards for this battle
         $this_ability_rewards = $this_battle->get_ability_rewards();
         if (!empty($this_ability_rewards) && rpg_game::is_user()) {
             $temp_abilities_index = $db->get_array_list("SELECT * FROM mmrpg_index_abilities WHERE ability_flag_complete = 1;", 'ability_token');
             foreach ($this_ability_rewards as $ability_reward_key => $ability_reward_info) {
                 // Collect the ability info from the index
                 $ability_info = rpg_ability::parse_index_info($temp_abilities_index[$ability_reward_info['token']]);
                 // Create the temporary robot object for event creation
                 $temp_ability = new rpg_ability($this_player, $this_robot, $ability_info);
                 // Collect or define the robot points and robot rewards variables
                 $this_ability_token = $ability_info['ability_token'];
                 // Now loop through all active robots on this side of the field
                 foreach ($this_player_info['values']['robots_active'] as $temp_key => $temp_info) {
                     // If this robot is a mecha, skip it!
                     if (!empty($temp_info['robot_class']) && $temp_info['robot_class'] == 'mecha') {
                         continue;
                     }
                     // Equip this ability to the robot is there was a match found
                     if (rpg_robot::has_ability_compatibility($temp_info['robot_token'], $ability_info['ability_token'])) {
                         if (!isset($_SESSION['GAME']['values']['battle_settings'][$this_player_info['player_token']]['player_robots'][$temp_info['robot_token']]['robot_abilities'])) {
                             $_SESSION['GAME']['values']['battle_settings'][$this_player_info['player_token']]['player_robots'][$temp_info['robot_token']]['robot_abilities'] = array();
                         }
                         if (count($_SESSION['GAME']['values']['battle_settings'][$this_player_info['player_token']]['player_robots'][$temp_info['robot_token']]['robot_abilities']) < 8) {
                             $_SESSION['GAME']['values']['battle_settings'][$this_player_info['player_token']]['player_robots'][$temp_info['robot_token']]['robot_abilities'][$ability_info['ability_token']] = array('ability_token' => $ability_info['ability_token']);
                         }
                     }
                 }
                 // If this ability has already been unlocked by the player, continue
                 if (rpg_game::ability_unlocked($this_player_token, false, $ability_reward_info['token'])) {
                     continue;
                 }
                 // Automatically unlock this ability for use in battle
                 $this_reward = array('ability_token' => $this_ability_token);
                 $show_event = !rpg_game::ability_unlocked('', '', $this_ability_token) ? true : false;
                 rpg_game::unlock_ability($this_player_info, false, $this_reward, $show_event);
                 // Display the robot reward message markup
                 $event_header = $ability_info['ability_name'] . ' Unlocked';
                 $event_body = rpg_functions::get_random_positive_word() . ' <span class="player_name">' . $this_player_info['player_name'] . '</span> unlocked new ability data!<br />';
                 $event_body .= '<span class="ability_name">' . $ability_info['ability_name'] . '</span> can now be used in battle!';
                 $event_options = array();
                 $event_options['console_show_target'] = false;
                 $event_options['this_header_float'] = $this_player->player_side;
                 $event_options['this_body_float'] = $this_player->player_side;
                 $event_options['this_ability'] = $temp_ability;
                 $event_options['this_ability_image'] = 'icon';
                 $event_options['console_show_this_player'] = false;
                 $event_options['console_show_this_robot'] = false;
                 $event_options['console_show_this_ability'] = true;
                 $event_options['canvas_show_this_ability'] = false;
                 $this_player->set_frame('victory');
                 $temp_ability->ability_frame = 'base';
                 $temp_ability->update_session();
                 $this_battle->events_create($this_robot, false, $event_header, $event_body, $event_options);
             }
         }
     }
     // end of BATTLE REWARDS
     // Check if there is a field star for this stage to collect
     if ($this_battle->battle_result == 'victory' && !empty($this_battle->values['field_star'])) {
         // Collect the field star data for this battle
         $temp_field_star = $this_battle->values['field_star'];
         // Print out the event for collecting the new field star
         $temp_name_markup = '<span class="field_name field_type field_type_' . (!empty($temp_field_star['star_type']) ? $temp_field_star['star_type'] : 'none') . (!empty($temp_field_star['star_type2']) ? '_' . $temp_field_star['star_type2'] : '') . '">' . $temp_field_star['star_name'] . ' Star</span>';
         $temp_event_header = $this_player->player_name . '&#39;s ' . ucfirst($temp_field_star['star_kind']) . ' Star';
         $temp_event_body = $this_player->print_name() . ' collected the ' . $temp_name_markup . '!<br />';
         $temp_event_body .= 'The new ' . ucfirst($temp_field_star['star_kind']) . ' Star amplifies your Starforce!';
         $temp_event_options = array();
         $temp_event_options['console_show_this_player'] = false;
         $temp_event_options['console_show_target_player'] = false;
         $temp_event_options['console_show_this_robot'] = false;
         $temp_event_options['console_show_target_robot'] = false;
         $temp_event_options['console_show_this_ability'] = false;
         $temp_event_options['console_show_this'] = true;
         $temp_event_options['console_show_this_star'] = true;
         $temp_event_options['this_header_float'] = $temp_event_options['this_body_float'] = $this_player->player_side;
         $temp_event_options['this_star'] = $temp_field_star;
         $temp_event_options['this_ability'] = false;
         $this_battle->events_create(false, false, $temp_event_header, $temp_event_body, $temp_event_options);
         // Update the session with this field star data
         $_SESSION['GAME']['values']['battle_stars'][$temp_field_star['star_token']] = $temp_field_star;
         // DEBUG DEBUG
         //$this_battle->events_create($this_robot, $target_robot, 'DEBUG FIELD STAR', 'You got a field star! The field star names '.implode(' | ', $temp_field_star));
     }
     // If this robot's image has been changed, reveert it back to what it was
     if ($this_robot->robot_core == 'copy') {
         if (isset($this_robot->robot_image_overlay['copy_type1'])) {
             unset($this_robot->robot_image_overlay['copy_type1']);
         }
         if (isset($this_robot->robot_image_overlay['copy_type2'])) {
             unset($this_robot->robot_image_overlay['copy_type2']);
         }
         $this_robot->update_session();
     }
     // If the target robot's image has been changed, reveert it back to what it was
     if ($target_robot->robot_core == 'copy') {
         if (isset($target_robot->robot_image_overlay['copy_type1'])) {
             unset($target_robot->robot_image_overlay['copy_type1']);
         }
         if (isset($target_robot->robot_image_overlay['copy_type2'])) {
             unset($target_robot->robot_image_overlay['copy_type2']);
         }
         $target_robot->update_session();
     }
     // Define the first event body markup, regardless of player type
     $first_event_header = $this_battle->battle_name . ($this_battle->battle_result == 'victory' ? ' Complete' : ' Failure') . ' <span class="pipe">|</span> ' . $this_battle->battle_field->field_name;
     if ($this_battle->battle_result == 'victory') {
         $first_event_body = 'Mission complete! <span class="pipe">|</span> ' . ($temp_human_rewards['battle_complete'] > 1 ? rpg_functions::get_random_positive_word() . ' That&#39;s ' . $temp_human_rewards['battle_complete'] . ' times now! ' : '') . rpg_functions::get_random_victory_quote();
     } elseif ($this_battle->battle_result == 'defeat') {
         $first_event_body = 'Mission failure. <span class="pipe">|</span> ' . ($temp_human_rewards['battle_failure'] > 1 ? 'That&#39;s ' . $temp_human_rewards['battle_failure'] . ' times now&hellip; ' : '') . rpg_functions::get_random_defeat_quote();
     }
     $first_event_body .= ' <span class="pipe">|</span> ' . $temp_rating_stars_markup . '<br />';
     // Print out the table and markup for the battle
     $first_event_body .= '<table class="full">';
     $first_event_body .= '<colgroup><col width="30%" /><col width="15%" /><col width="15%" /><col width="20%" /><col width="20%" /></colgroup>';
     $first_event_body .= '<tbody>';
     $first_event_body .= '<tr>';
     $first_event_body .= '<td class="left">Base Values</td>';
     $first_event_body .= '<td class="center"></td>';
     $first_event_body .= '<td class="center"></td>';
     $first_event_body .= '<td class="right">' . ($temp_reward_points_base == 1 ? '1 Point' : number_format($temp_reward_points_base, 0, '.', ',') . ' Points') . '</td>';
     $first_event_body .= '<td class="right">' . ($temp_reward_zenny_base == 1 ? '1 Zenny' : number_format($temp_reward_zenny_base, 0, '.', ',') . ' Zenny') . '</td>';
     $first_event_body .= '</tr> ';
     // Only grant bonuses if there was a victory
     if ($this_battle->battle_result == 'victory') {
         // Print out the label and target vs actual turn stats
         $first_event_body .= '<tr>';
         $first_event_body .= '<td class="left">Target Turns</td>';
         $first_event_body .= '<td class="center">' . $this_battle->counters['battle_turn'] . ' &nbsp;/&nbsp; ' . $this_battle->battle_turns_limit . '</td>';
         // Print the markup for the bonus/penalty percent
         if ($temp_turn_bonus > 0) {
             $first_event_body .= '<td class="center positive">+' . $temp_turn_bonus . '%</td>';
         } elseif ($temp_turn_bonus < 0) {
             $first_event_body .= '<td class="center negative">' . $temp_turn_bonus . '%</td>';
         } else {
             $first_event_body .= '<td class="center">+0%</td>';
         }
         // Print out any mods to the points
         $markup = $temp_turn_bonus_points == 1 ? '1 Point' : number_format($temp_turn_bonus_points, 0, '.', ',') . ' Points';
         if ($temp_turn_bonus_points > 0) {
             $first_event_body .= '<td class="right positive">+' . $markup . '</td>';
         } elseif ($temp_turn_bonus_points < 0) {
             $first_event_body .= '<td class="right negative">' . $markup . '</td>';
         } else {
             $first_event_body .= '<td class="right">-</td>';
         }
         // Print out any mods to the zenny
         $markup = $temp_turn_bonus_zenny == 1 ? '1 Zenny' : number_format($temp_turn_bonus_zenny, 0, '.', ',') . ' Zenny';
         if ($temp_turn_bonus_zenny > 0) {
             $first_event_body .= '<td class="right positive">+' . $markup . '</td>';
         } elseif ($temp_turn_bonus_zenny < 0) {
             $first_event_body .= '<td class="right negative">' . $markup . '</td>';
         } else {
             $first_event_body .= '<td class="right">-</td>';
         }
         $first_event_body .= '</tr>';
         // Print out the label and target vs actual robot stats
         $first_event_body .= '<tr>';
         $first_event_body .= '<td class="left">Target Robots</td>';
         $first_event_body .= '<td class="center">' . $this_player_info['counters']['robots_masters_total'] . ' &nbsp;/&nbsp; ' . $this_battle->battle_robots_limit . '</td>';
         // Print the markup for the bonus/penalty percent
         if ($temp_robot_bonus > 0) {
             $first_event_body .= '<td class="center positive">+' . $temp_robot_bonus . '%</td>';
         } elseif ($temp_robot_bonus < 0) {
             $first_event_body .= '<td class="center negative">' . $temp_robot_bonus . '%</td>';
         } else {
             $first_event_body .= '<td class="center">+0%</td>';
         }
         // Print out any mods to the points
         $markup = $temp_robot_bonus_points == 1 ? '1 Point' : number_format($temp_robot_bonus_points, 0, '.', ',') . ' Points';
         if ($temp_robot_bonus_points > 0) {
             $first_event_body .= '<td class="right positive">+' . $markup . '</td>';
         } elseif ($temp_robot_bonus_points < 0) {
             $first_event_body .= '<td class="right negative">' . $markup . '</td>';
         } else {
             $first_event_body .= '<td class="right">-</td>';
         }
         // Print out any mods to the zenny
         $markup = $temp_robot_bonus_zenny == 1 ? '1 Zenny' : number_format($temp_robot_bonus_zenny, 0, '.', ',') . ' Zenny';
         if ($temp_robot_bonus_zenny > 0) {
             $first_event_body .= '<td class="right positive">+' . $markup . '</td>';
         } elseif ($temp_robot_bonus_zenny < 0) {
             $first_event_body .= '<td class="right negative">' . $markup . '</td>';
         } else {
             $first_event_body .= '<td class="right">-</td>';
         }
         $first_event_body .= '</tr>';
     } elseif ($this_battle->battle_result == 'defeat') {
         // Print out the label and target vs actual turn stats
         $first_event_body .= '<tr>';
         $first_event_body .= '<td class="left">Target Turns</td>';
         $first_event_body .= '<td class="center">' . $this_battle->counters['battle_turn'] . ' &nbsp;/&nbsp; ' . $this_battle->battle_turns_limit . '</td>';
         // Print the markup for the empty fields
         $first_event_body .= '<td class="center">-</td>';
         $first_event_body .= '<td class="right">-</td>';
         $first_event_body .= '<td class="right">-</td>';
         $first_event_body .= '</tr>';
         // Print out the label and target vs actual robot stats
         $first_event_body .= '<tr>';
         $first_event_body .= '<td class="left">Target Robots</td>';
         $first_event_body .= '<td class="center">' . $this_player_info['counters']['robots_masters_total'] . ' &nbsp;/&nbsp; ' . $this_battle->battle_robots_limit . '</td>';
         // Print the markup for the empty fields
         $first_event_body .= '<td class="center">-</td>';
         $first_event_body .= '<td class="right">-</td>';
         $first_event_body .= '<td class="right">-</td>';
         $first_event_body .= '</tr>';
     }
     // Print out the final rewards for this battle
     $first_event_body .= '<tr>';
     $first_event_body .= '<td class="left"><strong>Final Rewards</strong></td>';
     $first_event_body .= '<td class="center"></td>';
     $first_event_body .= '<td class="center"></td>';
     $first_event_body .= '<td class="right"><strong>' . ($temp_reward_points_final != 1 ? number_format($temp_reward_points_final, 0, '.', ',') . ' Points' : '1 Point') . '</strong></td>';
     $first_event_body .= '<td class="right"><strong>' . ($temp_reward_zenny_final != 1 ? number_format($temp_reward_zenny_final, 0, '.', ',') . ' Zenny' : '1 Zenny') . '</strong></td>';
     $first_event_body .= '</tr>';
     // Finalize the table body for the results
     $first_event_body .= '</tbody>';
     $first_event_body .= '</table>';
     // Print the battle complete message
     $event_options = array();
     $event_options['this_header_float'] = 'center';
     $event_options['this_body_float'] = 'center';
     $event_options['this_event_class'] = false;
     $event_options['console_show_this'] = false;
     $event_options['console_show_target'] = false;
     $event_options['console_container_classes'] = 'field_type field_type_event field_type_' . ($this_battle->battle_result == 'victory' ? 'nature' : 'flame');
     $this_battle->events_create($target_robot, $this_robot, $first_event_header, $first_event_body, $event_options);
     // Add the flag to prevent any further messages from appearing
     $this_battle->set_flag('battle_complete_message_created', true);
     // Return the result for this battle function
     return $this_return;
 }