mirror of https://github.com/OpenTTD/OpenTTD
(svn r3789) Add an enum and function to handle DiagDirection changes
parent
af69dba514
commit
e68120034c
13
direction.h
13
direction.h
|
@ -65,6 +65,19 @@ static inline DiagDirection ReverseDiagDir(DiagDirection d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
typedef enum DiagDirDiff {
|
||||||
|
DIAGDIRDIFF_SAME = 0,
|
||||||
|
DIAGDIRDIFF_90RIGHT = 1,
|
||||||
|
DIAGDIRDIFF_REVERSE = 2,
|
||||||
|
DIAGDIRDIFF_90LEFT = 3
|
||||||
|
} DiagDirDiff;
|
||||||
|
|
||||||
|
static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
|
||||||
|
{
|
||||||
|
return (DiagDirection)((d + delta) % 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static inline DiagDirection DirToDiagDir(Direction dir)
|
static inline DiagDirection DirToDiagDir(Direction dir)
|
||||||
{
|
{
|
||||||
return (DiagDirection)(dir >> 1);
|
return (DiagDirection)(dir >> 1);
|
||||||
|
|
33
train_cmd.c
33
train_cmd.c
|
@ -1546,14 +1546,14 @@ static void ReverseTrainDirection(Vehicle *v)
|
||||||
/* Check if we were approaching a rail/road-crossing */
|
/* Check if we were approaching a rail/road-crossing */
|
||||||
{
|
{
|
||||||
TileIndex tile = v->tile;
|
TileIndex tile = v->tile;
|
||||||
int t;
|
DiagDirection dir = DirToDiagDir(v->direction);
|
||||||
|
|
||||||
/* Determine the diagonal direction in which we will exit this tile */
|
/* Determine the diagonal direction in which we will exit this tile */
|
||||||
t = v->direction >> 1;
|
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) {
|
||||||
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) {
|
dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT);
|
||||||
t = (t - 1) & 3;
|
|
||||||
}
|
}
|
||||||
/* Calculate next tile */
|
/* Calculate next tile */
|
||||||
tile += TileOffsByDir(t);
|
tile += TileOffsByDir(dir);
|
||||||
|
|
||||||
/* Check if the train left a rail/road-crossing */
|
/* Check if the train left a rail/road-crossing */
|
||||||
DisableTrainCrossing(tile);
|
DisableTrainCrossing(tile);
|
||||||
|
@ -1785,13 +1785,17 @@ static TrainFindDepotData FindClosestTrainDepot(Vehicle *v)
|
||||||
DiagDirection i;
|
DiagDirection i;
|
||||||
|
|
||||||
i = DirToDiagDir(v->direction);
|
i = DirToDiagDir(v->direction);
|
||||||
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) i = (i + 3) & 3;
|
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) {
|
||||||
|
i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT);
|
||||||
|
}
|
||||||
NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
|
NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
|
||||||
if (tfdd.best_length == (uint)-1){
|
if (tfdd.best_length == (uint)-1){
|
||||||
tfdd.reverse = true;
|
tfdd.reverse = true;
|
||||||
// search in backwards direction
|
// search in backwards direction
|
||||||
i = ReverseDiagDir(DirToDiagDir(v->direction));
|
i = ReverseDiagDir(DirToDiagDir(v->direction));
|
||||||
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) i = (i + 3) & 3;
|
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) {
|
||||||
|
i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT);
|
||||||
|
}
|
||||||
NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
|
NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3107,7 +3111,8 @@ static bool TrainCheckIfLineEnds(Vehicle *v)
|
||||||
TileIndex tile;
|
TileIndex tile;
|
||||||
uint x,y;
|
uint x,y;
|
||||||
uint16 break_speed;
|
uint16 break_speed;
|
||||||
DiagDirection t;
|
DiagDirection dir;
|
||||||
|
int t;
|
||||||
uint32 ts;
|
uint32 ts;
|
||||||
|
|
||||||
t = v->breakdown_ctr;
|
t = v->breakdown_ctr;
|
||||||
|
@ -3138,14 +3143,14 @@ static bool TrainCheckIfLineEnds(Vehicle *v)
|
||||||
return true;*/
|
return true;*/
|
||||||
|
|
||||||
/* Determine the non-diagonal direction in which we will exit this tile */
|
/* Determine the non-diagonal direction in which we will exit this tile */
|
||||||
t = v->direction >> 1;
|
dir = DirToDiagDir(v->direction);
|
||||||
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) {
|
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) {
|
||||||
t = (t - 1) & 3;
|
dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT);
|
||||||
}
|
}
|
||||||
/* Calculate next tile */
|
/* Calculate next tile */
|
||||||
tile += TileOffsByDir(t);
|
tile += TileOffsByDir(dir);
|
||||||
// determine the track status on the next tile.
|
// determine the track status on the next tile.
|
||||||
ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _reachable_tracks[t];
|
ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _reachable_tracks[dir];
|
||||||
|
|
||||||
/* Calc position within the current tile ?? */
|
/* Calc position within the current tile ?? */
|
||||||
x = v->x_pos & 0xF;
|
x = v->x_pos & 0xF;
|
||||||
|
@ -3166,7 +3171,7 @@ static bool TrainCheckIfLineEnds(Vehicle *v)
|
||||||
if (x + 4 > 15 &&
|
if (x + 4 > 15 &&
|
||||||
(!CheckCompatibleRail(v, tile) ||
|
(!CheckCompatibleRail(v, tile) ||
|
||||||
(IsTileDepotType(tile, TRANSPORT_RAIL) &&
|
(IsTileDepotType(tile, TRANSPORT_RAIL) &&
|
||||||
GetDepotDirection(tile, TRANSPORT_RAIL) == t))) {
|
GetDepotDirection(tile, TRANSPORT_RAIL) == dir))) {
|
||||||
v->cur_speed = 0;
|
v->cur_speed = 0;
|
||||||
ReverseTrainDirection(v);
|
ReverseTrainDirection(v);
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in New Issue