bridge_cmd.c
branchcustombridgeheads
changeset 5595 049ed4486972
parent 5594 04068f82afaa
child 5598 166345600ba9
--- a/bridge_cmd.c	Fri Dec 29 12:03:28 2006 +0000
+++ b/bridge_cmd.c	Fri Dec 29 12:18:43 2006 +0000
@@ -1,6 +1,6 @@
 /* $Id$ */
 
-/** @file tunnelbridge_cmd.c
+/** @file bridge_cmd.c
  * This file deals with tunnels and bridges (non-gui stuff)
  * @todo seperate this file into two
  */
@@ -395,123 +395,7 @@
 }
 
 
-/** Build Tunnel.
- * @param tile start tile of tunnel
- * @param p1 railtype, 0x200 for road tunnel
- * @param p2 unused
- */
-int32 CmdBuildTunnel(TileIndex start_tile, uint32 flags, uint32 p1, uint32 p2)
-{
-	TileIndexDiff delta;
-	TileIndex end_tile;
-	DiagDirection direction;
-	Slope start_tileh;
-	Slope end_tileh;
-	uint start_z;
-	uint end_z;
-	int32 cost;
-	int32 ret;
-
-	_build_tunnel_endtile = 0;
-
-	if (p1 != 0x200 && !ValParamRailtype(p1)) return CMD_ERROR;
-
-	start_tileh = GetTileSlope(start_tile, &start_z);
-
-	switch (start_tileh) {
-		case SLOPE_SW: direction = DIAGDIR_SW; break;
-		case SLOPE_SE: direction = DIAGDIR_SE; break;
-		case SLOPE_NW: direction = DIAGDIR_NW; break;
-		case SLOPE_NE: direction = DIAGDIR_NE; break;
-		default: return_cmd_error(STR_500B_SITE_UNSUITABLE_FOR_TUNNEL);
-	}
-
-	ret = DoCommand(start_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
-	if (CmdFailed(ret)) return ret;
-
-	/* XXX - do NOT change 'ret' in the loop, as it is used as the price
-	 * for the clearing of the entrance of the tunnel. Assigning it to
-	 * cost before the loop will yield different costs depending on start-
-	 * position, because of increased-cost-by-length: 'cost += cost >> 3' */
-	cost = 0;
-	delta = TileOffsByDiagDir(direction);
-	end_tile = start_tile;
-	for (;;) {
-		end_tile += delta;
-		end_tileh = GetTileSlope(end_tile, &end_z);
-
-		if (start_z == end_z) break;
-
-		if (!_cheats.crossing_tunnels.value && IsTunnelInWay(end_tile, start_z)) {
-			return_cmd_error(STR_5003_ANOTHER_TUNNEL_IN_THE_WAY);
-		}
-
-		cost += _price.build_tunnel;
-		cost += cost >> 3; // add a multiplier for longer tunnels
-		if (cost >= 400000000) cost = 400000000;
-	}
-
-	/* Add the cost of the entrance */
-	cost += _price.build_tunnel + ret;
-
-	// if the command fails from here on we want the end tile to be highlighted
-	_build_tunnel_endtile = end_tile;
-
-	// slope of end tile must be complementary to the slope of the start tile
-	if (end_tileh != ComplementSlope(start_tileh)) {
-		ret = DoCommand(end_tile, end_tileh & start_tileh, 0, flags, CMD_TERRAFORM_LAND);
-		if (CmdFailed(ret)) return_cmd_error(STR_5005_UNABLE_TO_EXCAVATE_LAND);
-	} else {
-		ret = DoCommand(end_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
-		if (CmdFailed(ret)) return ret;
-	}
-	cost += _price.build_tunnel + ret;
-
-	if (flags & DC_EXEC) {
-		if (GB(p1, 9, 1) == TRANSPORT_RAIL) {
-			MakeRailTunnel(start_tile, _current_player, direction,                 GB(p1, 0, 4));
-			MakeRailTunnel(end_tile,   _current_player, ReverseDiagDir(direction), GB(p1, 0, 4));
-			UpdateSignalsOnSegment(start_tile, direction);
-			YapfNotifyTrackLayoutChange(start_tile, AxisToTrack(DiagDirToAxis(direction)));
-		} else {
-			MakeRoadTunnel(start_tile, _current_player, direction);
-			MakeRoadTunnel(end_tile,   _current_player, ReverseDiagDir(direction));
-		}
-	}
-
-	return cost;
-}
-
-TileIndex CheckTunnelBusy(TileIndex tile, uint *length)
-{
-	uint z = GetTileZ(tile);
-	DiagDirection dir = GetTunnelDirection(tile);
-	TileIndexDiff delta = TileOffsByDiagDir(dir);
-	uint len = 0;
-	TileIndex starttile = tile;
-	Vehicle *v;
-
-	do {
-		tile += delta;
-		len++;
-	} while (
-		!IsTunnelTile(tile) ||
-		ReverseDiagDir(GetTunnelDirection(tile)) != dir ||
-		GetTileZ(tile) != z
-	);
-
-	v = FindVehicleBetween(starttile, tile, z);
-	if (v != NULL) {
-		_error_message = v->type == VEH_Train ?
-			STR_5000_TRAIN_IN_TUNNEL : STR_5001_ROAD_VEHICLE_IN_TUNNEL;
-		return INVALID_TILE;
-	}
-
-	if (length != NULL) *length = len;
-	return tile;
-}
-
-static inline bool CheckAllowRemoveTunnelBridge(TileIndex tile)
+static inline bool CheckAllowRemoveBridge(TileIndex tile)
 {
 	/* Floods can remove anything as well as the scenario editor */
 	if (_current_player == OWNER_WATER || _game_mode == GM_EDITOR) return true;
@@ -522,54 +406,6 @@
 	return false;
 }
 
-static int32 DoClearTunnel(TileIndex tile, uint32 flags)
-{
-	Town *t = NULL;
-	TileIndex endtile;
-	uint length;
-
-	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
-
-	if (!CheckAllowRemoveTunnelBridge(tile)) return CMD_ERROR;
-
-	endtile = CheckTunnelBusy(tile, &length);
-	if (endtile == INVALID_TILE) return CMD_ERROR;
-
-	_build_tunnel_endtile = endtile;
-
-	if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
-		t = ClosestTownFromTile(tile, (uint)-1); // town penalty rating
-
-		/* Check if you are allowed to remove the tunnel owned by a town
-		 * Removal depends on difficulty settings */
-		if (!CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE)) {
-			SetDParam(0, t->index);
-			return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
-		}
-	}
-
-	if (flags & DC_EXEC) {
-		// We first need to request the direction before calling DoClearSquare
-		//  else the direction is always 0.. dah!! ;)
-		DiagDirection dir = GetTunnelDirection(tile);
-		Track track;
-
-		// Adjust the town's player rating. Do this before removing the tile owner info.
-		if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR)
-			ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM);
-
-		DoClearSquare(tile);
-		DoClearSquare(endtile);
-		UpdateSignalsOnSegment(tile, ReverseDiagDir(dir));
-		UpdateSignalsOnSegment(endtile, dir);
-		track = AxisToTrack(DiagDirToAxis(dir));
-		YapfNotifyTrackLayoutChange(tile, track);
-		YapfNotifyTrackLayoutChange(endtile, track);
-	}
-	return _price.clear_tunnel * (length + 1);
-}
-
-
 static bool IsVehicleOnBridge(TileIndex starttile, TileIndex endtile, uint z)
 {
 	const Vehicle *v;
@@ -591,7 +427,7 @@
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
-	if (!CheckAllowRemoveTunnelBridge(tile)) return CMD_ERROR;
+	if (!CheckAllowRemoveBridge(tile)) return CMD_ERROR;
 
 	endtile = GetOtherBridgeEnd(tile);
 
@@ -641,12 +477,6 @@
 	return (DistanceManhattan(tile, endtile) + 1) * _price.clear_bridge;
 }
 
-static int32 ClearTile_Tunnel(TileIndex tile, byte flags)
-{
-	assert(IsTunnelTile(tile));
-	if (flags & DC_AUTO) return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST);
-	return DoClearTunnel(tile, flags);
-}
 
 static int32 ClearTile_Bridge(TileIndex tile, byte flags)
 {
@@ -655,37 +485,11 @@
 	return DoClearBridge(tile, flags);
 }
 
-int32 DoConvertTunnelBridgeRail(TileIndex tile, RailType totype, bool exec)
+int32 DoConvertBridgeRail(TileIndex tile, RailType totype, bool exec)
 {
 	TileIndex endtile;
 
-	if (IsTunnelTile(tile) && GetTunnelTransportType(tile) == TRANSPORT_RAIL) {
-		uint length;
-
-		if (!CheckTileOwnership(tile)) return CMD_ERROR;
-
-		if (GetRailType(tile) == totype) return CMD_ERROR;
-
-		// 'hidden' elrails can't be downgraded to normal rail when elrails are disabled
-		if (_patches.disable_elrails && totype == RAILTYPE_RAIL && GetRailType(tile) == RAILTYPE_ELECTRIC) return CMD_ERROR;
-
-		endtile = CheckTunnelBusy(tile, &length);
-		if (endtile == INVALID_TILE) return CMD_ERROR;
-
-		if (exec) {
-			Track track;
-			SetRailType(tile, totype);
-			SetRailType(endtile, totype);
-			MarkTileDirtyByTile(tile);
-			MarkTileDirtyByTile(endtile);
-
-			track = AxisToTrack(DiagDirToAxis(GetTunnelDirection(tile)));
-			YapfNotifyTrackLayoutChange(tile, track);
-			YapfNotifyTrackLayoutChange(endtile, track);
-		}
-		return (length + 1) * (_price.build_rail >> 1);
-	} else if (IsBridgeTile(tile) && GetBridgeTransportType(tile) == TRANSPORT_RAIL) {
-
+	if (GetBridgeTransportType(tile) == TRANSPORT_RAIL) {
 		if (!CheckTileOwnership(tile)) return CMD_ERROR;
 
 		endtile = GetOtherBridgeEnd(tile);
@@ -787,29 +591,6 @@
 	return i + 15;
 }
 
-/**
- * Draws a tunnel tile.
- * Please note that in this code, "roads" are treated as railtype 1, whilst the real railtypes are 0, 2 and 3
- */
-static void DrawTile_Tunnel(TileInfo *ti)
-{
-	uint32 image;
-
-	if (GetTunnelTransportType(ti->tile) == TRANSPORT_RAIL) {
-		image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.tunnel;
-	} else {
-		image = SPR_TUNNEL_ENTRY_REAR_ROAD;
-	}
-
-	if (HasTunnelSnowOrDesert(ti->tile)) image += 32;
-
-	image += GetTunnelDirection(ti->tile) * 2;
-	DrawGroundSprite(image);
-	if (GetRailType(ti->tile) == RAILTYPE_ELECTRIC) DrawCatenary(ti);
-
-	AddSortableSpriteToDraw(image+1, ti->x + TILE_SIZE - 1, ti->y + TILE_SIZE - 1, 1, 1, 8, (byte)ti->z);
-	DrawBridgeMiddle(ti);
-}
 
 /**
  * Draws a bridge tile.
@@ -994,33 +775,6 @@
 }
 
 
-/** Gets the absolute z coordinate of a point inside a tunnel tile
- *  When we're on the track (that means between position 5 and 10)
- *  on the coordinate perpendicular to the track it returns only the
- *  base height of the tile (because the track is horizontal).
- *  Outside this range (from 0 to 4 and from 11 to 15) it returns the
- *  "true" Z coordinate of the tile by taking the slope into account
- *  @param tile The index of the tile we are talking about
- *  @param x Absolute or relative x coordinate
- *  @param y Absolute or relative y coordinate
- *  @return Absolute z coordinate
- */
-static uint GetSlopeZ_Tunnel(TileIndex tile, uint x, uint y)
-{
-	uint z, pos;
-	Slope tileh = GetTileSlope(tile, &z);
-
-	x &= 0xF;
-	y &= 0xF;
-
-	pos = (DiagDirToAxis(GetTunnelDirection(tile)) == AXIS_X ? y : x);
-
-	// In the tunnel entrance?
-	if (5 <= pos && pos <= 10) return z;
-
-	return z + GetPartialZ(x, y, tileh);
-}
-
 /** Gets the absolute z coordinate of a point inside a bridge tile
  *  When we're on the track (that means between position 5 and 10)
  *  on the coordinate perpendicular to the track it returns the base
@@ -1075,11 +829,6 @@
 	return z + GetPartialZ(x, y, tileh);
 }
 
-static Slope GetSlopeTileh_Tunnel(TileIndex tile, Slope tileh)
-{
-	return tileh;
-}
-
 static Slope GetSlopeTileh_Bridge(TileIndex tile, Slope tileh)
 {
 	if (HASBIT(BRIDGE_NO_FOUNDATION, tileh)) {
@@ -1093,11 +842,6 @@
 	}
 }
 
-static void GetAcceptedCargo_Tunnel(TileIndex tile, AcceptedCargo ac)
-{
-	/* not used */
-}
-
 static void GetAcceptedCargo_Bridge(TileIndex tile, AcceptedCargo ac)
 {
 	/* not used */
@@ -1135,12 +879,6 @@
 	0, 0, 0,
 };
 
-static void GetTileDesc_Tunnel(TileIndex tile, TileDesc *td)
-{
-	td->str = (GetTunnelTransportType(tile) == TRANSPORT_RAIL) ?  STR_5017_RAILROAD_TUNNEL : STR_5018_ROAD_TUNNEL;
-	td->owner = GetTileOwner(tile);
-}
-
 static void GetTileDesc_Bridge(TileIndex tile, TileDesc *td)
 {
 	td->str = _bridge_tile_str[GetBridgeTransportType(tile) << 4 | GetBridgeType(tile)];
@@ -1148,35 +886,11 @@
 }
 
 
-static void AnimateTile_Tunnel(TileIndex tile)
-{
-	/* not used */
-}
-
 static void AnimateTile_Bridge(TileIndex tile)
 {
 	/* not used */
 }
 
-static void TileLoop_Tunnel(TileIndex tile)
-{
-	bool snow_or_desert = HasTunnelSnowOrDesert(tile);
-	switch (_opt.landscape) {
-		case LT_HILLY:
-			if (snow_or_desert != (GetTileZ(tile) > _opt.snow_line)) {
-				SetTunnelSnowOrDesert(tile, !snow_or_desert);
-				MarkTileDirtyByTile(tile);
-			}
-			break;
-
-		case LT_DESERT:
-			if (GetTropicZone(tile) == TROPICZONE_DESERT && !snow_or_desert) {
-				SetTunnelSnowOrDesert(tile, true);
-				MarkTileDirtyByTile(tile);
-			}
-			break;
-	}
-}
 
 static void TileLoop_Bridge(TileIndex tile)
 {
@@ -1198,39 +912,18 @@
 	}
 }
 
-static void ClickTile_Tunnel(TileIndex tile)
-{
-	/* not used */
-}
-
 static void ClickTile_Bridge(TileIndex tile)
 {
 	/* not used */
 }
 
 
-static uint32 GetTileTrackStatus_Tunnel(TileIndex tile, TransportType mode)
-{
-	if (GetTunnelTransportType(tile) != mode) return 0;
-	return AxisToTrackBits(DiagDirToAxis(GetTunnelDirection(tile))) * 0x101;
-}
-
 static uint32 GetTileTrackStatus_Bridge(TileIndex tile, TransportType mode)
 {
 	if (GetBridgeTransportType(tile) != mode) return 0;
 	return AxisToTrackBits(DiagDirToAxis(GetBridgeRampDirection(tile))) * 0x101;
 }
 
-static void ChangeTileOwner_Tunnel(TileIndex tile, PlayerID old_player, PlayerID new_player)
-{
-	if (!IsTileOwner(tile, old_player)) return;
-
-	if (new_player != PLAYER_SPECTATOR) {
-		SetTileOwner(tile, new_player);
-	} else {
-		DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
-	}
-}
 
 static void ChangeTileOwner_Bridge(TileIndex tile, PlayerID old_player, PlayerID new_player)
 {
@@ -1244,92 +937,6 @@
 }
 
 
-static const byte _tunnel_fractcoord_1[4]    = {0x8E, 0x18, 0x81, 0xE8};
-static const byte _tunnel_fractcoord_2[4]    = {0x81, 0x98, 0x87, 0x38};
-static const byte _tunnel_fractcoord_3[4]    = {0x82, 0x88, 0x86, 0x48};
-static const byte _exit_tunnel_track[4]      = {1, 2, 1, 2};
-
-static const byte _road_exit_tunnel_state[4] = {8, 9, 0, 1};
-static const byte _road_exit_tunnel_frame[4] = {2, 7, 9, 4};
-
-static const byte _tunnel_fractcoord_4[4]    = {0x52, 0x85, 0x98, 0x29};
-static const byte _tunnel_fractcoord_5[4]    = {0x92, 0x89, 0x58, 0x25};
-static const byte _tunnel_fractcoord_6[4]    = {0x92, 0x89, 0x56, 0x45};
-static const byte _tunnel_fractcoord_7[4]    = {0x52, 0x85, 0x96, 0x49};
-
-static uint32 VehicleEnter_Tunnel(Vehicle *v, TileIndex tile, int x, int y)
-{
-	int z = GetSlopeZ(x, y) - v->z_pos;
-
-	byte fc;
-	DiagDirection dir;
-	DiagDirection vdir;
-
-	if (myabs(z) > 2) return 8;
-
-	if (v->type == VEH_Train) {
-		fc = (x & 0xF) + (y << 4);
-
-		dir = GetTunnelDirection(tile);
-		vdir = DirToDiagDir(v->direction);
-
-		if (v->u.rail.track != 0x40 && dir == vdir) {
-			if (IsFrontEngine(v) && fc == _tunnel_fractcoord_1[dir]) {
-				if (!PlayVehicleSound(v, VSE_TUNNEL) && v->spritenum < 4) {
-					SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
-				}
-				return 0;
-			}
-			if (fc == _tunnel_fractcoord_2[dir]) {
-				v->tile = tile;
-				v->u.rail.track = 0x40;
-				v->vehstatus |= VS_HIDDEN;
-				return 4;
-			}
-		}
-
-		if (dir == ReverseDiagDir(vdir) && fc == _tunnel_fractcoord_3[dir] && z == 0) {
-			/* We're at the tunnel exit ?? */
-			v->tile = tile;
-			v->u.rail.track = _exit_tunnel_track[dir];
-			assert(v->u.rail.track);
-			v->vehstatus &= ~VS_HIDDEN;
-			return 4;
-		}
-	} else if (v->type == VEH_Road) {
-		fc = (x & 0xF) + (y << 4);
-		dir = GetTunnelDirection(tile);
-		vdir = DirToDiagDir(v->direction);
-
-		// Enter tunnel?
-		if (v->u.road.state != 0xFF && dir == vdir) {
-			if (fc == _tunnel_fractcoord_4[dir] ||
-					fc == _tunnel_fractcoord_5[dir]) {
-				v->tile = tile;
-				v->u.road.state = 0xFF;
-				v->vehstatus |= VS_HIDDEN;
-				return 4;
-			} else {
-				return 0;
-			}
-		}
-
-		if (dir == ReverseDiagDir(vdir) && (
-					/* We're at the tunnel exit ?? */
-					fc == _tunnel_fractcoord_6[dir] ||
-					fc == _tunnel_fractcoord_7[dir]
-				) &&
-				z == 0) {
-			v->tile = tile;
-			v->u.road.state = _road_exit_tunnel_state[dir];
-			v->u.road.frame = _road_exit_tunnel_frame[dir];
-			v->vehstatus &= ~VS_HIDDEN;
-			return 4;
-		}
-	}
-	return 0;
-}
-
 static uint32 VehicleEnter_Bridge(Vehicle *v, TileIndex tile, int x, int y)
 {
 	int z = GetSlopeZ(x, y) - v->z_pos;
@@ -1372,7 +979,8 @@
 			}
 		} else {
 			if (v->u.road.state == 0xFF) {
-				v->u.road.state = _road_exit_tunnel_state[dir];
+				static const byte road_exit_bridge_state[4] = {8, 9, 0, 1};
+				v->u.road.state = road_exit_bridge_state[dir];
 				v->u.road.frame = 0;
 				return 4;
 			}
@@ -1382,22 +990,6 @@
 	return 0;
 }
 
-const TileTypeProcs _tile_type_tunnel_procs = {
-	DrawTile_Tunnel,           /* draw_tile_proc */
-	GetSlopeZ_Tunnel,          /* get_slope_z_proc */
-	ClearTile_Tunnel,          /* clear_tile_proc */
-	GetAcceptedCargo_Tunnel,   /* get_accepted_cargo_proc */
-	GetTileDesc_Tunnel,        /* get_tile_desc_proc */
-	GetTileTrackStatus_Tunnel, /* get_tile_track_status_proc */
-	ClickTile_Tunnel,          /* click_tile_proc */
-	AnimateTile_Tunnel,        /* animate_tile_proc */
-	TileLoop_Tunnel,           /* tile_loop_clear */
-	ChangeTileOwner_Tunnel,    /* change_tile_owner_clear */
-	NULL,                            /* get_produced_cargo_proc */
-	VehicleEnter_Tunnel,       /* vehicle_enter_tile_proc */
-	GetSlopeTileh_Tunnel,      /* get_slope_tileh_proc */
-};
-
 const TileTypeProcs _tile_type_bridge_procs = {
 	DrawTile_Bridge,           /* draw_tile_proc */
 	GetSlopeZ_Bridge,          /* get_slope_z_proc */