00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "date_func.h"
00015 #include "newgrf.h"
00016 #include "newgrf_spritegroup.h"
00017 #include "newgrf_text.h"
00018 #include "station_base.h"
00019 #include "newgrf_class_func.h"
00020
00026 template <typename Tspec, typename Tid, Tid Tmax>
00027 void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
00028 {
00029 AirportClass::SetName(AirportClass::Allocate('SMAL'), STR_AIRPORT_CLASS_SMALL);
00030 AirportClass::SetName(AirportClass::Allocate('LARG'), STR_AIRPORT_CLASS_LARGE);
00031 AirportClass::SetName(AirportClass::Allocate('HUB_'), STR_AIRPORT_CLASS_HUB);
00032 AirportClass::SetName(AirportClass::Allocate('HELI'), STR_AIRPORT_CLASS_HELIPORTS);
00033 }
00034
00035 INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass, AirportSpec, AirportClassID, APC_MAX)
00036
00037
00038 AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);
00039
00040 AirportSpec AirportSpec::specs[NUM_AIRPORTS];
00041
00048 const AirportSpec *AirportSpec::Get(byte type)
00049 {
00050 assert(type < lengthof(AirportSpec::specs));
00051 const AirportSpec *as = &AirportSpec::specs[type];
00052 if (type >= NEW_AIRPORT_OFFSET && !as->enabled) {
00053 byte subst_id = _airport_mngr.GetSubstituteID(type);
00054 if (subst_id == AT_INVALID) return as;
00055 as = &AirportSpec::specs[subst_id];
00056 }
00057 if (as->grf_prop.override != AT_INVALID) return &AirportSpec::specs[as->grf_prop.override];
00058 return as;
00059 }
00060
00067 AirportSpec *AirportSpec::GetWithoutOverride(byte type)
00068 {
00069 assert(type < lengthof(AirportSpec::specs));
00070 return &AirportSpec::specs[type];
00071 }
00072
00074 bool AirportSpec::IsAvailable() const
00075 {
00076 if (!this->enabled) return false;
00077 if (_cur_year < this->min_year) return false;
00078 if (_settings_game.station.never_expire_airports) return true;
00079 return _cur_year <= this->max_year;
00080 }
00081
00085 void AirportSpec::ResetAirports()
00086 {
00087 extern const AirportSpec _origin_airport_specs[];
00088 memset(&AirportSpec::specs, 0, sizeof(AirportSpec::specs));
00089 memcpy(&AirportSpec::specs, &_origin_airport_specs, sizeof(AirportSpec) * NEW_AIRPORT_OFFSET);
00090
00091 _airport_mngr.ResetOverride();
00092 }
00093
00097 void BindAirportSpecs()
00098 {
00099 for (int i = 0; i < NUM_AIRPORTS; i++) {
00100 AirportSpec *as = AirportSpec::GetWithoutOverride(i);
00101 if (as->enabled) AirportClass::Assign(as);
00102 }
00103 }
00104
00105
00106 void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
00107 {
00108 byte airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);
00109
00110 if (airport_id == invalid_ID) {
00111 grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
00112 return;
00113 }
00114
00115 memcpy(AirportSpec::GetWithoutOverride(airport_id), as, sizeof(*as));
00116
00117
00118 for (int i = 0; i < max_offset; i++) {
00119 AirportSpec *overridden_as = AirportSpec::GetWithoutOverride(i);
00120
00121 if (entity_overrides[i] != as->grf_prop.local_id || grfid_overrides[i] != as->grf_prop.grffile->grfid) continue;
00122
00123 overridden_as->grf_prop.override = airport_id;
00124 overridden_as->enabled = false;
00125 entity_overrides[i] = invalid_ID;
00126 grfid_overrides[i] = 0;
00127 }
00128 }
00129
00130 uint32 AirportGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00131 {
00132 const Station *st = object->u.airport.st;
00133 byte layout = object->u.airport.layout;
00134
00135 if (object->scope == VSG_SCOPE_PARENT) {
00136 DEBUG(grf, 1, "Parent scope for airports unavailable");
00137 *available = false;
00138 return UINT_MAX;
00139 }
00140
00141 switch (variable) {
00142 case 0x40: return layout;
00143 }
00144
00145 if (st == NULL) {
00146 *available = false;
00147 return UINT_MAX;
00148 }
00149
00150 switch (variable) {
00151
00152 case 0x7C: return (st->airport.psa != NULL) ? st->airport.psa->GetValue(parameter) : 0;
00153
00154 case 0xF0: return st->facilities;
00155 case 0xFA: return Clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00156 }
00157
00158 return st->GetNewGRFVariable(object, variable, parameter, available);
00159 }
00160
00161 static const SpriteGroup *AirportResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00162 {
00163
00164
00165 if (group->num_loaded > 0) return group->loaded[0];
00166 if (group->num_loading > 0) return group->loading[0];
00167
00168 return NULL;
00169 }
00170
00171 static uint32 AirportGetRandomBits(const ResolverObject *object)
00172 {
00173 const Station *st = object->u.airport.st;
00174 return st == NULL ? 0 : st->random_bits;
00175 }
00176
00177 static uint32 AirportGetTriggers(const ResolverObject *object)
00178 {
00179 return 0;
00180 }
00181
00182 static void AirportSetTriggers(const ResolverObject *object, int triggers)
00183 {
00184 }
00185
00192 void AirportStorePSA(ResolverObject *object, uint pos, int32 value)
00193 {
00194 Station *st = object->u.airport.st;
00195 if (object->scope != VSG_SCOPE_SELF || st == NULL) return;
00196
00197 if (st->airport.psa == NULL) {
00198
00199 if (value == 0) return;
00200
00201
00202 uint32 grfid = (object->grffile != NULL) ? object->grffile->grfid : 0;
00203 assert(PersistentStorage::CanAllocateItem());
00204 st->airport.psa = new PersistentStorage(grfid);
00205 }
00206 st->airport.psa->StoreValue(pos, value);
00207 }
00208
00209 static void NewAirportResolver(ResolverObject *res, TileIndex tile, Station *st, byte airport_id, byte layout)
00210 {
00211 res->GetRandomBits = AirportGetRandomBits;
00212 res->GetTriggers = AirportGetTriggers;
00213 res->SetTriggers = AirportSetTriggers;
00214 res->GetVariable = AirportGetVariable;
00215 res->ResolveReal = AirportResolveReal;
00216 res->StorePSA = AirportStorePSA;
00217
00218 res->u.airport.st = st;
00219 res->u.airport.airport_id = airport_id;
00220 res->u.airport.layout = layout;
00221 res->u.airport.tile = tile;
00222
00223 res->callback = CBID_NO_CALLBACK;
00224 res->callback_param1 = 0;
00225 res->callback_param2 = 0;
00226 res->ResetState();
00227
00228 const AirportSpec *as = AirportSpec::Get(airport_id);
00229 res->grffile = as->grf_prop.grffile;
00230 }
00231
00232 SpriteID GetCustomAirportSprite(const AirportSpec *as, byte layout)
00233 {
00234 const SpriteGroup *group;
00235 ResolverObject object;
00236
00237 NewAirportResolver(&object, INVALID_TILE, NULL, as->GetIndex(), layout);
00238
00239 group = SpriteGroup::Resolve(as->grf_prop.spritegroup[0], &object);
00240 if (group == NULL) return as->preview_sprite;
00241
00242 return group->GetResult();
00243 }
00244
00245 uint16 GetAirportCallback(CallbackID callback, uint32 param1, uint32 param2, Station *st, TileIndex tile)
00246 {
00247 ResolverObject object;
00248
00249 NewAirportResolver(&object, tile, st, st->airport.type, st->airport.layout);
00250 object.callback = callback;
00251 object.callback_param1 = param1;
00252 object.callback_param2 = param2;
00253
00254 const SpriteGroup *group = SpriteGroup::Resolve(st->airport.GetSpec()->grf_prop.spritegroup[0], &object);
00255 if (group == NULL) return CALLBACK_FAILED;
00256
00257 return group->GetCallbackResult();
00258 }
00259
00267 StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback)
00268 {
00269 const SpriteGroup *group;
00270 ResolverObject object;
00271
00272 NewAirportResolver(&object, INVALID_TILE, NULL, as->GetIndex(), layout);
00273 object.callback = (CallbackID)callback;
00274
00275 group = SpriteGroup::Resolve(as->grf_prop.spritegroup[0], &object);
00276 uint16 cb_res = (group != NULL) ? group->GetCallbackResult() : CALLBACK_FAILED;
00277 if (cb_res == CALLBACK_FAILED || cb_res == 0x400) return STR_UNDEFINED;
00278 if (cb_res > 0x400) {
00279 ErrorUnknownCallbackResult(as->grf_prop.grffile->grfid, callback, cb_res);
00280 return STR_UNDEFINED;
00281 }
00282
00283 return GetGRFStringID(as->grf_prop.grffile->grfid, 0xD000 + cb_res);
00284 }