Mercurial > hg > openttd
changeset 3158:c926382ebec9 draft
(svn r3784) Add a type and functions to handle direction changes
author | tron <tron@openttd.org> |
---|---|
date | Wed, 08 Mar 2006 07:48:56 +0000 |
parents | c4114a2e86c5 |
children | 2fe962248dda |
files | aircraft_cmd.c direction.h train_cmd.c vehicle.c |
diffstat | 4 files changed, 46 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/aircraft_cmd.c +++ b/aircraft_cmd.c @@ -764,7 +764,7 @@ 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 @@ // 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);
--- a/direction.h +++ b/direction.h @@ -23,6 +23,32 @@ } +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 */
--- a/train_cmd.c +++ b/train_cmd.c @@ -2591,14 +2591,16 @@ /* 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 @@ { 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; }
--- a/vehicle.c +++ b/vehicle.c @@ -1930,7 +1930,7 @@ 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 @@ 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)