Changeset - r14559:94c8fa3de6fe
[Not reviewed]
master
0 1 0
alberth - 15 years ago 2010-02-15 19:52:40
alberth@openttd.org
(svn r19140) -Codechange: Move variables closer to their first use.
1 file changed with 27 insertions and 42 deletions:
0 comments (0 inline, 0 general)
src/rail_cmd.cpp
Show inline comments
 
@@ -171,22 +171,20 @@ static bool EnsureNoTrainOnTrack(TileInd
 

	
 
	return !HasVehicleOnPos(tile, &rail_bits, &EnsureNoTrainOnTrackProc);
 
}
 

	
 
static bool CheckTrackCombination(TileIndex tile, TrackBits to_build, uint flags)
 
{
 
	TrackBits current; // The current track layout
 
	TrackBits future;  // The track layout we want to build
 
	_error_message = STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION;
 

	
 
	if (!IsPlainRail(tile)) return false;
 

	
 
	/* So, we have a tile with tracks on it (and possibly signals). Let's see
 
	 * what tracks first */
 
	current = GetTrackBits(tile);
 
	future = current | to_build;
 
	TrackBits current = GetTrackBits(tile); // The current track layout.
 
	TrackBits future = current | to_build;  // The track layout we want to build.
 

	
 
	/* Are we really building something new? */
 
	if (current == future) {
 
		/* Nothing new is being built */
 
		_error_message = STR_ERROR_ALREADY_BUILT;
 
		return false;
 
@@ -364,38 +362,35 @@ static inline bool ValParamTrackOrientat
 
 * @param p2 rail track to build
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdBuildSingleRail(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Slope tileh;
 
	RailType railtype = (RailType)p1;
 
	Track track = (Track)p2;
 
	TrackBits trackbit;
 
	CommandCost cost(EXPENSES_CONSTRUCTION);
 
	CommandCost ret;
 

	
 
	if (!ValParamRailtype(railtype) || !ValParamTrackOrientation(track)) return CMD_ERROR;
 

	
 
	tileh = GetTileSlope(tile, NULL);
 
	trackbit = TrackToTrackBits(track);
 
	Slope tileh = GetTileSlope(tile, NULL);
 
	TrackBits trackbit = TrackToTrackBits(track);
 

	
 
	switch (GetTileType(tile)) {
 
		case MP_RAILWAY:
 
		case MP_RAILWAY: {
 
			if (!CheckTileOwnership(tile)) return CMD_ERROR;
 

	
 
			if (!IsPlainRail(tile)) return CMD_ERROR;
 

	
 
			if (!IsCompatibleRail(GetRailType(tile), railtype)) return_cmd_error(STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION);
 

	
 
			if (!CheckTrackCombination(tile, trackbit, flags) ||
 
					!EnsureNoTrainOnTrack(tile, track)) {
 
				return CMD_ERROR;
 
			}
 

	
 
			ret = CheckRailSlope(tileh, trackbit, GetTrackBits(tile), tile);
 
			CommandCost ret = CheckRailSlope(tileh, trackbit, GetTrackBits(tile), tile);
 
			if (ret.Failed()) return ret;
 
			cost.AddCost(ret);
 

	
 
			/* If the rail types don't match, try to convert only if engines of
 
			 * the new rail type are not powered on the present rail type and engines of
 
			 * the present rail type are powered on the new rail type. */
 
@@ -411,12 +406,13 @@ CommandCost CmdBuildSingleRail(TileIndex
 

	
 
			if (flags & DC_EXEC) {
 
				SetRailGroundType(tile, RAIL_GROUND_BARREN);
 
				SetTrackBits(tile, GetTrackBits(tile) | trackbit);
 
			}
 
			break;
 
		}
 

	
 
		case MP_ROAD:
 
#define M(x) (1 << (x))
 
			/* Level crossings may only be built on these slopes */
 
			if (!HasBit(M(SLOPE_SEN) | M(SLOPE_ENW) | M(SLOPE_NWS) | M(SLOPE_NS) | M(SLOPE_WSE) | M(SLOPE_EW) | M(SLOPE_FLAT), tileh)) {
 
				return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
 
@@ -460,17 +456,17 @@ CommandCost CmdBuildSingleRail(TileIndex
 

	
 
			if (IsLevelCrossing(tile) && GetCrossingRailBits(tile) == trackbit) {
 
				return_cmd_error(STR_ERROR_ALREADY_BUILT);
 
			}
 
			/* FALLTHROUGH */
 

	
 
		default:
 
		default: {
 
			/* Will there be flat water on the lower halftile? */
 
			bool water_ground = IsTileType(tile, MP_WATER) && IsSlopeWithOneCornerRaised(tileh);
 

	
 
			ret = CheckRailSlope(tileh, trackbit, TRACK_BIT_NONE, tile);
 
			CommandCost ret = CheckRailSlope(tileh, trackbit, TRACK_BIT_NONE, tile);
 
			if (ret.Failed()) return ret;
 
			cost.AddCost(ret);
 

	
 
			ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
			if (ret.Failed()) return ret;
 
			cost.AddCost(ret);
 
@@ -483,12 +479,13 @@ CommandCost CmdBuildSingleRail(TileIndex
 
			if (flags & DC_EXEC) {
 
				MakeRailNormal(tile, _current_company, trackbit, railtype);
 
				if (water_ground) SetRailGroundType(tile, RAIL_GROUND_WATER);
 
			}
 
			break;
 
	}
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		MarkTileDirtyByTile(tile);
 
		AddTrackToSignalBuffer(tile, track, _current_company);
 
		YapfNotifyTrackLayoutChange(tile, track);
 
	}
 
@@ -505,18 +502,17 @@ CommandCost CmdBuildSingleRail(TileIndex
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdRemoveSingleRail(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Track track = (Track)p2;
 
	TrackBits trackbit;
 
	CommandCost cost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_RAIL] );
 
	bool crossing = false;
 

	
 
	if (!ValParamTrackOrientation((Track)p2)) return CMD_ERROR;
 
	trackbit = TrackToTrackBits(track);
 
	TrackBits trackbit = TrackToTrackBits(track);
 

	
 
	/* Need to read tile owner now because it may change when the rail is removed
 
	 * Also, in case of floods, _current_company != owner
 
	 * There may be invalid tiletype even in exec run (when removing long track),
 
	 * so do not call GetTileOwner(tile) in any case here */
 
	Owner owner = INVALID_OWNER;
 
@@ -676,23 +672,22 @@ static const TileIndexDiffC _trackdelta[
 
static CommandCost ValidateAutoDrag(Trackdir *trackdir, TileIndex start, TileIndex end)
 
{
 
	int x = TileX(start);
 
	int y = TileY(start);
 
	int ex = TileX(end);
 
	int ey = TileY(end);
 
	int dx, dy, trdx, trdy;
 

	
 
	if (!ValParamTrackOrientation(TrackdirToTrack(*trackdir))) return CMD_ERROR;
 

	
 
	/* calculate delta x,y from start to end tile */
 
	dx = ex - x;
 
	dy = ey - y;
 
	int dx = ex - x;
 
	int dy = ey - y;
 

	
 
	/* calculate delta x,y for the first direction */
 
	trdx = _trackdelta[*trackdir].x;
 
	trdy = _trackdelta[*trackdir].y;
 
	int trdx = _trackdelta[*trackdir].x;
 
	int trdy = _trackdelta[*trackdir].y;
 

	
 
	if (!IsDiagonalTrackdir(*trackdir)) {
 
		trdx += _trackdelta[*trackdir ^ 1].x;
 
		trdy += _trackdelta[*trackdir ^ 1].y;
 
	}
 

	
 
@@ -822,18 +817,16 @@ CommandCost CmdRemoveRailroadTrack(TileI
 
 *
 
 * @todo When checking for the tile slope,
 
 * distingush between "Flat land required" and "land sloped in wrong direction"
 
 */
 
CommandCost CmdBuildTrainDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Slope tileh;
 

	
 
	/* check railtype and valid direction for depot (0 through 3), 4 in total */
 
	if (!ValParamRailtype((RailType)p1)) return CMD_ERROR;
 

	
 
	tileh = GetTileSlope(tile, NULL);
 
	Slope tileh = GetTileSlope(tile, NULL);
 

	
 
	DiagDirection dir = Extract<DiagDirection, 0>(p2);
 

	
 
	/* Prohibit construction if
 
	 * The tile is non-flat AND
 
	 * 1) build-on-slopes is disabled
 
@@ -897,13 +890,12 @@ CommandCost CmdBuildSingleSignal(TileInd
 
	bool ctrl_pressed = HasBit(p1, 3); // was the CTRL button pressed
 
	SignalVariant sigvar = (ctrl_pressed ^ HasBit(p1, 4)) ? SIG_SEMAPHORE : SIG_ELECTRIC; // the signal variant of the new signal
 
	SignalType sigtype = (SignalType)GB(p1, 5, 3); // the signal type of the new signal
 
	bool convert_signal = HasBit(p1, 8); // convert button pressed
 
	SignalType cycle_start = (SignalType)GB(p1, 9, 3);
 
	SignalType cycle_stop = (SignalType)GB(p1, 12, 3);
 
	CommandCost cost;
 
	uint num_dir_cycle = GB(p1, 15, 2);
 

	
 
	if (sigtype > SIGTYPE_LAST) return CMD_ERROR;
 

	
 
	/* You can only build signals on plain rail tiles, and the selected track must exist */
 
	if (!ValParamTrackOrientation(track) || !IsPlainRailTile(tile) ||
 
@@ -929,12 +921,13 @@ CommandCost CmdBuildSingleSignal(TileInd
 
	/* In case we don't want to change an existing signal, return without error. */
 
	if (HasBit(p1, 17) && HasSignalOnTrack(tile, track)) return CommandCost();
 

	
 
	/* you can not convert a signal if no signal is on track */
 
	if (convert_signal && !HasSignalOnTrack(tile, track)) return CMD_ERROR;
 

	
 
	CommandCost cost;
 
	if (!HasSignalOnTrack(tile, track)) {
 
		/* build new signals */
 
		cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_SIGNALS]);
 
	} else {
 
		if (p2 != 0 && sigvar != GetSignalVariant(tile, track)) {
 
			/* convert signals <-> semaphores */
 
@@ -1114,28 +1107,25 @@ static bool CheckSignalAutoFill(TileInde
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
static CommandCost CmdSignalTrackHelper(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	CommandCost ret, total_cost(EXPENSES_CONSTRUCTION);
 
	int signal_ctr;
 
	byte signals;
 
	bool err = true;
 
	TileIndex end_tile;
 
	TileIndex start_tile = tile;
 

	
 
	Track track = (Track)GB(p2, 0, 3);
 
	bool mode = HasBit(p2, 3);
 
	bool semaphores = HasBit(p2, 4);
 
	bool remove = HasBit(p2, 5);
 
	bool autofill = HasBit(p2, 6);
 
	Trackdir trackdir = TrackToTrackdir(track);
 
	byte signal_density = GB(p2, 24, 8);
 

	
 
	if (p1 >= MapSize()) return CMD_ERROR;
 
	end_tile = p1;
 
	TileIndex end_tile = p1;
 
	if (signal_density == 0 || signal_density > 20) return CMD_ERROR;
 

	
 
	if (!IsPlainRailTile(tile)) return CMD_ERROR;
 

	
 
	/* for vertical/horizontal tracks, double the given signals density
 
	 * since the original amount will be too dense (shorter tracks) */
 
@@ -1149,12 +1139,13 @@ static CommandCost CmdSignalTrackHelper(
 
	/* Must start on a valid track to be able to avoid loops */
 
	if (!HasTrack(tile, track)) return CMD_ERROR;
 

	
 
	SignalType sigtype = (SignalType)GB(p2, 7, 3);
 
	if (sigtype > SIGTYPE_LAST) return CMD_ERROR;
 

	
 
	byte signals;
 
	/* copy the signal-style of the first rail-piece if existing */
 
	if (HasSignalOnTrack(tile, track)) {
 
		signals = GetPresentSignals(tile) & SignalOnTrack(track);
 
		assert(signals != 0);
 

	
 
		/* copy signal/semaphores style (independent of CTRL) */
 
@@ -1176,13 +1167,13 @@ static CommandCost CmdSignalTrackHelper(
 
	 **********
 
	 * trackdir   - trackdir to build with autorail
 
	 * semaphores - semaphores or signals
 
	 * signals    - is there a signal/semaphore on the first tile, copy its style (two-way/single-way)
 
	 *              and convert all others to semaphore/signal
 
	 * remove     - 1 remove signals, 0 build signals */
 
	signal_ctr = 0;
 
	int signal_ctr = 0;
 
	for (;;) {
 
		/* only build/remove signals with the specified density */
 
		if ((remove && autofill) || signal_ctr % signal_density == 0) {
 
			uint32 p1 = GB(TrackdirToTrack(trackdir), 0, 3);
 
			SB(p1, 3, 1, mode);
 
			SB(p1, 4, 1, semaphores);
 
@@ -1552,13 +1543,12 @@ static CommandCost RemoveTrainDepot(Tile
 
	return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_TRAIN]);
 
}
 

	
 
static CommandCost ClearTile_Track(TileIndex tile, DoCommandFlag flags)
 
{
 
	CommandCost cost(EXPENSES_CONSTRUCTION);
 
	CommandCost ret;
 

	
 
	if (flags & DC_AUTO) {
 
		if (!IsTileOwner(tile, _current_company)) {
 
			return_cmd_error(STR_ERROR_AREA_IS_OWNED_BY_ANOTHER);
 
		}
 

	
 
@@ -1576,13 +1566,13 @@ static CommandCost ClearTile_Track(TileI
 
			/* Is there flat water on the lower halftile, that gets cleared expensively? */
 
			bool water_ground = (GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(tileh));
 

	
 
			TrackBits tracks = GetTrackBits(tile);
 
			while (tracks != TRACK_BIT_NONE) {
 
				Track track = RemoveFirstTrack(&tracks);
 
				ret = DoCommand(tile, 0, track, flags, CMD_REMOVE_SINGLE_RAIL);
 
				CommandCost ret = DoCommand(tile, 0, track, flags, CMD_REMOVE_SINGLE_RAIL);
 
				if (ret.Failed()) return CMD_ERROR;
 
				cost.AddCost(ret);
 
			}
 

	
 
			/* when bankrupting, don't make water dirty, there could be a ship on lower halftile */
 
			if (water_ground && !(flags & DC_BANKRUPT)) {
 
@@ -2137,13 +2127,12 @@ static void DrawSignals(TileIndex tile, 
 
	}
 
}
 

	
 
static void DrawTile_Track(TileInfo *ti)
 
{
 
	const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
 
	SpriteID image;
 

	
 
	_drawtile_track_palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
 

	
 
	if (IsPlainRail(ti->tile)) {
 
		TrackBits rails = GetTrackBits(ti->tile);
 

	
 
@@ -2166,12 +2155,13 @@ static void DrawTile_Track(TileInfo *ti)
 
			/* Draw rail instead of depot */
 
			dts = &_depot_invisible_gfx_table[GetRailDepotDirection(ti->tile)];
 
		} else {
 
			dts = &_depot_gfx_table[GetRailDepotDirection(ti->tile)];
 
		}
 

	
 
		SpriteID image;
 
		if (rti->UsesOverlay()) {
 
			image = SPR_FLAT_GRASS_TILE;
 
		} else {
 
			image = dts->ground.sprite;
 
			if (image != SPR_FLAT_GRASS_TILE) image += rti->total_offset;
 
		}
 
@@ -2639,36 +2629,31 @@ int TicksToLeaveDepot(const Train *v)
 
}
 

	
 
/** Tile callback routine when vehicle enters tile
 
 * @see vehicle_enter_tile_proc */
 
static VehicleEnterTileStatus VehicleEnter_Track(Vehicle *u, TileIndex tile, int x, int y)
 
{
 
	byte fract_coord;
 
	byte fract_coord_leave;
 
	DiagDirection dir;
 
	int length;
 

	
 
	/* this routine applies only to trains in depot tiles */
 
	if (u->type != VEH_TRAIN || !IsRailDepotTile(tile)) return VETSB_CONTINUE;
 

	
 
	Train *v = Train::From(u);
 

	
 
	/* depot direction */
 
	dir = GetRailDepotDirection(tile);
 
	DiagDirection dir = GetRailDepotDirection(tile);
 

	
 
	/* calculate the point where the following wagon should be activated
 
	 * this depends on the length of the current vehicle */
 
	length = v->tcache.cached_veh_length;
 

	
 
	fract_coord_leave =
 
	int length = v->tcache.cached_veh_length;
 

	
 
	byte fract_coord_leave =
 
		((_fractcoords_enter[dir] & 0x0F) + // x
 
			(length + 1) * _deltacoord_leaveoffset[dir]) +
 
		(((_fractcoords_enter[dir] >> 4) +  // y
 
			((length + 1) * _deltacoord_leaveoffset[dir + 4])) << 4);
 

	
 
	fract_coord = (x & 0xF) + ((y & 0xF) << 4);
 
	byte fract_coord = (x & 0xF) + ((y & 0xF) << 4);
 

	
 
	if (_fractcoords_behind[dir] == fract_coord) {
 
		/* make sure a train is not entering the tile from behind */
 
		return VETSB_CANNOT_ENTER;
 
	} else if (_fractcoords_enter[dir] == fract_coord) {
 
		if (DiagDirToDir(ReverseDiagDir(dir)) == v->direction) {
0 comments (0 inline, 0 general)