diff --git a/source.list b/source.list index b63ccc58de..125198c23c 100644 --- a/source.list +++ b/source.list @@ -404,6 +404,7 @@ transparency.h transparency_gui.h transport_type.h tunnelbridge.h +tunnel_base.h vehicle_base.h vehicle_func.h vehicle_gui.h diff --git a/src/tunnel_base.h b/src/tunnel_base.h new file mode 100644 index 0000000000..f03325eabc --- /dev/null +++ b/src/tunnel_base.h @@ -0,0 +1,40 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file tunnel_base.h Base for all tunnels */ + +#ifndef TUNNEL_BASE_H +#define TUNNEL_BASE_H + +#include "tunnel_map.h" +#include "core/pool_type.hpp" + +struct Tunnel; + +typedef Pool TunnelPool; +extern TunnelPool _tunnel_pool; + +struct Tunnel : TunnelPool::PoolItem<&_tunnel_pool> { + + TileIndex tile_n; // North tile of tunnel. + TileIndex tile_s; // South tile of tunnel. + + Tunnel(TileIndex tile_n = INVALID_TILE) : tile_n(tile_n) {} + ~Tunnel(); + + static inline Tunnel *GetByTile(TileIndex tile) + { + return Tunnel::Get(GetTunnelIndex(tile)); + } +}; + +#define FOR_ALL_TUNNELS_FROM(var, start) FOR_ALL_ITEMS_FROM(Tunnel, tunnel_index, var, start) +#define FOR_ALL_TUNNELS(var) FOR_ALL_TUNNELS_FROM(var, 0) + +#endif /* TUNNEL_BASE_H */ diff --git a/src/tunnel_map.cpp b/src/tunnel_map.cpp index 4e6d5a7e13..5c6ab5d598 100644 --- a/src/tunnel_map.cpp +++ b/src/tunnel_map.cpp @@ -12,8 +12,21 @@ #include "stdafx.h" #include "tunnelbridge_map.h" +#include "core/pool_func.hpp" + #include "safeguards.h" +/** All tunnel portals tucked away in a pool. */ +TunnelPool _tunnel_pool("Tunnel"); +INSTANTIATE_POOL_METHODS(Tunnel) + +/** + * Clean up a tunnel tile + */ +Tunnel::~Tunnel() +{ + if (CleaningPool()) return; +} /** * Gets the other end of the tunnel. Where a vehicle would reappear when it diff --git a/src/tunnel_map.h b/src/tunnel_map.h index ec798587a4..c1c1e45e24 100644 --- a/src/tunnel_map.h +++ b/src/tunnel_map.h @@ -14,6 +14,7 @@ #include "road_map.h" +typedef uint16 TunnelID; ///< Type for the unique identifier of tunnels. /** * Is this a tunnel (entrance)? @@ -37,6 +38,18 @@ static inline bool IsTunnelTile(TileIndex t) return IsTileType(t, MP_TUNNELBRIDGE) && IsTunnel(t); } +/** + * Get the index of tunnel tile. + * @param t the tile + * @pre IsTunnelTile(t) + * @return TunnelID + */ +static inline TunnelID GetTunnelIndex(TileIndex t) +{ + assert(IsTunnelTile(t)); + return _m[t].m2; +} + TileIndex GetOtherTunnelEnd(TileIndex); bool IsTunnelInWay(TileIndex, int z); bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir); @@ -48,11 +61,11 @@ bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir); * @param d the direction facing out of the tunnel * @param r the road type used in the tunnel */ -static inline void MakeRoadTunnel(TileIndex t, Owner o, DiagDirection d, RoadTypes r) +static inline void MakeRoadTunnel(TileIndex t, Owner o, TunnelID id, DiagDirection d, RoadTypes r) { SetTileType(t, MP_TUNNELBRIDGE); SetTileOwner(t, o); - _m[t].m2 = 0; + _m[t].m2 = id; _m[t].m3 = 0; _m[t].m4 = 0; _m[t].m5 = TRANSPORT_ROAD << 2 | d; @@ -70,11 +83,11 @@ static inline void MakeRoadTunnel(TileIndex t, Owner o, DiagDirection d, RoadTyp * @param d the direction facing out of the tunnel * @param r the rail type used in the tunnel */ -static inline void MakeRailTunnel(TileIndex t, Owner o, DiagDirection d, RailType r) +static inline void MakeRailTunnel(TileIndex t, Owner o,TunnelID id, DiagDirection d, RailType r) { SetTileType(t, MP_TUNNELBRIDGE); SetTileOwner(t, o); - _m[t].m2 = 0; + _m[t].m2 = id; SB(_m[t].m1, 7, 1, GB(r, 4, 1)); SB(_m[t].m3, 0, 4, GB(r, 0, 4)); SB(_m[t].m3, 4, 4, 0); @@ -84,4 +97,6 @@ static inline void MakeRailTunnel(TileIndex t, Owner o, DiagDirection d, RailTyp _me[t].m7 = 0; } + + #endif /* TUNNEL_MAP_H */ diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index 6cd8c04367..e5c20dffe2 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -27,6 +27,7 @@ #include "autoslope.h" #include "tunnelbridge_map.h" #include "bridge_signal_map.h" +#include "tunnel_base.h" #include "strings_func.h" #include "date_func.h" #include "clear_func.h" @@ -49,6 +50,7 @@ #include "safeguards.h" + BridgeSpec _bridge[MAX_BRIDGES]; ///< The specification of all bridges. TileIndex _build_tunnel_endtile; ///< The end of a tunnel; as hidden return from the tunnel build command for GUI purposes. @@ -747,10 +749,20 @@ CommandCost CmdBuildTunnel(TileIndex start_tile, DoCommandFlag flags, uint32 p1, if (flags & DC_EXEC) { Company *c = Company::GetIfValid(company); uint num_pieces = (tiles + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR; + + /* The most northern tile first. */ + TileIndex tn = start_tile; + TileIndex ts = end_tile; + if(start_tile > end_tile) Swap(tn, ts); + + if (!Tunnel::CanAllocateItem()) return CMD_ERROR; + Tunnel *t = new Tunnel(tn); + t->tile_s = ts; + if (transport_type == TRANSPORT_RAIL) { if (!IsTunnelTile(start_tile) && c != NULL) c->infrastructure.rail[railtype] += num_pieces; - MakeRailTunnel(start_tile, company, direction, railtype); - MakeRailTunnel(end_tile, company, ReverseDiagDir(direction), railtype); + MakeRailTunnel(start_tile, company, t->index, direction, railtype); + MakeRailTunnel(end_tile, company, t->index, ReverseDiagDir(direction), railtype); AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, company); YapfNotifyTrackLayoutChange(start_tile, DiagDirToDiagTrack(direction)); } else { @@ -760,8 +772,8 @@ CommandCost CmdBuildTunnel(TileIndex start_tile, DoCommandFlag flags, uint32 p1, c->infrastructure.road[rt] += num_pieces * 2; // A full diagonal road has two road bits. } } - MakeRoadTunnel(start_tile, company, direction, rts); - MakeRoadTunnel(end_tile, company, ReverseDiagDir(direction), rts); + MakeRoadTunnel(start_tile, company, t->index, direction, rts); + MakeRoadTunnel(end_tile, company, t->index, ReverseDiagDir(direction), rts); } DirtyCompanyInfrastructureWindows(company); } @@ -873,6 +885,8 @@ static CommandCost DoClearTunnel(TileIndex tile, DoCommandFlag flags) DirtyCompanyInfrastructureWindows(owner); } + delete Tunnel::GetByTile(tile); + DoClearSquare(tile); DoClearSquare(endtile); @@ -895,6 +909,8 @@ static CommandCost DoClearTunnel(TileIndex tile, DoCommandFlag flags) } } + delete Tunnel::GetByTile(tile); + DoClearSquare(tile); DoClearSquare(endtile); } diff --git a/src/tunnelbridge_map.h b/src/tunnelbridge_map.h index 150bf80a0b..98f6e9190d 100644 --- a/src/tunnelbridge_map.h +++ b/src/tunnelbridge_map.h @@ -13,7 +13,7 @@ #define TUNNELBRIDGE_MAP_H #include "bridge_map.h" -#include "tunnel_map.h" +#include "tunnel_base.h" #include "cmd_helper.h" #include "signal_type.h"