diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj
index ba645f2a93..7af0b34048 100644
--- a/src/openrct2/libopenrct2.vcxproj
+++ b/src/openrct2/libopenrct2.vcxproj
@@ -494,6 +494,7 @@
+
diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp
index b8fb46a882..0c173a3b56 100644
--- a/src/openrct2/rct1/S4Importer.cpp
+++ b/src/openrct2/rct1/S4Importer.cpp
@@ -63,6 +63,7 @@
#include "../world/SmallScenery.h"
#include "../world/Sprite.h"
#include "../world/Surface.h"
+#include "../world/TilePointerIndex.hpp"
#include "../world/Wall.h"
#include "RCT1.h"
#include "Tables.h"
@@ -1535,7 +1536,8 @@ namespace RCT1
gMapBaseZ = 7;
// Build tile pointer cache (needed to get the first element at a certain location)
- auto tilePointerIndex = TilePointerIndex(RCT1_MAX_MAP_SIZE, _s4.tile_elements);
+ auto tilePointerIndex = TilePointerIndex(
+ RCT1_MAX_MAP_SIZE, _s4.tile_elements, std::size(_s4.tile_elements));
std::vector tileElements;
for (TileCoordsXY coords = { 0, 0 }; coords.y < MAXIMUM_MAP_SIZE_TECHNICAL; coords.y++)
diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp
index 928ac83368..352fbe78fa 100644
--- a/src/openrct2/rct2/S6Importer.cpp
+++ b/src/openrct2/rct2/S6Importer.cpp
@@ -69,6 +69,7 @@
#include "../world/Scenery.h"
#include "../world/Sprite.h"
#include "../world/Surface.h"
+#include "../world/TilePointerIndex.hpp"
#include
#include
@@ -1084,7 +1085,8 @@ public:
void ImportTileElements()
{
// Build tile pointer cache (needed to get the first element at a certain location)
- auto tilePointerIndex = TilePointerIndex(RCT2_MAXIMUM_MAP_SIZE_TECHNICAL, _s6.tile_elements);
+ auto tilePointerIndex = TilePointerIndex(
+ RCT2_MAXIMUM_MAP_SIZE_TECHNICAL, _s6.tile_elements, std::size(_s6.tile_elements));
std::vector tileElements;
bool nextElementInvisible = false;
diff --git a/src/openrct2/world/Map.cpp b/src/openrct2/world/Map.cpp
index 4c12b2b578..35dbf7ad7c 100644
--- a/src/openrct2/world/Map.cpp
+++ b/src/openrct2/world/Map.cpp
@@ -43,6 +43,7 @@
#include "../scenario/Scenario.h"
#include "../util/Util.h"
#include "../windows/Intent.h"
+#include "../world/TilePointerIndex.hpp"
#include "Banner.h"
#include "Climate.h"
#include "Footpath.h"
@@ -146,7 +147,7 @@ const std::vector& GetTileElements()
void SetTileElements(std::vector&& tileElements)
{
_tileElements = std::move(tileElements);
- _tileIndex = TilePointerIndex(MAXIMUM_MAP_SIZE_TECHNICAL, _tileElements.data());
+ _tileIndex = TilePointerIndex(MAXIMUM_MAP_SIZE_TECHNICAL, _tileElements.data(), _tileElements.size());
_tileElementsInUse = _tileElements.size();
}
diff --git a/src/openrct2/world/Map.h b/src/openrct2/world/Map.h
index 8c63223d01..e142044f15 100644
--- a/src/openrct2/world/Map.h
+++ b/src/openrct2/world/Map.h
@@ -144,43 +144,6 @@ constexpr auto SURFACE_STYLE_FLAG_RAISE_OR_LOWER_BASE_HEIGHT = 0x20;
extern const uint8_t tile_element_lower_styles[9][32];
extern const uint8_t tile_element_raise_styles[9][32];
-template class TilePointerIndex
-{
- std::vector TilePointers;
- uint16_t MapSize{};
-
-public:
- TilePointerIndex() = default;
-
- explicit TilePointerIndex(const uint16_t mapSize, T* tileElements)
- {
- MapSize = mapSize;
- const uint16_t MaxTileElementPointers = MapSize * MapSize;
- TilePointers.reserve(MaxTileElementPointers);
-
- T* tileElement = tileElements;
- for (size_t y = 0; y < MapSize; y++)
- {
- for (size_t x = 0; x < MapSize; x++)
- {
- TilePointers.emplace_back(tileElement);
- while (!(tileElement++)->IsLastForTile())
- ;
- }
- }
- }
-
- T* GetFirstElementAt(TileCoordsXY coords)
- {
- return TilePointers[coords.x + (coords.y * MapSize)];
- }
-
- void SetTile(TileCoordsXY coords, T* tileElement)
- {
- TilePointers[coords.x + (coords.y * MapSize)] = tileElement;
- }
-};
-
void ReorganiseTileElements();
const std::vector& GetTileElements();
void SetTileElements(std::vector&& tileElements);
diff --git a/src/openrct2/world/TilePointerIndex.hpp b/src/openrct2/world/TilePointerIndex.hpp
new file mode 100644
index 0000000000..1493dd38d7
--- /dev/null
+++ b/src/openrct2/world/TilePointerIndex.hpp
@@ -0,0 +1,53 @@
+/*****************************************************************************
+ * Copyright (c) 2014-2021 OpenRCT2 developers
+ *
+ * For a complete list of all authors, please refer to contributors.md
+ * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
+ *
+ * OpenRCT2 is licensed under the GNU General Public License version 3.
+ *****************************************************************************/
+
+#pragma once
+
+#include
+#include
+#include
+
+template class TilePointerIndex
+{
+ std::vector TilePointers;
+ uint16_t MapSize{};
+
+public:
+ TilePointerIndex() = default;
+
+ explicit TilePointerIndex(const uint16_t mapSize, T* tileElements, size_t count)
+ {
+ MapSize = mapSize;
+ TilePointers.reserve(MapSize * MapSize);
+
+ size_t index = 0;
+ for (size_t y = 0; y < MapSize; y++)
+ {
+ for (size_t x = 0; x < MapSize; x++)
+ {
+ assert(index < count);
+ TilePointers.emplace_back(&tileElements[index]);
+ do
+ {
+ index++;
+ } while (!tileElements[index - 1].IsLastForTile());
+ }
+ }
+ }
+
+ T* GetFirstElementAt(TileCoordsXY coords)
+ {
+ return TilePointers[coords.x + (coords.y * MapSize)];
+ }
+
+ void SetTile(TileCoordsXY coords, T* tileElement)
+ {
+ TilePointers[coords.x + (coords.y * MapSize)] = tileElement;
+ }
+};