Changeset - r14774:6075f6397e6f
[Not reviewed]
master
0 7 0
alberth - 14 years ago 2010-03-07 20:44:05
alberth@openttd.org
(svn r19372) -Codechange: CheckTileOwnership() returns a CommandCost.
7 files changed with 74 insertions and 26 deletions:
0 comments (0 inline, 0 general)
src/company_cmd.cpp
Show inline comments
 
@@ -263,26 +263,25 @@ bool CheckOwnership(Owner owner, TileInd
 

	
 
/**
 
 * Check whether the current owner owns the stuff on
 
 * the given tile.  If that isn't the case an
 
 * appropriate error will be given.
 
 * @param tile the tile to check.
 
 * @return true iff it's owned by the current company.
 
 * @return A succeeded command iff it's owned by the current company, else a failed command.
 
 */
 
bool CheckTileOwnership(TileIndex tile)
 
CommandCost CheckTileOwnership(TileIndex tile)
 
{
 
	Owner owner = GetTileOwner(tile);
 

	
 
	assert(owner < OWNER_END);
 

	
 
	if (owner == _current_company) return true;
 
	_error_message = STR_ERROR_OWNED_BY;
 
	if (owner == _current_company) return CommandCost();
 

	
 
	/* no need to get the name of the owner unless we're the local company (saves some time) */
 
	if (IsLocalCompany()) GetNameOfOwner(owner, tile);
 
	return false;
 
	return_cmd_error(STR_ERROR_OWNED_BY);
 
}
 

	
 
static void GenerateCompanyName(Company *c)
 
{
 
	TileIndex tile;
 
	Town *t;
src/functions.h
Show inline comments
 
@@ -22,13 +22,13 @@ void TileLoopClearHelper(TileIndex tile)
 

	
 
/* company_cmd.cpp */
 
bool CheckCompanyHasMoney(CommandCost &cost);
 
void SubtractMoneyFromCompany(CommandCost cost);
 
void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cost);
 
bool CheckOwnership(Owner owner, TileIndex tile = 0);
 
bool CheckTileOwnership(TileIndex tile);
 
CommandCost CheckTileOwnership(TileIndex tile);
 

	
 
/* misc functions */
 
/**
 
 * Mark a tile given by its index dirty for repaint.
 
 *
 
 * @ingroup dirty
src/rail_cmd.cpp
Show inline comments
 
@@ -363,19 +363,21 @@ CommandCost CmdBuildSingleRail(TileIndex
 

	
 
	Slope tileh = GetTileSlope(tile, NULL);
 
	TrackBits trackbit = TrackToTrackBits(track);
 

	
 
	switch (GetTileType(tile)) {
 
		case MP_RAILWAY: {
 
			if (!CheckTileOwnership(tile)) return CMD_ERROR;
 
			CommandCost ret = CheckTileOwnership(tile);
 
			ret.SetGlobalErrorMessage();
 
			if (ret.Failed()) return ret;
 

	
 
			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);
 
			ret = CheckTrackCombination(tile, trackbit, flags);
 
			if (ret.Succeeded()) ret = EnsureNoTrainOnTrack(tile, track);
 
			ret.SetGlobalErrorMessage();
 
			if (ret.Failed()) return ret;
 

	
 
			ret = CheckRailSlope(tileh, trackbit, GetTrackBits(tile), tile);
 
			if (ret.Failed()) return ret;
 
@@ -511,17 +513,20 @@ CommandCost CmdRemoveSingleRail(TileInde
 
	Owner owner = INVALID_OWNER;
 

	
 
	Train *v = NULL;
 

	
 
	switch (GetTileType(tile)) {
 
		case MP_ROAD: {
 
			if (!IsLevelCrossing(tile) ||
 
					GetCrossingRailBits(tile) != trackbit ||
 
					(_current_company != OWNER_WATER && !CheckTileOwnership(tile))) {
 
				return CMD_ERROR;
 
			if (!IsLevelCrossing(tile) || GetCrossingRailBits(tile) != trackbit) return CMD_ERROR;
 

	
 
			if (_current_company != OWNER_WATER) {
 
				CommandCost ret = CheckTileOwnership(tile);
 
				ret.SetGlobalErrorMessage();
 
				if (ret.Failed()) return ret;
 
			}
 

	
 
			if (!(flags & DC_BANKRUPT)) {
 
				CommandCost ret = EnsureNoVehicleOnGround(tile);
 
				ret.SetGlobalErrorMessage();
 
				if (ret.Failed()) return ret;
 
			}
 

	
 
@@ -536,13 +541,19 @@ CommandCost CmdRemoveSingleRail(TileInde
 
			break;
 
		}
 

	
 
		case MP_RAILWAY: {
 
			TrackBits present;
 

	
 
			if (!IsPlainRail(tile) || (_current_company != OWNER_WATER && !CheckTileOwnership(tile))) return CMD_ERROR;
 
			if (!IsPlainRail(tile)) return CMD_ERROR;
 

	
 
			if (_current_company != OWNER_WATER) {
 
				CommandCost ret = CheckTileOwnership(tile);
 
				ret.SetGlobalErrorMessage();
 
				if (ret.Failed()) return ret;
 
			}
 

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

	
 
			present = GetTrackBits(tile);
 
@@ -903,13 +914,15 @@ CommandCost CmdBuildSingleSignal(TileInd
 
	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;
 
	ret = CheckTileOwnership(tile);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	{
 
		/* 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 &&
 
@@ -1262,13 +1275,17 @@ CommandCost CmdRemoveSingleSignal(TileIn
 
	}
 
	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;
 
	if (_current_company != OWNER_WATER) {
 
		CommandCost ret = CheckTileOwnership(tile);
 
		ret.SetGlobalErrorMessage();
 
		if (ret.Failed()) return ret;
 
	}
 

	
 
	/* Do it? */
 
	if (flags & DC_EXEC) {
 
		Train *v = NULL;
 
		if (HasReservedTracks(tile, TrackToTrackBits(track))) {
 
			v = GetTrainForReservation(tile, track);
 
@@ -1395,13 +1412,17 @@ CommandCost CmdConvertRail(TileIndex til
 
			RailType type = GetRailType(tile);
 

	
 
			/* Converting to the same type or converting 'hidden' elrail -> rail */
 
			if (type == totype || (_settings_game.vehicle.disable_elrails && totype == RAILTYPE_RAIL && type == RAILTYPE_ELECTRIC)) continue;
 

	
 
			/* Trying to convert other's rail */
 
			if (!CheckTileOwnership(tile)) continue;
 
			CommandCost ret = CheckTileOwnership(tile);
 
			if (ret.Failed()) {
 
				ret.SetGlobalErrorMessage();
 
				continue;
 
			}
 

	
 
			SmallVector<Train *, 2> vehicles_affected;
 

	
 
			/* Vehicle on the tile when not converting Rail <-> ElRail
 
			 * Tunnels and bridges have special check later */
 
			if (tt != MP_TUNNELBRIDGE) {
 
@@ -1524,14 +1545,17 @@ CommandCost CmdConvertRail(TileIndex til
 

	
 
	return (cost.GetCost() == 0) ? CMD_ERROR : cost;
 
}
 

	
 
static CommandCost RemoveTrainDepot(TileIndex tile, DoCommandFlag flags)
 
{
 
	if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER)
 
		return CMD_ERROR;
 
	if (_current_company != OWNER_WATER) {
 
		CommandCost ret = CheckTileOwnership(tile);
 
		ret.SetGlobalErrorMessage();
 
		if (ret.Failed()) return ret;
 
	}
 

	
 
	CommandCost ret = EnsureNoVehicleOnGround(tile);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	if (flags & DC_EXEC) {
src/road_cmd.cpp
Show inline comments
 
@@ -908,13 +908,17 @@ CommandCost CmdBuildRoadDepot(TileIndex 
 
	cost.AddCost(_price[PR_BUILD_DEPOT_ROAD]);
 
	return cost;
 
}
 

	
 
static CommandCost RemoveRoadDepot(TileIndex tile, DoCommandFlag flags)
 
{
 
	if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
 
	if (_current_company != OWNER_WATER) {
 
		CommandCost ret = CheckTileOwnership(tile);
 
		ret.SetGlobalErrorMessage();
 
		if (ret.Failed()) return ret;
 
	}
 

	
 
	CommandCost ret = EnsureNoVehicleOnGround(tile);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	if (flags & DC_EXEC) {
src/tunnelbridge_cmd.cpp
Show inline comments
 
@@ -584,13 +584,15 @@ static inline bool CheckAllowRemoveTunne
 
			Owner tram_owner = _current_company;
 

	
 
			if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
 
			if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
 

	
 
			/* We can remove unowned road and if the town allows it */
 
			if (road_owner == OWNER_TOWN && !(_settings_game.construction.extra_dynamite || _cheats.magic_bulldozer.value)) return CheckTileOwnership(tile);
 
			if (road_owner == OWNER_TOWN && !(_settings_game.construction.extra_dynamite || _cheats.magic_bulldozer.value)) {
 
				return CheckTileOwnership(tile).Succeeded();
 
			}
 
			if (road_owner == OWNER_NONE || road_owner == OWNER_TOWN) road_owner = _current_company;
 
			if (tram_owner == OWNER_NONE) tram_owner = _current_company;
 

	
 
			return CheckOwnership(road_owner, tile) && CheckOwnership(tram_owner, tile);
 
		}
 

	
src/unmovable_cmd.cpp
Show inline comments
 
@@ -172,13 +172,17 @@ CommandCost CmdPurchaseLandArea(TileInde
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdSellLandArea(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	if (!IsOwnedLandTile(tile)) return CMD_ERROR;
 
	if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
 
	if (_current_company != OWNER_WATER) {
 
		CommandCost ret = CheckTileOwnership(tile);
 
		ret.SetGlobalErrorMessage();
 
		if (ret.Failed()) return ret;
 
	}
 

	
 
	CommandCost ret = EnsureNoVehicleOnGround(tile);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	if (flags & DC_EXEC) DoClearSquare(tile);
 
@@ -497,13 +501,17 @@ static void ChangeTileOwner_Unmovable(Ti
 
	}
 
}
 

	
 
static CommandCost TerraformTile_Unmovable(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
 
{
 
	/* Owned land remains unsold */
 
	if (IsOwnedLand(tile) && CheckTileOwnership(tile)) return CommandCost();
 
	if (IsOwnedLand(tile)) {
 
		CommandCost ret = CheckTileOwnership(tile);
 
		ret.SetGlobalErrorMessage();
 
		if (ret.Succeeded()) return CommandCost();
 
	}
 

	
 
	if (AutoslopeEnabled() && (IsStatue(tile) || IsCompanyHQ(tile))) {
 
		if (!IsSteepSlope(tileh_new) && (z_new + GetSlopeMaxZ(tileh_new) == GetTileMaxZ(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
 
	}
 

	
 
	return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
src/water_cmd.cpp
Show inline comments
 
@@ -165,13 +165,16 @@ void MakeWaterKeepingClass(TileIndex til
 
	}
 
}
 

	
 
static CommandCost RemoveShipDepot(TileIndex tile, DoCommandFlag flags)
 
{
 
	if (!IsShipDepot(tile)) return CMD_ERROR;
 
	if (!CheckTileOwnership(tile)) return CMD_ERROR;
 

	
 
	CommandCost ret = CheckTileOwnership(tile);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	TileIndex tile2 = GetOtherShipDepotTile(tile);
 

	
 
	/* do not check for ship on tile when company goes bankrupt */
 
	if (!(flags & DC_BANKRUPT)) {
 
		CommandCost ret = EnsureNoVehicleOnGround(tile);
 
@@ -241,16 +244,20 @@ static CommandCost DoBuildShiplift(TileI
 
}
 

	
 
static CommandCost RemoveShiplift(TileIndex tile, DoCommandFlag flags)
 
{
 
	TileIndexDiff delta = TileOffsByDiagDir(GetLockDirection(tile));
 

	
 
	if (!CheckTileOwnership(tile) && GetTileOwner(tile) != OWNER_NONE) return CMD_ERROR;
 
	if (GetTileOwner(tile) != OWNER_NONE) {
 
		CommandCost ret = CheckTileOwnership(tile);
 
		ret.SetGlobalErrorMessage();
 
		if (ret.Failed()) return ret;
 
	}
 

	
 
	/* make sure no vehicle is on the tile. */
 
	CommandCost ret = EnsureNoVehicleOnGround(tile);
 
	ret = EnsureNoVehicleOnGround(tile);
 
	if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile + delta);
 
	if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile - delta);
 
	ret.SetGlobalErrorMessage();
 
	if (ret.Failed()) return ret;
 

	
 
	if (flags & DC_EXEC) {
 
@@ -358,13 +365,17 @@ static CommandCost ClearTile_Water(TileI
 

	
 
			/* Make sure no vehicle is on the tile */
 
			CommandCost ret = EnsureNoVehicleOnGround(tile);
 
			ret.SetGlobalErrorMessage();
 
			if (ret.Failed()) return ret;
 

	
 
			if (GetTileOwner(tile) != OWNER_WATER && GetTileOwner(tile) != OWNER_NONE && !CheckTileOwnership(tile)) return CMD_ERROR;
 
			if (GetTileOwner(tile) != OWNER_WATER && GetTileOwner(tile) != OWNER_NONE) {
 
				CommandCost ret = CheckTileOwnership(tile);
 
				ret.SetGlobalErrorMessage();
 
				if (ret.Failed()) return ret;
 
			}
 

	
 
			if (flags & DC_EXEC) {
 
				DoClearSquare(tile);
 
				MarkCanalsAndRiversAroundDirty(tile);
 
			}
 
			return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_WATER]);
0 comments (0 inline, 0 general)