OutpostUniverse / OP2Utility

C++ library for working with Outpost 2 related files and tasks.
MIT License
4 stars 0 forks source link

Improve documentation of tile indexing in Outpost 2 #332

Open Brett208 opened 4 years ago

Brett208 commented 4 years ago

Could we add more detail to the Map class explaining how tile indexing works? It could be placed in the map header or within the GetTileIndex function.

I wanted to write a function that allows changing the map size dynamically, but to do that, I need to understand the indexing better.

// Helper method to handle complex tile data indexing
std::size_t Map::GetTileIndex(std::size_t x, std::size_t y) const
{
    auto lowerX = x & 0x1F; // ... 0001 1111
    auto upperX = x >> 5;   // ... 1110 0000
    return (upperX * heightInTiles + y) * 32 + lowerX;
}
DanRStevens commented 4 years ago

Good idea.

The tiles are stored in columns, each 32 tiles wide. You end up with an array of columns, where each column is stored in normal array order.

Conceptually it's a bit like:

numColumns = mapWidth / 32;
TileData tileData[numColumns][mapHeight][32];

columnIndex = x / 32;
columnX = x % 32;
auto tile = tileData[columnIndex][y][columnX];