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, byte 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.Get(parameter);
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 const TileIndex tile = object->u.airport.tile;
00175 return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
00176 }
00177
00178 static uint32 AirportGetTriggers(const ResolverObject *object)
00179 {
00180 return 0;
00181 }
00182
00183 static void AirportSetTriggers(const ResolverObject *object, int triggers)
00184 {
00185 }
00186
00187 static void NewAirportResolver(ResolverObject *res, TileIndex tile, Station *st, byte airport_id, byte layout)
00188 {
00189 res->GetRandomBits = AirportGetRandomBits;
00190 res->GetTriggers = AirportGetTriggers;
00191 res->SetTriggers = AirportSetTriggers;
00192 res->GetVariable = AirportGetVariable;
00193 res->ResolveReal = AirportResolveReal;
00194
00195 res->psa = st != NULL ? &st->airport.psa : NULL;
00196 res->u.airport.st = st;
00197 res->u.airport.airport_id = airport_id;
00198 res->u.airport.layout = layout;
00199 res->u.airport.tile = tile;
00200
00201 res->callback = CBID_NO_CALLBACK;
00202 res->callback_param1 = 0;
00203 res->callback_param2 = 0;
00204 res->last_value = 0;
00205 res->trigger = 0;
00206 res->reseed = 0;
00207 res->count = 0;
00208
00209 const AirportSpec *as = AirportSpec::Get(airport_id);
00210 res->grffile = as->grf_prop.grffile;
00211 }
00212
00213 SpriteID GetCustomAirportSprite(const AirportSpec *as, byte layout)
00214 {
00215 const SpriteGroup *group;
00216 ResolverObject object;
00217
00218 NewAirportResolver(&object, INVALID_TILE, NULL, as->GetIndex(), layout);
00219
00220 group = SpriteGroup::Resolve(as->grf_prop.spritegroup[0], &object);
00221 if (group == NULL) return as->preview_sprite;
00222
00223 return group->GetResult();
00224 }
00225
00226 uint16 GetAirportCallback(CallbackID callback, uint32 param1, uint32 param2, Station *st, TileIndex tile)
00227 {
00228 ResolverObject object;
00229
00230 NewAirportResolver(&object, tile, st, st->airport.type, st->airport.layout);
00231 object.callback = callback;
00232 object.callback_param1 = param1;
00233 object.callback_param2 = param2;
00234
00235 const SpriteGroup *group = SpriteGroup::Resolve(st->airport.GetSpec()->grf_prop.spritegroup[0], &object);
00236 if (group == NULL) return CALLBACK_FAILED;
00237
00238 return group->GetCallbackResult();
00239 }
00240
00248 StringID GetAirportTextCallback(const AirportSpec *as, byte layout, uint16 callback)
00249 {
00250 const SpriteGroup *group;
00251 ResolverObject object;
00252
00253 NewAirportResolver(&object, INVALID_TILE, NULL, as->GetIndex(), layout);
00254 object.callback = (CallbackID)callback;
00255
00256 group = SpriteGroup::Resolve(as->grf_prop.spritegroup[0], &object);
00257 if (group == NULL) return STR_UNDEFINED;
00258
00259 return GetGRFStringID(as->grf_prop.grffile->grfid, 0xD000 + group->GetCallbackResult());
00260 }