(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
authortron
Wed, 08 Mar 2006 06:55:33 +0000
changeset 3157 3f35e2d9c8e3
parent 3156 f4caf4197189
child 3158 696a6ca0bfa9
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
ai/trolly/build.c
ai/trolly/pathfinder.c
ai/trolly/trolly.c
ai/trolly/trolly.h
aircraft_cmd.c
aircraft_gui.c
direction.h
disaster_cmd.c
dock_gui.c
industry_cmd.c
newgrf_engine.c
newgrf_engine.h
pathfind.c
rail_cmd.c
rail_gui.c
roadveh_cmd.c
roadveh_gui.c
ship_cmd.c
ship_gui.c
station_cmd.c
train_cmd.c
train_gui.c
tunnelbridge_cmd.c
vehicle.c
vehicle.h
water_cmd.c
waypoint.c
--- a/ai/trolly/build.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/ai/trolly/build.c	Wed Mar 08 06:55:33 2006 +0000
@@ -5,6 +5,7 @@
 #include "../../debug.h"
 #include "../../functions.h"
 #include "../../map.h"
+#include "../../road_map.h"
 #include "../../tile.h"
 #include "../../command.h"
 #include "trolly.h"
@@ -255,9 +256,8 @@
 	return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
 }
 
-int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag)
+int AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag)
 {
-	static const byte _roadbits_by_dir[4] = {2,1,8,4};
 	int ret, ret2;
 	if (p->ainew.tbt == AI_TRAIN)
 		return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
@@ -265,7 +265,7 @@
 	ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
 	if (CmdFailed(ret)) return ret;
 	// Try to build the road from the depot
-	ret2 = AI_DoCommand(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
+	ret2 = AI_DoCommand(tile + TileOffsByDir(direction), DiagDirToRoadBits(ReverseDiagDir(direction)), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
 	// If it fails, ignore it..
 	if (CmdFailed(ret2)) return ret;
 	return ret + ret2;
--- a/ai/trolly/pathfinder.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/ai/trolly/pathfinder.c	Wed Mar 08 06:55:33 2006 +0000
@@ -372,7 +372,7 @@
 
 extern uint GetRailFoundation(uint tileh, uint bits);
 extern uint GetRoadFoundation(uint tileh, uint bits);
-extern uint GetBridgeFoundation(uint tileh, byte direction);
+extern uint GetBridgeFoundation(uint tileh, Axis); // XXX function declaration in .c
 enum {
 	BRIDGE_NO_FOUNDATION = 1 << 0 | 1 << 3 | 1 << 6 | 1 << 9 | 1 << 12,
 };
--- a/ai/trolly/trolly.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/ai/trolly/trolly.c	Wed Mar 08 06:55:33 2006 +0000
@@ -1024,7 +1024,6 @@
 		// This means we are done building!
 
 		if (p->ainew.tbt == AI_TRUCK && !_patches.roadveh_queue) {
-			static const byte _roadbits_by_dir[4] = {2,1,8,4};
 			// If they not queue, they have to go up and down to try again at a station...
 			// We don't want that, so try building some road left or right of the station
 			int dir1, dir2, dir3;
@@ -1047,7 +1046,7 @@
 					dir3 = p->ainew.to_direction;
 				}
 
-				ret = AI_DoCommand(tile, _roadbits_by_dir[dir1], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 				if (!CmdFailed(ret)) {
 					dir1 = TileOffsByDir(dir1);
 					if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) {
@@ -1059,7 +1058,7 @@
 					}
 				}
 
-				ret = AI_DoCommand(tile, _roadbits_by_dir[dir2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 				if (!CmdFailed(ret)) {
 					dir2 = TileOffsByDir(dir2);
 					if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) {
@@ -1071,7 +1070,7 @@
 					}
 				}
 
-				ret = AI_DoCommand(tile, _roadbits_by_dir[dir3^2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+				ret = AI_DoCommand(tile, DiagDirToRoadBits(dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 				if (!CmdFailed(ret)) {
 					dir3 = TileOffsByDir(dir3);
 					if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) {
--- a/ai/trolly/trolly.h	Tue Mar 07 07:51:05 2006 +0000
+++ b/ai/trolly/trolly.h	Wed Mar 08 06:55:33 2006 +0000
@@ -256,6 +256,6 @@
 int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag);
 int AiNew_PickVehicle(Player *p);
 int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag);
-int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag);
+int AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag);
 
 #endif /* AI_TROLLY_H */
--- a/aircraft_cmd.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/aircraft_cmd.c	Wed Mar 08 06:55:33 2006 +0000
@@ -91,7 +91,7 @@
 }
 #endif
 
-int GetAircraftImage(const Vehicle *v, byte direction)
+int GetAircraftImage(const Vehicle* v, Direction direction)
 {
 	int spritenum = v->spritenum;
 
--- a/aircraft_gui.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/aircraft_gui.c	Wed Mar 08 06:55:33 2006 +0000
@@ -65,7 +65,7 @@
 static void DrawAircraftImage(const Vehicle *v, int x, int y, VehicleID selection)
 {
 	PalSpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
-	DrawSprite(GetAircraftImage(v, 6) | pal, x + 25, y + 10);
+	DrawSprite(GetAircraftImage(v, DIR_W) | pal, x + 25, y + 10);
 	if (v->subtype == 0) DrawSprite(SPR_ROTOR_STOPPED, x + 25, y + 5);
 	if (v->index == selection) {
 		DrawFrameRect(x - 1, y - 1, x + 58, y + 21, 0xF, FR_BORDERONLY);
@@ -730,7 +730,7 @@
 		if (v != NULL) {
 			WP(w,traindepot_d).sel = v->index;
 			SetWindowDirty(w);
-			SetObjectToPlaceWnd(GetVehiclePalette(v) | GetAircraftImage(v, 6), 4, w);
+			SetObjectToPlaceWnd(GetVehiclePalette(v) | GetAircraftImage(v, DIR_W), 4, w);
 		}
 		break;
 
--- a/direction.h	Tue Mar 07 07:51:05 2006 +0000
+++ b/direction.h	Wed Mar 08 06:55:33 2006 +0000
@@ -17,6 +17,11 @@
 	INVALID_DIR = 0xFF,
 } Direction;
 
+static inline Direction ReverseDir(Direction d)
+{
+	return (Direction)(4 ^ d);
+}
+
 
 /* Direction commonly used as the direction of entering and leaving tiles, 4-way */
 typedef enum DiagDirection {
--- a/disaster_cmd.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/disaster_cmd.c	Wed Mar 08 06:55:33 2006 +0000
@@ -74,7 +74,7 @@
 	v->cur_image = img;
 }
 
-static void InitializeDisasterVehicle(Vehicle *v, int x, int y, byte z, byte direction, byte subtype)
+static void InitializeDisasterVehicle(Vehicle* v, int x, int y, byte z, Direction direction, byte subtype)
 {
 	v->type = VEH_Disaster;
 	v->x_pos = x;
@@ -552,7 +552,7 @@
 			return;
 		}
 
-		InitializeDisasterVehicle(u, -6*16, v->y_pos, 135, 5, 11);
+		InitializeDisasterVehicle(u, -6 * 16, v->y_pos, 135, DIR_SW, 11);
 		u->u.disaster.unk2 = v->index;
 
 		w = ForceAllocateSpecialVehicle();
@@ -560,7 +560,7 @@
 			return;
 
 		u->next = w;
-		InitializeDisasterVehicle(w, -6*16, v->y_pos, 0, 5, 12);
+		InitializeDisasterVehicle(w, -6 * 16, v->y_pos, 0, DIR_SW, 12);
 		w->vehstatus |= VS_DISASTER;
 	} else if (v->current_order.station < 1) {
 
@@ -720,13 +720,13 @@
 		}
 	}
 
-	InitializeDisasterVehicle(v, x, 0, 135, 3, 0);
+	InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, 0);
 
 	// Allocate shadow too?
 	u = ForceAllocateSpecialVehicle();
 	if (u != NULL) {
 		v->next = u;
-		InitializeDisasterVehicle(u, x, 0, 0, 3, 1);
+		InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, 1);
 		u->vehstatus |= VS_DISASTER;
 	}
 }
@@ -741,7 +741,7 @@
 
 	x = TileX(Random()) * 16 + 8;
 
-	InitializeDisasterVehicle(v, x, 0, 135, 3, 2);
+	InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, 2);
 	v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
 	v->age = 0;
 
@@ -749,7 +749,7 @@
 	u = ForceAllocateSpecialVehicle();
 	if (u != NULL) {
 		v->next = u;
-		InitializeDisasterVehicle(u,x,0,0,3,3);
+		InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, 3);
 		u->vehstatus |= VS_DISASTER;
 	}
 }
@@ -780,12 +780,12 @@
 	x = (MapSizeX() + 9) * 16 - 1;
 	y = TileY(found->xy) * 16 + 37;
 
-	InitializeDisasterVehicle(v,x,y, 135,1,4);
+	InitializeDisasterVehicle(v, x, y, 135, DIR_NE, 4);
 
 	u = ForceAllocateSpecialVehicle();
 	if (u != NULL) {
 		v->next = u;
-		InitializeDisasterVehicle(u,x,y,0,3,5);
+		InitializeDisasterVehicle(u, x, y, 0, DIR_SE, 5);
 		u->vehstatus |= VS_DISASTER;
 	}
 }
@@ -816,18 +816,18 @@
 	x = -16 * 16;
 	y = TileY(found->xy) * 16 + 37;
 
-	InitializeDisasterVehicle(v,x,y, 135,5,6);
+	InitializeDisasterVehicle(v, x, y, 135, DIR_SW, 6);
 
 	u = ForceAllocateSpecialVehicle();
 	if (u != NULL) {
 		v->next = u;
-		InitializeDisasterVehicle(u,x,y,0,5,7);
+		InitializeDisasterVehicle(u, x, y, 0, DIR_SW, 7);
 		u->vehstatus |= VS_DISASTER;
 
 		w = ForceAllocateSpecialVehicle();
 		if (w != NULL) {
 			u->next = w;
-			InitializeDisasterVehicle(w,x,y,140,5,8);
+			InitializeDisasterVehicle(w, x, y, 140, DIR_SW, 8);
 		}
 	}
 }
@@ -842,7 +842,7 @@
 	x = TileX(Random()) * 16 + 8;
 
 	y = MapMaxX() * 16 - 1;
-	InitializeDisasterVehicle(v, x, y, 135, 7, 9);
+	InitializeDisasterVehicle(v, x, y, 135, DIR_NW, 9);
 	v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
 	v->age = 0;
 
@@ -850,7 +850,7 @@
 	u = ForceAllocateSpecialVehicle();
 	if (u != NULL) {
 		v->next = u;
-		InitializeDisasterVehicle(u,x,y,0,7,10);
+		InitializeDisasterVehicle(u, x, y, 0, DIR_NW, 10);
 		u->vehstatus |= VS_DISASTER;
 	}
 }
@@ -860,7 +860,7 @@
 {
 	Vehicle *v = ForceAllocateSpecialVehicle();
 	int x,y;
-	byte dir;
+	Direction dir;
 	uint32 r;
 
 	if (v == NULL) return;
@@ -868,10 +868,14 @@
 	r = Random();
 	x = TileX(r) * 16 + 8;
 
-	y = 8;
-	dir = 3;
-	if (r & 0x80000000) { y = MapMaxX() * 16 - 8 - 1; dir = 7; }
-	InitializeDisasterVehicle(v, x, y, 0, dir,13);
+	if (r & 0x80000000) {
+		y = MapMaxX() * 16 - 8 - 1;
+		dir = DIR_NW;
+	} else {
+		y = 8;
+		dir = DIR_SE;
+	}
+	InitializeDisasterVehicle(v, x, y, 0, dir, 13);
 	v->age = 0;
 }
 
@@ -880,7 +884,7 @@
 {
 	Vehicle *v = ForceAllocateSpecialVehicle();
 	int x,y;
-	byte dir;
+	Direction dir;
 	uint32 r;
 
 	if (v == NULL) return;
@@ -888,10 +892,14 @@
 	r = Random();
 	x = TileX(r) * 16 + 8;
 
-	y = 8;
-	dir = 3;
-	if (r & 0x80000000) { y = MapMaxX() * 16 - 8 - 1; dir = 7; }
-	InitializeDisasterVehicle(v, x, y, 0, dir,14);
+	if (r & 0x80000000) {
+		y = MapMaxX() * 16 - 8 - 1;
+		dir = DIR_NW;
+	} else {
+		y = 8;
+		dir = DIR_SE;
+	}
+	InitializeDisasterVehicle(v, x, y, 0, dir, 14);
 	v->age = 0;
 }
 
--- a/dock_gui.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/dock_gui.c	Wed Mar 08 06:55:33 2006 +0000
@@ -18,7 +18,7 @@
 static void ShowBuildDockStationPicker(void);
 static void ShowBuildDocksDepotPicker(void);
 
-static byte _ship_depot_direction;
+static Axis _ship_depot_direction;
 
 void CcBuildDocks(bool success, TileIndex tile, uint32 p1, uint32 p2)
 {
@@ -293,7 +293,7 @@
 
 static void UpdateDocksDirection(void)
 {
-	if (_ship_depot_direction != 0) {
+	if (_ship_depot_direction != AXIS_X) {
 		SetTileSelectSize(1, 2);
 	} else {
 		SetTileSelectSize(2, 1);
@@ -362,5 +362,5 @@
 
 void InitializeDockGui(void)
 {
-	_ship_depot_direction = 0;
+	_ship_depot_direction = AXIS_X;
 }
--- a/industry_cmd.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/industry_cmd.c	Wed Mar 08 06:55:33 2006 +0000
@@ -907,7 +907,7 @@
 	}
 }
 
-static void SetupFarmFieldFence(TileIndex tile, int size, byte type, int direction)
+static void SetupFarmFieldFence(TileIndex tile, int size, byte type, Axis direction)
 {
 	do {
 		tile = TILE_MASK(tile);
@@ -917,14 +917,14 @@
 
 			if (or == 1 && CHANCE16(1, 7)) or = 2;
 
-			if (direction) {
+			if (direction == AXIS_X) {
+				SetFenceSE(tile, or);
+			} else {
 				SetFenceSW(tile, or);
-			} else {
-				SetFenceSE(tile, or);
 			}
 		}
 
-		tile += direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
+		tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
 	} while (--size);
 }
 
@@ -984,10 +984,10 @@
 		type = _plantfarmfield_type[Random() & 0xF];
 	}
 
-	SetupFarmFieldFence(tile - TileDiffXY(1, 0), size_y, type, 1);
-	SetupFarmFieldFence(tile - TileDiffXY(0, 1), size_x, type, 0);
-	SetupFarmFieldFence(tile + TileDiffXY(size_x - 1, 0), size_y, type, 1);
-	SetupFarmFieldFence(tile + TileDiffXY(0, size_y - 1), size_x, type, 0);
+	SetupFarmFieldFence(tile - TileDiffXY(1, 0), size_y, type, AXIS_Y);
+	SetupFarmFieldFence(tile - TileDiffXY(0, 1), size_x, type, AXIS_X);
+	SetupFarmFieldFence(tile + TileDiffXY(size_x - 1, 0), size_y, type, AXIS_Y);
+	SetupFarmFieldFence(tile + TileDiffXY(0, size_y - 1), size_x, type, AXIS_X);
 }
 
 static void MaybePlantFarmField(const Industry* i)
--- a/newgrf_engine.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/newgrf_engine.c	Wed Mar 08 06:55:33 2006 +0000
@@ -388,7 +388,7 @@
 	return group;
 }
 
-int GetCustomEngineSprite(EngineID engine, const Vehicle *v, byte direction)
+int GetCustomEngineSprite(EngineID engine, const Vehicle* v, Direction direction)
 {
 	const SpriteGroup *group;
 	const RealSpriteGroup *rsg;
--- a/newgrf_engine.h	Tue Mar 07 07:51:05 2006 +0000
+++ b/newgrf_engine.h	Wed Mar 08 06:55:33 2006 +0000
@@ -3,6 +3,8 @@
 #ifndef NEWGRF_ENGINE_H
 #define NEWGRF_ENGINE_H
 
+#include "direction.h"
+
 /** @file newgrf_engine.h
  */
 
@@ -15,7 +17,7 @@
 void SetWagonOverrideSprites(EngineID engine, struct SpriteGroup *group, byte *train_id, int trains);
 void SetCustomEngineSprites(EngineID engine, byte cargo, struct SpriteGroup *group);
 // loaded is in percents, overriding_engine 0xffff is none
-int GetCustomEngineSprite(EngineID engine, const Vehicle *v, byte direction);
+int GetCustomEngineSprite(EngineID engine, const Vehicle* v, Direction direction);
 uint16 GetCallBackResult(uint16 callback_info, EngineID engine, const Vehicle *v);
 bool UsesWagonOverride(const Vehicle *v);
 #define GetCustomVehicleSprite(v, direction) GetCustomEngineSprite(v->engine_type, v, direction)
--- a/pathfind.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/pathfind.c	Wed Mar 08 06:55:33 2006 +0000
@@ -210,7 +210,7 @@
 /* Returns the end tile and the length of a tunnel. The length does not
  * include the starting tile (entry), it does include the end tile (exit).
  */
-FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, uint direction)
+FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, DiagDirection direction)
 {
 	FindLengthOfTunnelResult flotr;
 	int x,y;
@@ -246,7 +246,7 @@
 
 static const uint16 _tpfmode1_and[4] = { 0x1009, 0x16, 0x520, 0x2A00 };
 
-static uint SkipToEndOfTunnel(TrackPathFinder *tpf, TileIndex tile, int direction)
+static uint SkipToEndOfTunnel(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
 {
 	FindLengthOfTunnelResult flotr;
 	TPFSetTileBit(tpf, tile, 14);
--- a/rail_cmd.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/rail_cmd.c	Wed Mar 08 06:55:33 2006 +0000
@@ -1788,8 +1788,8 @@
 
 void SetSignalsOnBothDir(TileIndex tile, byte track)
 {
-	static const byte _search_dir_1[6] = {1, 3, 1, 3, 5, 3};
-	static const byte _search_dir_2[6] = {5, 7, 7, 5, 7, 1};
+	static const Direction _search_dir_1[] = { DIR_NE, DIR_SE, DIR_NE, DIR_SE, DIR_SW, DIR_SE };
+	static const Direction _search_dir_2[] = { DIR_SW, DIR_NW, DIR_NW, DIR_SW, DIR_NW, DIR_NE };
 
 	UpdateSignalsOnSegment(tile, _search_dir_1[track]);
 	UpdateSignalsOnSegment(tile, _search_dir_2[track]);
@@ -2069,15 +2069,13 @@
 	-1,  0,  1,  0, /* x */
 	 0,  1,  0, -1  /* y */
 };
-static const byte _enter_directions[4] = {5, 7, 1, 3};
-static const byte _leave_directions[4] = {1, 3, 5, 7};
 static const byte _depot_track_mask[4] = {1, 2, 1, 2};
 
 static uint32 VehicleEnter_Track(Vehicle *v, TileIndex tile, int x, int y)
 {
 	byte fract_coord;
 	byte fract_coord_leave;
-	int dir;
+	DiagDirection dir;
 	int length;
 
 	// this routine applies only to trains in depot tiles
@@ -2102,11 +2100,11 @@
 		/* make sure a train is not entering the tile from behind */
 		return 8;
 	} else if (_fractcoords_enter[dir] == fract_coord) {
-		if (_enter_directions[dir] == v->direction) {
+		if (DiagDirToDir(ReverseDiagDir(dir)) == v->direction) {
 			/* enter the depot */
 			v->u.rail.track = 0x80,
 			v->vehstatus |= VS_HIDDEN; /* hide it */
-			v->direction ^= 4;
+			v->direction = ReverseDir(v->direction);
 			if (v->next == NULL)
 				TrainEnterDepot(v, tile);
 			v->tile = tile;
@@ -2114,7 +2112,7 @@
 			return 4;
 		}
 	} else if (fract_coord_leave == fract_coord) {
-		if (_leave_directions[dir] == v->direction) {
+		if (DiagDirToDir(dir) == v->direction) {
 			/* leave the depot? */
 			if ((v = v->next) != NULL) {
 				v->vehstatus &= ~VS_HIDDEN;
--- a/rail_gui.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/rail_gui.c	Wed Mar 08 06:55:33 2006 +0000
@@ -23,7 +23,7 @@
 
 static RailType _cur_railtype;
 static bool _remove_button_clicked;
-static byte _build_depot_direction;
+static DiagDirection _build_depot_direction;
 static byte _waypoint_count = 1;
 static byte _cur_waypoint_type;
 
@@ -101,7 +101,7 @@
 void CcRailDepot(bool success, TileIndex tile, uint32 p1, uint32 p2)
 {
 	if (success) {
-		int dir = p2;
+		DiagDirection dir = p2;
 
 		SndPlayTileFx(SND_20_SPLAT_2, tile);
 		ResetObjectToPlace();
@@ -901,7 +901,7 @@
 
 void InitializeRailGui(void)
 {
-	_build_depot_direction = 3;
+	_build_depot_direction = DIAGDIR_NW;
 	_railstation.numtracks = 1;
 	_railstation.platlength = 1;
 	_railstation.dragdrop = true;
--- a/roadveh_cmd.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/roadveh_cmd.c	Wed Mar 08 06:55:33 2006 +0000
@@ -60,7 +60,7 @@
 	0x910, 0x1600, 0x2005, 0x2A
 };
 
-int GetRoadVehImage(const Vehicle *v, byte direction)
+int GetRoadVehImage(const Vehicle* v, Direction direction)
 {
 	int img = v->spritenum;
 	int image;
@@ -718,7 +718,7 @@
 	int x;
 	int y;
 	const Vehicle* veh;
-	byte dir;
+	Direction dir;
 } RoadVehFindData;
 
 static void* EnumCheckRoadVehClose(Vehicle *v, void* data)
@@ -744,7 +744,7 @@
 			v : NULL;
 }
 
-static Vehicle *RoadVehFindCloseTo(Vehicle *v, int x, int y, byte dir)
+static Vehicle* RoadVehFindCloseTo(Vehicle* v, int x, int y, Direction dir)
 {
 	RoadVehFindData rvf;
 	Vehicle *u;
@@ -833,12 +833,12 @@
 	return (t < v->progress);
 }
 
-static byte RoadVehGetNewDirection(Vehicle *v, int x, int y)
+static Direction RoadVehGetNewDirection(const Vehicle* v, int x, int y)
 {
-	static const byte _roadveh_new_dir[11] = {
-		0, 7, 6, 0,
-		1, 0, 5, 0,
-		2, 3, 4
+	static const Direction _roadveh_new_dir[] = {
+		DIR_N , DIR_NW, DIR_W , 0,
+		DIR_NE, DIR_N , DIR_SW, 0,
+		DIR_E , DIR_SE, DIR_S
 	};
 
 	x = x - v->x_pos + 1;
@@ -848,10 +848,11 @@
 	return _roadveh_new_dir[y * 4 + x];
 }
 
-static byte RoadVehGetSlidingDirection(Vehicle *v, int x, int y)
+static Direction RoadVehGetSlidingDirection(const Vehicle* v, int x, int y)
 {
-	byte b = RoadVehGetNewDirection(v, x, y);
-	byte d = v->direction;
+	Direction b = RoadVehGetNewDirection(v, x, y);
+	Direction d = v->direction;
+
 	if (b == d) return d;
 	d = (d + 1) & 7;
 	if (b == d) return d;
@@ -980,7 +981,7 @@
 
 // Returns direction to choose
 // or -1 if the direction is currently blocked
-static int RoadFindPathToDest(Vehicle *v, TileIndex tile, int enterdir)
+static int RoadFindPathToDest(Vehicle* v, TileIndex tile, DiagDirection enterdir)
 {
 #define return_track(x) {best_track = x; goto found_best_track; }
 
@@ -1154,7 +1155,8 @@
 
 static void RoadVehController(Vehicle *v)
 {
-	byte new_dir, old_dir;
+	Direction new_dir;
+	Direction old_dir;
 	RoadDriveEntry rd;
 	int x,y;
 	uint32 r;
@@ -1273,7 +1275,7 @@
 		TileIndex tile = v->tile + TileOffsByDir(rd.x & 3);
 		int dir = RoadFindPathToDest(v, tile, rd.x & 3);
 		uint32 r;
-		byte newdir;
+		Direction newdir;
 		const RoadDriveEntry *rdp;
 
 		if (dir == -1) {
@@ -1339,7 +1341,7 @@
 		int dir = RoadFindPathToDest(v, v->tile,	rd.x & 3);
 		uint32 r;
 		int tmp;
-		byte newdir;
+		Direction newdir;
 		const RoadDriveEntry *rdp;
 
 		if (dir == -1) {
--- a/roadveh_gui.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/roadveh_gui.c	Wed Mar 08 06:55:33 2006 +0000
@@ -65,7 +65,7 @@
 static void DrawRoadVehImage(const Vehicle *v, int x, int y, VehicleID selection)
 {
 	PalSpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
-	DrawSprite(GetRoadVehImage(v, 6) | pal, x + 14, y + 6);
+	DrawSprite(GetRoadVehImage(v, DIR_W) | pal, x + 14, y + 6);
 
 	if (v->index == selection) {
 		DrawFrameRect(x - 1, y - 1, x + 28, y + 12, 15, FR_BORDERONLY);
@@ -636,7 +636,7 @@
 		if (v != NULL) {
 			WP(w,traindepot_d).sel = v->index;
 			SetWindowDirty(w);
-			SetObjectToPlaceWnd(GetVehiclePalette(v) | GetRoadVehImage(v, 6), 4, w);
+			SetObjectToPlaceWnd(GetVehiclePalette(v) | GetRoadVehImage(v, DIR_W), 4, w);
 		}
 		break;
 
--- a/ship_cmd.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/ship_cmd.c	Wed Mar 08 06:55:33 2006 +0000
@@ -44,7 +44,7 @@
 	DrawSprite((6 + _ship_sprites[spritenum]) | image_ormod, x, y);
 }
 
-int GetShipImage(const Vehicle *v, byte direction)
+int GetShipImage(const Vehicle* v, Direction direction)
 {
 	int spritenum = v->spritenum;
 
@@ -579,10 +579,10 @@
 	}
 }
 
-static const byte _new_vehicle_direction_table[11] = {
-	0, 7, 6, 0,
-	1, 0, 5, 0,
-	2, 3, 4,
+static const Direction _new_vehicle_direction_table[] = {
+	DIR_N , DIR_NW, DIR_W , 0,
+	DIR_NE, DIR_N , DIR_SW, 0,
+	DIR_E , DIR_SE, DIR_S
 };
 
 static int ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile)
@@ -646,7 +646,9 @@
 	GetNewVehiclePosResult gp;
 	uint32 r;
 	const byte *b;
-	int dir,track,tracks;
+	Direction dir;
+	int track;
+	int tracks;
 
 	v->tick_counter++;
 
@@ -736,23 +738,24 @@
 			}
 		}
 	} else {
+		DiagDirection diagdir;
 		// new tile
 		if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY())
 			goto reverse_direction;
 
 		dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
-		assert(dir == 1 || dir == 3 || dir == 5 || dir == 7);
-		dir>>=1;
-		tracks = GetAvailShipTracks(gp.new_tile, dir);
+		assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
+		diagdir = DirToDiagDir(dir);
+		tracks = GetAvailShipTracks(gp.new_tile, diagdir);
 		if (tracks == 0)
 			goto reverse_direction;
 
 		// Choose a direction, and continue if we find one
-		track = ChooseShipTrack(v, gp.new_tile, dir, tracks);
+		track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
 		if (track < 0)
 			goto reverse_direction;
 
-		b = _ship_subcoord[dir][track];
+		b = _ship_subcoord[diagdir][track];
 
 		gp.x = (gp.x&~0xF) | b[0];
 		gp.y = (gp.y&~0xF) | b[1];
@@ -783,7 +786,7 @@
 	return;
 
 reverse_direction:
-	dir = v->direction ^ 4;
+	dir = ReverseDir(v->direction);
 	v->direction = dir;
 	goto getout;
 }
--- a/ship_gui.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/ship_gui.c	Wed Mar 08 06:55:33 2006 +0000
@@ -65,7 +65,7 @@
 
 static void DrawShipImage(const Vehicle *v, int x, int y, VehicleID selection)
 {
-	DrawSprite(GetShipImage(v, 6) | GetVehiclePalette(v), x + 32, y + 10);
+	DrawSprite(GetShipImage(v, DIR_W) | GetVehiclePalette(v), x + 32, y + 10);
 
 	if (v->index == selection) {
 		DrawFrameRect(x - 5, y - 1, x + 67, y + 21, 15, FR_BORDERONLY);
@@ -713,7 +713,7 @@
 		if (v != NULL) {
 			WP(w,traindepot_d).sel = v->index;
 			SetWindowDirty(w);
-			SetObjectToPlaceWnd(GetVehiclePalette(v) | GetShipImage(v, 6), 4, w);
+			SetObjectToPlaceWnd(GetVehiclePalette(v) | GetShipImage(v, DIR_W), 4, w);
 		}
 		break;
 
--- a/station_cmd.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/station_cmd.c	Wed Mar 08 06:55:33 2006 +0000
@@ -817,7 +817,7 @@
 	return cost;
 }
 
-static bool CanExpandRailroadStation(Station *st, uint *fin, int direction)
+static bool CanExpandRailroadStation(Station* st, uint* fin, Axis axis)
 {
 	uint curw = st->trainst_w, curh = st->trainst_h;
 	TileIndex tile = fin[0];
@@ -832,8 +832,8 @@
 		curh = max(TileY(st->train_tile) + curh, TileY(tile) + h) - y;
 		tile = TileXY(x, y);
 	} else {
-		// check so the direction is the same
-		if ((_m[st->train_tile].m5 & 1) != direction) {
+		// check so the orientation is the same
+		if ((_m[st->train_tile].m5 & 1U) != axis) {
 			_error_message = STR_306D_NONUNIFORM_STATIONS_DISALLOWED;
 			return false;
 		}
@@ -934,7 +934,7 @@
 	int32 cost, ret;
 	StationID est;
 	int plat_len, numtracks;
-	int direction;
+	Axis axis;
 	uint finalvalues[3];
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
@@ -946,16 +946,16 @@
 	if (!ValParamRailtype(p2 & 0xF)) return CMD_ERROR;
 
 	/* unpack parameters */
-	direction = p1 & 1;
+	axis = p1 & 1;
 	numtracks = GB(p1,  8, 8);
 	plat_len  = GB(p1, 16, 8);
 	/* w = length, h = num_tracks */
-	if (direction) {
+	if (axis == AXIS_X) {
+		w_org = plat_len;
+		h_org = numtracks;
+	} else {
 		h_org = plat_len;
 		w_org = numtracks;
-	} else {
-		w_org = plat_len;
-		h_org = numtracks;
 	}
 
 	if (h_org > _patches.station_spread || w_org > _patches.station_spread) return CMD_ERROR;
@@ -969,7 +969,7 @@
 	est = INVALID_STATION;
 	// If DC_EXEC is in flag, do not want to pass it to CheckFlatLandBelow, because of a nice bug
 	//  for detail info, see: https://sourceforge.net/tracker/index.php?func=detail&aid=1029064&group_id=103924&atid=636365
-	if (CmdFailed(ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags&~DC_EXEC, 5 << direction, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR;
+	if (CmdFailed(ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags&~DC_EXEC, 5 << axis, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR;
 	cost = ret + (numtracks * _price.train_station_track + _price.train_station_length) * plat_len;
 
 	// Make sure there are no similar stations around us.
@@ -991,7 +991,7 @@
 			// check if we want to expanding an already existing station?
 			if (_is_old_ai_player || !_patches.join_stations)
 				return_cmd_error(STR_3005_TOO_CLOSE_TO_ANOTHER_RAILROAD);
-			if (!CanExpandRailroadStation(st, finalvalues, direction))
+			if (!CanExpandRailroadStation(st, finalvalues, axis))
 				return CMD_ERROR;
 		}
 
@@ -1021,7 +1021,7 @@
 		// Now really clear the land below the station
 		// It should never return CMD_ERROR.. but you never know ;)
 		//  (a bit strange function name for it, but it really does clear the land, when DC_EXEC is in flags)
-		if (CmdFailed(CheckFlatLandBelow(tile_org, w_org, h_org, flags, 5 << direction, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR;
+		if (CmdFailed(CheckFlatLandBelow(tile_org, w_org, h_org, flags, 5 << axis, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR;
 
 		st->train_tile = finalvalues[0];
 		if (!st->facilities) st->xy = finalvalues[0];
@@ -1033,8 +1033,8 @@
 
 		st->build_date = _date;
 
-		tile_delta = direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
-		track = direction ? TRACK_Y : TRACK_X;
+		tile_delta = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
+		track = (axis == AXIS_X ? TRACK_X : TRACK_Y);
 
 		statspec = (p2 & 0x10) != 0 ? GetCustomStation(STAT_CLASS_DFLT, p2 >> 8) : NULL;
 		layout_ptr = alloca(numtracks * plat_len);
@@ -1051,7 +1051,7 @@
 					station_index, /* map2 parameter */
 					p2 & 0xFF,     /* map3lo parameter */
 					p2 >> 8,       /* map3hi parameter */
-					(*layout_ptr++) + direction   /* map5 parameter */
+					(*layout_ptr++) + axis   /* map5 parameter */
 				);
 
 				tile += tile_delta;
@@ -1172,27 +1172,27 @@
 {
 	TileIndex t;
 	TileIndexDiff delta;
-	int dir;
+	Axis dir;
 	uint len;
 	assert(TileBelongsToRailStation(st, tile));
 
 	len = 0;
 	dir = _m[tile].m5 & 1;
-	delta = dir ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
+	delta = (dir == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
 
 	// find starting tile..
 	t = tile;
 	do {
 		t -= delta;
 		len++;
-	} while (TileBelongsToRailStation(st, t) && (_m[t].m5 & 1) == dir);
+	} while (TileBelongsToRailStation(st, t) && (_m[t].m5 & 1U) == dir);
 
 	// find ending tile
 	t = tile;
 	do {
 		t += delta;
 		len++;
-	} while (TileBelongsToRailStation(st, t) && (_m[t].m5 & 1) == dir);
+	} while (TileBelongsToRailStation(st, t) && (_m[t].m5 & 1U) == dir);
 
 	return len - 1;
 }
@@ -1806,17 +1806,17 @@
 {
 	TileIndex tile = TileVirtXY(x, y);
 	TileIndex tile_cur;
-	int direction;
+	DiagDirection direction;
 	int32 cost;
 	Station *st;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
 	switch (GetTileSlope(tile, NULL)) {
-		case  3: direction = 0; break;
-		case  6: direction = 3; break;
-		case  9: direction = 1; break;
-		case 12: direction = 2; break;
+		case  3: direction = DIAGDIR_NE; break;
+		case  6: direction = DIAGDIR_NW; break;
+		case  9: direction = DIAGDIR_SE; break;
+		case 12: direction = DIAGDIR_SW; break;
 		default: return_cmd_error(STR_304B_SITE_UNSUITABLE);
 	}
 
@@ -1898,7 +1898,8 @@
 			MP_MAP2 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR |
 			MP_MAP5,
 			st->index,
-			(direction&1) + 0x50);
+			DiagDirToAxis(direction) + 0x50
+		);
 
 		UpdateStationVirtCoordDirty(st);
 		UpdateStationAcceptance(st, false);
--- a/train_cmd.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/train_cmd.c	Wed Mar 08 06:55:33 2006 +0000
@@ -239,8 +239,8 @@
 
 	//first find the curve speed limit
 	for (u = v; u->next != NULL; u = u->next, pos++) {
-		int dir = u->direction;
-		int ndir = u->next->direction;
+		Direction dir = u->direction;
+		Direction ndir = u->next->direction;
 		int i;
 
 		for (i = 0; i < 2; i++) {
@@ -373,7 +373,7 @@
 	v->acceleration = clamp(power / weight * 4, 1, 255);
 }
 
-int GetTrainImage(const Vehicle *v, byte direction)
+int GetTrainImage(const Vehicle* v, Direction direction)
 {
 	int img = v->spritenum;
 	int base;
@@ -1444,8 +1444,8 @@
 		swap_byte(&a->direction, &b->direction);
 
 		/* toggle direction */
-		if (!(a->u.rail.track & 0x80)) a->direction ^= 4;
-		if (!(b->u.rail.track & 0x80)) b->direction ^= 4;
+		if (!(a->u.rail.track & 0x80)) a->direction = ReverseDir(a->direction);
+		if (!(b->u.rail.track & 0x80)) b->direction = ReverseDir(b->direction);
 
 		/* swap more variables */
 		swap_int32(&a->x_pos, &b->x_pos);
@@ -1462,7 +1462,7 @@
 		VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
 		VehicleEnterTile(b, b->tile, b->x_pos, b->y_pos);
 	} else {
-		if (!(a->u.rail.track & 0x80)) a->direction ^= 4;
+		if (!(a->u.rail.track & 0x80)) a->direction = ReverseDir(a->direction);
 		UpdateVarsAfterSwap(a);
 
 		VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
@@ -2492,10 +2492,10 @@
 	return old_z;
 }
 
-static const byte _new_vehicle_direction_table[11] = {
-	0, 7, 6, 0,
-	1, 0, 5, 0,
-	2, 3, 4,
+static const Direction _new_vehicle_direction_table[11] = {
+	DIR_N , DIR_NW, DIR_W , 0,
+	DIR_NE, DIR_N , DIR_SW, 0,
+	DIR_E , DIR_SE, DIR_S
 };
 
 static Direction GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile)
@@ -2589,7 +2589,7 @@
 };
 
 /* Modify the speed of the vehicle due to a turn */
-static void AffectSpeedByDirChange(Vehicle *v, byte new_dir)
+static void AffectSpeedByDirChange(Vehicle* v, Direction new_dir)
 {
 	byte diff;
 	const RailtypeSlowdownParams *rsp;
@@ -2617,9 +2617,9 @@
 	}
 }
 
-static const byte _otherside_signal_directions[14] = {
-	1, 3, 1, 3, 5, 3, 0, 0,
-	5, 7, 7, 5, 7, 1,
+static const Direction _otherside_signal_directions[] = {
+	DIR_NE, DIR_SE, DIR_NE, DIR_SE, DIR_SW, DIR_SE, 0, 0,
+	DIR_SW, DIR_NW, DIR_NW, DIR_SW, DIR_NW, DIR_NE,
 };
 
 static void TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
@@ -2732,7 +2732,7 @@
 
 typedef struct VehicleAtSignalData {
 	TileIndex tile;
-	byte direction;
+	Direction direction;
 } VehicleAtSignalData;
 
 static void *CheckVehicleAtSignal(Vehicle *v, void *data)
@@ -2757,7 +2757,7 @@
 	DiagDirection enterdir;
 	Direction dir;
 	Direction newdir;
-	byte chosen_dir;
+	Direction chosen_dir;
 	byte chosen_track;
 	byte old_z;
 
@@ -2938,7 +2938,7 @@
 				TileIndex o_tile = gp.new_tile + TileOffsByDir(enterdir);
 				VehicleAtSignalData vasd;
 				vasd.tile = o_tile;
-				vasd.direction = dir ^ 4;
+				vasd.direction = ReverseDir(dir);
 
 				/* check if a train is waiting on the other side */
 				if (VehicleFromPos(o_tile, &vasd, CheckVehicleAtSignal) == NULL) return;
--- a/train_gui.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/train_gui.c	Wed Mar 08 06:55:33 2006 +0000
@@ -362,7 +362,7 @@
 
 			if (dx + width <= count) {
 				PalSpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
-				DrawSprite(GetTrainImage(v, 6) | pal, x + 14 + WagonLengthToPixels(dx), y + 6 + (is_custom_sprite(RailVehInfo(v->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0));
+				DrawSprite(GetTrainImage(v, DIR_W) | pal, x + 14 + WagonLengthToPixels(dx), y + 6 + (is_custom_sprite(RailVehInfo(v->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0));
 				if (v->index == selection)
 					DrawFrameRect(x - 1 + WagonLengthToPixels(dx), y - 1, x + WagonLengthToPixels(dx + width) - 1, y + 12, 15, FR_BORDERONLY);
 			}
@@ -577,7 +577,7 @@
 			TrainDepotMoveVehicle(v, sel, gdvp.head);
 		} else if (v != NULL) {
 			WP(w,traindepot_d).sel = v->index;
-			SetObjectToPlaceWnd(GetVehiclePalette(v) | GetTrainImage(v, 6), 4, w);
+			SetObjectToPlaceWnd(GetVehiclePalette(v) | GetTrainImage(v, DIR_W), 4, w);
 			SetWindowDirty(w);
 		}
 		break;
--- a/tunnelbridge_cmd.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/tunnelbridge_cmd.c	Wed Mar 08 06:55:33 2006 +0000
@@ -119,7 +119,7 @@
  *	is_start_tile = false		<-- end tile
  *	is_start_tile = true		<-- start tile
  */
-static uint32 CheckBridgeSlope(uint direction, uint tileh, bool is_start_tile)
+static uint32 CheckBridgeSlope(Axis direction, uint tileh, bool is_start_tile)
 {
 	if (IsSteepTileh(tileh)) return CMD_ERROR;
 
@@ -129,7 +129,7 @@
 				- direction X: tiles 0, 12
 				- direction Y: tiles 0,  9
 		*/
-		if ((direction ? 0x201 : 0x1001) & (1 << tileh)) return 0;
+		if ((direction == AXIS_X ? 0x1001 : 0x201) & (1 << tileh)) return 0;
 
 		// disallow certain start tiles to avoid certain crooked bridges
 		if (tileh == 2) return CMD_ERROR;
@@ -139,7 +139,7 @@
 				- direction X: tiles 0, 3
 				- direction Y: tiles 0, 6
 		*/
-		if ((direction? 0x41 : 0x9) & (1 << tileh)) return 0;
+		if ((direction == AXIS_X ? 0x9 : 0x41) & (1 << tileh)) return 0;
 
 		// disallow certain end tiles to avoid certain crooked bridges
 		if (tileh == 8) return CMD_ERROR;
@@ -149,8 +149,8 @@
 	 *	start-tile:	X 2,1 Y 2,4 (2 was disabled before)
 	 *	end-tile:		X 8,4 Y 8,1 (8 was disabled before)
 	 */
-	if ((tileh == 1 && is_start_tile != (bool)direction) ||
-			(tileh == 4 && is_start_tile == (bool)direction)) {
+	if ((tileh == 1 && is_start_tile != (direction != AXIS_X)) ||
+			(tileh == 4 && is_start_tile == (direction != AXIS_X))) {
 		return CMD_ERROR;
 	}
 
@@ -200,7 +200,7 @@
 	TileInfo ti_start, ti_end, ti; /* OPT: only 2 of those are ever used */
 	uint bridge_len;
 	uint odd_middle_part;
-	uint direction;
+	Axis direction;
 	uint i;
 	int32 cost, terraformcost, ret;
 	bool allow_on_slopes;
@@ -225,17 +225,16 @@
 	sx = TileX(p1) * 16;
 	sy = TileY(p1) * 16;
 
-	direction = 0;
-
 	/* check if valid, and make sure that (x,y) are smaller than (sx,sy) */
 	if (x == sx) {
 		if (y == sy) return_cmd_error(STR_5008_CANNOT_START_AND_END_ON);
-		direction = 1;
+		direction = AXIS_Y;
 		if (y > sy) {
 			intswap(y,sy);
 			intswap(x,sx);
 		}
 	} else if (y == sy) {
+		direction = AXIS_X;
 		if (x > sx) {
 			intswap(y,sy);
 			intswap(x,sx);
@@ -327,10 +326,10 @@
 	odd_middle_part = (bridge_len % 2) ? (bridge_len / 2) : bridge_len;
 
 	for (i = 0; i != bridge_len; i++) {
-		if (direction != 0) {
+		if (direction == AXIS_X) {
+			x += 16;
+		} else {
 			y += 16;
-		} else {
-			x += 16;
 		}
 
 		FindLandscapeHeight(&ti, x, y);
@@ -349,13 +348,15 @@
 				break;
 
 			case MP_RAILWAY:
-				if (ti.map5 != (direction == 0 ? 2 : 1)) goto not_valid_below;
+				if (ti.map5 != (direction == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X)) {
+					goto not_valid_below;
+				}
 				m5 = 0xE0;
 				break;
 
 			case MP_STREET:
 				if (GetRoadType(ti.tile) != ROAD_NORMAL ||
-						GetRoadBits(ti.tile) != (direction == 0 ? ROAD_Y : ROAD_X)) {
+						GetRoadBits(ti.tile) != (direction == AXIS_X ? ROAD_Y : ROAD_X)) {
 					goto not_valid_below;
 				}
 				m5 = 0xE8;
@@ -408,7 +409,7 @@
 		}
 	}
 
-	SetSignalsOnBothDir(ti_start.tile, (direction & 1) ? 1 : 0);
+	SetSignalsOnBothDir(ti_start.tile, direction == AXIS_X ? TRACK_X : TRACK_Y);
 
 	/*	for human player that builds the bridge he gets a selection to choose from bridges (DC_QUERY_COST)
 			It's unnecessary to execute this command every time for every bridge. So it is done only
@@ -546,7 +547,6 @@
 	Town *t;
 	TileIndex endtile;
 	uint length;
-	static const byte _updsignals_tunnel_dir[4] = { 5, 7, 1, 3};
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
@@ -574,8 +574,7 @@
 	if (flags & DC_EXEC) {
 		// We first need to request the direction before calling DoClearSquare
 		//  else the direction is always 0.. dah!! ;)
-		DiagDirection tile_dir    = GetTunnelDirection(tile);
-		DiagDirection endtile_dir = GetTunnelDirection(endtile);
+		DiagDirection dir = GetTunnelDirection(tile);
 
 		// Adjust the town's player rating. Do this before removing the tile owner info.
 		if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR)
@@ -583,22 +582,22 @@
 
 		DoClearSquare(tile);
 		DoClearSquare(endtile);
-		UpdateSignalsOnSegment(tile, _updsignals_tunnel_dir[tile_dir]);
-		UpdateSignalsOnSegment(endtile, _updsignals_tunnel_dir[endtile_dir]);
+		UpdateSignalsOnSegment(tile, DiagDirToDir(ReverseDiagDir(dir)));
+		UpdateSignalsOnSegment(endtile, DiagDirToDir(dir));
 	}
 	return _price.clear_tunnel * (length + 1);
 }
 
 static TileIndex FindEdgesOfBridge(TileIndex tile, TileIndex *endtile)
 {
-	int direction = GB(_m[tile].m5, 0, 1);
+	Axis direction = GB(_m[tile].m5, 0, 1);
 	TileIndex start;
 
 	// find start of bridge
 	for (;;) {
 		if (IsTileType(tile, MP_TUNNELBRIDGE) && (_m[tile].m5 & 0xE0) == 0x80)
 			break;
-		tile += direction ? TileDiffXY(0, -1) : TileDiffXY(-1, 0);
+		tile += (direction == AXIS_X ? TileDiffXY(-1, 0) : TileDiffXY(0, -1));
 	}
 
 	start = tile;
@@ -607,7 +606,7 @@
 	for (;;) {
 		if (IsTileType(tile, MP_TUNNELBRIDGE) && (_m[tile].m5 & 0xE0) == 0xA0)
 			break;
-		tile += direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
+		tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
 	}
 
 	*endtile = tile;
@@ -620,7 +619,7 @@
 	TileIndex endtile;
 	Vehicle *v;
 	Town *t;
-	int direction;
+	Axis direction;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
@@ -672,8 +671,8 @@
 			Omit tile and endtile, since these are already checked, thus solving the problem
 			of bridges over water, or higher bridges, where z is not increased, eg level bridge
 	*/
-	tile		+= direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
-	endtile	-= direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
+	tile		+= (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
+	endtile	-= (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
 	/* Bridges on slopes might have their Z-value offset..correct this */
 	v = FindVehicleBetween(tile, endtile, TilePixelHeight(tile) + 8 + GetCorrectTileHeight(tile));
 	if (v != NULL) {
@@ -682,9 +681,8 @@
 	}
 
 	/* Put the tiles back to start/end position */
-	tile		-= direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
-	endtile	+= direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
-
+	tile		-= (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
+	endtile	+= (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
 
 	t = ClosestTownFromTile(tile, (uint)-1); //needed for town rating penalty
 	// check if you're allowed to remove the bridge owned by a town.
@@ -726,17 +724,17 @@
 clear_it:;
 				DoClearSquare(c);
 			}
-			c += direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
+			c += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
 		} while (c <= endtile);
 
-		SetSignalsOnBothDir(tile, direction);
-		SetSignalsOnBothDir(endtile, direction);
+		SetSignalsOnBothDir(tile,    direction == AXIS_X ? TRACK_X : TRACK_Y);
+		SetSignalsOnBothDir(endtile, direction == AXIS_X ? TRACK_X : TRACK_Y);
 	}
 
-	if (direction) {
+	if (direction == AXIS_X) {
+		return (TileX(endtile) - TileX(tile) + 1) * _price.clear_bridge;
+	} else {
 		return (TileY(endtile) - TileY(tile) + 1) * _price.clear_bridge;
-	} else {
-		return (TileX(endtile) - TileX(tile) + 1) * _price.clear_bridge;
 	}
 }
 
@@ -918,7 +916,7 @@
 	}
 }
 
-uint GetBridgeFoundation(uint tileh, byte direction)
+uint GetBridgeFoundation(uint tileh, Axis axis)
 {
 	int i;
 	// normal level sloped building (7, 11, 13, 14)
@@ -931,8 +929,8 @@
 				(i += 2, tileh == 4) ||
 				(i += 2, tileh == 8)
 			) && (
-				direction == 0 ||
-				(i++, direction == 1)
+				      axis == AXIS_X ||
+				(i++, axis == AXIS_Y)
 			)) {
 		return i + 15;
 	}
--- a/vehicle.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/vehicle.c	Wed Mar 08 06:55:33 2006 +0000
@@ -1921,15 +1921,16 @@
 	return gp->old_tile == gp->new_tile;
 }
 
-static const byte _new_direction_table[9] = {
-	0, 7, 6,
-	1, 3, 5,
-	2, 3, 4,
+static const Direction _new_direction_table[] = {
+	DIR_N , DIR_NW, DIR_W ,
+	DIR_NE, DIR_SE, DIR_SW,
+	DIR_E , DIR_SE, DIR_S
 };
 
-byte GetDirectionTowards(const Vehicle *v, int x, int y)
+Direction GetDirectionTowards(const Vehicle* v, int x, int y)
 {
-	byte dirdiff, dir;
+	Direction dir;
+	byte dirdiff;
 	int i = 0;
 
 	if (y >= v->y_pos) {
--- a/vehicle.h	Tue Mar 07 07:51:05 2006 +0000
+++ b/vehicle.h	Wed Mar 08 06:55:33 2006 +0000
@@ -269,10 +269,10 @@
 void TrainEnterDepot(Vehicle *v, TileIndex tile);
 
 /* train_cmd.h */
-int GetTrainImage(const Vehicle *v, byte direction);
-int GetAircraftImage(const Vehicle *v, byte direction);
-int GetRoadVehImage(const Vehicle *v, byte direction);
-int GetShipImage(const Vehicle *v, byte direction);
+int GetTrainImage(const Vehicle* v, Direction direction);
+int GetAircraftImage(const Vehicle* v, Direction direction);
+int GetRoadVehImage(const Vehicle* v, Direction direction);
+int GetShipImage(const Vehicle* v, Direction direction);
 
 Vehicle *CreateEffectVehicle(int x, int y, int z, EffectVehicle type);
 Vehicle *CreateEffectVehicleAbove(int x, int y, int z, EffectVehicle type);
@@ -332,7 +332,7 @@
 
 /* returns true if staying in the same tile */
 bool GetNewVehiclePos(const Vehicle *v, GetNewVehiclePosResult *gp);
-byte GetDirectionTowards(const Vehicle *v, int x, int y);
+Direction GetDirectionTowards(const Vehicle* v, int x, int y);
 
 #define BEGIN_ENUM_WAGONS(v) do {
 #define END_ENUM_WAGONS(v) } while ( (v=v->next) != NULL);
--- a/water_cmd.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/water_cmd.c	Wed Mar 08 06:55:33 2006 +0000
@@ -127,7 +127,7 @@
 }
 
 // build a shiplift
-static int32 DoBuildShiplift(TileIndex tile, int dir, uint32 flags)
+static int32 DoBuildShiplift(TileIndex tile, DiagDirection dir, uint32 flags)
 {
 	int32 ret;
 	int delta;
@@ -189,17 +189,18 @@
 int32 CmdBuildLock(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	TileIndex tile = TileVirtXY(x, y);
-	uint tileh;
+	DiagDirection dir;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
-	tileh = GetTileSlope(tile, NULL);
 
-	if (tileh == 3 || tileh == 6 || tileh == 9 || tileh == 12) {
-		static const byte _shiplift_dirs[16] = {0, 0, 0, 2, 0, 0, 1, 0, 0, 3, 0, 0, 0};
-		return DoBuildShiplift(tile, _shiplift_dirs[tileh], flags);
+	switch (GetTileSlope(tile, NULL)) {
+		case  3: dir = DIAGDIR_SW; break;
+		case  6: dir = DIAGDIR_SE; break;
+		case  9: dir = DIAGDIR_NW; break;
+		case 12: dir = DIAGDIR_NE; break;
+		default: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
 	}
-
-	return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
+	return DoBuildShiplift(tile, dir, flags);
 }
 
 /** Build a piece of canal.
--- a/waypoint.c	Tue Mar 07 07:51:05 2006 +0000
+++ b/waypoint.c	Wed Mar 08 06:55:33 2006 +0000
@@ -292,7 +292,7 @@
 		return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
-		int direction = _m[tile].m5 & RAIL_WAYPOINT_TRACK_MASK;
+		Axis direction = _m[tile].m5 & RAIL_WAYPOINT_TRACK_MASK;
 
 		wp = GetWaypointByTile(tile);
 
@@ -304,7 +304,7 @@
 			MarkTileDirtyByTile(tile);
 		} else {
 			DoClearSquare(tile);
-			SetSignalsOnBothDir(tile, direction);
+			SetSignalsOnBothDir(tile, direction == AXIS_X ? TRACK_X : TRACK_Y);
 		}
 	}