#ifdef __SORT_INVENTORY_ITEMS__
// Function to sort items alphabetically by name
static bool SortItems(const LPITEM& s1, const LPITEM& s2)
{
std::string name(s1->GetName());
std::string name2(s2->GetName());
return name < name2;
}
// Sort normal inventory
void CHARACTER::SortInventoryItems()
{
int lastSortInventoryPulse = GetSortInventoryPulse();
int iPulse = thecore_pulse();
if (lastSortInventoryPulse - iPulse > 0)
{
int deltaInSeconds = ((lastSortInventoryPulse / PASSES_PER_SEC(1)) - (iPulse / PASSES_PER_SEC(1)));
ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You must wait %d seconds before sorting again."), deltaInSeconds);
return;
}
if (IsDead() || GetExchange() || GetMyShop() || GetShopOwner() || IsOpenSafebox() || IsCubeOpen())
{
ChatPacket(CHAT_TYPE_INFO, LC_TEXT("Please close all other windows before sorting your inventory."));
return;
}
std::vector<LPITEM> v;
LPITEM myitems;
v.clear();
for (int i = 0; i < INVENTORY_MAX_NUM; ++i)
{
if (!(myitems = GetInventoryItem(i)))
continue;
v.push_back(myitems);
myitems->RemoveFromCharacter();
}
std::sort(v.begin(), v.end(), SortItems);
for (auto item : v)
{
if (item)
{
AutoGiveItem(item, false); // Restore and save to DB
}
}
SetSortInventoryPulse(thecore_pulse() + PASSES_PER_SEC(180)); // 3 minutes cooldown
ChatPacket(CHAT_TYPE_INFO, LC_TEXT("Inventory has been successfully sorted."));
}
// Sort special inventory tabs (books, stones, etc.)
void CHARACTER::SortSpecialInventoryItems(BYTE type)
{
if (!IsPC() || !CanHandleItem())
return;
quest::PC* pPC = quest::CQuestManager::instance().GetPCForce(GetPlayerID());
if (!pPC || pPC->IsRunning())
return;
if (m_pkTimedEvent)
{
ChatPacket(CHAT_TYPE_INFO, LC_TEXT("Sorting cancelled."));
event_cancel(&m_pkTimedEvent);
}
int lastSortSpecialInventoryPulse = GetSortSpecialInventoryPulse();
int iPulse = thecore_pulse();
if (lastSortSpecialInventoryPulse - iPulse > 0)
{
int deltaInSeconds = ((lastSortSpecialInventoryPulse / PASSES_PER_SEC(1)) - (iPulse / PASSES_PER_SEC(1)));
ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You must wait %d seconds before sorting this tab again."), deltaInSeconds);
return;
}
if (IsDead() || GetExchange() || GetMyShop() || GetShopOwner() || IsOpenSafebox() || IsCubeOpen())
{
ChatPacket(CHAT_TYPE_INFO, LC_TEXT("Please close all other windows before sorting your special inventory."));
return;
}
std::vector<LPITEM> vecItems;
vecItems.clear();
LPITEM pkItem;
int startSlot = 0, endSlot = 0;
switch (type)
{
case 0: startSlot = SKILL_BOOK_INVENTORY_SLOT_START; endSlot = SKILL_BOOK_INVENTORY_SLOT_END; break;
case 1: startSlot = UPGRADE_ITEMS_INVENTORY_SLOT_START; endSlot = UPGRADE_ITEMS_INVENTORY_SLOT_END; break;
case 2: startSlot = STONE_INVENTORY_SLOT_START; endSlot = STONE_INVENTORY_SLOT_END; break;
case 3: startSlot = BOX_INVENTORY_SLOT_START; endSlot = BOX_INVENTORY_SLOT_END; break;
case 4: startSlot = EFSUN_INVENTORY_SLOT_START; endSlot = EFSUN_INVENTORY_SLOT_END; break;
case 5: startSlot = CICEK_INVENTORY_SLOT_START; endSlot = CICEK_INVENTORY_SLOT_END; break;
default: startSlot = SKILL_BOOK_INVENTORY_SLOT_START; endSlot = SKILL_BOOK_INVENTORY_SLOT_END; break;
}
for (int i = startSlot; i < endSlot; ++i)
{
if (!(pkItem = GetInventoryItem(i)))
continue;
vecItems.push_back(pkItem);
pkItem->RemoveFromCharacter();
}
std::sort(vecItems.begin(), vecItems.end(), SortItems);
for (auto item : vecItems)
{
if (item)
{
AutoGiveItem(item, false); // Restore and save to DB
}
}
SetSortSpecialInventoryPulse(thecore_pulse() + PASSES_PER_SEC(180)); // 3 minutes cooldown
ChatPacket(CHAT_TYPE_INFO, LC_TEXT("Special inventory tab has been successfully sorted."));
}
#endif // __SORT_INVENTORY_ITEMS__