Skip to content

Commit

Permalink
Do not depend on the global world map in the world vehicle code
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Oct 17, 2024
1 parent a04d6f4 commit 86953fa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
32 changes: 16 additions & 16 deletions src/smw/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void WorldVehicle::Init(short iCol, short iRow, short iAction, short iSprite, sh
iBoundary = boundary;
}

void WorldVehicle::Move()
void WorldVehicle::Move(const WorldMap& worldmap)
{
iNumMoves = RANDOM_INT(iMaxMoves - iMinMoves + 1) + iMinMoves;

Expand All @@ -241,19 +241,19 @@ void WorldVehicle::Move()
iPaceTimer = 0;
}

SetNextDest();
SetNextDest(worldmap);
}

void WorldVehicle::SetNextDest()
void WorldVehicle::SetNextDest(const WorldMap& worldmap)
{
if (iState != 0 || iMaxMoves == 0)
return;

WorldMapTile * tile = &g_worldmap.tiles.at(currentTile.x, currentTile.y);
const Vec2s iPlayerCurrentTile = g_worldmap.GetPlayerCurrentTile();
const WorldMapTile& tile = worldmap.tiles.at(currentTile.x, currentTile.y);
const Vec2s iPlayerCurrentTile = worldmap.GetPlayerCurrentTile();

if (iNumMoves-- <= 0) {
if (tile->iType == 0 && iPlayerCurrentTile != currentTile && g_worldmap.NumVehiclesInTile(currentTile) <= 1)
if (tile.iType == 0 && iPlayerCurrentTile != currentTile && worldmap.NumVehiclesInTile(currentTile) <= 1)
return;
}

Expand All @@ -268,15 +268,15 @@ void WorldVehicle::SetNextDest()
for (short iDirection = 0; iDirection < 4; iDirection++) {
bool fIsDoor = false;
if (iDirection == 0)
fIsDoor = g_worldmap.IsDoor(currentTile.x, currentTile.y - 1) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(currentTile.x, currentTile.y - 1) == iBoundary);
fIsDoor = worldmap.IsDoor(currentTile.x, currentTile.y - 1) || (iBoundary != 0 && worldmap.GetVehicleBoundary(currentTile.x, currentTile.y - 1) == iBoundary);
else if (iDirection == 1)
fIsDoor = g_worldmap.IsDoor(currentTile.x, currentTile.y + 1) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(currentTile.x, currentTile.y + 1) == iBoundary);
fIsDoor = worldmap.IsDoor(currentTile.x, currentTile.y + 1) || (iBoundary != 0 && worldmap.GetVehicleBoundary(currentTile.x, currentTile.y + 1) == iBoundary);
else if (iDirection == 2)
fIsDoor = g_worldmap.IsDoor(currentTile.x - 1, currentTile.y) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(currentTile.x - 1, currentTile.y) == iBoundary);
fIsDoor = worldmap.IsDoor(currentTile.x - 1, currentTile.y) || (iBoundary != 0 && worldmap.GetVehicleBoundary(currentTile.x - 1, currentTile.y) == iBoundary);
else if (iDirection == 3)
fIsDoor = g_worldmap.IsDoor(currentTile.x + 1, currentTile.y) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(currentTile.x + 1, currentTile.y) == iBoundary);
fIsDoor = worldmap.IsDoor(currentTile.x + 1, currentTile.y) || (iBoundary != 0 && worldmap.GetVehicleBoundary(currentTile.x + 1, currentTile.y) == iBoundary);

if (tile->fConnection[iDirection] && !fIsDoor)
if (tile.fConnection[iDirection] && !fIsDoor)
iConnections[iNumConnections++] = iDirection;
}

Expand All @@ -286,15 +286,15 @@ void WorldVehicle::SetNextDest()
}
}

bool WorldVehicle::Update()
bool WorldVehicle::Update(const WorldMap& worldmap)
{
bool fMoveDone = WorldMovingObject::Update();

if (fMoveDone) {
if (currentTile == g_worldmap.GetPlayerCurrentTile())
if (currentTile == worldmap.GetPlayerCurrentTile())
return true;

SetNextDest();
SetNextDest(worldmap);
}

//If we're done moving, start pacing in place
Expand Down Expand Up @@ -986,7 +986,7 @@ bool WorldMap::Update(bool * fPlayerVehicleCollision)
if (!vehicle.fEnabled)
continue;

*fPlayerVehicleCollision |= vehicle.Update();
*fPlayerVehicleCollision |= vehicle.Update(*this);

if (vehicle.iState > 0)
fPlayMovingVehicleSound = true;
Expand Down Expand Up @@ -1211,7 +1211,7 @@ void WorldMap::MoveVehicles()
{
for (WorldVehicle& vehicle : vehicles) {
if (vehicle.fEnabled)
vehicle.Move();
vehicle.Move(*this);
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/smw/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#define WORLD_FOREGROUND_SPRITE_OFFSET 700
#define WORLD_FOREGROUND_SPRITE_ANIMATED_OFFSET 900

class WorldMap;

struct WorldMapTile {
//Id is used for searching for AI
short iID;
Expand Down Expand Up @@ -89,14 +91,14 @@ class WorldVehicle : public WorldMovingObject
~WorldVehicle();

void Init(short iCol, short iRow, short iAction, short iSprite, short iMinMoves, short iMaxMoves, bool fSpritePaces, short iInitialDirection, short iBoundary, short tilesize);
void Move();
void Move(const WorldMap& worldmap);

bool Update();
bool Update(const WorldMap& parent);
void Draw(short iWorldOffsetX, short iWorldOffsetY, bool fVehiclesSleeping) const;

private:

void SetNextDest();
void SetNextDest(const WorldMap& parent);

SDL_Rect srcRects[5];

Expand Down

0 comments on commit 86953fa

Please sign in to comment.