summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ai/api/ai_bridge.cpp4
-rw-r--r--src/ai/api/ai_marine.cpp3
-rw-r--r--src/ai/api/ai_tunnel.cpp4
-rw-r--r--src/map_func.h20
-rw-r--r--src/train_cmd.cpp11
5 files changed, 26 insertions, 16 deletions
diff --git a/src/ai/api/ai_bridge.cpp b/src/ai/api/ai_bridge.cpp
index 58c52245e..27f5c116e 100644
--- a/src/ai/api/ai_bridge.cpp
+++ b/src/ai/api/ai_bridge.cpp
@@ -92,7 +92,7 @@ static void _DoCommandReturnBuildBridge1(class AIInstance *instance)
TileIndex end = AIObject::GetCallbackVariable(0);
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);
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 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);
return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
diff --git a/src/ai/api/ai_marine.cpp b/src/ai/api/ai_marine.cpp
index fb9bba882..fffa1e3a3 100644
--- a/src/ai/api/ai_marine.cpp
+++ b/src/ai/api/ai_marine.cpp
@@ -50,9 +50,8 @@
/* Tiles not neighbouring */
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 */
TrackBits gtts2 = ::TrackStatusToTrackBits(::GetTileTrackStatus(t2, TRANSPORT_WATER, 0, to_other_tile)) & ::DiagdirReachesTracks(to_other_tile);
diff --git a/src/ai/api/ai_tunnel.cpp b/src/ai/api/ai_tunnel.cpp
index 2b1fb1e31..cc7d95606 100644
--- a/src/ai/api/ai_tunnel.cpp
+++ b/src/ai/api/ai_tunnel.cpp
@@ -86,7 +86,7 @@ static void _DoCommandReturnBuildTunnel1(class AIInstance *instance)
TileIndex end = AIObject::GetCallbackVariable(0);
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);
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 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);
return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
diff --git a/src/map_func.h b/src/map_func.h
index 1d2eec8eb..932e2f8fd 100644
--- a/src/map_func.h
+++ b/src/map_func.h
@@ -375,6 +375,26 @@ static inline TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection 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.
*
* @param tile The tile to test
diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp
index 29dab40b4..e5df3b9c4 100644
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -3362,14 +3362,6 @@ static const Direction _new_vehicle_direction_table[11] = {
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)
{
byte offs;
@@ -3664,8 +3656,7 @@ static void TrainController(Vehicle *v, Vehicle *nomove)
/* A new tile is about to be entered. */
/* Determine what direction we're entering the new tile from */
- Direction dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile);
- enterdir = DirToDiagDir(dir);
+ enterdir = DiagdirBetweenTiles(gp.old_tile, gp.new_tile);
assert(IsValidDiagDirection(enterdir));
/* Get the status of the tracks in the new tile and mask