# newgrf_railtype.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_railtype.cpp 19258 2010-02-25 21:18:38Z rubidium $ */
    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 #include "core/bitmath_func.hpp"
    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, byte 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     }
    00045   }
    00046
    00047   switch (variable) {
    00048     case 0x40: return GetTerrainType(tile);
    00049     case 0x41: return 0;
    00050     case 0x42: return IsLevelCrossingTile(tile) && IsCrossingBarred(tile);
    00051   }
    00052
    00053   DEBUG(grf, 1, "Unhandled rail type tile property 0x%X", variable);
    00054
    00055   *available = false;
    00056   return UINT_MAX;
    00057 }
    00058
    00059 static const SpriteGroup *RailTypeResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
    00060 {
    00061   if (group->num_loading > 0) return group->loading[0];
    00062   if (group->num_loaded  > 0) return group->loaded[0];
    00063   return NULL;
    00064 }
    00065
    00066 static inline void NewRailTypeResolver(ResolverObject *res, TileIndex tile)
    00067 {
    00068   res->GetRandomBits = &RailTypeGetRandomBits;
    00069   res->GetTriggers   = &RailTypeGetTriggers;
    00070   res->SetTriggers   = &RailTypeSetTriggers;
    00071   res->GetVariable   = &RailTypeGetVariable;
    00072   res->ResolveReal   = &RailTypeResolveReal;
    00073
    00074   res->u.routes.tile = tile;
    00075
    00076   res->callback        = CBID_NO_CALLBACK;
    00077   res->callback_param1 = 0;
    00078   res->callback_param2 = 0;
    00079   res->last_value      = 0;
    00080   res->trigger         = 0;
    00081   res->reseed          = 0;
    00082   res->count           = 0;
    00083 }
    00084
    00085 SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg)
    00086 {
    00087   assert(rtsg < RTSG_END);
    00088
    00089   if (rti->group[rtsg] == NULL) return 0;
    00090
    00091   const SpriteGroup *group;
    00092   ResolverObject object;
    00093
    00094   NewRailTypeResolver(&object, tile);
    00095
    00096   group = SpriteGroup::Resolve(rti->group[rtsg], &object);
    00097   if (group == NULL || group->GetNumResults() == 0) return 0;
    00098
    00099   return group->GetResult();
    00100 }
    00101
    00102 uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
    00103 {
    00104   /* No rail type table present, return rail type as-is */
    00105   if (grffile->railtype_max == 0) return railtype;
    00106
    00107   /* Look for a matching rail type label in the table */
    00108   RailTypeLabel label = GetRailTypeInfo(railtype)->label;
    00109   for (uint i = 0; i < grffile->railtype_max; i++) {
    00110     if (label == grffile->railtype_list[i]) return i;
    00111   }
    00112
    00113   /* If not found, return as invalid */
    00114   return 0xFF;
    00115 }