changeset 7739:3387a53e113f draft

(svn r11276) -Codechange: be more consistent with naming of some accessors. -Fix: make sure canals are never owned by water. Based on a patch by boekabart.
author rubidium <rubidium@openttd.org>
date Tue, 16 Oct 2007 19:48:58 +0000
parents 744f1f6ea372
children 723ec770aa42
files src/industry_cmd.cpp src/openttd.cpp src/station_cmd.cpp src/station_map.h src/town_cmd.cpp src/tunnelbridge_cmd.cpp src/water_cmd.cpp src/water_map.h
diffstat 8 files changed, 52 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/industry_cmd.cpp
+++ b/src/industry_cmd.cpp
@@ -1236,11 +1236,11 @@
 					/* As soon as the tile is not water, bail out.
 					* But that does not mean the search is over.  You have
 					* to make sure every tile of the industry will be only water*/
-					if (!IsClearWaterTile(cur_tile)) return false;
+					if (!IsWaterTile(cur_tile)) return false;
 				} else {
 					Slope tileh;
 
-					if (IsClearWaterTile(cur_tile)) return false;
+					if (IsWaterTile(cur_tile)) return false;
 
 					tileh = GetTileSlope(cur_tile, NULL);
 
--- a/src/openttd.cpp
+++ b/src/openttd.cpp
@@ -1626,7 +1626,7 @@
 						if (GB(_m[t].m5, 3, 2) == 0) {
 							MakeClear(t, CLEAR_GRASS, 3);
 						} else {
-							MakeCanal(t, GetTileOwner(t));
+							MakeCanal(t, (GetTileOwner(t) == OWNER_WATER) ? OWNER_NONE : GetTileOwner(t));
 						}
 					}
 					SetBridgeMiddle(t, axis);
@@ -2160,6 +2160,19 @@
 		FOR_ALL_INDUSTRIES(i) i->founder = OWNER_NONE;
 	}
 
+	/* From version 82, old style canals (above sealevel (0), WATER owner) are no longer supported.
+	    Replace the owner for those by OWNER_NONE. */
+	if (CheckSavegameVersion(82)) {
+		for (TileIndex t = 0; t < map_size; t++) {
+			if (IsTileType(t, MP_WATER) &&
+					GetWaterTileType(t) == WATER_TILE_CLEAR &&
+					GetTileOwner(t) == OWNER_WATER &&
+					TileHeight(t) != 0) {
+				SetTileOwner(t, OWNER_NONE);
+			}
+		}
+	}
+
 	/* Recalculate */
 	Group *g;
 	FOR_ALL_GROUPS(g) {
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -1769,7 +1769,7 @@
 {
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
-	if (!IsClearWaterTile(tile) || tile == 0) return_cmd_error(STR_304B_SITE_UNSUITABLE);
+	if (!IsWaterTile(tile) || tile == 0) return_cmd_error(STR_304B_SITE_UNSUITABLE);
 	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
 
 	/* allocate and initialize new station */
@@ -2088,7 +2088,7 @@
 		DrawTramCatenary(ti, axis == AXIS_X ? ROAD_X : ROAD_Y);
 	}
 
-	if (IsBuoyTile(ti->tile) && (ti->z != 0 || !IsTileOwner(ti->tile, OWNER_WATER))) DrawCanalWater(ti->tile);
+	if (IsCanalBuoyTile(ti->tile)) DrawCanalWater(ti->tile);
 
 	const DrawTileSeqStruct *dtss;
 	foreach_draw_tile_seq(dtss, t->seq) {
--- a/src/station_map.h
+++ b/src/station_map.h
@@ -154,6 +154,15 @@
 	return IsTileType(t, MP_STATION) && IsBuoy(t);
 }
 
+static inline bool IsCanalBuoyTile(TileIndex t)
+{
+	return IsBuoyTile(t) && !IsTileOwner(t, OWNER_WATER);
+}
+
+static inline bool IsSeaBuoyTile(TileIndex t)
+{
+	return IsBuoyTile(t) && IsTileOwner(t, OWNER_WATER);
+}
 
 static inline bool IsHangarTile(TileIndex t)
 {
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -933,7 +933,7 @@
 			return false;
 		}
 		bridge_tile = TILE_MASK(bridge_tile + TileOffsByDiagDir(bridge_dir));
-	} while (IsClearWaterTile(bridge_tile));
+	} while (IsWaterTile(bridge_tile));
 
 	/* no water tiles in between? */
 	if (bridge_length == 1) return false;
@@ -1072,7 +1072,7 @@
 		TileIndex house_tile = TileAddByDiagDir(tile, target_dir); // position of a possible house
 
 		/* Don't walk into water. */
-		if (IsClearWaterTile(house_tile)) return;
+		if (IsWaterTile(house_tile)) return;
 
 		switch (_patches.town_layout) {
 			default: NOT_REACHED();
@@ -1121,7 +1121,7 @@
 	}
 
 	/* Return if a water tile */
-	if (IsClearWaterTile(tile)) return;
+	if (IsWaterTile(tile)) return;
 
 	/* Make the roads look nicer */
 	rcmd = CleanUpRoadBits(tile, rcmd);
--- a/src/tunnelbridge_cmd.cpp
+++ b/src/tunnelbridge_cmd.cpp
@@ -243,7 +243,7 @@
 	/* retrieve landscape height and ensure it's on land */
 	tile_start = TileXY(x, y);
 	tile_end = TileXY(sx, sy);
-	if (IsClearWaterTile(tile_start) || IsClearWaterTile(tile_end)) {
+	if (IsWaterTile(tile_start) || IsWaterTile(tile_end)) {
 		return_cmd_error(STR_02A0_ENDS_OF_BRIDGE_MUST_BOTH);
 	}
 
--- a/src/water_cmd.cpp
+++ b/src/water_cmd.cpp
@@ -72,7 +72,7 @@
 
 	tile2 = tile + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
 
-	if (!IsClearWaterTile(tile) || !IsClearWaterTile(tile2))
+	if (!IsWaterTile(tile) || !IsWaterTile(tile2))
 		return_cmd_error(STR_3801_MUST_BE_BUILT_ON_WATER);
 
 	if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
@@ -458,7 +458,7 @@
 	switch (GetWaterTileType(ti->tile)) {
 		case WATER_TILE_CLEAR:
 			DrawGroundSprite(SPR_FLAT_WATER_TILE, PAL_NONE);
-			if (ti->z != 0 || !IsTileOwner(ti->tile, OWNER_WATER)) DrawCanalWater(ti->tile);
+			if (IsCanal(ti->tile)) DrawCanalWater(ti->tile);
 			DrawBridgeMiddle(ti);
 			break;
 
@@ -518,7 +518,7 @@
 {
 	switch (GetWaterTileType(tile)) {
 		case WATER_TILE_CLEAR:
-			if (TilePixelHeight(tile) == 0 || IsTileOwner(tile, OWNER_WATER)) {
+			if (!IsCanal(tile)) {
 				td->str = STR_3804_WATER;
 			} else {
 				td->str = STR_LANDINFO_CANAL;
@@ -725,8 +725,10 @@
 		{{ 0, -1}, {0, 0}, {1, 0}, { 0, -1}, { 1, -1}}
 	};
 
-	/* Ensure sea-level canals and buoys on canal borders do not flood */
-	if ((IsTileType(tile, MP_WATER) || IsBuoyTile(tile)) && !IsTileOwner(tile, OWNER_WATER)) return;
+	/* Ensure buoys on canal borders do not flood */
+	if (IsCanalBuoyTile(tile)) return;
+	/* Ensure only sea and coast floods, not canals or rivers */
+	if (IsTileType(tile, MP_WATER) && !(IsSea(tile) || IsCoast(tile))) return;
 
 	/* floods in all four diagonal directions with the exception of the edges */
 	if (IS_INT_INSIDE(TileX(tile), 1, MapSizeX() - 3 + 1) &&
@@ -815,7 +817,7 @@
 static CommandCost TerraformTile_Water(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new)
 {
 	/* Canals can't be terraformed */
-	if (IsClearWaterTile(tile) && IsCanal(tile)) return_cmd_error(STR_MUST_DEMOLISH_CANAL_FIRST);
+	if (IsWaterTile(tile) && IsCanal(tile)) return_cmd_error(STR_MUST_DEMOLISH_CANAL_FIRST);
 
 	return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 }
--- a/src/water_map.h
+++ b/src/water_map.h
@@ -27,6 +27,8 @@
 
 static inline WaterTileType GetWaterTileType(TileIndex t)
 {
+	assert(IsTileType(t, MP_WATER));
+
 	if (_m[t].m5 == 0) return WATER_TILE_CLEAR;
 	if (_m[t].m5 == 1) return WATER_TILE_COAST;
 	if (IS_INT_INSIDE(_m[t].m5, LOCK_MIDDLE, LOCK_END)) return WATER_TILE_LOCK;
@@ -35,11 +37,19 @@
 	return WATER_TILE_DEPOT;
 }
 
+/** IsWater return true if any type of clear water like ocean, river, canal */
 static inline bool IsWater(TileIndex t)
 {
 	return GetWaterTileType(t) == WATER_TILE_CLEAR;
 }
 
+static inline bool IsSea(TileIndex t)
+{
+	if (GetWaterTileType(t) != WATER_TILE_CLEAR) return false;
+	if (!IsTileOwner(t, OWNER_WATER)) return false; // 'Human' built water = canal, not sea
+	return true;
+}
+
 static inline bool IsCoast(TileIndex t)
 {
 	return GetWaterTileType(t) == WATER_TILE_COAST;
@@ -50,9 +60,9 @@
 	return GetWaterTileType(t) == WATER_TILE_CLEAR && GetTileOwner(t) != OWNER_WATER;
 }
 
-static inline bool IsClearWaterTile(TileIndex t)
+static inline bool IsWaterTile(TileIndex t)
 {
-	return IsTileType(t, MP_WATER) && IsWater(t) && GetTileSlope(t, NULL) == SLOPE_FLAT;
+	return IsTileType(t, MP_WATER) && IsWater(t);
 }
 
 static inline TileIndex GetOtherShipDepotTile(TileIndex t)
@@ -109,6 +119,7 @@
 
 static inline void MakeCanal(TileIndex t, Owner o)
 {
+	assert(o != OWNER_WATER);
 	SetTileType(t, MP_WATER);
 	SetTileOwner(t, o);
 	_m[t].m2 = 0;