Changeset - r19730:2c33854b291a
[Not reviewed]
master
0 5 0
alberth - 12 years ago 2012-11-10 20:41:45
alberth@openttd.org
(svn r24685) -Codechange: Add resolver classes for rail types.
5 files changed with 62 insertions and 67 deletions:
0 comments (0 inline, 0 general)
src/newgrf_debug_gui.cpp
Show inline comments
 
@@ -23,24 +23,25 @@
 
#include "industry.h"
 
#include "object_base.h"
 
#include "station_base.h"
 
#include "town.h"
 
#include "vehicle_base.h"
 

	
 
#include "newgrf_airporttiles.h"
 
#include "newgrf_debug.h"
 
#include "newgrf_object.h"
 
#include "newgrf_spritegroup.h"
 
#include "newgrf_station.h"
 
#include "newgrf_town.h"
 
#include "newgrf_railtype.h"
 

	
 
#include "widgets/newgrf_debug_widget.h"
 

	
 
#include "table/strings.h"
 

	
 
/** The sprite picker. */
 
NewGrfDebugSpritePicker _newgrf_debug_sprite_picker = { SPM_NONE, NULL, 0, SmallVector<SpriteID, 256>() };
 

	
 
/**
 
 * Get the feature index related to the window number.
 
 * @param window_number The window to get the feature index from.
 
 * @return the feature index
src/newgrf_railtype.cpp
Show inline comments
 
@@ -2,153 +2,127 @@
 

	
 
/*
 
 * 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/>.
 
 */
 

	
 
/** @file newgrf_railtype.cpp NewGRF handling of rail types. */
 

	
 
#include "stdafx.h"
 
#include "debug.h"
 
#include "newgrf_spritegroup.h"
 
#include "newgrf_railtype.h"
 
#include "date_func.h"
 
#include "depot_base.h"
 
#include "town.h"
 

	
 
static uint32 RailTypeGetRandomBits(const ResolverObject *object)
 
/* virtual */ uint32 RailTypeScopeResolver::GetRandomBits() const
 
{
 
	TileIndex tile = object->u.routes.tile;
 
	uint tmp = CountBits(tile + (TileX(tile) + TileY(tile)) * TILE_SIZE);
 
	uint tmp = CountBits(this->tile + (TileX(this->tile) + TileY(this->tile)) * TILE_SIZE);
 
	return GB(tmp, 0, 2);
 
}
 

	
 
static uint32 RailTypeGetTriggers(const ResolverObject *object)
 
{
 
	return 0;
 
}
 

	
 
static void RailTypeSetTriggers(const ResolverObject *object, int triggers)
 
/* virtual */ uint32 RailTypeScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
 
{
 
}
 

	
 
static uint32 RailTypeGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
 
{
 
	TileIndex tile = object->u.routes.tile;
 

	
 
	if (tile == INVALID_TILE) {
 
	if (this->tile == INVALID_TILE) {
 
		switch (variable) {
 
			case 0x40: return 0;
 
			case 0x41: return 0;
 
			case 0x42: return 0;
 
			case 0x43: return _date;
 
			case 0x44: return HZB_TOWN_EDGE;
 
		}
 
	}
 

	
 
	switch (variable) {
 
		case 0x40: return GetTerrainType(tile, object->u.routes.context);
 
		case 0x40: return GetTerrainType(this->tile, this->context);
 
		case 0x41: return 0;
 
		case 0x42: return IsLevelCrossingTile(tile) && IsCrossingBarred(tile);
 
		case 0x42: return IsLevelCrossingTile(this->tile) && IsCrossingBarred(this->tile);
 
		case 0x43:
 
			if (IsRailDepotTile(tile)) return Depot::GetByTile(tile)->build_date;
 
			if (IsRailDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date;
 
			return _date;
 
		case 0x44: {
 
			const Town *t = NULL;
 
			if (IsRailDepotTile(tile)) {
 
				t = Depot::GetByTile(tile)->town;
 
			} else if (IsLevelCrossingTile(tile)) {
 
				t = ClosestTownFromTile(tile, UINT_MAX);
 
			if (IsRailDepotTile(this->tile)) {
 
				t = Depot::GetByTile(this->tile)->town;
 
			} else if (IsLevelCrossingTile(this->tile)) {
 
				t = ClosestTownFromTile(this->tile, UINT_MAX);
 
			}
 
			return t != NULL ? GetTownRadiusGroup(t, tile) : HZB_TOWN_EDGE;
 
			return t != NULL ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
 
		}
 
	}
 

	
 
	DEBUG(grf, 1, "Unhandled rail type tile variable 0x%X", variable);
 

	
 
	*available = false;
 
	return UINT_MAX;
 
}
 

	
 
static const SpriteGroup *RailTypeResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
 
/* virtual */ const SpriteGroup *RailTypeResolverObject::ResolveReal(const RealSpriteGroup *group) const
 
{
 
	if (group->num_loading > 0) return group->loading[0];
 
	if (group->num_loaded  > 0) return group->loaded[0];
 
	return NULL;
 
}
 

	
 
static inline void NewRailTypeResolver(ResolverObject *res, TileIndex tile, TileContext context, const GRFFile *grffile, uint32 param1 = 0, uint32 param2 = 0)
 
RailTypeScopeResolver::RailTypeScopeResolver(ResolverObject *ro, TileIndex tile, TileContext context) : ScopeResolver(ro)
 
{
 
	res->GetRandomBits = &RailTypeGetRandomBits;
 
	res->GetTriggers   = &RailTypeGetTriggers;
 
	res->SetTriggers   = &RailTypeSetTriggers;
 
	res->GetVariable   = &RailTypeGetVariable;
 
	res->ResolveRealMethod = &RailTypeResolveReal;
 
	this->tile = tile;
 
	this->context = context;
 
}
 

	
 
	res->u.routes.tile = tile;
 
	res->u.routes.context = context;
 

	
 
	res->callback        = CBID_NO_CALLBACK;
 
	res->callback_param1 = param1;
 
	res->callback_param2 = param2;
 
	res->ResetState();
 

	
 
	res->grffile         = grffile;
 
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)
 
{
 
}
 

	
 
/**
 
 * 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.
 
 */
 
SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context)
 
{
 
	assert(rtsg < RTSG_END);
 

	
 
	if (rti->group[rtsg] == NULL) return 0;
 

	
 
	const SpriteGroup *group;
 
	ResolverObject object;
 

	
 
	NewRailTypeResolver(&object, tile, context, rti->grffile[rtsg]);
 

	
 
	group = SpriteGroup::Resolve(rti->group[rtsg], &object);
 
	RailTypeResolverObject object(tile, context, rti->grffile[rtsg]);
 
	const SpriteGroup *group = SpriteGroup::Resolve(rti->group[rtsg], &object);
 
	if (group == NULL || group->GetNumResults() == 0) return 0;
 

	
 
	return group->GetResult();
 
}
 

	
 
/**
 
 * 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;
 

	
 
	ResolverObject object;
 

	
 
	uint32 param1 = gui ? 0x10 : 0x00;
 
	uint32 param2 = (type << 16) | (var << 8) | state;
 
	NewRailTypeResolver(&object, tile, TCX_NORMAL, rti->grffile[RTSG_SIGNALS], param1, param2);
 
	RailTypeResolverObject object(tile, TCX_NORMAL, rti->grffile[RTSG_SIGNALS], param1, param2);
 

	
 
	const SpriteGroup *group = SpriteGroup::Resolve(rti->group[RTSG_SIGNALS], &object);
 
	if (group == NULL || group->GetNumResults() == 0) return 0;
 

	
 
	return group->GetResult();
 
}
 

	
 
/**
 
 * 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.
 
@@ -157,24 +131,12 @@ uint8 GetReverseRailTypeTranslation(Rail
 
{
 
	/* No rail type table present, return rail type as-is */
 
	if (grffile == NULL || grffile->railtype_list.Length() == 0) return railtype;
 

	
 
	/* Look for a matching rail type label in the table */
 
	RailTypeLabel label = GetRailTypeInfo(railtype)->label;
 
	int index = grffile->railtype_list.FindIndex(label);
 
	if (index >= 0) return index;
 

	
 
	/* If not found, return as invalid */
 
	return 0xFF;
 
}
 

	
 
/**
 
 * 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)
 
{
 
	/* 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);
 
}
src/newgrf_railtype.h
Show inline comments
 
@@ -5,19 +5,48 @@
 
 * 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/>.
 
 */
 

	
 
/** @file newgrf_railtype.h NewGRF handling of rail types. */
 

	
 
#ifndef NEWGRF_RAILTYPE_H
 
#define NEWGRF_RAILTYPE_H
 

	
 
#include "rail.h"
 
#include "newgrf_commons.h"
 
#include "newgrf_spritegroup.h"
 

	
 

	
 
struct RailTypeScopeResolver : public ScopeResolver {
 
	TileIndex tile;      ///< Tracktile. For track on a bridge this is the southern bridgehead.
 
	TileContext context; ///< Are we resolving sprites for the upper halftile, or on a bridge?
 

	
 
	RailTypeScopeResolver(ResolverObject *ro, TileIndex tile, TileContext context);
 

	
 
	/* virtual */ uint32 GetRandomBits() const;
 
	/* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
 
};
 

	
 
/** Resolver object for rail types. */
 
struct RailTypeResolverObject : public ResolverObject {
 
	RailTypeScopeResolver railtype_scope;
 

	
 
	RailTypeResolverObject(TileIndex tile, TileContext context, const GRFFile *grffile, uint32 param1 = 0, uint32 param2 = 0);
 

	
 
	/* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
 
	{
 
		switch (scope) {
 
			case VSG_SCOPE_SELF: return &this->railtype_scope;
 
			default:             return &this->default_scope; // XXX ResolverObject::GetScope(scope, relative);
 
		}
 
	}
 

	
 
	/* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
 
};
 

	
 
SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context = TCX_NORMAL);
 
SpriteID GetCustomSignalSprite(const RailtypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, SignalState state, bool gui = false);
 

	
 
uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile);
 

	
 
#endif /* NEWGRF_RAILTYPE_H */
src/newgrf_spritegroup.h
Show inline comments
 
@@ -365,28 +365,24 @@ struct ResolverObject {
 
		} industry;
 
		struct {
 
			CargoID cargo_type;
 
			uint8 default_selection;
 
			uint8 src_industry;            ///< Source industry substitute type. 0xFF for "town", 0xFE for "unknown".
 
			uint8 dst_industry;            ///< Destination industry substitute type. 0xFF for "town", 0xFE for "unknown".
 
			uint8 distance;
 
			AIConstructionEvent event;
 
			uint8 count;
 
			uint8 station_size;
 
		} generic;
 
		struct {
 
			TileIndex tile;                ///< Tracktile. For track on a bridge this is the southern bridgehead.
 
			TileContext context;           ///< Are we resolving sprites for the upper halftile, or on a bridge?
 
		} routes;
 
		struct {
 
			struct Station *st;            ///< Station of the airport for which the callback is run, or NULL for build gui.
 
			byte airport_id;               ///< Type of airport for which the callback is run
 
			byte layout;                   ///< Layout of the airport to build.
 
			TileIndex tile;                ///< Tile for the callback, only valid for airporttile callbacks.
 
		} airport;
 
	} u;
 

	
 
	uint32 (*GetRandomBits)(const struct ResolverObject*);
 
	uint32 (*GetTriggers)(const struct ResolverObject*);
 
	void (*SetTriggers)(const struct ResolverObject*, int);
 
	uint32 (*GetVariable)(const struct ResolverObject *object, byte variable, uint32 parameter, bool *available);
 
	const SpriteGroup *(*ResolveRealMethod)(const struct ResolverObject*, const RealSpriteGroup*);
src/table/newgrf_debug_data.h
Show inline comments
 
@@ -389,25 +389,32 @@ static const NIVariable _niv_railtypes[]
 
	NIV(0x41, "enhanced tunnels"),
 
	NIV(0x42, "level crossing status"),
 
	NIV_END()
 
};
 

	
 
class NIHRailType : public NIHelper {
 
	bool IsInspectable(uint index) const                 { return true; }
 
	uint GetParent(uint index) const                     { return UINT32_MAX; }
 
	const void *GetInstance(uint index)const             { return NULL; }
 
	const void *GetSpec(uint index) const                { return NULL; }
 
	void SetStringParameters(uint index) const           { this->SetObjectAtStringParameters(STR_NEWGRF_INSPECT_CAPTION_OBJECT_AT_RAIL_TYPE, INVALID_STRING_ID, index); }
 
	uint32 GetGRFID(uint index) const                    { return 0; }
 
	void Resolve(ResolverObject *ro, uint32 index) const { extern void GetRailTypeResolver(ResolverObject *ro, uint index); GetRailTypeResolver(ro, index); }
 

	
 
	/* virtual */ uint Resolve(uint index, uint var, uint param, bool *avail) const
 
	{
 
		/* 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. */
 
		RailTypeResolverObject ro(index, TCX_NORMAL, NULL);
 
		return ro.GetScope(ro.scope)->GetVariable(var, param, avail);
 
	}
 
};
 

	
 
static const NIFeature _nif_railtype = {
 
	NULL,
 
	NULL,
 
	_niv_railtypes,
 
	new NIHRailType(),
 
};
 

	
 

	
 
/*** NewGRF airport tiles ***/
 

	
0 comments (0 inline, 0 general)