2022-08-06 03:01:59 +00:00
|
|
|
#include "Item.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "../dWorldServer/ObjectIDManager.h"
|
|
|
|
#include "GeneralUtils.h"
|
|
|
|
#include "GameMessages.h"
|
|
|
|
#include "Entity.h"
|
|
|
|
#include "Game.h"
|
2023-10-21 23:31:55 +00:00
|
|
|
#include "Logger.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "EntityManager.h"
|
|
|
|
#include "RenderComponent.h"
|
2022-09-02 18:49:19 +00:00
|
|
|
#include "PossessableComponent.h"
|
|
|
|
#include "CharacterComponent.h"
|
|
|
|
#include "eItemType.h"
|
2022-11-01 18:21:26 +00:00
|
|
|
#include "AssetManager.h"
|
2022-12-18 15:46:04 +00:00
|
|
|
#include "InventoryComponent.h"
|
2023-01-07 05:17:05 +00:00
|
|
|
#include "Loot.h"
|
2023-04-25 18:17:40 +00:00
|
|
|
#include "eObjectBits.h"
|
2023-03-04 07:16:37 +00:00
|
|
|
#include "eReplicaComponentType.h"
|
2023-05-02 22:39:21 +00:00
|
|
|
#include "eUseItemResponse.h"
|
2023-11-14 13:02:17 +00:00
|
|
|
#include "dZoneManager.h"
|
|
|
|
#include "ChatPackets.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-03-17 14:36:21 +00:00
|
|
|
#include "CDBrickIDTableTable.h"
|
|
|
|
#include "CDObjectSkillsTable.h"
|
|
|
|
#include "CDComponentsRegistryTable.h"
|
|
|
|
#include "CDPackageComponentTable.h"
|
|
|
|
|
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)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
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;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
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)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (isModMoveAndEquip) {
|
2021-12-05 17:54:36 +00:00
|
|
|
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();
|
|
|
|
|
2023-04-25 18:17:40 +00:00
|
|
|
GeneralUtils::SetBit(id, eObjectBits::CHARACTER);
|
|
|
|
GeneralUtils::SetBit(id, eObjectBits::PERSISTENT);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-09-02 18:49:19 +00:00
|
|
|
const auto type = static_cast<eItemType>(info->itemType);
|
|
|
|
|
2023-01-22 23:38:47 +00:00
|
|
|
if (type == eItemType::MOUNT) {
|
2023-04-25 18:17:40 +00:00
|
|
|
GeneralUtils::SetBit(id, eObjectBits::CLIENT);
|
2022-09-02 18:49:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
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);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (isModMoveAndEquip) {
|
2021-12-05 17:54:36 +00:00
|
|
|
Equip();
|
|
|
|
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Move and equipped (%i) from (%i)", this->lot, this->inventory->GetType());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-15 20:56:33 +00:00
|
|
|
Game::entityManager->SerializeEntity(inventory->GetComponent()->GetParent());
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
LWOOBJID Item::GetId() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
LOT Item::GetLot() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return lot;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
uint32_t Item::GetCount() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
uint32_t Item::GetSlot() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::vector<LDFBaseData*>& Item::GetConfig() {
|
2021-12-05 17:54:36 +00:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const CDItemComponent& Item::GetInfo() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return *info;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool Item::GetBound() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return bound;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Inventory* Item::GetInventory() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return inventory;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
LWOOBJID Item::GetParent() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
LWOOBJID Item::GetSubKey() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return subKey;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
PreconditionExpression* Item::GetPreconditionExpression() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
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) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
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) {
|
2021-12-05 17:54:36 +00:00
|
|
|
Disassemble();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!silent) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* entity = inventory->GetComponent()->GetParent();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (value > count) {
|
2022-04-24 03:35:34 +00:00
|
|
|
GameMessages::SendAddItemToInventoryClientSync(entity, entity->GetSystemAddress(), this, id, showFlyingLoot, delta, LWOOBJID_EMPTY, lootSourceType);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
GameMessages::SendRemoveItemFromInventory(entity, entity->GetSystemAddress(), id, lot, inventory->GetType(), delta, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count = value;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (count == 0) {
|
2021-12-05 17:54:36 +00:00
|
|
|
RemoveFromInventory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Item::SetSlot(const uint32_t value) {
|
|
|
|
if (slot == value) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& pair : inventory->GetItems()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* item = pair.second;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (item->slot == value) {
|
2021-12-05 17:54:36 +00:00
|
|
|
item->slot = slot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
slot = value;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Item::SetBound(const bool value) {
|
2021-12-05 17:54:36 +00:00
|
|
|
bound = value;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Item::SetSubKey(LWOOBJID value) {
|
2021-12-05 17:54:36 +00:00
|
|
|
subKey = value;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Item::SetInventory(Inventory* value) {
|
2021-12-05 17:54:36 +00:00
|
|
|
inventory->RemoveManagedItem(this);
|
|
|
|
|
|
|
|
inventory = value;
|
|
|
|
|
|
|
|
inventory->AddManagedItem(this);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Item::Equip(const bool skipChecks) {
|
|
|
|
if (IsEquipped()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
inventory->GetComponent()->EquipItem(this, skipChecks);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Item::UnEquip() {
|
|
|
|
if (!IsEquipped()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
inventory->GetComponent()->UnEquipItem(this);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool Item::IsEquipped() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* component = inventory->GetComponent();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& pair : component->GetEquippedItems()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto item = pair.second;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (item.id == id) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool Item::Consume() {
|
2023-03-17 14:36:21 +00:00
|
|
|
auto* skillsTable = CDClientManager::Instance().GetTable<CDObjectSkillsTable>();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto skills = skillsTable->Query([=](const CDObjectSkills entry) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return entry.objectTemplate == static_cast<uint32_t>(lot);
|
2022-07-28 13:39:57 +00:00
|
|
|
});
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
auto success = false;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto& skill : skills) {
|
2021-12-05 17:54:36 +00:00
|
|
|
if (skill.castOnType == 3) // Consumable type
|
|
|
|
{
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG_DEBUG("Consumed LOT (%i) itemID (%llu). Success=(%d)", lot, id, success);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
GameMessages::SendUseItemResult(inventory->GetComponent()->GetParent(), lot, success);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (success) {
|
2021-12-05 17:54:36 +00:00
|
|
|
inventory->GetComponent()->RemoveItem(lot, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2022-12-24 22:41:13 +00:00
|
|
|
void Item::UseNonEquip(Item* item) {
|
2022-12-11 08:27:01 +00:00
|
|
|
LOT thisLot = this->GetLot();
|
|
|
|
if (!GetInventory()) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG_DEBUG("item %i has no inventory??", this->GetLot());
|
2022-12-11 08:27:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* playerInventoryComponent = GetInventory()->GetComponent();
|
|
|
|
if (!playerInventoryComponent) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG_DEBUG("no inventory component attached to item id %llu lot %i", this->GetId(), this->GetLot());
|
2022-12-11 08:27:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* playerEntity = playerInventoryComponent->GetParent();
|
|
|
|
if (!playerEntity) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG_DEBUG("no player entity attached to inventory? item id is %llu", this->GetId());
|
2022-12-11 08:27:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-02 18:49:19 +00:00
|
|
|
const auto type = static_cast<eItemType>(info->itemType);
|
2023-01-22 23:38:47 +00:00
|
|
|
if (type == eItemType::MOUNT) {
|
2023-11-18 07:15:47 +00:00
|
|
|
if (Game::zoneManager->GetMountsAllowed()) {
|
2023-11-14 13:02:17 +00:00
|
|
|
playerInventoryComponent->HandlePossession(this);
|
|
|
|
} else {
|
|
|
|
ChatPackets::SendSystemMessage(playerEntity->GetSystemAddress(), u"Mounts are not allowed in this zone");
|
|
|
|
}
|
2023-11-18 07:15:47 +00:00
|
|
|
} else if (type == eItemType::PET_INVENTORY_ITEM && subKey != LWOOBJID_EMPTY) {
|
|
|
|
if (Game::zoneManager->GetPetsAllowed()) {
|
2023-11-14 13:02:17 +00:00
|
|
|
const auto& databasePet = playerInventoryComponent->GetDatabasePet(subKey);
|
|
|
|
if (databasePet.lot != LOT_NULL) {
|
|
|
|
playerInventoryComponent->SpawnPet(this);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ChatPackets::SendSystemMessage(playerEntity->GetSystemAddress(), u"Pets are not allowed in this zone");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-12-11 08:27:01 +00:00
|
|
|
// This precondition response is taken care of in SpawnPet().
|
2022-09-05 22:28:32 +00:00
|
|
|
} else {
|
2022-12-24 22:41:13 +00:00
|
|
|
bool success = false;
|
|
|
|
auto inventory = item->GetInventory();
|
|
|
|
if (inventory && inventory->GetType() == eInventoryType::ITEMS) {
|
2023-03-17 14:36:21 +00:00
|
|
|
auto* compRegistryTable = CDClientManager::Instance().GetTable<CDComponentsRegistryTable>();
|
2023-03-04 07:16:37 +00:00
|
|
|
const auto packageComponentId = compRegistryTable->GetByIDAndType(lot, eReplicaComponentType::PACKAGE);
|
2022-12-24 22:41:13 +00:00
|
|
|
|
|
|
|
if (packageComponentId == 0) return;
|
|
|
|
|
2023-03-17 14:36:21 +00:00
|
|
|
auto* packCompTable = CDClientManager::Instance().GetTable<CDPackageComponentTable>();
|
2022-12-24 22:41:13 +00:00
|
|
|
auto packages = packCompTable->Query([=](const CDPackageComponent entry) {return entry.id == static_cast<uint32_t>(packageComponentId); });
|
|
|
|
|
|
|
|
auto success = !packages.empty();
|
|
|
|
if (success) {
|
|
|
|
if (this->GetPreconditionExpression()->Check(playerInventoryComponent->GetParent())) {
|
|
|
|
auto* entityParent = playerInventoryComponent->GetParent();
|
|
|
|
// Roll the loot for all the packages then see if it all fits. If it fits, give it to the player, otherwise don't.
|
|
|
|
std::unordered_map<LOT, int32_t> rolledLoot{};
|
|
|
|
for (auto& pack : packages) {
|
2023-10-09 20:33:22 +00:00
|
|
|
auto thisPackage = Loot::RollLootMatrix(entityParent, pack.LootMatrixIndex);
|
2022-12-24 22:41:13 +00:00
|
|
|
for (auto& loot : thisPackage) {
|
|
|
|
// If we already rolled this lot, add it to the existing one, otherwise create a new entry.
|
|
|
|
auto existingLoot = rolledLoot.find(loot.first);
|
|
|
|
if (existingLoot == rolledLoot.end()) {
|
|
|
|
rolledLoot.insert(loot);
|
|
|
|
} else {
|
|
|
|
existingLoot->second += loot.second;
|
|
|
|
}
|
2022-12-11 08:27:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-24 22:41:13 +00:00
|
|
|
if (playerInventoryComponent->HasSpaceForLoot(rolledLoot)) {
|
2023-10-09 20:33:22 +00:00
|
|
|
Loot::GiveLoot(playerInventoryComponent->GetParent(), rolledLoot, eLootSourceType::CONSUMPTION);
|
2022-12-24 22:41:13 +00:00
|
|
|
item->SetCount(item->GetCount() - 1);
|
|
|
|
} else {
|
|
|
|
success = false;
|
|
|
|
}
|
2022-12-11 08:27:01 +00:00
|
|
|
} else {
|
2022-12-24 22:41:13 +00:00
|
|
|
GameMessages::SendUseItemRequirementsResponse(
|
|
|
|
playerInventoryComponent->GetParent()->GetObjectID(),
|
|
|
|
playerInventoryComponent->GetParent()->GetSystemAddress(),
|
2023-05-02 22:39:21 +00:00
|
|
|
eUseItemResponse::FailedPrecondition
|
2022-12-24 22:41:13 +00:00
|
|
|
);
|
2022-12-11 08:27:01 +00:00
|
|
|
success = false;
|
2022-09-02 18:49:19 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG_DEBUG("Player %llu %s used item %i", playerEntity->GetObjectID(), success ? "successfully" : "unsuccessfully", thisLot);
|
2022-12-11 08:27:01 +00:00
|
|
|
GameMessages::SendUseItemResult(playerInventoryComponent->GetParent(), thisLot, success);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Item::Disassemble(const eInventoryType inventoryType) {
|
|
|
|
for (auto* data : config) {
|
|
|
|
if (data->GetKey() == u"assemblyPartLOTs") {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto modStr = data->GetValueAsString();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-12-31 19:44:09 +00:00
|
|
|
// This shouldn't be null but always check your pointers.
|
|
|
|
if (GetInventory()) {
|
|
|
|
auto inventoryComponent = GetInventory()->GetComponent();
|
|
|
|
if (inventoryComponent) {
|
|
|
|
auto entity = inventoryComponent->GetParent();
|
|
|
|
if (entity) entity->SetVar<std::string>(u"currentModifiedBuild", modStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
std::vector<LOT> modArray;
|
|
|
|
|
|
|
|
std::stringstream ssData(modStr);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
std::string token;
|
|
|
|
|
|
|
|
const auto deliminator = '+';
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
while (std::getline(ssData, token, deliminator)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto modLot = std::stoi(token.substr(2, token.size() - 1));
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
modArray.push_back(modLot);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto mod : modArray) {
|
2023-05-02 22:39:21 +00:00
|
|
|
inventory->GetComponent()->AddItem(mod, 1, eLootSourceType::DELETION, inventoryType);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 01:29:53 +00:00
|
|
|
void Item::DisassembleModel(uint32_t numToDismantle) {
|
2023-03-17 14:36:21 +00:00
|
|
|
auto* table = CDClientManager::Instance().GetTable<CDComponentsRegistryTable>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-03-04 07:16:37 +00:00
|
|
|
const auto componentId = table->GetByIDAndType(GetLot(), eReplicaComponentType::RENDER);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-11-16 01:29:53 +00:00
|
|
|
auto query = CDClientDatabase::CreatePreppedStmt("SELECT render_asset, LXFMLFolder FROM RenderComponent WHERE id = ?;");
|
|
|
|
query.bind(1, static_cast<int>(componentId));
|
2022-01-13 03:48:27 +00:00
|
|
|
|
|
|
|
auto result = query.execQuery();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-04-03 06:26:44 +00:00
|
|
|
if (result.eof() || result.fieldIsNull(0)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2023-04-03 06:26:44 +00:00
|
|
|
std::string renderAsset = std::string(result.getStringField(0));
|
2023-11-18 07:15:47 +00:00
|
|
|
|
|
|
|
// normalize path slashes
|
|
|
|
for (auto& c : renderAsset) {
|
|
|
|
if (c == '\\') c = '/';
|
|
|
|
}
|
|
|
|
|
2023-04-03 06:26:44 +00:00
|
|
|
std::string lxfmlFolderName = std::string(result.getStringField(1));
|
2023-11-18 07:15:47 +00:00
|
|
|
if (!lxfmlFolderName.empty()) lxfmlFolderName.insert(0, "/");
|
2023-04-03 06:26:44 +00:00
|
|
|
|
2023-11-18 07:15:47 +00:00
|
|
|
std::vector<std::string> renderAssetSplit = GeneralUtils::SplitString(renderAsset, '/');
|
|
|
|
if (renderAssetSplit.empty()) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-11-18 07:15:47 +00:00
|
|
|
std::string lxfmlPath = "BrickModels" + lxfmlFolderName + "/" + GeneralUtils::SplitString(renderAssetSplit.back(), '.').at(0) + ".lxfml";
|
2023-12-23 17:24:16 +00:00
|
|
|
auto file = Game::assetManager->GetFile(lxfmlPath.c_str());
|
2022-11-01 18:21:26 +00:00
|
|
|
|
2023-12-23 17:24:16 +00:00
|
|
|
if (!file) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Failed to load %s to disassemble model into bricks, check that this file exists", lxfmlPath.c_str());
|
2022-11-10 18:59:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
std::stringstream data;
|
|
|
|
data << file.rdbuf();
|
|
|
|
|
2023-11-16 01:29:53 +00:00
|
|
|
uint32_t fileSize;
|
|
|
|
file.seekg(0, std::ios::end);
|
|
|
|
fileSize = static_cast<uint32_t>(file.tellg());
|
|
|
|
file.seekg(0, std::ios::beg);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-11-16 01:29:53 +00:00
|
|
|
if (fileSize == 0) return;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2023-11-16 01:29:53 +00:00
|
|
|
tinyxml2::XMLDocument doc;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-11-16 01:29:53 +00:00
|
|
|
if (doc.Parse(data.str().c_str(), data.str().size()) != tinyxml2::XML_SUCCESS) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-16 01:29:53 +00:00
|
|
|
auto* lxfml = doc.FirstChildElement("LXFML");
|
|
|
|
if (!lxfml) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* bricks = lxfml->FirstChildElement("Bricks");
|
|
|
|
std::string searchTerm = "Brick";
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!bricks) {
|
2021-12-05 17:54:36 +00:00
|
|
|
searchTerm = "Part";
|
2023-11-16 01:29:53 +00:00
|
|
|
auto* scene = lxfml->FirstChildElement("Scene");
|
|
|
|
if (!scene) return;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2023-11-16 01:29:53 +00:00
|
|
|
auto* model = scene->FirstChildElement("Model");
|
|
|
|
if (!model) return;
|
|
|
|
|
2023-11-18 07:15:47 +00:00
|
|
|
bricks = model->FirstChildElement("Group");
|
|
|
|
if (!bricks) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* currentBrick = bricks->FirstChildElement(searchTerm.c_str());
|
2023-11-16 01:29:53 +00:00
|
|
|
|
|
|
|
// First iteration gets the count
|
|
|
|
std::map<int32_t, int32_t> parts;
|
2022-07-28 13:39:57 +00:00
|
|
|
while (currentBrick) {
|
2023-11-16 01:29:53 +00:00
|
|
|
auto* designID = currentBrick->Attribute("designID");
|
|
|
|
if (designID) {
|
|
|
|
uint32_t designId;
|
|
|
|
if (!GeneralUtils::TryParse(designID, designId)) {
|
|
|
|
LOG("Failed to parse designID %s", designID);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
parts[designId]++;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
currentBrick = currentBrick->NextSiblingElement(searchTerm.c_str());
|
|
|
|
}
|
|
|
|
|
2023-03-17 14:36:21 +00:00
|
|
|
auto* brickIDTable = CDClientManager::Instance().GetTable<CDBrickIDTableTable>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-11-16 01:29:53 +00:00
|
|
|
// Second iteration actually distributes the bricks
|
2023-11-18 07:15:47 +00:00
|
|
|
for (const auto& [part, count] : parts) {
|
2023-11-16 01:29:53 +00:00
|
|
|
const auto partLocal = part;
|
|
|
|
const auto brickID = brickIDTable->Query([&](const CDBrickIDTable& entry) {
|
|
|
|
return entry.LEGOBrickID == partLocal;
|
2022-07-28 13:39:57 +00:00
|
|
|
});
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-11-16 01:29:53 +00:00
|
|
|
if (brickID.empty()) continue;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2023-11-16 01:29:53 +00:00
|
|
|
GetInventory()->GetComponent()->AddItem(brickID[0].NDObjectID, count * numToDismantle, eLootSourceType::DELETION);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Item::RemoveFromInventory() {
|
2021-12-05 17:54:36 +00:00
|
|
|
UnEquip();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
count = 0;
|
|
|
|
|
|
|
|
inventory->RemoveManagedItem(this);
|
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Item::~Item() {
|
2021-12-05 17:54:36 +00:00
|
|
|
delete preconditions;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* value : config) {
|
2021-12-05 17:54:36 +00:00
|
|
|
delete value;
|
|
|
|
}
|
|
|
|
|
|
|
|
config.clear();
|
|
|
|
}
|