newgrf_airporttiles.cpp

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