DarkflameServer/dGame/dInventory/Inventory.cpp

320 lines
6.5 KiB
C++
Raw Permalink Normal View History

#include "Inventory.h"
#include "GameMessages.h"
#include "Game.h"
#include "Item.h"
#include "InventoryComponent.h"
#include "eItemType.h"
#include "eReplicaComponentType.h"
#include "CDComponentsRegistryTable.h"
std::vector<LOT> Inventory::m_GameMasterRestrictedItems = {
1727, // GM Only - JetPack
2243, // GM Only - Hammer of Doom
3293, // GM Only - Flamethrower
3735, // GM Only - Large Jetpack
5873, // GM Only - Winged Helm of Speed
6407, // Gm Only - Hat of Pwnage
14442 // The jamesster jetpack
};
2022-07-28 13:39:57 +00:00
Inventory::Inventory(const eInventoryType type, const uint32_t size, const std::vector<Item*>& items, InventoryComponent* component) {
this->type = type;
this->size = size;
this->free = size;
this->component = component;
2022-07-28 13:39:57 +00:00
for (auto* item : items) {
AddManagedItem(item);
}
}
2022-07-28 13:39:57 +00:00
eInventoryType Inventory::GetType() const {
return type;
}
2022-07-28 13:39:57 +00:00
uint32_t Inventory::GetSize() const {
return size;
}
2022-07-28 13:39:57 +00:00
std::map<LWOOBJID, Item*>& Inventory::GetItems() {
return items;
}
2022-07-28 13:39:57 +00:00
std::map<uint32_t, Item*> Inventory::GetSlots() const {
std::map<uint32_t, Item*> slots;
2022-07-28 13:39:57 +00:00
for (const auto& pair : items) {
auto* item = pair.second;
slots.insert_or_assign(item->GetSlot(), item);
}
return slots;
}
2022-07-28 13:39:57 +00:00
InventoryComponent* Inventory::GetComponent() const {
return component;
}
2022-07-28 13:39:57 +00:00
uint32_t Inventory::GetLotCount(const LOT lot) const {
uint32_t count = 0;
2022-07-28 13:39:57 +00:00
for (const auto& pair : items) {
const auto* item = pair.second;
2022-07-28 13:39:57 +00:00
if (item->GetLot() == lot) {
count += item->GetCount();
}
}
return count;
}
2022-07-28 13:39:57 +00:00
void Inventory::SetSize(const uint32_t value) {
free += static_cast<int32_t>(value) - static_cast<int32_t>(size);
size = value;
GameMessages::SendSetInventorySize(component->GetParent(), type, static_cast<int>(size));
}
2022-07-28 13:39:57 +00:00
int32_t Inventory::FindEmptySlot() {
if (free <= 6) // Up from 1
{
2022-07-28 13:39:57 +00:00
if (type != ITEMS && type != VAULT_ITEMS && type != eInventoryType::VAULT_MODELS) {
uint32_t newSize = size;
2022-07-28 13:39:57 +00:00
if (type == MODELS) {
newSize = 240;
2022-07-28 13:39:57 +00:00
} else if (type == eInventoryType::VENDOR_BUYBACK) {
newSize += 9u;
2022-07-28 13:39:57 +00:00
} else {
2022-04-28 21:36:55 +00:00
newSize += 10u;
}
2022-07-28 13:39:57 +00:00
if (newSize > GetSize()) {
SetSize(newSize);
}
}
}
2022-07-28 13:39:57 +00:00
if (free == 0) {
return -1;
}
const auto slots = GetSlots();
2022-07-28 13:39:57 +00:00
for (auto i = 0u; i < size; ++i) {
if (slots.find(i) == slots.end()) {
return i;
}
}
return -1;
}
2022-07-28 13:39:57 +00:00
int32_t Inventory::GetEmptySlots() {
return free;
}
2022-07-28 13:39:57 +00:00
bool Inventory::IsSlotEmpty(int32_t slot) {
const auto slots = GetSlots();
const auto& index = slots.find(slot);
return index == slots.end();
}
2022-07-28 13:39:57 +00:00
Item* Inventory::FindItemById(const LWOOBJID id) const {
const auto& index = items.find(id);
2022-07-28 13:39:57 +00:00
if (index == items.end()) {
return nullptr;
}
return index->second;
}
2022-07-28 13:39:57 +00:00
Item* Inventory::FindItemByLot(const LOT lot, const bool ignoreEquipped, const bool ignoreBound) const {
Item* smallest = nullptr;
2022-07-28 13:39:57 +00:00
for (const auto& pair : items) {
auto* item = pair.second;
2022-07-28 13:39:57 +00:00
if (item->GetLot() != lot) {
continue;
}
2022-07-28 13:39:57 +00:00
if (ignoreEquipped && item->IsEquipped()) {
continue;
}
2022-07-28 13:39:57 +00:00
if (ignoreBound && item->GetBound()) {
continue;
}
2022-07-28 13:39:57 +00:00
if (smallest == nullptr) {
smallest = item;
continue;
}
2022-07-28 13:39:57 +00:00
if (smallest->GetCount() > item->GetCount()) {
smallest = item;
}
}
return smallest;
}
2022-07-28 13:39:57 +00:00
Item* Inventory::FindItemBySlot(const uint32_t slot) const {
const auto slots = GetSlots();
const auto index = slots.find(slot);
2022-07-28 13:39:57 +00:00
if (index == slots.end()) {
return nullptr;
}
return index->second;
}
2022-07-28 13:39:57 +00:00
Item* Inventory::FindItemBySubKey(LWOOBJID id) const {
for (const auto& item : items) {
if (item.second->GetSubKey() == id) {
return item.second;
}
}
return nullptr;
}
2022-07-28 13:39:57 +00:00
void Inventory::AddManagedItem(Item* item) {
const auto id = item->GetId();
2022-07-28 13:39:57 +00:00
if (items.find(id) != items.end()) {
Game::logger->Log("Inventory", "Attempting to add an item with an already present id (%llu)!", id);
return;
}
const auto slots = GetSlots();
const auto slot = item->GetSlot();
2022-07-28 13:39:57 +00:00
if (slots.find(slot) != slots.end()) {
Game::logger->Log("Inventory", "Attempting to add an item with an already present slot (%i)!", slot);
return;
}
items.insert_or_assign(id, item);
free--;
}
2022-07-28 13:39:57 +00:00
void Inventory::RemoveManagedItem(Item* item) {
const auto id = item->GetId();
2022-07-28 13:39:57 +00:00
if (items.find(id) == items.end()) {
Game::logger->Log("Inventory", "Attempting to remove an item with an invalid id (%llu), lot (%i)!", id, item->GetLot());
return;
}
items.erase(id);
free++;
}
2022-07-28 13:39:57 +00:00
eInventoryType Inventory::FindInventoryTypeForLot(const LOT lot) {
auto itemComponent = FindItemComponent(lot);
const auto itemType = static_cast<eItemType>(itemComponent.itemType);
switch (itemType) {
case eItemType::BRICK:
return BRICKS;
case eItemType::BEHAVIOR:
return BEHAVIORS;
case eItemType::PROPERTY:
return PROPERTY_DEEDS;
case eItemType::MODEL:
2023-03-13 10:16:14 +00:00
case eItemType::PET_INVENTORY_ITEM:
case eItemType::LOOT_MODEL:
2023-03-13 10:16:14 +00:00
case eItemType::VEHICLE:
case eItemType::MOUNT:
return MODELS;
case eItemType::HAT:
case eItemType::HAIR:
case eItemType::NECK:
case eItemType::LEFT_HAND:
case eItemType::RIGHT_HAND:
case eItemType::LEGS:
case eItemType::LEFT_TRINKET:
case eItemType::RIGHT_TRINKET:
case eItemType::COLLECTIBLE:
case eItemType::CONSUMABLE:
case eItemType::CHEST:
case eItemType::EGG:
case eItemType::PET_FOOD:
case eItemType::PACKAGE:
2023-03-13 10:16:14 +00:00
case eItemType::LUP_MODEL:
return ITEMS;
case eItemType::QUEST_OBJECT:
case eItemType::UNKNOWN:
default:
return QUEST;
}
}
2022-07-28 13:39:57 +00:00
const CDItemComponent& Inventory::FindItemComponent(const LOT lot) {
auto* registry = CDClientManager::Instance().GetTable<CDComponentsRegistryTable>();
auto* itemComponents = CDClientManager::Instance().GetTable<CDItemComponentTable>();
const auto componentId = registry->GetByIDAndType(lot, eReplicaComponentType::ITEM);
2022-07-28 13:39:57 +00:00
if (componentId == 0) {
Game::logger->Log("Inventory", "Failed to find item component for (%i)!", lot);
return CDItemComponentTable::Default;
}
const auto& itemComponent = itemComponents->GetItemComponentByID(componentId);
return itemComponent;
}
2022-07-28 13:39:57 +00:00
bool Inventory::IsValidItem(const LOT lot) {
auto* registry = CDClientManager::Instance().GetTable<CDComponentsRegistryTable>();
const auto componentId = registry->GetByIDAndType(lot, eReplicaComponentType::ITEM);
return componentId != 0;
}
2022-07-28 13:39:57 +00:00
const std::vector<LOT>& Inventory::GetAllGMItems() {
return m_GameMasterRestrictedItems;
}
void Inventory::DeleteAllItems() {
while (!this->items.empty()) {
if (items.begin()->second) items.begin()->second->SetCount(0);
}
}
2022-07-28 13:39:57 +00:00
Inventory::~Inventory() {
for (auto item : items) {
delete item.second;
}
items.clear();
}