refactor: Vendor inventory loading (#1163)

* refactor: Vendor inventory loading
Implement proper delta compression
dynamically determine multicostitems and standard cost items
Quatantine max's custom code

* address feedback

* fix newline

* oops

* remove header

* fix default and const for

* he said make it a reference too, not just const
This commit is contained in:
Aaron Kimbrell 2023-08-03 05:51:52 -05:00 committed by GitHub
parent 0d48cfe8c0
commit c2b4aa4026
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 142 additions and 165 deletions

View File

@ -22,7 +22,7 @@ CDVendorComponentTable::CDVendorComponentTable(void) {
while (!tableData.eof()) { while (!tableData.eof()) {
CDVendorComponent entry; CDVendorComponent entry;
entry.id = tableData.getIntField("id", -1); entry.id = tableData.getIntField("id", -1);
entry.buyScalar = tableData.getFloatField("buyScalar", -1.0f); entry.buyScalar = tableData.getFloatField("buyScalar", 0.0f);
entry.sellScalar = tableData.getFloatField("sellScalar", -1.0f); entry.sellScalar = tableData.getFloatField("sellScalar", -1.0f);
entry.refreshTimeSeconds = tableData.getFloatField("refreshTimeSeconds", -1.0f); entry.refreshTimeSeconds = tableData.getFloatField("refreshTimeSeconds", -1.0f);
entry.LootMatrixIndex = tableData.getIntField("LootMatrixIndex", -1); entry.LootMatrixIndex = tableData.getIntField("LootMatrixIndex", -1);

View File

@ -1,26 +1,28 @@
#include "VendorComponent.h" #include "VendorComponent.h"
#include "BitStream.h"
#include <BitStream.h>
#include "Game.h"
#include "dServer.h" #include "dServer.h"
#include "dZoneManager.h"
#include "WorldConfig.h"
#include "CDComponentsRegistryTable.h" #include "CDComponentsRegistryTable.h"
#include "CDVendorComponentTable.h" #include "CDVendorComponentTable.h"
#include "CDLootMatrixTable.h" #include "CDLootMatrixTable.h"
#include "CDLootTableTable.h" #include "CDLootTableTable.h"
#include "CDItemComponentTable.h"
VendorComponent::VendorComponent(Entity* parent) : Component(parent) { VendorComponent::VendorComponent(Entity* parent) : Component(parent) {
m_HasStandardCostItems = false;
m_HasMultiCostItems = false;
SetupConstants(); SetupConstants();
RefreshInventory(true); RefreshInventory(true);
} }
VendorComponent::~VendorComponent() = default;
void VendorComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) { void VendorComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
outBitStream->Write1(); outBitStream->Write(bIsInitialUpdate || m_DirtyVendor);
outBitStream->Write1(); // Has standard items (Required for vendors with missions.) if (bIsInitialUpdate || m_DirtyVendor) {
outBitStream->Write(HasCraftingStation()); // Has multi use items outBitStream->Write(m_HasStandardCostItems);
outBitStream->Write(m_HasMultiCostItems);
if (!bIsInitialUpdate) m_DirtyVendor = false;
}
} }
void VendorComponent::OnUse(Entity* originator) { void VendorComponent::OnUse(Entity* originator) {
@ -28,97 +30,74 @@ void VendorComponent::OnUse(Entity* originator) {
GameMessages::SendVendorStatusUpdate(m_Parent, originator->GetSystemAddress()); GameMessages::SendVendorStatusUpdate(m_Parent, originator->GetSystemAddress());
} }
float VendorComponent::GetBuyScalar() const {
return m_BuyScalar;
}
float VendorComponent::GetSellScalar() const {
return m_SellScalar;
}
void VendorComponent::SetBuyScalar(float value) {
m_BuyScalar = value;
}
void VendorComponent::SetSellScalar(float value) {
m_SellScalar = value;
}
std::map<LOT, int>& VendorComponent::GetInventory() {
return m_Inventory;
}
bool VendorComponent::HasCraftingStation() {
// As far as we know, only Umami has a crafting station
return m_Parent->GetLOT() == 13800;
}
void VendorComponent::RefreshInventory(bool isCreation) { void VendorComponent::RefreshInventory(bool isCreation) {
//Custom code for Max vanity NPC SetHasStandardCostItems(false);
if (m_Parent->GetLOT() == 9749 && Game::server->GetZoneID() == 1201) { SetHasMultiCostItems(false);
if (!isCreation) return; m_Inventory.clear();
m_Inventory.insert({ 11909, 0 }); //Top hat w frog
m_Inventory.insert({ 7785, 0 }); //Flash bulb // Custom code for Max vanity NPC and Mr.Ree cameras
m_Inventory.insert({ 12764, 0 }); //Big fountain soda if(isCreation && m_Parent->GetLOT() == 9749 && Game::server->GetZoneID() == 1201) {
m_Inventory.insert({ 12241, 0 }); //Hot cocoa (from fb) SetupMaxCustomVendor();
return; return;
} }
m_Inventory.clear();
auto* lootMatrixTable = CDClientManager::Instance().GetTable<CDLootMatrixTable>(); auto* lootMatrixTable = CDClientManager::Instance().GetTable<CDLootMatrixTable>();
std::vector<CDLootMatrix> lootMatrices = lootMatrixTable->Query([=](CDLootMatrix entry) { return (entry.LootMatrixIndex == m_LootMatrixID); }); const auto lootMatrices = lootMatrixTable->Query([=](CDLootMatrix entry) { return (entry.LootMatrixIndex == m_LootMatrixID); });
if (lootMatrices.empty()) return; if (lootMatrices.empty()) return;
// Done with lootMatrix table
auto* lootTableTable = CDClientManager::Instance().GetTable<CDLootTableTable>(); auto* lootTableTable = CDClientManager::Instance().GetTable<CDLootTableTable>();
auto* itemComponentTable = CDClientManager::Instance().GetTable<CDItemComponentTable>();
auto* compRegistryTable = CDClientManager::Instance().GetTable<CDComponentsRegistryTable>();
for (const auto& lootMatrix : lootMatrices) { for (const auto& lootMatrix : lootMatrices) {
int lootTableID = lootMatrix.LootTableIndex; int lootTableID = lootMatrix.LootTableIndex;
std::vector<CDLootTable> vendorItems = lootTableTable->Query([=](CDLootTable entry) { return (entry.LootTableIndex == lootTableID); }); auto vendorItems = lootTableTable->Query([=](CDLootTable entry) { return (entry.LootTableIndex == lootTableID); });
if (lootMatrix.maxToDrop == 0 || lootMatrix.minToDrop == 0) { if (lootMatrix.maxToDrop == 0 || lootMatrix.minToDrop == 0) {
for (CDLootTable item : vendorItems) { for (const auto& item : vendorItems) {
m_Inventory.insert({ item.itemid, item.sortPriority }); if (!m_HasStandardCostItems || !m_HasMultiCostItems) {
auto itemComponentID = compRegistryTable->GetByIDAndType(item.itemid, eReplicaComponentType::ITEM, -1);
if (itemComponentID == -1) {
Game::logger->Log("VendorComponent", "Attempted to add item %i with ItemComponent ID -1 to vendor %i inventory. Not adding item!", itemComponentID, m_Parent->GetLOT());
continue;
}
auto itemComponent = itemComponentTable->GetItemComponentByID(itemComponentID);
if (!m_HasStandardCostItems && itemComponent.baseValue != -1) SetHasStandardCostItems(true);
if (!m_HasMultiCostItems && !itemComponent.currencyCosts.empty()) SetHasMultiCostItems(true);
}
m_Inventory.push_back(SoldItem(item.itemid, item.sortPriority));
} }
} else { } else {
auto randomCount = GeneralUtils::GenerateRandomNumber<int32_t>(lootMatrix.minToDrop, lootMatrix.maxToDrop); auto randomCount = GeneralUtils::GenerateRandomNumber<int32_t>(lootMatrix.minToDrop, lootMatrix.maxToDrop);
for (size_t i = 0; i < randomCount; i++) { for (size_t i = 0; i < randomCount; i++) {
if (vendorItems.empty()) break; if (vendorItems.empty()) break;
auto randomItemIndex = GeneralUtils::GenerateRandomNumber<int32_t>(0, vendorItems.size() - 1); auto randomItemIndex = GeneralUtils::GenerateRandomNumber<int32_t>(0, vendorItems.size() - 1);
const auto& randomItem = vendorItems.at(randomItemIndex);
const auto& randomItem = vendorItems[randomItemIndex];
vendorItems.erase(vendorItems.begin() + randomItemIndex); vendorItems.erase(vendorItems.begin() + randomItemIndex);
if (!m_HasStandardCostItems || !m_HasMultiCostItems) {
m_Inventory.insert({ randomItem.itemid, randomItem.sortPriority }); auto itemComponentID = compRegistryTable->GetByIDAndType(randomItem.itemid, eReplicaComponentType::ITEM, -1);
if (itemComponentID == -1) {
Game::logger->Log("VendorComponent", "Attempted to add item %i with ItemComponent ID -1 to vendor %i inventory. Not adding item!", itemComponentID, m_Parent->GetLOT());
continue;
}
auto itemComponent = itemComponentTable->GetItemComponentByID(itemComponentID);
if (!m_HasStandardCostItems && itemComponent.baseValue != -1) SetHasStandardCostItems(true);
if (!m_HasMultiCostItems && !itemComponent.currencyCosts.empty()) SetHasMultiCostItems(true);
}
m_Inventory.push_back(SoldItem(randomItem.itemid, randomItem.sortPriority));
} }
} }
} }
HandleMrReeCameras();
//Because I want a vendor to sell these cameras
if (m_Parent->GetLOT() == 13569) {
auto randomCamera = GeneralUtils::GenerateRandomNumber<int32_t>(0, 2);
switch (randomCamera) {
case 0:
m_Inventory.insert({ 16253, 0 }); //Grungagroid
break;
case 1:
m_Inventory.insert({ 16254, 0 }); //Hipstabrick
break;
case 2:
m_Inventory.insert({ 16204, 0 }); //Megabrixel snapshot
break;
default:
break;
}
}
// Callback timer to refresh this inventory. // Callback timer to refresh this inventory.
if (m_RefreshTimeSeconds > 0.0) {
m_Parent->AddCallbackTimer(m_RefreshTimeSeconds, [this]() { m_Parent->AddCallbackTimer(m_RefreshTimeSeconds, [this]() {
RefreshInventory(); RefreshInventory();
}); });
}
Game::entityManager->SerializeEntity(m_Parent);
GameMessages::SendVendorStatusUpdate(m_Parent, UNASSIGNED_SYSTEM_ADDRESS); GameMessages::SendVendorStatusUpdate(m_Parent, UNASSIGNED_SYSTEM_ADDRESS);
} }
@ -129,12 +108,47 @@ void VendorComponent::SetupConstants() {
auto* vendorComponentTable = CDClientManager::Instance().GetTable<CDVendorComponentTable>(); auto* vendorComponentTable = CDClientManager::Instance().GetTable<CDVendorComponentTable>();
std::vector<CDVendorComponent> vendorComps = vendorComponentTable->Query([=](CDVendorComponent entry) { return (entry.id == componentID); }); std::vector<CDVendorComponent> vendorComps = vendorComponentTable->Query([=](CDVendorComponent entry) { return (entry.id == componentID); });
if (vendorComps.empty()) return; if (vendorComps.empty()) return;
m_BuyScalar = vendorComps[0].buyScalar; auto vendorData = vendorComps.at(0);
m_SellScalar = vendorComps[0].sellScalar; if (vendorData.buyScalar == 0.0) m_BuyScalar = Game::zoneManager->GetWorldConfig()->vendorBuyMultiplier;
m_RefreshTimeSeconds = vendorComps[0].refreshTimeSeconds; else m_BuyScalar = vendorData.buyScalar;
m_LootMatrixID = vendorComps[0].LootMatrixIndex; m_SellScalar = vendorData.sellScalar;
m_RefreshTimeSeconds = vendorData.refreshTimeSeconds;
m_LootMatrixID = vendorData.LootMatrixIndex;
} }
bool VendorComponent::SellsItem(const LOT item) const { bool VendorComponent::SellsItem(const LOT item) const {
return m_Inventory.find(item) != m_Inventory.end(); return std::count_if(m_Inventory.begin(), m_Inventory.end(), [item](const SoldItem& lhs) {
return lhs.lot == item;
}) > 0;
}
void VendorComponent::SetupMaxCustomVendor(){
SetHasStandardCostItems(true);
m_Inventory.push_back(SoldItem(11909, 0)); // Top hat w frog
m_Inventory.push_back(SoldItem(7785, 0)); // Flash bulb
m_Inventory.push_back(SoldItem(12764, 0)); // Big fountain soda
m_Inventory.push_back(SoldItem(12241, 0)); // Hot cocoa (from fb)
}
void VendorComponent::HandleMrReeCameras(){
if (m_Parent->GetLOT() == 13569) {
SetHasStandardCostItems(true);
auto randomCamera = GeneralUtils::GenerateRandomNumber<int32_t>(0, 2);
LOT camera = 0;
DluAssert(randomCamera >= 0 && randomCamera <= 2);
switch (randomCamera) {
case 0:
camera = 16253; // Grungagroid
break;
case 1:
camera = 16254; // Hipstabrick
break;
case 2:
camera = 16204; // Megabrixel snapshot
break;
}
m_Inventory.push_back(SoldItem(camera, 0));
}
} }

View File

@ -9,91 +9,54 @@
#include "RakNetTypes.h" #include "RakNetTypes.h"
#include "eReplicaComponentType.h" #include "eReplicaComponentType.h"
/** struct SoldItem {
* A component for vendor NPCs. A vendor sells items to the player. SoldItem(const LOT lot, const int32_t sortPriority) {
*/ this->lot = lot;
this->sortPriority = sortPriority;
};
LOT lot = 0;
int32_t sortPriority = 0;
};
class VendorComponent : public Component { class VendorComponent : public Component {
public: public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::VENDOR; inline static const eReplicaComponentType ComponentType = eReplicaComponentType::VENDOR;
VendorComponent(Entity* parent); VendorComponent(Entity* parent);
~VendorComponent() override;
void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags); void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags);
void OnUse(Entity* originator) override; void OnUse(Entity* originator) override;
/**
* Gets the buy scaler
* @return the buy scaler
*/
float GetBuyScalar() const;
/**
* Sets the buy scalar.
* @param value the new value.
*/
void SetBuyScalar(float value);
/**
* Gets the buy scaler
* @return the buy scaler
*/
float GetSellScalar() const;
/**
* Sets the sell scalar.
* @param value the new value.
*/
void SetSellScalar(float value);
/**
* True if the NPC LOT is 13800, the only NPC with a crafting station.
*/
bool HasCraftingStation();
/**
* Gets the list if items the vendor sells.
* @return the list of items.
*/
std::map<LOT, int>& GetInventory();
/**
* Refresh the inventory of this vendor.
*/
void RefreshInventory(bool isCreation = false); void RefreshInventory(bool isCreation = false);
/**
* Called on startup of vendor to setup the variables for the component.
*/
void SetupConstants(); void SetupConstants();
bool SellsItem(const LOT item) const; bool SellsItem(const LOT item) const;
float GetBuyScalar() const { return m_BuyScalar; }
float GetSellScalar() const { return m_SellScalar; }
void SetBuyScalar(const float value) { m_BuyScalar = value; }
void SetSellScalar(const float value) { m_SellScalar = value; }
const std::vector<SoldItem>& GetInventory() { return m_Inventory; }
void SetHasMultiCostItems(const bool hasMultiCostItems) {
if (m_HasMultiCostItems == hasMultiCostItems) return;
m_HasMultiCostItems = hasMultiCostItems;
m_DirtyVendor = true;
}
void SetHasStandardCostItems(const bool hasStandardCostItems) {
if (m_HasStandardCostItems == hasStandardCostItems) return;
m_HasStandardCostItems = hasStandardCostItems;
m_DirtyVendor = true;
}
private: private:
/** void SetupMaxCustomVendor();
* The buy scalar. void HandleMrReeCameras();
*/ float m_BuyScalar = 0.0f;
float m_BuyScalar; float m_SellScalar = 0.0f;
float m_RefreshTimeSeconds = 0.0f;
/** uint32_t m_LootMatrixID = 0;
* The sell scalar. std::vector<SoldItem> m_Inventory;
*/ bool m_DirtyVendor = false;
float m_SellScalar; bool m_HasStandardCostItems = false;
bool m_HasMultiCostItems = false;
/**
* The refresh time of this vendors' inventory.
*/
float m_RefreshTimeSeconds;
/**
* Loot matrix id of this vendor.
*/
uint32_t m_LootMatrixID;
/**
* The list of items the vendor sells.
*/
std::map<LOT, int> m_Inventory;
}; };
#endif // VENDORCOMPONENT_H #endif // VENDORCOMPONENT_H

View File

@ -1286,20 +1286,20 @@ void GameMessages::SendVendorStatusUpdate(Entity* entity, const SystemAddress& s
VendorComponent* vendor = static_cast<VendorComponent*>(entity->GetComponent(eReplicaComponentType::VENDOR)); VendorComponent* vendor = static_cast<VendorComponent*>(entity->GetComponent(eReplicaComponentType::VENDOR));
if (!vendor) return; if (!vendor) return;
std::map<LOT, int> vendorItems = vendor->GetInventory(); auto vendorItems = vendor->GetInventory();
bitStream.Write(entity->GetObjectID()); bitStream.Write(entity->GetObjectID());
bitStream.Write(eGameMessageType::VENDOR_STATUS_UPDATE); bitStream.Write(eGameMessageType::VENDOR_STATUS_UPDATE);
bitStream.Write(bUpdateOnly); bitStream.Write(bUpdateOnly);
bitStream.Write(static_cast<uint32_t>(vendorItems.size())); bitStream.Write<uint32_t>(vendorItems.size());
for (std::pair<LOT, int> item : vendorItems) { for (const auto& item : vendorItems) {
bitStream.Write(static_cast<int>(item.first)); bitStream.Write(item.lot);
bitStream.Write(static_cast<int>(item.second)); bitStream.Write(item.sortPriority);
} }
if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST;
SEND_PACKET; SEND_PACKET;
} }

View File

@ -277,7 +277,7 @@ void dZoneManager::LoadWorldConfig() {
m_WorldConfig->characterMaxSlope = worldConfig.getFloatField("character_max_slope"); m_WorldConfig->characterMaxSlope = worldConfig.getFloatField("character_max_slope");
m_WorldConfig->defaultRespawnTime = worldConfig.getFloatField("defaultrespawntime"); m_WorldConfig->defaultRespawnTime = worldConfig.getFloatField("defaultrespawntime");
m_WorldConfig->missionTooltipTimeout = worldConfig.getFloatField("mission_tooltip_timeout"); m_WorldConfig->missionTooltipTimeout = worldConfig.getFloatField("mission_tooltip_timeout");
m_WorldConfig->vendorBuyMultiplier = worldConfig.getFloatField("vendor_buy_multiplier"); m_WorldConfig->vendorBuyMultiplier = worldConfig.getFloatField("vendor_buy_multiplier", 0.1);
m_WorldConfig->petFollowRadius = worldConfig.getFloatField("pet_follow_radius"); m_WorldConfig->petFollowRadius = worldConfig.getFloatField("pet_follow_radius");
m_WorldConfig->characterEyeHeight = worldConfig.getFloatField("character_eye_height"); m_WorldConfig->characterEyeHeight = worldConfig.getFloatField("character_eye_height");
m_WorldConfig->flightVerticalVelocity = worldConfig.getFloatField("flight_vertical_velocity"); m_WorldConfig->flightVerticalVelocity = worldConfig.getFloatField("flight_vertical_velocity");