Changeset - r15730:99440cf75a72
[Not reviewed]
master
0 4 0
rubidium - 14 years ago 2010-08-07 22:08:20
rubidium@openttd.org
(svn r20407) -Codechange: make AirportClass use the new generic class
4 files changed with 41 insertions and 164 deletions:
0 comments (0 inline, 0 general)
src/airport_gui.cpp
Show inline comments
 
@@ -52,13 +52,13 @@ void CcBuildAirport(const CommandCost &r
 
static void PlaceAirport(TileIndex tile)
 
{
 
	if (_selected_airport_index == -1) return;
 
	uint32 p2 = _ctrl_pressed;
 
	SB(p2, 16, 16, INVALID_STATION); // no station to join
 

	
 
	uint32 p1 = GetAirportSpecFromClass(_selected_airport_class, _selected_airport_index)->GetIndex();
 
	uint32 p1 = AirportClass::Get(_selected_airport_class, _selected_airport_index)->GetIndex();
 
	p1 |= _selected_airport_layout << 8;
 
	CommandContainer cmdcont = { tile, p1, p2, CMD_BUILD_AIRPORT | CMD_MSG(STR_ERROR_CAN_T_BUILD_AIRPORT_HERE), CcBuildAirport, "" };
 
	ShowSelectStationIfNeeded(cmdcont, TileArea(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE));
 
}
 

	
 
/** Widget number of the airport build window. */
 
@@ -219,14 +219,14 @@ class BuildAirportWindow : public Picker
 

	
 
	/** Build a dropdown list of available airport classes */
 
	static DropDownList *BuildAirportClassDropDown()
 
	{
 
		DropDownList *list = new DropDownList();
 

	
 
		for (uint i = 0; i < GetNumAirportClasses(); i++) {
 
			list->push_back(new DropDownListStringItem(GetAirportClassName((AirportClassID)i), i, false));
 
		for (uint i = 0; i < AirportClass::GetCount(); i++) {
 
			list->push_back(new DropDownListStringItem(AirportClass::GetName((AirportClassID)i), i, false));
 
		}
 

	
 
		return list;
 
	}
 

	
 
public:
 
@@ -237,32 +237,32 @@ public:
 
		this->InitNested(desc, TRANSPORT_AIR);
 

	
 
		this->SetWidgetLoweredState(BAIRW_BTN_DONTHILIGHT, !_settings_client.gui.station_show_coverage);
 
		this->SetWidgetLoweredState(BAIRW_BTN_DOHILIGHT, _settings_client.gui.station_show_coverage);
 
		this->OnInvalidateData();
 

	
 
		this->vscroll.SetCount(GetNumAirportsInClass(_selected_airport_class));
 
		this->vscroll.SetCount(AirportClass::GetCount(_selected_airport_class));
 
		this->SelectFirstAvailableAirport(true);
 
	}
 

	
 
	virtual ~BuildAirportWindow()
 
	{
 
		DeleteWindowById(WC_SELECT_STATION, 0);
 
	}
 

	
 
	virtual void SetStringParameters(int widget) const
 
	{
 
		switch (widget) {
 
			case BAIRW_CLASS_DROPDOWN:
 
				SetDParam(0, GetAirportClassName(_selected_airport_class));
 
				SetDParam(0, AirportClass::GetName(_selected_airport_class));
 
				break;
 

	
 
			case BAIRW_LAYOUT_NUM:
 
				SetDParam(0, STR_EMPTY);
 
				if (_selected_airport_index != -1) {
 
					const AirportSpec *as = GetAirportSpecFromClass(_selected_airport_class, _selected_airport_index);
 
					const AirportSpec *as = AirportClass::Get(_selected_airport_class, _selected_airport_index);
 
					StringID string = GetAirportTextCallback(as, _selected_airport_layout, CBID_AIRPORT_LAYOUT_NAME);
 
					if (string != STR_UNDEFINED) {
 
						SetDParam(0, string);
 
					} else if (as->num_table > 1) {
 
						SetDParam(0, STR_STATION_BUILD_AIRPORT_LAYOUT_NAME);
 
						SetDParam(1, _selected_airport_layout + 1);
 
@@ -276,14 +276,14 @@ public:
 

	
 
	virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
 
	{
 
		switch (widget) {
 
			case BAIRW_CLASS_DROPDOWN: {
 
				Dimension d = {0, 0};
 
				for (uint i = 0; i < GetNumAirportClasses(); i++) {
 
					SetDParam(0, GetAirportClassName((AirportClassID)i));
 
				for (uint i = 0; i < AirportClass::GetCount(); i++) {
 
					SetDParam(0, AirportClass::GetName((AirportClassID)i));
 
					d = maxdim(d, GetStringBoundingBox(STR_BLACK_STRING));
 
				}
 
				d.width += padding.width;
 
				d.height += padding.height;
 
				*size = maxdim(*size, d);
 
				break;
 
@@ -340,14 +340,14 @@ public:
 

	
 
	virtual void DrawWidget(const Rect &r, int widget) const
 
	{
 
		switch (widget) {
 
			case BAIRW_AIRPORT_LIST: {
 
				int y = r.top;
 
				for (uint i = this->vscroll.GetPosition(); this->vscroll.IsVisible(i) && i < GetNumAirportsInClass(_selected_airport_class); i++) {
 
					const AirportSpec *as = GetAirportSpecFromClass(_selected_airport_class, i);
 
				for (uint i = this->vscroll.GetPosition(); this->vscroll.IsVisible(i) && i < AirportClass::GetCount(_selected_airport_class); i++) {
 
					const AirportSpec *as = AirportClass::Get(_selected_airport_class, i);
 
					if (!as->IsAvailable()) {
 
						GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->line_height - 2, 0, FILLRECT_CHECKER);
 
					}
 
					DrawString(r.left + WD_MATRIX_LEFT, r.right + WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, as->name, ((int)i == _selected_airport_index) ? TC_WHITE : TC_BLACK);
 
					y += this->line_height;
 
				}
 
@@ -360,13 +360,13 @@ public:
 
					DrawSprite(this->preview_sprite, COMPANY_SPRITE_COLOUR(_local_company), (r.left + r.right - d.width) / 2, (r.top + r.bottom - d.height) / 2);
 
				}
 
				break;
 

	
 
			case BAIRW_EXTRA_TEXT:
 
				if (_selected_airport_index != -1) {
 
					const AirportSpec *as = GetAirportSpecFromClass(_selected_airport_class, _selected_airport_index);
 
					const AirportSpec *as = AirportClass::Get(_selected_airport_class, _selected_airport_index);
 
					StringID string = GetAirportTextCallback(as, _selected_airport_layout, CBID_AIRPORT_ADDITIONAL_TEXT);
 
					if (string != STR_UNDEFINED) {
 
						SetDParam(0, string);
 
						DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_BLACK_STRING);
 
					}
 
				}
 
@@ -382,13 +382,13 @@ public:
 
		NWidgetBase *panel_nwi = this->GetWidget<NWidgetBase>(BAIRW_BOTTOMPANEL);
 

	
 
		int right = panel_nwi->pos_x +  panel_nwi->current_x;
 
		int bottom = panel_nwi->pos_y +  panel_nwi->current_y;
 

	
 
		if (_selected_airport_index != -1) {
 
			const AirportSpec *as = GetAirportSpecFromClass(_selected_airport_class, _selected_airport_index);
 
			const AirportSpec *as = AirportClass::Get(_selected_airport_class, _selected_airport_index);
 
			int rad = _settings_game.station.modified_catchment ? as->catchment : (uint)CA_UNMODIFIED;
 

	
 
			/* only show the station (airport) noise, if the noise option is activated */
 
			if (_settings_game.economy.station_noise_level) {
 
				/* show the noise of the selected airport */
 
				SetDParam(0, as->noise_level);
 
@@ -410,13 +410,13 @@ public:
 
	void SelectOtherAirport(int airport_index)
 
	{
 
		_selected_airport_index = airport_index;
 
		_selected_airport_layout = 0;
 

	
 
		if (_selected_airport_index != -1) {
 
			const AirportSpec *as = GetAirportSpecFromClass(_selected_airport_class, _selected_airport_index);
 
			const AirportSpec *as = AirportClass::Get(_selected_airport_class, _selected_airport_index);
 
			this->preview_sprite = GetCustomAirportSprite(as, _selected_airport_layout);
 
		}
 

	
 
		this->UpdateSelectSize();
 
		this->SetDirty();
 
	}
 
@@ -425,13 +425,13 @@ public:
 
	{
 
		if (_selected_airport_index == -1) {
 
			SetTileSelectSize(1, 1);
 
			this->DisableWidget(BAIRW_LAYOUT_DECREASE);
 
			this->DisableWidget(BAIRW_LAYOUT_INCREASE);
 
		} else {
 
			const AirportSpec *as = GetAirportSpecFromClass(_selected_airport_class, _selected_airport_index);
 
			const AirportSpec *as = AirportClass::Get(_selected_airport_class, _selected_airport_index);
 
			int w = as->size_x;
 
			int h = as->size_y;
 
			Direction rotation = as->rotation[_selected_airport_layout];
 
			if (rotation == DIR_E || rotation == DIR_W) Swap(w, h);
 
			SetTileSelectSize(w, h);
 

	
 
@@ -450,13 +450,13 @@ public:
 
				ShowDropDownList(this, BuildAirportClassDropDown(), _selected_airport_class, BAIRW_CLASS_DROPDOWN);
 
				break;
 

	
 
			case BAIRW_AIRPORT_LIST: {
 
				int num_clicked = this->vscroll.GetPosition() + (pt.y - this->nested_array[widget]->pos_y) / this->line_height;
 
				if (num_clicked >= this->vscroll.GetCount()) break;
 
				const AirportSpec *as = GetAirportSpecFromClass(_selected_airport_class, num_clicked);
 
				const AirportSpec *as = AirportClass::Get(_selected_airport_class, num_clicked);
 
				if (as->IsAvailable()) this->SelectOtherAirport(num_clicked);
 
				break;
 
			}
 

	
 
			case BAIRW_BTN_DONTHILIGHT: case BAIRW_BTN_DOHILIGHT:
 
				_settings_client.gui.station_show_coverage = (widget != BAIRW_BTN_DONTHILIGHT);
 
@@ -486,25 +486,25 @@ public:
 
	 * @param change_class If true, change the class if no airport in the current
 
	 *   class is available.
 
	 */
 
	void SelectFirstAvailableAirport(bool change_class)
 
	{
 
		/* First try to select an airport in the selected class. */
 
		for (uint i = 0; i < GetNumAirportsInClass(_selected_airport_class); i++) {
 
			const AirportSpec *as = GetAirportSpecFromClass(_selected_airport_class, i);
 
		for (uint i = 0; i < AirportClass::GetCount(_selected_airport_class); i++) {
 
			const AirportSpec *as = AirportClass::Get(_selected_airport_class, i);
 
			if (as->IsAvailable()) {
 
				this->SelectOtherAirport(i);
 
				return;
 
			}
 
		}
 
		if (change_class) {
 
			/* If that fails, select the first available airport
 
			 * from a random class. */
 
			for (AirportClassID j = APC_BEGIN; j < APC_MAX; j++) {
 
				for (uint i = 0; i < GetNumAirportsInClass(j); i++) {
 
					const AirportSpec *as = GetAirportSpecFromClass(j, i);
 
				for (uint i = 0; i < AirportClass::GetCount(j); i++) {
 
					const AirportSpec *as = AirportClass::Get(j, i);
 
					if (as->IsAvailable()) {
 
						_selected_airport_class = j;
 
						this->SelectOtherAirport(i);
 
						return;
 
					}
 
				}
 
@@ -515,13 +515,13 @@ public:
 
	}
 

	
 
	virtual void OnDropdownSelect(int widget, int index)
 
	{
 
		assert(widget == BAIRW_CLASS_DROPDOWN);
 
		_selected_airport_class = (AirportClassID)index;
 
		this->vscroll.SetCount(GetNumAirportsInClass(_selected_airport_class));
 
		this->vscroll.SetCount(AirportClass::GetCount(_selected_airport_class));
 
		this->SelectFirstAvailableAirport(false);
 
	}
 

	
 
	virtual void OnTick()
 
	{
 
		CheckRedrawStationCoverage(this);
src/newgrf.cpp
Show inline comments
 
@@ -6765,13 +6765,13 @@ static void ResetNewGRFData()
 

	
 
	/* Reset station classes */
 
	StationClass::Reset();
 
	ResetCustomStations();
 

	
 
	/* Reset airport-related structures */
 
	ResetAirportClasses();
 
	AirportClass::Reset();
 
	ResetCustomAirports();
 
	AirportSpec::ResetAirports();
 
	AirportTileSpec::ResetAirportTiles();
 

	
 
	/* Reset canal sprite groups and flags */
 
	memset(_water_feature, 0, sizeof(_water_feature));
src/newgrf_airport.cpp
Show inline comments
 
@@ -19,14 +19,30 @@
 
#include "newgrf.h"
 
#include "newgrf_commons.h"
 
#include "newgrf_spritegroup.h"
 
#include "newgrf_text.h"
 
#include "station_base.h"
 
#include "table/strings.h"
 
#include "newgrf_class_func.h"
 

	
 
static AirportClass _airport_classes[APC_MAX];
 
/**
 
 * Reset airport classes to their default state.
 
 * This includes initialising the defaults classes with an empty
 
 * entry, for standard airports.
 
 */
 
template <typename Tspec, typename Tid, Tid Tmax>
 
/* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
 
{
 
	AirportClass::SetName(AirportClass::Allocate('SMAL'), STR_AIRPORT_CLASS_SMALL);
 
	AirportClass::SetName(AirportClass::Allocate('LARG'), STR_AIRPORT_CLASS_LARGE);
 
	AirportClass::SetName(AirportClass::Allocate('HUB_'), STR_AIRPORT_CLASS_HUB);
 
	AirportClass::SetName(AirportClass::Allocate('HELI'), STR_AIRPORT_CLASS_HELIPORTS);
 
}
 

	
 
INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass, AirportSpec, AirportClassID, APC_MAX)
 

	
 

	
 
AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);
 

	
 
AirportSpec AirportSpec::specs[NUM_AIRPORTS];
 

	
 
/**
 
@@ -79,148 +95,23 @@ void AirportSpec::ResetAirports()
 
	memcpy(&AirportSpec::specs, &_origin_airport_specs, sizeof(AirportSpec) * NEW_AIRPORT_OFFSET);
 

	
 
	_airport_mngr.ResetOverride();
 
}
 

	
 
/**
 
 * Allocate an airport class for the given class id
 
 * @param cls A 32 bit value identifying the class
 
 * @return Index into _airport_classes of allocated class
 
 */
 
AirportClassID AllocateAirportClass(uint32 cls)
 
{
 
	for (AirportClassID i = APC_BEGIN; i < APC_MAX; i++) {
 
		if (_airport_classes[i].id == cls) {
 
			/* ClassID is already allocated, so reuse it. */
 
			return i;
 
		} else if (_airport_classes[i].id == 0) {
 
			/* This class is empty, so allocate it to the ClassID. */
 
			_airport_classes[i].id = cls;
 
			return i;
 
		}
 
	}
 

	
 
	grfmsg(2, "AllocateAirportClass: already allocated %d classes, using small airports class", APC_MAX);
 
	return APC_SMALL;
 
}
 

	
 
/**
 
 * Set the name of an airport class
 
 * @param id The id of the class to change the name from
 
 * @param name The new name for the class
 
 */
 
void SetAirportClassName(AirportClassID id, StringID name)
 
{
 
	assert(id < APC_MAX);
 
	_airport_classes[id].name = name;
 
}
 

	
 
/**
 
 * Retrieve the name of an airport class
 
 * @param id The id of the airport class to get the name from
 
 * @return The name of the given class
 
 */
 
StringID GetAirportClassName(AirportClassID id)
 
{
 
	assert(id < APC_MAX);
 
	return _airport_classes[id].name;
 
}
 

	
 
/**
 
 * Get the number of airport classes in use
 
 * @return The number of airport classes
 
 */
 
uint GetNumAirportClasses()
 
{
 
	uint i;
 
	for (i = APC_BEGIN; i < APC_MAX && _airport_classes[i].id != 0; i++) {}
 
	return i;
 
}
 

	
 
/**
 
 * Return the number of airports for the given airport class.
 
 * @param id Index of the airport class.
 
 * @return Number of airports in the class.
 
 */
 
uint GetNumAirportsInClass(AirportClassID id)
 
{
 
	assert(id < APC_MAX);
 
	return _airport_classes[id].airports;
 
}
 

	
 
/**
 
 * Tie an airport spec to its airport class.
 
 * @param statspec The airport spec.
 
 */
 
static void BindAirportSpecToClass(AirportSpec *as)
 
{
 
	assert(as->cls_id < APC_MAX);
 
	AirportClass *airport_class = &_airport_classes[as->cls_id];
 

	
 
	int i = airport_class->airports++;
 
	airport_class->spec = ReallocT(airport_class->spec, airport_class->airports);
 

	
 
	airport_class->spec[i] = as;
 
}
 

	
 
/**
 
 * Tie all airportspecs to their class.
 
 */
 
void BindAirportSpecs()
 
{
 
	for (int i = 0; i < NUM_AIRPORTS; i++) {
 
		AirportSpec *as = AirportSpec::GetWithoutOverride(i);
 
		if (as->enabled) BindAirportSpecToClass(as);
 
		if (as->enabled) AirportClass::Assign(as);
 
	}
 
}
 

	
 

	
 
/**
 
 * Retrieve an airport spec from a class.
 
 * @param aclass Index of the airport class.
 
 * @param airport The airport index with the class.
 
 * @return The station spec.
 
 */
 
const AirportSpec *GetAirportSpecFromClass(AirportClassID aclass, uint airport)
 
{
 
	assert(aclass < APC_MAX);
 
	assert(airport < _airport_classes[aclass].airports);
 

	
 
	return _airport_classes[aclass].spec[airport];
 
}
 

	
 
/**
 
 * Reset airport classes to their default state.
 
 * This includes initialising the defaults classes with an empty
 
 * entry, for standard airports.
 
 */
 
void ResetAirportClasses()
 
{
 
	for (AirportClassID i = APC_BEGIN; i < APC_MAX; i++) {
 
		_airport_classes[i].id = 0;
 
		_airport_classes[i].name = STR_EMPTY;
 
		_airport_classes[i].airports = 0;
 

	
 
		free(_airport_classes[i].spec);
 
		_airport_classes[i].spec = NULL;
 
	}
 

	
 
	/* Set up initial data */
 
	AirportClassID id = AllocateAirportClass('SMAL');
 
	SetAirportClassName(id, STR_AIRPORT_CLASS_SMALL);
 

	
 
	id = AllocateAirportClass('LARG');
 
	SetAirportClassName(id, STR_AIRPORT_CLASS_LARGE);
 

	
 
	id = AllocateAirportClass('HUB_');
 
	SetAirportClassName(id, STR_AIRPORT_CLASS_HUB);
 

	
 
	id = AllocateAirportClass('HELI');
 
	SetAirportClassName(id, STR_AIRPORT_CLASS_HELIPORTS);
 
}
 

	
 
void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
 
{
 
	byte airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);
 

	
 
	if (airport_id == invalid_ID) {
 
		grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
src/newgrf_airport.h
Show inline comments
 
@@ -11,13 +11,13 @@
 

	
 
#ifndef NEWGRF_AIRPORT_H
 
#define NEWGRF_AIRPORT_H
 

	
 
#include "date_type.h"
 
#include "map_type.h"
 
#include "strings_type.h"
 
#include "newgrf_class.h"
 
#include "newgrf_commons.h"
 
#include "gfx_type.h"
 

	
 
/* Copy from station_map.h */
 
typedef byte StationGfx;
 

	
 
@@ -96,27 +96,13 @@ struct AirportSpec {
 

	
 
private:
 
	static AirportSpec specs[NUM_AIRPORTS];
 
};
 

	
 
/** Information related to airport classes. */
 
struct AirportClass {
 
	uint32 id;          ///< ID of this class, e.g. 'SMAL', 'LARG', 'HUB_', 'HELI', etc.
 
	StringID name;      ///< name of this class
 
	uint airports;      ///< number of airports in this class
 
	AirportSpec **spec; ///< array of airport specifications
 
};
 

	
 
void ResetAirportClasses();
 
AirportClassID AllocateAirportClass(uint32 cls);
 
void SetAirportClassName(AirportClassID id, StringID name);
 
StringID GetAirportClassName(AirportClassID id);
 

	
 
uint GetNumAirportClasses();
 
uint GetNumAirportsInClass(AirportClassID id);
 
typedef NewGRFClass<AirportSpec, AirportClassID, APC_MAX> AirportClass;
 

	
 
void BindAirportSpecs();
 
const AirportSpec *GetAirportSpecFromClass(AirportClassID aclass, uint airport);
 

	
 
StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback);
 

	
 
#endif /* NEWGRF_AIRPORT_H */
0 comments (0 inline, 0 general)