src/airport_gui.cpp
branchNewGRF_ports
changeset 6760 d7a806a9aa44
parent 6757 60662379acb5
child 6762 aebd7285dbc2
--- a/src/airport_gui.cpp	Sun Aug 05 23:43:56 2007 +0000
+++ b/src/airport_gui.cpp	Mon Aug 06 00:44:01 2007 +0000
@@ -35,11 +35,8 @@
 	byte station_type;
 	byte station_count;
 } _airport;
-//TODO: remove above after conversion
 
-
-
-static byte _selected_airport_type;
+static byte _selected_airport_type = 0;
 
 static void ShowBuildAirportPicker();
 
@@ -54,7 +51,7 @@
 
 static void PlaceAirport(TileIndex tile)
 {
-	//DoCommandP(tile, _selected_airport_type, _ctrl_pressed, CcBuildAirport, CMD_BUILD_AIRPORT | CMD_AUTO | CMD_NO_WATER | CMD_MSG(STR_A001_CAN_T_BUILD_AIRPORT_HERE));
+	if (_airport.station_type != 0xFF) /*0xFF represents No Airports Available to build */
 		DoCommandP(tile,
 				_airport.orientation | (_airport.numtracks << 8) | (_airport.platlength << 16) | (_ctrl_pressed << 24),
 				_selected_airport_type | ((_airport.newstation_selected)? 0x80 : 0x00) | (_airport.fsmports_class << 8) | (_airport.station_type << 16), CcBuildAirport,
@@ -176,9 +173,22 @@
 
 	case WE_PAINT: {
 		bool newstations = (GetNumFSMportsClasses() != 0);
-		//DrawPixelInfo tmp_dpi, *old_dpi;
+		byte preview_sprite = 0xFF;
 		int y_offset;
-		const FSMportsSpec *fsmportspec = newstations ? GetCustomFSMportsSpec(_airport.fsmports_class, _airport.station_type) : NULL;
+
+		FSMportsSpec *fsmportspec = newstations ? GetCustomFSMportsSpec(_airport.fsmports_class, _airport.station_type) : NULL;
+		/* if the port is no longer available, find one that is */
+		if ((fsmportspec == NULL) || (HASBIT(fsmportspec->callbackmask, CBM_STATION_AVAIL) && GetFSMportsCallback(CBID_STATION_AVAILABILITY, 0, 0, fsmportspec, NULL, INVALID_TILE) == 0)) {
+			/* port not available, find new one*/
+			_airport.station_type = 0xFF;  //set to the invalid condition to prevent any selection if nothing is available
+			for (uint i = 0; i < _airport.station_count; i++) {
+				fsmportspec = GetCustomFSMportsSpec(_airport.fsmports_class, i);
+				if (!HASBIT(fsmportspec->callbackmask, CBM_STATION_AVAIL) || GetFSMportsCallback(CBID_STATION_AVAILABILITY, 0, 0, fsmportspec, NULL, INVALID_TILE) != 0) {
+					_airport.station_type = i;
+					break;
+				}
+			}
+		}
 
 		int i; // airport enabling loop
 		uint32 avail_airports;
@@ -186,28 +196,8 @@
 
 		if (WP(w, def_d).close) return;
 
-		avail_airports = GetValidAirports();
-
-		RaiseWindowWidget(w, _selected_airport_type + 7);
-		if (!HASBIT(avail_airports, 0) && _selected_airport_type == AT_SMALL) _selected_airport_type = AT_LARGE;
-		if (!HASBIT(avail_airports, 1) && _selected_airport_type == AT_LARGE) _selected_airport_type = AT_SMALL;
-		LowerWindowWidget(w, _selected_airport_type + 7);
-
-		/* 'Country Airport' starts at widget 7, and if its bit is set, it is
-		 * available, so take its opposite value to set the disabled state.
-		 * There are 9 buildable airports
-		 * XXX TODO : all airports should be held in arrays, with all relevant data.
-		 * This should be part of newgrf-airports, i suppose
-		 */
-		for (i = 0; i < 9; i++) SetWindowWidgetDisabledState(w, i + 7, !HASBIT(avail_airports, i));
 		airport = GetAirport(_selected_airport_type);
 
-		//if (_airport.newstation_selected) {
-			int x = fsmportspec->size_x[_airport.orientation];
-			int y = fsmportspec->size_y[_airport.orientation];
-			//if (_airport.orientation == 0) Swap(x, y);
-			SetTileSelectSize(x, y);
-
 		for (byte j = 0; j < 4; j++)
 		{
 			SetWindowWidgetDisabledState(w, 28 + j, true);
@@ -215,50 +205,51 @@
 			w->widget[28 + _airport.orientation].data = SPR_IMG_ARROW_NE_WHITE + j;  //reset arrows to white
 		}
 
-
-		byte preview_sprite = 0xFF;
-		for (byte j = 0; j < fsmportspec->numlayouts; j++)
+		if (_airport.station_type != 0xFF)
 		{
-			if (fsmportspec->layouts[j] == NULL) break;
-			/* enable the permitted orientations */
-			byte *layout_ptr = (byte*)alloca(x*y + 1);
-			memcpy(layout_ptr, fsmportspec->layouts[j], (x * y) + 1);
-			SetWindowWidgetDisabledState(w, 28 + GB((byte)*layout_ptr,1,2), false);
-			if (j == _airport.orientation) {
-				layout_ptr++;
-				preview_sprite = *layout_ptr;
+			int x = fsmportspec->size_x[_airport.orientation];
+			int y = fsmportspec->size_y[_airport.orientation];
+			SetTileSelectSize(x, y);
+
+			/* an airport is available, so read it and update screen accordingly */
+			for (byte j = 0; j < fsmportspec->numlayouts; j++)
+			{
+				if (fsmportspec->layouts[j] == NULL) break;
+				/* enable the permitted orientations */
+				byte *layout_ptr = (byte*)alloca(x*y + 1);
+				memcpy(layout_ptr, fsmportspec->layouts[j], (x * y) + 1);
+				SetWindowWidgetDisabledState(w, 28 + GB((byte)*layout_ptr,1,2), false);
+				if (j == _airport.orientation) {
+					layout_ptr++;
+					preview_sprite = *layout_ptr;
+				}
 			}
-		}
-		LowerWindowWidget(w, 28 + _airport.orientation);
-		w->widget[28 + _airport.orientation].data = SPR_IMG_ARROW_NE_YELLOW + _airport.orientation;  //show in yellow
+			LowerWindowWidget(w, 28 + _airport.orientation);
+			w->widget[28 + _airport.orientation].data = SPR_IMG_ARROW_NE_YELLOW + _airport.orientation;  //show in yellow
 
-		_thd.airport = _selected_airport_type;
-		_thd.airport_template = true;
+			_thd.airport = _airport.station_type;
+			_thd.airport_template = true;
 
-		int rad = _patches.modified_catchment ? ((_airport.newstation_selected)? fsmportspec->portFSM->catchment : airport->catchment) : 4;
-
-		if (_station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
+		}
 
 		SetDParam(0, GetFSMportsClassName(_airport.fsmports_class));
 		DrawWindowWidgets(w);
 
-		/* Set up a clipping area for the preview */
-		DrawFSMportsTile(9, 106, preview_sprite, _airport.fsmports_class, _airport.station_type);
-
-		//if (FillDrawPixelInfo(&tmp_dpi, 9, 106, 96, 64)) {
-			//old_dpi = _cur_dpi;
-			//_cur_dpi = &tmp_dpi;
-			//if (!DrawStationTile(32, 16, _cur_railtype, AXIS_X, _airport.station_class, _airport.station_type)) {
-			//	StationPickerDrawSprite(32, 16, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2);
-			//}
-			//_cur_dpi = old_dpi;
-		//}
+		/* only draw the preview image if the station is available */
+		if (!HASBIT(fsmportspec->callbackmask, CBM_STATION_AVAIL) || GetFSMportsCallback(CBID_STATION_AVAILABILITY, 0, 0, fsmportspec, NULL, INVALID_TILE) != 0) {
+			DrawFSMportsTile(9, 106, preview_sprite, _airport.fsmports_class, _airport.station_type);
+		}
 
 		y_offset = newstations ? 170 : 0;
 
 		// strings such as 'Size' and 'Coverage Area'
 		// 'Coverage Area'
-		DrawStationCoverageAreaText(2, 206 + y_offset, SCT_ALL, rad);
+		if (_airport.station_type != 0xFF)
+		{
+			int rad = _patches.modified_catchment ? ((_airport.newstation_selected)? fsmportspec->portFSM->catchment : airport->catchment) : 4;
+			if (_station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
+			DrawStationCoverageAreaText(2, 206 + y_offset, SCT_ALL, rad);
+		}
 
 		if (newstations) {
 			uint16 i;
@@ -271,7 +262,6 @@
 					if (HASBIT(fsmportspec->callbackmask, CBM_STATION_AVAIL) && GetFSMportsCallback(CBID_STATION_AVAILABILITY, 0, 0, fsmportspec, NULL, INVALID_TILE) == 0) {
 						GfxFillRect(8, y - 2, 127, y + 10, (1 << PALETTE_MODIFIER_GREYOUT));
 					}
-
 					DrawStringTruncated(9, y, fsmportspec->name, i == _airport.station_type ? 12 : 16, 118);
 				} else {
 					DrawStringTruncated(9, y, STR_STAT_CLASS_DFLT, i == _airport.station_type ? 12 : 16, 118);
@@ -473,6 +463,9 @@
 		_airport.newstation_selected = true;
 		_airport.newstations = true;
 		_airport.station_count = GetNumCustomFSMports(_airport.fsmports_class);
+		//if (HASBIT(fsmportspec->callbackmask, CBM_STATION_AVAIL) && GetFSMportsCallback(CBID_STATION_AVAILABILITY, 0, 0, fsmportspec, NULL, INVALID_TILE) == 0) {
+		//	_airport.station_type = 0;
+		//}
 
 		w->vscroll.count = _airport.station_count;
 		w->vscroll.cap   = 5;