newgrf_railtype.cpp

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