2010-02-07 22:22:54 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2010-07-22 16:50:58 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-02-07 22:22:54 +00:00
|
|
|
/** @file newgrf_railtype.cpp NewGRF handling of rail types. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "newgrf.h"
|
|
|
|
#include "newgrf_spritegroup.h"
|
2010-06-20 19:13:02 +00:00
|
|
|
#include "date_func.h"
|
|
|
|
#include "depot_base.h"
|
2010-02-07 22:22:54 +00:00
|
|
|
|
|
|
|
static uint32 RailTypeGetRandomBits(const ResolverObject *object)
|
|
|
|
{
|
2010-02-25 09:43:00 +00:00
|
|
|
TileIndex tile = object->u.routes.tile;
|
|
|
|
uint tmp = CountBits(tile + (TileX(tile) + TileY(tile)) * TILE_SIZE);
|
|
|
|
return GB(tmp, 0, 2);
|
2010-02-07 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32 RailTypeGetTriggers(const ResolverObject *object)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RailTypeSetTriggers(const ResolverObject *object, int triggers)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-08 17:23:30 +00:00
|
|
|
static uint32 RailTypeGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
|
2010-02-07 22:22:54 +00:00
|
|
|
{
|
|
|
|
TileIndex tile = object->u.routes.tile;
|
|
|
|
|
|
|
|
if (tile == INVALID_TILE) {
|
|
|
|
switch (variable) {
|
|
|
|
case 0x40: return 0;
|
|
|
|
case 0x41: return 0;
|
|
|
|
case 0x42: return 0;
|
2010-06-20 19:13:02 +00:00
|
|
|
case 0x43: return _date;
|
2010-02-07 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (variable) {
|
2010-08-09 07:10:42 +00:00
|
|
|
case 0x40: return GetTerrainType(tile, object->u.routes.context);
|
2010-02-07 22:22:54 +00:00
|
|
|
case 0x41: return 0;
|
|
|
|
case 0x42: return IsLevelCrossingTile(tile) && IsCrossingBarred(tile);
|
2010-06-20 19:13:02 +00:00
|
|
|
case 0x43:
|
|
|
|
if (IsRailDepotTile(tile)) return Depot::GetByTile(tile)->build_date;
|
|
|
|
return _date;
|
2010-02-07 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 16:43:46 +00:00
|
|
|
DEBUG(grf, 1, "Unhandled rail type tile variable 0x%X", variable);
|
2010-02-07 22:22:54 +00:00
|
|
|
|
|
|
|
*available = false;
|
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const SpriteGroup *RailTypeResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
|
|
|
|
{
|
|
|
|
if (group->num_loading > 0) return group->loading[0];
|
|
|
|
if (group->num_loaded > 0) return group->loaded[0];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-04 20:25:17 +00:00
|
|
|
static inline void NewRailTypeResolver(ResolverObject *res, TileIndex tile, TileContext context, const GRFFile *grffile)
|
2010-02-07 22:22:54 +00:00
|
|
|
{
|
|
|
|
res->GetRandomBits = &RailTypeGetRandomBits;
|
|
|
|
res->GetTriggers = &RailTypeGetTriggers;
|
|
|
|
res->SetTriggers = &RailTypeSetTriggers;
|
|
|
|
res->GetVariable = &RailTypeGetVariable;
|
|
|
|
res->ResolveReal = &RailTypeResolveReal;
|
|
|
|
|
|
|
|
res->u.routes.tile = tile;
|
2010-08-09 07:10:42 +00:00
|
|
|
res->u.routes.context = context;
|
2010-02-07 22:22:54 +00:00
|
|
|
|
|
|
|
res->callback = CBID_NO_CALLBACK;
|
|
|
|
res->callback_param1 = 0;
|
|
|
|
res->callback_param2 = 0;
|
2011-07-04 20:31:57 +00:00
|
|
|
res->ResetState();
|
2011-07-04 20:25:17 +00:00
|
|
|
|
|
|
|
res->grffile = grffile;
|
2010-02-07 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2011-05-02 16:14:23 +00:00
|
|
|
/**
|
|
|
|
* Get the sprite to draw for the given tile.
|
|
|
|
* @param rti The rail type data (spec).
|
|
|
|
* @param tile The tile to get the sprite for.
|
|
|
|
* @param rtsg The type of sprite to draw.
|
|
|
|
* @param content Where are we drawing the tile?
|
|
|
|
* @return The sprite to draw.
|
|
|
|
*/
|
2010-08-09 07:10:42 +00:00
|
|
|
SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context)
|
2010-02-07 22:22:54 +00:00
|
|
|
{
|
|
|
|
assert(rtsg < RTSG_END);
|
|
|
|
|
|
|
|
if (rti->group[rtsg] == NULL) return 0;
|
|
|
|
|
|
|
|
const SpriteGroup *group;
|
|
|
|
ResolverObject object;
|
|
|
|
|
2011-07-04 20:25:17 +00:00
|
|
|
NewRailTypeResolver(&object, tile, context, rti->grffile[rtsg]);
|
2010-02-07 22:22:54 +00:00
|
|
|
|
|
|
|
group = SpriteGroup::Resolve(rti->group[rtsg], &object);
|
|
|
|
if (group == NULL || group->GetNumResults() == 0) return 0;
|
|
|
|
|
|
|
|
return group->GetResult();
|
|
|
|
}
|
|
|
|
|
2011-05-02 16:14:23 +00:00
|
|
|
/**
|
|
|
|
* Perform a reverse railtype lookup to get the GRF internal ID.
|
|
|
|
* @param railtype The global (OpenTTD) railtype.
|
|
|
|
* @param grffile The GRF to do the lookup for.
|
|
|
|
* @return the GRF internal ID.
|
|
|
|
*/
|
2010-02-07 22:22:54 +00:00
|
|
|
uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
|
|
|
|
{
|
|
|
|
/* No rail type table present, return rail type as-is */
|
2010-07-16 19:02:59 +00:00
|
|
|
if (grffile == NULL || grffile->railtype_max == 0) return railtype;
|
2010-02-07 22:22:54 +00:00
|
|
|
|
|
|
|
/* Look for a matching rail type label in the table */
|
|
|
|
RailTypeLabel label = GetRailTypeInfo(railtype)->label;
|
|
|
|
for (uint i = 0; i < grffile->railtype_max; i++) {
|
|
|
|
if (label == grffile->railtype_list[i]) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If not found, return as invalid */
|
|
|
|
return 0xFF;
|
|
|
|
}
|
2010-04-24 13:35:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a railtypes's spec and such so we can get a variable.
|
|
|
|
* @param ro The resolver object to fill.
|
|
|
|
* @param index The rail tile to get the data from.
|
|
|
|
*/
|
|
|
|
void GetRailTypeResolver(ResolverObject *ro, uint index)
|
|
|
|
{
|
2011-07-04 20:25:17 +00:00
|
|
|
/* There is no unique GRFFile for the tile. Multiple GRFs can define different parts of the railtype.
|
|
|
|
* However, currently the NewGRF Debug GUI does not display variables depending on the GRF (like 0x7F) anyway. */
|
|
|
|
NewRailTypeResolver(ro, index, TCX_NORMAL, NULL);
|
2010-04-24 13:35:18 +00:00
|
|
|
}
|