From 9a0e16d824759f58072ed1daa6424fc480ea2a89 Mon Sep 17 00:00:00 2001 From: Harry Hopkinson Date: Mon, 21 Sep 2026 11:11:24 +0100 Subject: [PATCH] Create FlagHolder for ShopItems --- src/openrct2-ui/windows/Ride.cpp | 13 +++++----- src/openrct2/entity/Guest.cpp | 44 +++++++++++++++++--------------- src/openrct2/entity/Guest.h | 8 +++--- src/openrct2/park/ParkFile.cpp | 4 +-- src/openrct2/ride/ShopItem.cpp | 20 +++++++-------- src/openrct2/ride/ShopItem.h | 8 +++--- 6 files changed, 52 insertions(+), 45 deletions(-) diff --git a/src/openrct2-ui/windows/Ride.cpp b/src/openrct2-ui/windows/Ride.cpp index c23c74f79e..013fa50715 100644 --- a/src/openrct2-ui/windows/Ride.cpp +++ b/src/openrct2-ui/windows/Ride.cpp @@ -6526,22 +6526,23 @@ namespace OpenRCT2::Ui::Windows static void UpdateSamePriceThroughoutFlags(ShopItem shop_item) { const auto& gameState = getGameState(); - const auto existingFlags = gameState.park.samePriceThroughoutPark; + const auto existingFlags = ShopItems(gameState.park.samePriceThroughoutPark); auto newFlags = existingFlags; if (GetShopItemDescriptor(shop_item).IsPhoto()) { - if (existingFlags & EnumToFlag(shop_item)) - newFlags &= ~EnumsToFlags(ShopItem::photo, ShopItem::photo2, ShopItem::photo3, ShopItem::photo4); + if (existingFlags.has(shop_item)) + newFlags.unset(ShopItem::photo, ShopItem::photo2, ShopItem::photo3, ShopItem::photo4); else - newFlags |= EnumsToFlags(ShopItem::photo, ShopItem::photo2, ShopItem::photo3, ShopItem::photo4); + newFlags.set(ShopItem::photo, ShopItem::photo2, ShopItem::photo3, ShopItem::photo4); } else { - newFlags ^= EnumToFlag(shop_item); + newFlags.flip(shop_item); } - auto parkSetParameter = GameActions::ParkSetParameterAction(GameActions::ParkParameter::samePriceInPark, newFlags); + auto parkSetParameter = GameActions::ParkSetParameterAction( + GameActions::ParkParameter::samePriceInPark, newFlags.holder); GameActions::Execute(&parkSetParameter, getGameState()); } diff --git a/src/openrct2/entity/Guest.cpp b/src/openrct2/entity/Guest.cpp index 42efecbedd..d3b86076b0 100644 --- a/src/openrct2/entity/Guest.cpp +++ b/src/openrct2/entity/Guest.cpp @@ -745,7 +745,7 @@ namespace OpenRCT2 if (timeToConsume == 0) { - int32_t chosen_food = Numerics::bitScanForward(getFoodOrDrinkFlags()); + int32_t chosen_food = Numerics::bitScanForward(getFoodOrDrinkFlags().holder); if (chosen_food != -1) { ShopItem food = ShopItem(chosen_food); @@ -1294,19 +1294,23 @@ namespace OpenRCT2 * To simplify check of 0x36BA3E0 and 0x11FF78 * returns false on no food. */ - uint64_t Guest::getFoodOrDrinkFlags() const + ShopItems Guest::getFoodOrDrinkFlags() const { - return getItemFlags() & (ShopItemsGetAllFoods() | ShopItemsGetAllDrinks()); + ShopItems items = getItemFlags(); + items.holder &= (ShopItemsGetAllFoods() | ShopItemsGetAllDrinks()).holder; + return items; } - uint64_t Guest::getEmptyContainerFlags() const + ShopItems Guest::getEmptyContainerFlags() const { - return getItemFlags() & ShopItemsGetAllContainers(); + ShopItems items = getItemFlags(); + items.holder &= ShopItemsGetAllContainers().holder; + return items; } bool Guest::hasFoodOrDrink() const { - return getFoodOrDrinkFlags() != 0; + return !getFoodOrDrinkFlags().isEmpty(); } /** @@ -1315,12 +1319,12 @@ namespace OpenRCT2 */ bool Guest::hasDrink() const { - return getItemFlags() & ShopItemsGetAllDrinks(); + return getItemFlags().hasAny(ShopItemsGetAllDrinks()); } bool Guest::hasEmptyContainer() const { - return getEmptyContainerFlags() != 0; + return !getEmptyContainerFlags().isEmpty(); } /** @@ -1456,7 +1460,7 @@ namespace OpenRCT2 const auto& shopItemDescriptor = GetShopItemDescriptor(shopItem); if (shopItemDescriptor.IsFoodOrDrink()) { - int32_t food = Numerics::bitScanForward(guest.getFoodOrDrinkFlags()); + int32_t food = Numerics::bitScanForward(guest.getFoodOrDrinkFlags().holder); if (food != -1) { guest.insertNewThought(PeepThoughtType::haventFinished, static_cast(food)); @@ -5438,7 +5442,7 @@ namespace OpenRCT2 if ((!getNextIsSurface()) && (static_cast(id.ToUnderlying() & 0x1FF) == (currentTicks & 0x1FF)) && ((0xFFFF & ScenarioRand()) <= 4096)) { - int32_t container = Numerics::bitScanForward(getEmptyContainerFlags()); + int32_t container = Numerics::bitScanForward(getEmptyContainerFlags().holder); auto litterType = Litter::Type::vomit; if (container != -1) @@ -5963,14 +5967,14 @@ namespace OpenRCT2 // This counts down 2 = No rubbish, 0 = full uint8_t spaceLeftInBin = 0x3 & (foundElement->getAdditionStatus() >> selectedBin); - uint64_t emptyContainers = getEmptyContainerFlags(); + const auto emptyContainers = getEmptyContainerFlags(); for (uint8_t curContainer = 0; curContainer < 64; curContainer++) { - if (!(emptyContainers & (1uLL << curContainer))) + auto item = ShopItem(curContainer); + if (!emptyContainers.has(item)) continue; - auto item = ShopItem(curContainer); if (spaceLeftInBin != 0) { // OpenRCT2 modification: This previously used @@ -7545,31 +7549,31 @@ namespace OpenRCT2 } } - uint64_t Guest::getItemFlags() const + ShopItems Guest::getItemFlags() const { return itemFlags; } void Guest::setItemFlags(uint64_t value) { - itemFlags = value; + itemFlags = ShopItems(value); } void Guest::removeAllItems() { - itemFlags = 0; + itemFlags.clearAll(); timeToConsume = 0; } void Guest::removeItem(ShopItem item) { - itemFlags &= ~EnumToFlag(item); + itemFlags.unset(item); timeToConsume = 0; } void Guest::giveItem(ShopItem item) { - itemFlags |= EnumToFlag(item); + itemFlags.set(item); const auto& shopItemDescriptor = GetShopItemDescriptor(item); uint16_t consumptionTime = shopItemDescriptor.ConsumptionTime; @@ -7578,7 +7582,7 @@ namespace OpenRCT2 bool Guest::hasItem(ShopItem peepItem) const { - return getItemFlags() & EnumToFlag(peepItem); + return getItemFlags().has(peepItem); } static bool IsThoughtShopItemRelated(const PeepThoughtType type) @@ -7763,6 +7767,6 @@ namespace OpenRCT2 stream << hatColour; stream << favouriteRide; stream << favouriteRideRating; - stream << itemFlags; + stream << itemFlags.holder; } } // namespace OpenRCT2 diff --git a/src/openrct2/entity/Guest.h b/src/openrct2/entity/Guest.h index 9b4578d27c..b956c4f3a2 100644 --- a/src/openrct2/entity/Guest.h +++ b/src/openrct2/entity/Guest.h @@ -348,12 +348,12 @@ namespace OpenRCT2 Drawing::Colour hatColour; RideId favouriteRide; uint8_t favouriteRideRating; - uint64_t itemFlags; + ShopItems itemFlags; void update(); void tick128UpdateGuest(uint32_t index); - uint64_t getFoodOrDrinkFlags() const; - uint64_t getEmptyContainerFlags() const; + ShopItems getFoodOrDrinkFlags() const; + ShopItems getEmptyContainerFlags() const; bool hasDrink() const; bool hasFoodOrDrink() const; bool hasEmptyContainer() const; @@ -383,7 +383,7 @@ namespace OpenRCT2 bool updateQueuePosition(PeepActionType previous_action); void removeFromQueue(); - uint64_t getItemFlags() const; + ShopItems getItemFlags() const; void setItemFlags(uint64_t value); void removeAllItems(); void removeItem(ShopItem item); diff --git a/src/openrct2/park/ParkFile.cpp b/src/openrct2/park/ParkFile.cpp index 46bf563a76..2227c288dc 100644 --- a/src/openrct2/park/ParkFile.cpp +++ b/src/openrct2/park/ParkFile.cpp @@ -1917,7 +1917,7 @@ namespace OpenRCT2 return true; }); RideUse::GetTypeHistory().Set(guest->id, LegacyGetRideTypesBeenOn(rideTypeBeenOn)); - cs.readWrite(guest->itemFlags); + cs.readWrite(guest->itemFlags.holder); cs.readWrite(guest->photo2RideRef); cs.readWrite(guest->photo3RideRef); cs.readWrite(guest->photo4RideRef); @@ -2471,7 +2471,7 @@ namespace OpenRCT2 cs.readWrite(guest.hatColour); cs.readWrite(guest.favouriteRide); cs.readWrite(guest.favouriteRideRating); - cs.readWrite(guest.itemFlags); + cs.readWrite(guest.itemFlags.holder); } template<> diff --git a/src/openrct2/ride/ShopItem.cpp b/src/openrct2/ride/ShopItem.cpp index 0ea641e92f..9a7a0e2c81 100644 --- a/src/openrct2/ride/ShopItem.cpp +++ b/src/openrct2/ride/ShopItem.cpp @@ -25,7 +25,7 @@ ShopItem& operator++(ShopItem& d, int) // clang-format off /** rct2: 0x00982164 (cost, base value, hot and cold value); 0x00982358 (default price) */ -constexpr ShopItemDescriptor ShopItems[EnumValue(ShopItem::count)] = { +constexpr ShopItemDescriptor kShopItems[EnumValue(ShopItem::count)] = { // Item, Cost, Base value, Hot value, Cold value, Default price, Image, Price label, Singular, Plural, Indefinite, Display (in guest inventory), Shop Item Flag, Litter type, Consumption time, Discard Container, Peep thought price too much, Peep thought price good value, /* ShopItem::balloon */ { 0.30_GBP, 1.40_GBP, 1.40_GBP, 1.40_GBP, 0.90_GBP, SPR_SHOP_ITEM_BALLOON, { STR_SHOP_ITEM_PRICE_LABEL_BALLOON, STR_SHOP_ITEM_SINGULAR_BALLOON, STR_SHOP_ITEM_PLURAL_BALLOON, STR_SHOP_ITEM_INDEFINITE_BALLOON, STR_SHOP_ITEM_DISPLAY_BALLOON }, SHOP_ITEM_FLAG_IS_SOUVENIR | SHOP_ITEM_FLAG_IS_RECOLOURABLE, Litter::Type::rubbish, 0, ShopItem::none, PeepThoughtType::balloonMuch, PeepThoughtType::balloon }, /* ShopItem::toy */ { 1.50_GBP, 3.00_GBP, 3.00_GBP, 3.00_GBP, 2.50_GBP, SPR_SHOP_ITEM_TOY, { STR_SHOP_ITEM_PRICE_LABEL_CUDDLY_TOY, STR_SHOP_ITEM_SINGULAR_CUDDLY_TOY, STR_SHOP_ITEM_PLURAL_CUDDLY_TOY, STR_SHOP_ITEM_INDEFINITE_CUDDLY_TOY, STR_SHOP_ITEM_DISPLAY_CUDDLY_TOY }, SHOP_ITEM_FLAG_IS_SOUVENIR, Litter::Type::rubbish, 0, ShopItem::none, PeepThoughtType::toyMuch, PeepThoughtType::toy }, @@ -84,34 +84,34 @@ constexpr ShopItemDescriptor ShopItems[EnumValue(ShopItem::count)] = { }; // clang-format on -constexpr uint64_t GetAllShopItemsWithFlag(uint16_t flag) +constexpr ShopItems GetAllShopItemsWithFlag(uint16_t flag) { - uint64_t ret = 0; + ShopItems ret{}; for (auto i = 0; i < EnumValue(ShopItem::count); ++i) { - const auto& sid = ShopItems[i]; + const auto& sid = kShopItems[i]; if (sid.HasFlag(flag)) { - ret |= (1uLL << i); + ret.set(static_cast(i)); } } return ret; } constexpr auto AllFoodFlags = GetAllShopItemsWithFlag(SHOP_ITEM_FLAG_IS_FOOD); -uint64_t ShopItemsGetAllFoods() +ShopItems ShopItemsGetAllFoods() { return AllFoodFlags; } constexpr auto AllDrinkFlags = GetAllShopItemsWithFlag(SHOP_ITEM_FLAG_IS_DRINK); -uint64_t ShopItemsGetAllDrinks() +ShopItems ShopItemsGetAllDrinks() { return AllDrinkFlags; } constexpr auto AllContainerFlags = GetAllShopItemsWithFlag(SHOP_ITEM_FLAG_IS_CONTAINER); -uint64_t ShopItemsGetAllContainers() +ShopItems ShopItemsGetAllContainers() { return AllContainerFlags; } @@ -148,7 +148,7 @@ money64 ShopItemGetCommonPrice(Ride* forRide, const ShopItem shopItem) bool ShopItemHasCommonPrice(const ShopItem shopItem) { - return (getGameState().park.samePriceThroughoutPark & EnumToFlag(shopItem)) != 0; + return ShopItems(getGameState().park.samePriceThroughoutPark).has(shopItem); } bool ShopItemDescriptor::IsFood() const @@ -183,5 +183,5 @@ bool ShopItemDescriptor::IsRecolourable() const const ShopItemDescriptor& GetShopItemDescriptor(const ShopItem item) { - return ShopItems[EnumValue(item)]; + return kShopItems[EnumValue(item)]; } diff --git a/src/openrct2/ride/ShopItem.h b/src/openrct2/ride/ShopItem.h index d445c87d2c..f7661871c2 100644 --- a/src/openrct2/ride/ShopItem.h +++ b/src/openrct2/ride/ShopItem.h @@ -9,6 +9,7 @@ #pragma once +#include "../core/FlagHolder.hpp" #include "../core/Money.hpp" #include "../entity/Litter.h" @@ -75,6 +76,7 @@ enum class ShopItem : uint8_t count = 56, none = 255 }; +using ShopItems = FlagHolder; ShopItem& operator++(ShopItem& d, int); @@ -117,9 +119,9 @@ struct ShopItemDescriptor bool IsRecolourable() const; }; -uint64_t ShopItemsGetAllFoods(); -uint64_t ShopItemsGetAllDrinks(); -uint64_t ShopItemsGetAllContainers(); +ShopItems ShopItemsGetAllFoods(); +ShopItems ShopItemsGetAllDrinks(); +ShopItems ShopItemsGetAllContainers(); enum {