newgrf_airporttiles.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_airporttiles.cpp 21198 2010-11-15 16:43:46Z 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.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 
00117 uint32 GetNearbyAirportTileInformation(byte parameter, TileIndex tile, StationID index)
00118 {
00119   if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
00120   bool is_same_airport = (IsTileType(tile, MP_STATION) && IsAirport(tile) && GetStationIndex(tile) == index);
00121 
00122   return GetNearbyTileInformation(tile) | (is_same_airport ? 1 : 0) << 8;
00123 }
00124 
00125 
00134 static uint32 GetAirportTileIDAtOffset(TileIndex tile, const Station *st, uint32 cur_grfid)
00135 {
00136   if (!st->TileBelongsToAirport(tile)) {
00137     return 0xFFFF;
00138   }
00139 
00140   StationGfx gfx = GetAirportGfx(tile);
00141   const AirportTileSpec *ats = AirportTileSpec::Get(gfx);
00142 
00143   if (gfx < NEW_AIRPORTTILE_OFFSET) { // Does it belongs to an old type?
00144     /* It is an old tile.  We have to see if it's been overriden */
00145     if (ats->grf_prop.override == INVALID_AIRPORTTILE) { // has it been overridden?
00146       return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
00147     }
00148     /* Overriden */
00149     const AirportTileSpec *tile_ovr = AirportTileSpec::Get(ats->grf_prop.override);
00150 
00151     if (tile_ovr->grf_prop.grffile->grfid == cur_grfid) {
00152       return tile_ovr->grf_prop.local_id; // same grf file
00153     } else {
00154       return 0xFFFE; // not the same grf file
00155     }
00156   }
00157   /* Not an 'old type' tile */
00158   if (ats->grf_prop.spritegroup[0] != NULL) { // tile has a spritegroup ?
00159     if (ats->grf_prop.grffile->grfid == cur_grfid) { // same airport, same grf ?
00160       return ats->grf_prop.local_id;
00161     } else {
00162       return 0xFFFE; // Defined in another grf file
00163     }
00164   }
00165   /* The tile has no spritegroup */
00166   return 0xFF << 8 | ats->grf_prop.subst_id; // so just give him the substitute
00167 }
00168 
00169 static uint32 AirportTileGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00170 {
00171   const Station *st = object->u.airport.st;
00172   TileIndex tile    = object->u.airport.tile;
00173   assert(st != NULL);
00174 
00175   if (object->scope == VSG_SCOPE_PARENT) {
00176     DEBUG(grf, 1, "Parent scope for airport tiles unavailable");
00177     *available = false;
00178     return UINT_MAX;
00179   }
00180 
00181   extern uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile);
00182 
00183   switch (variable) {
00184     /* Terrain type */
00185     case 0x41: return GetTerrainType(tile);
00186 
00187     /* Current town zone of the tile in the nearest town */
00188     case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(tile, UINT_MAX), tile);
00189 
00190     /* Position relative to most northern airport tile. */
00191     case 0x43: return GetRelativePosition(tile, st->airport.tile);
00192 
00193     /* Animation frame of tile */
00194     case 0x44: return GetAnimationFrame(tile);
00195 
00196     /* Land info of nearby tiles */
00197     case 0x60: return GetNearbyAirportTileInformation(parameter, tile, st->index);
00198 
00199     /* Animation stage of nearby tiles */
00200     case 0x61:
00201       tile = GetNearbyTile(parameter, tile);
00202       if (st->TileBelongsToAirport(tile)) {
00203         return GetAnimationFrame(tile);
00204       }
00205       return UINT_MAX;
00206 
00207     /* Get airport tile ID at offset */
00208     case 0x62: return GetAirportTileIDAtOffset(GetNearbyTile(parameter, tile), st, object->grffile->grfid);
00209   }
00210 
00211   DEBUG(grf, 1, "Unhandled airport tile variable 0x%X", variable);
00212 
00213   *available = false;
00214   return UINT_MAX;
00215 }
00216 
00217 static uint32 AirportTileGetRandomBits(const ResolverObject *object)
00218 {
00219   const Station *st = object->u.airport.st;
00220   const TileIndex tile = object->u.airport.tile;
00221   return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
00222 }
00223 
00224 static void AirportTileResolver(ResolverObject *res, const AirportTileSpec *ats, TileIndex tile, const Station *st)
00225 {
00226   res->GetRandomBits = AirportTileGetRandomBits;
00227   res->GetTriggers   = NULL;
00228   res->SetTriggers   = NULL;
00229   res->GetVariable   = AirportTileGetVariable;
00230   res->ResolveReal   = AirportTileResolveReal;
00231 
00232   assert(st != NULL);
00233   res->psa                  = NULL;
00234   res->u.airport.airport_id = st->airport.type;
00235   res->u.airport.st         = st;
00236   res->u.airport.tile       = tile;
00237 
00238   res->callback        = CBID_NO_CALLBACK;
00239   res->callback_param1 = 0;
00240   res->callback_param2 = 0;
00241   res->last_value      = 0;
00242   res->trigger         = 0;
00243   res->reseed          = 0;
00244   res->count           = 0;
00245 
00246   res->grffile         = ats->grf_prop.grffile;
00247 }
00248 
00249 uint16 GetAirportTileCallback(CallbackID callback, uint32 param1, uint32 param2, const AirportTileSpec *ats, const Station *st, TileIndex tile)
00250 {
00251   ResolverObject object;
00252   const SpriteGroup *group;
00253 
00254   AirportTileResolver(&object, ats, tile, st);
00255   object.callback = callback;
00256   object.callback_param1 = param1;
00257   object.callback_param2 = param2;
00258 
00259   group = SpriteGroup::Resolve(ats->grf_prop.spritegroup[0], &object);
00260   if (group == NULL) return CALLBACK_FAILED;
00261 
00262   return group->GetCallbackResult();
00263 }
00264 
00265 static void AirportDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte colour, StationGfx gfx)
00266 {
00267   const DrawTileSprites *dts = group->dts;
00268 
00269   SpriteID image = dts->ground.sprite;
00270   SpriteID pal   = dts->ground.pal;
00271 
00272   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00273     if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
00274       DrawWaterClassGround(ti);
00275     } else {
00276       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(colour)));
00277     }
00278   }
00279 
00280   DrawNewGRFTileSeq(ti, dts, TO_BUILDINGS, 0, GENERAL_SPRITE_COLOUR(colour));
00281 }
00282 
00283 bool DrawNewAirportTile(TileInfo *ti, Station *st, StationGfx gfx, const AirportTileSpec *airts)
00284 {
00285   const SpriteGroup *group;
00286   ResolverObject object;
00287 
00288   if (ti->tileh != SLOPE_FLAT) {
00289     bool draw_old_one = true;
00290     if (HasBit(airts->callback_mask, CBM_AIRT_DRAW_FOUNDATIONS)) {
00291       /* Called to determine the type (if any) of foundation to draw */
00292       uint32 callback_res = GetAirportTileCallback(CBID_AIRPTILE_DRAW_FOUNDATIONS, 0, 0, airts, st, ti->tile);
00293       draw_old_one = (callback_res != 0);
00294     }
00295 
00296     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00297   }
00298 
00299   AirportTileResolver(&object, airts, ti->tile, st);
00300 
00301   group = SpriteGroup::Resolve(airts->grf_prop.spritegroup[0], &object);
00302   if (group == NULL || group->type != SGT_TILELAYOUT) {
00303     return false;
00304   }
00305 
00306   const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00307   AirportDrawTileLayout(ti, tlgroup, Company::Get(st->owner)->colour, gfx);
00308   return true;
00309 }
00310 
00312 struct AirportTileAnimationBase : public AnimationBase<AirportTileAnimationBase, AirportTileSpec, Station, GetAirportTileCallback> {
00313   static const CallbackID cb_animation_speed      = CBID_AIRPTILE_ANIMATION_SPEED;
00314   static const CallbackID cb_animation_next_frame = CBID_AIRPTILE_ANIM_NEXT_FRAME;
00315 
00316   static const AirportTileCallbackMask cbm_animation_speed      = CBM_AIRT_ANIM_SPEED;
00317   static const AirportTileCallbackMask cbm_animation_next_frame = CBM_AIRT_ANIM_NEXT_FRAME;
00318 };
00319 
00320 void AnimateAirportTile(TileIndex tile)
00321 {
00322   const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
00323   if (ats == NULL) return;
00324 
00325   AirportTileAnimationBase::AnimateTile(ats, Station::GetByTile(tile), tile, HasBit(ats->animation_special_flags, 0));
00326 }
00327 
00328 void AirportTileAnimationTrigger(Station *st, TileIndex tile, AirpAnimationTrigger trigger, CargoID cargo_type)
00329 {
00330   const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
00331   if (!HasBit(ats->animation.triggers, trigger)) return;
00332 
00333   AirportTileAnimationBase::ChangeAnimationFrame(CBID_AIRPTILE_ANIM_START_STOP, ats, st, tile, Random(), (uint8)trigger | (cargo_type << 8));
00334 }
00335 
00336 void AirportAnimationTrigger(Station *st, AirpAnimationTrigger trigger, CargoID cargo_type)
00337 {
00338   if (st->airport.tile == INVALID_TILE) return;
00339 
00340   TILE_AREA_LOOP(tile, st->airport) {
00341     if (st->TileBelongsToAirport(tile)) AirportTileAnimationTrigger(st, tile, trigger, cargo_type);
00342   }
00343 }
00344 
00350 void GetAirportTileTypeResolver(ResolverObject *ro, uint index)
00351 {
00352   AirportTileResolver(ro, AirportTileSpec::GetByTile(index), index, Station::GetByTile(index));
00353 }

Generated on Sun May 15 19:20:11 2011 for OpenTTD by  doxygen 1.6.1