When we built a track "virtually" on slope (while planning, that is @flags
don't contain DC_EXEC), we need to mark that somehow, otherwise we could
happily build another track there as well, that might not be allowed in
practice though and then while planning will success the real thing won't and
we will fail upon an assertion failure.

Index: functions.h
===================================================================
--- functions.h	(revision 539)
+++ functions.h	(working copy)
@@ -50,6 +50,13 @@
 int GetParamUint16();
 
 
+/* rail_cmd.c */
+uint32 CheckRailSlope(int tileh, uint rail_bits, uint existing);
+
+/* road_cmd.c */
+uint32 CheckRoadSlope(int tileh, byte *pieces, byte existing);
+
+
 /* sound.c */
 void SndPlayTileFx(int sound, TileIndex tile);
 void SndPlayVehicleFx(int sound, Vehicle *v);
Index: ai.c
===================================================================
--- ai.c	(revision 539)
+++ ai.c	(working copy)
@@ -1582,7 +1582,9 @@
 				if (j&1) {
 					k = i;
 					r = DoCommandByTile(t, _cur_ai_player->ai.railtype_to_use, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
+		printf("%d\n", __LINE__);
 					if (r == CMD_ERROR) return CMD_ERROR;
+		printf("%d\n", __LINE__);
 					total_cost += r;
 				}
 			}
@@ -1606,10 +1608,20 @@
 			}
 		} else if (p->mode == 3) {
 			//Clear stuff and then build single rail.
-			if (GetTileSlope(t,NULL) != 0)
+			TileInfo ti;
+			int x;
+
+			FindLandscapeHeight(&ti, GET_TILE_X(t)*16, GET_TILE_Y(t)*16);
+			if ((x = CheckRailSlope(ti.tileh, 1 << (p->attr&1), ti.map5 & 0x3F)) == CMD_ERROR) {
+				printf("[%d] Impossible slope at %d,%d (attr %x, map %x)\n", !!(flag & DC_EXEC), GET_TILE_X(t)*16, GET_TILE_Y(t)*16, p->attr, ti.map5);
 				return CMD_ERROR;
+			}
+			printf("[%d] Possible slope %d at %d,%d (attr %x, map %x)\n", !!(flag & DC_EXEC), x, GET_TILE_X(t)*16, GET_TILE_Y(t)*16, p->attr, ti.map5);
+
 			r = DoCommandByTile(t, 0, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_LANDSCAPE_CLEAR);
+		printf("%d\n", __LINE__);
 			if (r == CMD_ERROR) return CMD_ERROR;
+		printf("%d\n", __LINE__);
 			total_cost += r + _price.build_rail;
 
 			if (flag & DC_EXEC) {
@@ -1626,6 +1638,7 @@
 	}
 
 	if (city != NULL && rating > city->ratings[_current_player]) {
+		printf("%d\n", __LINE__);
 		return CMD_ERROR;
 	}
 
Index: road_cmd.c
===================================================================
--- road_cmd.c	(revision 539)
+++ road_cmd.c	(working copy)
@@ -253,7 +253,7 @@
 };
 
 
-static uint32 CheckRoadSlope(int tileh, byte *pieces, byte existing)	
+uint32 CheckRoadSlope(int tileh, byte *pieces, byte existing)
 {
 	if (!(tileh & 0x10)) {
 		byte road_bits = *pieces | existing;
Index: rail_cmd.c
===================================================================
--- rail_cmd.c	(revision 539)
+++ rail_cmd.c	(working copy)
@@ -182,7 +182,7 @@
 }
 
 //
-static uint32 CheckRailSlope(int tileh, uint rail_bits, uint existing)
+uint32 CheckRailSlope(int tileh, uint rail_bits, uint existing)
 {
 	// never allow building on top of steep tiles
 	if (!(tileh & 0x10)) {
@@ -295,19 +295,22 @@
 	}
 
 	ret = CheckRailSlope(ti.tileh, rail_bit, existing);
+	printf("Checked rail slope for %d,%d as %x %x %x, got %x\n", x, y, ti.tileh, rail_bit, existing, ret);
 	if (ret == CMD_ERROR)
 		return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
 	cost += ret;
 
-	// the AI is not allowed to used foundationed tiles.
-	if (ret && (_is_ai_player || !_patches.build_on_slopes))
+	printf("r%d\n", __LINE__);
+	if (ret && !_patches.build_on_slopes)
 		return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
 
 	if (flags & DC_EXEC && need_clear) {
 		ret = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
+	printf("r%d\n", __LINE__);
 		if (ret == CMD_ERROR) return CMD_ERROR;
 		cost += ret;
 	}
+	printf("r%d\n", __LINE__);
 
 	if (flags & DC_EXEC) {
 		_map_type_and_height[tile] &= 0xF;
