/* $Id: road_map.c 3766 2006-03-05 12:22:20Z tron $ */

#include "stdafx.h"
#include "openttd.h"
#include "road_map.h"
#include "station.h"


RoadBits GetAnyRoadBits(TileIndex tile)
{
	switch (GetTileType(tile)) {
		case MP_STREET:
			switch (GetRoadType(tile)) {
				default:
				case ROAD_NORMAL:   return GetRoadBits(tile);
				case ROAD_CROSSING: return GetCrossingRoadBits(tile);
				case ROAD_DEPOT:    return DiagDirToRoadBits(GB(_m[tile].m5, 0, 2));
			}

		case MP_STATION:
			if (!IsRoadStationTile(tile)) return 0;
			return DiagDirToRoadBits(GetRoadStationDir(tile));

		case MP_TUNNELBRIDGE:
			if (_m[tile].m5 & 0x80) {
				// bridge
				if (_m[tile].m5 & 0x40) {
					// middle part
					if ((_m[tile].m5 & 0x38) != 0x28) return 0; // no road under bridge
					return _m[tile].m5 & 1 ? ROAD_X : ROAD_Y;
				} else {
					// ending
					if (GB(_m[tile].m5, 1, 2) != TRANSPORT_ROAD) return 0; // not a road bridge
					return _m[tile].m5 & 1 ? ROAD_Y : ROAD_X;
				}
			} else {
				// tunnel
				if (GB(_m[tile].m5, 2, 2) != TRANSPORT_ROAD) return 0; // not a road tunnel
				return DiagDirToRoadBits(ReverseDiagDir(GB(_m[tile].m5, 0, 2)));
			}

		default: return 0;
	}
}
