From c95a3be2435a9d15b354934a54de63f1fbc87f51 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 15 Jan 2023 00:04:53 +0000 Subject: [PATCH] Change: Expose ObjectSpec vector to simplify iteration. (cherry picked from commit 51b112139235c66c3adb2c3a75919fc1d3eff9fc) --- src/newgrf_object.cpp | 5 +++++ src/newgrf_object.h | 1 + src/object_cmd.cpp | 17 ++++++++--------- src/object_gui.cpp | 9 ++++----- src/script/api/script_objecttypelist.cpp | 7 +++---- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/newgrf_object.cpp b/src/newgrf_object.cpp index 7825a2b743..4ef12435b5 100644 --- a/src/newgrf_object.cpp +++ b/src/newgrf_object.cpp @@ -33,6 +33,11 @@ extern const ObjectSpec _original_objects[NEW_OBJECT_OFFSET]; /** All the object specifications. */ std::vector _object_specs; +const std::vector &ObjectSpec::Specs() +{ + return _object_specs; +} + size_t ObjectSpec::Count() { return _object_specs.size(); diff --git a/src/newgrf_object.h b/src/newgrf_object.h index 2f41c3572a..5b6da81c19 100644 --- a/src/newgrf_object.h +++ b/src/newgrf_object.h @@ -134,6 +134,7 @@ struct ObjectSpec { bool IsAvailable() const; uint Index() const; + static const std::vector &Specs(); static size_t Count(); static const ObjectSpec *Get(ObjectType index); static const ObjectSpec *GetByTile(TileIndex tile); diff --git a/src/object_cmd.cpp b/src/object_cmd.cpp index e971f5acdb..430e4bde00 100644 --- a/src/object_cmd.cpp +++ b/src/object_cmd.cpp @@ -1101,21 +1101,20 @@ void GenerateObjects() } /* Iterate over all possible object types */ - for (uint i = 0; i < ObjectSpec::Count(); i++) { - const ObjectSpec *spec = ObjectSpec::Get(i); + for (const auto &spec : ObjectSpec::Specs()) { /* Continue, if the object was never available till now or shall not be placed */ - if (!spec->WasEverAvailable() || spec->generate_amount == 0) continue; + if (!spec.WasEverAvailable() || spec.generate_amount == 0) continue; - uint16 amount = spec->generate_amount; + uint16 amount = spec.generate_amount; /* Scale by map size */ - if ((spec->flags & OBJECT_FLAG_SCALE_BY_WATER) && _settings_game.construction.freeform_edges) { + if ((spec.flags & OBJECT_FLAG_SCALE_BY_WATER) && _settings_game.construction.freeform_edges) { /* Scale the amount of lighthouses with the amount of land at the borders. * The -6 is because the top borders are MP_VOID (-2) and all corners * are counted twice (-4). */ amount = ScaleByMapSize1D(amount * num_water_tiles) / (2 * MapMaxY() + 2 * MapMaxX() - 6); - } else if (spec->flags & OBJECT_FLAG_SCALE_BY_WATER) { + } else if (spec.flags & OBJECT_FLAG_SCALE_BY_WATER) { amount = ScaleByMapSize1D(amount); } else { amount = ScaleByMapSize(amount); @@ -1123,7 +1122,7 @@ void GenerateObjects() /* Now try to place the requested amount of this object */ for (uint j = ScaleByMapSize(1000); j != 0 && amount != 0 && Object::CanAllocateItem(); j--) { - switch (i) { + switch (spec.Index()) { case OBJECT_TRANSMITTER: if (TryBuildTransmitter()) amount--; break; @@ -1133,8 +1132,8 @@ void GenerateObjects() break; default: - uint8 view = RandomRange(spec->views); - if (CmdBuildObject(RandomTile(), DC_EXEC | DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, i, view, nullptr).Succeeded()) amount--; + uint8 view = RandomRange(spec.views); + if (CmdBuildObject(RandomTile(), DC_EXEC | DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, spec.Index(), view, nullptr).Succeeded()) amount--; break; } } diff --git a/src/object_gui.cpp b/src/object_gui.cpp index 0f2206f16c..968aa1ee8b 100644 --- a/src/object_gui.cpp +++ b/src/object_gui.cpp @@ -273,11 +273,10 @@ public: uint height[2] = {0, 0}; // The height for the different views; in this case views 1/2 and 4. /* Get the height and view information. */ - for (int i = 0; i < ObjectSpec::Count(); i++) { - const ObjectSpec *spec = ObjectSpec::Get(i); - if (!spec->IsEverAvailable()) continue; - two_wide |= spec->views >= 2; - height[spec->views / 4] = std::max(spec->height, height[spec->views / 4]); + for (const auto &spec : ObjectSpec::Specs()) { + if (!spec.IsEverAvailable()) continue; + two_wide |= spec.views >= 2; + height[spec.views / 4] = std::max(spec.height, height[spec.views / 4]); } /* Determine the pixel heights. */ diff --git a/src/script/api/script_objecttypelist.cpp b/src/script/api/script_objecttypelist.cpp index 0bb52b0576..725997067b 100644 --- a/src/script/api/script_objecttypelist.cpp +++ b/src/script/api/script_objecttypelist.cpp @@ -15,9 +15,8 @@ ScriptObjectTypeList::ScriptObjectTypeList() { - for (int i = 0; i < ObjectSpec::Count(); i++) { - const ObjectSpec *spec = ObjectSpec::Get(i); - if (!spec->IsEverAvailable()) continue; - this->AddItem(i); + for (const auto &spec : ObjectSpec::Specs()) { + if (!spec.IsEverAvailable()) continue; + this->AddItem(spec.Index()); } }