Changeset - r14123:a73ffee57a40
[Not reviewed]
master
0 1 0
peter1138 - 15 years ago 2010-01-01 18:10:50
peter1138@openttd.org
(svn r18680) -Fix: Whole station area is already correct size.
1 file changed with 0 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/newgrf_station.cpp
Show inline comments
 
/* $Id$ */
 

	
 
/*
 
 * 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_station.cpp Functions for dealing with station classes and custom stations. */
 

	
 
#include "stdafx.h"
 
#include "variables.h"
 
#include "landscape.h"
 
#include "debug.h"
 
#include "station_base.h"
 
#include "waypoint_base.h"
 
#include "roadstop_base.h"
 
#include "newgrf_cargo.h"
 
#include "newgrf_commons.h"
 
#include "newgrf_station.h"
 
#include "newgrf_spritegroup.h"
 
#include "newgrf_sound.h"
 
#include "town.h"
 
#include "newgrf_town.h"
 
#include "gfx_func.h"
 
#include "date_func.h"
 
#include "company_func.h"
 
#include "animated_tile_func.h"
 
#include "functions.h"
 
#include "tunnelbridge_map.h"
 
#include "spritecache.h"
 

	
 
#include "table/strings.h"
 

	
 
static StationClass _station_classes[STAT_CLASS_MAX];
 

	
 
enum {
 
	MAX_SPECLIST = 255,
 
};
 

	
 
enum TriggerArea {
 
	TA_TILE,
 
	TA_PLATFORM,
 
	TA_WHOLE,
 
};
 

	
 
struct ETileArea : TileArea {
 
	ETileArea(const BaseStation *st, TileIndex tile, TriggerArea ta)
 
	{
 
		switch (ta) {
 
			default: NOT_REACHED();
 

	
 
			case TA_TILE:
 
				this->tile = tile;
 
				this->w    = 1;
 
				this->h    = 1;
 
				break;
 

	
 
			case TA_PLATFORM: {
 
				TileIndex start, end;
 
				Axis axis = GetRailStationAxis(tile);
 
				TileIndexDiff delta = TileOffsByDiagDir(AxisToDiagDir(axis));
 

	
 
				for (end = tile; IsRailStationTile(end + delta) && IsCompatibleTrainStationTile(tile, end + delta); end += delta) { /* Nothing */ }
 
				for (start = tile; IsRailStationTile(start - delta) && IsCompatibleTrainStationTile(tile, start - delta); start -= delta) { /* Nothing */ }
 

	
 
				this->tile = start;
 
				this->w = TileX(end) - TileX(start) + 1;
 
				this->h = TileY(end) - TileY(start) + 1;
 
				break;
 
			}
 

	
 
			case TA_WHOLE:
 
				st->GetTileArea(this, Station::IsExpected(st) ? STATION_RAIL : STATION_WAYPOINT);
 
				this->w++;
 
				this->h++;
 
				break;
 
		}
 
	}
 
};
 

	
 

	
 
/**
 
 * Reset station classes to their default state.
 
 * This includes initialising the Default and Waypoint classes with an empty
 
 * entry, for standard stations and waypoints.
 
 */
 
void ResetStationClasses()
 
{
 
	for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
 
		_station_classes[i].id = 0;
 
		_station_classes[i].name = STR_EMPTY;
 
		_station_classes[i].stations = 0;
 

	
 
		free(_station_classes[i].spec);
 
		_station_classes[i].spec = NULL;
 
	}
 

	
 
	/* Set up initial data */
 
	_station_classes[0].id = 'DFLT';
 
	_station_classes[0].name = STR_STATION_CLASS_DFLT;
 
	_station_classes[0].stations = 1;
 
	_station_classes[0].spec = MallocT<StationSpec*>(1);
 
	_station_classes[0].spec[0] = NULL;
 

	
 
	_station_classes[1].id = 'WAYP';
 
	_station_classes[1].name = STR_STATION_CLASS_WAYP;
 
	_station_classes[1].stations = 1;
 
	_station_classes[1].spec = MallocT<StationSpec*>(1);
 
	_station_classes[1].spec[0] = NULL;
 
}
 

	
 
/**
 
 * Allocate a station class for the given class id.
 
 * @param cls A 32 bit value identifying the class.
 
 * @return Index into _station_classes of allocated class.
 
 */
 
StationClassID AllocateStationClass(uint32 cls)
 
{
 
	for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
 
		if (_station_classes[i].id == cls) {
 
			/* ClassID is already allocated, so reuse it. */
 
			return i;
 
		} else if (_station_classes[i].id == 0) {
 
			/* This class is empty, so allocate it to the ClassID. */
 
			_station_classes[i].id = cls;
 
			return i;
 
		}
 
	}
 

	
 
	grfmsg(2, "StationClassAllocate: already allocated %d classes, using default", STAT_CLASS_MAX);
 
	return STAT_CLASS_DFLT;
 
}
 

	
 
/** Set the name of a custom station class */
 
void SetStationClassName(StationClassID sclass, StringID name)
 
{
 
	assert(sclass < STAT_CLASS_MAX);
 
	_station_classes[sclass].name = name;
 
}
 

	
 
/** Retrieve the name of a custom station class */
 
StringID GetStationClassName(StationClassID sclass)
 
{
 
	assert(sclass < STAT_CLASS_MAX);
 
	return _station_classes[sclass].name;
 
}
 

	
 
/**
 
 * Get the number of station classes in use.
 
 * @return Number of station classes.
 
 */
 
uint GetNumStationClasses()
 
{
 
	uint i;
 
	for (i = 0; i < STAT_CLASS_MAX && _station_classes[i].id != 0; i++) {}
 
	return i;
 
}
 

	
 
/**
 
 * Return the number of stations for the given station class.
 
 * @param sclass Index of the station class.
 
 * @return Number of stations in the class.
 
 */
 
uint GetNumCustomStations(StationClassID sclass)
 
{
 
	assert(sclass < STAT_CLASS_MAX);
 
	return _station_classes[sclass].stations;
 
}
 

	
 
/**
 
 * Tie a station spec to its station class.
 
 * @param statspec The station spec.
 
 */
 
void SetCustomStationSpec(StationSpec *statspec)
 
{
 
	StationClass *station_class;
 
	int i;
 

	
 
	/* If the station has already been allocated, don't reallocate it. */
 
	if (statspec->allocated) return;
 

	
 
	assert(statspec->sclass < STAT_CLASS_MAX);
 
	station_class = &_station_classes[statspec->sclass];
 

	
 
	i = station_class->stations++;
 
	station_class->spec = ReallocT(station_class->spec, station_class->stations);
 

	
 
	station_class->spec[i] = statspec;
 
	statspec->allocated = true;
 
}
 

	
 
/**
 
 * Retrieve a station spec from a class.
 
 * @param sclass Index of the station class.
 
 * @param station The station index with the class.
 
 * @return The station spec.
 
 */
 
const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station)
 
{
 
	assert(sclass < STAT_CLASS_MAX);
 
	if (station < _station_classes[sclass].stations)
 
		return _station_classes[sclass].spec[station];
 

	
 
	/* If the custom station isn't defined any more, then the GRF file
 
	 * probably was not loaded. */
 
	return NULL;
 
}
 

	
 
/**
 
 * Retrieve a station spec by GRF location.
 
 * @param grfid    GRF ID of station spec.
 
 * @param localidx Index within GRF file of station spec.
 
 * @param index    Pointer to return the index of the station spec in its station class. If NULL then not used.
 
 * @return The station spec.
 
 */
 
const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx, int *index)
 
{
 
	uint j;
 

	
 
	for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
 
		for (j = 0; j < _station_classes[i].stations; j++) {
 
			const StationSpec *statspec = _station_classes[i].spec[j];
 
			if (statspec == NULL) continue;
 
			if (statspec->grffile->grfid == grfid && statspec->localidx == localidx) {
 
				if (index != NULL) *index = j;
 
				return statspec;
 
			}
 
		}
 
	}
 

	
 
	return NULL;
 
}
 

	
 

	
 
/* Evaluate a tile's position within a station, and return the result a bitstuffed format.
 
 * if not centred: .TNLcCpP, if centred: .TNL..CP
 
 * T = Tile layout number (GetStationGfx), N = Number of platforms, L = Length of platforms
 
 * C = Current platform number from start, c = from end
 
 * P = Position along platform from start, p = from end
 
 * if centred, C/P start from the centre and c/p are not available.
 
 */
 
uint32 GetPlatformInfo(Axis axis, byte tile, int platforms, int length, int x, int y, bool centred)
 
{
 
	uint32 retval = 0;
 

	
 
	if (axis == AXIS_X) {
 
		Swap(platforms, length);
 
		Swap(x, y);
 
	}
 

	
 
	/* Limit our sizes to 4 bits */
 
	platforms = min(15, platforms);
 
	length    = min(15, length);
 
	x = min(15, x);
 
	y = min(15, y);
 
	if (centred) {
 
		x -= platforms / 2;
 
		y -= length / 2;
 
		SB(retval,  0, 4, y & 0xF);
 
		SB(retval,  4, 4, x & 0xF);
 
	} else {
 
		SB(retval,  0, 4, y);
 
		SB(retval,  4, 4, length - y - 1);
 
		SB(retval,  8, 4, x);
 
		SB(retval, 12, 4, platforms - x - 1);
 
	}
 
	SB(retval, 16, 4, length);
 
	SB(retval, 20, 4, platforms);
 
	SB(retval, 24, 4, tile);
 

	
 
	return retval;
 
}
 

	
 

	
 
/* Find the end of a railway station, from the tile, in the direction of delta.
 
 * If check_type is set, we stop if the custom station type changes.
 
 * If check_axis is set, we stop if the station direction changes.
 
 */
 
static TileIndex FindRailStationEnd(TileIndex tile, TileIndexDiff delta, bool check_type, bool check_axis)
 
{
 
	byte orig_type = 0;
 
	Axis orig_axis = AXIS_X;
 
	StationID sid = GetStationIndex(tile);
 

	
 
	if (check_type) orig_type = GetCustomStationSpecIndex(tile);
 
	if (check_axis) orig_axis = GetRailStationAxis(tile);
 

	
 
	while (true) {
 
		TileIndex new_tile = TILE_ADD(tile, delta);
 

	
 
		if (!IsTileType(new_tile, MP_STATION) || GetStationIndex(new_tile) != sid) break;
 
		if (!HasStationRail(new_tile)) break;
 
		if (check_type && GetCustomStationSpecIndex(new_tile) != orig_type) break;
 
		if (check_axis && GetRailStationAxis(new_tile) != orig_axis) break;
 

	
 
		tile = new_tile;
 
	}
 
	return tile;
 
}
 

	
 

	
 
static uint32 GetPlatformInfoHelper(TileIndex tile, bool check_type, bool check_axis, bool centred)
 
{
 
	int tx = TileX(tile);
 
	int ty = TileY(tile);
 
	int sx = TileX(FindRailStationEnd(tile, TileDiffXY(-1,  0), check_type, check_axis));
 
	int sy = TileY(FindRailStationEnd(tile, TileDiffXY( 0, -1), check_type, check_axis));
 
	int ex = TileX(FindRailStationEnd(tile, TileDiffXY( 1,  0), check_type, check_axis)) + 1;
 
	int ey = TileY(FindRailStationEnd(tile, TileDiffXY( 0,  1), check_type, check_axis)) + 1;
 

	
 
	tx -= sx; ex -= sx;
 
	ty -= sy; ey -= sy;
 

	
 
	return GetPlatformInfo(GetRailStationAxis(tile), GetStationGfx(tile), ex, ey, tx, ty, centred);
 
}
 

	
 

	
 
static uint32 GetRailContinuationInfo(TileIndex tile)
 
{
 
	/* Tile offsets and exit dirs for X axis */
 
	static const Direction x_dir[8] = { DIR_SW, DIR_NE, DIR_SE, DIR_NW, DIR_S, DIR_E, DIR_W, DIR_N };
 
	static const DiagDirection x_exits[8] = { DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SW, DIAGDIR_NE };
 

	
 
	/* Tile offsets and exit dirs for Y axis */
 
	static const Direction y_dir[8] = { DIR_SE, DIR_NW, DIR_SW, DIR_NE, DIR_S, DIR_W, DIR_E, DIR_N };
 
	static const DiagDirection y_exits[8] = { DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_NW };
 

	
 
	Axis axis = GetRailStationAxis(tile);
 

	
 
	/* Choose appropriate lookup table to use */
 
	const Direction *dir = axis == AXIS_X ? x_dir : y_dir;
 
	const DiagDirection *diagdir = axis == AXIS_X ? x_exits : y_exits;
 

	
 
	uint32 res = 0;
 
	uint i;
 

	
 
	for (i = 0; i < lengthof(x_dir); i++, dir++, diagdir++) {
 
		TileIndex neighbour_tile = tile + TileOffsByDir(*dir);
 
		TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(neighbour_tile, TRANSPORT_RAIL, 0));
 
		if (trackbits != TRACK_BIT_NONE) {
 
			/* If there is any track on the tile, set the bit in the second byte */
 
			SetBit(res, i + 8);
 

	
 
			/* With tunnels and bridges the tile has tracks, but they are not necessarily connected
 
			 * with the next tile because the ramp is not going in the right direction. */
 
			if (IsTileType(neighbour_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(neighbour_tile) != *diagdir) {
 
				continue;
 
			}
 

	
 
			/* If any track reaches our exit direction, set the bit in the lower byte */
 
			if (trackbits & DiagdirReachesTracks(*diagdir)) SetBit(res, i);
 
		}
 
	}
 

	
 
	return res;
 
}
 

	
 

	
 
/* Station Resolver Functions */
 
static uint32 StationGetRandomBits(const ResolverObject *object)
 
{
 
	const BaseStation *st = object->u.station.st;
 
	const TileIndex tile = object->u.station.tile;
 
	return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
 
}
 

	
 

	
 
static uint32 StationGetTriggers(const ResolverObject *object)
 
{
 
	const BaseStation *st = object->u.station.st;
 
	return st == NULL ? 0 : st->waiting_triggers;
 
}
 

	
 

	
 
static void StationSetTriggers(const ResolverObject *object, int triggers)
 
{
 
	BaseStation *st = const_cast<BaseStation *>(object->u.station.st);
 
	assert(st != NULL);
 
	st->waiting_triggers = triggers;
 
}
 

	
 
/**
 
 * Station variable cache
 
 * This caches 'expensive' station variable lookups which iterate over
 
 * several tiles that may be called multiple times per Resolve().
 
 */
 
static struct {
 
	uint32 v40;
 
	uint32 v41;
 
	uint32 v45;
 
	uint32 v46;
 
	uint32 v47;
 
	uint32 v49;
 
	uint8 valid;
 
} _svc;
 

	
 
static uint32 StationGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
 
{
 
	const BaseStation *st = object->u.station.st;
 
	TileIndex tile = object->u.station.tile;
 

	
 
	if (object->scope == VSG_SCOPE_PARENT) {
 
		/* Pass the request on to the town of the station */
 
		const Town *t;
 

	
 
		if (st != NULL) {
 
			t = st->town;
 
		} else if (tile != INVALID_TILE) {
 
			t = ClosestTownFromTile(tile, UINT_MAX);
 
		} else {
 
			*available = false;
 
			return UINT_MAX;
 
		}
 

	
 
		return TownGetVariable(variable, parameter, available, t);
 
	}
 

	
 
	if (st == NULL) {
 
		/* Station does not exist, so we're in a purchase list */
 
		switch (variable) {
 
			case 0x40:
 
			case 0x41:
 
			case 0x46:
 
			case 0x47:
 
			case 0x49: return 0x2110000;        // Platforms, tracks & position
 
			case 0x42: return 0;                // Rail type (XXX Get current type from GUI?)
 
			case 0x43: return _current_company; // Station owner
 
			case 0x44: return 2;                // PBS status
 
			case 0xFA: return Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Build date, clamped to a 16 bit value
 
		}
 

	
 
		*available = false;
 
		return UINT_MAX;
 
	}
 

	
 
	switch (variable) {
 
		/* Calculated station variables */
 
		case 0x40:
 
			if (!HasBit(_svc.valid, 0)) { _svc.v40 = GetPlatformInfoHelper(tile, false, false, false); SetBit(_svc.valid, 0); }
 
			return _svc.v40;
 

	
 
		case 0x41:
 
			if (!HasBit(_svc.valid, 1)) { _svc.v41 = GetPlatformInfoHelper(tile, true,  false, false); SetBit(_svc.valid, 1); }
 
			return _svc.v41;
 

	
 
		case 0x42: return GetTerrainType(tile) | (GetRailType(tile) << 8);
 
		case 0x43: return st->owner; // Station owner
 
		case 0x44: return HasStationReservation(tile) ? 7 : 4; // PBS status
 
		case 0x45:
 
			if (!HasBit(_svc.valid, 2)) { _svc.v45 = GetRailContinuationInfo(tile); SetBit(_svc.valid, 2); }
 
			return _svc.v45;
 

	
 
		case 0x46:
 
			if (!HasBit(_svc.valid, 3)) { _svc.v46 = GetPlatformInfoHelper(tile, false, false, true); SetBit(_svc.valid, 3); }
 
			return _svc.v46;
 

	
 
		case 0x47:
 
			if (!HasBit(_svc.valid, 4)) { _svc.v47 = GetPlatformInfoHelper(tile, true,  false, true); SetBit(_svc.valid, 4); }
 
			return _svc.v47;
0 comments (0 inline, 0 general)