mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-16 00:35:27 +00:00
refactor(component): vendors share the client's base vendor component
LWOBaseVendorComponent is the base of the client's vendor, donation vendor and achievement vendor; DLU's vendor logic becomes BaseVendorComponent with VendorComponent, DonationVendorComponent and AchievementVendorComponent all built on it (the last two derived from VendorComponent before). Nothing sent changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -46,7 +46,7 @@ set(DGAME_DCOMPONENTS_SOURCES
|
||||
"SwitchComponent.cpp"
|
||||
"TriggerComponent.cpp"
|
||||
"Physics/HavokVehiclePhysicsComponent.cpp"
|
||||
"Vendor/VendorComponent.cpp"
|
||||
"Vendor/BaseVendorComponent.cpp"
|
||||
"ScriptComponent.cpp"
|
||||
)
|
||||
|
||||
|
||||
@@ -9,12 +9,12 @@
|
||||
#include "UserManager.h"
|
||||
#include "CDMissionsTable.h"
|
||||
|
||||
AchievementVendorComponent::AchievementVendorComponent(Entity* parent, const int32_t componentID) : VendorComponent(parent, componentID) {
|
||||
AchievementVendorComponent::AchievementVendorComponent(Entity* parent, const int32_t componentID) : BaseVendorComponent(parent, componentID) {
|
||||
}
|
||||
|
||||
// The base vendor's template load, then the achievement vendor's stock
|
||||
void AchievementVendorComponent::LoadDataFromTemplate() {
|
||||
VendorComponent::LoadDataFromTemplate();
|
||||
BaseVendorComponent::LoadDataFromTemplate();
|
||||
RefreshInventory(true);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
#ifndef __ACHIEVEMENTVENDORCOMPONENT__H__
|
||||
#define __ACHIEVEMENTVENDORCOMPONENT__H__
|
||||
|
||||
#include "VendorComponent.h"
|
||||
#include "BaseVendorComponent.h"
|
||||
#include "eReplicaComponentType.h"
|
||||
#include <set>
|
||||
#include <map>
|
||||
|
||||
class Entity;
|
||||
|
||||
class AchievementVendorComponent final : public VendorComponent {
|
||||
class AchievementVendorComponent final : public BaseVendorComponent {
|
||||
public:
|
||||
static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::ACHIEVEMENT_VENDOR;
|
||||
AchievementVendorComponent(Entity* parent, const int32_t componentID);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "VendorComponent.h"
|
||||
#include "BaseVendorComponent.h"
|
||||
#include "ObjectMessages.h"
|
||||
#include "BitStream.h"
|
||||
#include "dServer.h"
|
||||
@@ -21,19 +21,19 @@
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
|
||||
VendorComponent::VendorComponent(Entity* parent, const int32_t componentID) : Component(parent, componentID) {
|
||||
RegisterMsg(&VendorComponent::OnRequestUse);
|
||||
BaseVendorComponent::BaseVendorComponent(Entity* parent, const int32_t componentID) : Component(parent, componentID) {
|
||||
RegisterMsg(&BaseVendorComponent::OnRequestUse);
|
||||
m_HasStandardCostItems = false;
|
||||
m_HasMultiCostItems = false;
|
||||
}
|
||||
|
||||
// The vendor's constants and its stock (the base's refresh: a derived vendor adds its own after)
|
||||
void VendorComponent::LoadDataFromTemplate() {
|
||||
void BaseVendorComponent::LoadDataFromTemplate() {
|
||||
SetupConstants();
|
||||
VendorComponent::RefreshInventory(true);
|
||||
BaseVendorComponent::RefreshInventory(true);
|
||||
}
|
||||
|
||||
void VendorComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
|
||||
void BaseVendorComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
|
||||
outBitStream.Write(bIsInitialUpdate || m_DirtyVendor);
|
||||
if (bIsInitialUpdate || m_DirtyVendor) {
|
||||
outBitStream.Write(m_HasStandardCostItems);
|
||||
@@ -42,7 +42,7 @@ void VendorComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitial
|
||||
}
|
||||
}
|
||||
|
||||
bool VendorComponent::OnRequestUse(GameMessages::RequestUseEvent& event) {
|
||||
bool BaseVendorComponent::OnRequestUse(GameMessages::RequestUseEvent& event) {
|
||||
if (event.msg.IsMissionChoice()) return false;
|
||||
auto* const originator = event.user;
|
||||
GameMessages::VendorOpenWindow openWindow;
|
||||
@@ -50,12 +50,12 @@ bool VendorComponent::OnRequestUse(GameMessages::RequestUseEvent& event) {
|
||||
openWindow.SendToClient(originator->GetSystemAddress());
|
||||
|
||||
// Only the entity's VENDOR component sends its stock (achievement and donation vendors never did).
|
||||
auto* vendor = static_cast<VendorComponent*>(m_Parent->GetComponent(eReplicaComponentType::VENDOR));
|
||||
auto* vendor = static_cast<BaseVendorComponent*>(m_Parent->GetComponent(eReplicaComponentType::VENDOR));
|
||||
if (vendor) vendor->SendStatusUpdate(originator->GetSystemAddress());
|
||||
return true;
|
||||
}
|
||||
|
||||
void VendorComponent::RefreshInventory(bool isCreation) {
|
||||
void BaseVendorComponent::RefreshInventory(bool isCreation) {
|
||||
SetHasStandardCostItems(false);
|
||||
SetHasMultiCostItems(false);
|
||||
m_Inventory.clear();
|
||||
@@ -111,11 +111,11 @@ void VendorComponent::RefreshInventory(bool isCreation) {
|
||||
}
|
||||
Game::entityManager->SerializeEntity(m_Parent);
|
||||
// Only the entity's VENDOR component sends its stock (achievement and donation vendors never did).
|
||||
auto* vendor = static_cast<VendorComponent*>(m_Parent->GetComponent(eReplicaComponentType::VENDOR));
|
||||
auto* vendor = static_cast<BaseVendorComponent*>(m_Parent->GetComponent(eReplicaComponentType::VENDOR));
|
||||
if (vendor) vendor->SendStatusUpdate(UNASSIGNED_SYSTEM_ADDRESS);
|
||||
}
|
||||
|
||||
void VendorComponent::SetupConstants() {
|
||||
void BaseVendorComponent::SetupConstants() {
|
||||
auto* compRegistryTable = CDClientManager::GetTable<CDComponentsRegistryTable>();
|
||||
int componentID = compRegistryTable->GetByIDAndType(m_Parent->GetLOT(), eReplicaComponentType::VENDOR);
|
||||
|
||||
@@ -130,13 +130,13 @@ void VendorComponent::SetupConstants() {
|
||||
m_LootMatrixID = vendorData.LootMatrixIndex;
|
||||
}
|
||||
|
||||
bool VendorComponent::SellsItem(const LOT item) const {
|
||||
bool BaseVendorComponent::SellsItem(const LOT item) const {
|
||||
return std::count_if(m_Inventory.begin(), m_Inventory.end(), [item](const SoldItem& lhs) {
|
||||
return lhs.lot == item;
|
||||
}) > 0;
|
||||
}
|
||||
|
||||
void VendorComponent::HandleMrReeCameras(){
|
||||
void BaseVendorComponent::HandleMrReeCameras(){
|
||||
if (m_Parent->GetLOT() == 13569) {
|
||||
SetHasStandardCostItems(true);
|
||||
auto randomCamera = GeneralUtils::GenerateRandomNumber<int32_t>(0, 2);
|
||||
@@ -159,7 +159,7 @@ void VendorComponent::HandleMrReeCameras(){
|
||||
}
|
||||
|
||||
|
||||
void VendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) {
|
||||
void BaseVendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) {
|
||||
|
||||
if (!SellsItem(lot)) {
|
||||
auto* user = UserManager::Instance()->GetUser(buyer->GetSystemAddress());
|
||||
@@ -225,7 +225,7 @@ void VendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) {
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_SUCCESS);
|
||||
}
|
||||
|
||||
bool VendorComponent::SetupItem(LOT item) {
|
||||
bool BaseVendorComponent::SetupItem(LOT item) {
|
||||
|
||||
auto* itemComponentTable = CDClientManager::GetTable<CDItemComponentTable>();
|
||||
auto* compRegistryTable = CDClientManager::GetTable<CDComponentsRegistryTable>();
|
||||
@@ -246,7 +246,7 @@ bool VendorComponent::SetupItem(LOT item) {
|
||||
}
|
||||
|
||||
|
||||
void VendorComponent::SendStatusUpdate(const SystemAddress& sysAddr, bool bUpdateOnly) {
|
||||
void BaseVendorComponent::SendStatusUpdate(const SystemAddress& sysAddr, bool bUpdateOnly) {
|
||||
GameMessages::VendorStatusUpdate statusUpdate;
|
||||
statusUpdate.target = m_Parent->GetObjectID();
|
||||
statusUpdate.bUpdateOnly = bUpdateOnly;
|
||||
@@ -255,14 +255,14 @@ void VendorComponent::SendStatusUpdate(const SystemAddress& sysAddr, bool bUpdat
|
||||
statusUpdate.Send(sysAddr);
|
||||
}
|
||||
|
||||
void VendorComponent::SendTransactionResult(LWOOBJID target, const SystemAddress& sysAddr, eVendorTransactionResult result) {
|
||||
void BaseVendorComponent::SendTransactionResult(LWOOBJID target, const SystemAddress& sysAddr, eVendorTransactionResult result) {
|
||||
GameMessages::VendorTransactionResult transactionResult;
|
||||
transactionResult.target = target;
|
||||
transactionResult.iResult = result;
|
||||
transactionResult.SendToClient(sysAddr);
|
||||
}
|
||||
|
||||
void VendorComponent::SellToVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count) {
|
||||
void BaseVendorComponent::SellToVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count) {
|
||||
Character* character = player.GetCharacter();
|
||||
if (!character) return;
|
||||
InventoryComponent* inv = static_cast<InventoryComponent*>(player.GetComponent(eReplicaComponentType::INVENTORY));
|
||||
@@ -295,7 +295,7 @@ void VendorComponent::SellToVendor(Entity& player, const SystemAddress& sysAddr,
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::SELL_SUCCESS);
|
||||
}
|
||||
|
||||
void VendorComponent::BuybackFromVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count) {
|
||||
void BaseVendorComponent::BuybackFromVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count) {
|
||||
Character* character = player.GetCharacter();
|
||||
if (!character) return;
|
||||
InventoryComponent* inv = static_cast<InventoryComponent*>(player.GetComponent(eReplicaComponentType::INVENTORY));
|
||||
@@ -334,7 +334,7 @@ void VendorComponent::BuybackFromVendor(Entity& player, const SystemAddress& sys
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_SUCCESS);
|
||||
}
|
||||
|
||||
void VendorComponent::MakeRoomInBuyback(InventoryComponent& inventory, const Item& sold, const uint32_t count) {
|
||||
void BaseVendorComponent::MakeRoomInBuyback(InventoryComponent& inventory, const Item& sold, const uint32_t count) {
|
||||
auto* const buyback = inventory.GetInventory(eInventoryType::VENDOR_BUYBACK);
|
||||
if (!buyback) return;
|
||||
|
||||
91
dGame/dComponents/Vendor/BaseVendorComponent.h
vendored
Normal file
91
dGame/dComponents/Vendor/BaseVendorComponent.h
vendored
Normal file
@@ -0,0 +1,91 @@
|
||||
#pragma once
|
||||
#ifndef BASEVENDORCOMPONENT_H
|
||||
#define BASEVENDORCOMPONENT_H
|
||||
|
||||
#include "CDClientManager.h"
|
||||
#include "Component.h"
|
||||
#include "Entity.h"
|
||||
#include "GameMessages.h"
|
||||
#include "RakNetTypes.h"
|
||||
#include "eReplicaComponentType.h"
|
||||
|
||||
enum class eVendorTransactionResult : uint32_t;
|
||||
class InventoryComponent;
|
||||
class Item;
|
||||
|
||||
struct SoldItem {
|
||||
SoldItem(const LOT lot, const int32_t sortPriority) {
|
||||
this->lot = lot;
|
||||
this->sortPriority = sortPriority;
|
||||
};
|
||||
LOT lot = 0;
|
||||
int32_t sortPriority = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* The base of the vendors, as the client's LWOBaseVendorComponent: vendor (16), donation vendor (100) and achievement
|
||||
* vendor (102) are built on it.
|
||||
*/
|
||||
class BaseVendorComponent : public Component {
|
||||
public:
|
||||
BaseVendorComponent(Entity* parent, const int32_t componentID);
|
||||
|
||||
void LoadDataFromTemplate() override;
|
||||
|
||||
void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) override;
|
||||
|
||||
bool OnRequestUse(GameMessages::RequestUseEvent& event);
|
||||
virtual void RefreshInventory(bool isCreation = false);
|
||||
void SetupConstants();
|
||||
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;
|
||||
}
|
||||
|
||||
void Buy(Entity* buyer, LOT lot, uint32_t count);
|
||||
|
||||
// SellToVendor / BuybackFromVendor from player (sent from sysAddr), for the item with object ID itemObjID.
|
||||
void SellToVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count);
|
||||
void BuybackFromVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count);
|
||||
|
||||
// Sends this vendor's stock (VendorStatusUpdate) to sysAddr; UNASSIGNED_SYSTEM_ADDRESS broadcasts.
|
||||
void SendStatusUpdate(const SystemAddress& sysAddr, bool bUpdateOnly = false);
|
||||
|
||||
// Sends a VendorTransactionResult targeting target to sysAddr only.
|
||||
static void SendTransactionResult(LWOOBJID target, const SystemAddress& sysAddr, eVendorTransactionResult result);
|
||||
|
||||
// How many items the buyback inventory keeps (the vendor window's buyback page)
|
||||
static constexpr size_t BUYBACK_SIZE = 27;
|
||||
|
||||
// Before count of sold go to the buyback inventory: drops the oldest buyback items while it is full and they need
|
||||
// a slot of their own.
|
||||
static void MakeRoomInBuyback(InventoryComponent& inventory, const Item& sold, uint32_t count);
|
||||
|
||||
private:
|
||||
void HandleMrReeCameras();
|
||||
bool SetupItem(LOT item);
|
||||
float m_BuyScalar = 0.0f;
|
||||
float m_SellScalar = 0.0f;
|
||||
float m_RefreshTimeSeconds = 0.0f;
|
||||
uint32_t m_LootMatrixID = 0;
|
||||
std::vector<SoldItem> m_Inventory;
|
||||
bool m_DirtyVendor = false;
|
||||
bool m_HasStandardCostItems = false;
|
||||
bool m_HasMultiCostItems = false;
|
||||
};
|
||||
|
||||
#endif // BASEVENDORCOMPONENT_H
|
||||
@@ -14,7 +14,7 @@
|
||||
#include "User.h"
|
||||
#include "UserManager.h"
|
||||
|
||||
DonationVendorComponent::DonationVendorComponent(Entity* parent, const int32_t componentID) : VendorComponent(parent, componentID) {
|
||||
DonationVendorComponent::DonationVendorComponent(Entity* parent, const int32_t componentID) : BaseVendorComponent(parent, componentID) {
|
||||
}
|
||||
|
||||
// The level config: the donation goal and activity, and the total donated so far
|
||||
@@ -53,7 +53,7 @@ void DonationVendorComponent::SubmitDonation(uint32_t count) {
|
||||
}
|
||||
|
||||
void DonationVendorComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
|
||||
VendorComponent::Serialize(outBitStream, bIsInitialUpdate);
|
||||
BaseVendorComponent::Serialize(outBitStream, bIsInitialUpdate);
|
||||
outBitStream.Write(bIsInitialUpdate || m_DirtyDonationVendor);
|
||||
if (bIsInitialUpdate || m_DirtyDonationVendor) {
|
||||
outBitStream.Write(m_PercentComplete);
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#ifndef __DONATIONVENDORCOMPONENT__H__
|
||||
#define __DONATIONVENDORCOMPONENT__H__
|
||||
|
||||
#include "VendorComponent.h"
|
||||
#include "BaseVendorComponent.h"
|
||||
#include "eReplicaComponentType.h"
|
||||
|
||||
class Entity;
|
||||
|
||||
class DonationVendorComponent final : public VendorComponent {
|
||||
class DonationVendorComponent final : public BaseVendorComponent {
|
||||
public:
|
||||
static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::DONATION_VENDOR;
|
||||
DonationVendorComponent(Entity* parent, const int32_t componentID);
|
||||
|
||||
84
dGame/dComponents/Vendor/VendorComponent.h
vendored
84
dGame/dComponents/Vendor/VendorComponent.h
vendored
@@ -1,88 +1,16 @@
|
||||
#pragma once
|
||||
#ifndef VENDORCOMPONENT_H
|
||||
#define VENDORCOMPONENT_H
|
||||
|
||||
#include "CDClientManager.h"
|
||||
#include "Component.h"
|
||||
#include "Entity.h"
|
||||
#include "GameMessages.h"
|
||||
#include "RakNetTypes.h"
|
||||
#include "eReplicaComponentType.h"
|
||||
#include "BaseVendorComponent.h"
|
||||
|
||||
enum class eVendorTransactionResult : uint32_t;
|
||||
class InventoryComponent;
|
||||
class Item;
|
||||
|
||||
struct SoldItem {
|
||||
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 {
|
||||
/**
|
||||
* A vendor (16), as the client's LWOVendorComponent: a BaseVendorComponent.
|
||||
*/
|
||||
class VendorComponent final : public BaseVendorComponent {
|
||||
public:
|
||||
static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::VENDOR;
|
||||
VendorComponent(Entity* parent, const int32_t componentID);
|
||||
|
||||
void LoadDataFromTemplate() override;
|
||||
|
||||
void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) override;
|
||||
|
||||
bool OnRequestUse(GameMessages::RequestUseEvent& event);
|
||||
virtual void RefreshInventory(bool isCreation = false);
|
||||
void SetupConstants();
|
||||
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;
|
||||
}
|
||||
|
||||
void Buy(Entity* buyer, LOT lot, uint32_t count);
|
||||
|
||||
// SellToVendor / BuybackFromVendor from player (sent from sysAddr), for the item with object ID itemObjID.
|
||||
void SellToVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count);
|
||||
void BuybackFromVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count);
|
||||
|
||||
// Sends this vendor's stock (VendorStatusUpdate) to sysAddr; UNASSIGNED_SYSTEM_ADDRESS broadcasts.
|
||||
void SendStatusUpdate(const SystemAddress& sysAddr, bool bUpdateOnly = false);
|
||||
|
||||
// Sends a VendorTransactionResult targeting target to sysAddr only.
|
||||
static void SendTransactionResult(LWOOBJID target, const SystemAddress& sysAddr, eVendorTransactionResult result);
|
||||
|
||||
// How many items the buyback inventory keeps (the vendor window's buyback page)
|
||||
static constexpr size_t BUYBACK_SIZE = 27;
|
||||
|
||||
// Before count of sold go to the buyback inventory: drops the oldest buyback items while it is full and they need
|
||||
// a slot of their own.
|
||||
static void MakeRoomInBuyback(InventoryComponent& inventory, const Item& sold, uint32_t count);
|
||||
|
||||
private:
|
||||
void HandleMrReeCameras();
|
||||
bool SetupItem(LOT item);
|
||||
float m_BuyScalar = 0.0f;
|
||||
float m_SellScalar = 0.0f;
|
||||
float m_RefreshTimeSeconds = 0.0f;
|
||||
uint32_t m_LootMatrixID = 0;
|
||||
std::vector<SoldItem> m_Inventory;
|
||||
bool m_DirtyVendor = false;
|
||||
bool m_HasStandardCostItems = false;
|
||||
bool m_HasMultiCostItems = false;
|
||||
VendorComponent(Entity* parent, const int32_t componentID) : BaseVendorComponent(parent, componentID) {}
|
||||
};
|
||||
|
||||
#endif // VENDORCOMPONENT_H
|
||||
|
||||
Reference in New Issue
Block a user