From e898e9211d2f484f14f0a3558ae6233282c9f341 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Wed, 27 Mar 2024 20:13:10 +0100 Subject: [PATCH] Move kObjectEntryGroupCounts to ObjectList.cpp --- .../windows/EditorInventionsList.cpp | 5 +++-- .../windows/EditorObjectSelection.cpp | 4 ++-- src/openrct2/Context.cpp | 2 +- src/openrct2/Editor.cpp | 2 +- src/openrct2/EditorObjectSelectionSession.cpp | 6 ++--- src/openrct2/EditorObjectSelectionSession.h | 2 +- src/openrct2/interface/InteractiveConsole.cpp | 7 +++--- src/openrct2/object/Object.h | 2 -- src/openrct2/object/ObjectList.cpp | 22 +++++++++++++++---- src/openrct2/object/ObjectList.h | 4 +++- src/openrct2/object/ObjectManager.cpp | 11 +++++----- src/openrct2/rct1/S4Importer.cpp | 2 +- src/openrct2/ride/Ride.cpp | 2 +- .../bindings/object/ScObjectManager.cpp | 6 ++--- src/openrct2/world/MapGen.cpp | 4 ++-- 15 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/openrct2-ui/windows/EditorInventionsList.cpp b/src/openrct2-ui/windows/EditorInventionsList.cpp index 749f1cb23f..1319afcb74 100644 --- a/src/openrct2-ui/windows/EditorInventionsList.cpp +++ b/src/openrct2-ui/windows/EditorInventionsList.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -84,8 +85,8 @@ static Widget _inventionListDragWidgets[] = { // Reset all objects to not required for (auto objectType : getTransientObjectTypes()) { - auto maxObjects = object_entry_group_counts[EnumValue(objectType)]; - for (int32_t i = 0; i < maxObjects; i++) + auto maxObjects = getObjectEntryGroupCount(objectType); + for (auto i = 0u; i < maxObjects; i++) { Editor::ClearSelectedObject(objectType, i, ObjectSelectionFlags::AllFlags); } diff --git a/src/openrct2-ui/windows/EditorObjectSelection.cpp b/src/openrct2-ui/windows/EditorObjectSelection.cpp index 32a2b797d2..f7b516803b 100644 --- a/src/openrct2-ui/windows/EditorObjectSelection.cpp +++ b/src/openrct2-ui/windows/EditorObjectSelection.cpp @@ -1048,8 +1048,8 @@ static std::vector _window_editor_object_selection_widgets = { { auto screenPos = windowPos + ScreenCoordsXY{ 3, height - 13 }; - int32_t numSelected = _numSelectedObjectsForType[EnumValue(GetSelectedObjectType())]; - int32_t totalSelectable = object_entry_group_counts[EnumValue(GetSelectedObjectType())]; + auto numSelected = _numSelectedObjectsForType[EnumValue(GetSelectedObjectType())]; + auto totalSelectable = getObjectEntryGroupCount(GetSelectedObjectType()); auto ft = Formatter(); ft.Add(numSelected); diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index a3bf35234f..aee148a5fc 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -808,7 +808,7 @@ namespace OpenRCT2 { for (auto objectType : ObjectTypes) { - auto maxObjectsOfType = static_cast(object_entry_group_counts[EnumValue(objectType)]); + auto maxObjectsOfType = static_cast(getObjectEntryGroupCount(objectType)); for (ObjectEntryIndex i = 0; i < maxObjectsOfType; i++) { auto obj = _objectManager->GetLoadedObject(objectType, i); diff --git a/src/openrct2/Editor.cpp b/src/openrct2/Editor.cpp index 174f7ad753..6d7274568c 100644 --- a/src/openrct2/Editor.cpp +++ b/src/openrct2/Editor.cpp @@ -562,7 +562,7 @@ namespace Editor { if (index != OBJECT_ENTRY_INDEX_NULL) { - assert(static_cast(objectType) < object_entry_group_counts[EnumValue(ObjectType::Paths)]); + assert(index < getObjectEntryGroupCount(ObjectType::Paths)); auto& list = _editorSelectedObjectFlags[EnumValue(objectType)]; if (list.size() <= index) { diff --git a/src/openrct2/EditorObjectSelectionSession.cpp b/src/openrct2/EditorObjectSelectionSession.cpp index 951d7df7a1..869084f587 100644 --- a/src/openrct2/EditorObjectSelectionSession.cpp +++ b/src/openrct2/EditorObjectSelectionSession.cpp @@ -36,7 +36,7 @@ std::optional _gSceneryGroupPartialSelectError; std::vector _objectSelectionFlags; -int32_t _numSelectedObjectsForType[EnumValue(ObjectType::Count)]; +uint32_t _numSelectedObjectsForType[EnumValue(ObjectType::Count)]; static int32_t _numAvailableObjectsForType[EnumValue(ObjectType::Count)]; static void SetupInUseSelectionFlags(); @@ -127,7 +127,7 @@ void SetupInUseSelectionFlags() for (auto objectType : getTransientObjectTypes()) { - for (int32_t i = 0; i < object_entry_group_counts[EnumValue(objectType)]; i++) + for (auto i = 0u; i < getObjectEntryGroupCount(objectType); i++) { Editor::ClearSelectedObject(static_cast(objectType), i, ObjectSelectionFlags::AllFlags); @@ -580,7 +580,7 @@ ResultWithMessage WindowEditorObjectSelectionSelectObject( } ObjectType objectType = item->Type; - uint16_t maxObjects = object_entry_group_counts[EnumValue(objectType)]; + auto maxObjects = getObjectEntryGroupCount(objectType); if (maxObjects <= _numSelectedObjectsForType[EnumValue(objectType)]) { diff --git a/src/openrct2/EditorObjectSelectionSession.h b/src/openrct2/EditorObjectSelectionSession.h index b7194011f7..aa05d05b03 100644 --- a/src/openrct2/EditorObjectSelectionSession.h +++ b/src/openrct2/EditorObjectSelectionSession.h @@ -27,7 +27,7 @@ enum EDITOR_INPUT_FLAGS extern std::optional _gSceneryGroupPartialSelectError; extern std::vector _objectSelectionFlags; -extern int32_t _numSelectedObjectsForType[EnumValue(ObjectType::Count)]; +extern uint32_t _numSelectedObjectsForType[EnumValue(ObjectType::Count)]; bool EditorCheckObjectGroupAtLeastOneSelected(ObjectType checkObjectType); bool EditorCheckObjectGroupAtLeastOneSurfaceSelected(bool queue); diff --git a/src/openrct2/interface/InteractiveConsole.cpp b/src/openrct2/interface/InteractiveConsole.cpp index 3692ce8ba4..d3249ae854 100644 --- a/src/openrct2/interface/InteractiveConsole.cpp +++ b/src/openrct2/interface/InteractiveConsole.cpp @@ -1313,8 +1313,8 @@ static int32_t ConsoleCommandCountObjects(InteractiveConsole& console, [[maybe_u { for (auto objectType : ObjectTypes) { - int32_t entryGroupIndex = 0; - for (; entryGroupIndex < object_entry_group_counts[EnumValue(objectType)]; entryGroupIndex++) + uint32_t entryGroupIndex = 0; + for (; entryGroupIndex < getObjectEntryGroupCount(objectType); entryGroupIndex++) { if (ObjectEntryGetObject(objectType, entryGroupIndex) == nullptr) { @@ -1322,8 +1322,7 @@ static int32_t ConsoleCommandCountObjects(InteractiveConsole& console, [[maybe_u } } console.WriteFormatLine( - "%s: %d/%d", _objectTypeNames[EnumValue(objectType)], entryGroupIndex, - object_entry_group_counts[EnumValue(objectType)]); + "%s: %d/%d", _objectTypeNames[EnumValue(objectType)], entryGroupIndex, getObjectEntryGroupCount(objectType)); } return 0; diff --git a/src/openrct2/object/Object.h b/src/openrct2/object/Object.h index c5a397328a..06dba26287 100644 --- a/src/openrct2/object/Object.h +++ b/src/openrct2/object/Object.h @@ -342,8 +342,6 @@ public: # pragma GCC diagnostic pop #endif -extern int32_t object_entry_group_counts[]; - int32_t ObjectCalculateChecksum(const RCTObjectEntry* entry, const void* data, size_t dataLength); void ObjectCreateIdentifierName(char* string_buffer, size_t size, const RCTObjectEntry* object); diff --git a/src/openrct2/object/ObjectList.cpp b/src/openrct2/object/ObjectList.cpp index 3a447fe9ce..7c542b30b6 100644 --- a/src/openrct2/object/ObjectList.cpp +++ b/src/openrct2/object/ObjectList.cpp @@ -14,6 +14,7 @@ #include "../object/Object.h" #include "../util/SawyerCoding.h" #include "../util/Util.h" +#include "ObjectLimits.h" #include "ObjectManager.h" #include "ObjectRepository.h" @@ -21,8 +22,8 @@ #include #include -// 98DA00 -int32_t object_entry_group_counts[] = { +// 0x0098DA00 +static constexpr std::array kObjectEntryGroupCounts = { MAX_RIDE_OBJECTS, // rides MAX_SMALL_SCENERY_OBJECTS, // small scenery MAX_LARGE_SCENERY_OBJECTS, // large scenery @@ -42,7 +43,20 @@ int32_t object_entry_group_counts[] = { MAX_FOOTPATH_RAILINGS_OBJECTS, MAX_AUDIO_OBJECTS, }; -static_assert(std::size(object_entry_group_counts) == EnumValue(ObjectType::Count)); +static_assert(std::size(kObjectEntryGroupCounts) == EnumValue(ObjectType::Count)); + +size_t getObjectEntryGroupCount(ObjectType objectType) +{ + return kObjectEntryGroupCounts[EnumValue(objectType)]; +} + +size_t getObjectTypeLimit(ObjectType type) +{ + auto index = EnumValue(type); + if (index >= EnumValue(ObjectType::Count)) + return 0; + return static_cast(kObjectEntryGroupCounts[index]); +} ObjectList::const_iterator::const_iterator(const ObjectList* parent, bool end) { @@ -186,7 +200,7 @@ void ObjectCreateIdentifierName(char* string_buffer, size_t size, const RCTObjec void ObjectGetTypeEntryIndex(size_t index, ObjectType* outObjectType, ObjectEntryIndex* outEntryIndex) { uint8_t objectType = EnumValue(ObjectType::Ride); - for (size_t groupCount : object_entry_group_counts) + for (size_t groupCount : kObjectEntryGroupCounts) { if (index >= groupCount) { diff --git a/src/openrct2/object/ObjectList.h b/src/openrct2/object/ObjectList.h index 362c9aede1..3cc725cf10 100644 --- a/src/openrct2/object/ObjectList.h +++ b/src/openrct2/object/ObjectList.h @@ -10,7 +10,6 @@ #pragma once #include "Object.h" -#include "ObjectLimits.h" #include @@ -51,3 +50,6 @@ public: }; void ObjectGetTypeEntryIndex(size_t index, ObjectType* outObjectType, ObjectEntryIndex* outEntryIndex); + +size_t getObjectEntryGroupCount(ObjectType objectType); +size_t getObjectTypeLimit(ObjectType type); diff --git a/src/openrct2/object/ObjectManager.cpp b/src/openrct2/object/ObjectManager.cpp index 418286555b..9164b7654a 100644 --- a/src/openrct2/object/ObjectManager.cpp +++ b/src/openrct2/object/ObjectManager.cpp @@ -21,6 +21,7 @@ #include "BannerSceneryEntry.h" #include "LargeSceneryObject.h" #include "Object.h" +#include "ObjectLimits.h" #include "ObjectList.h" #include "ObjectRepository.h" #include "PathAdditionObject.h" @@ -79,7 +80,7 @@ public: return nullptr; } - if (index >= static_cast(object_entry_group_counts[EnumValue(objectType)])) + if (index >= static_cast(getObjectEntryGroupCount(objectType))) { #ifdef DEBUG if (index != OBJECT_ENTRY_INDEX_NULL) @@ -144,7 +145,7 @@ public: ObjectList objectList; for (auto objectType : ObjectTypes) { - auto maxObjectsOfType = static_cast(object_entry_group_counts[EnumValue(objectType)]); + auto maxObjectsOfType = static_cast(getObjectEntryGroupCount(objectType)); for (ObjectEntryIndex i = 0; i < maxObjectsOfType; i++) { auto obj = GetLoadedObject(objectType, i); @@ -384,7 +385,7 @@ private: return static_cast(std::distance(list.begin(), it)); } - auto maxSize = object_entry_group_counts[EnumValue(objectType)]; + auto maxSize = getObjectEntryGroupCount(objectType); if (list.size() < static_cast(maxSize)) { list.emplace_back(); @@ -524,7 +525,7 @@ private: for (auto objectType : ObjectTypes) { auto& descriptors = objectList.GetList(objectType); - auto maxSize = static_cast(object_entry_group_counts[EnumValue(objectType)]); + auto maxSize = static_cast(getObjectEntryGroupCount(objectType)); auto listSize = static_cast(std::min(descriptors.size(), maxSize)); for (ObjectEntryIndex i = 0; i < listSize; i++) { @@ -724,7 +725,7 @@ private: } // Build object lists - const auto maxRideObjects = static_cast(object_entry_group_counts[EnumValue(ObjectType::Ride)]); + const auto maxRideObjects = static_cast(getObjectEntryGroupCount(ObjectType::Ride)); for (size_t i = 0; i < maxRideObjects; i++) { auto* rideObject = static_cast(GetLoadedObject(ObjectType::Ride, i)); diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index 69ebdcaff1..f7027e4fd6 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -570,7 +570,7 @@ namespace RCT1 RCT12::EntryList* entries = GetEntryList(objectType); // Check if there are spare entries available - size_t maxEntries = static_cast(object_entry_group_counts[EnumValue(objectType)]); + size_t maxEntries = static_cast(getObjectEntryGroupCount(objectType)); if (entries != nullptr && entries->GetCount() < maxEntries) { entries->GetOrAddEntry(objectName); diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 26782267d4..db44c4ceaa 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -257,7 +257,7 @@ const RideObjectEntry* GetRideEntryByIndex(ObjectEntryIndex index) std::string_view GetRideEntryName(ObjectEntryIndex index) { - if (index >= object_entry_group_counts[EnumValue(ObjectType::Ride)]) + if (index >= getObjectEntryGroupCount(ObjectType::Ride)) { LOG_ERROR("invalid index %d for ride type", index); return {}; diff --git a/src/openrct2/scripting/bindings/object/ScObjectManager.cpp b/src/openrct2/scripting/bindings/object/ScObjectManager.cpp index cab4d4c1c0..cd38f55b18 100644 --- a/src/openrct2/scripting/bindings/object/ScObjectManager.cpp +++ b/src/openrct2/scripting/bindings/object/ScObjectManager.cpp @@ -106,7 +106,7 @@ DukValue ScObjectManager::load(const DukValue& p1, const DukValue& p2) throw DukException() << "Expected number for 'index'."; auto index = static_cast(p2.as_int()); - auto limit = GetObjectTypeLimit(installedObject->Type); + auto limit = getObjectTypeLimit(installedObject->Type); if (index < limit) { auto loadedObject = objectManager.GetLoadedObject(installedObject->Type, index); @@ -214,8 +214,8 @@ std::vector ScObjectManager::getAllObjects(const std::string& typez) c auto type = ScObject::StringToObjectType(typez); if (type) { - auto count = object_entry_group_counts[EnumValue(*type)]; - for (int32_t i = 0; i < count; i++) + auto count = getObjectEntryGroupCount(*type); + for (auto i = 0u; i < count; i++) { auto obj = objManager.GetLoadedObject(*type, i); if (obj != nullptr) diff --git a/src/openrct2/world/MapGen.cpp b/src/openrct2/world/MapGen.cpp index c035acd20c..389301afed 100644 --- a/src/openrct2/world/MapGen.cpp +++ b/src/openrct2/world/MapGen.cpp @@ -18,8 +18,8 @@ #include "../core/String.hpp" #include "../localisation/Localisation.h" #include "../localisation/StringIds.h" -#include "../object/Object.h" #include "../object/ObjectEntryManager.h" +#include "../object/ObjectList.h" #include "../object/ObjectManager.h" #include "../object/SmallSceneryEntry.h" #include "../object/TerrainEdgeObject.h" @@ -311,7 +311,7 @@ static void MapGenPlaceTrees() std::vector desertTreeIds; std::vector snowTreeIds; - for (int32_t i = 0; i < object_entry_group_counts[EnumValue(ObjectType::SmallScenery)]; i++) + for (auto i = 0u; i < getObjectEntryGroupCount(ObjectType::SmallScenery); i++) { auto* sceneryEntry = OpenRCT2::ObjectManager::GetObjectEntry(i); auto entry = ObjectEntryGetObject(ObjectType::SmallScenery, i);