Codechange: Use the new 'frame' variable to handle road vehicles entering or leaving a tunnel. diff -r 7f3c27c4bece src/tunnelbridge_cmd.cpp --- a/src/tunnelbridge_cmd.cpp Fri Oct 22 16:28:12 2010 +0200 +++ b/src/tunnelbridge_cmd.cpp Fri Oct 22 16:53:33 2010 +0200 @@ -1495,12 +1495,18 @@ * to the 'outside', i.e. at (TILE_SIZE-1) - (frame) + 1 */ static const byte _train_tunnel_frame[DIAGDIR_END] = {14, 9, 7, 12}; -static const byte _road_exit_tunnel_frame[4] = {2, 7, 9, 4}; +/** Frame when a road vehicle enters a tunnel with a certain direction. + * This differs per direction, like for trains. To make it even more fun, + * the entry and exit frames are not consistent. This is the entry frame, + * the road vehicle should be hidden when it reaches this frame.*/ +static const byte _road_enter_tunnel_frame[DIAGDIR_END] = {13, 8, 8, 13}; -static const byte _tunnel_fractcoord_4[4] = {0x52, 0x85, 0x98, 0x29}; -static const byte _tunnel_fractcoord_5[4] = {0x92, 0x89, 0x58, 0x25}; -static const byte _tunnel_fractcoord_6[4] = {0x92, 0x89, 0x56, 0x45}; -static const byte _tunnel_fractcoord_7[4] = {0x52, 0x85, 0x96, 0x49}; +/** Frame when a road vehicle exits a tunnel with a certain direction. + * Note that 'direction' refers to the tunnel direction, not the + * vehicle direction. As stated above, this frame is not the same as the + * entry frame, for unclear (historical?) reasons. */ +static const byte _road_exit_tunnel_frame[DIAGDIR_END] = {2, 7, 9, 4}; + static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y) { @@ -1517,8 +1523,6 @@ byte frame = (vdir == DIAGDIR_NE || vdir == DIAGDIR_NW) ? TILE_SIZE - 1 - pos : pos; if (IsTunnel(tile)) { - byte fc = (x & 0xF) + (y << 4); - if (v->type == VEH_TRAIN) { Train *t = Train::From(v); @@ -1550,8 +1554,9 @@ /* Enter tunnel? */ if (rv->state != RVSB_WORMHOLE && dir == vdir) { - if (fc == _tunnel_fractcoord_4[dir] || - fc == _tunnel_fractcoord_5[dir]) { + if (frame == _road_enter_tunnel_frame[dir]) { + /* Frame should be equal to the next frame number in the RV's movement */ + assert(frame == rv->frame + 1); rv->tile = tile; rv->state = RVSB_WORMHOLE; rv->vehstatus |= VS_HIDDEN; @@ -1561,15 +1566,11 @@ } } - if (dir == ReverseDiagDir(vdir) && ( - /* We're at the tunnel exit ?? */ - fc == _tunnel_fractcoord_6[dir] || - fc == _tunnel_fractcoord_7[dir] - ) && - z == 0) { + /* We're at the tunnel exit ?? */ + if (dir == ReverseDiagDir(vdir) && frame == _road_exit_tunnel_frame[dir] && z == 0) { rv->tile = tile; rv->state = DiagDirToDiagTrackdir(vdir); - rv->frame = _road_exit_tunnel_frame[dir]; + rv->frame = frame; rv->vehstatus &= ~VS_HIDDEN; return VETSB_ENTERED_WORMHOLE; }