# HG changeset patch # User tron # Date 2006-03-08 07:48:56 # Node ID b7e44d9906abfd1e68ac4e100065cad989c3c8cb # Parent d2dcc469a38bccfb2e8640c400a32d6c9d0e25f9 (svn r3784) Add a type and functions to handle direction changes diff --git a/aircraft_cmd.c b/aircraft_cmd.c --- a/aircraft_cmd.c +++ b/aircraft_cmd.c @@ -764,7 +764,7 @@ static bool AircraftController(Vehicle * 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 * // 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 --- a/direction.h +++ b/direction.h @@ -23,6 +23,32 @@ static inline Direction ReverseDir(Direc } +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 --- a/train_cmd.c +++ b/train_cmd.c @@ -2591,14 +2591,16 @@ static const RailtypeSlowdownParams _rai /* 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(Vehicl { const VehicleAtSignalData* vasd = data; - if (v->type == VEH_Train && IsFrontEngine(v) && - v->tile == vasd->tile) { - byte diff = (v->direction - vasd->direction + 2) & 7; - - if (diff == 2 || (v->cur_speed <= 5 && diff <= 4)) return v; + if (v->type == VEH_Train && IsFrontEngine(v) && v->tile == vasd->tile) { + DirDiff diff = ChangeDirDiff(DirDifference(v->direction, vasd->direction), DIRDIFF_90RIGHT); + + if (diff == DIRDIFF_90RIGHT || (v->cur_speed <= 5 && diff <= DIRDIFF_REVERSE)) return v; } return NULL; } diff --git a/vehicle.c b/vehicle.c --- a/vehicle.c +++ b/vehicle.c @@ -1930,7 +1930,7 @@ static const Direction _new_direction_ta 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 Vehi 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)