tunnel_cmd.c
author celestar
Sat, 30 Dec 2006 13:15:15 +0000
branchcustombridgeheads
changeset 5603 f3aa14b91b0a
parent 5595 049ed4486972
permissions -rw-r--r--
(svn r7648) [cbh] - Feature: Allow removal of tracks from bridge heads. However, trains on the bridge must still be able to access the bridge head
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
2262
bd59b2d8d75f (svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents: 2261
diff changeset
     3
/** @file tunnelbridge_cmd.c
bd59b2d8d75f (svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents: 2261
diff changeset
     4
 * This file deals with tunnels and bridges (non-gui stuff)
bd59b2d8d75f (svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents: 2261
diff changeset
     5
 * @todo seperate this file into two
bd59b2d8d75f (svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents: 2261
diff changeset
     6
 */
bd59b2d8d75f (svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents: 2261
diff changeset
     7
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     8
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1884
diff changeset
     9
#include "openttd.h"
3189
1af302c5abd0 (svn r3846) Add functions to set the type of stuff (clear, water, rail, road) under bridges
tron
parents: 3188
diff changeset
    10
#include "bridge_map.h"
3187
0813719b05a9 (svn r3837) Partially unmagicfy restoring the tiles when deleting a bridge
tron
parents: 3184
diff changeset
    11
#include "rail_map.h"
3144
426b825578f9 (svn r3763) Adapt to the new 'map accessors go in foo_map.h'-scheme
tron
parents: 3069
diff changeset
    12
#include "road_map.h"
1363
01d3de5d8039 (svn r1867) Include tables/sprites.h only in files which need it
tron
parents: 1335
diff changeset
    13
#include "table/sprites.h"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 497
diff changeset
    14
#include "table/strings.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
    15
#include "functions.h"
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 541
diff changeset
    16
#include "map.h"
1209
a1ac96655b79 (svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents: 1192
diff changeset
    17
#include "tile.h"
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
    18
#include "tunnel_map.h"
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5116
diff changeset
    19
#include "unmovable_map.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#include "viewport.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
#include "player.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
#include "town.h"
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 334
diff changeset
    25
#include "sound.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    26
#include "variables.h"
2262
bd59b2d8d75f (svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents: 2261
diff changeset
    27
#include "bridge.h"
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2663
diff changeset
    28
#include "train.h"
3187
0813719b05a9 (svn r3837) Partially unmagicfy restoring the tiles when deleting a bridge
tron
parents: 3184
diff changeset
    29
#include "water_map.h"
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3881
diff changeset
    30
#include "yapf/yapf.h"
4261
2ec8f5a9747b (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4246
diff changeset
    31
#include "date.h"
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4559
diff changeset
    32
#include "newgrf_sound.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2260
diff changeset
    34
#include "table/bridge_land.h"
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2260
diff changeset
    35
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
    36
/** Build Tunnel.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3435
diff changeset
    37
 * @param tile start tile of tunnel
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1782
diff changeset
    38
 * @param p1 railtype, 0x200 for road tunnel
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
    39
 * @param p2 unused
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3435
diff changeset
    41
int32 CmdBuildTunnel(TileIndex start_tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
{
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    43
	TileIndexDiff delta;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    44
	TileIndex end_tile;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    45
	DiagDirection direction;
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    46
	Slope start_tileh;
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    47
	Slope end_tileh;
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    48
	uint start_z;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    49
	uint end_z;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    50
	int32 cost;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    51
	int32 ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    53
	_build_tunnel_endtile = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1782
diff changeset
    55
	if (p1 != 0x200 && !ValParamRailtype(p1)) return CMD_ERROR;
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
    56
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    57
	start_tileh = GetTileSlope(start_tile, &start_z);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    59
	switch (start_tileh) {
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    60
		case SLOPE_SW: direction = DIAGDIR_SW; break;
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    61
		case SLOPE_SE: direction = DIAGDIR_SE; break;
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    62
		case SLOPE_NW: direction = DIAGDIR_NW; break;
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    63
		case SLOPE_NE: direction = DIAGDIR_NE; break;
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    64
		default: return_cmd_error(STR_500B_SITE_UNSUITABLE_FOR_TUNNEL);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
    65
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3435
diff changeset
    67
	ret = DoCommand(start_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    68
	if (CmdFailed(ret)) return ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
4273
6467c2c54d58 (svn r5901) -Fix [FS#253]: Differing price calculation for tunnels depending on starting point
Darkvater
parents: 4261
diff changeset
    70
	/* XXX - do NOT change 'ret' in the loop, as it is used as the price
6467c2c54d58 (svn r5901) -Fix [FS#253]: Differing price calculation for tunnels depending on starting point
Darkvater
parents: 4261
diff changeset
    71
	 * for the clearing of the entrance of the tunnel. Assigning it to
6467c2c54d58 (svn r5901) -Fix [FS#253]: Differing price calculation for tunnels depending on starting point
Darkvater
parents: 4261
diff changeset
    72
	 * cost before the loop will yield different costs depending on start-
6467c2c54d58 (svn r5901) -Fix [FS#253]: Differing price calculation for tunnels depending on starting point
Darkvater
parents: 4261
diff changeset
    73
	 * position, because of increased-cost-by-length: 'cost += cost >> 3' */
6467c2c54d58 (svn r5901) -Fix [FS#253]: Differing price calculation for tunnels depending on starting point
Darkvater
parents: 4261
diff changeset
    74
	cost = 0;
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4549
diff changeset
    75
	delta = TileOffsByDiagDir(direction);
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    76
	end_tile = start_tile;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    77
	for (;;) {
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    78
		end_tile += delta;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    79
		end_tileh = GetTileSlope(end_tile, &end_z);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    81
		if (start_z == end_z) break;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    82
3156
028b6756b279 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    83
		if (!_cheats.crossing_tunnels.value && IsTunnelInWay(end_tile, start_z)) {
028b6756b279 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    84
			return_cmd_error(STR_5003_ANOTHER_TUNNEL_IN_THE_WAY);
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    85
		}
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    86
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    87
		cost += _price.build_tunnel;
4273
6467c2c54d58 (svn r5901) -Fix [FS#253]: Differing price calculation for tunnels depending on starting point
Darkvater
parents: 4261
diff changeset
    88
		cost += cost >> 3; // add a multiplier for longer tunnels
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    89
		if (cost >= 400000000) cost = 400000000;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
4273
6467c2c54d58 (svn r5901) -Fix [FS#253]: Differing price calculation for tunnels depending on starting point
Darkvater
parents: 4261
diff changeset
    92
	/* Add the cost of the entrance */
6467c2c54d58 (svn r5901) -Fix [FS#253]: Differing price calculation for tunnels depending on starting point
Darkvater
parents: 4261
diff changeset
    93
	cost += _price.build_tunnel + ret;
6467c2c54d58 (svn r5901) -Fix [FS#253]: Differing price calculation for tunnels depending on starting point
Darkvater
parents: 4261
diff changeset
    94
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    95
	// if the command fails from here on we want the end tile to be highlighted
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    96
	_build_tunnel_endtile = end_tile;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    97
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
    98
	// slope of end tile must be complementary to the slope of the start tile
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    99
	if (end_tileh != ComplementSlope(start_tileh)) {
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3435
diff changeset
   100
		ret = DoCommand(end_tile, end_tileh & start_tileh, 0, flags, CMD_TERRAFORM_LAND);
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   101
		if (CmdFailed(ret)) return_cmd_error(STR_5005_UNABLE_TO_EXCAVATE_LAND);
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   102
	} else {
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3435
diff changeset
   103
		ret = DoCommand(end_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   104
		if (CmdFailed(ret)) return ret;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   105
	}
3064
9b9e5b2e2c20 (svn r3653) -Fix: You didn't pay for terraforming at the end tile when building a tunnel
tron
parents: 3063
diff changeset
   106
	cost += _price.build_tunnel + ret;
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   107
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   108
	if (flags & DC_EXEC) {
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   109
		if (GB(p1, 9, 1) == TRANSPORT_RAIL) {
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   110
			MakeRailTunnel(start_tile, _current_player, direction,                 GB(p1, 0, 4));
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   111
			MakeRailTunnel(end_tile,   _current_player, ReverseDiagDir(direction), GB(p1, 0, 4));
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3881
diff changeset
   112
			UpdateSignalsOnSegment(start_tile, direction);
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   113
			YapfNotifyTrackLayoutChange(start_tile, AxisToTrack(DiagDirToAxis(direction)));
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   114
		} else {
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   115
			MakeRoadTunnel(start_tile, _current_player, direction);
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   116
			MakeRoadTunnel(end_tile,   _current_player, ReverseDiagDir(direction));
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   117
		}
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   118
	}
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   119
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   120
	return cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   123
TileIndex CheckTunnelBusy(TileIndex tile, uint *length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
{
1335
a635854c23b6 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1330
diff changeset
   125
	uint z = GetTileZ(tile);
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   126
	DiagDirection dir = GetTunnelDirection(tile);
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4549
diff changeset
   127
	TileIndexDiff delta = TileOffsByDiagDir(dir);
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   128
	uint len = 0;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
   129
	TileIndex starttile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
	Vehicle *v;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   131
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1005
diff changeset
   132
	do {
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1005
diff changeset
   133
		tile += delta;
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1005
diff changeset
   134
		len++;
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1005
diff changeset
   135
	} while (
3184
7405329343ce (svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents: 3183
diff changeset
   136
		!IsTunnelTile(tile) ||
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   137
		ReverseDiagDir(GetTunnelDirection(tile)) != dir ||
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1005
diff changeset
   138
		GetTileZ(tile) != z
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1005
diff changeset
   139
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   141
	v = FindVehicleBetween(starttile, tile, z);
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   142
	if (v != NULL) {
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   143
		_error_message = v->type == VEH_Train ?
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   144
			STR_5000_TRAIN_IN_TUNNEL : STR_5001_ROAD_VEHICLE_IN_TUNNEL;
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   145
		return INVALID_TILE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   146
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   147
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   148
	if (length != NULL) *length = len;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
	return tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   150
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
5595
049ed4486972 (svn r7613) [cbh] - Codechange: Removed tunnel-specific functions from bridge_cmd.c and bridge-specific functions from tunnel_cmd.c
celestar
parents: 5594
diff changeset
   152
static inline bool CheckAllowRemoveTunnel(TileIndex tile)
5009
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   153
{
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   154
	/* Floods can remove anything as well as the scenario editor */
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   155
	if (_current_player == OWNER_WATER || _game_mode == GM_EDITOR) return true;
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   156
	/* Obviously if the bridge/tunnel belongs to us, or no-one, we can remove it */
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   157
	if (CheckTileOwnership(tile) || IsTileOwner(tile, OWNER_NONE)) return true;
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   158
	/* Otherwise we can only remove town-owned stuff with extra patch-settings, or cheat */
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   159
	if (IsTileOwner(tile, OWNER_TOWN) && (_patches.extra_dynamite || _cheats.magic_bulldozer.value)) return true;
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   160
	return false;
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   161
}
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   162
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
   163
static int32 DoClearTunnel(TileIndex tile, uint32 flags)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
{
5009
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   165
	Town *t = NULL;
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   166
	TileIndex endtile;
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   167
	uint length;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
5595
049ed4486972 (svn r7613) [cbh] - Codechange: Removed tunnel-specific functions from bridge_cmd.c and bridge-specific functions from tunnel_cmd.c
celestar
parents: 5594
diff changeset
   171
	if (!CheckAllowRemoveTunnel(tile)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   172
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
	endtile = CheckTunnelBusy(tile, &length);
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   174
	if (endtile == INVALID_TILE) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
	_build_tunnel_endtile = endtile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   177
1901
fb05044cf5c3 (svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents: 1891
diff changeset
   178
	if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
5009
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   179
		t = ClosestTownFromTile(tile, (uint)-1); // town penalty rating
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   180
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   181
		/* Check if you are allowed to remove the tunnel owned by a town
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   182
		 * Removal depends on difficulty settings */
2958
3f8946daf55f (svn r3520) Remove unused parameters from some functions
tron
parents: 2952
diff changeset
   183
		if (!CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE)) {
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 511
diff changeset
   184
			SetDParam(0, t->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
			return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
	if (flags & DC_EXEC) {
38
d20549a6ea13 (svn r39) -Fix [1008605] Signals not updated after ClearTunnel Bug [985920] (TrueLight)
darkvater
parents: 22
diff changeset
   190
		// We first need to request the direction before calling DoClearSquare
d20549a6ea13 (svn r39) -Fix [1008605] Signals not updated after ClearTunnel Bug [985920] (TrueLight)
darkvater
parents: 22
diff changeset
   191
		//  else the direction is always 0.. dah!! ;)
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3156
diff changeset
   192
		DiagDirection dir = GetTunnelDirection(tile);
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   193
		Track track;
2870
32c980d2b8e9 (svn r3418) - Fix: When removing a town-owned tunnel the player's rating was not reduced, as it checked the ownership of the tunnel after clearing it. Now we perform the rating adjustment before clearing the tiles. (spotted by glx)
peter1138
parents: 2843
diff changeset
   194
32c980d2b8e9 (svn r3418) - Fix: When removing a town-owned tunnel the player's rating was not reduced, as it checked the ownership of the tunnel after clearing it. Now we perform the rating adjustment before clearing the tiles. (spotted by glx)
peter1138
parents: 2843
diff changeset
   195
		// Adjust the town's player rating. Do this before removing the tile owner info.
32c980d2b8e9 (svn r3418) - Fix: When removing a town-owned tunnel the player's rating was not reduced, as it checked the ownership of the tunnel after clearing it. Now we perform the rating adjustment before clearing the tiles. (spotted by glx)
peter1138
parents: 2843
diff changeset
   196
		if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR)
32c980d2b8e9 (svn r3418) - Fix: When removing a town-owned tunnel the player's rating was not reduced, as it checked the ownership of the tunnel after clearing it. Now we perform the rating adjustment before clearing the tiles. (spotted by glx)
peter1138
parents: 2843
diff changeset
   197
			ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM);
32c980d2b8e9 (svn r3418) - Fix: When removing a town-owned tunnel the player's rating was not reduced, as it checked the ownership of the tunnel after clearing it. Now we perform the rating adjustment before clearing the tiles. (spotted by glx)
peter1138
parents: 2843
diff changeset
   198
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
		DoClearSquare(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
		DoClearSquare(endtile);
3172
1605cb59ccd9 (svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents: 3157
diff changeset
   201
		UpdateSignalsOnSegment(tile, ReverseDiagDir(dir));
1605cb59ccd9 (svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents: 3157
diff changeset
   202
		UpdateSignalsOnSegment(endtile, dir);
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   203
		track = AxisToTrack(DiagDirToAxis(dir));
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   204
		YapfNotifyTrackLayoutChange(tile, track);
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   205
		YapfNotifyTrackLayoutChange(endtile, track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
	}
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   207
	return _price.clear_tunnel * (length + 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   210
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   212
static int32 ClearTile_Tunnel(TileIndex tile, byte flags)
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
   213
{
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   214
	assert(IsTunnelTile(tile));
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   215
	if (flags & DC_AUTO) return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   216
	return DoClearTunnel(tile, flags);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   217
}
1109
1bab892228cd (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1095
diff changeset
   218
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
5595
049ed4486972 (svn r7613) [cbh] - Codechange: Removed tunnel-specific functions from bridge_cmd.c and bridge-specific functions from tunnel_cmd.c
celestar
parents: 5594
diff changeset
   220
int32 DoConvertTunnelRail(TileIndex tile, RailType totype, bool exec)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
{
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   222
	TileIndex endtile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
5595
049ed4486972 (svn r7613) [cbh] - Codechange: Removed tunnel-specific functions from bridge_cmd.c and bridge-specific functions from tunnel_cmd.c
celestar
parents: 5594
diff changeset
   224
	if (GetTunnelTransportType(tile) == TRANSPORT_RAIL) {
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3996
diff changeset
   225
		uint length;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3996
diff changeset
   226
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
		if (!CheckTileOwnership(tile)) return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   228
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3234
diff changeset
   229
		if (GetRailType(tile) == totype) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   230
5116
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 5057
diff changeset
   231
		// 'hidden' elrails can't be downgraded to normal rail when elrails are disabled
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 5057
diff changeset
   232
		if (_patches.disable_elrails && totype == RAILTYPE_RAIL && GetRailType(tile) == RAILTYPE_ELECTRIC) return CMD_ERROR;
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 5057
diff changeset
   233
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
		endtile = CheckTunnelBusy(tile, &length);
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   235
		if (endtile == INVALID_TILE) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
		if (exec) {
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   238
			Track track;
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3234
diff changeset
   239
			SetRailType(tile, totype);
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3234
diff changeset
   240
			SetRailType(endtile, totype);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
			MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
			MarkTileDirtyByTile(endtile);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3881
diff changeset
   243
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   244
			track = AxisToTrack(DiagDirToAxis(GetTunnelDirection(tile)));
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3881
diff changeset
   245
			YapfNotifyTrackLayoutChange(tile, track);
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   246
			YapfNotifyTrackLayoutChange(endtile, track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
		return (length + 1) * (_price.build_rail >> 1);
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3996
diff changeset
   249
	} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
		return CMD_ERROR;
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3996
diff changeset
   251
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
2536
8c4e298f4886 (svn r3065) -Codechange/Add: Modified the bridge drawing code so that the basic offset is read from the RailTypeInfo struct. This is (hopefully) the last DrawTile change on the way to electrified railways. While being at it, de-mystified the function a bit and added some asserts.
celestar
parents: 2535
diff changeset
   254
/**
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   255
 * Draws a tunnel tile.
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   256
 * Please note that in this code, "roads" are treated as railtype 1, whilst the real railtypes are 0, 2 and 3
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   257
 */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   258
static void DrawTile_Tunnel(TileInfo *ti)
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   259
{
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   260
	uint32 image;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   261
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   262
	if (GetTunnelTransportType(ti->tile) == TRANSPORT_RAIL) {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   263
		image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.tunnel;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   264
	} else {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   265
		image = SPR_TUNNEL_ENTRY_REAR_ROAD;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   266
	}
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   267
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   268
	if (HasTunnelSnowOrDesert(ti->tile)) image += 32;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   269
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   270
	image += GetTunnelDirection(ti->tile) * 2;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   271
	DrawGroundSprite(image);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   272
	if (GetRailType(ti->tile) == RAILTYPE_ELECTRIC) DrawCatenary(ti);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   273
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   274
	AddSortableSpriteToDraw(image+1, ti->x + TILE_SIZE - 1, ti->y + TILE_SIZE - 1, 1, 1, 8, (byte)ti->z);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   275
	DrawBridgeMiddle(ti);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   276
}
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   277
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5116
diff changeset
   278
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   279
/** Gets the absolute z coordinate of a point inside a tunnel tile
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   280
 *  When we're on the track (that means between position 5 and 10)
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   281
 *  on the coordinate perpendicular to the track it returns only the
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   282
 *  base height of the tile (because the track is horizontal).
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   283
 *  Outside this range (from 0 to 4 and from 11 to 15) it returns the
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   284
 *  "true" Z coordinate of the tile by taking the slope into account
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   285
 *  @param tile The index of the tile we are talking about
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   286
 *  @param x Absolute or relative x coordinate
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   287
 *  @param y Absolute or relative y coordinate
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   288
 *  @return Absolute z coordinate
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   289
 */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   290
static uint GetSlopeZ_Tunnel(TileIndex tile, uint x, uint y)
2537
d9c0df52a466 (svn r3066) Constify the parameter of GetSlopeZ_*()
tron
parents: 2536
diff changeset
   291
{
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   292
	uint z, pos;
4231
dea6a63dd058 (svn r5794) Pass the TileIndex plus x and y coordinates into GetSlopeZ_* instead of a TileInfo
tron
parents: 4191
diff changeset
   293
	Slope tileh = GetTileSlope(tile, &z);
dea6a63dd058 (svn r5794) Pass the TileIndex plus x and y coordinates into GetSlopeZ_* instead of a TileInfo
tron
parents: 4191
diff changeset
   294
dea6a63dd058 (svn r5794) Pass the TileIndex plus x and y coordinates into GetSlopeZ_* instead of a TileInfo
tron
parents: 4191
diff changeset
   295
	x &= 0xF;
dea6a63dd058 (svn r5794) Pass the TileIndex plus x and y coordinates into GetSlopeZ_* instead of a TileInfo
tron
parents: 4191
diff changeset
   296
	y &= 0xF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   297
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   298
	pos = (DiagDirToAxis(GetTunnelDirection(tile)) == AXIS_X ? y : x);
3517
76814013e912 (svn r4373) Rewrite GetSlopeZ_TunnelBridge() and slightly change its behavior:
tron
parents: 3493
diff changeset
   299
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   300
	// In the tunnel entrance?
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   301
	if (5 <= pos && pos <= 10) return z;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
3517
76814013e912 (svn r4373) Rewrite GetSlopeZ_TunnelBridge() and slightly change its behavior:
tron
parents: 3493
diff changeset
   303
	return z + GetPartialZ(x, y, tileh);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   306
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   307
static Slope GetSlopeTileh_Tunnel(TileIndex tile, Slope tileh)
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   308
{
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   309
	return tileh;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   310
}
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   311
39
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 38
diff changeset
   312
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   313
static void GetAcceptedCargo_Tunnel(TileIndex tile, AcceptedCargo ac)
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   314
{
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   315
	/* not used */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   316
}
39
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 38
diff changeset
   317
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   318
static void GetTileDesc_Tunnel(TileIndex tile, TileDesc *td)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
{
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   320
	td->str = (GetTunnelTransportType(tile) == TRANSPORT_RAIL) ?  STR_5017_RAILROAD_TUNNEL : STR_5018_ROAD_TUNNEL;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   321
	td->owner = GetTileOwner(tile);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   322
}
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   323
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   324
static void AnimateTile_Tunnel(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   326
	/* not used */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   327
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   329
static void TileLoop_Tunnel(TileIndex tile)
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   330
{
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   331
	bool snow_or_desert = HasTunnelSnowOrDesert(tile);
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
   332
	switch (_opt.landscape) {
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
   333
		case LT_HILLY:
5592
fd60d4ecc921 (svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents: 5591
diff changeset
   334
			if (snow_or_desert != (GetTileZ(tile) > _opt.snow_line)) {
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   335
				SetTunnelSnowOrDesert(tile, !snow_or_desert);
4160
49ccad677eb7 (svn r5592) Merge two complementary cases
tron
parents: 4158
diff changeset
   336
				MarkTileDirtyByTile(tile);
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
   337
			}
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
   338
			break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
   339
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
   340
		case LT_DESERT:
5592
fd60d4ecc921 (svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents: 5591
diff changeset
   341
			if (GetTropicZone(tile) == TROPICZONE_DESERT && !snow_or_desert) {
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   342
				SetTunnelSnowOrDesert(tile, true);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   343
				MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
			}
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
   345
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   349
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   350
static void ClickTile_Tunnel(TileIndex tile)
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   351
{
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   352
	/* not used */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   353
}
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   354
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   355
static uint32 GetTileTrackStatus_Tunnel(TileIndex tile, TransportType mode)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
{
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   357
	if (GetTunnelTransportType(tile) != mode) return 0;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   358
	return AxisToTrackBits(DiagDirToAxis(GetTunnelDirection(tile))) * 0x101;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   359
}
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   360
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   361
static void ChangeTileOwner_Tunnel(TileIndex tile, PlayerID old_player, PlayerID new_player)
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   362
{
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   363
	if (!IsTileOwner(tile, old_player)) return;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   364
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   365
	if (new_player != PLAYER_SPECTATOR) {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   366
		SetTileOwner(tile, new_player);
3996
5a1d1fc1bd07 (svn r5199) Make the control flow of GetTileTrackStatus_TunnelBridge() more comprehensible
tron
parents: 3977
diff changeset
   367
	} else {
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   368
		DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4304
diff changeset
   373
static const byte _tunnel_fractcoord_1[4]    = {0x8E, 0x18, 0x81, 0xE8};
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4304
diff changeset
   374
static const byte _tunnel_fractcoord_2[4]    = {0x81, 0x98, 0x87, 0x38};
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4304
diff changeset
   375
static const byte _tunnel_fractcoord_3[4]    = {0x82, 0x88, 0x86, 0x48};
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4304
diff changeset
   376
static const byte _exit_tunnel_track[4]      = {1, 2, 1, 2};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   378
static const byte _road_exit_tunnel_state[4] = {8, 9, 0, 1};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
static const byte _road_exit_tunnel_frame[4] = {2, 7, 9, 4};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4304
diff changeset
   381
static const byte _tunnel_fractcoord_4[4]    = {0x52, 0x85, 0x98, 0x29};
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4304
diff changeset
   382
static const byte _tunnel_fractcoord_5[4]    = {0x92, 0x89, 0x58, 0x25};
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4304
diff changeset
   383
static const byte _tunnel_fractcoord_6[4]    = {0x92, 0x89, 0x56, 0x45};
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4304
diff changeset
   384
static const byte _tunnel_fractcoord_7[4]    = {0x52, 0x85, 0x96, 0x49};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   386
static uint32 VehicleEnter_Tunnel(Vehicle *v, TileIndex tile, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
{
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5116
diff changeset
   388
	int z = GetSlopeZ(x, y) - v->z_pos;
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5116
diff changeset
   389
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   390
	byte fc;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   391
	DiagDirection dir;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   392
	DiagDirection vdir;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   393
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5116
diff changeset
   394
	if (myabs(z) > 2) return 8;
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5116
diff changeset
   395
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   396
	if (v->type == VEH_Train) {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   397
		fc = (x & 0xF) + (y << 4);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   399
		dir = GetTunnelDirection(tile);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   400
		vdir = DirToDiagDir(v->direction);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   401
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   402
		if (v->u.rail.track != 0x40 && dir == vdir) {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   403
			if (IsFrontEngine(v) && fc == _tunnel_fractcoord_1[dir]) {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   404
				if (!PlayVehicleSound(v, VSE_TUNNEL) && v->spritenum < 4) {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   405
					SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
				}
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   407
				return 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
			}
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   409
			if (fc == _tunnel_fractcoord_2[dir]) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
				v->tile = tile;
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   411
				v->u.rail.track = 0x40;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   412
				v->vehstatus |= VS_HIDDEN;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
				return 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
		}
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5116
diff changeset
   416
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   417
		if (dir == ReverseDiagDir(vdir) && fc == _tunnel_fractcoord_3[dir] && z == 0) {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   418
			/* We're at the tunnel exit ?? */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   419
			v->tile = tile;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   420
			v->u.rail.track = _exit_tunnel_track[dir];
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   421
			assert(v->u.rail.track);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   422
			v->vehstatus &= ~VS_HIDDEN;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   423
			return 4;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   424
		}
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   425
	} else if (v->type == VEH_Road) {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   426
		fc = (x & 0xF) + (y << 4);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   427
		dir = GetTunnelDirection(tile);
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   428
		vdir = DirToDiagDir(v->direction);
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5116
diff changeset
   429
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   430
		// Enter tunnel?
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   431
		if (v->u.road.state != 0xFF && dir == vdir) {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   432
			if (fc == _tunnel_fractcoord_4[dir] ||
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   433
					fc == _tunnel_fractcoord_5[dir]) {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   434
				v->tile = tile;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   435
				v->u.road.state = 0xFF;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   436
				v->vehstatus |= VS_HIDDEN;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   437
				return 4;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   438
			} else {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   439
				return 0;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   440
			}
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5116
diff changeset
   441
		}
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5116
diff changeset
   442
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   443
		if (dir == ReverseDiagDir(vdir) && (
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   444
					/* We're at the tunnel exit ?? */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   445
					fc == _tunnel_fractcoord_6[dir] ||
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   446
					fc == _tunnel_fractcoord_7[dir]
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   447
				) &&
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   448
				z == 0) {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   449
			v->tile = tile;
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   450
			v->u.road.state = _road_exit_tunnel_state[dir];
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   451
			v->u.road.frame = _road_exit_tunnel_frame[dir];
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   452
			v->vehstatus &= ~VS_HIDDEN;
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5116
diff changeset
   453
			return 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   459
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   460
const TileTypeProcs _tile_type_tunnel_procs = {
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   461
	DrawTile_Tunnel,           /* draw_tile_proc */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   462
	GetSlopeZ_Tunnel,          /* get_slope_z_proc */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   463
	ClearTile_Tunnel,          /* clear_tile_proc */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   464
	GetAcceptedCargo_Tunnel,   /* get_accepted_cargo_proc */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   465
	GetTileDesc_Tunnel,        /* get_tile_desc_proc */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   466
	GetTileTrackStatus_Tunnel, /* get_tile_track_status_proc */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   467
	ClickTile_Tunnel,          /* click_tile_proc */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   468
	AnimateTile_Tunnel,        /* animate_tile_proc */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   469
	TileLoop_Tunnel,           /* tile_loop_clear */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   470
	ChangeTileOwner_Tunnel,    /* change_tile_owner_clear */
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4304
diff changeset
   471
	NULL,                            /* get_produced_cargo_proc */
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   472
	VehicleEnter_Tunnel,       /* vehicle_enter_tile_proc */
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   473
	GetSlopeTileh_Tunnel,      /* get_slope_tileh_proc */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
};
5593
3e0dd1f014ca (svn r7610) [cbh] - Codechange: Created seperate Tile Type Procs for tunnel and bridge tiles
celestar
parents: 5592
diff changeset
   475