Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "newgrf.h"
00015 #include "newgrf_spritegroup.h"
00016 #include "date_func.h"
00017 #include "depot_base.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 }
00046 }
00047
00048 switch (variable) {
00049 case 0x40: return GetTerrainType(tile, object->u.routes.context);
00050 case 0x41: return 0;
00051 case 0x42: return IsLevelCrossingTile(tile) && IsCrossingBarred(tile);
00052 case 0x43:
00053 if (IsRailDepotTile(tile)) return Depot::GetByTile(tile)->build_date;
00054 return _date;
00055 }
00056
00057 DEBUG(grf, 1, "Unhandled rail type tile variable 0x%X", variable);
00058
00059 *available = false;
00060 return UINT_MAX;
00061 }
00062
00063 static const SpriteGroup *RailTypeResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00064 {
00065 if (group->num_loading > 0) return group->loading[0];
00066 if (group->num_loaded > 0) return group->loaded[0];
00067 return NULL;
00068 }
00069
00070 static inline void NewRailTypeResolver(ResolverObject *res, TileIndex tile, TileContext context, const GRFFile *grffile)
00071 {
00072 res->GetRandomBits = &RailTypeGetRandomBits;
00073 res->GetTriggers = &RailTypeGetTriggers;
00074 res->SetTriggers = &RailTypeSetTriggers;
00075 res->GetVariable = &RailTypeGetVariable;
00076 res->ResolveReal = &RailTypeResolveReal;
00077
00078 res->u.routes.tile = tile;
00079 res->u.routes.context = context;
00080
00081 res->callback = CBID_NO_CALLBACK;
00082 res->callback_param1 = 0;
00083 res->callback_param2 = 0;
00084 res->ResetState();
00085
00086 res->grffile = grffile;
00087 }
00088
00097 SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context)
00098 {
00099 assert(rtsg < RTSG_END);
00100
00101 if (rti->group[rtsg] == NULL) return 0;
00102
00103 const SpriteGroup *group;
00104 ResolverObject object;
00105
00106 NewRailTypeResolver(&object, tile, context, rti->grffile[rtsg]);
00107
00108 group = SpriteGroup::Resolve(rti->group[rtsg], &object);
00109 if (group == NULL || group->GetNumResults() == 0) return 0;
00110
00111 return group->GetResult();
00112 }
00113
00120 uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
00121 {
00122
00123 if (grffile == NULL || grffile->railtype_max == 0) return railtype;
00124
00125
00126 RailTypeLabel label = GetRailTypeInfo(railtype)->label;
00127 for (uint i = 0; i < grffile->railtype_max; i++) {
00128 if (label == grffile->railtype_list[i]) return i;
00129 }
00130
00131
00132 return 0xFF;
00133 }
00134
00140 void GetRailTypeResolver(ResolverObject *ro, uint index)
00141 {
00142
00143
00144 NewRailTypeResolver(ro, index, TCX_NORMAL, NULL);
00145 }