1
0
Fork 0

Codechange: Replace bitstuffed VehicleEnterTileStatus. (#14027)

VehicleEnterTileStatus was an bitset-style enum, but bitstuffed with a StationID. However the StationID part was only used by trains, and only in two locations.

Instead, return just the enum bitset. The two places which require the StationID just call GetStationIndex() directly.
pull/14052/head
Peter Nelson 2025-04-20 21:10:02 +01:00 committed by GitHub
parent cb113cfed0
commit fc45bb5a2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 67 additions and 81 deletions

View File

@ -2933,10 +2933,10 @@ int TicksToLeaveDepot(const Train *v)
* Tile callback routine when vehicle enters tile
* @see vehicle_enter_tile_proc
*/
static VehicleEnterTileStatus VehicleEnter_Track(Vehicle *u, TileIndex tile, int x, int y)
static VehicleEnterTileStates VehicleEnter_Track(Vehicle *u, TileIndex tile, int x, int y)
{
/* This routine applies only to trains in depot tiles. */
if (u->type != VEH_TRAIN || !IsRailDepotTile(tile)) return VETSB_CONTINUE;
if (u->type != VEH_TRAIN || !IsRailDepotTile(tile)) return {};
/* Depot direction. */
DiagDirection dir = GetRailDepotDirection(tile);
@ -2944,7 +2944,7 @@ static VehicleEnterTileStatus VehicleEnter_Track(Vehicle *u, TileIndex tile, int
uint8_t fract_coord = (x & 0xF) + ((y & 0xF) << 4);
/* Make sure a train is not entering the tile from behind. */
if (_fractcoords_behind[dir] == fract_coord) return VETSB_CANNOT_ENTER;
if (_fractcoords_behind[dir] == fract_coord) return VehicleEnterTileState::CannotEnter;
Train *v = Train::From(u);
@ -2976,10 +2976,10 @@ static VehicleEnterTileStatus VehicleEnter_Track(Vehicle *u, TileIndex tile, int
v->tile = tile;
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
return VETSB_ENTERED_WORMHOLE;
return VehicleEnterTileState::EnteredWormhole;
}
return VETSB_CONTINUE;
return {};
}
/**

View File

@ -2250,7 +2250,7 @@ static const uint8_t _roadveh_enter_depot_dir[4] = {
TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
};
static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int, int)
static VehicleEnterTileStates VehicleEnter_Road(Vehicle *v, TileIndex tile, int, int)
{
switch (GetRoadTileType(tile)) {
case ROAD_TILE_DEPOT: {
@ -2266,14 +2266,14 @@ static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int,
rv->tile = tile;
InvalidateWindowData(WC_VEHICLE_DEPOT, rv->tile);
return VETSB_ENTERED_WORMHOLE;
return VehicleEnterTileState::EnteredWormhole;
}
break;
}
default: break;
}
return VETSB_CONTINUE;
return {};
}

View File

@ -1164,7 +1164,7 @@ bool IndividualRoadVehicleController(RoadVehicle *v, const RoadVehicle *prev)
}
}
if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && VehicleEnterTile(v, gp.new_tile, gp.x, gp.y).Test(VehicleEnterTileState::EnteredWormhole)) {
/* Vehicle has just entered a bridge or tunnel */
v->x_pos = gp.x;
v->y_pos = gp.y;
@ -1284,8 +1284,8 @@ again:
}
}
uint32_t r = VehicleEnterTile(v, tile, x, y);
if (HasBit(r, VETS_CANNOT_ENTER)) {
auto vets = VehicleEnterTile(v, tile, x, y);
if (vets.Test(VehicleEnterTileState::CannotEnter)) {
if (!IsTileType(tile, MP_TUNNELBRIDGE)) {
v->cur_speed = 0;
return false;
@ -1321,7 +1321,7 @@ again:
}
}
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
if (!vets.Test(VehicleEnterTileState::EnteredWormhole)) {
TileIndex old_tile = v->tile;
v->tile = tile;
@ -1399,8 +1399,8 @@ again:
}
}
uint32_t r = VehicleEnterTile(v, v->tile, x, y);
if (HasBit(r, VETS_CANNOT_ENTER)) {
auto vets = VehicleEnterTile(v, v->tile, x, y);
if (vets.Test(VehicleEnterTileState::CannotEnter)) {
v->cur_speed = 0;
return false;
}
@ -1541,8 +1541,8 @@ again:
/* Check tile position conditions - i.e. stop position in depot,
* entry onto bridge or into tunnel */
uint32_t r = VehicleEnterTile(v, v->tile, x, y);
if (HasBit(r, VETS_CANNOT_ENTER)) {
auto vets = VehicleEnterTile(v, v->tile, x, y);
if (vets.Test(VehicleEnterTileState::CannotEnter)) {
v->cur_speed = 0;
return false;
}
@ -1553,7 +1553,7 @@ again:
/* Move to next frame unless vehicle arrived at a stop position
* in a depot or entered a tunnel/bridge */
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) v->frame++;
if (!vets.Test(VehicleEnterTileState::EnteredWormhole)) v->frame++;
v->x_pos = x;
v->y_pos = y;
v->UpdatePosition();

View File

@ -742,8 +742,8 @@ static void ShipController(Ship *v)
gp.y = v->y_pos;
} else {
/* Not inside depot */
const VehicleEnterTileStatus r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
if (HasBit(r, VETS_CANNOT_ENTER)) return ReverseShip(v);
auto vets = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
if (vets.Test(VehicleEnterTileState::CannotEnter)) return ReverseShip(v);
/* A leave station order only needs one tick to get processed, so we can
* always skip ahead. */
@ -810,10 +810,10 @@ static void ShipController(Ship *v)
gp.y = (gp.y & ~0xF) | b.y_subcoord;
/* Call the landscape function and tell it that the vehicle entered the tile */
const VehicleEnterTileStatus r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
if (HasBit(r, VETS_CANNOT_ENTER)) return ReverseShip(v);
auto vets = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
if (vets.Test(VehicleEnterTileState::CannotEnter)) return ReverseShip(v);
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
if (!vets.Test(VehicleEnterTileState::EnteredWormhole)) {
v->tile = gp.new_tile;
v->state = TrackToTrackBits(track);
@ -843,7 +843,7 @@ static void ShipController(Ship *v)
}
} else {
/* On a bridge */
if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !VehicleEnterTile(v, gp.new_tile, gp.x, gp.y).Test(VehicleEnterTileState::EnteredWormhole)) {
v->x_pos = gp.x;
v->y_pos = gp.y;
v->UpdatePosition();

View File

@ -3696,12 +3696,12 @@ static bool ClickTile_Station(TileIndex tile)
return true;
}
static VehicleEnterTileStatus VehicleEnter_Station(Vehicle *v, TileIndex tile, int x, int y)
static VehicleEnterTileStates VehicleEnter_Station(Vehicle *v, TileIndex tile, int x, int y)
{
if (v->type == VEH_TRAIN) {
StationID station_id = GetStationIndex(tile);
if (!v->current_order.ShouldStopAtStation(v, station_id)) return VETSB_CONTINUE;
if (!IsRailStation(tile) || !v->IsFrontEngine()) return VETSB_CONTINUE;
if (!v->current_order.ShouldStopAtStation(v, station_id)) return {};
if (!IsRailStation(tile) || !v->IsFrontEngine()) return {};
int station_ahead;
int station_length;
@ -3711,7 +3711,7 @@ static VehicleEnterTileStatus VehicleEnter_Station(Vehicle *v, TileIndex tile, i
* begin of the platform to the stop location is longer than the length
* of the platform. Station ahead 'includes' the current tile where the
* vehicle is on, so we need to subtract that. */
if (stop + station_ahead - (int)TILE_SIZE >= station_length) return VETSB_CONTINUE;
if (stop + station_ahead - (int)TILE_SIZE >= station_length) return {};
DiagDirection dir = DirToDiagDir(v->direction);
@ -3724,7 +3724,7 @@ static VehicleEnterTileStatus VehicleEnter_Station(Vehicle *v, TileIndex tile, i
stop &= TILE_SIZE - 1;
if (x == stop) {
return VETSB_ENTERED_STATION | static_cast<VehicleEnterTileStatus>(station_id.base() << VETS_STATION_ID_OFFSET); // enter station
return VehicleEnterTileState::EnteredStation; // enter station
} else if (x < stop) {
v->vehstatus.Set(VehState::TrainSlowing);
uint16_t spd = std::max(0, (stop - x) * 20 - 15);
@ -3736,12 +3736,13 @@ static VehicleEnterTileStatus VehicleEnter_Station(Vehicle *v, TileIndex tile, i
if (rv->state < RVSB_IN_ROAD_STOP && !IsReversingRoadTrackdir((Trackdir)rv->state) && rv->frame == 0) {
if (IsStationRoadStop(tile) && rv->IsFrontEngine()) {
/* Attempt to allocate a parking bay in a road stop */
return RoadStop::GetByTile(tile, GetRoadStopType(tile))->Enter(rv) ? VETSB_CONTINUE : VETSB_CANNOT_ENTER;
if (RoadStop::GetByTile(tile, GetRoadStopType(tile))->Enter(rv)) return {};
return VehicleEnterTileState::CannotEnter;
}
}
}
return VETSB_CONTINUE;
return {};
}
/**

View File

@ -17,27 +17,13 @@
#include "tile_map.h"
#include "timer/timer_game_calendar.h"
/** The returned bits of VehicleEnterTile. */
enum VehicleEnterTileStatus : uint32_t {
VETS_ENTERED_STATION = 1, ///< The vehicle entered a station
VETS_ENTERED_WORMHOLE = 2, ///< The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/tunnel)
VETS_CANNOT_ENTER = 3, ///< The vehicle cannot enter the tile
/**
* Shift the VehicleEnterTileStatus this many bits
* to the right to get the station ID when
* VETS_ENTERED_STATION is set
*/
VETS_STATION_ID_OFFSET = 8,
VETS_STATION_MASK = 0xFFFF << VETS_STATION_ID_OFFSET,
/** Bit sets of the above specified bits */
VETSB_CONTINUE = 0, ///< The vehicle can continue normally
VETSB_ENTERED_STATION = 1 << VETS_ENTERED_STATION, ///< The vehicle entered a station
VETSB_ENTERED_WORMHOLE = 1 << VETS_ENTERED_WORMHOLE, ///< The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/tunnel)
VETSB_CANNOT_ENTER = 1 << VETS_CANNOT_ENTER, ///< The vehicle cannot enter the tile
enum class VehicleEnterTileState : uint8_t {
EnteredStation, ///< The vehicle entered a station
EnteredWormhole, ///< The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/tunnel)
CannotEnter, ///< The vehicle cannot enter the tile
};
DECLARE_ENUM_AS_BIT_SET(VehicleEnterTileStatus)
using VehicleEnterTileStates = EnumBitSet<VehicleEnterTileState, uint8_t>;
/** Tile information, used while rendering the tile */
struct TileInfo {
@ -131,8 +117,7 @@ typedef void AnimateTileProc(TileIndex tile);
typedef void TileLoopProc(TileIndex tile);
typedef void ChangeTileOwnerProc(TileIndex tile, Owner old_owner, Owner new_owner);
/** @see VehicleEnterTileStatus to see what the return values mean */
typedef VehicleEnterTileStatus VehicleEnterTileProc(Vehicle *v, TileIndex tile, int x, int y);
typedef VehicleEnterTileStates VehicleEnterTileProc(Vehicle *v, TileIndex tile, int x, int y);
typedef Foundation GetFoundationProc(TileIndex tile, Slope tileh);
/**
@ -176,7 +161,7 @@ struct TileTypeProcs {
extern const TileTypeProcs * const _tile_type_procs[16];
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side = INVALID_DIAGDIR);
VehicleEnterTileStatus VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y);
VehicleEnterTileStates VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y);
void ChangeTileOwner(TileIndex tile, Owner old_owner, Owner new_owner);
void GetTileDesc(TileIndex tile, TileDesc &td);

View File

@ -3300,13 +3300,13 @@ bool TrainController(Train *v, Vehicle *nomove, bool reverse)
/* Reverse when we are at the end of the track already, do not move to the new position */
if (v->IsFrontEngine() && !TrainCheckIfLineEnds(v, reverse)) return false;
uint32_t r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
if (HasBit(r, VETS_CANNOT_ENTER)) {
auto vets = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
if (vets.Test(VehicleEnterTileState::CannotEnter)) {
goto invalid_rail;
}
if (HasBit(r, VETS_ENTERED_STATION)) {
if (vets.Test(VehicleEnterTileState::EnteredStation)) {
/* The new position is the end of the platform */
TrainEnterStation(v, StationID(r >> VETS_STATION_ID_OFFSET));
TrainEnterStation(v, GetStationIndex(gp.new_tile));
}
}
} else {
@ -3447,12 +3447,12 @@ bool TrainController(Train *v, Vehicle *nomove, bool reverse)
Direction chosen_dir = (Direction)b[2];
/* Call the landscape function and tell it that the vehicle entered the tile */
uint32_t r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
if (HasBit(r, VETS_CANNOT_ENTER)) {
auto vets = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
if (vets.Test(VehicleEnterTileState::CannotEnter)) {
goto invalid_rail;
}
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
if (!vets.Test(VehicleEnterTileState::EnteredWormhole)) {
Track track = FindFirstTrack(chosen_track);
Trackdir tdir = TrackDirectionToTrackdir(track, chosen_dir);
if (v->IsFrontEngine() && HasPbsSignalOnTrackdir(gp.new_tile, tdir)) {
@ -3498,13 +3498,13 @@ bool TrainController(Train *v, Vehicle *nomove, bool reverse)
CheckNextTrainTile(v);
}
if (HasBit(r, VETS_ENTERED_STATION)) {
if (vets.Test(VehicleEnterTileState::EnteredStation)) {
/* The new position is the location where we want to stop */
TrainEnterStation(v, StationID(r >> VETS_STATION_ID_OFFSET));
TrainEnterStation(v, GetStationIndex(gp.new_tile));
}
}
} else {
if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && VehicleEnterTile(v, gp.new_tile, gp.x, gp.y).Test(VehicleEnterTileState::EnteredWormhole)) {
/* Perform look-ahead on tunnel exit. */
if (v->IsFrontEngine()) {
TryReserveRailTrack(gp.new_tile, DiagDirToDiagTrack(GetTunnelBridgeDirection(gp.new_tile)));

View File

@ -1882,11 +1882,11 @@ static const uint8_t TUNNEL_SOUND_FRAME = 1;
*/
extern const uint8_t _tunnel_visibility_frame[DIAGDIR_END] = {12, 8, 8, 12};
static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
static VehicleEnterTileStates VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
{
int z = GetSlopePixelZ(x, y, true) - v->z_pos;
if (abs(z) > 2) return VETSB_CANNOT_ENTER;
if (abs(z) > 2) return VehicleEnterTileState::CannotEnter;
/* Direction into the wormhole */
const DiagDirection dir = GetTunnelBridgeDirection(tile);
/* Direction of the vehicle */
@ -1905,13 +1905,13 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
if (!PlayVehicleSound(t, VSE_TUNNEL) && RailVehInfo(t->engine_type)->engclass == 0) {
SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
}
return VETSB_CONTINUE;
return {};
}
if (frame == _tunnel_visibility_frame[dir]) {
t->tile = tile;
t->track = TRACK_BIT_WORMHOLE;
t->vehstatus.Set(VehState::Hidden);
return VETSB_ENTERED_WORMHOLE;
return VehicleEnterTileState::EnteredWormhole;
}
}
@ -1921,7 +1921,7 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
t->track = DiagDirToDiagTrackBits(vdir);
assert(t->track);
t->vehstatus.Reset(VehState::Hidden);
return VETSB_ENTERED_WORMHOLE;
return VehicleEnterTileState::EnteredWormhole;
}
} else if (v->type == VEH_ROAD) {
RoadVehicle *rv = RoadVehicle::From(v);
@ -1934,9 +1934,9 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
rv->tile = tile;
rv->state = RVSB_WORMHOLE;
rv->vehstatus.Set(VehState::Hidden);
return VETSB_ENTERED_WORMHOLE;
return VehicleEnterTileState::EnteredWormhole;
} else {
return VETSB_CONTINUE;
return {};
}
}
@ -1946,7 +1946,7 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
rv->state = DiagDirToDiagTrackdir(vdir);
rv->frame = frame;
rv->vehstatus.Reset(VehState::Hidden);
return VETSB_ENTERED_WORMHOLE;
return VehicleEnterTileState::EnteredWormhole;
}
}
} else { // IsBridge(tile)
@ -1961,7 +1961,7 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
if (vdir == dir) {
/* Vehicle enters bridge at the last frame inside this tile. */
if (frame != TILE_SIZE - 1) return VETSB_CONTINUE;
if (frame != TILE_SIZE - 1) return {};
switch (v->type) {
case VEH_TRAIN: {
Train *t = Train::From(v);
@ -1983,7 +1983,7 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
default: NOT_REACHED();
}
return VETSB_ENTERED_WORMHOLE;
return VehicleEnterTileState::EnteredWormhole;
} else if (vdir == ReverseDiagDir(dir)) {
v->tile = tile;
switch (v->type) {
@ -1991,7 +1991,7 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
Train *t = Train::From(v);
if (t->track == TRACK_BIT_WORMHOLE) {
t->track = DiagDirToDiagTrackBits(vdir);
return VETSB_ENTERED_WORMHOLE;
return VehicleEnterTileState::EnteredWormhole;
}
break;
}
@ -2001,7 +2001,7 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
if (rv->state == RVSB_WORMHOLE) {
rv->state = DiagDirToDiagTrackdir(vdir);
rv->frame = 0;
return VETSB_ENTERED_WORMHOLE;
return VehicleEnterTileState::EnteredWormhole;
}
break;
}
@ -2010,7 +2010,7 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
Ship *ship = Ship::From(v);
if (ship->state == TRACK_BIT_WORMHOLE) {
ship->state = DiagDirToDiagTrackBits(vdir);
return VETSB_ENTERED_WORMHOLE;
return VehicleEnterTileState::EnteredWormhole;
}
break;
}
@ -2019,7 +2019,7 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
}
}
}
return VETSB_CONTINUE;
return {};
}
static CommandCost TerraformTile_TunnelBridge(TileIndex tile, DoCommandFlags flags, int z_new, Slope tileh_new)

View File

@ -1837,9 +1837,9 @@ Direction GetDirectionTowards(const Vehicle *v, int x, int y)
* @param x X position
* @param y Y position
* @return Some meta-data over the to be entered tile.
* @see VehicleEnterTileStatus to see what the bits in the return value mean.
* @see VehicleEnterTileStates to see what the bits in the return value mean.
*/
VehicleEnterTileStatus VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y)
VehicleEnterTileStates VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y)
{
return _tile_type_procs[GetTileType(tile)]->vehicle_enter_tile_proc(v, tile, x, y);
}

View File

@ -1400,9 +1400,9 @@ static void ChangeTileOwner_Water(TileIndex tile, Owner old_owner, Owner new_own
}
}
static VehicleEnterTileStatus VehicleEnter_Water(Vehicle *, TileIndex, int, int)
static VehicleEnterTileStates VehicleEnter_Water(Vehicle *, TileIndex, int, int)
{
return VETSB_CONTINUE;
return {};
}
static CommandCost TerraformTile_Water(TileIndex tile, DoCommandFlags flags, int, Slope)