diff --git a/aircraft_cmd.c b/aircraft_cmd.c index f74d92ec2c..ba1c9ee2b1 100644 --- a/aircraft_cmd.c +++ b/aircraft_cmd.c @@ -764,7 +764,7 @@ static bool AircraftController(Vehicle *v) Station *st; const AirportMovingData *amd; Vehicle *u; - byte z,dirdiff,newdir,maxz,curz; + byte z,newdir,maxz,curz; GetNewVehiclePosResult gp; uint dist; int x,y; @@ -853,20 +853,22 @@ static bool AircraftController(Vehicle *v) // At final pos? if (dist == 0) { + DirDiff dirdiff; + if (v->cur_speed > 12) v->cur_speed = 12; // Change direction smoothly to final direction. - dirdiff = amd->direction - v->direction; + dirdiff = DirDifference(amd->direction, v->direction); // if distance is 0, and plane points in right direction, no point in calling // UpdateAircraftSpeed(). So do it only afterwards - if (dirdiff == 0) { + if (dirdiff == DIRDIFF_SAME) { v->cur_speed = 0; return true; } if (!UpdateAircraftSpeed(v)) return false; - v->direction = (v->direction+((dirdiff&7)<5?1:-1)) & 7; + v->direction = ChangeDir(v->direction, dirdiff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT); v->cur_speed >>= 1; SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos); diff --git a/direction.h b/direction.h index e29fb3c49c..4aed8bea30 100644 --- a/direction.h +++ b/direction.h @@ -23,6 +23,32 @@ static inline Direction ReverseDir(Direction d) } +typedef enum DirDiff { + DIRDIFF_SAME = 0, + DIRDIFF_45RIGHT = 1, + DIRDIFF_90RIGHT = 2, + DIRDIFF_REVERSE = 4, + DIRDIFF_90LEFT = 6, + DIRDIFF_45LEFT = 7 +} DirDiff; + +static inline DirDiff DirDifference(Direction d0, Direction d1) +{ + return (DirDiff)(d0 + 8 - d1) % 8; +} + +static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta) +{ + return (DirDiff)((d + delta) % 8); +} + + +static inline Direction ChangeDir(Direction d, DirDiff delta) +{ + return (Direction)((d + delta) % 8); +} + + /* Direction commonly used as the direction of entering and leaving tiles, 4-way */ typedef enum DiagDirection { DIAGDIR_NE = 0, /* Northeast, upper right on your monitor */ diff --git a/train_cmd.c b/train_cmd.c index 49c186e8f9..960af695a8 100644 --- a/train_cmd.c +++ b/train_cmd.c @@ -2591,14 +2591,16 @@ static const RailtypeSlowdownParams _railtype_slowdown[3] = { /* Modify the speed of the vehicle due to a turn */ static void AffectSpeedByDirChange(Vehicle* v, Direction new_dir) { - byte diff; + DirDiff diff; const RailtypeSlowdownParams *rsp; - if (_patches.realistic_acceleration || (diff = (v->direction - new_dir) & 7) == 0) - return; + if (_patches.realistic_acceleration) return; + + diff = DirDifference(v->direction, new_dir); + if (diff == DIRDIFF_SAME) return; rsp = &_railtype_slowdown[v->u.rail.railtype]; - v->cur_speed -= ((diff == 1 || diff == 7) ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8; + v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8; } /* Modify the speed of the vehicle due to a change in altitude */ @@ -2739,11 +2741,10 @@ static void *CheckVehicleAtSignal(Vehicle *v, void *data) { const VehicleAtSignalData* vasd = data; - if (v->type == VEH_Train && IsFrontEngine(v) && - v->tile == vasd->tile) { - byte diff = (v->direction - vasd->direction + 2) & 7; + if (v->type == VEH_Train && IsFrontEngine(v) && v->tile == vasd->tile) { + DirDiff diff = ChangeDirDiff(DirDifference(v->direction, vasd->direction), DIRDIFF_90RIGHT); - if (diff == 2 || (v->cur_speed <= 5 && diff <= 4)) return v; + if (diff == DIRDIFF_90RIGHT || (v->cur_speed <= 5 && diff <= DIRDIFF_REVERSE)) return v; } return NULL; } diff --git a/vehicle.c b/vehicle.c index e4dd53774f..24d7b731f6 100644 --- a/vehicle.c +++ b/vehicle.c @@ -1930,7 +1930,7 @@ static const Direction _new_direction_table[] = { Direction GetDirectionTowards(const Vehicle* v, int x, int y) { Direction dir; - byte dirdiff; + DirDiff dirdiff; int i = 0; if (y >= v->y_pos) { @@ -1945,10 +1945,9 @@ Direction GetDirectionTowards(const Vehicle* v, int x, int y) dir = v->direction; - dirdiff = _new_direction_table[i] - dir; - if (dirdiff == 0) - return dir; - return (dir+((dirdiff&7)<5?1:-1)) & 7; + dirdiff = DirDifference(_new_direction_table[i], dir); + if (dirdiff == DIRDIFF_SAME) return dir; + return ChangeDir(dir, dirdiff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT); } Trackdir GetVehicleTrackdir(const Vehicle* v)