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"
|
2012-11-10 20:41:45 +00:00
|
|
|
#include "newgrf_railtype.h"
|
2010-06-20 19:13:02 +00:00
|
|
|
#include "date_func.h"
|
|
|
|
#include "depot_base.h"
|
2012-01-28 19:36:22 +00:00
|
|
|
#include "town.h"
|
2010-02-07 22:22:54 +00:00
|
|
|
|
2012-11-10 20:41:45 +00:00
|
|
|
/* virtual */ uint32 RailTypeScopeResolver::GetRandomBits() const
|
2010-02-07 22:22:54 +00:00
|
|
|
{
|
2012-11-10 20:41:45 +00:00
|
|
|
uint tmp = CountBits(this->tile + (TileX(this->tile) + TileY(this->tile)) * TILE_SIZE);
|
2010-02-25 09:43:00 +00:00
|
|
|
return GB(tmp, 0, 2);
|
2010-02-07 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 20:41:45 +00:00
|
|
|
/* virtual */ uint32 RailTypeScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
|
2010-02-07 22:22:54 +00:00
|
|
|
{
|
2012-11-10 20:41:45 +00:00
|
|
|
if (this->tile == INVALID_TILE) {
|
2010-02-07 22:22:54 +00:00
|
|
|
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;
|
2012-01-28 19:36:22 +00:00
|
|
|
case 0x44: return HZB_TOWN_EDGE;
|
2010-02-07 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (variable) {
|
2012-11-10 20:41:45 +00:00
|
|
|
case 0x40: return GetTerrainType(this->tile, this->context);
|
2010-02-07 22:22:54 +00:00
|
|
|
case 0x41: return 0;
|
2012-11-10 20:41:45 +00:00
|
|
|
case 0x42: return IsLevelCrossingTile(this->tile) && IsCrossingBarred(this->tile);
|
2010-06-20 19:13:02 +00:00
|
|
|
case 0x43:
|
2012-11-10 20:41:45 +00:00
|
|
|
if (IsRailDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date;
|
2010-06-20 19:13:02 +00:00
|
|
|
return _date;
|
2012-01-28 19:36:22 +00:00
|
|
|
case 0x44: {
|
|
|
|
const Town *t = NULL;
|
2012-11-10 20:41:45 +00:00
|
|
|
if (IsRailDepotTile(this->tile)) {
|
|
|
|
t = Depot::GetByTile(this->tile)->town;
|
|
|
|
} else if (IsLevelCrossingTile(this->tile)) {
|
|
|
|
t = ClosestTownFromTile(this->tile, UINT_MAX);
|
2012-01-28 19:36:22 +00:00
|
|
|
}
|
2012-11-10 20:41:45 +00:00
|
|
|
return t != NULL ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
|
2012-01-28 19:36:22 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:41:45 +00:00
|
|
|
/* virtual */ const SpriteGroup *RailTypeResolverObject::ResolveReal(const RealSpriteGroup *group) const
|
2010-02-07 22:22:54 +00:00
|
|
|
{
|
|
|
|
if (group->num_loading > 0) return group->loading[0];
|
|
|
|
if (group->num_loaded > 0) return group->loaded[0];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/**
|
|
|
|
* Constructor of the railtype scope resolvers.
|
|
|
|
* @param ro Surrounding resolver.
|
|
|
|
* @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
|
|
|
|
* @param context Are we resolving sprites for the upper halftile, or on a bridge?
|
|
|
|
*/
|
2012-11-10 20:41:45 +00:00
|
|
|
RailTypeScopeResolver::RailTypeScopeResolver(ResolverObject *ro, TileIndex tile, TileContext context) : ScopeResolver(ro)
|
2010-02-07 22:22:54 +00:00
|
|
|
{
|
2012-11-10 20:41:45 +00:00
|
|
|
this->tile = tile;
|
|
|
|
this->context = context;
|
|
|
|
}
|
2011-07-04 20:25:17 +00:00
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/**
|
|
|
|
* Resolver object for rail types.
|
|
|
|
* @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
|
|
|
|
* @param context Are we resolving sprites for the upper halftile, or on a bridge?
|
|
|
|
* @param grffile The GRF to do the lookup for.
|
|
|
|
* @param param1 Extra parameter (first parameter of the callback, except railtypes do not have callbacks).
|
|
|
|
* @param param2 Extra parameter (second parameter of the callback, except railtypes do not have callbacks).
|
|
|
|
*/
|
2012-11-10 20:41:45 +00:00
|
|
|
RailTypeResolverObject::RailTypeResolverObject(TileIndex tile, TileContext context, const GRFFile *grffile, uint32 param1, uint32 param2)
|
|
|
|
: ResolverObject(grffile, CBID_NO_CALLBACK, param1, param2), railtype_scope(this, tile, context)
|
|
|
|
{
|
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;
|
|
|
|
|
2012-11-10 20:41:45 +00:00
|
|
|
RailTypeResolverObject object(tile, context, rti->grffile[rtsg]);
|
|
|
|
const SpriteGroup *group = SpriteGroup::Resolve(rti->group[rtsg], &object);
|
2010-02-07 22:22:54 +00:00
|
|
|
if (group == NULL || group->GetNumResults() == 0) return 0;
|
|
|
|
|
|
|
|
return group->GetResult();
|
|
|
|
}
|
|
|
|
|
2012-07-01 23:12:50 +00:00
|
|
|
/**
|
|
|
|
* Get the sprite to draw for a given signal.
|
|
|
|
* @param rti The rail type data (spec).
|
|
|
|
* @param tile The tile to get the sprite for.
|
|
|
|
* @param type Signal type.
|
|
|
|
* @param var Signal variant.
|
|
|
|
* @param state Signal state.
|
|
|
|
* @param gui Is the sprite being used on the map or in the GUI?
|
|
|
|
* @return The sprite to draw.
|
|
|
|
*/
|
|
|
|
SpriteID GetCustomSignalSprite(const RailtypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, SignalState state, bool gui)
|
|
|
|
{
|
|
|
|
if (rti->group[RTSG_SIGNALS] == NULL) return 0;
|
|
|
|
|
|
|
|
uint32 param1 = gui ? 0x10 : 0x00;
|
|
|
|
uint32 param2 = (type << 16) | (var << 8) | state;
|
2012-11-10 20:41:45 +00:00
|
|
|
RailTypeResolverObject object(tile, TCX_NORMAL, rti->grffile[RTSG_SIGNALS], param1, param2);
|
2012-07-01 23:12:50 +00:00
|
|
|
|
|
|
|
const SpriteGroup *group = SpriteGroup::Resolve(rti->group[RTSG_SIGNALS], &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 */
|
2012-05-25 20:57:36 +00:00
|
|
|
if (grffile == NULL || grffile->railtype_list.Length() == 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;
|
2012-05-25 20:57:36 +00:00
|
|
|
int index = grffile->railtype_list.FindIndex(label);
|
|
|
|
if (index >= 0) return index;
|
2010-02-07 22:22:54 +00:00
|
|
|
|
|
|
|
/* If not found, return as invalid */
|
|
|
|
return 0xFF;
|
|
|
|
}
|