(svn r20407) -Codechange: make AirportClass use the new generic class

This commit is contained in:
rubidium 2010-08-07 22:08:20 +00:00
parent edbadde301
commit ef50a89ca5
4 changed files with 41 additions and 164 deletions

View File

@ -55,7 +55,7 @@ static void PlaceAirport(TileIndex tile)
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));
@ -222,8 +222,8 @@ class BuildAirportWindow : public PickerWindowBase {
{
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;
@ -240,7 +240,7 @@ public:
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);
}
@ -253,13 +253,13 @@ public:
{
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);
@ -279,8 +279,8 @@ public:
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;
@ -343,8 +343,8 @@ public:
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);
}
@ -363,7 +363,7 @@ public:
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);
@ -385,7 +385,7 @@ public:
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 */
@ -413,7 +413,7 @@ public:
_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);
}
@ -428,7 +428,7 @@ public:
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];
@ -453,7 +453,7 @@ public:
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;
}
@ -489,8 +489,8 @@ public:
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;
@ -500,8 +500,8 @@ public:
/* 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);
@ -518,7 +518,7 @@ public:
{
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);
}

View File

@ -6768,7 +6768,7 @@ static void ResetNewGRFData()
ResetCustomStations();
/* Reset airport-related structures */
ResetAirportClasses();
AirportClass::Reset();
ResetCustomAirports();
AirportSpec::ResetAirports();
AirportTileSpec::ResetAirportTiles();

View File

@ -22,8 +22,24 @@
#include "newgrf_text.h"
#include "station_base.h"
#include "table/strings.h"
#include "newgrf_class_func.h"
/**
* 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)
static AirportClass _airport_classes[APC_MAX];
AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);
@ -81,87 +97,6 @@ void AirportSpec::ResetAirports()
_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.
*/
@ -169,55 +104,11 @@ 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);

View File

@ -14,7 +14,7 @@
#include "date_type.h"
#include "map_type.h"
#include "strings_type.h"
#include "newgrf_class.h"
#include "newgrf_commons.h"
#include "gfx_type.h"
@ -99,23 +99,9 @@ private:
};
/** 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);