mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r5582) Add and use AxisToTrack{Bits,}()
This commit is contained in:
parent
475f276769
commit
7ec704564a
|
@ -115,10 +115,15 @@ static inline RoadBits GetRoadBitsUnderBridge(TileIndex t)
|
|||
return GetBridgeAxis(t) == AXIS_X ? ROAD_Y : ROAD_X;
|
||||
}
|
||||
|
||||
static inline TrackBits GetRailBitsUnderBridge(TileIndex t)
|
||||
static inline Track GetRailUnderBridge(TileIndex t)
|
||||
{
|
||||
assert(GetTransportTypeUnderBridge(t) == TRANSPORT_RAIL);
|
||||
return GetBridgeAxis(t) == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X;
|
||||
return AxisToTrack(OtherAxis(GetBridgeAxis(t)));
|
||||
}
|
||||
|
||||
static inline TrackBits GetRailBitsUnderBridge(TileIndex t)
|
||||
{
|
||||
return TrackToTrackBits(GetRailUnderBridge(t));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -98,6 +98,12 @@ typedef enum Axis {
|
|||
} Axis;
|
||||
|
||||
|
||||
static inline Axis OtherAxis(Axis a)
|
||||
{
|
||||
return (Axis)(a ^ 1);
|
||||
}
|
||||
|
||||
|
||||
static inline Axis DiagDirToAxis(DiagDirection d)
|
||||
{
|
||||
return (Axis)(d & 1);
|
||||
|
|
4
elrail.c
4
elrail.c
|
@ -89,7 +89,7 @@ static TrackBits GetRailTrackBitsUniversal(TileIndex t, byte *override)
|
|||
if (IsTunnel(t)) {
|
||||
if (GetRailType(t) != RAILTYPE_ELECTRIC) return 0;
|
||||
if (override != NULL) *override = 1 << GetTunnelDirection(t);
|
||||
return DiagDirToAxis(GetTunnelDirection(t)) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y;
|
||||
return AxisToTrackBits(DiagDirToAxis(GetTunnelDirection(t)));
|
||||
} else {
|
||||
if (GetRailType(t) != RAILTYPE_ELECTRIC) return 0;
|
||||
if (IsBridgeMiddle(t)) {
|
||||
|
@ -102,7 +102,7 @@ static TrackBits GetRailTrackBitsUniversal(TileIndex t, byte *override)
|
|||
} else {
|
||||
if (override != NULL && DistanceMax(t, GetOtherBridgeEnd(t)) > 1) *override = 1 << GetBridgeRampDirection(t);
|
||||
|
||||
return DiagDirToAxis(GetBridgeRampDirection(t)) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y;
|
||||
return AxisToTrackBits(DiagDirToAxis(GetBridgeRampDirection(t)));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
31
rail.h
31
rail.h
|
@ -34,6 +34,17 @@ typedef enum Track {
|
|||
} Track;
|
||||
|
||||
|
||||
/** Convert an Axis to the corresponding Track
|
||||
* AXIS_X -> TRACK_X
|
||||
* AXIS_Y -> TRACK_Y
|
||||
* Uses the fact that they share the same internal encoding
|
||||
*/
|
||||
static inline Track AxisToTrack(Axis a)
|
||||
{
|
||||
return (Track)a;
|
||||
}
|
||||
|
||||
|
||||
/** Bitfield corresponding to Track */
|
||||
typedef enum TrackBits {
|
||||
TRACK_BIT_NONE = 0U,
|
||||
|
@ -55,6 +66,21 @@ typedef enum TrackBits {
|
|||
} TrackBits;
|
||||
|
||||
|
||||
/**
|
||||
* Maps a Track to the corresponding TrackBits value
|
||||
*/
|
||||
static inline TrackBits TrackToTrackBits(Track track)
|
||||
{
|
||||
return (TrackBits)(1 << track);
|
||||
}
|
||||
|
||||
|
||||
static inline TrackBits AxisToTrackBits(Axis a)
|
||||
{
|
||||
return TrackToTrackBits(AxisToTrack(a));
|
||||
}
|
||||
|
||||
|
||||
/** These are a combination of tracks and directions. Values are 0-5 in one
|
||||
direction (corresponding to the Track enum) and 8-13 in the other direction. */
|
||||
typedef enum Trackdirs {
|
||||
|
@ -241,11 +267,6 @@ static inline Trackdir ReverseTrackdir(Trackdir trackdir) {
|
|||
return (Trackdir)(trackdir ^ 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a Track to the corresponding TrackBits value
|
||||
*/
|
||||
static inline TrackBits TrackToTrackBits(Track track) { return (TrackBits)(1 << track); }
|
||||
|
||||
/**
|
||||
* Returns the Track that a given Trackdir represents
|
||||
*/
|
||||
|
|
|
@ -234,7 +234,7 @@ int32 CmdBuildSingleRail(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
|||
case MP_TUNNELBRIDGE:
|
||||
if (!IsBridge(tile) ||
|
||||
!IsBridgeMiddle(tile) ||
|
||||
(GetBridgeAxis(tile) == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X) != trackbit) {
|
||||
AxisToTrackBits(OtherAxis(GetBridgeAxis(tile))) != trackbit) {
|
||||
// Get detailed error message
|
||||
return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
||||
}
|
||||
|
@ -1918,7 +1918,7 @@ static uint32 GetTileTrackStatus_Track(TileIndex tile, TransportType mode)
|
|||
return ret;
|
||||
} else {
|
||||
if (GetRailTileSubtype(tile) == RAIL_SUBTYPE_DEPOT) {
|
||||
return (DiagDirToAxis(GetRailDepotDirection(tile)) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y) * 0x101;
|
||||
return AxisToTrackBits(DiagDirToAxis(GetRailDepotDirection(tile))) * 0x101;
|
||||
} else {
|
||||
return GetRailWaypointBits(tile) * 0x101;
|
||||
}
|
||||
|
|
20
rail_map.h
20
rail_map.h
|
@ -126,22 +126,22 @@ static inline DiagDirection GetRailDepotDirection(TileIndex t)
|
|||
return (DiagDirection)GB(_m[t].m5, 0, 2);
|
||||
}
|
||||
|
||||
static inline Track GetRailWaypointTrack(TileIndex t)
|
||||
{
|
||||
return HASBIT(_m[t].m5, 0) ? TRACK_Y : TRACK_X;
|
||||
}
|
||||
|
||||
static inline TrackBits GetRailWaypointBits(TileIndex t)
|
||||
{
|
||||
return _m[t].m5 & 1 ? TRACK_BIT_Y : TRACK_BIT_X;
|
||||
}
|
||||
|
||||
|
||||
static inline Axis GetWaypointAxis(TileIndex t)
|
||||
{
|
||||
return HASBIT(_m[t].m5, 0) ? AXIS_Y : AXIS_X;
|
||||
}
|
||||
|
||||
static inline Track GetRailWaypointTrack(TileIndex t)
|
||||
{
|
||||
return AxisToTrack(GetWaypointAxis(t));
|
||||
}
|
||||
|
||||
static inline TrackBits GetRailWaypointBits(TileIndex t)
|
||||
{
|
||||
return TrackToTrackBits(GetRailWaypointTrack(t));
|
||||
}
|
||||
|
||||
|
||||
typedef enum SignalType {
|
||||
SIGTYPE_NORMAL = 0, // normal signal
|
||||
|
|
|
@ -991,7 +991,7 @@ static uint32 GetTileTrackStatus_Road(TileIndex tile, TransportType mode)
|
|||
return HasRoadWorks(tile) ? 0 : _road_trackbits[GetRoadBits(tile)] * 0x101;
|
||||
|
||||
case ROAD_TILE_CROSSING: {
|
||||
uint32 r = (GetCrossingRoadAxis(tile) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y) * 0x101;
|
||||
uint32 r = AxisToTrackBits(GetCrossingRoadAxis(tile)) * 0x101;
|
||||
|
||||
if (IsCrossingBarred(tile)) r *= 0x10001;
|
||||
return r;
|
||||
|
@ -999,7 +999,7 @@ static uint32 GetTileTrackStatus_Road(TileIndex tile, TransportType mode)
|
|||
|
||||
default:
|
||||
case ROAD_TILE_DEPOT:
|
||||
return (DiagDirToAxis(GetRoadDepotDirection(tile)) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y) * 0x101;
|
||||
return AxisToTrackBits(DiagDirToAxis(GetRoadDepotDirection(tile))) * 0x101;
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ static inline RoadBits GetCrossingRoadBits(TileIndex tile)
|
|||
|
||||
static inline TrackBits GetCrossingRailBits(TileIndex tile)
|
||||
{
|
||||
return GetCrossingRoadAxis(tile) == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X;
|
||||
return AxisToTrackBits(OtherAxis(GetCrossingRoadAxis(tile)));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1087,7 +1087,7 @@ int32 CmdBuildRailroadStation(TileIndex tile_org, uint32 flags, uint32 p1, uint3
|
|||
st->build_date = _date;
|
||||
|
||||
tile_delta = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
|
||||
track = (axis == AXIS_X ? TRACK_X : TRACK_Y);
|
||||
track = AxisToTrack(axis);
|
||||
|
||||
layout_ptr = alloca(numtracks * plat_len);
|
||||
GetStationLayout(layout_ptr, numtracks, plat_len, statspec);
|
||||
|
@ -2216,7 +2216,7 @@ static uint32 GetTileTrackStatus_Station(TileIndex tile, TransportType mode)
|
|||
|
||||
case TRANSPORT_ROAD:
|
||||
if (IsRoadStopTile(tile)) {
|
||||
return (DiagDirToAxis(GetRoadStopDir(tile)) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y) * 0x101;
|
||||
return AxisToTrackBits(DiagDirToAxis(GetRoadStopDir(tile))) * 0x101;
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
|
@ -188,7 +188,7 @@ static inline Axis GetRailStationAxis(TileIndex t)
|
|||
|
||||
static inline Track GetRailStationTrack(TileIndex t)
|
||||
{
|
||||
return GetRailStationAxis(t) == AXIS_X ? TRACK_X : TRACK_Y;
|
||||
return AxisToTrack(GetRailStationAxis(t));
|
||||
}
|
||||
|
||||
static inline bool IsCompatibleTrainStationTile(TileIndex t1, TileIndex t2)
|
||||
|
|
|
@ -335,7 +335,7 @@ int32 CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
|
|||
|
||||
case MP_RAILWAY:
|
||||
if (GetRailTileType(tile) != RAIL_TILE_NORMAL ||
|
||||
GetTrackBits(tile) != (direction == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X)) {
|
||||
GetTrackBits(tile) != AxisToTrackBits(OtherAxis(direction))) {
|
||||
goto not_valid_below;
|
||||
}
|
||||
transport_under = TRANSPORT_RAIL;
|
||||
|
@ -415,8 +415,8 @@ not_valid_below:;
|
|||
}
|
||||
}
|
||||
|
||||
SetSignalsOnBothDir(tile_start, direction == AXIS_X ? TRACK_X : TRACK_Y);
|
||||
YapfNotifyTrackLayoutChange(tile_start, direction == AXIS_X ? TRACK_X : TRACK_Y);
|
||||
SetSignalsOnBothDir(tile_start, AxisToTrack(direction));
|
||||
YapfNotifyTrackLayoutChange(tile_start, AxisToTrack(direction));
|
||||
|
||||
/* for human player that builds the bridge he gets a selection to choose from bridges (DC_QUERY_COST)
|
||||
It's unnecessary to execute this command every time for every bridge. So it is done only
|
||||
|
@ -507,7 +507,7 @@ int32 CmdBuildTunnel(TileIndex start_tile, uint32 flags, uint32 p1, uint32 p2)
|
|||
MakeRailTunnel(start_tile, _current_player, direction, GB(p1, 0, 4));
|
||||
MakeRailTunnel(end_tile, _current_player, ReverseDiagDir(direction), GB(p1, 0, 4));
|
||||
UpdateSignalsOnSegment(start_tile, direction);
|
||||
YapfNotifyTrackLayoutChange(start_tile, DiagDirToAxis(direction) == AXIS_X ? TRACK_X : TRACK_Y);
|
||||
YapfNotifyTrackLayoutChange(start_tile, AxisToTrack(DiagDirToAxis(direction)));
|
||||
} else {
|
||||
MakeRoadTunnel(start_tile, _current_player, direction);
|
||||
MakeRoadTunnel(end_tile, _current_player, ReverseDiagDir(direction));
|
||||
|
@ -579,6 +579,7 @@ static int32 DoClearTunnel(TileIndex tile, uint32 flags)
|
|||
// We first need to request the direction before calling DoClearSquare
|
||||
// else the direction is always 0.. dah!! ;)
|
||||
DiagDirection dir = GetTunnelDirection(tile);
|
||||
Track track;
|
||||
|
||||
// Adjust the town's player rating. Do this before removing the tile owner info.
|
||||
if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR)
|
||||
|
@ -588,8 +589,9 @@ static int32 DoClearTunnel(TileIndex tile, uint32 flags)
|
|||
DoClearSquare(endtile);
|
||||
UpdateSignalsOnSegment(tile, ReverseDiagDir(dir));
|
||||
UpdateSignalsOnSegment(endtile, dir);
|
||||
YapfNotifyTrackLayoutChange(tile, DiagDirToAxis(dir) == AXIS_X ? TRACK_X : TRACK_Y);
|
||||
YapfNotifyTrackLayoutChange(endtile, DiagDirToAxis(dir) == AXIS_X ? TRACK_X : TRACK_Y);
|
||||
track = AxisToTrack(DiagDirToAxis(dir));
|
||||
YapfNotifyTrackLayoutChange(tile, track);
|
||||
YapfNotifyTrackLayoutChange(endtile, track);
|
||||
}
|
||||
return _price.clear_tunnel * (length + 1);
|
||||
}
|
||||
|
@ -687,6 +689,7 @@ static int32 DoClearBridge(TileIndex tile, uint32 flags)
|
|||
|
||||
if (flags & DC_EXEC) {
|
||||
TileIndex c;
|
||||
Track track;
|
||||
|
||||
//checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
|
||||
// you have a "Poor" (0) town rating
|
||||
|
@ -724,8 +727,9 @@ static int32 DoClearBridge(TileIndex tile, uint32 flags)
|
|||
|
||||
UpdateSignalsOnSegment(tile, ReverseDiagDir(direction));
|
||||
UpdateSignalsOnSegment(endtile, direction);
|
||||
YapfNotifyTrackLayoutChange(tile, DiagDirToAxis(direction) == AXIS_X ? TRACK_X : TRACK_Y);
|
||||
YapfNotifyTrackLayoutChange(endtile, DiagDirToAxis(direction) == AXIS_X ? TRACK_X : TRACK_Y);
|
||||
track = AxisToTrack(DiagDirToAxis(direction));
|
||||
YapfNotifyTrackLayoutChange(tile, track);
|
||||
YapfNotifyTrackLayoutChange(endtile, track);
|
||||
}
|
||||
|
||||
return (DistanceManhattan(tile, endtile) + 1) * _price.clear_bridge;
|
||||
|
@ -759,17 +763,15 @@ int32 DoConvertTunnelBridgeRail(TileIndex tile, RailType totype, bool exec)
|
|||
if (endtile == INVALID_TILE) return CMD_ERROR;
|
||||
|
||||
if (exec) {
|
||||
Track track, endtrack;
|
||||
Track track;
|
||||
SetRailType(tile, totype);
|
||||
SetRailType(endtile, totype);
|
||||
MarkTileDirtyByTile(tile);
|
||||
MarkTileDirtyByTile(endtile);
|
||||
|
||||
// notify YAPF about the track layout change
|
||||
track = TrackdirToTrack(DiagdirToDiagTrackdir(GetTunnelDirection(tile)));
|
||||
endtrack = TrackdirToTrack(DiagdirToDiagTrackdir(GetTunnelDirection(endtile)));
|
||||
track = AxisToTrack(DiagDirToAxis(GetTunnelDirection(tile)));
|
||||
YapfNotifyTrackLayoutChange(tile, track);
|
||||
YapfNotifyTrackLayoutChange(endtile, endtrack);
|
||||
YapfNotifyTrackLayoutChange(endtile, track);
|
||||
}
|
||||
return (length + 1) * (_price.build_rail >> 1);
|
||||
} else if (IsBridge(tile) &&
|
||||
|
@ -783,13 +785,10 @@ int32 DoConvertTunnelBridgeRail(TileIndex tile, RailType totype, bool exec)
|
|||
if (GetRailType(tile) == totype) return CMD_ERROR;
|
||||
|
||||
if (exec) {
|
||||
TrackBits tracks;
|
||||
SetRailType(tile, totype);
|
||||
MarkTileDirtyByTile(tile);
|
||||
|
||||
// notify YAPF about the track layout change
|
||||
for (tracks = GetRailBitsUnderBridge(tile); tracks != TRACK_BIT_NONE; tracks = KILL_FIRST_BIT(tracks))
|
||||
YapfNotifyTrackLayoutChange(tile, FIND_FIRST_BIT(tracks));
|
||||
YapfNotifyTrackLayoutChange(tile, GetRailUnderBridge(tile));
|
||||
}
|
||||
return _price.build_rail >> 1;
|
||||
} else if (IsBridge(tile) && IsBridgeRamp(tile) && GetBridgeTransportType(tile) == TRANSPORT_RAIL) {
|
||||
|
@ -813,17 +812,15 @@ int32 DoConvertTunnelBridgeRail(TileIndex tile, RailType totype, bool exec)
|
|||
if (GetRailType(tile) == totype) return CMD_ERROR;
|
||||
|
||||
if (exec) {
|
||||
Track track, endtrack;
|
||||
Track track;
|
||||
SetRailType(tile, totype);
|
||||
SetRailType(endtile, totype);
|
||||
MarkTileDirtyByTile(tile);
|
||||
MarkTileDirtyByTile(endtile);
|
||||
|
||||
// notify YAPF about the track layout change
|
||||
track = TrackdirToTrack(DiagdirToDiagTrackdir(GetBridgeRampDirection(tile)));
|
||||
endtrack = TrackdirToTrack(DiagdirToDiagTrackdir(GetBridgeRampDirection(endtile)));
|
||||
track = AxisToTrack(DiagDirToAxis(GetBridgeRampDirection(tile)));
|
||||
YapfNotifyTrackLayoutChange(tile, track);
|
||||
YapfNotifyTrackLayoutChange(endtile, endtrack);
|
||||
YapfNotifyTrackLayoutChange(endtile, track);
|
||||
}
|
||||
cost = 2 * (_price.build_rail >> 1);
|
||||
delta = TileOffsByDir(GetBridgeRampDirection(tile));
|
||||
|
@ -1273,20 +1270,20 @@ static uint32 GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode
|
|||
{
|
||||
if (IsTunnel(tile)) {
|
||||
if (GetTunnelTransportType(tile) != mode) return 0;
|
||||
return (DiagDirToAxis(GetTunnelDirection(tile)) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y) * 0x101;
|
||||
return AxisToTrackBits(DiagDirToAxis(GetTunnelDirection(tile))) * 0x101;
|
||||
} else {
|
||||
if (IsBridgeRamp(tile)) {
|
||||
if (GetBridgeTransportType(tile) != mode) return 0;
|
||||
return (DiagDirToAxis(GetBridgeRampDirection(tile)) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y) * 0x101;
|
||||
return AxisToTrackBits(DiagDirToAxis(GetBridgeRampDirection(tile))) * 0x101;
|
||||
} else {
|
||||
uint32 result = 0;
|
||||
|
||||
if (GetBridgeTransportType(tile) == mode) {
|
||||
result = (GetBridgeAxis(tile) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y) * 0x101;
|
||||
result = AxisToTrackBits(GetBridgeAxis(tile)) * 0x101;
|
||||
}
|
||||
if ((IsTransportUnderBridge(tile) && mode == GetTransportTypeUnderBridge(tile)) ||
|
||||
(IsWaterUnderBridge(tile) && mode == TRANSPORT_WATER)) {
|
||||
result |= (GetBridgeAxis(tile) == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X) * 0x101;
|
||||
result |= AxisToTrackBits(OtherAxis(GetBridgeAxis(tile))) * 0x101;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue