DarkflameServer/dGame/dInventory/Item.cpp

424 lines
10 KiB
C++
Raw Normal View History

2022-08-06 03:01:59 +00:00
#include "Item.h"
#include <sstream>
#include "../dWorldServer/ObjectIDManager.h"
#include "GeneralUtils.h"
#include "GameMessages.h"
#include "Entity.h"
#include "Game.h"
#include "dLogger.h"
#include "EntityManager.h"
#include "RenderComponent.h"
#include "PossessableComponent.h"
#include "CharacterComponent.h"
#include "eItemType.h"
class Inventory;
2022-07-28 13:39:57 +00:00
Item::Item(const LWOOBJID id, const LOT lot, Inventory* inventory, const uint32_t slot, const uint32_t count, const bool bound, const std::vector<LDFBaseData*>& config, const LWOOBJID parent, LWOOBJID subKey, eLootSourceType lootSourceType) {
if (!Inventory::IsValidItem(lot)) {
return;
}
this->id = id;
this->lot = lot;
this->inventory = inventory;
this->slot = slot;
this->count = count;
this->bound = bound;
this->config = config;
this->parent = parent;
this->info = &Inventory::FindItemComponent(lot);
this->preconditions = new PreconditionExpression(this->info->reqPrecondition);
this->subKey = subKey;
inventory->AddManagedItem(this);
}
Item::Item(
const LOT lot,
Inventory* inventory,
const uint32_t slot,
const uint32_t count,
const std::vector<LDFBaseData*>& config,
const LWOOBJID parent,
bool showFlyingLoot,
bool isModMoveAndEquip,
LWOOBJID subKey,
2022-04-23 12:13:06 +00:00
bool bound,
2022-07-28 13:39:57 +00:00
eLootSourceType lootSourceType) {
if (!Inventory::IsValidItem(lot)) {
return;
}
2022-07-28 13:39:57 +00:00
if (isModMoveAndEquip) {
showFlyingLoot = false;
}
this->lot = lot;
this->inventory = inventory;
this->slot = slot;
this->count = count;
this->config = config;
this->parent = parent;
this->id = LWOOBJID_EMPTY;
this->info = &Inventory::FindItemComponent(lot);
this->bound = info->isBOP || bound;
this->preconditions = new PreconditionExpression(this->info->reqPrecondition);
this->subKey = subKey;
LWOOBJID id = ObjectIDManager::GenerateRandomObjectID();
id = GeneralUtils::SetBit(id, OBJECT_BIT_CHARACTER);
id = GeneralUtils::SetBit(id, OBJECT_BIT_PERSISTENT);
const auto type = static_cast<eItemType>(info->itemType);
if (type == eItemType::ITEM_TYPE_MOUNT) {
id = GeneralUtils::SetBit(id, OBJECT_BIT_CLIENT);
}
this->id = id;
inventory->AddManagedItem(this);
auto* entity = inventory->GetComponent()->GetParent();
2022-04-23 12:13:06 +00:00
GameMessages::SendAddItemToInventoryClientSync(entity, entity->GetSystemAddress(), this, id, showFlyingLoot, static_cast<int>(this->count), subKey, lootSourceType);
2022-07-28 13:39:57 +00:00
if (isModMoveAndEquip) {
Equip();
Game::logger->Log("Item", "Move and equipped (%i) from (%i)", this->lot, this->inventory->GetType());
EntityManager::Instance()->SerializeEntity(inventory->GetComponent()->GetParent());
}
}
2022-07-28 13:39:57 +00:00
LWOOBJID Item::GetId() const {
return id;
}
2022-07-28 13:39:57 +00:00
LOT Item::GetLot() const {
return lot;
}
2022-07-28 13:39:57 +00:00
uint32_t Item::GetCount() const {
return count;
}
2022-07-28 13:39:57 +00:00
uint32_t Item::GetSlot() const {
return slot;
}
2022-07-28 13:39:57 +00:00
std::vector<LDFBaseData*>& Item::GetConfig() {
return config;
}
2022-07-28 13:39:57 +00:00
const CDItemComponent& Item::GetInfo() const {
return *info;
}
2022-07-28 13:39:57 +00:00
bool Item::GetBound() const {
return bound;
}
2022-07-28 13:39:57 +00:00
Inventory* Item::GetInventory() const {
return inventory;
}
2022-07-28 13:39:57 +00:00
LWOOBJID Item::GetParent() const {
return parent;
}
2022-07-28 13:39:57 +00:00
LWOOBJID Item::GetSubKey() const {
return subKey;
}
2022-07-28 13:39:57 +00:00
PreconditionExpression* Item::GetPreconditionExpression() const {
return preconditions;
}
2022-07-28 13:39:57 +00:00
void Item::SetCount(const uint32_t value, const bool silent, const bool disassemble, const bool showFlyingLoot, eLootSourceType lootSourceType) {
if (value == count) {
return;
}
const auto delta = std::abs(static_cast<int32_t>(value) - static_cast<int32_t>(count));
const auto type = static_cast<eItemType>(info->itemType);
2022-07-28 13:39:57 +00:00
if (disassemble) {
if (value < count) {
for (auto i = 0; i < delta; ++i) {
Disassemble();
}
}
}
2022-07-28 13:39:57 +00:00
if (!silent) {
auto* entity = inventory->GetComponent()->GetParent();
2022-07-28 13:39:57 +00:00
if (value > count) {
GameMessages::SendAddItemToInventoryClientSync(entity, entity->GetSystemAddress(), this, id, showFlyingLoot, delta, LWOOBJID_EMPTY, lootSourceType);
2022-07-28 13:39:57 +00:00
} else {
GameMessages::SendRemoveItemFromInventory(entity, entity->GetSystemAddress(), id, lot, inventory->GetType(), delta, value);
}
}
count = value;
2022-07-28 13:39:57 +00:00
if (count == 0) {
RemoveFromInventory();
}
}
2022-07-28 13:39:57 +00:00
void Item::SetSlot(const uint32_t value) {
if (slot == value) {
return;
}
2022-07-28 13:39:57 +00:00
for (const auto& pair : inventory->GetItems()) {
auto* item = pair.second;
2022-07-28 13:39:57 +00:00
if (item->slot == value) {
item->slot = slot;
}
}
slot = value;
}
2022-07-28 13:39:57 +00:00
void Item::SetBound(const bool value) {
bound = value;
}
2022-07-28 13:39:57 +00:00
void Item::SetSubKey(LWOOBJID value) {
subKey = value;
}
2022-07-28 13:39:57 +00:00
void Item::SetInventory(Inventory* value) {
inventory->RemoveManagedItem(this);
inventory = value;
inventory->AddManagedItem(this);
}
2022-07-28 13:39:57 +00:00
void Item::Equip(const bool skipChecks) {
if (IsEquipped()) {
return;
}
inventory->GetComponent()->EquipItem(this, skipChecks);
}
2022-07-28 13:39:57 +00:00
void Item::UnEquip() {
if (!IsEquipped()) {
return;
}
inventory->GetComponent()->UnEquipItem(this);
}
2022-07-28 13:39:57 +00:00
bool Item::IsEquipped() const {
auto* component = inventory->GetComponent();
2022-07-28 13:39:57 +00:00
for (const auto& pair : component->GetEquippedItems()) {
const auto item = pair.second;
2022-07-28 13:39:57 +00:00
if (item.id == id) {
return true;
}
}
return false;
}
2022-07-28 13:39:57 +00:00
bool Item::Consume() {
auto* skillsTable = CDClientManager::Instance()->GetTable<CDObjectSkillsTable>("ObjectSkills");
2022-07-28 13:39:57 +00:00
auto skills = skillsTable->Query([=](const CDObjectSkills entry) {
return entry.objectTemplate == static_cast<uint32_t>(lot);
2022-07-28 13:39:57 +00:00
});
auto success = false;
2022-07-28 13:39:57 +00:00
for (auto& skill : skills) {
if (skill.castOnType == 3) // Consumable type
{
success = true;
}
}
Game::logger->Log("Item", "Consumed (%i) / (%llu) with (%d)", lot, id, success);
GameMessages::SendUseItemResult(inventory->GetComponent()->GetParent(), lot, success);
2022-07-28 13:39:57 +00:00
if (success) {
inventory->GetComponent()->RemoveItem(lot, 1);
}
return success;
}
void Item::UseNonEquip() {
const auto type = static_cast<eItemType>(info->itemType);
if (type == eItemType::ITEM_TYPE_MOUNT) {
GetInventory()->GetComponent()->HandlePossession(this);
} else if (type == eItemType::ITEM_TYPE_PET_INVENTORY_ITEM && subKey != LWOOBJID_EMPTY) {
const auto& databasePet = GetInventory()->GetComponent()->GetDatabasePet(subKey);
2022-07-28 13:39:57 +00:00
if (databasePet.lot != LOT_NULL) {
GetInventory()->GetComponent()->SpawnPet(this);
}
} else if (type == eItemType::ITEM_TYPE_PACKAGE) {
auto* compRegistryTable = CDClientManager::Instance()->GetTable<CDComponentsRegistryTable>("ComponentsRegistry");
const auto packageComponentId = compRegistryTable->GetByIDAndType(lot, COMPONENT_TYPE_PACKAGE);
auto* packCompTable = CDClientManager::Instance()->GetTable<CDPackageComponentTable>("PackageComponent");
auto packages = packCompTable->Query([=](const CDPackageComponent entry) {return entry.id == static_cast<uint32_t>(packageComponentId); });
const auto success = !packages.empty();
if (success) {
auto* entityParent = inventory->GetComponent()->GetParent();
for (auto& pack : packages) {
std::unordered_map<LOT, int32_t> result{};
result = LootGenerator::Instance().RollLootMatrix(entityParent, pack.LootMatrixIndex);
if (!inventory->GetComponent()->HasSpaceForLoot(result)) {
}
LootGenerator::Instance().GiveLoot(inventory->GetComponent()->GetParent(), result, eLootSourceType::LOOT_SOURCE_CONSUMPTION);
}
inventory->GetComponent()->RemoveItem(lot, 1);
}
}
}
2022-07-28 13:39:57 +00:00
void Item::Disassemble(const eInventoryType inventoryType) {
for (auto* data : config) {
if (data->GetKey() == u"assemblyPartLOTs") {
auto modStr = data->GetValueAsString();
std::vector<LOT> modArray;
std::stringstream ssData(modStr);
std::string token;
const auto deliminator = '+';
2022-07-28 13:39:57 +00:00
while (std::getline(ssData, token, deliminator)) {
const auto modLot = std::stoi(token.substr(2, token.size() - 1));
modArray.push_back(modLot);
}
2022-07-28 13:39:57 +00:00
for (const auto mod : modArray) {
inventory->GetComponent()->AddItem(mod, 1, eLootSourceType::LOOT_SOURCE_DELETION, inventoryType);
}
}
}
}
2022-07-28 13:39:57 +00:00
void Item::DisassembleModel() {
auto* table = CDClientManager::Instance()->GetTable<CDComponentsRegistryTable>("ComponentsRegistry");
const auto componentId = table->GetByIDAndType(GetLot(), COMPONENT_TYPE_RENDER);
auto query = CDClientDatabase::CreatePreppedStmt(
"SELECT render_asset FROM RenderComponent WHERE id = ?;");
2022-07-28 13:39:57 +00:00
query.bind(1, (int)componentId);
auto result = query.execQuery();
2022-07-28 13:39:57 +00:00
if (result.eof()) {
return;
}
std::string renderAsset = result.fieldIsNull(0) ? "" : std::string(result.getStringField(0));
std::vector<std::string> renderAssetSplit = GeneralUtils::SplitString(renderAsset, '\\');
std::string lxfmlPath = "res/BrickModels/" + GeneralUtils::SplitString(renderAssetSplit.back(), '.')[0] + ".lxfml";
std::ifstream file(lxfmlPath);
result.finalize();
2022-07-28 13:39:57 +00:00
if (!file.good()) {
return;
}
std::stringstream data;
data << file.rdbuf();
2022-07-28 13:39:57 +00:00
if (data.str().empty()) {
return;
}
auto* doc = new tinyxml2::XMLDocument();
2022-07-28 13:39:57 +00:00
if (!doc) {
return;
}
2022-07-28 13:39:57 +00:00
if (doc->Parse(data.str().c_str(), data.str().size()) != 0) {
return;
}
std::vector<int> parts;
auto* lxfml = doc->FirstChildElement("LXFML");
auto* bricks = lxfml->FirstChildElement("Bricks");
std::string searchTerm = "Brick";
2022-07-28 13:39:57 +00:00
if (!bricks) {
searchTerm = "Part";
bricks = lxfml->FirstChildElement("Scene")->FirstChildElement("Model")->FirstChildElement("Group");
2022-07-28 13:39:57 +00:00
if (!bricks) {
return;
}
}
auto* currentBrick = bricks->FirstChildElement(searchTerm.c_str());
2022-07-28 13:39:57 +00:00
while (currentBrick) {
if (currentBrick->Attribute("designID") != nullptr) {
parts.push_back(std::stoi(currentBrick->Attribute("designID")));
}
currentBrick = currentBrick->NextSiblingElement(searchTerm.c_str());
}
auto* brickIDTable = CDClientManager::Instance()->GetTable<CDBrickIDTableTable>("BrickIDTable");
2022-07-28 13:39:57 +00:00
for (unsigned int part : parts) {
const auto brickID = brickIDTable->Query([=](const CDBrickIDTable& entry) {
return entry.LEGOBrickID == part;
2022-07-28 13:39:57 +00:00
});
2022-07-28 13:39:57 +00:00
if (brickID.empty()) {
continue;
}
GetInventory()->GetComponent()->AddItem(brickID[0].NDObjectID, 1, eLootSourceType::LOOT_SOURCE_DELETION);
}
}
2022-07-28 13:39:57 +00:00
void Item::RemoveFromInventory() {
UnEquip();
count = 0;
inventory->RemoveManagedItem(this);
delete this;
}
2022-07-28 13:39:57 +00:00
Item::~Item() {
delete preconditions;
2022-07-28 13:39:57 +00:00
for (auto* value : config) {
delete value;
}
config.clear();
}