From 5a7c9037709772e4678ee57d9b42f5332bd66f97 Mon Sep 17 00:00:00 2001 From: smatz Date: Wed, 23 Jan 2008 22:34:04 +0000 Subject: [PATCH] (svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead --- projects/openttd.vcproj | 3 +++ projects/openttd_vs80.vcproj | 4 ++++ projects/openttd_vs90.vcproj | 4 ++++ source.list | 1 + src/ai/default/default.cpp | 5 +++-- src/ai/trolly/build.cpp | 4 +++- src/ai/trolly/pathfinder.cpp | 7 ++++--- src/bridge.h | 1 - src/bridge_gui.cpp | 3 ++- src/elrail.cpp | 7 ++++--- src/npf.cpp | 7 +++---- src/pathfind.cpp | 40 +++++++----------------------------- src/pathfind.h | 7 ------- src/rail_cmd.cpp | 5 +++-- src/road_cmd.cpp | 5 +++-- src/tunnelbridge.h | 26 +++++++++++++++++++++++ src/tunnelbridge_cmd.cpp | 20 +++++------------- src/yapf/follow_track.hpp | 38 +++++++++++++--------------------- src/yapf/yapf.hpp | 1 + 19 files changed, 90 insertions(+), 98 deletions(-) create mode 100644 src/tunnelbridge.h diff --git a/projects/openttd.vcproj b/projects/openttd.vcproj index 5f47f21399..45d91cc329 100644 --- a/projects/openttd.vcproj +++ b/projects/openttd.vcproj @@ -708,6 +708,9 @@ + + diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj index 43fb0e9bcc..4bd4c4cf75 100644 --- a/projects/openttd_vs80.vcproj +++ b/projects/openttd_vs80.vcproj @@ -1175,6 +1175,10 @@ RelativePath=".\..\src\transparency_gui.h" > + + diff --git a/projects/openttd_vs90.vcproj b/projects/openttd_vs90.vcproj index 7e43c891e8..9b81614af7 100644 --- a/projects/openttd_vs90.vcproj +++ b/projects/openttd_vs90.vcproj @@ -1172,6 +1172,10 @@ RelativePath=".\..\src\transparency_gui.h" > + + diff --git a/source.list b/source.list index 05023b1d3d..4b182cf248 100644 --- a/source.list +++ b/source.list @@ -202,6 +202,7 @@ town.h train.h transparency.h transparency_gui.h +tunnelbridge.h variables.h vehicle.h vehicle_gui.h diff --git a/src/ai/default/default.cpp b/src/ai/default/default.cpp index 2ea82bcda2..49bd07fe94 100644 --- a/src/ai/default/default.cpp +++ b/src/ai/default/default.cpp @@ -31,6 +31,7 @@ #include "../../player_base.h" #include "../../settings_type.h" #include "default.h" +#include "../../tunnelbridge.h" #include "../../table/ai_rail.h" @@ -2173,7 +2174,7 @@ static void AiBuildRailConstruct(Player *p) if (arf.best_ptr[0] & 0x80) { int i; - int32 bridge_len = GetBridgeLength(arf.bridge_end_tile, _players_ai[p->index].cur_tile_a); + int32 bridge_len = GetTunnelBridgeLength(arf.bridge_end_tile, _players_ai[p->index].cur_tile_a); /* Figure out which (rail)bridge type to build * start with best bridge, then go down to worse and worse bridges @@ -3065,7 +3066,7 @@ do_some_terraform: int i; int32 bridge_len; _players_ai[p->index].cur_tile_a = arf.bridge_end_tile; - bridge_len = GetBridgeLength(tile, _players_ai[p->index].cur_tile_a); // tile + bridge_len = GetTunnelBridgeLength(tile, _players_ai[p->index].cur_tile_a); // tile /* Figure out what (road)bridge type to build * start with best bridge, then go down to worse and worse bridges diff --git a/src/ai/trolly/build.cpp b/src/ai/trolly/build.cpp index 329c54d34f..c6bf330e73 100644 --- a/src/ai/trolly/build.cpp +++ b/src/ai/trolly/build.cpp @@ -15,6 +15,8 @@ #include "../../player_base.h" #include "../../player_func.h" #include "../ai.h" +#include "../../tunnelbridge.h" + // Build HQ // Params: @@ -58,7 +60,7 @@ CommandCost AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, by int bridge_type, bridge_len, type, type2; // Find a good bridgetype (the best money can buy) - bridge_len = GetBridgeLength(tile_a, tile_b); + bridge_len = GetTunnelBridgeLength(tile_a, tile_b); type = type2 = 0; for (bridge_type = MAX_BRIDGES-1; bridge_type >= 0; bridge_type--) { if (CheckBridge_Stuff(bridge_type, bridge_len)) { diff --git a/src/ai/trolly/pathfinder.cpp b/src/ai/trolly/pathfinder.cpp index caef801476..3580f48a3b 100644 --- a/src/ai/trolly/pathfinder.cpp +++ b/src/ai/trolly/pathfinder.cpp @@ -14,6 +14,7 @@ #include "../../variables.h" #include "../../player_base.h" #include "../../player_func.h" +#include "../../tunnelbridge.h" #define TEST_STATION_NO_DIR 0xFF @@ -320,7 +321,7 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr new_tile += TileOffsByDiagDir(dir); // Precheck, is the length allowed? - if (!CheckBridge_Stuff(0, GetBridgeLength(tile, new_tile))) break; + if (!CheckBridge_Stuff(0, GetTunnelBridgeLength(tile, new_tile))) break; // Check if we hit the station-tile.. we don't like that! if (TILES_BETWEEN(new_tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br)) break; @@ -425,14 +426,14 @@ static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current, int r; // Tunnels are very expensive when build on long routes.. // Ironicly, we are using BridgeCode here ;) - r = AI_PATHFINDER_TUNNEL_PENALTY * GetBridgeLength(current->tile, parent->path.node.tile); + r = AI_PATHFINDER_TUNNEL_PENALTY * GetTunnelBridgeLength(current->tile, parent->path.node.tile); res += r + (r >> 8); } // Are we part of a bridge? if ((AI_PATHFINDER_FLAG_BRIDGE & current->user_data[0]) != 0) { // That means for every length a penalty - res += AI_PATHFINDER_BRIDGE_PENALTY * GetBridgeLength(current->tile, parent->path.node.tile); + res += AI_PATHFINDER_BRIDGE_PENALTY * GetTunnelBridgeLength(current->tile, parent->path.node.tile); // Check if we are going up or down, first for the starting point // In user_data[0] is at the 8th bit the direction if (!HasBridgeFlatRamp(parent_tileh, (Axis)((current->user_data[0] >> 8) & 1))) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY; diff --git a/src/bridge.h b/src/bridge.h index 0f17980b16..2df66ae2cd 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -43,7 +43,6 @@ static inline const Bridge *GetBridge(uint i) void DrawBridgeMiddle(const TileInfo *ti); bool CheckBridge_Stuff(byte bridge_type, uint bridge_len); -uint32 GetBridgeLength(TileIndex begin, TileIndex end); int CalcBridgeLenCostFactor(int x); #endif /* BRIDGE_H */ diff --git a/src/bridge_gui.cpp b/src/bridge_gui.cpp index 596ac11076..5b74e0cded 100644 --- a/src/bridge_gui.cpp +++ b/src/bridge_gui.cpp @@ -16,6 +16,7 @@ #include "map_func.h" #include "viewport_func.h" #include "gfx_func.h" +#include "tunnelbridge.h" #include "table/strings.h" @@ -182,7 +183,7 @@ void ShowBuildBridgeWindow(TileIndex start, TileIndex end, byte bridge_type) /* check which bridges can be built * get absolute bridge length * length of the middle parts of the bridge */ - const uint bridge_len = GetBridgeLength(start, end); + const uint bridge_len = GetTunnelBridgeLength(start, end); /* total length of bridge */ const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2); diff --git a/src/elrail.cpp b/src/elrail.cpp index 154590b9bb..0e429e04e8 100644 --- a/src/elrail.cpp +++ b/src/elrail.cpp @@ -64,6 +64,7 @@ #include "tunnelbridge_map.h" #include "vehicle_func.h" #include "player_base.h" +#include "tunnelbridge.h" #include "table/sprites.h" #include "table/elrail_data.h" @@ -383,8 +384,8 @@ static void DrawCatenaryOnBridge(const TileInfo *ti) TileIndex end = GetSouthernBridgeEnd(ti->tile); TileIndex start = GetOtherBridgeEnd(end); - uint length = GetBridgeLength(start, end); - uint num = DistanceMax(ti->tile, start); + uint length = GetTunnelBridgeLength(start, end); + uint num = GetTunnelBridgeLength(ti->tile, start) + 1; uint height; const SortableSpriteStruct *sss; @@ -421,7 +422,7 @@ static void DrawCatenaryOnBridge(const TileInfo *ti) } /* need a pylon on the southern end of the bridge */ - if (DistanceMax(ti->tile, start) == length) { + if (GetTunnelBridgeLength(ti->tile, start) + 1 == length) { DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_SW : DIAGDIR_SE); Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE); if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos); diff --git a/src/npf.cpp b/src/npf.cpp index 31e791674b..582413c632 100644 --- a/src/npf.cpp +++ b/src/npf.cpp @@ -22,6 +22,7 @@ #include "functions.h" #include "vehicle_base.h" #include "settings_type.h" +#include "tunnelbridge.h" static AyStar _npf_aystar; @@ -176,9 +177,7 @@ static uint NPFTunnelCost(AyStarNode* current) if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) { /* We just popped out if this tunnel, since were * facing the tunnel exit */ - FindLengthOfTunnelResult flotr; - flotr = FindLengthOfTunnel(tile, ReverseDiagDir(exitdir)); - return flotr.length * NPF_TILE_LENGTH; + return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1); /* @todo: Penalty for tunnels? */ } else { /* We are entering the tunnel, the enter tile is just a @@ -189,7 +188,7 @@ static uint NPFTunnelCost(AyStarNode* current) static inline uint NPFBridgeCost(AyStarNode *current) { - return NPF_TILE_LENGTH * GetBridgeLength(current->tile, GetOtherBridgeEnd(current->tile)); + return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile)); } static uint NPFSlopeCost(AyStarNode* current) diff --git a/src/pathfind.cpp b/src/pathfind.cpp index 5b1d1b69fb..06374666ba 100644 --- a/src/pathfind.cpp +++ b/src/pathfind.cpp @@ -17,6 +17,7 @@ #include "depot.h" #include "tunnelbridge_map.h" #include "core/random_func.hpp" +#include "tunnelbridge.h" /* remember which tiles we have already visited so we don't visit them again. */ static bool TPFSetTileBit(TrackPathFinder *tpf, TileIndex tile, int dir) @@ -192,32 +193,6 @@ continue_here: } - -/* Returns the end tile and the length of a tunnel. The length does not - * include the starting tile (entry), it does include the end tile (exit). - */ -FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, DiagDirection dir) -{ - TileIndexDiff delta = TileOffsByDiagDir(dir); - uint z = GetTileZ(tile); - FindLengthOfTunnelResult flotr; - - flotr.length = 0; - - dir = ReverseDiagDir(dir); - do { - flotr.length++; - tile += delta; - } while( - !IsTunnelTile(tile) || - GetTunnelBridgeDirection(tile) != dir || - GetTileZ(tile) != z - ); - - flotr.tile = tile; - return flotr; -} - /** * Checks if any vehicle can enter/leave tile in given diagdir * Checks only for rail/road depots and road non-drivethrough stations @@ -257,7 +232,7 @@ static void TPFMode1(TrackPathFinder* tpf, TileIndex tile, DiagDirection directi if (dir == direction) { TileIndex endtile = GetOtherTunnelBridgeEnd(tile); - tpf->rd.cur_length += DistanceManhattan(tile, endtile); + tpf->rd.cur_length += GetTunnelBridgeLength(tile, endtile) + 1; TPFSetTileBit(tpf, tile, 14); TPFSetTileBit(tpf, endtile, 14); @@ -665,8 +640,6 @@ start_at: if (IsTileType(tile, MP_TUNNELBRIDGE)) { if (IsTunnel(tile)) { if (GetTunnelBridgeDirection(tile) != ReverseDiagDir(direction)) { - FindLengthOfTunnelResult flotr; - /* We are not just driving out of the tunnel */ if (GetTunnelBridgeDirection(tile) != direction || GetTunnelBridgeTransportType(tile) != tpf->tracktype) { @@ -677,9 +650,10 @@ start_at: bits = TRACK_BIT_NONE; break; } - flotr = FindLengthOfTunnel(tile, direction); - si.cur_length += flotr.length * DIAG_FACTOR; - tile = flotr.tile; + + TileIndex endtile = GetOtherTunnelEnd(tile); + si.cur_length += DIAG_FACTOR * (GetTunnelBridgeLength(tile, endtile) + 1); + tile = endtile; /* tile now points to the exit tile of the tunnel */ } } else { // IsBridge(tile) @@ -693,7 +667,7 @@ start_at: } } tile_end = GetOtherBridgeEnd(tile); - si.cur_length += DistanceManhattan(tile, tile_end) * DIAG_FACTOR; + si.cur_length += DIAG_FACTOR * (GetTunnelBridgeLength(tile, tile_end) + 1); tile = tile_end; } } diff --git a/src/pathfind.h b/src/pathfind.h index 53eb6062a9..bafcad9056 100644 --- a/src/pathfind.h +++ b/src/pathfind.h @@ -68,13 +68,6 @@ struct TrackPathFinder { }; void FollowTrack(TileIndex tile, uint16 flags, uint sub_type, DiagDirection direction, TPFEnumProc* enum_proc, TPFAfterProc* after_proc, void* data); - -struct FindLengthOfTunnelResult { - TileIndex tile; - int length; -}; -FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, DiagDirection direction); - void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data); #endif /* PATHFIND_H */ diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index 3c39cf2666..c646b3a5c5 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -40,6 +40,7 @@ #include "vehicle_func.h" #include "sound_func.h" #include "signal_func.h" +#include "tunnelbridge.h" #include "table/sprites.h" #include "table/strings.h" @@ -939,7 +940,7 @@ static bool CheckSignalAutoFill(TileIndex &tile, Trackdir &trackdir, int &signal * note that tile is a parameter by reference, so it must be updated */ tile = GetOtherTunnelBridgeEnd(tile); - signal_ctr += 2 + DistanceMax(orig_tile, tile) * 2; + signal_ctr += (GetTunnelBridgeLength(orig_tile, tile) + 2) * 2; return true; } @@ -1294,7 +1295,7 @@ CommandCost CmdConvertRail(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) } } - cost.AddCost((DistanceManhattan(tile, endtile) + 1) * RailConvertCost(type, totype)); + cost.AddCost((GetTunnelBridgeLength(tile, endtile) + 2) * RailConvertCost(type, totype)); } break; default: // MP_STATION, MP_ROAD diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index 7aaf7f8a3b..060b61a244 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -33,6 +33,7 @@ #include "vehicle_base.h" #include "sound_func.h" #include "road_func.h" +#include "tunnelbridge.h" #include "table/sprites.h" #include "table/strings.h" @@ -159,7 +160,7 @@ CommandCost CmdRemoveRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) if (IsTileType(tile, MP_TUNNELBRIDGE)) { TileIndex other_end = GetOtherTunnelBridgeEnd(tile); /* Pay for *every* tile of the bridge or tunnel */ - cost.AddCost((DistanceManhattan(other_end, tile) + 1) * _price.remove_road); + cost.AddCost((GetTunnelBridgeLength(other_end, tile) + 2) * _price.remove_road); if (flags & DC_EXEC) { SetRoadTypes(other_end, GetRoadTypes(other_end) & ~RoadTypeToRoadTypes(rt)); SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt)); @@ -551,7 +552,7 @@ do_clear:; cost.AddCost(CountBits(pieces) * _price.build_road); if (IsTileType(tile, MP_TUNNELBRIDGE)) { /* Pay for *every* tile of the bridge or tunnel */ - cost.MultiplyCost(DistanceManhattan(GetOtherTunnelBridgeEnd(tile), tile) + 1); + cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2); } if (flags & DC_EXEC) { diff --git a/src/tunnelbridge.h b/src/tunnelbridge.h new file mode 100644 index 0000000000..758ba59a56 --- /dev/null +++ b/src/tunnelbridge.h @@ -0,0 +1,26 @@ +/* $Id$ */ + +/** @file tunnelbridge.h Header file for things common for tunnels and bridges */ + +#ifndef TUNNELBRIDGE_H +#define TUNNELBRIDGE_H + +#include "tile_type.h" +#include "map_func.h" +#include "tunnelbridge_map.h" + +/** + * Calculates the length of a tunnel or a bridge (without end tiles) + * @return length of bridge/tunnel middle + */ +static inline uint GetTunnelBridgeLength(TileIndex begin, TileIndex end) +{ + int x1 = TileX(begin); + int y1 = TileY(begin); + int x2 = TileX(end); + int y2 = TileY(end); + + return abs(x2 + y2 - x1 - y1) - 1; +} + +#endif /* TUNNELBRIDGE_H */ diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index a8eee30540..aebc76581d 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -32,6 +32,7 @@ #include "vehicle_func.h" #include "sound_func.h" #include "signal_func.h" +#include "tunnelbridge.h" #include "table/sprites.h" #include "table/strings.h" @@ -160,17 +161,6 @@ static CommandCost CheckBridgeSlopeSouth(Axis axis, Slope *tileh, uint *z) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); } - -uint32 GetBridgeLength(TileIndex begin, TileIndex end) -{ - int x1 = TileX(begin); - int y1 = TileY(begin); - int x2 = TileX(end); - int y2 = TileY(end); - - return abs(x2 + y2 - x1 - y1) - 1; -} - bool CheckBridge_Stuff(byte bridge_type, uint bridge_len) { const Bridge *b = &_bridge[bridge_type]; @@ -633,7 +623,7 @@ static CommandCost DoClearTunnel(TileIndex tile, uint32 flags) DoClearSquare(endtile); } } - return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_tunnel * (DistanceManhattan(tile, endtile) + 1)); + return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_tunnel * (GetTunnelBridgeLength(tile, endtile) + 2)); } @@ -693,7 +683,7 @@ static CommandCost DoClearBridge(TileIndex tile, uint32 flags) } } - return CommandCost(EXPENSES_CONSTRUCTION, (DistanceManhattan(tile, endtile) + 1) * _price.clear_bridge); + return CommandCost(EXPENSES_CONSTRUCTION, (GetTunnelBridgeLength(tile, endtile) + 2) * _price.clear_bridge); } static CommandCost ClearTile_TunnelBridge(TileIndex tile, byte flags) @@ -1018,8 +1008,8 @@ void DrawBridgeMiddle(const TileInfo* ti) axis = GetBridgeAxis(ti->tile); piece = CalcBridgePiece( - DistanceManhattan(ti->tile, rampnorth), - DistanceManhattan(ti->tile, rampsouth) + GetTunnelBridgeLength(ti->tile, rampnorth) + 1, + GetTunnelBridgeLength(ti->tile, rampsouth) + 1 ); type = GetBridgeType(rampsouth); diff --git a/src/yapf/follow_track.hpp b/src/yapf/follow_track.hpp index d6e0552a0a..d4cd455b59 100644 --- a/src/yapf/follow_track.hpp +++ b/src/yapf/follow_track.hpp @@ -76,32 +76,22 @@ protected: m_is_station = m_is_bridge = m_is_tunnel = false; m_tiles_skipped = 0; - // extra handling for tunnels in our direction - if (IsTunnelTile(m_old_tile)) { - DiagDirection tunnel_enterdir = GetTunnelBridgeDirection(m_old_tile); - if (tunnel_enterdir == m_exitdir) { - // we are entering the tunnel - FindLengthOfTunnelResult flotr = FindLengthOfTunnel(m_old_tile, m_exitdir); - m_new_tile = flotr.tile; - m_is_tunnel = true; - m_tiles_skipped = flotr.length - 1; + // extra handling for tunnels and bridges in our direction + if (IsTileType(m_old_tile, MP_TUNNELBRIDGE)) { + DiagDirection enterdir = GetTunnelBridgeDirection(m_old_tile); + if (enterdir == m_exitdir) { + // we are entering the tunnel / bridge + if (IsTunnel(m_old_tile)) { + m_is_tunnel = true; + m_new_tile = GetOtherTunnelEnd(m_old_tile); + } else { // IsBridge(m_old_tile) + m_is_bridge = true; + m_new_tile = GetOtherBridgeEnd(m_old_tile); + } + m_tiles_skipped = GetTunnelBridgeLength(m_new_tile, m_old_tile); return; } - assert(ReverseDiagDir(tunnel_enterdir) == m_exitdir); - } - - // extra handling for bridge ramp in our direction - if (IsBridgeTile(m_old_tile)) { - DiagDirection bridge_enterdir = GetTunnelBridgeDirection(m_old_tile); - if (bridge_enterdir == m_exitdir) { - // we are entering the bridge ramp - m_new_tile = GetOtherBridgeEnd(m_old_tile); - uint32 bridge_length = GetBridgeLength(m_old_tile, m_new_tile); - m_tiles_skipped = bridge_length; - m_is_bridge = true; - return; - } - assert(ReverseDiagDir(bridge_enterdir) == m_exitdir); + assert(ReverseDiagDir(enterdir) == m_exitdir); } // normal or station tile, do one step diff --git a/src/yapf/yapf.hpp b/src/yapf/yapf.hpp index d728d0bfe6..b98dfdf4bd 100644 --- a/src/yapf/yapf.hpp +++ b/src/yapf/yapf.hpp @@ -23,6 +23,7 @@ #include "../waypoint.h" #include "../debug.h" #include "../settings_type.h" +#include "../tunnelbridge.h" extern uint64 _rdtsc();