Changeset - r6368:b0a38d4167ad
[Not reviewed]
master
0 4 0
celestar - 17 years ago 2007-03-24 09:12:03
celestar@openttd.org
(svn r9425) -Codechange/Fix (FS#689): Housekeeping in the convert rail functions: Changed the order of error checks to generate more meaningful error messages, added some doxygen comments and replaced bitshifting by proper mathematical operations
4 files changed with 42 insertions and 7 deletions:
0 comments (0 inline, 0 general)
src/rail_cmd.cpp
Show inline comments
 
@@ -846,20 +846,29 @@ int32 CmdRemoveSignalTrack(TileIndex til
 
{
 
	return CmdSignalTrackHelper(tile, flags, p1, SETBIT(p2, 5)); // bit 5 is remove bit
 
}
 

	
 
typedef int32 DoConvertRailProc(TileIndex tile, RailType totype, bool exec);
 

	
 
/**
 
 * Switches the rail type.
 
 * Railtypes are stored on a per-tile basis, not on a per-track basis, so
 
 * all the tracks in the given tile will be converted.
 
 * @param tile        The tile on which the railtype is to be convert.
 
 * @param totype      The railtype we want to convert to
 
 * @param exec        Switches between test and execute mode
 
 * @return            The cost and state of the operation
 
 * @retval CMD_ERROR  An error occured during the operation.
 
 */
 
static int32 DoConvertRail(TileIndex tile, RailType totype, bool exec)
 
{
 
	if (!CheckTileOwnership(tile)) return CMD_ERROR;
 

	
 
	if (!EnsureNoVehicle(tile) && (!IsCompatibleRail(GetRailType(tile), totype) || IsPlainRailTile(tile))) return CMD_ERROR;
 
	if (GetRailType(tile) == totype) return CMD_ERROR;
 

	
 
	// tile is already of requested type?
 
	if (GetRailType(tile) == totype) return CMD_ERROR;
 
	if (!EnsureNoVehicle(tile) && (!IsCompatibleRail(GetRailType(tile), totype) || IsPlainRailTile(tile))) 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;
 

	
 
	// change type.
 
	if (exec) {
src/road_cmd.cpp
Show inline comments
 
@@ -364,12 +364,20 @@ do_clear:;
 

	
 
		MarkTileDirtyByTile(tile);
 
	}
 
	return cost;
 
}
 

	
 
/**
 
 * Switches the rail type on a level crossing.
 
 * @param tile        The tile on which the railtype is to be convert.
 
 * @param totype      The railtype we want to convert to
 
 * @param exec        Switches between test and execute mode
 
 * @return            The cost and state of the operation
 
 * @retval CMD_ERROR  An error occured during the operation.
 
 */
 
int32 DoConvertStreetRail(TileIndex tile, RailType totype, bool exec)
 
{
 
	// not a railroad crossing?
 
	if (!IsLevelCrossing(tile)) return CMD_ERROR;
 

	
 
	// not owned by me?
 
@@ -383,13 +391,13 @@ int32 DoConvertStreetRail(TileIndex tile
 
	if (exec) {
 
		SetRailType(tile, totype);
 
		MarkTileDirtyByTile(tile);
 
		YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetCrossingRailBits(tile)));
 
	}
 

	
 
	return _price.build_rail >> 1;
 
	return _price.build_rail / 2;
 
}
 

	
 

	
 
/** Build a long piece of road.
 
 * @param end_tile end tile of drag
 
 * @param p1 start tile of drag
src/station_cmd.cpp
Show inline comments
 
@@ -1117,12 +1117,20 @@ static int32 RemoveRailroadStation(Stati
 
		DeleteStationIfEmpty(st);
 
	}
 

	
 
	return cost;
 
}
 

	
 
/**
 
 * Switches the rail type at a railway station tile.
 
 * @param tile        The tile on which the railtype is to be convert.
 
 * @param totype      The railtype we want to convert to
 
 * @param exec        Switches between test and execute mode
 
 * @return            The cost and state of the operation
 
 * @retval CMD_ERROR  An error occured during the operation.
 
 */
 
int32 DoConvertStationRail(TileIndex tile, RailType totype, bool exec)
 
{
 
	const Station* st = GetStationByTile(tile);
 

	
 
	if (!CheckOwnership(st->owner) || !EnsureNoVehicle(tile)) return CMD_ERROR;
 

	
 
@@ -1137,13 +1145,13 @@ int32 DoConvertStationRail(TileIndex til
 
	if (exec) {
 
		SetRailType(tile, totype);
 
		MarkTileDirtyByTile(tile);
 
		YapfNotifyTrackLayoutChange(tile, GetRailStationTrack(tile));
 
	}
 

	
 
	return _price.build_rail >> 1;
 
	return _price.build_rail / 2;
 
}
 

	
 
/**
 
 * @param[in] truck_station Determines whether a stop is RoadStop::BUS or RoadStop::TRUCK
 
 * @param[in] station The station to do the whole procedure for
 
 * @return a pointer to where to link a new RoadStop*
src/tunnelbridge_cmd.cpp
Show inline comments
 
@@ -693,12 +693,22 @@ static int32 ClearTile_TunnelBridge(Tile
 
		return DoClearBridge(tile, flags);
 
	}
 

	
 
	return CMD_ERROR;
 
}
 

	
 
/**
 
 * Switches the rail type for a tunnel or a bridgehead. As the railtype
 
 * on the bridge are determined by the one of the bridgehead, this
 
 * functions converts the railtype on the entire bridge.
 
 * @param tile        The tile on which the railtype is to be convert.
 
 * @param totype      The railtype we want to convert to
 
 * @param exec        Switches between test and execute mode
 
 * @return            The cost and state of the operation
 
 * @retval CMD_ERROR  An error occured during the operation.
 
 */
 
int32 DoConvertTunnelBridgeRail(TileIndex tile, RailType totype, bool exec)
 
{
 
	TileIndex endtile;
 

	
 
	if (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_RAIL) {
 
		uint length;
 
@@ -721,13 +731,13 @@ int32 DoConvertTunnelBridgeRail(TileInde
 
			MarkTileDirtyByTile(endtile);
 

	
 
			track = AxisToTrack(DiagDirToAxis(GetTunnelDirection(tile)));
 
			YapfNotifyTrackLayoutChange(tile, track);
 
			YapfNotifyTrackLayoutChange(endtile, track);
 
		}
 
		return (length + 1) * (_price.build_rail >> 1);
 
		return (length + 1) * (_price.build_rail / 2);
 
	} else if (IsBridge(tile) && GetBridgeTransportType(tile) == TRANSPORT_RAIL) {
 

	
 
		if (!CheckTileOwnership(tile)) return CMD_ERROR;
 

	
 
		endtile = GetOtherBridgeEnd(tile);
 

	
 
@@ -755,13 +765,13 @@ int32 DoConvertTunnelBridgeRail(TileInde
 
			delta = TileOffsByDiagDir(GetBridgeRampDirection(tile));
 
			for (tile += delta; tile != endtile; tile += delta) {
 
				MarkTileDirtyByTile(tile); // TODO encapsulate this into a function
 
			}
 
		}
 

	
 
		return (DistanceManhattan(tile, endtile) + 1) * (_price.build_rail >> 1);
 
		return (DistanceManhattan(tile, endtile) + 1) * (_price.build_rail / 2);
 
	} else {
 
		return CMD_ERROR;
 
	}
 
}
 

	
 

	
0 comments (0 inline, 0 general)