changeset 9341:98ef22a4841d draft

(svn r13233) -Fix: Replace some (incorrect) evaluations of TileOwner by RoadOwner. -Fix: Before evaluating RoadOwner, check if the roadtype is present. -Fix: Some places assumed that MP_ROAD means normal street.
author frosch <frosch@openttd.org>
date Sat, 24 May 2008 19:36:20 +0000
parents 3733ee0cf3cd
children 6357104c8bb9
files src/ai/default/default.cpp src/road_cmd.cpp src/road_map.h src/smallmap_gui.cpp src/station_cmd.cpp src/town_cmd.cpp
diffstat 6 files changed, 34 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/default/default.cpp
+++ b/src/ai/default/default.cpp
@@ -3763,11 +3763,11 @@
 			DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 		}
 	} else if (IsTileType(tile, MP_ROAD)) {
-		if (!IsTileOwner(tile, _current_player)) return;
-
 		if (IsLevelCrossing(tile)) goto is_rail_crossing;
 
 		if (IsRoadDepot(tile)) {
+			if (!IsTileOwner(tile, _current_player)) return;
+
 			DiagDirection dir;
 			TileIndex t;
 
--- a/src/road_cmd.cpp
+++ b/src/road_cmd.cpp
@@ -173,11 +173,6 @@
 	return true;
 }
 
-static bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, bool *edge_road, RoadType rt)
-{
-	return CheckAllowRemoveRoad(tile, remove, GetRoadOwner(tile, rt), edge_road, rt);
-}
-
 
 /** Delete a piece of road.
  * @param tile tile where to remove road from
@@ -195,10 +190,14 @@
 	 * false if it was a center piece. Affects town ratings drop */
 	bool edge_road;
 
+	RoadTypes rts = GetRoadTypes(tile);
+	/* The tile doesn't have the given road type */
+	if (!HasBit(rts, rt)) return CMD_ERROR;
+
 	Town *t = NULL;
 	switch (GetTileType(tile)) {
 		case MP_ROAD:
-			if (_game_mode != GM_EDITOR && GetRoadOwner(tile, rt) == OWNER_TOWN) t = GetTownByTile(tile);
+			if (_game_mode != GM_EDITOR && IsRoadOwner(tile, rt, OWNER_TOWN)) t = GetTownByTile(tile);
 			if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 			break;
 
@@ -216,11 +215,7 @@
 			return CMD_ERROR;
 	}
 
-	RoadTypes rts = GetRoadTypes(tile);
-	/* The tile doesn't have the given road type */
-	if (!HasBit(rts, rt)) return CMD_ERROR;
-
-	if (!CheckAllowRemoveRoad(tile, pieces, &edge_road, rt)) return CMD_ERROR;
+	if (!CheckAllowRemoveRoad(tile, pieces, GetRoadOwner(tile, rt), &edge_road, rt)) return CMD_ERROR;
 
 	/* check if you're allowed to remove the street owned by a town
 	 * removal allowance depends on difficulty setting */
@@ -501,7 +496,7 @@
 					}
 					if ((existing & pieces) == pieces) {
 						/* We only want to set the (dis)allowed road directions */
-						if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM && GetRoadOwner(tile, ROADTYPE_ROAD) == _current_player) {
+						if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM && IsRoadOwner(tile, ROADTYPE_ROAD, _current_player)) {
 							if (crossing) return_cmd_error(STR_ERR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
 
 							if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
@@ -894,7 +889,7 @@
 			/* Clear the road if only one piece is on the tile OR the AI tries
 			 * to clear town road OR we are not using the DC_AUTO flag */
 			if ((CountBits(b) == 1 && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) ||
-			    ((flags & DC_AI_BUILDING) && IsTileOwner(tile, OWNER_TOWN)) ||
+			    ((flags & DC_AI_BUILDING) && GetOtherRoadBits(tile, ROADTYPE_ROAD) == ROAD_NONE && IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)) ||
 			    !(flags & DC_AUTO)
 				) {
 				RoadTypes rts = GetRoadTypes(tile);
@@ -1549,9 +1544,7 @@
 	}
 
 	for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
-		/* ROADTYPE_ROAD denotes the tile owner, so update it too */
-		if (rt != ROADTYPE_ROAD && !HasTileRoadType(tile, rt)) continue;
-
+		/* Update all roadtypes, no matter if they are present */
 		if (GetRoadOwner(tile, rt) == old_player) {
 			SetRoadOwner(tile, rt, new_player == PLAYER_SPECTATOR ? OWNER_NONE : new_player);
 		}
--- a/src/road_map.h
+++ b/src/road_map.h
@@ -187,6 +187,12 @@
 	}
 }
 
+static inline bool IsRoadOwner(TileIndex t, RoadType rt, Owner o)
+{
+	assert(HasTileRoadType(t, rt));
+	return (GetRoadOwner(t, rt) == o);
+}
+
 /** Which directions are disallowed ? */
 enum DisallowedRoadDirections {
 	DRD_NONE,       ///< None of the directions are disallowed
--- a/src/smallmap_gui.cpp
+++ b/src/smallmap_gui.cpp
@@ -490,6 +490,10 @@
 		case MP_INDUSTRY: o = OWNER_END;          break;
 		case MP_HOUSE:    o = OWNER_TOWN;         break;
 		default:          o = GetTileOwner(tile); break;
+		/* FIXME: For MP_ROAD there are multiple owners.
+		 * GetTileOwner returns the rail owner (level crossing) resp. the owner of ROADTYPE_ROAD (normal road),
+		 * even if there are no ROADTYPE_ROAD bits on the tile.
+		 */
 	}
 
 	return _owner_colors[o];
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -1300,7 +1300,7 @@
 	bool type = HasBit(p2, 0);
 	bool is_drive_through = HasBit(p2, 1);
 	bool build_over_road  = is_drive_through && IsNormalRoadTile(tile);
-	bool town_owned_road  = build_over_road && IsTileOwner(tile, OWNER_TOWN);
+	bool town_owned_road  = false;
 	RoadTypes rts = (RoadTypes)GB(p2, 2, 3);
 
 	if (!AreValidRoadTypes(rts) || !HasRoadTypesAvail(_current_player, rts)) return CMD_ERROR;
@@ -1319,14 +1319,17 @@
 
 	/* Not allowed to build over this road */
 	if (build_over_road) {
-		if (IsTileOwner(tile, OWNER_TOWN) && !_patches.road_stop_on_town_road) return_cmd_error(STR_DRIVE_THROUGH_ERROR_ON_TOWN_ROAD);
-
 		RoadTypes cur_rts = GetRoadTypes(tile);
 
 		/* there is a road, check if we can build road+tram stop over it */
 		if (HasBit(cur_rts, ROADTYPE_ROAD)) {
 			Owner road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
-			if (road_owner != OWNER_TOWN && road_owner != OWNER_NONE && !CheckOwnership(road_owner)) return CMD_ERROR;
+			if (road_owner == OWNER_TOWN) {
+				town_owned_road = true;
+				if (!_patches.road_stop_on_town_road) return_cmd_error(STR_DRIVE_THROUGH_ERROR_ON_TOWN_ROAD);
+			} else {
+				if (road_owner != OWNER_NONE && !CheckOwnership(road_owner)) return CMD_ERROR;
+			}
 		}
 
 		/* there is a tram, check if we can build road+tram stop over it */
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -1212,14 +1212,14 @@
 		do target_dir = RandomDiagDir(); while (!(cur_rb & DiagDirToRoadBits(target_dir)));
 		tile = TileAddByDiagDir(tile, target_dir);
 
-		if (IsTileType(tile, MP_ROAD)) {
+		if (IsTileType(tile, MP_ROAD) && !IsRoadDepot(tile) && HasTileRoadType(tile, ROADTYPE_ROAD)) {
 			/* Don't allow building over roads of other cities */
-			if (IsTileOwner(tile, OWNER_TOWN) && GetTownByTile(tile) != t) {
+			if (IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN) && GetTownByTile(tile) != t) {
 				_grow_town_result = GROWTH_SUCCEED;
-			} else if (IsTileOwner(tile, OWNER_NONE) && _game_mode == GM_EDITOR) {
+			} else if (IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_NONE) && _game_mode == GM_EDITOR) {
 				/* If we are in the SE, and this road-piece has no town owner yet, it just found an
 				 * owner :) (happy happy happy road now) */
-				SetTileOwner(tile, OWNER_TOWN);
+				SetRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN);
 				SetTownIndex(tile, t->index);
 			}
 		}
@@ -2438,8 +2438,8 @@
 Town *ClosestTownFromTile(TileIndex tile, uint threshold)
 {
 	if (IsTileType(tile, MP_HOUSE) || (
-				IsTileType(tile, MP_ROAD) &&
-				GetRoadOwner(tile, ROADTYPE_ROAD) == OWNER_TOWN
+				IsTileType(tile, MP_ROAD) && HasTileRoadType(tile, ROADTYPE_ROAD) &&
+				IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)
 			)) {
 		return GetTownByTile(tile);
 	} else {