Changeset - r14814:3d4abc0944bc
[Not reviewed]
master
0 6 0
alberth - 15 years ago 2010-03-14 12:58:51
alberth@openttd.org
(svn r19415) -Codechange: Forward CommandCost with an error back to the caller.
6 files changed with 27 insertions and 19 deletions:
0 comments (0 inline, 0 general)
src/command.cpp
Show inline comments
 
@@ -413,13 +413,13 @@ CommandCost DoCommand(TileIndex tile, ui
 
			goto error;
 
		}
 

	
 
		if (_docommand_recursive == 1 &&
 
				!(flags & DC_QUERY_COST) &&
 
				!(flags & DC_BANKRUPT) &&
 
				!CheckCompanyHasMoney(res)) {
 
				!CheckCompanyHasMoney(res)) { // CheckCompanyHasMoney() modifies 'res' to an error if it fails.
 
			goto error;
 
		}
 

	
 
		if (!(flags & DC_EXEC)) {
 
			_docommand_recursive--;
 
			return res;
 
@@ -430,13 +430,13 @@ CommandCost DoCommand(TileIndex tile, ui
 
	 * themselves to the cost object at some point */
 
	res = proc(tile, flags, p1, p2, text);
 
	if (res.Failed()) {
 
error:
 
		res.SetGlobalErrorMessage();
 
		_docommand_recursive--;
 
		return CMD_ERROR;
 
		return res;
 
	}
 

	
 
	/* if toplevel, subtract the money. */
 
	if (--_docommand_recursive == 0 && !(flags & DC_BANKRUPT)) {
 
		SubtractMoneyFromCompany(res);
 
	}
src/rail_cmd.cpp
Show inline comments
 
@@ -738,28 +738,30 @@ static CommandCost ValidateAutoDrag(Trac
 
 * - p2 = (bit 8)   - 0 = build up to an obstacle, 1 = fail if an obstacle is found (used for AIs).
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
static CommandCost CmdRailTrackHelper(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	CommandCost ret, total_cost(EXPENSES_CONSTRUCTION);
 
	CommandCost total_cost(EXPENSES_CONSTRUCTION);
 
	Track track = (Track)GB(p2, 4, 3);
 
	bool remove = HasBit(p2, 7);
 
	RailType railtype = (RailType)GB(p2, 0, 4);
 

	
 
	if (!ValParamRailtype(railtype) || !ValParamTrackOrientation(track)) return CMD_ERROR;
 
	if (p1 >= MapSize()) return CMD_ERROR;
 
	TileIndex end_tile = p1;
 
	Trackdir trackdir = TrackToTrackdir(track);
 

	
 
	if (ValidateAutoDrag(&trackdir, tile, end_tile).Failed()) return CMD_ERROR;
 
	CommandCost ret = ValidateAutoDrag(&trackdir, tile, end_tile);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	if (flags & DC_EXEC) SndPlayTileFx(SND_20_SPLAT_2, tile);
 

	
 
	for (;;) {
 
		ret = DoCommand(tile, railtype, TrackdirToTrack(trackdir), flags, remove ? CMD_REMOVE_SINGLE_RAIL : CMD_BUILD_SINGLE_RAIL);
 
		CommandCost ret = DoCommand(tile, railtype, TrackdirToTrack(trackdir), flags, remove ? CMD_REMOVE_SINGLE_RAIL : CMD_BUILD_SINGLE_RAIL);
 

	
 
		if (ret.Failed()) {
 
			if (_error_message != STR_ERROR_ALREADY_BUILT && !remove) {
 
				if (HasBit(p2, 8)) return CMD_ERROR;
 
				break;
 
			}
 
@@ -848,13 +850,13 @@ CommandCost CmdBuildTrainDepot(TileIndex
 
				!CanBuildDepotByTileh(dir, tileh)
 
			)) {
 
		return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
 
	}
 

	
 
	CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (cost.Failed()) return CMD_ERROR;
 
	if (cost.Failed()) return cost;
 

	
 
	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
	if (!Depot::CanAllocateItem()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
@@ -1119,13 +1121,13 @@ static bool CheckSignalAutoFill(TileInde
 
 * - p2 = (bit 24-31) - user defined signals_density
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
static CommandCost CmdSignalTrackHelper(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	CommandCost ret, total_cost(EXPENSES_CONSTRUCTION);
 
	CommandCost total_cost(EXPENSES_CONSTRUCTION);
 
	bool err = true;
 
	TileIndex start_tile = tile;
 

	
 
	Track track = (Track)GB(p2, 0, 3);
 
	bool mode = HasBit(p2, 3);
 
	bool semaphores = HasBit(p2, 4);
 
@@ -1141,13 +1143,15 @@ static CommandCost CmdSignalTrackHelper(
 
	if (!IsPlainRailTile(tile)) return CMD_ERROR;
 

	
 
	/* for vertical/horizontal tracks, double the given signals density
 
	 * since the original amount will be too dense (shorter tracks) */
 
	signal_density *= 2;
 

	
 
	if (ValidateAutoDrag(&trackdir, tile, end_tile).Failed()) return CMD_ERROR;
 
	CommandCost ret = ValidateAutoDrag(&trackdir, tile, end_tile);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	track = TrackdirToTrack(trackdir); // trackdir might have changed, keep track in sync
 
	Trackdir start_trackdir = trackdir;
 

	
 
	/* Must start on a valid track to be able to avoid loops */
 
	if (!HasTrack(tile, track)) return CMD_ERROR;
 
@@ -1195,13 +1199,13 @@ static CommandCost CmdSignalTrackHelper(
 

	
 
			/* Pick the correct orientation for the track direction */
 
			signals = 0;
 
			if (HasBit(signal_dir, 0)) signals |= SignalAlongTrackdir(trackdir);
 
			if (HasBit(signal_dir, 1)) signals |= SignalAgainstTrackdir(trackdir);
 

	
 
			ret = DoCommand(tile, p1, signals, flags, remove ? CMD_REMOVE_SIGNALS : CMD_BUILD_SIGNALS);
 
			CommandCost ret = DoCommand(tile, p1, signals, flags, remove ? CMD_REMOVE_SIGNALS : CMD_BUILD_SIGNALS);
 

	
 
			/* Be user-friendly and try placing signals as much as possible */
 
			if (ret.Succeeded()) {
 
				err = false;
 
				total_cost.AddCost(ret);
 
			}
 
@@ -1603,13 +1607,13 @@ static CommandCost ClearTile_Track(TileI
 
			bool water_ground = (GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(tileh));
 

	
 
			TrackBits tracks = GetTrackBits(tile);
 
			while (tracks != TRACK_BIT_NONE) {
 
				Track track = RemoveFirstTrack(&tracks);
 
				CommandCost ret = DoCommand(tile, 0, track, flags, CMD_REMOVE_SINGLE_RAIL);
 
				if (ret.Failed()) return CMD_ERROR;
 
				if (ret.Failed()) return ret;
 
				cost.AddCost(ret);
 
			}
 

	
 
			/* when bankrupting, don't make water dirty, there could be a ship on lower halftile */
 
			if (water_ground && !(flags & DC_BANKRUPT)) {
 
				CommandCost ret = EnsureNoVehicleOnGround(tile);
src/road_cmd.cpp
Show inline comments
 
@@ -901,13 +901,13 @@ CommandCost CmdBuildRoadDepot(TileIndex 
 
				!CanBuildDepotByTileh(dir, tileh)
 
			)) {
 
		return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
 
	}
 

	
 
	CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (cost.Failed()) return CMD_ERROR;
 
	if (cost.Failed()) return cost;
 

	
 
	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
	if (!Depot::CanAllocateItem()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
src/station_cmd.cpp
Show inline comments
 
@@ -2380,26 +2380,30 @@ CommandCost CmdBuildDock(TileIndex tile,
 
	CommandCost ret = CheckIfAuthorityAllowsNewStation(tile, flags);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
	if (DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR).Failed()) return CMD_ERROR;
 
	ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	TileIndex tile_cur = tile + TileOffsByDiagDir(direction);
 

	
 
	if (!IsTileType(tile_cur, MP_WATER) || GetTileSlope(tile_cur, NULL) != SLOPE_FLAT) {
 
		return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
 
	}
 

	
 
	if (MayHaveBridgeAbove(tile_cur) && IsBridgeAbove(tile_cur)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
	/* Get the water class of the water tile before it is cleared.*/
 
	WaterClass wc = GetWaterClass(tile_cur);
 

	
 
	if (DoCommand(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR).Failed()) return CMD_ERROR;
 
	ret = DoCommand(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	tile_cur += TileOffsByDiagDir(direction);
 
	if (!IsTileType(tile_cur, MP_WATER) || GetTileSlope(tile_cur, NULL) != SLOPE_FLAT) {
 
		return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
 
	}
 

	
src/unmovable_cmd.cpp
Show inline comments
 
@@ -149,13 +149,13 @@ CommandCost CmdPurchaseLandArea(TileInde
 

	
 
	if (IsOwnedLandTile(tile) && IsTileOwner(tile, _current_company)) {
 
		return_cmd_error(STR_ERROR_YOU_ALREADY_OWN_IT);
 
	}
 

	
 
	cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (cost.Failed()) return CMD_ERROR;
 
	if (cost.Failed()) return cost;
 

	
 
	if (flags & DC_EXEC) {
 
		MakeOwnedLand(tile, _current_company);
 
		MarkTileDirtyByTile(tile);
 
	}
 

	
src/water_cmd.cpp
Show inline comments
 
@@ -123,15 +123,15 @@ CommandCost CmdBuildShipDepot(TileIndex 
 
		return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
 
	}
 

	
 
	WaterClass wc1 = GetWaterClass(tile);
 
	WaterClass wc2 = GetWaterClass(tile2);
 
	ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (ret.Failed()) return CMD_ERROR;
 
	if (ret.Failed()) return ret;
 
	ret = DoCommand(tile2, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (ret.Failed()) return CMD_ERROR;
 
	if (ret.Failed()) return ret;
 

	
 
	if (!Depot::CanAllocateItem()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Depot *depot = new Depot(tile);
 
		depot->town_index = ClosestTownFromTile(tile, UINT_MAX)->index;
 
@@ -201,29 +201,29 @@ static CommandCost DoBuildShiplift(TileI
 
{
 
	CommandCost ret;
 
	int delta;
 

	
 
	/* middle tile */
 
	ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (ret.Failed()) return CMD_ERROR;
 
	if (ret.Failed()) return ret;
 

	
 
	delta = TileOffsByDiagDir(dir);
 
	/* lower tile */
 
	WaterClass wc_lower = IsWaterTile(tile - delta) ? GetWaterClass(tile - delta) : WATER_CLASS_CANAL;
 

	
 
	ret = DoCommand(tile - delta, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (ret.Failed()) return CMD_ERROR;
 
	if (ret.Failed()) return ret;
 
	if (GetTileSlope(tile - delta, NULL) != SLOPE_FLAT) {
 
		return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
 
	}
 

	
 
	/* upper tile */
 
	WaterClass wc_upper = IsWaterTile(tile + delta) ? GetWaterClass(tile + delta) : WATER_CLASS_CANAL;
 

	
 
	ret = DoCommand(tile + delta, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (ret.Failed()) return CMD_ERROR;
 
	if (ret.Failed()) return ret;
 
	if (GetTileSlope(tile + delta, NULL) != SLOPE_FLAT) {
 
		return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
 
	}
 

	
 
	if ((MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) ||
 
	    (MayHaveBridgeAbove(tile - delta) && IsBridgeAbove(tile - delta)) ||
0 comments (0 inline, 0 general)