2022-08-06 03:01:59 +00:00
|
|
|
#include "Item.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
2024-01-05 12:31:22 +00:00
|
|
|
#include "ObjectIDManager.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#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"
|
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
namespace {
|
|
|
|
const std::map<std::string, std::string> ExtraSettingAbbreviations = {
|
|
|
|
{ "assemblyPartLOTs", "ma" },
|
|
|
|
{ "blueprintID", "b" },
|
|
|
|
{ "userModelID", "ui" },
|
|
|
|
{ "userModelName", "un" },
|
|
|
|
{ "userModelDesc", "ud" },
|
|
|
|
{ "userModelHasBhvr", "ub" },
|
|
|
|
{ "userModelBehaviors", "ubh" },
|
|
|
|
{ "userModelBehaviorSourceID", "ubs" },
|
|
|
|
{ "userModelPhysicsType", "up" },
|
|
|
|
{ "userModelMod", "um" },
|
|
|
|
{ "userModelOpt", "uo" },
|
|
|
|
{ "reforgedLOT", "rl" },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
std::vector<LDFBaseData*> Item::GetConfig() const {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
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() {
|
2024-02-09 05:40:43 +00:00
|
|
|
auto* skillsTable = CDClientManager::GetTable<CDObjectSkillsTable>();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2024-01-02 07:53:00 +00:00
|
|
|
auto skills = skillsTable->Query([this](const CDObjectSkills entry) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return entry.objectTemplate == static_cast<uint32_t>(lot);
|
2024-05-23 00:06:52 +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) {
|
2024-02-09 05:40:43 +00:00
|
|
|
auto* compRegistryTable = CDClientManager::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;
|
|
|
|
|
2024-02-09 05:40:43 +00:00
|
|
|
auto* packCompTable = CDClientManager::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) {
|
2024-02-09 05:40:43 +00:00
|
|
|
auto* table = CDClientManager::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
|
|
|
|
2024-04-14 04:41:51 +00:00
|
|
|
if (result.eof() || result.fieldIsNull("render_asset")) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2024-04-14 04:41:51 +00:00
|
|
|
std::string renderAsset = std::string(result.getStringField("render_asset"));
|
2023-11-18 07:15:47 +00:00
|
|
|
|
|
|
|
// normalize path slashes
|
|
|
|
for (auto& c : renderAsset) {
|
|
|
|
if (c == '\\') c = '/';
|
|
|
|
}
|
|
|
|
|
2024-04-14 04:41:51 +00:00
|
|
|
std::string lxfmlFolderName = std::string(result.getStringField("LXFMLFolder"));
|
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) {
|
2024-02-10 11:05:25 +00:00
|
|
|
const char* const designID = currentBrick->Attribute("designID");
|
2023-11-16 01:29:53 +00:00
|
|
|
if (designID) {
|
2024-02-10 11:05:25 +00:00
|
|
|
const auto designId = GeneralUtils::TryParse<uint32_t>(designID);
|
|
|
|
if (!designId) {
|
2023-11-16 01:29:53 +00:00
|
|
|
LOG("Failed to parse designID %s", designID);
|
|
|
|
continue;
|
|
|
|
}
|
2024-02-10 11:05:25 +00:00
|
|
|
parts[designId.value()]++;
|
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());
|
|
|
|
}
|
|
|
|
|
2024-02-09 05:40:43 +00:00
|
|
|
auto* brickIDTable = CDClientManager::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();
|
|
|
|
}
|
2024-05-23 00:06:52 +00:00
|
|
|
|
|
|
|
void Item::SaveConfigXml(tinyxml2::XMLElement& i) const {
|
|
|
|
tinyxml2::XMLElement* x = nullptr;
|
|
|
|
|
|
|
|
for (const auto* config : this->config) {
|
|
|
|
const auto& key = GeneralUtils::UTF16ToWTF8(config->GetKey());
|
|
|
|
const auto saveKey = ExtraSettingAbbreviations.find(key);
|
|
|
|
if (saveKey == ExtraSettingAbbreviations.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!x) {
|
|
|
|
x = i.InsertNewChildElement("x");
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto dataToSave = config->GetString(false);
|
|
|
|
x->SetAttribute(saveKey->second.c_str(), dataToSave.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Item::LoadConfigXml(const tinyxml2::XMLElement& i) {
|
|
|
|
const auto* x = i.FirstChildElement("x");
|
|
|
|
if (!x) return;
|
|
|
|
|
|
|
|
for (const auto& pair : ExtraSettingAbbreviations) {
|
|
|
|
const auto* data = x->Attribute(pair.second.c_str());
|
|
|
|
if (!data) continue;
|
|
|
|
|
|
|
|
const auto value = pair.first + "=" + data;
|
|
|
|
config.push_back(LDFBaseData::DataFromString(value));
|
|
|
|
}
|
|
|
|
}
|