From 2b07389fe66471c48d7cd6bdcd96cbe98327e90c Mon Sep 17 00:00:00 2001
From: alberth <alberth@openttd.org>
Date: Sun, 21 Feb 2010 20:34:57 +0000
Subject: [PATCH] (svn r19189) -Codechange: CheckTrackCombination() returns a
 CommandCost.

---
 src/rail_cmd.cpp | 33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp
index 5679eca246..69e13620ca 100644
--- a/src/rail_cmd.cpp
+++ b/src/rail_cmd.cpp
@@ -172,11 +172,15 @@ static bool EnsureNoTrainOnTrack(TileIndex tile, Track track)
 	return !HasVehicleOnPos(tile, &rail_bits, &EnsureNoTrainOnTrackProc);
 }
 
-static bool CheckTrackCombination(TileIndex tile, TrackBits to_build, uint flags)
+/** 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)
 {
-	_error_message = STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION;
-
-	if (!IsPlainRail(tile)) return false;
+	if (!IsPlainRail(tile)) return_cmd_error(STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION);
 
 	/* So, we have a tile with tracks on it (and possibly signals). Let's see
 	 * what tracks first */
@@ -186,19 +190,17 @@ static bool CheckTrackCombination(TileIndex tile, TrackBits to_build, uint flags
 	/* Are we really building something new? */
 	if (current == future) {
 		/* Nothing new is being built */
-		_error_message = STR_ERROR_ALREADY_BUILT;
-		return false;
+		return_cmd_error(STR_ERROR_ALREADY_BUILT);
 	}
 
 	/* Let's see if we may build this */
 	if ((flags & DC_NO_RAIL_OVERLAP) || HasSignals(tile)) {
 		/* If we are not allowed to overlap (flag is on for ai companies or we have
 		 * signals on the tile), check that */
-		return future == TRACK_BIT_HORZ || future == TRACK_BIT_VERT;
-	} else {
-		/* Normally, we may overlap and any combination is valid */
-		return true;
+		if (future != TRACK_BIT_HORZ && future != TRACK_BIT_VERT) return_cmd_error(STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION);
 	}
+	/* Normally, we may overlap and any combination is valid */
+	return CommandCost();
 }
 
 
@@ -382,12 +384,13 @@ CommandCost CmdBuildSingleRail(TileIndex tile, DoCommandFlag flags, uint32 p1, u
 
 			if (!IsCompatibleRail(GetRailType(tile), railtype)) return_cmd_error(STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION);
 
-			if (!CheckTrackCombination(tile, trackbit, flags) ||
-					!EnsureNoTrainOnTrack(tile, track)) {
-				return CMD_ERROR;
-			}
+			CommandCost ret = CheckTrackCombination(tile, trackbit, flags);
+			ret.SetGlobalErrorMessage();
+			if (ret.Failed()) return ret;
 
-			CommandCost ret = CheckRailSlope(tileh, trackbit, GetTrackBits(tile), tile);
+			if (!EnsureNoTrainOnTrack(tile, track)) return CMD_ERROR;
+
+			ret = CheckRailSlope(tileh, trackbit, GetTrackBits(tile), tile);
 			if (ret.Failed()) return ret;
 			cost.AddCost(ret);