# newgrf_airport.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "debug.h"
    00014 #include "date_func.h"
    00015 #include "newgrf_spritegroup.h"
    00016 #include "newgrf_text.h"
    00017 #include "station_base.h"
    00018 #include "newgrf_class_func.h"
    00019
    00025 template <typename Tspec, typename Tid, Tid Tmax>
    00026 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
    00027 {
    00028   AirportClass::SetName(AirportClass::Allocate('SMAL'), STR_AIRPORT_CLASS_SMALL);
    00029   AirportClass::SetName(AirportClass::Allocate('LARG'), STR_AIRPORT_CLASS_LARGE);
    00030   AirportClass::SetName(AirportClass::Allocate('HUB_'), STR_AIRPORT_CLASS_HUB);
    00031   AirportClass::SetName(AirportClass::Allocate('HELI'), STR_AIRPORT_CLASS_HELIPORTS);
    00032 }
    00033
    00034 INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass, AirportSpec, AirportClassID, APC_MAX)
    00035
    00036
    00037 AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);
    00038
    00039 AirportSpec AirportSpec::specs[NUM_AIRPORTS];
    00040
    00047 /* static */ const AirportSpec *AirportSpec::Get(byte type)
    00048 {
    00049   assert(type < lengthof(AirportSpec::specs));
    00050   const AirportSpec *as = &AirportSpec::specs[type];
    00051   if (type >= NEW_AIRPORT_OFFSET && !as->enabled) {
    00052     byte subst_id = _airport_mngr.GetSubstituteID(type);
    00053     if (subst_id == AT_INVALID) return as;
    00054     as = &AirportSpec::specs[subst_id];
    00055   }
    00056   if (as->grf_prop.override != AT_INVALID) return &AirportSpec::specs[as->grf_prop.override];
    00057   return as;
    00058 }
    00059
    00066 /* static */ AirportSpec *AirportSpec::GetWithoutOverride(byte type)
    00067 {
    00068   assert(type < lengthof(AirportSpec::specs));
    00069   return &AirportSpec::specs[type];
    00070 }
    00071
    00073 bool AirportSpec::IsAvailable() const
    00074 {
    00075   if (!this->enabled) return false;
    00076   if (_cur_year < this->min_year) return false;
    00077   if (_settings_game.station.never_expire_airports) return true;
    00078   return _cur_year <= this->max_year;
    00079 }
    00080
    00084 void AirportSpec::ResetAirports()
    00085 {
    00086   extern const AirportSpec _origin_airport_specs[];
    00087   memset(&AirportSpec::specs, 0, sizeof(AirportSpec::specs));
    00088   memcpy(&AirportSpec::specs, &_origin_airport_specs, sizeof(AirportSpec) * NEW_AIRPORT_OFFSET);
    00089
    00090   _airport_mngr.ResetOverride();
    00091 }
    00092
    00096 void BindAirportSpecs()
    00097 {
    00098   for (int i = 0; i < NUM_AIRPORTS; i++) {
    00099     AirportSpec *as = AirportSpec::GetWithoutOverride(i);
    00100     if (as->enabled) AirportClass::Assign(as);
    00101   }
    00102 }
    00103
    00104
    00105 void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
    00106 {
    00107   byte airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);
    00108
    00109   if (airport_id == invalid_ID) {
    00110     grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
    00111     return;
    00112   }
    00113
    00114   memcpy(AirportSpec::GetWithoutOverride(airport_id), as, sizeof(*as));
    00115
    00116   /* Now add the overrides. */
    00117   for (int i = 0; i < max_offset; i++) {
    00118     AirportSpec *overridden_as = AirportSpec::GetWithoutOverride(i);
    00119
    00120     if (entity_overrides[i] != as->grf_prop.local_id || grfid_overrides[i] != as->grf_prop.grffile->grfid) continue;
    00121
    00122     overridden_as->grf_prop.override = airport_id;
    00123     overridden_as->enabled = false;
    00124     entity_overrides[i] = invalid_ID;
    00125     grfid_overrides[i] = 0;
    00126   }
    00127 }
    00128
    00129 uint32 AirportGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
    00130 {
    00131   const Station *st = object->u.airport.st;
    00132   byte layout       = object->u.airport.layout;
    00133
    00134   if (object->scope == VSG_SCOPE_PARENT) {
    00135     DEBUG(grf, 1, "Parent scope for airports unavailable");
    00136     *available = false;
    00137     return UINT_MAX;
    00138   }
    00139
    00140   switch (variable) {
    00141     case 0x40: return layout;
    00142   }
    00143
    00144   if (st == NULL) {
    00145     *available = false;
    00146     return UINT_MAX;
    00147   }
    00148
    00149   switch (variable) {
    00150     /* Get a variable from the persistent storage */
    00151     case 0x7C: return (st->airport.psa != NULL) ? st->airport.psa->GetValue(parameter) : 0;
    00152
    00153     case 0xF0: return st->facilities;
    00154     case 0xFA: return Clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
    00155   }
    00156
    00157   return st->GetNewGRFVariable(object, variable, parameter, available);
    00158 }
    00159
    00160 static const SpriteGroup *AirportResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
    00161 {
    00162   /* Airport action 2s should always have only 1 "loaded" state, but some
    00163    * times things don't follow the spec... */
    00164   if (group->num_loaded > 0) return group->loaded[0];
    00165   if (group->num_loading > 0) return group->loading[0];
    00166
    00167   return NULL;
    00168 }
    00169
    00170 static uint32 AirportGetRandomBits(const ResolverObject *object)
    00171 {
    00172   const Station *st = object->u.airport.st;
    00173   return st == NULL ? 0 : st->random_bits;
    00174 }
    00175
    00176 static uint32 AirportGetTriggers(const ResolverObject *object)
    00177 {
    00178   return 0;
    00179 }
    00180
    00181 static void AirportSetTriggers(const ResolverObject *object, int triggers)
    00182 {
    00183 }
    00184
    00191 void AirportStorePSA(ResolverObject *object, uint pos, int32 value)
    00192 {
    00193   Station *st = object->u.airport.st;
    00194   if (object->scope != VSG_SCOPE_SELF || st == NULL) return;
    00195
    00196   if (st->airport.psa == NULL) {
    00197     /* There is no need to create a storage if the value is zero. */
    00198     if (value == 0) return;
    00199
    00200     /* Create storage on first modification. */
    00201     uint32 grfid = (object->grffile != NULL) ? object->grffile->grfid : 0;
    00202     assert(PersistentStorage::CanAllocateItem());
    00203     st->airport.psa = new PersistentStorage(grfid);
    00204   }
    00205   st->airport.psa->StoreValue(pos, value);
    00206 }
    00207
    00208 static void NewAirportResolver(ResolverObject *res, TileIndex tile, Station *st, byte airport_id, byte layout)
    00209 {
    00210   res->GetRandomBits = AirportGetRandomBits;
    00211   res->GetTriggers   = AirportGetTriggers;
    00212   res->SetTriggers   = AirportSetTriggers;
    00213   res->GetVariable   = AirportGetVariable;
    00214   res->ResolveReal   = AirportResolveReal;
    00215   res->StorePSA      = AirportStorePSA;
    00216
    00217   res->u.airport.st         = st;
    00218   res->u.airport.airport_id = airport_id;
    00219   res->u.airport.layout     = layout;
    00220   res->u.airport.tile       = tile;
    00221
    00222   res->callback        = CBID_NO_CALLBACK;
    00223   res->callback_param1 = 0;
    00224   res->callback_param2 = 0;
    00225   res->ResetState();
    00226
    00227   const AirportSpec *as = AirportSpec::Get(airport_id);
    00228   res->grffile         = as->grf_prop.grffile;
    00229 }
    00230
    00231 SpriteID GetCustomAirportSprite(const AirportSpec *as, byte layout)
    00232 {
    00233   const SpriteGroup *group;
    00234   ResolverObject object;
    00235
    00236   NewAirportResolver(&object, INVALID_TILE, NULL, as->GetIndex(), layout);
    00237
    00238   group = SpriteGroup::Resolve(as->grf_prop.spritegroup[0], &object);
    00239   if (group == NULL) return as->preview_sprite;
    00240
    00241   return group->GetResult();
    00242 }
    00243
    00244 uint16 GetAirportCallback(CallbackID callback, uint32 param1, uint32 param2, Station *st, TileIndex tile)
    00245 {
    00246   ResolverObject object;
    00247
    00248   NewAirportResolver(&object, tile, st, st->airport.type, st->airport.layout);
    00249   object.callback = callback;
    00250   object.callback_param1 = param1;
    00251   object.callback_param2 = param2;
    00252
    00253   const SpriteGroup *group = SpriteGroup::Resolve(st->airport.GetSpec()->grf_prop.spritegroup[0], &object);
    00254   if (group == NULL) return CALLBACK_FAILED;
    00255
    00256   return group->GetCallbackResult();
    00257 }
    00258
    00266 StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback)
    00267 {
    00268   const SpriteGroup *group;
    00269   ResolverObject object;
    00270
    00271   NewAirportResolver(&object, INVALID_TILE, NULL, as->GetIndex(), layout);
    00272   object.callback = (CallbackID)callback;
    00273
    00274   group = SpriteGroup::Resolve(as->grf_prop.spritegroup[0], &object);
    00275   uint16 cb_res = (group != NULL) ? group->GetCallbackResult() : CALLBACK_FAILED;
    00276   if (cb_res == CALLBACK_FAILED || cb_res == 0x400) return STR_UNDEFINED;
    00277   if (cb_res > 0x400) {
    00278     ErrorUnknownCallbackResult(as->grf_prop.grffile->grfid, callback, cb_res);
    00279     return STR_UNDEFINED;
    00280   }
    00281
    00282   return GetGRFStringID(as->grf_prop.grffile->grfid, 0xD000 + cb_res);
    00283 }