Skip to content

Commit

Permalink
Simplified the world warp class
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Oct 12, 2024
1 parent e9b6e52 commit a4ae02a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 124 deletions.
7 changes: 7 additions & 0 deletions src/common/math/Vec2.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ template<typename T> constexpr Vec2<T> operator/(const Vec2<T>& lhs, const Vec2<
return {lhs.x / rhs.x, lhs.y / rhs.y};
}

template<typename T> constexpr bool operator==(const Vec2<T>& lhs, const Vec2<T>& rhs) {
return lhs.x == rhs.x && lhs.y == rhs.y;
}
template<typename T> constexpr bool operator!=(const Vec2<T>& lhs, const Vec2<T>& rhs) {
return !(lhs == rhs);
}

template<typename T, typename Scalar> constexpr Vec2<T> operator*(const Vec2<T>& lhs, Scalar scalar) {
return {lhs.x * scalar, lhs.y * scalar};
}
Expand Down
90 changes: 29 additions & 61 deletions src/smw/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 7 additions & 17 deletions src/smw/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define WORLD_H

#include "SDL.h"
#include "math/Vec2.h"

#include <string>
#include <vector>
Expand Down Expand Up @@ -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 {
Expand Down
79 changes: 33 additions & 46 deletions src/worldeditor/worldeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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;
}
Expand All @@ -2099,59 +2097,48 @@ 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)
{
std::vector<WorldWarp*>::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);
lim = warplist.end();

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);
lim = warplist.end();

return;
} else {
warp->iCol2 = -1;
warp->iRow2 = -1;
}
}
warp->posB = {-1, -1};
}
}

itr++;
}
itr++;
}
}

void UpdatePathSprite(short iCol, short iRow)
Expand Down Expand Up @@ -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++;
}
Expand Down

0 comments on commit a4ae02a

Please sign in to comment.