tunnelbridge_cmd.c
author rubidium
Fri, 22 Jun 2007 20:12:09 +0000
branch0.5
changeset 5513 0d3227e250eb
parent 5504 520481b11c2c
child 5519 f666f6b62978
permissions -rw-r--r--
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
- Fix: Do not look in every direction for tunnels when building one, one direction is enough (r10258)
- Fix: Take the age of the front vehicle for station rating (r10246)
- Fix: Terraforming wipes out canals. Now you always have to remove the canal before terraforming, instead of "just" removing the canal [FS#594] (r10240)
- Fix: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could technically cause desyncs in network games as the collision hash order is not guaranteed [FS#892] (r10222)
- Fix: Land under foundations was terraform when it shouldn't be terraformed [FS#882, FS#890] (r10219)
- Fix: Some NewGRFs use the same (unused in the "current" climate) sprite IDs. Normally this gives some artefacts, but when one NewGRF expects it to be a sprite and another NewGRF overwrites it with a non-sprite nasty things happen (drawing a non-sprite crashes OTTD) [FS#838] (r10109)
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"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
#include "viewport.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
#include "player.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
#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
    24
#include "sound.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    25
#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
    26
#include "bridge.h"
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2663
diff changeset
    27
#include "train.h"
3187
0813719b05a9 (svn r3837) Partially unmagicfy restoring the tiles when deleting a bridge
tron
parents: 3184
diff changeset
    28
#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
    29
#include "yapf/yapf.h"
4261
2ec8f5a9747b (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4246
diff changeset
    30
#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
    31
#include "newgrf_sound.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2260
diff changeset
    33
#include "table/bridge_land.h"
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2260
diff changeset
    34
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
    35
extern const byte _track_sloped_sprites[14];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
    36
extern const SpriteID _water_shore_sprites[15];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
    37
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
    38
extern void DrawCanalWater(TileIndex tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
    39
2478
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
    40
const Bridge orig_bridge[] = {
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
    41
/*
4293
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    42
	     year of availablity
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    43
	     |  minimum length
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    44
	     |  |   maximum length
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    45
	     |  |   |    price
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    46
	     |  |   |    |    maximum speed
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    47
	     |  |   |    |    |  sprite to use in GUI                string with description
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    48
	     |  |   |    |    |  |                                   |                            */
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    49
	{    0, 0, 16,  80,  32, 0xA24                             , STR_5012_WOODEN             , NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    50
	{    0, 0,  2, 112,  48, 0xA26 | PALETTE_TO_STRUCT_RED     , STR_5013_CONCRETE           , NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    51
	{ 1930, 0,  5, 144,  64, 0xA25                             , STR_500F_GIRDER_STEEL       , NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    52
	{    0, 2, 10, 168,  80, 0xA22 | PALETTE_TO_STRUCT_CONCRETE, STR_5011_SUSPENSION_CONCRETE, NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    53
	{ 1930, 3, 16, 185,  96, 0xA22                             , STR_500E_SUSPENSION_STEEL   , NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    54
	{ 1930, 3, 16, 192, 112, 0xA22 | PALETTE_TO_STRUCT_YELLOW  , STR_500E_SUSPENSION_STEEL   , NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    55
	{ 1930, 3,  7, 224, 160, 0xA23                             , STR_5010_CANTILEVER_STEEL   , NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    56
	{ 1930, 3,  8, 232, 208, 0xA23 | PALETTE_TO_STRUCT_BROWN   , STR_5010_CANTILEVER_STEEL   , NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    57
	{ 1930, 3,  9, 248, 240, 0xA23 | PALETTE_TO_STRUCT_RED     , STR_5010_CANTILEVER_STEEL   , NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    58
	{ 1930, 0,  2, 240, 256, 0xA27                             , STR_500F_GIRDER_STEEL       , NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    59
	{ 1995, 2, 16, 255, 320, 0xA28                             , STR_5014_TUBULAR_STEEL      , NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    60
	{ 2005, 2, 32, 380, 512, 0xA28 | PALETTE_TO_STRUCT_YELLOW  , STR_5014_TUBULAR_STEEL      , NULL, 0 },
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4291
diff changeset
    61
	{ 2010, 2, 32, 510, 608, 0xA28 | PALETTE_TO_STRUCT_GREY    , STR_BRIDGE_TUBULAR_SILICON  , NULL, 0 }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
2763
d579caf58b59 (svn r3308) BAD
tron
parents: 2737
diff changeset
    64
Bridge _bridge[MAX_BRIDGES];
d579caf58b59 (svn r3308) BAD
tron
parents: 2737
diff changeset
    65
d579caf58b59 (svn r3308) BAD
tron
parents: 2737
diff changeset
    66
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
// calculate the price factor for building a long bridge.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
// basically the cost delta is 1,1, 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5, 6,6,6,6,6,6,  7,7,7,7,7,7,7,  8,8,8,8,8,8,8,8,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
int CalcBridgeLenCostFactor(int x)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
    71
	int n;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
    72
	int r;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
    73
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
	if (x < 2) return x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
	x -= 2;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
    76
	for (n = 0, r = 2;; n++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
		if (x <= n) return r + x * n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
		r += n * n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
		x -= n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    83
#define M(x) (1 << (x))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
enum {
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    85
	// foundation, whole tile is leveled up --> 3 corners raised
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    86
	BRIDGE_FULL_LEVELED_FOUNDATION = M(SLOPE_WSE) | M(SLOPE_NWS) | M(SLOPE_ENW) | M(SLOPE_SEN),
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    87
	// foundation, tile is partly leveled up --> 1 corner raised
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    88
	BRIDGE_PARTLY_LEVELED_FOUNDATION = M(SLOPE_W) | M(SLOPE_S) | M(SLOPE_E) | M(SLOPE_N),
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    89
	// no foundations (X,Y direction)
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    90
	BRIDGE_NO_FOUNDATION = M(SLOPE_FLAT) | M(SLOPE_SW) | M(SLOPE_SE) | M(SLOPE_NW) | M(SLOPE_NE),
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    91
	BRIDGE_HORZ_RAMP = (BRIDGE_PARTLY_LEVELED_FOUNDATION | BRIDGE_NO_FOUNDATION) & ~M(SLOPE_FLAT)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
};
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
    93
#undef M
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
2478
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
    95
static inline const PalSpriteID *GetBridgeSpriteTable(int index, byte table)
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
    96
{
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
    97
	const Bridge *bridge = &_bridge[index];
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
    98
	assert(table < 7);
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
    99
	if (bridge->sprite_table == NULL || bridge->sprite_table[table] == NULL) {
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
   100
		return _bridge_sprite_table[index][table];
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
   101
	} else {
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
   102
		return bridge->sprite_table[table];
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
   103
	}
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
   104
}
16b05f1de6bb (svn r3004) -Feature, NewGRF: Support loading of bridge attributes and tables from GRF. Currently drawing tall pillars uses old data.
peter1138
parents: 2436
diff changeset
   105
3556
d25b35568c48 (svn r4430) - NewGRF: Use the bridge's sprite layout for drawing high pillars. This fixes newgrf's high-bridge pillars drawing. Also remove the now obsolete _bridge_poles_table which was already integrated into the bridge sprite tables
Darkvater
parents: 3539
diff changeset
   106
static inline byte GetBridgeFlags(int index) { return _bridge[index].flags;}
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
   107
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
4239
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   109
/** Check the slope at the bridge ramps in three easy steps:
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   110
 * - valid slopes without foundation
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   111
 * - valid slopes with foundation
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   112
 * - rest is invalid
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   113
 */
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   114
#define M(x) (1 << (x))
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   115
static int32 CheckBridgeSlopeNorth(Axis axis, Slope tileh)
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   116
{
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   117
	uint32 valid;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
4239
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   119
	valid = M(SLOPE_FLAT) | (axis == AXIS_X ? M(SLOPE_NE) : M(SLOPE_NW));
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   120
	if (HASBIT(valid, tileh)) return 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
4239
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   122
	valid =
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   123
		BRIDGE_FULL_LEVELED_FOUNDATION | M(SLOPE_N) | M(SLOPE_STEEP_N) |
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   124
		(axis == AXIS_X ? M(SLOPE_E) | M(SLOPE_STEEP_E) : M(SLOPE_W) | M(SLOPE_STEEP_W));
4239
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   125
	if (HASBIT(valid, tileh)) return _price.terraform;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   126
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
	return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
4239
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   130
static int32 CheckBridgeSlopeSouth(Axis axis, Slope tileh)
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   131
{
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   132
	uint32 valid;
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   133
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   134
	valid = M(SLOPE_FLAT) | (axis == AXIS_X ? M(SLOPE_SW) : M(SLOPE_SE));
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   135
	if (HASBIT(valid, tileh)) return 0;
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   136
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   137
	valid =
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   138
		BRIDGE_FULL_LEVELED_FOUNDATION | M(SLOPE_S) | M(SLOPE_STEEP_S) |
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   139
		(axis == AXIS_X ? M(SLOPE_W) | M(SLOPE_STEEP_W) : M(SLOPE_E) | M(SLOPE_STEEP_E));
4239
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   140
	if (HASBIT(valid, tileh)) return _price.terraform;
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   141
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   142
	return CMD_ERROR;
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   143
}
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   144
#undef M
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   145
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   146
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
uint32 GetBridgeLength(TileIndex begin, TileIndex end)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   148
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   149
	int x1 = TileX(begin);
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   150
	int y1 = TileY(begin);
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   151
	int x2 = TileX(end);
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   152
	int y2 = TileY(end);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   153
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   154
	return abs(x2 + y2 - x1 - y1) - 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   157
bool CheckBridge_Stuff(byte bridge_type, uint bridge_len)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
{
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
   159
	const Bridge *b = &_bridge[bridge_type];
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   160
	uint max; // max possible length of a bridge (with patch 100)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
1781
92e08797c84b (svn r2285) - Codechange: Fix up some of the missing things from server-checking; namely bridge-type, bridge-length, dragged end-tile (bridge/station), station_spread
Darkvater
parents: 1775
diff changeset
   162
	if (bridge_type >= MAX_BRIDGES) return false;
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
   163
	if (b->avail_year > _cur_year) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
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
   165
	max = b->max_length;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   166
	if (max >= 16 && _patches.longbridges) max = 100;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   168
	return b->min_length <= bridge_len && bridge_len <= max;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
1775
08ff0f12ccdc (svn r2279) - Fix: Check the parameters of the first 10 Commands. While there also add proper comments for the functions and fix up CmdFailed()
Darkvater
parents: 1587
diff changeset
   171
/** Build a Bridge
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
   172
 * @param end_tile end tile
1775
08ff0f12ccdc (svn r2279) - Fix: Check the parameters of the first 10 Commands. While there also add proper comments for the functions and fix up CmdFailed()
Darkvater
parents: 1587
diff changeset
   173
 * @param p1 packed start tile coords (~ dx)
08ff0f12ccdc (svn r2279) - Fix: Check the parameters of the first 10 Commands. While there also add proper comments for the functions and fix up CmdFailed()
Darkvater
parents: 1587
diff changeset
   174
 * @param p2 various bitstuffed elements
08ff0f12ccdc (svn r2279) - Fix: Check the parameters of the first 10 Commands. While there also add proper comments for the functions and fix up CmdFailed()
Darkvater
parents: 1587
diff changeset
   175
 * - p2 = (bit 0- 7) - bridge type (hi bh)
08ff0f12ccdc (svn r2279) - Fix: Check the parameters of the first 10 Commands. While there also add proper comments for the functions and fix up CmdFailed()
Darkvater
parents: 1587
diff changeset
   176
 * - p2 = (bit 8-..) - rail type. bit15 ((x>>8)&0x80) means road bridge.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
 */
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
   178
int32 CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
	int bridge_type;
3180
2123f8062a2d (svn r3820) Be a bit more strict with types: use special types instead of generic byte and don't fill arbitrary data into inappropriate types
tron
parents: 3178
diff changeset
   181
	TransportType transport;
2123f8062a2d (svn r3820) Be a bit more strict with types: use special types instead of generic byte and don't fill arbitrary data into inappropriate types
tron
parents: 3178
diff changeset
   182
	RailType railtype;
3853
c49992ab497b (svn r4881) -Codechange: Be a little more strict about types and remove some null-operations
celestar
parents: 3794
diff changeset
   183
	uint x;
c49992ab497b (svn r4881) -Codechange: Be a little more strict about types and remove some null-operations
celestar
parents: 3794
diff changeset
   184
	uint y;
c49992ab497b (svn r4881) -Codechange: Be a little more strict about types and remove some null-operations
celestar
parents: 3794
diff changeset
   185
	uint sx;
c49992ab497b (svn r4881) -Codechange: Be a little more strict about types and remove some null-operations
celestar
parents: 3794
diff changeset
   186
	uint sy;
3301
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   187
	TileIndex tile_start;
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   188
	TileIndex tile_end;
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
   189
	Slope tileh_start;
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
   190
	Slope tileh_end;
3301
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   191
	uint z_start;
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   192
	uint z_end;
3178
c275826135da (svn r3814) Remove another call to FindLandscapeHeight()
tron
parents: 3172
diff changeset
   193
	TileIndex tile;
c275826135da (svn r3814) Remove another call to FindLandscapeHeight()
tron
parents: 3172
diff changeset
   194
	TileIndexDiff delta;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   195
	uint bridge_len;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   196
	uint odd_middle_part;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3156
diff changeset
   197
	Axis direction;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   198
	uint i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
	int32 cost, terraformcost, ret;
1585
3dc279e97623 (svn r2089) - Codechange: Simplify slopes check in CmdBuildBridge(). Inspired by st3wis' patch 1144746.
pasky
parents: 1562
diff changeset
   200
	bool allow_on_slopes;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   203
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
	/* unpack parameters */
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2133
diff changeset
   205
	bridge_type = GB(p2, 0, 8);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   206
2934
3b7eef9871f8 (svn r3490) -Fix: A bunch (10) of off-by-one errors when checking if a TileIndex points to a tile on the map
tron
parents: 2931
diff changeset
   207
	if (p1 >= MapSize()) return CMD_ERROR;
1775
08ff0f12ccdc (svn r2279) - Fix: Check the parameters of the first 10 Commands. While there also add proper comments for the functions and fix up CmdFailed()
Darkvater
parents: 1587
diff changeset
   208
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
	// type of bridge
3180
2123f8062a2d (svn r3820) Be a bit more strict with types: use special types instead of generic byte and don't fill arbitrary data into inappropriate types
tron
parents: 3178
diff changeset
   210
	if (HASBIT(p2, 15)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
		railtype = 0;
3180
2123f8062a2d (svn r3820) Be a bit more strict with types: use special types instead of generic byte and don't fill arbitrary data into inappropriate types
tron
parents: 3178
diff changeset
   212
		transport = TRANSPORT_ROAD;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
	} else {
3180
2123f8062a2d (svn r3820) Be a bit more strict with types: use special types instead of generic byte and don't fill arbitrary data into inappropriate types
tron
parents: 3178
diff changeset
   214
		if (!ValParamRailtype(GB(p2, 8, 8))) return CMD_ERROR;
2123f8062a2d (svn r3820) Be a bit more strict with types: use special types instead of generic byte and don't fill arbitrary data into inappropriate types
tron
parents: 3178
diff changeset
   215
		railtype = GB(p2, 8, 8);
2123f8062a2d (svn r3820) Be a bit more strict with types: use special types instead of generic byte and don't fill arbitrary data into inappropriate types
tron
parents: 3178
diff changeset
   216
		transport = TRANSPORT_RAIL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   217
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
3493
4d5830846ec2 (svn r4344) Use tile coordinates or even TileIndices instead of virtual tile coordinates where it suffices.
tron
parents: 3491
diff changeset
   219
	x = TileX(end_tile);
4d5830846ec2 (svn r4344) Use tile coordinates or even TileIndices instead of virtual tile coordinates where it suffices.
tron
parents: 3491
diff changeset
   220
	y = TileY(end_tile);
4d5830846ec2 (svn r4344) Use tile coordinates or even TileIndices instead of virtual tile coordinates where it suffices.
tron
parents: 3491
diff changeset
   221
	sx = TileX(p1);
4d5830846ec2 (svn r4344) Use tile coordinates or even TileIndices instead of virtual tile coordinates where it suffices.
tron
parents: 3491
diff changeset
   222
	sy = TileY(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
	/* check if valid, and make sure that (x,y) are smaller than (sx,sy) */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
	if (x == sx) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   226
		if (y == sy) return_cmd_error(STR_5008_CANNOT_START_AND_END_ON);
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3156
diff changeset
   227
		direction = AXIS_Y;
3854
4821667ad368 (svn r4882) -Fix: Forgot a change in previous commit
celestar
parents: 3853
diff changeset
   228
		if (y > sy) uintswap(y,sy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
	} else if (y == sy) {
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3156
diff changeset
   230
		direction = AXIS_X;
3854
4821667ad368 (svn r4882) -Fix: Forgot a change in previous commit
celestar
parents: 3853
diff changeset
   231
		if (x > sx) uintswap(x,sx);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   232
	} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
		return_cmd_error(STR_500A_START_AND_END_MUST_BE_IN);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   234
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
1781
92e08797c84b (svn r2285) - Codechange: Fix up some of the missing things from server-checking; namely bridge-type, bridge-length, dragged end-tile (bridge/station), station_spread
Darkvater
parents: 1775
diff changeset
   236
	/* set and test bridge length, availability */
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
   237
	bridge_len = sx + sy - x - y - 1;
1781
92e08797c84b (svn r2285) - Codechange: Fix up some of the missing things from server-checking; namely bridge-type, bridge-length, dragged end-tile (bridge/station), station_spread
Darkvater
parents: 1775
diff changeset
   238
	if (!CheckBridge_Stuff(bridge_type, bridge_len)) return_cmd_error(STR_5015_CAN_T_BUILD_BRIDGE_HERE);
92e08797c84b (svn r2285) - Codechange: Fix up some of the missing things from server-checking; namely bridge-type, bridge-length, dragged end-tile (bridge/station), station_spread
Darkvater
parents: 1775
diff changeset
   239
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
	/* retrieve landscape height and ensure it's on land */
3493
4d5830846ec2 (svn r4344) Use tile coordinates or even TileIndices instead of virtual tile coordinates where it suffices.
tron
parents: 3491
diff changeset
   241
	tile_start = TileXY(x, y);
4d5830846ec2 (svn r4344) Use tile coordinates or even TileIndices instead of virtual tile coordinates where it suffices.
tron
parents: 3491
diff changeset
   242
	tile_end = TileXY(sx, sy);
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
   243
	if (IsClearWaterTile(tile_start) || IsClearWaterTile(tile_end)) {
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
   244
		return_cmd_error(STR_02A0_ENDS_OF_BRIDGE_MUST_BOTH);
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
   245
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
3301
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   247
	tileh_start = GetTileSlope(tile_start, &z_start);
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   248
	tileh_end = GetTileSlope(tile_end, &z_end);
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   249
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   250
	if (IsSteepSlope(tileh_start)) z_start += TILE_HEIGHT;
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
   251
	if (HASBIT(BRIDGE_FULL_LEVELED_FOUNDATION, tileh_start)) {
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
   252
		z_start += TILE_HEIGHT;
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
   253
		tileh_start = SLOPE_FLAT;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   256
	if (IsSteepSlope(tileh_end)) z_end += TILE_HEIGHT;
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
   257
	if (HASBIT(BRIDGE_FULL_LEVELED_FOUNDATION, tileh_end)) {
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
   258
		z_end += TILE_HEIGHT;
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
   259
		tileh_end = SLOPE_FLAT;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
3301
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   262
	if (z_start != z_end) return_cmd_error(STR_5009_LEVEL_LAND_OR_WATER_REQUIRED);
1585
3dc279e97623 (svn r2089) - Codechange: Simplify slopes check in CmdBuildBridge(). Inspired by st3wis' patch 1144746.
pasky
parents: 1562
diff changeset
   263
3dc279e97623 (svn r2089) - Codechange: Simplify slopes check in CmdBuildBridge(). Inspired by st3wis' patch 1144746.
pasky
parents: 1562
diff changeset
   264
	// Towns are not allowed to use bridges on slopes.
2422
914a12dee832 (svn r2948) -Fix: the old AI needs a special flag that triggers all kind of special
truelight
parents: 2314
diff changeset
   265
	allow_on_slopes = (!_is_old_ai_player
1781
92e08797c84b (svn r2285) - Codechange: Fix up some of the missing things from server-checking; namely bridge-type, bridge-length, dragged end-tile (bridge/station), station_spread
Darkvater
parents: 1775
diff changeset
   266
	                   && _current_player != OWNER_TOWN && _patches.build_on_slopes);
1585
3dc279e97623 (svn r2089) - Codechange: Simplify slopes check in CmdBuildBridge(). Inspired by st3wis' patch 1144746.
pasky
parents: 1562
diff changeset
   267
3dc279e97623 (svn r2089) - Codechange: Simplify slopes check in CmdBuildBridge(). Inspired by st3wis' patch 1144746.
pasky
parents: 1562
diff changeset
   268
	/* Try and clear the start landscape */
3dc279e97623 (svn r2089) - Codechange: Simplify slopes check in CmdBuildBridge(). Inspired by st3wis' patch 1144746.
pasky
parents: 1562
diff changeset
   269
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
   270
	ret = DoCommand(tile_start, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
3183
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3181
diff changeset
   271
	if (CmdFailed(ret)) return ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
	cost = ret;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
4239
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   274
	terraformcost = CheckBridgeSlopeNorth(direction, tileh_start);
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   275
	if (CmdFailed(terraformcost) || (terraformcost != 0 && !allow_on_slopes))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
		return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   277
	cost += terraformcost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   278
1585
3dc279e97623 (svn r2089) - Codechange: Simplify slopes check in CmdBuildBridge(). Inspired by st3wis' patch 1144746.
pasky
parents: 1562
diff changeset
   279
	/* Try and clear the end landscape */
3dc279e97623 (svn r2089) - Codechange: Simplify slopes check in CmdBuildBridge(). Inspired by st3wis' patch 1144746.
pasky
parents: 1562
diff changeset
   280
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
   281
	ret = DoCommand(tile_end, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
3183
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3181
diff changeset
   282
	if (CmdFailed(ret)) return ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
	cost += ret;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   284
1585
3dc279e97623 (svn r2089) - Codechange: Simplify slopes check in CmdBuildBridge(). Inspired by st3wis' patch 1144746.
pasky
parents: 1562
diff changeset
   285
	// false - end tile slope check
4239
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   286
	terraformcost = CheckBridgeSlopeSouth(direction, tileh_end);
9ec74a9b8b58 (svn r5809) Turn the tests for valid bridge ramp slopes into something comprehensible
tron
parents: 4231
diff changeset
   287
	if (CmdFailed(terraformcost) || (terraformcost != 0 && !allow_on_slopes))
1585
3dc279e97623 (svn r2089) - Codechange: Simplify slopes check in CmdBuildBridge(). Inspired by st3wis' patch 1144746.
pasky
parents: 1562
diff changeset
   288
		return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
3dc279e97623 (svn r2089) - Codechange: Simplify slopes check in CmdBuildBridge(). Inspired by st3wis' patch 1144746.
pasky
parents: 1562
diff changeset
   289
	cost += terraformcost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   291
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   292
	/* do the drill? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
	if (flags & DC_EXEC) {
3209
e0b7d48351a3 (svn r3877) Add functions to turn a tile into a rail/road bridge ramp/middle part
tron
parents: 3196
diff changeset
   294
		DiagDirection dir = AxisToDiagDir(direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   295
3209
e0b7d48351a3 (svn r3877) Add functions to turn a tile into a rail/road bridge ramp/middle part
tron
parents: 3196
diff changeset
   296
		if (transport == TRANSPORT_RAIL) {
3301
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   297
			MakeRailBridgeRamp(tile_start, _current_player, bridge_type, dir, railtype);
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   298
			MakeRailBridgeRamp(tile_end,   _current_player, bridge_type, ReverseDiagDir(dir), railtype);
3209
e0b7d48351a3 (svn r3877) Add functions to turn a tile into a rail/road bridge ramp/middle part
tron
parents: 3196
diff changeset
   299
		} else {
3301
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   300
			MakeRoadBridgeRamp(tile_start, _current_player, bridge_type, dir);
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   301
			MakeRoadBridgeRamp(tile_end,   _current_player, bridge_type, ReverseDiagDir(dir));
3209
e0b7d48351a3 (svn r3877) Add functions to turn a tile into a rail/road bridge ramp/middle part
tron
parents: 3196
diff changeset
   302
		}
3301
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   303
		MarkTileDirtyByTile(tile_start);
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   304
		MarkTileDirtyByTile(tile_end);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   307
	// position of middle part of the odd bridge (larger than MAX(i) otherwise)
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   308
	odd_middle_part = (bridge_len % 2) ? (bridge_len / 2) : bridge_len;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   309
3301
4ac662c79ad3 (svn r4047) Remove two calls to FindLandscapeHeight() and some confusing use of the comma operator
tron
parents: 3267
diff changeset
   310
	tile = tile_start;
3178
c275826135da (svn r3814) Remove another call to FindLandscapeHeight()
tron
parents: 3172
diff changeset
   311
	delta = (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
1781
92e08797c84b (svn r2285) - Codechange: Fix up some of the missing things from server-checking; namely bridge-type, bridge-length, dragged end-tile (bridge/station), station_spread
Darkvater
parents: 1775
diff changeset
   312
	for (i = 0; i != bridge_len; i++) {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   313
		TransportType transport_under;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   314
		Owner owner_under = OWNER_NONE;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   315
		RailType rail_under = INVALID_RAILTYPE;
3178
c275826135da (svn r3814) Remove another call to FindLandscapeHeight()
tron
parents: 3172
diff changeset
   316
		uint z;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
3178
c275826135da (svn r3814) Remove another call to FindLandscapeHeight()
tron
parents: 3172
diff changeset
   318
		tile += delta;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   320
		if (GetTileSlope(tile, &z) != SLOPE_FLAT && z >= z_start) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   321
			return_cmd_error(STR_5009_LEVEL_LAND_OR_WATER_REQUIRED);
3183
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3181
diff changeset
   322
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
3178
c275826135da (svn r3814) Remove another call to FindLandscapeHeight()
tron
parents: 3172
diff changeset
   324
		switch (GetTileType(tile)) {
3065
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   325
			case MP_WATER:
3183
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3181
diff changeset
   326
				if (!EnsureNoVehicle(tile)) return_cmd_error(STR_980E_SHIP_IN_THE_WAY);
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
   327
				if (!IsWater(tile) && !IsCoast(tile)) goto not_valid_below;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   328
				transport_under = TRANSPORT_WATER;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   329
				owner_under = GetTileOwner(tile);
3065
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   330
				break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
3065
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   332
			case MP_RAILWAY:
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   333
				if (GetRailTileType(tile) != RAIL_TILE_NORMAL ||
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   334
						GetTrackBits(tile) != AxisToTrackBits(OtherAxis(direction))) {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   335
					goto not_valid_below;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   336
				}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   337
				transport_under = TRANSPORT_RAIL;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   338
				owner_under = GetTileOwner(tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   339
				rail_under = GetRailType(tile);
3065
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   340
				break;
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   341
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   342
			case MP_STREET:
4046
566de0b02064 (svn r5315) -Fix: Prohibit altering a road tile while road works are in progress
tron
parents: 4022
diff changeset
   343
				if (GetRoadTileType(tile) != ROAD_TILE_NORMAL) goto not_valid_below;
566de0b02064 (svn r5315) -Fix: Prohibit altering a road tile while road works are in progress
tron
parents: 4022
diff changeset
   344
				if (HasRoadWorks(tile)) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS);
566de0b02064 (svn r5315) -Fix: Prohibit altering a road tile while road works are in progress
tron
parents: 4022
diff changeset
   345
				if (GetRoadBits(tile) != (direction == AXIS_X ? ROAD_Y : ROAD_X)) {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   346
					goto not_valid_below;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   347
				}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   348
				transport_under = TRANSPORT_ROAD;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   349
				owner_under = GetTileOwner(tile);
3065
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   350
				break;
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   351
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   352
			default:
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
not_valid_below:;
3065
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   354
				/* try and clear the middle landscape */
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
   355
				ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
3183
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3181
diff changeset
   356
				if (CmdFailed(ret)) return ret;
3065
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   357
				cost += ret;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   358
				transport_under = INVALID_TRANSPORT;
3065
2bce72c02f63 (svn r3654) Turn another if-cascade into a switch
tron
parents: 3064
diff changeset
   359
				break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   362
		if (flags & DC_EXEC) {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   363
			uint piece;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   364
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   365
			//bridges pieces sequence (middle parts)
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   366
			// bridge len 1: 0
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   367
			// bridge len 2: 0 1
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   368
			// bridge len 3: 0 4 1
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   369
			// bridge len 4: 0 2 3 1
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   370
			// bridge len 5: 0 2 5 3 1
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   371
			// bridge len 6: 0 2 3 2 3 1
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   372
			// bridge len 7: 0 2 3 4 2 3 1
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   373
			// #0 - always as first, #1 - always as last (if len>1)
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   374
			// #2,#3 are to pair in order
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   375
			// for odd bridges: #5 is going in the bridge middle if on even position, #4 on odd (counting from 0)
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   376
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   377
			if (i == 0) { // first tile
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   378
				piece = 0;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   379
			} else if (i == bridge_len - 1) { // last tile
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   380
				piece = 1;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   381
			} else if (i == odd_middle_part) { // we are on the middle of odd bridge: #5 on even pos, #4 on odd
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   382
				piece = 5 - (i % 2);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   383
			} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   384
					// generate #2 and #3 in turns [i%2==0], after the middle of odd bridge
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   385
					// this sequence swaps [... XOR (i>odd_middle_part)],
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   386
					// for even bridges XOR does not apply as odd_middle_part==bridge_len
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   387
					piece = 2 + ((i % 2 == 0) ^ (i > odd_middle_part));
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   388
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   389
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   390
			if (transport == TRANSPORT_RAIL) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   391
				MakeRailBridgeMiddle(tile, bridge_type, piece, direction, railtype);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   392
			} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   393
				MakeRoadBridgeMiddle(tile, bridge_type, piece, direction);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   394
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   395
			switch (transport_under) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   396
				case TRANSPORT_RAIL: SetRailUnderBridge(tile, owner_under, rail_under); break;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   397
				case TRANSPORT_ROAD: SetRoadUnderBridge(tile, owner_under); break;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   398
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   399
				case TRANSPORT_WATER:
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   400
					if (owner_under == OWNER_WATER) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   401
						SetWaterUnderBridge(tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   402
					} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   403
						SetCanalUnderBridge(tile, owner_under);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   404
					}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   405
					break;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   406
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   407
				default: SetClearUnderBridge(tile); break;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   408
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   409
3178
c275826135da (svn r3814) Remove another call to FindLandscapeHeight()
tron
parents: 3172
diff changeset
   410
			MarkTileDirtyByTile(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
5400
5cb194d32614 (svn r7957) -Backport from trunk (r7936):
Darkvater
parents: 5116
diff changeset
   414
	if (flags & DC_EXEC) {
5cb194d32614 (svn r7957) -Backport from trunk (r7936):
Darkvater
parents: 5116
diff changeset
   415
		Axis axis = AxisToTrack(direction);
5cb194d32614 (svn r7957) -Backport from trunk (r7936):
Darkvater
parents: 5116
diff changeset
   416
		SetSignalsOnBothDir(tile_start, axis);
5cb194d32614 (svn r7957) -Backport from trunk (r7936):
Darkvater
parents: 5116
diff changeset
   417
		YapfNotifyTrackLayoutChange(tile_start, axis);
5cb194d32614 (svn r7957) -Backport from trunk (r7936):
Darkvater
parents: 5116
diff changeset
   418
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4344
diff changeset
   420
	/* for human player that builds the bridge he gets a selection to choose from bridges (DC_QUERY_COST)
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4344
diff changeset
   421
	 * It's unnecessary to execute this command every time for every bridge. So it is done only
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4344
diff changeset
   422
	 * and cost is computed in "bridge_gui.c". For AI, Towns this has to be of course calculated
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4344
diff changeset
   423
	 */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   424
	if (!(flags & DC_QUERY_COST)) {
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
   425
		const Bridge *b = &_bridge[bridge_type];
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
   426
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4344
diff changeset
   427
		bridge_len += 2; // begin and end tiles/ramps
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
4850
b4e9be22945f (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4849
diff changeset
   429
		if (IsValidPlayer(_current_player) && !_is_old_ai_player)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
			bridge_len = CalcBridgeLenCostFactor(bridge_len);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   432
		cost += (int64)bridge_len * _price.build_bridge * b->price >> 8;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   439
/** 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
   440
 * @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
   441
 * @param p1 railtype, 0x200 for road tunnel
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   442
 * @param p2 unused
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   443
 */
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
   444
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
   445
{
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   446
	TileIndexDiff delta;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   447
	TileIndex end_tile;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   448
	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
   449
	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
   450
	Slope end_tileh;
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   451
	uint start_z;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   452
	uint end_z;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   453
	int32 cost;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   454
	int32 ret;
5513
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5504
diff changeset
   455
	DiagDirection tunnel_in_way_dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   457
	_build_tunnel_endtile = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1782
diff changeset
   459
	if (p1 != 0x200 && !ValParamRailtype(p1)) return CMD_ERROR;
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   460
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   461
	start_tileh = GetTileSlope(start_tile, &start_z);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   463
	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
   464
		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
   465
		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
   466
		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
   467
		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
   468
		default: return_cmd_error(STR_500B_SITE_UNSUITABLE_FOR_TUNNEL);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   469
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
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
   471
	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
   472
	if (CmdFailed(ret)) return ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   473
4273
6467c2c54d58 (svn r5901) -Fix [FS#253]: Differing price calculation for tunnels depending on starting point
Darkvater
parents: 4261
diff changeset
   474
	/* 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
   475
	 * 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
   476
	 * 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
   477
	 * 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
   478
	cost = 0;
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4549
diff changeset
   479
	delta = TileOffsByDiagDir(direction);
5513
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5504
diff changeset
   480
	if (OtherAxis(DiagDirToAxis(direction)) == AXIS_X) {
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5504
diff changeset
   481
		tunnel_in_way_dir = (TileX(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE;
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5504
diff changeset
   482
	} else {
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5504
diff changeset
   483
		tunnel_in_way_dir = (TileY(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW;
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5504
diff changeset
   484
	}
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5504
diff changeset
   485
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   486
	end_tile = start_tile;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   487
	for (;;) {
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   488
		end_tile += delta;
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   489
		end_tileh = GetTileSlope(end_tile, &end_z);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   491
		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
   492
5513
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5504
diff changeset
   493
		if (!_cheats.crossing_tunnels.value && IsTunnelInWayDir(end_tile, start_z, tunnel_in_way_dir)) {
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
   494
			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
   495
		}
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   496
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   497
		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
   498
		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
   499
		if (cost >= 400000000) cost = 400000000;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
4273
6467c2c54d58 (svn r5901) -Fix [FS#253]: Differing price calculation for tunnels depending on starting point
Darkvater
parents: 4261
diff changeset
   502
	/* 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
   503
	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
   504
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   505
	// 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
   506
	_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
   507
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   508
	// 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
   509
	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
   510
		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
   511
		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
   512
	} 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
   513
		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
   514
		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
   515
	}
3064
9b9e5b2e2c20 (svn r3653) -Fix: You didn't pay for terraforming at the end tile when building a tunnel
tron
parents: 3063
diff changeset
   516
	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
   517
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   518
	if (flags & DC_EXEC) {
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   519
		if (GB(p1, 9, 1) == TRANSPORT_RAIL) {
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   520
			MakeRailTunnel(start_tile, _current_player, direction,                 GB(p1, 0, 4));
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   521
			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
   522
			UpdateSignalsOnSegment(start_tile, direction);
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   523
			YapfNotifyTrackLayoutChange(start_tile, AxisToTrack(DiagDirToAxis(direction)));
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   524
		} else {
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   525
			MakeRoadTunnel(start_tile, _current_player, direction);
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   526
			MakeRoadTunnel(end_tile,   _current_player, ReverseDiagDir(direction));
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   527
		}
3063
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   528
	}
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   529
794656a0856e (svn r3652) Rewrite CmdBuildTunnel in the hope to make it a bit more comprehensible
tron
parents: 3056
diff changeset
   530
	return cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   532
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   533
TileIndex CheckTunnelBusy(TileIndex tile, uint *length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   534
{
1335
a635854c23b6 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1330
diff changeset
   535
	uint z = GetTileZ(tile);
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   536
	DiagDirection dir = GetTunnelDirection(tile);
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4549
diff changeset
   537
	TileIndexDiff delta = TileOffsByDiagDir(dir);
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   538
	uint len = 0;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
   539
	TileIndex starttile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
	Vehicle *v;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   541
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
   542
	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
   543
		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
   544
		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
   545
	} 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
   546
		!IsTunnelTile(tile) ||
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   547
		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
   548
		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
   549
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
5427
63ce73145566 (svn r8623) [0.5] -Backport from trunk (8593, 8608, 8619, 8620)
rubidium
parents: 5400
diff changeset
   551
	v = FindVehicleBetween(starttile, tile, z, false);
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   552
	if (v != NULL) {
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   553
		_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
   554
			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
   555
		return INVALID_TILE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   556
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   557
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   558
	if (length != NULL) *length = len;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   559
	return tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
5009
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   562
static inline bool CheckAllowRemoveTunnelBridge(TileIndex tile)
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   563
{
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   564
	/* 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
   565
	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
   566
	/* 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
   567
	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
   568
	/* 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
   569
	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
   570
	return false;
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   571
}
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   572
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
   573
static int32 DoClearTunnel(TileIndex tile, uint32 flags)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
{
5009
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   575
	Town *t = NULL;
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   576
	TileIndex endtile;
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   577
	uint length;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
5009
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   581
	if (!CheckAllowRemoveTunnelBridge(tile)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
	endtile = CheckTunnelBusy(tile, &length);
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   584
	if (endtile == INVALID_TILE) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
	_build_tunnel_endtile = endtile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   587
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
   588
	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
   589
		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
   590
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   591
		/* 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
   592
		 * Removal depends on difficulty settings */
2958
3f8946daf55f (svn r3520) Remove unused parameters from some functions
tron
parents: 2952
diff changeset
   593
		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
   594
			SetDParam(0, t->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
			return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
	if (flags & DC_EXEC) {
38
d20549a6ea13 (svn r39) -Fix [1008605] Signals not updated after ClearTunnel Bug [985920] (TrueLight)
darkvater
parents: 22
diff changeset
   600
		// 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
   601
		//  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
   602
		DiagDirection dir = GetTunnelDirection(tile);
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   603
		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
   604
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
   605
		// 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
   606
		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
   607
			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
   608
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
		DoClearSquare(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
		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
   611
		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
   612
		UpdateSignalsOnSegment(endtile, dir);
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   613
		track = AxisToTrack(DiagDirToAxis(dir));
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   614
		YapfNotifyTrackLayoutChange(tile, track);
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   615
		YapfNotifyTrackLayoutChange(endtile, track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
	}
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   617
	return _price.clear_tunnel * (length + 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   620
5439
7af52897085a (svn r8687) [0.5] -Fix [FS#617]: assertion when getting the height of a bridge from the ramp tile.
rubidium
parents: 5427
diff changeset
   621
uint GetBridgeHeightRamp(TileIndex t)
3779
86bbf020ec2c (svn r4771) -Regression: It was possible to remove a bridge while a vehicle was on it
tron
parents: 3774
diff changeset
   622
{
4156
0292b2b52e03 (svn r5580) -Regression: Under certain circumstances it was possible to remove/convert bridges while a vehicle was on it. Fix this by properly calculating the height of a bridge
tron
parents: 4087
diff changeset
   623
	uint h;
0292b2b52e03 (svn r5580) -Regression: Under certain circumstances it was possible to remove/convert bridges while a vehicle was on it. Fix this by properly calculating the height of a bridge
tron
parents: 4087
diff changeset
   624
	uint tileh = GetTileSlope(t, &h);
0292b2b52e03 (svn r5580) -Regression: Under certain circumstances it was possible to remove/convert bridges while a vehicle was on it. Fix this by properly calculating the height of a bridge
tron
parents: 4087
diff changeset
   625
	uint f = GetBridgeFoundation(tileh, DiagDirToAxis(GetBridgeRampDirection(t)));
0292b2b52e03 (svn r5580) -Regression: Under certain circumstances it was possible to remove/convert bridges while a vehicle was on it. Fix this by properly calculating the height of a bridge
tron
parents: 4087
diff changeset
   626
0292b2b52e03 (svn r5580) -Regression: Under certain circumstances it was possible to remove/convert bridges while a vehicle was on it. Fix this by properly calculating the height of a bridge
tron
parents: 4087
diff changeset
   627
	// one height level extra if the ramp is on a flat foundation
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   628
	return
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   629
		h + TILE_HEIGHT +
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   630
		(IS_INT_INSIDE(f, 1, 15) ? TILE_HEIGHT : 0) +
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   631
		(IsSteepSlope(tileh) ? TILE_HEIGHT : 0);
3779
86bbf020ec2c (svn r4771) -Regression: It was possible to remove a bridge while a vehicle was on it
tron
parents: 3774
diff changeset
   632
}
86bbf020ec2c (svn r4771) -Regression: It was possible to remove a bridge while a vehicle was on it
tron
parents: 3774
diff changeset
   633
86bbf020ec2c (svn r4771) -Regression: It was possible to remove a bridge while a vehicle was on it
tron
parents: 3774
diff changeset
   634
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
   635
static int32 DoClearBridge(TileIndex tile, uint32 flags)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
{
3227
8bb33a2299a5 (svn r3900) When clearing a bridge determine the bridge direction and tile offset once instead of all over the place; also use UpdateSignalsOnSegment() instead of SetSignalsOnBothDir(), because this is sufficient
tron
parents: 3226
diff changeset
   637
	DiagDirection direction;
8bb33a2299a5 (svn r3900) When clearing a bridge determine the bridge direction and tile offset once instead of all over the place; also use UpdateSignalsOnSegment() instead of SetSignalsOnBothDir(), because this is sufficient
tron
parents: 3226
diff changeset
   638
	TileIndexDiff delta;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
   639
	TileIndex endtile;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   640
	Vehicle *v;
5009
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   641
	Town *t = NULL;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   642
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   645
	if (IsBridgeMiddle(tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   646
		if (IsTransportUnderBridge(tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   647
			/* delete transport route under the bridge */
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   648
			int32 cost;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   649
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   650
			// check if we own the tile below the bridge..
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   651
			if (_current_player != OWNER_WATER && (!CheckTileOwnership(tile) || !EnsureNoVehicleOnGround(tile)))
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   652
				return CMD_ERROR;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   653
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   654
			if (GetTransportTypeUnderBridge(tile) == TRANSPORT_RAIL) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   655
				cost = _price.remove_rail;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   656
			} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   657
				cost = _price.remove_road * 2;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   658
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   659
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   660
			if (flags & DC_EXEC) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   661
				SetClearUnderBridge(tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   662
				MarkTileDirtyByTile(tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   663
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   664
			return cost;
4087
5ae6c880d66c (svn r5403) - Feature: Allow building canals at sea-level, using ctrl to toggle canal or plain water tile. This allows building of non-raisable sea-level water ways (useful in multiplayer) and dikes for low-level areas.
peter1138
parents: 4077
diff changeset
   665
		} else if (IsWaterUnderBridge(tile) && !IsTileOwner(tile, OWNER_WATER)) {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   666
			/* delete canal under bridge */
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   667
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   668
			// check for vehicles under bridge
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   669
			if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   670
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   671
			if (flags & DC_EXEC) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   672
				SetClearUnderBridge(tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   673
				MarkTileDirtyByTile(tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   674
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   675
			return _price.clear_water;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   676
		}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   677
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   678
		tile = GetSouthernBridgeEnd(tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   679
	}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   680
5009
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   681
	if (!CheckAllowRemoveTunnelBridge(tile)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   682
3228
270ab760cc16 (svn r3901) Remove the last user of FindEdgesOfBridge() by using functions with similar functionality
tron
parents: 3227
diff changeset
   683
	endtile = GetOtherBridgeEnd(tile);
270ab760cc16 (svn r3901) Remove the last user of FindEdgesOfBridge() by using functions with similar functionality
tron
parents: 3227
diff changeset
   684
5494
374bd2a631c7 (svn r9849) [0.5] -Backport from trunk (r9693, r9694, r9697, r9718, r9719, r9725 and 9726):
rubidium
parents: 5483
diff changeset
   685
	if (!EnsureNoVehicle(tile) || !EnsureNoVehicle(endtile)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   686
3227
8bb33a2299a5 (svn r3900) When clearing a bridge determine the bridge direction and tile offset once instead of all over the place; also use UpdateSignalsOnSegment() instead of SetSignalsOnBothDir(), because this is sufficient
tron
parents: 3226
diff changeset
   687
	direction = GetBridgeRampDirection(tile);
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4549
diff changeset
   688
	delta = TileOffsByDiagDir(direction);
3227
8bb33a2299a5 (svn r3900) When clearing a bridge determine the bridge direction and tile offset once instead of all over the place; also use UpdateSignalsOnSegment() instead of SetSignalsOnBothDir(), because this is sufficient
tron
parents: 3226
diff changeset
   689
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
   690
	/* Make sure there's no vehicle on the bridge
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
   691
	 * Omit tile and endtile, since these are already checked, thus solving the
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
   692
	 * problem of bridges over water, or higher bridges, where z is not increased,
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
   693
	 * eg level bridge
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
   694
	 */
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   695
	v = FindVehicleBetween(
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   696
		tile    + delta,
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   697
		endtile - delta,
5427
63ce73145566 (svn r8623) [0.5] -Backport from trunk (8593, 8608, 8619, 8620)
rubidium
parents: 5400
diff changeset
   698
		GetBridgeHeightRamp(tile), false
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   699
	);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   700
	if (v != NULL) return_cmd_error(VehicleInTheWayErrMsg(v));
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   701
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
   702
	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
   703
		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
   704
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   705
		/* Check if you are allowed to remove the bridge 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
   706
		 * Removal depends on difficulty settings */
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   707
		if (!CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE)) {
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   708
			SetDParam(0, t->index);
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   709
			return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
437645a303b4 (svn r7028) -Fix [FS#200]: Scenario bridges/tunnels cannot be demolished; now it's possible to
Darkvater
parents: 4850
diff changeset
   710
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   711
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
	if (flags & DC_EXEC) {
3217
60f832048cd4 (svn r3890) Simplify deleting a bridge by handling the ramps outside of the removal loop
tron
parents: 3216
diff changeset
   714
		TileIndex c;
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   715
		Track track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
1005
8c6a9bf44bf1 (svn r1504) enummed town ratings (Jango)
celestar
parents: 959
diff changeset
   717
		//checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
		// you have a "Poor" (0) town rating
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
   719
		if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR)
1005
8c6a9bf44bf1 (svn r1504) enummed town ratings (Jango)
celestar
parents: 959
diff changeset
   720
			ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   721
3217
60f832048cd4 (svn r3890) Simplify deleting a bridge by handling the ramps outside of the removal loop
tron
parents: 3216
diff changeset
   722
		DoClearSquare(tile);
60f832048cd4 (svn r3890) Simplify deleting a bridge by handling the ramps outside of the removal loop
tron
parents: 3216
diff changeset
   723
		DoClearSquare(endtile);
60f832048cd4 (svn r3890) Simplify deleting a bridge by handling the ramps outside of the removal loop
tron
parents: 3216
diff changeset
   724
		for (c = tile + delta; c != endtile; c += delta) {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   725
			if (IsTransportUnderBridge(c)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   726
				if (GetTransportTypeUnderBridge(c) == TRANSPORT_RAIL) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   727
					MakeRailNormal(c, GetTileOwner(c), GetRailBitsUnderBridge(c), GetRailType(c));
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   728
				} else {
4849
9a3c3ae7f62f (svn r6775) -Codechange: Use some more proper types, especially Owner and PlayerID as
Darkvater
parents: 4848
diff changeset
   729
					TownID town = IsTileOwner(c, OWNER_TOWN) ? ClosestTownFromTile(c, (uint)-1)->index : 0;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   730
					MakeRoadNormal(c, GetTileOwner(c), GetRoadBitsUnderBridge(c), town);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   731
				}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   732
				MarkTileDirtyByTile(c);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   733
			} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   734
				if (IsClearUnderBridge(c)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   735
					DoClearSquare(c);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   736
				} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   737
					if (GetTileSlope(c, NULL) == SLOPE_FLAT) {
4087
5ae6c880d66c (svn r5403) - Feature: Allow building canals at sea-level, using ctrl to toggle canal or plain water tile. This allows building of non-raisable sea-level water ways (useful in multiplayer) and dikes for low-level areas.
peter1138
parents: 4077
diff changeset
   738
						if (IsTileOwner(c, OWNER_WATER)) {
5ae6c880d66c (svn r5403) - Feature: Allow building canals at sea-level, using ctrl to toggle canal or plain water tile. This allows building of non-raisable sea-level water ways (useful in multiplayer) and dikes for low-level areas.
peter1138
parents: 4077
diff changeset
   739
							MakeWater(c);
5ae6c880d66c (svn r5403) - Feature: Allow building canals at sea-level, using ctrl to toggle canal or plain water tile. This allows building of non-raisable sea-level water ways (useful in multiplayer) and dikes for low-level areas.
peter1138
parents: 4077
diff changeset
   740
						} else {
5ae6c880d66c (svn r5403) - Feature: Allow building canals at sea-level, using ctrl to toggle canal or plain water tile. This allows building of non-raisable sea-level water ways (useful in multiplayer) and dikes for low-level areas.
peter1138
parents: 4077
diff changeset
   741
							MakeCanal(c, GetTileOwner(c));
5ae6c880d66c (svn r5403) - Feature: Allow building canals at sea-level, using ctrl to toggle canal or plain water tile. This allows building of non-raisable sea-level water ways (useful in multiplayer) and dikes for low-level areas.
peter1138
parents: 4077
diff changeset
   742
						}
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   743
					} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   744
						MakeShore(c);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   745
					}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   746
					MarkTileDirtyByTile(c);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   747
				}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   748
			}
3217
60f832048cd4 (svn r3890) Simplify deleting a bridge by handling the ramps outside of the removal loop
tron
parents: 3216
diff changeset
   749
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   750
3227
8bb33a2299a5 (svn r3900) When clearing a bridge determine the bridge direction and tile offset once instead of all over the place; also use UpdateSignalsOnSegment() instead of SetSignalsOnBothDir(), because this is sufficient
tron
parents: 3226
diff changeset
   751
		UpdateSignalsOnSegment(tile, ReverseDiagDir(direction));
8bb33a2299a5 (svn r3900) When clearing a bridge determine the bridge direction and tile offset once instead of all over the place; also use UpdateSignalsOnSegment() instead of SetSignalsOnBothDir(), because this is sufficient
tron
parents: 3226
diff changeset
   752
		UpdateSignalsOnSegment(endtile, direction);
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   753
		track = AxisToTrack(DiagDirToAxis(direction));
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   754
		YapfNotifyTrackLayoutChange(tile, track);
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   755
		YapfNotifyTrackLayoutChange(endtile, track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   756
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   757
3227
8bb33a2299a5 (svn r3900) When clearing a bridge determine the bridge direction and tile offset once instead of all over the place; also use UpdateSignalsOnSegment() instead of SetSignalsOnBothDir(), because this is sufficient
tron
parents: 3226
diff changeset
   758
	return (DistanceManhattan(tile, endtile) + 1) * _price.clear_bridge;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   759
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   760
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
   761
static int32 ClearTile_TunnelBridge(TileIndex tile, byte flags)
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
   762
{
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
   763
	if (IsTunnel(tile)) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   764
		if (flags & DC_AUTO) return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   765
		return DoClearTunnel(tile, flags);
3234
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3233
diff changeset
   766
	} else if (IsBridge(tile)) { // XXX Is this necessary?
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   767
		if (flags & DC_AUTO) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
1082
56a4c048c5c3 (svn r1583) -Fix: You should no longer be able to delete bridges on any type of underground when there is a vehicle on it
darkvater
parents: 1073
diff changeset
   768
		return DoClearBridge(tile, flags);
1109
1bab892228cd (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1095
diff changeset
   769
	}
1bab892228cd (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1095
diff changeset
   770
1082
56a4c048c5c3 (svn r1583) -Fix: You should no longer be able to delete bridges on any type of underground when there is a vehicle on it
darkvater
parents: 1073
diff changeset
   771
	return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   772
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   773
5481
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   774
/**
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   775
 * Switches the rail type for a tunnel or a bridgehead. As the railtype
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   776
 * on the bridge are determined by the one of the bridgehead, this
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   777
 * functions converts the railtype on the entire bridge.
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   778
 * @param tile        The tile on which the railtype is to be convert.
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   779
 * @param totype      The railtype we want to convert to
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   780
 * @param exec        Switches between test and execute mode
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   781
 * @return            The cost and state of the operation
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   782
 * @retval CMD_ERROR  An error occured during the operation.
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   783
 */
3435
f299c6f48763 (svn r4265) - Fix: compile warning on VS2005 (parameter 2 different from declaration)
Darkvater
parents: 3422
diff changeset
   784
int32 DoConvertTunnelBridgeRail(TileIndex tile, RailType totype, bool exec)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   785
{
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   786
	TileIndex endtile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   787
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
   788
	if (IsTunnel(tile) && 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
   789
		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
   790
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   791
		if (!CheckTileOwnership(tile)) return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3234
diff changeset
   793
		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
   794
5116
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 5057
diff changeset
   795
		// '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
   796
		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
   797
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   798
		endtile = CheckTunnelBusy(tile, &length);
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1419
diff changeset
   799
		if (endtile == INVALID_TILE) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   801
		if (exec) {
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   802
			Track track;
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3234
diff changeset
   803
			SetRailType(tile, totype);
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3234
diff changeset
   804
			SetRailType(endtile, totype);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   805
			MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
			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
   807
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   808
			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
   809
			YapfNotifyTrackLayoutChange(tile, track);
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   810
			YapfNotifyTrackLayoutChange(endtile, track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   811
		}
5481
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   812
		return (length + 1) * (_price.build_rail / 2);
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   813
	} else if (IsBridge(tile) &&
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   814
			IsBridgeMiddle(tile) &&
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   815
			IsTransportUnderBridge(tile) &&
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   816
			GetTransportTypeUnderBridge(tile) == TRANSPORT_RAIL) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   817
		// only check for train under bridge
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   818
		if (!CheckTileOwnership(tile) || !EnsureNoVehicleOnGround(tile))
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   819
			return CMD_ERROR;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   820
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   821
		if (GetRailType(tile) == totype) return CMD_ERROR;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   822
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   823
		if (exec) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   824
			SetRailType(tile, totype);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   825
			MarkTileDirtyByTile(tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   826
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   827
			YapfNotifyTrackLayoutChange(tile, GetRailUnderBridge(tile));
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   828
		}
5481
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   829
		return _price.build_rail / 2;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   830
	} else if (IsBridge(tile) && IsBridgeRamp(tile) && GetBridgeTransportType(tile) == TRANSPORT_RAIL) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   831
		TileIndexDiff delta;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   832
		int32 cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
		if (!CheckTileOwnership(tile)) return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   835
3215
56c250955b3b (svn r3888) If you think you've checked everything, check once more. *sigh* Fix r3887
tron
parents: 3214
diff changeset
   836
		endtile = GetOtherBridgeEnd(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   837
5494
374bd2a631c7 (svn r9849) [0.5] -Backport from trunk (r9693, r9694, r9697, r9718, r9719, r9725 and 9726):
rubidium
parents: 5483
diff changeset
   838
		if (!EnsureNoVehicle(tile) ||
374bd2a631c7 (svn r9849) [0.5] -Backport from trunk (r9693, r9694, r9697, r9718, r9719, r9725 and 9726):
rubidium
parents: 5483
diff changeset
   839
				!EnsureNoVehicle(endtile) ||
5427
63ce73145566 (svn r8623) [0.5] -Backport from trunk (8593, 8608, 8619, 8620)
rubidium
parents: 5400
diff changeset
   840
				FindVehicleBetween(tile, endtile, GetBridgeHeightRamp(tile), false) != NULL) {
3881
b2a66893f250 (svn r4937) Reduce the use of _error_message a bit
tron
parents: 3878
diff changeset
   841
			return_cmd_error(STR_8803_TRAIN_IN_THE_WAY);
1073
0e844583b549 (svn r1574) -Fix [ 1105281 ] upgrade rail fails when train under bridge
celestar
parents: 1067
diff changeset
   842
		}
0e844583b549 (svn r1574) -Fix [ 1105281 ] upgrade rail fails when train under bridge
celestar
parents: 1067
diff changeset
   843
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3234
diff changeset
   844
		if (GetRailType(tile) == totype) return CMD_ERROR;
3213
8e56434d36d5 (svn r3885) Simplify DoConvertTunnelBridgeRail() a bit
tron
parents: 3209
diff changeset
   845
3218
68decfba698f (svn r3891) Fix r3885
tron
parents: 3217
diff changeset
   846
		if (exec) {
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   847
			Track track;
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3234
diff changeset
   848
			SetRailType(tile, totype);
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3234
diff changeset
   849
			SetRailType(endtile, totype);
3218
68decfba698f (svn r3891) Fix r3885
tron
parents: 3217
diff changeset
   850
			MarkTileDirtyByTile(tile);
68decfba698f (svn r3891) Fix r3885
tron
parents: 3217
diff changeset
   851
			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
   852
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   853
			track = AxisToTrack(DiagDirToAxis(GetBridgeRampDirection(tile)));
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   854
			YapfNotifyTrackLayoutChange(tile, track);
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
   855
			YapfNotifyTrackLayoutChange(endtile, track);
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   856
		}
5481
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   857
		cost = 2 * (_price.build_rail / 2);
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4549
diff changeset
   858
		delta = TileOffsByDiagDir(GetBridgeRampDirection(tile));
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   859
		for (tile += delta; tile != endtile; tile += delta) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   860
			if (exec) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   861
				SetRailTypeOnBridge(tile, totype);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   862
				MarkTileDirtyByTile(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   863
			}
5481
acf12c0e6f31 (svn r9582) [0.5] -Backport from trunk (r9425, r9437, r9455, r9519):
truelight
parents: 5459
diff changeset
   864
			cost += _price.build_rail / 2;
3213
8e56434d36d5 (svn r3885) Simplify DoConvertTunnelBridgeRail() a bit
tron
parents: 3209
diff changeset
   865
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   867
		return cost;
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
   868
	} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   869
		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
   870
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   871
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   872
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   873
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   874
// fast routine for getting the height of a middle bridge tile. 'tile' MUST be a middle bridge tile.
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   875
uint GetBridgeHeight(TileIndex t)
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   876
{
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   877
	return GetBridgeHeightRamp(GetSouthernBridgeEnd(t));
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   878
}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   879
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   880
static const byte _bridge_foundations[][31] = {
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
   881
	// 0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15                              _S              _W      _N  _E
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
   882
	{  0, 16, 18,  3, 20,  5,  0,  7, 22,  0, 10, 11, 12, 13, 14,  0,  0,  0,  0,  0,  0,  0,  0, 18,  0,  0,  0, 16,  0, 22, 20 },
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
   883
	{  0, 15, 17,  0, 19,  5,  6,  7, 21,  9, 10, 11,  0, 13, 14,  0,  0,  0,  0,  0,  0,  0,  0, 17,  0,  0,  0, 15,  0, 21, 19 }
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   884
};
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   885
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   886
extern const byte _road_sloped_sprites[14];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   887
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   888
static void DrawBridgePillars(PalSpriteID image, const TileInfo *ti, int x, int y, int z)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
	if (image != 0) {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   891
		Axis axis = GetBridgeAxis(ti->tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   892
		bool drawfarpillar = !HASBIT(GetBridgeFlags(GetBridgeType(ti->tile)), 0);
3556
d25b35568c48 (svn r4430) - NewGRF: Use the bridge's sprite layout for drawing high pillars. This fixes newgrf's high-bridge pillars drawing. Also remove the now obsolete _bridge_poles_table which was already integrated into the bridge sprite tables
Darkvater
parents: 3539
diff changeset
   893
		int back_height, front_height;
d25b35568c48 (svn r4430) - NewGRF: Use the bridge's sprite layout for drawing high pillars. This fixes newgrf's high-bridge pillars drawing. Also remove the now obsolete _bridge_poles_table which was already integrated into the bridge sprite tables
Darkvater
parents: 3539
diff changeset
   894
		int i = z;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
		const byte *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   896
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
		static const byte _tileh_bits[4][8] = {
4191
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
   898
			{ 2, 1, 8, 4,  16,  2, 0, 9 },
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
   899
			{ 1, 8, 4, 2,   2, 16, 9, 0 },
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
   900
			{ 4, 8, 1, 2,  16,  2, 0, 9 },
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
   901
			{ 2, 4, 8, 1,   2, 16, 9, 0 }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   902
		};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
2148
47ba4a1b1c3b (svn r2658) -Codechange: Use MAKE_TRANSPARENT to display a transparented sprite
celestar
parents: 2140
diff changeset
   904
		if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
333
6dee54ed9701 (svn r500) -Fix: Some bridge part isn't displayed transparent in transparent mode
tron
parents: 241
diff changeset
   905
3234
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3233
diff changeset
   906
		p = _tileh_bits[(image & 1) * 2 + (axis == AXIS_X ? 0 : 1)];
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
   907
		front_height = ti->z + (ti->tileh & p[0] ? TILE_HEIGHT : 0);
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
   908
		back_height  = ti->z + (ti->tileh & p[1] ? TILE_HEIGHT : 0);
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
   909
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
   910
		if (IsSteepSlope(ti->tileh)) {
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
   911
			if (!(ti->tileh & p[2])) front_height += TILE_HEIGHT;
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
   912
			if (!(ti->tileh & p[3])) back_height  += TILE_HEIGHT;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   913
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   914
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
   915
		for (; z >= front_height || z >= back_height; z -= TILE_HEIGHT) {
4191
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
   916
			/* HACK set height of the BB of pillars to 1, because the origin of the
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
   917
			 * sprites is at the top
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
   918
			 */
3556
d25b35568c48 (svn r4430) - NewGRF: Use the bridge's sprite layout for drawing high pillars. This fixes newgrf's high-bridge pillars drawing. Also remove the now obsolete _bridge_poles_table which was already integrated into the bridge sprite tables
Darkvater
parents: 3539
diff changeset
   919
			if (z >= front_height) { // front facing pillar
4191
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
   920
				AddSortableSpriteToDraw(image, x, y, p[4], p[5], 1, z);
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   921
			}
3556
d25b35568c48 (svn r4430) - NewGRF: Use the bridge's sprite layout for drawing high pillars. This fixes newgrf's high-bridge pillars drawing. Also remove the now obsolete _bridge_poles_table which was already integrated into the bridge sprite tables
Darkvater
parents: 3539
diff changeset
   922
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
   923
			if (drawfarpillar && z >= back_height && z < i - TILE_HEIGHT) { // back facing pillar
4191
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
   924
				AddSortableSpriteToDraw(image, x - p[6], y - p[7], p[4], p[5], 1, z);
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   925
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   926
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   927
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   928
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   929
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
   930
uint GetBridgeFoundation(Slope tileh, Axis axis)
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   931
{
3878
91a8b87f641e (svn r4927) Replace 3 big ifs (which regard foundations) by a bit less confusing code
tron
parents: 3854
diff changeset
   932
	uint i;
91a8b87f641e (svn r4927) Replace 3 big ifs (which regard foundations) by a bit less confusing code
tron
parents: 3854
diff changeset
   933
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
   934
	if (HASBIT(BRIDGE_FULL_LEVELED_FOUNDATION, tileh)) return tileh;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
	// inclined sloped building
3878
91a8b87f641e (svn r4927) Replace 3 big ifs (which regard foundations) by a bit less confusing code
tron
parents: 3854
diff changeset
   937
	switch (tileh) {
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   938
		case SLOPE_W:
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   939
		case SLOPE_STEEP_W: i = 0; break;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   940
		case SLOPE_S:
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   941
		case SLOPE_STEEP_S: i = 2; break;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   942
		case SLOPE_E:
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   943
		case SLOPE_STEEP_E: i = 4; break;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   944
		case SLOPE_N:
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   945
		case SLOPE_STEEP_N: i = 6; break;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
   946
		default: return 0;
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
   947
	}
3878
91a8b87f641e (svn r4927) Replace 3 big ifs (which regard foundations) by a bit less confusing code
tron
parents: 3854
diff changeset
   948
	if (axis != AXIS_X) ++i;
91a8b87f641e (svn r4927) Replace 3 big ifs (which regard foundations) by a bit less confusing code
tron
parents: 3854
diff changeset
   949
	return i + 15;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
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
   952
/**
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   953
 * Draws a tunnel of bridge tile.
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   954
 * For tunnels, this is rather simple, as you only needa draw the entrance.
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   955
 * Bridges are a bit more complex. base_offset is where the sprite selection comes into play
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   956
 * and it works a bit like a bitmask.<p> For bridge heads:
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   957
 * <ul><li>Bit 0: direction</li>
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   958
 * <li>Bit 1: northern or southern heads</li>
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   959
 * <li>Bit 2: Set if the bridge head is sloped</li>
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   960
 * <li>Bit 3 and more: Railtype Specific subset</li>
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   961
 * </ul>
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   962
 * For middle parts:
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   963
 * <ul><li>Bits 0-1: need to be 0</li>
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   964
 * <li>Bit 2: direction</li>
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   965
 * <li>Bit 3 and above: Railtype Specific subset</li>
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   966
 * </ul>
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   967
 * Please note that in this code, "roads" are treated as railtype 1, whilst the real railtypes are 0, 2 and 3
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   968
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
static void DrawTile_TunnelBridge(TileInfo *ti)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
	uint32 image;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   972
	const PalSpriteID *b;
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2008
diff changeset
   973
	bool ice = _m[ti->tile].m4 & 0x80;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   974
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
   975
	if (IsTunnel(ti->tile)) {
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   976
		if (GetTunnelTransportType(ti->tile) == TRANSPORT_RAIL) {
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3234
diff changeset
   977
			image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.tunnel;
2511
0a81d9ca79bc (svn r3037) Don't deduce the sprites for tunnels and level crossings from magic numbers, but fetch them from the central rail info array. This is a preparation step for electrified rails
tron
parents: 2502
diff changeset
   978
		} else {
0a81d9ca79bc (svn r3037) Don't deduce the sprites for tunnels and level crossings from magic numbers, but fetch them from the central rail info array. This is a preparation step for electrified rails
tron
parents: 2502
diff changeset
   979
			image = SPR_TUNNEL_ENTRY_REAR_ROAD;
0a81d9ca79bc (svn r3037) Don't deduce the sprites for tunnels and level crossings from magic numbers, but fetch them from the central rail info array. This is a preparation step for electrified rails
tron
parents: 2502
diff changeset
   980
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
2511
0a81d9ca79bc (svn r3037) Don't deduce the sprites for tunnels and level crossings from magic numbers, but fetch them from the central rail info array. This is a preparation step for electrified rails
tron
parents: 2502
diff changeset
   982
		if (ice) image += 32;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
   984
		image += GetTunnelDirection(ti->tile) * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
		DrawGroundSprite(image);
3534
8a32ddcb9951 (svn r4391) -Fix: faulty ")" in previous commit
celestar
parents: 3533
diff changeset
   986
		if (GetRailType(ti->tile) == RAILTYPE_ELECTRIC) DrawCatenary(ti);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
   988
		AddSortableSpriteToDraw(image+1, ti->x + TILE_SIZE - 1, ti->y + TILE_SIZE - 1, 1, 1, 8, (byte)ti->z);
3234
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3233
diff changeset
   989
	} else if (IsBridge(ti->tile)) { // XXX is this necessary?
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
   990
		int base_offset;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
3234
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3233
diff changeset
   992
		if (GetBridgeTransportType(ti->tile) == TRANSPORT_RAIL) {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   993
			RailType rt;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   994
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   995
			if (IsBridgeRamp(ti->tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   996
				rt = GetRailType(ti->tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   997
			} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   998
				rt = GetRailTypeOnBridge(ti->tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
   999
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1000
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1001
			base_offset = GetRailTypeInfo(rt)->bridge_offset;
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
  1002
			assert(base_offset != 8); /* This one is used for roads */
3234
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3233
diff changeset
  1003
		} else {
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3233
diff changeset
  1004
			base_offset = 8;
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
  1005
		}
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
  1006
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
  1007
		/* as the lower 3 bits are used for other stuff, make sure they are clear */
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
  1008
		assert( (base_offset & 0x07) == 0x00);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1010
		if (IsBridgeRamp(ti->tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1011
			if (!HASBIT(BRIDGE_NO_FOUNDATION, ti->tileh)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1012
				int f = GetBridgeFoundation(ti->tileh, DiagDirToAxis(GetBridgeRampDirection(ti->tile)));
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1013
				if (f) DrawFoundation(ti, f);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1014
			}
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3900
diff changeset
  1015
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1016
			// HACK Wizardry to convert the bridge ramp direction into a sprite offset
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1017
			base_offset += (6 - GetBridgeRampDirection(ti->tile)) % 4;
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3900
diff changeset
  1018
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1019
			if (ti->tileh == SLOPE_FLAT) base_offset += 4; // sloped bridge head
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1020
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1021
			/* Table number 6 always refers to the bridge heads for any bridge type */
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1022
			image = GetBridgeSpriteTable(GetBridgeType(ti->tile), 6)[base_offset];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1023
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1024
			if (!ice) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1025
				DrawClearLandTile(ti, 3);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1026
			} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1027
				DrawGroundSprite(SPR_FLAT_SNOWY_TILE + _tileh_to_sprite[ti->tileh]);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1028
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1029
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1030
			if (GetRailType(ti->tile) == RAILTYPE_ELECTRIC) DrawCatenary(ti);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1031
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1032
			// draw ramp
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1033
			if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
4191
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
  1034
			/* HACK set the height of the BB of a sloped ramp to 1 so a vehicle on
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
  1035
			 * it doesn't disappear behind it
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
  1036
			 */
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
  1037
			AddSortableSpriteToDraw(
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
  1038
				image, ti->x, ti->y, 16, 16, ti->tileh == SLOPE_FLAT ? 1 : 8, ti->z
25538ade07c2 (svn r5641) -Fix: Adjust/correct some bounding boxes. This fixes some graphical glitches near bridges
tron
parents: 4173
diff changeset
  1039
			);
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1040
		} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1041
			// bridge middle part.
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1042
			Axis axis = GetBridgeAxis(ti->tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1043
			uint z;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1044
			int x,y;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1045
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1046
			if (IsTransportUnderBridge(ti->tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1047
				uint f = _bridge_foundations[axis][ti->tileh];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1048
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1049
				if (f != 0) DrawFoundation(ti, f);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1050
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1051
				if (GetTransportTypeUnderBridge(ti->tile) == TRANSPORT_RAIL) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1052
					const RailtypeInfo* rti = GetRailTypeInfo(GetRailType(ti->tile));
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1053
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1054
					if (ti->tileh == SLOPE_FLAT) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1055
						image = (axis == AXIS_X ? SPR_RAIL_TRACK_Y : SPR_RAIL_TRACK_X);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1056
					} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1057
						image = SPR_RAIL_TRACK_Y + _track_sloped_sprites[ti->tileh - 1];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1058
					}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1059
					image += rti->total_offset;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1060
					if (ice) image += rti->snow_offset;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1061
				} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1062
					if (ti->tileh == SLOPE_FLAT) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1063
						image = (axis == AXIS_X ? SPR_ROAD_Y : SPR_ROAD_X);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1064
					} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1065
						image = _road_sloped_sprites[ti->tileh - 1] + 0x53F;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1066
					}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1067
					if (ice) image += 19;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1068
				}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1069
				DrawGroundSprite(image);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1070
			} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1071
				if (IsClearUnderBridge(ti->tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1072
					image = (ice ? SPR_FLAT_SNOWY_TILE : SPR_FLAT_GRASS_TILE);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1073
					DrawGroundSprite(image + _tileh_to_sprite[ti->tileh]);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1074
				} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1075
					if (ti->tileh == SLOPE_FLAT) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1076
						DrawGroundSprite(SPR_FLAT_WATER_TILE);
4087
5ae6c880d66c (svn r5403) - Feature: Allow building canals at sea-level, using ctrl to toggle canal or plain water tile. This allows building of non-raisable sea-level water ways (useful in multiplayer) and dikes for low-level areas.
peter1138
parents: 4077
diff changeset
  1077
						if (ti->z != 0 || !IsTileOwner(ti->tile, OWNER_WATER)) DrawCanalWater(ti->tile);
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1078
					} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1079
						DrawGroundSprite(_water_shore_sprites[ti->tileh]);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1080
					}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1081
				}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1082
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1083
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1084
			if (axis != AXIS_X) base_offset += 4;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1085
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1086
			/*  base_offset needs to be 0 due to the structure of the sprite table see table/bridge_land.h */
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1087
			assert( (base_offset & 0x03) == 0x00);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1088
			// get bridge sprites
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1089
			b = GetBridgeSpriteTable(GetBridgeType(ti->tile), GetBridgePiece(ti->tile)) + base_offset;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1090
4021
1e764a101ffc (svn r5259) -Codechange: GetBridgeHeight{Ramp,}() now returns the height of the bridge, not one level lower
tron
parents: 4000
diff changeset
  1091
			z = GetBridgeHeight(ti->tile) - 3;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1092
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1093
			// draw rail or road component
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1094
			image = b[0];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1095
			if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1096
			if (axis == AXIS_X) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1097
				AddSortableSpriteToDraw(image, ti->x, ti->y, 16, 11, 1, z);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1098
			} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1099
				AddSortableSpriteToDraw(image, ti->x, ti->y, 11, 16, 1, z);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1100
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1101
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1102
			x = ti->x;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1103
			y = ti->y;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1104
			image = b[1];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1105
			if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1106
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1107
			// draw roof, the component of the bridge which is logically between the vehicle and the camera
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1108
			if (axis == AXIS_X) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1109
				y += 12;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1110
				if (image & SPRITE_MASK) AddSortableSpriteToDraw(image, x, y, 16, 1, 0x28, z);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1111
			} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1112
				x += 12;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1113
				if (image & SPRITE_MASK) AddSortableSpriteToDraw(image, x, y, 1, 16, 0x28, z);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1114
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1115
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1116
			if (GetRailType(ti->tile) == RAILTYPE_ELECTRIC || GetRailTypeOnBridge(ti->tile) == RAILTYPE_ELECTRIC) DrawCatenary(ti);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1117
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1118
			if (ti->z + 5 == z) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1119
				// draw poles below for small bridges
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1120
				image = b[2];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1121
				if (image != 0) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1122
					if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1123
					DrawGroundSpriteAt(image, x, y, z);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1124
				}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1125
			} else if (_patches.bridge_pillars) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1126
				// draw pillars below for high bridges
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1127
				DrawBridgePillars(b[2], ti, x, y, z);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1128
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1129
		}
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3900
diff changeset
  1130
	}
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3900
diff changeset
  1131
}
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3900
diff changeset
  1132
4231
dea6a63dd058 (svn r5794) Pass the TileIndex plus x and y coordinates into GetSlopeZ_* instead of a TileInfo
tron
parents: 4191
diff changeset
  1133
static uint GetSlopeZ_TunnelBridge(TileIndex tile, uint x, uint y)
2537
d9c0df52a466 (svn r3066) Constify the parameter of GetSlopeZ_*()
tron
parents: 2536
diff changeset
  1134
{
4231
dea6a63dd058 (svn r5794) Pass the TileIndex plus x and y coordinates into GetSlopeZ_* instead of a TileInfo
tron
parents: 4191
diff changeset
  1135
	uint z;
dea6a63dd058 (svn r5794) Pass the TileIndex plus x and y coordinates into GetSlopeZ_* instead of a TileInfo
tron
parents: 4191
diff changeset
  1136
	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
  1137
dea6a63dd058 (svn r5794) Pass the TileIndex plus x and y coordinates into GetSlopeZ_* instead of a TileInfo
tron
parents: 4191
diff changeset
  1138
	x &= 0xF;
dea6a63dd058 (svn r5794) Pass the TileIndex plus x and y coordinates into GetSlopeZ_* instead of a TileInfo
tron
parents: 4191
diff changeset
  1139
	y &= 0xF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
3517
76814013e912 (svn r4373) Rewrite GetSlopeZ_TunnelBridge() and slightly change its behavior:
tron
parents: 3493
diff changeset
  1141
	if (IsTunnel(tile)) {
76814013e912 (svn r4373) Rewrite GetSlopeZ_TunnelBridge() and slightly change its behavior:
tron
parents: 3493
diff changeset
  1142
		uint pos = (DiagDirToAxis(GetTunnelDirection(tile)) == AXIS_X ? y : x);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1143
3517
76814013e912 (svn r4373) Rewrite GetSlopeZ_TunnelBridge() and slightly change its behavior:
tron
parents: 3493
diff changeset
  1144
		// In the tunnel entrance?
76814013e912 (svn r4373) Rewrite GetSlopeZ_TunnelBridge() and slightly change its behavior:
tron
parents: 3493
diff changeset
  1145
		if (5 <= pos && pos <= 10) return z;
76814013e912 (svn r4373) Rewrite GetSlopeZ_TunnelBridge() and slightly change its behavior:
tron
parents: 3493
diff changeset
  1146
	} else {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1147
		if (IsBridgeRamp(tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1148
			DiagDirection dir = GetBridgeRampDirection(tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1149
			uint pos = (DiagDirToAxis(dir) == AXIS_X ? y : x);
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3900
diff changeset
  1150
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1151
			// On the bridge ramp?
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1152
			if (5 <= pos && pos <= 10) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1153
				uint delta;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1154
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1155
				if (IsSteepSlope(tileh)) return z + TILE_HEIGHT * 2;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1156
				if (HASBIT(BRIDGE_HORZ_RAMP, tileh)) return z + TILE_HEIGHT;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1157
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1158
				if (HASBIT(BRIDGE_FULL_LEVELED_FOUNDATION, tileh)) z += TILE_HEIGHT;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1159
				switch (dir) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1160
					default:
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1161
					case DIAGDIR_NE: delta = (TILE_SIZE - 1 - x) / 2; break;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1162
					case DIAGDIR_SE: delta = y / 2; break;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1163
					case DIAGDIR_SW: delta = x / 2; break;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1164
					case DIAGDIR_NW: delta = (TILE_SIZE - 1 - y) / 2; break;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1165
				}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1166
				return z + 1 + delta;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1167
			} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1168
				uint f = GetBridgeFoundation(tileh, DiagDirToAxis(dir));
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1169
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1170
				if (f != 0) {
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1171
					if (IsSteepSlope(tileh)) {
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1172
						z += TILE_HEIGHT;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1173
					} else if (f < 15) {
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1174
						return z + TILE_HEIGHT;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1175
					}
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1176
					tileh = _inclined_tileh[f - 15];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1177
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1178
			}
3517
76814013e912 (svn r4373) Rewrite GetSlopeZ_TunnelBridge() and slightly change its behavior:
tron
parents: 3493
diff changeset
  1179
		} else {
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1180
			uint ground_z;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1181
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1182
			// HACK on the bridge?
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1183
			ground_z = z + TILE_HEIGHT;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1184
			if (tileh != SLOPE_FLAT) ground_z += TILE_HEIGHT;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1185
			if (IsSteepSlope(tileh)) ground_z += TILE_HEIGHT;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1186
			if (_get_z_hint >= ground_z) return _get_z_hint;
3517
76814013e912 (svn r4373) Rewrite GetSlopeZ_TunnelBridge() and slightly change its behavior:
tron
parents: 3493
diff changeset
  1187
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1188
			if (IsTransportUnderBridge(tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1189
				uint f = _bridge_foundations[GetBridgeAxis(tile)][tileh];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1190
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1191
				if (f != 0) {
4246
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1192
					if (IsSteepSlope(tileh)) {
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1193
						z += TILE_HEIGHT;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1194
					} else if (f < 15) {
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1195
						return z + TILE_HEIGHT;
14a8849c32df (svn r5833) -Feature: Allow building of (certain) rails, roads and bridge ramps on steep sloped tiles
tron
parents: 4239
diff changeset
  1196
					}
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1197
					tileh = _inclined_tileh[f - 15];
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1198
				}
3517
76814013e912 (svn r4373) Rewrite GetSlopeZ_TunnelBridge() and slightly change its behavior:
tron
parents: 3493
diff changeset
  1199
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1200
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1201
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1202
3517
76814013e912 (svn r4373) Rewrite GetSlopeZ_TunnelBridge() and slightly change its behavior:
tron
parents: 3493
diff changeset
  1203
	return z + GetPartialZ(x, y, tileh);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1204
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1205
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3556
diff changeset
  1206
static Slope GetSlopeTileh_TunnelBridge(TileIndex tile, Slope tileh)
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1207
{
4062
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1208
	uint f;
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1209
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1210
	if (IsTunnel(tile)) {
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1211
		return tileh;
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1212
	} else {
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1213
		if (IsBridgeRamp(tile)) {
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1214
			if (HASBIT(BRIDGE_NO_FOUNDATION, tileh)) {
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1215
				return tileh;
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1216
			} else {
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1217
				f = GetBridgeFoundation(tileh, DiagDirToAxis(GetBridgeRampDirection(tile)));
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1218
			}
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1219
		} else {
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1220
			if (IsTransportUnderBridge(tile)) {
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1221
				f = _bridge_foundations[GetBridgeAxis(tile)][tileh];
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1222
			} else {
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1223
				return tileh;
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1224
			}
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1225
		}
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1226
	}
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1227
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1228
	if (f == 0) return tileh;
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1229
	if (f < 15) return SLOPE_FLAT;
e4a3faed6fe4 (svn r5352) -Fix: Return accurate slope information for tunnels and bridges to fix several foundation graphics glitches
tron
parents: 4046
diff changeset
  1230
	return _inclined_tileh[f - 15];
39
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 38
diff changeset
  1231
}
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 38
diff changeset
  1232
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 38
diff changeset
  1233
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
  1234
static void GetAcceptedCargo_TunnelBridge(TileIndex tile, AcceptedCargo ac)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1235
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1236
	/* not used */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1237
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1238
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1239
static const StringID _bridge_tile_str[(MAX_BRIDGES + 3) + (MAX_BRIDGES + 3)] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1240
	STR_501F_WOODEN_RAIL_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1241
	STR_5020_CONCRETE_RAIL_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1242
	STR_501C_STEEL_GIRDER_RAIL_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1243
	STR_501E_REINFORCED_CONCRETE_SUSPENSION,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1244
	STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1245
	STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1246
	STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1247
	STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1248
	STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1249
	STR_501C_STEEL_GIRDER_RAIL_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1250
	STR_5027_TUBULAR_RAIL_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1251
	STR_5027_TUBULAR_RAIL_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1252
	STR_5027_TUBULAR_RAIL_BRIDGE,
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
  1253
	0, 0, 0,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1255
	STR_5025_WOODEN_ROAD_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1256
	STR_5026_CONCRETE_ROAD_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1257
	STR_5022_STEEL_GIRDER_ROAD_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1258
	STR_5024_REINFORCED_CONCRETE_SUSPENSION,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1259
	STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1260
	STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1261
	STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1262
	STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1263
	STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1264
	STR_5022_STEEL_GIRDER_ROAD_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1265
	STR_5028_TUBULAR_ROAD_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1266
	STR_5028_TUBULAR_ROAD_BRIDGE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1267
	STR_5028_TUBULAR_ROAD_BRIDGE,
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
  1268
	0, 0, 0,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1269
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1270
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
  1271
static void GetTileDesc_TunnelBridge(TileIndex tile, TileDesc *td)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1272
{
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
  1273
	if (IsTunnel(tile)) {
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
  1274
		td->str = (GetTunnelTransportType(tile) == TRANSPORT_RAIL) ?
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
  1275
			STR_5017_RAILROAD_TUNNEL : STR_5018_ROAD_TUNNEL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1276
	} else {
3234
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3233
diff changeset
  1277
		td->str = _bridge_tile_str[GetBridgeTransportType(tile) << 4 | GetBridgeType(tile)];
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1278
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1279
		// the owner is stored at the end of the bridge
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1280
		if (IsBridgeMiddle(tile)) tile = GetSouthernBridgeEnd(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1281
	}
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
  1282
	td->owner = GetTileOwner(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1283
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1284
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1285
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
  1286
static void AnimateTile_TunnelBridge(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1287
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1288
	/* not used */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1289
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1290
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
  1291
static void TileLoop_TunnelBridge(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1292
{
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
  1293
	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
  1294
		case LT_HILLY:
4160
49ccad677eb7 (svn r5592) Merge two complementary cases
tron
parents: 4158
diff changeset
  1295
			if (HASBIT(_m[tile].m4, 7) != (GetTileZ(tile) > _opt.snow_line)) {
49ccad677eb7 (svn r5592) Merge two complementary cases
tron
parents: 4158
diff changeset
  1296
				TOGGLEBIT(_m[tile].m4, 7);
49ccad677eb7 (svn r5592) Merge two complementary cases
tron
parents: 4158
diff changeset
  1297
				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
  1298
			}
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
  1299
			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
  1300
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
  1301
		case LT_DESERT:
3379
ea8aa9e71328 (svn r4181) CodeChange : Replaced [G/S]etMapExtraBits by [G/S]etTropicZone. Although it was an accessor, nor his usage nor the values were clear.
belugas
parents: 3367
diff changeset
  1302
			if (GetTropicZone(tile) == TROPICZONE_DESERT && !(_m[tile].m4 & 0x80)) {
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2008
diff changeset
  1303
				_m[tile].m4 |= 0x80;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1304
				MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1305
			}
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
  1306
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1307
	}
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1308
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1309
	if (IsBridge(tile) && IsBridgeMiddle(tile) && IsWaterUnderBridge(tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1310
		TileLoop_Water(tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1311
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1312
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1313
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
  1314
static void ClickTile_TunnelBridge(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1315
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1316
	/* not used */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1317
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1318
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1319
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
  1320
static uint32 GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1321
{
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
  1322
	if (IsTunnel(tile)) {
3996
5a1d1fc1bd07 (svn r5199) Make the control flow of GetTileTrackStatus_TunnelBridge() more comprehensible
tron
parents: 3977
diff changeset
  1323
		if (GetTunnelTransportType(tile) != mode) return 0;
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
  1324
		return AxisToTrackBits(DiagDirToAxis(GetTunnelDirection(tile))) * 0x101;
3996
5a1d1fc1bd07 (svn r5199) Make the control flow of GetTileTrackStatus_TunnelBridge() more comprehensible
tron
parents: 3977
diff changeset
  1325
	} else {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1326
		if (IsBridgeRamp(tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1327
			if (GetBridgeTransportType(tile) != mode) return 0;
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
  1328
			return AxisToTrackBits(DiagDirToAxis(GetBridgeRampDirection(tile))) * 0x101;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1329
		} else {
3996
5a1d1fc1bd07 (svn r5199) Make the control flow of GetTileTrackStatus_TunnelBridge() more comprehensible
tron
parents: 3977
diff changeset
  1330
			uint32 result = 0;
5a1d1fc1bd07 (svn r5199) Make the control flow of GetTileTrackStatus_TunnelBridge() more comprehensible
tron
parents: 3977
diff changeset
  1331
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1332
			if (GetBridgeTransportType(tile) == mode) {
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
  1333
				result = AxisToTrackBits(GetBridgeAxis(tile)) * 0x101;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1334
			}
3996
5a1d1fc1bd07 (svn r5199) Make the control flow of GetTileTrackStatus_TunnelBridge() more comprehensible
tron
parents: 3977
diff changeset
  1335
			if ((IsTransportUnderBridge(tile) && mode == GetTransportTypeUnderBridge(tile)) ||
5057
f266cc90b396 (svn r7110) -Fix: Do not let ships enter partial water tiles under bridges; they will travel up land...
peter1138
parents: 5009
diff changeset
  1336
					(IsWaterUnderBridge(tile)     && mode == TRANSPORT_WATER && GetTileSlope(tile, NULL) == SLOPE_FLAT)) {
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4156
diff changeset
  1337
				result |= AxisToTrackBits(OtherAxis(GetBridgeAxis(tile))) * 0x101;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1338
			}
3996
5a1d1fc1bd07 (svn r5199) Make the control flow of GetTileTrackStatus_TunnelBridge() more comprehensible
tron
parents: 3977
diff changeset
  1339
			return result;
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1340
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1341
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1342
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1343
2436
177cb6a8339f (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2422
diff changeset
  1344
static void ChangeTileOwner_TunnelBridge(TileIndex tile, PlayerID old_player, PlayerID new_player)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1345
{
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
  1346
	if (!IsTileOwner(tile, old_player)) return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  1347
4848
56549aa3e234 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4656
diff changeset
  1348
	if (new_player != PLAYER_SPECTATOR) {
1902
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1901
diff changeset
  1349
		SetTileOwner(tile, new_player);
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4344
diff changeset
  1350
	} else {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1351
		if (IsBridge(tile) && IsBridgeMiddle(tile) && IsTransportUnderBridge(tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1352
			// the stuff BELOW the middle part is owned by the deleted player.
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1353
			if (GetTransportTypeUnderBridge(tile) == TRANSPORT_RAIL) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1354
				SetClearUnderBridge(tile);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1355
			} else {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1356
				SetTileOwner(tile, OWNER_NONE);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1357
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1358
		} else {
5504
520481b11c2c (svn r9968) [0.5] -Backport from trunk (r9966):
rubidium
parents: 5494
diff changeset
  1359
			if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR))) {
520481b11c2c (svn r9968) [0.5] -Backport from trunk (r9966):
rubidium
parents: 5494
diff changeset
  1360
				/* When clearing the bridge/tunnel failed there are still vehicles on/in
520481b11c2c (svn r9968) [0.5] -Backport from trunk (r9966):
rubidium
parents: 5494
diff changeset
  1361
				* the bridge/tunnel. As all *our* vehicles are already removed, they
520481b11c2c (svn r9968) [0.5] -Backport from trunk (r9966):
rubidium
parents: 5494
diff changeset
  1362
				* must be of another owner. Therefor this must be a road bridge/tunnel.
520481b11c2c (svn r9968) [0.5] -Backport from trunk (r9966):
rubidium
parents: 5494
diff changeset
  1363
				* In that case we can safely reassign the ownership to OWNER_NONE. */
520481b11c2c (svn r9968) [0.5] -Backport from trunk (r9966):
rubidium
parents: 5494
diff changeset
  1364
				SetTileOwner(tile, OWNER_NONE);
520481b11c2c (svn r9968) [0.5] -Backport from trunk (r9966):
rubidium
parents: 5494
diff changeset
  1365
			}
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1366
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1367
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1368
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1369
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1370
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
  1371
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
  1372
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
  1373
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
  1374
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
  1375
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1376
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
  1377
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
  1378
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
  1379
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
  1380
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
  1381
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
  1382
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
  1383
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1930
diff changeset
  1384
static uint32 VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
{
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
  1386
	if (IsTunnel(tile)) {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1387
		int z = GetSlopeZ(x, y) - v->z_pos;
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2958
diff changeset
  1388
		byte fc;
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
  1389
		DiagDirection dir;
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
  1390
		DiagDirection vdir;
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2958
diff changeset
  1391
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1392
		if (myabs(z) > 2) return 8;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1393
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1394
		if (v->type == VEH_Train) {
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2934
diff changeset
  1395
			fc = (x & 0xF) + (y << 4);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  1396
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
  1397
			dir = GetTunnelDirection(tile);
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
  1398
			vdir = DirToDiagDir(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1399
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1400
			if (v->u.rail.track != 0x40 && dir == vdir) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2663
diff changeset
  1401
				if (IsFrontEngine(v) && fc == _tunnel_fractcoord_1[dir]) {
5459
74356913b47d (svn r9341) [0.5] -Backport from trunk (r8943, r8955, r8976, r8999, r9009):
rubidium
parents: 5458
diff changeset
  1402
					if (!PlayVehicleSound(v, VSE_TUNNEL) && RailVehInfo(v->engine_type)->engclass == 0) {
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 534
diff changeset
  1403
						SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4559
diff changeset
  1404
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1405
					return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1406
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1407
				if (fc == _tunnel_fractcoord_2[dir]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1408
					v->tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1409
					v->u.rail.track = 0x40;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1410
					v->vehstatus |= VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1411
					return 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1412
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1413
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1414
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
  1415
			if (dir == ReverseDiagDir(vdir) && fc == _tunnel_fractcoord_3[dir] && z == 0) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
  1416
				/* We're at the tunnel exit ?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1417
				v->tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1418
				v->u.rail.track = _exit_tunnel_track[dir];
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1209
diff changeset
  1419
				assert(v->u.rail.track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1420
				v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1421
				return 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1422
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1423
		} else if (v->type == VEH_Road) {
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2934
diff changeset
  1424
			fc = (x & 0xF) + (y << 4);
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
  1425
			dir = GetTunnelDirection(tile);
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
  1426
			vdir = DirToDiagDir(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1427
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1428
			// Enter tunnel?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1429
			if (v->u.road.state != 0xFF && dir == vdir) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1430
				if (fc == _tunnel_fractcoord_4[dir] ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1431
						fc == _tunnel_fractcoord_5[dir]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1432
					v->tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1433
					v->u.road.state = 0xFF;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  1434
					v->vehstatus |= VS_HIDDEN;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1435
					return 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1436
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1437
					return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1438
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1439
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1440
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
  1441
			if (dir == ReverseDiagDir(vdir) && (
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2934
diff changeset
  1442
						/* We're at the tunnel exit ?? */
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2934
diff changeset
  1443
						fc == _tunnel_fractcoord_6[dir] ||
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2934
diff changeset
  1444
						fc == _tunnel_fractcoord_7[dir]
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2934
diff changeset
  1445
					) &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1446
					z == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1447
				v->tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1448
				v->u.road.state = _road_exit_tunnel_state[dir];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1449
				v->u.road.frame = _road_exit_tunnel_frame[dir];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1450
				v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1451
				return 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1452
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1453
		}
3234
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3233
diff changeset
  1454
	} else if (IsBridge(tile)) { // XXX is this necessary?
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2663
diff changeset
  1455
		if (v->type == VEH_Road || (v->type == VEH_Train && IsFrontEngine(v))) {
5458
0a52d7d0e410 (svn r9128) [0.5] -Fix: trains slow down under bridges when they were going down on the tile before the bridge tile.
rubidium
parents: 5439
diff changeset
  1456
			/* The train calls this (EnterTile) function before entering the tile.
0a52d7d0e410 (svn r9128) [0.5] -Fix: trains slow down under bridges when they were going down on the tile before the bridge tile.
rubidium
parents: 5439
diff changeset
  1457
			 * This has a drawback that the position of the train has not yet
0a52d7d0e410 (svn r9128) [0.5] -Fix: trains slow down under bridges when they were going down on the tile before the bridge tile.
rubidium
parents: 5439
diff changeset
  1458
			 * changed. So, when a train comes down a slope it is above the tile
0a52d7d0e410 (svn r9128) [0.5] -Fix: trains slow down under bridges when they were going down on the tile before the bridge tile.
rubidium
parents: 5439
diff changeset
  1459
			 * height of the current slope. Catch this situation by adding a
0a52d7d0e410 (svn r9128) [0.5] -Fix: trains slow down under bridges when they were going down on the tile before the bridge tile.
rubidium
parents: 5439
diff changeset
  1460
			 * small correction when trying to determine whether the train is on
0a52d7d0e410 (svn r9128) [0.5] -Fix: trains slow down under bridges when they were going down on the tile before the bridge tile.
rubidium
parents: 5439
diff changeset
  1461
			 * the bridge or under the bridge */
0a52d7d0e410 (svn r9128) [0.5] -Fix: trains slow down under bridges when they were going down on the tile before the bridge tile.
rubidium
parents: 5439
diff changeset
  1462
			if (IsBridgeRamp(tile) || v->z_pos > (GetTileMaxZ(tile) + 1)) {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1463
				/* modify speed of vehicle */
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1464
				uint16 spd = _bridge[GetBridgeType(tile)].speed;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1465
				if (v->type == VEH_Road) spd *= 2;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1466
				if (v->cur_speed > spd) v->cur_speed = spd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1467
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1468
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1469
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1470
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1471
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1472
4291
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1473
/** Retrieve the exit-tile of the vehicle from inside a tunnel
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1474
 * Very similar to GetOtherTunnelEnd(), but we use the vehicle's
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1475
 * direction for determining which end of the tunnel to find
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1476
 * @param v the vehicle which is inside the tunnel and needs an exit
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1477
 * @return the exit-tile of the tunnel based on the vehicle's direction */
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1478
TileIndex GetVehicleOutOfTunnelTile(const Vehicle *v)
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1479
{
4304
5e26af218efd (svn r5951) - Codechange: GetVehicleOutOfTunnelTile() never did anything, and making it work properly only resulted in massive performance drops, and it not working properly also seemed to work, so make it not work properly once again.
Darkvater
parents: 4293
diff changeset
  1480
#if 1
5e26af218efd (svn r5951) - Codechange: GetVehicleOutOfTunnelTile() never did anything, and making it work properly only resulted in massive performance drops, and it not working properly also seemed to work, so make it not work properly once again.
Darkvater
parents: 4293
diff changeset
  1481
	return v->tile;
5e26af218efd (svn r5951) - Codechange: GetVehicleOutOfTunnelTile() never did anything, and making it work properly only resulted in massive performance drops, and it not working properly also seemed to work, so make it not work properly once again.
Darkvater
parents: 4293
diff changeset
  1482
#else
4291
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1483
	TileIndex tile = v->tile;
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1484
	DiagDirection dir = DirToDiagDir(v->direction);
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4549
diff changeset
  1485
	TileIndexDiff delta = TileOffsByDiagDir(dir);
4291
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1486
	byte z = v->z_pos;
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1487
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1488
	dir = ReverseDiagDir(dir);
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1489
	while (
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1490
		!IsTunnelTile(tile) ||
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1491
		GetTunnelDirection(tile) != dir ||
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1492
		GetTileZ(tile) != z
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1493
	) {
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1494
		tile += delta;
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1495
	}
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1496
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1497
	return tile;
4304
5e26af218efd (svn r5951) - Codechange: GetVehicleOutOfTunnelTile() never did anything, and making it work properly only resulted in massive performance drops, and it not working properly also seemed to work, so make it not work properly once again.
Darkvater
parents: 4293
diff changeset
  1498
#endif
4291
c0e769957f8b (svn r5924) Revert accidently commited files
tron
parents: 4290
diff changeset
  1499
}
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3938
diff changeset
  1500
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1501
const TileTypeProcs _tile_type_tunnelbridge_procs = {
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
  1502
	DrawTile_TunnelBridge,           /* draw_tile_proc */
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
  1503
	GetSlopeZ_TunnelBridge,          /* get_slope_z_proc */
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
  1504
	ClearTile_TunnelBridge,          /* clear_tile_proc */
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
  1505
	GetAcceptedCargo_TunnelBridge,   /* get_accepted_cargo_proc */
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
  1506
	GetTileDesc_TunnelBridge,        /* get_tile_desc_proc */
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
  1507
	GetTileTrackStatus_TunnelBridge, /* get_tile_track_status_proc */
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
  1508
	ClickTile_TunnelBridge,          /* click_tile_proc */
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
  1509
	AnimateTile_TunnelBridge,        /* animate_tile_proc */
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
  1510
	TileLoop_TunnelBridge,           /* tile_loop_clear */
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
  1511
	ChangeTileOwner_TunnelBridge,    /* change_tile_owner_clear */
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
  1512
	NULL,                            /* get_produced_cargo_proc */
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
  1513
	VehicleEnter_TunnelBridge,       /* vehicle_enter_tile_proc */
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
  1514
	GetSlopeTileh_TunnelBridge,      /* get_slope_tileh_proc */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1515
};