Changeset - r8601:45db2d2005d0
[Not reviewed]
master
0 1 0
smatz - 16 years ago 2008-02-18 22:12:27
smatz@openttd.org
(svn r12183) -Codechange: give a better error message when building road over existing road with vehicle on it, or do not fail at all
1 file changed with 6 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/road_cmd.cpp
Show inline comments
 
@@ -417,57 +417,57 @@ CommandCost CmdBuildRoad(TileIndex tile,
 
	RoadType rt = (RoadType)GB(p1, 4, 2);
 
	if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
 

	
 
	DisallowedRoadDirections toggle_drd = (DisallowedRoadDirections)GB(p1, 6, 2);
 

	
 
	tileh = GetTileSlope(tile, NULL);
 

	
 
	switch (GetTileType(tile)) {
 
		case MP_ROAD:
 
			switch (GetRoadTileType(tile)) {
 
				case ROAD_TILE_NORMAL: {
 
					if (HasRoadWorks(tile)) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS);
 
					if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 

	
 
					all_bits = GetAllRoadBits(tile);
 
					if (!HasTileRoadType(tile, rt)) break;
 

	
 
					existing = GetRoadBits(tile, rt);
 
					RoadBits merged = existing | pieces;
 
					bool crossing = (merged != ROAD_X && merged != ROAD_Y);
 
					if (rt != ROADTYPE_TRAM && (GetDisallowedRoadDirections(tile) != DRD_NONE || toggle_drd != DRD_NONE) && crossing) {
 
						/* Junctions cannot be one-way */
 
						return_cmd_error(STR_ERR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
 
					}
 
					if ((existing & pieces) == pieces) {
 
						/* We only want to set the (dis)allowed road directions */
 
						if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM && GetRoadOwner(tile, ROADTYPE_ROAD) == _current_player) {
 
							if (crossing) return_cmd_error(STR_ERR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
 

	
 
							if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 

	
 
							/* Ignore half built tiles */
 
							if (flags & DC_EXEC && rt != ROADTYPE_TRAM && (existing == ROAD_X || existing == ROAD_Y)) {
 
								SetDisallowedRoadDirections(tile, GetDisallowedRoadDirections(tile) ^ toggle_drd);
 
								MarkTileDirtyByTile(tile);
 
							}
 
							return CommandCost();
 
						}
 
						return_cmd_error(STR_1007_ALREADY_BUILT);
 
					}
 
				} break;
 

	
 
				case ROAD_TILE_CROSSING:
 
					if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT);
 
					all_bits = GetCrossingRoadBits(tile);
 
					if (pieces & ComplementRoadBits(all_bits)) goto do_clear;
 
					if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 
					break;
 

	
 
				default:
 
				case ROAD_TILE_DEPOT:
 
					goto do_clear;
 
			}
 
			break;
 

	
 
		case MP_RAILWAY: {
 
			Axis roaddir;
 

	
 
			if (IsSteepSlope(tileh)) {
 
@@ -500,26 +500,24 @@ CommandCost CmdBuildRoad(TileIndex tile,
 
				YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile)));
 
				/* Always add road to the roadtypes (can't draw without it) */
 
				MakeRoadCrossing(tile, _current_player, _current_player, _current_player, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2);
 
				UpdateLevelCrossing(tile, false);
 
				MarkTileDirtyByTile(tile);
 
			}
 
			return CommandCost(EXPENSES_CONSTRUCTION, _price.build_road * (rt == ROADTYPE_ROAD ? 2 : 4));
 
		}
 

	
 
		case MP_STATION:
 
			if (!IsDriveThroughStopTile(tile)) return CMD_ERROR;
 
			if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT);
 
			/* Don't allow adding roadtype to the roadstop when vehicles are already driving on it */
 
			if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 
			break;
 

	
 
		case MP_TUNNELBRIDGE:
 
			if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR;
 
			if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT);
 
			/* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */
 
			if (GetVehicleTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile)) != NULL) return CMD_ERROR;
 
			break;
 

	
 
		default:
 
do_clear:;
 
			ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
@@ -551,24 +549,26 @@ do_clear:;
 
			for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) {
 
				if (rtest != rt) { // check only other road types
 
					RoadBits bits = GetRoadBits(tile, rtest);
 
					/* do not check if there are not road bits of given type */
 
					if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) {
 
						return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
 
					}
 
				}
 
			}
 
		}
 
	}
 

	
 
	if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 

	
 
	cost.AddCost(CountBits(pieces) * _price.build_road);
 
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
 
		/* Pay for *every* tile of the bridge or tunnel */
 
		cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		switch (GetTileType(tile)) {
 
			case MP_ROAD: {
 
				RoadTileType rtt = GetRoadTileType(tile);
 
				if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
 
					SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
 
@@ -625,24 +625,26 @@ do_clear:;
 
 * - p2 = (bit 3 + 4) - road type
 
 * - p2 = (bit 5) - set road direction
 
 */
 
CommandCost CmdBuildLongRoad(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	TileIndex start_tile, tile;
 
	CommandCost ret, cost(EXPENSES_CONSTRUCTION);
 
	bool had_bridge = false;
 
	bool had_tunnel = false;
 
	bool had_success = false;
 
	DisallowedRoadDirections drd = DRD_NORTHBOUND;
 

	
 
	_error_message = INVALID_STRING_ID;
 

	
 
	if (p1 >= MapSize()) return CMD_ERROR;
 

	
 
	start_tile = p1;
 
	RoadType rt = (RoadType)GB(p2, 3, 2);
 
	if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
 

	
 
	/* Only drag in X or Y direction dictated by the direction variable */
 
	if (!HasBit(p2, 2) && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
 
	if (HasBit(p2, 2)  && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
 

	
 
	/* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
 
	if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
 
@@ -662,25 +664,24 @@ CommandCost CmdBuildLongRoad(TileIndex e
 

	
 
	tile = start_tile;
 
	/* Start tile is the small number. */
 
	for (;;) {
 
		RoadBits bits = HasBit(p2, 2) ? ROAD_Y : ROAD_X;
 

	
 
		if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
 
		if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
 

	
 
		ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
 
		if (CmdFailed(ret)) {
 
			if (_error_message != STR_1007_ALREADY_BUILT) return CMD_ERROR;
 
			_error_message = INVALID_STRING_ID;
 
		} else {
 
			had_success = true;
 
			/* Only pay for the upgrade on one side of the bridges and tunnels */
 
			if (IsTileType(tile, MP_TUNNELBRIDGE)) {
 
				if (IsBridge(tile)) {
 
					if ((!had_bridge || GetTunnelBridgeDirection(tile) == DIAGDIR_SE || GetTunnelBridgeDirection(tile) == DIAGDIR_SW)) {
 
						cost.AddCost(ret);
 
					}
 
					had_bridge = true;
 
				} else { // IsTunnel(tile)
 
					if ((!had_tunnel || GetTunnelBridgeDirection(tile) == DIAGDIR_SE || GetTunnelBridgeDirection(tile) == DIAGDIR_SW)) {
 
						cost.AddCost(ret);
0 comments (0 inline, 0 general)