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