# HG changeset patch
# Parent 16ba057450e850f13a9e66afcee00ecb5801ecb9

diff -r 16ba057450e8 src/ai/api/ai_airport.cpp
--- a/src/ai/api/ai_airport.cpp	Wed Sep 07 19:30:24 2011 +0200
+++ b/src/ai/api/ai_airport.cpp	Wed Sep 07 21:24:44 2011 +0200
@@ -30,15 +30,18 @@
 	return ::IsTileType(tile, MP_STATION) && ::IsAirport(tile);
 }
 
-/* static */ bool AIAirport::BuildAirport(TileIndex tile, AirportType type, StationID station_id)
+/* static */ bool AIAirport::BuildAirport(TileIndex tile, AirportType type, AirportView view, StationID station_id)
 {
 	EnforcePrecondition(false, ::IsValidTile(tile));
 	EnforcePrecondition(false, AIAirportType::IsBuildableAirportType(type));
+	EnforcePrecondition(false, AIAirportType::IsValidAirportView(view, type));
 	EnforcePrecondition(false, station_id == AIStation::STATION_NEW || station_id == AIStation::STATION_JOIN_ADJACENT || AIStation::IsValidStation(station_id));
 
+	uint p1 = type;
+	p1 |= view << 8;
 	uint p2 = station_id == AIStation::STATION_JOIN_ADJACENT ? 0 : 1;
 	p2 |= (AIStation::IsValidStation(station_id) ? station_id : INVALID_STATION) << 16;
-	return AIObject::DoCommand(tile, type, p2, CMD_BUILD_AIRPORT);
+	return AIObject::DoCommand(tile, p1, p2, CMD_BUILD_AIRPORT);
 }
 
 /* static */ bool AIAirport::RemoveAirport(TileIndex tile)
@@ -84,3 +87,17 @@
 
 	return (AirportType)::Station::Get(station_id)->airport.type;
 }
+
+/* static */ AirportView AIAirport::GetAirportView(TileIndex tile)
+{
+	if (!AITile::IsStationTile(tile)) return AT_INVALID;
+
+	StationID station_id = ::GetStationIndex(tile);
+
+	/* Reject every station that is not an airport owned by the ai company */
+	if (!AIStation::HasStationType(station_id, AIStation::STATION_AIRPORT)) return AT_INVALID;
+
+	Station *st = ::Station::Get(station_id);
+
+	return st->airport.layout;
+}
\ No newline at end of file
diff -r 16ba057450e8 src/ai/api/ai_airport.hpp
--- a/src/ai/api/ai_airport.hpp	Wed Sep 07 19:30:24 2011 +0200
+++ b/src/ai/api/ai_airport.hpp	Wed Sep 07 21:24:44 2011 +0200
@@ -63,9 +63,11 @@
 	 * Builds a airport with tile at the topleft corner.
 	 * @param tile The topleft corner of the airport.
 	 * @param type The type of airport to build.
+	 * @param view The view of given airport type to build
 	 * @param station_id The station to join, AIStation::STATION_NEW or AIStation::STATION_JOIN_ADJACENT.
 	 * @pre AIMap::IsValidTile(tile).
 	 * @pre AIAirportType::IsBuildableAirportType(type).
+	 * @pre AIAirportType::IsValidAirportView(type, view).
 	 * @pre station_id == AIStation::STATION_NEW || station_id == AIStation::STATION_JOIN_ADJACENT || AIStation::IsValidStation(station_id).
 	 * @exception AIError::ERR_AREA_NOT_CLEAR
 	 * @exception AIError::ERR_FLAT_LAND_REQUIRED
@@ -74,7 +76,7 @@
 	 * @exception AIStation::ERR_STATION_TOO_CLOSE_TO_ANOTHER_STATION
 	 * @return Whether the airport has been/can be build or not.
 	 */
-	static bool BuildAirport(TileIndex tile, AirportType type, StationID station_id);
+	static bool BuildAirport(TileIndex tile, AirportType type, AirportView view, StationID station_id);
 
 	/**
 	 * Removes an airport.
@@ -93,6 +95,15 @@
 	 * @return The AirportType of the airport.
 	 */
 	static AirportType GetAirportType(TileIndex tile);
+
+	/**
+	 * Get the AirportView of an existing airport
+	 * @param tile Any tile of the airport.
+	 * @pre AITile::IsStationTile(tile)
+	 * @pre AIStation::HasStationType(AIStation.GetStationID(tile), AIStation::STATION_AIRPORT).
+	 * @return The AirportView of the airport.
+	 */
+	static AirportView GetAirportView(TileIndex tile);
 };
 
 #endif /* AI_AIRPORT_HPP */
diff -r 16ba057450e8 src/ai/api/ai_airport.hpp.sq
--- a/src/ai/api/ai_airport.hpp.sq	Wed Sep 07 19:30:24 2011 +0200
+++ b/src/ai/api/ai_airport.hpp.sq	Wed Sep 07 21:24:44 2011 +0200
@@ -30,9 +30,10 @@
 	SQAIAirport.DefSQStaticMethod(engine, &AIAirport::IsAirportTile,      "IsAirportTile",      2, ".i");
 	SQAIAirport.DefSQStaticMethod(engine, &AIAirport::GetNumHangars,      "GetNumHangars",      2, ".i");
 	SQAIAirport.DefSQStaticMethod(engine, &AIAirport::GetHangarOfAirport, "GetHangarOfAirport", 2, ".i");
-	SQAIAirport.DefSQStaticMethod(engine, &AIAirport::BuildAirport,       "BuildAirport",       4, ".iii");
+	SQAIAirport.DefSQStaticMethod(engine, &AIAirport::BuildAirport,       "BuildAirport",       5, ".iiii");
 	SQAIAirport.DefSQStaticMethod(engine, &AIAirport::RemoveAirport,      "RemoveAirport",      2, ".i");
 	SQAIAirport.DefSQStaticMethod(engine, &AIAirport::GetAirportType,     "GetAirportType",     2, ".i");
+	SQAIAirport.DefSQStaticMethod(engine, &AIAirport::GetAirportView,     "GetAirportView",     2, ".i");
 
 	SQAIAirport.PostRegister(engine);
 }
diff -r 16ba057450e8 src/ai/api/ai_airporttype.cpp
--- a/src/ai/api/ai_airporttype.cpp	Wed Sep 07 19:30:24 2011 +0200
+++ b/src/ai/api/ai_airporttype.cpp	Wed Sep 07 21:24:44 2011 +0200
@@ -46,18 +46,48 @@
 	return _price[PR_BUILD_STATION_AIRPORT] * as->size_x * as->size_y;
 }
 
-/* static */ int32 AIAirportType::GetAirportWidth(AirportType type)
+/* static */ int32 AIAirportType::GetAirportWidth(AirportType type, AirportView view)
 {
 	if (!IsAirportTypeInformationAvailable(type)) return -1;
+	if (!IsValidAirportView(view, type)) return -1;
 
-	return ::AirportSpec::Get(type)->size_x;
+	const AirportSpec *as = ::AirportSpec::Get(type);
+	Direction dir = as->rotation[view];
+	switch(dir)
+	{
+		case DIR_N:
+		case DIR_S:
+			return as->size_x;
+
+		case DIR_E:
+		case DIR_W:
+			return as->size_y;
+
+		default:
+			NOT_REACHED();
+	}
 }
 
-/* static */ int32 AIAirportType::GetAirportHeight(AirportType type)
+/* static */ int32 AIAirportType::GetAirportHeight(AirportType type, AirportView view)
 {
 	if (!IsAirportTypeInformationAvailable(type)) return -1;
+	if (!IsValidAirportView(view, type)) return -1;
 
-	return ::AirportSpec::Get(type)->size_y;
+	const AirportSpec *as = ::AirportSpec::Get(type);
+	Direction dir = as->rotation[view];
+	switch(dir)
+	{
+		case DIR_N:
+		case DIR_S:
+			return as->size_y;
+
+		case DIR_E:
+		case DIR_W:
+			return as->size_x;
+
+		default:
+			NOT_REACHED();
+	}
 }
 
 /* static */ int32 AIAirportType::GetNumHangars(AirportType type)
diff -r 16ba057450e8 src/ai/api/ai_airporttype.hpp
--- a/src/ai/api/ai_airporttype.hpp	Wed Sep 07 19:30:24 2011 +0200
+++ b/src/ai/api/ai_airporttype.hpp	Wed Sep 07 21:24:44 2011 +0200
@@ -79,18 +79,22 @@
 	/**
 	 * Get the width of of the given airport type.
 	 * @param type The type of airport.
+	 * @param view The view of the airport type.
 	 * @pre IsAirportTypeInformationAvailable(type).
+	 * @pre IsValidAirportView(type, view).
 	 * @return The width in tiles.
 	 */
-	static int32 GetAirportWidth(AirportType type);
+	static int32 GetAirportWidth(AirportType type, AirportView view);
 
 	/**
 	 * Get the height of of the given airport type.
 	 * @param type The type of airport.
+	 * @param view The view of the airport type.
 	 * @pre IsAirportTypeInformationAvailable(type).
+	 * @pre IsValidAirportView(type, view).
 	 * @return The height in tiles.
 	 */
-	static int32 GetAirportHeight(AirportType type);
+	static int32 GetAirportHeight(AirportType type, AirportView view);
 
 	/**
 	 * Get the number of hangars of the given airport type.
diff -r 16ba057450e8 src/ai/api/ai_airporttype.hpp.sq
--- a/src/ai/api/ai_airporttype.hpp.sq	Wed Sep 07 19:30:24 2011 +0200
+++ b/src/ai/api/ai_airporttype.hpp.sq	Wed Sep 07 21:24:44 2011 +0200
@@ -37,10 +37,11 @@
 
 	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::IsBuildableAirportType,            "IsBuildableAirportType",            2, ".i");
 	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::IsAirportTypeInformationAvailable, "IsAirportTypeInformationAvailable", 2, ".i");
+	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::IsValidAirportView,                "IsValidAirportView",                3, ".ii");
 	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::IsValidPlaneType,                  "IsValidPlaneType",                  2, ".i");
 	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::GetPrice,                          "GetPrice",                          2, ".i");
-	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::GetAirportWidth,                   "GetAirportWidth",                   2, ".i");
-	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::GetAirportHeight,                  "GetAirportHeight",                  2, ".i");
+	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::GetAirportWidth,                   "GetAirportWidth",                   3, ".ii");
+	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::GetAirportHeight,                  "GetAirportHeight",                  3, ".ii");
 	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::GetNumHangars,                     "GetNumHangars",                     2, ".i");
 	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::GetNumHelipads,                    "GetNumHelipads",                    2, ".i");
 	SQAIAirportType.DefSQStaticMethod(engine, &AIAirportType::GetNumTerminals,                   "GetNumTerminals",                   2, ".i");
