mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r15789) -Codechange: Add DiagdirBetweenTiles() and use it.
This commit is contained in:
parent
fd0f0bda72
commit
d452a0a0ec
|
@ -92,7 +92,7 @@ static void _DoCommandReturnBuildBridge1(class AIInstance *instance)
|
||||||
TileIndex end = AIObject::GetCallbackVariable(0);
|
TileIndex end = AIObject::GetCallbackVariable(0);
|
||||||
TileIndex start = AIObject::GetCallbackVariable(1);
|
TileIndex start = AIObject::GetCallbackVariable(1);
|
||||||
|
|
||||||
DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
|
DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
|
||||||
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
|
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
|
||||||
|
|
||||||
if (!AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &_DoCommandReturnBuildBridge2)) return false;
|
if (!AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &_DoCommandReturnBuildBridge2)) return false;
|
||||||
|
@ -107,7 +107,7 @@ static void _DoCommandReturnBuildBridge1(class AIInstance *instance)
|
||||||
TileIndex end = AIObject::GetCallbackVariable(0);
|
TileIndex end = AIObject::GetCallbackVariable(0);
|
||||||
TileIndex start = AIObject::GetCallbackVariable(1);
|
TileIndex start = AIObject::GetCallbackVariable(1);
|
||||||
|
|
||||||
DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
|
DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
|
||||||
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
|
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
|
||||||
|
|
||||||
return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
|
return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
|
||||||
|
|
|
@ -50,9 +50,8 @@
|
||||||
|
|
||||||
/* Tiles not neighbouring */
|
/* Tiles not neighbouring */
|
||||||
if (::DistanceManhattan(t1, t2) != 1) return false;
|
if (::DistanceManhattan(t1, t2) != 1) return false;
|
||||||
if (t1 > t2) Swap(t1, t2);
|
|
||||||
|
|
||||||
DiagDirection to_other_tile = (TileX(t1) == TileX(t2)) ? DIAGDIR_SE : DIAGDIR_SW;
|
DiagDirection to_other_tile = ::DiagdirBetweenTiles(t1, t2);
|
||||||
|
|
||||||
/* Determine the reachable tracks from the shared edge */
|
/* Determine the reachable tracks from the shared edge */
|
||||||
TrackBits gtts2 = ::TrackStatusToTrackBits(::GetTileTrackStatus(t2, TRANSPORT_WATER, 0, to_other_tile)) & ::DiagdirReachesTracks(to_other_tile);
|
TrackBits gtts2 = ::TrackStatusToTrackBits(::GetTileTrackStatus(t2, TRANSPORT_WATER, 0, to_other_tile)) & ::DiagdirReachesTracks(to_other_tile);
|
||||||
|
|
|
@ -86,7 +86,7 @@ static void _DoCommandReturnBuildTunnel1(class AIInstance *instance)
|
||||||
TileIndex end = AIObject::GetCallbackVariable(0);
|
TileIndex end = AIObject::GetCallbackVariable(0);
|
||||||
TileIndex start = AITunnel::GetOtherTunnelEnd(end);
|
TileIndex start = AITunnel::GetOtherTunnelEnd(end);
|
||||||
|
|
||||||
DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
|
DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
|
||||||
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
|
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
|
||||||
|
|
||||||
if (!AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &_DoCommandReturnBuildTunnel2)) return false;
|
if (!AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &_DoCommandReturnBuildTunnel2)) return false;
|
||||||
|
@ -101,7 +101,7 @@ static void _DoCommandReturnBuildTunnel1(class AIInstance *instance)
|
||||||
TileIndex end = AIObject::GetCallbackVariable(0);
|
TileIndex end = AIObject::GetCallbackVariable(0);
|
||||||
TileIndex start = AITunnel::GetOtherTunnelEnd(end);
|
TileIndex start = AITunnel::GetOtherTunnelEnd(end);
|
||||||
|
|
||||||
DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
|
DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
|
||||||
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
|
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
|
||||||
|
|
||||||
return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
|
return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
|
||||||
|
|
|
@ -374,6 +374,26 @@ static inline TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
|
||||||
return TILE_ADD(tile, TileOffsByDiagDir(dir));
|
return TILE_ADD(tile, TileOffsByDiagDir(dir));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines the DiagDirection to get from one tile to another.
|
||||||
|
* The tiles do not necessarily have to be adjacent.
|
||||||
|
* @param tile_from Origin tile
|
||||||
|
* @param tile_to Destination tile
|
||||||
|
* @return DiagDirection from tile_from towards tile_to, or INVALID_DIAGDIR if the tiles are not on an axis
|
||||||
|
*/
|
||||||
|
static inline DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
|
||||||
|
{
|
||||||
|
int dx = (int)TileX(tile_to) - (int)TileX(tile_from);
|
||||||
|
int dy = (int)TileY(tile_to) - (int)TileY(tile_from);
|
||||||
|
if (dx == 0) {
|
||||||
|
if (dy == 0) return INVALID_DIAGDIR;
|
||||||
|
return (dy < 0 ? DIAGDIR_NW : DIAGDIR_SE);
|
||||||
|
} else {
|
||||||
|
if (dy != 0) return INVALID_DIAGDIR;
|
||||||
|
return (dx < 0 ? DIAGDIR_NE : DIAGDIR_SW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A callback function type for searching tiles.
|
* A callback function type for searching tiles.
|
||||||
*
|
*
|
||||||
|
|
|
@ -3362,14 +3362,6 @@ static const Direction _new_vehicle_direction_table[11] = {
|
||||||
DIR_E , DIR_SE, DIR_S
|
DIR_E , DIR_SE, DIR_S
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline Direction GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile)
|
|
||||||
{
|
|
||||||
uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
|
|
||||||
TileX(new_tile) - TileX(old_tile) + 1;
|
|
||||||
assert(offs < 11);
|
|
||||||
return _new_vehicle_direction_table[offs];
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int GetDirectionToVehicle(const Vehicle *v, int x, int y)
|
static inline int GetDirectionToVehicle(const Vehicle *v, int x, int y)
|
||||||
{
|
{
|
||||||
byte offs;
|
byte offs;
|
||||||
|
@ -3664,8 +3656,7 @@ static void TrainController(Vehicle *v, Vehicle *nomove)
|
||||||
/* A new tile is about to be entered. */
|
/* A new tile is about to be entered. */
|
||||||
|
|
||||||
/* Determine what direction we're entering the new tile from */
|
/* Determine what direction we're entering the new tile from */
|
||||||
Direction dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile);
|
enterdir = DiagdirBetweenTiles(gp.old_tile, gp.new_tile);
|
||||||
enterdir = DirToDiagDir(dir);
|
|
||||||
assert(IsValidDiagDirection(enterdir));
|
assert(IsValidDiagDirection(enterdir));
|
||||||
|
|
||||||
/* Get the status of the tracks in the new tile and mask
|
/* Get the status of the tracks in the new tile and mask
|
||||||
|
|
Loading…
Reference in New Issue