Load item tables + price implementation

This commit is contained in:
Gent 2020-09-14 00:25:14 -04:00
parent f55cc8f36d
commit c91022030c
4 changed files with 44 additions and 7 deletions

View File

@ -7,6 +7,7 @@
#include <string.h> // for memset() and memcmp() #include <string.h> // for memset() and memcmp()
#include <assert.h> #include <assert.h>
std::map<std::pair<int32_t, int32_t>, Item> ItemManager::ItemData;
std::map<int32_t, std::vector<VendorListing>> ItemManager::VendorTables; std::map<int32_t, std::vector<VendorListing>> ItemManager::VendorTables;
void ItemManager::init() { void ItemManager::init() {
@ -749,3 +750,11 @@ int ItemManager::findFreeSlot(Player *plr) {
// not found // not found
return -1; return -1;
} }
bool ItemManager::isItemRegistered(int32_t id, int32_t type) {
return ItemData.find(std::pair<int32_t, int32_t>(id, type)) != ItemData.end();
}
Item ItemManager::getItemData(int32_t id, int32_t type) {
return ItemData[std::pair<int32_t, int32_t>(id, type)];
}

View File

@ -3,8 +3,12 @@
#include "CNShardServer.hpp" #include "CNShardServer.hpp"
#include "Player.hpp" #include "Player.hpp"
struct Item {
bool tradeable, sellable;
int buyPrice, sellPrice, stackSize, level; // TODO: implement more as needed
};
struct VendorListing { struct VendorListing {
int sort, type, iID, price; int sort, type, iID;
}; };
namespace ItemManager { namespace ItemManager {
@ -14,6 +18,7 @@ namespace ItemManager {
BANK = 3 BANK = 3
}; };
// hopefully this is fine since it's never modified after load // hopefully this is fine since it's never modified after load
extern std::map<std::pair<int32_t, int32_t>, Item> ItemData; // <id, type> -> data
extern std::map<int32_t, std::vector<VendorListing>> VendorTables; extern std::map<int32_t, std::vector<VendorListing>> VendorTables;
void init(); void init();
@ -36,4 +41,6 @@ namespace ItemManager {
void chestOpenHandler(CNSocket* sock, CNPacketData* data); void chestOpenHandler(CNSocket* sock, CNPacketData* data);
int findFreeSlot(Player *plr); int findFreeSlot(Player *plr);
bool isItemRegistered(int32_t id, int32_t type);
Item getItemData(int32_t id, int32_t type);
} }

View File

@ -31,10 +31,17 @@ void NPCManager::npcVendorBuy(CNSocket* sock, CNPacketData* data) {
sP_CL2FE_REQ_PC_VENDOR_ITEM_BUY* req = (sP_CL2FE_REQ_PC_VENDOR_ITEM_BUY*)data->buf; sP_CL2FE_REQ_PC_VENDOR_ITEM_BUY* req = (sP_CL2FE_REQ_PC_VENDOR_ITEM_BUY*)data->buf;
Player *plr = PlayerManager::getPlayer(sock); Player *plr = PlayerManager::getPlayer(sock);
int itemCost = 100; // TODO: placeholder, look up the price of item if (!ItemManager::isItemRegistered(req->Item.iID, req->Item.iType)) {
std::cout << "[WARN] Item id " << req->Item.iID << " with type " << req->Item.iType << " not found" << std::endl;
// NOTE: VENDOR_ITEM_BUY_FAIL is not actually handled client-side.
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_BUY_FAIL, failResp);
failResp.iErrorCode = 0;
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_VENDOR_ITEM_BUY_FAIL, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_BUY_FAIL));
return;
}
int itemCost = ItemManager::getItemData(req->Item.iID, req->Item.iType).buyPrice;
int slot = ItemManager::findFreeSlot(plr); int slot = ItemManager::findFreeSlot(plr);
if (itemCost > plr->money || slot == -1) { if (itemCost > plr->money || slot == -1) {
// NOTE: VENDOR_ITEM_BUY_FAIL is not actually handled client-side. // NOTE: VENDOR_ITEM_BUY_FAIL is not actually handled client-side.
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_BUY_FAIL, failResp); INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_BUY_FAIL, failResp);
@ -79,7 +86,7 @@ void NPCManager::npcVendorSell(CNSocket* sock, CNPacketData* data) {
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_SELL_SUCC, resp); INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_SELL_SUCC, resp);
int sellValue = 100 * req->iItemCnt; // TODO: lookup item price int sellValue = ItemManager::getItemData(item->iID, item->iType).sellPrice * req->iItemCnt; // TODO: lookup item price
// increment taros // increment taros
plr->money = plr->money + sellValue; plr->money = plr->money + sellValue;
@ -124,7 +131,7 @@ void NPCManager::npcVendorTable(CNSocket* sock, CNPacketData* data) {
vItem.item = base; vItem.item = base;
vItem.iSortNum = listings[i].sort; vItem.iSortNum = listings[i].sort;
vItem.iVendorID = req->iVendorID; vItem.iVendorID = req->iVendorID;
vItem.fBuyCost = listings[i].price; //vItem.fBuyCost = listings[i].price;
resp.item[i] = vItem; resp.item[i] = vItem;
} }

View File

@ -121,11 +121,25 @@ void TableData::init() {
std::cout << "[INFO] Loaded mission-related data" << std::endl; std::cout << "[INFO] Loaded mission-related data" << std::endl;
// load all item data. i'm sorry. it has to be done
const char* setNames[10] = { "m_pBackItemTable", "m_pFaceItemTable", "m_pGlassItemTable", "m_pHatItemTable",
"m_pHeadItemTable", "m_pPantsItemTable", "m_pShirtsItemTable", "m_pShoesItemTable", "m_pWeaponItemTable",
"m_pVehicleItemTable"};
nlohmann::json itemSet;
for (int i = 0; i < 10; i++) {
itemSet = xdtData[setNames[i]]["m_pItemData"];
for (nlohmann::json::iterator item = itemSet.begin(); item != itemSet.end(); item++)
ItemManager::ItemData[std::pair<int32_t, int32_t>(item.value()["m_iItemNumber"], item.value()["m_iEquipLoc"])]
= { item.value()["m_iTradeAble"] == 1, item.value()["m_iSellAble"] == 1, item.value()["m_iItemPrice"], item.value()["m_iItemSellPrice"], item.value()["m_iStackNumber"], item.value()["m_iMinReqLev"] };
}
std::cout << "[INFO] Loaded " << ItemManager::ItemData.size() << " items" << std::endl;
// load vendor listings // load vendor listings
nlohmann::json listings = xdtData["m_pVendorTable"]["m_pItemData"]; nlohmann::json listings = xdtData["m_pVendorTable"]["m_pItemData"];
for (nlohmann::json::iterator listing = listings.begin(); listing != listings.end(); listing++) { for (nlohmann::json::iterator listing = listings.begin(); listing != listings.end(); listing++) {
VendorListing vListing = {listing.value()["m_iSortNumber"], listing.value()["m_iItemType"], listing.value()["m_iitemID"], listing.value()["m_iSellCost"]}; VendorListing vListing = { listing.value()["m_iSortNumber"], listing.value()["m_iItemType"], listing.value()["m_iitemID"] };
ItemManager::VendorTables[listing.value()["m_iNpcNumber"]].push_back(vListing); ItemManager::VendorTables[listing.value()["m_iNpcNumber"]].push_back(vListing);
} }