From c452b97ad6dfe88c40857d2dbcb1a26017c9aebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ty=C3=A1s=20Mustoha?= Date: Thu, 17 Oct 2024 17:47:37 +0200 Subject: [PATCH] Optimized the tile storage in world maps --- src/common/CMakeLists.txt | 1 + src/common/util/Grid.h | 60 +++++++++ src/smw/ui/MI_World.cpp | 10 +- src/smw/world.cpp | 186 +++++++++++++--------------- src/smw/world.h | 3 +- src/worldeditor/worldeditor.cpp | 208 ++++++++++++++++---------------- 6 files changed, 258 insertions(+), 210 deletions(-) create mode 100644 src/common/util/Grid.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index a636b30b..807ff205 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -71,6 +71,7 @@ add_library(CommonFiles STATIC Version.h WorldTourStop.cpp WorldTourStop.h + util/Grid.h ) add_subdirectory(ui) diff --git a/src/common/util/Grid.h b/src/common/util/Grid.h new file mode 100644 index 00000000..1024ac9e --- /dev/null +++ b/src/common/util/Grid.h @@ -0,0 +1,60 @@ +#pragma once + +#include +#include +#include + + +template +class Grid { +private: + size_t width = 0; + size_t height = 0; + std::vector data; + +public: + Grid(size_t w, size_t h) + : width(w) + , height(h) + , data(w * h) + {} + Grid() : Grid(0, 0) {}; + + size_t cols() const { return width; } + size_t rows() const { return height; } + size_t empty() const { return data.empty(); } + size_t size() const { return data.size(); } + + void clear() { + data.clear(); + height = 0; + width = 0; + } + + void fill(const T& val) { + data = decltype(data)(width * height, val); + } + + void swap(Grid& other) noexcept { + std::swap(width, other.width); + std::swap(height, other.height); + data.swap(other.data); + } + + T& at(size_t x, size_t y) { + assert(y * width + x < data.size()); + return data[y * width + x]; + } + const T& at(size_t x, size_t y) const { + assert(y * width + x < data.size()); + return data[y * width + x]; + } + + auto begin() noexcept { return data.begin(); } + auto begin() const noexcept { return data.begin(); } + auto cbegin() const noexcept { return data.cbegin(); } + + auto end() noexcept { return data.end(); } + auto end() const noexcept { return data.end(); } + auto cend() const noexcept { return data.cend(); } +}; diff --git a/src/smw/ui/MI_World.cpp b/src/smw/ui/MI_World.cpp index 1cc505cf..f1d908ce 100644 --- a/src/smw/ui/MI_World.cpp +++ b/src/smw/ui/MI_World.cpp @@ -154,7 +154,7 @@ void MI_World::SetCurrentStageToCompleted(short iWinningTeam) } else { const Vec2s iPlayerCurrentTile = g_worldmap.GetPlayerCurrentTile(); - WorldMapTile * tile = &g_worldmap.tiles[iPlayerCurrentTile.x][iPlayerCurrentTile.y]; + WorldMapTile * tile = &g_worldmap.tiles.at(iPlayerCurrentTile.x, iPlayerCurrentTile.y); //tile->iForegroundSprite = game_values.colorids[game_values.teamids[iWinningTeam][0]] + WORLD_WINNING_TEAM_SPRITE_OFFSET; //Update with team completed sprite //tile->fAnimated = false; //Update with team completed sprite tile->iCompleted = game_values.colorids[game_values.teamids[iWinningTeam][0]]; @@ -619,7 +619,7 @@ MenuCodeEnum MI_World::SendInput(CPlayerInput * playerInput) if (iState == -1 && iNumPopups == 0 && iPopupFlag[0] == false && iPopupFlag[1] == false && iPopupFlag[2] == false && iPopupFlag[3] == false) { if (iControllingTeam == LookupTeamID(iPlayer) && iPlayerState == 0 && game_values.playercontrol[iPlayer] > 0) { //if this player is player or cpu - WorldMapTile * tile = &g_worldmap.tiles[iPlayerCurrentTile.x][iPlayerCurrentTile.y]; + WorldMapTile * tile = &g_worldmap.tiles.at(iPlayerCurrentTile.x, iPlayerCurrentTile.y); short iTemp; //Just a temp value so we can call the GetVehicleInPlayerTile method bool fVehicleInTile = g_worldmap.GetVehicleInPlayerTile(&iTemp) >= 0; @@ -714,7 +714,7 @@ MenuCodeEnum MI_World::SendInput(CPlayerInput * playerInput) } //if it is a stage, then load the stage - WorldMapTile * tile = &g_worldmap.tiles[iPlayerCurrentTile.x][iPlayerCurrentTile.y]; + WorldMapTile * tile = &g_worldmap.tiles.at(iPlayerCurrentTile.x, iPlayerCurrentTile.y); short iType = tile->iType - 6; if (iType >= 0 && iType < g_worldmap.iNumStages && tile->iCompleted == -2) { @@ -892,7 +892,7 @@ bool MI_World::UsePowerup(short iPlayer, short iTeam, short iIndex, bool fPopupI } } else if (iPowerup == NUM_POWERUPS + 3 && iState == -1) { //Advance Turn const Vec2s iDest = g_worldmap.GetPlayerDestTile(); - short iSprite = g_worldmap.tiles[iDest.x][iDest.y].iForegroundSprite; + short iSprite = g_worldmap.tiles.at(iDest.x, iDest.y).iForegroundSprite; if (iSprite < WORLD_BRIDGE_SPRITE_OFFSET || iSprite > WORLD_BRIDGE_SPRITE_OFFSET + 3) { AdvanceTurn(); @@ -901,7 +901,7 @@ bool MI_World::UsePowerup(short iPlayer, short iTeam, short iIndex, bool fPopupI } } else if (iPowerup == NUM_POWERUPS + 4 && iState == -1) { //Revive stage const Vec2s iDest = g_worldmap.GetPlayerDestTile(); - WorldMapTile * tile = &g_worldmap.tiles[iDest.x][iDest.y]; + WorldMapTile * tile = &g_worldmap.tiles.at(iDest.x, iDest.y); if (tile->iType >= 6 && tile->iCompleted >= 0) { tile->iCompleted = -2; diff --git a/src/smw/world.cpp b/src/smw/world.cpp index 4c51c4d0..4ccc1fe0 100644 --- a/src/smw/world.cpp +++ b/src/smw/world.cpp @@ -251,7 +251,7 @@ void WorldVehicle::SetNextDest() if (iState != 0 || iMaxMoves == 0) return; - WorldMapTile * tile = &g_worldmap.tiles[currentTile.x][currentTile.y]; + WorldMapTile * tile = &g_worldmap.tiles.at(currentTile.x, currentTile.y); const Vec2s iPlayerCurrentTile = g_worldmap.GetPlayerCurrentTile(); if (iNumMoves-- <= 0) { @@ -362,15 +362,10 @@ Vec2s WorldWarp::getOtherSide(Vec2s target) const **********************************/ WorldMap::WorldMap(short w, short h) + : iWidth(w) + , iHeight(h) + , tiles(w, h) { - iWidth = w; - iHeight = h; - - tiles.clear(); - tiles.resize(iWidth); - for (short iCol = 0; iCol < iWidth; iCol++) - tiles[iCol].resize(iHeight); - ResetTourStops(); // FIXME } @@ -429,10 +424,7 @@ WorldMap::WorldMap(const std::string& path, short tilesize) iHeight = std::stoi(line); iReadType = 4; - tiles.clear(); - tiles.resize(iWidth); - for (short iCol = 0; iCol < iWidth; iCol++) - tiles[iCol].resize(iHeight); + tiles = decltype(tiles)(iWidth, iHeight); short iDrawSurfaceTiles = iWidth * iHeight; @@ -446,7 +438,7 @@ WorldMap::WorldMap(const std::string& path, short tilesize) goto RETURN; for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); tile.iBackgroundWater = popNextInt(tokens); } @@ -460,7 +452,7 @@ WorldMap::WorldMap(const std::string& path, short tilesize) goto RETURN; for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); tile.iBackgroundSprite = popNextInt(tokens); tile.fAnimated = (tile.iBackgroundSprite % WORLD_BACKGROUND_SPRITE_SET_SIZE) != 1; @@ -479,7 +471,7 @@ WorldMap::WorldMap(const std::string& path, short tilesize) goto RETURN; for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); tile.iForegroundSprite = popNextInt(tokens); short iForegroundSprite = tile.iForegroundSprite; @@ -509,7 +501,7 @@ WorldMap::WorldMap(const std::string& path, short tilesize) goto RETURN; for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); tile.iConnectionType = popNextInt(tokens); } @@ -533,7 +525,7 @@ WorldMap::WorldMap(const std::string& path, short tilesize) goto RETURN; for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); tile.iType = popNextInt(tokens); tile.iWarp = -1; @@ -556,7 +548,7 @@ WorldMap::WorldMap(const std::string& path, short tilesize) goto RETURN; for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); tile.iVehicleBoundary = popNextInt(tokens); } @@ -581,7 +573,7 @@ WorldMap::WorldMap(const std::string& path, short tilesize) short iMaxStage = game_values.tourstops.size() + 5; for (short iRow = 0; iRow < iHeight; iRow++) { for (short iCol = 0; iCol < iWidth; iCol++) { - short iType = tiles[iCol][iRow].iType; + short iType = tiles.at(iCol, iRow).iType; if (iType < 0 || iType > iMaxStage) goto RETURN; } @@ -610,8 +602,8 @@ WorldMap::WorldMap(const std::string& path, short tilesize) short warpId = warps.size(); warps.emplace_back(WorldWarp(warpId, {iCol1, iRow1}, {iCol2, iRow2})); - tiles[iCol1][iRow1].iWarp = warpId; - tiles[iCol2][iRow2].iWarp = warpId; + tiles.at(iCol1, iRow1).iWarp = warpId; + tiles.at(iCol2, iRow2).iWarp = warpId; if (warps.size() >= iNumWarps) iReadType = 14; @@ -697,13 +689,13 @@ void WorldMap::SetTileConnections(short iCol, short iRow) if (iCol < 0 || iRow < 0 || iCol >= iWidth || iRow >= iHeight) return; - WorldMapTile& tile = tiles[iCol][iRow]; + WorldMapTile& tile = tiles.at(iCol, iRow); for (short iDirection = 0; iDirection < 4; iDirection++) tile.fConnection[iDirection] = false; if (iRow > 0) { - const WorldMapTile& topTile = tiles[iCol][iRow - 1]; + const WorldMapTile& topTile = tiles.at(iCol, iRow - 1); tile.fConnection[0] = (topTile.iConnectionType == 1 || topTile.iConnectionType == 5 || topTile.iConnectionType == 6 || topTile.iConnectionType == 7 || topTile.iConnectionType == 9 || topTile.iConnectionType == 10 || @@ -714,7 +706,7 @@ void WorldMap::SetTileConnections(short iCol, short iRow) } if (iRow < iHeight - 1) { - const WorldMapTile& bottomTile = tiles[iCol][iRow + 1]; + const WorldMapTile& bottomTile = tiles.at(iCol, iRow + 1); tile.fConnection[1] = (bottomTile.iConnectionType == 1 || bottomTile.iConnectionType == 3 || bottomTile.iConnectionType == 4 || bottomTile.iConnectionType == 7 || bottomTile.iConnectionType == 8 || bottomTile.iConnectionType == 9 || @@ -725,7 +717,7 @@ void WorldMap::SetTileConnections(short iCol, short iRow) } if (iCol > 0) { - const WorldMapTile& leftTile = tiles[iCol - 1][iRow]; + const WorldMapTile& leftTile = tiles.at(iCol - 1, iRow); tile.fConnection[2] = (leftTile.iConnectionType == 2 || leftTile.iConnectionType == 4 || leftTile.iConnectionType == 5 || leftTile.iConnectionType == 8 || leftTile.iConnectionType == 9 || leftTile.iConnectionType == 10 || @@ -735,7 +727,7 @@ void WorldMap::SetTileConnections(short iCol, short iRow) } if (iCol < iWidth - 1) { - const WorldMapTile& rightTile = tiles[iCol + 1][iRow]; + const WorldMapTile& rightTile = tiles.at(iCol + 1, iRow); tile.fConnection[3] = (rightTile.iConnectionType == 2 || rightTile.iConnectionType == 3 || rightTile.iConnectionType == 6 || rightTile.iConnectionType == 7 || rightTile.iConnectionType == 8 || rightTile.iConnectionType == 10 || @@ -772,7 +764,7 @@ bool WorldMap::Save(const std::string& szPath) const for (short iMapTileReadRow = 0; iMapTileReadRow < iHeight; iMapTileReadRow++) { for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - const WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + const WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); fprintf(file, "%d", tile.iBackgroundWater); if (iMapTileReadCol == iWidth - 1) @@ -787,7 +779,7 @@ bool WorldMap::Save(const std::string& szPath) const for (short iMapTileReadRow = 0; iMapTileReadRow < iHeight; iMapTileReadRow++) { for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - const WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + const WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); fprintf(file, "%d", tile.iBackgroundSprite); if (iMapTileReadCol == iWidth - 1) @@ -802,7 +794,7 @@ bool WorldMap::Save(const std::string& szPath) const for (short iMapTileReadRow = 0; iMapTileReadRow < iHeight; iMapTileReadRow++) { for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - const WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + const WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); fprintf(file, "%d", tile.iForegroundSprite); if (iMapTileReadCol == iWidth - 1) @@ -817,7 +809,7 @@ bool WorldMap::Save(const std::string& szPath) const for (short iMapTileReadRow = 0; iMapTileReadRow < iHeight; iMapTileReadRow++) { for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - const WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + const WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); fprintf(file, "%d", tile.iConnectionType); if (iMapTileReadCol == iWidth - 1) @@ -832,7 +824,7 @@ bool WorldMap::Save(const std::string& szPath) const for (short iMapTileReadRow = 0; iMapTileReadRow < iHeight; iMapTileReadRow++) { for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - const WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + const WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); fprintf(file, "%d", tile.iType); if (iMapTileReadCol == iWidth - 1) @@ -847,7 +839,7 @@ bool WorldMap::Save(const std::string& szPath) const for (short iMapTileReadRow = 0; iMapTileReadRow < iHeight; iMapTileReadRow++) { for (short iMapTileReadCol = 0; iMapTileReadCol < iWidth; iMapTileReadCol++) { - const WorldMapTile& tile = tiles[iMapTileReadCol][iMapTileReadRow]; + const WorldMapTile& tile = tiles.at(iMapTileReadCol, iMapTileReadRow); fprintf(file, "%d", tile.iVehicleBoundary); if (iMapTileReadCol == iWidth - 1) @@ -933,12 +925,9 @@ bool WorldMap::Save(const std::string& szPath) const void WorldMap::Clear() { - for (size_t col = 0; col < tiles.size(); col++) { - std::vector& column = tiles[col]; - - for (size_t row = 0; row < column.size(); row++) { - WorldMapTile& tile = column[row]; - + for (size_t row = 0; row < tiles.rows(); row++) { + for (size_t col = 0; col < tiles.cols(); col++) { + WorldMapTile& tile = tiles.at(col, row); tile.iBackgroundSprite = 0; tile.iBackgroundWater = 0; tile.iForegroundSprite = 0; @@ -957,38 +946,35 @@ void WorldMap::Clear() //Resizes world keeping intact current tiles (if possible) void WorldMap::Resize(short w, short h) { - //Copy tiles from old map - std::vector> tempTiles; - tempTiles.swap(tiles); - short iOldWidth = iWidth; short iOldHeight = iHeight; //Create new map iWidth = w; iHeight = h; - - tiles.resize(iWidth); + Grid newTiles(w, h); //Copy tiles to new map for (short iCol = 0; iCol < iWidth; iCol++) { - tiles[iCol].resize(iHeight); - for (short iRow = 0; iRow < iHeight; iRow++) { - if (iCol < iOldWidth && iRow < iOldHeight) - tiles[iCol][iRow] = tempTiles[iCol][iRow]; - else { - tiles[iCol][iRow].iBackgroundSprite = 0; - tiles[iCol][iRow].iBackgroundWater = 0; - tiles[iCol][iRow].iForegroundSprite = 0; - tiles[iCol][iRow].iConnectionType = 0; - tiles[iCol][iRow].iType = 0; - tiles[iCol][iRow].iID = iRow * iWidth + iCol; - tiles[iCol][iRow].iVehicleBoundary = 0; - tiles[iCol][iRow].iWarp = 0; + WorldMapTile& newTile = newTiles.at(iCol, iRow); + if (iCol < iOldWidth && iRow < iOldHeight) { + newTile = std::move(tiles.at(iCol, iRow)); + } else { + newTile.iBackgroundSprite = 0; + newTile.iBackgroundWater = 0; + newTile.iForegroundSprite = 0; + newTile.iConnectionType = 0; + newTile.iType = 0; + newTile.iID = iRow * iWidth + iCol; + newTile.iVehicleBoundary = 0; + newTile.iWarp = 0; } } } + + // Apply the new tiles + tiles = std::move(newTiles); } bool WorldMap::Update(bool * fPlayerVehicleCollision) @@ -1076,7 +1062,7 @@ void WorldMap::DrawMapToSurface(short iCycleIndex, bool fFullRefresh, SDL_Surfac void WorldMap::DrawTileToSurface(SDL_Surface* surface, short iCol, short iRow, short iMapDrawOffsetCol, short iMapDrawOffsetRow, bool fFullRefresh, short iAnimationFrame, short iLayer) const { - const WorldMapTile& tile = tiles[iCol + iMapDrawOffsetCol][iRow + iMapDrawOffsetRow]; + const WorldMapTile& tile = tiles.at(iCol + iMapDrawOffsetCol, iRow + iMapDrawOffsetRow); if (!tile.fAnimated && !fFullRefresh) return; @@ -1251,7 +1237,7 @@ short WorldMap::GetVehicleStageScore(short iVehicleIndex) const bool WorldMap::GetWarpInPlayerTile(short * iWarpCol, short * iWarpRow) const { - short iWarp = tiles[player.currentTile.x][player.currentTile.y].iWarp; + short iWarp = tiles.at(player.currentTile.x, player.currentTile.y).iWarp; if (iWarp < 0) return false; @@ -1265,47 +1251,47 @@ void WorldMap::MoveBridges() { for (short iRow = 0; iRow < iHeight; iRow++) { for (short iCol = 0; iCol < iWidth; iCol++) { - if (tiles[iCol][iRow].iConnectionType == 12) { - tiles[iCol][iRow].iConnectionType = 13; + if (tiles.at(iCol, iRow).iConnectionType == 12) { + tiles.at(iCol, iRow).iConnectionType = 13; SetTileConnections(iCol, iRow); SetTileConnections(iCol - 1, iRow); SetTileConnections(iCol + 1, iRow); - } else if (tiles[iCol][iRow].iConnectionType == 13) { - tiles[iCol][iRow].iConnectionType = 12; + } else if (tiles.at(iCol, iRow).iConnectionType == 13) { + tiles.at(iCol, iRow).iConnectionType = 12; SetTileConnections(iCol, iRow); SetTileConnections(iCol - 1, iRow); SetTileConnections(iCol + 1, iRow); - } else if (tiles[iCol][iRow].iConnectionType == 14) { - tiles[iCol][iRow].iConnectionType = 15; + } else if (tiles.at(iCol, iRow).iConnectionType == 14) { + tiles.at(iCol, iRow).iConnectionType = 15; SetTileConnections(iCol, iRow); SetTileConnections(iCol, iRow - 1); SetTileConnections(iCol, iRow + 1); - } else if (tiles[iCol][iRow].iConnectionType == 15) { - tiles[iCol][iRow].iConnectionType = 14; + } else if (tiles.at(iCol, iRow).iConnectionType == 15) { + tiles.at(iCol, iRow).iConnectionType = 14; SetTileConnections(iCol, iRow); SetTileConnections(iCol, iRow - 1); SetTileConnections(iCol, iRow + 1); } - if (tiles[iCol][iRow].iForegroundSprite == WORLD_BRIDGE_SPRITE_OFFSET) - tiles[iCol][iRow].iForegroundSprite = WORLD_BRIDGE_SPRITE_OFFSET + 1; - else if (tiles[iCol][iRow].iForegroundSprite == WORLD_BRIDGE_SPRITE_OFFSET + 1) - tiles[iCol][iRow].iForegroundSprite = WORLD_BRIDGE_SPRITE_OFFSET; - else if (tiles[iCol][iRow].iForegroundSprite == WORLD_BRIDGE_SPRITE_OFFSET + 2) - tiles[iCol][iRow].iForegroundSprite = WORLD_BRIDGE_SPRITE_OFFSET + 3; - else if (tiles[iCol][iRow].iForegroundSprite == WORLD_BRIDGE_SPRITE_OFFSET + 3) - tiles[iCol][iRow].iForegroundSprite = WORLD_BRIDGE_SPRITE_OFFSET + 2; + if (tiles.at(iCol, iRow).iForegroundSprite == WORLD_BRIDGE_SPRITE_OFFSET) + tiles.at(iCol, iRow).iForegroundSprite = WORLD_BRIDGE_SPRITE_OFFSET + 1; + else if (tiles.at(iCol, iRow).iForegroundSprite == WORLD_BRIDGE_SPRITE_OFFSET + 1) + tiles.at(iCol, iRow).iForegroundSprite = WORLD_BRIDGE_SPRITE_OFFSET; + else if (tiles.at(iCol, iRow).iForegroundSprite == WORLD_BRIDGE_SPRITE_OFFSET + 2) + tiles.at(iCol, iRow).iForegroundSprite = WORLD_BRIDGE_SPRITE_OFFSET + 3; + else if (tiles.at(iCol, iRow).iForegroundSprite == WORLD_BRIDGE_SPRITE_OFFSET + 3) + tiles.at(iCol, iRow).iForegroundSprite = WORLD_BRIDGE_SPRITE_OFFSET + 2; } } } void WorldMap::IsTouchingDoor(short iCol, short iRow, bool doors[4]) const { - const WorldMapTile& tile = tiles[iCol][iRow]; + const WorldMapTile& tile = tiles.at(iCol, iRow); if (iCol > 0) { if (tile.iCompleted >= -1) { - short iType = tiles[iCol - 1][iRow].iType - 2; + short iType = tiles.at(iCol - 1, iRow).iType - 2; if (iType >= 0 && iType <= 3) doors[iType] = true; @@ -1314,7 +1300,7 @@ void WorldMap::IsTouchingDoor(short iCol, short iRow, bool doors[4]) const if (iCol < iWidth - 1) { if (tile.iCompleted >= -1) { - short iType = tiles[iCol + 1][iRow].iType - 2; + short iType = tiles.at(iCol + 1, iRow).iType - 2; if (iType >= 0 && iType <= 3) doors[iType] = true; @@ -1323,7 +1309,7 @@ void WorldMap::IsTouchingDoor(short iCol, short iRow, bool doors[4]) const if (iRow > 0) { if (tile.iCompleted >= -1) { - short iType = tiles[iCol][iRow - 1].iType - 2; + short iType = tiles.at(iCol, iRow - 1).iType - 2; if (iType >= 0 && iType <= 3) doors[iType] = true; @@ -1332,7 +1318,7 @@ void WorldMap::IsTouchingDoor(short iCol, short iRow, bool doors[4]) const if (iCol < iHeight - 1) { if (tile.iCompleted >= -1) { - short iType = tiles[iCol][iRow + 1].iType - 2; + short iType = tiles.at(iCol, iRow + 1).iType - 2; if (iType >= 0 && iType <= 3) doors[iType] = true; @@ -1343,7 +1329,7 @@ void WorldMap::IsTouchingDoor(short iCol, short iRow, bool doors[4]) const bool WorldMap::IsDoor(short iCol, short iRow) const { if (iCol >= 0 && iRow >= 0 && iCol < iWidth && iRow < iHeight) { - short iType = tiles[iCol][iRow].iType; + short iType = tiles.at(iCol, iRow).iType; if (iType >= 2 && iType <= 5) return true; } @@ -1355,32 +1341,32 @@ short WorldMap::UseKey(short iKeyType, short iCol, short iRow, bool fCloud) { short iDoorsOpened = 0; - const WorldMapTile& tile = tiles[iCol][iRow]; + const WorldMapTile& tile = tiles.at(iCol, iRow); if (iCol > 0) { - if ((tile.iCompleted >= -1 || fCloud) && tiles[iCol - 1][iRow].iType - 2 == iKeyType) { - tiles[iCol - 1][iRow].iType = 0; + if ((tile.iCompleted >= -1 || fCloud) && tiles.at(iCol - 1, iRow).iType - 2 == iKeyType) { + tiles.at(iCol - 1, iRow).iType = 0; iDoorsOpened |= 1; } } if (iCol < iWidth - 1) { - if ((tile.iCompleted >= -1 || fCloud) && tiles[iCol + 1][iRow].iType - 2 == iKeyType) { - tiles[iCol + 1][iRow].iType = 0; + if ((tile.iCompleted >= -1 || fCloud) && tiles.at(iCol + 1, iRow).iType - 2 == iKeyType) { + tiles.at(iCol + 1, iRow).iType = 0; iDoorsOpened |= 2; } } if (iRow > 0) { - if ((tile.iCompleted >= -1 || fCloud) && tiles[iCol][iRow - 1].iType - 2 == iKeyType) { - tiles[iCol][iRow - 1].iType = 0; + if ((tile.iCompleted >= -1 || fCloud) && tiles.at(iCol, iRow - 1).iType - 2 == iKeyType) { + tiles.at(iCol, iRow - 1).iType = 0; iDoorsOpened |= 4; } } if (iRow < iHeight - 1) { - if ((tile.iCompleted >= -1 || fCloud) && tiles[iCol][iRow + 1].iType - 2 == iKeyType) { - tiles[iCol][iRow + 1].iType = 0; + if ((tile.iCompleted >= -1 || fCloud) && tiles.at(iCol, iRow + 1).iType - 2 == iKeyType) { + tiles.at(iCol, iRow + 1).iType = 0; iDoorsOpened |= 8; } } @@ -1391,7 +1377,7 @@ short WorldMap::UseKey(short iKeyType, short iCol, short iRow, bool fCloud) short WorldMap::GetVehicleBoundary(short iCol, short iRow) const { if (iCol >= 0 && iRow >= 0 && iCol < iWidth && iRow < iHeight) { - return tiles[iCol][iRow].iVehicleBoundary; + return tiles.at(iCol, iRow).iVehicleBoundary; } return 0; @@ -1400,7 +1386,7 @@ short WorldMap::GetVehicleBoundary(short iCol, short iRow) const //Implements breadth first search to find a stage or vehicle of interest short WorldMap::GetNextInterestingMove(short iCol, short iRow) const { - const WorldMapTile& currentTile = tiles[iCol][iRow]; + const WorldMapTile& currentTile = tiles.at(iCol, iRow); //Look for stages or vehicles, but not bonus houses if ((currentTile.iType >= 6 && currentTile.iCompleted == -2) || NumVehiclesInTile({iCol, iRow}) > 0) @@ -1437,8 +1423,8 @@ short WorldMap::GetNextInterestingMove(short iCol, short iRow) const iBackTileId += 1; else if (iBackTileDirection == 4) { const Vec2s target(iBackTileId % iWidth, iBackTileId / iWidth); - const Vec2s pos = warps[tiles[iCol][iRow].iWarp].getOtherSide(target); - iBackTileId = tiles[pos.x][pos.y].iID; + const Vec2s pos = warps[tiles.at(iCol, iRow).iWarp].getOtherSide(target); + iBackTileId = tiles.at(pos.x, pos.y).iID; } if (iBackTileId == iCurrentId) { @@ -1457,7 +1443,7 @@ short WorldMap::GetNextInterestingMove(short iCol, short iRow) const for (short iNeighbor = 0; iNeighbor < 4; iNeighbor++) { if (tile->fConnection[iNeighbor]) { if (iNeighbor == 0 && tile->iRow > 0) { - const WorldMapTile& topTile = tiles[tile->iCol][tile->iRow - 1]; + const WorldMapTile& topTile = tiles.at(tile->iCol, tile->iRow - 1); //Stop at door tiles if (topTile.iType >= 2 && topTile.iType <= 5) @@ -1468,7 +1454,7 @@ short WorldMap::GetNextInterestingMove(short iCol, short iRow) const next.push(&topTile); } } else if (iNeighbor == 1 && tile->iRow < iHeight - 1) { - const WorldMapTile& bottomTile = tiles[tile->iCol][tile->iRow + 1]; + const WorldMapTile& bottomTile = tiles.at(tile->iCol, tile->iRow + 1); //Stop at door tiles if (bottomTile.iType >= 2 && bottomTile.iType <= 5) @@ -1479,7 +1465,7 @@ short WorldMap::GetNextInterestingMove(short iCol, short iRow) const next.push(&bottomTile); } } else if (iNeighbor == 2 && tile->iCol > 0) { - const WorldMapTile& leftTile = tiles[tile->iCol - 1][tile->iRow]; + const WorldMapTile& leftTile = tiles.at(tile->iCol - 1, tile->iRow); //Stop at door tiles if (leftTile.iType >= 2 && leftTile.iType <= 5) @@ -1490,7 +1476,7 @@ short WorldMap::GetNextInterestingMove(short iCol, short iRow) const next.push(&leftTile); } } else if (iNeighbor == 3 && tile->iCol < iWidth - 1) { - const WorldMapTile& rightTile = tiles[tile->iCol + 1][tile->iRow]; + const WorldMapTile& rightTile = tiles.at(tile->iCol + 1, tile->iRow); //Stop at door tiles if (rightTile.iType >= 2 && rightTile.iType <= 5) @@ -1505,7 +1491,7 @@ short WorldMap::GetNextInterestingMove(short iCol, short iRow) const if (tile->iWarp >= 0) { const Vec2s pos = warps[tile->iWarp].getOtherSide({tile->iCol, tile->iRow}); - const WorldMapTile& warpTile = tiles[pos.x][pos.y]; + const WorldMapTile& warpTile = tiles.at(pos.x, pos.y); //Stop at door tiles if (warpTile.iType >= 2 && warpTile.iType <= 5) diff --git a/src/smw/world.h b/src/smw/world.h index 0419e3da..d9a9e89b 100644 --- a/src/smw/world.h +++ b/src/smw/world.h @@ -3,6 +3,7 @@ #include "SDL.h" #include "math/Vec2.h" +#include "util/Grid.h" #include #include @@ -208,7 +209,7 @@ class WorldMap { short iNumStages = 0; - std::vector> tiles; + Grid tiles; WorldPlayer player; std::vector vehicles; std::vector warps; diff --git a/src/worldeditor/worldeditor.cpp b/src/worldeditor/worldeditor.cpp index d537e2c3..66ae7fa2 100644 --- a/src/worldeditor/worldeditor.cpp +++ b/src/worldeditor/worldeditor.cpp @@ -1362,10 +1362,10 @@ int editor_edit() if (iButtonX >= 0 && iButtonY >= 0 && iButtonX < iWorldWidth * TILESIZE && iButtonY < iWorldHeight * TILESIZE) { if (event.button.button == SDL_BUTTON_LEFT && !ignoreclick) { if (edit_mode == 0) { //selected background - if (g_worldmap.tiles[iCol][iRow].iBackgroundSprite != set_tile || fAutoPaint) { + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite != set_tile || fAutoPaint) { bool fNeedUpdate = false; - if (g_worldmap.tiles[iCol][iRow].iBackgroundSprite != set_tile) { - g_worldmap.tiles[iCol][iRow].iBackgroundSprite = set_tile; + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite != set_tile) { + g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite = set_tile; fNeedUpdate = true; } @@ -1378,33 +1378,33 @@ int editor_edit() updateworldsurface(); } } else if (edit_mode == 1) { //selected foreground - if (g_worldmap.tiles[iCol][iRow].iForegroundSprite != set_tile) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = set_tile; + if (g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != set_tile) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = set_tile; updateworldsurface(); if (set_tile >= WORLD_BRIDGE_SPRITE_OFFSET && set_tile <= WORLD_BRIDGE_SPRITE_OFFSET + 3) - g_worldmap.tiles[iCol][iRow].iConnectionType = set_tile - WORLD_BRIDGE_SPRITE_OFFSET + 12; + g_worldmap.tiles.at(iCol, iRow).iConnectionType = set_tile - WORLD_BRIDGE_SPRITE_OFFSET + 12; } } else if (edit_mode == 2) { //selected connection - g_worldmap.tiles[iCol][iRow].iConnectionType = set_tile; + g_worldmap.tiles.at(iCol, iRow).iConnectionType = set_tile; if (fAutoPaint) UpdatePath(iCol, iRow); } else if (edit_mode == 3) { //selected type //start tiles if (set_tile <= 1) { - if (g_worldmap.tiles[iCol][iRow].iForegroundSprite != set_tile + WORLD_START_SPRITE_OFFSET) { - g_worldmap.tiles[iCol][iRow].iType = 1; - g_worldmap.tiles[iCol][iRow].iForegroundSprite = set_tile + WORLD_START_SPRITE_OFFSET; + if (g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != set_tile + WORLD_START_SPRITE_OFFSET) { + g_worldmap.tiles.at(iCol, iRow).iType = 1; + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = set_tile + WORLD_START_SPRITE_OFFSET; updateworldsurface(); } } else if (set_tile <= 5) { //doors - if (g_worldmap.tiles[iCol][iRow].iType != set_tile) { + if (g_worldmap.tiles.at(iCol, iRow).iType != set_tile) { //if the door was placed on a start tile - if (g_worldmap.tiles[iCol][iRow].iType == 1) - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + if (g_worldmap.tiles.at(iCol, iRow).iType == 1) + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; - g_worldmap.tiles[iCol][iRow].iType = set_tile; + g_worldmap.tiles.at(iCol, iRow).iType = set_tile; updateworldsurface(); } } @@ -1412,8 +1412,8 @@ int editor_edit() short iAdjustedTile = AdjustForeground(set_tile, iCol, iRow); bool fNeedUpdate = false; - if (!fAutoPaint && g_worldmap.tiles[iCol][iRow].iForegroundSprite != iAdjustedTile) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = iAdjustedTile; + if (!fAutoPaint && g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != iAdjustedTile) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = iAdjustedTile; fNeedUpdate = true; } @@ -1421,7 +1421,7 @@ int editor_edit() if (fAutoPaint) { short iOldTiles[9]; GetForegroundTileValues(iCol, iRow, iOldTiles); - g_worldmap.tiles[iCol][iRow].iForegroundSprite = iAdjustedTile; + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = iAdjustedTile; UpdatePathSprite(iCol, iRow); if (ForegroundTileValuesChanged(iCol, iRow, iOldTiles)) @@ -1435,28 +1435,28 @@ int editor_edit() } else if (edit_mode == 6) { //selected warp AddWarpToTile(iCol, iRow, set_tile); } else if (edit_mode == 7) { //water - if (g_worldmap.tiles[iCol][iRow].iBackgroundWater != set_tile) { - g_worldmap.tiles[iCol][iRow].iBackgroundWater = set_tile; + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundWater != set_tile) { + g_worldmap.tiles.at(iCol, iRow).iBackgroundWater = set_tile; updateworldsurface(); } } else if (edit_mode == 8) { //boundary - g_worldmap.tiles[iCol][iRow].iVehicleBoundary = set_tile; + g_worldmap.tiles.at(iCol, iRow).iVehicleBoundary = set_tile; } else if (edit_mode == 9) { //if the stage was placed on a start tile - if (g_worldmap.tiles[iCol][iRow].iType == 1) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + if (g_worldmap.tiles.at(iCol, iRow).iType == 1) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; updateworldsurface(); } - g_worldmap.tiles[iCol][iRow].iType = set_tile; + g_worldmap.tiles.at(iCol, iRow).iType = set_tile; } } else if (event.button.button == SDL_BUTTON_RIGHT) { if (edit_mode == 0) { - if (g_worldmap.tiles[iCol][iRow].iBackgroundSprite != 0 || fAutoPaint) { + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite != 0 || fAutoPaint) { bool fNeedUpdate = false; - if (g_worldmap.tiles[iCol][iRow].iBackgroundSprite != 0) { - g_worldmap.tiles[iCol][iRow].iBackgroundSprite = 0; + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite != 0) { + g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite = 0; fNeedUpdate = true; } @@ -1469,30 +1469,30 @@ int editor_edit() updateworldsurface(); } } else if (edit_mode == 1) { - if (g_worldmap.tiles[iCol][iRow].iForegroundSprite != 0) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + if (g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != 0) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; updateworldsurface(); } } else if (edit_mode == 2) { - g_worldmap.tiles[iCol][iRow].iConnectionType = 0; + g_worldmap.tiles.at(iCol, iRow).iConnectionType = 0; if (fAutoPaint) UpdatePath(iCol, iRow); } else if (edit_mode == 3) { //selected start/door - if (g_worldmap.tiles[iCol][iRow].iType == 1) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + if (g_worldmap.tiles.at(iCol, iRow).iType == 1) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; updateworldsurface(); - } else if (g_worldmap.tiles[iCol][iRow].iType <= 5) { - g_worldmap.tiles[iCol][iRow].iType = 0; + } else if (g_worldmap.tiles.at(iCol, iRow).iType <= 5) { + g_worldmap.tiles.at(iCol, iRow).iType = 0; updateworldsurface(); } - g_worldmap.tiles[iCol][iRow].iType = 0; + g_worldmap.tiles.at(iCol, iRow).iType = 0; } else if (edit_mode == 4) { bool fNeedUpdate = false; - if (!fAutoPaint && g_worldmap.tiles[iCol][iRow].iForegroundSprite != 0) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + if (!fAutoPaint && g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != 0) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; fNeedUpdate = true; } @@ -1500,7 +1500,7 @@ int editor_edit() if (fAutoPaint) { short iOldTiles[9]; GetForegroundTileValues(iCol, iRow, iOldTiles); - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; UpdatePathSprite(iCol, iRow); if (ForegroundTileValuesChanged(iCol, iRow, iOldTiles)) @@ -1515,14 +1515,14 @@ int editor_edit() } else if (edit_mode == 6) { RemoveWarpFromTile(iCol, iRow); } else if (edit_mode == 7) { //water - if (g_worldmap.tiles[iCol][iRow].iBackgroundWater != 0) { - g_worldmap.tiles[iCol][iRow].iBackgroundWater = 0; + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundWater != 0) { + g_worldmap.tiles.at(iCol, iRow).iBackgroundWater = 0; updateworldsurface(); } } else if (edit_mode == 8) { //boundary - g_worldmap.tiles[iCol][iRow].iVehicleBoundary = 0; + g_worldmap.tiles.at(iCol, iRow).iVehicleBoundary = 0; } else if (edit_mode == 9) { //stage - g_worldmap.tiles[iCol][iRow].iType = 0; + g_worldmap.tiles.at(iCol, iRow).iType = 0; iStageDisplay = -1; } } @@ -1541,10 +1541,10 @@ int editor_edit() if (iButtonX >= 0 && iButtonY >= 0 && iButtonX < iWorldWidth * TILESIZE && iButtonY < iWorldHeight * TILESIZE) { if (event.motion.state == SDL_BUTTON(SDL_BUTTON_LEFT) && !ignoreclick) { if (edit_mode == 0) { //selected background - if (g_worldmap.tiles[iCol][iRow].iBackgroundSprite != set_tile || fAutoPaint) { + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite != set_tile || fAutoPaint) { bool fNeedUpdate = false; - if (g_worldmap.tiles[iCol][iRow].iBackgroundSprite != set_tile) { - g_worldmap.tiles[iCol][iRow].iBackgroundSprite = set_tile; + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite != set_tile) { + g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite = set_tile; fNeedUpdate = true; } @@ -1557,29 +1557,29 @@ int editor_edit() updateworldsurface(); } } else if (edit_mode == 1) { - if (g_worldmap.tiles[iCol][iRow].iForegroundSprite != set_tile) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = set_tile; + if (g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != set_tile) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = set_tile; updateworldsurface(); } } else if (edit_mode == 2) { - g_worldmap.tiles[iCol][iRow].iConnectionType = set_tile; + g_worldmap.tiles.at(iCol, iRow).iConnectionType = set_tile; if (fAutoPaint) UpdatePath(iCol, iRow); } else if (edit_mode == 3) { //selected stage/door if (set_tile <= 1) { - if (g_worldmap.tiles[iCol][iRow].iForegroundSprite != set_tile + WORLD_START_SPRITE_OFFSET) { - g_worldmap.tiles[iCol][iRow].iType = 1; - g_worldmap.tiles[iCol][iRow].iForegroundSprite = set_tile + WORLD_START_SPRITE_OFFSET; + if (g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != set_tile + WORLD_START_SPRITE_OFFSET) { + g_worldmap.tiles.at(iCol, iRow).iType = 1; + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = set_tile + WORLD_START_SPRITE_OFFSET; updateworldsurface(); } } else if (set_tile <= 5) { - if (g_worldmap.tiles[iCol][iRow].iType != set_tile) { + if (g_worldmap.tiles.at(iCol, iRow).iType != set_tile) { //if the door was placed on a start tile - if (g_worldmap.tiles[iCol][iRow].iType == 1) - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + if (g_worldmap.tiles.at(iCol, iRow).iType == 1) + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; - g_worldmap.tiles[iCol][iRow].iType = set_tile; + g_worldmap.tiles.at(iCol, iRow).iType = set_tile; updateworldsurface(); } } @@ -1587,8 +1587,8 @@ int editor_edit() short iAdjustedTile = AdjustForeground(set_tile, iCol, iRow); bool fNeedUpdate = false; - if (!fAutoPaint && g_worldmap.tiles[iCol][iRow].iForegroundSprite != iAdjustedTile) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = iAdjustedTile; + if (!fAutoPaint && g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != iAdjustedTile) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = iAdjustedTile; fNeedUpdate = true; } @@ -1596,7 +1596,7 @@ int editor_edit() if (fAutoPaint) { short iOldTiles[9]; GetForegroundTileValues(iCol, iRow, iOldTiles); - g_worldmap.tiles[iCol][iRow].iForegroundSprite = iAdjustedTile; + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = iAdjustedTile; UpdatePathSprite(iCol, iRow); if (ForegroundTileValuesChanged(iCol, iRow, iOldTiles)) @@ -1610,25 +1610,25 @@ int editor_edit() } else if (edit_mode == 6) { AddWarpToTile(iCol, iRow, set_tile); } else if (edit_mode == 7) { //water - if (g_worldmap.tiles[iCol][iRow].iBackgroundWater != set_tile) { - g_worldmap.tiles[iCol][iRow].iBackgroundWater = set_tile; + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundWater != set_tile) { + g_worldmap.tiles.at(iCol, iRow).iBackgroundWater = set_tile; updateworldsurface(); } } else if (edit_mode == 9) { //stage //if the stage was placed on a start tile - if (g_worldmap.tiles[iCol][iRow].iType == 1) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + if (g_worldmap.tiles.at(iCol, iRow).iType == 1) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; updateworldsurface(); } - g_worldmap.tiles[iCol][iRow].iType = set_tile; + g_worldmap.tiles.at(iCol, iRow).iType = set_tile; } } else if (event.motion.state == SDL_BUTTON(SDL_BUTTON_RIGHT)) { if (edit_mode == 0) { //selected background - if (g_worldmap.tiles[iCol][iRow].iBackgroundSprite != 0 || fAutoPaint) { + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite != 0 || fAutoPaint) { bool fNeedUpdate = false; - if (g_worldmap.tiles[iCol][iRow].iBackgroundSprite != 0) { - g_worldmap.tiles[iCol][iRow].iBackgroundSprite = 0; + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite != 0) { + g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite = 0; fNeedUpdate = true; } @@ -1641,30 +1641,30 @@ int editor_edit() updateworldsurface(); } } else if (edit_mode == 1) { - if (g_worldmap.tiles[iCol][iRow].iForegroundSprite != 0) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + if (g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != 0) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; updateworldsurface(); } } else if (edit_mode == 2) { - g_worldmap.tiles[iCol][iRow].iConnectionType = 0; + g_worldmap.tiles.at(iCol, iRow).iConnectionType = 0; if (fAutoPaint) UpdatePath(iCol, iRow); } else if (edit_mode == 3) { - if (g_worldmap.tiles[iCol][iRow].iType == 1) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + if (g_worldmap.tiles.at(iCol, iRow).iType == 1) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; updateworldsurface(); - } else if (g_worldmap.tiles[iCol][iRow].iType <= 5) { - g_worldmap.tiles[iCol][iRow].iType = 0; + } else if (g_worldmap.tiles.at(iCol, iRow).iType <= 5) { + g_worldmap.tiles.at(iCol, iRow).iType = 0; updateworldsurface(); } - g_worldmap.tiles[iCol][iRow].iType = 0; + g_worldmap.tiles.at(iCol, iRow).iType = 0; } else if (edit_mode == 4) { bool fNeedUpdate = false; - if (!fAutoPaint && g_worldmap.tiles[iCol][iRow].iForegroundSprite != 0) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + if (!fAutoPaint && g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != 0) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; fNeedUpdate = true; } @@ -1672,7 +1672,7 @@ int editor_edit() if (fAutoPaint) { short iOldTiles[9]; GetForegroundTileValues(iCol, iRow, iOldTiles); - g_worldmap.tiles[iCol][iRow].iForegroundSprite = 0; + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0; UpdatePathSprite(iCol, iRow); if (ForegroundTileValuesChanged(iCol, iRow, iOldTiles)) @@ -1687,12 +1687,12 @@ int editor_edit() } else if (edit_mode == 6) { //Warps RemoveWarpFromTile(iCol, iRow); } else if (edit_mode == 7) { //water - if (g_worldmap.tiles[iCol][iRow].iBackgroundWater != 0) { - g_worldmap.tiles[iCol][iRow].iBackgroundWater = 0; + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundWater != 0) { + g_worldmap.tiles.at(iCol, iRow).iBackgroundWater = 0; updateworldsurface(); } } else if (edit_mode == 9) { //stage - g_worldmap.tiles[iCol][iRow].iType = 0; + g_worldmap.tiles.at(iCol, iRow).iType = 0; iStageDisplay = -1; } } @@ -1715,7 +1715,7 @@ int editor_edit() } else if (edit_mode == 9) { iStageDisplay = -1; if (iCol >= 0 && iRow >= 0 && iCol < iWorldWidth && iRow < iWorldHeight) { - short iType = g_worldmap.tiles[iCol][iRow].iType - 6; + short iType = g_worldmap.tiles.at(iCol, iRow).iType - 6; if (iType >= 0) { iStageDisplay = iType; } @@ -1776,7 +1776,7 @@ int editor_edit() if (edit_mode == 2) { for (short iRow = draw_offset_row; iRow < draw_offset_row + 15 && iRow < iWorldHeight; iRow++) { for (short iCol = draw_offset_col; iCol <= draw_offset_col + 20 && iCol < iWorldWidth; iCol++) { - short iConnection = g_worldmap.tiles[iCol][iRow].iConnectionType; + short iConnection = g_worldmap.tiles.at(iCol, iRow).iConnectionType; if (iConnection > 0) spr_path.draw((iCol - draw_offset_col) * TILESIZE + draw_offset_x, (iRow - draw_offset_row) * TILESIZE + draw_offset_y, (iConnection - 1) << 5, 0, TILESIZE, TILESIZE); @@ -1809,7 +1809,7 @@ int editor_edit() int color = SDL_MapRGB(blitdest->format, 255, 0, 255); for (short iRow = draw_offset_row; iRow < draw_offset_row + 15 && iRow < iWorldHeight; iRow++) { for (short iCol = draw_offset_col; iCol <= draw_offset_col + 20 && iCol < iWorldWidth; iCol++) { - short iBoundary = g_worldmap.tiles[iCol][iRow].iVehicleBoundary - 1; + short iBoundary = g_worldmap.tiles.at(iCol, iRow).iVehicleBoundary - 1; if (iBoundary >= 0) { short ix = (iCol - draw_offset_col) * TILESIZE + draw_offset_x; @@ -1825,7 +1825,7 @@ int editor_edit() int color = SDL_MapRGB(blitdest->format, 0, 0, 255); for (short iRow = draw_offset_row; iRow < draw_offset_row + 15 && iRow < iWorldHeight; iRow++) { for (short iCol = draw_offset_col; iCol <= draw_offset_col + 20 && iCol < iWorldWidth; iCol++) { - short iType = g_worldmap.tiles[iCol][iRow].iType - 6; + short iType = g_worldmap.tiles.at(iCol, iRow).iType - 6; if (iType >= 0) { short ix = (iCol - draw_offset_col) * TILESIZE + draw_offset_x; @@ -1932,7 +1932,7 @@ void GetForegroundTileValues(short iCol, short iRow, short iOldTiles[9]) for (short iAutoRow = iRow - 1; iAutoRow <= iRow + 1; iAutoRow++) { for (short iAutoCol = iCol - 1; iAutoCol <= iCol + 1; iAutoCol++) { if (iAutoRow >= 0 && iAutoRow < iWorldHeight && iAutoCol >= 0 && iAutoCol < iWorldWidth) { - iOldTiles[iIndex++] = g_worldmap.tiles[iAutoCol][iAutoRow].iForegroundSprite; + iOldTiles[iIndex++] = g_worldmap.tiles.at(iAutoCol, iAutoRow).iForegroundSprite; } } } @@ -1947,7 +1947,7 @@ bool ForegroundTileValuesChanged(short iCol, short iRow, short iOldTiles[9]) for (short iAutoRow = iRow - 1; iAutoRow <= iRow + 1; iAutoRow++) { for (short iAutoCol = iCol - 1; iAutoCol <= iCol + 1; iAutoCol++) { if (iAutoRow >= 0 && iAutoRow < iWorldHeight && iAutoCol >= 0 && iAutoCol < iWorldWidth) { - if (g_worldmap.tiles[iAutoCol][iAutoRow].iForegroundSprite != iOldTiles[iIndex++]) + if (g_worldmap.tiles.at(iAutoCol, iAutoRow).iForegroundSprite != iOldTiles[iIndex++]) return true; } } @@ -2159,7 +2159,7 @@ void AutoSetPathSprite(short iCol, short iRow) short iPath = 0; short iNeighborIndex = 0; - short iForegroundSprite = g_worldmap.tiles[iCol][iRow].iForegroundSprite; + short iForegroundSprite = g_worldmap.tiles.at(iCol, iRow).iForegroundSprite; short iForegroundStyle = iForegroundSprite / WORLD_PATH_SPRITE_SET_SIZE; if (iForegroundSprite == 0 || iForegroundSprite >= WORLD_FOREGROUND_STAGE_OFFSET) @@ -2172,7 +2172,7 @@ void AutoSetPathSprite(short iCol, short iRow) if ((iAutoCol == iCol && iAutoRow != iRow) || (iAutoCol != iCol && iAutoRow == iRow)) { if (iAutoRow >= 0 && iAutoRow < iWorldHeight && iAutoCol >= 0 && iAutoCol < iWorldWidth) { - iForegroundSprite = g_worldmap.tiles[iAutoCol][iAutoRow].iForegroundSprite; + iForegroundSprite = g_worldmap.tiles.at(iAutoCol, iAutoRow).iForegroundSprite; if ((iForegroundSprite >= WORLD_BRIDGE_SPRITE_OFFSET && iForegroundSprite <= WORLD_BRIDGE_SPRITE_OFFSET + 3) || (iForegroundSprite >= WORLD_FOREGROUND_STAGE_OFFSET && iForegroundSprite <= WORLD_FOREGROUND_STAGE_OFFSET + 399) || @@ -2194,7 +2194,7 @@ void AutoSetPathSprite(short iCol, short iRow) } //#1 == - 2 == | 3 == -o 4 == ! 5 == -` 6 == o - g_worldmap.tiles[iCol][iRow].iForegroundSprite = AdjustForeground(iPathTypes[iPath] + iForegroundStyle * WORLD_PATH_SPRITE_SET_SIZE, iCol, iRow); + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = AdjustForeground(iPathTypes[iPath] + iForegroundStyle * WORLD_PATH_SPRITE_SET_SIZE, iCol, iRow); } //Convert foreground sprite to match the background sprite @@ -2203,7 +2203,7 @@ short AdjustForeground(short iSprite, short iCol, short iRow) if (iSprite >= WORLD_FOREGROUND_STAGE_OFFSET) return iSprite; - short iBackgroundSprite = g_worldmap.tiles[iCol][iRow].iBackgroundSprite % WORLD_BACKGROUND_SPRITE_SET_SIZE; + short iBackgroundSprite = g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite % WORLD_BACKGROUND_SPRITE_SET_SIZE; short iPathStyle = iSprite / WORLD_PATH_SPRITE_SET_SIZE; iSprite %= WORLD_PATH_SPRITE_SET_SIZE; @@ -2268,7 +2268,7 @@ void AutoSetPath(short iCol, short iRow) short iPath = 0; short iNeighborIndex = 0; - if (g_worldmap.tiles[iCol][iRow].iConnectionType == 0) + if (g_worldmap.tiles.at(iCol, iRow).iConnectionType == 0) return; for (short iAutoRow = iRow - 1; iAutoRow <= iRow + 1; iAutoRow++) { @@ -2278,7 +2278,7 @@ void AutoSetPath(short iCol, short iRow) if ((iAutoCol == iCol && iAutoRow != iRow) || (iAutoCol != iCol && iAutoRow == iRow)) { if (iAutoRow >= 0 && iAutoRow < iWorldHeight && iAutoCol >= 0 && iAutoCol < iWorldWidth) { - if (g_worldmap.tiles[iAutoCol][iAutoRow].iConnectionType > 0) + if (g_worldmap.tiles.at(iAutoCol, iAutoRow).iConnectionType > 0) iPath += 1 << iNeighborIndex; } @@ -2291,7 +2291,7 @@ void AutoSetPath(short iCol, short iRow) //#7 == -| 8 == -`- 9 == |- 10 == -,- 11 == + short iPathType = iPathTypes[iPath]; - short iForegroundSprite = g_worldmap.tiles[iCol][iRow].iForegroundSprite; + short iForegroundSprite = g_worldmap.tiles.at(iCol, iRow).iForegroundSprite; if (iPathType == 2 && iForegroundSprite >= WORLD_BRIDGE_SPRITE_OFFSET && iForegroundSprite <= WORLD_BRIDGE_SPRITE_OFFSET + 1) { iPathType = iForegroundSprite - WORLD_BRIDGE_SPRITE_OFFSET + 12; @@ -2299,15 +2299,15 @@ void AutoSetPath(short iCol, short iRow) iPathType = iForegroundSprite - WORLD_BRIDGE_SPRITE_OFFSET + 12; } - g_worldmap.tiles[iCol][iRow].iConnectionType = iPathType; + g_worldmap.tiles.at(iCol, iRow).iConnectionType = iPathType; } bool UpdateForeground(short iCol, short iRow) { - short iNewForeground = AdjustForeground(g_worldmap.tiles[iCol][iRow].iForegroundSprite, iCol, iRow); + short iNewForeground = AdjustForeground(g_worldmap.tiles.at(iCol, iRow).iForegroundSprite, iCol, iRow); - if (g_worldmap.tiles[iCol][iRow].iForegroundSprite != iNewForeground) { - g_worldmap.tiles[iCol][iRow].iForegroundSprite = iNewForeground; + if (g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != iNewForeground) { + g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = iNewForeground; return true; } @@ -2336,7 +2336,7 @@ bool UpdateCoastline(short iCol, short iRow) bool AutoSetTile(short iCol, short iRow) { //Don't need to do anything if this tile is solid - if (g_worldmap.tiles[iCol][iRow].iBackgroundSprite % WORLD_BACKGROUND_SPRITE_SET_SIZE == 1) + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite % WORLD_BACKGROUND_SPRITE_SET_SIZE == 1) return false; bool iTile[8] = {0, 0, 0, 0, 0, 0, 0, 0}; @@ -2350,7 +2350,7 @@ bool AutoSetTile(short iCol, short iRow) continue; if (iAutoRow >= 0 && iAutoRow < iWorldHeight && iAutoCol >= 0 && iAutoCol < iWorldWidth) { - short iBackgroundSprite = g_worldmap.tiles[iAutoCol][iAutoRow].iBackgroundSprite; + short iBackgroundSprite = g_worldmap.tiles.at(iAutoCol, iAutoRow).iBackgroundSprite; if (iBackgroundSprite % WORLD_BACKGROUND_SPRITE_SET_SIZE == 1) { iTile[iNeighborIndex] = true; @@ -2468,8 +2468,8 @@ bool AutoSetTile(short iCol, short iRow) iNewTile = iTileStyleOffset + 0; } - if (g_worldmap.tiles[iCol][iRow].iBackgroundSprite != iNewTile) { - g_worldmap.tiles[iCol][iRow].iBackgroundSprite = iNewTile; + if (g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite != iNewTile) { + g_worldmap.tiles.at(iCol, iRow).iBackgroundSprite = iNewTile; return true; } @@ -4167,10 +4167,10 @@ int editor_stage() //and decrement stage numbers greater than this stage for (short iRow = 0; iRow < iWorldHeight; iRow++) { for (short iCol = 0; iCol < iWorldWidth; iCol++) { - if (g_worldmap.tiles[iCol][iRow].iType == iEditStage + 6) { - g_worldmap.tiles[iCol][iRow].iType = 0; - } else if (g_worldmap.tiles[iCol][iRow].iType > iEditStage + 6) { - g_worldmap.tiles[iCol][iRow].iType--; + if (g_worldmap.tiles.at(iCol, iRow).iType == iEditStage + 6) { + g_worldmap.tiles.at(iCol, iRow).iType = 0; + } else if (g_worldmap.tiles.at(iCol, iRow).iType > iEditStage + 6) { + g_worldmap.tiles.at(iCol, iRow).iType--; } } }