newgrf_airporttiles.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_airporttiles.cpp 23740 2012-01-03 21:32:51Z rubidium $ */
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 "newgrf_airporttiles.h"
00015 #include "newgrf_spritegroup.h"
00016 #include "newgrf_sound.h"
00017 #include "station_base.h"
00018 #include "water.h"
00019 #include "landscape.h"
00020 #include "company_base.h"
00021 #include "town.h"
00022 #include "table/strings.h"
00023 #include "table/airporttiles.h"
00024 #include "newgrf_animation_base.h"
00025 
00026 
00027 AirportTileSpec AirportTileSpec::tiles[NUM_AIRPORTTILES];
00028 
00029 AirportTileOverrideManager _airporttile_mngr(NEW_AIRPORTTILE_OFFSET, NUM_AIRPORTTILES, INVALID_AIRPORTTILE);
00030 
00036 /* static */ const AirportTileSpec *AirportTileSpec::Get(StationGfx gfx)
00037 {
00038   /* should be assert(gfx < lengthof(tiles)), but that gives compiler warnings
00039    * since it's always true if the following holds: */
00040   assert_compile(MAX_UVALUE(StationGfx) + 1 == lengthof(tiles));
00041   return &AirportTileSpec::tiles[gfx];
00042 }
00043 
00049 /* static */ const AirportTileSpec *AirportTileSpec::GetByTile(TileIndex tile)
00050 {
00051   return AirportTileSpec::Get(GetAirportGfx(tile));
00052 }
00053 
00057 void AirportTileSpec::ResetAirportTiles()
00058 {
00059   memset(&AirportTileSpec::tiles, 0, sizeof(AirportTileSpec::tiles));
00060   memcpy(&AirportTileSpec::tiles, &_origin_airporttile_specs, sizeof(_origin_airporttile_specs));
00061 
00062   /* Reset any overrides that have been set. */
00063   _airporttile_mngr.ResetOverride();
00064 }
00065 
00066 void AirportTileOverrideManager::SetEntitySpec(const AirportTileSpec *airpts)
00067 {
00068   StationGfx airpt_id = this->AddEntityID(airpts->grf_prop.local_id, airpts->grf_prop.grffile->grfid, airpts->grf_prop.subst_id);
00069 
00070   if (airpt_id == invalid_ID) {
00071     grfmsg(1, "AirportTile.SetEntitySpec: Too many airport tiles allocated. Ignoring.");
00072     return;
00073   }
00074 
00075   memcpy(&AirportTileSpec::tiles[airpt_id], airpts, sizeof(*airpts));
00076 
00077   /* Now add the overrides. */
00078   for (int i = 0; i < max_offset; i++) {
00079     AirportTileSpec *overridden_airpts = &AirportTileSpec::tiles[i];
00080 
00081     if (entity_overrides[i] != airpts->grf_prop.local_id || grfid_overrides[i] != airpts->grf_prop.grffile->grfid) continue;
00082 
00083     overridden_airpts->grf_prop.override = airpt_id;
00084     overridden_airpts->enabled = false;
00085     entity_overrides[i] = invalid_ID;
00086     grfid_overrides[i] = 0;
00087   }
00088 }
00089 
00095 StationGfx GetTranslatedAirportTileID(StationGfx gfx)
00096 {
00097   const AirportTileSpec *it = AirportTileSpec::Get(gfx);
00098   return it->grf_prop.override == INVALID_AIRPORTTILE ? gfx : it->grf_prop.override;
00099 }
00100 
00101 
00102 static const SpriteGroup *AirportTileResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00103 {
00104   /* AirportTile do not have 'real' groups. */
00105   return NULL;
00106 }
00107 
00116 static uint32 GetNearbyAirportTileInformation(byte parameter, TileIndex tile, StationID index, bool grf_version8)
00117 {
00118   if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
00119   bool is_same_airport = (IsTileType(tile, MP_STATION) && IsAirport(tile) && GetStationIndex(tile) == index);
00120 
00121   return GetNearbyTileInformation(tile, grf_version8) | (is_same_airport ? 1 : 0) << 8;
00122 }
00123 
00124 
00133 static uint32 GetAirportTileIDAtOffset(TileIndex tile, const Station *st, uint32 cur_grfid)
00134 {
00135   if (!st->TileBelongsToAirport(tile)) {
00136     return 0xFFFF;
00137   }
00138 
00139   StationGfx gfx = GetAirportGfx(tile);
00140   const AirportTileSpec *ats = AirportTileSpec::Get(gfx);
00141 
00142   if (gfx < NEW_AIRPORTTILE_OFFSET) { // Does it belongs to an old type?
00143     /* It is an old tile.  We have to see if it's been overriden */
00144     if (ats->grf_prop.override == INVALID_AIRPORTTILE) { // has it been overridden?
00145       return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
00146     }
00147     /* Overriden */
00148     const AirportTileSpec *tile_ovr = AirportTileSpec::Get(ats->grf_prop.override);
00149 
00150     if (tile_ovr->grf_prop.grffile->grfid == cur_grfid) {
00151       return tile_ovr->grf_prop.local_id; // same grf file
00152     } else {
00153       return 0xFFFE; // not the same grf file
00154     }
00155   }
00156   /* Not an 'old type' tile */
00157   if (ats->grf_prop.spritegroup[0] != NULL) { // tile has a spritegroup ?
00158     if (ats->grf_prop.grffile->grfid == cur_grfid) { // same airport, same grf ?
00159       return ats->grf_prop.local_id;
00160     } else {
00161       return 0xFFFE; // Defined in another grf file
00162     }
00163   }
00164   /* The tile has no spritegroup */
00165   return 0xFF << 8 | ats->grf_prop.subst_id; // so just give him the substitute
00166 }
00167 
00168 static uint32 AirportTileGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00169 {
00170   const Station *st = object->u.airport.st;
00171   TileIndex tile    = object->u.airport.tile;
00172   assert(st != NULL);
00173 
00174   if (object->scope == VSG_SCOPE_PARENT) {
00175     DEBUG(grf, 1, "Parent scope for airport tiles unavailable");
00176     *available = false;
00177     return UINT_MAX;
00178   }
00179 
00180   extern uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile);
00181 
00182   switch (variable) {
00183     /* Terrain type */
00184     case 0x41: return GetTerrainType(tile);
00185 
00186     /* Current town zone of the tile in the nearest town */
00187     case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(tile, UINT_MAX), tile);
00188 
00189     /* Position relative to most northern airport tile. */
00190     case 0x43: return GetRelativePosition(tile, st->airport.tile);
00191 
00192     /* Animation frame of tile */
00193     case 0x44: return GetAnimationFrame(tile);
00194 
00195     /* Land info of nearby tiles */
00196     case 0x60: return GetNearbyAirportTileInformation(parameter, tile, st->index, object->grffile->grf_version >= 8);
00197 
00198     /* Animation stage of nearby tiles */
00199     case 0x61:
00200       tile = GetNearbyTile(parameter, tile);
00201       if (st->TileBelongsToAirport(tile)) {
00202         return GetAnimationFrame(tile);
00203       }
00204       return UINT_MAX;
00205 
00206     /* Get airport tile ID at offset */
00207     case 0x62: return GetAirportTileIDAtOffset(GetNearbyTile(parameter, tile), st, object->grffile->grfid);
00208   }
00209 
00210   DEBUG(grf, 1, "Unhandled airport tile variable 0x%X", variable);
00211 
00212   *available = false;
00213   return UINT_MAX;
00214 }
00215 
00216 static uint32 AirportTileGetRandomBits(const ResolverObject *object)
00217 {
00218   const Station *st = object->u.airport.st;
00219   const TileIndex tile = object->u.airport.tile;
00220   return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
00221 }
00222 
00223 static void AirportTileResolver(ResolverObject *res, const AirportTileSpec *ats, TileIndex tile, Station *st)
00224 {
00225   res->GetRandomBits = AirportTileGetRandomBits;
00226   res->GetTriggers   = NULL;
00227   res->SetTriggers   = NULL;
00228   res->GetVariable   = AirportTileGetVariable;
00229   res->ResolveReal   = AirportTileResolveReal;
00230   res->StorePSA      = NULL;
00231 
00232   assert(st != NULL);
00233   res->u.airport.airport_id = st->airport.type;
00234   res->u.airport.st         = st;
00235   res->u.airport.tile       = tile;
00236 
00237   res->callback        = CBID_NO_CALLBACK;
00238   res->callback_param1 = 0;
00239   res->callback_param2 = 0;
00240   res->ResetState();
00241 
00242   res->grffile         = ats->grf_prop.grffile;
00243 }
00244 
00245 uint16 GetAirportTileCallback(CallbackID callback, uint32 param1, uint32 param2, const AirportTileSpec *ats, Station *st, TileIndex tile, int extra_data = 0)
00246 {
00247   ResolverObject object;
00248   const SpriteGroup *group;
00249 
00250   AirportTileResolver(&object, ats, tile, st);
00251   object.callback = callback;
00252   object.callback_param1 = param1;
00253   object.callback_param2 = param2;
00254 
00255   group = SpriteGroup::Resolve(ats->grf_prop.spritegroup[0], &object);
00256   if (group == NULL) return CALLBACK_FAILED;
00257 
00258   return group->GetCallbackResult();
00259 }
00260 
00261 static void AirportDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte colour, StationGfx gfx)
00262 {
00263   const DrawTileSprites *dts = group->ProcessRegisters(NULL);
00264 
00265   SpriteID image = dts->ground.sprite;
00266   SpriteID pal   = dts->ground.pal;
00267 
00268   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00269     if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
00270       DrawWaterClassGround(ti);
00271     } else {
00272       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(colour)));
00273     }
00274   }
00275 
00276   DrawNewGRFTileSeq(ti, dts, TO_BUILDINGS, 0, GENERAL_SPRITE_COLOUR(colour));
00277 }
00278 
00279 bool DrawNewAirportTile(TileInfo *ti, Station *st, StationGfx gfx, const AirportTileSpec *airts)
00280 {
00281   const SpriteGroup *group;
00282   ResolverObject object;
00283 
00284   if (ti->tileh != SLOPE_FLAT) {
00285     bool draw_old_one = true;
00286     if (HasBit(airts->callback_mask, CBM_AIRT_DRAW_FOUNDATIONS)) {
00287       /* Called to determine the type (if any) of foundation to draw */
00288       uint32 callback_res = GetAirportTileCallback(CBID_AIRPTILE_DRAW_FOUNDATIONS, 0, 0, airts, st, ti->tile);
00289       if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(airts->grf_prop.grffile, CBID_AIRPTILE_DRAW_FOUNDATIONS, callback_res);
00290     }
00291 
00292     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00293   }
00294 
00295   AirportTileResolver(&object, airts, ti->tile, st);
00296 
00297   group = SpriteGroup::Resolve(airts->grf_prop.spritegroup[0], &object);
00298   if (group == NULL || group->type != SGT_TILELAYOUT) {
00299     return false;
00300   }
00301 
00302   const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00303   AirportDrawTileLayout(ti, tlgroup, Company::Get(st->owner)->colour, gfx);
00304   return true;
00305 }
00306 
00308 struct AirportTileAnimationBase : public AnimationBase<AirportTileAnimationBase, AirportTileSpec, Station, int, GetAirportTileCallback> {
00309   static const CallbackID cb_animation_speed      = CBID_AIRPTILE_ANIMATION_SPEED;
00310   static const CallbackID cb_animation_next_frame = CBID_AIRPTILE_ANIM_NEXT_FRAME;
00311 
00312   static const AirportTileCallbackMask cbm_animation_speed      = CBM_AIRT_ANIM_SPEED;
00313   static const AirportTileCallbackMask cbm_animation_next_frame = CBM_AIRT_ANIM_NEXT_FRAME;
00314 };
00315 
00316 void AnimateAirportTile(TileIndex tile)
00317 {
00318   const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
00319   if (ats == NULL) return;
00320 
00321   AirportTileAnimationBase::AnimateTile(ats, Station::GetByTile(tile), tile, HasBit(ats->animation_special_flags, 0));
00322 }
00323 
00324 void AirportTileAnimationTrigger(Station *st, TileIndex tile, AirpAnimationTrigger trigger, CargoID cargo_type)
00325 {
00326   const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
00327   if (!HasBit(ats->animation.triggers, trigger)) return;
00328 
00329   AirportTileAnimationBase::ChangeAnimationFrame(CBID_AIRPTILE_ANIM_START_STOP, ats, st, tile, Random(), (uint8)trigger | (cargo_type << 8));
00330 }
00331 
00332 void AirportAnimationTrigger(Station *st, AirpAnimationTrigger trigger, CargoID cargo_type)
00333 {
00334   if (st->airport.tile == INVALID_TILE) return;
00335 
00336   TILE_AREA_LOOP(tile, st->airport) {
00337     if (st->TileBelongsToAirport(tile)) AirportTileAnimationTrigger(st, tile, trigger, cargo_type);
00338   }
00339 }
00340 
00346 void GetAirportTileTypeResolver(ResolverObject *ro, uint index)
00347 {
00348   AirportTileResolver(ro, AirportTileSpec::GetByTile(index), index, Station::GetByTile(index));
00349 }