# HG changeset patch # User smatz # Date 1200064259 0 # Node ID 2174c426ac5a50515efa9d1eaf5a0b904a06d75c # Parent 16e1c8db6f55af92372947b33d2a2b95ccd0e213 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions diff -r 16e1c8db6f55 -r 2174c426ac5a src/train_cmd.cpp --- a/src/train_cmd.cpp Fri Jan 11 00:30:32 2008 +0000 +++ b/src/train_cmd.cpp Fri Jan 11 15:10:59 2008 +0000 @@ -54,7 +54,28 @@ static const byte _vehicle_initial_x_fract[4] = {10, 8, 4, 8}; static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10}; -static const TrackBits _state_dir_table[4] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER }; + + +/** + * Determine the side in which the train will leave the tile + * + * @param direction vehicle direction + * @param track vehicle track bits + * @return side of tile the train will leave + */ +static inline DiagDirection TrainExitDir(Direction direction, TrackBits track) +{ + static const TrackBits state_dir_table[DIAGDIR_END] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER }; + + DiagDirection diagdir = DirToDiagDir(direction); + + /* Determine the diagonal direction in which we will exit this tile */ + if (!HasBit(direction, 0) && track != state_dir_table[diagdir]) { + diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT); + } + + return diagdir; +} /** Return the cargo weight multiplier to use for a rail vehicle @@ -1666,16 +1687,10 @@ /* Check if we were approaching a rail/road-crossing */ { - TileIndex tile = v->tile; - DiagDirection dir = DirToDiagDir(v->direction); - /* Determine the diagonal direction in which we will exit this tile */ - if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) { - dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT); - } + DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track); /* Calculate next tile */ - tile += TileOffsByDiagDir(dir); - + TileIndex tile = v->tile + TileOffsByDiagDir(dir); /* Check if the train left a rail/road-crossing */ DisableTrainCrossing(tile); } @@ -1938,18 +1953,12 @@ } } else { /* search in the forward direction first. */ - DiagDirection i = DirToDiagDir(v->direction); - if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { - i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT); - } + DiagDirection i = TrainExitDir(v->direction, v->u.rail.track); NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd); if (tfdd.best_length == (uint)-1){ tfdd.reverse = true; /* search in backwards direction */ - i = ReverseDiagDir(DirToDiagDir(v->direction)); - if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { - i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT); - } + i = TrainExitDir(ReverseDir(v->direction), v->u.rail.track); NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd); } } @@ -3251,10 +3260,7 @@ return true;*/ /* Determine the non-diagonal direction in which we will exit this tile */ - DiagDirection dir = DirToDiagDir(v->direction); - if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) { - dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT); - } + DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track); /* Calculate next tile */ tile += TileOffsByDiagDir(dir); // determine the track status on the next tile.