diff --git a/dGame/dComponents/InventoryComponent.cpp b/dGame/dComponents/InventoryComponent.cpp index a2798068c..ff6e17179 100644 --- a/dGame/dComponents/InventoryComponent.cpp +++ b/dGame/dComponents/InventoryComponent.cpp @@ -2008,7 +2008,8 @@ void InventoryComponent::UpdateGroup(const GroupUpdate& groupUpdate) { return; } - if (groupUpdate.command == GroupUpdateCommand::ADD && groups.size() >= MaximumGroupCount) { + const auto unlockedGroups = std::ranges::count_if(groups, [](const Group& group) { return !group.locked; }); + if (groupUpdate.command == GroupUpdateCommand::ADD && !groupUpdate.locked && unlockedGroups >= MaximumGroupCount) { LOG("Cannot add group to inventory %s. Maximum group count reached.", StringifiedEnum::ToString(groupUpdate.inventory).data()); return; } @@ -2018,11 +2019,12 @@ void InventoryComponent::UpdateGroup(const GroupUpdate& groupUpdate) { auto& group = groups.emplace_back(); group.groupId = groupUpdate.groupId; group.groupName = groupUpdate.groupName; + group.locked = groupUpdate.locked; break; } case GroupUpdateCommand::ADD_LOT: { - // msgUpdateInventoryGroupContents (0x00ce1f50): a group holds at most 1000 LOTs - if (groupItr->lots.size() >= MaximumGroupLotCount) break; + // msgUpdateInventoryGroupContents (0x00ce1f50): an unlocked group holds at most 1000 LOTs + if (!groupItr->locked && groupItr->lots.size() >= MaximumGroupLotCount) break; groupItr->lots.insert(groupUpdate.lot); break; } @@ -2057,7 +2059,7 @@ void InventoryComponent::UpdateGroupXml(tinyxml2::XMLElement& groups) const { groupElement->SetAttribute("id", group.groupId.c_str()); groupElement->SetAttribute("n", group.groupName.c_str()); groupElement->SetAttribute("t", static_cast(inventory)); - groupElement->SetAttribute("u", 0); + groupElement->SetAttribute("u", group.locked ? 1 : 0); std::ostringstream lots; bool first = true; for (const auto lot : group.lots) { @@ -2084,6 +2086,8 @@ void InventoryComponent::LoadGroupXml(const tinyxml2::XMLElement& groups) { groupElement->QueryStringAttribute("n", &groupName); groupElement->QueryStringAttribute("l", &lots); groupElement->QueryAttribute("t", &inventory); + bool locked = false; + groupElement->QueryBoolAttribute("u", &locked); if (!groupId || !groupName || !lots) { LOG("Failed to load group from xml id %i name %i lots %i", @@ -2092,6 +2096,7 @@ void InventoryComponent::LoadGroupXml(const tinyxml2::XMLElement& groups) { auto& group = m_Groups[static_cast(inventory)].emplace_back(); group.groupId = groupId; group.groupName = groupName; + group.locked = locked; for (const auto& lotStr : GeneralUtils::SplitString(lots, ' ')) { auto lot = GeneralUtils::TryParse(lotStr); @@ -2409,6 +2414,7 @@ void InventoryComponent::OnUpdateInventoryGroup(const GameMessages::UpdateInvent groupUpdate.inventory = msg.inventoryType; // msgUpdateInventoryGroup (0x00c955b0) cuts names to 64 characters groupUpdate.groupName = GeneralUtils::UTF16ToWTF8(msg.groupName.substr(0, MaximumGroupNameLength)); + groupUpdate.locked = msg.locked; if (msg.action == "ADD") groupUpdate.command = GroupUpdateCommand::ADD; else if (msg.action == "MODIFY") groupUpdate.command = GroupUpdateCommand::MODIFY; diff --git a/dGame/dComponents/InventoryComponent.h b/dGame/dComponents/InventoryComponent.h index d7d7e1832..a51990659 100644 --- a/dGame/dComponents/InventoryComponent.h +++ b/dGame/dComponents/InventoryComponent.h @@ -78,6 +78,8 @@ public: std::string groupName; // All the lots the user has in the group. std::set lots; + // Locked groups don't count toward MaximumGroupCount and have no LOT cap (saved as u) + bool locked = false; }; enum class GroupUpdateCommand { @@ -97,6 +99,7 @@ public: LOT lot; eInventoryType inventory; GroupUpdateCommand command; + bool locked = false; }; static constexpr uint32_t MaximumGroupCount = 50; diff --git a/tests/dGameTests/dComponentsTests/InventoryGroupTests.cpp b/tests/dGameTests/dComponentsTests/InventoryGroupTests.cpp index e9a8376c6..414d4be3a 100644 --- a/tests/dGameTests/dComponentsTests/InventoryGroupTests.cpp +++ b/tests/dGameTests/dComponentsTests/InventoryGroupTests.cpp @@ -7,6 +7,7 @@ #include "InventoryMessages.h" #include +#include // Inventory groups as LWOInventoryComponent_Common::msgUpdateInventoryGroup (0x00c955b0) and // msgUpdateInventoryGroupContents (0x00ce1f50) keep them. @@ -96,3 +97,38 @@ TEST_F(InventoryGroupTests, AGroupHoldsAtMost1000Lots) { EXPECT_EQ(Groups()[0].lots.size(), 1000u); EXPECT_FALSE(Groups()[0].lots.contains(1001)); } + +TEST_F(InventoryGroupTests, LockedGroupsHaveNoLimits) { + for (uint32_t i = 0; i < InventoryComponent::MaximumGroupCount; i++) Group("ADD", "user_group" + std::to_string(i), u"g"); + Group("ADD", "over", u"g"); + EXPECT_EQ(Groups().size(), InventoryComponent::MaximumGroupCount); + + Group("ADD", "locked", u"g", eInventoryType::BRICKS, true); + ASSERT_EQ(Groups().size(), InventoryComponent::MaximumGroupCount + 1); + EXPECT_TRUE(Groups().back().locked); + for (LOT lot = 1; lot <= 1001; lot++) Contents("ADD", "locked", lot); + EXPECT_EQ(Groups().back().lots.size(), 1001u); +} + +TEST_F(InventoryGroupTests, TheLockedFlagIsSavedAndLoaded) { + Group("ADD", "locked", u"a", eInventoryType::BRICKS, true); + Group("ADD", "unlocked", u"b"); + + tinyxml2::XMLDocument doc; + doc.Parse(""); + inventory->WriteSaveData(doc); + auto* const grp = doc.FirstChildElement("obj")->FirstChildElement("inv")->FirstChildElement("grps")->FirstChildElement("grp"); + ASSERT_NE(grp, nullptr); + EXPECT_STREQ(grp->Attribute("u"), "1"); + EXPECT_STREQ(grp->NextSiblingElement("grp")->Attribute("u"), "0"); + + // Saves from before have u="0" or no u + grp->NextSiblingElement("grp")->DeleteAttribute("u"); + auto loaded = std::make_unique(2, info); + auto* const loadedInventory = loaded->AddComponent(-1); + loadedInventory->LoadXml(doc); + const auto& groups = loadedInventory->GetGroups().at(eInventoryType::BRICKS); + ASSERT_EQ(groups.size(), 2u); + EXPECT_TRUE(groups[0].locked); + EXPECT_FALSE(groups[1].locked); +}