Changeset - r14420:5d77bf502c4e
[Not reviewed]
master
0 3 0
terkhen - 15 years ago 2010-02-02 22:27:03
terkhen@openttd.org
(svn r18987) -Fix: [NoAI] Make building long rails fail for AIs if there is an obstacle in the way.
3 files changed with 7 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/ai/api/ai_rail.cpp
Show inline comments
 
@@ -323,25 +323,25 @@ static uint32 SimulateDrag(TileIndex fro
 
{
 
	EnforcePrecondition(false, ::IsValidTile(from));
 
	EnforcePrecondition(false, ::IsValidTile(tile));
 
	EnforcePrecondition(false, ::IsValidTile(to));
 
	EnforcePrecondition(false, ::DistanceManhattan(from, tile) == 1);
 
	EnforcePrecondition(false, ::DistanceManhattan(tile, to) >= 1);
 
	EnforcePrecondition(false, IsRailTypeAvailable(GetCurrentRailType()));
 
	int diag_offset = abs(abs((int)::TileX(to) - (int)::TileX(tile)) - abs((int)::TileY(to) - (int)::TileY(tile)));
 
	EnforcePrecondition(false, diag_offset <= 1 ||
 
			(::TileX(from) == ::TileX(tile) && ::TileX(tile) == ::TileX(to)) ||
 
			(::TileY(from) == ::TileY(tile) && ::TileY(tile) == ::TileY(to)));
 

	
 
	uint32 p2 = SimulateDrag(from, tile, &to);
 
	uint32 p2 = SimulateDrag(from, tile, &to) | 1 << 8;
 
	return AIObject::DoCommand(tile, to, p2, CMD_BUILD_RAILROAD_TRACK);
 
}
 

	
 
/* static */ bool AIRail::RemoveRail(TileIndex from, TileIndex tile, TileIndex to)
 
{
 
	EnforcePrecondition(false, ::IsValidTile(from));
 
	EnforcePrecondition(false, ::IsValidTile(tile));
 
	EnforcePrecondition(false, ::IsValidTile(to));
 
	EnforcePrecondition(false, ::DistanceManhattan(from, tile) == 1);
 
	EnforcePrecondition(false, ::DistanceManhattan(tile, to) >= 1);
 
	int diag_offset = abs(abs((int)::TileX(to) - (int)::TileX(tile)) - abs((int)::TileY(to) - (int)::TileY(tile)));
 
	EnforcePrecondition(false, diag_offset <= 1 ||
src/ai/api/ai_rail.hpp
Show inline comments
 
@@ -371,24 +371,25 @@ public:
 
	 * @pre AIMap::DistanceManhattan(from, tile) == 1.
 
	 * @pre AIMap::DistanceManhattan(to, tile) >= 1.
 
	 * @pre (abs(abs(AIMap::GetTileX(to) - AIMap::GetTileX(tile)) -
 
	 *          abs(AIMap::GetTileY(to) - AIMap::GetTileY(tile))) <= 1) ||
 
	 *      (AIMap::GetTileX(from) == AIMap::GetTileX(tile) && AIMap::GetTileX(tile) == AIMap::GetTileX(to)) ||
 
	 *      (AIMap::GetTileY(from) == AIMap::GetTileY(tile) && AIMap::GetTileY(tile) == AIMap::GetTileY(to)).
 
	 * @pre IsRailTypeAvailable(GetCurrentRailType()).
 
	 * @exception AIError::ERR_AREA_NOT_CLEAR
 
	 * @exception AIError::ERR_LAND_SLOPED_WRONG
 
	 * @exception AIRail::ERR_CROSSING_ON_ONEWAY_ROAD
 
	 * @exception AIRoad::ERR_ROAD_WORKS_IN_PROGRESS
 
	 * @exception AIError::ERR_ALREADY_BUILT
 
	 * @note Construction will fail if an obstacle is found between the start and end tiles.
 
	 * @return Whether the rail has been/can be build or not.
 
	 */
 
	static bool BuildRail(TileIndex from, TileIndex tile, TileIndex to);
 

	
 
	/**
 
	 * Remove a rail connection between two tiles.
 
	 * @param from The tile just before the tile to remove rail from.
 
	 * @param tile The first tile to remove rail from.
 
	 * @param to The tile just after the last tile to remove rail from.
 
	 * @pre from != to.
 
	 * @pre AIMap::DistanceManhattan(from, tile) == 1.
 
	 * @pre AIMap::DistanceManhattan(to, tile) >= 1.
src/rail_cmd.cpp
Show inline comments
 
@@ -689,48 +689,52 @@ static CommandCost ValidateAutoDrag(Trac
 

	
 
	return CommandCost();
 
}
 

	
 
/** Build a stretch of railroad tracks.
 
 * @param tile start tile of drag
 
 * @param flags operation to perform
 
 * @param p1 end tile of drag
 
 * @param p2 various bitstuffed elements
 
 * - p2 = (bit 0-3) - railroad type normal/maglev (0 = normal, 1 = mono, 2 = maglev)
 
 * - p2 = (bit 4-6) - track-orientation, valid values: 0-5 (Track enum)
 
 * - p2 = (bit 7)   - 0 = build, 1 = remove tracks
 
 * - p2 = (bit 8)   - 0 = build up to an obstacle, 1 = fail if an obstacle is found (used for AIs).
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
static CommandCost CmdRailTrackHelper(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	CommandCost ret, total_cost(EXPENSES_CONSTRUCTION);
 
	Track track = (Track)GB(p2, 4, 3);
 
	bool remove = HasBit(p2, 7);
 
	RailType railtype = (RailType)GB(p2, 0, 4);
 

	
 
	if (!ValParamRailtype(railtype) || !ValParamTrackOrientation(track)) return CMD_ERROR;
 
	if (p1 >= MapSize()) return CMD_ERROR;
 
	TileIndex end_tile = p1;
 
	Trackdir trackdir = TrackToTrackdir(track);
 

	
 
	if (ValidateAutoDrag(&trackdir, tile, end_tile).Failed()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) SndPlayTileFx(SND_20_SPLAT_2, tile);
 

	
 
	for (;;) {
 
		ret = DoCommand(tile, railtype, TrackdirToTrack(trackdir), flags, remove ? CMD_REMOVE_SINGLE_RAIL : CMD_BUILD_SINGLE_RAIL);
 

	
 
		if (ret.Failed()) {
 
			if (_error_message != STR_ERROR_ALREADY_BUILT && !remove) break;
 
			if (_error_message != STR_ERROR_ALREADY_BUILT && !remove) {
 
				if (HasBit(p2, 8)) return CMD_ERROR;
 
				break;
 
			}
 
			_error_message = INVALID_STRING_ID;
 
		} else {
 
			total_cost.AddCost(ret);
 
		}
 

	
 
		if (tile == end_tile) break;
 

	
 
		tile += ToTileIndexDiff(_trackdelta[trackdir]);
 

	
 
		/* toggle railbit for the non-diagonal tracks */
 
		if (!IsDiagonalTrackdir(trackdir)) ToggleBit(trackdir, 0);
 
	}
0 comments (0 inline, 0 general)