newgrf_railtype.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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_spritegroup.h"
00015 #include "date_func.h"
00016 #include "depot_base.h"
00017 #include "town.h"
00018 
00019 static uint32 RailTypeGetRandomBits(const ResolverObject *object)
00020 {
00021   TileIndex tile = object->u.routes.tile;
00022   uint tmp = CountBits(tile + (TileX(tile) + TileY(tile)) * TILE_SIZE);
00023   return GB(tmp, 0, 2);
00024 }
00025 
00026 static uint32 RailTypeGetTriggers(const ResolverObject *object)
00027 {
00028   return 0;
00029 }
00030 
00031 static void RailTypeSetTriggers(const ResolverObject *object, int triggers)
00032 {
00033 }
00034 
00035 static uint32 RailTypeGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00036 {
00037   TileIndex tile = object->u.routes.tile;
00038 
00039   if (tile == INVALID_TILE) {
00040     switch (variable) {
00041       case 0x40: return 0;
00042       case 0x41: return 0;
00043       case 0x42: return 0;
00044       case 0x43: return _date;
00045       case 0x44: return HZB_TOWN_EDGE;
00046     }
00047   }
00048 
00049   switch (variable) {
00050     case 0x40: return GetTerrainType(tile, object->u.routes.context);
00051     case 0x41: return 0;
00052     case 0x42: return IsLevelCrossingTile(tile) && IsCrossingBarred(tile);
00053     case 0x43:
00054       if (IsRailDepotTile(tile)) return Depot::GetByTile(tile)->build_date;
00055       return _date;
00056     case 0x44: {
00057       const Town *t = NULL;
00058       if (IsRailDepotTile(tile)) {
00059         t = Depot::GetByTile(tile)->town;
00060       } else if (IsLevelCrossingTile(tile)) {
00061         t = ClosestTownFromTile(tile, UINT_MAX);
00062       }
00063       return t != NULL ? GetTownRadiusGroup(t, tile) : HZB_TOWN_EDGE;
00064     }
00065   }
00066 
00067   DEBUG(grf, 1, "Unhandled rail type tile variable 0x%X", variable);
00068 
00069   *available = false;
00070   return UINT_MAX;
00071 }
00072 
00073 static const SpriteGroup *RailTypeResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00074 {
00075   if (group->num_loading > 0) return group->loading[0];
00076   if (group->num_loaded  > 0) return group->loaded[0];
00077   return NULL;
00078 }
00079 
00080 static inline void NewRailTypeResolver(ResolverObject *res, TileIndex tile, TileContext context, const GRFFile *grffile)
00081 {
00082   res->GetRandomBits = &RailTypeGetRandomBits;
00083   res->GetTriggers   = &RailTypeGetTriggers;
00084   res->SetTriggers   = &RailTypeSetTriggers;
00085   res->GetVariable   = &RailTypeGetVariable;
00086   res->ResolveReal   = &RailTypeResolveReal;
00087 
00088   res->u.routes.tile = tile;
00089   res->u.routes.context = context;
00090 
00091   res->callback        = CBID_NO_CALLBACK;
00092   res->callback_param1 = 0;
00093   res->callback_param2 = 0;
00094   res->ResetState();
00095 
00096   res->grffile         = grffile;
00097 }
00098 
00107 SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context)
00108 {
00109   assert(rtsg < RTSG_END);
00110 
00111   if (rti->group[rtsg] == NULL) return 0;
00112 
00113   const SpriteGroup *group;
00114   ResolverObject object;
00115 
00116   NewRailTypeResolver(&object, tile, context, rti->grffile[rtsg]);
00117 
00118   group = SpriteGroup::Resolve(rti->group[rtsg], &object);
00119   if (group == NULL || group->GetNumResults() == 0) return 0;
00120 
00121   return group->GetResult();
00122 }
00123 
00130 uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
00131 {
00132   /* No rail type table present, return rail type as-is */
00133   if (grffile == NULL || grffile->railtype_list.Length() == 0) return railtype;
00134 
00135   /* Look for a matching rail type label in the table */
00136   RailTypeLabel label = GetRailTypeInfo(railtype)->label;
00137   int index = grffile->railtype_list.FindIndex(label);
00138   if (index >= 0) return index;
00139 
00140   /* If not found, return as invalid */
00141   return 0xFF;
00142 }
00143 
00149 void GetRailTypeResolver(ResolverObject *ro, uint index)
00150 {
00151   /* There is no unique GRFFile for the tile. Multiple GRFs can define different parts of the railtype.
00152    * However, currently the NewGRF Debug GUI does not display variables depending on the GRF (like 0x7F) anyway. */
00153   NewRailTypeResolver(ro, index, TCX_NORMAL, NULL);
00154 }