/** * 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 ){ // Ready to open vending for this char if ( sd && autotrader_count > 0 && autotraders){ uint16 i; uint8 *data, *p; 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 = entry->amount; *value = entry->price; p += 8; } // Set him into a hacked prevend state sd->state.prevend = 1; // Open the vending again if( vending_openvending(sd, autotraders[i]->title, data, count) ){ // Set him to autotrade if (Sql_Query( mmysql_handle, "UPDATE `%s` SET `autotrade` = 1 WHERE `id` = %d;", vendings_db, sd->vender_id ) != SQL_SUCCESS ) { Sql_ShowDebug( mmysql_handle ); } // Make him look perfect unit_setdir(&sd->bl,battle_config.feature_autotrade_direction); if( battle_config.feature_autotrade_sit ) pc_setsit(sd); ShowInfo("Loaded autotrade vending data 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 ShowWarning("Failed to load autotrade vending data for '"CL_WHITE"%s"CL_RESET"' with '"CL_WHITE"%d"CL_RESET"' items\n", sd->status.name, count ); map_quit( sd ); } aFree(data); //If the last autotrade is loaded, clear autotraders [Cydh] if (i+1 >= autotrader_count) do_final_vending_autotrade(); } }