changeset 1922:71fe89d907bf draft

(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it. - Codechange: Remove some magic numbers (PALETTE_CRASH)
author hackykid <hackykid@openttd.org>
date Mon, 06 Jun 2005 22:44:11 +0000
parents 828d36e8c6f7
children 03586fd053ab
files aircraft_gui.c engine.h newgrf.c rail_cmd.c roadveh_gui.c table/engines.h train_cmd.c train_gui.c vehicle.c vehicle.h
diffstat 10 files changed, 254 insertions(+), 149 deletions(-) [+]
line wrap: on
line diff
--- a/aircraft_gui.c
+++ b/aircraft_gui.c
@@ -1,6 +1,7 @@
 #include "stdafx.h"
 #include "openttd.h"
 #include "debug.h"
+#include "table/sprites.h"
 #include "table/strings.h"
 #include "map.h"
 #include "tile.h"
@@ -60,7 +61,8 @@
 {
 	int image = GetAircraftImage(v, 6);
 	uint32 ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner));
-	if (v->vehstatus & VS_CRASHED) ormod = 0x3248000;
+	if (v->vehstatus & VS_CRASHED)
+		ormod = PALETTE_CRASH;
 	DrawSprite(image | ormod, x+25, y+10);
 	if (v->subtype == 0)
 		DrawSprite(0xF3D, x+25, y+5);
--- a/engine.h
+++ b/engine.h
@@ -21,6 +21,7 @@
 	                    //       for when the 'powered wagon' callback fails. But it should really also determine what
 	                    //       kind of visual effect to generate for a vehicle (default, steam, diesel, electric).
 	                    //       Same goes for the callback result, which atm is only used to check if a wagon is powered.
+	byte shorten_factor;	// length on main map for this type is 8 - shorten_factor
 } RailVehicleInfo;
 
 typedef struct ShipVehicleInfo {
@@ -136,6 +137,10 @@
 	// TODO: interpret the rest of the result, aka "visual effects"
 	CBID_WAGON_POWER = 0x10,
 
+	// Vehicle length, returns the amount of 1/8's the vehicle is shorter
+	// only for train vehicles
+	CBID_VEH_LENGTH = 0x11,
+
 	// Refit capacity, the passed vehicle needs to have its ->cargo_type set to
 	// the cargo we are refitting to, returns the new cargo capacity
 	CBID_REFIT_CAP = 0x15,
@@ -145,6 +150,7 @@
 // (some callbacks are always used, and dont appear here)
 enum CallbackMask {
 	CBM_WAGON_POWER = 0,
+	CBM_VEH_LENGTH = 1,
 	CBM_REFIT_CAP = 3,
 };
 
--- a/newgrf.c
+++ b/newgrf.c
@@ -353,9 +353,17 @@
 		case 0x1E: { /* Callback */
 			FOR_EACH_OBJECT {
 				byte callbacks = grf_load_byte(&buf);
+
 				rvi[i].callbackmask = callbacks;
 			}
 		} break;
+		case 0x21: { /* Shorter vehicle */
+			FOR_EACH_OBJECT {
+				byte shorten_factor = grf_load_byte(&buf);
+
+				rvi[i].shorten_factor = shorten_factor;
+			}
+		} break;
 		case 0x22: { /* Visual effect */
 			// see note in engine.h about rvi->visual_effect
 			FOR_EACH_OBJECT {
@@ -377,7 +385,6 @@
 		case 0x1C:	/* Refit cost */
 		case 0x1F:	/* Tractive effort */
 		case 0x20:	/* Air drag */
-		case 0x21:	/* Shorter tenders */
 		case 0x24:	/* High byte of vehicle weight */
 		case 0x25:	/* User-defined bit mask to set when checking veh. var. 42 */
 		case 0x26:	/* Retire vehicle early */
--- a/rail_cmd.c
+++ b/rail_cmd.c
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "openttd.h"
+#include "debug.h"
 #include "table/sprites.h"
 #include "table/strings.h"
 #include "gfx.h"
@@ -9,6 +10,7 @@
 #include "viewport.h"
 #include "command.h"
 #include "pathfind.h"
+#include "engine.h"
 #include "town.h"
 #include "sound.h"
 #include "station.h"
@@ -2036,7 +2038,10 @@
 
 static const byte _fractcoords_behind[4] = { 0x8F, 0x8, 0x80, 0xF8 };
 static const byte _fractcoords_enter[4] = { 0x8A, 0x48, 0x84, 0xA8 };
-static const byte _fractcoords_leave[4] = { 0x81, 0xD8, 0x8D, 0x18 };
+static const byte _deltacoord_leaveoffset[8] = {
+	-1,  0,  1,  0, /* x */
+	 0,  1,  0, -1  /* y */
+};
 static const byte _enter_directions[4] = {5, 7, 1, 3};
 static const byte _leave_directions[4] = {1, 3, 5, 7};
 static const byte _depot_track_mask[4] = {1, 2, 1, 2};
@@ -2044,7 +2049,9 @@
 static uint32 VehicleEnter_Track(Vehicle *v, uint tile, int x, int y)
 {
 	byte fract_coord;
+	byte fract_coord_leave;
 	int dir;
+	int length;
 
 	// this routine applies only to trains in depot tiles
 	if (v->type != VEH_Train || !IsRailDepot(_map5[tile]))
@@ -2053,7 +2060,18 @@
 	/* depot direction */
 	dir = _map5[tile] & RAIL_DEPOT_DIR;
 
+	/* calculate the point where the following wagon should be activated */
+	/* this depends on the length of the current vehicle */
+	length = v->u.rail.cached_veh_length;
+
+	fract_coord_leave =
+		((_fractcoords_enter[dir] & 0x0F) +				// x
+			(length + 1) * _deltacoord_leaveoffset[dir]) +
+		(((_fractcoords_enter[dir] >> 4) +				// y
+			((length + 1) * _deltacoord_leaveoffset[dir+4])) << 4);
+
 	fract_coord = (x & 0xF) + ((y & 0xF) << 4);
+
 	if (_fractcoords_behind[dir] == fract_coord) {
 		/* make sure a train is not entering the tile from behind */
 		return 8;
@@ -2069,7 +2087,7 @@
 			InvalidateWindow(WC_VEHICLE_DEPOT, tile);
 			return 4;
 		}
-	} else if (_fractcoords_leave[dir] == fract_coord) {
+	} else if (fract_coord_leave == fract_coord) {
 		if (_leave_directions[dir] == v->direction) {
 			/* leave the depot? */
 			if ((v=v->next) != NULL) {
--- a/roadveh_gui.c
+++ b/roadveh_gui.c
@@ -1,6 +1,7 @@
 #include "stdafx.h"
 #include "openttd.h"
 #include "debug.h"
+#include "table/sprites.h"
 #include "table/strings.h"
 #include "map.h"
 #include "window.h"
@@ -60,7 +61,8 @@
 {
 	int image = GetRoadVehImage(v, 6);
 	uint32 ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner));
-	if (v->vehstatus & VS_CRASHED) ormod = 0x3248000;
+	if (v->vehstatus & VS_CRASHED)
+		ormod = PALETTE_CRASH;
 	DrawSprite(image | ormod, x+14, y+6);
 
 	if (v->index == selection) {
--- a/table/engines.h
+++ b/table/engines.h
@@ -266,127 +266,127 @@
 };
 
 RailVehicleInfo _rail_vehicle_info[NUM_TRAIN_ENGINES] = {
-	// image_index  max_speed (kph)      running_cost_base                callback bitmask
-	// |  flags     |        power (hp)  |    running_cost_class          |  powered wagons power
-	// |  |    base_cost     |    weight      |    capacity               |  |  powered wagons weight
-	// |  |    |    |        |    |      |    |    |    cargo_type        |  |  |  visual effects
-	// |  |    |    |        |    |      |    |    |    |                 |  |  |  |
-	{  2, 0,   7,  64,     300,  47,    50,   0,   0,   0               , 0, 0, 0, 0 }, /*   0 */
-	{ 19, 0,   8,  80,     600,  65,    65,   1,   0,   0               , 0, 0, 0, 0 }, /*   1 */
-	{  2, 0,  10,  72,     400,  85,    90,   0,   0,   0               , 0, 0, 0, 0 }, /*   2 */
-	{  0, 0,  15,  96,     900, 130,   130,   0,   0,   0               , 0, 0, 0, 0 }, /*   3 */
-	{  1, 0,  19, 112,    1000, 140,   145,   0,   0,   0               , 0, 0, 0, 0 }, /*   4 */
-	{ 12, 0,  16, 120,    1400,  95,   125,   1,   0,   0               , 0, 0, 0, 0 }, /*   5 */
-	{ 14, 0,  20, 152,    2000, 120,   135,   1,   0,   0               , 0, 0, 0, 0 }, /*   6 */
-	{  3, 0,  14,  88,    1100, 145,   130,   0,   0,   0               , 0, 0, 0, 0 }, /*   7 */
-	{  0, 0,  13, 112,    1000, 131,   120,   0,   0,   0               , 0, 0, 0, 0 }, /*   8 */
-	{  1, 0,  19, 128,    1200, 162,   140,   0,   0,   0               , 0, 0, 0, 0 }, /*   9 */
-	{  0, 0,  22, 144,    1600, 170,   130,   0,   0,   0               , 0, 0, 0, 0 }, /*  10 */
-	{  8, 1,  11, 112,   600/2,32/2,  85/2,   1,  38,   CT_PASSENGERS   , 0, 0, 0, 0 }, /*  11 */
-	{ 10, 1,  14, 120,   700/2,38/2,  70/2,   1,  40,   CT_PASSENGERS   , 0, 0, 0, 0 }, /*  12 */
-	{  4, 0,  15, 128,    1250,  72,    95,   1,   0,   0               , 0, 0, 0, 0 }, /*  13 */
-	{  5, 0,  17, 144,    1750, 101,   120,   1,   0,   0               , 0, 0, 0, 0 }, /*  14 */
-	{  4, 0,  18, 160,    2580, 112,   140,   1,   0,   0               , 0, 0, 0, 0 }, /*  15 */
-	{ 14, 0,  23,  96,    4000, 150,   135,   1,   0,   0               , 0, 0, 0, 0 }, /*  16 */
-	{ 12, 0,  16, 112,    2400, 120,   105,   1,   0,   0               , 0, 0, 0, 0 }, /*  17 */
-	{ 13, 0,  30, 112,    6600, 207,   155,   1,   0,   0               , 0, 0, 0, 0 }, /*  18 */
-	{ 15, 0,  18, 104,    1500, 110,   105,   1,   0,   0               , 0, 0, 0, 0 }, /*  19 */
-	{ 16, 1,  35, 160,  3500/2,95/2, 205/2,   1,   0,   0               , 0, 0, 0, 0 }, /*  20 */
-	{ 18, 0,  21, 104,    2200, 120,   145,   1,   0,   0               , 0, 0, 0, 0 }, /*  21 */
-	{  6, 1,  20, 200,  4500/2,70/2, 190/2,   1,   4,   CT_MAIL         , 0, 0, 0, 0 }, /*  22 */
-	{ 20, 0,  26, 160,    3600,  84,   180,   2,   0,   0               , 0, 0, 0, 0 }, /*  23 */
-	{ 20, 0,  30, 176,    5000,  82,   205,   2,   0,   0               , 0, 0, 0, 0 }, /*  24 */
-	{ 21, 1,  40, 240,  7000/2,90/2, 240/2,   2,   0,   0               , 0, 0, 0, 0 }, /*  25 */
-	{ 23, 1,  43, 264,  8000/2,95/2, 250/2,   2,   0,   0               , 0, 0, 0, 0 }, /*  26 */
-	{ 33, 2, 247,   0,       0,  25,     0,   0,  40,   CT_PASSENGERS   , 0, 0, 0, 0 }, /*  27 */
-	{ 35, 2, 228,   0,       0,  21,     0,   0,  30,   CT_MAIL         , 0, 0, 0, 0 }, /*  28 */
-	{ 34, 2, 176,   0,       0,  18,     0,   0,  30,   CT_COAL         , 0, 0, 0, 0 }, /*  29 */
-	{ 36, 2, 200,   0,       0,  24,     0,   0,  30,   CT_OIL          , 0, 0, 0, 0 }, /*  30 */
-	{ 37, 2, 192,   0,       0,  20,     0,   0,  25,   CT_LIVESTOCK    , 0, 0, 0, 0 }, /*  31 */
-	{ 38, 2, 190,   0,       0,  21,     0,   0,  25,   CT_GOODS        , 0, 0, 0, 0 }, /*  32 */
-	{ 39, 2, 182,   0,       0,  19,     0,   0,  30,   CT_GRAIN        , 0, 0, 0, 0 }, /*  33 */
-	{ 40, 2, 181,   0,       0,  16,     0,   0,  30,   CT_WOOD         , 0, 0, 0, 0 }, /*  34 */
-	{ 41, 2, 179,   0,       0,  19,     0,   0,  30,   CT_IRON_ORE     , 0, 0, 0, 0 }, /*  35 */
-	{ 42, 2, 196,   0,       0,  18,     0,   0,  20,   CT_STEEL        , 0, 0, 0, 0 }, /*  36 */
-	{ 43, 2, 255,   0,       0,  30,     0,   0,  20,   CT_VALUABLES    , 0, 0, 0, 0 }, /*  37 */
-	{ 44, 2, 191,   0,       0,  22,     0,   0,  25,   CT_FOOD         , 0, 0, 0, 0 }, /*  38 */
-	{ 45, 2, 196,   0,       0,  18,     0,   0,  20,   CT_PAPER        , 0, 0, 0, 0 }, /*  39 */
-	{ 46, 2, 179,   0,       0,  19,     0,   0,  30,   CT_COPPER_ORE   , 0, 0, 0, 0 }, /*  40 */
-	{ 47, 2, 199,   0,       0,  25,     0,   0,  25,   CT_WATER        , 0, 0, 0, 0 }, /*  41 */
-	{ 48, 2, 182,   0,       0,  18,     0,   0,  25,   CT_FRUIT        , 0, 0, 0, 0 }, /*  42 */
-	{ 49, 2, 185,   0,       0,  19,     0,   0,  21,   CT_RUBBER       , 0, 0, 0, 0 }, /*  43 */
-	{ 50, 2, 176,   0,       0,  19,     0,   0,  30,   CT_SUGAR        , 0, 0, 0, 0 }, /*  44 */
-	{ 51, 2, 178,   0,       0,  20,     0,   0,  30,   CT_COTTON_CANDY , 0, 0, 0, 0 }, /*  45 */
-	{ 52, 2, 192,   0,       0,  20,     0,   0,  30,   CT_TOFFEE       , 0, 0, 0, 0 }, /*  46 */
-	{ 53, 2, 190,   0,       0,  21,     0,   0,  20,   CT_BUBBLES      , 0, 0, 0, 0 }, /*  47 */
-	{ 54, 2, 182,   0,       0,  24,     0,   0,  25,   CT_COLA         , 0, 0, 0, 0 }, /*  48 */
-	{ 55, 2, 181,   0,       0,  21,     0,   0,  25,   CT_CANDY        , 0, 0, 0, 0 }, /*  49 */
-	{ 56, 2, 183,   0,       0,  21,     0,   0,  20,   CT_TOYS         , 0, 0, 0, 0 }, /*  50 */
-	{ 57, 2, 196,   0,       0,  18,     0,   0,  22,   CT_BATTERIES    , 0, 0, 0, 0 }, /*  51 */
-	{ 58, 2, 193,   0,       0,  18,     0,   0,  25,   CT_FIZZY_DRINKS , 0, 0, 0, 0 }, /*  52 */
-	{ 59, 2, 191,   0,       0,  18,     0,   0,  30,   CT_PLASTIC      , 0, 0, 0, 0 }, /*  53 */
-	{ 25, 0,  52, 304,    9000,  95,   230,   2,   0,   0               , 0, 0, 0, 0 }, /*  54 */
-	{ 26, 1,  60, 336, 10000/2,85/2, 240/2,   2,  25,   CT_PASSENGERS   , 0, 0, 0, 0 }, /*  55 */
-	{ 26, 0,  53, 320,    5000,  95,   230,   2,   0,   0               , 0, 0, 0, 0 }, /*  56 */
-	{ 60, 2, 247,   0,       0,  25,     0,   0,  45,   CT_PASSENGERS   , 0, 0, 0, 0 }, /*  57 */
-	{ 62, 2, 228,   0,       0,  21,     0,   0,  35,   CT_MAIL         , 0, 0, 0, 0 }, /*  58 */
-	{ 61, 2, 176,   0,       0,  18,     0,   0,  35,   CT_COAL         , 0, 0, 0, 0 }, /*  59 */
-	{ 63, 2, 200,   0,       0,  24,     0,   0,  35,   CT_OIL          , 0, 0, 0, 0 }, /*  60 */
-	{ 64, 2, 192,   0,       0,  20,     0,   0,  30,   CT_LIVESTOCK    , 0, 0, 0, 0 }, /*  61 */
-	{ 65, 2, 190,   0,       0,  21,     0,   0,  30,   CT_GOODS        , 0, 0, 0, 0 }, /*  62 */
-	{ 66, 2, 182,   0,       0,  19,     0,   0,  35,   CT_GRAIN        , 0, 0, 0, 0 }, /*  63 */
-	{ 67, 2, 181,   0,       0,  16,     0,   0,  35,   CT_WOOD         , 0, 0, 0, 0 }, /*  64 */
-	{ 68, 2, 179,   0,       0,  19,     0,   0,  35,   CT_IRON_ORE     , 0, 0, 0, 0 }, /*  65 */
-	{ 69, 2, 196,   0,       0,  18,     0,   0,  25,   CT_STEEL        , 0, 0, 0, 0 }, /*  66 */
-	{ 70, 2, 255,   0,       0,  30,     0,   0,  25,   CT_VALUABLES    , 0, 0, 0, 0 }, /*  67 */
-	{ 71, 2, 191,   0,       0,  22,     0,   0,  30,   CT_FOOD         , 0, 0, 0, 0 }, /*  68 */
-	{ 72, 2, 196,   0,       0,  18,     0,   0,  25,   CT_PAPER        , 0, 0, 0, 0 }, /*  69 */
-	{ 73, 2, 179,   0,       0,  19,     0,   0,  35,   CT_COPPER_ORE   , 0, 0, 0, 0 }, /*  70 */
-	{ 47, 2, 199,   0,       0,  25,     0,   0,  30,   CT_WATER        , 0, 0, 0, 0 }, /*  71 */
-	{ 48, 2, 182,   0,       0,  18,     0,   0,  30,   CT_FRUIT        , 0, 0, 0, 0 }, /*  72 */
-	{ 49, 2, 185,   0,       0,  19,     0,   0,  26,   CT_RUBBER       , 0, 0, 0, 0 }, /*  73 */
-	{ 50, 2, 176,   0,       0,  19,     0,   0,  35,   CT_SUGAR        , 0, 0, 0, 0 }, /*  74 */
-	{ 51, 2, 178,   0,       0,  20,     0,   0,  35,   CT_COTTON_CANDY , 0, 0, 0, 0 }, /*  75 */
-	{ 52, 2, 192,   0,       0,  20,     0,   0,  35,   CT_TOFFEE       , 0, 0, 0, 0 }, /*  76 */
-	{ 53, 2, 190,   0,       0,  21,     0,   0,  25,   CT_BUBBLES      , 0, 0, 0, 0 }, /*  77 */
-	{ 54, 2, 182,   0,       0,  24,     0,   0,  30,   CT_COLA         , 0, 0, 0, 0 }, /*  78 */
-	{ 55, 2, 181,   0,       0,  21,     0,   0,  30,   CT_CANDY        , 0, 0, 0, 0 }, /*  79 */
-	{ 56, 2, 183,   0,       0,  21,     0,   0,  25,   CT_TOYS         , 0, 0, 0, 0 }, /*  80 */
-	{ 57, 2, 196,   0,       0,  18,     0,   0,  27,   CT_BATTERIES    , 0, 0, 0, 0 }, /*  81 */
-	{ 58, 2, 193,   0,       0,  18,     0,   0,  30,   CT_FIZZY_DRINKS , 0, 0, 0, 0 }, /*  82 */
-	{ 59, 2, 191,   0,       0,  18,     0,   0,  35,   CT_PLASTIC      , 0, 0, 0, 0 }, /*  83 */
-	{ 28, 0,  70, 400,   10000, 105,   250,   2,   0,   0               , 0, 0, 0, 0 }, /*  84 */
-	{ 29, 0,  74, 448,   12000, 120,   253,   2,   0,   0               , 0, 0, 0, 0 }, /*  85 */
-	{ 30, 0,  82, 480,   15000, 130,   254,   2,   0,   0               , 0, 0, 0, 0 }, /*  86 */
-	{ 31, 1,  95, 640, 20000/2,150/2,255/2,   2,   0,   0               , 0, 0, 0, 0 }, /*  87 */
-	{ 28, 0,  70, 480,   10000, 120,   250,   2,   0,   0               , 0, 0, 0, 0 }, /*  88 */
-	{ 60, 2, 247,   0,       0,  25,     0,   0,  47,   CT_PASSENGERS   , 0, 0, 0, 0 }, /*  89 */
-	{ 62, 2, 228,   0,       0,  21,     0,   0,  37,   CT_MAIL         , 0, 0, 0, 0 }, /*  90 */
-	{ 61, 2, 176,   0,       0,  18,     0,   0,  37,   CT_COAL         , 0, 0, 0, 0 }, /*  91 */
-	{ 63, 2, 200,   0,       0,  24,     0,   0,  37,   CT_OIL          , 0, 0, 0, 0 }, /*  92 */
-	{ 64, 2, 192,   0,       0,  20,     0,   0,  32,   CT_LIVESTOCK    , 0, 0, 0, 0 }, /*  93 */
-	{ 65, 2, 190,   0,       0,  21,     0,   0,  32,   CT_GOODS        , 0, 0, 0, 0 }, /*  94 */
-	{ 66, 2, 182,   0,       0,  19,     0,   0,  37,   CT_GRAIN        , 0, 0, 0, 0 }, /*  95 */
-	{ 67, 2, 181,   0,       0,  16,     0,   0,  37,   CT_WOOD         , 0, 0, 0, 0 }, /*  96 */
-	{ 68, 2, 179,   0,       0,  19,     0,   0,  37,   CT_IRON_ORE     , 0, 0, 0, 0 }, /*  97 */
-	{ 69, 2, 196,   0,       0,  18,     0,   0,  27,   CT_STEEL        , 0, 0, 0, 0 }, /*  98 */
-	{ 70, 2, 255,   0,       0,  30,     0,   0,  27,   CT_VALUABLES    , 0, 0, 0, 0 }, /*  99 */
-	{ 71, 2, 191,   0,       0,  22,     0,   0,  32,   CT_FOOD         , 0, 0, 0, 0 }, /* 100 */
-	{ 72, 2, 196,   0,       0,  18,     0,   0,  27,   CT_PAPER        , 0, 0, 0, 0 }, /* 101 */
-	{ 73, 2, 179,   0,       0,  19,     0,   0,  37,   CT_COPPER_ORE   , 0, 0, 0, 0 }, /* 102 */
-	{ 47, 2, 199,   0,       0,  25,     0,   0,  32,   CT_WATER        , 0, 0, 0, 0 }, /* 103 */
-	{ 48, 2, 182,   0,       0,  18,     0,   0,  32,   CT_FRUIT        , 0, 0, 0, 0 }, /* 104 */
-	{ 49, 2, 185,   0,       0,  19,     0,   0,  28,   CT_RUBBER       , 0, 0, 0, 0 }, /* 105 */
-	{ 50, 2, 176,   0,       0,  19,     0,   0,  37,   CT_SUGAR        , 0, 0, 0, 0 }, /* 106 */
-	{ 51, 2, 178,   0,       0,  20,     0,   0,  37,   CT_COTTON_CANDY , 0, 0, 0, 0 }, /* 107 */
-	{ 52, 2, 192,   0,       0,  20,     0,   0,  37,   CT_TOFFEE       , 0, 0, 0, 0 }, /* 108 */
-	{ 53, 2, 190,   0,       0,  21,     0,   0,  27,   CT_BUBBLES      , 0, 0, 0, 0 }, /* 109 */
-	{ 54, 2, 182,   0,       0,  24,     0,   0,  32,   CT_COLA         , 0, 0, 0, 0 }, /* 110 */
-	{ 55, 2, 181,   0,       0,  21,     0,   0,  32,   CT_CANDY        , 0, 0, 0, 0 }, /* 111 */
-	{ 56, 2, 183,   0,       0,  21,     0,   0,  27,   CT_TOYS         , 0, 0, 0, 0 }, /* 112 */
-	{ 57, 2, 196,   0,       0,  18,     0,   0,  29,   CT_BATTERIES    , 0, 0, 0, 0 }, /* 113 */
-	{ 58, 2, 193,   0,       0,  18,     0,   0,  32,   CT_FIZZY_DRINKS , 0, 0, 0, 0 }, /* 114 */
-	{ 59, 2, 191,   0,       0,  18,     0,   0,  37,   CT_PLASTIC      , 0, 0, 0, 0 }, /* 115 */
+	// image_index  max_speed (kph)      running_cost_base                 callbackmask    shortened factor
+	// |  flags     |        power (hp)  |    running_cost_class           |   powered wagons power
+	// |  |    base_cost     |    weight      |    capacity                |   |   powered wagons weight
+	// |  |    |    |        |    |      |    |    |    cargo_type         |   |   |   visual effects
+	// |  |    |    |        |    |      |    |    |    |                  |   |   |   |   |
+	{  2, 0,   7,  64,     300,  47,    50,   0,   0,   0               ,  0,  0,  0,  0,  0 }, /*   0 */
+	{ 19, 0,   8,  80,     600,  65,    65,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*   1 */
+	{  2, 0,  10,  72,     400,  85,    90,   0,   0,   0               ,  0,  0,  0,  0,  0 }, /*   2 */
+	{  0, 0,  15,  96,     900, 130,   130,   0,   0,   0               ,  0,  0,  0,  0,  0 }, /*   3 */
+	{  1, 0,  19, 112,    1000, 140,   145,   0,   0,   0               ,  0,  0,  0,  0,  0 }, /*   4 */
+	{ 12, 0,  16, 120,    1400,  95,   125,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*   5 */
+	{ 14, 0,  20, 152,    2000, 120,   135,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*   6 */
+	{  3, 0,  14,  88,    1100, 145,   130,   0,   0,   0               ,  0,  0,  0,  0,  0 }, /*   7 */
+	{  0, 0,  13, 112,    1000, 131,   120,   0,   0,   0               ,  0,  0,  0,  0,  0 }, /*   8 */
+	{  1, 0,  19, 128,    1200, 162,   140,   0,   0,   0               ,  0,  0,  0,  0,  0 }, /*   9 */
+	{  0, 0,  22, 144,    1600, 170,   130,   0,   0,   0               ,  0,  0,  0,  0,  0 }, /*  10 */
+	{  8, 1,  11, 112,   600/2,32/2,  85/2,   1,  38,   CT_PASSENGERS   ,  0,  0,  0,  0,  0 }, /*  11 */
+	{ 10, 1,  14, 120,   700/2,38/2,  70/2,   1,  40,   CT_PASSENGERS   ,  0,  0,  0,  0,  0 }, /*  12 */
+	{  4, 0,  15, 128,    1250,  72,    95,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*  13 */
+	{  5, 0,  17, 144,    1750, 101,   120,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*  14 */
+	{  4, 0,  18, 160,    2580, 112,   140,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*  15 */
+	{ 14, 0,  23,  96,    4000, 150,   135,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*  16 */
+	{ 12, 0,  16, 112,    2400, 120,   105,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*  17 */
+	{ 13, 0,  30, 112,    6600, 207,   155,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*  18 */
+	{ 15, 0,  18, 104,    1500, 110,   105,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*  19 */
+	{ 16, 1,  35, 160,  3500/2,95/2, 205/2,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*  20 */
+	{ 18, 0,  21, 104,    2200, 120,   145,   1,   0,   0               ,  0,  0,  0,  0,  0 }, /*  21 */
+	{  6, 1,  20, 200,  4500/2,70/2, 190/2,   1,   4,   CT_MAIL         ,  0,  0,  0,  0,  0 }, /*  22 */
+	{ 20, 0,  26, 160,    3600,  84,   180,   2,   0,   0               ,  0,  0,  0,  0,  0 }, /*  23 */
+	{ 20, 0,  30, 176,    5000,  82,   205,   2,   0,   0               ,  0,  0,  0,  0,  0 }, /*  24 */
+	{ 21, 1,  40, 240,  7000/2,90/2, 240/2,   2,   0,   0               ,  0,  0,  0,  0,  0 }, /*  25 */
+	{ 23, 1,  43, 264,  8000/2,95/2, 250/2,   2,   0,   0               ,  0,  0,  0,  0,  0 }, /*  26 */
+	{ 33, 2, 247,   0,       0,  25,     0,   0,  40,   CT_PASSENGERS   ,  0,  0,  0,  0,  0 }, /*  27 */
+	{ 35, 2, 228,   0,       0,  21,     0,   0,  30,   CT_MAIL         ,  0,  0,  0,  0,  0 }, /*  28 */
+	{ 34, 2, 176,   0,       0,  18,     0,   0,  30,   CT_COAL         ,  0,  0,  0,  0,  0 }, /*  29 */
+	{ 36, 2, 200,   0,       0,  24,     0,   0,  30,   CT_OIL          ,  0,  0,  0,  0,  0 }, /*  30 */
+	{ 37, 2, 192,   0,       0,  20,     0,   0,  25,   CT_LIVESTOCK    ,  0,  0,  0,  0,  0 }, /*  31 */
+	{ 38, 2, 190,   0,       0,  21,     0,   0,  25,   CT_GOODS        ,  0,  0,  0,  0,  0 }, /*  32 */
+	{ 39, 2, 182,   0,       0,  19,     0,   0,  30,   CT_GRAIN        ,  0,  0,  0,  0,  0 }, /*  33 */
+	{ 40, 2, 181,   0,       0,  16,     0,   0,  30,   CT_WOOD         ,  0,  0,  0,  0,  0 }, /*  34 */
+	{ 41, 2, 179,   0,       0,  19,     0,   0,  30,   CT_IRON_ORE     ,  0,  0,  0,  0,  0 }, /*  35 */
+	{ 42, 2, 196,   0,       0,  18,     0,   0,  20,   CT_STEEL        ,  0,  0,  0,  0,  0 }, /*  36 */
+	{ 43, 2, 255,   0,       0,  30,     0,   0,  20,   CT_VALUABLES    ,  0,  0,  0,  0,  0 }, /*  37 */
+	{ 44, 2, 191,   0,       0,  22,     0,   0,  25,   CT_FOOD         ,  0,  0,  0,  0,  0 }, /*  38 */
+	{ 45, 2, 196,   0,       0,  18,     0,   0,  20,   CT_PAPER        ,  0,  0,  0,  0,  0 }, /*  39 */
+	{ 46, 2, 179,   0,       0,  19,     0,   0,  30,   CT_COPPER_ORE   ,  0,  0,  0,  0,  0 }, /*  40 */
+	{ 47, 2, 199,   0,       0,  25,     0,   0,  25,   CT_WATER        ,  0,  0,  0,  0,  0 }, /*  41 */
+	{ 48, 2, 182,   0,       0,  18,     0,   0,  25,   CT_FRUIT        ,  0,  0,  0,  0,  0 }, /*  42 */
+	{ 49, 2, 185,   0,       0,  19,     0,   0,  21,   CT_RUBBER       ,  0,  0,  0,  0,  0 }, /*  43 */
+	{ 50, 2, 176,   0,       0,  19,     0,   0,  30,   CT_SUGAR        ,  0,  0,  0,  0,  0 }, /*  44 */
+	{ 51, 2, 178,   0,       0,  20,     0,   0,  30,   CT_COTTON_CANDY ,  0,  0,  0,  0,  0 }, /*  45 */
+	{ 52, 2, 192,   0,       0,  20,     0,   0,  30,   CT_TOFFEE       ,  0,  0,  0,  0,  0 }, /*  46 */
+	{ 53, 2, 190,   0,       0,  21,     0,   0,  20,   CT_BUBBLES      ,  0,  0,  0,  0,  0 }, /*  47 */
+	{ 54, 2, 182,   0,       0,  24,     0,   0,  25,   CT_COLA         ,  0,  0,  0,  0,  0 }, /*  48 */
+	{ 55, 2, 181,   0,       0,  21,     0,   0,  25,   CT_CANDY        ,  0,  0,  0,  0,  0 }, /*  49 */
+	{ 56, 2, 183,   0,       0,  21,     0,   0,  20,   CT_TOYS         ,  0,  0,  0,  0,  0 }, /*  50 */
+	{ 57, 2, 196,   0,       0,  18,     0,   0,  22,   CT_BATTERIES    ,  0,  0,  0,  0,  0 }, /*  51 */
+	{ 58, 2, 193,   0,       0,  18,     0,   0,  25,   CT_FIZZY_DRINKS ,  0,  0,  0,  0,  0 }, /*  52 */
+	{ 59, 2, 191,   0,       0,  18,     0,   0,  30,   CT_PLASTIC      ,  0,  0,  0,  0,  0 }, /*  53 */
+	{ 25, 0,  52, 304,    9000,  95,   230,   2,   0,   0               ,  0,  0,  0,  0,  0 }, /*  54 */
+	{ 26, 1,  60, 336, 10000/2,85/2, 240/2,   2,  25,   CT_PASSENGERS   ,  0,  0,  0,  0,  0 }, /*  55 */
+	{ 26, 0,  53, 320,    5000,  95,   230,   2,   0,   0               ,  0,  0,  0,  0,  0 }, /*  56 */
+	{ 60, 2, 247,   0,       0,  25,     0,   0,  45,   CT_PASSENGERS   ,  0,  0,  0,  0,  0 }, /*  57 */
+	{ 62, 2, 228,   0,       0,  21,     0,   0,  35,   CT_MAIL         ,  0,  0,  0,  0,  0 }, /*  58 */
+	{ 61, 2, 176,   0,       0,  18,     0,   0,  35,   CT_COAL         ,  0,  0,  0,  0,  0 }, /*  59 */
+	{ 63, 2, 200,   0,       0,  24,     0,   0,  35,   CT_OIL          ,  0,  0,  0,  0,  0 }, /*  60 */
+	{ 64, 2, 192,   0,       0,  20,     0,   0,  30,   CT_LIVESTOCK    ,  0,  0,  0,  0,  0 }, /*  61 */
+	{ 65, 2, 190,   0,       0,  21,     0,   0,  30,   CT_GOODS        ,  0,  0,  0,  0,  0 }, /*  62 */
+	{ 66, 2, 182,   0,       0,  19,     0,   0,  35,   CT_GRAIN        ,  0,  0,  0,  0,  0 }, /*  63 */
+	{ 67, 2, 181,   0,       0,  16,     0,   0,  35,   CT_WOOD         ,  0,  0,  0,  0,  0 }, /*  64 */
+	{ 68, 2, 179,   0,       0,  19,     0,   0,  35,   CT_IRON_ORE     ,  0,  0,  0,  0,  0 }, /*  65 */
+	{ 69, 2, 196,   0,       0,  18,     0,   0,  25,   CT_STEEL        ,  0,  0,  0,  0,  0 }, /*  66 */
+	{ 70, 2, 255,   0,       0,  30,     0,   0,  25,   CT_VALUABLES    ,  0,  0,  0,  0,  0 }, /*  67 */
+	{ 71, 2, 191,   0,       0,  22,     0,   0,  30,   CT_FOOD         ,  0,  0,  0,  0,  0 }, /*  68 */
+	{ 72, 2, 196,   0,       0,  18,     0,   0,  25,   CT_PAPER        ,  0,  0,  0,  0,  0 }, /*  69 */
+	{ 73, 2, 179,   0,       0,  19,     0,   0,  35,   CT_COPPER_ORE   ,  0,  0,  0,  0,  0 }, /*  70 */
+	{ 47, 2, 199,   0,       0,  25,     0,   0,  30,   CT_WATER        ,  0,  0,  0,  0,  0 }, /*  71 */
+	{ 48, 2, 182,   0,       0,  18,     0,   0,  30,   CT_FRUIT        ,  0,  0,  0,  0,  0 }, /*  72 */
+	{ 49, 2, 185,   0,       0,  19,     0,   0,  26,   CT_RUBBER       ,  0,  0,  0,  0,  0 }, /*  73 */
+	{ 50, 2, 176,   0,       0,  19,     0,   0,  35,   CT_SUGAR        ,  0,  0,  0,  0,  0 }, /*  74 */
+	{ 51, 2, 178,   0,       0,  20,     0,   0,  35,   CT_COTTON_CANDY ,  0,  0,  0,  0,  0 }, /*  75 */
+	{ 52, 2, 192,   0,       0,  20,     0,   0,  35,   CT_TOFFEE       ,  0,  0,  0,  0,  0 }, /*  76 */
+	{ 53, 2, 190,   0,       0,  21,     0,   0,  25,   CT_BUBBLES      ,  0,  0,  0,  0,  0 }, /*  77 */
+	{ 54, 2, 182,   0,       0,  24,     0,   0,  30,   CT_COLA         ,  0,  0,  0,  0,  0 }, /*  78 */
+	{ 55, 2, 181,   0,       0,  21,     0,   0,  30,   CT_CANDY        ,  0,  0,  0,  0,  0 }, /*  79 */
+	{ 56, 2, 183,   0,       0,  21,     0,   0,  25,   CT_TOYS         ,  0,  0,  0,  0,  0 }, /*  80 */
+	{ 57, 2, 196,   0,       0,  18,     0,   0,  27,   CT_BATTERIES    ,  0,  0,  0,  0,  0 }, /*  81 */
+	{ 58, 2, 193,   0,       0,  18,     0,   0,  30,   CT_FIZZY_DRINKS ,  0,  0,  0,  0,  0 }, /*  82 */
+	{ 59, 2, 191,   0,       0,  18,     0,   0,  35,   CT_PLASTIC      ,  0,  0,  0,  0,  0 }, /*  83 */
+	{ 28, 0,  70, 400,   10000, 105,   250,   2,   0,   0               ,  0,  0,  0,  0,  0 }, /*  84 */
+	{ 29, 0,  74, 448,   12000, 120,   253,   2,   0,   0               ,  0,  0,  0,  0,  0 }, /*  85 */
+	{ 30, 0,  82, 480,   15000, 130,   254,   2,   0,   0               ,  0,  0,  0,  0,  0 }, /*  86 */
+	{ 31, 1,  95, 640, 20000/2,150/2,255/2,   2,   0,   0               ,  0,  0,  0,  0,  0 }, /*  87 */
+	{ 28, 0,  70, 480,   10000, 120,   250,   2,   0,   0               ,  0,  0,  0,  0,  0 }, /*  88 */
+	{ 60, 2, 247,   0,       0,  25,     0,   0,  47,   CT_PASSENGERS   ,  0,  0,  0,  0,  0 }, /*  89 */
+	{ 62, 2, 228,   0,       0,  21,     0,   0,  37,   CT_MAIL         ,  0,  0,  0,  0,  0 }, /*  90 */
+	{ 61, 2, 176,   0,       0,  18,     0,   0,  37,   CT_COAL         ,  0,  0,  0,  0,  0 }, /*  91 */
+	{ 63, 2, 200,   0,       0,  24,     0,   0,  37,   CT_OIL          ,  0,  0,  0,  0,  0 }, /*  92 */
+	{ 64, 2, 192,   0,       0,  20,     0,   0,  32,   CT_LIVESTOCK    ,  0,  0,  0,  0,  0 }, /*  93 */
+	{ 65, 2, 190,   0,       0,  21,     0,   0,  32,   CT_GOODS        ,  0,  0,  0,  0,  0 }, /*  94 */
+	{ 66, 2, 182,   0,       0,  19,     0,   0,  37,   CT_GRAIN        ,  0,  0,  0,  0,  0 }, /*  95 */
+	{ 67, 2, 181,   0,       0,  16,     0,   0,  37,   CT_WOOD         ,  0,  0,  0,  0,  0 }, /*  96 */
+	{ 68, 2, 179,   0,       0,  19,     0,   0,  37,   CT_IRON_ORE     ,  0,  0,  0,  0,  0 }, /*  97 */
+	{ 69, 2, 196,   0,       0,  18,     0,   0,  27,   CT_STEEL        ,  0,  0,  0,  0,  0 }, /*  98 */
+	{ 70, 2, 255,   0,       0,  30,     0,   0,  27,   CT_VALUABLES    ,  0,  0,  0,  0,  0 }, /*  99 */
+	{ 71, 2, 191,   0,       0,  22,     0,   0,  32,   CT_FOOD         ,  0,  0,  0,  0,  0 }, /* 100 */
+	{ 72, 2, 196,   0,       0,  18,     0,   0,  27,   CT_PAPER        ,  0,  0,  0,  0,  0 }, /* 101 */
+	{ 73, 2, 179,   0,       0,  19,     0,   0,  37,   CT_COPPER_ORE   ,  0,  0,  0,  0,  0 }, /* 102 */
+	{ 47, 2, 199,   0,       0,  25,     0,   0,  32,   CT_WATER        ,  0,  0,  0,  0,  0 }, /* 103 */
+	{ 48, 2, 182,   0,       0,  18,     0,   0,  32,   CT_FRUIT        ,  0,  0,  0,  0,  0 }, /* 104 */
+	{ 49, 2, 185,   0,       0,  19,     0,   0,  28,   CT_RUBBER       ,  0,  0,  0,  0,  0 }, /* 105 */
+	{ 50, 2, 176,   0,       0,  19,     0,   0,  37,   CT_SUGAR        ,  0,  0,  0,  0,  0 }, /* 106 */
+	{ 51, 2, 178,   0,       0,  20,     0,   0,  37,   CT_COTTON_CANDY ,  0,  0,  0,  0,  0 }, /* 107 */
+	{ 52, 2, 192,   0,       0,  20,     0,   0,  37,   CT_TOFFEE       ,  0,  0,  0,  0,  0 }, /* 108 */
+	{ 53, 2, 190,   0,       0,  21,     0,   0,  27,   CT_BUBBLES      ,  0,  0,  0,  0,  0 }, /* 109 */
+	{ 54, 2, 182,   0,       0,  24,     0,   0,  32,   CT_COLA         ,  0,  0,  0,  0,  0 }, /* 110 */
+	{ 55, 2, 181,   0,       0,  21,     0,   0,  32,   CT_CANDY        ,  0,  0,  0,  0,  0 }, /* 111 */
+	{ 56, 2, 183,   0,       0,  21,     0,   0,  27,   CT_TOYS         ,  0,  0,  0,  0,  0 }, /* 112 */
+	{ 57, 2, 196,   0,       0,  18,     0,   0,  29,   CT_BATTERIES    ,  0,  0,  0,  0,  0 }, /* 113 */
+	{ 58, 2, 193,   0,       0,  18,     0,   0,  32,   CT_FIZZY_DRINKS ,  0,  0,  0,  0,  0 }, /* 114 */
+	{ 59, 2, 191,   0,       0,  18,     0,   0,  37,   CT_PLASTIC      ,  0,  0,  0,  0,  0 }, /* 115 */
 };
 
 ShipVehicleInfo _ship_vehicle_info[NUM_SHIP_ENGINES] = {
--- a/train_cmd.c
+++ b/train_cmd.c
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "openttd.h"
+#include "debug.h"
 #include "table/strings.h"
 #include "map.h"
 #include "tile.h"
@@ -22,6 +23,7 @@
 	(is_custom_sprite(spritenum) ? IS_CUSTOM_FIRSTHEAD_SPRITE(spritenum) : _engine_sprite_add[spritenum] == 0)
 
 static bool TrainCheckIfLineEnds(Vehicle *v);
+static void TrainController(Vehicle *v);
 extern void ShowTrainViewWindow(Vehicle *v);
 
 static const byte _vehicle_initial_x_fract[4] = {10,8,4,8};
@@ -81,6 +83,7 @@
 
 	for (u = v; u != NULL; u = u->next) {
 		const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
+		uint16 veh_len;
 
 		// update the 'first engine'
 		u->u.rail.first_engine = (v == u) ? INVALID_VEHICLE : first_engine;
@@ -109,6 +112,16 @@
 		// max speed is the minimum of the speed limits of all vehicles in the consist
 		if (rvi_u->max_speed != 0)
 			max_speed = min(rvi_u->max_speed, max_speed);
+
+		// check the vehicle length (callback)
+		veh_len = CALLBACK_FAILED;
+		if (HASBIT(rvi_u->callbackmask, CBM_VEH_LENGTH))
+			veh_len = GetCallBackResult(CBID_VEH_LENGTH,  u->engine_type, u);
+		if (veh_len == CALLBACK_FAILED)
+			veh_len = rvi_u->shorten_factor;
+		assert(veh_len < 8);
+		u->u.rail.cached_veh_length = 8 - veh_len;
+
 	};
 
 	// store consist weight/max speed in cache
@@ -1257,6 +1270,52 @@
 	}
 }
 
+/**
+ * Advances wagons for train reversing, needed for variable length wagons.
+ * Needs to be called once before the train is reversed, and once after it.
+ * @param v First vehicle in chain
+ * @param before Set to true for the call before reversing, false otherwise
+ */
+static void AdvanceWagons(Vehicle *v, bool before)
+{
+	Vehicle *base, *first, *last, *tempnext;
+	int i, length;
+	int differential;
+
+	base = v;
+	first = base->next;
+	length = CountVehiclesInChain(v);
+
+	while (length > 2) {
+		// find pairwise matching wagon
+		// start<>end, start+1<>end-1, ... */
+		last = first;
+		for (i = length - 3; i; i--) {
+			last = last->next;
+		}
+
+		differential = last->u.rail.cached_veh_length - base->u.rail.cached_veh_length;
+		if (before)
+			differential *= -1;
+
+		if (differential > 0) {
+			// disconnect last car to make sure only this subset moves
+			tempnext = last->next;
+			last->next = NULL;
+
+			for (i = 0; i < differential; i++) {
+				TrainController(first);
+			}
+
+			last->next = tempnext;
+		}
+
+		base = first;
+		first = first->next;
+		length -= 2;
+	}
+}
+
 static void ReverseTrainDirection(Vehicle *v)
 {
 	int l = 0, r = -1;
@@ -1285,11 +1344,15 @@
 	u = v;
 	do r++; while ( (u = u->next) != NULL );
 
+	AdvanceWagons(v, true);
+
 	/* swap start<>end, start+1<>end-1, ... */
 	do {
 		ReverseTrainSwapVeh(v, l++, r--);
 	} while (l <= r);
 
+	AdvanceWagons(v, false);
+
 	if (IsTileDepotType(v->tile, TRANSPORT_RAIL))
 		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
 
@@ -2723,8 +2786,6 @@
 			v->x_pos = gp.x;
 			v->y_pos = gp.y;
 			VehiclePositionChanged(v);
-			if (prev == NULL)
-				CheckTrainCollision(v);
 			continue;
 		}
 common:;
@@ -2743,7 +2804,6 @@
 		if (prev == NULL) {
 			/* This is the first vehicle in the train */
 			AffectSpeedByZChange(v, old_z);
-			CheckTrainCollision(v);
 		}
 	}
 	return;
@@ -3106,6 +3166,7 @@
 
 		do {
 			TrainController(v);
+			CheckTrainCollision(v);
 			if (v->cur_speed <= 0x100)
 				break;
 		} while (--j != 0);
--- a/train_gui.c
+++ b/train_gui.c
@@ -1,6 +1,7 @@
 #include "stdafx.h"
 #include "openttd.h"
 #include "debug.h"
+#include "table/sprites.h"
 #include "table/strings.h"
 #include "map.h"
 #include "window.h"
@@ -328,20 +329,27 @@
 
 static void DrawTrainImage(const Vehicle *v, int x, int y, int count, int skip, VehicleID selection)
 {
+	int max_x = x + count * 29;
+
 	do {
 		if (--skip < 0) {
 			int image = GetTrainImage(v, 6);
 			uint32 ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner));
-			if (v->vehstatus & VS_CRASHED) ormod = 0x3248000;
-			DrawSprite(image | ormod, x+14, y+6+_traininfo_vehicle_pitch);
-			if (v->index == selection) DrawFrameRect(x-1, y-1, x+28, y+12, 15, 0x10);
-			x += 29;
-			count--;
+			int width = v->u.rail.cached_veh_length * 3;
+
+			if (x + width + 4 <= max_x) {
+				if (v->vehstatus & VS_CRASHED)
+					ormod = PALETTE_CRASH;
+				DrawSprite(image | ormod, x + 14, y + 6 + _traininfo_vehicle_pitch);
+				if (v->index == selection)
+					DrawFrameRect(x - 1, y - 1, x + width + 4, y + 12, 15, 0x10);
+			}
+			x += width + 5;
 		}
 
 		if (!(v = v->next))
 			break;
-	} while (count);
+	} while (x < max_x);
 }
 
 static void DrawTrainDepotWindow(Window *w)
@@ -409,8 +417,10 @@
 			/*Draw the train counter */
 			i = 0;
 			u = v;
-			do i++; while ( (u=u->next) != NULL);		//Determine length of train
-			SetDParam(0, i);				//Set the counter
+			do {
+				i += u->u.rail.cached_veh_length + 1;
+			} while ( (u=u->next) != NULL);			//Determine length of train
+			SetDParam(0, (i+8) / 9);			//Set the counter
 			i = (w->hscroll.cap * 29) + (x + 26);		//Calculate position of text according to window size
 			DrawStringCentered(i, y+5, STR_TINY_BLACK, 0);	//Draw the counter
 
@@ -451,16 +461,11 @@
 
 static int GetVehicleFromTrainDepotWndPt(Window *w, int x, int y, GetDepotVehiclePtData *d)
 {
-	int area_x;
 	int row;
+	int skip = 0;
 	Vehicle *v;
 
 	x = x - 23;
-	if (x < 0) {
-		area_x = (x >= -10) ? -2 : -1;
-	} else {
-		area_x = x / 29;
-	}
 
 	row = (y - 14) / 14;
 	if ( (uint) row >= w->vscroll.cap)
@@ -475,12 +480,12 @@
 				v->tile == w->window_number &&
 				v->u.rail.track == 0x80 &&
 				--row < 0) {
-					if (area_x >= 0) area_x += w->hscroll.pos;
+					skip = w->hscroll.pos;
 					goto found_it;
 		}
 	}
 
-	area_x--; /* free wagons don't have an initial loco. */
+	x -= 29; /* free wagons don't have an initial loco. */
 
 	/* and then the list of free wagons */
 	FOR_ALL_VEHICLES(v) {
@@ -502,11 +507,14 @@
 	d->head = d->wagon = v;
 
 	/* either pressed the flag or the number, but only when it's a loco */
-	if (area_x < 0 && v->subtype == TS_Front_Engine)
-		return area_x;
+	if (x < 0 && v->subtype == TS_Front_Engine)
+		return (x >= -10) ? -2 : -1;
+
+	// skip vehicles that are scrolled off the left side
+	while (skip--) v = v->next;
 
 	/* find the vehicle in this row that was clicked */
-	while (--area_x >= 0) {
+	while ((x -= v->u.rail.cached_veh_length * 3 + 5) >= 0) {
 		v = v->next;
 		if (v == NULL) break;
 	}
--- a/vehicle.c
+++ b/vehicle.c
@@ -528,7 +528,7 @@
 	if (v->vehstatus & VS_DISASTER) {
 		image |= 0x3224000;
 	} else if (v->vehstatus & VS_DEFPAL) {
-		image |= (v->vehstatus & VS_CRASHED) ? 0x3248000 : SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner));
+		image |= (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner));
 	}
 
 	AddSortableSpriteToDraw(image, v->x_pos + v->x_offs, v->y_pos + v->y_offs,
--- a/vehicle.h
+++ b/vehicle.h
@@ -53,6 +53,7 @@
 	// cached values, recalculated on load and each time a vehicle is added to/removed from the consist.
 	uint16 cached_max_speed;  // max speed of the consist. (minimum of the max speed of all vehicles in the consist)
 	uint32 cached_power;      // total power of the consist.
+	uint8 cached_veh_length;  // length of this vehicle in units of 1/8 of normal length, cached because this can be set by a callback
 	// cached values, recalculated when the cargo on a train changes (in addition to the conditions above)
 	uint16 cached_weight;     // total weight of the consist.
 	uint16 cached_veh_weight; // weight of the vehicle.