Mercurial > hg > openttd
changeset 8088:5d7c61206209 draft
(svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
author | smatz <smatz@openttd.org> |
---|---|
date | Sun, 16 Dec 2007 19:30:42 +0000 |
parents | 4537e595b8b6 |
children | 353987b7661f |
files | src/ai/trolly/pathfinder.cpp src/elrail.cpp src/npf.cpp src/openttd.cpp src/pathfind.cpp src/rail.cpp src/rail_cmd.cpp src/rail_gui.cpp src/road_cmd.cpp src/road_map.cpp src/roadveh_cmd.cpp src/smallmap_gui.cpp src/town_cmd.cpp src/train_cmd.cpp src/tunnelbridge_cmd.cpp |
diffstat | 15 files changed, 84 insertions(+), 190 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ai/trolly/pathfinder.cpp +++ b/src/ai/trolly/pathfinder.cpp @@ -45,10 +45,7 @@ return // MP_ROAD, but not a road depot? (IsTileType(tile, MP_ROAD) && !IsTileDepotType(tile, TRANSPORT_ROAD)) || - (IsTileType(tile, MP_TUNNELBRIDGE) && ( - (IsTunnel(tile) && GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) || - (IsBridge(tile) && GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) - )); + (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD); } @@ -236,11 +233,7 @@ // If the next step is a bridge, we have to enter it the right way if (!PathFinderInfo->rail_or_road && IsRoad(atile)) { if (IsTileType(atile, MP_TUNNELBRIDGE)) { - if (IsTunnel(atile)) { - if (GetTunnelBridgeDirection(atile) != i) continue; - } else { - if (GetTunnelBridgeDirection(atile) != i) continue; - } + if (GetTunnelBridgeDirection(atile) != i) continue; } }
--- a/src/elrail.cpp +++ b/src/elrail.cpp @@ -93,17 +93,11 @@ break; case MP_TUNNELBRIDGE: - if (IsTunnel(t)) { - if (GetRailType(t) != RAILTYPE_ELECTRIC) return TRACK_BIT_NONE; - if (override != NULL) *override = 1 << GetTunnelBridgeDirection(t); - return AxisToTrackBits(DiagDirToAxis(GetTunnelBridgeDirection(t))); - } else { - if (GetRailType(t) != RAILTYPE_ELECTRIC) return TRACK_BIT_NONE; - if (override != NULL && DistanceMax(t, GetOtherBridgeEnd(t)) > 1) { - *override = 1 << GetTunnelBridgeDirection(t); - } - return AxisToTrackBits(DiagDirToAxis(GetTunnelBridgeDirection(t))); + if (GetRailType(t) != RAILTYPE_ELECTRIC) return TRACK_BIT_NONE; + if (override != NULL && (IsTunnel(t) || DistanceMax(t, GetOtherBridgeEnd(t)) > 1)) { + *override = 1 << GetTunnelBridgeDirection(t); } + return AxisToTrackBits(DiagDirToAxis(GetTunnelBridgeDirection(t))); case MP_ROAD: if (GetRoadTileType(t) != ROAD_TILE_CROSSING) return TRACK_BIT_NONE;
--- a/src/npf.cpp +++ b/src/npf.cpp @@ -481,8 +481,7 @@ break; case MP_TUNNELBRIDGE: - if ((IsTunnel(tile) && GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) || - (IsBridge(tile) && GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL)) { + if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) { return IsTileOwner(tile, owner); } break; @@ -534,14 +533,11 @@ DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile); /* Find dest tile */ - if (IsTunnelTile(src_tile) && GetTunnelBridgeDirection(src_tile) == src_exitdir) { - /* This is a tunnel. We know this tunnel is our type, + if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) { + /* This is a tunnel/bridge. We know this tunnel/bridge is our type, * otherwise we wouldn't have got here. It is also facing us, * so we should skip it's body */ - dst_tile = GetOtherTunnelEnd(src_tile); - override_dst_check = true; - } else if (IsBridgeTile(src_tile) && GetTunnelBridgeDirection(src_tile) == src_exitdir) { - dst_tile = GetOtherBridgeEnd(src_tile); + dst_tile = IsTunnel(src_tile) ? GetOtherTunnelEnd(src_tile) : GetOtherBridgeEnd(src_tile); override_dst_check = true; } else if (type != TRANSPORT_WATER && (IsStandardRoadStopTile(src_tile) || IsTileDepotType(src_tile, type))) { /* This is a road station (non drive-through) or a train or road depot. We can enter and exit @@ -590,14 +586,9 @@ /* I can't enter a tunnel entry/exit tile from a tile above the tunnel. Note * that I can enter the tunnel from a tile below the tunnel entrance. This * solves the problem of vehicles wanting to drive off a tunnel entrance */ - if (!override_dst_check) { - if (IsTileType(dst_tile, MP_TUNNELBRIDGE)) { - if (IsTunnel(dst_tile)) { - if (GetTunnelBridgeDirection(dst_tile) != src_exitdir) return; - } else { - if (GetTunnelBridgeDirection(dst_tile) != src_exitdir) return; - } - } + if (!override_dst_check && IsTileType(dst_tile, MP_TUNNELBRIDGE) && + GetTunnelBridgeDirection(dst_tile) != src_exitdir) { + return; } /* check correct rail type (mono, maglev, etc) */
--- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1759,14 +1759,8 @@ break; case MP_TUNNELBRIDGE: - if (IsTunnel(t)) { - if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) { - SetRailType(t, UpdateRailType(GetRailType(t), min_rail)); - } - } else { - if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) { - SetRailType(t, UpdateRailType(GetRailType(t), min_rail)); - } + if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) { + SetRailType(t, UpdateRailType(GetRailType(t), min_rail)); } break;
--- a/src/pathfind.cpp +++ b/src/pathfind.cpp @@ -260,16 +260,9 @@ /* Check if the new tile is a tunnel or bridge head and that the direction * and transport type match */ if (IsTileType(tile, MP_TUNNELBRIDGE)) { - if (IsTunnel(tile)) { - if (GetTunnelBridgeDirection(tile) != direction || - GetTunnelBridgeTransportType(tile) != tpf->tracktype) { - return; - } - } else if (IsBridge(tile)) { - if (GetTunnelBridgeDirection(tile) != direction || - GetTunnelBridgeTransportType(tile) != tpf->tracktype) { - return; - } + if (GetTunnelBridgeDirection(tile) != direction || + GetTunnelBridgeTransportType(tile) != tpf->tracktype) { + return; } }
--- a/src/rail.cpp +++ b/src/rail.cpp @@ -132,11 +132,7 @@ break; case MP_TUNNELBRIDGE: - if (IsTunnel(tile)) { - if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) return GetRailType(tile); - } else { - if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) return GetRailType(tile); - } + if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) return GetRailType(tile); break; default:
--- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -926,17 +926,15 @@ return true; case MP_TUNNELBRIDGE: { - TileIndex orig_tile = tile; - /* Skip to end of tunnel or bridge */ - if (IsBridge(tile)) { - if (GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL) return false; - if (GetTunnelBridgeDirection(tile) != TrackdirToExitdir(trackdir)) return false; - tile = GetOtherBridgeEnd(tile); - } else { - if (GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL) return false; - if (GetTunnelBridgeDirection(tile) != TrackdirToExitdir(trackdir)) return false; - tile = GetOtherTunnelEnd(tile); - } + TileIndex orig_tile = tile; // backup old value + + if (GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL) return false; + if (GetTunnelBridgeDirection(tile) != TrackdirToExitdir(trackdir)) return false; + + /* Skip to end of tunnel or bridge + * note that tile is a parameter by reference, so it must be updated */ + tile = IsTunnel(tile) ? GetOtherTunnelEnd(tile) : GetOtherBridgeEnd(tile); + signal_ctr += 2 + DistanceMax(orig_tile, tile) * 2; return true; }
--- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1522,12 +1522,8 @@ RailType count[RAILTYPE_END]; memset(count, 0, sizeof(count)); for (TileIndex t = 0; t < MapSize(); t++) { - if (IsTileType(t, MP_RAILWAY) || - IsLevelCrossingTile(t) || - IsRailwayStationTile(t) || - (IsTunnelTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) || - (IsBridgeTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) - ) { + if (IsTileType(t, MP_RAILWAY) || IsLevelCrossingTile(t) || IsRailwayStationTile(t) || + (IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL)) { count[GetRailType(t)]++; } }
--- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -133,15 +133,8 @@ case MP_TUNNELBRIDGE: { - TileIndex endtile; - if (IsTunnel(tile)) { - if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR; - endtile = GetOtherTunnelEnd(tile); - } else { - if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR; - endtile = GetOtherBridgeEnd(tile); - } - + if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR; + TileIndex endtile = IsTunnel(tile) ? GetOtherTunnelEnd(tile) : GetOtherBridgeEnd(tile); if (GetVehicleTunnelBridge(tile, endtile) != NULL) return CMD_ERROR; } break; @@ -514,15 +507,11 @@ case MP_TUNNELBRIDGE: { - TileIndex endtile; - if (IsTunnel(tile)) { - if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR; - endtile = GetOtherTunnelEnd(tile); - } else { - if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR; - endtile = GetOtherBridgeEnd(tile); - } + if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR; if (HasBit(GetRoadTypes(tile), rt)) return_cmd_error(STR_1007_ALREADY_BUILT); + + TileIndex endtile = IsTunnel(tile) ? GetOtherTunnelEnd(tile) : GetOtherBridgeEnd(tile); + /* Don't allow "upgrading" the bridge/tunnel when vehicles are already driving on it */ if (GetVehicleTunnelBridge(tile, endtile) != NULL) return CMD_ERROR; } break;
--- a/src/road_map.cpp +++ b/src/road_map.cpp @@ -34,13 +34,8 @@ return DiagDirToRoadBits(GetRoadStopDir(tile)); case MP_TUNNELBRIDGE: - if (IsTunnel(tile)) { - if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return ROAD_NONE; - return DiagDirToRoadBits(ReverseDiagDir(GetTunnelBridgeDirection(tile))); - } else { - if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return ROAD_NONE; - return DiagDirToRoadBits(ReverseDiagDir(GetTunnelBridgeDirection(tile))); - } + if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return ROAD_NONE; + return DiagDirToRoadBits(ReverseDiagDir(GetTunnelBridgeDirection(tile))); default: return ROAD_NONE; }
--- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -551,8 +551,7 @@ if (IsTileType(v->tile, MP_ROAD) && GetRoadTileType(v->tile) == ROAD_TILE_NORMAL && GetDisallowedRoadDirections(v->tile) != DRD_NONE) return CMD_ERROR; - if (IsTunnelTile(v->tile) && DirToDiagDir(v->direction) == GetTunnelBridgeDirection(v->tile)) return CMD_ERROR; - if (IsBridgeTile(v->tile) && DirToDiagDir(v->direction) == GetTunnelBridgeDirection(v->tile)) return CMD_ERROR; + if (IsTileType(v->tile, MP_TUNNELBRIDGE) && DirToDiagDir(v->direction) == GetTunnelBridgeDirection(v->tile)) return CMD_ERROR; if (flags & DC_EXEC) v->u.road.reverse_ctr = 180; @@ -1407,9 +1406,7 @@ if (prev_state == RVSB_WORMHOLE || prev_state == RVSB_IN_DEPOT) { DiagDirection diag_dir = INVALID_DIAGDIR; - if (IsTunnelTile(tile)) { - diag_dir = GetTunnelBridgeDirection(tile); - } else if (IsBridgeTile(tile)) { + if (IsTileType(tile, MP_TUNNELBRIDGE)) { diag_dir = GetTunnelBridgeDirection(tile); } else if (IsTileType(tile, MP_ROAD) && GetRoadTileType(tile) == ROAD_TILE_DEPOT) { diag_dir = ReverseDiagDir(GetRoadDepotDirection(tile));
--- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -339,13 +339,8 @@ TileType t = GetTileType(tile); if (t == MP_TUNNELBRIDGE) { - TransportType tt; + TransportType tt = GetTunnelBridgeTransportType(tile); - if (IsTunnel(tile)) { - tt = GetTunnelBridgeTransportType(tile); - } else { - tt = GetTunnelBridgeTransportType(tile); - } switch (tt) { case TRANSPORT_RAIL: t = MP_RAILWAY; break; case TRANSPORT_ROAD: t = MP_ROAD; break;
--- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -1064,10 +1064,8 @@ /* Reached a tunnel/bridge? Then continue at the other side of it. */ if (IsTileType(tile, MP_TUNNELBRIDGE)) { - if (IsTunnel(tile) && GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) { - *tile_ptr = GetOtherTunnelEnd(tile); - } else if (IsBridge(tile) && GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) { - *tile_ptr = GetOtherBridgeEnd(tile); + if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) { + *tile_ptr = IsTunnel(tile) ? GetOtherTunnelEnd(tile) : GetOtherBridgeEnd(tile); } return; }
--- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -3059,7 +3059,7 @@ if (GetVehicleTunnelBridge(v->tile, endtile) != NULL) return; // tunnel / bridge is busy - DiagDirection dir = IsTunnel(v->tile) ? GetTunnelBridgeDirection(v->tile) : GetTunnelBridgeDirection(v->tile); + DiagDirection dir = GetTunnelBridgeDirection(v->tile); /* v->direction is "random", so it cannot be used to determine the direction of the track */ UpdateSignalsOnSegment(v->tile, dir); @@ -3176,7 +3176,7 @@ TileIndex tile = v->tile; if (IsTileType(tile, MP_TUNNELBRIDGE)) { - DiagDirection dir = IsTunnel(tile) ? GetTunnelBridgeDirection(tile) : GetTunnelBridgeDirection(tile); + DiagDirection dir = GetTunnelBridgeDirection(tile); if (DiagDirToDir(dir) == v->direction) return true; }
--- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -702,63 +702,39 @@ */ CommandCost DoConvertTunnelBridgeRail(TileIndex tile, RailType totype, bool exec) { - if (IsTunnel(tile) && GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) { - TileIndex endtile = GetOtherTunnelEnd(tile); - - /* If not coverting rail <-> el. rail, any vehicle cannot be in tunnel */ - if (!IsCompatibleRail(GetRailType(tile), totype) && - GetVehicleTunnelBridge(tile, endtile) != NULL) { - return CMD_ERROR; - } + if (GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL) return CMD_ERROR; - if (exec) { - SetRailType(tile, totype); - SetRailType(endtile, totype); - MarkTileDirtyByTile(tile); - MarkTileDirtyByTile(endtile); - - Track track = AxisToTrack(DiagDirToAxis(GetTunnelBridgeDirection(tile))); - - YapfNotifyTrackLayoutChange(tile, track); - YapfNotifyTrackLayoutChange(endtile, track); - - VehicleFromPos(tile, NULL, &UpdateTrainPowerProc); - VehicleFromPos(endtile, NULL, &UpdateTrainPowerProc); - } - - return CommandCost((DistanceManhattan(tile, endtile) + 1) * RailConvertCost(GetRailType(tile), totype)); - } else if (IsBridge(tile) && GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) { - TileIndex endtile = GetOtherBridgeEnd(tile); + TileIndex endtile = IsTunnel(tile) ? GetOtherTunnelEnd(tile) : GetOtherBridgeEnd(tile); - if (!IsCompatibleRail(GetRailType(tile), totype) && - GetVehicleTunnelBridge(tile, endtile) != NULL) { - return CMD_ERROR; - } - - if (exec) { - SetRailType(tile, totype); - SetRailType(endtile, totype); - MarkTileDirtyByTile(tile); - MarkTileDirtyByTile(endtile); - - Track track = AxisToTrack(DiagDirToAxis(GetTunnelBridgeDirection(tile))); - TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile)); - - YapfNotifyTrackLayoutChange(tile, track); - YapfNotifyTrackLayoutChange(endtile, track); - - VehicleFromPos(tile, NULL, &UpdateTrainPowerProc); - VehicleFromPos(endtile, NULL, &UpdateTrainPowerProc); - - for (tile += delta; tile != endtile; tile += delta) { - MarkTileDirtyByTile(tile); // TODO encapsulate this into a function - } - } - - return CommandCost((DistanceManhattan(tile, endtile) + 1) * RailConvertCost(GetRailType(tile), totype)); - } else { + /* If not coverting rail <-> el. rail, any vehicle cannot be in tunnel/bridge */ + if (!IsCompatibleRail(GetRailType(tile), totype) && + GetVehicleTunnelBridge(tile, endtile) != NULL) { return CMD_ERROR; } + + if (exec) { + SetRailType(tile, totype); + SetRailType(endtile, totype); + + Track track = AxisToTrack(DiagDirToAxis(GetTunnelBridgeDirection(tile))); + + YapfNotifyTrackLayoutChange(tile, track); + YapfNotifyTrackLayoutChange(endtile, track); + + VehicleFromPos(tile, NULL, &UpdateTrainPowerProc); + VehicleFromPos(endtile, NULL, &UpdateTrainPowerProc); + + MarkTileDirtyByTile(tile); + MarkTileDirtyByTile(endtile); + + if (IsBridge(tile)) { + TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile)); + TileIndex t = tile + delta; + for (; t != endtile; t += delta) MarkTileDirtyByTile(t); // TODO encapsulate this into a function + } + } + + return CommandCost((DistanceManhattan(tile, endtile) + 1) * RailConvertCost(GetRailType(tile), totype)); } @@ -1261,29 +1237,24 @@ static void TileLoop_TunnelBridge(TileIndex tile) { - bool snow_or_desert = IsTunnelTile(tile) ? HasTunnelBridgeSnowOrDesert(tile) : HasTunnelBridgeSnowOrDesert(tile); + bool snow_or_desert = HasTunnelBridgeSnowOrDesert(tile); switch (_opt.landscape) { case LT_ARCTIC: if (snow_or_desert != (GetTileZ(tile) > GetSnowLine())) { - if (IsTunnelTile(tile)) { - SetTunnelBridgeSnowOrDesert(tile, !snow_or_desert); - } else { - SetTunnelBridgeSnowOrDesert(tile, !snow_or_desert); - } + SetTunnelBridgeSnowOrDesert(tile, !snow_or_desert); MarkTileDirtyByTile(tile); } break; case LT_TROPIC: if (GetTropicZone(tile) == TROPICZONE_DESERT && !snow_or_desert) { - if (IsTunnelTile(tile)) { - SetTunnelBridgeSnowOrDesert(tile, true); - } else { - SetTunnelBridgeSnowOrDesert(tile, true); - } + SetTunnelBridgeSnowOrDesert(tile, true); MarkTileDirtyByTile(tile); } break; + + default: + break; } } @@ -1295,15 +1266,9 @@ static uint32 GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode, uint sub_mode) { - if (IsTunnel(tile)) { - if (GetTunnelBridgeTransportType(tile) != mode) return 0; - if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD && (GetRoadTypes(tile) & sub_mode) == 0) return 0; - return AxisToTrackBits(DiagDirToAxis(GetTunnelBridgeDirection(tile))) * 0x101; - } else { - if (GetTunnelBridgeTransportType(tile) != mode) return 0; - if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD && (GetRoadTypes(tile) & sub_mode) == 0) return 0; - return AxisToTrackBits(DiagDirToAxis(GetTunnelBridgeDirection(tile))) * 0x101; - } + if (GetTunnelBridgeTransportType(tile) != mode) return 0; + if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD && (GetRoadTypes(tile) & sub_mode) == 0) return 0; + return AxisToTrackBits(DiagDirToAxis(GetTunnelBridgeDirection(tile))) * 0x101; } static void ChangeTileOwner_TunnelBridge(TileIndex tile, PlayerID old_player, PlayerID new_player) @@ -1318,7 +1283,7 @@ * the bridge/tunnel. As all *our* vehicles are already removed, they * must be of another owner. Therefor this must be a road bridge/tunnel. * In that case we can safely reassign the ownership to OWNER_NONE. */ - assert((IsTunnel(tile) ? GetTunnelBridgeTransportType(tile) : GetTunnelBridgeTransportType(tile)) == TRANSPORT_ROAD); + assert(GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD); SetTileOwner(tile, OWNER_NONE); } }