From a4ae02a2226558610ed6eb39c8941def29ff31b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ty=C3=A1s=20Mustoha?= Date: Sat, 12 Oct 2024 20:31:15 +0200 Subject: [PATCH] Simplified the world warp class --- src/common/math/Vec2.h | 7 +++ src/smw/world.cpp | 90 +++++++++++---------------------- src/smw/world.h | 24 +++------ src/worldeditor/worldeditor.cpp | 79 ++++++++++++----------------- 4 files changed, 76 insertions(+), 124 deletions(-) diff --git a/src/common/math/Vec2.h b/src/common/math/Vec2.h index 112d6d56..7b873c8f 100644 --- a/src/common/math/Vec2.h +++ b/src/common/math/Vec2.h @@ -59,6 +59,13 @@ template constexpr Vec2 operator/(const Vec2& lhs, const Vec2< return {lhs.x / rhs.x, lhs.y / rhs.y}; } +template constexpr bool operator==(const Vec2& lhs, const Vec2& rhs) { + return lhs.x == rhs.x && lhs.y == rhs.y; +} +template constexpr bool operator!=(const Vec2& lhs, const Vec2& rhs) { + return !(lhs == rhs); +} + template constexpr Vec2 operator*(const Vec2& lhs, Scalar scalar) { return {lhs.x * scalar, lhs.y * scalar}; } diff --git a/src/smw/world.cpp b/src/smw/world.cpp index 0f85855b..795cd622 100644 --- a/src/smw/world.cpp +++ b/src/smw/world.cpp @@ -323,35 +323,21 @@ void WorldVehicle::Draw(short iWorldOffsetX, short iWorldOffsetY, bool fVehicles * WorldWarp **********************************/ -WorldWarp::WorldWarp() -{ - iCol1 = 0; - iRow1 = 0; - iCol2 = 0; - iRow2 = 0; -} - -void WorldWarp::Init(short id, short col1, short row1, short col2, short row2) -{ - iID = id; - iCol1 = col1; - iRow1 = row1; - iCol2 = col2; - iRow2 = row2; -} +WorldWarp::WorldWarp(short id, Vec2s posA, Vec2s posB) + : id(id) + , posA(posA) + , posB(posB) +{} -void WorldWarp::GetOtherSide(short iCol, short iRow, short * iOtherCol, short * iOtherRow) const +Vec2s WorldWarp::getOtherSide(Vec2s target) const { - if (iCol1 == iCol && iRow1 == iRow) { - *iOtherCol = iCol2; - *iOtherRow = iRow2; - } else if (iCol2 == iCol && iRow2 == iRow) { - *iOtherCol = iCol1; - *iOtherRow = iRow1; - } else { - *iOtherCol = iCol; - *iOtherRow = iRow; + if (target == posA) { + return posB; + } + if (target == posB) { + return posA; } + return target; } @@ -629,36 +615,22 @@ bool WorldMap::Load(short tilesize) iReadType = iNumWarps == 0 ? 14 : 13; } else if (iReadType == 13) { //warp details char * psz = strtok(buffer, ",\n"); - if (!psz) goto RETURN; - short iCol1 = atoi(psz); - if (iCol1 < 0) - iCol1 = 0; + short iCol1 = std::max(0, atoi(psz)); psz = strtok(NULL, ",\n"); - - short iRow1 = atoi(psz); - if (iRow1 < 0) - iRow1 = 0; + short iRow1 = std::max(0, atoi(psz)); psz = strtok(NULL, ",\n"); - - short iCol2 = atoi(psz); - if (iCol2 < 0) - iCol2 = 0; + short iCol2 = std::max(0, atoi(psz)); psz = strtok(NULL, ",\n"); - - short iRow2 = atoi(psz); - if (iRow2 < 0) - iRow2 = 0; + short iRow2 = std::max(0, atoi(psz)); short warpId = warps.size(); - - warps.emplace_back(WorldWarp()); - warps.back().Init(warpId, iCol1, iRow1, iCol2, iRow2); + warps.emplace_back(WorldWarp(warpId, {iCol1, iRow1}, {iCol2, iRow2})); tiles[iCol1][iRow1].iWarp = warpId; tiles[iCol2][iRow2].iWarp = warpId; @@ -954,10 +926,10 @@ bool WorldMap::Save(const std::string& szPath) const fprintf(file, "%d\n", warps.size()); for (const WorldWarp& warp : warps) { - fprintf(file, "%d,", warp.iCol1); - fprintf(file, "%d,", warp.iRow1); - fprintf(file, "%d,", warp.iCol2); - fprintf(file, "%d\n", warp.iRow2); + fprintf(file, "%d,", warp.posA.x); + fprintf(file, "%d,", warp.posA.y); + fprintf(file, "%d,", warp.posB.x); + fprintf(file, "%d\n", warp.posB.y); } fprintf(file, "\n"); @@ -1350,11 +1322,12 @@ short WorldMap::GetVehicleInPlayerTile(short * vehicleIndex) const bool WorldMap::GetWarpInPlayerTile(short * iWarpCol, short * iWarpRow) const { short iWarp = tiles[player.iCurrentTileX][player.iCurrentTileY].iWarp; - if (iWarp < 0) return false; - warps[iWarp].GetOtherSide(player.iCurrentTileX, player.iCurrentTileY, iWarpCol, iWarpRow); + Vec2s pos = warps[iWarp].getOtherSide({player.iCurrentTileX, player.iCurrentTileY}); + *iWarpCol = pos.x; + *iWarpRow = pos.y; return true; } @@ -1576,12 +1549,9 @@ short WorldMap::GetNextInterestingMove(short iCol, short iRow) const else if (iBackTileDirection == 3) iBackTileId += 1; else if (iBackTileDirection == 4) { - short iWarpCol, iWarpRow; - short iCol = iBackTileId % iWidth; - short iRow = iBackTileId / iWidth; - - warps[tiles[iCol][iRow].iWarp].GetOtherSide(iCol, iRow, &iWarpCol, &iWarpRow); - iBackTileId = tiles[iWarpCol][iWarpRow].iID; + const Vec2s target(iBackTileId % iWidth, iBackTileId / iWidth); + const Vec2s pos = warps[tiles[iCol][iRow].iWarp].getOtherSide(target); + iBackTileId = tiles[pos.x][pos.y].iID; } if (iBackTileId == iCurrentId) { @@ -1647,10 +1617,8 @@ short WorldMap::GetNextInterestingMove(short iCol, short iRow) const } if (tile->iWarp >= 0) { - short iWarpCol, iWarpRow; - warps[tile->iWarp].GetOtherSide(tile->iCol, tile->iRow, &iWarpCol, &iWarpRow); - - const WorldMapTile& warpTile = tiles[iWarpCol][iWarpRow]; + const Vec2s pos = warps[tile->iWarp].getOtherSide({tile->iCol, tile->iRow}); + const WorldMapTile& warpTile = tiles[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 437d8dcb..b862d31c 100644 --- a/src/smw/world.h +++ b/src/smw/world.h @@ -2,6 +2,7 @@ #define WORLD_H #include "SDL.h" +#include "math/Vec2.h" #include #include @@ -135,25 +136,14 @@ class WorldVehicle : public WorldMovingObject friend int main(int argc, char *argv[]); }; -class WorldWarp -{ - public: - WorldWarp(); - void Init(short id, short col1, short row1, short col2, short row2); - void GetOtherSide(short iCol, short iRow, short * iOtherCol, short * iOtherRow) const; +struct WorldWarp { + WorldWarp(short id, Vec2s posA, Vec2s posB); - private: - short iCol1, iRow1; - short iCol2, iRow2; - short iID; + [[nodiscard]] Vec2s getOtherSide(Vec2s target) const; - friend class WorldMap; - friend void AddWarpToTile(short iCol, short iRow, short iType); - friend void RemoveWarpFromTile(short iCol, short iRow); - friend void ReadWarpsIntoEditor(); - friend void WriteWarpsIntoWorld(); - friend int editor_edit(); - friend void takescreenshot(); + short id; + Vec2s posA; + Vec2s posB; }; class WorldMap { diff --git a/src/worldeditor/worldeditor.cpp b/src/worldeditor/worldeditor.cpp index 90341be9..b7142fc9 100644 --- a/src/worldeditor/worldeditor.cpp +++ b/src/worldeditor/worldeditor.cpp @@ -1789,18 +1789,18 @@ int editor_edit() short ix, iy; - if (warp->iCol1 >= 0) { - ix = (warp->iCol1 - draw_offset_col) * TILESIZE + draw_offset_x; - iy = (warp->iRow1 - draw_offset_row) * TILESIZE + draw_offset_y; + if (warp->posA.x >= 0) { + ix = (warp->posA.x - draw_offset_col) * TILESIZE + draw_offset_x; + iy = (warp->posA.y - draw_offset_row) * TILESIZE + draw_offset_y; - spr_warps[0].draw(ix, iy, warp->iID << 5, 0, 32, 32); + spr_warps[0].draw(ix, iy, warp->id << 5, 0, 32, 32); } - if (warp->iCol2 >= 0) { - ix = (warp->iCol2 - draw_offset_col) * TILESIZE + draw_offset_x; - iy = (warp->iRow2 - draw_offset_row) * TILESIZE + draw_offset_y; + if (warp->posB.x >= 0) { + ix = (warp->posB.x - draw_offset_col) * TILESIZE + draw_offset_x; + iy = (warp->posB.y - draw_offset_row) * TILESIZE + draw_offset_y; - spr_warps[0].draw(ix, iy, warp->iID << 5, 0, 32, 32); + spr_warps[0].draw(ix, iy, warp->id << 5, 0, 32, 32); } itr++; @@ -2065,9 +2065,7 @@ void ReadWarpsIntoEditor() warplist.clear(); for (const WorldWarp& warp : g_worldmap.warps) { - WorldWarp* warpcopy = new WorldWarp(); - *warpcopy = warp; - warplist.push_back(warpcopy); + warplist.push_back(new WorldWarp(warp)); } } @@ -2090,7 +2088,7 @@ void AddWarpToTile(short iCol, short iRow, short iType) WorldWarp * newwarp = NULL; while (itr != lim) { WorldWarp * warp = *itr; - if (warp->iID == iType) { + if (warp->id == iType) { newwarp = warp; break; } @@ -2099,24 +2097,15 @@ void AddWarpToTile(short iCol, short iRow, short iType) } if (!newwarp) { - newwarp = new WorldWarp(); - - newwarp->iID = iType; - newwarp->iCol1 = iCol; - newwarp->iRow1 = iRow; - newwarp->iCol2 = -1; - newwarp->iRow2 = -1; - - warplist.push_back(newwarp); + newwarp = new WorldWarp(iType, {iCol, iRow}, {-1, -1}); + warplist.push_back(newwarp); } else { - if (newwarp->iCol1 == -1) { - newwarp->iCol1 = iCol; - newwarp->iRow1 = iRow; - } else if (newwarp->iCol1 != iCol || newwarp->iRow1 != iRow) { - newwarp->iCol2 = iCol; - newwarp->iRow2 = iRow; - } - } + if (newwarp->posA.x == -1) { + newwarp->posA = {iCol, iRow}; + } else if (newwarp->posA.x != iCol || newwarp->posA.y != iRow) { + newwarp->posB = {iCol, iRow}; + } + } } void RemoveWarpFromTile(short iCol, short iRow) @@ -2124,8 +2113,8 @@ void RemoveWarpFromTile(short iCol, short iRow) std::vector::iterator itr = warplist.begin(), lim = warplist.end(); while (itr != lim) { WorldWarp * warp = *itr; - if (warp->iCol1 == iCol && warp->iRow1 == iRow) { - if (warp->iCol2 == -1 && warp->iRow2 == -1) { + if (warp->posA.x == iCol && warp->posA.y == iRow) { + if (warp->posB.x == -1 && warp->posB.y == -1) { delete (*itr); itr = warplist.erase(itr); @@ -2133,11 +2122,10 @@ void RemoveWarpFromTile(short iCol, short iRow) return; } else { - warp->iCol1 = -1; - warp->iRow1 = -1; - } - } else if (warp->iCol2 == iCol && warp->iRow2 == iRow) { - if (warp->iCol1 == -1 && warp->iRow1 == -1) { + warp->posA = {-1, -1}; + } + } else if (warp->posB.x == iCol && warp->posB.y == iRow) { + if (warp->posA.x == -1 && warp->posA.y == -1) { delete (*itr); itr = warplist.erase(itr); @@ -2145,13 +2133,12 @@ void RemoveWarpFromTile(short iCol, short iRow) return; } else { - warp->iCol2 = -1; - warp->iRow2 = -1; - } - } + warp->posB = {-1, -1}; + } + } - itr++; - } + itr++; + } } void UpdatePathSprite(short iCol, short iRow) @@ -4839,11 +4826,11 @@ void takescreenshot() while (itrWarp != limWarp) { WorldWarp * warp = *itrWarp; - if (warp->iCol1 >= 0) - spr_warps[iScreenshotSize].draw(warp->iCol1 * iTileSize, warp->iRow1 * iTileSize, warp->iID * iTileSize, 0, iTileSize, iTileSize); + if (warp->posA.x >= 0) + spr_warps[iScreenshotSize].draw(warp->posA.x * iTileSize, warp->posA.y * iTileSize, warp->id * iTileSize, 0, iTileSize, iTileSize); - if (warp->iCol2 >= 0) - spr_warps[iScreenshotSize].draw(warp->iCol2 * iTileSize, warp->iRow2 * iTileSize, warp->iID * iTileSize, 0, iTileSize, iTileSize); + if (warp->posB.x >= 0) + spr_warps[iScreenshotSize].draw(warp->posB.x * iTileSize, warp->posB.y * iTileSize, warp->id * iTileSize, 0, iTileSize, iTileSize); itrWarp++; }