function event_trigger_get_item($zone)
{
    global $area_id, $db, $lang, $user_id;
    //Select zone specific items
    $sql = " SELECT * FROM  " . ADR_SHOPS_ITEMS_TABLE . " \n\t\tWHERE item_owner_id = '1'\n\t\tAND ( item_zone = '{$area_id}' || item_zone = '0' )\n\t\tORDER BY rand() LIMIT 1 ";
    if (!($result = $db->sql_query($sql))) {
        message_die(GENERAL_ERROR, 'Could not query battle list', '', __LINE__, __FILE__, $sql);
    }
    $new_item = $db->sql_fetchrow($result);
    $db->sql_freeresult($result);
    if (!$new_item) {
        return;
    }
    adr_shop_insert_item($new_item['item_id'], adr_make_new_item_id($user_id), $user_id, 1);
    $item_name = $new_item['item_name'];
    $message = '<img src="adr/images/zones/get_item.gif"><br /><br />' . $lang['Adr_zone_event_item'] . '<br /><br /><b>' . $item_name . '</b><br /><br /><img src="adr/images/items/' . $item_icon . '"><br /><br />' . $item_desc . '<br /><br />' . $lang['Adr_zone_event_return'] . '<br /><br />';
    message_die(GENERAL_ERROR, $message, 'Zones', '');
}
function adr_steal_item($user_id, $item_id, $shop_owner_id, $shop_id)
{
    global $db, $lang, $phpEx, $adr_general, $board_config, $store_id;
    // Fix the values
    $user_id = intval($user_id);
    $item_id = intval($item_id);
    $shop_owner_id = intval($shop_owner_id);
    $shop_id = intval($shop_id);
    if ($shop_owner_id != 1) {
        $direction = append_sid("adr_shops.{$phpEx}?mode=see_shop&amp;shop_id=" . $shop_id);
    } else {
        $direction = append_sid("adr_shops.{$phpEx}?mode=view_store&amp;shop_id=" . $shop_id);
    }
    // Check if stealing is enabled by admin
    if (!$adr_general['allow_shop_steal']) {
        $message = sprintf($lang['Adr_steal_item_forbidden'], $sum, $points_name);
        $message .= '<br /><br />' . sprintf($lang['Adr_return'], "<a href=\"" . $direction . "\">", "</a>");
        message_die(GENERAL_MESSAGE, $message);
    }
    // Check if user is high enough lvl to be allowed steal attempt
    if ($adr_general['Adr_shop_steal_min_lvl'] > '0') {
        $adr_user = adr_get_user_infos($user_id);
        if ($adr_user['character_level'] < $adr_general['Adr_shop_steal_min_lvl']) {
            $message = $lang['Adr_shop_steal_min_lvl'] . '<br /><br />';
            $message .= sprintf($lang['Adr_shop_steal_min_lvl2'], '<b>', $adr_general['Adr_shop_steal_min_lvl'], '</b>');
            $message .= '<br /><br />' . sprintf($lang['Adr_return'], "<a href=\"" . $direction . "\">", "</a>");
            message_die(GENERAL_MESSAGE, $message);
        }
    }
    $sql = "SELECT character_thief_limit FROM " . ADR_CHARACTERS_TABLE . "\n\t\tWHERE character_id = {$user_id} ";
    $result = $db->sql_query($sql);
    if (!$result) {
        message_die(GENERAL_ERROR, 'Could not obtain thief limit info', "", __LINE__, __FILE__, $sql);
    }
    $thief_limit = $db->sql_fetchrow($result);
    if ($adr_general['Adr_character_limit_enable'] != 0 && $thief_limit['character_thief_limit'] < 1) {
        adr_previous(Adr_thief_limit, adr_shops, '');
    }
    // Select the item infos
    $shop_more_sql = $shop_owner_id != '1' ? 'AND item_in_shop = 1' : '';
    $sql = "SELECT * FROM " . ADR_SHOPS_ITEMS_TABLE . "\n\t\tWHERE item_owner_id = '{$shop_owner_id}'\n\t\tAND item_id = '{$item_id}'\n\t\t{$shop_more_sql}";
    $result = $db->sql_query($sql);
    if (!$result) {
        message_die(GENERAL_ERROR, 'Could not obtain shops items information', "", __LINE__, __FILE__, $sql);
    }
    $item_data = $db->sql_fetchrow($result);
    // Check item is stealable
    if ($item_data['item_steal_dc'] == '0' && $shop_owner_id == '1') {
        //		adr_previous(Adr_store_not_stealable, shops, "view_store&amp;shop_id=".$shop_id."", '', 'adr/images/store_owners/'.$store_info['shop_logo'].'');
        adr_previous(Adr_store_not_stealable, adr_shops);
    }
    // Check for successful steal or not
    $success = adr_use_skill_thief($user_id, $item_data['item_steal_dc']);
    if ($success == TRUE) {
        // Make the new id for the item
        $sql = "SELECT item_id FROM " . ADR_SHOPS_ITEMS_TABLE . "\n\t\t\tWHERE item_owner_id = '{$user_id}'\n\t\t\tORDER BY item_id\n\t\t\tDESC LIMIT 1";
        $result = $db->sql_query($sql);
        if (!$result) {
            message_die(GENERAL_ERROR, 'Could not obtain item information', "", __LINE__, __FILE__, $sql);
        }
        $data = $db->sql_fetchrow($result);
        $new_item_id = $data['item_id'] + 1;
        if ($shop_owner_id != '1') {
            // This will never be TRUE as of v0.4.3 because there is no player store stealing allowed
            $sql = "UPDATE " . ADR_SHOPS_ITEMS_TABLE . "\n\t\t\t\tSET item_owner_id = {$user_id},\n\t\t\t\t\titem_id = {$new_item_id},\n\t\t\t\t\titem_bought_timestamp = " . time() . ",\n\t\t\t\t\titem_in_shop = '0', \n\t\t\t\t\titem_auth = '0'\n\t\t\t\t\titem_stolen_by = '',\n\t\t\t\t\titem_stolen_timestamp = " . time . "\n\t\t\t\tWHERE item_owner_id = '{$shop_owner_id}'\n\t\t\t\tAND item_id = '{$item_id}'";
            $result = $db->sql_query($sql);
            if (!$result) {
                message_die(GENERAL_ERROR, 'Could not obtain shops items information', "", __LINE__, __FILE__, $sql);
            }
        } else {
            // Insert item details
            $type = 1;
            //This is so the insert function knows that this is a stolen item for update purposes only (optional function variable)
            adr_shop_insert_item($item_id, $new_item_id, $user_id, $shop_owner_id, $type, $shop_id);
        }
        adr_thief_limit($user_id);
        adr_store_stats_update_steal($success, $user_id, $shop_owner_id, $store_id);
        $message = sprintf($lang['Adr_steal_item_success'], $sum, $points_name);
        $message .= '<br /><br />' . sprintf($lang['Adr_return'], "<a href=\"" . $direction . "\">", "</a>");
        message_die(GENERAL_MESSAGE, $message);
    } else {
        $message = sprintf($lang['Adr_steal_item_failure'], $sum, $points_name);
        if ($adr_general['skill_thief_failure_damage']) {
            // Select the item price
            $shop_more_sql = $shop_owner_id != 1 ? 'AND i.item_in_shop = 1' : '';
            // Prevents users to buy items not in shops
            $sql = "SELECT i.item_price, i.item_name, u.user_points FROM " . ADR_SHOPS_ITEMS_TABLE . " i , " . USERS_TABLE . " u\n\t\t\t\tWHERE i.item_owner_id = {$shop_owner_id}\n\t\t\t\t\tAND u.user_id = {$user_id}\n\t\t\t\t\tAND i.item_id = {$item_id} \n\t\t\t\t{$shop_more_sql} ";
            $result = $db->sql_query($sql);
            if (!$result) {
                message_die(GENERAL_ERROR, 'Could not obtain shops items information', "", __LINE__, __FILE__, $sql);
            }
            $data = $db->sql_fetchrow($result);
            $user_points = $data['user_points'];
            $price = $data['item_price'];
            $fine = $price < intval($adr_general['skill_thief_failure_damage']) ? intval($adr_general['skill_thief_failure_damage']) : $price;
            if ($fine > $user_points && $adr_general['skill_thief_failure_punishment']) {
                $sql = " SELECT owner_id , account_sum FROM " . ADR_VAULT_USERS_TABLE . "\n\t\t\t\t\t\tWHERE owner_id = {$user_id} ";
                if (!($result = $db->sql_query($sql))) {
                    message_die(GENERAL_ERROR, 'Could not query user stats page', '', __LINE__, __FILE__, $sql);
                }
                $vault_sum = $db->sql_fetchrow($result);
                if ($adr_general['skill_thief_failure_punishment'] == 1) {
                    if (is_numeric($vault_sum['owner_id']) && $vault_sum['account_sum'] >= $fine) {
                        $vault_fine = $fine;
                        $fine = 0;
                        // Remove cash from Vault instead
                        $sql = "UPDATE " . ADR_VAULT_USERS_TABLE . "\n\t\t\t\t\t\t\tSET account_sum = account_sum - {$vault_fine}\n\t\t\t\t\t\t\tWHERE owner_id = {$user_id} ";
                        if (!$db->sql_query($sql)) {
                            message_die(GENERAL_ERROR, 'Could not update user points from Vault', "", __LINE__, __FILE__, $sql);
                        }
                    } elseif (is_numeric($vault_sum['owner_id']) && $vault_sum['account_sum'] > 0 && $vault_sum['account_sum'] < $fine) {
                        $vault_fine = $vault_sum['account_sum'];
                        $fine = 0;
                        // Remove cash from Vault instead
                        $sql = "UPDATE " . ADR_VAULT_USERS_TABLE . "\n\t\t\t\t\t\t\tSET account_sum = account_sum - {$vault_fine}\n\t\t\t\t\t\t\tWHERE owner_id = {$user_id} ";
                        if (!$db->sql_query($sql)) {
                            message_die(GENERAL_ERROR, 'Could not update user points from Vault', "", __LINE__, __FILE__, $sql);
                        }
                    } else {
                        $fine = $user_points;
                        $vault_fine = 0;
                    }
                } else {
                    if ($adr_general['skill_thief_failure_punishment'] == 2) {
                        adr_cell_imprison_user($user_id, 0, $adr_general['skill_thief_failure_time'], 0, $price, 1, 1, $lang['Adr_steal_item_failure_critical_all_sentence'], $adr_general['skill_thief_failure_type']);
                        $failure[0] = $lang['Adr_steal_item_failure_critical_all'];
                        $failure[1] = $lang['Adr_steal_item_failure_critical_post'];
                        $failure[2] = $lang['Adr_steal_item_failure_critical_read'];
                        $fail = $failure[$adr_general['skill_thief_failure_type']];
                        $fail_message = sprintf($lang['Adr_steal_item_failure_critical'], '<b>', adr_get_lang($data['item_name']), '</b>') . '<br /><br />' . $fail;
                        $fail_message .= '<br /><br />' . sprintf($lang['Adr_return'], "<a href=\"" . $direction . "\">", "</a>");
                        message_die(GENERAL_MESSAGE, $fail_message);
                    }
                }
            }
            adr_thief_limit($user_id);
            adr_store_stats_update_steal($success, $user_id, $shop_owner_id, $store_id);
            subtract_reward($user_id, $fine);
        }
        if ($fine != 0) {
            $fine = $fine;
        } else {
            $fine = $vault_fine;
        }
        $message .= '<br /><br />' . sprintf($lang['Adr_steal_item_failure_suite'], $fine, $points_name);
        $message .= '<br /><br />' . sprintf($lang['Adr_return'], "<a href=\"" . $direction . "\">", "</a>");
        message_die(GENERAL_MESSAGE, $message);
    }
}
function zone_npc_actions()
{
    global $db, $userdata, $area_id, $user_id, $lang, $adr_user, $user_npc_visit_array, $user_npc_quest_array;
    if (isset($_GET['npc']) || isset($_POST['npc'])) {
        $npc_action = isset($_POST['npc']) ? intval($_POST['npc']) : intval($_GET['npc']);
    }
    $npc_give_action = isset($_POST['npc_give']) ? $_POST['npc_give'] : null;
    if (isset($npc_action)) {
        // Deny access if user is imprisioned
        if ($userdata['user_cell_time']) {
            adr_previous('Adr_zone_no_thief_npc', 'adr_cell', '');
        }
        if (isset($_GET['npc_id']) || isset($_POST['npc_id'])) {
            $npc_id = isset($_POST['npc_id']) ? intval($_POST['npc_id']) : intval($_GET['npc_id']);
        }
        $adr_user = adr_npc_visit_update($npc_id, $adr_user);
        $sql = "SELECT * FROM  " . ADR_NPC_TABLE . "\n\t\t\t\tWHERE npc_id = '{$npc_id}'\n\t\t\t\t\tAND npc_enable = '1'";
        if (!($result = $db->sql_query($sql))) {
            message_die(GENERAL_ERROR, 'Could not query npc information', '', __LINE__, __FILE__, $sql);
        }
        //prevent user exploit
        if (!($npc_row = $db->sql_fetchrow($result))) {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        }
        $npc_zone_array = explode(',', $npc_row['npc_zone']);
        $npc_race_array = explode(',', $npc_row['npc_race']);
        $npc_class_array = explode(',', $npc_row['npc_class']);
        $npc_alignment_array = explode(',', $npc_row['npc_alignment']);
        $npc_element_array = explode(',', $npc_row['npc_element']);
        $npc_character_level_array = explode(',', $npc_row['npc_character_level']);
        $npc_visit_array = explode(',', $npc_row['npc_visit_prerequisite']);
        $npc_quest_array = explode(',', $npc_row['npc_quest_prerequisite']);
        $npc_visit = array();
        $npc_quest = array();
        $no_talk_message = array();
        $npc_quest_hide_array = array();
        // you can't click a disabled npc
        if (!$npc_row['npc_enable']) {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        }
        // you can't click a npc in another area
        if (!in_array($area_id, $npc_zone_array) && $npc_zone_array[0] != '0') {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        }
        if (!in_array($adr_user['character_race'], $npc_race_array) && $npc_race_array[0] != '0' && !$npc_row['npc_view']) {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        } else {
            if (!in_array($adr_user['character_race'], $npc_race_array) && $npc_race_array[0] != '0' && $npc_row['npc_view']) {
                $no_talk_message[] = $lang['Adr_Npc_race_no_talk_message'];
            }
        }
        if (!in_array($adr_user['character_class'], $npc_class_array) && $npc_class_array[0] != '0' && !$npc_row['npc_view']) {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        } else {
            if (!in_array($adr_user['character_class'], $npc_class_array) && $npc_class_array[0] != '0' && $npc_row['npc_view']) {
                $no_talk_message[] = $lang['Adr_Npc_class_no_talk_message'];
            }
        }
        if (!in_array($adr_user['character_alignment'], $npc_alignment_array) && $npc_alignment_array[0] != '0' && !$npc_row['npc_view']) {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        } else {
            if (!in_array($adr_user['character_alignment'], $npc_alignment_array) && $npc_alignment_array[0] != '0' && $npc_row['npc_view']) {
                $no_talk_message[] = $lang['Adr_Npc_alignment_no_talk_message'];
            }
        }
        if (!in_array($adr_user['character_element'], $npc_element_array) && $npc_element_array[0] != '0' && !$npc_row['npc_view']) {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        } else {
            if (!in_array($adr_user['character_element'], $npc_element_array) && $npc_element_array[0] != '0' && $npc_row['npc_view']) {
                $no_talk_message[] = $lang['Adr_Npc_element_no_talk_message'];
            }
        }
        if (!in_array($adr_user['character_level'], $npc_character_level_array) && $npc_character_level_array[0] != '0' && !$npc_row['npc_view']) {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        } else {
            if (!in_array($adr_user['character_level'], $npc_character_level_array) && $npc_character_level_array[0] != '0' && $npc_row['npc_view']) {
                $no_talk_message[] = $lang['Adr_Npc_character_level_no_talk_message'];
            }
        }
        for ($y = 0; $y < count($user_npc_visit_array); $y++) {
            $npc_visit[$y] = in_array($user_npc_visit_array[$y], $npc_visit_array) ? '1' : '0';
        }
        if (!in_array('1', $npc_visit) && $npc_visit_array[0] != '0' && !$npc_row['npc_view']) {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        } else {
            if (!in_array('1', $npc_visit) && $npc_visit_array[0] != '0' && $npc_row['npc_view']) {
                $no_talk_message[] = $lang['Adr_Npc_character_visit_no_talk_message'];
            }
        }
        for ($y = 0; $y < count($user_npc_quest_array); $y++) {
            $npc_quest_id = explode(':', $user_npc_quest_array[$y]);
            $npc_quest[$y] = in_array($npc_quest_id[0], $npc_quest_array) ? '1' : '0';
            $npc_quest_hide_array[$y] = $npc_quest_id[0] == $npc_row['npc_id'] ? '1' : '0';
        }
        if (!in_array('1', $npc_quest) && $npc_quest_array[0] != '0' && !$npc_row['npc_view']) {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        } else {
            if (!in_array('1', $npc_quest) && $npc_quest_array[0] != '0' && $npc_row['npc_view']) {
                $no_talk_message[$x] = $lang['Adr_Npc_character_quest_no_talk_message'];
            }
        }
        if (in_array('1', $npc_quest_hide_array) && $npc_row['npc_quest_hide']) {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        }
        $adr_moderators_array = explode(',', $board_config['zone_adr_moderators']);
        if ($npc_row['npc_user_level'] != '0' && !($npc_row['npc_user_level'] == '1' && $userdata['user_level'] == ADMIN || $npc_row['npc_user_level'] == '2' && (in_array($user_id, $adr_moderators_array) || $userdata['user_level'] == ADMIN))) {
            adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
        }
        //----
        adr_substract_points($user_id, $npc_row['npc_price'], 'adr_zones', '');
        if (count($no_talk_message)) {
            $message_id = rand(0, count($no_talk_message) - 1);
            $message = "<img src=\"adr/images/zones/npc/" . $npc_row['npc_img'] . "\"><br /><br /><b>" . $npc_row['npc_name'] . ":</b> <i>\"" . $no_talk_message[$message_id] . "\"</i><br /><br />" . $lang['Adr_zone_event_return'];
            $adr_zone_npc_title = sprintf($lang['Adr_Npc_speaking_with'], $npc_row['npc_name']);
            message_die(GENERAL_MESSAGE, $message, $adr_zone_npc_title, '');
            break;
        } else {
            if (adr_item_quest_check($npc_row['npc_id'], $adr_user['character_npc_check'], $npc_row['npc_times'])) {
                zone_npc_item_quest_check($npc_row);
            } else {
                $message = "<img src=\"adr/images/zones/npc/" . $npc_row['npc_img'] . "\"><br /><br /><b>" . $npc_row['npc_name'] . ":</b> <i>\"" . $npc_row['npc_message3'] . "\"</i><br /><br />" . $lang['Adr_zone_event_return'];
                $adr_zone_npc_title = sprintf($lang['Adr_Npc_speaking_with'], $npc_row['npc_name']);
                message_die(GENERAL_ERROR, $message, $adr_zone_npc_title, '');
                break;
            }
        }
    } else {
        if (isset($npc_give_action)) {
            $npc_id = intval($_POST['npc_id']);
            $item_id_array = explode(',', $_POST['item_id']);
            $sql = "SELECT * FROM  " . ADR_NPC_TABLE . "\n\t\t\t\tWHERE npc_id = '{$npc_id}'";
            if (!($result = $db->sql_query($sql))) {
                message_die(GENERAL_ERROR, 'Could not query npc information', '', __LINE__, __FILE__, $sql);
            }
            //prevent user exploit
            if (!($npc_give_row = $db->sql_fetchrow($result))) {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            if (!$npc_give_row['npc_enable']) {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            // V: let's add some security to this bullshit.
            $sql = "SELECT * FROM " . ADR_QUEST_LOG_TABLE . "\n\t\t\t\t\t   \t\tWHERE user_id = '{$user_id}'\n\t\t\t\t\t\t\tAND npc_id = '{$npc_id}'\n\t\t\t\t\t\t\t";
            if (!($result = $db->sql_query($sql))) {
                message_die(GENERAL_ERROR, 'Could not query quest log information', __LINE__, __FILE__, $sql);
            }
            $quest = $db->sql_fetchrow($result);
            if (!$quest) {
                $message = "<img src=\"adr/images/zones/npc/" . $npc_give_row['npc_img'] . "\"><br /><br /><b>" . $lang['npc_cant_give_quest_u_dont_have'] . "</b><br /><br />" . $lang['Adr_zone_event_return'];
                $adr_zone_npc_title = sprintf($lang['Adr_Npc_speaking_with'], $npc_row['npc_name']);
                message_die(GENERAL_ERROR, $message, $adr_zone_npc_title, '');
                break;
            }
            $npc_zone_array = explode(',', $npc_give_row['npc_zone']);
            $npc_race_array = explode(',', $npc_give_row['npc_race']);
            $npc_class_array = explode(',', $npc_give_row['npc_class']);
            $npc_alignment_array = explode(',', $npc_give_row['npc_alignment']);
            $npc_element_array = explode(',', $npc_give_row['npc_element']);
            $npc_character_level_array = explode(',', $npc_give_row['npc_character_level']);
            $npc_visit_array = explode(',', $npc_give_row['npc_visit_prerequisite']);
            $npc_quest_array = explode(',', $npc_give_row['npc_quest_prerequisite']);
            $npc_visit = array();
            $npc_quest = array();
            $npc_quest_hide_array = array();
            if (!in_array($area_id, $npc_zone_array) && $npc_zone_array[0] != '0') {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            if (!in_array($adr_user['character_race'], $npc_race_array) && $npc_race_array[0] != '0') {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            if (!in_array($adr_user['character_class'], $npc_class_array) && $npc_class_array[0] != '0') {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            if (!in_array($adr_user['character_alignment'], $npc_alignment_array) && $npc_alignment_array[0] != '0') {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            if (!in_array($adr_user['character_element'], $npc_element_array) && $npc_element_array[0] != '0') {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            if (!in_array($adr_user['character_level'], $npc_character_level_array) && $npc_character_level_array[0] != '0') {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            for ($y = 0; $y < count($user_npc_visit_array); $y++) {
                $npc_visit[$y] = in_array($user_npc_visit_array[$y], $npc_visit_array) ? '1' : '0';
            }
            if (!in_array('1', $npc_visit) && $npc_visit_array[0] != '0' && !$npc_give_row['npc_view']) {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            for ($y = 0; $y < count($user_npc_quest_array); $y++) {
                $npc_quest_id = explode(':', $user_npc_quest_array[$y]);
                $npc_quest[$y] = in_array($npc_quest_id[0], $npc_quest_array) ? '1' : '0';
                $npc_quest_hide_array[$y] = $npc_quest_id[0] == $npc_give_row['npc_id'] ? '1' : '0';
            }
            if (!in_array('1', $npc_quest) && $npc_quest_array[0] != '0' && !$npc_give_row['npc_view']) {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            if (in_array('1', $npc_quest_hide_array) && $npc_give_row['npc_quest_hide']) {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            $adr_moderators_array = explode(',', $board_config['zone_adr_moderators']);
            if ($npc_give_row['npc_user_level'] != '0' && !($npc_give_row['npc_user_level'] == '1' && $userdata['user_level'] == ADMIN || $npc_give_row['npc_user_level'] == '2' && (in_array($user_id, $adr_moderators_array) || $userdata['user_level'] == ADMIN))) {
                adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
            }
            if (!$npc_give_row['npc_quest_clue']) {
                // Check if user has the item(s)
                $npc_item_array = explode(',', $npc_give_row['npc_item']);
                for ($i = 0; $i < count($npc_item_array); $i++) {
                    $sql = "SELECT * FROM  " . ADR_SHOPS_ITEMS_TABLE . "\n\t   \t\t\t\t\tWHERE item_id = '" . $item_id_array[$i] . "'\n\t   \t\t\t\t\t    AND item_name = '" . $npc_item_array[$i] . "'\n\t   \t\t\t\t\t\tAND item_owner_id = '{$user_id}'\n\t\t   \t\t\t\t\tAND item_in_shop = '0'\n\t\t\t   \t\t\t\tAND item_in_warehouse = '0'\n\t\t\t\t\t\tLIMIT 1 ";
                    if (!($result = $db->sql_query($sql))) {
                        message_die(GENERAL_ERROR, 'Could not query shop item information', '', __LINE__, __FILE__, $sql);
                    }
                    $item_npc = $db->sql_fetchrowset($result);
                    if (count($item_npc) == 0 && ($npc_give_row['npc_kill_monster'] == "" or $npc_give_row['npc_kill_monster'] == '0')) {
                        adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_1']);
                    }
                }
            } else {
                if (!$npc_give_row['npc_quest_clue']) {
                    adr_item_quest_cheat_notification($user_id, $lang['Adr_zone_npc_cheating_type_2']);
                }
            }
            if (adr_item_quest_check($npc_give_row['npc_id'], $adr_user['character_npc_check'], $npc_give_row['npc_times'])) {
                if (!$npc_give_row['npc_quest_clue']) {
                    //Delete item(s) from character inventory
                    for ($i = 0; $i < count($npc_item_array); $i++) {
                        $delsql = "DELETE FROM " . ADR_SHOPS_ITEMS_TABLE . "\n\t\t\t\t\t\t\t\tWHERE item_owner_id = '{$user_id}'\n\t\t\t\t\t\t\t\t\tAND item_id = '" . $item_id_array[$i] . "' ";
                        if (!($aresult = $db->sql_query($delsql))) {
                            message_die(GENERAL_ERROR, "Couldn't update inventory info", "", __LINE__, __FILE__, $asql);
                        }
                        $sql5 = "SELECT * FROM " . ADR_QUEST_LOG_TABLE . "\n\t\t\t\t\t\tWHERE quest_item_need like '" . $npc_item_array[$i] . "," . "%' \n\t\t\t\t\t\tOR quest_item_need like '" . $npc_item_array[$i] . "'\n\t\t\t\t\t\tOR quest_item_need like '" . $npc_item_array[$i] . "," . "'\n\t\t\t\t\t\tOR quest_item_need like '%" . "," . $npc_item_array[$i] . "," . "%'\n\t\t\t\t\t\tOR quest_item_need like '%" . "," . $npc_item_array[$i] . "'\n\t\t\t\t\t\tAND user_id = '{$user_id}'\n\t\t\t\t\t\t";
                        $cresult = $db->sql_query($sql5);
                        if (!$cresult) {
                            message_die(GENERAL_ERROR, 'Could not obtain required quest information', "", __LINE__, __FILE__, $sql5);
                        }
                        if ($got_item_log = $db->sql_fetchrow($cresult)) {
                            $got_item += 1;
                        }
                        if ($got_item == count($npc_item_array) && ($npc_give_row['npc_kill_monster'] == '0' || $npc_give_row['npc_kill_monster'] == "")) {
                            //Copy Quest to the History
                            $insertsql = "INSERT INTO " . ADR_QUEST_LOG_HISTORY_TABLE . "\n\t\t\t\t\t\t\t( user_id, quest_killed_monster , quest_killed_monsters_amount , npc_id, quest_item_gave)\n\t\t\t\t\t\t\tVALUES ( '{$user_id}' , '" . $npc_give_row['npc_kill_monster'] . "' , '" . $npc_give_row['npc_monster_amount'] . "' , '" . $npc_give_row['npc_id'] . "', '" . $npc_give_row['npc_item'] . "' )";
                            $result = $db->sql_query($insertsql);
                            if (!$result) {
                                message_die(GENERAL_ERROR, "Couldn't insert finished quest", "", __LINE__, __FILE__, $insertsql);
                            }
                            //Delete the Quest of the log
                            $delsql2 = " DELETE FROM  " . ADR_QUEST_LOG_TABLE . "\n\t\t\t\t\t   \t\tWHERE user_id = '{$user_id}'\n\t\t\t\t\t\t\tAND npc_id = '{$npc_id}'\n\t\t\t\t\t   \t\t";
                            if (!($bresult = $db->sql_query($delsql2))) {
                                message_die(GENERAL_ERROR, "Couldn't update questlog info", "", __LINE__, __FILE__, $delsql2);
                            }
                        }
                    }
                    if ($npc_give_row['npc_kill_monster'] != '0' && $npc_give_row['npc_kill_monster'] != "" && $npc_give_row['quest_kill_monster_current_amount'] == $npc_give_row['npc_kill_monster_amount']) {
                        //Copy Quest to the History
                        $insertsql = "INSERT INTO " . ADR_QUEST_LOG_HISTORY_TABLE . "\n\t\t\t\t\t\t( user_id, quest_killed_monster , quest_killed_monsters_amount , npc_id, quest_item_gave)\n\t\t\t\t\t\tVALUES ( '{$user_id}' , '" . $npc_give_row['npc_kill_monster'] . "' , '" . $npc_give_row['npc_monster_amount'] . "' , '" . $npc_give_row['npc_id'] . "', '" . $npc_give_row['npc_item'] . "' )";
                        $result = $db->sql_query($insertsql);
                        if (!$result) {
                            message_die(GENERAL_ERROR, "Couldn't insert finished quest", "", __LINE__, __FILE__, $insertsql);
                        }
                        //Delete the Quest of the log
                        $delsql3 = " DELETE FROM  " . ADR_QUEST_LOG_TABLE . "\n\t\t\t   \t\t\tWHERE quest_kill_monster = '" . $npc_give_row['npc_kill_monster'] . "'\n\t\t\t   \t\t\tAND quest_kill_monster_current_amount = '" . $npc_give_row['npc_monster_amount'] . "'\n\t\t\t\t\t\tAND user_id = '{$user_id}'\n\t\t\t\t\t\tAND npc_id = '{$npc_id}'\n\t\t\t\t   \t\t";
                        if (!($dresult = $db->sql_query($delsql3))) {
                            message_die(GENERAL_ERROR, "Couldn't update questlog info", "", __LINE__, __FILE__, $delsql3);
                        }
                    }
                } else {
                    adr_substract_points($user_id, $npc_give_row['npc_quest_clue_price'], adr_zones, '');
                    //Delete the Quest of the log
                    $delsql2 = " DELETE FROM  " . ADR_QUEST_LOG_TABLE . "\n\t\t\t   \t\tWHERE user_id = '{$user_id}'\n\t\t\t\t\tAND npc_id = '{$npc_id}'\n\t\t\t   \t\t";
                    if (!($bresult = $db->sql_query($delsql2))) {
                        message_die(GENERAL_ERROR, "Couldn't update questlog info", "", __LINE__, __FILE__, $delsql2);
                    }
                }
                //give points prize
                adr_add_points($user_id, $npc_give_row['npc_points']);
                //give exp and sp prize
                $sql = "UPDATE  " . ADR_CHARACTERS_TABLE . "\n\t\t\t\t\tSET character_xp = character_xp + '" . $npc_give_row['npc_exp'] . "',\n\t\t\t\t\t\tcharacter_sp = character_sp + '" . $npc_give_row['npc_sp'] . "'\n\t\t\t\t\tWHERE character_id = '{$user_id}' ";
                if (!($result = $db->sql_query($sql))) {
                    message_die(GENERAL_ERROR, 'Could not update character information', '', __LINE__, __FILE__, $sql);
                }
                $prize_item = '';
                if ($npc_give_row['npc_item2'] != "0" && $npc_give_row['npc_item2'] != "") {
                    $npc_item2_array = explode(',', $npc_give_row['npc_item2']);
                    for ($i = 0; $i < count($npc_item2_array); $i++) {
                        adr_shop_insert_item($npc_item2_array[$i], adr_make_new_item_id($user_id), $user_id, 1, 0, 0, 'item_name');
                        if (count($npc_item2_array) == 1) {
                            $prize_item .= adr_get_lang($npc_item2_array[$i]);
                        } else {
                            if ($i >= 1 && $i == count($npc_item2_array) + 1) {
                                $prize_item .= ' and ' . adr_get_lang($npc_item2_array[$i]);
                            } else {
                                $prize_item .= ', ' . adr_get_lang($npc_item2_array[$i]);
                            }
                        }
                    }
                    $prize_message = sprintf($lang['Adr_zone_npc_item_prize'], $npc_give_row['npc_name'], $prize_item);
                }
                //Insert Character in check field
                if ($npc_give_row['npc_times'] > 0) {
                    adr_item_quest_check_insert($adr_user['character_npc_check'], $npc_give_row['npc_id'], $user_id);
                }
                //----
                $points_prize_lang = $npc_give_row['npc_points'] == 0 ? "" : sprintf($lang['Adr_zone_npc_points_prize'], number_format(intval($npc_give_row['npc_points'])), $board_config['points_name']);
                $exp_prize_lang = $npc_give_row['npc_exp'] == 0 ? "" : sprintf($lang['Adr_zone_npc_exp_prize'], number_format(intval($npc_give_row['npc_exp'])));
                $sp_prize_lang = $npc_give_row['npc_sp'] == 0 ? "" : sprintf($lang['Adr_zone_npc_sp_prize'], number_format(intval($npc_give_row['npc_sp'])));
                $item_prize_lang = count($npc_item2_array) == 0 || $npc_give_row['npc_item2'] == "" ? "" : $prize_message;
                $message = "<img src=\"adr/images/zones/npc/" . $npc_give_row['npc_img'] . "\"><br /><br /><b>" . $npc_give_row['npc_name'] . ":</b> <i>\"" . $npc_give_row['npc_message2'] . "\"</i><br /><br />" . $item_prize_lang . "" . $points_prize_lang . "" . $exp_prize_lang . "" . $sp_prize_lang . "<br />" . $lang['Adr_zone_event_return'];
                $adr_zone_npc_title = sprintf($lang['Adr_Npc_speaking_with'], $npc_row['npc_name']);
                message_die(GENERAL_ERROR, $message, $adr_zone_npc_title, '');
                break;
            } else {
                $message = "<img src=\"adr/images/zones/npc/" . $npc_give_row['npc_img'] . "\"><br /><br /><b>" . $npc_give_row['npc_name'] . ":</b> <i>\"" . $npc_give_row['npc_message2'] . "\"</i><br /><br />" . $lang['Adr_zone_event_return'];
                $adr_zone_npc_title = sprintf($lang['Adr_Npc_speaking_with'], $npc_row['npc_name']);
                message_die(GENERAL_ERROR, $message, $adr_zone_npc_title, '');
                break;
            }
        }
    }
}
function drop_gather_loot($zone_id, $user_id, $type, $skill_number)
{
    define('IN_ADR_ZONES', true);
    /******************************************************************************
     * $zone_id = zone the chracter is in
     * $user_id = chracters id
     * $type = type of gathering being done, mine, fish, etc, corresponds to 
     * 	               the loottable field in phpbb_adr_zone table
     * $skill_number = number of skill given in phpbb_adr_skills	                
     ******************************************************************************/
    global $db, $lang, $adr_general;
    $user_id = intval($user_id);
    $zone_id = intval($zone_id);
    $sql = "SELECT * FROM " . ADR_ZONES_TABLE . "\r\n\tWHERE zone_id = {$zone_id}\r\n\t";
    $result = $db->sql_query($sql);
    if (!$result) {
        message_die(GENERAL_ERROR, 'Could not obtain zone information', "", __LINE__, __FILE__, $sql);
    }
    $zone = $db->sql_fetchrow($result);
    //build zone check from table
    $loot_to_find = 'zone_' . $type . '_table';
    // V: return early
    if ($zone[$loot_to_find] == '' || $zone[$loot_to_find] == '0') {
        // return empty
        return null;
    }
    $zone_loottables = explode(':', $zone[$loot_to_find]);
    $sql = "SELECT * FROM " . ADR_SKILLS_TABLE . "\r\n\tWHERE skill_id = {$skill_number}";
    $result = $db->sql_query($sql);
    if (!$result) {
        message_die(GENERAL_ERROR, 'Could not obtain skill information', "", __LINE__, __FILE__, $sql);
    }
    $skill_data = mysql_fetch_array($result);
    //build character check from table
    $skill_to_use = "character_skill_" . $type;
    //$difficulty = loottable drop chance?
    $user_chance = $adr_user[$skill_to_use] * $skill_data['skill_chance'];
    do {
        $timer++;
        //roll the loottable
        $rnd_loottable = rand(0, count($zone_loottables));
        //sort out deactivated loottables
        $sql = "SELECT * FROM " . ADR_LOOTTABLES_TABLE . "\r\n\t\t\tWHERE loottable_status = 1\r\n\t\t\tAND loottable_id = '" . $zone_loottables[$rnd_loottable] . "'";
        $result = $db->sql_query($sql);
        if (!$result) {
            message_die(GENERAL_ERROR, 'Could not obtain loottable information', "", __LINE__, __FILE__, $sql);
        }
        //incase all monsters loottables are deactivated for some reason
        if ($timer > 10) {
            break;
        }
    } while (!($rolled_loottable = $db->sql_fetchrow($result)));
    //incase all monsters loottables are deactivated for some reason
    //if ($timer > 10000){break;}
    //now roll to see if we actually get an item
    $max = $user_chance * 100;
    $dicer = rand(1, $max);
    $dicer = $dicer + $adr_user[$skill_to_use] * $skill_data['skill_chance'];
    if ($dicer >= $rolled_loottable['loottable_dropchance']) {
        $sql = "SELECT * FROM " . ADR_SHOPS_ITEMS_TABLE . "\r\n\t\tWHERE item_owner_id = 1\r\n\t\tAND (item_loottables like '" . $rolled_loottable['loottable_id'] . ":" . "%'\r\n\t\tOR item_loottables like '" . $rolled_loottable['loottable_id'] . "'\r\n\t\tOR item_loottables like '%" . ":" . $rolled_loottable['loottable_id'] . ":" . "%'\r\n\t\tOR item_loottables like '%" . ":" . $rolled_loottable['loottable_id'] . "')";
        if (!($result = $db->sql_query($sql))) {
            message_die(GENERAL_ERROR, 'Could not query items list', '', __LINE__, __FILE__, $sql);
        }
        $possible_items_db = $db->sql_fetchrowset($result);
        //now roll for the item
        $rnd_item = rand(0, count($possible_items_db) - 1);
        //echo "<br> possible_items_db[rnd_item]['item_id']" . $possible_items_db[$rnd_item]['item_id'];
        $item_id = $possible_items_db[$rnd_item]['item_id'];
        // V: REFACTOOOR
        $item_data = adr_shop_insert_item($item_id, adr_make_new_item_id($user_id), $user_id, 1);
        $message .= "<br><br><table align=\"center\" border=\"0\" cellpadding=\"0\" class=\"gen\"><tr>";
        $message .= "<tr><td align=\"center\"  valign=\"top\"><span class=\"gen\">Vous avez obtenu : " . adr_get_lang($item_data['item_name']) . "<br><img src=\"./adr/images/items/" . $item_data['item_icon'] . "\"</span></td></tr>";
        $message .= "</table>";
    }
    return $message;
}