changeset 16056:feba693a0c56 draft

(svn r20749) -Fix: allow overbuilding objects with buoys and ship depots
author yexo <yexo@openttd.org>
date Sun, 05 Sep 2010 16:33:32 +0000
parents 6727ccc49c0f
children 818d37748bc0
files src/water_cmd.cpp src/waypoint_cmd.cpp
diffstat 2 files changed, 25 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/water_cmd.cpp
+++ b/src/water_cmd.cpp
@@ -100,7 +100,7 @@
 
 	TileIndex tile2 = tile + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
 
-	if (!IsWaterTile(tile) || !IsWaterTile(tile2)) {
+	if (!HasTileWaterClass(tile) || !IsTileOnWater(tile) || !HasTileWaterClass(tile2) || !IsTileOnWater(tile2)) {
 		return_cmd_error(STR_ERROR_MUST_BE_BUILT_ON_WATER);
 	}
 
@@ -111,14 +111,24 @@
 		return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
 	}
 
+	if (!Depot::CanAllocateItem()) return CMD_ERROR;
+
 	WaterClass wc1 = GetWaterClass(tile);
 	WaterClass wc2 = GetWaterClass(tile2);
-	CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
+	CommandCost cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_DEPOT_SHIP]);
+
+	bool add_cost = !IsWaterTile(tile);
+	CommandCost ret = DoCommand(tile, 0, 0, flags | DC_AUTO, CMD_LANDSCAPE_CLEAR);
 	if (ret.Failed()) return ret;
-	ret = DoCommand(tile2, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
+	if (add_cost) {
+		cost.AddCost(ret);
+	}
+	add_cost = !IsWaterTile(tile2);
+	ret = DoCommand(tile2, 0, 0, flags | DC_AUTO, CMD_LANDSCAPE_CLEAR);
 	if (ret.Failed()) return ret;
-
-	if (!Depot::CanAllocateItem()) return CMD_ERROR;
+	if (add_cost) {
+		cost.AddCost(ret);
+	}
 
 	if (flags & DC_EXEC) {
 		Depot *depot = new Depot(tile);
@@ -131,7 +141,7 @@
 		MakeDefaultName(depot);
 	}
 
-	return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_DEPOT_SHIP]);
+	return cost;
 }
 
 void MakeWaterKeepingClass(TileIndex tile, Owner o)
--- a/src/waypoint_cmd.cpp
+++ b/src/waypoint_cmd.cpp
@@ -282,7 +282,7 @@
  */
 CommandCost CmdBuildBuoy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 {
-	if (!IsWaterTile(tile) || tile == 0) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
+	if (!HasTileWaterClass(tile) || !IsTileOnWater(tile) || tile == 0) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
 	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
 
 	if (GetTileSlope(tile, NULL) != SLOPE_FLAT) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
@@ -291,6 +291,13 @@
 	Waypoint *wp = FindDeletedWaypointCloseTo(tile, STR_SV_STNAME_BUOY, OWNER_NONE);
 	if (wp == NULL && !Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
 
+	CommandCost cost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_WAYPOINT_BUOY]);
+	if (!IsWaterTile(tile)) {
+		CommandCost ret = DoCommand(tile, 0, 0, flags | DC_AUTO, CMD_LANDSCAPE_CLEAR);
+		if (ret.Failed()) return ret;
+		cost.AddCost(ret);
+	}
+
 	if (flags & DC_EXEC) {
 		if (wp == NULL) {
 			wp = new Waypoint(tile);
@@ -316,7 +323,7 @@
 		InvalidateWindowData(WC_WAYPOINT_VIEW, wp->index);
 	}
 
-	return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_WAYPOINT_BUOY]);
+	return cost;
 }
 
 /**