/** [Cydh] * Gives item(s) to the player based on item group * @param sd: Player that obtains item from item group * @param group_id: The group ID of item that obtained by player * @param *group: struct s_item_group from itemgroup_db[group_id].random[idx] or itemgroup_db[group_id].must[sub_group][idx] */ static void itemdb_pc_get_itemgroup_sub(struct map_session_data *sd, uint16 group_id, struct s_item_group *group) { uint16 i; struct item tmp; nullpo_retv(group); memset(&tmp, 0, sizeof(tmp)); tmp.nameid = group->nameid; tmp.amount = (itemdb_isstackable(group->nameid)) ? group->amount : 1; tmp.bound = group->bound; tmp.identify = 1; tmp.expire_time = (group->duration) ? (unsigned int)(time(NULL) + group->duration * 60) : 0; if (group->isNamed) { tmp.card[0] = itemdb_isequip(group->nameid) ? CARD0_FORGE : CARD0_CREATE; tmp.card[1] = 0; tmp.card[2] = GetWord(sd->status.char_id, 0); tmp.card[3] = GetWord(sd->status.char_id, 1); } //Do loop for non-stackable item for (i = 0; i < group->amount; i++) { int flag; if ((flag = pc_additem(sd,&tmp,tmp.amount,LOG_TYPE_SCRIPT))) clif_additem(sd,0,0,flag); else if (!flag && group->isAnnounced) { char output[CHAT_SIZE_MAX]; sprintf(output, msg_txt(717), sd->status.name, itemdb_jname(group->nameid), itemdb_jname(sd->itemid)); clif_broadcast(&sd->bl, output,strlen(output), 0, ALL_CLIENT); } if (itemdb_isstackable(group->nameid)) break; } }
/** [Cydh] * Gives item(s) to the player based on item group * @param sd: Player that obtains item from item group * @param group_id: The group ID of item that obtained by player * @param *group: struct s_item_group from itemgroup_db[group_id].random[idx] or itemgroup_db[group_id].must[sub_group][idx] */ static void itemdb_pc_get_itemgroup_sub(struct map_session_data *sd, struct s_item_group_entry *data) { uint16 i; struct item tmp; nullpo_retv(data); memset(&tmp, 0, sizeof(tmp)); tmp.nameid = data->nameid; tmp.amount = (itemdb_isstackable(data->nameid)) ? data->amount : 1; tmp.bound = data->bound; tmp.identify = 1; tmp.expire_time = (data->duration) ? (unsigned int)(time(NULL) + data->duration*60) : 0; if (data->isNamed) { tmp.card[0] = itemdb_isequip(data->nameid) ? CARD0_FORGE : CARD0_CREATE; tmp.card[1] = 0; tmp.card[2] = GetWord(sd->status.char_id, 0); tmp.card[3] = GetWord(sd->status.char_id, 1); } // Do loop for non-stackable item for (i = 0; i < data->amount; i++) { char flag = 0; #ifdef ENABLE_ITEM_GUID tmp.unique_id = data->GUID ? pc_generate_unique_id(sd) : 0; // Generate UID #endif if ((flag = pc_additem(sd, &tmp, tmp.amount, LOG_TYPE_SCRIPT))) clif_additem(sd, 0, 0, flag); else if (!flag && data->isAnnounced) { char output[CHAT_SIZE_MAX]; sprintf(output, msg_txt(NULL, 717), sd->status.name, itemdb_jname(data->nameid), itemdb_jname(sd->itemid)); //! TODO: Move this broadcast to proper packet intif_broadcast(output, strlen(output) + 1, BC_DEFAULT); //clif_broadcast_obtain_special_item(); } if (itemdb_isstackable(data->nameid)) break; } }
/** [Cydh] * Gives item(s) to the player based on item group * @param sd: Player that obtains item from item group * @param group_id: The group ID of item that obtained by player * @param *group: struct s_item_group from itemgroup_db[group_id].random[idx] or itemgroup_db[group_id].must[sub_group][idx] */ static void itemdb_pc_get_itemgroup_sub(struct map_session_data *sd, struct s_item_group_entry *data) { uint16 i, get_amt = 0; struct item tmp; nullpo_retv(data); memset(&tmp, 0, sizeof(tmp)); tmp.nameid = data->nameid; tmp.bound = data->bound; tmp.identify = 1; tmp.expire_time = (data->duration) ? (unsigned int)(time(NULL) + data->duration*60) : 0; if (data->isNamed) { tmp.card[0] = itemdb_isequip(data->nameid) ? CARD0_FORGE : CARD0_CREATE; tmp.card[1] = 0; tmp.card[2] = GetWord(sd->status.char_id, 0); tmp.card[3] = GetWord(sd->status.char_id, 1); } if (!itemdb_isstackable(data->nameid)) get_amt = 1; else get_amt = data->amount; // Do loop for non-stackable item for (i = 0; i < data->amount; i += get_amt) { char flag = 0; tmp.unique_id = data->GUID ? pc_generate_unique_id(sd) : 0; // Generate GUID if ((flag = pc_additem(sd, &tmp, get_amt, LOG_TYPE_SCRIPT))) { clif_additem(sd, 0, 0, flag); if (pc_candrop(sd, &tmp)) map_addflooritem(&tmp, tmp.amount, sd->bl.m, sd->bl.x,sd->bl.y, 0, 0, 0, 0, 0); } else if (!flag && data->isAnnounced) intif_broadcast_obtain_special_item(sd, data->nameid, sd->itemid, ITEMOBTAIN_TYPE_BOXITEM); } }
/** * Open vending for Autotrader * @param sd Player as autotrader */ void vending_reopen( struct map_session_data* sd ){ nullpo_retv(sd); // Ready to open vending for this char if ( autotrader_count > 0 && autotraders){ uint16 i; uint8 *data, *p, fail = 0; uint16 j, count; ARR_FIND(0,autotrader_count,i,autotraders[i] && autotraders[i]->char_id == sd->status.char_id); if (i >= autotrader_count) { return; } // Init vending data for autotrader CREATE(data, uint8, autotraders[i]->count * 8); for (j = 0, p = data, count = autotraders[i]->count; j < autotraders[i]->count; j++) { struct s_autotrade_entry *entry = autotraders[i]->entries[j]; uint16 *index = (uint16*)(p + 0); uint16 *amount = (uint16*)(p + 2); uint32 *value = (uint32*)(p + 4); // Find item position in cart ARR_FIND(0, MAX_CART, entry->index, sd->status.cart[entry->index].id == entry->cartinventory_id); if (entry->index == MAX_CART) { count--; continue; } *index = entry->index + 2; *amount = itemdb_isstackable(sd->status.cart[entry->index].nameid) ? entry->amount : 1; *value = entry->price; p += 8; } // Set him into a hacked prevend state sd->state.prevend = 1; // Make sure abort all NPCs npc_event_dequeue(sd); pc_cleareventtimer(sd); // Open the vending again if( (fail = vending_openvending(sd, autotraders[i]->title, data, count)) == 0 ){ // Set him to autotrade if (Sql_Query( mmysql_handle, "UPDATE `%s` SET `autotrade` = 1, `body_direction` = '%d', `head_direction` = '%d', `sit` = '%d' " "WHERE `id` = %d;", vendings_db, autotraders[i]->dir, autotraders[i]->head_dir, autotraders[i]->sit, sd->vender_id ) != SQL_SUCCESS ) { Sql_ShowDebug( mmysql_handle ); } // Make vendor look perfect pc_setdir(sd, autotraders[i]->dir, autotraders[i]->head_dir); clif_changed_dir(&sd->bl, AREA_WOS); if( autotraders[i]->sit ) { pc_setsit(sd); skill_sit(sd, 1); clif_sitting(&sd->bl); } // Immediate save chrif_save(sd, 3); ShowInfo("Loaded vending for '"CL_WHITE"%s"CL_RESET"' with '"CL_WHITE"%d"CL_RESET"' items at "CL_WHITE"%s (%d,%d)"CL_RESET"\n", sd->status.name, count, mapindex_id2name(sd->mapindex), sd->bl.x, sd->bl.y); }else{ // Failed to open the vending, set him offline ShowError("Failed (%d) to load autotrade vending data for '"CL_WHITE"%s"CL_RESET"' with '"CL_WHITE"%d"CL_RESET"' items\n", fail, sd->status.name, count ); map_quit( sd ); } aFree(data); //If the last autotrade is loaded, clear autotraders [Cydh] if (++autotrader_loaded_count >= autotrader_count) do_final_vending_autotrade(); } }
/** * Open vending for Autotrader * @param sd Player as autotrader */ void vending_reopen( struct map_session_data* sd ) { struct s_autotrader *at = NULL; int8 fail = -1; nullpo_retv(sd); // Open vending for this autotrader if ((at = uidb_get(vending_autotrader_db, sd->status.char_id)) && at->count && at->entries) { uint8 *data, *p; uint16 j, count; // Init vending data for autotrader CREATE(data, uint8, at->count * 8); for (j = 0, p = data, count = at->count; j < at->count; j++) { struct s_autotrade_entry *entry = at->entries[j]; uint16 *index = (uint16*)(p + 0); uint16 *amount = (uint16*)(p + 2); uint32 *value = (uint32*)(p + 4); // Find item position in cart ARR_FIND(0, MAX_CART, entry->index, sd->status.cart[entry->index].id == entry->cartinventory_id); if (entry->index == MAX_CART) { count--; continue; } *index = entry->index + 2; *amount = itemdb_isstackable(sd->status.cart[entry->index].nameid) ? entry->amount : 1; *value = entry->price; p += 8; } sd->state.prevend = 1; // Set him into a hacked prevend state sd->state.autotrade = 1; // Make sure abort all NPCs npc_event_dequeue(sd); pc_cleareventtimer(sd); // Open the vending again if( (fail = vending_openvending(sd, at->title, data, count, at)) == 0 ) { // Make vendor look perfect pc_setdir(sd, at->dir, at->head_dir); clif_changed_dir(&sd->bl, AREA_WOS); if( at->sit ) { pc_setsit(sd); skill_sit(sd, 1); clif_sitting(&sd->bl); } // Immediate save chrif_save(sd, 3); ShowInfo("Vending loaded for '"CL_WHITE"%s"CL_RESET"' with '"CL_WHITE"%d"CL_RESET"' items at "CL_WHITE"%s (%d,%d)"CL_RESET"\n", sd->status.name, count, mapindex_id2name(sd->mapindex), sd->bl.x, sd->bl.y); } aFree(data); } if (at) { vending_autotrader_remove(at, true); if (db_size(vending_autotrader_db) == 0) vending_autotrader_db->clear(vending_autotrader_db, vending_autotrader_free); } if (fail != 0) { ShowError("vending_reopen: (Error:%d) Load failed for autotrader '"CL_WHITE"%s"CL_RESET"' (CID=%/AID=%d)\n", fail, sd->status.name, sd->status.char_id, sd->status.account_id); map_quit(sd); } }
/** * Attempt to set item or zeny to a mail * @param sd : player attaching the content * @param idx 0 - Zeny; >= 2 - Inventory item * @param amount : amout of zeny or number of item * @return see enum mail_attach_result in mail.h */ enum mail_attach_result mail_setitem(struct map_session_data *sd, short idx, uint32 amount) { if( pc_istrading(sd) ) return MAIL_ATTACH_ERROR; if( idx == 0 ) { // Zeny Transfer if( !pc_can_give_items(sd) ) return MAIL_ATTACH_UNTRADEABLE; #if PACKETVER < 20150513 if( amount > sd->status.zeny ) amount = sd->status.zeny; // TODO: confirm this behavior for old mail system #else if( ( amount + battle_config.mail_zeny_fee / 100 * amount ) > sd->status.zeny ) return MAIL_ATTACH_ERROR; #endif sd->mail.zeny = amount; // clif_updatestatus(sd, SP_ZENY); return MAIL_ATTACH_SUCCESS; } else { // Item Transfer int i, j, total = 0; idx -= 2; if( idx < 0 || idx >= MAX_INVENTORY ) return MAIL_ATTACH_ERROR; #if PACKETVER < 20150513 i = 0; // Remove existing item mail_removeitem(sd, 0, sd->mail.item[i].index + 2, sd->mail.item[i].amount); #else ARR_FIND(0, MAIL_MAX_ITEM, i, sd->mail.item[i].index == idx && sd->mail.item[i].nameid > 0 ); // The same item had already been added to the mail if( i < MAIL_MAX_ITEM ){ // Check if it is stackable if( !itemdb_isstackable(sd->mail.item[i].nameid) ){ return MAIL_ATTACH_ERROR; } // Check if it exceeds the total amount if( ( amount + sd->mail.item[i].amount ) > sd->inventory.u.items_inventory[idx].amount ){ return MAIL_ATTACH_ERROR; } // Check if it exceeds the total weight if( battle_config.mail_attachment_weight ){ for( j = 0; j < i; j++ ){ total += sd->mail.item[j].amount * ( sd->inventory_data[sd->mail.item[j].index]->weight / 10 ); } total += amount * sd->inventory_data[idx]->weight / 10; if( total > battle_config.mail_attachment_weight ){ return MAIL_ATTACH_WEIGHT; } } sd->mail.item[i].amount += amount; return MAIL_ATTACH_SUCCESS; }else{ ARR_FIND(0, MAIL_MAX_ITEM, i, sd->mail.item[i].nameid == 0); if( i == MAIL_MAX_ITEM ){ return MAIL_ATTACH_SPACE; } // Check if it exceeds the total weight if( battle_config.mail_attachment_weight ){ for( j = 0; j < i; j++ ){ total += sd->mail.item[j].amount * ( sd->inventory_data[sd->mail.item[j].index]->weight / 10 ); } total += amount * sd->inventory_data[idx]->weight / 10; if( total > battle_config.mail_attachment_weight ){ return MAIL_ATTACH_WEIGHT; } } } #endif if( amount > sd->inventory.u.items_inventory[idx].amount ) return MAIL_ATTACH_ERROR; if( !pc_can_give_items(sd) || sd->inventory.u.items_inventory[idx].expire_time || !itemdb_available(sd->inventory.u.items_inventory[idx].nameid) || !itemdb_canmail(&sd->inventory.u.items_inventory[idx],pc_get_group_level(sd)) || (sd->inventory.u.items_inventory[idx].bound && !pc_can_give_bounded_items(sd)) ) return MAIL_ATTACH_UNTRADEABLE; sd->mail.item[i].index = idx; sd->mail.item[i].nameid = sd->inventory.u.items_inventory[idx].nameid; sd->mail.item[i].amount = amount; return MAIL_ATTACH_SUCCESS; } }