newgrf_airporttiles.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_airporttiles.cpp 24900 2013-01-08 22:46:42Z planetmaker $ */
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 
00109 static uint32 GetNearbyAirportTileInformation(byte parameter, TileIndex tile, StationID index, bool grf_version8)
00110 {
00111   if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
00112   bool is_same_airport = (IsTileType(tile, MP_STATION) && IsAirport(tile) && GetStationIndex(tile) == index);
00113 
00114   return GetNearbyTileInformation(tile, grf_version8) | (is_same_airport ? 1 : 0) << 8;
00115 }
00116 
00117 
00126 static uint32 GetAirportTileIDAtOffset(TileIndex tile, const Station *st, uint32 cur_grfid)
00127 {
00128   if (!st->TileBelongsToAirport(tile)) {
00129     return 0xFFFF;
00130   }
00131 
00132   StationGfx gfx = GetAirportGfx(tile);
00133   const AirportTileSpec *ats = AirportTileSpec::Get(gfx);
00134 
00135   if (gfx < NEW_AIRPORTTILE_OFFSET) { // Does it belongs to an old type?
00136     /* It is an old tile.  We have to see if it's been overridden */
00137     if (ats->grf_prop.override == INVALID_AIRPORTTILE) { // has it been overridden?
00138       return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
00139     }
00140     /* Overridden */
00141     const AirportTileSpec *tile_ovr = AirportTileSpec::Get(ats->grf_prop.override);
00142 
00143     if (tile_ovr->grf_prop.grffile->grfid == cur_grfid) {
00144       return tile_ovr->grf_prop.local_id; // same grf file
00145     } else {
00146       return 0xFFFE; // not the same grf file
00147     }
00148   }
00149   /* Not an 'old type' tile */
00150   if (ats->grf_prop.spritegroup[0] != NULL) { // tile has a spritegroup ?
00151     if (ats->grf_prop.grffile->grfid == cur_grfid) { // same airport, same grf ?
00152       return ats->grf_prop.local_id;
00153     } else {
00154       return 0xFFFE; // Defined in another grf file
00155     }
00156   }
00157   /* The tile has no spritegroup */
00158   return 0xFF << 8 | ats->grf_prop.subst_id; // so just give him the substitute
00159 }
00160 
00161 /* virtual */ uint32 AirportTileScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
00162 {
00163   assert(this->st != NULL);
00164 
00165   extern uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile);
00166 
00167   switch (variable) {
00168     /* Terrain type */
00169     case 0x41: return GetTerrainType(this->tile);
00170 
00171     /* Current town zone of the tile in the nearest town */
00172     case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(this->tile, UINT_MAX), this->tile);
00173 
00174     /* Position relative to most northern airport tile. */
00175     case 0x43: return GetRelativePosition(this->tile, this->st->airport.tile);
00176 
00177     /* Animation frame of tile */
00178     case 0x44: return GetAnimationFrame(this->tile);
00179 
00180     /* Land info of nearby tiles */
00181     case 0x60: return GetNearbyAirportTileInformation(parameter, this->tile, this->st->index, this->ro->grffile->grf_version >= 8);
00182 
00183     /* Animation stage of nearby tiles */
00184     case 0x61: {
00185       TileIndex tile = GetNearbyTile(parameter, this->tile);
00186       if (this->st->TileBelongsToAirport(tile)) {
00187         return GetAnimationFrame(tile);
00188       }
00189       return UINT_MAX;
00190     }
00191 
00192     /* Get airport tile ID at offset */
00193     case 0x62: return GetAirportTileIDAtOffset(GetNearbyTile(parameter, this->tile), this->st, this->ro->grffile->grfid);
00194   }
00195 
00196   DEBUG(grf, 1, "Unhandled airport tile variable 0x%X", variable);
00197 
00198   *available = false;
00199   return UINT_MAX;
00200 }
00201 
00202 /* virtual */ uint32 AirportTileScopeResolver::GetRandomBits() const
00203 {
00204   return (this->st == NULL ? 0 : this->st->random_bits) | (this->tile == INVALID_TILE ? 0 : GetStationTileRandomBits(this->tile) << 16);
00205 }
00206 
00216 AirportTileResolverObject::AirportTileResolverObject(const AirportTileSpec *ats, TileIndex tile, Station *st,
00217     CallbackID callback, uint32 callback_param1, uint32 callback_param2)
00218   : ResolverObject(ats->grf_prop.grffile, callback, callback_param1, callback_param2), tiles_scope(this, ats, tile, st)
00219 {
00220 }
00221 
00228 AirportTileScopeResolver::AirportTileScopeResolver(ResolverObject *ro, const AirportTileSpec *ats, TileIndex tile, Station *st) : ScopeResolver(ro)
00229 {
00230   assert(st != NULL);
00231 
00232   this->st = st;
00233   this->airport_id = st->airport.type;
00234   this->tile = tile;
00235 }
00236 
00237 uint16 GetAirportTileCallback(CallbackID callback, uint32 param1, uint32 param2, const AirportTileSpec *ats, Station *st, TileIndex tile, int extra_data = 0)
00238 {
00239   AirportTileResolverObject object(ats, tile, st, callback, param1, param2);
00240   const SpriteGroup *group = SpriteGroup::Resolve(ats->grf_prop.spritegroup[0], &object);
00241   if (group == NULL) return CALLBACK_FAILED;
00242 
00243   return group->GetCallbackResult();
00244 }
00245 
00246 static void AirportDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte colour, StationGfx gfx)
00247 {
00248   const DrawTileSprites *dts = group->ProcessRegisters(NULL);
00249 
00250   SpriteID image = dts->ground.sprite;
00251   SpriteID pal   = dts->ground.pal;
00252 
00253   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00254     if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
00255       DrawWaterClassGround(ti);
00256     } else {
00257       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(colour)));
00258     }
00259   }
00260 
00261   DrawNewGRFTileSeq(ti, dts, TO_BUILDINGS, 0, GENERAL_SPRITE_COLOUR(colour));
00262 }
00263 
00264 bool DrawNewAirportTile(TileInfo *ti, Station *st, StationGfx gfx, const AirportTileSpec *airts)
00265 {
00266   if (ti->tileh != SLOPE_FLAT) {
00267     bool draw_old_one = true;
00268     if (HasBit(airts->callback_mask, CBM_AIRT_DRAW_FOUNDATIONS)) {
00269       /* Called to determine the type (if any) of foundation to draw */
00270       uint32 callback_res = GetAirportTileCallback(CBID_AIRPTILE_DRAW_FOUNDATIONS, 0, 0, airts, st, ti->tile);
00271       if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(airts->grf_prop.grffile, CBID_AIRPTILE_DRAW_FOUNDATIONS, callback_res);
00272     }
00273 
00274     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00275   }
00276 
00277   AirportTileResolverObject object(airts, ti->tile, st);
00278   const SpriteGroup *group = SpriteGroup::Resolve(airts->grf_prop.spritegroup[0], &object);
00279   if (group == NULL || group->type != SGT_TILELAYOUT) {
00280     return false;
00281   }
00282 
00283   const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00284   AirportDrawTileLayout(ti, tlgroup, Company::Get(st->owner)->colour, gfx);
00285   return true;
00286 }
00287 
00289 struct AirportTileAnimationBase : public AnimationBase<AirportTileAnimationBase, AirportTileSpec, Station, int, GetAirportTileCallback> {
00290   static const CallbackID cb_animation_speed      = CBID_AIRPTILE_ANIMATION_SPEED;
00291   static const CallbackID cb_animation_next_frame = CBID_AIRPTILE_ANIM_NEXT_FRAME;
00292 
00293   static const AirportTileCallbackMask cbm_animation_speed      = CBM_AIRT_ANIM_SPEED;
00294   static const AirportTileCallbackMask cbm_animation_next_frame = CBM_AIRT_ANIM_NEXT_FRAME;
00295 };
00296 
00297 void AnimateAirportTile(TileIndex tile)
00298 {
00299   const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
00300   if (ats == NULL) return;
00301 
00302   AirportTileAnimationBase::AnimateTile(ats, Station::GetByTile(tile), tile, HasBit(ats->animation_special_flags, 0));
00303 }
00304 
00305 void AirportTileAnimationTrigger(Station *st, TileIndex tile, AirpAnimationTrigger trigger, CargoID cargo_type)
00306 {
00307   const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
00308   if (!HasBit(ats->animation.triggers, trigger)) return;
00309 
00310   AirportTileAnimationBase::ChangeAnimationFrame(CBID_AIRPTILE_ANIM_START_STOP, ats, st, tile, Random(), (uint8)trigger | (cargo_type << 8));
00311 }
00312 
00313 void AirportAnimationTrigger(Station *st, AirpAnimationTrigger trigger, CargoID cargo_type)
00314 {
00315   if (st->airport.tile == INVALID_TILE) return;
00316 
00317   TILE_AREA_LOOP(tile, st->airport) {
00318     if (st->TileBelongsToAirport(tile)) AirportTileAnimationTrigger(st, tile, trigger, cargo_type);
00319   }
00320 }
00321