(svn r3784) Add a type and functions to handle direction changes

This commit is contained in:
tron 2006-03-08 07:48:56 +00:00
parent fbe939b31f
commit ecabf8a5f0
4 changed files with 45 additions and 17 deletions

View File

@ -764,7 +764,7 @@ static bool AircraftController(Vehicle *v)
Station *st; Station *st;
const AirportMovingData *amd; const AirportMovingData *amd;
Vehicle *u; Vehicle *u;
byte z,dirdiff,newdir,maxz,curz; byte z,newdir,maxz,curz;
GetNewVehiclePosResult gp; GetNewVehiclePosResult gp;
uint dist; uint dist;
int x,y; int x,y;
@ -853,20 +853,22 @@ static bool AircraftController(Vehicle *v)
// At final pos? // At final pos?
if (dist == 0) { if (dist == 0) {
DirDiff dirdiff;
if (v->cur_speed > 12) v->cur_speed = 12; if (v->cur_speed > 12) v->cur_speed = 12;
// Change direction smoothly to final direction. // 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 // if distance is 0, and plane points in right direction, no point in calling
// UpdateAircraftSpeed(). So do it only afterwards // UpdateAircraftSpeed(). So do it only afterwards
if (dirdiff == 0) { if (dirdiff == DIRDIFF_SAME) {
v->cur_speed = 0; v->cur_speed = 0;
return true; return true;
} }
if (!UpdateAircraftSpeed(v)) return false; 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; v->cur_speed >>= 1;
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos); SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);

View File

@ -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 */ /* Direction commonly used as the direction of entering and leaving tiles, 4-way */
typedef enum DiagDirection { typedef enum DiagDirection {
DIAGDIR_NE = 0, /* Northeast, upper right on your monitor */ DIAGDIR_NE = 0, /* Northeast, upper right on your monitor */

View File

@ -2591,14 +2591,16 @@ static const RailtypeSlowdownParams _railtype_slowdown[3] = {
/* Modify the speed of the vehicle due to a turn */ /* Modify the speed of the vehicle due to a turn */
static void AffectSpeedByDirChange(Vehicle* v, Direction new_dir) static void AffectSpeedByDirChange(Vehicle* v, Direction new_dir)
{ {
byte diff; DirDiff diff;
const RailtypeSlowdownParams *rsp; const RailtypeSlowdownParams *rsp;
if (_patches.realistic_acceleration || (diff = (v->direction - new_dir) & 7) == 0) if (_patches.realistic_acceleration) return;
return;
diff = DirDifference(v->direction, new_dir);
if (diff == DIRDIFF_SAME) return;
rsp = &_railtype_slowdown[v->u.rail.railtype]; 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 */ /* 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; const VehicleAtSignalData* vasd = data;
if (v->type == VEH_Train && IsFrontEngine(v) && if (v->type == VEH_Train && IsFrontEngine(v) && v->tile == vasd->tile) {
v->tile == vasd->tile) { DirDiff diff = ChangeDirDiff(DirDifference(v->direction, vasd->direction), DIRDIFF_90RIGHT);
byte diff = (v->direction - vasd->direction + 2) & 7;
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; return NULL;
} }

View File

@ -1930,7 +1930,7 @@ static const Direction _new_direction_table[] = {
Direction GetDirectionTowards(const Vehicle* v, int x, int y) Direction GetDirectionTowards(const Vehicle* v, int x, int y)
{ {
Direction dir; Direction dir;
byte dirdiff; DirDiff dirdiff;
int i = 0; int i = 0;
if (y >= v->y_pos) { if (y >= v->y_pos) {
@ -1945,10 +1945,9 @@ Direction GetDirectionTowards(const Vehicle* v, int x, int y)
dir = v->direction; dir = v->direction;
dirdiff = _new_direction_table[i] - dir; dirdiff = DirDifference(_new_direction_table[i], dir);
if (dirdiff == 0) if (dirdiff == DIRDIFF_SAME) return dir;
return dir; return ChangeDir(dir, dirdiff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
return (dir+((dirdiff&7)<5?1:-1)) & 7;
} }
Trackdir GetVehicleTrackdir(const Vehicle* v) Trackdir GetVehicleTrackdir(const Vehicle* v)