Mercurial > hg > openttd
changeset 4158:d85be5d5df04 draft
(svn r5582) Add and use AxisToTrack{Bits,}()
author | tron <tron@openttd.org> |
---|---|
date | Sat, 22 Jul 2006 08:59:52 +0000 |
parents | b0e2004122f9 |
children | da28073983dd |
files | bridge_map.h direction.h elrail.c rail.h rail_cmd.c rail_map.h road_cmd.c road_map.h station_cmd.c station_map.h tunnelbridge_cmd.c |
diffstat | 11 files changed, 80 insertions(+), 51 deletions(-) [+] |
line wrap: on
line diff
--- a/bridge_map.h +++ b/bridge_map.h @@ -115,10 +115,15 @@ return GetBridgeAxis(t) == AXIS_X ? ROAD_Y : ROAD_X; } +static inline Track GetRailUnderBridge(TileIndex t) +{ + assert(GetTransportTypeUnderBridge(t) == TRANSPORT_RAIL); + return AxisToTrack(OtherAxis(GetBridgeAxis(t))); +} + static inline TrackBits GetRailBitsUnderBridge(TileIndex t) { - assert(GetTransportTypeUnderBridge(t) == TRANSPORT_RAIL); - return GetBridgeAxis(t) == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X; + return TrackToTrackBits(GetRailUnderBridge(t)); }
--- a/direction.h +++ b/direction.h @@ -98,6 +98,12 @@ } Axis; +static inline Axis OtherAxis(Axis a) +{ + return (Axis)(a ^ 1); +} + + static inline Axis DiagDirToAxis(DiagDirection d) { return (Axis)(d & 1);
--- a/elrail.c +++ b/elrail.c @@ -89,7 +89,7 @@ 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 @@ } 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))); } }
--- a/rail.h +++ b/rail.h @@ -34,6 +34,17 @@ } 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 @@ } 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 { @@ -242,11 +268,6 @@ } /** - * 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 */ static inline Track TrackdirToTrack(Trackdir trackdir) { return (Track)(trackdir & 0x7); }
--- a/rail_cmd.c +++ b/rail_cmd.c @@ -234,7 +234,7 @@ 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 @@ 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; }
--- a/rail_map.h +++ b/rail_map.h @@ -126,20 +126,20 @@ return (DiagDirection)GB(_m[t].m5, 0, 2); } + +static inline Axis GetWaypointAxis(TileIndex t) +{ + return HASBIT(_m[t].m5, 0) ? AXIS_Y : AXIS_X; +} + static inline Track GetRailWaypointTrack(TileIndex t) { - return HASBIT(_m[t].m5, 0) ? TRACK_Y : TRACK_X; + return AxisToTrack(GetWaypointAxis(t)); } 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; + return TrackToTrackBits(GetRailWaypointTrack(t)); }
--- a/road_cmd.c +++ b/road_cmd.c @@ -991,7 +991,7 @@ 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 @@ 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;
--- a/road_map.h +++ b/road_map.h @@ -57,7 +57,7 @@ static inline TrackBits GetCrossingRailBits(TileIndex tile) { - return GetCrossingRoadAxis(tile) == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X; + return AxisToTrackBits(OtherAxis(GetCrossingRoadAxis(tile))); }
--- a/station_cmd.c +++ b/station_cmd.c @@ -1087,7 +1087,7 @@ 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 @@ 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;
--- a/station_map.h +++ b/station_map.h @@ -188,7 +188,7 @@ 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)
--- a/tunnelbridge_cmd.c +++ b/tunnelbridge_cmd.c @@ -335,7 +335,7 @@ 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 @@ } } - 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 @@ 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 @@ // 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ { 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; }