changeset 1901:74e0d3101dfb draft

(svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
author tron <tron@openttd.org>
date Sat, 04 Jun 2005 11:56:32 +0000
parents f528689f3d83
children 8088638e7d4f
files ai.c ai_new.c aircraft_cmd.c aircraft_gui.c clear_cmd.c disaster_cmd.c openttd.c order_gui.c pathfind.c players.c rail_cmd.c road_cmd.c roadveh_cmd.c roadveh_gui.c ship_cmd.c ship_gui.c smallmap_gui.c station_cmd.c town_cmd.c train_cmd.c train_gui.c tree_cmd.c tunnelbridge_cmd.c unmovable_cmd.c water_cmd.c
diffstat 25 files changed, 118 insertions(+), 122 deletions(-) [+]
line wrap: on
line diff
--- a/ai.c
+++ b/ai.c
@@ -3637,8 +3637,8 @@
 	byte m5;
 
 	if (IsTileType(tile, MP_RAILWAY)) {
-		if (_map_owner[tile] != _current_player)
-			return;
+		if (!IsTileOwner(tile, _current_player)) return;
+
 		m5 = _map5[tile];
 		if ((m5&~0x3) != 0xC0) {
 is_rail_crossing:;
@@ -3696,8 +3696,7 @@
 			DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 		}
 	} else if (IsTileType(tile, MP_STREET)) {
-		if (_map_owner[tile] != _current_player)
-			return;
+		if (!IsTileOwner(tile, _current_player)) return;
 
 		if ( (_map5[tile]&0xF0) == 0x10)
 			goto is_rail_crossing;
@@ -3707,19 +3706,19 @@
 
 			// Check if there are any stations around.
 			if (IsTileType(tile + TILE_XY(-1,0), MP_STATION) &&
-					_map_owner[tile + TILE_XY(-1,0)] == _current_player)
+					IsTileOwner(tile + TILE_XY(-1, 0), _current_player))
 						return;
 
 			if (IsTileType(tile + TILE_XY(1,0), MP_STATION) &&
-					_map_owner[tile + TILE_XY(1,0)] == _current_player)
+					IsTileOwner(tile + TILE_XY(1, 0), _current_player))
 						return;
 
 			if (IsTileType(tile + TILE_XY(0,-1), MP_STATION) &&
-					_map_owner[tile + TILE_XY(0,-1)] == _current_player)
+					IsTileOwner(tile + TILE_XY(0, -1), _current_player))
 						return;
 
 			if (IsTileType(tile + TILE_XY(0,1), MP_STATION) &&
-					_map_owner[tile + TILE_XY(0,1)] == _current_player)
+					IsTileOwner(tile + TILE_XY(0, 1), _current_player))
 						return;
 
 			dir = _map5[tile] & 3;
@@ -3735,7 +3734,7 @@
 	} else if (IsTileType(tile, MP_TUNNELBRIDGE)) {
 		byte b;
 
-		if (_map_owner[tile] != _current_player || (_map5[tile] & 0xC6) != 0x80)
+		if (!IsTileOwner(tile, _current_player) || (_map5[tile] & 0xC6) != 0x80)
 			return;
 
 		m5 = 0;
--- a/ai_new.c
+++ b/ai_new.c
@@ -763,7 +763,7 @@
 				// Its a street, test if it is a depot
 				if (_map5[tile + TileOffsByDir(j)] & 0x20) {
 					// We found a depot, is it ours? (TELL ME!!!)
-					if (_map_owner[tile + TileOffsByDir(j)] == _current_player) {
+					if (IsTileOwner(tile + TileOffsByDir(j), _current_player)) {
 						// Now, is it pointing to the right direction.........
 						if ((_map5[tile + TileOffsByDir(j)] & 3) == (j ^ 2)) {
 							// Yeah!!!
@@ -1062,7 +1062,7 @@
 	assert(p->ainew.state == AI_STATE_BUILD_DEPOT);
 
 	if (IsTileType(p->ainew.depot_tile, MP_STREET) && _map5[p->ainew.depot_tile] & 0x20) {
-		if (_map_owner[p->ainew.depot_tile] == _current_player) {
+		if (IsTileOwner(p->ainew.depot_tile, _current_player)) {
 			// The depot is already builded!
 			p->ainew.state = AI_STATE_BUILD_VEHICLE;
 			return;
--- a/aircraft_cmd.c
+++ b/aircraft_cmd.c
@@ -178,7 +178,7 @@
 	// Workaround: TODO: make AI players try to build planes in a hangar instead of just an airport tile.
 	if (!IsAircraftHangarTile(tile) && IS_HUMAN_PLAYER(_current_player)) return CMD_ERROR;
 
-	if (_map_owner[tile] != _current_player && IS_HUMAN_PLAYER(_current_player)) return CMD_ERROR;
+	if (!IsTileOwner(tile, _current_player) && IS_HUMAN_PLAYER(_current_player)) return CMD_ERROR;
 
 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 
--- a/aircraft_gui.c
+++ b/aircraft_gui.c
@@ -199,7 +199,7 @@
 	w->resize.step_height = 24;
 
 	if (tile != 0) {
-		w->caption_color = _map_owner[tile];
+		w->caption_color = GetTileOwner(tile);
 	} else {
 		w->caption_color = _local_player;
 	}
@@ -612,7 +612,8 @@
 	tile = w->window_number;
 
 	/* setup disabled buttons */
-	w->disabled_state = (_map_owner[tile]==_local_player) ? 0 : ((1<<4)|(1<<7));
+	w->disabled_state =
+		IsTileOwner(tile, _local_player) ? 0 : ((1 << 4) | (1 << 7));
 
 	/* determine amount of items for scroller */
 	num = 0;
@@ -828,7 +829,7 @@
 
 	w = AllocateWindowDescFront(&_aircraft_depot_desc, tile);
 	if (w) {
-		w->caption_color = _map_owner[tile];
+		w->caption_color = GetTileOwner(tile);
 		w->vscroll.cap = 2;
 		w->hscroll.cap = 4;
 		w->resize.step_width = 74;
@@ -1013,7 +1014,7 @@
 
 			tile = _last_built_aircraft_depot_tile;
 			do {
-				if (_map_owner[tile] == _local_player && IsAircraftHangarTile(tile)) {
+				if (IsTileOwner(tile, _local_player) && IsAircraftHangarTile(tile)) {
 					ShowAircraftDepotWindow(tile);
 					ShowBuildAircraftWindow(tile);
 					return;
--- a/clear_cmd.c
+++ b/clear_cmd.c
@@ -399,7 +399,8 @@
 
 	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 
-	if (IsTileType(tile, MP_UNMOVABLE) && _map5[tile] == 3 && _map_owner[tile] == _current_player)
+	if (IsTileType(tile, MP_UNMOVABLE) && _map5[tile] == 3 &&
+			IsTileOwner(tile, _current_player))
 		return_cmd_error(STR_5807_YOU_ALREADY_OWN_IT);
 
 	cost = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
@@ -837,7 +838,7 @@
 	if (i == 0)
 		i = (_map5[tile] & 3) + 8;
 	td->str = _clear_land_str[i - 1];
-	td->owner = _map_owner[tile];
+	td->owner = GetTileOwner(tile);
 }
 
 static void ChangeTileOwner_Clear(uint tile, byte old_player, byte new_player)
--- a/disaster_cmd.c
+++ b/disaster_cmd.c
@@ -21,7 +21,7 @@
 
 	switch (GetTileType(tile)) {
 		case MP_RAILWAY:
-			if (IS_HUMAN_PLAYER(_map_owner[tile])) DoClearSquare(tile);
+			if (IS_HUMAN_PLAYER(GetTileOwner(tile))) DoClearSquare(tile);
 			break;
 
 		case MP_HOUSE: {
@@ -173,9 +173,8 @@
 
 			if (IsValidTile(tile) &&
 					IsTileType(tile, MP_STATION) &&
-				IS_BYTE_INSIDE(_map5[tile], 8, 0x43) &&
-				IS_HUMAN_PLAYER(_map_owner[tile])) {
-
+					IS_BYTE_INSIDE(_map5[tile], 8, 0x43) &&
+					IS_HUMAN_PLAYER(GetTileOwner(tile))) {
 				v->current_order.station = 1;
 				v->age = 0;
 
@@ -199,9 +198,8 @@
 
 		if (IsValidTile(tile) &&
 				IsTileType(tile, MP_STATION) &&
-			IS_BYTE_INSIDE(_map5[tile], 8, 0x43) &&
-			IS_HUMAN_PLAYER(_map_owner[tile])) {
-
+				IS_BYTE_INSIDE(_map5[tile], 8, 0x43) &&
+				IS_HUMAN_PLAYER(GetTileOwner(tile))) {
 			st = GetStation(_map2[tile]);
 			CLRBITS(st->airport_flags, RUNWAY_IN_block);
 		}
@@ -242,8 +240,8 @@
 	tile = v->tile;/**/
 	if (IsValidTile(tile) &&
 			IsTileType(tile, MP_STATION) &&
-		IS_BYTE_INSIDE(_map5[tile], 8, 0x43) &&
-		IS_HUMAN_PLAYER(_map_owner[tile])) {
+			IS_BYTE_INSIDE(_map5[tile], 8, 0x43) &&
+			IS_HUMAN_PLAYER(GetTileOwner(tile))) {
 
 		st = GetStation(_map2[tile]);
 		SETBITS(st->airport_flags, RUNWAY_IN_block);
@@ -581,8 +579,8 @@
 		tile_org = tile = TILE_MASK(Random());
 		do {
 			if (IsTileType(tile, MP_RAILWAY) &&
-					(_map5[tile]&~3)!=0xC0 &&	IS_HUMAN_PLAYER(_map_owner[tile]))
-						break;
+					(_map5[tile] & ~3) != 0xC0 && IS_HUMAN_PLAYER(GetTileOwner(tile)))
+				break;
 			tile = TILE_MASK(tile+1);
 		} while (tile != tile_org);
 		v->dest_tile = tile;
--- a/openttd.c
+++ b/openttd.c
@@ -1299,7 +1299,7 @@
 		uint h = MapSizeY();
 
 		BEGIN_TILE_LOOP(tile_cur, w, h, tile)
-			if (IsTileType(tile_cur, MP_WATER) && _map_owner[tile_cur] >= MAX_PLAYERS)
+			if (IsTileType(tile_cur, MP_WATER) && GetTileOwner(tile_cur) >= MAX_PLAYERS)
 				_map_owner[tile_cur] = OWNER_WATER;
 		END_TILE_LOOP(tile_cur, w, h, tile)
 	}
@@ -1393,13 +1393,14 @@
 				SetTileType(tile, MP_HOUSE);
 			} else if (IsTileType(tile, MP_STREET)) {
 				//XXX magic
-				SetTileType(tile, MP_VOID);
 				_map3_hi[tile] |= (_map2[tile] << 4);
-				if ( _map_owner[tile] == OWNER_TOWN)
+				if (IsTileOwner(tile, OWNER_TOWN)) {
+					SetTileType(tile, MP_VOID);
 					_map2[tile] = ClosestTownFromTile(tile,(uint)-1)->index;
-				else
+					SetTileType(tile, MP_STREET);
+				} else {
 					_map2[tile] = 0;
-				SetTileType(tile, MP_STREET);
+				}
 			}
 		} END_TILE_LOOP(tile, MapSizeX(), MapSizeY(), 0);
 	}
--- a/order_gui.c
+++ b/order_gui.c
@@ -167,7 +167,7 @@
 	if (_patches.gotodepot) {
 		switch (GetTileType(tile)) {
 		case MP_RAILWAY:
-			if (v->type == VEH_Train && _map_owner[tile] == _local_player) {
+			if (v->type == VEH_Train && IsTileOwner(tile, _local_player)) {
 				if ((_map5[tile]&0xFC)==0xC0) {
 					order.type = OT_GOTO_DEPOT;
 					order.flags = OF_PART_OF_ORDERS;
@@ -178,7 +178,7 @@
 			break;
 
 		case MP_STREET:
-			if ((_map5[tile] & 0xF0) == 0x20 && v->type == VEH_Road && _map_owner[tile] == _local_player) {
+			if ((_map5[tile] & 0xF0) == 0x20 && v->type == VEH_Road && IsTileOwner(tile, _local_player)) {
 				order.type = OT_GOTO_DEPOT;
 				order.flags = OF_PART_OF_ORDERS;
 				order.station = GetDepotByTile(tile)->index;
@@ -188,7 +188,7 @@
 
 		case MP_STATION:
 			if (v->type != VEH_Aircraft) break;
-			if ( IsAircraftHangarTile(tile) && _map_owner[tile] == _local_player) {
+			if (IsAircraftHangarTile(tile) && IsTileOwner(tile, _local_player)) {
 				order.type = OT_GOTO_DEPOT;
 				order.flags = OF_PART_OF_ORDERS | OF_NON_STOP;	//XXX - whats the nonstop stuff doing here?
 				order.station = _map2[tile];
@@ -216,10 +216,10 @@
 	}
 
 	// check waypoint
-	if (IsTileType(tile, MP_RAILWAY)
-	&& v->type == VEH_Train
-	&& _map_owner[tile] == _local_player
-	&& (_map5[tile]&0xFE)==0xC4) {
+	if (IsTileType(tile, MP_RAILWAY) &&
+			v->type == VEH_Train &&
+			IsTileOwner(tile, _local_player) &&
+			(_map5[tile] & 0xFE) == 0xC4) {
 		order.type = OT_GOTO_WAYPOINT;
 		order.flags = 0;
 		order.station = GetWaypointByTile(tile)->index;
--- a/pathfind.c
+++ b/pathfind.c
@@ -133,7 +133,7 @@
 
 	if (tpf->tracktype == TRANSPORT_RAIL) {
 		if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNELBRIDGE)) {
-			owner = _map_owner[tile];
+			owner = GetTileOwner(tile);
 			/* Check if we are on the middle of a bridge (has no owner) */
 			if (IsTileType(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xC0) == 0xC0)
 				owner = -1;
@@ -150,7 +150,7 @@
 		if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNELBRIDGE))
 			/* Check if we are on the middle of a bridge (has no owner) */
 			if (!IsTileType(tile, MP_TUNNELBRIDGE) || (_map5[tile] & 0xC0) != 0xC0)
-				if (owner != -1 && _map_owner[tile] != owner)
+				if (owner != -1 && !IsTileOwner(tile, owner))
 					return;
 	}
 
@@ -296,7 +296,7 @@
 				/* Check if we are on a bridge (middle parts don't have an owner */
 				if (!IsTileType(tile, MP_TUNNELBRIDGE) || (_map5[tile] & 0xC0) != 0xC0)
 					if (!IsTileType(tile_org, MP_TUNNELBRIDGE) || (_map5[tile_org] & 0xC0) != 0xC0)
-						if (_map_owner[tile_org] != _map_owner[tile])
+						if (GetTileOwner(tile_org) != GetTileOwner(tile))
 							return;
 	}
 
--- a/players.c
+++ b/players.c
@@ -281,8 +281,10 @@
 
 bool CheckTileOwnership(uint tile)
 {
-	byte owner = _map_owner[tile];
+	byte owner = GetTileOwner(tile);
+
 	assert(owner <= OWNER_WATER);
+
 	if (owner == _current_player)
 		return true;
 	_error_message = STR_013B_OWNED_BY;
--- a/rail_cmd.c
+++ b/rail_cmd.c
@@ -364,7 +364,7 @@
 						(rail_bit == 2 && m5 == 0x0A) // correct direction?
 					)) {
 				if (flags & DC_EXEC) {
-					_map3_lo[tile] = _map_owner[tile];
+					_map3_lo[tile] = GetTileOwner(tile);
 					_map_owner[tile] = _current_player;
 					_map3_hi[tile] = p1;
 					_map5[tile] = 0x10 | (rail_bit == 1 ? 0x08 : 0x00); // level crossing
@@ -1067,7 +1067,7 @@
 		if (m5 & RAIL_TYPE_SPECIAL)
 			return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
 
-		if (_map_owner[tile] != _current_player)
+		if (!IsTileOwner(tile, _current_player))
 			return_cmd_error(STR_1024_AREA_IS_OWNED_BY_ANOTHER);
 
 		return_cmd_error(STR_1008_MUST_REMOVE_RAILROAD_TRACK);
@@ -1324,7 +1324,7 @@
 	uint32 tracktype_offs, image;
 	byte m5;
 
-	_drawtile_track_palette = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_map_owner[ti->tile]));
+	_drawtile_track_palette = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile)));
 
 	tracktype_offs = (_map3_lo[ti->tile] & 0xF) * TRACKTYPE_SPRITE_PITCH;
 
@@ -1900,32 +1900,32 @@
 		} else if (rail == RAIL_BIT_RIGHT) {
 			a2 = RAIL_GROUND_FENCE_VERT2;
 		} else {
-			owner = _map_owner[tile];
+			owner = GetTileOwner(tile);
 
 			if ( (!(rail&(RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LEFT)) && (rail&RAIL_BIT_DIAG1)) || rail==(RAIL_BIT_LOWER|RAIL_BIT_RIGHT)) {
 				if (!IsTileType(tile + TILE_XY(0,-1), MP_RAILWAY) ||
-						owner != _map_owner[tile + TILE_XY(0,-1)] ||
+						!IsTileOwner(tile + TILE_XY(0, -1), owner) ||
 						(_map5[tile + TILE_XY(0,-1)]==RAIL_BIT_UPPER || _map5[tile + TILE_XY(0,-1)]==RAIL_BIT_LEFT))
 							a2 = RAIL_GROUND_FENCE_NW;
 			}
 
 			if ( (!(rail&(RAIL_BIT_DIAG2|RAIL_BIT_LOWER|RAIL_BIT_RIGHT)) && (rail&RAIL_BIT_DIAG1)) || rail==(RAIL_BIT_UPPER|RAIL_BIT_LEFT)) {
 				if (!IsTileType(tile + TILE_XY(0,1), MP_RAILWAY) ||
-						owner != _map_owner[tile + TILE_XY(0,1)] ||
+						!IsTileOwner(tile + TILE_XY(0, 1), owner) ||
 						(_map5[tile + TILE_XY(0,1)]==RAIL_BIT_LOWER || _map5[tile + TILE_XY(0,1)]==RAIL_BIT_RIGHT))
 							a2 = (a2 == RAIL_GROUND_FENCE_NW) ? RAIL_GROUND_FENCE_SENW : RAIL_GROUND_FENCE_SE;
 			}
 
 			if ( (!(rail&(RAIL_BIT_DIAG1|RAIL_BIT_UPPER|RAIL_BIT_RIGHT)) && (rail&RAIL_BIT_DIAG2)) || rail==(RAIL_BIT_LOWER|RAIL_BIT_LEFT)) {
 				if (!IsTileType(tile + TILE_XY(-1,0), MP_RAILWAY) ||
-						owner != _map_owner[tile + TILE_XY(-1,0)] ||
+						!IsTileOwner(tile + TILE_XY(-1, 0), owner) ||
 						(_map5[tile + TILE_XY(-1,0)]==RAIL_BIT_UPPER || _map5[tile + TILE_XY(-1,0)]==RAIL_BIT_RIGHT))
 							a2 = RAIL_GROUND_FENCE_NE;
 			}
 
 			if ( (!(rail&(RAIL_BIT_DIAG1|RAIL_BIT_LOWER|RAIL_BIT_LEFT)) && (rail&RAIL_BIT_DIAG2)) || rail==(RAIL_BIT_UPPER|RAIL_BIT_RIGHT)) {
 				if (!IsTileType(tile + TILE_XY(1,0), MP_RAILWAY) ||
-						owner != _map_owner[tile + TILE_XY(1,0)] ||
+						!IsTileOwner(tile + TILE_XY(1, 0), owner) ||
 						(_map5[tile + TILE_XY(1,0)]==RAIL_BIT_LOWER || _map5[tile + TILE_XY(1,0)]==RAIL_BIT_LEFT))
 							a2 = (a2 == RAIL_GROUND_FENCE_NE) ? RAIL_GROUND_FENCE_NESW : RAIL_GROUND_FENCE_SW;
 			}
@@ -1997,7 +1997,7 @@
 
 static void GetTileDesc_Track(TileIndex tile, TileDesc *td)
 {
-	td->owner = _map_owner[tile];
+	td->owner = GetTileOwner(tile);
 	switch (_map5[tile] & RAIL_TYPE_MASK) {
 		case RAIL_TYPE_NORMAL:
 			td->str = STR_1021_RAILROAD_TRACK;
@@ -2025,8 +2025,7 @@
 
 static void ChangeTileOwner_Track(uint tile, byte old_player, byte new_player)
 {
-	if (_map_owner[tile] != old_player)
-		return;
+	if (!IsTileOwner(tile, old_player)) return;
 
 	if (new_player != 255) {
 		_map_owner[tile] = new_player;
--- a/road_cmd.c
+++ b/road_cmd.c
@@ -81,7 +81,7 @@
 	if (IsTileType(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) {
 		owner = _map3_lo[tile];
 	} else {
-		owner = _map_owner[tile];
+		owner = GetTileOwner(tile);
 	}
 	// Only do the special processing if the road is owned
 	// by a town
@@ -157,7 +157,7 @@
 	if (IsTileType(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) {
 		owner = _map3_lo[tile];
 	} else
-		owner = _map_owner[tile];
+		owner = GetTileOwner(tile);
 
 	if (owner == OWNER_TOWN && _game_mode != GM_EDITOR) {
 		if (IsTileType(tile, MP_TUNNELBRIDGE)) { // index of town is not saved for bridge (no space)
@@ -686,7 +686,7 @@
 		byte b = m5 & 0xF;
 
 		if (! ((1 << b) & (M(1)|M(2)|M(4)|M(8))) ) {
-			if ( (!(flags & DC_AI_BUILDING) || _map_owner[tile]!=OWNER_TOWN) && flags&DC_AUTO)
+			if ((!(flags & DC_AI_BUILDING) || !IsTileOwner(tile, OWNER_TOWN)) && flags & DC_AUTO)
 				return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
 		}
 		return DoCommandByTile(tile, b, 0, flags, CMD_REMOVE_ROAD);
@@ -835,7 +835,7 @@
 		if (ti->tileh != 0) { DrawFoundation(ti, ti->tileh); }
 
 		ormod = 0x315;
-		player = _map_owner[ti->tile];
+		player = GetTileOwner(ti->tile);
 		if (player < MAX_PLAYERS)
 			ormod = PLAYER_SPRITE_COLOR(player);
 
@@ -1101,7 +1101,7 @@
 	if (i == 0)
 		i = ((_map3_hi[tile] & 0x70) >> 4) + 3;
 	td->str = _road_tile_strings[i - 1];
-	td->owner = _map_owner[tile];
+	td->owner = GetTileOwner(tile);
 }
 
 static const byte _roadveh_enter_depot_unk0[4] = {
@@ -1145,8 +1145,7 @@
 		_map3_lo[tile] = (new_player == 0xFF) ? OWNER_NONE : new_player;
 	}
 
-	if (_map_owner[tile] != old_player)
-		return;
+	if (!IsTileOwner(tile, old_player)) return;
 
 	if (new_player != 255) {
 		_map_owner[tile] = new_player;
--- a/roadveh_cmd.c
+++ b/roadveh_cmd.c
@@ -133,7 +133,7 @@
 	/* The ai_new queries the vehicle cost before building the route,
 	 * so we must check against cheaters no sooner than now. --pasky */
 	if (!IsTileDepotType(tile, TRANSPORT_ROAD)) return CMD_ERROR;
-	if (_map_owner[tile] != _current_player) return CMD_ERROR;
+	if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
 
 	v = AllocateVehicle();
 	if (v == NULL || IsOrderPoolFull())
@@ -297,7 +297,7 @@
 
 	if (IsTileType(tile, MP_STREET) &&
 			(_map5[tile] & 0xF0) == 0x20 &&
-			_map_owner[tile] == rfdd->owner) {
+			IsTileOwner(tile, rfdd->owner)) {
 
 		if (length < rfdd->best_length) {
 			rfdd->best_length = length;
@@ -1048,11 +1048,11 @@
 	}
 
 	if (IsTileType(tile, MP_STREET)) {
-		if ((_map5[tile]&0xF0) == 0x20 && v->owner == _map_owner[tile])
+		if ((_map5[tile]&0xF0) == 0x20 && IsTileOwner(tile, v->owner))
 			/* Road crossing */
 			bitmask |= _road_veh_fp_ax_or[_map5[tile]&3];
 	} else if (IsTileType(tile, MP_STATION)) {
-		if (_map_owner[tile] == OWNER_NONE || _map_owner[tile] == v->owner) {
+		if (IsTileOwner(tile, OWNER_NONE) || IsTileOwner(tile, v->owner)) {
 			/* Our station */
 			Station *st = GetStation(_map2[tile]);
 			byte val = _map5[tile];
--- a/roadveh_gui.c
+++ b/roadveh_gui.c
@@ -494,7 +494,7 @@
 	w->resize.height = w->height - 14 * 4; /* Minimum of 4 vehicles in the display */
 
 	if (tile != 0) {
-		w->caption_color = _map_owner[tile];
+		w->caption_color = GetTileOwner(tile);
 	} else {
 		w->caption_color = _local_player;
 	}
@@ -510,7 +510,8 @@
 	tile = w->window_number;
 
 	/* setup disabled buttons */
-	w->disabled_state = (_map_owner[tile]==_local_player) ? 0 : ((1<<4)|(1<<7));
+	w->disabled_state =
+		IsTileOwner(tile, _local_player) ? 0 : ((1 << 4) | (1 << 7));
 
 	/* determine amount of items for scroller */
 	num = 0;
@@ -734,7 +735,7 @@
 
 	w = AllocateWindowDescFront(&_road_depot_desc, tile);
 	if (w) {
-		w->caption_color = _map_owner[w->window_number];
+		w->caption_color = GetTileOwner(w->window_number);
 		w->hscroll.cap = 5;
 		w->vscroll.cap = 3;
 		w->resize.step_width = 56;
--- a/ship_cmd.c
+++ b/ship_cmd.c
@@ -876,7 +876,7 @@
 	/* The ai_new queries the vehicle cost before building the route,
 	 * so we must check against cheaters no sooner than now. --pasky */
 	if (!IsTileDepotType(tile, TRANSPORT_WATER)) return CMD_ERROR;
-	if (_map_owner[tile] != _current_player) return CMD_ERROR;
+	if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
 
 	v = AllocateVehicle();
 	if (v == NULL || IsOrderPoolFull() ||
--- a/ship_gui.c
+++ b/ship_gui.c
@@ -428,7 +428,7 @@
 	w->resize.step_height = 24;
 
 	if (tile != 0) {
-		w->caption_color = _map_owner[tile];
+		w->caption_color = GetTileOwner(tile);
 	} else {
 		w->caption_color = _local_player;
 	}
@@ -588,7 +588,8 @@
 	tile = w->window_number;
 
 	/* setup disabled buttons */
-	w->disabled_state = (_map_owner[tile]==_local_player) ? 0 : ((1<<4)|(1<<7));
+	w->disabled_state =
+		IsTileOwner(tile, _local_player) ? 0 : ((1 << 4) | (1 << 7));
 
 	/* determine amount of items for scroller */
 	num = 0;
@@ -808,7 +809,7 @@
 
 	w = AllocateWindowDescFront(&_ship_depot_desc,tile);
 	if (w) {
-		w->caption_color = _map_owner[w->window_number];
+		w->caption_color = GetTileOwner(w->window_number);
 		w->vscroll.cap = 2;
 		w->hscroll.cap = 3;
 		w->resize.step_width = 90;
@@ -995,7 +996,7 @@
 
 			tile = _last_built_ship_depot_tile;
 			do {
-				if (_map_owner[tile] == _local_player && IsTileDepotType(tile, TRANSPORT_WATER)) {
+				if (IsTileOwner(tile, _local_player) && IsTileDepotType(tile, TRANSPORT_WATER)) {
 					ShowShipDepotWindow(tile);
 					ShowBuildShipWindow(tile);
 					return;
--- a/smallmap_gui.c
+++ b/smallmap_gui.c
@@ -496,12 +496,12 @@
 {
 	TileType t = GetTileType(tile);
 
-	if (t == MP_HOUSE || _map_owner[tile] == OWNER_TOWN) {
+	if (t == MP_INDUSTRY) {
+		t = 0xff;
+	} else if (t == MP_HOUSE || IsTileOwner(tile, OWNER_TOWN)) {
 		t = 0x80;
-	} else if (t == MP_INDUSTRY) {
-		t = 0xff;
 	} else {
-		t = _map_owner[tile];
+		t = GetTileOwner(tile);
 	}
 
 	return _owner_colors[t];
--- a/station_cmd.c
+++ b/station_cmd.c
@@ -2103,7 +2103,7 @@
 	uint32 relocation = 0;
 
 	{
-		uint owner = _map_owner[ti->tile];
+		uint owner = GetTileOwner(ti->tile);
 		image_or_modificator = 0x315 << 16; /* NOTE: possible bug in ttd here? */
 		if (owner < MAX_PLAYERS)
 			image_or_modificator = PLAYER_SPRITE_COLOR(owner);
@@ -2204,7 +2204,7 @@
 	byte m5;
 	StringID str;
 
-	td->owner = _map_owner[tile];
+	td->owner = GetTileOwner(tile);
 	td->build_date = GetStation(_map2[tile])->build_date;
 
 	m5 = _map5[tile];
@@ -2880,8 +2880,7 @@
 
 static void ChangeTileOwner_Station(uint tile, byte old_player, byte new_player)
 {
-	if (_map_owner[tile] != old_player)
-		return;
+	if (!IsTileOwner(tile, old_player)) return;
 
 	if (new_player != 255) {
 		Station *st = GetStation(_map2[tile]);
--- a/town_cmd.c
+++ b/town_cmd.c
@@ -741,7 +741,7 @@
 
 		if (IsTileType(tile, MP_STREET)) {
 			/* Don't allow building over roads of other cities */
-			if (_map_owner[tile] == OWNER_TOWN && GetTown(_map2[tile]) != t)
+			if (IsTileOwner(tile, OWNER_TOWN) && GetTown(_map2[tile]) != t)
 				_grow_town_result = -1;
 			else if (_game_mode == GM_EDITOR) {
 				/* If we are in the SE, and this road-piece has no town owner yet, it just found an
@@ -1495,7 +1495,7 @@
 
 			case MP_STREET:
 			case MP_TUNNELBRIDGE:
-				if (_map_owner[tile] == OWNER_TOWN &&
+				if (IsTileOwner(tile, OWNER_TOWN) &&
 						ClosestTownFromTile(tile, (uint)-1) == t)
 					DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 				break;
@@ -1837,15 +1837,12 @@
 	Town *t;
 	uint dist, best = threshold;
 	Town *best_town = NULL;
-	byte owner;
 
 	// XXX - Fix this so for a given tiletype the owner of the type is in the same variable
-	if (IsTileType(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) { // rail crossing
-		owner = _map3_lo[tile];
-	} else
-		owner = _map_owner[tile];
-
-	if ((IsTileType(tile, MP_STREET) && owner == OWNER_TOWN) || IsTileType(tile, MP_HOUSE))
+	if (IsTileType(tile, MP_HOUSE) || (
+				IsTileType(tile, MP_STREET) &&
+				((_map5[tile] & 0xF0) != 0x10 ? GetTileOwner(tile) : _map3_lo[tile]) == OWNER_TOWN
+			))
 		return GetTown(_map2[tile]);
 
 	FOR_ALL_TOWNS(t) {
--- a/train_cmd.c
+++ b/train_cmd.c
@@ -538,7 +538,7 @@
 	 * to the player. Doesn't matter if only the cost is queried */
 	if (!(flags & DC_QUERY_COST)) {
 		if (!IsTileDepotType(tile, TRANSPORT_RAIL)) return CMD_ERROR;
-		if (_map_owner[tile] != _current_player) return CMD_ERROR;
+		if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
 	}
 
 	_cmd_build_rail_veh_var1 = 0;
@@ -1387,7 +1387,7 @@
 
 static bool TrainFindDepotEnumProc(uint tile, TrainFindDepotData *tfdd, int track, uint length, byte *state)
 {
-	if (IsTileType(tile, MP_RAILWAY) && _map_owner[tile] == tfdd->owner) {
+	if (IsTileType(tile, MP_RAILWAY) && IsTileOwner(tile, tfdd->owner)) {
 		if ((_map5[tile] & ~0x3) == 0xC0) {
 			if (length < tfdd->best_length) {
 				tfdd->best_length = length;
@@ -2314,7 +2314,7 @@
 		case MP_STREET:
 			// tracks over roads, do owner check of tracks (_map_owner[tile])
 			return
-				_map_owner[tile] == v->owner &&
+				IsTileOwner(tile, v->owner) &&
 				(v->subtype != TS_Front_Engine || (_map3_hi[tile] & 0xF) == v->u.rail.railtype);
 
 		default:
@@ -2322,7 +2322,7 @@
 	}
 
 	return
-		_map_owner[tile] == v->owner &&
+		IsTileOwner(tile, v->owner) &&
 		(v->subtype != TS_Front_Engine || (_map3_lo[tile] & 0xF) == v->u.rail.railtype);
 }
 
--- a/train_gui.c
+++ b/train_gui.c
@@ -271,7 +271,7 @@
 	w->resize.height = w->height - 14 * 4; /* Minimum of 4 vehicles in the display */
 
 	if (tile != 0) {
-		w->caption_color = _map_owner[tile];
+		w->caption_color = GetTileOwner(tile);
 		WP(w,buildtrain_d).railtype = _map3_lo[tile] & 0xF;
 	} else {
 		w->caption_color = _local_player;
@@ -307,7 +307,8 @@
 	tile = w->window_number;
 
 	/* setup disabled buttons */
-	w->disabled_state = (_map_owner[tile]==_local_player) ? 0 : ((1<<4)|(1<<5)|(1<<8));
+	w->disabled_state =
+		IsTileOwner(tile, _local_player) ? 0 : ((1 << 4) | (1 << 5) | (1 << 8));
 
 	/* determine amount of items for scroller */
 	num = 0;
@@ -651,7 +652,7 @@
 
 	w = AllocateWindowDescFront(&_train_depot_desc, tile);
 	if (w) {
-		w->caption_color = _map_owner[w->window_number];
+		w->caption_color = GetTileOwner(w->window_number);
 		w->vscroll.cap = 6;
 		w->hscroll.cap = 10;
 		w->resize.step_width = 29;
@@ -1345,7 +1346,7 @@
 
 			tile = _last_built_train_depot_tile;
 			do {
-				if (_map_owner[tile] == _local_player && IsTileDepotType(tile, TRANSPORT_RAIL)) {
+				if (IsTileOwner(tile, _local_player) && IsTileDepotType(tile, TRANSPORT_RAIL)) {
 					ShowTrainDepotWindow(tile);
 					ShowBuildTrainWindow(tile);
 					return;
--- a/tree_cmd.c
+++ b/tree_cmd.c
@@ -195,7 +195,7 @@
 					break;
 
 				case MP_CLEAR:
-					if (_map_owner[ti.tile] != OWNER_NONE) {
+					if (!IsTileOwner(ti.tile, OWNER_NONE)) {
 						_error_message = STR_2804_SITE_UNSUITABLE;
 						continue;
 					}
@@ -396,7 +396,7 @@
 	byte b;
 	StringID str;
 
-	td->owner = _map_owner[tile];
+	td->owner = GetTileOwner(tile);
 
 	b = _map3_lo[tile];
 	(str=STR_2810_CACTUS_PLANTS, b==0x1B) ||
--- a/tunnelbridge_cmd.c
+++ b/tunnelbridge_cmd.c
@@ -628,7 +628,7 @@
 
 	// in scenario editor you can always destroy tunnels
 	if (_game_mode != GM_EDITOR && !CheckTileOwnership(tile)) {
-		if (!(_patches.extra_dynamite || _cheats.magic_bulldozer.value) || _map_owner[tile] != OWNER_TOWN)
+		if (!(_patches.extra_dynamite || _cheats.magic_bulldozer.value) || !IsTileOwner(tile, OWNER_TOWN))
 			return CMD_ERROR;
 	}
 
@@ -640,7 +640,7 @@
 	t = ClosestTownFromTile(tile, (uint)-1); //needed for town rating penalty
 	// check if you're allowed to remove the tunnel owned by a town
 	// removal allowal depends on difficulty settings
-	if(_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR ) {
+	if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
 		if (!CheckforTownRating(tile, flags, t, TUNNELBRIDGE_REMOVE)) {
 			SetDParam(0, t->index);
 			return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
@@ -656,7 +656,7 @@
 		DoClearSquare(endtile);
 		UpdateSignalsOnSegment(tile, _updsignals_tunnel_dir[tile_dir]);
 		UpdateSignalsOnSegment(endtile, _updsignals_tunnel_dir[endtile_dir]);
-		if (_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR)
+		if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR)
 			ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM);
 	}
 	return  _price.clear_tunnel * (length + 1);
@@ -736,7 +736,7 @@
 
 	// floods, scenario editor can always destroy bridges
 	if (_current_player != OWNER_WATER && _game_mode != GM_EDITOR && !CheckTileOwnership(tile)) {
-		if (!(_patches.extra_dynamite || _cheats.magic_bulldozer.value) || _map_owner[tile] != OWNER_TOWN)
+		if (!(_patches.extra_dynamite || _cheats.magic_bulldozer.value) || !IsTileOwner(tile, OWNER_TOWN))
 			return CMD_ERROR;
 	}
 
@@ -763,7 +763,7 @@
 	t = ClosestTownFromTile(tile, (uint)-1); //needed for town rating penalty
 	// check if you're allowed to remove the bridge owned by a town.
 	// removal allowal depends on difficulty settings
-	if(_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR) {
+	if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
 		if (!CheckforTownRating(tile, flags, t, TUNNELBRIDGE_REMOVE))
 			return CMD_ERROR;
 	}
@@ -775,7 +775,7 @@
 
 		//checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
 		// you have a "Poor" (0) town rating
-		if (_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR)
+		if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR)
 			ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM);
 
 		do {
@@ -1281,7 +1281,7 @@
 			do tile += delta; while (_map5[tile] & 0x40);
 		}
 	}
-	td->owner = _map_owner[tile];
+	td->owner = GetTileOwner(tile);
 }
 
 
@@ -1370,8 +1370,7 @@
 
 static void ChangeTileOwner_TunnelBridge(uint tile, byte old_player, byte new_player)
 {
-	if (_map_owner[tile] != old_player)
-		return;
+	if (!IsTileOwner(tile, old_player)) return;
 
 	if (new_player != 255) {
 		_map_owner[tile] = new_player;
--- a/unmovable_cmd.c
+++ b/unmovable_cmd.c
@@ -122,7 +122,7 @@
 			// statue
 			DrawGroundSprite(0x58C);
 
-			image = PLAYER_SPRITE_COLOR(_map_owner[ti->tile]);
+			image = PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile));
 			image += 0x8A48;
 			if (_display_opt & DO_TRANS_BUILDINGS)
 				image = (image & 0x3FFF) | 0x3224000;
@@ -133,7 +133,7 @@
 			DrawClearLandTile(ti, 0);
 
 			AddSortableSpriteToDraw(
-				PLAYER_SPRITE_COLOR(_map_owner[ti->tile]) + 0x92B6,
+				PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile)) + 0x92B6,
 				ti->x+8, ti->y+8,
 				1, 1,
 				10,
@@ -165,7 +165,7 @@
 
 		if (ti->tileh) DrawFoundation(ti, ti->tileh);
 
-		ormod = PLAYER_SPRITE_COLOR(_map_owner[ti->tile]);
+		ormod = PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile));
 
 		t = &_unmovable_display_datas[ti->map5 & 0x7F];
 		DrawGroundSprite(t->ground_sprite | ormod);
@@ -255,7 +255,7 @@
 	int i = _map5[tile];
 	if (i & 0x80) i = -1;
 	td->str = _unmovable_tile_str[i + 1];
-	td->owner = _map_owner[tile];
+	td->owner = GetTileOwner(tile);
 }
 
 static void AnimateTile_Unmovable(uint tile)
@@ -308,7 +308,7 @@
 static void ClickTile_Unmovable(uint tile)
 {
 	if (_map5[tile] & 0x80) {
-		ShowPlayerCompany(_map_owner[tile]);
+		ShowPlayerCompany(GetTileOwner(tile));
 	}
 }
 
@@ -396,8 +396,7 @@
 
 static void ChangeTileOwner_Unmovable(uint tile, byte old_player, byte new_player)
 {
-	if (_map_owner[tile] != old_player)
-		return;
+	if (!IsTileOwner(tile, old_player)) return;
 
 	if (_map5[tile]==3 && new_player != 255) {
 		_map_owner[tile] = new_player;
--- a/water_cmd.c
+++ b/water_cmd.c
@@ -421,7 +421,7 @@
 		return;
 	}
 
-	DrawWaterStuff(ti, _shipdepot_display_seq[ti->map5 & 0x7F], PLAYER_SPRITE_COLOR(_map_owner[ti->tile]), 0);
+	DrawWaterStuff(ti, _shipdepot_display_seq[ti->map5 & 0x7F], PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile)), 0);
 }
 
 void DrawShipDepotSprite(int x, int y, int image)
@@ -465,7 +465,7 @@
 	else
 		td->str = STR_3806_SHIP_DEPOT;
 
-	td->owner = _map_owner[tile];
+	td->owner = GetTileOwner(tile);
 }
 
 static void AnimateTile_Water(uint tile)
@@ -686,8 +686,7 @@
 
 static void ChangeTileOwner_Water(uint tile, byte old_player, byte new_player)
 {
-	if (_map_owner[tile] != old_player)
-		return;
+	if (!IsTileOwner(tile, old_player)) return;
 
 	if (new_player != 255) {
 		_map_owner[tile] = new_player;