# newgrf_railtype.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_railtype.cpp 19056 2010-02-07 22:22:54Z peter1138 $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "variables.h"
    00008 #include "debug.h"
    00009 #include "strings_type.h"
    00010 #include "rail.h"
    00011 #include "road_map.h"
    00012 #include "newgrf.h"
    00013 #include "newgrf_callbacks.h"
    00014 #include "newgrf_commons.h"
    00015 #include "newgrf_railtype.h"
    00016 #include "newgrf_spritegroup.h"
    00017
    00018 static uint32 RailTypeGetRandomBits(const ResolverObject *object)
    00019 {
    00020   return 0;
    00021 }
    00022
    00023 static uint32 RailTypeGetTriggers(const ResolverObject *object)
    00024 {
    00025   return 0;
    00026 }
    00027
    00028 static void RailTypeSetTriggers(const ResolverObject *object, int triggers)
    00029 {
    00030 }
    00031
    00032 static uint32 RailTypeGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
    00033 {
    00034   TileIndex tile = object->u.routes.tile;
    00035
    00036   if (tile == INVALID_TILE) {
    00037     switch (variable) {
    00038       case 0x40: return 0;
    00039       case 0x41: return 0;
    00040       case 0x42: return 0;
    00041     }
    00042   }
    00043
    00044   switch (variable) {
    00045     case 0x40: return GetTerrainType(tile);
    00046     case 0x41: return 0;
    00047     case 0x42: return IsLevelCrossingTile(tile) && IsCrossingBarred(tile);
    00048   }
    00049
    00050   DEBUG(grf, 1, "Unhandled rail type tile property 0x%X", variable);
    00051
    00052   *available = false;
    00053   return UINT_MAX;
    00054 }
    00055
    00056 static const SpriteGroup *RailTypeResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
    00057 {
    00058   if (group->num_loading > 0) return group->loading[0];
    00059   if (group->num_loaded  > 0) return group->loaded[0];
    00060   return NULL;
    00061 }
    00062
    00063 static inline void NewRailTypeResolver(ResolverObject *res, TileIndex tile)
    00064 {
    00065   res->GetRandomBits = &RailTypeGetRandomBits;
    00066   res->GetTriggers   = &RailTypeGetTriggers;
    00067   res->SetTriggers   = &RailTypeSetTriggers;
    00068   res->GetVariable   = &RailTypeGetVariable;
    00069   res->ResolveReal   = &RailTypeResolveReal;
    00070
    00071   res->u.routes.tile = tile;
    00072
    00073   res->callback        = CBID_NO_CALLBACK;
    00074   res->callback_param1 = 0;
    00075   res->callback_param2 = 0;
    00076   res->last_value      = 0;
    00077   res->trigger         = 0;
    00078   res->reseed          = 0;
    00079   res->count           = 0;
    00080 }
    00081
    00082 SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg)
    00083 {
    00084   assert(rtsg < RTSG_END);
    00085
    00086   if (rti->group[rtsg] == NULL) return 0;
    00087
    00088   const SpriteGroup *group;
    00089   ResolverObject object;
    00090
    00091   NewRailTypeResolver(&object, tile);
    00092
    00093   group = SpriteGroup::Resolve(rti->group[rtsg], &object);
    00094   if (group == NULL || group->GetNumResults() == 0) return 0;
    00095
    00096   return group->GetResult();
    00097 }
    00098
    00099 uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
    00100 {
    00101   /* No rail type table present, return rail type as-is */
    00102   if (grffile->railtype_max == 0) return railtype;
    00103
    00104   /* Look for a matching rail type label in the table */
    00105   RailTypeLabel label = GetRailTypeInfo(railtype)->label;
    00106   for (uint i = 0; i < grffile->railtype_max; i++) {
    00107     if (label == grffile->railtype_list[i]) return i;
    00108   }
    00109
    00110   /* If not found, return as invalid */
    00111   return 0xFF;
    00112 }