From d95e2c2dd10a0dfc1704962a68a2bd32b635d158 Mon Sep 17 00:00:00 2001 From: celestar Date: Wed, 27 Dec 2006 12:38:02 +0000 Subject: [PATCH] (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals) --- BUGS | 8 + ai/default/default.c | 5 +- bridge.h | 1 + bridge_map.c | 34 +- bridge_map.h | 159 ++------ clear_cmd.c | 31 +- debug.h | 6 +- docs/landscape.html | 27 +- docs/landscape_grid.html | 13 +- elrail.c | 70 ++-- landscape.c | 3 + misc.c | 39 +- npf.c | 46 ++- openttd.c | 89 ++++- pathfind.c | 98 ++--- rail.c | 22 +- rail_cmd.c | 77 +--- rail_map.h | 10 - road_cmd.c | 153 +++----- road_map.c | 18 +- roadveh_cmd.c | 18 +- saveload.c | 2 +- station_cmd.c | 5 + table/sprites.h | 21 ++ terraform_gui.c | 18 +- town_cmd.c | 6 + train_cmd.c | 57 ++- tree_cmd.c | 16 +- tunnelbridge_cmd.c | 779 +++++++++++++++------------------------ unmovable_cmd.c | 2 + variables.h | 1 - vehicle.h | 1 - water_cmd.c | 79 ++-- waypoint.c | 3 + yapf/follow_track.hpp | 61 ++- yapf/yapf.h | 1 + yapf/yapf_costbase.hpp | 2 +- yapf/yapf_rail.cpp | 42 +-- yapf/yapf_road.cpp | 1 - 39 files changed, 845 insertions(+), 1179 deletions(-) diff --git a/BUGS b/BUGS index a95afd696e..d031e8b3e9 100644 --- a/BUGS +++ b/BUGS @@ -2,6 +2,14 @@ KNOWN BUGS / PROBLEMS: +bridges: +- Clearing tiles which may not have a bridge above can cause random bridge pieces to appear, which leads to crash in the long run + +electrified rails: Normal and elrail depots look the same. Use 'X' (transparent buildings) to distinguish between them Missing curors / icons for construction (currently using the conventional ones) + +TODO: +- Reallow the AI to build signals under bridges (about ai/default/default.c:1600) as + r4911 manually disallows it (not via the command) diff --git a/ai/default/default.c b/ai/default/default.c index f21a9174a6..0f69322b07 100644 --- a/ai/default/default.c +++ b/ai/default/default.c @@ -2183,9 +2183,7 @@ static bool AiRemoveTileAndGoForward(Player *p) return false; p->ai.cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDiagDir(p->ai.cur_dir_a)); return true; - } - - if (IsBridgeRamp(tile)) { + } else { // Check if the bridge points in the right direction. // This is not really needed the first place AiRemoveTileAndGoForward is called. if (DiagDirToAxis(GetBridgeRampDirection(tile)) != (p->ai.cur_dir_a & 1U)) return false; @@ -3704,7 +3702,6 @@ pos_3: } else if (IsTileType(tile, MP_TUNNELBRIDGE)) { if (!IsTileOwner(tile, _current_player) || !IsBridge(tile) || - !IsBridgeRamp(tile) || GetBridgeTransportType(tile) != TRANSPORT_RAIL) { return; } diff --git a/bridge.h b/bridge.h index b7000513d9..7a63e28d7d 100644 --- a/bridge.h +++ b/bridge.h @@ -27,5 +27,6 @@ extern const Bridge orig_bridge[MAX_BRIDGES]; extern Bridge _bridge[MAX_BRIDGES]; uint GetBridgeFoundation(Slope tileh, Axis axis); +uint SetSpeedLimitOnBridge(Vehicle *); #endif /* BRIDGE_H */ diff --git a/bridge_map.c b/bridge_map.c index c9bd59df93..8e2405ea34 100644 --- a/bridge_map.c +++ b/bridge_map.c @@ -3,22 +3,28 @@ #include "stdafx.h" #include "openttd.h" #include "bridge_map.h" +#include "variables.h" TileIndex GetBridgeEnd(TileIndex tile, DiagDirection dir) { TileIndexDiff delta = TileOffsByDiagDir(dir); - assert(DiagDirToAxis(dir) == GetBridgeAxis(tile)); - + dir = ReverseDiagDir(dir); do { tile += delta; - } while (!IsBridgeRamp(tile)); + } while (!IsBridgeTile(tile) || GetBridgeRampDirection(tile) != dir); return tile; } +TileIndex GetNorthernBridgeEnd(TileIndex t) +{ + return GetBridgeEnd(t, ReverseDiagDir(AxisToDiagDir(GetBridgeAxis(t)))); +} + + TileIndex GetSouthernBridgeEnd(TileIndex t) { return GetBridgeEnd(t, AxisToDiagDir(GetBridgeAxis(t))); @@ -27,11 +33,19 @@ TileIndex GetSouthernBridgeEnd(TileIndex t) TileIndex GetOtherBridgeEnd(TileIndex tile) { - TileIndexDiff delta = TileOffsByDiagDir(GetBridgeRampDirection(tile)); - - do { - tile += delta; - } while (!IsBridgeRamp(tile)); - - return tile; + assert(IsBridgeTile(tile)); + return GetBridgeEnd(tile, GetBridgeRampDirection(tile)); +} + +uint GetBridgeHeight(TileIndex t) +{ + uint h; + uint tileh = GetTileSlope(t, &h); + uint f = GetBridgeFoundation(tileh, DiagDirToAxis(GetBridgeRampDirection(t))); + + // one height level extra if the ramp is on a flat foundation + return + h + TILE_HEIGHT + + (IS_INT_INSIDE(f, 1, 15) ? TILE_HEIGHT : 0) + + (IsSteepSlope(tileh) ? TILE_HEIGHT : 0); } diff --git a/bridge_map.h b/bridge_map.h index 4547c9dbbb..34da494bfb 100644 --- a/bridge_map.h +++ b/bridge_map.h @@ -11,6 +11,9 @@ #include "tile.h" +void DrawBridgeMiddle(const TileInfo* ti); // XXX + + static inline bool IsBridge(TileIndex t) { assert(IsTileType(t, MP_TUNNELBRIDGE)); @@ -23,28 +26,22 @@ static inline bool IsBridgeTile(TileIndex t) } -static inline bool IsBridgeRamp(TileIndex t) +static inline bool MayHaveBridgeAbove(TileIndex t) { - assert(IsBridgeTile(t)); - return !HASBIT(_m[t].m5, 6); -} - -static inline bool IsBridgeMiddle(TileIndex t) -{ - assert(IsBridgeTile(t)); - return HASBIT(_m[t].m5, 6); + return + IsTileType(t, MP_CLEAR) || + IsTileType(t, MP_RAILWAY) || + IsTileType(t, MP_STREET) || + IsTileType(t, MP_WATER) || + IsTileType(t, MP_TUNNELBRIDGE) || + IsTileType(t, MP_UNMOVABLE); } -/** - * Determines which piece of a bridge is contained in the current tile - * @param tile The tile to analyze - * @return the piece - */ -static inline uint GetBridgePiece(TileIndex t) +static inline bool IsBridgeAbove(TileIndex t) { - assert(IsBridgeMiddle(t)); - return GB(_m[t].m2, 0, 4); + assert(MayHaveBridgeAbove(t)); + return GB(_m[t].extra, 6, 2) != 0; } @@ -65,65 +62,22 @@ static inline uint GetBridgeType(TileIndex t) */ static inline DiagDirection GetBridgeRampDirection(TileIndex t) { - assert(IsBridgeRamp(t)); - return ReverseDiagDir(XYNSToDiagDir((Axis)GB(_m[t].m5, 0, 1), GB(_m[t].m5, 5, 1))); + assert(IsBridgeTile(t)); + return (DiagDirection)GB(_m[t].m5, 0, 2); } static inline Axis GetBridgeAxis(TileIndex t) { - assert(IsBridgeMiddle(t)); - return (Axis)GB(_m[t].m5, 0, 1); + assert(IsBridgeAbove(t)); + return (Axis)(GB(_m[t].extra, 6, 2) - 1); } static inline TransportType GetBridgeTransportType(TileIndex t) { assert(IsBridgeTile(t)); - return (TransportType)GB(_m[t].m5, 1, 2); -} - - -static inline bool IsClearUnderBridge(TileIndex t) -{ - assert(IsBridgeMiddle(t)); - return GB(_m[t].m5, 3, 3) == 0; -} - -static inline bool IsWaterUnderBridge(TileIndex t) -{ - assert(IsBridgeMiddle(t)); - return GB(_m[t].m5, 3, 3) == 1; -} - - -static inline bool IsTransportUnderBridge(TileIndex t) -{ - assert(IsBridgeMiddle(t)); - return HASBIT(_m[t].m5, 5); -} - -static inline TransportType GetTransportTypeUnderBridge(TileIndex t) -{ - assert(IsTransportUnderBridge(t)); - return (TransportType)GB(_m[t].m5, 3, 2); -} - -static inline RoadBits GetRoadBitsUnderBridge(TileIndex t) -{ - assert(GetTransportTypeUnderBridge(t) == TRANSPORT_ROAD); - return GetBridgeAxis(t) == AXIS_X ? ROAD_Y : ROAD_X; -} - -static inline Track GetRailUnderBridge(TileIndex t) -{ - assert(GetTransportTypeUnderBridge(t) == TRANSPORT_RAIL); - return AxisToTrack(OtherAxis(GetBridgeAxis(t))); -} - -static inline TrackBits GetRailBitsUnderBridge(TileIndex t) -{ - return TrackToTrackBits(GetRailUnderBridge(t)); + return (TransportType)GB(_m[t].m5, 2, 2); } @@ -132,6 +86,11 @@ static inline TrackBits GetRailBitsUnderBridge(TileIndex t) */ TileIndex GetBridgeEnd(TileIndex, DiagDirection); +/** + * Finds the northern end of a bridge starting at a middle tile + */ +TileIndex GetNorthernBridgeEnd(TileIndex t); + /** * Finds the southern end of a bridge starting at a middle tile */ @@ -143,58 +102,36 @@ TileIndex GetSouthernBridgeEnd(TileIndex t); */ TileIndex GetOtherBridgeEnd(TileIndex); -uint GetBridgeHeight(TileIndex t); +uint GetBridgeHeight(TileIndex tile); +uint GetBridgeFoundation(Slope tileh, Axis axis); -static inline void SetClearUnderBridge(TileIndex t) +static inline void ClearSingleBridgeMiddle(TileIndex t, Axis a) { - assert(IsBridgeMiddle(t)); - SetTileOwner(t, OWNER_NONE); - SB(_m[t].m5, 3, 3, 0 << 2 | 0); - SB(_m[t].m3, 0, 4, 0); + assert(MayHaveBridgeAbove(t)); + CLRBIT(_m[t].extra, 6 + a); } -static inline void SetWaterUnderBridge(TileIndex t) + +static inline void ClearBridgeMiddle(TileIndex t) { - assert(IsBridgeMiddle(t)); - SetTileOwner(t, OWNER_WATER); - SB(_m[t].m5, 3, 3, 0 << 2 | 1); - SB(_m[t].m3, 0, 4, 0); + ClearSingleBridgeMiddle(t, AXIS_X); + ClearSingleBridgeMiddle(t, AXIS_Y); } -static inline void SetCanalUnderBridge(TileIndex t, Owner o) +static inline void SetBridgeMiddle(TileIndex t, Axis a) { - assert(IsBridgeMiddle(t)); - SetTileOwner(t, o); - SB(_m[t].m5, 3, 3, 0 << 2 | 1); - SB(_m[t].m3, 0, 4, 0); -} - -static inline void SetRailUnderBridge(TileIndex t, Owner o, RailType r) -{ - assert(IsBridgeMiddle(t)); - SetTileOwner(t, o); - SB(_m[t].m5, 3, 3, 1 << 2 | TRANSPORT_RAIL); - SB(_m[t].m3, 0, 4, r); -} - -static inline void SetRoadUnderBridge(TileIndex t, Owner o) -{ - assert(IsBridgeMiddle(t)); - SetTileOwner(t, o); - SB(_m[t].m5, 3, 3, 1 << 2 | TRANSPORT_ROAD); - SB(_m[t].m3, 0, 4, 0); + assert(MayHaveBridgeAbove(t)); + SETBIT(_m[t].extra, 6 + a); } static inline void MakeBridgeRamp(TileIndex t, Owner o, uint bridgetype, DiagDirection d, TransportType tt) { - uint northsouth = (d == DIAGDIR_NE || d == DIAGDIR_NW); - SetTileType(t, MP_TUNNELBRIDGE); SetTileOwner(t, o); _m[t].m2 = bridgetype << 4; _m[t].m4 = 0; - _m[t].m5 = 1 << 7 | 0 << 6 | northsouth << 5 | tt << 1 | DiagDirToAxis(d); + _m[t].m5 = 1 << 7 | tt << 2 | d; } static inline void MakeRoadBridgeRamp(TileIndex t, Owner o, uint bridgetype, DiagDirection d) @@ -210,26 +147,4 @@ static inline void MakeRailBridgeRamp(TileIndex t, Owner o, uint bridgetype, Dia } -static inline void MakeBridgeMiddle(TileIndex t, uint bridgetype, uint piece, Axis a, TransportType tt) -{ - SetTileType(t, MP_TUNNELBRIDGE); - SetTileOwner(t, OWNER_NONE); - _m[t].m2 = bridgetype << 4 | piece; - _m[t].m3 = 0; - _m[t].m4 = 0; - _m[t].m5 = 1 << 7 | 1 << 6 | 0 << 5 | 0 << 3 | tt << 1 | a; -} - -static inline void MakeRoadBridgeMiddle(TileIndex t, uint bridgetype, uint piece, Axis a) -{ - MakeBridgeMiddle(t, bridgetype, piece, a, TRANSPORT_ROAD); -} - -static inline void MakeRailBridgeMiddle(TileIndex t, uint bridgetype, uint piece, Axis a, RailType r) -{ - MakeBridgeMiddle(t, bridgetype, piece, a, TRANSPORT_RAIL); - SB(_m[t].m3, 4, 4, r); -} - - #endif /* BRIDGE_MAP_H */ diff --git a/clear_cmd.c b/clear_cmd.c index 9ebd9e8bd3..4d1a78ba97 100644 --- a/clear_cmd.c +++ b/clear_cmd.c @@ -12,6 +12,7 @@ #include "viewport.h" #include "command.h" #include "tunnel_map.h" +#include "bridge_map.h" #include "variables.h" #include "table/sprites.h" #include "unmovable_map.h" @@ -276,25 +277,32 @@ int32 CmdTerraformLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) } } - if (direction == -1) { + { /* Check if tunnel would take damage */ int count; TileIndex *ti = ts.tile_table; for (count = ts.tile_table_count; count != 0; count--, ti++) { - uint z, t; TileIndex tile = *ti; - z = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 0)); - t = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 0)); - if (t <= z) z = t; - t = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 1)); - if (t <= z) z = t; - t = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 1)); - if (t <= z) z = t; + if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) { + return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); + } - if (IsTunnelInWay(tile, z * TILE_HEIGHT)) { - return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE); + if (direction == -1) { + uint z, t; + + z = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 0)); + t = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 0)); + if (t <= z) z = t; + t = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 1)); + if (t <= z) z = t; + t = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 1)); + if (t <= z) z = t; + + if (IsTunnelInWay(tile, z * TILE_HEIGHT)) { + return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE); + } } } } @@ -534,6 +542,7 @@ static void DrawTile_Clear(TileInfo *ti) } DrawClearLandFence(ti); + DrawBridgeMiddle(ti); } static uint GetSlopeZ_Clear(TileIndex tile, uint x, uint y) diff --git a/debug.h b/debug.h index 87c2862e52..f45db4d3fa 100644 --- a/debug.h +++ b/debug.h @@ -20,11 +20,7 @@ #ifdef NO_DEBUG_MESSAGES #define DEBUG(name, level, ...) #else - #if defined(__GNUC__) && (__GNUC__ < 3) - #define DEBUG(name, level, args...) if ((level == 0) || ( _debug_ ## name ## _level >= level)) debug(#name, args) - #else - #define DEBUG(name, level, ...) if (level == 0 || _debug_ ## name ## _level >= level) debug(#name, __VA_ARGS__) - #endif + #define DEBUG(name, level, ...) if (level == 0 || _debug_ ## name ## _level >= level) debug(#name, __VA_ARGS__) extern int _debug_ai_level; extern int _debug_driver_level; diff --git a/docs/landscape.html b/docs/landscape.html index 4383fab8ac..30f71dff4f 100644 --- a/docs/landscape.html +++ b/docs/landscape.html @@ -554,31 +554,12 @@ m5 bits 7..4 clear: tunnel entrance/exit
  • m3 bits 3..0 = track type for railway tunnel, must be 0 for road tunnel
  • m4 bit 7 set = on snow or desert
  • -m5 bit 7 set: bridge -