A lot of debugging messages. Also, road bridges are not really done, but it
should be straightforward to do them in the same way as rail bridges.

The problem is that it works only time by time, sometimes the AI misses the
opportunity for some reason. And sometimes it is too active and it builds
two-level bridge from one level over water.

Index: ai.c
===================================================================
--- ai.c	(revision 539)
+++ ai.c	(working copy)
@@ -1918,7 +1918,7 @@
 	return better;
 }
 
-static void FORCEINLINE AiCheckBuildRailBridgeHere(AiRailFinder *arf, TileIndex tile, const byte *p)
+static int FORCEINLINE AiCheckBuildRailBridgeHere(AiRailFinder *arf, TileIndex tile, const byte *p)
 {
 	TileIndex tile_new;
 	bool flag;
@@ -1927,26 +1927,40 @@
 	
 	FindLandscapeHeightByTile(&arf->ti, tile);
 
+	printf("Bridge search from %04X... z %d, slope %d\n", tile, arf->ti.z, arf->ti.tileh);
+
 	if (arf->ti.tileh == _dir_table_1[dir2] || (arf->ti.tileh==0 && arf->ti.z!=0)) {
+		int start_z = arf->ti.z;
+
+		printf("Going for it, base z %d\n", start_z);
+
 		tile_new = tile;
 		// Allow bridges directly over bottom tiles
 		flag = arf->ti.z == 0;
 		for(;;) {
 			tile_new = TILE_MASK(tile_new + _tileoffs_by_dir[dir2]);
 			FindLandscapeHeightByTile(&arf->ti, tile_new);
-			if (arf->ti.tileh != 0 || arf->ti.type == MP_CLEAR || arf->ti.type == MP_TREES) {
-				if (!flag) return;
+			printf("[%04X] Slope %d, z %d, type %d\n", tile_new, arf->ti.tileh, arf->ti.z, arf->ti.type);
+			/* End the bridge at this tile also if it is sloped and
+			 * by this slope we will end up at the height where we
+			 * started off (or higher). */
+			if ((arf->ti.tileh != 0 && arf->ti.z >= start_z)
+			    || arf->ti.type == MP_CLEAR
+			    || arf->ti.type == MP_TREES) {
+				if (!flag) return 0;
 				break;
 			}
 			if (arf->ti.type != MP_WATER && arf->ti.type != MP_RAILWAY && arf->ti.type != MP_STREET)
-				return;
+				return 0;
 			flag = true;
 		}
 
 		// Build bridge?
+		printf("So I guess I will try it.\n");
 		if (DoCommandByTile(tile_new, tile, arf->player->ai.railtype_to_use<<8, 
 			DC_AUTO, CMD_BUILD_BRIDGE) == CMD_ERROR)
-				return;
+				return 0;
+		printf("Success!\n");
 		AiBuildRailRecursive(arf, tile_new, dir2);
 		
 		// At the bottom depth, check if the new path is better than the old one.
@@ -1954,7 +1968,11 @@
 			if (AiCheckRailPathBetter(arf, p))
 				arf->bridge_end_tile = tile_new;
 		}
+
+		return 1;
 	}
+
+	return 0;
 }
 
 static void FORCEINLINE AiCheckBuildRailTunnelHere(AiRailFinder *arf, TileIndex tile, const byte *p)
@@ -2020,9 +2038,18 @@
 		p += 6;
 	} else {
 		do {
+			// First, what about a bridge here?
+			if (!AiIsTileBanned(arf->player, tile, _ai_table_15[dir][6])
+			    && AiCheckBuildRailBridgeHere(arf, tile, &_ai_table_15[dir][6])) {
+				p += 6;
+				goto built_bridge;
+
 			// Make sure the tile is not in the list of banned tiles and that a rail can be built here.
-			if (!AiIsTileBanned(arf->player, tile, p[0]) &&
-					DoCommandByTile(tile, arf->player->ai.railtype_to_use, p[0], DC_AUTO | DC_NO_WATER | DC_NO_RAIL_OVERLAP, CMD_BUILD_SINGLE_RAIL) != CMD_ERROR) {
+			} else if (!AiIsTileBanned(arf->player, tile, p[0])
+			           && DoCommandByTile(tile, arf->player->ai.railtype_to_use, p[0],
+			                              DC_AUTO | DC_NO_WATER | DC_NO_RAIL_OVERLAP,
+			                              CMD_BUILD_SINGLE_RAIL)
+			              != CMD_ERROR) {
 				AiBuildRailRecursive(arf, tile, p[1]);
 			}
 
@@ -2036,6 +2063,7 @@
 	}
 
 	AiCheckBuildRailBridgeHere(arf, tile, p);
+built_bridge:
 	AiCheckBuildRailTunnelHere(arf, tile, p+1);
 
 	arf->depth--;
@@ -2793,18 +2821,32 @@
 	bool flag;
 
 	int dir2 = p[0] & 3;
+
+	printf("Bridge search...\n");
 	
 	FindLandscapeHeightByTile(&arf->ti, tile);
+
 	if (arf->ti.tileh == _dir_table_1[dir2] || (arf->ti.tileh==0 && arf->ti.z!=0)) {
+		int start_z = arf->ti.z;
+
+		printf("Going for it, base z %d\n", start_z);
+
 		tile_new = tile;
 		// Allow bridges directly over bottom tiles
 		flag = arf->ti.z == 0;
 		for(;;) {
 			tile_new = TILE_MASK(tile_new + _tileoffs_by_dir[dir2]);
 			FindLandscapeHeightByTile(&arf->ti, tile_new);
-			if (arf->ti.tileh != 0 || arf->ti.type == MP_CLEAR || arf->ti.type == MP_TREES) {
-				// Allow a bridge if either we have a tile that's water, rail or street,
-				// or if we found an up tile.
+			printf("Slope %d, z %d, type %d\n", arf->ti.tileh, arf->ti.z, arf->ti.type);
+			/* End the bridge at this tile also if it is sloped and
+			 * by this slope we will end up at the height where we
+			 * started off (or higher). We add one level to the
+			 * tile height because for sloped tiles the height is
+			 * the minimal one but we care about the maximal one
+			 * when we are interested in slopes at all. */
+			if ((arf->ti.tileh != 0 && arf->ti.z + 8 >= start_z)
+			    || arf->ti.type == MP_CLEAR
+			    || arf->ti.type == MP_TREES) {
 				if (!flag) return;
 				break;
 			}
@@ -2813,9 +2855,11 @@
 			flag = true;
 		}
 
+		printf("So I guess I will try it.\n");
 		// Build bridge?
 		if (DoCommandByTile(tile_new, tile, 0x8000, DC_AUTO, CMD_BUILD_BRIDGE) == CMD_ERROR)
 			return;
+		printf("Success!\n");
 		AiBuildRoadRecursive(arf, tile_new, dir2);
 		
 		// At the bottom depth, check if the new path is better than the old one.
