Changeset - r14772:59c9e3e72143
[Not reviewed]
master
0 1 0
alberth - 15 years ago 2010-03-07 18:29:53
alberth@openttd.org
(svn r19370) -Codechange: EnsureNoTrainOnTrack() returns a CommandCost.
1 file changed with 16 insertions and 14 deletions:
0 comments (0 inline, 0 general)
src/rail_cmd.cpp
Show inline comments
 
@@ -141,32 +141,30 @@ static const byte _track_sloped_sprites[
 
 * MAP5:         00abcdef => rail
 
 *               01abcdef => rail w/ signals
 
 *               10uuuuuu => unused
 
 *               11uuuudd => rail depot
 
 */
 

	
 
/**
 
 * Tests if a vehicle interacts with the specified track.
 
 * All track bits interact except parallel #TRACK_BIT_HORZ or #TRACK_BIT_VERT.
 
 *
 
 * @param tile The tile.
 
 * @param track The track.
 
 * @return \c true if no train that interacts, is found. \c false if a train is found.
 
 * @return Succeeded command (no train found), or a failed command (a train was found).
 
 */
 
static bool EnsureNoTrainOnTrack(TileIndex tile, Track track)
 
static CommandCost EnsureNoTrainOnTrack(TileIndex tile, Track track)
 
{
 
	TrackBits rail_bits = TrackToTrackBits(track);
 
	CommandCost ret = EnsureNoTrainOnTrackBits(tile, rail_bits);
 
	ret.SetGlobalErrorMessage();
 
	return ret.Succeeded();
 
	return EnsureNoTrainOnTrackBits(tile, rail_bits);
 
}
 

	
 
/** Check that the new track bits may be built.
 
 * @param tile %Tile to build on.
 
 * @param to_build New track bits.
 
 * @param flags    Flags of the operation.
 
 * @return Succeeded or failed command.
 
 */
 
static CommandCost CheckTrackCombination(TileIndex tile, TrackBits to_build, uint flags)
 
{
 
	if (!IsPlainRail(tile)) return_cmd_error(STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION);
 

	
 
@@ -366,29 +364,28 @@ CommandCost CmdBuildSingleRail(TileIndex
 
	Slope tileh = GetTileSlope(tile, NULL);
 
	TrackBits trackbit = TrackToTrackBits(track);
 

	
 
	switch (GetTileType(tile)) {
 
		case MP_RAILWAY: {
 
			if (!CheckTileOwnership(tile)) return CMD_ERROR;
 

	
 
			if (!IsPlainRail(tile)) return CMD_ERROR;
 

	
 
			if (!IsCompatibleRail(GetRailType(tile), railtype)) return_cmd_error(STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION);
 

	
 
			CommandCost ret = CheckTrackCombination(tile, trackbit, flags);
 
			if (ret.Succeeded()) ret = EnsureNoTrainOnTrack(tile, track);
 
			ret.SetGlobalErrorMessage();
 
			if (ret.Failed()) return ret;
 

	
 
			if (!EnsureNoTrainOnTrack(tile, track)) return CMD_ERROR;
 

	
 
			ret = CheckRailSlope(tileh, trackbit, GetTrackBits(tile), tile);
 
			if (ret.Failed()) return ret;
 
			cost.AddCost(ret);
 

	
 
			/* If the rail types don't match, try to convert only if engines of
 
			 * the new rail type are not powered on the present rail type and engines of
 
			 * the present rail type are powered on the new rail type. */
 
			if (GetRailType(tile) != railtype && !HasPowerOnRail(railtype, GetRailType(tile))) {
 
				if (HasPowerOnRail(GetRailType(tile), railtype)) {
 
					ret = DoCommand(tile, tile, railtype, flags, CMD_CONVERT_RAIL);
 
					if (ret.Failed()) return ret;
 
					cost.AddCost(ret);
 
@@ -533,29 +530,29 @@ CommandCost CmdRemoveSingleRail(TileInde
 
					v = GetTrainForReservation(tile, track);
 
					if (v != NULL) FreeTrainTrackReservation(v);
 
				}
 
				owner = GetTileOwner(tile);
 
				MakeRoadNormal(tile, GetCrossingRoadBits(tile), GetRoadTypes(tile), GetTownIndex(tile), GetRoadOwner(tile, ROADTYPE_ROAD), GetRoadOwner(tile, ROADTYPE_TRAM));
 
			}
 
			break;
 
		}
 

	
 
		case MP_RAILWAY: {
 
			TrackBits present;
 

	
 
			if (!IsPlainRail(tile) ||
 
					(_current_company != OWNER_WATER && !CheckTileOwnership(tile)) ||
 
					!EnsureNoTrainOnTrack(tile, track)) {
 
				return CMD_ERROR;
 
			}
 
			if (!IsPlainRail(tile) || (_current_company != OWNER_WATER && !CheckTileOwnership(tile))) return CMD_ERROR;
 

	
 
			CommandCost ret = EnsureNoTrainOnTrack(tile, track);
 
			ret.SetGlobalErrorMessage();
 
			if (ret.Failed()) return ret;
 

	
 
			present = GetTrackBits(tile);
 
			if ((present & trackbit) == 0) return CMD_ERROR;
 
			if (present == (TRACK_BIT_X | TRACK_BIT_Y)) crossing = true;
 

	
 
			/* Charge extra to remove signals on the track, if they are there */
 
			if (HasSignalOnTrack(tile, track))
 
				cost.AddCost(DoCommand(tile, track, 0, flags, CMD_REMOVE_SIGNALS));
 

	
 
			if (flags & DC_EXEC) {
 
				if (HasReservedTracks(tile, trackbit)) {
 
					v = GetTrainForReservation(tile, track);
 
@@ -890,27 +887,30 @@ CommandCost CmdBuildSingleSignal(TileInd
 
	bool ctrl_pressed = HasBit(p1, 3); // was the CTRL button pressed
 
	SignalVariant sigvar = (ctrl_pressed ^ HasBit(p1, 4)) ? SIG_SEMAPHORE : SIG_ELECTRIC; // the signal variant of the new signal
 
	SignalType sigtype = (SignalType)GB(p1, 5, 3); // the signal type of the new signal
 
	bool convert_signal = HasBit(p1, 8); // convert button pressed
 
	SignalType cycle_start = (SignalType)GB(p1, 9, 3);
 
	SignalType cycle_stop = (SignalType)GB(p1, 12, 3);
 
	uint num_dir_cycle = GB(p1, 15, 2);
 

	
 
	if (sigtype > SIGTYPE_LAST) return CMD_ERROR;
 

	
 
	/* You can only build signals on plain rail tiles, and the selected track must exist */
 
	if (!ValParamTrackOrientation(track) || !IsPlainRailTile(tile) ||
 
			!HasTrack(tile, track) || !EnsureNoTrainOnTrack(tile, track)) {
 
			!HasTrack(tile, track)) {
 
		return CMD_ERROR;
 
	}
 
	CommandCost ret = EnsureNoTrainOnTrack(tile, track);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	/* Protect against invalid signal copying */
 
	if (p2 != 0 && (p2 & SignalOnTrack(track)) == 0) return CMD_ERROR;
 

	
 
	if (!CheckTileOwnership(tile)) return CMD_ERROR;
 

	
 
	{
 
		/* See if this is a valid track combination for signals, (ie, no overlap) */
 
		TrackBits trackbits = GetTrackBits(tile);
 
		if (KillFirstBit(trackbits) != TRACK_BIT_NONE && // More than one track present
 
				trackbits != TRACK_BIT_HORZ &&
 
				trackbits != TRACK_BIT_VERT) {
 
@@ -1248,28 +1248,30 @@ CommandCost CmdBuildSignalTrack(TileInde
 
 *           - (bit  4)    - 0 = signals, 1 = semaphores
 
 * @param p2 unused
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdRemoveSingleSignal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Track track = (Track)GB(p1, 0, 3);
 

	
 
	if (!ValParamTrackOrientation(track) ||
 
			!IsPlainRailTile(tile) ||
 
			!HasTrack(tile, track) ||
 
			!EnsureNoTrainOnTrack(tile, track) ||
 
			!HasSignalOnTrack(tile, track)) {
 
		return CMD_ERROR;
 
	}
 
	CommandCost ret = EnsureNoTrainOnTrack(tile, track);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	/* Only water can remove signals from anyone */
 
	if (_current_company != OWNER_WATER && !CheckTileOwnership(tile)) return CMD_ERROR;
 

	
 
	/* Do it? */
 
	if (flags & DC_EXEC) {
 
		Train *v = NULL;
 
		if (HasReservedTracks(tile, TrackToTrackBits(track))) {
 
			v = GetTrainForReservation(tile, track);
 
		} else if (IsPbsSignal(GetSignalType(tile, track))) {
 
			/* PBS signal, might be the end of a path reservation. */
 
			Trackdir td = TrackToTrackdir(track);
0 comments (0 inline, 0 general)