diff --git a/src/ItemManager.cpp b/src/ItemManager.cpp index b67f73c..994f9ea 100644 --- a/src/ItemManager.cpp +++ b/src/ItemManager.cpp @@ -9,13 +9,13 @@ #include // for memset() #include -std::map, Item> ItemManager::ItemData; +std::map, ItemManager::Item> ItemManager::ItemData; std::map> ItemManager::VendorTables; std::map ItemManager::CrocPotTable; std::map> ItemManager::RarityRatios; std::map ItemManager::Crates; // pair Itemset, Rarity -> vector of pointers (map iterators) to records in ItemData -std::map, std::vector, Item>::iterator>> ItemManager::CrateItems; +std::map, std::vector, ItemManager::Item>::iterator>> ItemManager::CrateItems; void ItemManager::init() { REGISTER_SHARD_PACKET(P_CL2FE_REQ_ITEM_MOVE, itemMoveHandler); @@ -1012,9 +1012,9 @@ int ItemManager::findFreeSlot(Player *plr) { return -1; } -Item* ItemManager::getItemData(int32_t id, int32_t type) { - if(ItemData.find(std::pair(id, type)) != ItemData.end()) - return &ItemData[std::pair(id, type)]; +ItemManager::Item* ItemManager::getItemData(int32_t id, int32_t type) { + if(ItemData.find(std::make_pair(id, type)) != ItemData.end()) + return &ItemData[std::make_pair(id, type)]; return nullptr; } diff --git a/src/ItemManager.hpp b/src/ItemManager.hpp index 3708f01..6505965 100644 --- a/src/ItemManager.hpp +++ b/src/ItemManager.hpp @@ -3,10 +3,6 @@ #include "CNShardServer.hpp" #include "Player.hpp" -struct Item { - bool tradeable, sellable; - int buyPrice, sellPrice, stackSize, level, rarity, pointDamage, groupDamage, defense, gender; // TODO: implement more as needed -}; struct VendorListing { int sort, type, iID; }; @@ -25,6 +21,10 @@ namespace ItemManager { INVENTORY = 1, BANK = 3 }; + struct Item { + bool tradeable, sellable; + int buyPrice, sellPrice, stackSize, level, rarity, pointDamage, groupDamage, defense, gender; // TODO: implement more as needed + }; // hopefully this is fine since it's never modified after load extern std::map, Item> ItemData; // -> data extern std::map> VendorTables; diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 8aa4c1d..94ad665 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -120,7 +120,7 @@ void NPCManager::npcVendorBuy(CNSocket* sock, CNPacketData* data) { sP_CL2FE_REQ_PC_VENDOR_ITEM_BUY* req = (sP_CL2FE_REQ_PC_VENDOR_ITEM_BUY*)data->buf; Player *plr = PlayerManager::getPlayer(sock); - Item* item = ItemManager::getItemData(req->Item.iID, req->Item.iType); + ItemManager::Item* item = ItemManager::getItemData(req->Item.iID, req->Item.iType); if (item == nullptr) { std::cout << "[WARN] Item id " << req->Item.iID << " with type " << req->Item.iType << " not found (buy)" << std::endl; @@ -178,7 +178,7 @@ void NPCManager::npcVendorSell(CNSocket* sock, CNPacketData* data) { } sItemBase* item = &plr->Inven[req->iInvenSlotNum]; - Item* itemData = ItemManager::getItemData(item->iID, item->iType); + ItemManager::Item* itemData = ItemManager::getItemData(item->iID, item->iType); if (itemData == nullptr || !itemData->sellable) { // sanity + sellable check std::cout << "[WARN] Item id " << item->iID << " with type " << item->iType << " not found (sell)" << std::endl; @@ -225,7 +225,7 @@ void NPCManager::npcVendorBuyback(CNSocket* sock, CNPacketData* data) { sP_CL2FE_REQ_PC_VENDOR_ITEM_RESTORE_BUY* req = (sP_CL2FE_REQ_PC_VENDOR_ITEM_RESTORE_BUY*)data->buf; Player* plr = PlayerManager::getPlayer(sock); - Item* item = ItemManager::getItemData(req->Item.iID, req->Item.iType); + ItemManager::Item* item = ItemManager::getItemData(req->Item.iID, req->Item.iType); if (item == nullptr) { std::cout << "[WARN] Item id " << req->Item.iID << " with type " << req->Item.iType << " not found (rebuy)" << std::endl; @@ -364,8 +364,8 @@ void NPCManager::npcCombineItems(CNSocket* sock, CNPacketData* data) { sItemBase* itemStats = &plr->Inven[req->iStatItemSlot]; sItemBase* itemLooks = &plr->Inven[req->iCostumeItemSlot]; - Item* itemStatsDat = ItemManager::getItemData(itemStats->iID, itemStats->iType); - Item* itemLooksDat = ItemManager::getItemData(itemLooks->iID, itemLooks->iType); + ItemManager::Item* itemStatsDat = ItemManager::getItemData(itemStats->iID, itemStats->iType); + ItemManager::Item* itemLooksDat = ItemManager::getItemData(itemLooks->iID, itemLooks->iType); if (itemStatsDat == nullptr || itemLooksDat == nullptr || ItemManager::CrocPotTable.find(abs(itemStatsDat->level - itemLooksDat->level)) == ItemManager::CrocPotTable.end()) { // sanity check 2 diff --git a/src/TableData.cpp b/src/TableData.cpp index 31dd0c4..79c0b46 100644 --- a/src/TableData.cpp +++ b/src/TableData.cpp @@ -130,9 +130,23 @@ void TableData::init() { itemSet = xdtData[setNames[i]]["m_pItemData"]; for (nlohmann::json::iterator _item = itemSet.begin(); _item != itemSet.end(); _item++) { auto item = _item.value(); + int itemID = item["m_iItemNumber"]; int typeOverride = getItemType(i); // used for special cases where iEquipLoc doesn't indicate item type - ItemManager::ItemData[std::pair(item["m_iItemNumber"], typeOverride != -1 ? typeOverride : (int)item["m_iEquipLoc"])] - = { item["m_iTradeAble"] == 1, item["m_iSellAble"] == 1, item["m_iItemPrice"], item["m_iItemSellPrice"], item["m_iStackNumber"], i > 9 ? 0 : (int)item["m_iMinReqLev"], i > 9 ? 1 : (int)item["m_iRarity"], i > 9 ? 0 : (int)item["m_iPointRat"], i > 9 ? 0 : (int)item["m_iGroupRat"], i > 9 ? 0 : (int)item["m_iDefenseRat"], i > 9 ? 0 : (int)item["m_iReqSex"] }; + INITSTRUCT(ItemManager::Item, itemData); + itemData.tradeable = item["m_iTradeAble"] == 1; + itemData.sellable = item["m_iSellAble"] == 1; + itemData.buyPrice = item["m_iItemPrice"]; + itemData.sellPrice = item["m_iItemSellPrice"]; + itemData.stackSize = item["m_iStackNumber"]; + itemData.rarity = i > 9 ? 1 : (int)item["m_iRarity"]; + if (i <= 9) { + itemData.level = item["m_iMinReqLev"]; + itemData.pointDamage = item["m_iPointRat"]; + itemData.groupDamage = item["m_iGroupRat"]; + itemData.defense = item["m_iDefenseRat"]; + itemData.gender = item["m_iReqSex"]; + } + ItemManager::ItemData[std::make_pair(itemID, typeOverride != -1 ? typeOverride : (int)item["m_iEquipLoc"])] = itemData; } } @@ -478,11 +492,11 @@ void TableData::loadDrops() { throw TableException(std::string(buff)); } - std::map, Item>::iterator toAdd = ItemManager::ItemData.find(itemDataKey); + std::map, ItemManager::Item>::iterator toAdd = ItemManager::ItemData.find(itemDataKey); // if item collection doesn't exist, start a new one if (ItemManager::CrateItems.find(itemSetkey) == ItemManager::CrateItems.end()) { - std::vector, Item>::iterator> vector; + std::vector, ItemManager::Item>::iterator> vector; vector.push_back(toAdd); ItemManager::CrateItems[itemSetkey] = vector; } else // else add a new element to existing collection