src/tunnelbridge_cmd.cpp
branchNewGRF_ports
changeset 6878 7d1ff2f621c7
parent 6877 889301acc299
child 10184 fcf5fb2548eb
--- a/src/tunnelbridge_cmd.cpp	Sun Feb 03 20:34:26 2008 +0000
+++ b/src/tunnelbridge_cmd.cpp	Mon Mar 10 15:26:39 2008 +0000
@@ -33,82 +33,30 @@
 #include "sound_func.h"
 #include "signal_func.h"
 #include "tunnelbridge.h"
+#include "player_base.h"
 
 #include "table/sprites.h"
 #include "table/strings.h"
 #include "table/bridge_land.h"
 
-
-/** Describes the data that defines each bridge in the game
- * @param y   year of availablity
- * @param mnl minimum length
- * @param mxl maximum length
- * @param p   price
- * @param mxs maximum speed allowed
- * @param spr sprite to use in purchase GUI
- * @param plt palette for the sprite in purchase GUI
- * @param dsc description of the bridge in purchase GUI
- * @param nrl description of the rail bridge in query tool
- * @param nrd description of the road bridge in query tool
- */
-#define MB(y, mnl, mxl, p, mxs, spr, plt, dsc, nrl, nrd) \
-	{y, mnl, mxl, p, mxs, spr, plt, dsc, nrl, nrd, NULL, 0}
-
-const Bridge orig_bridge[] = {
-/*
-	       year of availablity
-	       |  minimum length
-	       |  |   maximum length
-	       |  |   |    price
-	       |  |   |    |    maximum speed
-	       |  |   |    |    |  sprite to use in GUI
-	       |  |   |    |    |  |      palette in GUI
-	   string with description        name on rail                             name on road
-	   |                              |                                        | */
-	MB(    0, 0, 16,  80,  32, 0xA24, PAL_NONE,
-	   STR_5012_WOODEN,               STR_501F_WOODEN_RAIL_BRIDGE,             STR_5025_WOODEN_ROAD_BRIDGE),
-
-	MB(    0, 0,  2, 112,  48, 0xA26, PALETTE_TO_STRUCT_RED,
-	   STR_5013_CONCRETE,             STR_5020_CONCRETE_RAIL_BRIDGE,           STR_5026_CONCRETE_ROAD_BRIDGE),
+BridgeSpec _bridge[MAX_BRIDGES];
 
-	MB( 1930, 0,  5, 144,  64, 0xA25, PAL_NONE,
-	   STR_500F_GIRDER_STEEL,         STR_501C_STEEL_GIRDER_RAIL_BRIDGE,       STR_5022_STEEL_GIRDER_ROAD_BRIDGE),
-
-	MB(    0, 2, 10, 168,  80, 0xA22, PALETTE_TO_STRUCT_CONCRETE,
-	   STR_5011_SUSPENSION_CONCRETE,  STR_501E_REINFORCED_CONCRETE_SUSPENSION, STR_5024_REINFORCED_CONCRETE_SUSPENSION),
-
-	MB( 1930, 3, 16, 185,  96, 0xA22, PAL_NONE,
-	   STR_500E_SUSPENSION_STEEL,     STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE,   STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE),
-
-	MB( 1930, 3, 16, 192, 112, 0xA22, PALETTE_TO_STRUCT_YELLOW,
-	   STR_500E_SUSPENSION_STEEL,     STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE,   STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE),
-
-	MB( 1930, 3,  7, 224, 160, 0xA23, PAL_NONE,
-	   STR_5010_CANTILEVER_STEEL,     STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE,   STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE),
-
-	MB( 1930, 3,  8, 232, 208, 0xA23, PALETTE_TO_STRUCT_BROWN,
-	   STR_5010_CANTILEVER_STEEL,     STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE,   STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE),
+/** Reset the data been eventually changed by the grf loaded. */
+void ResetBridges()
+{
+	/* First, free sprite table data */
+	for (BridgeType i = 0; i < MAX_BRIDGES; i++) {
+		if (_bridge[i].sprite_table != NULL) {
+			for (uint j = 0; j < 7; j++) free(_bridge[i].sprite_table[j]);
+			free(_bridge[i].sprite_table);
+		}
+	}
 
-	MB( 1930, 3,  9, 248, 240, 0xA23, PALETTE_TO_STRUCT_RED,
-	   STR_5010_CANTILEVER_STEEL,     STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE,   STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE),
-
-	MB( 1930, 0,  2, 240, 256, 0xA27, PAL_NONE,
-	   STR_500F_GIRDER_STEEL,         STR_501C_STEEL_GIRDER_RAIL_BRIDGE,       STR_5022_STEEL_GIRDER_ROAD_BRIDGE),
-
-	MB( 1995, 2, 16, 255, 320, 0xA28, PAL_NONE,
-	   STR_5014_TUBULAR_STEEL,        STR_5027_TUBULAR_RAIL_BRIDGE,            STR_5028_TUBULAR_ROAD_BRIDGE),
-
-	MB( 2005, 2, 32, 380, 512, 0xA28, PALETTE_TO_STRUCT_YELLOW,
-	   STR_5014_TUBULAR_STEEL,        STR_5027_TUBULAR_RAIL_BRIDGE,            STR_5028_TUBULAR_ROAD_BRIDGE),
-
-	MB( 2010, 2, 32, 510, 608, 0xA28, PALETTE_TO_STRUCT_GREY,
-	   STR_BRIDGE_TUBULAR_SILICON,    STR_5027_TUBULAR_RAIL_BRIDGE,            STR_5028_TUBULAR_ROAD_BRIDGE)
-};
-
-#undef MB
-
-Bridge _bridge[MAX_BRIDGES];
-
+	/* Then, wipe out current bidges */
+	memset(&_bridge, 0, sizeof(_bridge));
+	/* And finally, reinstall default data */
+	memcpy(&_bridge, &_orig_bridge, sizeof(_orig_bridge));
+}
 
 /** calculate the price factor for building a long bridge.
  * basically the cost delta is 1,1, 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5, 6,6,6,6,6,6,  7,7,7,7,7,7,7,  8,8,8,8,8,8,8,8,
@@ -152,7 +100,7 @@
 
 static inline const PalSpriteID *GetBridgeSpriteTable(int index, byte table)
 {
-	const Bridge *bridge = &_bridge[index];
+	const BridgeSpec *bridge = GetBridgeSpec(index);
 	assert(table < 7);
 	if (bridge->sprite_table == NULL || bridge->sprite_table[table] == NULL) {
 		return _bridge_sprite_table[index][table];
@@ -161,8 +109,6 @@
 	}
 }
 
-static inline byte GetBridgeFlags(int index) { return _bridge[index].flags;}
-
 
 /**
  * Determines the foundation for the north bridge head, and tests if the resulting slope is valid.
@@ -206,9 +152,9 @@
 	return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
 }
 
-bool CheckBridge_Stuff(byte bridge_type, uint bridge_len)
+bool CheckBridge_Stuff(BridgeType bridge_type, uint bridge_len)
 {
-	const Bridge *b = &_bridge[bridge_type];
+	const BridgeSpec *b = GetBridgeSpec(bridge_type);
 	uint max; // max possible length of a bridge (with patch 100)
 
 	if (bridge_type >= MAX_BRIDGES) return false;
@@ -231,9 +177,9 @@
  */
 CommandCost CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
 {
-	uint bridge_type;
-	RailType railtype;
-	RoadTypes roadtypes;
+	BridgeType bridge_type;
+	RailType railtype = INVALID_RAILTYPE;
+	RoadTypes roadtypes = ROADTYPES_NONE;
 	uint x;
 	uint y;
 	uint sx;
@@ -251,22 +197,32 @@
 	CommandCost cost(EXPENSES_CONSTRUCTION);
 	CommandCost ret;
 	bool replace_bridge = false;
-	uint replaced_bridge_type;
+	BridgeType replaced_bridge_type;
+	TransportType transport_type;
 
 	/* unpack parameters */
 	bridge_type = GB(p2, 0, 8);
 
 	if (p1 >= MapSize()) return CMD_ERROR;
 
+	transport_type = (TransportType)GB(p2, 15, 2);
+
 	/* type of bridge */
-	if (HasBit(p2, 15)) {
-		railtype = INVALID_RAILTYPE; // road bridge
-		roadtypes = (RoadTypes)GB(p2, 8, 3);
-		if (!AreValidRoadTypes(roadtypes) || !HasRoadTypesAvail(_current_player, roadtypes)) return CMD_ERROR;
-	} else {
-		if (!ValParamRailtype((RailType)GB(p2, 8, 8))) return CMD_ERROR;
-		railtype = (RailType)GB(p2, 8, 8);
-		roadtypes = ROADTYPES_NONE;
+	switch (transport_type) {
+		case TRANSPORT_ROAD:
+			roadtypes = (RoadTypes)GB(p2, 8, 3);
+			if (!AreValidRoadTypes(roadtypes) || !HasRoadTypesAvail(_current_player, roadtypes)) return CMD_ERROR;
+			break;
+
+		case TRANSPORT_RAIL:
+			railtype = (RailType)GB(p2, 8, 8);
+			if (!ValParamRailtype(railtype)) return CMD_ERROR;
+			break;
+
+		default:
+			/* For now, only TRANSPORT_RAIL and TRANSPORT_ROAD are allowed.
+			 * But let not this stops us for preparing the future */
+			return CMD_ERROR;
 	}
 
 	x = TileX(end_tile);
@@ -301,12 +257,10 @@
 	tileh_end = GetTileSlope(tile_end, &z_end);
 
 	CommandCost terraform_cost_north = CheckBridgeSlopeNorth(direction, &tileh_start, &z_start);
-	CommandCost terraform_cost_south = CheckBridgeSlopeSouth(direction, &tileh_end, &z_end);
+	CommandCost terraform_cost_south = CheckBridgeSlopeSouth(direction, &tileh_end,   &z_end);
 
 	if (z_start != z_end) return_cmd_error(STR_BRIDGEHEADS_NOT_SAME_HEIGHT);
 
-	TransportType transport_type = railtype == INVALID_RAILTYPE ? TRANSPORT_ROAD : TRANSPORT_RAIL;
-
 	if (IsBridgeTile(tile_start) && IsBridgeTile(tile_end) &&
 			GetOtherBridgeEnd(tile_start) == tile_end &&
 			GetTunnelBridgeTransportType(tile_start) == transport_type) {
@@ -319,7 +273,7 @@
 
 		/* Do not replace town bridges with lower speed bridges. */
 		if (!(flags & DC_QUERY_COST) && IsTileOwner(tile_start, OWNER_TOWN) &&
-				_bridge[bridge_type].speed < _bridge[GetBridgeType(tile_start)].speed) {
+				GetBridgeSpec(bridge_type)->speed < GetBridgeSpec(GetBridgeType(tile_start))->speed) {
 			Town *t = ClosestTownFromTile(tile_start, UINT_MAX);
 
 			if (t == NULL) {
@@ -395,12 +349,20 @@
 		DiagDirection dir = AxisToDiagDir(direction);
 		Owner owner = (replace_bridge && IsTileOwner(tile_start, OWNER_TOWN)) ? OWNER_TOWN : _current_player;
 
-		if (railtype != INVALID_RAILTYPE) {
-			MakeRailBridgeRamp(tile_start, owner, bridge_type, dir, railtype);
-			MakeRailBridgeRamp(tile_end,   owner, bridge_type, ReverseDiagDir(dir), railtype);
-		} else {
-			MakeRoadBridgeRamp(tile_start, owner, bridge_type, dir, roadtypes);
-			MakeRoadBridgeRamp(tile_end,   owner, bridge_type, ReverseDiagDir(dir), roadtypes);
+		switch (transport_type) {
+			case TRANSPORT_RAIL:
+				MakeRailBridgeRamp(tile_start, owner, bridge_type, dir,                 railtype);
+				MakeRailBridgeRamp(tile_end,   owner, bridge_type, ReverseDiagDir(dir), railtype);
+				break;
+
+			case TRANSPORT_ROAD:
+				MakeRoadBridgeRamp(tile_start, owner, bridge_type, dir,                 roadtypes);
+				MakeRoadBridgeRamp(tile_end,   owner, bridge_type, ReverseDiagDir(dir), roadtypes);
+				break;
+
+			default:
+				NOT_REACHED();
+				break;
 		}
 		MarkTileDirtyByTile(tile_start);
 		MarkTileDirtyByTile(tile_end);
@@ -426,7 +388,7 @@
 				break;
 
 			case MP_ROAD:
-				if (GetRoadTileType(tile) == ROAD_TILE_DEPOT) goto not_valid_below;
+				if (IsRoadDepot(tile)) goto not_valid_below;
 				break;
 
 			case MP_TUNNELBRIDGE:
@@ -458,7 +420,7 @@
 		}
 	}
 
-	if (flags & DC_EXEC && railtype != INVALID_RAILTYPE) {
+	if (flags & DC_EXEC && transport_type == TRANSPORT_RAIL) {
 		Track track = AxisToTrack(direction);
 		AddSideToSignalBuffer(tile_start, INVALID_DIAGDIR, _current_player);
 		YapfNotifyTrackLayoutChange(tile_start, track);
@@ -468,15 +430,13 @@
 	 * It's unnecessary to execute this command every time for every bridge. So it is done only
 	 * and cost is computed in "bridge_gui.c". For AI, Towns this has to be of course calculated
 	 */
-	if (!(flags & DC_QUERY_COST)) {
-		const Bridge *b = &_bridge[bridge_type];
-
+	if (!(flags & DC_QUERY_COST) || (IsValidPlayer(_current_player) && GetPlayer(_current_player)->is_ai)) {
 		bridge_len += 2; // begin and end tiles/ramps
 
 		if (IsValidPlayer(_current_player) && !_is_old_ai_player)
 			bridge_len = CalcBridgeLenCostFactor(bridge_len);
 
-		cost.AddCost((int64)bridge_len * _price.build_bridge * b->price >> 8);
+		cost.AddCost((int64)bridge_len * _price.build_bridge * GetBridgeSpec(bridge_type)->price >> 8);
 	}
 
 	return cost;
@@ -496,13 +456,14 @@
 	DiagDirection direction;
 	Slope start_tileh;
 	Slope end_tileh;
+	TransportType transport_type = (TransportType)GB(p1, 9, 1);
 	uint start_z;
 	uint end_z;
 	CommandCost cost(EXPENSES_CONSTRUCTION);
 	CommandCost ret;
 
 	_build_tunnel_endtile = 0;
-	if (!HasBit(p1, 9)) {
+	if (transport_type == TRANSPORT_RAIL) {
 		if (!ValParamRailtype((RailType)p1)) return CMD_ERROR;
 	} else {
 		const RoadTypes rts = (RoadTypes)GB(p1, 0, 3);
@@ -585,7 +546,7 @@
 	cost.AddCost(ret);
 
 	if (flags & DC_EXEC) {
-		if (GB(p1, 9, 1) == TRANSPORT_RAIL) {
+		if (transport_type == TRANSPORT_RAIL) {
 			MakeRailTunnel(start_tile, _current_player, direction,                 (RailType)GB(p1, 0, 4));
 			MakeRailTunnel(end_tile,   _current_player, ReverseDiagDir(direction), (RailType)GB(p1, 0, 4));
 			AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, _current_player);
@@ -651,11 +612,11 @@
 			DoClearSquare(endtile);
 
 			/* cannot use INVALID_DIAGDIR for signal update because the tunnel doesn't exist anymore */
-			AddSideToSignalBuffer(tile, ReverseDiagDir(dir), owner);
-			AddSideToSignalBuffer(endtile, dir, owner);
+			AddSideToSignalBuffer(tile,    ReverseDiagDir(dir), owner);
+			AddSideToSignalBuffer(endtile, dir,                 owner);
 
 			Track track = AxisToTrack(DiagDirToAxis(dir));
-			YapfNotifyTrackLayoutChange(tile, track);
+			YapfNotifyTrackLayoutChange(tile,    track);
 			YapfNotifyTrackLayoutChange(endtile, track);
 		} else {
 			DoClearSquare(tile);
@@ -703,21 +664,27 @@
 		/* read this value before actual removal of bridge */
 		bool rail = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL;
 		Owner owner = GetTileOwner(tile);
+		uint height = GetBridgeHeight(tile);
 
 		DoClearSquare(tile);
 		DoClearSquare(endtile);
 		for (TileIndex c = tile + delta; c != endtile; c += delta) {
+			/* do not let trees appear from 'nowhere' after removing bridge */
+			if (IsNormalRoadTile(c) && GetRoadside(c) == ROADSIDE_TREES) {
+				uint minz = GetTileMaxZ(c) + 3 * TILE_HEIGHT;
+				if (height < minz) SetRoadside(c, ROADSIDE_PAVED);
+			}
 			ClearBridgeMiddle(c);
 			MarkTileDirtyByTile(c);
 		}
 
 		if (rail) {
 			/* cannot use INVALID_DIAGDIR for signal update because the bridge doesn't exist anymore */
-			AddSideToSignalBuffer(tile, ReverseDiagDir(direction), owner);
-			AddSideToSignalBuffer(endtile, direction, owner);
+			AddSideToSignalBuffer(tile,    ReverseDiagDir(direction), owner);
+			AddSideToSignalBuffer(endtile, direction,                 owner);
 
 			Track track = AxisToTrack(DiagDirToAxis(direction));
-			YapfNotifyTrackLayoutChange(tile, track);
+			YapfNotifyTrackLayoutChange(tile,    track);
 			YapfNotifyTrackLayoutChange(endtile, track);
 		}
 	}
@@ -749,11 +716,11 @@
  * @param y Sprite Y position of front pillar.
  * @param z_bridge Absolute height of bridge bottom.
  */
-static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo* ti, Axis axis, uint type, int x, int y, int z_bridge)
+static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo* ti, Axis axis, BridgeType type, int x, int y, int z_bridge)
 {
 	SpriteID image = psid->sprite;
 	if (image != 0) {
-		bool drawfarpillar = !HasBit(GetBridgeFlags(type), 0);
+		bool drawfarpillar = !HasBit(GetBridgeSpec(type)->flags, 0);
 
 		/* "side" specifies the side the pillars stand on.
 		 * The length of the pillars is then set to the height of the bridge over the corners of this edge.
@@ -819,14 +786,14 @@
 	 * The bounding boxes here are the same as for bridge front/roof */
 	AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + tram_offsets[overlay][offset], PAL_NONE, x, y, size_x[offset], size_y[offset], 0x28, z, IsTransparencySet(TO_BRIDGES));
 
-	AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + back_offsets[offset],  PAL_NONE, x, y, size_x[offset], size_y[offset], 0x28, z, IsTransparencySet(TO_BUILDINGS));
+	AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + back_offsets[offset],  PAL_NONE, x, y, size_x[offset], size_y[offset], 0x28, z, IsTransparencySet(TO_CATENARY));
 
 	/* Start a new SpriteCombine for the front part */
 	EndSpriteCombine();
 	StartSpriteCombine();
 
 	/* For sloped sprites the bounding box needs to be higher, as the pylons stop on a higher point */
-	AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + front_offsets[offset], PAL_NONE, x, y, size_x[offset] + front_bb_offset_x[offset], size_y[offset] + front_bb_offset_y[offset], 0x28, z, IsTransparencySet(TO_BUILDINGS), front_bb_offset_x[offset], front_bb_offset_y[offset]);
+	AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + front_offsets[offset], PAL_NONE, x, y, size_x[offset] + front_bb_offset_x[offset], size_y[offset] + front_bb_offset_y[offset], 0x28, z, IsTransparencySet(TO_CATENARY), front_bb_offset_x[offset], front_bb_offset_y[offset]);
 }
 
 /**
@@ -845,6 +812,8 @@
 static void DrawTile_TunnelBridge(TileInfo *ti)
 {
 	SpriteID image;
+	TransportType transport_type = GetTunnelBridgeTransportType(ti->tile);
+	DiagDirection tunnelbridge_direction = GetTunnelBridgeDirection(ti->tile);
 
 	if (IsTunnel(ti->tile)) {
 		/* Front view of tunnel bounding boxes:
@@ -864,11 +833,11 @@
 			{  1,  0, -15, -14,  0, 15, 16,  1, 0, 1, 16, 15 }, // SW
 			{  0,  1, -14, -15, 15,  0,  1, 16, 1, 0, 15, 16 }, // NW
 		};
-		const int *BB_data = _tunnel_BB[GetTunnelBridgeDirection(ti->tile)];
+		const int *BB_data = _tunnel_BB[tunnelbridge_direction];
 
 		bool catenary = false;
 
-		if (GetTunnelBridgeTransportType(ti->tile) == TRANSPORT_RAIL) {
+		if (transport_type == TRANSPORT_RAIL) {
 			image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.tunnel;
 		} else {
 			image = SPR_TUNNEL_ENTRY_REAR_ROAD;
@@ -876,20 +845,19 @@
 
 		if (HasTunnelBridgeSnowOrDesert(ti->tile)) image += 32;
 
-		image += GetTunnelBridgeDirection(ti->tile) * 2;
+		image += tunnelbridge_direction * 2;
 		DrawGroundSprite(image, PAL_NONE);
-		if (GetTunnelBridgeTransportType(ti->tile) == TRANSPORT_ROAD) {
-			DiagDirection dir = GetTunnelBridgeDirection(ti->tile);
+		if (transport_type == TRANSPORT_ROAD) {
 			RoadTypes rts = GetRoadTypes(ti->tile);
 
 			if (HasBit(rts, ROADTYPE_TRAM)) {
 				static const SpriteID tunnel_sprites[2][4] = { { 28, 78, 79, 27 }, {  5, 76, 77,  4 } };
 
-				DrawGroundSprite(SPR_TRAMWAY_BASE + tunnel_sprites[rts - ROADTYPES_TRAM][dir], PAL_NONE);
+				DrawGroundSprite(SPR_TRAMWAY_BASE + tunnel_sprites[rts - ROADTYPES_TRAM][tunnelbridge_direction], PAL_NONE);
 
 				catenary = true;
 				StartSpriteCombine();
-				AddSortableSpriteToDraw(SPR_TRAMWAY_TUNNEL_WIRES + dir, PAL_NONE, ti->x, ti->y, BB_data[10], BB_data[11], TILE_HEIGHT, ti->z, IsTransparencySet(TO_BUILDINGS), BB_data[8], BB_data[9], BB_Z_SEPARATOR);
+				AddSortableSpriteToDraw(SPR_TRAMWAY_TUNNEL_WIRES + tunnelbridge_direction, PAL_NONE, ti->x, ti->y, BB_data[10], BB_data[11], TILE_HEIGHT, ti->z, IsTransparencySet(TO_CATENARY), BB_data[8], BB_data[9], BB_Z_SEPARATOR);
 			}
 		} else if (GetRailType(ti->tile) == RAILTYPE_ELECTRIC) {
 			DrawCatenary(ti);
@@ -913,7 +881,7 @@
 		int base_offset;
 		bool ice = HasTunnelBridgeSnowOrDesert(ti->tile);
 
-		if (GetTunnelBridgeTransportType(ti->tile) == TRANSPORT_RAIL) {
+		if (transport_type == TRANSPORT_RAIL) {
 			base_offset = GetRailTypeInfo(GetRailType(ti->tile))->bridge_offset;
 			assert(base_offset != 8); // This one is used for roads
 		} else {
@@ -923,10 +891,10 @@
 		/* as the lower 3 bits are used for other stuff, make sure they are clear */
 		assert( (base_offset & 0x07) == 0x00);
 
-		DrawFoundation(ti, GetBridgeFoundation(ti->tileh, DiagDirToAxis(GetTunnelBridgeDirection(ti->tile))));
+		DrawFoundation(ti, GetBridgeFoundation(ti->tileh, DiagDirToAxis(tunnelbridge_direction)));
 
 		/* HACK Wizardry to convert the bridge ramp direction into a sprite offset */
-		base_offset += (6 - GetTunnelBridgeDirection(ti->tile)) % 4;
+		base_offset += (6 - tunnelbridge_direction) % 4;
 
 		if (ti->tileh == SLOPE_FLAT) base_offset += 4; // sloped bridge head
 
@@ -942,7 +910,7 @@
 		/* draw ramp */
 
 		/* Draw Trambits as SpriteCombine */
-		if (GetTunnelBridgeTransportType(ti->tile) == TRANSPORT_ROAD) StartSpriteCombine();
+		if (transport_type == TRANSPORT_ROAD) StartSpriteCombine();
 
 		/* HACK set the height of the BB of a sloped ramp to 1 so a vehicle on
 		 * it doesn't disappear behind it
@@ -951,11 +919,11 @@
 			psid->sprite, psid->pal, ti->x, ti->y, 16, 16, ti->tileh == SLOPE_FLAT ? 0 : 8, ti->z, IsTransparencySet(TO_BRIDGES)
 		);
 
-		if (GetTunnelBridgeTransportType(ti->tile) == TRANSPORT_ROAD) {
+		if (transport_type == TRANSPORT_ROAD) {
 			RoadTypes rts = GetRoadTypes(ti->tile);
 
 			if (HasBit(rts, ROADTYPE_TRAM)) {
-				uint offset = GetTunnelBridgeDirection(ti->tile);
+				uint offset = tunnelbridge_direction;
 				uint z = ti->z;
 				if (ti->tileh != SLOPE_FLAT) {
 					offset = (offset + 1) & 1;
@@ -1033,9 +1001,10 @@
 	uint base_offset;
 	TileIndex rampnorth;
 	TileIndex rampsouth;
+	TransportType transport_type;
 	Axis axis;
 	uint piece;
-	uint type;
+	BridgeType type;
 	int x;
 	int y;
 	uint z;
@@ -1044,6 +1013,7 @@
 
 	rampnorth = GetNorthernBridgeEnd(ti->tile);
 	rampsouth = GetSouthernBridgeEnd(ti->tile);
+	transport_type = GetTunnelBridgeTransportType(rampsouth);
 
 	axis = GetBridgeAxis(ti->tile);
 	piece = CalcBridgePiece(
@@ -1052,7 +1022,7 @@
 	);
 	type = GetBridgeType(rampsouth);
 
-	if (GetTunnelBridgeTransportType(rampsouth) == TRANSPORT_RAIL) {
+	if (transport_type == TRANSPORT_RAIL) {
 		base_offset = GetRailTypeInfo(GetRailType(rampsouth))->bridge_offset;
 	} else {
 		base_offset = 8;
@@ -1070,7 +1040,7 @@
 	AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, x, y, 16, 16, 1, bridge_z - TILE_HEIGHT + BB_Z_SEPARATOR);
 
 	/* Draw Trambits as SpriteCombine */
-	if (GetTunnelBridgeTransportType(rampsouth) == TRANSPORT_ROAD) StartSpriteCombine();
+	if (transport_type == TRANSPORT_ROAD) StartSpriteCombine();
 
 	/* Draw floor and far part of bridge*/
 	if (axis == AXIS_X) {
@@ -1081,7 +1051,7 @@
 
 	psid++;
 
-	if (GetTunnelBridgeTransportType(rampsouth) == TRANSPORT_ROAD) {
+	if (transport_type == TRANSPORT_ROAD) {
 		RoadTypes rts = GetRoadTypes(rampsouth);
 
 		if (HasBit(rts, ROADTYPE_TRAM)) {
@@ -1105,7 +1075,7 @@
 	}
 
 	/* Draw TramFront as SpriteCombine */
-	if (GetTunnelBridgeTransportType(rampsouth) == TRANSPORT_ROAD) EndSpriteCombine();
+	if (transport_type == TRANSPORT_ROAD) EndSpriteCombine();
 
 	psid++;
 	if (ti->z + 5 == z) {
@@ -1183,8 +1153,7 @@
 		td->str = (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) ?
 			STR_5017_RAILROAD_TUNNEL : STR_5018_ROAD_TUNNEL;
 	} else { //so it must be a bridge
-		int brtype = GetBridgeType(tile);
-		td->str = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL ? _bridge[brtype].name_rail : _bridge[brtype].name_road;
+		td->str = GetBridgeSpec(GetBridgeType(tile))->transport_name[GetTunnelBridgeTransportType(tile)];
 	}
 	td->owner = GetTileOwner(tile);
 }
@@ -1224,11 +1193,14 @@
 }
 
 
-static uint32 GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode, uint sub_mode)
+static TrackStatus GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
 {
-	if (GetTunnelBridgeTransportType(tile) != mode) return 0;
-	if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD && (GetRoadTypes(tile) & sub_mode) == 0) return 0;
-	return AxisToTrackBits(DiagDirToAxis(GetTunnelBridgeDirection(tile))) * 0x101;
+	TransportType transport_type = GetTunnelBridgeTransportType(tile);
+	if (transport_type != mode || (transport_type == TRANSPORT_ROAD && (GetRoadTypes(tile) & sub_mode) == 0)) return 0;
+
+	DiagDirection dir = GetTunnelBridgeDirection(tile);
+	if (side != INVALID_DIAGDIR && side != ReverseDiagDir(dir)) return 0;
+	return CombineTrackStatus(TrackBitsToTrackdirBits(AxisToTrackBits(DiagDirToAxis(dir))), TRACKDIR_BIT_NONE);
 }
 
 static void ChangeTileOwner_TunnelBridge(TileIndex tile, PlayerID old_player, PlayerID new_player)
@@ -1238,7 +1210,7 @@
 	if (new_player != PLAYER_SPECTATOR) {
 		SetTileOwner(tile, new_player);
 	} else {
-		if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR))) {
+		if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR))) {
 			/* When clearing the bridge/tunnel failed there are still vehicles on/in
 			 * the bridge/tunnel. As all *our* vehicles are already removed, they
 			 * must be of another owner. Therefor this must be a road bridge/tunnel.
@@ -1271,16 +1243,15 @@
 	int z = GetSlopeZ(x, y) - v->z_pos;
 
 	if (abs(z) > 2) return VETSB_CANNOT_ENTER;
+	const DiagDirection dir = GetTunnelBridgeDirection(tile);
 
 	if (IsTunnel(tile)) {
 		byte fc;
-		DiagDirection dir;
 		DiagDirection vdir;
 
 		if (v->type == VEH_TRAIN) {
 			fc = (x & 0xF) + (y << 4);
 
-			dir = GetTunnelBridgeDirection(tile);
 			vdir = DirToDiagDir(v->direction);
 
 			if (v->u.rail.track != TRACK_BIT_WORMHOLE && dir == vdir) {
@@ -1308,7 +1279,6 @@
 			}
 		} else if (v->type == VEH_ROAD) {
 			fc = (x & 0xF) + (y << 4);
-			dir = GetTunnelBridgeDirection(tile);
 			vdir = DirToDiagDir(v->direction);
 
 			/* Enter tunnel? */
@@ -1338,17 +1308,15 @@
 			}
 		}
 	} else { // IsBridge(tile)
-		DiagDirection dir;
 
 		if (v->IsPrimaryVehicle()) {
 			/* modify speed of vehicle */
-			uint16 spd = _bridge[GetBridgeType(tile)].speed;
+			uint16 spd = GetBridgeSpec(GetBridgeType(tile))->speed;
 
 			if (v->type == VEH_ROAD) spd *= 2;
 			if (v->cur_speed > spd) v->cur_speed = spd;
 		}
 
-		dir = GetTunnelBridgeDirection(tile);
 		if (DirToDiagDir(v->direction) == dir) {
 			switch (dir) {
 				default: NOT_REACHED();