Skip to content

Commit

Permalink
Optimized the tile storage in world maps
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Oct 17, 2024
1 parent 95b4336 commit c452b97
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 210 deletions.
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ add_library(CommonFiles STATIC
Version.h
WorldTourStop.cpp
WorldTourStop.h
util/Grid.h
)

add_subdirectory(ui)
Expand Down
60 changes: 60 additions & 0 deletions src/common/util/Grid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

#include <cassert>
#include <cstddef>
#include <vector>


template<typename T>
class Grid {
private:
size_t width = 0;
size_t height = 0;
std::vector<T> 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<T>& 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(); }
};
10 changes: 5 additions & 5 deletions src/smw/ui/MI_World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down
Loading

0 comments on commit c452b97

Please sign in to comment.