src/ai/api/ai_tunnel.cpp
author glx
Sat, 24 May 2008 22:48:46 +0000
branchnoai
changeset 10691 a60393d87c0b
parent 10679 fcd493243794
child 10977 6c1a6657c7db
permissions -rw-r--r--
(svn r13235) [NoAI] -Change [API CHANGE]: AITunnel.GetOtherTunnelEnd() now returns INVALID_TILE if no other end is found
[NoAI] -Add [FS#2028]: added AIBridge.GetOtherBridgeEnd() which allows you to find the other end of a bridge
/* $Id$ */

/** @file ai_tunnel.cpp Implementation of AITunnel. */

#include "ai_tunnel.hpp"
#include "ai_map.hpp"
#include "../../openttd.h"
#include "../../landscape.h"
#include "../../tunnel_map.h"
#include "../../road_type.h"
#include "../../command_func.h"
#include "../../tunnelbridge.h"
#include "../../road_func.h"

/* static */ bool AITunnel::IsTunnelTile(TileIndex tile)
{
	return ::IsTunnelTile(tile);
}

/* static */ TileIndex AITunnel::GetOtherTunnelEnd(TileIndex tile)
{
	if (!::IsValidTile(tile)) return INVALID_TILE;

	/* If it's a tunnel alread, take the easy way out! */
	if (IsTunnelTile(tile)) return ::GetOtherTunnelEnd(tile);

	::DoCommand(tile, 0, 0, DC_AUTO, CMD_BUILD_TUNNEL);
	return _build_tunnel_endtile == 0 ? INVALID_TILE : _build_tunnel_endtile;
}

/* static */ bool AITunnel::BuildTunnel(AIVehicle::VehicleType vehicle_type, TileIndex start)
{
	EnforcePrecondition(false, ::IsValidTile(start));
	EnforcePrecondition(false, vehicle_type == AIVehicle::VEHICLE_RAIL || vehicle_type == AIVehicle::VEHICLE_ROAD);

	uint type = 0;
	if (vehicle_type == AIVehicle::VEHICLE_ROAD) {
		type |= (TRANSPORT_ROAD << 9);
		type |= RoadTypeToRoadTypes((::RoadType)AIObject::GetRoadType());
	} else {
		type |= (TRANSPORT_RAIL << 9);
		type |= RAILTYPES_RAIL;
	}

	return AIObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL);
}

/* static */ bool AITunnel::RemoveTunnel(TileIndex tile)
{
	EnforcePrecondition(false, IsTunnelTile(tile));

	return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
}