rail_cmd.c
author tron
Sat, 25 Jun 2005 06:15:43 +0000
changeset 1980 9ea0c89fbb58
parent 1977 4392ae3d8e31
child 1981 de031d2aed47
permissions -rw-r--r--
(svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     1
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1818
diff changeset
     2
#include "openttd.h"
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
     3
#include "debug.h"
1363
01d3de5d8039 (svn r1867) Include tables/sprites.h only in files which need it
tron
parents: 1330
diff changeset
     4
#include "table/sprites.h"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 497
diff changeset
     5
#include "table/strings.h"
1009
a7ccb8e39cbc (svn r1508) Remove duplicate declarations and include proper headers where necessary
tron
parents: 926
diff changeset
     6
#include "gfx.h"
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 555
diff changeset
     7
#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: 1123
diff changeset
     8
#include "tile.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     9
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "viewport.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "pathfind.h"
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
    13
#include "engine.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "town.h"
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 283
diff changeset
    15
#include "sound.h"
389
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
    16
#include "station.h"
405
6830ae7a0d5d (svn r602) -newgrf: Move DrawTileSeqStruct & co and struct SpriteGroup to sprite.h (pasky)
darkvater
parents: 403
diff changeset
    17
#include "sprite.h"
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1247
diff changeset
    18
#include "depot.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1536
diff changeset
    19
#include "waypoint.h"
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
    20
#include "rail.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
415
423de36034b8 (svn r612) [newgrf] Fix: custom waypoints on monorail/maglev are displayed correctly
dominik
parents: 413
diff changeset
    22
extern uint16 _custom_sprites_base;
423de36034b8 (svn r612) [newgrf] Fix: custom waypoints on monorail/maglev are displayed correctly
dominik
parents: 413
diff changeset
    23
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
    24
void ShowTrainDepotWindow(TileIndex tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
/* Format of rail map5 byte.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
 * 00 abcdef  => Normal rail
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
 * 01 abcdef  => Rail with signals
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    29
 * 10 ??????  => Unused
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
 * 11 ????dd  => Depot
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
    31
 *
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
    32
 * abcdef is a bitmask, which contains ones for all present tracks. Below the
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
    33
 * value for each track is given.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
/*         4
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
 *     ---------
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
 *    |\       /|
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
 *    | \    1/ |
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
 *    |  \   /  |
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
 *    |   \ /   |
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
 *  16|    \    |32
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
    43
 *    |   / \2  |
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
 *    |  /   \  |
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
 *    | /     \ |
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
 *    |/       \|
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
 *     ---------
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
 *         8
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
// Constants for lower part of Map2 byte.
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
    53
enum RailMap2Lower4 {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
	RAIL_MAP2LO_GROUND_MASK = 0xF,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
	RAIL_GROUND_BROWN = 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
	RAIL_GROUND_GREEN = 1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
	RAIL_GROUND_FENCE_NW = 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
	RAIL_GROUND_FENCE_SE = 3,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
	RAIL_GROUND_FENCE_SENW = 4,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
	RAIL_GROUND_FENCE_NE = 5,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
	RAIL_GROUND_FENCE_SW = 6,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
	RAIL_GROUND_FENCE_NESW = 7,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
	RAIL_GROUND_FENCE_VERT1 = 8,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
	RAIL_GROUND_FENCE_VERT2 = 9,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
	RAIL_GROUND_FENCE_HORIZ1 = 10,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
	RAIL_GROUND_FENCE_HORIZ2 = 11,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
	RAIL_GROUND_ICE_DESERT = 12,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
    71
/* MAP2 byte:    abcd???? => Signal On? Same coding as map3lo
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
    72
 * MAP3LO byte:  abcd???? => Signal Exists?
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
    73
 *				 a and b are for diagonals, upper and left,
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
    74
 *				 one for each direction. (ie a == NE->SW, b ==
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
    75
 *				 SW->NE, or v.v., I don't know. b and c are
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
    76
 *				 similar for lower and right.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
 * MAP2 byte:    ????abcd => Type of ground.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
 * MAP3LO byte:  ????abcd => Type of rail.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
 * MAP5:         00abcdef => rail
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
 *               01abcdef => rail w/ signals
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
 *               10uuuuuu => unused
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
    82
 *               11uuuudd => rail depot
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
    85
static bool CheckTrackCombination(TileIndex tile, TrackBits to_build, uint flags)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
{
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
    87
	RailTileType type = GetRailTileType(tile);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
    88
	TrackBits current; /* The current track layout */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
    89
	TrackBits future; /* The track layout we want to build */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
	_error_message = STR_1001_IMPOSSIBLE_TRACK_COMBINATION;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
    92
	if (type != RAIL_TYPE_NORMAL && type != RAIL_TYPE_SIGNALS)
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
    93
		return false; /* Cannot build anything on depots and checkpoints */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
    95
	/* So, we have a tile with tracks on it (and possibly signals). Let's see
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
    96
	 * what tracks first */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
    97
	current = GetTrackBits(tile);
1946
68e7ee03016a (svn r2452) Fix defect in r2448 which caused building tracks unexpectedly fail or succeed
tron
parents: 1942
diff changeset
    98
	future = current | to_build;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
    99
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   100
	/* Are we really building something new? */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   101
	if (current == future) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   102
		/* Nothing new is being built */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   103
		_error_message = STR_1007_ALREADY_BUILT;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   104
		return false;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   105
	}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   106
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   107
	/* Let's see if we may build this */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   108
	if ((flags & DC_NO_RAIL_OVERLAP) || type == RAIL_TYPE_SIGNALS) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   109
		/* If we are not allowed to overlap (flag is on for ai players or we have
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   110
		 * signals on the tile), check that */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   111
		return
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   112
			future == (TRACK_BIT_UPPER | TRACK_BIT_LOWER) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   113
			future == (TRACK_BIT_LEFT  | TRACK_BIT_RIGHT);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   114
	} else {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   115
		/* Normally, we may overlap and any combination is valid */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   116
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
13
3e7c1f74a996 (svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents: 0
diff changeset
   121
static const byte _valid_tileh_slopes[4][15] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
// set of normal ones
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
{
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   125
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   126
	TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   127
	TRACK_BIT_UPPER,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   128
	TRACK_BIT_DIAG1,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   130
	TRACK_BIT_LEFT,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
	0,
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   132
	TRACK_BIT_DIAG2,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   133
	TRACK_BIT_LOWER,
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   134
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   135
	TRACK_BIT_LOWER,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   136
	TRACK_BIT_DIAG2,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
	0,
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   138
	TRACK_BIT_LEFT,
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   139
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   140
	TRACK_BIT_DIAG1,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   141
	TRACK_BIT_UPPER,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   142
	TRACK_BIT_RIGHT,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
// allowed rail for an evenly raised platform
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   146
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
	0,
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   148
	TRACK_BIT_LEFT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   149
	TRACK_BIT_LOWER,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   150
	TRACK_BIT_DIAG2 | TRACK_BIT_LOWER | TRACK_BIT_LEFT,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   152
	TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   153
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   154
	TRACK_BIT_DIAG1 | TRACK_BIT_LOWER | TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   155
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   157
	TRACK_BIT_UPPER,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   158
	TRACK_BIT_DIAG1 | TRACK_BIT_UPPER | TRACK_BIT_LEFT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   159
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   160
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   162
	TRACK_BIT_DIAG2 | TRACK_BIT_UPPER | TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   163
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   164
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
// allowed rail on coast tile
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
	0,
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   170
	TRACK_BIT_LEFT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   171
	TRACK_BIT_LOWER,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   172
	TRACK_BIT_DIAG2|TRACK_BIT_LEFT|TRACK_BIT_LOWER,
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   173
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   174
	TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   175
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   176
	TRACK_BIT_DIAG1|TRACK_BIT_RIGHT|TRACK_BIT_LOWER,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   177
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   179
	TRACK_BIT_UPPER,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   180
	TRACK_BIT_DIAG1|TRACK_BIT_LEFT|TRACK_BIT_UPPER,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   181
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   182
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   184
	TRACK_BIT_DIAG2|TRACK_BIT_RIGHT|TRACK_BIT_UPPER,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   185
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   186
	TRACK_BIT_DIAG1|TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LOWER|TRACK_BIT_LEFT|TRACK_BIT_RIGHT,
13
3e7c1f74a996 (svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents: 0
diff changeset
   187
	},
3e7c1f74a996 (svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents: 0
diff changeset
   188
3e7c1f74a996 (svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents: 0
diff changeset
   189
	// valid railway crossings on slopes
3e7c1f74a996 (svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents: 0
diff changeset
   190
	{
3e7c1f74a996 (svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents: 0
diff changeset
   191
		1, 0, 0, // 0, 1, 2
3e7c1f74a996 (svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents: 0
diff changeset
   192
		0, 0, 1, // 3, 4, 5
3e7c1f74a996 (svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents: 0
diff changeset
   193
		0, 1, 0, // 6, 7, 8
3e7c1f74a996 (svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents: 0
diff changeset
   194
		0, 1, 1, // 9, 10, 11
3e7c1f74a996 (svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents: 0
diff changeset
   195
		0, 1, 1, // 12, 13, 14
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents: 59
diff changeset
   199
uint GetRailFoundation(uint tileh, uint bits)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
	if ((~_valid_tileh_slopes[0][tileh] & bits) == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
	if ((~_valid_tileh_slopes[1][tileh] & bits) == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
		return tileh;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   209
	if ( ((i=0, tileh == 1) || (i+=2, tileh == 2) || (i+=2, tileh == 4) || (i+=2, tileh == 8)) && (bits == TRACK_BIT_DIAG1 || (i++, bits == TRACK_BIT_DIAG2)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
		return i + 15;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   212
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
//
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   216
static uint32 CheckRailSlope(uint tileh, TrackBits rail_bits, TrackBits existing, TileIndex tile)
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
	// never allow building on top of steep tiles
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
	if (!(tileh & 0x10)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
		rail_bits |= existing;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
		// don't allow building on the lower side of a coast
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   223
		if (IsTileType(tile, MP_WATER) &&
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   224
				~_valid_tileh_slopes[2][tileh] & rail_bits) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
			return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   228
		// no special foundation
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
		if ((~_valid_tileh_slopes[0][tileh] & rail_bits) == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   230
			return 0;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   231
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   232
		if ((~_valid_tileh_slopes[1][tileh] & rail_bits) == 0 || ( // whole tile is leveled up
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   233
					(rail_bits == TRACK_BIT_DIAG1 || rail_bits == TRACK_BIT_DIAG2) &&
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   234
					(tileh == 1 || tileh == 2 || tileh == 4 || tileh == 8)
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   235
				)) { // partly up
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   236
			if (existing != 0) {
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   237
				return 0;
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   238
			} else if (!_patches.build_on_slopes ||
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   239
					(_is_ai_player && !_patches.ainew_active)) {
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   240
				return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   241
			} else {
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   242
				return _price.terraform;
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   243
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
	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
   247
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
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: 1719
diff changeset
   249
/* Validate functions for rail building */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   250
static inline bool ValParamTrackOrientation(Track track) {return IsValidTrack(track);}
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: 1719
diff changeset
   251
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: 1719
diff changeset
   252
/** Build a single piece of rail
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: 1719
diff changeset
   253
 * @param x,y coordinates on where to build
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: 1719
diff changeset
   254
 * @param p1 railtype of being built piece (normal, mono, maglev)
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   255
 * @param p2 rail track to build
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: 1719
diff changeset
   256
 */
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: 1719
diff changeset
   257
int32 CmdBuildSingleRail(int x, int y, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
{
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   259
	TileIndex tile;
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   260
	uint tileh;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   261
	uint m5; /* XXX: Used only as a cache, should probably be removed? */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   262
	Track track = (Track)p2;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   263
	TrackBits trackbit;
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   264
	int32 cost = 0;
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   265
	int32 ret;
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   266
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   267
	if (!ValParamRailtype(p1) || !ValParamTrackOrientation(track)) return CMD_ERROR;
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   268
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   269
	tile = TileVirtXY(x, y);
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   270
	tileh = GetTileSlope(tile, NULL);
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   271
	m5 = _map5[tile];
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   272
	trackbit = TrackToTrackBits(track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   275
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   276
	switch (GetTileType(tile)) {
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   277
		case MP_TUNNELBRIDGE:
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   278
			if ((m5 & 0xC0) != 0xC0 || // not bridge middle part?
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   279
					(m5 & 0x01 ? 1 : 2) != trackbit) { // wrong direction?
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   280
				// Get detailed error message
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   281
				return DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   282
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
1630
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   284
			switch (m5 & 0x38) { // what's under the bridge?
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   285
				case 0x00: // clear land
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   286
					ret = CheckRailSlope(tileh, trackbit, 0, tile);
1691
fcd9fefaed02 (svn r2195) Add CmdFailed() as the One True Way(tm) to check if a command failed.
tron
parents: 1669
diff changeset
   287
					if (CmdFailed(ret)) return ret;
1630
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   288
					cost += ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
1630
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   290
					if (flags & DC_EXEC) {
1902
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1901
diff changeset
   291
						SetTileOwner(tile, _current_player);
1630
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   292
						_map3_lo[tile] &= ~0x0F;
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: 1719
diff changeset
   293
						_map3_lo[tile] |= p1;
1630
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   294
						_map5[tile] = (m5 & 0xC7) | 0x20; // railroad under bridge
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   295
					}
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   296
					break;
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   297
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   298
				case 0x20: // rail already there
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   299
					return_cmd_error(STR_1007_ALREADY_BUILT);
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   300
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   301
				default:
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   302
					// Get detailed error message
15a88bda48fd (svn r2134) Return the correct error message if rail is already under a bridge, fix for r2131
tron
parents: 1628
diff changeset
   303
					return DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   304
			}
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   305
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   307
		case MP_RAILWAY:
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   308
			if (!CheckTrackCombination(tile, trackbit, flags) ||
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   309
					!EnsureNoVehicle(tile)) {
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   310
				return CMD_ERROR;
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   311
			}
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   312
			if (m5 & RAIL_TYPE_SPECIAL ||
1903
725cdd1ec467 (svn r2409) Missed one _map_owner -> IsTileOwner()
tron
parents: 1902
diff changeset
   313
					!IsTileOwner(tile, _current_player) ||
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: 1719
diff changeset
   314
					(_map3_lo[tile] & 0xFU) != p1) {
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   315
				// Get detailed error message
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   316
				return DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   317
			}
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   318
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   319
			ret = CheckRailSlope(tileh, trackbit, GetTrackBits(tile), tile);
1691
fcd9fefaed02 (svn r2195) Add CmdFailed() as the One True Way(tm) to check if a command failed.
tron
parents: 1669
diff changeset
   320
			if (CmdFailed(ret)) return ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
			cost += ret;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
1719
b428568cec03 (svn r2223) When adding tracks to a railway tile reset the ground to bare land, fix for a glitch in r2131
tron
parents: 1691
diff changeset
   323
			if (flags & DC_EXEC) {
b428568cec03 (svn r2223) When adding tracks to a railway tile reset the ground to bare land, fix for a glitch in r2131
tron
parents: 1691
diff changeset
   324
				_map2[tile] &= ~RAIL_MAP2LO_GROUND_MASK; // Bare land
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   325
				_map5[tile] = m5 | trackbit;
1719
b428568cec03 (svn r2223) When adding tracks to a railway tile reset the ground to bare land, fix for a glitch in r2131
tron
parents: 1691
diff changeset
   326
			}
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   327
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   329
		case MP_STREET:
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   330
			if (!_valid_tileh_slopes[3][tileh]) // prevent certain slopes
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   331
				return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   332
			if (!EnsureNoVehicle(tile)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   334
			if ((m5 & 0xF0) == 0 && ( // normal road?
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   335
						(track == TRACK_DIAG1 && m5 == 0x05) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   336
						(track == TRACK_DIAG2 && m5 == 0x0A) // correct direction?
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   337
					)) {
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   338
				if (flags & DC_EXEC) {
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
   339
					_map3_lo[tile] = GetTileOwner(tile);
1902
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1901
diff changeset
   340
					SetTileOwner(tile, _current_player);
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: 1719
diff changeset
   341
					_map3_hi[tile] = p1;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   342
					_map5[tile] = 0x10 | (track == TRACK_DIAG1 ? 0x08 : 0x00); // level crossing
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   343
				}
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   344
				break;
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   345
			}
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   346
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   347
			if (IsLevelCrossing(tile) && (m5 & 0x08 ? TRACK_DIAG1 : TRACK_DIAG2) == track)
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   348
				return_cmd_error(STR_1007_ALREADY_BUILT);
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   349
			/* FALLTHROUGH */
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   350
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   351
		default:
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   352
			ret = CheckRailSlope(tileh, trackbit, 0, tile);
1691
fcd9fefaed02 (svn r2195) Add CmdFailed() as the One True Way(tm) to check if a command failed.
tron
parents: 1669
diff changeset
   353
			if (CmdFailed(ret)) return ret;
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   354
			cost += ret;
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   355
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   356
			ret = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
1691
fcd9fefaed02 (svn r2195) Add CmdFailed() as the One True Way(tm) to check if a command failed.
tron
parents: 1669
diff changeset
   357
			if (CmdFailed(ret)) return ret;
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   358
			cost += ret;
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   359
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   360
			if (flags & DC_EXEC) {
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   361
				SetTileType(tile, MP_RAILWAY);
1902
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1901
diff changeset
   362
				SetTileOwner(tile, _current_player);
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   363
				_map2[tile] = 0; // Bare land
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: 1719
diff changeset
   364
				_map3_lo[tile] = p1; // No signals, rail type
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   365
				_map5[tile] = trackbit;
1627
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   366
			}
d88508d9ced0 (svn r2131) -Fix: Rewrite CmdBuildSingleRail(), this addresses several issues:
tron
parents: 1609
diff changeset
   367
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
		MarkTileDirtyByTile(tile);
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   372
		SetSignalsOnBothDir(tile, track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
	return cost + _price.build_rail;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   378
static const byte _signals_table[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
	0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0, // direction 1
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
	0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0  // direction 2
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   383
static const byte _signals_table_other[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
	0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0, // direction 1
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
	0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0  // direction 2
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
static const byte _signals_table_both[] = {
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   389
	0xC0, 0xC0, 0xC0, 0x30, 0xC0, 0x30, 0, 0,	// both directions combined
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
	0xC0, 0xC0, 0xC0, 0x30, 0xC0, 0x30, 0, 0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   391
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
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: 1719
diff changeset
   394
/** Remove a single piece of track
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: 1719
diff changeset
   395
 * @param x,y coordinates for removal of track
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: 1719
diff changeset
   396
 * @param p1 unused
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: 1719
diff changeset
   397
 * @param p2 rail orientation
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
int32 CmdRemoveSingleRail(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
{
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   401
	Track track = (Track)p2;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   402
	TrackBits trackbit;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   403
	uint tileh;
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: 1719
diff changeset
   404
	TileIndex tile;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   405
	byte m5;
1963
8a398139fb87 (svn r2469) - Add: When removing tracks with the 'remove' tool, have it automatically remove signals on the tracks.
hackykid
parents: 1954
diff changeset
   406
	int32 cost = _price.remove_rail;
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: 1719
diff changeset
   407
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: 1719
diff changeset
   408
	if (!ValParamTrackOrientation(p2)) return CMD_ERROR;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   409
	trackbit = TrackToTrackBits(track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   413
	tile = TileVirtXY(x, y);
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   414
	tileh = GetTileSlope(tile, NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   416
	if (!IsTileType(tile, MP_TUNNELBRIDGE) && !IsTileType(tile, MP_STREET) && !IsTileType(tile, MP_RAILWAY))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
	if (_current_player != OWNER_WATER && !CheckTileOwnership(tile))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
	// allow building rail under bridge
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   423
	if (!IsTileType(tile, MP_TUNNELBRIDGE) && !EnsureNoVehicle(tile))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   426
	switch(GetTileType(tile))
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   427
	{
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   428
		case MP_TUNNELBRIDGE:
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   429
			if (!EnsureNoVehicleZ(tile, TilePixelHeight(tile)))
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   430
				return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   432
			if ((_map5[tile] & 0xF8) != 0xE0)
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   433
				return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   435
			if ( ((_map5[tile] & 1) ? 1 : 2) != trackbit )
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   436
				return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   438
			if (!(flags & DC_EXEC))
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   439
				return _price.remove_rail;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   440
1902
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1901
diff changeset
   441
		SetTileOwner(tile, OWNER_NONE);
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   442
			_map5[tile] = _map5[tile] & 0xC7;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   443
			break;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   444
		case MP_STREET:
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   445
			if (!(_map5[tile] & 0xF0))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
				return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   448
			if (_map5[tile] & 0xE0)
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   449
				return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   451
			/* This is a crossing, let's check if the direction is correct */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   452
			if (_map5[tile] & 8) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   453
				m5 = 5;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   454
				if (track != TRACK_DIAG1)
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   455
					return CMD_ERROR;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   456
			} else {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   457
				m5 = 10;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   458
				if (track != TRACK_DIAG2)
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   459
					return CMD_ERROR;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   460
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   462
			if (!(flags & DC_EXEC))
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   463
				return _price.remove_rail;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   465
			_map5[tile] = m5;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   466
			SetTileOwner(tile, _map3_lo[tile]);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   467
			_map2[tile] = 0;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   468
			break;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   469
		case MP_RAILWAY:
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   470
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   471
			if (!IsPlainRailTile(tile))
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   472
				return CMD_ERROR;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   473
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   474
			/* See if the track to remove is actually there */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   475
			if (!(GetTrackBits(tile) & trackbit))
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   476
				return CMD_ERROR;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   477
1963
8a398139fb87 (svn r2469) - Add: When removing tracks with the 'remove' tool, have it automatically remove signals on the tracks.
hackykid
parents: 1954
diff changeset
   478
			/* Charge extra to remove signals on the track, if they are there */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   479
			if (HasSignalOnTrack(tile, track))
1963
8a398139fb87 (svn r2469) - Add: When removing tracks with the 'remove' tool, have it automatically remove signals on the tracks.
hackykid
parents: 1954
diff changeset
   480
				cost += DoCommand(x, y, track, 0, flags, CMD_REMOVE_SIGNALS);
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   481
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   482
			if (!(flags & DC_EXEC))
1963
8a398139fb87 (svn r2469) - Add: When removing tracks with the 'remove' tool, have it automatically remove signals on the tracks.
hackykid
parents: 1954
diff changeset
   483
				return cost;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   484
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   485
			/* We remove the trackbit here. */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   486
			_map5[tile] &= ~trackbit;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   487
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   488
			if (GetTrackBits(tile)  == 0) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   489
				/* The tile has no tracks left, it is no longer a rail tile */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   490
				DoClearSquare(tile);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   491
				/* XXX: This is an optimisation, right? Is it really worth the ugly goto? */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   492
				goto skip_mark_dirty;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   493
			}
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   494
			break;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   495
		default:
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   496
			assert(0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   497
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
	/* mark_dirty */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
	MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   502
skip_mark_dirty:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   504
	SetSignalsOnBothDir(tile, track);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   505
1963
8a398139fb87 (svn r2469) - Add: When removing tracks with the 'remove' tool, have it automatically remove signals on the tracks.
hackykid
parents: 1954
diff changeset
   506
	return cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
static const struct {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   510
	int8 xinc[16];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
	int8 yinc[16];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   512
} _railbit = {{
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   513
//  0   1   2   3   4   5
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   514
	-16,  0,-16,  0, 16,  0,    0,  0,
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   515
	 16,  0,  0, 16,  0,-16,    0,  0,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   516
},{
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   517
	  0, 16,  0, 16,  0, 16,    0,  0,
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   518
	  0,-16,-16,  0,-16,  0,    0,  0,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
}};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   520
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   521
static int32 ValidateAutoDrag(Trackdir *trackdir, int x, int y, int ex, int ey)
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   522
{
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   523
	int dx, dy, trdx, trdy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   525
	if (!ValParamTrackOrientation(*trackdir)) return CMD_ERROR;
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   526
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   527
	// calculate delta x,y from start to end tile
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   528
	dx = ex - x;
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   529
	dy = ey - y;
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   530
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   531
	// calculate delta x,y for the first direction
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   532
	trdx = _railbit.xinc[*trackdir];
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   533
	trdy = _railbit.yinc[*trackdir];
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   534
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   535
	if (!IsDiagonalTrackdir(*trackdir)) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   536
		trdx += _railbit.xinc[*trackdir ^ 1];
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   537
		trdy += _railbit.yinc[*trackdir ^ 1];
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   538
	}
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   539
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   540
	// validate the direction
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   541
	while (((trdx <= 0) && (dx > 0)) || ((trdx >= 0) && (dx < 0)) ||
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   542
	       ((trdy <= 0) && (dy > 0)) || ((trdy >= 0) && (dy < 0))) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   543
		if (!HASBIT(*trackdir, 3)) { // first direction is invalid, try the other
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   544
			SETBIT(*trackdir, 3); // reverse the direction
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   545
			trdx = -trdx;
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   546
			trdy = -trdy;
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   547
		} else // other direction is invalid too, invalid drag
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   548
			return CMD_ERROR;
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   549
	}
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   550
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   551
	// (for diagonal tracks, this is already made sure of by above test), but:
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   552
	// for non-diagonal tracks, check if the start and end tile are on 1 line
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   553
	if (!IsDiagonalTrackdir(*trackdir)) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   554
		trdx = _railbit.xinc[*trackdir];
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   555
		trdy = _railbit.yinc[*trackdir];
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   556
		if ((abs(dx) != abs(dy)) && (abs(dx) + abs(trdy) != abs(dy) + abs(trdx)))
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   557
			return CMD_ERROR;
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   558
	}
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   559
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   560
	return 0;
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   561
}
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   562
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: 1719
diff changeset
   563
/** Build a stretch of railroad tracks.
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: 1719
diff changeset
   564
 * @param x,y start tile of drag
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: 1719
diff changeset
   565
 * @param p1 end tile of drag
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: 1719
diff changeset
   566
 * @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: 1719
diff changeset
   567
 * - p2 = (bit 0-3) - railroad type normal/maglev (0 = normal, 1 = mono, 2 = maglev)
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   568
 * - p2 = (bit 4-6) - track-orientation, valid values: 0-5 (Track enum)
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: 1719
diff changeset
   569
 * - p2 = (bit 7)   - 0 = build, 1 = remove tracks
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
 */
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   571
static int32 CmdRailTrackHelper(int x, int y, uint32 flags, uint32 p1, uint32 p2)
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   572
{
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   573
	int ex, ey;
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   574
	int32 ret, total_cost = 0;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   575
	Track track = (Track)((p2 >> 4) & 7);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   576
	Trackdir trackdir;
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   577
	byte mode = HASBIT(p2, 7);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   579
	if (!ValParamRailtype(p2 & 0x3) || !ValParamTrackOrientation(track)) return CMD_ERROR;
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
   580
	if (p1 > MapSize()) return CMD_ERROR;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   581
	trackdir = TrackToTrackdir(track);
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: 1719
diff changeset
   582
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   583
	/* unpack end point */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   584
	ex = TileX(p1) * TILE_SIZE;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   585
	ey = TileY(p1) * TILE_SIZE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
889
7f8630bfea41 (svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents: 826
diff changeset
   587
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
7f8630bfea41 (svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents: 826
diff changeset
   588
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   589
	if (CmdFailed(ValidateAutoDrag(&trackdir, x, y, ex, ey))) return CMD_ERROR;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   590
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   591
	if (flags & DC_EXEC) SndPlayTileFx(SND_20_SPLAT_2, TileVirtXY(x, y));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
	for(;;) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   594
		ret = DoCommand(x, y, p2 & 0x3, TrackdirToTrack(trackdir), flags, (mode == 0) ? CMD_BUILD_SINGLE_RAIL : CMD_REMOVE_SINGLE_RAIL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
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: 1719
diff changeset
   596
		if (CmdFailed(ret)) {
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   597
			if ((_error_message != STR_1007_ALREADY_BUILT) && (mode == 0))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
		} else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
			total_cost += ret;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   602
		if (x == ex && y == ey)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   605
		x += _railbit.xinc[trackdir];
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   606
		y += _railbit.yinc[trackdir];
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   607
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   608
		// toggle railbit for the non-diagonal tracks
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   609
		if (!IsDiagonalTrackdir(trackdir)) trackdir ^= 1;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   610
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
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: 1719
diff changeset
   612
	return (total_cost == 0) ? CMD_ERROR : total_cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   615
/** Build rail on a stretch of track.
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   616
 * Stub for the unified rail builder/remover
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   617
 * @see CmdRailTrackHelper
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   618
 */
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   619
int32 CmdBuildRailroadTrack(int x, int y, uint32 flags, uint32 p1, uint32 p2)
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   620
{
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: 1719
diff changeset
   621
	return CmdRailTrackHelper(x, y, flags, p1, CLRBIT(p2, 7));
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   622
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   624
/** Build rail on a stretch of track.
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   625
 * Stub for the unified rail builder/remover
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   626
 * @see CmdRailTrackHelper
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   627
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
int32 CmdRemoveRailroadTrack(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
{
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   630
	return CmdRailTrackHelper(x, y, flags, p1, SETBIT(p2, 7));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
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: 1719
diff changeset
   633
/** Build a train depot
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: 1719
diff changeset
   634
 * @param x,y position of the train depot
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: 1719
diff changeset
   635
 * @param p1 rail type
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: 1719
diff changeset
   636
 * @param p2 depot direction (0 through 3), where 0 is NW, 1 is NE, etc.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
int32 CmdBuildTrainDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
{
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: 1719
diff changeset
   640
	Depot *d;
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   641
	TileIndex tile = TileVirtXY(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
	int32 cost, ret;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
	uint tileh;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   647
	if (!EnsureNoVehicle(tile)) 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: 1719
diff changeset
   648
	/* check railtype and valid direction for depot (0 through 3), 4 in total */
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: 1719
diff changeset
   649
	if (!ValParamRailtype(p1) || p2 > 3) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
	tileh = GetTileSlope(tile, NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
	if (tileh != 0) {
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents: 59
diff changeset
   653
		if ((!_patches.ainew_active && _is_ai_player) || !_patches.build_on_slopes || (tileh & 0x10 || !((0x4C >> p2) & tileh) ))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
			return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
	ret = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
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: 1719
diff changeset
   658
	if (CmdFailed(ret)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
	cost = ret;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
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: 1719
diff changeset
   661
	d = AllocateDepot();
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: 1719
diff changeset
   662
	if (d == NULL)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   663
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   664
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   665
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
		if (_current_player == _local_player)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   667
			_last_built_train_depot_tile = (TileIndex)tile;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   668
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   669
		ModifyTile(tile,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   670
			MP_SETTYPE(MP_RAILWAY) |
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   671
			MP_MAP3LO | MP_MAPOWNER_CURRENT | MP_MAP5,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   672
			p1, /* map3_lo */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   673
			p2 | RAIL_TYPE_DEPOT_WAYPOINT /* map5 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   674
		);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   675
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: 1719
diff changeset
   676
		d->xy = tile;
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: 1719
diff changeset
   677
		d->town_index = ClosestTownFromTile(tile, (uint)-1)->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   678
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   679
		SetSignalsOnBothDir(tile, (p2&1) ? 2 : 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   680
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   681
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   682
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   683
	return cost + _price.build_train_depot;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   684
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
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: 1719
diff changeset
   686
/** Build signals, alternate between double/single, signal/semaphore,
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: 1719
diff changeset
   687
 * pre/exit/combo-signals, and what-else not
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: 1719
diff changeset
   688
 * @param x,y coordinates where signals is being built
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: 1719
diff changeset
   689
 * @param p1 various bitstuffed elements
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   690
 * - p1 = (bit 0-2) - track-orientation, valid values: 0-5 (Track enum)
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: 1719
diff changeset
   691
 * - p1 = (bit 3)   - choose semaphores/signals or cycle normal/pre/exit/combo depending on context
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   692
 * @param p2 used for CmdBuildManySignals() to copy direction of first signal
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   693
 * TODO: p2 should be replaced by two bits for "along" and "against" the track.
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   694
 */
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   695
int32 CmdBuildSingleSignal(int x, int y, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   696
{
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   697
	TileIndex tile = TileVirtXY(x, y);
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   698
	bool semaphore;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   699
	bool pre_signal;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   700
	Track track = (Track)(p1 & 0x7);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   701
	byte m5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
	int32 cost;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   703
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   704
	// Same bit, used in different contexts
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   705
	semaphore = pre_signal = HASBIT(p1, 3);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   706
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: 1719
diff changeset
   707
	if (!ValParamTrackOrientation(track) || !IsTileType(tile, MP_RAILWAY) || !EnsureNoVehicle(tile))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   708
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   709
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: 1719
diff changeset
   710
	/* Protect against invalid signal copying */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   711
	if (p2 != 0 && (p2 & SignalOnTrack(track)) == 0) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   713
	m5 = _map5[tile];
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   714
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   715
	/* You can only build signals on plain rail tiles, and the selected track must exist */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   716
	if (!IsPlainRailTile(tile) || !HasTrack(tile, track)) return CMD_ERROR;
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   717
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   718
	if (!CheckTileOwnership(tile)) return CMD_ERROR;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   719
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   720
	_error_message = STR_1005_NO_SUITABLE_RAILROAD_TRACK;
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   721
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   722
	{
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   723
 		/* See if this is a valid track combination for signals, (ie, no overlap) */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   724
 		TrackBits trackbits = GetTrackBits(tile);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   725
		if (KILL_FIRST_BIT(trackbits) != 0 && /* More than one track present */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   726
				trackbits != (TRACK_BIT_UPPER | TRACK_BIT_LOWER) && /* Horizontal parallel, non-intersecting tracks */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   727
				trackbits != (TRACK_BIT_LEFT | TRACK_BIT_RIGHT) /* Vertical parallel, non-intersecting tracks */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   728
		)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   729
			return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   730
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   732
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   734
	if (!HasSignalOnTrack(tile, track)) {
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   735
		// build new signals
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
		cost = _price.build_signals;
1066
aad3c6ad147f (svn r1567) -Fix: [110452] On horizontal/vertical tracks you are also charged for building/removing signals on the parallel track on the same tile. Signal updating is also correct. (Hackykid)
darkvater
parents: 1059
diff changeset
   737
	} else {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   738
		if (p2 != 0 && semaphore != HasSemaphores(tile, track)) {
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   739
			// convert signals <-> semaphores
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   740
			cost = _price.build_signals + _price.remove_signals;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   741
		} else {
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   742
			// it is free to change orientation/pre-exit-combo signals
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   743
			cost = 0;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   744
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   745
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   747
	if (flags & DC_EXEC) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   748
		if (GetRailTileType(tile) != RAIL_TYPE_SIGNALS) {
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   749
			// there are no signals at all on this tile yet
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   750
			_map5[tile] |= RAIL_TYPE_SIGNALS; // change into signals
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   751
			_map2[tile] |= 0xF0;              // all signals are on
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   752
			_map3_lo[tile] &= ~0xF0;          // no signals built by default
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   753
			_map3_hi[tile] = semaphore ? 4 : 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   755
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   756
		if (p2 == 0) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   757
			if (!HasSignalOnTrack(tile, track)) {
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   758
				// build new signals
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   759
				_map3_lo[tile] |= SignalOnTrack(track);
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   760
			} else {
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   761
				if (pre_signal) {
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   762
					// cycle between normal -> pre -> exit -> combo -> ...
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   763
					byte type = (GetSignalType(tile, track) + 1) & 0x03;
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   764
					_map3_hi[tile] &= ~0x03;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   765
					_map3_hi[tile] |= type;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   766
				} else {
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   767
					// cycle between two-way -> one-way -> one-way -> ...
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   768
					/* TODO: Rewrite switch into something more general */
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   769
					switch (track) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   770
						case TRACK_LOWER:
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   771
						case TRACK_RIGHT: {
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   772
							byte signal = (_map3_lo[tile] - 0x10) & 0x30;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   773
							if (signal == 0) signal = 0x30;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   774
							_map3_lo[tile] &= ~0x30;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   775
							_map3_lo[tile] |= signal;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   776
							break;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   777
						}
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   778
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   779
						default: {
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   780
							byte signal = (_map3_lo[tile] - 0x40) & 0xC0;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   781
							if (signal == 0) signal = 0xC0;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   782
							_map3_lo[tile] &= ~0xC0;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   783
							_map3_lo[tile] |= signal;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   784
							break;
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   785
						}
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   786
					}
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   787
				}
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   788
			}
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   789
		} else {
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   790
			/* If CmdBuildManySignals is called with copying signals, just copy the
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   791
			 * direction of the first signal given as parameter by CmdBuildManySignals */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   792
			_map3_lo[tile] &= ~SignalOnTrack(track);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   793
			_map3_lo[tile] |= p2 & SignalOnTrack(track);
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   794
			// convert between signal<->semaphores when dragging
1084
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   795
			if (semaphore)
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   796
				SETBIT(_map3_hi[tile], 2);
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   797
			else
16db2b52597e (svn r1585) Rewrite CmdBuildSignals()
tron
parents: 1066
diff changeset
   798
				CLRBIT(_map3_hi[tile], 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   799
		}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   800
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   801
		MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   802
		SetSignalsOnBothDir(tile, track);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   803
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   804
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   805
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   808
/**	Build many signals by dragging; AutoSignals
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   809
 * @param x,y start tile of drag
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   810
 * @param p1  end tile of drag
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   811
 * @param p2 various bitstuffed elements
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   812
 * - p2 = (bit  0)    - 0 = build, 1 = remove signals
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   813
 * - p2 = (bit  3)    - 0 = signals, 1 = semaphores
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   814
 * - p2 = (bit  4- 6) - track-orientation, valid values: 0-5 (Track enum)
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   815
 * - p2 = (bit 24-31) - user defined signals_density
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   816
 */
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   817
static int32 CmdSignalTrackHelper(int x, int y, uint32 flags, uint32 p1, uint32 p2)
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   818
{
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   819
	int ex, ey;
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   820
	int32 ret, total_cost, signal_ctr;
1954
58fee2f9eb81 (svn r2460) - Fix: Prevent assertion failure when trying to drag signals starting from a non-rail tile.
hackykid
parents: 1946
diff changeset
   821
	byte signals;
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   822
	TileIndex tile = TileVirtXY(x, y);
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   823
	bool error = true;
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   824
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   825
	int mode = p2 & 0x1;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   826
	Track track = (p2 >> 4) & 7;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   827
	Trackdir trackdir = TrackToTrackdir(track);
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   828
	byte semaphores = (HASBIT(p2, 3)) ? 8 : 0;
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   829
	byte signal_density = (p2 >> 24);
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   830
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   831
	if (p1 > MapSize()) return CMD_ERROR;
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   832
	if (signal_density == 0 || signal_density > 20) return CMD_ERROR;
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   833
1954
58fee2f9eb81 (svn r2460) - Fix: Prevent assertion failure when trying to drag signals starting from a non-rail tile.
hackykid
parents: 1946
diff changeset
   834
	if (!IsTileType(tile, MP_RAILWAY))
58fee2f9eb81 (svn r2460) - Fix: Prevent assertion failure when trying to drag signals starting from a non-rail tile.
hackykid
parents: 1946
diff changeset
   835
		return CMD_ERROR;
58fee2f9eb81 (svn r2460) - Fix: Prevent assertion failure when trying to drag signals starting from a non-rail tile.
hackykid
parents: 1946
diff changeset
   836
889
7f8630bfea41 (svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents: 826
diff changeset
   837
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
7f8630bfea41 (svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents: 826
diff changeset
   838
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   839
	/* for vertical/horizontal tracks, double the given signals density
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   840
	* since the original amount will be too dense (shorter tracks) */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   841
	if (!IsDiagonalTrack(track))
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   842
		signal_density *= 2;
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   843
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   844
	// unpack end tile
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   845
	ex = TileX(p1) * TILE_SIZE;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   846
	ey = TileY(p1) * TILE_SIZE;
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   847
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   848
	if (CmdFailed(ValidateAutoDrag(&trackdir, x, y, ex, ey))) return CMD_ERROR;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   849
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   850
	track = TrackdirToTrack(trackdir); /* trackdir might have changed, keep track in sync */
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   851
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   852
	// copy the signal-style of the first rail-piece if existing
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   853
	if (GetRailTileType(tile) == RAIL_TYPE_SIGNALS && GetTrackBits(tile) != 0) { /* XXX: GetTrackBits check useless? */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   854
		signals = _map3_lo[tile] & SignalOnTrack(track);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   855
		if (signals == 0) signals = SignalOnTrack(track); /* Can this actually occur? */
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   856
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   857
		semaphores = (HasSemaphores(tile, track) ? 8 : 0); // copy signal/semaphores style (independent of CTRL)
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   858
	} else // no signals exist, drag a two-way signal stretch
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   859
		signals = SignalOnTrack(track);
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   860
1954
58fee2f9eb81 (svn r2460) - Fix: Prevent assertion failure when trying to drag signals starting from a non-rail tile.
hackykid
parents: 1946
diff changeset
   861
	/* signal_ctr         - amount of tiles already processed
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   862
	 * signals_density    - patch setting to put signal on every Nth tile (double space on |, -- tracks)
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   863
	 **********
1954
58fee2f9eb81 (svn r2460) - Fix: Prevent assertion failure when trying to drag signals starting from a non-rail tile.
hackykid
parents: 1946
diff changeset
   864
	 * trackdir   - trackdir to build with autorail
58fee2f9eb81 (svn r2460) - Fix: Prevent assertion failure when trying to drag signals starting from a non-rail tile.
hackykid
parents: 1946
diff changeset
   865
	 * semaphores - semaphores or signals
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   866
	 * signals    - is there a signal/semaphore on the first tile, copy its style (two-way/single-way)
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   867
	                and convert all others to semaphore/signal
1954
58fee2f9eb81 (svn r2460) - Fix: Prevent assertion failure when trying to drag signals starting from a non-rail tile.
hackykid
parents: 1946
diff changeset
   868
	 * mode       - 1 remove signals, 0 build signals */
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   869
	signal_ctr = total_cost = 0;
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   870
	for (;;) {
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   871
		// only build/remove signals with the specified density
1954
58fee2f9eb81 (svn r2460) - Fix: Prevent assertion failure when trying to drag signals starting from a non-rail tile.
hackykid
parents: 1946
diff changeset
   872
		if ((signal_ctr % signal_density) == 0 ) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   873
			ret = DoCommand(x, y, TrackdirToTrack(trackdir) | semaphores, signals, flags, (mode == 1) ? CMD_REMOVE_SIGNALS : CMD_BUILD_SIGNALS);
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   874
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 817
diff changeset
   875
			/* Abort placement for any other error than NOT_SUITABLE_TRACK
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   876
			 * This includes vehicles on track, competitor's tracks, etc. */
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   877
			if (CmdFailed(ret)) {
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   878
				if (_error_message != STR_1005_NO_SUITABLE_RAILROAD_TRACK && mode != 1) return CMD_ERROR;
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   879
			} else {
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   880
				error = false;
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   881
				total_cost += ret;
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   882
			}
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   883
		}
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   884
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   885
		if (ex == x && ey == y) break; // reached end of drag
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   886
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   887
		x += _railbit.xinc[trackdir];
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   888
		y += _railbit.yinc[trackdir];
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   889
		signal_ctr++;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   890
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   891
		// toggle railbit for the non-diagonal tracks (|, -- tracks)
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   892
		if (!IsDiagonalTrackdir(trackdir)) trackdir ^= 1;
58
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   893
	}
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   894
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   895
	return (error) ? CMD_ERROR : total_cost;
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   896
}
b9fdcc9b5c90 (svn r59) -Feature: Added Autosignals, just like Autorail. Can copy signal style, convert signal<->semaphore, etc. Big thanks to betatesters Dribbel and Testman57 (Darkvater)
darkvater
parents: 39
diff changeset
   897
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   898
/** Build signals on a stretch of track.
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   899
 * Stub for the unified signal builder/remover
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   900
 * @see CmdSignalTrackHelper
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   901
 */
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   902
int32 CmdBuildSignalTrack(int x, int y, uint32 flags, uint32 p1, uint32 p2)
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   903
{
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   904
	return CmdSignalTrackHelper(x, y, flags, p1, p2);
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   905
}
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   906
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: 1719
diff changeset
   907
/** Remove signals
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: 1719
diff changeset
   908
 * @param x,y coordinates where signal is being deleted from
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   909
 * @param p1 track to remove signal from (Track enum)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   910
 */
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   911
int32 CmdRemoveSingleSignal(int x, int y, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
{
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   913
	TileIndex tile = TileVirtXY(x, y);
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   914
	Track track = (Track)(p1 & 0x7);
1518
ade7a74ac31a (svn r2022) Revise CmdRemoveSingleSignal: Check parameters for validity and simplify the function
tron
parents: 1477
diff changeset
   915
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: 1719
diff changeset
   916
	if (!ValParamTrackOrientation(track) || !IsTileType(tile, MP_RAILWAY) || !EnsureNoVehicle(tile))
1518
ade7a74ac31a (svn r2022) Revise CmdRemoveSingleSignal: Check parameters for validity and simplify the function
tron
parents: 1477
diff changeset
   917
		return CMD_ERROR;
ade7a74ac31a (svn r2022) Revise CmdRemoveSingleSignal: Check parameters for validity and simplify the function
tron
parents: 1477
diff changeset
   918
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   919
	if (!HasSignalOnTrack(tile, track)) // no signals on track?
1518
ade7a74ac31a (svn r2022) Revise CmdRemoveSingleSignal: Check parameters for validity and simplify the function
tron
parents: 1477
diff changeset
   920
		return CMD_ERROR;
ade7a74ac31a (svn r2022) Revise CmdRemoveSingleSignal: Check parameters for validity and simplify the function
tron
parents: 1477
diff changeset
   921
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: 1719
diff changeset
   922
	/* Only water can remove signals from anyone */
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: 1719
diff changeset
   923
	if (_current_player != OWNER_WATER && !CheckTileOwnership(tile)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   925
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
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
	/* Do it? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   928
	if (flags & DC_EXEC) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   929
		_map3_lo[tile] &= ~SignalOnTrack(track);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   930
1066
aad3c6ad147f (svn r1567) -Fix: [110452] On horizontal/vertical tracks you are also charged for building/removing signals on the parallel track on the same tile. Signal updating is also correct. (Hackykid)
darkvater
parents: 1059
diff changeset
   931
		/* removed last signal from tile? */
aad3c6ad147f (svn r1567) -Fix: [110452] On horizontal/vertical tracks you are also charged for building/removing signals on the parallel track on the same tile. Signal updating is also correct. (Hackykid)
darkvater
parents: 1059
diff changeset
   932
		if ((_map3_lo[tile] & 0xF0) == 0) {
aad3c6ad147f (svn r1567) -Fix: [110452] On horizontal/vertical tracks you are also charged for building/removing signals on the parallel track on the same tile. Signal updating is also correct. (Hackykid)
darkvater
parents: 1059
diff changeset
   933
			_map5[tile] &= ~RAIL_TYPE_SIGNALS;
aad3c6ad147f (svn r1567) -Fix: [110452] On horizontal/vertical tracks you are also charged for building/removing signals on the parallel track on the same tile. Signal updating is also correct. (Hackykid)
darkvater
parents: 1059
diff changeset
   934
			_map2[tile] &= ~0xF0;
aad3c6ad147f (svn r1567) -Fix: [110452] On horizontal/vertical tracks you are also charged for building/removing signals on the parallel track on the same tile. Signal updating is also correct. (Hackykid)
darkvater
parents: 1059
diff changeset
   935
			CLRBIT(_map3_hi[tile], 2); // remove any possible semaphores
aad3c6ad147f (svn r1567) -Fix: [110452] On horizontal/vertical tracks you are also charged for building/removing signals on the parallel track on the same tile. Signal updating is also correct. (Hackykid)
darkvater
parents: 1059
diff changeset
   936
		}
1109
1bab892228cd (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1095
diff changeset
   937
1066
aad3c6ad147f (svn r1567) -Fix: [110452] On horizontal/vertical tracks you are also charged for building/removing signals on the parallel track on the same tile. Signal updating is also correct. (Hackykid)
darkvater
parents: 1059
diff changeset
   938
		SetSignalsOnBothDir(tile, track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
		MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
	}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
   942
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
	return _price.remove_signals;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   946
/** Remove signals on a stretch of track.
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   947
 * Stub for the unified signal builder/remover
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   948
 * @see CmdSignalTrackHelper
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   949
 */
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   950
int32 CmdRemoveSignalTrack(int x, int y, uint32 flags, uint32 p1, uint32 p2)
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   951
{
1229
4c965ad79a0b (svn r1733) - Fix: oops "| 1" is SETBIT(x, 0), not 1 :(. Now you can remove signals again
darkvater
parents: 1227
diff changeset
   952
	return CmdSignalTrackHelper(x, y, flags, p1, SETBIT(p2, 0));
1227
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   953
}
3552f20fcfcb (svn r1731) - Fix: [ 1106930 ] BugFix: placing signals with 2x1 drags workaround is completely rewritten. Also features checks for hacked/modified clients. Thanks a lot Hackykid!
darkvater
parents: 1209
diff changeset
   954
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
   955
typedef int32 DoConvertRailProc(TileIndex tile, uint totype, bool exec);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
   957
static int32 DoConvertRail(TileIndex tile, uint totype, bool exec)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
	if (!CheckTileOwnership(tile) || !EnsureNoVehicle(tile))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
	// tile is already of requested type?
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   963
	if ( GetRailType(tile) == totype)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   964
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
	// change type.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
	if (exec) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
		_map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
		MarkTileDirtyByTile(tile);
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
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   972
	return _price.build_rail / 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
   975
extern int32 DoConvertStationRail(TileIndex tile, uint totype, bool exec);
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
   976
extern int32 DoConvertStreetRail(TileIndex tile, uint totype, bool exec);
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
   977
extern int32 DoConvertTunnelBridgeRail(TileIndex tile, uint totype, bool exec);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   979
/** Convert one rail type to the other. You can convert normal rail to
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   980
 * monorail/maglev easily or vice-versa.
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   981
 * @param ex,ey end tile of rail conversion drag
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   982
 * @param p1 start tile of drag
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   983
 * @param p2 new railtype to convert to
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   984
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
int32 CmdConvertRail(int ex, int ey, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   986
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
	int32 ret, cost, money;
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   988
	int sx, sy, x, y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
889
7f8630bfea41 (svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents: 826
diff changeset
   990
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
7f8630bfea41 (svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents: 826
diff changeset
   991
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   992
	if (!ValParamRailtype(p2)) return CMD_ERROR;
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   993
	if (p1 > MapSize()) return CMD_ERROR;
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
   994
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   995
	// make sure sx,sy are smaller than ex,ey
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   996
	sx = TileX(p1) * TILE_SIZE;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
   997
	sy = TileY(p1) * TILE_SIZE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
	if (ex < sx) intswap(ex, sx);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
	if (ey < sy) intswap(ey, sy);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1000
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
	money = GetAvailableMoneyForCommand();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
	cost = 0;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1003
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1004
	for (x = sx; x <= ex; x += TILE_SIZE) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1005
		for (y = sy; y <= ey; y += TILE_SIZE) {
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
  1006
			TileIndex tile = TileVirtXY(x, y);
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
  1007
			DoConvertRailProc *proc;
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
  1008
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
  1009
			if (IsTileType(tile, MP_RAILWAY)) proc = DoConvertRail;
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
  1010
			else if (IsTileType(tile, MP_STATION)) proc = DoConvertStationRail;
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
  1011
			else if (IsTileType(tile, MP_STREET)) proc = DoConvertStreetRail;
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
  1012
			else if (IsTileType(tile, MP_TUNNELBRIDGE)) proc = DoConvertTunnelBridgeRail;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
			else continue;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1014
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
  1015
			ret = proc(tile, p2, false);
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
  1016
			if (CmdFailed(ret)) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1017
			cost += ret;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1018
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1019
			if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1020
				if ( (money -= ret) < 0) { _additional_cash_required = ret; return cost - ret; }
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
  1021
				proc(tile, p2, true);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1022
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1023
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1024
	}
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
  1025
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1781
diff changeset
  1026
	return (cost == 0) ? CMD_ERROR : cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1027
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1028
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  1029
static int32 RemoveTrainDepot(TileIndex tile, uint32 flags)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1030
{
149
5f7d4b21df01 (svn r150) -Fix: [1010833] Turning on the magic bulldozer removes oil rigs
darkvater
parents: 84
diff changeset
  1031
	if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1032
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1033
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1034
	if (!EnsureNoVehicle(tile))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1035
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1036
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
	if (flags & DC_EXEC) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1038
		int track = TrackdirToTrack(DiagdirToDiagTrackdir(GetDepotDirection(tile, TRANSPORT_RAIL)));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1039
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1040
		DoDeleteDepot(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1041
		SetSignalsOnBothDir(tile, track);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1042
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1044
	return _price.remove_train_depot;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1045
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1046
1534
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1047
static int32 ClearTile_Track(TileIndex tile, byte flags)
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1048
{
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1049
	int32 cost;
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1050
	int32 ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1051
	byte m5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1052
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
	m5 = _map5[tile];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
	if (flags & DC_AUTO) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1056
		if (m5 & RAIL_TYPE_SPECIAL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1057
			return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1058
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
  1059
		if (!IsTileOwner(tile, _current_player))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1060
			return_cmd_error(STR_1024_AREA_IS_OWNED_BY_ANOTHER);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1061
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1062
		return_cmd_error(STR_1008_MUST_REMOVE_RAILROAD_TRACK);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
	cost = 0;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1066
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1067
	switch (GetRailTileType(tile)) {
1534
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1068
		case RAIL_TYPE_SIGNALS:
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1069
			if (_map3_lo[tile] & _signals_table_both[0]) {
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1070
				ret = DoCommandByTile(tile, 0, 0, flags, CMD_REMOVE_SIGNALS);
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1071
				if (ret == CMD_ERROR) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
				cost += ret;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
			}
1534
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1074
			if (_map3_lo[tile] & _signals_table_both[3]) {
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1075
				ret = DoCommandByTile(tile, 3, 0, flags, CMD_REMOVE_SIGNALS);
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1076
				if (ret == CMD_ERROR) return CMD_ERROR;
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1077
				cost += ret;
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1078
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1079
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1080
			m5 &= TRACK_BIT_MASK;
1598
4425f756c19b (svn r2102) Fix bug introduced in r2038, which gave you money for clearing occupied railway tiles ([1171926])
tron
parents: 1591
diff changeset
  1081
			if (!(flags & DC_EXEC)) {
4425f756c19b (svn r2102) Fix bug introduced in r2038, which gave you money for clearing occupied railway tiles ([1171926])
tron
parents: 1591
diff changeset
  1082
				for (; m5 != 0; m5 >>= 1) if (m5 & 1) cost += _price.remove_rail;
4425f756c19b (svn r2102) Fix bug introduced in r2038, which gave you money for clearing occupied railway tiles ([1171926])
tron
parents: 1591
diff changeset
  1083
				return cost;
4425f756c19b (svn r2102) Fix bug introduced in r2038, which gave you money for clearing occupied railway tiles ([1171926])
tron
parents: 1591
diff changeset
  1084
			}
1534
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1085
			/* FALLTHROUGH */
1109
1bab892228cd (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1095
diff changeset
  1086
1534
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1087
		case RAIL_TYPE_NORMAL: {
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1088
			uint i;
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1089
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1090
			for (i = 0; m5 != 0; i++, m5 >>= 1) {
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1091
				if (m5 & 1) {
1598
4425f756c19b (svn r2102) Fix bug introduced in r2038, which gave you money for clearing occupied railway tiles ([1171926])
tron
parents: 1591
diff changeset
  1092
					ret = DoCommandByTile(tile, 0, i, flags, CMD_REMOVE_SINGLE_RAIL);
4425f756c19b (svn r2102) Fix bug introduced in r2038, which gave you money for clearing occupied railway tiles ([1171926])
tron
parents: 1591
diff changeset
  1093
					if (ret == CMD_ERROR) return CMD_ERROR;
1534
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1094
					cost += ret;
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1095
				}
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1096
			}
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1097
			return cost;
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1098
		}
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1099
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1100
		case RAIL_TYPE_DEPOT_WAYPOINT:
1534
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1101
			switch (m5 & RAIL_SUBTYPE_MASK) {
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1102
				case RAIL_SUBTYPE_DEPOT:
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1103
					return RemoveTrainDepot(tile, flags);
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1104
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1105
				case RAIL_SUBTYPE_WAYPOINT:
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1106
					return RemoveTrainWaypoint(tile, flags, false);
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1107
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1108
				default:
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1109
					return CMD_ERROR;
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1110
			}
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1111
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1112
		default:
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1113
			return CMD_ERROR;
2f7388d38291 (svn r2038) -Fix: A player only received the money for one rail when clearing a tile with 2 rails and signals
tron
parents: 1518
diff changeset
  1114
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1115
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1116
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1117
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1118
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
#include "table/track_land.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1120
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1121
// used for presignals
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1122
static const SpriteID _signal_base_sprites[16] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
	0x4FB,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1124
	0x1323,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1125
	0x1333,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1126
	0x1343,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1128
	// use semaphores instead of signals?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1129
	0x1353,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1130
	0x1363,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1131
	0x1373,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1132
	0x1383,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1133
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1134
	// mirrored versions
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1135
	0x4FB,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1136
	0x1323,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1137
	0x1333,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
	0x1343,
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1139
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
	0x13C6,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1141
	0x13D6,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1142
	0x13E6,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1143
	0x13F6,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1144
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1145
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1146
// used to determine the side of the road for the signal
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1147
static const byte _signal_position[24] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1148
	/* original: left side position */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1149
	0x58,0x1E,0xE1,0xB9,0x01,0xA3,0x4B,0xEE,0x3B,0xD4,0x43,0xBD,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1150
	/* patch: ride side position */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
	0x1E,0xAC,0x64,0xE1,0x4A,0x10,0xEE,0xC5,0xDB,0x34,0x4D,0xB3
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1152
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1153
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1154
static void DrawSignalHelper(TileInfo *ti, byte condition, uint32 image_and_pos)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1155
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1156
	bool otherside = _opt.road_side & _patches.signal_side;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1157
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1158
	uint v = _signal_position[(image_and_pos & 0xF) + (otherside ? 12 : 0)];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1159
	uint x = ti->x | (v&0xF);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1160
	uint y = ti->y | (v>>4);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1161
	uint sprite = _signal_base_sprites[(_map3_hi[ti->tile] & 7) + (otherside ? 8 : 0)] + (image_and_pos>>4) + ((condition != 0) ? 1 : 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1162
	AddSortableSpriteToDraw(sprite, x, y, 1, 1, 10, GetSlopeZ(x,y));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1163
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1164
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1165
static uint32 _drawtile_track_palette;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1166
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1167
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1168
static void DrawTrackFence_NW(TileInfo *ti)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1169
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1170
	uint32 image = 0x515;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1171
	if (ti->tileh != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1172
		image = 0x519;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1173
		if (!(ti->tileh & 2)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1174
			image = 0x51B;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1175
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1176
	}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1177
	AddSortableSpriteToDraw(image | _drawtile_track_palette,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1178
		ti->x, ti->y+1, 16, 1, 4, ti->z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1179
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1180
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
static void DrawTrackFence_SE(TileInfo *ti)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1182
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1183
	uint32 image = 0x515;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
	if (ti->tileh != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1185
		image = 0x519;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1186
		if (!(ti->tileh & 2)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1187
			image = 0x51B;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1188
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1189
	}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1190
	AddSortableSpriteToDraw(image | _drawtile_track_palette,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1191
		ti->x, ti->y+15, 16, 1, 4, ti->z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1192
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1193
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1194
static void DrawTrackFence_NW_SE(TileInfo *ti)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1195
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1196
	DrawTrackFence_NW(ti);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1197
	DrawTrackFence_SE(ti);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1198
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1199
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1200
static void DrawTrackFence_NE(TileInfo *ti)
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
	uint32 image = 0x516;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1203
	if (ti->tileh != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1204
		image = 0x51A;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1205
		if (!(ti->tileh & 2)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1206
			image = 0x51C;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1207
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1208
	}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1209
	AddSortableSpriteToDraw(image | _drawtile_track_palette,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1210
		ti->x+1, ti->y, 1, 16, 4, ti->z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1211
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1212
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1213
static void DrawTrackFence_SW(TileInfo *ti)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1214
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1215
	uint32 image = 0x516;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1216
	if (ti->tileh != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1217
		image = 0x51A;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1218
		if (!(ti->tileh & 2)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1219
			image = 0x51C;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1220
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1221
	}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1222
	AddSortableSpriteToDraw(image | _drawtile_track_palette,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1223
		ti->x+15, ti->y, 1, 16, 4, ti->z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1224
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1225
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1226
static void DrawTrackFence_NE_SW(TileInfo *ti)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1227
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1228
	DrawTrackFence_NE(ti);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1229
	DrawTrackFence_SW(ti);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1230
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1231
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1232
static void DrawTrackFence_NS_1(TileInfo *ti)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1233
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1234
	int z = ti->z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1235
	if (ti->tileh & 1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1236
		z += 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1237
	AddSortableSpriteToDraw(0x517 | _drawtile_track_palette,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1238
		ti->x + 8, ti->y + 8, 1, 1, 4, z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1239
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1240
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1241
static void DrawTrackFence_NS_2(TileInfo *ti)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1242
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1243
	int z = ti->z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1244
	if (ti->tileh & 4)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1245
		z += 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1246
	AddSortableSpriteToDraw(0x517 | _drawtile_track_palette,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1247
		ti->x + 8, ti->y + 8, 1, 1, 4, z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1248
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1249
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1250
static void DrawTrackFence_WE_1(TileInfo *ti)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1251
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1252
	int z = ti->z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1253
	if (ti->tileh & 8)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
		z += 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1255
	AddSortableSpriteToDraw(0x518 | _drawtile_track_palette,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1256
		ti->x + 8, ti->y + 8, 1, 1, 4, z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1257
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1258
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1259
static void DrawTrackFence_WE_2(TileInfo *ti)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1260
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1261
	int z = ti->z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1262
	if (ti->tileh & 2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1263
		z += 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1264
	AddSortableSpriteToDraw(0x518 | _drawtile_track_palette,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1265
		ti->x + 8, ti->y + 8, 1, 1, 4, z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1266
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1267
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1268
static void DetTrackDrawProc_Null(TileInfo *ti)
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
	/* nothing should be here */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1271
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1272
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1273
typedef void DetailedTrackProc(TileInfo *ti);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1274
DetailedTrackProc * const _detailed_track_proc[16] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1275
	DetTrackDrawProc_Null,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1276
	DetTrackDrawProc_Null,
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1277
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1278
	DrawTrackFence_NW,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1279
	DrawTrackFence_SE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1280
	DrawTrackFence_NW_SE,
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1281
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1282
	DrawTrackFence_NE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1283
	DrawTrackFence_SW,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1284
	DrawTrackFence_NE_SW,
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1285
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1286
	DrawTrackFence_NS_1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1287
	DrawTrackFence_NS_2,
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1288
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1289
	DrawTrackFence_WE_1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1290
	DrawTrackFence_WE_2,
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1291
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1292
	DetTrackDrawProc_Null,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1293
	DetTrackDrawProc_Null,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1294
	DetTrackDrawProc_Null,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1295
	DetTrackDrawProc_Null,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1296
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1297
389
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1298
static void DrawSpecialBuilding(uint32 image, uint32 tracktype_offs,
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1299
                                TileInfo *ti,
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1300
                                byte x, byte y, byte z,
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1301
                                byte xsize, byte ysize, byte zsize)
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1302
{
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1303
	if (image & 0x8000)
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1304
		image |= _drawtile_track_palette;
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1305
	image += tracktype_offs;
497
2d6d31173813 (svn r787) Invert the sense of the DO_TRANS_BUILDINGS flag to be consistent with its own name and all other DO_* flags.
tron
parents: 473
diff changeset
  1306
	if (_display_opt & DO_TRANS_BUILDINGS) // show transparent depots
389
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1307
		image = (image & 0x3FFF) | 0x3224000;
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1308
	AddSortableSpriteToDraw(image, ti->x + x, ti->y + y, xsize, ysize, zsize, ti->z + z);
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1309
}
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1310
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1311
static void DrawTile_Track(TileInfo *ti)
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
	uint32 tracktype_offs, image;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1314
	byte m5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1315
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
  1316
	_drawtile_track_palette = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile)));
0
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
	tracktype_offs = (_map3_lo[ti->tile] & 0xF) * TRACKTYPE_SPRITE_PITCH;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1319
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1320
	m5 = (byte)ti->map5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1321
	if (!(m5 & RAIL_TYPE_SPECIAL)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1322
		bool special;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1323
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1324
		m5 &= TRACK_BIT_MASK;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1325
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1326
		special = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1327
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1328
		// select the sprite to use based on the map5 byte.
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1329
		(image = 0x3F3, m5 == TRACK_BIT_DIAG2) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1330
		(image++,				m5 == TRACK_BIT_DIAG1) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1331
		(image++,				m5 == TRACK_BIT_UPPER) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1332
		(image++,				m5 == TRACK_BIT_LOWER) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1333
		(image++,				m5 == TRACK_BIT_RIGHT) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1334
		(image++,				m5 == TRACK_BIT_LEFT) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1335
		(image++,				m5 == (TRACK_BIT_DIAG1|TRACK_BIT_DIAG2)) ||
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1336
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1337
		(image = 0x40B, m5 == (TRACK_BIT_UPPER|TRACK_BIT_LOWER)) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1338
		(image++,				m5 == (TRACK_BIT_LEFT|TRACK_BIT_RIGHT)) ||
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1339
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1340
		(special=true, false) ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1341
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1342
		(image = 0x3FA, !(m5 & (TRACK_BIT_RIGHT|TRACK_BIT_UPPER|TRACK_BIT_DIAG1))) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1343
		(image++,				!(m5 & (TRACK_BIT_LEFT|TRACK_BIT_LOWER|TRACK_BIT_DIAG1))) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1344
		(image++,				!(m5 & (TRACK_BIT_LEFT|TRACK_BIT_UPPER|TRACK_BIT_DIAG2))) ||
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1345
		(image++,				!(m5 & (TRACK_BIT_RIGHT|TRACK_BIT_LOWER|TRACK_BIT_DIAG2))) ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1346
		(image++, true);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1347
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1348
		if (ti->tileh != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1349
			int f = GetRailFoundation(ti->tileh, ti->map5 & 0x3F);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1350
			if (f) DrawFoundation(ti, f);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1351
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1352
			// default sloped sprites..
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1353
			if (ti->tileh != 0) image = _track_sloped_sprites[ti->tileh - 1] + 0x3F3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1354
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1355
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1356
		if ((_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK)==RAIL_GROUND_BROWN)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1357
			image = (image & 0xFFFF) | 0x3178000; // use a brown palette
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1358
		else if ((_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK)==RAIL_GROUND_ICE_DESERT)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1359
			image += 26;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1360
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1361
		DrawGroundSprite(image + tracktype_offs);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1362
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1363
		if (special) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1364
			if (m5 & TRACK_BIT_DIAG1) DrawGroundSprite(0x3ED + tracktype_offs);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1365
			if (m5 & TRACK_BIT_DIAG2) DrawGroundSprite(0x3EE + tracktype_offs);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1366
			if (m5 & TRACK_BIT_UPPER) DrawGroundSprite(0x3EF + tracktype_offs);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1367
			if (m5 & TRACK_BIT_LOWER) DrawGroundSprite(0x3F0 + tracktype_offs);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1368
			if (m5 & TRACK_BIT_LEFT)  DrawGroundSprite(0x3F2 + tracktype_offs);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1369
			if (m5 & TRACK_BIT_RIGHT) DrawGroundSprite(0x3F1 + tracktype_offs);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1370
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1371
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1372
		if (_display_opt & DO_FULL_DETAIL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1373
			_detailed_track_proc[_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK](ti);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1374
		}
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
		/* draw signals also? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1377
		if (!(ti->map5 & RAIL_TYPE_SIGNALS))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1378
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1379
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1380
		{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1381
			byte m23;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1382
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1383
			m23 = (_map3_lo[ti->tile] >> 4) | (_map2[ti->tile] & 0xF0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1384
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
#define HAS_SIGNAL(x) (m23 & (byte)(0x1 << (x)))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1386
#define ISON_SIGNAL(x) (m23 & (byte)(0x10 << (x)))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1387
#define MAYBE_DRAW_SIGNAL(x,y,z) if (HAS_SIGNAL(x)) DrawSignalHelper(ti, ISON_SIGNAL(x), ((y-0x4FB) << 4)|(z))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1388
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1389
		if (!(m5 & TRACK_BIT_DIAG2)) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1390
			if (!(m5 & TRACK_BIT_DIAG1)) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1391
				if (m5 & TRACK_BIT_LEFT) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1392
					MAYBE_DRAW_SIGNAL(2, 0x509, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1393
					MAYBE_DRAW_SIGNAL(3, 0x507, 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1394
				}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1395
				if (m5 & TRACK_BIT_RIGHT) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1396
					MAYBE_DRAW_SIGNAL(0, 0x509, 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1397
					MAYBE_DRAW_SIGNAL(1, 0x507, 3);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1398
				}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1399
				if (m5 & TRACK_BIT_UPPER) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1400
					MAYBE_DRAW_SIGNAL(3, 0x505, 4);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1401
					MAYBE_DRAW_SIGNAL(2, 0x503, 5);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1402
				}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1403
				if (m5 & TRACK_BIT_LOWER) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1404
					MAYBE_DRAW_SIGNAL(1, 0x505, 6);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1405
					MAYBE_DRAW_SIGNAL(0, 0x503, 7);
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
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1408
				MAYBE_DRAW_SIGNAL(3, 0x4FB, 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1409
				MAYBE_DRAW_SIGNAL(2, 0x4FD, 9);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1410
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1411
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1412
			MAYBE_DRAW_SIGNAL(3, 0x4FF, 10);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1413
			MAYBE_DRAW_SIGNAL(2, 0x501, 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1414
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1415
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1416
	} else {
395
4c990f33dab7 (svn r587) -newgrf: Rename all /Checkpoint/i tokens to 'Waypoint's. The name actually makes some sense and is also compatible with TTDPatch (pasky).
darkvater
parents: 393
diff changeset
  1417
		/* draw depots / waypoints */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1418
		const DrawTrackSeqStruct *drss;
395
4c990f33dab7 (svn r587) -newgrf: Rename all /Checkpoint/i tokens to 'Waypoint's. The name actually makes some sense and is also compatible with TTDPatch (pasky).
darkvater
parents: 393
diff changeset
  1419
		byte type = m5 & 0x3F; // 0-3: depots, 4-5: waypoints
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1420
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1421
		if (!(m5 & (RAIL_TILE_TYPE_MASK&~RAIL_TYPE_SPECIAL)))
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1422
			/* XXX: There used to be "return;" here, but since I could not find out
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1423
			 * why this would ever occur, I put assert(0) here. Let's see if someone
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1424
			 * complains about it. If not, we'll remove this check. (Matthijs). */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1425
			 assert(0);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1426
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1427
		if (ti->tileh != 0) { DrawFoundation(ti, ti->tileh); }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1428
1535
a5f686a20d2c (svn r2039) IS_RAIL_DEPOT -> IsRailDepot, IS_RAIL_WAYPOINT -> IsRailWaypoint
tron
parents: 1534
diff changeset
  1429
		if (IsRailWaypoint(m5) && _map3_lo[ti->tile] & 16) {
389
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1430
			// look for customization
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1399
diff changeset
  1431
			StationSpec *stat = GetCustomStation(STAT_CLASS_WAYP, _map3_hi[ti->tile]);
389
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1432
403
bfb365c74e80 (svn r600) -newgrf: Relocation offset for custom station sprites is now stored separately, making it possible to show different sprites in waypoint selection dialog (pasky).
darkvater
parents: 399
diff changeset
  1433
			if (stat) {
389
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1434
				DrawTileSeqStruct const *seq;
403
bfb365c74e80 (svn r600) -newgrf: Relocation offset for custom station sprites is now stored separately, making it possible to show different sprites in waypoint selection dialog (pasky).
darkvater
parents: 399
diff changeset
  1435
				// emulate station tile - open with building
bfb365c74e80 (svn r600) -newgrf: Relocation offset for custom station sprites is now stored separately, making it possible to show different sprites in waypoint selection dialog (pasky).
darkvater
parents: 399
diff changeset
  1436
				DrawTileSprites *cust = &stat->renderdata[2 + (m5 & 0x1)];
413
36afcda97345 (svn r610) -newgrf: Support for some basic deterministical spritegroups regarding stations. Waypoints look changes based on year now :^) (pasky).
darkvater
parents: 412
diff changeset
  1437
				uint32 relocation = GetCustomStationRelocation(stat, ComposeWaypointStation(ti->tile), 0);
415
423de36034b8 (svn r612) [newgrf] Fix: custom waypoints on monorail/maglev are displayed correctly
dominik
parents: 413
diff changeset
  1438
				int railtype=(_map3_lo[ti->tile] & 0xF);
389
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1439
447
aaf1f59a8957 (svn r657) Do not touch 0x8000 bit in sprite index for custom waypoints. (pasky)
celestar
parents: 438
diff changeset
  1440
				/* We don't touch the 0x8000 bit. In all this
aaf1f59a8957 (svn r657) Do not touch 0x8000 bit in sprite index for custom waypoints. (pasky)
celestar
parents: 438
diff changeset
  1441
				 * waypoint code, it is used to indicate that
aaf1f59a8957 (svn r657) Do not touch 0x8000 bit in sprite index for custom waypoints. (pasky)
celestar
parents: 438
diff changeset
  1442
				 * we should offset by railtype, but we always
aaf1f59a8957 (svn r657) Do not touch 0x8000 bit in sprite index for custom waypoints. (pasky)
celestar
parents: 438
diff changeset
  1443
				 * do that for custom ground sprites and never
aaf1f59a8957 (svn r657) Do not touch 0x8000 bit in sprite index for custom waypoints. (pasky)
celestar
parents: 438
diff changeset
  1444
				 * for station sprites. And in the drawing
aaf1f59a8957 (svn r657) Do not touch 0x8000 bit in sprite index for custom waypoints. (pasky)
celestar
parents: 438
diff changeset
  1445
				 * code, it is used to indicate that the sprite
aaf1f59a8957 (svn r657) Do not touch 0x8000 bit in sprite index for custom waypoints. (pasky)
celestar
parents: 438
diff changeset
  1446
				 * should be drawn in company colors, and it's
aaf1f59a8957 (svn r657) Do not touch 0x8000 bit in sprite index for custom waypoints. (pasky)
celestar
parents: 438
diff changeset
  1447
				 * up to the GRF file to decide that. */
aaf1f59a8957 (svn r657) Do not touch 0x8000 bit in sprite index for custom waypoints. (pasky)
celestar
parents: 438
diff changeset
  1448
417
0c63a94557a5 (svn r614) [newgrf] Some minor code fixes
dominik
parents: 415
diff changeset
  1449
				image = cust->ground_sprite;
0c63a94557a5 (svn r614) [newgrf] Some minor code fixes
dominik
parents: 415
diff changeset
  1450
				image += railtype*((image<_custom_sprites_base)?TRACKTYPE_SPRITE_PITCH:1);
415
423de36034b8 (svn r612) [newgrf] Fix: custom waypoints on monorail/maglev are displayed correctly
dominik
parents: 413
diff changeset
  1451
389
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1452
				DrawGroundSprite(image);
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1453
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1454
				foreach_draw_tile_seq(seq, cust->seq) {
403
bfb365c74e80 (svn r600) -newgrf: Relocation offset for custom station sprites is now stored separately, making it possible to show different sprites in waypoint selection dialog (pasky).
darkvater
parents: 399
diff changeset
  1455
					uint32 image = seq->image + relocation;
447
aaf1f59a8957 (svn r657) Do not touch 0x8000 bit in sprite index for custom waypoints. (pasky)
celestar
parents: 438
diff changeset
  1456
					DrawSpecialBuilding(image, 0, ti,
389
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1457
					                    seq->delta_x, seq->delta_y, seq->delta_z,
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1458
					                    seq->width, seq->height, seq->unk);
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1459
				}
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1460
				return;
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1461
			}
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1462
		}
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1463
1399
4503bd2bfb82 (svn r1903) Replace some casts and macro magic with proper typing, similar to r1902
tron
parents: 1363
diff changeset
  1464
		drss = _track_depot_layout_table[type];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1465
1399
4503bd2bfb82 (svn r1903) Replace some casts and macro magic with proper typing, similar to r1902
tron
parents: 1363
diff changeset
  1466
		image = drss++->image;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1467
		if (image & 0x8000) image = (image & 0x7FFF) + tracktype_offs;
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
		// adjust ground tile for desert
283
c1a99d78e5a3 (svn r289) Fix: Checkpoints on snow have correct ground now
dominik
parents: 282
diff changeset
  1470
		// (don't adjust for arctic depots, because snow in depots looks weird)
c1a99d78e5a3 (svn r289) Fix: Checkpoints on snow have correct ground now
dominik
parents: 282
diff changeset
  1471
		if ((_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK)==RAIL_GROUND_ICE_DESERT && (_opt.landscape == LT_DESERT || type>=4))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1472
		{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1473
			if(image!=3981)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1474
				image += 26; // tile with tracks
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1475
			else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1476
				image = 4550; // flat ground
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1477
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1478
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1479
		DrawGroundSprite(image);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1480
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1481
		while ((image=drss->image) != 0) {
389
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1482
			DrawSpecialBuilding(image, type < 4 ? tracktype_offs : 0, ti,
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1483
			                    drss->subcoord_x, drss->subcoord_y, 0,
2d394eeba5e5 (svn r581) -newgrf: Basic support for new stations - only waypoints supported now and only
celestar
parents: 337
diff changeset
  1484
			                    drss->width, drss->height, 0x17);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1485
			drss++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1486
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1487
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1488
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1489
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1490
void DrawTrainDepotSprite(int x, int y, int image, int railtype)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1491
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1492
	uint32 ormod, img;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1493
	const DrawTrackSeqStruct *dtss;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1494
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1495
	/* baseimage */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1496
	railtype *= TRACKTYPE_SPRITE_PITCH;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1497
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1498
	ormod = PLAYER_SPRITE_COLOR(_local_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1499
1399
4503bd2bfb82 (svn r1903) Replace some casts and macro magic with proper typing, similar to r1902
tron
parents: 1363
diff changeset
  1500
	dtss = _track_depot_layout_table[image];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1501
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1502
	x+=33;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1503
	y+=17;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1504
1399
4503bd2bfb82 (svn r1903) Replace some casts and macro magic with proper typing, similar to r1902
tron
parents: 1363
diff changeset
  1505
	img = dtss++->image;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1506
	if (img & 0x8000) img = (img & 0x7FFF) + railtype;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1507
	DrawSprite(img, x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1508
1399
4503bd2bfb82 (svn r1903) Replace some casts and macro magic with proper typing, similar to r1902
tron
parents: 1363
diff changeset
  1509
	for (; dtss->image != 0; dtss++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1510
		Point pt = RemapCoords(dtss->subcoord_x, dtss->subcoord_y, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1511
		image = dtss->image;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1512
		if (image & 0x8000) image |= ormod;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1513
		DrawSprite(image + railtype, x + pt.x, y + pt.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1514
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1515
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1516
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1517
#define NUM_SSD_ENTRY 256
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1518
#define NUM_SSD_STACK 32
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1519
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1520
typedef struct SetSignalsData {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1521
	int cur;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1522
	int cur_stack;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1523
	bool stop;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1524
	bool has_presignal;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1525
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
	// presignal info
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1527
	int presignal_exits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1528
	int presignal_exits_free;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1529
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1530
	// these are used to keep track of the signals that change.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1531
	byte bit[NUM_SSD_ENTRY];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1532
	TileIndex tile[NUM_SSD_ENTRY];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1533
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1534
	// these are used to keep track of the stack that modifies presignals recursively
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1535
	TileIndex next_tile[NUM_SSD_STACK];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1536
	byte next_dir[NUM_SSD_STACK];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1537
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1538
} SetSignalsData;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1539
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  1540
static bool SetSignalsEnumProc(TileIndex tile, SetSignalsData *ssd, int track, uint length, byte *state)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1541
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1542
	// the tile has signals?
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: 1024
diff changeset
  1543
	if (IsTileType(tile, MP_RAILWAY)) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1544
		if (HasSignalOnTrack(tile, TrackdirToTrack(track))) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1545
			if ((_map3_lo[tile] & _signals_table[track]) != 0) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1546
				// yes, add the signal to the list of signals
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1547
				if (ssd->cur != NUM_SSD_ENTRY) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1548
					ssd->tile[ssd->cur] = tile; // remember the tile index
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1549
					ssd->bit[ssd->cur] = track; // and the controlling bit number
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1550
					ssd->cur++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1551
				}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1552
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1553
				// remember if this block has a presignal.
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1554
				ssd->has_presignal |= (_map3_hi[tile]&1);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1555
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1556
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1557
			// is this an exit signal that points out from the segment?
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1558
			if ((_map3_hi[tile]&2) && _map3_lo[tile]&_signals_table_other[track]) {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1559
				ssd->presignal_exits++;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1560
				if ((_map2[tile]&_signals_table_other[track]) != 0)
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1561
					ssd->presignal_exits_free++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
			}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1563
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1564
			return true;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1565
		} else if (IsTileDepotType(tile, TRANSPORT_RAIL))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1566
			return true; // don't look further if the tile is a depot
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1567
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1568
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1569
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1570
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1571
/* Struct to parse data from VehicleFromPos to SignalVehicleCheckProc */
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1572
typedef struct SignalVehicleCheckStruct {
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1573
	TileIndex tile;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1574
	uint track;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1575
} SignalVehicleCheckStruct;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1576
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1577
static void *SignalVehicleCheckProc(Vehicle *v, void *data)
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1578
{
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1579
	SignalVehicleCheckStruct *dest = data;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1580
	TileIndex tile;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1581
1609
b4d11170cbb5 (svn r2113) -Fix: first check if a vehicle is a train, before accessing u.rail
truelight
parents: 1598
diff changeset
  1582
	if (v->type != VEH_Train)
b4d11170cbb5 (svn r2113) -Fix: first check if a vehicle is a train, before accessing u.rail
truelight
parents: 1598
diff changeset
  1583
		return NULL;
b4d11170cbb5 (svn r2113) -Fix: first check if a vehicle is a train, before accessing u.rail
truelight
parents: 1598
diff changeset
  1584
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1585
	/* Find the tile outside the tunnel, for signalling */
1609
b4d11170cbb5 (svn r2113) -Fix: first check if a vehicle is a train, before accessing u.rail
truelight
parents: 1598
diff changeset
  1586
	if (v->u.rail.track == 0x40)
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1587
		tile = GetVehicleOutOfTunnelTile(v);
1609
b4d11170cbb5 (svn r2113) -Fix: first check if a vehicle is a train, before accessing u.rail
truelight
parents: 1598
diff changeset
  1588
	else
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1589
		tile = v->tile;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1590
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1591
	/* Wrong tile, or no train? Not a match */
1609
b4d11170cbb5 (svn r2113) -Fix: first check if a vehicle is a train, before accessing u.rail
truelight
parents: 1598
diff changeset
  1592
	if (tile != dest->tile)
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1593
		return NULL;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1594
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1595
	/* Are we on the same piece of track? */
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1596
	if (dest->track & (v->u.rail.track + (v->u.rail.track<<8)))
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1597
		return v;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1598
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1599
	return NULL;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1600
}
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1601
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1602
/* Special check for SetSignalsAfterProc, to see if there is a vehicle on this tile */
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1603
bool SignalVehicleCheck(TileIndex tile, uint track)
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1604
{
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1605
	SignalVehicleCheckStruct dest;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1606
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1607
	dest.tile = tile;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1608
	dest.track = track;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1609
1818
88ab405a9dfc (svn r2322) - CodeChange: doxygen commented the tunnel-crash fix. Also if (bla) {return;} else {return;} is not proper code. It's if (bla) {return;} return; ;)
Darkvater
parents: 1816
diff changeset
  1610
	/** @todo "Hackish" fix for the tunnel problems. This is needed because a tunnel
88ab405a9dfc (svn r2322) - CodeChange: doxygen commented the tunnel-crash fix. Also if (bla) {return;} else {return;} is not proper code. It's if (bla) {return;} return; ;)
Darkvater
parents: 1816
diff changeset
  1611
	 * is some kind of invisible black hole, and there is some special magic going
88ab405a9dfc (svn r2322) - CodeChange: doxygen commented the tunnel-crash fix. Also if (bla) {return;} else {return;} is not proper code. It's if (bla) {return;} return; ;)
Darkvater
parents: 1816
diff changeset
  1612
	 * on in there. This 'workaround' can be removed once the maprewrite is done.
88ab405a9dfc (svn r2322) - CodeChange: doxygen commented the tunnel-crash fix. Also if (bla) {return;} else {return;} is not proper code. It's if (bla) {return;} return; ;)
Darkvater
parents: 1816
diff changeset
  1613
	 */
1816
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1614
	if (GetTileType(tile)==MP_TUNNELBRIDGE && ((_map5[tile] & 0xF0)==0)) {
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1615
		// It is a tunnel we're checking, we need to do some special stuff
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1616
		// because VehicleFromPos will not find the vihicle otherwise
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1617
		byte direction = _map5[tile] & 3;
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1618
		FindLengthOfTunnelResult flotr;
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1619
		flotr = FindLengthOfTunnel(tile, direction);
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1620
		dest.track = 1 << (direction & 1); // get the trackbit the vehicle would have if it has not entered the tunnel yet (ie is still visible)
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1621
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1622
		// check for a vehicle with that trackdir on the start tile of the tunnel
1818
88ab405a9dfc (svn r2322) - CodeChange: doxygen commented the tunnel-crash fix. Also if (bla) {return;} else {return;} is not proper code. It's if (bla) {return;} return; ;)
Darkvater
parents: 1816
diff changeset
  1623
		if (VehicleFromPos(tile, &dest, SignalVehicleCheckProc) != NULL) return true;
1816
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1624
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1625
		// check for a vehicle with that trackdir on the end tile of the tunnel
1818
88ab405a9dfc (svn r2322) - CodeChange: doxygen commented the tunnel-crash fix. Also if (bla) {return;} else {return;} is not proper code. It's if (bla) {return;} return; ;)
Darkvater
parents: 1816
diff changeset
  1626
		if (VehicleFromPos(flotr.tile, &dest, SignalVehicleCheckProc) != NULL) return true;
1816
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1627
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1628
		// now check all tiles from start to end for a "hidden" vehicle
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1629
		// NOTE: the hashes for tiles may overlap, so this could maybe be optimised a bit by not checking every tile?
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1630
		dest.track = 0x40; // trackbit for vehicles "hidden" inside a tunnel
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1631
		for (; tile != flotr.tile; tile += TileOffsByDir(direction)) {
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1632
			if (VehicleFromPos(tile, &dest, SignalVehicleCheckProc) != NULL)
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1633
				return true;
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1634
		}
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1635
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1636
		// no vehicle found
e96c4a8136bf (svn r2320) - Fix: [ 1185176 ] Train in tunnel is not properly detected by signal code (Hackykid)
matthijs
parents: 1813
diff changeset
  1637
		return false;
1818
88ab405a9dfc (svn r2322) - CodeChange: doxygen commented the tunnel-crash fix. Also if (bla) {return;} else {return;} is not proper code. It's if (bla) {return;} return; ;)
Darkvater
parents: 1816
diff changeset
  1638
	}
88ab405a9dfc (svn r2322) - CodeChange: doxygen commented the tunnel-crash fix. Also if (bla) {return;} else {return;} is not proper code. It's if (bla) {return;} return; ;)
Darkvater
parents: 1816
diff changeset
  1639
88ab405a9dfc (svn r2322) - CodeChange: doxygen commented the tunnel-crash fix. Also if (bla) {return;} else {return;} is not proper code. It's if (bla) {return;} return; ;)
Darkvater
parents: 1816
diff changeset
  1640
	return VehicleFromPos(tile, &dest, SignalVehicleCheckProc) != NULL;
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1641
}
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1642
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1643
static void SetSignalsAfterProc(TrackPathFinder *tpf)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1644
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1645
	SetSignalsData *ssd = tpf->userdata;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1646
	TrackPathFinderLink *link;
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1647
	uint offs;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1648
	uint i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1649
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1650
	ssd->stop = false;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1651
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1652
	/* Go through all the PF tiles */
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1653
	for (i = 0; i < lengthof(tpf->hash_head); i++) {
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1654
		/* Empty hash item */
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1655
		if (tpf->hash_head[i] == 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1656
			continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1657
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1658
		/* If 0x8000 is not set, there is only 1 item */
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1659
		if (!(tpf->hash_head[i] & 0x8000)) {
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1660
			/* Check if there is a vehicle on this tile */
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1661
			if (SignalVehicleCheck(tpf->hash_tile[i], tpf->hash_head[i])) {
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1662
				ssd->stop = true;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1663
				return;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1664
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1665
		} else {
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1666
			/* There are multiple items, where hash_tile points to the first item in the list */
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1667
			offs = tpf->hash_tile[i];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1668
			do {
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1669
				/* Find the next item */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1670
				link = PATHFIND_GET_LINK_PTR(tpf, offs);
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1671
				/* Check if there is a vehicle on this tile */
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1672
				if (SignalVehicleCheck(link->tile, link->flags)) {
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1673
					ssd->stop = true;
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1674
					return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1675
				}
1555
c7e5bb32f536 (svn r2059) -Codechange: rewrote SetSignalsAfterProc so now the tiles from the PF
truelight
parents: 1542
diff changeset
  1676
				/* Goto the next item */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1677
			} while ((offs=link->next) != 0xFFFF);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1678
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1679
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1680
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1681
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1682
static const byte _dir_from_track[14] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1683
	0,1,0,1,2,1, 0,0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1684
	2,3,3,2,3,0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1685
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1686
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1687
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1688
static void ChangeSignalStates(SetSignalsData *ssd)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1689
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1690
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1691
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1692
	// thinking about presignals...
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1693
	// the presignal is green if,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1694
	//   if no train is in the segment AND
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1695
	//   there is at least one green exit signal OR
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1696
	//   there are no exit signals in the segment
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1697
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1698
	// then mark the signals in the segment accordingly
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1699
	for(i=0; i!=ssd->cur; i++) {
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  1700
		TileIndex tile = ssd->tile[i];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1701
		byte bit = _signals_table[ssd->bit[i]];
817
4f9377b7fd2b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 679
diff changeset
  1702
		uint16 m2 = _map2[tile];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1703
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1704
		// presignals don't turn green if there is at least one presignal exit and none are free
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1705
		if (_map3_hi[tile] & 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1706
			int ex = ssd->presignal_exits, exfree = ssd->presignal_exits_free;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1707
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1708
			// subtract for dual combo signals so they don't count themselves
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1709
			if (_map3_hi[tile]&2 && _map3_lo[tile]&_signals_table_other[ssd->bit[i]]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1710
				ex--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1711
				if ((_map2[tile]&_signals_table_other[ssd->bit[i]]) != 0) exfree--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1712
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1713
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1714
			// if we have exits and none are free, make red.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1715
			if (ex && !exfree) goto make_red;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1716
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1717
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1718
		// check if the signal is unaffected.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1719
		if (ssd->stop) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1720
make_red:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1721
			// turn red
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1722
			if ( (bit&m2) == 0 )
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1723
				continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1724
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1725
			// turn green
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1726
			if ( (bit&m2) != 0 )
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1727
				continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1728
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1729
1813
a4b17980b9e4 (svn r2317) - Fix: [ 1193048 ] Pre-signal stays red when there is only a single exit signal (dinno)
Darkvater
parents: 1796
diff changeset
  1730
		/* Update signals on the other side of this exit-combo signal; it changed. */
a4b17980b9e4 (svn r2317) - Fix: [ 1193048 ] Pre-signal stays red when there is only a single exit signal (dinno)
Darkvater
parents: 1796
diff changeset
  1731
		if (_map3_hi[tile] & 2 ) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1732
			if (ssd->cur_stack != NUM_SSD_STACK) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1733
				ssd->next_tile[ssd->cur_stack] = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1734
				ssd->next_dir[ssd->cur_stack] = _dir_from_track[ssd->bit[i]];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1735
				ssd->cur_stack++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1736
			} else {
1813
a4b17980b9e4 (svn r2317) - Fix: [ 1193048 ] Pre-signal stays red when there is only a single exit signal (dinno)
Darkvater
parents: 1796
diff changeset
  1737
				printf("NUM_SSD_STACK too small\n"); /// @todo WTF is this???
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1738
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1739
		}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1740
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1741
		// it changed, so toggle it
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1742
		_map2[tile] = m2 ^ bit;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1743
		MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1744
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1745
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1746
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1747
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  1748
bool UpdateSignalsOnSegment(TileIndex tile, byte direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1749
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1750
	SetSignalsData ssd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1751
	int result = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1752
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1753
	ssd.cur_stack = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1754
	direction>>=1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1755
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1756
	for(;;) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1757
		// go through one segment and update all signals pointing into that segment.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1758
		ssd.cur = ssd.presignal_exits = ssd.presignal_exits_free = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1759
		ssd.has_presignal = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1760
159
139cf78bfb28 (svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents: 149
diff changeset
  1761
		FollowTrack(tile, 0xC000 | TRANSPORT_RAIL, direction, (TPFEnumProc*)SetSignalsEnumProc, SetSignalsAfterProc, &ssd);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1762
		ChangeSignalStates(&ssd);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1763
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1764
		// remember the result only for the first iteration.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1765
		if (result < 0) result = ssd.stop;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1766
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1767
		// if any exit signals were changed, we need to keep going to modify the stuff behind those.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1768
		if(!ssd.cur_stack)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1769
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1770
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1771
		// one or more exit signals were changed, so we need to update another segment too.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1772
		tile = ssd.next_tile[--ssd.cur_stack];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1773
		direction = ssd.next_dir[ssd.cur_stack];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1774
	}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1775
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1776
	return (bool)result;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1777
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1778
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  1779
void SetSignalsOnBothDir(TileIndex tile, byte track)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1780
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1781
	static const byte _search_dir_1[6] = {1, 3, 1, 3, 5, 3};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1782
	static const byte _search_dir_2[6] = {5, 7, 7, 5, 7, 1};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1783
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1784
	UpdateSignalsOnSegment(tile, _search_dir_1[track]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1785
	UpdateSignalsOnSegment(tile, _search_dir_2[track]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1786
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1787
410
8de2aaf20800 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 408
diff changeset
  1788
static uint GetSlopeZ_Track(TileInfo *ti)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1789
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1790
	uint z = ti->z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1791
	int th = ti->tileh;
13
3e7c1f74a996 (svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents: 0
diff changeset
  1792
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1793
	// check if it's a foundation
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1794
	if (ti->tileh != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1795
		if ((ti->map5 & 0x80) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1796
			uint f = GetRailFoundation(ti->tileh, ti->map5 & 0x3F);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1797
			if (f != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1798
				if (f < 15) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1799
					// leveled foundation
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1800
					return z + 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1801
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1802
				// inclined foundation
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1803
				th = _inclined_tileh[f - 15];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1804
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1805
		} else if ((ti->map5 & 0xC0) == 0xC0) {
395
4c990f33dab7 (svn r587) -newgrf: Rename all /Checkpoint/i tokens to 'Waypoint's. The name actually makes some sense and is also compatible with TTDPatch (pasky).
darkvater
parents: 393
diff changeset
  1806
			// depot or waypoint
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1807
			return z + 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1808
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1809
		return GetPartialZ(ti->x&0xF, ti->y&0xF, th) + z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1810
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1811
	return z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1812
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1813
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
  1814
static uint GetSlopeTileh_Track(TileInfo *ti)
39
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1815
{
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1816
	// check if it's a foundation
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1817
	if (ti->tileh != 0) {
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1818
		if ((ti->map5 & 0x80) == 0) {
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1819
			uint f = GetRailFoundation(ti->tileh, ti->map5 & 0x3F);
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1820
			if (f != 0) {
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1821
				if (f < 15) {
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1822
					// leveled foundation
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1823
					return 0;
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1824
				}
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1825
				// inclined foundation
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1826
				return _inclined_tileh[f - 15];
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1827
			}
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1828
		} else if ((ti->map5 & 0xC0) == 0xC0) {
395
4c990f33dab7 (svn r587) -newgrf: Rename all /Checkpoint/i tokens to 'Waypoint's. The name actually makes some sense and is also compatible with TTDPatch (pasky).
darkvater
parents: 393
diff changeset
  1829
			// depot or waypoint
39
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1830
			return 0;
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1831
		}
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1832
	}
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1833
	return ti->tileh;
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1834
}
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  1835
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  1836
static void GetAcceptedCargo_Track(TileIndex tile, AcceptedCargo ac)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1837
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1838
	/* not used */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1839
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1840
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  1841
static void AnimateTile_Track(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1842
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1843
	/* not used */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1844
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1845
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  1846
static void TileLoop_Track(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1847
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1848
	byte a2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1849
	byte rail;
817
4f9377b7fd2b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 679
diff changeset
  1850
	uint16 m2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1851
	byte owner;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1852
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1853
	m2 = _map2[tile] & 0xF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1854
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1855
	/* special code for alps landscape */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1856
	if (_opt.landscape == LT_HILLY) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1857
		/* convert into snow? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1858
		if (GetTileZ(tile) > _opt.snow_line) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1859
			a2 = RAIL_GROUND_ICE_DESERT;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1860
			goto modify_me;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1861
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1862
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1863
	/* special code for desert landscape */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1864
	} else if (_opt.landscape == LT_DESERT) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1865
		/* convert into desert? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1866
		if (GetMapExtraBits(tile) == 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1867
			a2 = RAIL_GROUND_ICE_DESERT;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1868
			goto modify_me;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1869
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1870
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1871
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1872
	// Don't continue tile loop for depots
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1873
	if (_map5[tile] & RAIL_TYPE_SPECIAL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1874
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1875
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1876
	a2 = RAIL_GROUND_GREEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1877
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1878
	if (m2 != RAIL_GROUND_BROWN) { /* wait until bottom is green */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1879
		/* determine direction of fence */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1880
		rail = _map5[tile] & TRACK_BIT_MASK;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1881
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1882
		if (rail == TRACK_BIT_UPPER) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1883
			a2 = RAIL_GROUND_FENCE_HORIZ1;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1884
		} else if (rail == TRACK_BIT_LOWER) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1885
			a2 = RAIL_GROUND_FENCE_HORIZ2;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1886
		} else if (rail == TRACK_BIT_LEFT) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1887
			a2 = RAIL_GROUND_FENCE_VERT1;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1888
		} else if (rail == TRACK_BIT_RIGHT) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1889
			a2 = RAIL_GROUND_FENCE_VERT2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1890
		} else {
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
  1891
			owner = GetTileOwner(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1892
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1893
			if ( (!(rail&(TRACK_BIT_DIAG2|TRACK_BIT_UPPER|TRACK_BIT_LEFT)) && (rail&TRACK_BIT_DIAG1)) || rail==(TRACK_BIT_LOWER|TRACK_BIT_RIGHT)) {
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: 1024
diff changeset
  1894
				if (!IsTileType(tile + TILE_XY(0,-1), MP_RAILWAY) ||
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
  1895
						!IsTileOwner(tile + TILE_XY(0, -1), owner) ||
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1896
						(_map5[tile + TILE_XY(0,-1)]==TRACK_BIT_UPPER || _map5[tile + TILE_XY(0,-1)]==TRACK_BIT_LEFT))
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1897
							a2 = RAIL_GROUND_FENCE_NW;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1898
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1899
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1900
			if ( (!(rail&(TRACK_BIT_DIAG2|TRACK_BIT_LOWER|TRACK_BIT_RIGHT)) && (rail&TRACK_BIT_DIAG1)) || rail==(TRACK_BIT_UPPER|TRACK_BIT_LEFT)) {
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: 1024
diff changeset
  1901
				if (!IsTileType(tile + TILE_XY(0,1), MP_RAILWAY) ||
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
  1902
						!IsTileOwner(tile + TILE_XY(0, 1), owner) ||
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1903
						(_map5[tile + TILE_XY(0,1)]==TRACK_BIT_LOWER || _map5[tile + TILE_XY(0,1)]==TRACK_BIT_RIGHT))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1904
							a2 = (a2 == RAIL_GROUND_FENCE_NW) ? RAIL_GROUND_FENCE_SENW : RAIL_GROUND_FENCE_SE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1905
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1906
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1907
			if ( (!(rail&(TRACK_BIT_DIAG1|TRACK_BIT_UPPER|TRACK_BIT_RIGHT)) && (rail&TRACK_BIT_DIAG2)) || rail==(TRACK_BIT_LOWER|TRACK_BIT_LEFT)) {
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: 1024
diff changeset
  1908
				if (!IsTileType(tile + TILE_XY(-1,0), MP_RAILWAY) ||
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
  1909
						!IsTileOwner(tile + TILE_XY(-1, 0), owner) ||
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1910
						(_map5[tile + TILE_XY(-1,0)]==TRACK_BIT_UPPER || _map5[tile + TILE_XY(-1,0)]==TRACK_BIT_RIGHT))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1911
							a2 = RAIL_GROUND_FENCE_NE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1912
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1913
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1914
			if ( (!(rail&(TRACK_BIT_DIAG1|TRACK_BIT_LOWER|TRACK_BIT_LEFT)) && (rail&TRACK_BIT_DIAG2)) || rail==(TRACK_BIT_UPPER|TRACK_BIT_RIGHT)) {
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: 1024
diff changeset
  1915
				if (!IsTileType(tile + TILE_XY(1,0), MP_RAILWAY) ||
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
  1916
						!IsTileOwner(tile + TILE_XY(1, 0), owner) ||
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1917
						(_map5[tile + TILE_XY(1,0)]==TRACK_BIT_LOWER || _map5[tile + TILE_XY(1,0)]==TRACK_BIT_LEFT))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1918
							a2 = (a2 == RAIL_GROUND_FENCE_NE) ? RAIL_GROUND_FENCE_NESW : RAIL_GROUND_FENCE_SW;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1919
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1920
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1921
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1922
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1923
modify_me:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1924
	/* tile changed? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1925
	if ( m2 != a2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1926
		_map2[tile] = (_map2[tile] & ~RAIL_MAP2LO_GROUND_MASK) | a2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1927
		MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1928
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1929
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1930
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1931
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  1932
static uint32 GetTileTrackStatus_Track(TileIndex tile, TransportType mode)
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  1933
{
817
4f9377b7fd2b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 679
diff changeset
  1934
	byte m5, a;
4f9377b7fd2b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 679
diff changeset
  1935
	uint16 b;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1936
	uint32 ret;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1937
159
139cf78bfb28 (svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents: 149
diff changeset
  1938
	if (mode != TRANSPORT_RAIL)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1939
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1940
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1941
	m5 = _map5[tile];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1942
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1943
	if (!(m5 & RAIL_TYPE_SPECIAL)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1944
		ret = (m5 | (m5 << 8)) & 0x3F3F;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1945
		if (!(m5 & RAIL_TYPE_SIGNALS)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1946
			if ( (ret & 0xFF) == 3)
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
  1947
			/* Diagonal crossing? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1948
				ret |= 0x40;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1949
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1950
			/* has_signals */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1951
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1952
			a = _map3_lo[tile];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1953
			b = _map2[tile];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1954
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1955
			b &= a;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1956
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
  1957
			/* When signals are not present (in neither
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
  1958
			 * direction), we pretend them to be green. (So if
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
  1959
			 * signals are only one way, the other way will
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 13
diff changeset
  1960
			 * implicitely become `red' */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1961
			if ((a & 0xC0) == 0) { b |= 0xC0; }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1962
			if ((a & 0x30) == 0) { b |= 0x30; }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1963
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1964
			if ( (b & 0x80) == 0)	ret |= 0x10070000;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1965
			if ( (b & 0x40) == 0)	ret |= 0x7100000;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1966
			if ( (b & 0x20) == 0)	ret |= 0x20080000;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1967
			if ( (b & 0x10) == 0)	ret |= 0x8200000;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1968
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1969
	} else if (m5 & 0x40) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1970
		static const byte _train_spec_tracks[6] = {1,2,1,2,1,2};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1971
		m5 = _train_spec_tracks[m5 & 0x3F];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1972
		ret = (m5 << 8) + m5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1973
	} else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1974
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1975
	return ret;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1976
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1977
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  1978
static void ClickTile_Track(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1979
{
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1980
	if (IsTileDepotType(tile, TRANSPORT_RAIL))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1981
		ShowTrainDepotWindow(tile);
1535
a5f686a20d2c (svn r2039) IS_RAIL_DEPOT -> IsRailDepot, IS_RAIL_WAYPOINT -> IsRailWaypoint
tron
parents: 1534
diff changeset
  1982
	else if (IsRailWaypoint(_map5[tile]))
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1536
diff changeset
  1983
		ShowRenameWaypointWindow(GetWaypointByTile(tile));
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  1984
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1985
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1986
1590
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  1987
static void GetTileDesc_Track(TileIndex tile, TileDesc *td)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1988
{
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
  1989
	td->owner = GetTileOwner(tile);
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  1990
	switch (GetRailTileType(tile)) {
1590
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  1991
		case RAIL_TYPE_NORMAL:
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1992
			td->str = STR_1021_RAILROAD_TRACK;
1590
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  1993
			break;
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  1994
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  1995
		case RAIL_TYPE_SIGNALS: {
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  1996
			const StringID signal_type[] = {
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  1997
				STR_RAILROAD_TRACK_WITH_NORMAL_SIGNALS,
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  1998
				STR_RAILROAD_TRACK_WITH_PRESIGNALS,
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  1999
				STR_RAILROAD_TRACK_WITH_EXITSIGNALS,
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  2000
				STR_RAILROAD_TRACK_WITH_COMBOSIGNALS
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  2001
			};
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  2002
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  2003
			td->str = signal_type[_map3_hi[tile] & 0x03];
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  2004
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2005
		}
1590
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  2006
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  2007
		case RAIL_TYPE_DEPOT_WAYPOINT:
1590
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  2008
		default:
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  2009
			td->str = ((_map5[tile] & RAIL_SUBTYPE_MASK) == RAIL_SUBTYPE_DEPOT) ?
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  2010
				STR_1023_RAILROAD_TRAIN_DEPOT : STR_LANDINFO_WAYPOINT;
4e39d1da10c7 (svn r2094) In GetTileDesc_Track(): uint -> TileIndex, use enums, if () cascade -> switch
tron
parents: 1555
diff changeset
  2011
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2012
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2013
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2014
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  2015
static void ChangeTileOwner_Track(TileIndex tile, byte old_player, byte new_player)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2016
{
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
  2017
	if (!IsTileOwner(tile, old_player)) return;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  2018
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2019
	if (new_player != 255) {
1902
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1901
diff changeset
  2020
		SetTileOwner(tile, new_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2021
	}	else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2022
		DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2023
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2024
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2025
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2026
static const byte _fractcoords_behind[4] = { 0x8F, 0x8, 0x80, 0xF8 };
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2027
static const byte _fractcoords_enter[4] = { 0x8A, 0x48, 0x84, 0xA8 };
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2028
static const byte _deltacoord_leaveoffset[8] = {
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2029
	-1,  0,  1,  0, /* x */
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2030
	 0,  1,  0, -1  /* y */
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2031
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2032
static const byte _enter_directions[4] = {5, 7, 1, 3};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2033
static const byte _leave_directions[4] = {1, 3, 5, 7};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2034
static const byte _depot_track_mask[4] = {1, 2, 1, 2};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2035
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1963
diff changeset
  2036
static uint32 VehicleEnter_Track(Vehicle *v, TileIndex tile, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2037
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2038
	byte fract_coord;
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2039
	byte fract_coord_leave;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2040
	int dir;
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2041
	int length;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  2042
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2043
	// this routine applies only to trains in depot tiles
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  2044
	if (v->type != VEH_Train || !IsTileDepotType(tile, TRANSPORT_RAIL))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2045
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2046
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2047
	/* depot direction */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1928
diff changeset
  2048
	dir = GetDepotDirection(tile, TRANSPORT_RAIL);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 159
diff changeset
  2049
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2050
	/* calculate the point where the following wagon should be activated */
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2051
	/* this depends on the length of the current vehicle */
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2052
	length = v->u.rail.cached_veh_length;
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2053
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2054
	fract_coord_leave =
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2055
		((_fractcoords_enter[dir] & 0x0F) +				// x
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2056
			(length + 1) * _deltacoord_leaveoffset[dir]) +
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2057
		(((_fractcoords_enter[dir] >> 4) +				// y
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2058
			((length + 1) * _deltacoord_leaveoffset[dir+4])) << 4);
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2059
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2060
	fract_coord = (x & 0xF) + ((y & 0xF) << 4);
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2061
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2062
	if (_fractcoords_behind[dir] == fract_coord) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2063
		/* make sure a train is not entering the tile from behind */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2064
		return 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2065
	} else if (_fractcoords_enter[dir] == fract_coord) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2066
		if (_enter_directions[dir] == v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2067
			/* enter the depot */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2068
			v->u.rail.track = 0x80,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2069
			v->vehstatus |= VS_HIDDEN; /* hide it */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2070
			v->direction ^= 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2071
			if (v->next == NULL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2072
				TrainEnterDepot(v, tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2073
			v->tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2074
			InvalidateWindow(WC_VEHICLE_DEPOT, tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2075
			return 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2076
		}
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1903
diff changeset
  2077
	} else if (fract_coord_leave == fract_coord) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2078
		if (_leave_directions[dir] == v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2079
			/* leave the depot? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2080
			if ((v=v->next) != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2081
				v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2082
				v->u.rail.track = _depot_track_mask[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: 1328
diff changeset
  2083
				assert(v->u.rail.track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2084
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2085
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2086
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2087
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2088
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2089
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2090
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1084
diff changeset
  2091
void InitializeRail(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2092
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2093
	_last_built_train_depot_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2094
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2095
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2096
const TileTypeProcs _tile_type_rail_procs = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2097
	DrawTile_Track,						/* draw_tile_proc */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2098
	GetSlopeZ_Track,					/* get_slope_z_proc */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2099
	ClearTile_Track,					/* clear_tile_proc */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2100
	GetAcceptedCargo_Track,		/* get_accepted_cargo_proc */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2101
	GetTileDesc_Track,				/* get_tile_desc_proc */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2102
	GetTileTrackStatus_Track,	/* get_tile_track_status_proc */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2103
	ClickTile_Track,					/* click_tile_proc */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2104
	AnimateTile_Track,				/* animate_tile_proc */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2105
	TileLoop_Track,						/* tile_loop_clear */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2106
	ChangeTileOwner_Track,		/* change_tile_owner_clear */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2107
	NULL,											/* get_produced_cargo_proc */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2108
	VehicleEnter_Track,				/* vehicle_enter_tile_proc */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2109
	NULL,											/* vehicle_leave_tile_proc */
39
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 22
diff changeset
  2110
	GetSlopeTileh_Track,			/* get_slope_tileh_proc */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2111
};