src/ai/api/ai_road.cpp
author rubidium
Thu, 22 Mar 2007 09:52:12 +0000
branchnoai
changeset 9511 f767ad06e86b
parent 9504 de4993fa3d98
child 9513 258f78c74b0c
permissions -rw-r--r--
(svn r9407) [NoAI] -Add: placing of signs.
/* $Id$ */

/** @file ai_town.cpp handles the functions of the AIRoad class */

#include "ai_road.hpp"
#include "../../command.h"
#include "../../road_map.h"
#include "../../station_map.h"

bool AIRoad::IsRoadTile(TileIndex tile)
{
	/* Outside of the map */
	if (tile >= _map_size) return false;

	return IsTileType(tile, MP_STREET) && GetRoadTileType(tile) != ROAD_TILE_DEPOT;
}

bool AIRoad::BuildRoad(TileIndex start, TileIndex end)
{
	/* Outside of the map */
	if (start >= _map_size || end >= _map_size || start == end) return false;
	/* Not on one line */
	if (TileX(start) != TileX(end) &&
			TileY(start) != TileY(end)) return false;

	return this->DoCommand(end, start, (TileY(start) != TileY(end) ? 4 : 0) | (start < end ? 1 : 2), CMD_BUILD_LONG_ROAD);
}

bool AIRoad::BuildRoadDepot(TileIndex tile, TileIndex front)
{
	/* Outside of the map */
	if (tile >= _map_size || tile == front) return false;

	uint entrance_dir = (TileX(tile) == TileX(front)) ? (TileY(tile) < TileY(front) ? 1 : 3) : (TileX(tile) < TileX(front) ? 2 : 0);

	return this->DoCommand(tile, entrance_dir, 0, CMD_BUILD_ROAD_DEPOT);
}

bool AIRoad::BuildRoadStation(TileIndex tile, TileIndex front, bool truck, bool drive_through)
{
	/* Outside of the map */
	if (tile >= _map_size || tile == front) return false;

	uint entrance_dir;
	if (drive_through) {
		entrance_dir = TileY(tile) != TileY(front);
	} else {
		entrance_dir = (TileX(tile) == TileX(front)) ? (TileY(tile) < TileY(front) ? 1 : 3) : (TileX(tile) < TileX(front) ? 2 : 0);
	}

	return this->DoCommand(tile, entrance_dir, (drive_through ? 2 : 0) | (truck ? 1 : 0), CMD_BUILD_ROAD_STOP);
}

bool AIRoad::RemoveRoad(TileIndex start, TileIndex end)
{
	/* Outside of the map */
	if (start >= _map_size || end >= _map_size) return false;
	/* Not on one line */
	if (TileX(start) != TileX(end) &&
			TileY(start) != TileY(end)) return false;

	return this->DoCommand(end, start, (TileY(start) != TileY(end) ? 4 : 0) | (start < end ? 1 : 2), CMD_REMOVE_LONG_ROAD);
}

bool AIRoad::RemoveRoadDepot(TileIndex tile)
{
	/* Outside of the map */
	if (tile >= _map_size) return false;

	/* Not a road depot tile */
	if (!IsTileType(tile, MP_STREET) || GetRoadTileType(tile) != ROAD_TILE_DEPOT) return false;

	return this->DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
}

bool AIRoad::RemoveRoadStation(TileIndex tile)
{
	/* Outside of the map */
	if (tile >= _map_size) return false;

	/* Not a road station tile */
	if (!IsTileType(tile, MP_STATION) || !IsRoadStop(tile)) return false;

	return this->DoCommand(tile, 0, GetRoadStopType(tile), CMD_REMOVE_ROAD_STOP);
}