Initial support for rail custom bridge heads
This commit is contained in:
206
src/bridge_map.h
206
src/bridge_map.h
@@ -171,9 +171,22 @@ static inline void MakeRoadBridgeRamp(TileIndex t, Owner o, Owner owner_road, Ow
|
||||
* @param d the direction this ramp must be facing
|
||||
* @param r the rail type of the bridge
|
||||
*/
|
||||
static inline void MakeRailBridgeRamp(TileIndex t, Owner o, BridgeType bridgetype, DiagDirection d, RailType r)
|
||||
static inline void MakeRailBridgeRamp(TileIndex t, Owner o, BridgeType bridgetype, DiagDirection d, RailType r, bool upgrade)
|
||||
{
|
||||
// Backup custom bridgehead data.
|
||||
uint custom_bridge_head_reservation_backup = GB(_m[t].m2, 0, 4);
|
||||
uint custom_bridge_head_tracks_backup = GB(_m[t].m4, 0, 6);
|
||||
|
||||
MakeBridgeRamp(t, o, bridgetype, d, TRANSPORT_RAIL, r);
|
||||
|
||||
if (upgrade) {
|
||||
// Restore custom bridgehead data if we're upgrading an existing bridge.
|
||||
SB(_m[t].m2, 0, 4, custom_bridge_head_reservation_backup);
|
||||
SB(_m[t].m4, 0, 6, custom_bridge_head_tracks_backup);
|
||||
} else {
|
||||
// Set bridge head tracks to axial track only.
|
||||
SB(_m[t].m4, 0, 6, DiagDirToDiagTrackBits(d));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -255,4 +268,195 @@ static inline void SetCustomBridgeHeadRoadBits(TileIndex t, RoadType rt, RoadBit
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this tile is a rail bridge head
|
||||
* @param t The tile to analyze
|
||||
* @return true if it is a rail bridge head
|
||||
*/
|
||||
static inline bool IsRailBridgeHeadTile(TileIndex t)
|
||||
{
|
||||
return IsBridgeTile(t) && (TransportType)GB(_m[t].m5, 2, 2) == TRANSPORT_RAIL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this tile is a flat rail bridge head
|
||||
* @param t The tile to analyze
|
||||
* @return true if it is a flat rail bridge head
|
||||
*/
|
||||
static inline bool IsFlatRailBridgeHeadTile(TileIndex t)
|
||||
{
|
||||
return IsRailBridgeHeadTile(t) && HasBridgeFlatRamp(GetTileSlope(t), DiagDirToAxis((DiagDirection)GB(_m[t].m5, 0, 2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the track bits for a (possibly custom) rail bridge head
|
||||
* @param tile the tile to get the track bits from
|
||||
* @pre IsRailBridgeHeadTile(t)
|
||||
* @return road bits for the bridge head
|
||||
*/
|
||||
static inline TrackBits GetCustomBridgeHeadTrackBits(TileIndex t)
|
||||
{
|
||||
assert(IsRailBridgeHeadTile(t));
|
||||
return (TrackBits)GB(_m[t].m4, 0, 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the road track for a (possibly custom) rail bridge head
|
||||
* @param t the tile to set the track bits of
|
||||
* @param b the new track bits for the tile
|
||||
* @pre IsRailBridgeHeadTile(t)
|
||||
*/
|
||||
static inline void SetCustomBridgeHeadTrackBits(TileIndex t, TrackBits b)
|
||||
{
|
||||
assert(IsRailBridgeHeadTile(t));
|
||||
SB(_m[t].m4, 0, 6, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this rail bridge head is a custom bridge head
|
||||
* @param t The tile to analyze
|
||||
* @pre IsRailBridgeHeadTile(t)
|
||||
* @return true if it is a custom bridge head
|
||||
*/
|
||||
static inline bool IsRailCustomBridgeHead(TileIndex t)
|
||||
{
|
||||
assert(IsRailBridgeHeadTile(t));
|
||||
return GetCustomBridgeHeadTrackBits(t) != DiagDirToDiagTrackBits((DiagDirection)GB(_m[t].m5, 0, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this tile is a rail bridge head with a custom bridge head
|
||||
* @param t The tile to analyze
|
||||
* @return true if it is a rail bridge head with a custom bridge head
|
||||
*/
|
||||
static inline bool IsRailCustomBridgeHeadTile(TileIndex t)
|
||||
{
|
||||
return IsRailBridgeHeadTile(t) && IsRailCustomBridgeHead(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this tile is a bridge head with a custom bridge head
|
||||
* @param t The tile to analyze
|
||||
* @return true if it is a bridge head with a custom bridge head
|
||||
*/
|
||||
static inline bool IsCustomBridgeHeadTile(TileIndex t)
|
||||
{
|
||||
return IsRailCustomBridgeHeadTile(t) || IsRoadCustomBridgeHeadTile(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the reserved track bits for a rail bridge head
|
||||
* @pre IsRailBridgeHeadTile(t)
|
||||
* @param t the tile
|
||||
* @return reserved track bits
|
||||
*/
|
||||
static inline TrackBits GetBridgeReservationTrackBits(TileIndex t)
|
||||
{
|
||||
assert(IsRailBridgeHeadTile(t));
|
||||
byte track_b = GB(_m[t].m2, 0, 3);
|
||||
Track track = (Track)(track_b - 1); // map array saves Track+1
|
||||
if (track_b == 0) return TRACK_BIT_NONE;
|
||||
return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 3) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the reserved track bits of the rail bridge head
|
||||
* @pre IsRailBridgeHeadTile(t)
|
||||
* @param t the tile to change
|
||||
* @param b the track bits
|
||||
*/
|
||||
static inline void SetBridgeReservationTrackBits(TileIndex t, TrackBits b)
|
||||
{
|
||||
assert(IsRailBridgeHeadTile(t));
|
||||
assert(!TracksOverlap(b));
|
||||
Track track = RemoveFirstTrack(&b);
|
||||
SB(_m[t].m2, 0, 3, track == INVALID_TRACK ? 0 : track + 1);
|
||||
SB(_m[t].m2, 3, 1, (byte)(b != TRACK_BIT_NONE));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Try to reserve a specific track on a rail bridge head tile
|
||||
* @pre IsRailBridgeHeadTile(t) && HasBit(GetCustomBridgeHeadTrackBits(tile), t)
|
||||
* @param tile the tile
|
||||
* @param t the rack to reserve
|
||||
* @return true if successful
|
||||
*/
|
||||
static inline bool TryReserveRailBridgeHead(TileIndex tile, Track t)
|
||||
{
|
||||
assert(IsRailBridgeHeadTile(tile));
|
||||
assert(HasBit(GetCustomBridgeHeadTrackBits(tile), t));
|
||||
TrackBits bits = TrackToTrackBits(t);
|
||||
TrackBits res = GetBridgeReservationTrackBits(tile);
|
||||
if ((res & bits) != TRACK_BIT_NONE) return false; // already reserved
|
||||
res |= bits;
|
||||
if (TracksOverlap(res)) return false; // crossing reservation present
|
||||
SetBridgeReservationTrackBits(tile, res);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Lift the reservation of a specific track on a rail bridge head tile
|
||||
* @pre IsRailBridgeHeadTile(t) && HasBit(GetCustomBridgeHeadTrackBits(tile), t)
|
||||
* @param tile the tile
|
||||
* @param t the track to free
|
||||
*/
|
||||
static inline void UnreserveRailBridgeHeadTrack(TileIndex tile, Track t)
|
||||
{
|
||||
assert(IsRailBridgeHeadTile(tile));
|
||||
assert(HasBit(GetCustomBridgeHeadTrackBits(tile), t));
|
||||
TrackBits res = GetBridgeReservationTrackBits(tile);
|
||||
res &= ~TrackToTrackBits(t);
|
||||
SetBridgeReservationTrackBits(tile, res);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the possible track bits of the bridge head tile onto/across the bridge
|
||||
* @pre IsRailBridgeHeadTile(t)
|
||||
* @param t the tile
|
||||
* @return reservation state
|
||||
*/
|
||||
static inline TrackBits GetAcrossBridgePossibleTrackBits(TileIndex t)
|
||||
{
|
||||
assert(IsRailBridgeHeadTile(t));
|
||||
return DiagdirReachesTracks(ReverseDiagDir((DiagDirection)GB(_m[t].m5, 0, 2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the reserved track bits of the bridge head tile onto/across the bridge
|
||||
* @pre IsBridgeTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL
|
||||
* @param t the tile
|
||||
* @return reservation state
|
||||
*/
|
||||
static inline TrackBits GetAcrossBridgeReservationTrackBits(TileIndex t)
|
||||
{
|
||||
return GetBridgeReservationTrackBits(t) & GetAcrossBridgePossibleTrackBits(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the reservation state of the bridge head tile onto/across the bridge
|
||||
* @pre IsBridgeTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL
|
||||
* @param t the tile
|
||||
* @return reservation state
|
||||
*/
|
||||
static inline bool HasAcrossBridgeReservation(TileIndex t)
|
||||
{
|
||||
return GetAcrossBridgeReservationTrackBits(t) != TRACK_BIT_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lift the reservation of a specific track on a rail bridge head tile
|
||||
* @pre IsRailBridgeHeadTile(t)
|
||||
* @param tile the tile
|
||||
*/
|
||||
static inline void UnreserveAcrossRailBridgeHead(TileIndex tile)
|
||||
{
|
||||
assert(IsRailBridgeHeadTile(tile));
|
||||
TrackBits res = GetAcrossBridgeReservationTrackBits(tile);
|
||||
if (res != TRACK_BIT_NONE) {
|
||||
SetBridgeReservationTrackBits(tile, GetBridgeReservationTrackBits(tile) & ~res);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* BRIDGE_MAP_H */
|
||||
|
Reference in New Issue
Block a user