Mercurial > hg > openttd
changeset 9224:c8a0fa0e208a draft
(svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
author | smatz <smatz@openttd.org> |
---|---|
date | Wed, 14 May 2008 18:31:21 +0000 |
parents | 0b8bbdd54e9a |
children | 1e40b8e2533d |
files | src/ai/default/default.cpp src/elrail.cpp src/rail_cmd.cpp src/rail_map.h src/road_cmd.cpp src/roadveh_cmd.cpp src/track_func.h src/train_cmd.cpp src/tunnelbridge_cmd.cpp src/vehicle.cpp src/water_cmd.cpp src/yapf/yapf_road.cpp |
diffstat | 12 files changed, 64 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ai/default/default.cpp +++ b/src/ai/default/default.cpp @@ -2198,7 +2198,7 @@ int32 bridge_len = GetTunnelBridgeLength(t1, t2); DiagDirection dir = (TileX(t1) == TileX(t2) ? DIAGDIR_SE : DIAGDIR_SW); - Track track = AxisToTrack(DiagDirToAxis(dir)); + Track track = DiagDirToDiagTrack(dir); if (t2 < t1) dir = ReverseDiagDir(dir);
--- a/src/elrail.cpp +++ b/src/elrail.cpp @@ -104,7 +104,7 @@ if (override != NULL && (IsTunnel(t) || GetTunnelBridgeLength(t, GetOtherBridgeEnd(t)) > 0)) { *override = 1 << GetTunnelBridgeDirection(t); } - return AxisToTrackBits(DiagDirToAxis(GetTunnelBridgeDirection(t))); + return DiagDirToDiagTrackBits(GetTunnelBridgeDirection(t)); case MP_ROAD: if (!IsLevelCrossing(t)) return TRACK_BIT_NONE;
--- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -778,7 +778,7 @@ d->town_index = ClosestTownFromTile(tile, (uint)-1)->index; AddSideToSignalBuffer(tile, INVALID_DIAGDIR, _current_player); - YapfNotifyTrackLayoutChange(tile, TrackdirToTrack(DiagdirToDiagTrackdir(dir))); + YapfNotifyTrackLayoutChange(tile, DiagDirToDiagTrack(dir)); } return cost.AddCost(_price.build_train_depot); @@ -1247,7 +1247,7 @@ case RAIL_TILE_WAYPOINT: if (flags & DC_EXEC) { /* notify YAPF about the track layout change */ - YapfNotifyTrackLayoutChange(tile, AxisToTrack(GetWaypointAxis(tile))); + YapfNotifyTrackLayoutChange(tile, GetRailWaypointTrack(tile)); } cost.AddCost(RailConvertCost(type, totype)); break; @@ -1255,7 +1255,7 @@ case RAIL_TILE_DEPOT: if (flags & DC_EXEC) { /* notify YAPF about the track layout change */ - YapfNotifyTrackLayoutChange(tile, AxisToTrack(DiagDirToAxis(GetRailDepotDirection(tile)))); + YapfNotifyTrackLayoutChange(tile, GetRailDepotTrack(tile)); /* Update build vehicle window related to this depot */ InvalidateWindowData(WC_VEHICLE_DEPOT, tile); @@ -1296,7 +1296,7 @@ VehicleFromPos(tile, NULL, &UpdateTrainPowerProc); VehicleFromPos(endtile, NULL, &UpdateTrainPowerProc); - Track track = AxisToTrack(DiagDirToAxis(GetTunnelBridgeDirection(tile))); + Track track = DiagDirToDiagTrack(GetTunnelBridgeDirection(tile)); YapfNotifyTrackLayoutChange(tile, track); YapfNotifyTrackLayoutChange(endtile, track); @@ -1345,7 +1345,7 @@ DoClearSquare(tile); delete GetDepotByTile(tile); AddSideToSignalBuffer(tile, dir, owner); - YapfNotifyTrackLayoutChange(tile, TrackdirToTrack(DiagdirToDiagTrackdir(dir))); + YapfNotifyTrackLayoutChange(tile, DiagDirToDiagTrack(dir)); } return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_train_depot); @@ -2150,7 +2150,7 @@ if (side != INVALID_DIAGDIR && side != dir) break; - trackbits = AxisToTrackBits(DiagDirToAxis(dir)); + trackbits = DiagDirToDiagTrackBits(dir); break; }
--- a/src/rail_map.h +++ b/src/rail_map.h @@ -168,6 +168,17 @@ return (DiagDirection)GB(_m[t].m5, 0, 2); } +/** + * Returns the track of a depot, ignoring direction + * @pre IsRailDepotTile(t) + * @param t the tile to get the depot track from + * @return the track of the depot + */ +static inline Track GetRailDepotTrack(TileIndex t) +{ + return DiagDirToDiagTrack(GetRailDepotDirection(t)); +} + /** * Returns the axis of the waypoint
--- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -1391,7 +1391,7 @@ if (side != INVALID_DIAGDIR && side != dir) break; - trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(DiagDirToAxis(dir))); + trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir)); break; } }
--- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -970,7 +970,7 @@ /* Check if vehicle is in a road stop, depot, tunnel or bridge or not on a straight road */ if (v->u.road.state >= RVSB_IN_ROAD_STOP || !IsStraightRoadTrackdir((Trackdir)(v->u.road.state & RVSB_TRACKDIR_MASK))) return; - od.trackdir = DiagdirToDiagTrackdir(DirToDiagDir(v->direction)); + od.trackdir = DiagDirToDiagTrackdir(DirToDiagDir(v->direction)); /* Are the current and the next tile suitable for overtaking? * - Does the track continue along od.trackdir @@ -1135,7 +1135,7 @@ NPFFindStationOrTileData fstd; NPFFillWithOrderData(&fstd, v); - Trackdir trackdir = DiagdirToDiagTrackdir(enterdir); + Trackdir trackdir = DiagDirToDiagTrackdir(enterdir); //debug("Finding path. Enterdir: %d, Trackdir: %d", enterdir, trackdir); NPFFoundTargetData ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPES); @@ -1325,7 +1325,7 @@ } if (diag_dir == INVALID_DIAGDIR) return INVALID_TRACKDIR; - dir = DiagdirToDiagTrackdir(diag_dir); + dir = DiagDirToDiagTrackdir(diag_dir); } else { if (already_reversed && prev->tile != tile) { /*
--- a/src/track_func.h +++ b/src/track_func.h @@ -24,8 +24,6 @@ return (Track)a; } - - /** * Maps a Track to the corresponding TrackBits value * @param track the track to convert @@ -277,7 +275,7 @@ * Converts TrackBits to TrackdirBits while allowing both directions. * * @param bits The TrackBits - * @return The TrackDirBits containing of bits in both directions. + * @return The TrackdirBits containing of bits in both directions. */ static inline TrackdirBits TrackBitsToTrackdirBits(TrackBits bits) { @@ -436,13 +434,35 @@ } /** + * Maps a (4-way) direction to the diagonal track incidating with that diagdir + * + * @param diagdir The direction + * @return The resulting Track + */ +static inline Track DiagDirToDiagTrack(DiagDirection diagdir) +{ + return (Track)(diagdir & 1); +} + +/** + * Maps a (4-way) direction to the diagonal track bits incidating with that diagdir + * + * @param diagdir The direction + * @return The resulting TrackBits + */ +static inline TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir) +{ + return TrackToTrackBits(DiagDirToDiagTrack(diagdir)); +} + +/** * Maps a (4-way) direction to the diagonal trackdir that runs in that * direction. * * @param diagdir The direction * @return The resulting Trackdir direction */ -static inline Trackdir DiagdirToDiagTrackdir(DiagDirection diagdir) +static inline Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir) { extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END]; return _dir_to_diag_trackdir[diagdir];
--- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -1764,7 +1764,7 @@ if (d <= 0) { leave->vehstatus &= ~VS_HIDDEN; // move it out of the depot - leave->u.rail.track = AxisToTrackBits(DiagDirToAxis(GetRailDepotDirection(leave->tile))); + leave->u.rail.track = TrackToTrackBits(GetRailDepotTrack(leave->tile)); for (int i = 0; i >= d; i--) TrainController(leave, NULL, false); // maybe move it, and maybe let another wagon leave } } else {
--- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -494,7 +494,7 @@ delta = TileOffsByDiagDir(direction); DiagDirection tunnel_in_way_dir; - if (OtherAxis(DiagDirToAxis(direction)) == AXIS_X) { + if (DiagDirToAxis(direction) == AXIS_Y) { tunnel_in_way_dir = (TileX(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE; } else { tunnel_in_way_dir = (TileY(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW; @@ -558,7 +558,7 @@ MakeRailTunnel(start_tile, _current_player, direction, (RailType)GB(p1, 0, 4)); MakeRailTunnel(end_tile, _current_player, ReverseDiagDir(direction), (RailType)GB(p1, 0, 4)); AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, _current_player); - YapfNotifyTrackLayoutChange(start_tile, AxisToTrack(DiagDirToAxis(direction))); + YapfNotifyTrackLayoutChange(start_tile, DiagDirToDiagTrack(direction)); } else { MakeRoadTunnel(start_tile, _current_player, direction, (RoadTypes)GB(p1, 0, 3)); MakeRoadTunnel(end_tile, _current_player, ReverseDiagDir(direction), (RoadTypes)GB(p1, 0, 3)); @@ -623,7 +623,7 @@ AddSideToSignalBuffer(tile, ReverseDiagDir(dir), owner); AddSideToSignalBuffer(endtile, dir, owner); - Track track = AxisToTrack(DiagDirToAxis(dir)); + Track track = DiagDirToDiagTrack(dir); YapfNotifyTrackLayoutChange(tile, track); YapfNotifyTrackLayoutChange(endtile, track); } else { @@ -691,7 +691,7 @@ AddSideToSignalBuffer(tile, ReverseDiagDir(direction), owner); AddSideToSignalBuffer(endtile, direction, owner); - Track track = AxisToTrack(DiagDirToAxis(direction)); + Track track = DiagDirToDiagTrack(direction); YapfNotifyTrackLayoutChange(tile, track); YapfNotifyTrackLayoutChange(endtile, track); } @@ -1233,7 +1233,7 @@ DiagDirection dir = GetTunnelBridgeDirection(tile); if (side != INVALID_DIAGDIR && side != ReverseDiagDir(dir)) return 0; - return CombineTrackStatus(TrackBitsToTrackdirBits(AxisToTrackBits(DiagDirToAxis(dir))), TRACKDIR_BIT_NONE); + return CombineTrackStatus(TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir)), TRACKDIR_BIT_NONE); } static void ChangeTileOwner_TunnelBridge(TileIndex tile, PlayerID old_player, PlayerID new_player)
--- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1867,34 +1867,34 @@ switch (v->type) { case VEH_TRAIN: if (v->u.rail.track == TRACK_BIT_DEPOT) // We'll assume the train is facing outwards - return DiagdirToDiagTrackdir(GetRailDepotDirection(v->tile)); // Train in depot + return DiagDirToDiagTrackdir(GetRailDepotDirection(v->tile)); // Train in depot if (v->u.rail.track == TRACK_BIT_WORMHOLE) // train in tunnel, so just use his direction and assume a diagonal track - return DiagdirToDiagTrackdir(DirToDiagDir(v->direction)); + return DiagDirToDiagTrackdir(DirToDiagDir(v->direction)); return TrackDirectionToTrackdir(FindFirstTrack(v->u.rail.track), v->direction); case VEH_SHIP: if (v->IsInDepot()) // We'll assume the ship is facing outwards - return DiagdirToDiagTrackdir(GetShipDepotDirection(v->tile)); + return DiagDirToDiagTrackdir(GetShipDepotDirection(v->tile)); return TrackDirectionToTrackdir(FindFirstTrack(v->u.ship.state), v->direction); case VEH_ROAD: if (v->IsInDepot()) // We'll assume the road vehicle is facing outwards - return DiagdirToDiagTrackdir(GetRoadDepotDirection(v->tile)); + return DiagDirToDiagTrackdir(GetRoadDepotDirection(v->tile)); if (IsStandardRoadStopTile(v->tile)) // We'll assume the road vehicle is facing outwards - return DiagdirToDiagTrackdir(GetRoadStopDir(v->tile)); // Road vehicle in a station - - if (IsDriveThroughStopTile(v->tile)) return DiagdirToDiagTrackdir(DirToDiagDir(v->direction)); + return DiagDirToDiagTrackdir(GetRoadStopDir(v->tile)); // Road vehicle in a station + + if (IsDriveThroughStopTile(v->tile)) return DiagDirToDiagTrackdir(DirToDiagDir(v->direction)); /* If vehicle's state is a valid track direction (vehicle is not turning around) return it */ if (!IsReversingRoadTrackdir((Trackdir)v->u.road.state)) return (Trackdir)v->u.road.state; /* Vehicle is turning around, get the direction from vehicle's direction */ - return DiagdirToDiagTrackdir(DirToDiagDir(v->direction)); + return DiagDirToDiagTrackdir(DirToDiagDir(v->direction)); /* case VEH_AIRCRAFT: case VEH_EFFECT: case VEH_DISASTER: */ default: return INVALID_TRACKDIR;
--- a/src/water_cmd.cpp +++ b/src/water_cmd.cpp @@ -1123,7 +1123,7 @@ switch (GetWaterTileType(tile)) { case WATER_TILE_CLEAR: ts = (GetTileSlope(tile, NULL) == SLOPE_FLAT) ? TRACK_BIT_ALL : TRACK_BIT_NONE; break; case WATER_TILE_COAST: ts = (TrackBits)coast_tracks[GetTileSlope(tile, NULL) & 0xF]; break; - case WATER_TILE_LOCK: ts = AxisToTrackBits(DiagDirToAxis(GetLockDirection(tile))); break; + case WATER_TILE_LOCK: ts = DiagDirToDiagTrackBits(GetLockDirection(tile)); break; case WATER_TILE_DEPOT: ts = AxisToTrackBits(GetShipDepotAxis(tile)); break; default: return 0; }
--- a/src/yapf/yapf_road.cpp +++ b/src/yapf/yapf_road.cpp @@ -88,7 +88,7 @@ if (v->current_order.IsType(OT_GOTO_STATION) && tile == v->dest_tile) break; // stop if we have just entered the depot - if (IsRoadDepotTile(tile) && trackdir == DiagdirToDiagTrackdir(ReverseDiagDir(GetRoadDepotDirection(tile)))) { + if (IsRoadDepotTile(tile) && trackdir == DiagDirToDiagTrackdir(ReverseDiagDir(GetRoadDepotDirection(tile)))) { // next time we will reverse and leave the depot break; } @@ -264,7 +264,7 @@ // handle special case - when next tile is destination tile if (tile == v->dest_tile) { // choose diagonal trackdir reachable from enterdir - return (Trackdir)DiagdirToDiagTrackdir(enterdir); + return DiagDirToDiagTrackdir(enterdir); } // our source tile will be the next vehicle tile (should be the given one) TileIndex src_tile = tile;