mirror of https://github.com/OpenTTD/OpenTTD
(svn r3985) Make CmdBuildSingleRail() a bit more comprehensible
parent
a751a796e1
commit
9f3aa9057d
82
rail_cmd.c
82
rail_cmd.c
|
@ -393,83 +393,69 @@ int32 CmdRemoveSingleRail(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
||||||
|
|
||||||
tile = TileVirtXY(x, y);
|
tile = TileVirtXY(x, y);
|
||||||
|
|
||||||
if (!IsTileType(tile, MP_TUNNELBRIDGE) && !IsTileType(tile, MP_STREET) && !IsTileType(tile, MP_RAILWAY))
|
switch (GetTileType(tile)) {
|
||||||
return CMD_ERROR;
|
|
||||||
|
|
||||||
if (_current_player != OWNER_WATER && !CheckTileOwnership(tile))
|
|
||||||
return CMD_ERROR;
|
|
||||||
|
|
||||||
// allow building rail under bridge
|
|
||||||
if (!IsTileType(tile, MP_TUNNELBRIDGE) && !EnsureNoVehicle(tile))
|
|
||||||
return CMD_ERROR;
|
|
||||||
|
|
||||||
switch (GetTileType(tile))
|
|
||||||
{
|
|
||||||
case MP_TUNNELBRIDGE:
|
case MP_TUNNELBRIDGE:
|
||||||
if (!IsBridge(tile) ||
|
if (!IsBridge(tile) ||
|
||||||
!IsBridgeMiddle(tile) ||
|
!IsBridgeMiddle(tile) ||
|
||||||
!IsTransportUnderBridge(tile) ||
|
!IsTransportUnderBridge(tile) ||
|
||||||
GetTransportTypeUnderBridge(tile) != TRANSPORT_RAIL ||
|
GetTransportTypeUnderBridge(tile) != TRANSPORT_RAIL ||
|
||||||
GetRailBitsUnderBridge(tile) != trackbit ||
|
GetRailBitsUnderBridge(tile) != trackbit ||
|
||||||
|
(_current_player != OWNER_WATER && !CheckTileOwnership(tile)) ||
|
||||||
!EnsureNoVehicleZ(tile, TilePixelHeight(tile))) {
|
!EnsureNoVehicleZ(tile, TilePixelHeight(tile))) {
|
||||||
return CMD_ERROR;
|
return CMD_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(flags & DC_EXEC))
|
if (flags & DC_EXEC) SetClearUnderBridge(tile);
|
||||||
return _price.remove_rail;
|
|
||||||
|
|
||||||
SetClearUnderBridge(tile);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MP_STREET: {
|
case MP_STREET: {
|
||||||
if (!IsLevelCrossing(tile)) return CMD_ERROR;
|
if (!IsLevelCrossing(tile) ||
|
||||||
|
GetCrossingRailBits(tile) != trackbit ||
|
||||||
|
(_current_player != OWNER_WATER && !CheckTileOwnership(tile)) ||
|
||||||
|
!EnsureNoVehicle(tile)) {
|
||||||
|
return CMD_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
/* This is a crossing, let's check if the direction is correct */
|
if (flags & DC_EXEC) {
|
||||||
if (GetCrossingRailBits(tile) != trackbit) return CMD_ERROR;
|
MakeRoadNormal(tile, _m[tile].m3, GetCrossingRoadBits(tile), _m[tile].m2);
|
||||||
|
}
|
||||||
if (!(flags & DC_EXEC))
|
|
||||||
return _price.remove_rail;
|
|
||||||
|
|
||||||
MakeRoadNormal(tile, _m[tile].m3, GetCrossingRoadBits(tile), _m[tile].m2);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case MP_RAILWAY:
|
case MP_RAILWAY: {
|
||||||
if (!IsPlainRailTile(tile))
|
TrackBits present;
|
||||||
return CMD_ERROR;
|
|
||||||
|
|
||||||
/* See if the track to remove is actually there */
|
if (!IsPlainRailTile(tile) ||
|
||||||
if (!(GetTrackBits(tile) & trackbit))
|
(_current_player != OWNER_WATER && !CheckTileOwnership(tile)) ||
|
||||||
|
!EnsureNoVehicle(tile)) {
|
||||||
return CMD_ERROR;
|
return CMD_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
present = GetTrackBits(tile);
|
||||||
|
if ((present & trackbit) == 0) return CMD_ERROR;
|
||||||
|
|
||||||
/* Charge extra to remove signals on the track, if they are there */
|
/* Charge extra to remove signals on the track, if they are there */
|
||||||
if (HasSignalOnTrack(tile, track))
|
if (HasSignalOnTrack(tile, track))
|
||||||
cost += DoCommand(x, y, track, 0, flags, CMD_REMOVE_SIGNALS);
|
cost += DoCommand(x, y, track, 0, flags, CMD_REMOVE_SIGNALS);
|
||||||
|
|
||||||
if (!(flags & DC_EXEC))
|
if (flags & DC_EXEC) {
|
||||||
return cost;
|
present ^= trackbit;
|
||||||
|
if (present == 0) {
|
||||||
/* We remove the trackbit here. */
|
DoClearSquare(tile);
|
||||||
_m[tile].m5 &= ~trackbit;
|
} else {
|
||||||
|
SetTrackBits(tile, present);
|
||||||
if (GetTrackBits(tile) == 0) {
|
}
|
||||||
/* The tile has no tracks left, it is no longer a rail tile */
|
|
||||||
DoClearSquare(tile);
|
|
||||||
/* XXX: This is an optimisation, right? Is it really worth the ugly goto? */
|
|
||||||
goto skip_mark_dirty;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default: return CMD_ERROR;
|
||||||
assert(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* mark_dirty */
|
if (flags & DC_EXEC) {
|
||||||
MarkTileDirtyByTile(tile);
|
MarkTileDirtyByTile(tile);
|
||||||
|
SetSignalsOnBothDir(tile, track);
|
||||||
skip_mark_dirty:;
|
}
|
||||||
|
|
||||||
SetSignalsOnBothDir(tile, track);
|
|
||||||
|
|
||||||
return cost;
|
return cost;
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,6 +110,11 @@ static inline TrackBits GetTrackBits(TileIndex tile)
|
||||||
return (TrackBits)GB(_m[tile].m5, 0, 6);
|
return (TrackBits)GB(_m[tile].m5, 0, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void SetTrackBits(TileIndex t, TrackBits b)
|
||||||
|
{
|
||||||
|
SB(_m[t].m5, 0, 6, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static inline DiagDirection GetRailDepotDirection(TileIndex t)
|
static inline DiagDirection GetRailDepotDirection(TileIndex t)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue